@zlink-systems/framework-codec-protobuf 0.10.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.
- package/LICENSE +105 -0
- package/dist/browser/index.d.ts +26 -0
- package/dist/browser/index.mjs +57 -0
- package/dist/framework.d.ts +12 -0
- package/dist/index.d.ts +26 -0
- package/dist/server/framework.cjs +323 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-ALv2
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 ZLink Systems
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ZlinkStreamEncodedPayload, ZlinkStreamPayloadCodec } from '@zlink-systems/stream-connector';
|
|
2
|
+
export declare const ZLINK_PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
|
|
3
|
+
export declare const zlinkStreamProtobufCodecName = "protobuf";
|
|
4
|
+
export interface ProtobufType<T> {
|
|
5
|
+
encode(message: T): {
|
|
6
|
+
finish(): Uint8Array;
|
|
7
|
+
};
|
|
8
|
+
decode(reader: Uint8Array): T;
|
|
9
|
+
}
|
|
10
|
+
export interface ProtobufEnvelopeCodecOptions {
|
|
11
|
+
encode(payload: unknown, context?: ProtobufEncodeContext | Function): ZlinkStreamEncodedPayload;
|
|
12
|
+
decode<TPayload = unknown>(payload: ZlinkStreamEncodedPayload): TPayload;
|
|
13
|
+
}
|
|
14
|
+
export type ProtobufMessageDirection = 'Request' | 'Response' | 'Error' | 'Send';
|
|
15
|
+
export interface ProtobufEncodeContext {
|
|
16
|
+
readonly messageType?: Function;
|
|
17
|
+
readonly packetName?: string;
|
|
18
|
+
readonly direction: ProtobufMessageDirection;
|
|
19
|
+
}
|
|
20
|
+
export type ZlinkStreamProtobufEnvelopeCodec = ZlinkStreamPayloadCodec & {
|
|
21
|
+
encode(payload: unknown, context?: ProtobufEncodeContext | Function): ZlinkStreamEncodedPayload;
|
|
22
|
+
};
|
|
23
|
+
export declare function createZlinkStreamProtobufCodec<T>(type: ProtobufType<T>): ZlinkStreamPayloadCodec;
|
|
24
|
+
export declare function createZlinkStreamProtobufEnvelopeCodec(options: ProtobufEnvelopeCodecOptions): ZlinkStreamProtobufEnvelopeCodec;
|
|
25
|
+
export declare function toProto<T>(value: T, type: ProtobufType<T>, messageType?: Function): ZlinkStreamEncodedPayload;
|
|
26
|
+
export declare function fromProto<T>(payload: ZlinkStreamEncodedPayload, type: ProtobufType<T>): T;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// packages/stream-wire/src/lz4-pickle.ts
|
|
2
|
+
var defaultMaxDecompressedPayloadSize = 64 * 1024;
|
|
3
|
+
|
|
4
|
+
// packages/framework-codec-protobuf/src/index.ts
|
|
5
|
+
var ZLINK_PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
|
|
6
|
+
var zlinkStreamProtobufCodecName = "protobuf";
|
|
7
|
+
function createZlinkStreamProtobufCodec(type) {
|
|
8
|
+
return {
|
|
9
|
+
encode(payload, messageType) {
|
|
10
|
+
return toProto(payload, type, messageType);
|
|
11
|
+
},
|
|
12
|
+
decode(payload) {
|
|
13
|
+
return fromProto(payload, type);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function createZlinkStreamProtobufEnvelopeCodec(options) {
|
|
18
|
+
return {
|
|
19
|
+
encode(payload, context) {
|
|
20
|
+
return options.encode(payload, context);
|
|
21
|
+
},
|
|
22
|
+
decode(payload) {
|
|
23
|
+
return options.decode(payload);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function toProto(value, type, messageType) {
|
|
28
|
+
return {
|
|
29
|
+
codec: 3 /* Protobuf */,
|
|
30
|
+
payload: type.encode(value).finish(),
|
|
31
|
+
messageType: messageType ?? inferMessageType(value)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function fromProto(payload, type) {
|
|
35
|
+
ensureProtobuf(payload);
|
|
36
|
+
return type.decode(payload.payload);
|
|
37
|
+
}
|
|
38
|
+
function ensureProtobuf(payload) {
|
|
39
|
+
if (payload.codec !== 3 /* Protobuf */) {
|
|
40
|
+
throw new Error(`Stream payload codec is ${payload.codec}, not Protobuf.`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function inferMessageType(value) {
|
|
44
|
+
if (value === null || value === void 0) {
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
const constructor = value.constructor;
|
|
48
|
+
return constructor === Object ? void 0 : constructor;
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
ZLINK_PROTOBUF_CONTENT_TYPE,
|
|
52
|
+
createZlinkStreamProtobufCodec,
|
|
53
|
+
createZlinkStreamProtobufEnvelopeCodec,
|
|
54
|
+
fromProto,
|
|
55
|
+
toProto,
|
|
56
|
+
zlinkStreamProtobufCodecName
|
|
57
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ZLinkCodecExtension, type ZLinkMessageSerializer } from '@zlink-systems/framework';
|
|
2
|
+
import type { ZlinkStreamEncodedPayload, ZlinkStreamPayloadCodec } from '@zlink-systems/stream-connector';
|
|
3
|
+
import { type ProtobufEncodeContext, type ProtobufEnvelopeCodecOptions } from './index';
|
|
4
|
+
export { ZLINK_PROTOBUF_CONTENT_TYPE } from './index';
|
|
5
|
+
export type ZLinkProtobufCodecExtension = ZLinkCodecExtension & ZlinkStreamPayloadCodec;
|
|
6
|
+
export type ZLinkProtobufEnvelopeCodecExtension = ZLinkProtobufCodecExtension & {
|
|
7
|
+
encode(payload: unknown, context?: ProtobufEncodeContext | Function): ZlinkStreamEncodedPayload;
|
|
8
|
+
};
|
|
9
|
+
export declare function zlinkProtobufCodec(): ZLinkProtobufCodecExtension;
|
|
10
|
+
export declare function createProtobufMessageSerializer(): ZLinkMessageSerializer;
|
|
11
|
+
export declare function createZlinkProtobufEnvelopeCodec(options: ProtobufEnvelopeCodecOptions): ZLinkProtobufEnvelopeCodecExtension;
|
|
12
|
+
export declare function createProtobufEnvelopeMessageSerializer(options: ProtobufEnvelopeCodecOptions): ZLinkMessageSerializer;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ZlinkStreamEncodedPayload, ZlinkStreamPayloadCodec } from '@zlink-systems/stream-connector';
|
|
2
|
+
export declare const ZLINK_PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
|
|
3
|
+
export declare const zlinkStreamProtobufCodecName = "protobuf";
|
|
4
|
+
export interface ProtobufType<T> {
|
|
5
|
+
encode(message: T): {
|
|
6
|
+
finish(): Uint8Array;
|
|
7
|
+
};
|
|
8
|
+
decode(reader: Uint8Array): T;
|
|
9
|
+
}
|
|
10
|
+
export interface ProtobufEnvelopeCodecOptions {
|
|
11
|
+
encode(payload: unknown, context?: ProtobufEncodeContext | Function): ZlinkStreamEncodedPayload;
|
|
12
|
+
decode<TPayload = unknown>(payload: ZlinkStreamEncodedPayload): TPayload;
|
|
13
|
+
}
|
|
14
|
+
export type ProtobufMessageDirection = 'Request' | 'Response' | 'Error' | 'Send';
|
|
15
|
+
export interface ProtobufEncodeContext {
|
|
16
|
+
readonly messageType?: Function;
|
|
17
|
+
readonly packetName?: string;
|
|
18
|
+
readonly direction: ProtobufMessageDirection;
|
|
19
|
+
}
|
|
20
|
+
export type ZlinkStreamProtobufEnvelopeCodec = ZlinkStreamPayloadCodec & {
|
|
21
|
+
encode(payload: unknown, context?: ProtobufEncodeContext | Function): ZlinkStreamEncodedPayload;
|
|
22
|
+
};
|
|
23
|
+
export declare function createZlinkStreamProtobufCodec<T>(type: ProtobufType<T>): ZlinkStreamPayloadCodec;
|
|
24
|
+
export declare function createZlinkStreamProtobufEnvelopeCodec(options: ProtobufEnvelopeCodecOptions): ZlinkStreamProtobufEnvelopeCodec;
|
|
25
|
+
export declare function toProto<T>(value: T, type: ProtobufType<T>, messageType?: Function): ZlinkStreamEncodedPayload;
|
|
26
|
+
export declare function fromProto<T>(payload: ZlinkStreamEncodedPayload, type: ProtobufType<T>): T;
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// packages/framework-codec-protobuf/src/framework.ts
|
|
21
|
+
var framework_exports = {};
|
|
22
|
+
__export(framework_exports, {
|
|
23
|
+
ZLINK_PROTOBUF_CONTENT_TYPE: () => ZLINK_PROTOBUF_CONTENT_TYPE,
|
|
24
|
+
createProtobufEnvelopeMessageSerializer: () => createProtobufEnvelopeMessageSerializer,
|
|
25
|
+
createProtobufMessageSerializer: () => createProtobufMessageSerializer,
|
|
26
|
+
createZlinkProtobufEnvelopeCodec: () => createZlinkProtobufEnvelopeCodec,
|
|
27
|
+
zlinkProtobufCodec: () => zlinkProtobufCodec
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(framework_exports);
|
|
30
|
+
var import_framework = require("@zlink-systems/framework");
|
|
31
|
+
var import_stream_wire2 = require("@zlink-systems/stream-wire");
|
|
32
|
+
|
|
33
|
+
// packages/framework-codec-protobuf/src/dynamic-value-wire.ts
|
|
34
|
+
function createDynamicValueProtobufType() {
|
|
35
|
+
return {
|
|
36
|
+
encode(value) {
|
|
37
|
+
return { finish: () => encodeDynamicValue(value) };
|
|
38
|
+
},
|
|
39
|
+
decode(reader) {
|
|
40
|
+
return decodeDynamicValue(Buffer.from(reader));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function encodeDynamicValue(value) {
|
|
45
|
+
if (value === null || value === void 0) {
|
|
46
|
+
return encodeFields([encodeVarintField(1, 0 /* Null */)]);
|
|
47
|
+
}
|
|
48
|
+
if (typeof value === "boolean") {
|
|
49
|
+
return encodeFields([
|
|
50
|
+
encodeVarintField(1, 1 /* Bool */),
|
|
51
|
+
encodeVarintField(2, value ? 1 : 0)
|
|
52
|
+
]);
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "number") {
|
|
55
|
+
return encodeFields([
|
|
56
|
+
encodeVarintField(1, 2 /* Number */),
|
|
57
|
+
encodeDoubleField(3, value)
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
return encodeFields([
|
|
62
|
+
encodeVarintField(1, 3 /* String */),
|
|
63
|
+
encodeBytesField(4, Buffer.from(value))
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(value)) {
|
|
67
|
+
return encodeFields([
|
|
68
|
+
encodeVarintField(1, 5 /* Array */),
|
|
69
|
+
...value.map((item) => encodeBytesField(6, encodeDynamicValue(item)))
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
if (typeof value === "object") {
|
|
73
|
+
return encodeFields([
|
|
74
|
+
encodeVarintField(1, 4 /* Object */),
|
|
75
|
+
...Object.entries(value).map(([key, entryValue]) => encodeBytesField(5, encodeObjectEntry(key, entryValue)))
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Protobuf serializer cannot encode value of type '${typeof value}'.`);
|
|
79
|
+
}
|
|
80
|
+
function decodeDynamicValue(bytes) {
|
|
81
|
+
let kind = 0 /* Null */;
|
|
82
|
+
let boolValue = false;
|
|
83
|
+
let numberValue = 0;
|
|
84
|
+
let stringValue = "";
|
|
85
|
+
const objectValue = {};
|
|
86
|
+
const arrayValue = [];
|
|
87
|
+
for (const field of readFields(bytes)) {
|
|
88
|
+
switch (field.fieldNumber) {
|
|
89
|
+
case 1:
|
|
90
|
+
kind = Number(readVarintPayload(field));
|
|
91
|
+
break;
|
|
92
|
+
case 2:
|
|
93
|
+
boolValue = readVarintPayload(field) !== 0n;
|
|
94
|
+
break;
|
|
95
|
+
case 3:
|
|
96
|
+
numberValue = readDoublePayload(field);
|
|
97
|
+
break;
|
|
98
|
+
case 4:
|
|
99
|
+
stringValue = readBytesPayload(field).toString();
|
|
100
|
+
break;
|
|
101
|
+
case 5: {
|
|
102
|
+
const entry = decodeObjectEntry(readBytesPayload(field));
|
|
103
|
+
objectValue[entry.key] = entry.value;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case 6:
|
|
107
|
+
arrayValue.push(decodeDynamicValue(readBytesPayload(field)));
|
|
108
|
+
break;
|
|
109
|
+
default:
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
switch (kind) {
|
|
114
|
+
case 0 /* Null */:
|
|
115
|
+
return null;
|
|
116
|
+
case 1 /* Bool */:
|
|
117
|
+
return boolValue;
|
|
118
|
+
case 2 /* Number */:
|
|
119
|
+
return numberValue;
|
|
120
|
+
case 3 /* String */:
|
|
121
|
+
return stringValue;
|
|
122
|
+
case 4 /* Object */:
|
|
123
|
+
return objectValue;
|
|
124
|
+
case 5 /* Array */:
|
|
125
|
+
return arrayValue;
|
|
126
|
+
default:
|
|
127
|
+
throw new Error(`Protobuf serializer cannot decode value kind '${kind}'.`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function encodeObjectEntry(key, value) {
|
|
131
|
+
return encodeFields([
|
|
132
|
+
encodeBytesField(1, Buffer.from(key)),
|
|
133
|
+
encodeBytesField(2, encodeDynamicValue(value))
|
|
134
|
+
]);
|
|
135
|
+
}
|
|
136
|
+
function decodeObjectEntry(bytes) {
|
|
137
|
+
let key = "";
|
|
138
|
+
let value = null;
|
|
139
|
+
for (const field of readFields(bytes)) {
|
|
140
|
+
if (field.fieldNumber === 1) {
|
|
141
|
+
key = readBytesPayload(field).toString();
|
|
142
|
+
} else if (field.fieldNumber === 2) {
|
|
143
|
+
value = decodeDynamicValue(readBytesPayload(field));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return { key, value };
|
|
147
|
+
}
|
|
148
|
+
function encodeFields(fields) {
|
|
149
|
+
return Buffer.concat(fields);
|
|
150
|
+
}
|
|
151
|
+
function encodeVarintField(fieldNumber, value) {
|
|
152
|
+
return Buffer.concat([encodeVarint(fieldKey(fieldNumber, 0 /* Varint */)), encodeVarint(value)]);
|
|
153
|
+
}
|
|
154
|
+
function encodeDoubleField(fieldNumber, value) {
|
|
155
|
+
const payload = Buffer.allocUnsafe(8);
|
|
156
|
+
payload.writeDoubleLE(value);
|
|
157
|
+
return Buffer.concat([encodeVarint(fieldKey(fieldNumber, 1 /* Fixed64 */)), payload]);
|
|
158
|
+
}
|
|
159
|
+
function encodeBytesField(fieldNumber, value) {
|
|
160
|
+
return Buffer.concat([
|
|
161
|
+
encodeVarint(fieldKey(fieldNumber, 2 /* LengthDelimited */)),
|
|
162
|
+
encodeVarint(value.length),
|
|
163
|
+
value
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
function fieldKey(fieldNumber, wireType) {
|
|
167
|
+
return fieldNumber << 3 | wireType;
|
|
168
|
+
}
|
|
169
|
+
function encodeVarint(value) {
|
|
170
|
+
let remaining = BigInt(value);
|
|
171
|
+
const bytes = Buffer.allocUnsafe(10);
|
|
172
|
+
let offset = 0;
|
|
173
|
+
do {
|
|
174
|
+
let byte = Number(remaining & 0x7fn);
|
|
175
|
+
remaining >>= 7n;
|
|
176
|
+
if (remaining !== 0n) byte |= 128;
|
|
177
|
+
bytes[offset++] = byte;
|
|
178
|
+
} while (remaining !== 0n);
|
|
179
|
+
return Buffer.from(bytes.subarray(0, offset));
|
|
180
|
+
}
|
|
181
|
+
function readFields(bytes) {
|
|
182
|
+
const fields = [];
|
|
183
|
+
let offset = 0;
|
|
184
|
+
while (offset < bytes.length) {
|
|
185
|
+
const key = readVarint(bytes, offset);
|
|
186
|
+
offset = key.offset;
|
|
187
|
+
const fieldNumber = Number(key.value >> 3n);
|
|
188
|
+
const wireType = Number(key.value & 0x07n);
|
|
189
|
+
if (wireType === 0 /* Varint */) {
|
|
190
|
+
const value = readVarint(bytes, offset);
|
|
191
|
+
fields.push({ fieldNumber, wireType, payload: bytes.subarray(offset, value.offset) });
|
|
192
|
+
offset = value.offset;
|
|
193
|
+
} else if (wireType === 1 /* Fixed64 */) {
|
|
194
|
+
fields.push({ fieldNumber, wireType, payload: bytes.subarray(offset, offset + 8) });
|
|
195
|
+
offset += 8;
|
|
196
|
+
} else if (wireType === 2 /* LengthDelimited */) {
|
|
197
|
+
const length = readVarint(bytes, offset);
|
|
198
|
+
offset = length.offset;
|
|
199
|
+
const end = offset + Number(length.value);
|
|
200
|
+
fields.push({ fieldNumber, wireType, payload: bytes.subarray(offset, end) });
|
|
201
|
+
offset = end;
|
|
202
|
+
} else {
|
|
203
|
+
throw new Error(`Protobuf serializer cannot read wire type '${wireType}'.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return fields;
|
|
207
|
+
}
|
|
208
|
+
function readVarintPayload(field) {
|
|
209
|
+
ensureWireType(field, 0 /* Varint */);
|
|
210
|
+
return readVarint(field.payload, 0).value;
|
|
211
|
+
}
|
|
212
|
+
function readDoublePayload(field) {
|
|
213
|
+
ensureWireType(field, 1 /* Fixed64 */);
|
|
214
|
+
return field.payload.readDoubleLE();
|
|
215
|
+
}
|
|
216
|
+
function readBytesPayload(field) {
|
|
217
|
+
ensureWireType(field, 2 /* LengthDelimited */);
|
|
218
|
+
return field.payload;
|
|
219
|
+
}
|
|
220
|
+
function ensureWireType(field, expected) {
|
|
221
|
+
if (field.wireType !== expected) {
|
|
222
|
+
throw new Error(`Protobuf field '${field.fieldNumber}' has wire type '${field.wireType}', not '${expected}'.`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function readVarint(bytes, start) {
|
|
226
|
+
let value = 0n;
|
|
227
|
+
let shift = 0n;
|
|
228
|
+
let offset = start;
|
|
229
|
+
while (offset < bytes.length) {
|
|
230
|
+
const byte = BigInt(bytes[offset]);
|
|
231
|
+
value |= (byte & 0x7fn) << shift;
|
|
232
|
+
offset += 1;
|
|
233
|
+
if ((byte & 0x80n) === 0n) return { value, offset };
|
|
234
|
+
shift += 7n;
|
|
235
|
+
}
|
|
236
|
+
throw new Error("Protobuf varint is truncated.");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// packages/framework-codec-protobuf/src/index.ts
|
|
240
|
+
var import_stream_wire = require("@zlink-systems/stream-wire");
|
|
241
|
+
var ZLINK_PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
|
|
242
|
+
function createZlinkStreamProtobufEnvelopeCodec(options) {
|
|
243
|
+
return {
|
|
244
|
+
encode(payload, context) {
|
|
245
|
+
return options.encode(payload, context);
|
|
246
|
+
},
|
|
247
|
+
decode(payload) {
|
|
248
|
+
return options.decode(payload);
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// packages/framework-codec-protobuf/src/framework.ts
|
|
254
|
+
function zlinkProtobufCodec() {
|
|
255
|
+
const type = createDynamicValueProtobufType();
|
|
256
|
+
return {
|
|
257
|
+
register(codecs) {
|
|
258
|
+
codecs.addSerializer(ZLINK_PROTOBUF_CONTENT_TYPE, createProtobufMessageSerializer());
|
|
259
|
+
codecs.addStreamCodec(ZLINK_PROTOBUF_CONTENT_TYPE, this);
|
|
260
|
+
},
|
|
261
|
+
encode(payload, messageType) {
|
|
262
|
+
return {
|
|
263
|
+
codec: import_stream_wire2.ZlinkStreamCodec.Protobuf,
|
|
264
|
+
payload: type.encode(payload).finish(),
|
|
265
|
+
messageType
|
|
266
|
+
};
|
|
267
|
+
},
|
|
268
|
+
decode(payload) {
|
|
269
|
+
return type.decode(payload.payload);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function createProtobufMessageSerializer() {
|
|
274
|
+
return {
|
|
275
|
+
serialize(value) {
|
|
276
|
+
return import_framework.ZLinkEncodedPayload.from(encodeDynamicValue(value));
|
|
277
|
+
},
|
|
278
|
+
deserialize(payload) {
|
|
279
|
+
const bytes = payload.data();
|
|
280
|
+
return decodeDynamicValue(
|
|
281
|
+
Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
function createZlinkProtobufEnvelopeCodec(options) {
|
|
287
|
+
const streamCodec = createZlinkStreamProtobufEnvelopeCodec(options);
|
|
288
|
+
return {
|
|
289
|
+
register(codecs) {
|
|
290
|
+
codecs.addSerializer(ZLINK_PROTOBUF_CONTENT_TYPE, createProtobufEnvelopeMessageSerializer(options));
|
|
291
|
+
codecs.addStreamCodec(ZLINK_PROTOBUF_CONTENT_TYPE, this);
|
|
292
|
+
},
|
|
293
|
+
encode: streamCodec.encode,
|
|
294
|
+
decode: streamCodec.decode
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function createProtobufEnvelopeMessageSerializer(options) {
|
|
298
|
+
return {
|
|
299
|
+
serialize(value) {
|
|
300
|
+
const encoded = options.encode(value, inferMessageType(value));
|
|
301
|
+
return import_framework.ZLinkEncodedPayload.from(encoded.payload);
|
|
302
|
+
},
|
|
303
|
+
deserialize(payload) {
|
|
304
|
+
return options.decode({
|
|
305
|
+
codec: import_stream_wire2.ZlinkStreamCodec.Protobuf,
|
|
306
|
+
payload: payload.data()
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function inferMessageType(value) {
|
|
312
|
+
if (value === null || value === void 0) return void 0;
|
|
313
|
+
const constructor = value.constructor;
|
|
314
|
+
return constructor === Object ? void 0 : constructor;
|
|
315
|
+
}
|
|
316
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
317
|
+
0 && (module.exports = {
|
|
318
|
+
ZLINK_PROTOBUF_CONTENT_TYPE,
|
|
319
|
+
createProtobufEnvelopeMessageSerializer,
|
|
320
|
+
createProtobufMessageSerializer,
|
|
321
|
+
createZlinkProtobufEnvelopeCodec,
|
|
322
|
+
zlinkProtobufCodec
|
|
323
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zlink-systems/framework-codec-protobuf",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
5
|
+
"files": ["dist/browser", "dist/server", "dist/framework.d.ts", "dist/index.d.ts", "LICENSE"],
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/browser/index.d.ts",
|
|
9
|
+
"import": "./dist/browser/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"./framework": {
|
|
12
|
+
"types": "./dist/framework.d.ts",
|
|
13
|
+
"require": "./dist/server/framework.cjs",
|
|
14
|
+
"default": "./dist/server/framework.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@zlink-systems/stream-wire": "0.10.0"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@zlink-systems/framework": "0.10.0",
|
|
22
|
+
"@zlink-systems/stream-connector": "0.10.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependenciesMeta": {
|
|
25
|
+
"@zlink-systems/framework": { "optional": true }
|
|
26
|
+
}
|
|
27
|
+
}
|