A small JS package for reading a ReadableStream of NDJSON
npm install ndjson-readablestreamA small JS package for reading a ReadableStream of NDJSON.
readNDJSONStream() accepts a ReadableStream object,
and returns an AsyncGenerator where each yielded event is a valid JSON object.
Example usage:
``javascript
import readNDJSONStream from 'ndjson-readablestream';
const response = await fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: 'Hi, how are you?' }),
});
for await (const event of readNDJSONStream(response.body)) {
console.log('Received', event);
}
`
Example usage in a webpage:
`html`
For more details and examples, read my blog post on Fetching JSON over streaming HTTP.
Install the development dependencies:
``
npm install
Run the tests:
``
npm test
Format the code:
``
npm run format
These instructions are for maintainers only.
0. Create a branch
1. Bump the version in package.json using either:
- npm version patchnpm version minor
- npm version major
- npm install
2. Run to update package-lock.jsonnpm run build
3. Update CHANGELOG.md with description of changes in version
4. Create a pull request
5. Once merged, checkout main and run:
1. npm login
1. npm publish`
1.