@zlink-systems/stream-wire 0.32.1 → 0.33.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.
@@ -4,5 +4,7 @@ export interface FrameworkJsonParseOptions {
4
4
  readonly rejectPropertyName?: (name: string) => boolean;
5
5
  readonly onDuplicateProperty?: (name: string, path: readonly string[]) => void;
6
6
  }
7
- export declare function stringifyFrameworkJsonV1(value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown): string;
8
- export declare function parseFrameworkJsonV1(text: string, options?: FrameworkJsonParseOptions, schema?: ZLinkJsonSchema, reviver?: (this: unknown, key: string, value: unknown) => unknown): unknown;
7
+ export declare const stringifyFrameworkJsonV1: (value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown) => string;
8
+ /** @internal Configures byte conversion once while keeping JSON policy shared. */
9
+ export declare function createFrameworkJsonV1Stringifier(encodeBytes: (bytes: Uint8Array) => string): (value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown) => string;
10
+ export declare function parseFrameworkJsonV1(text: string, options?: FrameworkJsonParseOptions, schema?: ZLinkJsonSchema, reviver?: (this: unknown, key: string, value: unknown) => unknown, decodeBytes?: (value: string) => Uint8Array): unknown;
@@ -4,5 +4,7 @@ export interface FrameworkJsonParseOptions {
4
4
  readonly rejectPropertyName?: (name: string) => boolean;
5
5
  readonly onDuplicateProperty?: (name: string, path: readonly string[]) => void;
6
6
  }
7
- export declare function stringifyFrameworkJsonV1(value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown): string;
8
- export declare function parseFrameworkJsonV1(text: string, options?: FrameworkJsonParseOptions, schema?: ZLinkJsonSchema, reviver?: (this: unknown, key: string, value: unknown) => unknown): unknown;
7
+ export declare const stringifyFrameworkJsonV1: (value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown) => string;
8
+ /** @internal Configures byte conversion once while keeping JSON policy shared. */
9
+ export declare function createFrameworkJsonV1Stringifier(encodeBytes: (bytes: Uint8Array) => string): (value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown) => string;
10
+ export declare function parseFrameworkJsonV1(text: string, options?: FrameworkJsonParseOptions, schema?: ZLinkJsonSchema, reviver?: (this: unknown, key: string, value: unknown) => unknown, decodeBytes?: (value: string) => Uint8Array): unknown;
@@ -1,37 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FRAMEWORK_JSON_V1_PROFILE = void 0;
4
- exports.stringifyFrameworkJsonV1 = stringifyFrameworkJsonV1;
3
+ exports.stringifyFrameworkJsonV1 = exports.FRAMEWORK_JSON_V1_PROFILE = void 0;
4
+ exports.createFrameworkJsonV1Stringifier = createFrameworkJsonV1Stringifier;
5
5
  exports.parseFrameworkJsonV1 = parseFrameworkJsonV1;
6
6
  exports.FRAMEWORK_JSON_V1_PROFILE = 'framework-json-v1';
7
7
  const SIGNED_64_MIN = -(1n << 63n);
8
8
  const UNSIGNED_64_MAX = (1n << 64n) - 1n;
9
- function stringifyFrameworkJsonV1(value, schema, replacer) {
10
- if (value === undefined || typeof value === 'function' || typeof value === 'symbol') {
11
- throw new TypeError('framework-json-v1 payload is not a JSON value.');
9
+ exports.stringifyFrameworkJsonV1 = createFrameworkJsonV1Stringifier(encodeBase64);
10
+ /** @internal Configures byte conversion once while keeping JSON policy shared. */
11
+ function createFrameworkJsonV1Stringifier(encodeBytes) {
12
+ // JSON.stringify has already called toJSON. Read the raw source from the
13
+ // holder, including the root wrapper, before applying the shared policy.
14
+ function frameworkJsonV1Replacer(key, item) {
15
+ return frameworkJsonV1Value(this[key], item, encodeBytes);
12
16
  }
13
- const encoded = replacer === undefined
14
- ? JSON.stringify(value, frameworkJsonV1Replacer)
15
- : stringifyWithReplacer(value, replacer);
16
- if (schema !== undefined) {
17
- validateFrameworkJsonV1Value(JSON.parse(encoded), schema, '$', false);
18
- }
19
- return encoded;
17
+ return function stringifyFrameworkJsonV1(value, schema, replacer) {
18
+ if (value === undefined || typeof value === 'function' || typeof value === 'symbol') {
19
+ throw new TypeError('framework-json-v1 payload is not a JSON value.');
20
+ }
21
+ const encoded = replacer === undefined
22
+ ? JSON.stringify(value, frameworkJsonV1Replacer)
23
+ : stringifyWithReplacer(value, replacer, encodeBytes);
24
+ if (schema !== undefined) {
25
+ validateFrameworkJsonV1Value(JSON.parse(encoded), schema, '$', false);
26
+ }
27
+ return encoded;
28
+ };
20
29
  }
21
30
  // Keep custom callback captures off the default encoding path.
22
- function stringifyWithReplacer(value, replacer) {
31
+ function stringifyWithReplacer(value, replacer, encodeBytes) {
23
32
  return JSON.stringify(value, function (key, item) {
24
33
  const transformed = replacer.call(this, key, item);
25
- return frameworkJsonV1Value(transformed, transformed);
34
+ return frameworkJsonV1Value(transformed, transformed, encodeBytes);
26
35
  });
27
36
  }
28
- // Module-level so JSON.stringify does not allocate a fresh closure per
29
- // encoded message. On the root call `this` is the {'': value} wrapper, so
30
- // this[key] resolves the raw (pre-toJSON) source for every key including ''.
31
- function frameworkJsonV1Replacer(key, item) {
32
- return frameworkJsonV1Value(this[key], item);
33
- }
34
- function frameworkJsonV1Value(source, item) {
37
+ function frameworkJsonV1Value(source, item, encodeBytes) {
35
38
  if (typeof source === 'number' && !Number.isFinite(source)) {
36
39
  throw new TypeError('framework-json-v1 only accepts finite JSON numbers.');
37
40
  }
@@ -42,7 +45,7 @@ function frameworkJsonV1Value(source, item) {
42
45
  return source.toString(10);
43
46
  }
44
47
  if (source instanceof Uint8Array) {
45
- return encodeBase64(source);
48
+ return encodeBytes(source);
46
49
  }
47
50
  if (source instanceof Date) {
48
51
  throw new TypeError('framework-json-v1 does not implicitly encode Date values.');
@@ -54,13 +57,15 @@ function frameworkJsonV1Value(source, item) {
54
57
  }
55
58
  return item;
56
59
  }
57
- function parseFrameworkJsonV1(text, options = {}, schema, reviver) {
60
+ function parseFrameworkJsonV1(text, options = {}, schema, reviver, decodeBytes = decodeBase64) {
58
61
  if (text.charCodeAt(0) === 0xfeff) {
59
62
  throw new SyntaxError('framework-json-v1 does not allow a UTF-8 BOM.');
60
63
  }
61
64
  new JsonPropertyScanner(text, options).scan();
62
65
  const parsed = JSON.parse(text, reviver);
63
- return schema === undefined ? parsed : validateFrameworkJsonV1Value(parsed, schema, '$', true);
66
+ return schema === undefined
67
+ ? parsed
68
+ : validateFrameworkJsonV1Value(parsed, schema, '$', true, decodeBytes);
64
69
  }
65
70
  // Sticky pattern shared across scans; matching happens synchronously so the
66
71
  // mutable lastIndex never crosses calls.
@@ -69,7 +74,7 @@ const BASE64_PATTERN = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/
69
74
  const INT32_MIN = -2_147_483_648;
70
75
  const INT32_MAX = 2_147_483_647;
71
76
  const UINT32_MAX = 4_294_967_295;
72
- function validateFrameworkJsonV1Value(value, schema, path, decode) {
77
+ function validateFrameworkJsonV1Value(value, schema, path, decode, decodeBytes = decodeBase64) {
73
78
  switch (schema.type) {
74
79
  case 'boolean':
75
80
  if (typeof value !== 'boolean')
@@ -105,8 +110,8 @@ function validateFrameworkJsonV1Value(value, schema, path, decode) {
105
110
  }
106
111
  if (!hasCanonicalBase64Padding(value))
107
112
  schemaFailure(path, 'canonical padded RFC 4648 base64');
108
- // decodeBase64 returns fresh storage owned by this value.
109
- return decode ? decodeBase64(value) : value;
113
+ // The selected decoder returns fresh storage owned by this value.
114
+ return decode ? decodeBytes(value) : value;
110
115
  }
111
116
  case 'enum':
112
117
  if (typeof value !== 'string' || !schema.names.includes(value)) {
@@ -116,17 +121,17 @@ function validateFrameworkJsonV1Value(value, schema, path, decode) {
116
121
  case 'nullable':
117
122
  return value === null
118
123
  ? null
119
- : validateFrameworkJsonV1Value(value, schema.value, path, decode);
124
+ : validateFrameworkJsonV1Value(value, schema.value, path, decode, decodeBytes);
120
125
  case 'array':
121
126
  if (!Array.isArray(value))
122
127
  schemaFailure(path, 'an array');
123
- return value.map((item, index) => validateFrameworkJsonV1Value(item, schema.items, `${path}[${index}]`, decode));
128
+ return value.map((item, index) => validateFrameworkJsonV1Value(item, schema.items, `${path}[${index}]`, decode, decodeBytes));
124
129
  case 'record': {
125
130
  if (!isJsonObject(value))
126
131
  schemaFailure(path, 'an object');
127
132
  const result = {};
128
133
  for (const [name, item] of Object.entries(value)) {
129
- result[name] = validateFrameworkJsonV1Value(item, schema.values, `${path}.${name}`, decode);
134
+ result[name] = validateFrameworkJsonV1Value(item, schema.values, `${path}.${name}`, decode, decodeBytes);
130
135
  }
131
136
  return decode ? result : value;
132
137
  }
@@ -148,7 +153,7 @@ function validateFrameworkJsonV1Value(value, schema, path, decode) {
148
153
  }
149
154
  continue;
150
155
  }
151
- result[name] = validateFrameworkJsonV1Value(item, propertySchema, `${path}.${name}`, decode);
156
+ result[name] = validateFrameworkJsonV1Value(item, propertySchema, `${path}.${name}`, decode, decodeBytes);
152
157
  }
153
158
  return decode ? result : value;
154
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zlink-systems/stream-wire",
3
- "version": "0.32.1",
3
+ "version": "0.33.0",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",