Example #1
0
function make_minified(files, out, cb) {
	var buf = new streamBuffers.WritableStreamBuffer();
	buf.once('error', cb);
	make_client(files, buf, function (err) {
		if (err)
			return cb(err);
		var src = buf.getContentsAsString('utf-8');
		if (!src || !src.length)
			return cb('make_minified: no client JS was generated');
		minify(src);
	});

	function minify(src) {
		var UglifyJS = require('uglify-es');
		var ugly;
		try {
			ugly = UglifyJS.minify(src, {
				mangle: false,
			});
		}
		catch (e) {
			return cb(e);
		}
		out.write(ugly.code, cb);
	}
};
    ps.pull('Hello'.length, function (err, data) {
      if (err) {
        return t.done(err);
      }
      t.equal('Hello', data.toString());
      t.equal(0, data.posInStream);

      var writableStream = new streamBuffers.WritableStreamBuffer({
        initialSize: 100
      });
      writableStream.on('close', function () {
        var str = writableStream.getContentsAsString('utf8');
        t.equal(' World', str);

        ps.pull(function (err, data) {
          if (err) {
            return t.done(err);
          }
          t.equal('!', data.toString());
          t.equal('Hello World'.length, data.posInStream);
        });
      });

      ps.pipe(' World'.length, writableStream);
    });
test("pipe until pattern", function (t) {
  t.plan(3);
  var us = new UntilStream({ pattern: 'jumps'});
  us.on('finish', function () {
    sourceStream.destroy();
  });

  var sourceStream = new streamBuffers.ReadableStreamBuffer();
  sourceStream.put("The quick brown fox jumps over the lazy dog");

  var writableStream = new streamBuffers.WritableStreamBuffer();

  writableStream.on('close', function () {
    var str = writableStream.getContentsAsString('utf8');
    t.equal(str, 'The quick brown fox ');
    var data = us.read();
    t.equal(data.toString(), 'jumps');
    writableStream = new streamBuffers.WritableStreamBuffer();
    us.pipe(writableStream);
    writableStream.on('close', function () {
      var str = writableStream.getContentsAsString('utf8');
      t.equal(str, ' over the lazy dog');
      t.end();
    });
  });

  sourceStream.pipe(us).pipe(writableStream);
});
 it('should log when given a logger', function (done) {
   var streamBuffers = require('stream-buffers')
   var myStream = new streamBuffers.WritableStreamBuffer({})
   var log = require('bunyan').createLogger({
     name: 'test-log',
     stream: myStream
   })
   assert.equal(myStream.size(), 0)
   searchindex({
     indexPath: sandboxPath + '/test-log-index',
     log: log,
     logLevel: 'info'
   }, function(err, si) {
     should.exist(si.log)
     si.add({
       id: 1,
       name: 'The First Doc',
       test: 'this is the first doc'
     }, function (err) {
       should(err).be.undefined
       myStream.size().should.be.above(0)
       done()
     })
   })
 })
Example #5
0
    "pipe with no length": function (t) {
        t.expect(2);
        var ps = new PullStream({lowWaterMark: 0});
        ps.on('end', function () {
            t.ok(true, "pullstream should end");
        });

        var writableStream = new streamBuffers.WritableStreamBuffer({
            initialSize: 100
        });
        writableStream.on('close', function () {
            var str = writableStream.getContentsAsString('utf8');
            t.equal('Hello World!', str);
            t.done();
        });

        ps.pipe(writableStream);

        process.nextTick(function () {
            ps.write(new Buffer('Hello', 'utf8'));
            ps.write(new Buffer(' World', 'utf8'));
            process.nextTick(function () {
                ps.write(new Buffer('!', 'utf8'));
                ps.end();
            });
        });
    },
        res.end = function(data) {
            if(data) {
                buffer.write(data);
            }

            if (!buffer.size()) {
                return oldEnd.call(this, buffer.getContents());
            }

            var body = buffer.getContentsAsString();
            var includes = body.match(/<!-- #include file=\".+\" -->/g);
            if (!includes) {
                return oldEnd.call(this, body);
            }


            var remaining = includes.length;

            _.each(includes, function(include) {
                var file = path.join(rootDir, include.match(/<!-- #include file=\"(.+)\" -->/)[1]);

                fs.readFile(file, 'utf8', function(err, data) {
                    if(err) {
                        console.log("ERROR including file " + file + ": " + err);
                    }
                    else {
                        body = body.replace(include, data);
                    }

                    if (!--remaining) {
                        oldEnd.call(this, body);
                    }
                });
            });
        }
Example #7
0
function generateCSV(t, fn) {
  var ostream = new WritableStreamBuffer();
  csv(ostream, t);
  ostream
  .on('error', fn)
  .on('finish', function() {
    fn(null, ostream.getContentsAsString('utf8'));
  });
}
 writableStream.on('close', function () {
   var str = writableStream.getContentsAsString('utf8');
   t.equal(str, 'The quick brown fox ');
   var data = us.read();
   t.equal(data.toString(), 'jumps');
   writableStream = new streamBuffers.WritableStreamBuffer();
   us.pipe(writableStream);
   writableStream.on('close', function () {
     var str = writableStream.getContentsAsString('utf8');
     t.equal(str, ' over the lazy dog');
     t.end();
   });
 });
Example #9
0
 .on('entry', function(entry) {
   if (entry.path === 'file.txt') {
     var writableStream = new streamBuffers.WritableStreamBuffer();
     writableStream.on('close', function () {
       var str = writableStream.getContentsAsString('utf8');
       var fileStr = fs.readFileSync(path.join(__dirname, '../testData/compressed-standard/inflated/file.txt'), 'utf8')
       t.equal(str, fileStr);
       t.end();
     });
     entry.pipe(writableStream);
   } else {
     entry.autodrain();
   }
 });
      grunt.util.async.forEachSeries(filePair.src, function(src, nextFile) {
        // Must be a file
        if (grunt.file.isDir(src)) {
          return nextFile();
        }

        // Ensure the dest folder exists
        grunt.file.mkdir(path.dirname(filePair.dest));

        var srcStream = fs.createReadStream(src);
        var originalSize = exports.getSize(src);

        var destStream;
        function init_destStream() {
          destStream = fs.createWriteStream(filePair.dest);

          destStream.on('close', function() {
            var compressedSize = exports.getSize(filePair.dest);
            var ratio = Math.round(parseInt(compressedSize) / parseInt(originalSize) * 100) + '%';

            grunt.verbose.writeln('Created ' + chalk.cyan(filePair.dest) + ' (' + compressedSize + ') - ' + chalk.cyan(ratio) + ' of the original size');
            nextFile();
          });
        }

        // write to memory stream if source and destination are the same
        var tmpStream;
        if (src === filePair.dest) {
          tmpStream = new streamBuffers.WritableStreamBuffer();
          tmpStream.on('close', function() {
            init_destStream();

            destStream.write(this.getContents());
            destStream.end();
          });
        } else {
          init_destStream();
        }

        var compressor = algorithm.call(zlib, exports.options);

        compressor.on('error', function(err) {
          grunt.log.error(err);
          grunt.fail.warn(algorithm + ' failed.');
          nextFile();
        });

        srcStream.pipe(compressor).pipe(tmpStream || destStream);
      }, nextPair);
        res.end = function(data) {
			var context = this;
		
            if(data) {
                buffer.write(data);
            }

            if (!buffer.size()) {
                return oldEnd.call(context, buffer.getContents());
            }

            var body = buffer.getContentsAsString();
            var includes = body.match(/<!--\s?#include (virtual|file)=\".+\" -->/g);
            if (!includes) {
                return oldEnd.call(context, body);
            }

			var remaining = includes.length;
			
            _.each(includes, function(include) {
			
				var pathDirName = path.dirname(req.url);

				if(include.match(/<!--\s?#include (virtual|file)=\"(.+)\" -->/)[1] === 'virtual'){
					pathDirName = '';
				}
				
                var fileName = include.match(/<!--\s?#include (virtual|file)=\"(.+)\" -->/)[2];
                var file = path.join(rootDir, pathDirName, fileName);

                fs.readFile(file, 'utf8', function(err, data) {
                    if(err) {
                        console.log("ERROR including file " + file + ": " + err);
                    }
                    else {
                        if (options.debug) {
                            data = createHtmlComment( path.join(pathDirName, fileName) ) + data;
                        }

                        body = body.replace(include, data);
                    }
					
                    if (!--remaining) {
                        oldEnd.call(context, body);
                    }
                });
            });

        }
Example #12
0
const capture = async (actions, options, args) => {

    const buffer = new streamBuffers.WritableStreamBuffer();

    const c = cli(actions, {
        ...options,
        outputStream: buffer,
    });

    await c.exec(args);

    const contents = buffer.getContentsAsString('utf8');

    return contents === false ? '' : contents;
};
Example #13
0
test("pipe until pattern", function (t) {
    t.plan(2);

    var ms = new MatchStream({pattern: 'World'}, function (buf, matched, extra) {
        if (!matched) {
            return this.push(buf);
        }
        this.push(buf);
        t.equal(extra.toString(), 'World');
        return this.push(null); //end the stream
    });

    var sourceStream = new streamBuffers.ReadableStreamBuffer();
    sourceStream.put("Hello World");
    var writableStream = new streamBuffers.WritableStreamBuffer();

    sourceStream
    .pipe(ms)
    .pipe(writableStream)
    .once('close', function () {
        var str = writableStream.getContentsAsString('utf8');
        t.equal(str, 'Hello ');
        sourceStream.destroy();
        t.end();
    });
});
var createRecords = function(constructor, num) {
	var testRecord = constructor();
	num = num || 1000;

	// Make a write buffer big enough to fit everything.
	var writeStream = new streamBuffers.WritableStreamBuffer({
	});
	
	var fastcgiStream = new fastcgi.FastCGIStream(writeStream);
	
	var start = Date.now();
	var running = true;
	var i = 1;

	setTimeout(function() {
		running = false;
		console.log("Took " + (Date.now() - start) + "ms to write " + i + " records");
		console.log("Buffer has " + writeStream.size() + " bytes.");
	}, 1000);
	
	var doWrite = function() {
		fastcgiStream.writeRecord(i++, constructor());
		if(running) process.nextTick(doWrite);
	};
	
	process.nextTick(doWrite);
};
 it('downloads a file', () => {
   const w = new streamBuffers.WritableStreamBuffer();
   return download('https://api.github.com/files/file-darwin-amd64.txt', w)
     .then(() => {
       w.getContentsAsString('utf8').should.be.exactly(fileTxt);
     });
 });
 writableStream.on('close', function () {
   var str = writableStream.getContentsAsString('utf8');
   t.equal(str, 'Hello Wordy ');
   var data = us.read();
   t.equal(data.toString(), 'World');
   t.end();
 });
 contentBuffer.on('close', function () {
   if (contentBuffer.statusCode === 404) {
     resolve(null);
   } else {
     resolve(contentBuffer.getContents().toString());
   }
 });
Example #18
0
test("pipe a fixed length number of bytes, then end the stream", function (t) {
    t.plan(2);

    var ss = new SliceStream({length: 5}, function (buf, sliceEnd, extra) {
        if (!sliceEnd) {
            return this.push(buf);
        }
        this.push(buf);
        t.equal(extra.toString(), ' World');
        return this.push(null);
    });

    var sourceStream = new streamBuffers.ReadableStreamBuffer();
    sourceStream.put("Hello World");
    var writableStream = new streamBuffers.WritableStreamBuffer();

    sourceStream
    .pipe(ss)
    .pipe(writableStream)
    .once('close', function () {
        var str = writableStream.getContentsAsString('utf8');
        t.equal(str, 'Hello');
        sourceStream.destroy();
        t.end();
    });
});
Example #19
0
 .then(success => {
   const buffer = bufferStream.getContents();
   const workbook = XLSX.read(bufferToString(buffer), { type: 'binary' });
   const firstSheetName = workbook.SheetNames[0];
   const worksheet = workbook.Sheets[firstSheetName];
   const sheetValues = XLSX.utils.sheet_to_json(worksheet);
   console.log(JSON.stringify(sheetValues, null, 2));
 })
			here({}, out, function() {
				try {
					assert.equal(out.getContentsAsString(), "now I'm here now I'm there")
					done()
				} catch(ex) {
					done(ex)
				}
			})
			first({abc: {num1: 1, num2: 3}, num2: 2}, out, function() {
				try {
					assert.equal(out.getContentsAsString(), "1 3 2 2")
					done()
				} catch(ex) {
					done(ex)
				}
			})
			hello({firstName: 'Dan'}, out, function() {
				try {
					assert.equal(out.getContentsAsString(), 'hello,  ! world there')
					done()
				} catch(ex) {
					done(ex)
				}
			})
    return function(req, res, next) {
        var Buffer = require('buffer').Buffer;
        var WritableStream = require("stream-buffers").WritableStreamBuffer;
        var fs = require('fs');
        var path = require('path');
        var _ = require('underscore');

        if(req.url != "/" && !req.url.match(/\.html$/))
            return next();

        var buffer = new WritableStream();

        var oldWrite = res.write;
        res.write = function(chunk) {
            buffer.write(chunk);
            return true;
        };

        var oldEnd = res.end;
        res.end = function(data) {
            if(data) {
                buffer.write(data);
            }

            if (!buffer.size()) {
                return oldEnd.call(this, buffer.getContents());
            }

            var body = buffer.getContentsAsString();
            var includes = body.match(/<!-- #include file=\".+\" -->/g);
            if (!includes) {
                return oldEnd.call(this, body);
            }


            var remaining = includes.length;

            _.each(includes, function(include) {
                var file = path.join(rootDir, include.match(/<!-- #include file=\"(.+)\" -->/)[1]);

                fs.readFile(file, 'utf8', function(err, data) {
                    if(err) {
                        console.log("ERROR including file " + file + ": " + err);
                    }
                    else {
                        body = body.replace(include, data);
                    }

                    if (!--remaining) {
                        oldEnd.call(this, body);
                    }
                });
            });
        }

        next();
    }
Example #24
0
 var r = request(url, function (error, response, buffer) {
   if (!error && response.statusCode == 200) {
       var data = myWritableStreamBuffer.getContents().toString('base64');
       var result = {'audio' : data, 'success' : true };
       callback(result);
    } else {
      var result = {'success' : false, 'error' : error, 'responseCode' : response.statusCode };
      callback(result);
    }
 }).pipe(myWritableStreamBuffer);
      writableStream.on('close', function () {
        var str = writableStream.getContentsAsString('utf8');
        t.equal(' World', str);

        ps.pull(function (err, data) {
          if (err) {
            return t.done(err);
          }
          t.equal('!', data.toString());
        });
      });
test("if pattern option unset, behave as a PassThrough stream", function(t) {
  t.plan(1);
  var us = new UntilStream();
  us.on('finish', function () {
    sourceStream.destroy();
  });

  var sourceStream = new streamBuffers.ReadableStreamBuffer();
  sourceStream.put("Hello World!");

  var writableStream = new streamBuffers.WritableStreamBuffer();

  writableStream.on('close', function () {
    var str = writableStream.getContentsAsString('utf8');
    t.equal(str, 'Hello World!');
    t.end();
  });

  sourceStream.pipe(us).pipe(writableStream);
});
Example #27
0
  it('GET', function (done) {
    var sb = new sbuffers.WritableStreamBuffer();

    request
      .get(router.linkTo('core.gridfs', { bucket: info._id }))
      .on('end', function () {
        assert.equal(sb.getContents().toString('hex'), fileContent.toString('hex'));
        done();
      })
      .pipe(sb);
  });
test("first chunk ends with potential pattern", function (t) {
  t.plan(2);
  var us = new UntilStream({ pattern: 'World'});
  us.on('finish', function () {
    sourceStream.destroy();
  });

  var sourceStream = new streamBuffers.ReadableStreamBuffer();
  sourceStream.put("Hello Wordy World");

  var writableStream = new streamBuffers.WritableStreamBuffer();

  writableStream.on('close', function () {
    var str = writableStream.getContentsAsString('utf8');
    t.equal(str, 'Hello Wordy ');
    var data = us.read();
    t.equal(data.toString(), 'World');
    t.end();
  });

  sourceStream.pipe(us).pipe(writableStream, { chunkSize: 9 });
});
UstadMobileAppImplNodeWebkit.prototype.unzipSingleFile = function(zipPath, zipEntryName, baseFolder, callback) {
    var fs = require('fs');
    var fse = require("fs-extra");
    var path = require("path");
    var unzip = require("unzip");
    var streamBuffers = require("stream-buffers");
    
    var streamWriter = new streamBuffers.WritableStreamBuffer();
    var destFilePath = path.join(baseFolder, zipEntryName);
    var destDir = path.dirname(destFilePath);
    
    var fileFound = false;
    
    if(!fs.existsSync(destDir)) {
        fse.mkdirsSync(destDir);
    }
        
    streamWriter.on("close", function() {
        //save that to disk
        var fileContents = streamWriter.getContentsAsString('utf8');
        fs.writeFileSync(destFilePath, fileContents);
        callback(null, fileContents);
    });
    
    fs.createReadStream(zipPath)
        .pipe(unzip.Parse())
        .on('entry', function (entry) {
            if(entry.path === zipEntryName && entry.type === "File") {
                fileFound = true;
                entry.pipe(streamWriter);
            }else {
                entry.autodrain();
            }
        }).on("close", function() {
            if(!fileFound) {
                callback("NOTFOUND", null);
            }
        });
};
Example #30
0
    file.put(fileName, { metadata: { origName: fileBase } }, function (err, info) {
      if (err) { done(err); return; }

      // Read & compare data
      var sb = new sbuffers.WritableStreamBuffer();

      file.getStream(info._id)
        .on('end', function () {
          assert.equal(sb.getContents().toString('hex'), fileContent.toString('hex'));
          file.remove(info._id, done);
        })
        .pipe(sb);
    });