npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@avreg/yaml-doc-splitter

v0.1.0

Published

A Node.js transform stream to split multi-doc YAML stream

Downloads

5

Readme

YAML multi-doc stream splitter

The "zero dependency" package @avreg/yaml-doc-splitter exports YamlDocSplitter class as Node.js Transform stream to build parser|decoder|convert pipeline.

The YamlDocSplitter reads an multi-document YAML input stream, sequentially splits it into many individual YAML documents as data arrives, and pushes it on to the next streams in the pipeline.

Example of multi-document YAML:

# Implicit end with version
%YAML 1.2
---
doc: 1

# Explicit end without version
---
doc: 2
...

# Explicit end with another version
%YAML 1.1
---
doc: 3
...

Yes, js-yaml (and many another) package have loadAll() method, but YamlDocSplitter can useful in the following cases:

  • if input static file have big size,
  • realtime input stream.

Restrictions

  • Node.js >= 12.
  • Only UTF-8 encoding supports now.

Installation

npm install @avreg/yaml-doc-splitter
# or
yarn add @avreg/yaml-doc-splitter

Usage example

CommonJS (simplest example)

/***
 * Usage:
 *  $ node path/to/yaml-splitter.cjs multi-doc.yaml
 *  # or
 *  $ cat multi-doc.yaml | node path/to/yaml-splitter.cjs
 */

const fs = require('fs');
const stream = require('stream');
const { YamlDocSplitter } = require('@avreg/yaml-doc-splitter');

class DocDumper extends stream.Writable {
   constructor() {
      super();

      this._docNbr = 0;
   }

   _write(yamlDocBuffer, _encoding, callback) {
      const yamlDocString = yamlDocBuffer.toString();

      this._docNbr += 1;

      console.log();
      console.log(`@@@@@@@ Yaml doc #${this._docNbr} @@@@@@@`);
      console.log(yamlDocString);

      callback();
   }

   _final(callback) {
      console.log(`Total Yaml docs count: ${this._docNbr}`);

      callback();
   }
}

const inputStream = process.argv[2]
   ? fs.createReadStream(process.argv[2])
   : process.stdio;
const splitter = new YamlDocSplitter();
const dumper = new DocDumper();

inputStream.pipe(splitter).pipe(dumper);

ES6 modules (more complete)

/***
 * Usage:
 *  $ node path/to/yaml2json-dumper.mjs multi-doc.yaml
 *  # or
 *  $ cat multi-doc.yaml | node path/to/yaml2json-dumper.mjs
 */

import fs from 'node:fs';
import util from 'node:util';
import stream from 'node:stream';
import os from 'node:os';
import process from 'node:process';
import ydsPkg from '@avreg/yaml-doc-splitter.js';
import yaml from 'js-yaml';

const { YamlDocSplitter } = ydsPkg;

const pipeline = util.promisify(stream.pipeline);

class YamlToJson extends stream.Transform {
   constructor() {
      super({
         readableObjectMode: true
      });
   }

   _transform(stringChunk, _encoding, callback) {
      try {
         const jsonObj = yaml.load(stringChunk);
         if (jsonObj !== null && jsonObj !== undefined) {
            callback(null, jsonObj);
         } else {
            // empty object
            callback();
         }
      } catch (err) {
         callback(err);
      }
   }
}

class JsonStringifier extends stream.Transform {
   constructor() {
      super({
         writableObjectMode: true
      });
   }

   _transform(jsonObj, _encoding, callback) {
      try {
         callback(null, JSON.stringify(jsonObj, null, 3) + os.EOL);
      } catch (err) {
         callback(err);
      }
   }
}

const inputStream = process.argv[2]
   ? fs.createReadStream(process.argv[2])
   : process.stdio;
const splitter = new YamlDocSplitter();
const convertor = new YamlToJson();
const stringifier = new JsonStringifier();

(async () => {
   try {
      await pipeline(
         inputStream,
         splitter,
         convertor,
         stringifier,
         process.stdout
      );
   } catch (err) {
      console.error('Pipeline job failed:', err.message || `${err}`);
   }
})();

TODO

  • support all encodings;
  • make "dist" package.json.

BUGS

Report bugs to [email protected]