Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

jsonsurge

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonsurge

A JSON parsing utility for streaming data

npmnpm
Version
1.0.2
Version published
Weekly downloads
9
-76.32%
Maintainers
1
Weekly downloads
 
Created
Source

StreamJSONParser

StreamJSONParser is a JavaScript module that provides a way to parse JSON data from a readable stream in an asynchronous and efficient manner. It is particularly useful when dealing with streaming JSON data, such as data received from server-sent events (SSE) or other real-time data sources.

Installation

You can install StreamJSONParser using npm or yarn:

npm install stream-json-parser

or

yarn add stream-json-parser

Usage

To use StreamJSONParser, you need to import it into your JavaScript or TypeScript code:

import streamJSONParser from 'stream-json-parser';

Example Usage

Here is an example of how to use StreamJSONParser to parse JSON data from a readable stream:

import streamJSONParser from 'stream-json-parser';

// Create a ReadableStream (e.g., from a fetch response)
const response = await fetch('https://example.com/streaming-data');
const stream = response.body;

for await (const chunk of streamjsonparser(response.body)) {
                console.log(chunk)
            }

How It Works

The streamJSONParser function accepts a readable stream as its input and returns an asynchronous generator that yields parsed JSON objects as they are received from the stream.

  • The readChunks function reads the stream in chunks, asynchronously yielding each chunk.

  • The readLines function processes the chunks to split them into lines of text. It handles cases where a single chunk might contain multiple lines.

  • The main streamJSONParser function trims each line of text, removes any "data: " prefix, and parses the resulting JSON. It then yields the parsed JSON objects.

Contributing

If you'd like to contribute to StreamJSONParser, please open an issue or submit a pull request on GitHub.

License

StreamJSONParser is licensed under the MIT License.

Acknowledgments

StreamJSONParser is inspired by the need for efficient parsing of streaming JSON data in real-time applications.

About

StreamJSONParser is maintained by [Your Name]. You can contact me at [your.email@example.com].

For more information, visit the GitHub repository.

Note: Replace [your/repo], [Your Name], [your.email@example.com], and [MIT License] with your actual repository URL, name, contact information, and license information.

This README is a template, and you should customize it according to your project's specific details.

FAQs

Package last updated on 25 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts