@bufbuild/protobuf 0.0.1-alpha.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/README.md +6 -0
- package/dist/cjs/binary-encoding.js +434 -0
- package/dist/cjs/binary-format.js +2 -0
- package/dist/cjs/descriptor-registry.js +518 -0
- package/dist/cjs/descriptor-set.js +557 -0
- package/dist/cjs/enum.js +2 -0
- package/dist/cjs/field-list.js +2 -0
- package/dist/cjs/field.js +40 -0
- package/dist/cjs/google/protobuf/any_pb.js +280 -0
- package/dist/cjs/google/protobuf/api_pb.js +383 -0
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +334 -0
- package/dist/cjs/google/protobuf/descriptor_pb.js +2311 -0
- package/dist/cjs/google/protobuf/duration_pb.js +201 -0
- package/dist/cjs/google/protobuf/empty_pb.js +72 -0
- package/dist/cjs/google/protobuf/field_mask_pb.js +347 -0
- package/dist/cjs/google/protobuf/source_context_pb.js +74 -0
- package/dist/cjs/google/protobuf/struct_pb.js +288 -0
- package/dist/cjs/google/protobuf/timestamp_pb.js +271 -0
- package/dist/cjs/google/protobuf/type_pb.js +613 -0
- package/dist/cjs/google/protobuf/wrappers_pb.js +658 -0
- package/dist/cjs/google/varint.js +280 -0
- package/dist/cjs/index.js +148 -0
- package/dist/cjs/json-format.js +2 -0
- package/dist/cjs/message-type.js +2 -0
- package/dist/cjs/message.js +98 -0
- package/dist/cjs/private/assert.js +52 -0
- package/dist/cjs/private/base64.js +118 -0
- package/dist/cjs/private/binary-format-common.js +253 -0
- package/dist/cjs/private/binary-format-proto2.js +120 -0
- package/dist/cjs/private/binary-format-proto3.js +102 -0
- package/dist/cjs/private/enum.js +70 -0
- package/dist/cjs/private/field-list.js +62 -0
- package/dist/cjs/private/field-wrapper.js +25 -0
- package/dist/cjs/private/field.js +34 -0
- package/dist/cjs/private/json-format-common.js +482 -0
- package/dist/cjs/private/json-format-proto2.js +100 -0
- package/dist/cjs/private/json-format-proto3.js +103 -0
- package/dist/cjs/private/message-type.js +37 -0
- package/dist/cjs/private/names.js +101 -0
- package/dist/cjs/private/options-map.js +2 -0
- package/dist/cjs/private/proto-runtime.js +24 -0
- package/dist/cjs/private/scalars.js +138 -0
- package/dist/cjs/private/util-common.js +229 -0
- package/dist/cjs/private/util.js +2 -0
- package/dist/cjs/proto-int64.js +121 -0
- package/dist/cjs/proto2.js +86 -0
- package/dist/cjs/proto3.js +99 -0
- package/dist/cjs/service-type.js +43 -0
- package/dist/cjs/type-registry.js +42 -0
- package/dist/esm/binary-encoding.js +434 -0
- package/dist/esm/binary-format.js +1 -0
- package/dist/esm/descriptor-registry.js +521 -0
- package/dist/esm/descriptor-set.js +517 -0
- package/dist/esm/enum.js +1 -0
- package/dist/esm/field-list.js +1 -0
- package/dist/esm/field.js +37 -0
- package/dist/esm/google/protobuf/any_pb.js +276 -0
- package/dist/esm/google/protobuf/api_pb.js +350 -0
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +318 -0
- package/dist/esm/google/protobuf/descriptor_pb.js +2213 -0
- package/dist/esm/google/protobuf/duration_pb.js +197 -0
- package/dist/esm/google/protobuf/empty_pb.js +68 -0
- package/dist/esm/google/protobuf/field_mask_pb.js +343 -0
- package/dist/esm/google/protobuf/source_context_pb.js +70 -0
- package/dist/esm/google/protobuf/struct_pb.js +282 -0
- package/dist/esm/google/protobuf/timestamp_pb.js +267 -0
- package/dist/esm/google/protobuf/type_pb.js +569 -0
- package/dist/esm/google/protobuf/wrappers_pb.js +574 -0
- package/dist/esm/google/varint.js +265 -0
- package/dist/esm/index.js +24 -0
- package/dist/esm/json-format.js +1 -0
- package/dist/esm/message-type.js +1 -0
- package/dist/esm/message.js +94 -0
- package/dist/esm/private/assert.js +41 -0
- package/dist/esm/private/base64.js +113 -0
- package/dist/esm/private/binary-format-common.js +243 -0
- package/dist/esm/private/binary-format-proto2.js +87 -0
- package/dist/esm/private/binary-format-proto3.js +74 -0
- package/dist/esm/private/enum.js +64 -0
- package/dist/esm/private/field-list.js +58 -0
- package/dist/esm/private/field-wrapper.js +20 -0
- package/dist/esm/private/field.js +27 -0
- package/dist/esm/private/json-format-common.js +473 -0
- package/dist/esm/private/json-format-proto2.js +90 -0
- package/dist/esm/private/json-format-proto3.js +89 -0
- package/dist/esm/private/message-type.js +33 -0
- package/dist/esm/private/names.js +90 -0
- package/dist/esm/private/options-map.js +1 -0
- package/dist/esm/private/proto-runtime.js +15 -0
- package/dist/esm/private/scalars.js +129 -0
- package/dist/esm/private/util-common.js +207 -0
- package/dist/esm/private/util.js +1 -0
- package/dist/esm/proto-int64.js +118 -0
- package/dist/esm/proto2.js +77 -0
- package/dist/esm/proto3.js +90 -0
- package/dist/esm/service-type.js +37 -0
- package/dist/esm/type-registry.js +38 -0
- package/dist/types/binary-encoding.d.ts +421 -0
- package/dist/types/binary-format.d.ts +109 -0
- package/dist/types/descriptor-registry.d.ts +42 -0
- package/dist/types/descriptor-set.d.ts +171 -0
- package/dist/types/enum.d.ts +31 -0
- package/dist/types/field-list.d.ts +27 -0
- package/dist/types/field.d.ts +295 -0
- package/dist/types/google/protobuf/any_pb.d.ts +167 -0
- package/dist/types/google/protobuf/api_pb.d.ts +296 -0
- package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +294 -0
- package/dist/types/google/protobuf/descriptor_pb.d.ts +1959 -0
- package/dist/types/google/protobuf/duration_pb.d.ts +117 -0
- package/dist/types/google/protobuf/empty_pb.d.ts +44 -0
- package/dist/types/google/protobuf/field_mask_pb.d.ts +243 -0
- package/dist/types/google/protobuf/source_context_pb.d.ts +44 -0
- package/dist/types/google/protobuf/struct_pb.d.ts +205 -0
- package/dist/types/google/protobuf/timestamp_pb.d.ts +151 -0
- package/dist/types/google/protobuf/type_pb.d.ts +482 -0
- package/dist/types/google/protobuf/wrappers_pb.d.ts +379 -0
- package/dist/types/google/varint.d.ts +65 -0
- package/dist/types/index.d.ts +53 -0
- package/dist/types/json-format.d.ts +129 -0
- package/dist/types/message-type.d.ts +58 -0
- package/dist/types/message.d.ts +124 -0
- package/dist/types/private/assert.d.ts +19 -0
- package/dist/types/private/base64.d.ts +18 -0
- package/dist/types/private/binary-format-common.d.ts +38 -0
- package/dist/types/private/binary-format-proto2.d.ts +2 -0
- package/dist/types/private/binary-format-proto3.d.ts +2 -0
- package/dist/types/private/enum.d.ts +29 -0
- package/dist/types/private/field-list.d.ts +25 -0
- package/dist/types/private/field-wrapper.d.ts +27 -0
- package/dist/types/private/field.d.ts +15 -0
- package/dist/types/private/json-format-common.d.ts +30 -0
- package/dist/types/private/json-format-proto2.d.ts +2 -0
- package/dist/types/private/json-format-proto3.d.ts +2 -0
- package/dist/types/private/message-type.d.ts +15 -0
- package/dist/types/private/names.d.ts +19 -0
- package/dist/types/private/options-map.d.ts +7 -0
- package/dist/types/private/proto-runtime.d.ts +55 -0
- package/dist/types/private/scalars.d.ts +33 -0
- package/dist/types/private/util-common.d.ts +5 -0
- package/dist/types/private/util.d.ts +55 -0
- package/dist/types/proto-int64.d.ts +94 -0
- package/dist/types/proto2.d.ts +4 -0
- package/dist/types/proto3.d.ts +4 -0
- package/dist/types/service-type.d.ts +118 -0
- package/dist/types/type-registry.d.ts +37 -0
- package/package.json +31 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment, @typescript-eslint/no-unnecessary-condition, prefer-const */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.base64encode = exports.base64decode = void 0;
|
|
5
|
+
// lookup table from base64 character to byte
|
|
6
|
+
let encTable =
|
|
7
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
|
|
8
|
+
// lookup table from base64 character *code* to byte because lookup by number is fast
|
|
9
|
+
let decTable = [];
|
|
10
|
+
for (let i = 0; i < encTable.length; i++)
|
|
11
|
+
decTable[encTable[i].charCodeAt(0)] = i;
|
|
12
|
+
// support base64url variants
|
|
13
|
+
decTable["-".charCodeAt(0)] = encTable.indexOf("+");
|
|
14
|
+
decTable["_".charCodeAt(0)] = encTable.indexOf("/");
|
|
15
|
+
/**
|
|
16
|
+
* Decodes a base64 string to a byte array.
|
|
17
|
+
*
|
|
18
|
+
* - ignores white-space, including line breaks and tabs
|
|
19
|
+
* - allows inner padding (can decode concatenated base64 strings)
|
|
20
|
+
* - does not require padding
|
|
21
|
+
* - understands base64url encoding:
|
|
22
|
+
* "-" instead of "+",
|
|
23
|
+
* "_" instead of "/",
|
|
24
|
+
* no padding
|
|
25
|
+
*/
|
|
26
|
+
function base64decode(base64Str) {
|
|
27
|
+
// estimate byte size, not accounting for inner padding and whitespace
|
|
28
|
+
let es = (base64Str.length * 3) / 4;
|
|
29
|
+
// if (es % 3 !== 0)
|
|
30
|
+
// throw new Error("invalid base64 string");
|
|
31
|
+
if (base64Str[base64Str.length - 2] == "=") es -= 2;
|
|
32
|
+
else if (base64Str[base64Str.length - 1] == "=") es -= 1;
|
|
33
|
+
let bytes = new Uint8Array(es),
|
|
34
|
+
bytePos = 0, // position in byte array
|
|
35
|
+
groupPos = 0, // position in base64 group
|
|
36
|
+
b, // current byte
|
|
37
|
+
p = 0; // previous byte
|
|
38
|
+
for (let i = 0; i < base64Str.length; i++) {
|
|
39
|
+
b = decTable[base64Str.charCodeAt(i)];
|
|
40
|
+
if (b === undefined) {
|
|
41
|
+
switch (base64Str[i]) {
|
|
42
|
+
// @ts-ignore TS7029: Fallthrough case in switch
|
|
43
|
+
case "=":
|
|
44
|
+
groupPos = 0; // reset state when padding found
|
|
45
|
+
// @ts-ignore TS7029: Fallthrough case in switch
|
|
46
|
+
case "\n":
|
|
47
|
+
case "\r":
|
|
48
|
+
case "\t":
|
|
49
|
+
case " ":
|
|
50
|
+
continue; // skip white-space, and padding
|
|
51
|
+
default:
|
|
52
|
+
throw Error("invalid base64 string.");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
switch (groupPos) {
|
|
56
|
+
case 0:
|
|
57
|
+
p = b;
|
|
58
|
+
groupPos = 1;
|
|
59
|
+
break;
|
|
60
|
+
case 1:
|
|
61
|
+
bytes[bytePos++] = (p << 2) | ((b & 48) >> 4);
|
|
62
|
+
p = b;
|
|
63
|
+
groupPos = 2;
|
|
64
|
+
break;
|
|
65
|
+
case 2:
|
|
66
|
+
bytes[bytePos++] = ((p & 15) << 4) | ((b & 60) >> 2);
|
|
67
|
+
p = b;
|
|
68
|
+
groupPos = 3;
|
|
69
|
+
break;
|
|
70
|
+
case 3:
|
|
71
|
+
bytes[bytePos++] = ((p & 3) << 6) | b;
|
|
72
|
+
groupPos = 0;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (groupPos == 1) throw Error("invalid base64 string.");
|
|
77
|
+
return bytes.subarray(0, bytePos);
|
|
78
|
+
}
|
|
79
|
+
exports.base64decode = base64decode;
|
|
80
|
+
/**
|
|
81
|
+
* Encodes a byte array to a base64 string.
|
|
82
|
+
* Adds padding at the end.
|
|
83
|
+
* Does not insert newlines.
|
|
84
|
+
*/
|
|
85
|
+
function base64encode(bytes) {
|
|
86
|
+
let base64 = "",
|
|
87
|
+
groupPos = 0, // position in base64 group
|
|
88
|
+
b, // current byte
|
|
89
|
+
p = 0; // carry over from previous byte
|
|
90
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
91
|
+
b = bytes[i];
|
|
92
|
+
switch (groupPos) {
|
|
93
|
+
case 0:
|
|
94
|
+
base64 += encTable[b >> 2];
|
|
95
|
+
p = (b & 3) << 4;
|
|
96
|
+
groupPos = 1;
|
|
97
|
+
break;
|
|
98
|
+
case 1:
|
|
99
|
+
base64 += encTable[p | (b >> 4)];
|
|
100
|
+
p = (b & 15) << 2;
|
|
101
|
+
groupPos = 2;
|
|
102
|
+
break;
|
|
103
|
+
case 2:
|
|
104
|
+
base64 += encTable[p | (b >> 6)];
|
|
105
|
+
base64 += encTable[b & 63];
|
|
106
|
+
groupPos = 0;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// padding required?
|
|
111
|
+
if (groupPos) {
|
|
112
|
+
base64 += encTable[p];
|
|
113
|
+
base64 += "=";
|
|
114
|
+
if (groupPos == 1) base64 += "=";
|
|
115
|
+
}
|
|
116
|
+
return base64;
|
|
117
|
+
}
|
|
118
|
+
exports.base64encode = base64encode;
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writePacked =
|
|
4
|
+
exports.writeScalar =
|
|
5
|
+
exports.writeMessageField =
|
|
6
|
+
exports.writeMapEntry =
|
|
7
|
+
exports.makeBinaryFormatCommon =
|
|
8
|
+
void 0;
|
|
9
|
+
const binary_encoding_js_1 = require("../binary-encoding.js");
|
|
10
|
+
const field_js_1 = require("../field.js");
|
|
11
|
+
const field_wrapper_js_1 = require("./field-wrapper.js");
|
|
12
|
+
const scalars_js_1 = require("./scalars.js");
|
|
13
|
+
const assert_js_1 = require("./assert.js");
|
|
14
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unnecessary-condition, no-case-declarations, prefer-const */
|
|
15
|
+
const unknownFieldsSymbol = Symbol("@bufbuild/protobuf/unknown-fields");
|
|
16
|
+
// Default options for parsing binary data.
|
|
17
|
+
const readDefaults = {
|
|
18
|
+
readUnknownFields: true,
|
|
19
|
+
readerFactory: (bytes) => new binary_encoding_js_1.BinaryReader(bytes),
|
|
20
|
+
};
|
|
21
|
+
// Default options for serializing binary data.
|
|
22
|
+
const writeDefaults = {
|
|
23
|
+
writeUnknownFields: true,
|
|
24
|
+
writerFactory: () => new binary_encoding_js_1.BinaryWriter(),
|
|
25
|
+
};
|
|
26
|
+
function makeReadOptions(options) {
|
|
27
|
+
return options ? { ...readDefaults, ...options } : readDefaults;
|
|
28
|
+
}
|
|
29
|
+
function makeWriteOptions(options) {
|
|
30
|
+
return options ? { ...writeDefaults, ...options } : writeDefaults;
|
|
31
|
+
}
|
|
32
|
+
function makeBinaryFormatCommon() {
|
|
33
|
+
return {
|
|
34
|
+
makeReadOptions,
|
|
35
|
+
makeWriteOptions,
|
|
36
|
+
listUnknownFields(message) {
|
|
37
|
+
return message[unknownFieldsSymbol] ?? [];
|
|
38
|
+
},
|
|
39
|
+
discardUnknownFields(message) {
|
|
40
|
+
delete message[unknownFieldsSymbol];
|
|
41
|
+
},
|
|
42
|
+
writeUnknownFields(message, writer) {
|
|
43
|
+
const m = message;
|
|
44
|
+
const c = m[unknownFieldsSymbol];
|
|
45
|
+
if (c) {
|
|
46
|
+
for (const f of c) {
|
|
47
|
+
writer.tag(f.no, f.wireType).raw(f.data);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
onUnknownField(message, no, wireType, data) {
|
|
52
|
+
const m = message;
|
|
53
|
+
if (!Array.isArray(m[unknownFieldsSymbol])) {
|
|
54
|
+
m[unknownFieldsSymbol] = [];
|
|
55
|
+
}
|
|
56
|
+
m[unknownFieldsSymbol].push({ no, wireType, data });
|
|
57
|
+
},
|
|
58
|
+
readMessage(message, reader, length, options) {
|
|
59
|
+
const type = message.getType();
|
|
60
|
+
const end = length === undefined ? reader.len : reader.pos + length;
|
|
61
|
+
while (reader.pos < end) {
|
|
62
|
+
const [fieldNo, wireType] = reader.tag(),
|
|
63
|
+
field = type.fields.find(fieldNo);
|
|
64
|
+
if (!field) {
|
|
65
|
+
const data = reader.skip(wireType);
|
|
66
|
+
if (options.readUnknownFields) {
|
|
67
|
+
this.onUnknownField(message, fieldNo, wireType, data);
|
|
68
|
+
}
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
let target = message,
|
|
72
|
+
repeated = field.repeated,
|
|
73
|
+
localName = field.localName;
|
|
74
|
+
if (field.oneof) {
|
|
75
|
+
target = target[field.oneof.localName];
|
|
76
|
+
if (target.case != localName) {
|
|
77
|
+
delete target.value;
|
|
78
|
+
}
|
|
79
|
+
target.case = localName;
|
|
80
|
+
localName = "value";
|
|
81
|
+
}
|
|
82
|
+
switch (field.kind) {
|
|
83
|
+
case "scalar":
|
|
84
|
+
case "enum":
|
|
85
|
+
const scalarType =
|
|
86
|
+
field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
87
|
+
if (repeated) {
|
|
88
|
+
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
|
|
89
|
+
if (
|
|
90
|
+
wireType == binary_encoding_js_1.WireType.LengthDelimited &&
|
|
91
|
+
scalarType != field_js_1.ScalarType.STRING &&
|
|
92
|
+
scalarType != field_js_1.ScalarType.BYTES
|
|
93
|
+
) {
|
|
94
|
+
let e = reader.uint32() + reader.pos;
|
|
95
|
+
while (reader.pos < e) {
|
|
96
|
+
arr.push(readScalar(reader, scalarType));
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
arr.push(readScalar(reader, scalarType));
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
target[localName] = readScalar(reader, scalarType);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
case "message":
|
|
106
|
+
const messageType = field.T;
|
|
107
|
+
if (repeated) {
|
|
108
|
+
// safe to assume presence of array, oneof cannot contain repeated values
|
|
109
|
+
target[localName].push(
|
|
110
|
+
messageType.fromBinary(reader.bytes(), options)
|
|
111
|
+
);
|
|
112
|
+
} else {
|
|
113
|
+
if (target[localName] instanceof messageType) {
|
|
114
|
+
target[localName].fromBinary(reader.bytes(), options);
|
|
115
|
+
} else {
|
|
116
|
+
target[localName] = (0, field_wrapper_js_1.unwrapField)(
|
|
117
|
+
messageType,
|
|
118
|
+
messageType.fromBinary(reader.bytes(), options)
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
case "map":
|
|
124
|
+
let [mapKey, mapVal] = readMapEntry(field, reader, options);
|
|
125
|
+
// safe to assume presence of map object, oneof cannot contain repeated values
|
|
126
|
+
target[localName][mapKey] = mapVal;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
exports.makeBinaryFormatCommon = makeBinaryFormatCommon;
|
|
134
|
+
// Read a map field, expecting key field = 1, value field = 2
|
|
135
|
+
function readMapEntry(field, reader, options) {
|
|
136
|
+
const length = reader.uint32(),
|
|
137
|
+
end = reader.pos + length;
|
|
138
|
+
let key, val;
|
|
139
|
+
while (reader.pos < end) {
|
|
140
|
+
let [fieldNo] = reader.tag();
|
|
141
|
+
switch (fieldNo) {
|
|
142
|
+
case 1:
|
|
143
|
+
key = readScalar(reader, field.K);
|
|
144
|
+
break;
|
|
145
|
+
case 2:
|
|
146
|
+
switch (field.V.kind) {
|
|
147
|
+
case "scalar":
|
|
148
|
+
val = readScalar(reader, field.V.T);
|
|
149
|
+
break;
|
|
150
|
+
case "enum":
|
|
151
|
+
val = reader.int32();
|
|
152
|
+
break;
|
|
153
|
+
case "message":
|
|
154
|
+
val = field.V.T.fromBinary(reader.bytes(), options);
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (key === undefined) {
|
|
161
|
+
let keyRaw = (0, scalars_js_1.scalarDefaultValue)(field.K);
|
|
162
|
+
key = field.K == field_js_1.ScalarType.BOOL ? keyRaw.toString() : keyRaw;
|
|
163
|
+
}
|
|
164
|
+
if (typeof key != "string" && typeof key != "number") {
|
|
165
|
+
key = key.toString();
|
|
166
|
+
}
|
|
167
|
+
if (val === undefined) {
|
|
168
|
+
switch (field.V.kind) {
|
|
169
|
+
case "scalar":
|
|
170
|
+
val = (0, scalars_js_1.scalarDefaultValue)(field.V.T);
|
|
171
|
+
break;
|
|
172
|
+
case "enum":
|
|
173
|
+
val = 0;
|
|
174
|
+
break;
|
|
175
|
+
case "message":
|
|
176
|
+
val = new field.V.T();
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return [key, val];
|
|
181
|
+
}
|
|
182
|
+
function readScalar(reader, type) {
|
|
183
|
+
let [, method] = (0, scalars_js_1.scalarTypeInfo)(type);
|
|
184
|
+
return reader[method]();
|
|
185
|
+
}
|
|
186
|
+
function writeMapEntry(writer, options, field, key, value) {
|
|
187
|
+
writer.tag(field.no, binary_encoding_js_1.WireType.LengthDelimited);
|
|
188
|
+
writer.fork();
|
|
189
|
+
// javascript only allows number or string for object properties
|
|
190
|
+
// we convert from our representation to the protobuf type
|
|
191
|
+
let keyValue = key;
|
|
192
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we deliberately handle just the special cases for map keys
|
|
193
|
+
switch (field.K) {
|
|
194
|
+
case field_js_1.ScalarType.INT32:
|
|
195
|
+
case field_js_1.ScalarType.FIXED32:
|
|
196
|
+
case field_js_1.ScalarType.UINT32:
|
|
197
|
+
case field_js_1.ScalarType.SFIXED32:
|
|
198
|
+
case field_js_1.ScalarType.SINT32:
|
|
199
|
+
keyValue = Number.parseInt(key);
|
|
200
|
+
break;
|
|
201
|
+
case field_js_1.ScalarType.BOOL:
|
|
202
|
+
(0, assert_js_1.assert)(key == "true" || key == "false");
|
|
203
|
+
keyValue = key == "true";
|
|
204
|
+
break;
|
|
205
|
+
}
|
|
206
|
+
// write key, expecting key field number = 1
|
|
207
|
+
writeScalar(writer, field.K, 1, keyValue, true);
|
|
208
|
+
// write value, expecting value field number = 2
|
|
209
|
+
switch (field.V.kind) {
|
|
210
|
+
case "scalar":
|
|
211
|
+
writeScalar(writer, field.V.T, 2, value, true);
|
|
212
|
+
break;
|
|
213
|
+
case "enum":
|
|
214
|
+
writeScalar(writer, field_js_1.ScalarType.INT32, 2, value, true);
|
|
215
|
+
break;
|
|
216
|
+
case "message":
|
|
217
|
+
writeMessageField(writer, options, field.V.T, 2, value);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
writer.join();
|
|
221
|
+
}
|
|
222
|
+
exports.writeMapEntry = writeMapEntry;
|
|
223
|
+
function writeMessageField(writer, options, type, fieldNo, value) {
|
|
224
|
+
if (value !== undefined) {
|
|
225
|
+
const message = (0, field_wrapper_js_1.wrapField)(type, value);
|
|
226
|
+
writer
|
|
227
|
+
.tag(fieldNo, binary_encoding_js_1.WireType.LengthDelimited)
|
|
228
|
+
.bytes(message.toBinary(options));
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.writeMessageField = writeMessageField;
|
|
232
|
+
function writeScalar(writer, type, fieldNo, value, emitIntrinsicDefault) {
|
|
233
|
+
let [wireType, method, isIntrinsicDefault] = (0, scalars_js_1.scalarTypeInfo)(
|
|
234
|
+
type,
|
|
235
|
+
value
|
|
236
|
+
);
|
|
237
|
+
if (!isIntrinsicDefault || emitIntrinsicDefault) {
|
|
238
|
+
writer.tag(fieldNo, wireType)[method](value);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.writeScalar = writeScalar;
|
|
242
|
+
function writePacked(writer, type, fieldNo, value) {
|
|
243
|
+
if (!value.length) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
writer.tag(fieldNo, binary_encoding_js_1.WireType.LengthDelimited).fork();
|
|
247
|
+
let [, method] = (0, scalars_js_1.scalarTypeInfo)(type);
|
|
248
|
+
for (let i = 0; i < value.length; i++) {
|
|
249
|
+
writer[method](value[i]);
|
|
250
|
+
}
|
|
251
|
+
writer.join();
|
|
252
|
+
}
|
|
253
|
+
exports.writePacked = writePacked;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeBinaryFormatProto2 = void 0;
|
|
4
|
+
const field_js_1 = require("../field.js");
|
|
5
|
+
const binary_format_common_js_1 = require("./binary-format-common.js");
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, no-case-declarations, prefer-const */
|
|
7
|
+
function makeBinaryFormatProto2() {
|
|
8
|
+
return {
|
|
9
|
+
...(0, binary_format_common_js_1.makeBinaryFormatCommon)(),
|
|
10
|
+
writeMessage(message, writer, options) {
|
|
11
|
+
const type = message.getType();
|
|
12
|
+
let field;
|
|
13
|
+
try {
|
|
14
|
+
for (field of type.fields.byNumber()) {
|
|
15
|
+
let value, // this will be our field value, whether it is member of a oneof or not
|
|
16
|
+
repeated = field.repeated,
|
|
17
|
+
localName = field.localName;
|
|
18
|
+
if (field.oneof) {
|
|
19
|
+
const oneof = message[field.oneof.localName];
|
|
20
|
+
if (oneof.case !== localName) {
|
|
21
|
+
continue; // field is not selected, skip
|
|
22
|
+
}
|
|
23
|
+
value = oneof.value;
|
|
24
|
+
} else {
|
|
25
|
+
value = message[localName];
|
|
26
|
+
// In contrast to proto3, we raise an error if a non-optional (proto2 required)
|
|
27
|
+
// field is missing a value.
|
|
28
|
+
if (value === undefined && !field.oneof && !field.opt) {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`cannot encode field ${type.typeName}.${field.name} to JSON: required field not set`
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
switch (field.kind) {
|
|
35
|
+
case "scalar":
|
|
36
|
+
case "enum":
|
|
37
|
+
let scalarType =
|
|
38
|
+
field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
39
|
+
if (repeated) {
|
|
40
|
+
if (field.packed) {
|
|
41
|
+
(0, binary_format_common_js_1.writePacked)(
|
|
42
|
+
writer,
|
|
43
|
+
scalarType,
|
|
44
|
+
field.no,
|
|
45
|
+
value
|
|
46
|
+
);
|
|
47
|
+
} else {
|
|
48
|
+
for (const item of value) {
|
|
49
|
+
(0, binary_format_common_js_1.writeScalar)(
|
|
50
|
+
writer,
|
|
51
|
+
scalarType,
|
|
52
|
+
field.no,
|
|
53
|
+
item,
|
|
54
|
+
true
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
if (value !== undefined) {
|
|
60
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
61
|
+
// Explicit default values are not special cased either.
|
|
62
|
+
(0, binary_format_common_js_1.writeScalar)(
|
|
63
|
+
writer,
|
|
64
|
+
scalarType,
|
|
65
|
+
field.no,
|
|
66
|
+
value,
|
|
67
|
+
true
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case "message":
|
|
73
|
+
if (repeated) {
|
|
74
|
+
for (const item of value) {
|
|
75
|
+
(0, binary_format_common_js_1.writeMessageField)(
|
|
76
|
+
writer,
|
|
77
|
+
options,
|
|
78
|
+
field.T,
|
|
79
|
+
field.no,
|
|
80
|
+
item
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
} else {
|
|
84
|
+
(0, binary_format_common_js_1.writeMessageField)(
|
|
85
|
+
writer,
|
|
86
|
+
options,
|
|
87
|
+
field.T,
|
|
88
|
+
field.no,
|
|
89
|
+
value
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
case "map":
|
|
94
|
+
for (const [key, val] of Object.entries(value)) {
|
|
95
|
+
(0, binary_format_common_js_1.writeMapEntry)(
|
|
96
|
+
writer,
|
|
97
|
+
options,
|
|
98
|
+
field,
|
|
99
|
+
key,
|
|
100
|
+
val
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} catch (e) {
|
|
107
|
+
let m = field
|
|
108
|
+
? `cannot encode field ${type.typeName}.${field?.name} to binary`
|
|
109
|
+
: `cannot encode message ${type.typeName} to binary`;
|
|
110
|
+
let r = e instanceof Error ? e.message : String(e);
|
|
111
|
+
throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
|
|
112
|
+
}
|
|
113
|
+
if (options.writeUnknownFields) {
|
|
114
|
+
this.writeUnknownFields(message, writer);
|
|
115
|
+
}
|
|
116
|
+
return writer;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
exports.makeBinaryFormatProto2 = makeBinaryFormatProto2;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeBinaryFormatProto3 = void 0;
|
|
4
|
+
const field_js_1 = require("../field.js");
|
|
5
|
+
const binary_format_common_js_1 = require("./binary-format-common.js");
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, prefer-const, no-case-declarations */
|
|
7
|
+
function makeBinaryFormatProto3() {
|
|
8
|
+
return {
|
|
9
|
+
...(0, binary_format_common_js_1.makeBinaryFormatCommon)(),
|
|
10
|
+
writeMessage(message, writer, options) {
|
|
11
|
+
const type = message.getType();
|
|
12
|
+
for (const field of type.fields.byNumber()) {
|
|
13
|
+
let value, // this will be our field value, whether it is member of a oneof or regular field
|
|
14
|
+
repeated = field.repeated,
|
|
15
|
+
localName = field.localName;
|
|
16
|
+
if (field.oneof) {
|
|
17
|
+
const oneof = message[field.oneof.localName];
|
|
18
|
+
if (oneof.case !== localName) {
|
|
19
|
+
continue; // field is not selected, skip
|
|
20
|
+
}
|
|
21
|
+
value = oneof.value;
|
|
22
|
+
} else {
|
|
23
|
+
value = message[localName];
|
|
24
|
+
}
|
|
25
|
+
switch (field.kind) {
|
|
26
|
+
case "scalar":
|
|
27
|
+
case "enum":
|
|
28
|
+
let scalarType =
|
|
29
|
+
field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
30
|
+
if (repeated) {
|
|
31
|
+
if (field.packed) {
|
|
32
|
+
(0, binary_format_common_js_1.writePacked)(
|
|
33
|
+
writer,
|
|
34
|
+
scalarType,
|
|
35
|
+
field.no,
|
|
36
|
+
value
|
|
37
|
+
);
|
|
38
|
+
} else {
|
|
39
|
+
for (const item of value) {
|
|
40
|
+
(0, binary_format_common_js_1.writeScalar)(
|
|
41
|
+
writer,
|
|
42
|
+
scalarType,
|
|
43
|
+
field.no,
|
|
44
|
+
item,
|
|
45
|
+
true
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
if (value !== undefined) {
|
|
51
|
+
(0, binary_format_common_js_1.writeScalar)(
|
|
52
|
+
writer,
|
|
53
|
+
scalarType,
|
|
54
|
+
field.no,
|
|
55
|
+
value,
|
|
56
|
+
!!field.oneof || field.opt
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case "message":
|
|
62
|
+
if (repeated) {
|
|
63
|
+
for (const item of value) {
|
|
64
|
+
(0, binary_format_common_js_1.writeMessageField)(
|
|
65
|
+
writer,
|
|
66
|
+
options,
|
|
67
|
+
field.T,
|
|
68
|
+
field.no,
|
|
69
|
+
item
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
(0, binary_format_common_js_1.writeMessageField)(
|
|
74
|
+
writer,
|
|
75
|
+
options,
|
|
76
|
+
field.T,
|
|
77
|
+
field.no,
|
|
78
|
+
value
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case "map":
|
|
83
|
+
for (const [key, val] of Object.entries(value)) {
|
|
84
|
+
(0, binary_format_common_js_1.writeMapEntry)(
|
|
85
|
+
writer,
|
|
86
|
+
options,
|
|
87
|
+
field,
|
|
88
|
+
key,
|
|
89
|
+
val
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (options.writeUnknownFields) {
|
|
96
|
+
this.writeUnknownFields(message, writer);
|
|
97
|
+
}
|
|
98
|
+
return writer;
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
exports.makeBinaryFormatProto3 = makeBinaryFormatProto3;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.makeEnumType = exports.setEnumType = exports.getEnumType = void 0;
|
|
4
|
+
const assert_js_1 = require("./assert.js");
|
|
5
|
+
const enumTypeSymbol = Symbol("@bufbuild/protobuf/enum-type");
|
|
6
|
+
/**
|
|
7
|
+
* Get reflection information from a generated enum.
|
|
8
|
+
* If this function is called on something other than a generated
|
|
9
|
+
* enum, it raises an error.
|
|
10
|
+
*/
|
|
11
|
+
function getEnumType(enumObject) {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any
|
|
13
|
+
const t = enumObject[enumTypeSymbol];
|
|
14
|
+
(0, assert_js_1.assert)(t, "missing enum type on enum object");
|
|
15
|
+
return t; // eslint-disable-line @typescript-eslint/no-unsafe-return
|
|
16
|
+
}
|
|
17
|
+
exports.getEnumType = getEnumType;
|
|
18
|
+
/**
|
|
19
|
+
* Sets reflection information on a generated enum.
|
|
20
|
+
*/
|
|
21
|
+
function setEnumType(
|
|
22
|
+
enumObject,
|
|
23
|
+
typeName,
|
|
24
|
+
values
|
|
25
|
+
// We do not surface options at this time
|
|
26
|
+
// opt?: {
|
|
27
|
+
// options?: { readonly [extensionName: string]: JsonValue };
|
|
28
|
+
// },
|
|
29
|
+
) {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
31
|
+
enumObject[enumTypeSymbol] = makeEnumType(typeName, values);
|
|
32
|
+
}
|
|
33
|
+
exports.setEnumType = setEnumType;
|
|
34
|
+
// We do not surface options at this time
|
|
35
|
+
// export type PartialEnumValue = Omit<EnumValueInfo, 'options'> & Partial<Pick<EnumValueInfo, "options">>;
|
|
36
|
+
/**
|
|
37
|
+
* Create a new EnumType with the given values.
|
|
38
|
+
*/
|
|
39
|
+
function makeEnumType(
|
|
40
|
+
typeName,
|
|
41
|
+
values
|
|
42
|
+
// We do not surface options at this time
|
|
43
|
+
// opt?: {
|
|
44
|
+
// options?: { readonly [extensionName: string]: JsonValue };
|
|
45
|
+
// },
|
|
46
|
+
) {
|
|
47
|
+
const names = Object.create(null);
|
|
48
|
+
const numbers = Object.create(null);
|
|
49
|
+
const normalValues = [];
|
|
50
|
+
for (const value of values) {
|
|
51
|
+
// We do not surface options at this time
|
|
52
|
+
// const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
|
|
53
|
+
normalValues.push(value);
|
|
54
|
+
names[value.name] = value;
|
|
55
|
+
numbers[value.no] = value;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
typeName,
|
|
59
|
+
values: normalValues,
|
|
60
|
+
// We do not surface options at this time
|
|
61
|
+
// options: opt?.options ?? Object.create(null),
|
|
62
|
+
findName(name) {
|
|
63
|
+
return names[name];
|
|
64
|
+
},
|
|
65
|
+
findNumber(no) {
|
|
66
|
+
return numbers[no];
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
exports.makeEnumType = makeEnumType;
|