-
-
Notifications
You must be signed in to change notification settings - Fork 53
emit()
Eugene Lazutkin edited this page Mar 16, 2026
·
5 revisions
emit() helps to deal with different types of data items produced by Parser and filters. It takes a stream instance, adds a data listener, then emits events on the same stream instance, which correspond to data types: startObject, endNumber, nullValue, and so on.
const {parser} = require('stream-json/parser.js');
const emit = require('stream-json/utils/emit.js');
const fs = require('fs');
const pipeline = fs.createReadStream('sample.json').pipe(parser.asStream());
emit(pipeline);
let objectCounter = 0;
pipeline.on('startObject', () => ++objectCounter);
pipeline.on('end', console.log(`Found ${objectCounter} objects.`));emit() is a function with one argument, which should be a stream. Usually, it is the last stream of the pipeline: a parser, or a filter. Its returned value is the same stream for possible chaining.
The whole implementation of emit() is very simple:
const emit = stream => {
stream.on('data', item => stream.emit(item.name, item.value));
return stream;
};Start here
Core
Filters
Streamers
Essentials
Utilities
File I/O (Node-only)
JSONC
JSONL (use stream-chain)
Reference
Built on stream-chain