@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.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 +21 -1
- package/dist/cjs/binary-encoding.js +402 -410
- package/dist/cjs/descriptor-registry.js +407 -449
- package/dist/cjs/descriptor-set.js +416 -513
- package/dist/cjs/field.js +30 -30
- package/dist/cjs/google/protobuf/any_pb.js +122 -140
- package/dist/cjs/google/protobuf/api_pb.js +177 -224
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
- package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
- package/dist/cjs/google/protobuf/duration_pb.js +72 -88
- package/dist/cjs/google/protobuf/empty_pb.js +17 -17
- package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
- package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
- package/dist/cjs/google/protobuf/struct_pb.js +144 -181
- package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
- package/dist/cjs/google/protobuf/type_pb.js +406 -462
- package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
- package/dist/cjs/index.js +24 -122
- package/dist/cjs/message.js +81 -89
- package/dist/cjs/private/assert.js +20 -26
- package/dist/cjs/private/base64.js +79 -78
- package/dist/cjs/private/binary-format-common.js +195 -208
- package/dist/cjs/private/binary-format-proto2.js +75 -109
- package/dist/cjs/private/binary-format-proto3.js +57 -90
- package/dist/cjs/private/enum.js +38 -43
- package/dist/cjs/private/field-list.js +51 -50
- package/dist/cjs/private/field-wrapper.js +8 -10
- package/dist/cjs/private/field.js +22 -25
- package/dist/cjs/private/goog-varint.js +275 -0
- package/dist/cjs/private/json-format-common.js +414 -440
- package/dist/cjs/private/json-format-proto2.js +78 -89
- package/dist/cjs/private/json-format-proto3.js +69 -93
- package/dist/cjs/private/message-type.js +26 -27
- package/dist/cjs/private/names.js +58 -62
- package/dist/cjs/private/proto-runtime.js +11 -16
- package/dist/cjs/private/scalars.js +100 -103
- package/dist/cjs/private/util-common.js +180 -210
- package/dist/cjs/proto-int64.js +111 -112
- package/dist/cjs/proto2.js +50 -63
- package/dist/cjs/proto3.js +61 -74
- package/dist/cjs/service-type.js +14 -17
- package/dist/cjs/type-registry.js +33 -31
- package/dist/esm/binary-encoding.js +401 -414
- package/dist/esm/descriptor-registry.js +409 -458
- package/dist/esm/descriptor-set.js +417 -478
- package/dist/esm/field.js +29 -29
- package/dist/esm/google/protobuf/any_pb.js +122 -140
- package/dist/esm/google/protobuf/api_pb.js +177 -197
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
- package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
- package/dist/esm/google/protobuf/duration_pb.js +72 -88
- package/dist/esm/google/protobuf/empty_pb.js +17 -17
- package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
- package/dist/esm/google/protobuf/source_context_pb.js +25 -25
- package/dist/esm/google/protobuf/struct_pb.js +143 -180
- package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
- package/dist/esm/google/protobuf/type_pb.js +401 -421
- package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
- package/dist/esm/index.js +1 -1
- package/dist/esm/message.js +81 -89
- package/dist/esm/private/assert.js +19 -21
- package/dist/esm/private/base64.js +79 -78
- package/dist/esm/private/binary-format-common.js +196 -207
- package/dist/esm/private/binary-format-proto2.js +76 -81
- package/dist/esm/private/binary-format-proto3.js +58 -67
- package/dist/esm/private/enum.js +38 -43
- package/dist/esm/private/field-list.js +51 -50
- package/dist/esm/private/field-wrapper.js +8 -10
- package/dist/esm/private/field.js +22 -22
- package/dist/esm/private/goog-varint.js +266 -0
- package/dist/esm/private/json-format-common.js +414 -435
- package/dist/esm/private/json-format-proto2.js +76 -81
- package/dist/esm/private/json-format-proto3.js +66 -80
- package/dist/esm/private/message-type.js +27 -28
- package/dist/esm/private/names.js +57 -57
- package/dist/esm/private/proto-runtime.js +11 -11
- package/dist/esm/private/scalars.js +99 -99
- package/dist/esm/private/util-common.js +180 -192
- package/dist/esm/proto-int64.js +111 -112
- package/dist/esm/proto2.js +50 -57
- package/dist/esm/proto3.js +61 -68
- package/dist/esm/service-type.js +12 -12
- package/dist/esm/type-registry.js +33 -31
- package/dist/types/binary-encoding.d.ts +398 -398
- package/dist/types/binary-format.d.ts +69 -91
- package/dist/types/descriptor-registry.d.ts +23 -29
- package/dist/types/descriptor-set.d.ts +107 -128
- package/dist/types/enum.d.ts +21 -21
- package/dist/types/field-list.d.ts +21 -21
- package/dist/types/field.d.ts +191 -231
- package/dist/types/google/protobuf/any_pb.d.ts +56 -77
- package/dist/types/google/protobuf/api_pb.d.ts +144 -187
- package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
- package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
- package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
- package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
- package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
- package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
- package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
- package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
- package/dist/types/google/protobuf/type_pb.d.ts +355 -422
- package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
- package/dist/types/index.d.ts +4 -27
- package/dist/types/json-format.d.ts +70 -89
- package/dist/types/message-type.d.ts +38 -46
- package/dist/types/message.d.ts +73 -99
- package/dist/types/private/assert.d.ts +1 -4
- package/dist/types/private/binary-format-common.d.ts +6 -32
- package/dist/types/private/enum.d.ts +4 -11
- package/dist/types/private/field-list.d.ts +14 -21
- package/dist/types/private/field-wrapper.d.ts +5 -11
- package/dist/types/private/field.d.ts +12 -12
- package/dist/types/{google/varint.d.ts → private/goog-varint.d.ts} +6 -13
- package/dist/types/private/json-format-common.d.ts +5 -27
- package/dist/types/private/message-type.d.ts +3 -8
- package/dist/types/private/names.d.ts +1 -4
- package/dist/types/private/options-map.d.ts +1 -1
- package/dist/types/private/proto-runtime.d.ts +29 -42
- package/dist/types/private/scalars.d.ts +5 -12
- package/dist/types/private/util-common.d.ts +1 -4
- package/dist/types/private/util.d.ts +29 -41
- package/dist/types/proto-int64.d.ts +42 -42
- package/dist/types/service-type.d.ts +37 -57
- package/dist/types/type-registry.d.ts +13 -15
- package/package.json +2 -2
- package/dist/cjs/google/varint.js +0 -280
- package/dist/esm/google/varint.js +0 -265
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
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;
|
|
3
|
+
exports.writePacked = exports.writeScalar = exports.writeMessageField = exports.writeMapEntry = exports.makeBinaryFormatCommon = void 0;
|
|
9
4
|
const binary_encoding_js_1 = require("../binary-encoding.js");
|
|
10
5
|
const field_js_1 = require("../field.js");
|
|
11
6
|
const field_wrapper_js_1 = require("./field-wrapper.js");
|
|
@@ -15,239 +10,231 @@ const assert_js_1 = require("./assert.js");
|
|
|
15
10
|
const unknownFieldsSymbol = Symbol("@bufbuild/protobuf/unknown-fields");
|
|
16
11
|
// Default options for parsing binary data.
|
|
17
12
|
const readDefaults = {
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
readUnknownFields: true,
|
|
14
|
+
readerFactory: (bytes) => new binary_encoding_js_1.BinaryReader(bytes),
|
|
20
15
|
};
|
|
21
16
|
// Default options for serializing binary data.
|
|
22
17
|
const writeDefaults = {
|
|
23
|
-
|
|
24
|
-
|
|
18
|
+
writeUnknownFields: true,
|
|
19
|
+
writerFactory: () => new binary_encoding_js_1.BinaryWriter(),
|
|
25
20
|
};
|
|
26
21
|
function makeReadOptions(options) {
|
|
27
|
-
|
|
22
|
+
return options ? { ...readDefaults, ...options } : readDefaults;
|
|
28
23
|
}
|
|
29
24
|
function makeWriteOptions(options) {
|
|
30
|
-
|
|
25
|
+
return options ? { ...writeDefaults, ...options } : writeDefaults;
|
|
31
26
|
}
|
|
32
27
|
function makeBinaryFormatCommon() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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));
|
|
28
|
+
return {
|
|
29
|
+
makeReadOptions,
|
|
30
|
+
makeWriteOptions,
|
|
31
|
+
listUnknownFields(message) {
|
|
32
|
+
return message[unknownFieldsSymbol] ?? [];
|
|
33
|
+
},
|
|
34
|
+
discardUnknownFields(message) {
|
|
35
|
+
delete message[unknownFieldsSymbol];
|
|
36
|
+
},
|
|
37
|
+
writeUnknownFields(message, writer) {
|
|
38
|
+
const m = message;
|
|
39
|
+
const c = m[unknownFieldsSymbol];
|
|
40
|
+
if (c) {
|
|
41
|
+
for (const f of c) {
|
|
42
|
+
writer.tag(f.no, f.wireType).raw(f.data);
|
|
97
43
|
}
|
|
98
|
-
} else {
|
|
99
|
-
arr.push(readScalar(reader, scalarType));
|
|
100
|
-
}
|
|
101
|
-
} else {
|
|
102
|
-
target[localName] = readScalar(reader, scalarType);
|
|
103
44
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
if (
|
|
108
|
-
|
|
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
|
-
}
|
|
45
|
+
},
|
|
46
|
+
onUnknownField(message, no, wireType, data) {
|
|
47
|
+
const m = message;
|
|
48
|
+
if (!Array.isArray(m[unknownFieldsSymbol])) {
|
|
49
|
+
m[unknownFieldsSymbol] = [];
|
|
121
50
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
51
|
+
m[unknownFieldsSymbol].push({ no, wireType, data });
|
|
52
|
+
},
|
|
53
|
+
readMessage(message, reader, length, options) {
|
|
54
|
+
const type = message.getType();
|
|
55
|
+
const end = length === undefined ? reader.len : reader.pos + length;
|
|
56
|
+
while (reader.pos < end) {
|
|
57
|
+
const [fieldNo, wireType] = reader.tag(), field = type.fields.find(fieldNo);
|
|
58
|
+
if (!field) {
|
|
59
|
+
const data = reader.skip(wireType);
|
|
60
|
+
if (options.readUnknownFields) {
|
|
61
|
+
this.onUnknownField(message, fieldNo, wireType, data);
|
|
62
|
+
}
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
let target = message, repeated = field.repeated, localName = field.localName;
|
|
66
|
+
if (field.oneof) {
|
|
67
|
+
target = target[field.oneof.localName];
|
|
68
|
+
if (target.case != localName) {
|
|
69
|
+
delete target.value;
|
|
70
|
+
}
|
|
71
|
+
target.case = localName;
|
|
72
|
+
localName = "value";
|
|
73
|
+
}
|
|
74
|
+
switch (field.kind) {
|
|
75
|
+
case "scalar":
|
|
76
|
+
case "enum":
|
|
77
|
+
const scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
78
|
+
if (repeated) {
|
|
79
|
+
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
|
|
80
|
+
if (wireType == binary_encoding_js_1.WireType.LengthDelimited &&
|
|
81
|
+
scalarType != field_js_1.ScalarType.STRING &&
|
|
82
|
+
scalarType != field_js_1.ScalarType.BYTES) {
|
|
83
|
+
let e = reader.uint32() + reader.pos;
|
|
84
|
+
while (reader.pos < e) {
|
|
85
|
+
arr.push(readScalar(reader, scalarType));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
arr.push(readScalar(reader, scalarType));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
target[localName] = readScalar(reader, scalarType);
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
case "message":
|
|
97
|
+
const messageType = field.T;
|
|
98
|
+
if (repeated) {
|
|
99
|
+
// safe to assume presence of array, oneof cannot contain repeated values
|
|
100
|
+
target[localName].push(messageType.fromBinary(reader.bytes(), options));
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
if (target[localName] instanceof messageType) {
|
|
104
|
+
target[localName].fromBinary(reader.bytes(), options);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
target[localName] = (0, field_wrapper_js_1.unwrapField)(messageType, messageType.fromBinary(reader.bytes(), options));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
case "map":
|
|
112
|
+
let [mapKey, mapVal] = readMapEntry(field, reader, options);
|
|
113
|
+
// safe to assume presence of map object, oneof cannot contain repeated values
|
|
114
|
+
target[localName][mapKey] = mapVal;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
};
|
|
132
120
|
}
|
|
133
121
|
exports.makeBinaryFormatCommon = makeBinaryFormatCommon;
|
|
134
122
|
// Read a map field, expecting key field = 1, value field = 2
|
|
135
123
|
function readMapEntry(field, reader, options) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
124
|
+
const length = reader.uint32(), end = reader.pos + length;
|
|
125
|
+
let key, val;
|
|
126
|
+
while (reader.pos < end) {
|
|
127
|
+
let [fieldNo] = reader.tag();
|
|
128
|
+
switch (fieldNo) {
|
|
129
|
+
case 1:
|
|
130
|
+
key = readScalar(reader, field.K);
|
|
131
|
+
break;
|
|
132
|
+
case 2:
|
|
133
|
+
switch (field.V.kind) {
|
|
134
|
+
case "scalar":
|
|
135
|
+
val = readScalar(reader, field.V.T);
|
|
136
|
+
break;
|
|
137
|
+
case "enum":
|
|
138
|
+
val = reader.int32();
|
|
139
|
+
break;
|
|
140
|
+
case "message":
|
|
141
|
+
val = field.V.T.fromBinary(reader.bytes(), options);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
156
145
|
}
|
|
157
|
-
break;
|
|
158
146
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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;
|
|
147
|
+
if (key === undefined) {
|
|
148
|
+
let keyRaw = (0, scalars_js_1.scalarDefaultValue)(field.K);
|
|
149
|
+
key =
|
|
150
|
+
field.K == field_js_1.ScalarType.BOOL
|
|
151
|
+
? keyRaw.toString()
|
|
152
|
+
: keyRaw;
|
|
178
153
|
}
|
|
179
|
-
|
|
180
|
-
|
|
154
|
+
if (typeof key != "string" && typeof key != "number") {
|
|
155
|
+
key = key.toString();
|
|
156
|
+
}
|
|
157
|
+
if (val === undefined) {
|
|
158
|
+
switch (field.V.kind) {
|
|
159
|
+
case "scalar":
|
|
160
|
+
val = (0, scalars_js_1.scalarDefaultValue)(field.V.T);
|
|
161
|
+
break;
|
|
162
|
+
case "enum":
|
|
163
|
+
val = 0;
|
|
164
|
+
break;
|
|
165
|
+
case "message":
|
|
166
|
+
val = new field.V.T();
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return [key, val];
|
|
181
171
|
}
|
|
182
172
|
function readScalar(reader, type) {
|
|
183
|
-
|
|
184
|
-
|
|
173
|
+
let [, method] = (0, scalars_js_1.scalarTypeInfo)(type);
|
|
174
|
+
return reader[method]();
|
|
185
175
|
}
|
|
186
176
|
function writeMapEntry(writer, options, field, key, value) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
177
|
+
writer.tag(field.no, binary_encoding_js_1.WireType.LengthDelimited);
|
|
178
|
+
writer.fork();
|
|
179
|
+
// javascript only allows number or string for object properties
|
|
180
|
+
// we convert from our representation to the protobuf type
|
|
181
|
+
let keyValue = key;
|
|
182
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we deliberately handle just the special cases for map keys
|
|
183
|
+
switch (field.K) {
|
|
184
|
+
case field_js_1.ScalarType.INT32:
|
|
185
|
+
case field_js_1.ScalarType.FIXED32:
|
|
186
|
+
case field_js_1.ScalarType.UINT32:
|
|
187
|
+
case field_js_1.ScalarType.SFIXED32:
|
|
188
|
+
case field_js_1.ScalarType.SINT32:
|
|
189
|
+
keyValue = Number.parseInt(key);
|
|
190
|
+
break;
|
|
191
|
+
case field_js_1.ScalarType.BOOL:
|
|
192
|
+
(0, assert_js_1.assert)(key == "true" || key == "false");
|
|
193
|
+
keyValue = key == "true";
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
// write key, expecting key field number = 1
|
|
197
|
+
writeScalar(writer, field.K, 1, keyValue, true);
|
|
198
|
+
// write value, expecting value field number = 2
|
|
199
|
+
switch (field.V.kind) {
|
|
200
|
+
case "scalar":
|
|
201
|
+
writeScalar(writer, field.V.T, 2, value, true);
|
|
202
|
+
break;
|
|
203
|
+
case "enum":
|
|
204
|
+
writeScalar(writer, field_js_1.ScalarType.INT32, 2, value, true);
|
|
205
|
+
break;
|
|
206
|
+
case "message":
|
|
207
|
+
writeMessageField(writer, options, field.V.T, 2, value);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
writer.join();
|
|
221
211
|
}
|
|
222
212
|
exports.writeMapEntry = writeMapEntry;
|
|
223
213
|
function writeMessageField(writer, options, type, fieldNo, value) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
214
|
+
if (value !== undefined) {
|
|
215
|
+
const message = (0, field_wrapper_js_1.wrapField)(type, value);
|
|
216
|
+
writer
|
|
217
|
+
.tag(fieldNo, binary_encoding_js_1.WireType.LengthDelimited)
|
|
218
|
+
.bytes(message.toBinary(options));
|
|
219
|
+
}
|
|
230
220
|
}
|
|
231
221
|
exports.writeMessageField = writeMessageField;
|
|
232
222
|
function writeScalar(writer, type, fieldNo, value, emitIntrinsicDefault) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (!isIntrinsicDefault || emitIntrinsicDefault) {
|
|
238
|
-
writer.tag(fieldNo, wireType)[method](value);
|
|
239
|
-
}
|
|
223
|
+
let [wireType, method, isIntrinsicDefault] = (0, scalars_js_1.scalarTypeInfo)(type, value);
|
|
224
|
+
if (!isIntrinsicDefault || emitIntrinsicDefault) {
|
|
225
|
+
writer.tag(fieldNo, wireType)[method](value);
|
|
226
|
+
}
|
|
240
227
|
}
|
|
241
228
|
exports.writeScalar = writeScalar;
|
|
242
229
|
function writePacked(writer, type, fieldNo, value) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
230
|
+
if (!value.length) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
writer.tag(fieldNo, binary_encoding_js_1.WireType.LengthDelimited).fork();
|
|
234
|
+
let [, method] = (0, scalars_js_1.scalarTypeInfo)(type);
|
|
235
|
+
for (let i = 0; i < value.length; i++) {
|
|
236
|
+
writer[method](value[i]);
|
|
237
|
+
}
|
|
238
|
+
writer.join();
|
|
252
239
|
}
|
|
253
240
|
exports.writePacked = writePacked;
|
|
@@ -5,116 +5,82 @@ const field_js_1 = require("../field.js");
|
|
|
5
5
|
const binary_format_common_js_1 = require("./binary-format-common.js");
|
|
6
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
7
|
function makeBinaryFormatProto2() {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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, localName = field.localName;
|
|
17
|
+
if (field.oneof) {
|
|
18
|
+
const oneof = message[field.oneof.localName];
|
|
19
|
+
if (oneof.case !== localName) {
|
|
20
|
+
continue; // field is not selected, skip
|
|
21
|
+
}
|
|
22
|
+
value = oneof.value;
|
|
23
|
+
}
|
|
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(`cannot encode field ${type.typeName}.${field.name} to JSON: required field not set`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
switch (field.kind) {
|
|
33
|
+
case "scalar":
|
|
34
|
+
case "enum":
|
|
35
|
+
let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
36
|
+
if (repeated) {
|
|
37
|
+
if (field.packed) {
|
|
38
|
+
(0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
for (const item of value) {
|
|
42
|
+
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
if (value !== undefined) {
|
|
48
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
49
|
+
// Explicit default values are not special cased either.
|
|
50
|
+
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, true);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
break;
|
|
54
|
+
case "message":
|
|
55
|
+
if (repeated) {
|
|
56
|
+
for (const item of value) {
|
|
57
|
+
(0, binary_format_common_js_1.writeMessageField)(writer, options, field.T, field.no, item);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
(0, binary_format_common_js_1.writeMessageField)(writer, options, field.T, field.no, value);
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
64
|
+
case "map":
|
|
65
|
+
for (const [key, val] of Object.entries(value)) {
|
|
66
|
+
(0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
|
|
67
|
+
}
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
22
71
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
throw new Error(
|
|
30
|
-
`cannot encode field ${type.typeName}.${field.name} to JSON: required field not set`
|
|
31
|
-
);
|
|
72
|
+
catch (e) {
|
|
73
|
+
let m = field
|
|
74
|
+
? `cannot encode field ${type.typeName}.${field?.name} to binary`
|
|
75
|
+
: `cannot encode message ${type.typeName} to binary`;
|
|
76
|
+
let r = e instanceof Error ? e.message : String(e);
|
|
77
|
+
throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
|
|
32
78
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
};
|
|
79
|
+
if (options.writeUnknownFields) {
|
|
80
|
+
this.writeUnknownFields(message, writer);
|
|
81
|
+
}
|
|
82
|
+
return writer;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
119
85
|
}
|
|
120
86
|
exports.makeBinaryFormatProto2 = makeBinaryFormatProto2;
|