How to use the obj function from through2

Find comprehensive JavaScript through2.obj code examples handpicked from public code repositorys.

through2.obj is a method in the through2 module of Node.js that creates a stream which passes data through a function that operates on the data, returning a new stream of transformed data.

23
24
25
26
27
28
29
30
31
32
exports.stringify = jsonStream.stringify.bind(jsonStream);
exports.stringify.obj = jsonStream.stringifyObject = function (options) {
  
  options = options || {};
  
  return through.obj(function (chunk, enc, done) {
    
    done(null, JSON.stringify(chunk, null, options.space));
  });
}
fork icon98
star icon166
watch icon56

+ 9 other calls in file

38
39
40
41
42
43
44
45
46
47
48
exports.parse = jsonStream.parse.bind(jsonStream);
exports.stringify = jsonStream.stringify.bind(jsonStream);
exports.stringify.obj = jsonStream.stringifyObject = (options) => {
  options = options || {};


  return through.obj((chunk, enc, done) => {
    done(null, JSON.stringify(chunk, null, options.space));
  });
};
exports.validate = isStream;
fork icon59
star icon52
watch icon49

+ 9 other calls in file

How does through2.obj work?

through2.obj is a function in the through2 library that creates a stream transform object, which means it can be used as a readable and writable stream that transforms data in-between, while also being an object with methods for controlling the stream flow. The .obj version creates a stream that expects and returns objects, rather than just raw data.

55
56
57
58
59
60
61
62
63
64
fitGTFS: function (osmExtract, src, dst) {
  return run('gtfs_shape_mapfit/fit_gtfs.bash', osmExtract, src, dst)
},

fitGTFSTask: (configs) => {
  return through.obj(function (file, encoding, callback) {
    const gtfsFile = file.history[file.history.length - 1]
    const fileName = gtfsFile.split('/').pop()
    const relativeFilename = path.relative(process.cwd(), gtfsFile)
    const id = fileName.substring(0, fileName.indexOf('-gtfs'))
fork icon21
star icon11
watch icon18

+ 3 other calls in file

59
60
61
62
63
64
65
66
67
68
69
70


/**
 * Make router data ready for inclusion in data container.
 */
module.exports = function (configs) {
  const stream = through.obj()


  configs.forEach(config => {
    stream.push(createFile(config, 'build-config.json', `${routerDir(config)}/build-config.json`))
    stream.push(createFile(config, 'otp-config.json', `${routerDir(config)}/otp-config.json`))
fork icon21
star icon11
watch icon18

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const through = require("through2");

const stream = through.obj(function (obj, enc, next) {
  // transform the object in some way
  obj.prop = obj.prop.toUpperCase();

  // push the transformed object downstream
  this.push(obj);

  // call the callback to signal that we're done processing this object
  next();
});

// write some objects to the stream
stream.write({ prop: "foo" });
stream.write({ prop: "bar" });

// signal that we're done writing to the stream
stream.end();

// consume the transformed objects from the stream
stream.on("data", function (obj) {
  console.log(obj); // { prop: 'FOO' }, then { prop: 'BAR' }
});

6
7
8
9
10
11
12
13
14
15
16
17
 * Download external data (gtfs, osm) resources.
 */
module.exports = function (entries) {
  let downloadCount = 0


  var stream = through.obj()


  const incProcessed = () => {
    downloadCount += 1
    if (downloadCount !== entries.length) {
fork icon21
star icon11
watch icon18

+ 3 other calls in file

14
15
16
17
18
19
20
21
22
23
24
25


function plugin(options = {}){
	var selector = options.selector || 'img[src]';
	var attribute = options.attribute || 'src';


	return through.obj(function(file, encoding, callback){
		if(file.isStream()){
			this.emit('error', new util.PluginError(PLUGIN_NAME, 'Streams are not supported!'));
			return callback();
		}
fork icon7
star icon3
watch icon0

7
8
9
10
11
12
13
14
15
16
17
18
let es = require('event-stream');
let through2 = require('through2');


// simple callback stream used to synchronize stuff
function synchro(done) {
    return through2.obj(function (data, enc, cb) { cb(); },
    function (cb) { cb(); done(); });
}


var igConfig = require('./gulp-config.js')
fork icon5
star icon7
watch icon35

+ 3 other calls in file

465
466
467
468
469
470
471
472
473
474
};
const { events } = buildConfiguration;
events.on('configurePipeline', ({ pipeline }) => {
  pipeline.get('minify').push(
    // this is the "gulp-terser-js" wrapper around the latest version of terser
    through.obj(
      callbackify(async (file, _enc) => {
        const input = {
          [file.sourceMap.file]: file.contents.toString(),
        };
fork icon1
star icon1
watch icon1

+ 3 other calls in file

18
19
20
21
22
23
24
25
26
27
 *
 * @returns {Stream} caches files that match INLINE_EXTS
 */
exports.create = function (api) {
  var cache = {};
  var stream = through2.obj(undefined, function (file, enc, cb) {
    var ext = file.extname;
    if (!INLINE_EXTS[ext] || file.inline === false) {
      // do not inline
      this.push(file);
fork icon0
star icon0
watch icon2

+ 2 other calls in file

112
113
114
115
116
117
118
119
120
121
122
exports.File = ResourceFile;


exports.getDirectories = require('./directories').get;
exports.getMetadata = require('./metadata').get;
exports.createFileStream = function (api, app, config, outputDirectory, directories) {
  var stream = through2.obj(undefined);
  var statCache = {};
  Promise.resolve(directories || exports.getDirectories(api, app, config))
    .map(function (directory) {
      var files = directory.files && Promise.resolve(directory.files)
fork icon0
star icon0
watch icon2

+ 2 other calls in file

25
26
27
28
29
30
31
32
33
34
35
 *                          later in the build pipe
 */
module.exports = function createFileStream(api, app, config, opts) {


  var blockingEnd = [];
  var stream = through2.obj(undefined,
        opts.onFile && onFile,
        onFinish);
  return stream;

fork icon0
star icon0
watch icon2

+ 2 other calls in file

145
146
147
148
149
150
151
152
153
    }


    callback();
  }


  return through.obj(minify);
};
fork icon0
star icon0
watch icon2

+ 2 other calls in file

1
2
3
4
5
6
7
8
9
10
11
var through = require('through2')
var ndjson = require('ndjson')
var each = require('./')


tape('each', function (t) {
  var s = through.obj()
  s.write('a')
  s.write('b')
  s.write('c')
  s.end()
fork icon0
star icon0
watch icon1

+ 13 other calls in file

108
109
110
111
112
113
114
115
116
117
118
  LIB: false,
  IMAGE_DECODERS: false,
});


function transform(charEncoding, transformFunction) {
  return through.obj(function (vinylFile, enc, done) {
    const transformedFile = vinylFile.clone();
    transformedFile.contents = Buffer.from(
      transformFunction(transformedFile.contents),
      charEncoding
fork icon0
star icon0
watch icon0

+ 3 other calls in file

48
49
50
51
52
53
54
55
56
57
58
59


  return escodegen.generate(rootProgram, {comment: true});
}


module.exports = function() {
  return through.obj(function(file, enc, cb) {
    if (file.isBuffer()) {
      file.contents = new Buffer(uniffe(file.contents.toString(enc)), enc);
    }

fork icon0
star icon0
watch icon2

198
199
200
201
202
203
204
205
206
207
    buffer = lines[lines.length - 1];
    callback();
  })
)
.pipe(
  through2.obj(function(data, enc, callback) {
    // This is JSONL, so each line is a separate JSON object
    const obj = JSON.parse(data);
    context.symbolicateAttribution(obj);
    this.push(JSON.stringify(obj) + "\n");
fork icon0
star icon0
watch icon1

+ 4 other calls in file

351
352
353
354
355
356
357
358
359
360
  done()
}

_outStream () {
  const self = this
  return Through.obj(transform)

  function transform (message, _, callback) {
    message.from = self.id.toString()
    this.push(message)
fork icon0
star icon0
watch icon1

+ 9 other calls in file

90
91
92
93
94
95
96
97
98
99
100
101
  };
  const flush = function(cb) {
    cb();
  };


  return through.obj(transform, flush);
}


const addFrontmatterLine = (name, value, file) => {
  const chunk = String(file.contents);
fork icon0
star icon0
watch icon1

29
30
31
32
33
34
35
36
37
38
	(Array.isArray(reporter) ? reporter : [reporter]).forEach(function (el) {
		miniJasmineLib.addReporter(el);
	});
}

return through.obj(function (file, enc, cb) {
	if (file.isNull()) {
		cb(null, file);
		return;
	}
fork icon0
star icon0
watch icon1