@zlink-systems/stream-wire 0.29.1 → 0.30.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 +201 -104
- package/dist/esm/internal/framework-json-v1.d.ts +7 -0
- package/dist/esm/internal/json-contract.d.ts +47 -0
- package/dist/esm/internal/json.d.ts +4 -0
- package/dist/esm/internal/packet-json-contract.d.ts +15 -0
- package/dist/internal/framework-json-v1.d.ts +7 -0
- package/dist/internal/framework-json-v1.js +319 -0
- package/dist/internal/json-contract.d.ts +47 -0
- package/dist/internal/json-contract.js +156 -0
- package/dist/internal/json.d.ts +4 -0
- package/dist/internal/json.js +20 -0
- package/dist/internal/packet-json-contract.d.ts +15 -0
- package/dist/internal/packet-json-contract.js +32 -0
- package/package.json +15 -3
package/LICENSE
CHANGED
|
@@ -1,105 +1,202 @@
|
|
|
1
|
-
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
1
|
|
|
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
|
-
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ZLinkJsonSchema } from './json-contract';
|
|
2
|
+
export declare const FRAMEWORK_JSON_V1_PROFILE = "framework-json-v1";
|
|
3
|
+
export interface FrameworkJsonParseOptions {
|
|
4
|
+
readonly rejectPropertyName?: (name: string) => boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function stringifyFrameworkJsonV1(value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown): string;
|
|
7
|
+
export declare function parseFrameworkJsonV1(text: string, options?: FrameworkJsonParseOptions, schema?: ZLinkJsonSchema, reviver?: (this: unknown, key: string, value: unknown) => unknown): unknown;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Describes the fields accepted by the default framework-json-v1 serializer. */
|
|
2
|
+
export type ZLinkJsonSchema = {
|
|
3
|
+
readonly type: 'boolean';
|
|
4
|
+
} | {
|
|
5
|
+
readonly type: 'string';
|
|
6
|
+
} | {
|
|
7
|
+
readonly type: 'number';
|
|
8
|
+
} | {
|
|
9
|
+
readonly type: 'int32';
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: 'uint32';
|
|
12
|
+
} | {
|
|
13
|
+
readonly type: 'int64';
|
|
14
|
+
} | {
|
|
15
|
+
readonly type: 'uint64';
|
|
16
|
+
} | {
|
|
17
|
+
readonly type: 'bytes';
|
|
18
|
+
} | {
|
|
19
|
+
readonly type: 'enum';
|
|
20
|
+
readonly names: readonly string[];
|
|
21
|
+
} | {
|
|
22
|
+
readonly type: 'nullable';
|
|
23
|
+
readonly value: ZLinkJsonSchema;
|
|
24
|
+
} | {
|
|
25
|
+
readonly type: 'array';
|
|
26
|
+
readonly items: ZLinkJsonSchema;
|
|
27
|
+
} | {
|
|
28
|
+
readonly type: 'record';
|
|
29
|
+
readonly values: ZLinkJsonSchema;
|
|
30
|
+
} | {
|
|
31
|
+
readonly type: 'object';
|
|
32
|
+
readonly properties: Readonly<Record<string, ZLinkJsonSchema>>;
|
|
33
|
+
readonly required: readonly string[];
|
|
34
|
+
/** Unknown properties are ignored during decoding unless this is false. */
|
|
35
|
+
readonly additionalProperties?: boolean;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Describes the request and optional reply fields carried under one packet
|
|
39
|
+
* name by the default JSON serializer.
|
|
40
|
+
*/
|
|
41
|
+
export interface ZLinkPacketJsonContract {
|
|
42
|
+
readonly payload: ZLinkJsonSchema;
|
|
43
|
+
readonly reply?: ZLinkJsonSchema;
|
|
44
|
+
}
|
|
45
|
+
export declare function defineZLinkPacketJsonContract(packetName: string, contract: ZLinkPacketJsonContract): ZLinkPacketJsonContract;
|
|
46
|
+
export declare function readZLinkPacketJsonContract(packetName: string): ZLinkPacketJsonContract | undefined;
|
|
47
|
+
export declare function isPrototypeKey(name: string): boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ZLinkPacketJsonContract } from './json-contract';
|
|
2
|
+
export declare const ZLINK_DECORATOR_METADATA: unique symbol;
|
|
3
|
+
export interface ZLinkDecoratorMetadata {
|
|
4
|
+
readonly kind: string;
|
|
5
|
+
readonly packetName?: string;
|
|
6
|
+
readonly groupName?: string;
|
|
7
|
+
readonly methodName?: string;
|
|
8
|
+
readonly meshName?: string;
|
|
9
|
+
readonly channelName?: string;
|
|
10
|
+
readonly topic?: string;
|
|
11
|
+
readonly jsonContract?: ZLinkPacketJsonContract;
|
|
12
|
+
}
|
|
13
|
+
export declare function readZLinkDecoratorMetadata(target: object): readonly ZLinkDecoratorMetadata[];
|
|
14
|
+
export declare function readFrameworkPacketJsonContract(type: object, explicitPacketName?: string): ZLinkPacketJsonContract | undefined;
|
|
15
|
+
export declare function normalizePacketName(value: unknown): string | undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ZLinkJsonSchema } from './json-contract';
|
|
2
|
+
export declare const FRAMEWORK_JSON_V1_PROFILE = "framework-json-v1";
|
|
3
|
+
export interface FrameworkJsonParseOptions {
|
|
4
|
+
readonly rejectPropertyName?: (name: string) => boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare function stringifyFrameworkJsonV1(value: unknown, schema?: ZLinkJsonSchema, replacer?: (this: unknown, key: string, value: unknown) => unknown): string;
|
|
7
|
+
export declare function parseFrameworkJsonV1(text: string, options?: FrameworkJsonParseOptions, schema?: ZLinkJsonSchema, reviver?: (this: unknown, key: string, value: unknown) => unknown): unknown;
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FRAMEWORK_JSON_V1_PROFILE = void 0;
|
|
4
|
+
exports.stringifyFrameworkJsonV1 = stringifyFrameworkJsonV1;
|
|
5
|
+
exports.parseFrameworkJsonV1 = parseFrameworkJsonV1;
|
|
6
|
+
exports.FRAMEWORK_JSON_V1_PROFILE = 'framework-json-v1';
|
|
7
|
+
const SIGNED_64_MIN = -(1n << 63n);
|
|
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.');
|
|
12
|
+
}
|
|
13
|
+
const encoded = JSON.stringify(value, replacer === undefined
|
|
14
|
+
? frameworkJsonV1Replacer
|
|
15
|
+
: function (key, item) {
|
|
16
|
+
const transformed = replacer.call(this, key, item);
|
|
17
|
+
return frameworkJsonV1Replacer.call({ [key]: transformed }, key, transformed);
|
|
18
|
+
});
|
|
19
|
+
if (schema !== undefined) {
|
|
20
|
+
validateFrameworkJsonV1Value(JSON.parse(encoded), schema, '$', false);
|
|
21
|
+
}
|
|
22
|
+
return encoded;
|
|
23
|
+
}
|
|
24
|
+
// Module-level so JSON.stringify does not allocate a fresh closure per
|
|
25
|
+
// encoded message. On the root call `this` is the {'': value} wrapper, so
|
|
26
|
+
// this[key] resolves the raw (pre-toJSON) source for every key including ''.
|
|
27
|
+
function frameworkJsonV1Replacer(key, item) {
|
|
28
|
+
const source = this[key];
|
|
29
|
+
if (typeof source === 'number' && !Number.isFinite(source)) {
|
|
30
|
+
throw new TypeError('framework-json-v1 only accepts finite JSON numbers.');
|
|
31
|
+
}
|
|
32
|
+
if (typeof source === 'bigint') {
|
|
33
|
+
if (source < SIGNED_64_MIN || source > UNSIGNED_64_MAX) {
|
|
34
|
+
throw new RangeError('framework-json-v1 64-bit integer is outside the supported range.');
|
|
35
|
+
}
|
|
36
|
+
return source.toString(10);
|
|
37
|
+
}
|
|
38
|
+
if (source instanceof Uint8Array) {
|
|
39
|
+
return encodeBase64(source);
|
|
40
|
+
}
|
|
41
|
+
if (source instanceof Date) {
|
|
42
|
+
throw new TypeError('framework-json-v1 does not implicitly encode Date values.');
|
|
43
|
+
}
|
|
44
|
+
if (typeof source === 'object' &&
|
|
45
|
+
source !== null &&
|
|
46
|
+
typeof source.toJSON === 'function') {
|
|
47
|
+
throw new TypeError('framework-json-v1 does not implicitly invoke custom toJSON methods.');
|
|
48
|
+
}
|
|
49
|
+
return item;
|
|
50
|
+
}
|
|
51
|
+
function parseFrameworkJsonV1(text, options = {}, schema, reviver) {
|
|
52
|
+
if (text.charCodeAt(0) === 0xfeff) {
|
|
53
|
+
throw new SyntaxError('framework-json-v1 does not allow a UTF-8 BOM.');
|
|
54
|
+
}
|
|
55
|
+
new JsonPropertyScanner(text, options).scan();
|
|
56
|
+
const parsed = JSON.parse(text, reviver);
|
|
57
|
+
return schema === undefined ? parsed : validateFrameworkJsonV1Value(parsed, schema, '$', true);
|
|
58
|
+
}
|
|
59
|
+
// Sticky pattern shared across scans; matching happens synchronously so the
|
|
60
|
+
// mutable lastIndex never crosses calls.
|
|
61
|
+
const NUMBER_TOKEN_PATTERN = /-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/uy;
|
|
62
|
+
const BASE64_PATTERN = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/u;
|
|
63
|
+
const INT32_MIN = -2_147_483_648;
|
|
64
|
+
const INT32_MAX = 2_147_483_647;
|
|
65
|
+
const UINT32_MAX = 4_294_967_295;
|
|
66
|
+
function validateFrameworkJsonV1Value(value, schema, path, decode) {
|
|
67
|
+
switch (schema.type) {
|
|
68
|
+
case 'boolean':
|
|
69
|
+
if (typeof value !== 'boolean')
|
|
70
|
+
schemaFailure(path, 'a boolean');
|
|
71
|
+
return value;
|
|
72
|
+
case 'string':
|
|
73
|
+
if (typeof value !== 'string')
|
|
74
|
+
schemaFailure(path, 'a string');
|
|
75
|
+
return value;
|
|
76
|
+
case 'number':
|
|
77
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
78
|
+
schemaFailure(path, 'a finite number');
|
|
79
|
+
return value;
|
|
80
|
+
case 'int32':
|
|
81
|
+
if (!Number.isInteger(value) ||
|
|
82
|
+
value < INT32_MIN ||
|
|
83
|
+
value > INT32_MAX) {
|
|
84
|
+
schemaFailure(path, 'an int32 JSON number');
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
case 'uint32':
|
|
88
|
+
if (!Number.isInteger(value) || value < 0 || value > UINT32_MAX) {
|
|
89
|
+
schemaFailure(path, 'a uint32 JSON number');
|
|
90
|
+
}
|
|
91
|
+
return value;
|
|
92
|
+
case 'int64':
|
|
93
|
+
return validateDecimalInteger(value, path, true, decode);
|
|
94
|
+
case 'uint64':
|
|
95
|
+
return validateDecimalInteger(value, path, false, decode);
|
|
96
|
+
case 'bytes': {
|
|
97
|
+
if (typeof value !== 'string' || !BASE64_PATTERN.test(value)) {
|
|
98
|
+
schemaFailure(path, 'padded RFC 4648 base64');
|
|
99
|
+
}
|
|
100
|
+
const bytes = decodeBase64(value);
|
|
101
|
+
if (encodeBase64(bytes) !== value)
|
|
102
|
+
schemaFailure(path, 'canonical padded RFC 4648 base64');
|
|
103
|
+
return decode ? Uint8Array.from(bytes) : value;
|
|
104
|
+
}
|
|
105
|
+
case 'enum':
|
|
106
|
+
if (typeof value !== 'string' || !schema.names.includes(value)) {
|
|
107
|
+
schemaFailure(path, `one of ${schema.names.join(', ')}`);
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
case 'nullable':
|
|
111
|
+
return value === null
|
|
112
|
+
? null
|
|
113
|
+
: validateFrameworkJsonV1Value(value, schema.value, path, decode);
|
|
114
|
+
case 'array':
|
|
115
|
+
if (!Array.isArray(value))
|
|
116
|
+
schemaFailure(path, 'an array');
|
|
117
|
+
return value.map((item, index) => validateFrameworkJsonV1Value(item, schema.items, `${path}[${index}]`, decode));
|
|
118
|
+
case 'record': {
|
|
119
|
+
if (!isJsonObject(value))
|
|
120
|
+
schemaFailure(path, 'an object');
|
|
121
|
+
const result = {};
|
|
122
|
+
for (const [name, item] of Object.entries(value)) {
|
|
123
|
+
result[name] = validateFrameworkJsonV1Value(item, schema.values, `${path}.${name}`, decode);
|
|
124
|
+
}
|
|
125
|
+
return decode ? result : value;
|
|
126
|
+
}
|
|
127
|
+
case 'object': {
|
|
128
|
+
if (!isJsonObject(value))
|
|
129
|
+
schemaFailure(path, 'an object');
|
|
130
|
+
const input = value;
|
|
131
|
+
for (const name of schema.required) {
|
|
132
|
+
if (!Object.prototype.hasOwnProperty.call(input, name)) {
|
|
133
|
+
throw new TypeError(`framework-json-v1 required property '${path}.${name}' is missing.`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const result = {};
|
|
137
|
+
for (const [name, item] of Object.entries(input)) {
|
|
138
|
+
const propertySchema = schema.properties[name];
|
|
139
|
+
if (propertySchema === undefined) {
|
|
140
|
+
if (schema.additionalProperties === false) {
|
|
141
|
+
throw new TypeError(`framework-json-v1 property '${path}.${name}' is not allowed.`);
|
|
142
|
+
}
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
result[name] = validateFrameworkJsonV1Value(item, propertySchema, `${path}.${name}`, decode);
|
|
146
|
+
}
|
|
147
|
+
return decode ? result : value;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function validateDecimalInteger(value, path, signed, decode) {
|
|
152
|
+
if (typeof value !== 'string' || !/^(?:0|[1-9][0-9]*|-[1-9][0-9]*)$/u.test(value)) {
|
|
153
|
+
schemaFailure(path, signed ? 'a canonical int64 decimal string' : 'a canonical uint64 decimal string');
|
|
154
|
+
}
|
|
155
|
+
const parsed = BigInt(value);
|
|
156
|
+
const minimum = signed ? SIGNED_64_MIN : 0n;
|
|
157
|
+
const maximum = signed ? (1n << 63n) - 1n : UNSIGNED_64_MAX;
|
|
158
|
+
if (parsed < minimum || parsed > maximum)
|
|
159
|
+
schemaFailure(path, signed ? 'an int64 value' : 'a uint64 value');
|
|
160
|
+
return decode ? parsed : value;
|
|
161
|
+
}
|
|
162
|
+
function isJsonObject(value) {
|
|
163
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
164
|
+
}
|
|
165
|
+
function schemaFailure(path, expected) {
|
|
166
|
+
throw new TypeError(`framework-json-v1 value '${path}' must be ${expected}.`);
|
|
167
|
+
}
|
|
168
|
+
class JsonPropertyScanner {
|
|
169
|
+
text;
|
|
170
|
+
options;
|
|
171
|
+
index = 0;
|
|
172
|
+
constructor(text, options) {
|
|
173
|
+
this.text = text;
|
|
174
|
+
this.options = options;
|
|
175
|
+
}
|
|
176
|
+
scan() {
|
|
177
|
+
this.skipWhitespace();
|
|
178
|
+
this.scanValue();
|
|
179
|
+
this.skipWhitespace();
|
|
180
|
+
if (this.index !== this.text.length)
|
|
181
|
+
this.fail('trailing data');
|
|
182
|
+
}
|
|
183
|
+
scanValue() {
|
|
184
|
+
this.skipWhitespace();
|
|
185
|
+
const token = this.text[this.index];
|
|
186
|
+
if (token === '{')
|
|
187
|
+
return this.scanObject();
|
|
188
|
+
if (token === '[')
|
|
189
|
+
return this.scanArray();
|
|
190
|
+
if (token === '"') {
|
|
191
|
+
this.scanString();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (token === 't')
|
|
195
|
+
return this.scanLiteral('true');
|
|
196
|
+
if (token === 'f')
|
|
197
|
+
return this.scanLiteral('false');
|
|
198
|
+
if (token === 'n')
|
|
199
|
+
return this.scanLiteral('null');
|
|
200
|
+
this.scanNumber();
|
|
201
|
+
}
|
|
202
|
+
scanObject() {
|
|
203
|
+
this.index++;
|
|
204
|
+
this.skipWhitespace();
|
|
205
|
+
if (this.text[this.index] === '}') {
|
|
206
|
+
this.index++;
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
const properties = new Set();
|
|
210
|
+
for (;;) {
|
|
211
|
+
this.skipWhitespace();
|
|
212
|
+
if (this.text[this.index] !== '"')
|
|
213
|
+
this.fail('object property name');
|
|
214
|
+
const property = this.scanString();
|
|
215
|
+
if (properties.has(property)) {
|
|
216
|
+
throw new SyntaxError(`framework-json-v1 duplicate property '${property}'.`);
|
|
217
|
+
}
|
|
218
|
+
properties.add(property);
|
|
219
|
+
if (this.options.rejectPropertyName?.(property) === true) {
|
|
220
|
+
throw new SyntaxError(`framework-json-v1 property '${property}' is not allowed.`);
|
|
221
|
+
}
|
|
222
|
+
this.skipWhitespace();
|
|
223
|
+
if (this.text[this.index] !== ':')
|
|
224
|
+
this.fail("':' after object property");
|
|
225
|
+
this.index++;
|
|
226
|
+
this.scanValue();
|
|
227
|
+
this.skipWhitespace();
|
|
228
|
+
const delimiter = this.text[this.index++];
|
|
229
|
+
if (delimiter === '}')
|
|
230
|
+
return;
|
|
231
|
+
if (delimiter !== ',')
|
|
232
|
+
this.fail("',' or '}' after object property");
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
scanArray() {
|
|
236
|
+
this.index++;
|
|
237
|
+
this.skipWhitespace();
|
|
238
|
+
if (this.text[this.index] === ']') {
|
|
239
|
+
this.index++;
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
for (;;) {
|
|
243
|
+
this.scanValue();
|
|
244
|
+
this.skipWhitespace();
|
|
245
|
+
const delimiter = this.text[this.index++];
|
|
246
|
+
if (delimiter === ']')
|
|
247
|
+
return;
|
|
248
|
+
if (delimiter !== ',')
|
|
249
|
+
this.fail("',' or ']' after array item");
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
scanString() {
|
|
253
|
+
const start = this.index++;
|
|
254
|
+
let sawEscape = false;
|
|
255
|
+
for (;;) {
|
|
256
|
+
if (this.index >= this.text.length)
|
|
257
|
+
this.fail('unterminated string');
|
|
258
|
+
const character = this.text[this.index++];
|
|
259
|
+
if (character === '"') {
|
|
260
|
+
// Without escapes the token content is verbatim; the outer
|
|
261
|
+
// JSON.parse pass still rejects any raw control characters.
|
|
262
|
+
return sawEscape
|
|
263
|
+
? JSON.parse(this.text.slice(start, this.index))
|
|
264
|
+
: this.text.slice(start + 1, this.index - 1);
|
|
265
|
+
}
|
|
266
|
+
if (character === '\\') {
|
|
267
|
+
sawEscape = true;
|
|
268
|
+
if (this.index >= this.text.length)
|
|
269
|
+
this.fail('unterminated escape');
|
|
270
|
+
const escaped = this.text[this.index++];
|
|
271
|
+
if (escaped === 'u')
|
|
272
|
+
this.index += 4;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
scanLiteral(value) {
|
|
277
|
+
if (!this.text.startsWith(value, this.index))
|
|
278
|
+
this.fail(value);
|
|
279
|
+
this.index += value.length;
|
|
280
|
+
}
|
|
281
|
+
scanNumber() {
|
|
282
|
+
NUMBER_TOKEN_PATTERN.lastIndex = this.index;
|
|
283
|
+
const match = NUMBER_TOKEN_PATTERN.exec(this.text);
|
|
284
|
+
if (match === null)
|
|
285
|
+
this.fail('JSON value');
|
|
286
|
+
if (!Number.isFinite(Number(match[0]))) {
|
|
287
|
+
throw new TypeError('framework-json-v1 only accepts finite JSON numbers.');
|
|
288
|
+
}
|
|
289
|
+
this.index += match[0].length;
|
|
290
|
+
}
|
|
291
|
+
skipWhitespace() {
|
|
292
|
+
for (;;) {
|
|
293
|
+
const code = this.text.charCodeAt(this.index);
|
|
294
|
+
if (code === 0x20 || code === 0x09 || code === 0x0a || code === 0x0d) {
|
|
295
|
+
this.index += 1;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
fail(expected) {
|
|
302
|
+
throw new SyntaxError(`framework-json-v1 expected ${expected} at offset ${this.index}.`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
const BASE64_STRING_CHUNK_BYTES = 8192;
|
|
306
|
+
function encodeBase64(bytes) {
|
|
307
|
+
let binary = '';
|
|
308
|
+
for (let offset = 0; offset < bytes.length; offset += BASE64_STRING_CHUNK_BYTES) {
|
|
309
|
+
binary += String.fromCharCode(...bytes.subarray(offset, offset + BASE64_STRING_CHUNK_BYTES));
|
|
310
|
+
}
|
|
311
|
+
return btoa(binary);
|
|
312
|
+
}
|
|
313
|
+
function decodeBase64(value) {
|
|
314
|
+
const binary = atob(value);
|
|
315
|
+
const bytes = new Uint8Array(binary.length);
|
|
316
|
+
for (let index = 0; index < binary.length; index++)
|
|
317
|
+
bytes[index] = binary.charCodeAt(index);
|
|
318
|
+
return bytes;
|
|
319
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Describes the fields accepted by the default framework-json-v1 serializer. */
|
|
2
|
+
export type ZLinkJsonSchema = {
|
|
3
|
+
readonly type: 'boolean';
|
|
4
|
+
} | {
|
|
5
|
+
readonly type: 'string';
|
|
6
|
+
} | {
|
|
7
|
+
readonly type: 'number';
|
|
8
|
+
} | {
|
|
9
|
+
readonly type: 'int32';
|
|
10
|
+
} | {
|
|
11
|
+
readonly type: 'uint32';
|
|
12
|
+
} | {
|
|
13
|
+
readonly type: 'int64';
|
|
14
|
+
} | {
|
|
15
|
+
readonly type: 'uint64';
|
|
16
|
+
} | {
|
|
17
|
+
readonly type: 'bytes';
|
|
18
|
+
} | {
|
|
19
|
+
readonly type: 'enum';
|
|
20
|
+
readonly names: readonly string[];
|
|
21
|
+
} | {
|
|
22
|
+
readonly type: 'nullable';
|
|
23
|
+
readonly value: ZLinkJsonSchema;
|
|
24
|
+
} | {
|
|
25
|
+
readonly type: 'array';
|
|
26
|
+
readonly items: ZLinkJsonSchema;
|
|
27
|
+
} | {
|
|
28
|
+
readonly type: 'record';
|
|
29
|
+
readonly values: ZLinkJsonSchema;
|
|
30
|
+
} | {
|
|
31
|
+
readonly type: 'object';
|
|
32
|
+
readonly properties: Readonly<Record<string, ZLinkJsonSchema>>;
|
|
33
|
+
readonly required: readonly string[];
|
|
34
|
+
/** Unknown properties are ignored during decoding unless this is false. */
|
|
35
|
+
readonly additionalProperties?: boolean;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Describes the request and optional reply fields carried under one packet
|
|
39
|
+
* name by the default JSON serializer.
|
|
40
|
+
*/
|
|
41
|
+
export interface ZLinkPacketJsonContract {
|
|
42
|
+
readonly payload: ZLinkJsonSchema;
|
|
43
|
+
readonly reply?: ZLinkJsonSchema;
|
|
44
|
+
}
|
|
45
|
+
export declare function defineZLinkPacketJsonContract(packetName: string, contract: ZLinkPacketJsonContract): ZLinkPacketJsonContract;
|
|
46
|
+
export declare function readZLinkPacketJsonContract(packetName: string): ZLinkPacketJsonContract | undefined;
|
|
47
|
+
export declare function isPrototypeKey(name: string): boolean;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineZLinkPacketJsonContract = defineZLinkPacketJsonContract;
|
|
4
|
+
exports.readZLinkPacketJsonContract = readZLinkPacketJsonContract;
|
|
5
|
+
exports.isPrototypeKey = isPrototypeKey;
|
|
6
|
+
const MAX_CONTRACT_SCHEMA_DEPTH = 64;
|
|
7
|
+
const ZLINK_PACKET_JSON_CONTRACTS = Symbol.for('@zlink-systems/framework:packet-json-contracts');
|
|
8
|
+
function defineZLinkPacketJsonContract(packetName, contract) {
|
|
9
|
+
if (packetName.trim().length === 0) {
|
|
10
|
+
throw new TypeError('ZLink packet name must not be empty.');
|
|
11
|
+
}
|
|
12
|
+
const runtimeContract = contract;
|
|
13
|
+
if (typeof runtimeContract !== 'object' || runtimeContract === null) {
|
|
14
|
+
throw new TypeError('ZLink packet JSON contract must be an object.');
|
|
15
|
+
}
|
|
16
|
+
const normalizedContract = runtimeContract;
|
|
17
|
+
const normalized = Object.freeze({
|
|
18
|
+
payload: normalizeJsonSchema(normalizedContract.payload, new Set(), 0),
|
|
19
|
+
...(normalizedContract.reply === undefined
|
|
20
|
+
? {}
|
|
21
|
+
: { reply: normalizeJsonSchema(normalizedContract.reply, new Set(), 0) })
|
|
22
|
+
});
|
|
23
|
+
const contracts = packetJsonContracts();
|
|
24
|
+
const current = contracts.get(packetName);
|
|
25
|
+
if (current !== undefined && canonicalContract(current) !== canonicalContract(normalized)) {
|
|
26
|
+
throw new TypeError(`ZLink packet '${packetName}' has conflicting JSON contracts in one process.`);
|
|
27
|
+
}
|
|
28
|
+
if (current !== undefined)
|
|
29
|
+
return current;
|
|
30
|
+
contracts.set(packetName, normalized);
|
|
31
|
+
return normalized;
|
|
32
|
+
}
|
|
33
|
+
function readZLinkPacketJsonContract(packetName) {
|
|
34
|
+
return packetJsonContracts().get(packetName);
|
|
35
|
+
}
|
|
36
|
+
function normalizeJsonSchema(schema, parents, depth) {
|
|
37
|
+
// Keep the runtime boundary defensive for JavaScript callers even though
|
|
38
|
+
// the TypeScript signature describes the accepted shape.
|
|
39
|
+
const runtimeSchema = schema;
|
|
40
|
+
if (typeof runtimeSchema !== 'object' ||
|
|
41
|
+
runtimeSchema === null ||
|
|
42
|
+
depth > MAX_CONTRACT_SCHEMA_DEPTH) {
|
|
43
|
+
throw new TypeError(`ZLink packet JSON schema must be an acyclic object with at most ${MAX_CONTRACT_SCHEMA_DEPTH} levels.`);
|
|
44
|
+
}
|
|
45
|
+
const normalizedSchema = runtimeSchema;
|
|
46
|
+
if (parents.has(normalizedSchema))
|
|
47
|
+
throw new TypeError('ZLink packet JSON schema must not contain a cycle.');
|
|
48
|
+
const nextParents = new Set(parents).add(normalizedSchema);
|
|
49
|
+
switch (normalizedSchema.type) {
|
|
50
|
+
case 'boolean':
|
|
51
|
+
case 'string':
|
|
52
|
+
case 'number':
|
|
53
|
+
case 'int32':
|
|
54
|
+
case 'uint32':
|
|
55
|
+
case 'int64':
|
|
56
|
+
case 'uint64':
|
|
57
|
+
case 'bytes':
|
|
58
|
+
return Object.freeze({ type: normalizedSchema.type });
|
|
59
|
+
case 'enum': {
|
|
60
|
+
if (!Array.isArray(normalizedSchema.names) ||
|
|
61
|
+
normalizedSchema.names.length === 0 ||
|
|
62
|
+
normalizedSchema.names.some((name) => typeof name !== 'string' || name.length === 0)) {
|
|
63
|
+
throw new TypeError('ZLink packet enum schema requires non-empty string names.');
|
|
64
|
+
}
|
|
65
|
+
if (new Set(normalizedSchema.names).size !== normalizedSchema.names.length) {
|
|
66
|
+
throw new TypeError('ZLink packet enum schema names must be unique.');
|
|
67
|
+
}
|
|
68
|
+
return Object.freeze({ type: 'enum', names: Object.freeze([...normalizedSchema.names]) });
|
|
69
|
+
}
|
|
70
|
+
case 'nullable':
|
|
71
|
+
return Object.freeze({
|
|
72
|
+
type: 'nullable',
|
|
73
|
+
value: normalizeJsonSchema(normalizedSchema.value, nextParents, depth + 1)
|
|
74
|
+
});
|
|
75
|
+
case 'array':
|
|
76
|
+
return Object.freeze({
|
|
77
|
+
type: 'array',
|
|
78
|
+
items: normalizeJsonSchema(normalizedSchema.items, nextParents, depth + 1)
|
|
79
|
+
});
|
|
80
|
+
case 'record':
|
|
81
|
+
return Object.freeze({
|
|
82
|
+
type: 'record',
|
|
83
|
+
values: normalizeJsonSchema(normalizedSchema.values, nextParents, depth + 1)
|
|
84
|
+
});
|
|
85
|
+
case 'object': {
|
|
86
|
+
const runtimeProperties = normalizedSchema.properties;
|
|
87
|
+
if (typeof runtimeProperties !== 'object' ||
|
|
88
|
+
runtimeProperties === null ||
|
|
89
|
+
Array.isArray(runtimeProperties)) {
|
|
90
|
+
throw new TypeError('ZLink packet object schema properties must be an object.');
|
|
91
|
+
}
|
|
92
|
+
if (!Array.isArray(normalizedSchema.required) ||
|
|
93
|
+
normalizedSchema.required.some((name) => typeof name !== 'string' || name.length === 0)) {
|
|
94
|
+
throw new TypeError('ZLink packet object schema required must contain property names.');
|
|
95
|
+
}
|
|
96
|
+
if (normalizedSchema.additionalProperties !== undefined &&
|
|
97
|
+
typeof normalizedSchema.additionalProperties !== 'boolean') {
|
|
98
|
+
throw new TypeError('ZLink packet object schema additionalProperties must be boolean.');
|
|
99
|
+
}
|
|
100
|
+
const properties = {};
|
|
101
|
+
for (const [name, property] of Object.entries(runtimeProperties)) {
|
|
102
|
+
if (isPrototypeKey(name)) {
|
|
103
|
+
throw new TypeError(`ZLink packet JSON property '${name}' is not allowed.`);
|
|
104
|
+
}
|
|
105
|
+
properties[name] = normalizeJsonSchema(property, nextParents, depth + 1);
|
|
106
|
+
}
|
|
107
|
+
if (new Set(normalizedSchema.required).size !== normalizedSchema.required.length) {
|
|
108
|
+
throw new TypeError('ZLink packet JSON required properties must be unique.');
|
|
109
|
+
}
|
|
110
|
+
for (const name of normalizedSchema.required) {
|
|
111
|
+
if (isPrototypeKey(name)) {
|
|
112
|
+
throw new TypeError(`ZLink packet JSON property '${name}' is not allowed.`);
|
|
113
|
+
}
|
|
114
|
+
if (!Object.prototype.hasOwnProperty.call(properties, name)) {
|
|
115
|
+
throw new TypeError(`ZLink packet JSON required property '${name}' has no property schema.`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return Object.freeze({
|
|
119
|
+
type: 'object',
|
|
120
|
+
properties: Object.freeze(properties),
|
|
121
|
+
required: Object.freeze([...normalizedSchema.required]),
|
|
122
|
+
...(normalizedSchema.additionalProperties === undefined
|
|
123
|
+
? {}
|
|
124
|
+
: { additionalProperties: normalizedSchema.additionalProperties })
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
throw new TypeError(`ZLink packet JSON schema type '${String(normalizedSchema.type)}' is not supported.`);
|
|
129
|
+
}
|
|
130
|
+
function packetJsonContracts() {
|
|
131
|
+
const root = globalThis;
|
|
132
|
+
let contracts = root[ZLINK_PACKET_JSON_CONTRACTS];
|
|
133
|
+
if (contracts === undefined) {
|
|
134
|
+
contracts = new Map();
|
|
135
|
+
Object.defineProperty(root, ZLINK_PACKET_JSON_CONTRACTS, {
|
|
136
|
+
configurable: false,
|
|
137
|
+
enumerable: false,
|
|
138
|
+
value: contracts,
|
|
139
|
+
writable: false
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return contracts;
|
|
143
|
+
}
|
|
144
|
+
function canonicalContract(value) {
|
|
145
|
+
if (Array.isArray(value))
|
|
146
|
+
return `[${value.map(canonicalContract).join(',')}]`;
|
|
147
|
+
if (typeof value !== 'object' || value === null)
|
|
148
|
+
return JSON.stringify(value);
|
|
149
|
+
return `{${Object.entries(value)
|
|
150
|
+
.sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
|
|
151
|
+
.map(([key, item]) => `${JSON.stringify(key)}:${canonicalContract(item)}`)
|
|
152
|
+
.join(',')}}`;
|
|
153
|
+
}
|
|
154
|
+
function isPrototypeKey(name) {
|
|
155
|
+
return name === '__proto__' || name === 'prototype' || name === 'constructor';
|
|
156
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/** @internal Shared JSON profile; not an application codec registration API. */
|
|
18
|
+
__exportStar(require("./framework-json-v1"), exports);
|
|
19
|
+
__exportStar(require("./json-contract"), exports);
|
|
20
|
+
__exportStar(require("./packet-json-contract"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ZLinkPacketJsonContract } from './json-contract';
|
|
2
|
+
export declare const ZLINK_DECORATOR_METADATA: unique symbol;
|
|
3
|
+
export interface ZLinkDecoratorMetadata {
|
|
4
|
+
readonly kind: string;
|
|
5
|
+
readonly packetName?: string;
|
|
6
|
+
readonly groupName?: string;
|
|
7
|
+
readonly methodName?: string;
|
|
8
|
+
readonly meshName?: string;
|
|
9
|
+
readonly channelName?: string;
|
|
10
|
+
readonly topic?: string;
|
|
11
|
+
readonly jsonContract?: ZLinkPacketJsonContract;
|
|
12
|
+
}
|
|
13
|
+
export declare function readZLinkDecoratorMetadata(target: object): readonly ZLinkDecoratorMetadata[];
|
|
14
|
+
export declare function readFrameworkPacketJsonContract(type: object, explicitPacketName?: string): ZLinkPacketJsonContract | undefined;
|
|
15
|
+
export declare function normalizePacketName(value: unknown): string | undefined;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ZLINK_DECORATOR_METADATA = void 0;
|
|
4
|
+
exports.readZLinkDecoratorMetadata = readZLinkDecoratorMetadata;
|
|
5
|
+
exports.readFrameworkPacketJsonContract = readFrameworkPacketJsonContract;
|
|
6
|
+
exports.normalizePacketName = normalizePacketName;
|
|
7
|
+
exports.ZLINK_DECORATOR_METADATA = Symbol.for('@zlink-systems/framework:decorator');
|
|
8
|
+
function readZLinkDecoratorMetadata(target) {
|
|
9
|
+
if (!Object.prototype.hasOwnProperty.call(target, exports.ZLINK_DECORATOR_METADATA)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
return target[exports.ZLINK_DECORATOR_METADATA];
|
|
13
|
+
}
|
|
14
|
+
function readFrameworkPacketJsonContract(type, explicitPacketName) {
|
|
15
|
+
const expectedPacketName = normalizePacketName(explicitPacketName);
|
|
16
|
+
for (const metadata of readZLinkDecoratorMetadata(type)) {
|
|
17
|
+
if (metadata.kind === 'packet' &&
|
|
18
|
+
metadata.jsonContract !== undefined &&
|
|
19
|
+
(expectedPacketName === undefined ||
|
|
20
|
+
normalizePacketName(metadata.packetName) === expectedPacketName)) {
|
|
21
|
+
return metadata.jsonContract;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
function normalizePacketName(value) {
|
|
27
|
+
if (typeof value !== 'string') {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const trimmed = value.trim();
|
|
31
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zlink-systems/stream-wire",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"license": "
|
|
3
|
+
"version": "0.30.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/zlink-systems/zlink.git",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"dist/index.d.ts",
|
|
17
17
|
"dist/lz4-pickle.js",
|
|
18
18
|
"dist/lz4-pickle.d.ts",
|
|
19
|
-
"LICENSE"
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"dist/internal"
|
|
20
21
|
],
|
|
21
22
|
"exports": {
|
|
22
23
|
".": {
|
|
@@ -24,6 +25,17 @@
|
|
|
24
25
|
"import": "./dist/esm/index.mjs",
|
|
25
26
|
"require": "./dist/index.js",
|
|
26
27
|
"default": "./dist/esm/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./internal/json": {
|
|
30
|
+
"types": "./dist/internal/json.d.ts",
|
|
31
|
+
"default": "./dist/internal/json.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"typesVersions": {
|
|
35
|
+
"*": {
|
|
36
|
+
"internal/json": [
|
|
37
|
+
"dist/internal/json.d.ts"
|
|
38
|
+
]
|
|
27
39
|
}
|
|
28
40
|
}
|
|
29
41
|
}
|