@zlink-systems/stream-wire 0.33.0 → 0.35.0

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.
@@ -154,6 +154,9 @@ var ZlinkStreamCloseReasonCode = /* @__PURE__ */ ((ZlinkStreamCloseReasonCode2)
154
154
  return ZlinkStreamCloseReasonCode2;
155
155
  })(ZlinkStreamCloseReasonCode || {});
156
156
  var unicodeWhiteSpaceOnly = /^\p{White_Space}*$/u;
157
+ var utf8Encoder = new TextEncoder();
158
+ var utf8Decoder = new TextDecoder();
159
+ var controlTextDecoder = new TextDecoder("utf-8", { fatal: true });
157
160
  var validMessageKinds = new Set(
158
161
  Object.values(ZlinkStreamMessageKind).filter((value) => typeof value === "number")
159
162
  );
@@ -270,7 +273,7 @@ function decodeStreamWireSessionClosing(payload) {
270
273
  return { closeReason, diagnostic };
271
274
  }
272
275
  function decodeControlText(value) {
273
- return new TextDecoder("utf-8", { fatal: true }).decode(value);
276
+ return controlTextDecoder.decode(value);
274
277
  }
275
278
  function splitStreamWireFrames(chunk) {
276
279
  if (chunk.length === 0) throw new Error("Stream frame prefix is incomplete.");
@@ -612,10 +615,10 @@ function lz4UnpicklePayload2(payload, maxDecompressedSize) {
612
615
  return lz4UnpicklePayload(payload, maxDecompressedSize ?? defaultMaxDecompressedPayloadSize);
613
616
  }
614
617
  function utf8Encode(value) {
615
- return new TextEncoder().encode(value);
618
+ return utf8Encoder.encode(value);
616
619
  }
617
620
  function utf8Decode(value) {
618
- return new TextDecoder().decode(value);
621
+ return utf8Decoder.decode(value);
619
622
  }
620
623
  function decodeStreamWireHeaderMetadata(header, offset) {
621
624
  if (header.length - offset < UINT16_BYTES) {
package/dist/index.js CHANGED
@@ -65,6 +65,10 @@ var ZlinkStreamCloseReasonCode;
65
65
  ZlinkStreamCloseReasonCode[ZlinkStreamCloseReasonCode["TransportError"] = 6] = "TransportError";
66
66
  })(ZlinkStreamCloseReasonCode || (exports.ZlinkStreamCloseReasonCode = ZlinkStreamCloseReasonCode = {}));
67
67
  const unicodeWhiteSpaceOnly = /^\p{White_Space}*$/u;
68
+ const utf8Encoder = new TextEncoder();
69
+ // Each non-streaming decode resets its state, including after invalid input.
70
+ const utf8Decoder = new TextDecoder();
71
+ const controlTextDecoder = new TextDecoder('utf-8', { fatal: true });
68
72
  const validMessageKinds = new Set(Object.values(ZlinkStreamMessageKind).filter((value) => typeof value === 'number'));
69
73
  const validCodecs = new Set(Object.values(ZlinkStreamCodec).filter((value) => typeof value === 'number'));
70
74
  function isStreamWireMessageKind(value) {
@@ -185,7 +189,7 @@ function decodeStreamWireSessionClosing(payload) {
185
189
  return { closeReason, diagnostic };
186
190
  }
187
191
  function decodeControlText(value) {
188
- return new TextDecoder('utf-8', { fatal: true }).decode(value);
192
+ return controlTextDecoder.decode(value);
189
193
  }
190
194
  function splitStreamWireFrames(chunk) {
191
195
  if (chunk.length === 0)
@@ -549,10 +553,10 @@ function lz4UnpicklePayload(payload, maxDecompressedSize) {
549
553
  return (0, lz4_pickle_1.lz4UnpicklePayload)(payload, maxDecompressedSize ?? lz4_pickle_1.defaultMaxDecompressedPayloadSize);
550
554
  }
551
555
  function utf8Encode(value) {
552
- return new TextEncoder().encode(value);
556
+ return utf8Encoder.encode(value);
553
557
  }
554
558
  function utf8Decode(value) {
555
- return new TextDecoder().decode(value);
559
+ return utf8Decoder.decode(value);
556
560
  }
557
561
  function decodeStreamWireHeaderMetadata(header, offset) {
558
562
  if (header.length - offset < UINT16_BYTES) {
@@ -61,7 +61,7 @@ function parseFrameworkJsonV1(text, options = {}, schema, reviver, decodeBytes =
61
61
  if (text.charCodeAt(0) === 0xfeff) {
62
62
  throw new SyntaxError('framework-json-v1 does not allow a UTF-8 BOM.');
63
63
  }
64
- new JsonPropertyScanner(text, options).scan();
64
+ new JsonPropertyScanner(text, options).scan(schema);
65
65
  const parsed = JSON.parse(text, reviver);
66
66
  return schema === undefined
67
67
  ? parsed
@@ -139,21 +139,26 @@ function validateFrameworkJsonV1Value(value, schema, path, decode, decodeBytes =
139
139
  if (!isJsonObject(value))
140
140
  schemaFailure(path, 'an object');
141
141
  const input = value;
142
- for (const name of schema.required) {
142
+ const { properties, required } = schema;
143
+ for (let index = 0; index < required.length; index++) {
144
+ const name = required[index];
143
145
  if (!Object.prototype.hasOwnProperty.call(input, name)) {
144
146
  throw new TypeError(`framework-json-v1 required property '${path}.${name}' is missing.`);
145
147
  }
146
148
  }
147
149
  const result = {};
148
- for (const [name, item] of Object.entries(input)) {
149
- const propertySchema = schema.properties[name];
150
+ const entries = Object.entries(input);
151
+ for (let index = 0; index < entries.length; index++) {
152
+ const entry = entries[index];
153
+ const name = entry[0];
154
+ const propertySchema = properties[name];
150
155
  if (propertySchema === undefined) {
151
156
  if (schema.additionalProperties === false) {
152
157
  throw new TypeError(`framework-json-v1 property '${path}.${name}' is not allowed.`);
153
158
  }
154
159
  continue;
155
160
  }
156
- result[name] = validateFrameworkJsonV1Value(item, propertySchema, `${path}.${name}`, decode, decodeBytes);
161
+ result[name] = validateFrameworkJsonV1Value(entry[1], propertySchema, `${path}.${name}`, decode, decodeBytes);
157
162
  }
158
163
  return decode ? result : value;
159
164
  }
@@ -176,6 +181,21 @@ function isJsonObject(value) {
176
181
  function schemaFailure(path, expected) {
177
182
  throw new TypeError(`framework-json-v1 value '${path}' must be ${expected}.`);
178
183
  }
184
+ const propertyPlans = new WeakMap();
185
+ function knownProperties(properties) {
186
+ const cached = propertyPlans.get(properties);
187
+ if (cached !== undefined)
188
+ return cached;
189
+ const plan = new Map();
190
+ let index = 0;
191
+ for (const [name, schema] of Object.entries(properties)) {
192
+ plan.set(name, { schema, mask: index >= 32 ? 0 : 1 << index });
193
+ index++;
194
+ }
195
+ if (Object.isFrozen(properties))
196
+ propertyPlans.set(properties, plan);
197
+ return plan;
198
+ }
179
199
  class JsonPropertyScanner {
180
200
  text;
181
201
  options;
@@ -187,22 +207,24 @@ class JsonPropertyScanner {
187
207
  const escape = text.indexOf('\\');
188
208
  this.firstEscape = escape === -1 ? text.length : escape;
189
209
  }
190
- scan() {
191
- this.skipWhitespace();
192
- this.scanValue([]);
210
+ scan(schema) {
211
+ this.scanValue(this.options.onDuplicateProperty === undefined ? undefined : [], schema);
193
212
  this.skipWhitespace();
194
213
  if (this.index !== this.text.length)
195
214
  this.fail('trailing data');
196
215
  }
197
- scanValue(path) {
216
+ scanValue(path, schema) {
198
217
  this.skipWhitespace();
199
218
  const token = this.text[this.index];
200
- if (token === '{')
201
- return this.scanObject(path);
202
- if (token === '[')
203
- return this.scanArray(path);
219
+ if (token === '{' || token === '[') {
220
+ while (schema?.type === 'nullable')
221
+ schema = schema.value;
222
+ return token === '{'
223
+ ? this.scanObject(path, schema)
224
+ : this.scanArray(path, schema?.type === 'array' ? schema.items : undefined);
225
+ }
204
226
  if (token === '"') {
205
- this.scanString();
227
+ this.scanString(false);
206
228
  return;
207
229
  }
208
230
  if (token === 't')
@@ -213,28 +235,41 @@ class JsonPropertyScanner {
213
235
  return this.scanLiteral('null');
214
236
  this.scanNumber();
215
237
  }
216
- scanObject(path) {
238
+ scanObject(path, schema) {
217
239
  this.index++;
218
240
  this.skipWhitespace();
219
241
  if (this.text[this.index] === '}') {
220
242
  this.index++;
221
243
  return;
222
244
  }
223
- const properties = new Set();
245
+ const plan = schema?.type === 'object' ? knownProperties(schema.properties) : undefined;
246
+ const recordValue = schema?.type === 'record' ? schema.values : undefined;
247
+ let seen = 0;
248
+ let other;
224
249
  for (;;) {
225
- this.skipWhitespace();
226
250
  if (this.text[this.index] !== '"')
227
251
  this.fail('object property name');
228
- const property = this.scanString();
229
- if (properties.has(property)) {
252
+ const property = this.scanString(true);
253
+ const known = plan?.get(property);
254
+ const mask = known?.mask ?? 0;
255
+ let duplicate;
256
+ if (mask === 0) {
257
+ other ??= new Set();
258
+ duplicate = other.has(property);
259
+ other.add(property);
260
+ }
261
+ else {
262
+ duplicate = (seen & mask) !== 0;
263
+ seen |= mask;
264
+ }
265
+ if (duplicate) {
230
266
  if (this.options.onDuplicateProperty !== undefined) {
231
- this.options.onDuplicateProperty(property, path);
267
+ this.options.onDuplicateProperty(property, path ?? []);
232
268
  }
233
269
  else {
234
270
  throw new SyntaxError(`framework-json-v1 duplicate property '${property}'.`);
235
271
  }
236
272
  }
237
- properties.add(property);
238
273
  if (this.options.rejectPropertyName?.(property) === true) {
239
274
  throw new SyntaxError(`framework-json-v1 property '${property}' is not allowed.`);
240
275
  }
@@ -242,16 +277,17 @@ class JsonPropertyScanner {
242
277
  if (this.text[this.index] !== ':')
243
278
  this.fail("':' after object property");
244
279
  this.index++;
245
- this.scanValue(this.options.onDuplicateProperty === undefined ? path : [...path, property]);
280
+ this.scanValue(path === undefined ? undefined : [...path, property], known?.schema ?? recordValue);
246
281
  this.skipWhitespace();
247
282
  const delimiter = this.text[this.index++];
248
283
  if (delimiter === '}')
249
284
  return;
250
285
  if (delimiter !== ',')
251
286
  this.fail("',' or '}' after object property");
287
+ this.skipWhitespace();
252
288
  }
253
289
  }
254
- scanArray(path) {
290
+ scanArray(path, itemSchema) {
255
291
  this.index++;
256
292
  this.skipWhitespace();
257
293
  if (this.text[this.index] === ']') {
@@ -259,7 +295,7 @@ class JsonPropertyScanner {
259
295
  return;
260
296
  }
261
297
  for (;;) {
262
- this.scanValue(this.options.onDuplicateProperty === undefined ? path : [...path, '[]']);
298
+ this.scanValue(path === undefined ? undefined : [...path, '[]'], itemSchema);
263
299
  this.skipWhitespace();
264
300
  const delimiter = this.text[this.index++];
265
301
  if (delimiter === ']')
@@ -268,7 +304,7 @@ class JsonPropertyScanner {
268
304
  this.fail("',' or ']' after array item");
269
305
  }
270
306
  }
271
- scanString() {
307
+ scanString(readProperty) {
272
308
  const text = this.text;
273
309
  const start = this.index;
274
310
  let index = start + 1;
@@ -277,7 +313,7 @@ class JsonPropertyScanner {
277
313
  const end = text.indexOf('"', index);
278
314
  if (end !== -1 && end < this.firstEscape) {
279
315
  this.index = end + 1;
280
- return text.slice(index, end);
316
+ return readProperty ? text.slice(index, end) : undefined;
281
317
  }
282
318
  let sawEscape = false;
283
319
  for (;;) {
@@ -288,11 +324,9 @@ class JsonPropertyScanner {
288
324
  const character = text[index++];
289
325
  if (character === '"') {
290
326
  this.index = index;
291
- // Without escapes the token content is verbatim; the outer
292
- // JSON.parse pass still rejects any raw control characters.
293
- return sawEscape
294
- ? JSON.parse(text.slice(start, index))
295
- : text.slice(start + 1, index - 1);
327
+ if (sawEscape)
328
+ return JSON.parse(text.slice(start, index));
329
+ return readProperty ? text.slice(start + 1, index - 1) : undefined;
296
330
  }
297
331
  if (character === '\\') {
298
332
  sawEscape = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlink-systems/stream-wire",
3
- "version": "0.33.0",
3
+ "version": "0.35.0",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",