-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJsonHigh.d.ts
192 lines (167 loc) · 5.57 KB
/
JsonHigh.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
export declare const JsonHigh: JsonHigh
export type JsonHigh = <Feedback, End>(
next: JsonHighHandlers<Feedback, End>,
) => {
chunk(chunk: string): Feedback,
end(): End,
/**
* Reports current depth (level of nesting).
*
* **NOTE**: an `open*` or `close*` handler is always called *after* the depth is updated, meaning:
*
* * in `openObject`/`openArray` handlers the lowest depth reported will be 1 -- we entered a top-level object/array and are now at depth 1
*
* * in `closeObject`/`closeArray` handlers the lowest depth reported will be 0 -- we exited a top-level object/array and are now at depth 0 (top-level)
*/
depth(): number,
// todo: type for state()
}
export type JsonHighHandlers<Feedback, End> = {
/**
* ### Options
*/
/**
* @defaultValue `Infinity`
*
* The maximum length of the key or string buffer, in code points.
*
* If set, the `key` event handler won't be called and, for strings, the `value` event handler won't be called.
*
* Instead, the `bufferKey` and `bufferString` handlers will be called as soon as the given number of code points has been collected or when the key/string is finished.
*
* In the latter case the number of code points may be smaller than `maxStringBufferLength` and a `closeKey`/`closeString` handler will be called after the last `*Buffer` event to signal the finish.
*
* This is useful when dealing with long strings where it's desirable to stream them piece-by-piece, e.g. when working with LLMs (Large Language Models).
*
* See [Add support for incomplete key and value strings #10](https://github.com/xtao-org/jsonhilo/issues/10) for more information.
*
*/
maxStringBufferLength?: number,
/**
* @defaultValue `8192`
*
* Specifies the maximum length of a number value (in characters).
*
*/
maxNumberLength?: number,
/**
* @defaultValue `true`
*
* Controls whether numbers should be parsed (converted to JavaScript `number` type) which is the case by default.
*
* If set to `false`, the `value` event handler won't be called for numbers. Instead, the `bufferNumber` handler will be called with the number as a string.
*
* This is useful when dealing with big numbers which would lose precision when converted to the `number` type.
*
*/
parseNumbers?: boolean,
/**
* ### Basic events
*
* The basic usage of `JsonHigh` involves 4 event handlers without arguments which indicate start and end of structures:
*/
/**
* An array started (`[`).
*/
openArray?: JsonHighHandler<Feedback>,
/**
* An array ended (`]`).
*/
closeArray?: JsonHighHandler<Feedback>,
/**
* An object started (`{`).
*/
openObject?: JsonHighHandler<Feedback>,
/**
* An object ended (`}`).
*/
closeObject?: JsonHighHandler<Feedback>,
/**
* And 2 event handlers with one argument which capture primitives:
*/
/**
* An object's key ended.
*
* The argument of the handler contains the key as a JavaScript string.
*
* This event can be suppressed by setting [`maxStringBufferLength`](#maxStringBufferLength).
*/
key?: (key: string) => Feedback,
/**
* A primitive JSON value ended.
*
* The argument of the event contains the corresponding JavaScript value: `true`, `false`, `null`, a number, or a string.
*
* This event can be suppressed for strings if [`maxStringBufferLength`](#maxStringBufferLength) is set and for numbers if [`parseNumbers`](#parseNumbers) is set to `false`.
*/
value?: (value: string | number | null | boolean) => Feedback,
/**
* Finally, there is the argumentless `end` event handler:
*/
/**
* Called by the `end` method of the stream to confirm that the parsed JSON document is complete and valid.
*/
end?: () => End,
/**
* ### Extra events
*
* These handlers take no arguments.
*/
/**
* A key started (`"`, in key position).
*/
openKey?: JsonHighHandler<Feedback>,
/**
* A string value started (`"`).
*/
openString?: JsonHighHandler<Feedback>,
/**
* A number value started.
*/
openNumber?: JsonHighHandler<Feedback>,
/**
* ### Conditional events
*
* These handlers take no arguments:
*/
/**
* A key ended (`"`).
*
* Called instead of the `key` event **when `maxStringBufferLength` is set**.
*/
closeKey?: JsonHighHandler<Feedback>,
/**
* A string value ended (`"`).
*
* Called instead of the `value` event **when `maxStringBufferLength` is set**.
*/
closeString?: JsonHighHandler<Feedback>,
/**
* These handlers receive the buffer that should be consumed:
*/
/**
* Key buffer is ready for consumption.
*
* Called instead of the `key` event **when `maxStringBufferLength` is set**.
*
* The `buffer` then contains `maxStringBufferLength` code points or possibly less if we reached the end of a key.
*/
bufferKey?: (buffer: string) => Feedback,
/**
* String buffer is ready for consumption.
*
* For string values, called instead of the `value` event **when `maxStringBufferLength` is set**.
*
* The buffer then contains `maxStringBufferLength` code points or possibly less if we reached the end of a string value.
*/
bufferString?: (buffer: string) => Feedback,
/**
* Number buffer is ready for consumption.
*
* For number values, called instead of the `value` event **when `parseNumbers` is set**.
*
* The buffer then contains the unparsed number (represented as a string).
*/
bufferNumber?: (buffer: string) => Feedback,
}
export type JsonHighHandler<Feedback> = () => Feedback