@pairdraw/kiwi 0.0.0-stage → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +28 -0
- package/README.md +112 -2
- package/dist/chunks/fig/codec.js +1371 -0
- package/dist/chunks/fig/codec.js.map +1 -0
- package/dist/fig/codec.d.ts +477 -0
- package/dist/fig/codec.js +2 -0
- package/dist/fig/container.d.ts +48 -0
- package/dist/fig/container.js +2 -0
- package/dist/fig/guid.d.ts +13 -0
- package/dist/fig/guid.js +31 -0
- package/dist/fig/guid.js.map +1 -0
- package/dist/fig/parse.d.ts +33 -0
- package/dist/fig/parse.js +83 -0
- package/dist/fig/parse.js.map +1 -0
- package/dist/fig.d.ts +185 -0
- package/dist/fig.js +117 -0
- package/dist/fig.js.map +1 -0
- package/dist/index.d.ts +182 -0
- package/dist/page-manifest.d.ts +16 -0
- package/dist/schema-runtime.d.ts +2 -0
- package/dist/schema-runtime.js +2 -0
- package/dist/types.d.ts +26 -0
- package/package.json +64 -4
- package/src/fig/codec.ts +536 -0
- package/src/fig/container.ts +180 -0
- package/src/fig/guid.ts +24 -0
- package/src/fig/index.ts +5 -0
- package/src/fig/page-manifest.ts +149 -0
- package/src/fig/parse.ts +136 -0
- package/src/fig/protocol.ts +237 -0
- package/src/fig/schema/fig.kiwi +5628 -0
- package/src/fig/schema.ts +7 -0
- package/src/fig/types.ts +25 -0
- package/src/fig/variable-bindings.ts +11 -0
- package/src/global.d.ts +4 -0
- package/src/schema-runtime/bb.ts +284 -0
- package/src/schema-runtime/binary.ts +102 -0
- package/src/schema-runtime/index.ts +25 -0
- package/src/schema-runtime/interpreter.ts +465 -0
- package/src/schema-runtime/parser.ts +287 -0
- package/src/schema-runtime/schema.ts +24 -0
- package/src/schema-runtime/util.ts +10 -0
- package/src/schema-runtime/validate.ts +88 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { ByteBuffer } from './bb'
|
|
2
|
+
import type { Definition, DefinitionKind, Field, Schema } from './schema'
|
|
3
|
+
import { error, quote } from './util'
|
|
4
|
+
|
|
5
|
+
export type RuntimeValue =
|
|
6
|
+
| boolean
|
|
7
|
+
| number
|
|
8
|
+
| string
|
|
9
|
+
| bigint
|
|
10
|
+
| Uint8Array
|
|
11
|
+
| RuntimeMessage
|
|
12
|
+
| RuntimeValue[]
|
|
13
|
+
export type RuntimeMessage = { [name: string]: RuntimeValue }
|
|
14
|
+
type RuntimeEnum = { [name: string]: string | number }
|
|
15
|
+
type DecodeFunction = (bb: ByteBuffer | Uint8Array) => RuntimeMessage
|
|
16
|
+
type EncodeFunction = (message: RuntimeMessage, bb?: ByteBuffer) => Uint8Array | undefined
|
|
17
|
+
type RuntimeEntry =
|
|
18
|
+
| typeof ByteBuffer
|
|
19
|
+
| CompiledSchema
|
|
20
|
+
| RuntimeEnum
|
|
21
|
+
| RuntimeValue
|
|
22
|
+
| DecodeFunction
|
|
23
|
+
| EncodeFunction
|
|
24
|
+
| undefined
|
|
25
|
+
export type RuntimeCodec = {
|
|
26
|
+
ByteBuffer: typeof ByteBuffer
|
|
27
|
+
/** The tables the codec reads with, for scanners that pick single fields out of a message. */
|
|
28
|
+
schema: CompiledSchema
|
|
29
|
+
encodeMessage?: EncodeFunction
|
|
30
|
+
decodeMessage?: DecodeFunction
|
|
31
|
+
encodePaint?: EncodeFunction
|
|
32
|
+
encodeNodeChange?: EncodeFunction
|
|
33
|
+
[name: string]: RuntimeEntry
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Message types decoded as headers: only the named fields are read, and the rest stay in the
|
|
38
|
+
* message bytes until `decodeWhole` reads the message in full. A large message of many such
|
|
39
|
+
* messages then holds a little of each rather than every value of every one.
|
|
40
|
+
*/
|
|
41
|
+
export type HeaderFields = Readonly<Record<string, readonly string[]>>
|
|
42
|
+
|
|
43
|
+
/** What a field holds: a native type, or an enum, struct, or message of the schema. */
|
|
44
|
+
export const TYPE = {
|
|
45
|
+
BOOL: 0,
|
|
46
|
+
BYTE: 1,
|
|
47
|
+
INT: 2,
|
|
48
|
+
UINT: 3,
|
|
49
|
+
FLOAT: 4,
|
|
50
|
+
STRING: 5,
|
|
51
|
+
INT64: 6,
|
|
52
|
+
UINT64: 7,
|
|
53
|
+
ENUM: 8,
|
|
54
|
+
STRUCT: 9,
|
|
55
|
+
MESSAGE: 10
|
|
56
|
+
} as const
|
|
57
|
+
export type TypeCode = (typeof TYPE)[keyof typeof TYPE]
|
|
58
|
+
|
|
59
|
+
const NATIVE_TYPES: ReadonlyMap<string, TypeCode> = new Map([
|
|
60
|
+
['bool', TYPE.BOOL],
|
|
61
|
+
['byte', TYPE.BYTE],
|
|
62
|
+
['int', TYPE.INT],
|
|
63
|
+
['uint', TYPE.UINT],
|
|
64
|
+
['float', TYPE.FLOAT],
|
|
65
|
+
['string', TYPE.STRING],
|
|
66
|
+
['int64', TYPE.INT64],
|
|
67
|
+
['uint64', TYPE.UINT64]
|
|
68
|
+
])
|
|
69
|
+
|
|
70
|
+
const KIND_TYPES: Readonly<Record<DefinitionKind, TypeCode>> = {
|
|
71
|
+
ENUM: TYPE.ENUM,
|
|
72
|
+
STRUCT: TYPE.STRUCT,
|
|
73
|
+
MESSAGE: TYPE.MESSAGE
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A field resolved once: its type as a number, and the definition behind it. */
|
|
77
|
+
export interface CompiledField {
|
|
78
|
+
readonly name: string
|
|
79
|
+
readonly id: number
|
|
80
|
+
readonly isArray: boolean
|
|
81
|
+
readonly isDeprecated: boolean
|
|
82
|
+
readonly type: TypeCode
|
|
83
|
+
/** The definition an enum, struct, or message field holds; null for a native type. */
|
|
84
|
+
readonly definition: CompiledDefinition | null
|
|
85
|
+
/** Whether a header of the owning message keeps this field. */
|
|
86
|
+
readonly kept: boolean
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface CompiledDefinition {
|
|
90
|
+
readonly name: string
|
|
91
|
+
readonly kind: DefinitionKind
|
|
92
|
+
readonly fields: readonly CompiledField[]
|
|
93
|
+
/** A message's fields by ID, with a hole at every ID the definition does not use. */
|
|
94
|
+
readonly byId: ReadonlyArray<CompiledField | undefined>
|
|
95
|
+
/** An enum's member names by value. */
|
|
96
|
+
readonly names: ReadonlyArray<string | undefined>
|
|
97
|
+
/** An enum's values by member name. */
|
|
98
|
+
readonly values: ReadonlyMap<string, number>
|
|
99
|
+
/** Whether messages of this definition decode as headers. */
|
|
100
|
+
readonly header: boolean
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A schema's definitions resolved into tables, and the one reader, skipper, and writer that
|
|
105
|
+
* every codec and scanner shares; nothing is looked up by name while a message is read.
|
|
106
|
+
*/
|
|
107
|
+
export interface CompiledSchema {
|
|
108
|
+
readonly definitions: readonly CompiledDefinition[]
|
|
109
|
+
/** A definition by name; throws for one the schema lacks. */
|
|
110
|
+
definition(name: string): CompiledDefinition
|
|
111
|
+
/** One field's value, its array included; its shape follows the schema at run time. */
|
|
112
|
+
read(field: CompiledField, bb: ByteBuffer): unknown
|
|
113
|
+
/** Past one field's value, its array included. */
|
|
114
|
+
skip(field: CompiledField, bb: ByteBuffer): void
|
|
115
|
+
/** One field's value, its array included; a message field writes its own end marker. */
|
|
116
|
+
write(field: CompiledField, value: RuntimeValue, bb: ByteBuffer): void
|
|
117
|
+
/** A struct or message read in full, or as a header where the definition is one. */
|
|
118
|
+
decode(definition: CompiledDefinition, bb: ByteBuffer): RuntimeMessage
|
|
119
|
+
/** A struct or message written in full. */
|
|
120
|
+
encode(definition: CompiledDefinition, message: RuntimeMessage, bb: ByteBuffer): void
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* What a header needs to be read whole: the bytes it came from, where it starts in them, and
|
|
125
|
+
* the decoder that reads every field. They are hidden properties of the header, so each costs a
|
|
126
|
+
* slot on the object; neither spread nor `structuredClone` copies them.
|
|
127
|
+
*/
|
|
128
|
+
const HEADER_BYTES = Symbol('header bytes')
|
|
129
|
+
const HEADER_START = Symbol('header start')
|
|
130
|
+
const HEADER_WHOLE = Symbol('header whole decoder')
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A header read again in full from the bytes it came from, as a new message the caller owns,
|
|
134
|
+
* or undefined for a message that was decoded whole.
|
|
135
|
+
*/
|
|
136
|
+
export function decodeWhole(message: object): RuntimeMessage | undefined {
|
|
137
|
+
const bytes: unknown = Reflect.get(message, HEADER_BYTES)
|
|
138
|
+
const start: unknown = Reflect.get(message, HEADER_START)
|
|
139
|
+
const whole: unknown = Reflect.get(message, HEADER_WHOLE)
|
|
140
|
+
if (!(bytes instanceof Uint8Array) || typeof start !== 'number' || typeof whole !== 'function')
|
|
141
|
+
return undefined
|
|
142
|
+
const buffer = new ByteBuffer(bytes)
|
|
143
|
+
buffer.offset = start
|
|
144
|
+
return (whole as (bb: ByteBuffer) => RuntimeMessage)(buffer)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function hide(message: RuntimeMessage, key: symbol, value: unknown): void {
|
|
148
|
+
Reflect.defineProperty(message, key, { value, enumerable: false })
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function invalidMessage(): never {
|
|
152
|
+
throw new Error('Attempted to parse invalid message')
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function compileDefinitions(schema: Schema, headers?: HeaderFields): CompiledDefinition[] {
|
|
156
|
+
const byName = new Map<string, Definition>()
|
|
157
|
+
for (const definition of schema.definitions) byName.set(definition.name, definition)
|
|
158
|
+
const compiled = new Map<string, CompiledDefinition>()
|
|
159
|
+
const definitions = schema.definitions.map((definition): CompiledDefinition => {
|
|
160
|
+
const fields: CompiledField[] = []
|
|
161
|
+
const byId: (CompiledField | undefined)[] = []
|
|
162
|
+
const names: (string | undefined)[] = []
|
|
163
|
+
const values = new Map<string, number>()
|
|
164
|
+
if (definition.kind === 'ENUM')
|
|
165
|
+
for (const field of definition.fields) {
|
|
166
|
+
names[field.value] = field.name
|
|
167
|
+
values.set(field.name, field.value)
|
|
168
|
+
}
|
|
169
|
+
const kept = headers?.[definition.name]
|
|
170
|
+
const result: CompiledDefinition = {
|
|
171
|
+
name: definition.name,
|
|
172
|
+
kind: definition.kind,
|
|
173
|
+
fields,
|
|
174
|
+
byId,
|
|
175
|
+
names,
|
|
176
|
+
values,
|
|
177
|
+
header: definition.kind === 'MESSAGE' && kept !== undefined
|
|
178
|
+
}
|
|
179
|
+
compiled.set(definition.name, result)
|
|
180
|
+
return result
|
|
181
|
+
})
|
|
182
|
+
schema.definitions.forEach((definition, index) => {
|
|
183
|
+
if (definition.kind === 'ENUM') return
|
|
184
|
+
const target = definitions[index]
|
|
185
|
+
const fields = target.fields as CompiledField[]
|
|
186
|
+
const byId = target.byId as (CompiledField | undefined)[]
|
|
187
|
+
const kept = headers?.[definition.name]
|
|
188
|
+
for (const field of definition.fields) {
|
|
189
|
+
const compiledField = compileField(field, kept, byName, compiled)
|
|
190
|
+
fields.push(compiledField)
|
|
191
|
+
byId[field.value] = compiledField
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
return definitions
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function compileField(
|
|
198
|
+
field: Field,
|
|
199
|
+
kept: readonly string[] | undefined,
|
|
200
|
+
byName: ReadonlyMap<string, Definition>,
|
|
201
|
+
compiled: ReadonlyMap<string, CompiledDefinition>
|
|
202
|
+
): CompiledField {
|
|
203
|
+
const type = field.type
|
|
204
|
+
if (type === null)
|
|
205
|
+
error('Invalid type null for field ' + quote(field.name), field.line, field.column)
|
|
206
|
+
const native = NATIVE_TYPES.get(type)
|
|
207
|
+
const definition = native === undefined ? byName.get(type) : undefined
|
|
208
|
+
if (native === undefined && !definition)
|
|
209
|
+
error(
|
|
210
|
+
'Invalid type ' + quote(type) + ' for field ' + quote(field.name),
|
|
211
|
+
field.line,
|
|
212
|
+
field.column
|
|
213
|
+
)
|
|
214
|
+
return {
|
|
215
|
+
name: field.name,
|
|
216
|
+
id: field.value,
|
|
217
|
+
isArray: field.isArray,
|
|
218
|
+
isDeprecated: field.isDeprecated,
|
|
219
|
+
type: native ?? KIND_TYPES[(definition as Definition).kind],
|
|
220
|
+
definition: definition ? (compiled.get(definition.name) ?? null) : null,
|
|
221
|
+
kept: kept === undefined || kept.includes(field.name)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export function compileSchemaTables(schema: Schema, headers?: HeaderFields): CompiledSchema {
|
|
226
|
+
const definitions = compileDefinitions(schema, headers)
|
|
227
|
+
const byName = new Map(definitions.map((definition) => [definition.name, definition]))
|
|
228
|
+
/**
|
|
229
|
+
* How many whole decodes are running. A message type can nest itself, as instance overrides
|
|
230
|
+
* are records too, and a message read whole is read whole all the way down.
|
|
231
|
+
*/
|
|
232
|
+
let wholeDepth = 0
|
|
233
|
+
const wholeDecoders = new WeakMap<CompiledDefinition, (bb: ByteBuffer) => RuntimeMessage>()
|
|
234
|
+
|
|
235
|
+
const definition = (name: string): CompiledDefinition => {
|
|
236
|
+
const found = byName.get(name)
|
|
237
|
+
if (!found) error('Invalid type ' + quote(name), 0, 0)
|
|
238
|
+
return found
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const readValue = (field: CompiledField, bb: ByteBuffer): RuntimeValue => {
|
|
242
|
+
switch (field.type) {
|
|
243
|
+
case TYPE.BOOL:
|
|
244
|
+
return !!bb.readByte()
|
|
245
|
+
case TYPE.BYTE:
|
|
246
|
+
return bb.readByte()
|
|
247
|
+
case TYPE.INT:
|
|
248
|
+
return bb.readVarInt()
|
|
249
|
+
case TYPE.UINT:
|
|
250
|
+
return bb.readVarUint()
|
|
251
|
+
case TYPE.FLOAT:
|
|
252
|
+
return bb.readVarFloat()
|
|
253
|
+
case TYPE.STRING:
|
|
254
|
+
return bb.readString()
|
|
255
|
+
case TYPE.INT64:
|
|
256
|
+
return bb.readVarInt64()
|
|
257
|
+
case TYPE.UINT64:
|
|
258
|
+
return bb.readVarUint64()
|
|
259
|
+
case TYPE.ENUM:
|
|
260
|
+
return (field.definition as CompiledDefinition).names[bb.readVarUint()] as RuntimeValue
|
|
261
|
+
default:
|
|
262
|
+
return decode(field.definition as CompiledDefinition, bb)
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const read = (field: CompiledField, bb: ByteBuffer): RuntimeValue => {
|
|
267
|
+
if (!field.isArray) return readValue(field, bb)
|
|
268
|
+
if (field.type === TYPE.BYTE) return bb.readByteArray()
|
|
269
|
+
const length = bb.readVarUint()
|
|
270
|
+
const values: RuntimeValue[] = Array.from({ length })
|
|
271
|
+
for (let i = 0; i < length; i++) values[i] = readValue(field, bb)
|
|
272
|
+
return values
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const skipValue = (field: CompiledField, bb: ByteBuffer): void => {
|
|
276
|
+
switch (field.type) {
|
|
277
|
+
case TYPE.BOOL:
|
|
278
|
+
case TYPE.BYTE:
|
|
279
|
+
bb.readByte()
|
|
280
|
+
return
|
|
281
|
+
case TYPE.INT:
|
|
282
|
+
case TYPE.UINT:
|
|
283
|
+
case TYPE.ENUM:
|
|
284
|
+
bb.readVarUint()
|
|
285
|
+
return
|
|
286
|
+
case TYPE.FLOAT:
|
|
287
|
+
bb.readVarFloat()
|
|
288
|
+
return
|
|
289
|
+
case TYPE.STRING:
|
|
290
|
+
bb.skipString()
|
|
291
|
+
return
|
|
292
|
+
case TYPE.INT64:
|
|
293
|
+
case TYPE.UINT64:
|
|
294
|
+
bb.readVarUint64()
|
|
295
|
+
return
|
|
296
|
+
case TYPE.STRUCT:
|
|
297
|
+
for (const inner of (field.definition as CompiledDefinition).fields) skip(inner, bb)
|
|
298
|
+
return
|
|
299
|
+
default: {
|
|
300
|
+
const byId = (field.definition as CompiledDefinition).byId
|
|
301
|
+
for (let id = bb.readVarUint(); id !== 0; id = bb.readVarUint())
|
|
302
|
+
skip(byId[id] ?? invalidMessage(), bb)
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const skip = (field: CompiledField, bb: ByteBuffer): void => {
|
|
308
|
+
if (!field.isArray) {
|
|
309
|
+
skipValue(field, bb)
|
|
310
|
+
return
|
|
311
|
+
}
|
|
312
|
+
if (field.type === TYPE.BYTE) {
|
|
313
|
+
bb.skipByteArray()
|
|
314
|
+
return
|
|
315
|
+
}
|
|
316
|
+
for (let length = bb.readVarUint(); length > 0; length--) skipValue(field, bb)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const readInto = (field: CompiledField, bb: ByteBuffer, result: RuntimeMessage): void => {
|
|
320
|
+
if (field.isDeprecated) skip(field, bb)
|
|
321
|
+
else result[field.name] = read(field, bb)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const decode = (target: CompiledDefinition, bb: ByteBuffer): RuntimeMessage => {
|
|
325
|
+
const result: RuntimeMessage = {}
|
|
326
|
+
if (target.kind === 'STRUCT') {
|
|
327
|
+
for (const field of target.fields) readInto(field, bb, result)
|
|
328
|
+
return result
|
|
329
|
+
}
|
|
330
|
+
const asHeader = target.header && wholeDepth === 0
|
|
331
|
+
if (asHeader) {
|
|
332
|
+
hide(result, HEADER_BYTES, bb.bytes)
|
|
333
|
+
hide(result, HEADER_START, bb.offset)
|
|
334
|
+
hide(result, HEADER_WHOLE, wholeDecoder(target))
|
|
335
|
+
}
|
|
336
|
+
const byId = target.byId
|
|
337
|
+
for (let id = bb.readVarUint(); id !== 0; id = bb.readVarUint()) {
|
|
338
|
+
const field = byId[id] ?? invalidMessage()
|
|
339
|
+
if (asHeader && !field.kept) skip(field, bb)
|
|
340
|
+
else readInto(field, bb, result)
|
|
341
|
+
}
|
|
342
|
+
return result
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const wholeDecoder = (target: CompiledDefinition): ((bb: ByteBuffer) => RuntimeMessage) => {
|
|
346
|
+
let whole = wholeDecoders.get(target)
|
|
347
|
+
if (!whole) {
|
|
348
|
+
whole = (bb) => {
|
|
349
|
+
wholeDepth++
|
|
350
|
+
try {
|
|
351
|
+
return decode(target, bb)
|
|
352
|
+
} finally {
|
|
353
|
+
wholeDepth--
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
wholeDecoders.set(target, whole)
|
|
357
|
+
}
|
|
358
|
+
return whole
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const writeValue = (field: CompiledField, value: RuntimeValue, bb: ByteBuffer): void => {
|
|
362
|
+
switch (field.type) {
|
|
363
|
+
case TYPE.BOOL:
|
|
364
|
+
case TYPE.BYTE:
|
|
365
|
+
bb.writeByte(value as number)
|
|
366
|
+
return
|
|
367
|
+
case TYPE.INT:
|
|
368
|
+
bb.writeVarInt(value as number)
|
|
369
|
+
return
|
|
370
|
+
case TYPE.UINT:
|
|
371
|
+
bb.writeVarUint(value as number)
|
|
372
|
+
return
|
|
373
|
+
case TYPE.FLOAT:
|
|
374
|
+
bb.writeVarFloat(value as number)
|
|
375
|
+
return
|
|
376
|
+
case TYPE.STRING:
|
|
377
|
+
bb.writeString(value as string)
|
|
378
|
+
return
|
|
379
|
+
case TYPE.INT64:
|
|
380
|
+
bb.writeVarInt64(value as bigint | string)
|
|
381
|
+
return
|
|
382
|
+
case TYPE.UINT64:
|
|
383
|
+
bb.writeVarUint64(value as bigint | string)
|
|
384
|
+
return
|
|
385
|
+
case TYPE.ENUM: {
|
|
386
|
+
const target = field.definition as CompiledDefinition
|
|
387
|
+
const encoded = target.values.get(value as string)
|
|
388
|
+
if (encoded === undefined)
|
|
389
|
+
throw new Error(
|
|
390
|
+
'Invalid value ' + JSON.stringify(value) + ' for enum ' + quote(target.name)
|
|
391
|
+
)
|
|
392
|
+
bb.writeVarUint(encoded)
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
default:
|
|
396
|
+
encode(field.definition as CompiledDefinition, value as RuntimeMessage, bb)
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const write = (field: CompiledField, value: RuntimeValue, bb: ByteBuffer): void => {
|
|
401
|
+
if (!field.isArray) {
|
|
402
|
+
writeValue(field, value, bb)
|
|
403
|
+
return
|
|
404
|
+
}
|
|
405
|
+
if (field.type === TYPE.BYTE) {
|
|
406
|
+
bb.writeByteArray(value as Uint8Array)
|
|
407
|
+
return
|
|
408
|
+
}
|
|
409
|
+
const values = value as RuntimeValue[]
|
|
410
|
+
bb.writeVarUint(values.length)
|
|
411
|
+
for (const item of values) writeValue(field, item, bb)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const encode = (target: CompiledDefinition, message: RuntimeMessage, bb: ByteBuffer): void => {
|
|
415
|
+
const isMessage = target.kind === 'MESSAGE'
|
|
416
|
+
for (const field of target.fields) {
|
|
417
|
+
if (field.isDeprecated) continue
|
|
418
|
+
const value = message[field.name]
|
|
419
|
+
if (value != null) {
|
|
420
|
+
if (isMessage) bb.writeVarUint(field.id)
|
|
421
|
+
write(field, value, bb)
|
|
422
|
+
} else if (!isMessage) {
|
|
423
|
+
throw new Error('Missing required field ' + quote(field.name))
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (isMessage) bb.writeVarUint(0)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return { definitions, definition, read, skip, write, decode, encode }
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Interprets a schema through its compiled tables instead of generating and `eval`-ing a
|
|
434
|
+
* per-schema decoder/encoder (as `compileSchemaJS` + `new Function` do). A `new Function(...)`-
|
|
435
|
+
* built codec is indistinguishable from `eval` to a CSP: embedders that run under `script-src`
|
|
436
|
+
* without `unsafe-eval` (e.g. a sandboxed plugin host) cannot call `compileSchema` at all
|
|
437
|
+
* otherwise. The tables produce byte-identical output to the generated code: same field order,
|
|
438
|
+
* same tag/no-tag rules for MESSAGE vs STRUCT, same deprecated-field skip behavior.
|
|
439
|
+
*/
|
|
440
|
+
export function compileSchemaRuntime(schema: Schema, headers?: HeaderFields): RuntimeCodec {
|
|
441
|
+
const compiled = compileSchemaTables(schema, headers)
|
|
442
|
+
const codec: RuntimeCodec = { ByteBuffer, schema: compiled }
|
|
443
|
+
for (const definition of compiled.definitions) {
|
|
444
|
+
if (definition.kind === 'ENUM') {
|
|
445
|
+
const value: RuntimeEnum = {}
|
|
446
|
+
for (const [name, member] of definition.values) {
|
|
447
|
+
value[name] = member
|
|
448
|
+
value[member] = name
|
|
449
|
+
}
|
|
450
|
+
codec[definition.name] = value
|
|
451
|
+
continue
|
|
452
|
+
}
|
|
453
|
+
codec['decode' + definition.name] = (bb: ByteBuffer | Uint8Array): RuntimeMessage =>
|
|
454
|
+
compiled.decode(definition, bb instanceof codec.ByteBuffer ? bb : new codec.ByteBuffer(bb))
|
|
455
|
+
codec['encode' + definition.name] = (
|
|
456
|
+
message: RuntimeMessage,
|
|
457
|
+
bb?: ByteBuffer
|
|
458
|
+
): Uint8Array | undefined => {
|
|
459
|
+
const buffer = bb ?? new codec.ByteBuffer()
|
|
460
|
+
compiled.encode(definition, message, buffer)
|
|
461
|
+
return bb ? undefined : buffer.toUint8Array()
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return codec
|
|
465
|
+
}
|