@streamparser/json-node
Advanced tools
Comparing version
@@ -13,2 +13,2 @@ export { default as JSONParser } from "./jsonparser.ts"; | ||
TokenType, | ||
} from "https://deno.land/x/streamparser_json@v0.0.17/index.ts"; | ||
} from "https://deno.land/x/streamparser_json@v0.0.18/index.ts"; |
@@ -6,3 +6,3 @@ import { | ||
} from "stream"; | ||
import { JSONParser, type JSONParserOptions } from "https://deno.land/x/streamparser_json@v0.0.17/index.ts"; | ||
import { JSONParser, type JSONParserOptions } from "https://deno.land/x/streamparser_json@v0.0.18/index.ts"; | ||
@@ -9,0 +9,0 @@ export default class JSONParserTransform extends Transform { |
@@ -55,2 +55,3 @@ # @streamparser/json-node | ||
separator: <string>, // separator between object. For example `\n` for nd-js. | ||
emitPartialTokens: <boolean> // whether to emit tokens mid-parsing. | ||
} | ||
@@ -86,2 +87,3 @@ ``` | ||
separator: <string>, // separator between object. For example `\n` for nd-js. If left empty or set to undefined, the token parser will end after parsing the first object. To parse multiple object without any delimiter just set it to the empty string `''`. | ||
emitPartialValues: <boolean>, // whether to emit values mid-parsing. | ||
} | ||
@@ -113,3 +115,2 @@ ``` | ||
You can subscribe to the resulting data using the | ||
@@ -144,3 +145,3 @@ | ||
const reader = response.body.pipe(parser); | ||
reader.on('data', value => /* process element */) | ||
reader.on('data', value => /* process element */); | ||
``` | ||
@@ -159,3 +160,3 @@ | ||
const reader = response.body.pipe(parse)getReader(); | ||
const reader = response.body.pipe(parse).getReader(); | ||
@@ -165,2 +166,24 @@ reader.on('data', ({ value, key, parent, stack }) => /* process element */) | ||
### Stream-parsing a fetch request returning a very long string getting previews of the string | ||
Imagine an endpoint that send a large amount of JSON objects one after the other (`"Once upon a midnight <...>"`). | ||
```js | ||
import { JSONParser } from '@streamparser/json-node'; | ||
const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); | ||
const response = await fetch('http://example.com/'); | ||
const reader = response.body.pipe(parse).getReader(); | ||
reader.on('data', ({ value, key, parent, stack, partial }) => { | ||
if (partial) { | ||
console.log(`Parsing value: ${value}... (still parsing)`); | ||
} else { | ||
console.log(`Value parsed: ${value}`); | ||
} | ||
}); | ||
``` | ||
## License | ||
@@ -167,0 +190,0 @@ |
@@ -8,3 +8,3 @@ import { | ||
type TokenizerOptions, | ||
} from "https://deno.land/x/streamparser_json@v0.0.17/tokenizer.ts"; | ||
} from "https://deno.land/x/streamparser_json@v0.0.18/tokenizer.ts"; | ||
@@ -11,0 +11,0 @@ export default class TokenizerTransform extends Transform { |
@@ -6,3 +6,3 @@ import { | ||
} from "stream"; | ||
import { TokenParser, type TokenParserOptions } from "https://deno.land/x/streamparser_json@v0.0.17/index.ts"; | ||
import { TokenParser, type TokenParserOptions } from "https://deno.land/x/streamparser_json@v0.0.18/index.ts"; | ||
@@ -9,0 +9,0 @@ export default class TokenParserTransform extends Transform { |
{ | ||
"name": "@streamparser/json-node", | ||
"description": "Streaming JSON parser in Javascript for Node.js, Deno and the browser", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"main": "./dist/mjs/index.js", | ||
@@ -45,3 +45,3 @@ "module": "./dist/mjs/index.js", | ||
"dependencies": { | ||
"@streamparser/json": "^0.0.17" | ||
"@streamparser/json": "^0.0.18" | ||
}, | ||
@@ -48,0 +48,0 @@ "devDependencies": { |
@@ -55,2 +55,3 @@ # @streamparser/json-node | ||
separator: <string>, // separator between object. For example `\n` for nd-js. | ||
emitPartialTokens: <boolean> // whether to emit tokens mid-parsing. | ||
} | ||
@@ -86,2 +87,3 @@ ``` | ||
separator: <string>, // separator between object. For example `\n` for nd-js. If left empty or set to undefined, the token parser will end after parsing the first object. To parse multiple object without any delimiter just set it to the empty string `''`. | ||
emitPartialValues: <boolean>, // whether to emit values mid-parsing. | ||
} | ||
@@ -113,3 +115,2 @@ ``` | ||
You can subscribe to the resulting data using the | ||
@@ -144,3 +145,3 @@ | ||
const reader = response.body.pipe(parser); | ||
reader.on('data', value => /* process element */) | ||
reader.on('data', value => /* process element */); | ||
``` | ||
@@ -159,3 +160,3 @@ | ||
const reader = response.body.pipe(parse)getReader(); | ||
const reader = response.body.pipe(parse).getReader(); | ||
@@ -165,2 +166,24 @@ reader.on('data', ({ value, key, parent, stack }) => /* process element */) | ||
### Stream-parsing a fetch request returning a very long string getting previews of the string | ||
Imagine an endpoint that send a large amount of JSON objects one after the other (`"Once upon a midnight <...>"`). | ||
```js | ||
import { JSONParser } from '@streamparser/json-node'; | ||
const parser = new JSONParser({ stringBufferSize: undefined, paths: ['$.*'], keepStack: false }); | ||
const response = await fetch('http://example.com/'); | ||
const reader = response.body.pipe(parse).getReader(); | ||
reader.on('data', ({ value, key, parent, stack, partial }) => { | ||
if (partial) { | ||
console.log(`Parsing value: ${value}... (still parsing)`); | ||
} else { | ||
console.log(`Value parsed: ${value}`); | ||
} | ||
}); | ||
``` | ||
## License | ||
@@ -167,0 +190,0 @@ |
@@ -29,3 +29,3 @@ import { | ||
separators.forEach((separator) => { | ||
test(`separator: ${separator}`, async () => { | ||
test(`separator: "${separator}"`, async () => { | ||
let i = 0; | ||
@@ -32,0 +32,0 @@ |
@@ -54,3 +54,3 @@ import { runJSONParserTest } from "../utils/testRunner.js"; | ||
const bufferSizes = [0, 64 * 1024]; | ||
const bufferSizes = [0, 1, 64 * 1024]; | ||
@@ -57,0 +57,0 @@ bufferSizes.forEach((numberBufferSize) => { |
@@ -18,3 +18,3 @@ import { runJSONParserTest } from "../utils/testRunner.js"; | ||
const bufferSizes = [0, 64 * 1024]; | ||
const bufferSizes = [0, 1, 64 * 1024]; | ||
@@ -163,8 +163,18 @@ bufferSizes.forEach((stringBufferSize) => { | ||
const invalidValues = ["\n", "\\j", "\\ua", "\\u1*", "\\u12*", "\\u123*"]; | ||
const invalidValues = [ | ||
'"\n"', | ||
'"\\j"', | ||
'"\\ua"', | ||
'"\\u1*"', | ||
'"\\u12*"', | ||
"\\u123*", | ||
'"\0"', | ||
'"\\uG"', | ||
'"\\u000G"', | ||
]; | ||
invalidValues.forEach((value) => { | ||
test("fail on invalid values", async () => { | ||
test(`fail on invalid values ${value}`, async () => { | ||
try { | ||
await runJSONParserTest(new JSONParser(), [value]); | ||
await runJSONParserTest(new JSONParser(), value); | ||
fail(`Expected to fail on value "${value}"`); | ||
@@ -171,0 +181,0 @@ } catch (e) { |
@@ -9,3 +9,3 @@ import { Readable } from "stream"; | ||
export type TestData = { | ||
value: string | Iterable<number>; | ||
value: string | string[] | Iterable<number>; | ||
paths?: string[]; | ||
@@ -12,0 +12,0 @@ expected: any[]; |
113291
22.66%2551
31.02%194
13.45%64
-11.11%+ Added
- Removed
Updated