Skip to content
Eugene Lazutkin edited this page Dec 27, 2022 · 7 revisions

Disassembler receives a stream of JavaScript objects and produces a token stream.

Introduction

const {chain} = require('stream-chain');

const {disassembler} = require('stream-json/Disassembler');
const {pick} = require('stream-json/filters/Pick');
const {ignore} = require('stream-json/filters/Ignore');
const {streamValues} = require('stream-json/streamers/StreamValues');

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.

API

Disassembler is a Transform stream, which operates in object mode. It doesn't have any special public properties nor methods. In general, it was modeled on Parser.

constructor(options)

As Parser it supports all packing and streaming options described in Parser's constructor options.

Additionally, it supports the following options:

  • (since 1.5.0) replacer can 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.

Static methods and properties

disassembler(options) and make(options)

make() and disassembler() are two aliases of the factory function. It takes options described above, and return a new instance of Disassembler. disassembler() helps to reduce a boilerplate when creating data processing pipelines:

const {disassembler} = require('stream-json/Disassembler');

const {Readable} = require('stream');
const {chain} = require('stream-chain');

const pipeline = chain([
  new Readable({
    objectMode: true,
    read(size) {
      // let readNext() to return an object or null when we are done
      database.readNext().then(data => this.push(data));
    }
  }),
  disassembler()
  // editing streams
]);

make.Constructor

Constructor property of make() (and disassembler()) is set to Disassembler. It can be used for indirect creating of disassemblers or metaprogramming if needed.

Notes

Disassembler cannot accept top-level null due to restrictions of Node's stream system.

Clone this wiki locally