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

ndjson-rxjs

v1.0.0

Published

Newline-delimited JSON parser (and XMLHttpRequest interface) that submits decoded objects to an Observable.

Downloads

74

Readme

Newline delimited JSON streaming in RxJS

A parser for Newline-delimited JSON that uses an XMLHttpRequest as source and delivers the parsed objects as values in an RxJS Observable. The advantage of this over more traditional approaches to JSON or similar APIs is that the client does not need to wait for all data to be available before processing can begin; downloaded objects are delivered as soon as the lines that contain them have fully downloaded.

Installation and importing

npm install ndjson-rxjs --save

We recommend using Babel to allow using ES6 modules in your project; if you do this you can use the following line to import the parser:

import * as NDJsonRxJS from 'ndjson-rxjs';

This will create an object NDJsonRxJS that contains the functions described below. For most purposes, you are only likely to need the stream function; if its name doesn't conflict with anything in your project, you could import it only using the simpler:

import { stream } from 'ndjson-rxjs';

Or, if there is a conflict:

import { stream as ndjsonStream } from 'ndjson-rxjs';

If you're still using require rather than ES6 modules, the equivalent of the first method of importing above is:

var NDJsonRxJS = require('ndjson-rxjs');

Usage

The main function provided is NDJsonRxJS.stream (url, [options]). With no specified options this downloads the requested URL with a GET request, and returns an Observable that will provide the contained JSON objects, one at a time. Empty lines are ignored.

Supported options are:

  • method - to specify an HTTP method other than GET
  • postData - to specify data to send with the request
  • beforeOpen - a callback function that can be used to customize the XMLHttpRequest object (which is passed as its first argument)
  • xhrFactory - a callback which can be used to provide an alternative way of constructing an XMLHttpRequest object (default is to just use new XMLHttpRequest();)

There are two other functions exported that could be useful in some situations, e.g. if building another similar protocol.

NDJsonRxJS.extractStream(xhr, [options]) takes an XMLHttpRequest object and returns an Observable that provides blocks of text as they are downloaded. The options object currently has only one valid option: if endWithNewline is set to true a newline will be appended if the final chunk of the stream did not end with one.

NDJsonRxJS.collate (observable) is a filter on an observable that takes chunks of text and returns arrays of complete lines. Note that if the final chunk of text does not end in a newline, the last line will not be returned, so using the endWithNewline option to extractStream to produce the source observable is sensible here.