jsonl-parser-web
TypeScript icon, indicating that this package has built-in type declarations

0.0.0 • Public • Published

jsonl-parser-web

Simple JSONLines parser (.jsonl) that works with streams for web runtime, with Typescript support. The goal was to have a simple API for a specific use case.

Current only supports stream inputs.

Installation

npm install jsonlines-p
yarn add jsonlines-p
pnpm add jsonlines-p

Usage

Fetch

Pass the respones body to the parser.

import { JsonLines, JsonLineStream } from 'jsonlines-p';

const response = await fetch("/remote/items.jsonl");
// Aggregates the lines into an array.
const items = await JsonLines<Item>(response.body);
// or if you want to access the stream directly
const stream = JsonLineStream<Item>(response.body);

Options

Use a custom line parser and error handler.

// Usage
JsonLines(response.body, options)

export type ParserOptions<T extends JsonLine> = {
  /**
   * Parse each JSON line.
   */
  parser?: (line: string) => T, 
  /**
   * Callback for errors thrown by the parser.
   * If not provided, errors will be silently ignored.
   * 
   * Note that this might be called multiple times for a stream, for each line.
   */
  onError?: (error: Error, line: string) => void;
}

Internals

These are also exported if you need more control.

export function createLineStreamTransformer<T>(
  options: ParserOptions<T> = {}
): TransformStream<Uint8Array, T>

License

The MIT License.

Package Sidebar

Install

npm i jsonl-parser-web

Weekly Downloads

1

Version

0.0.0

License

MIT

Unpacked Size

27.9 kB

Total Files

13

Last publish

Collaborators

  • ryoid