-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes bindings parsing work really in streaming
- Loading branch information
Showing
5 changed files
with
234 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import {Transform} from "readable-stream"; | ||
import {JsonEvent} from "json-event-parser"; | ||
|
||
export type JsonValue = string | number | boolean | null | JsonValue[] | {[key: string]: JsonValue}; | ||
|
||
export type QueryDefinition = { | ||
id: string; | ||
path: (string | null)[], | ||
returnContent: boolean | ||
} | ||
|
||
export type QueryResult = { | ||
query: string; | ||
value?: JsonValue | ||
} | ||
|
||
/** | ||
* Emits all JSON blobs matching one of the given query. | ||
* A query is a path composed of object keys or `null` for array keys. | ||
*/ | ||
export class JsonStreamQueryTransformer extends Transform { | ||
private readonly queries: QueryDefinition[]; | ||
private keyStack: (string | number)[] = []; | ||
private valueStack: JsonValue[] = []; | ||
private currentQuery?: QueryDefinition = undefined; | ||
|
||
constructor(queries: QueryDefinition[]) { | ||
super({writableObjectMode: true, readableObjectMode: true}); | ||
this.queries = queries; | ||
} | ||
|
||
_transform(event: JsonEvent, encoding: BufferEncoding, callback: (error?: (Error | null), data?: any) => void): void { | ||
this.currentQuery === undefined ? this.onOtherEvent(event) : this.onKeptEvent(event); | ||
callback(); | ||
} | ||
|
||
private onOtherEvent(event: JsonEvent): void { | ||
switch (event.type) { | ||
case "open-object": | ||
if(event.key !== undefined) { | ||
this.keyStack.push(event.key); | ||
} | ||
this.currentQuery = this.findCurrentQuery(); | ||
if(this.currentQuery) { | ||
if(this.currentQuery.returnContent) { | ||
this.valueStack.push({}); | ||
} else { | ||
this.push({ | ||
query: this.currentQuery.id, | ||
}) | ||
this.currentQuery = undefined; | ||
} | ||
} | ||
return; | ||
case "open-array": | ||
if(event.key !== undefined) { | ||
this.keyStack.push(event.key); | ||
} | ||
this.currentQuery = this.findCurrentQuery(); | ||
if(this.currentQuery) { | ||
if(this.currentQuery.returnContent) { | ||
this.valueStack.push([]); | ||
} else { | ||
this.push({ | ||
query: this.currentQuery.id | ||
}) | ||
this.currentQuery = undefined; | ||
} | ||
} | ||
return; | ||
case "value": | ||
if(event.key !== undefined) { | ||
this.keyStack.push(event.key); | ||
} | ||
this.currentQuery = this.findCurrentQuery(); | ||
if(this.currentQuery) { | ||
this.push({ | ||
query: this.currentQuery.id, | ||
value: event.value | ||
}) | ||
this.currentQuery = undefined; | ||
} | ||
case "close-object": | ||
case "close-array": | ||
this.keyStack.pop(); | ||
} | ||
} | ||
|
||
private findCurrentQuery(): QueryDefinition | undefined { | ||
for(const query of this.queries) { | ||
if(query.path.length === this.keyStack.length && query.path.every((value, i) => value === null ? typeof this.keyStack[i] === 'number' : value === this.keyStack[i])) { | ||
return query; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
private onKeptEvent(event: JsonEvent): void { | ||
let value: any; | ||
switch (event.type) { | ||
case "open-object": | ||
value = {}; | ||
this.insertInPreviousValue(event.key, value); | ||
this.valueStack.push(value); | ||
return; | ||
case "open-array": | ||
value = []; | ||
this.insertInPreviousValue(event.key, value); | ||
this.valueStack.push(value); | ||
return; | ||
case "value": | ||
this.insertInPreviousValue(event.key, event.value); | ||
return; | ||
case "close-object": | ||
case "close-array": | ||
value = this.valueStack.pop(); | ||
if(this.valueStack.length === 0) { | ||
// End | ||
this.push({ | ||
query: this.currentQuery.id, | ||
value | ||
}) | ||
this.currentQuery = undefined; | ||
this.keyStack.pop(); | ||
} | ||
} | ||
} | ||
|
||
private insertInPreviousValue(key: number | string | undefined, value: JsonValue): void { | ||
const previousValue = this.valueStack[this.valueStack.length - 1]; | ||
if(typeof key === "number") { | ||
(<JsonValue[]>previousValue).push(value); | ||
} else if(typeof key === "string") { | ||
(<any>previousValue)[key] = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2369,6 +2369,14 @@ jsesc@^2.5.1: | |
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4" | ||
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA== | ||
|
||
[email protected]: | ||
version "1.0.0-beta.1" | ||
resolved "https://registry.yarnpkg.com/json-event-parser/-/json-event-parser-1.0.0-beta.1.tgz#08ca3dc6c13184012754a8ff2b7e78a0e99bd95f" | ||
integrity sha512-Ylf8GZNH2ftqvzVxOv7AdZ/+hf7Wy29JWvFe2g8wAmgKfyQDlP+H10dGOgwbGmeoPVhx19WDHWq+W8bLs5a1wg== | ||
dependencies: | ||
buffer "^6.0.3" | ||
readable-stream "^4.0.0" | ||
|
||
json-parse-better-errors@^1.0.1: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" | ||
|
@@ -2399,11 +2407,6 @@ json5@^2.2.1: | |
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" | ||
integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== | ||
|
||
jsonparse@^1.3.1: | ||
version "1.3.1" | ||
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280" | ||
integrity sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg== | ||
|
||
jsprim@^1.2.2: | ||
version "1.4.2" | ||
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.2.tgz#712c65533a15c878ba59e9ed5f0e26d5b77c5feb" | ||
|