| 52 | }; |
| 53 | |
| 54 | export class CompressedJSON { |
| 55 | private _rootValue: Value | undefined; |
| 56 | |
| 57 | private _ctx: Context | undefined; |
| 58 | private _contextStack: Context[] = []; |
| 59 | |
| 60 | private _strings: string[] = []; |
| 61 | private _stringValues: { [str: string]: Value } = {}; |
| 62 | private _objects: Value[][] = []; |
| 63 | private _arrays: Value[][] = []; |
| 64 | |
| 65 | constructor( |
| 66 | private readonly _makeDate: boolean, |
| 67 | private readonly _makeTime: boolean, |
| 68 | private readonly _makeDateTime: boolean |
| 69 | ) {} |
| 70 | |
| 71 | async readFromStream(readStream: stream.Readable): Promise<Value> { |
| 72 | const jsonSource = makeSource(); |
| 73 | jsonSource.on("startObject", this.handleStartObject); |
| 74 | jsonSource.on("endObject", this.handleEndObject); |
| 75 | jsonSource.on("startArray", this.handleStartArray); |
| 76 | jsonSource.on("endArray", this.handleEndArray); |
| 77 | jsonSource.on("startKey", this.handleStartKey); |
| 78 | jsonSource.on("endKey", this.handleEndKey); |
| 79 | jsonSource.on("startString", this.handleStartString); |
| 80 | jsonSource.on("stringChunk", this.handleStringChunk); |
| 81 | jsonSource.on("endString", this.handleEndString); |
| 82 | jsonSource.on("startNumber", this.handleStartNumber); |
| 83 | jsonSource.on("numberChunk", this.handleNumberChunk); |
| 84 | jsonSource.on("endNumber", this.handleEndNumber); |
| 85 | jsonSource.on("nullValue", this.handleNullValue); |
| 86 | jsonSource.on("trueValue", this.handleTrueValue); |
| 87 | jsonSource.on("falseValue", this.handleFalseValue); |
| 88 | const promise = new Promise<Value>(resolve => { |
| 89 | jsonSource.on("end", () => { |
| 90 | resolve(this.finish()); |
| 91 | }); |
| 92 | }); |
| 93 | readStream.setEncoding("utf8"); |
| 94 | readStream.pipe(jsonSource.input); |
| 95 | readStream.resume(); |
| 96 | return promise; |
| 97 | } |
| 98 | |
| 99 | getStringForValue = (v: Value): string => { |
| 100 | return this._strings[getIndex(v, Tag.InternedString)]; |
| 101 | }; |
| 102 | |
| 103 | getObjectForValue = (v: Value): Value[] => { |
| 104 | return this._objects[getIndex(v, Tag.Object)]; |
| 105 | }; |
| 106 | |
| 107 | getArrayForValue = (v: Value): Value[] => { |
| 108 | return this._arrays[getIndex(v, Tag.Array)]; |
| 109 | }; |
| 110 | |
| 111 | private internString = (s: string): Value => { |
nothing calls this directly
no test coverage detected
searching dependent graphs…