-
-
Notifications
You must be signed in to change notification settings - Fork 53
Disassembler
Disassembler receives a stream of JavaScript objects and produces a token stream.
const {chain} = require('stream-chain');
const {disassembler} = require('stream-json/disassembler.js');
const {pick} = require('stream-json/filters/pick.js');
const {ignore} = require('stream-json/filters/ignore.js');
const {streamValues} = require('stream-json/streamers/stream-values.js');
const pipeline = chain([readObjectFromDatabase(), disassembler(), pick({filter: 'content'}), ignore({filter: /\b_/}), streamValues()]);In this example, we read objects from a database, convert them to a stream of tokens, pick only values of top-level property content, remove all properties that start with _ (underscore), and assemble them back into a stream of objects.
(Since 1.5.0) Disassembler supports logic defined by JSON.stringify(): skipping non-JSON properties or replacing them with null when appropriate, supporting toJSON() method, and replacer.
disassembler() returns a function for use in a chain() pipeline. disassembler.asStream() wraps it as a Duplex stream. It operates in object mode and is modeled on Parser.
Like Parser, it supports all packing and streaming options described in Parser.
Additionally, it supports the following options:
-
(since 1.5.0)
replacercan be one of two optional objects:- It can be a function, which takes two parameters and returns a value.
- It can be an array of strings or numbers to define a whitelist for object properties.
- See JSON.stringify() for more details.
Wraps the disassembler as a Duplex stream:
const {disassembler} = require('stream-json/disassembler.js');
const {Readable} = require('stream');
const {chain} = require('stream-chain');
const pipeline = chain([
new Readable({
objectMode: true,
read(size) {
database.readNext().then(data => this.push(data));
}
}),
disassembler()
]);disassembler ships in two substrate-specific entries with the same factory shape:
-
Node —
stream-json/disassembler.js. HasasStream(Node Duplex) andasWebStream(Web{readable, writable}pair). -
Web —
stream-json/web/disassembler.js. HasasWebStreamonly. Pulls in no Node-stream imports.
Both factories return the same generator pipeline, so chain on either substrate auto-wraps it. Use chain from stream-chain on Node and from stream-chain/web on Web.
// Web
import {chain} from 'stream-chain/web';
import {disassembler} from 'stream-json/web/disassembler.js';
import {stringer} from 'stream-json/web/stringer.js';
const pipeline = chain([objectSource, disassembler(), stringer()]);
for await (const chunk of pipeline.readable) console.log(chunk);Disassembler cannot accept top-level null due to restrictions of Node's stream system.
Start here
Core
Filters
Streamers
Essentials
Utilities
File I/O (Node-only)
JSONC
JSONL (use stream-chain)
Reference
Built on stream-chain