Skip to content

Commit

Permalink
Cleanup markdown sample code (#30)
Browse files Browse the repository at this point in the history
* fix markdown code snippets

* Use const/let in sample code

* run eslint on markdown code

* run eslint on markdown code
  • Loading branch information
Christopher Baker authored Feb 15, 2018
1 parent 3a8c6c1 commit fa318fd
Showing 1 changed file with 66 additions and 60 deletions.
126 changes: 66 additions & 60 deletions docs/can-ndjson-stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@

The `can-ndjson-stream` module converts a stream of NDJSON to a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) of JavaScript objects. It is likely that you would use this module to parse an NDJSON stream `response` object received from a [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) request to a service that sends NDJSON streams.
```js
const ndjsonStream = require('can-ndjson-stream');
import ndjsonStream from "can-ndjson-stream";

fetch('/some/endpoint') // make a fetch request to a NDJSON stream service
.then((response) => {
return ndjsonStream(response.body); //ndjsonStream parses the response.body
fetch( "/some/endpoint" ) // make a fetch request to a NDJSON stream service
.then( ( response ) => {
return ndjsonStream( response.body ); //ndjsonStream parses the response.body

}).then((exampleStream) => {
const reader = exampleStream.getReader();
let read;
reader.read().then(read = (result) => {
if (result.done) return;
} ).then( ( exampleStream ) => {
const reader = exampleStream.getReader();
let read;
reader.read().then( read = ( result ) => {
if ( result.done ) {
return;
}

console.log(result.value);
reader.read().then(read);
console.log( result.value );
reader.read().then( read );

});
});
} );
} );
```

@param {ReadableStream<Byte>} stream A readable [NDJSON](http://www.ndjson.org/) byte stream.
Expand All @@ -43,7 +45,7 @@ This module is typically used with [fetch](https://developer.mozilla.org/en-US/d

Assuming your raw data looks something like this:

```js
```
{"item":"first"}\n
{"item":"second"}\n
{"item":"third"}\n
Expand All @@ -59,28 +61,31 @@ follow these steps to make a request from an NDJSON service at `/some/endpoint`:
6. The result of that promise will be one JS object from your NDJSON: `{item: "first"}`

```js
const ndjsonStream = require('can-ndjson-stream');

fetch('/some/endpoint') // make a fetch request to a NDJSON stream service
.then((response) => {
return ndjsonStream(response.body); //ndjsonStream parses the response.body
}).then((exampleStream) => {
//retain access to the reader so that you can cancel it
const reader = exampleStream.getReader();
let read;

reader.read().then(read = (result) => {
if (result.done) return;
console.log(result.value); //logs {item:"first"}
exampleStream.getReader().read().then(read);
});
});
import ndjsonStream from "can-ndjson-stream";

fetch( "/some/endpoint" ) // make a fetch request to a NDJSON stream service
.then( ( response ) => {
return ndjsonStream( response.body ); //ndjsonStream parses the response.body
} ).then( ( exampleStream ) => {

//retain access to the reader so that you can cancel it
const reader = exampleStream.getReader();
let read;

reader.read().then( read = ( result ) => {
if ( result.done ) {
return;
}
console.log( result.value ); //logs {item:"first"}
exampleStream.getReader().read().then( read );
} );
} );
```
## What is NDJSON?

[NDJSON](http://ndjson.org) is a data format that is separated into individual JSON objects with a newline character (`\n`). The 'nd' stands for newline delimited JSON. Essentially, you have some data that is formatted like this:

```js
```
{"item":"first"}\n
{"item":"second"}\n
{"item":"third"}\n
Expand All @@ -103,35 +108,36 @@ $ npm i express path fs ndjson

```js
// server.js
const express = require('express');
import express from "express";

const app = express();
const path = require('path');
const fs = require('fs');
const ndjson = require('ndjson');

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
let readStream = fs.createReadStream(__dirname + '/todos.ndjson').pipe(ndjson.parse())

readStream.on('data', (data) => {
chunks.push(JSON.stringify(data));
});

readStream.on('end', () => {
var id = setInterval(() => {
if (chunks.length) {
res.write(chunks.shift() + '\n');
} else {
clearInterval(id);
res.end();
}
}, 500);
});
});

app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
import path from "path";
import fs from "fs";
import ndjson from "ndjson";

app.use( express.static( path.join( __dirname, "public" ) ) );

app.get( "/", ( req, res ) => {
let readStream = fs.createReadStream( __dirname + "/todos.ndjson" ).pipe( ndjson.parse() );

readStream.on( "data", ( data ) => {
chunks.push( JSON.stringify( data ) );
} );

readStream.on( "end", () => {
const id = setInterval( () => {
if ( chunks.length ) {
res.write( chunks.shift() + "\n" );
} else {
clearInterval( id );
res.end();
}
}, 500 );
} );
} );

app.listen( 3000, () => {
console.log( "Example app listening on port 3000!" );
} );
```
We use a `setInterval` to slow the stream down so that you can see the stream in action. Feel free to remove the setInterval and use a `while` loop to remove the delay.

0 comments on commit fa318fd

Please sign in to comment.