ndjson-readablestream
v1.2.0
Published
A small JS package for reading a ReadableStream of NDJSON
Downloads
159,847
Readme
ndjson-readablestream
A 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:
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:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ndjson-readablestream.umd.js"></script>
<script>
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);
}
</script>
For more details and examples, read my blog post on Fetching JSON over streaming HTTP.
Contributing
Install the development dependencies:
npm install
Run the tests:
npm test
Format the code:
npm run format
Publishing a new version
These instructions are for maintainers only.
- Create a branch
- Bump the version in package.json using either:
npm version patch
npm version minor
npm version major
- Run
npm install
to update package-lock.json - Update CHANGELOG.md with description of changes in version
- Create a pull request
- Once merged, checkout main and run:
npm run build
npm login
npm publish