@aptre/protobuf-es-lite 0.1.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/dist/binary.js ADDED
@@ -0,0 +1,389 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeMapEntry = exports.writeMessage = exports.readMessage = exports.makeWriteOptions = exports.makeReadOptions = exports.readScalarLTString = exports.readScalar = exports.readMapEntry = exports.readField = void 0;
4
+ const protobuf_1 = require("@bufbuild/protobuf");
5
+ const field_js_1 = require("./field.js");
6
+ const is_message_js_1 = require("./is-message.js");
7
+ const unknown_js_1 = require("./unknown.js");
8
+ const field_wrapper_js_1 = require("./field-wrapper.js");
9
+ const scalar_js_1 = require("./scalar.js");
10
+ const assert_js_1 = require("./assert.js");
11
+ function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
12
+ reader, field, wireType, options) {
13
+ let { repeated, localName } = field;
14
+ if (field.oneof) {
15
+ target = target[field.oneof.localName];
16
+ if (target.case != localName) {
17
+ delete target.value;
18
+ }
19
+ target.case = localName;
20
+ localName = "value";
21
+ }
22
+ switch (field.kind) {
23
+ case "scalar":
24
+ case "enum":
25
+ const scalarType = field.kind == "enum" ? protobuf_1.ScalarType.INT32 : field.T;
26
+ let read = readScalar;
27
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
28
+ if (field.kind == "scalar" && field.L > 0) {
29
+ read = readScalarLTString;
30
+ }
31
+ if (repeated) {
32
+ let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
33
+ const isPacked = wireType == protobuf_1.WireType.LengthDelimited &&
34
+ scalarType != protobuf_1.ScalarType.STRING &&
35
+ scalarType != protobuf_1.ScalarType.BYTES;
36
+ if (isPacked) {
37
+ let e = reader.uint32() + reader.pos;
38
+ while (reader.pos < e) {
39
+ arr.push(read(reader, scalarType));
40
+ }
41
+ }
42
+ else {
43
+ arr.push(read(reader, scalarType));
44
+ }
45
+ }
46
+ else {
47
+ target[localName] = read(reader, scalarType);
48
+ }
49
+ break;
50
+ case "message":
51
+ const messageType = field.T;
52
+ if (repeated) {
53
+ // safe to assume presence of array, oneof cannot contain repeated values
54
+ target[localName].push(readMessageField(reader, messageType.create(), messageType.fields, options, field));
55
+ }
56
+ else {
57
+ if ((0, is_message_js_1.isCompleteMessage)(target[localName], messageType.fields.list())) {
58
+ readMessageField(reader, target[localName], messageType.fields, options, field);
59
+ }
60
+ else {
61
+ target[localName] = readMessageField(reader, messageType.create(), messageType.fields, options, field);
62
+ if (messageType.fieldWrapper && !field.oneof && !field.repeated) {
63
+ target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
64
+ }
65
+ }
66
+ }
67
+ break;
68
+ case "map":
69
+ let [mapKey, mapVal] = readMapEntry(field, reader, options);
70
+ // safe to assume presence of map object, oneof cannot contain repeated values
71
+ target[localName][mapKey] = mapVal;
72
+ break;
73
+ }
74
+ }
75
+ exports.readField = readField;
76
+ // Read a map field, expecting key field = 1, value field = 2
77
+ function readMapEntry(field, reader, options) {
78
+ const length = reader.uint32(), end = reader.pos + length;
79
+ let key, val;
80
+ while (reader.pos < end) {
81
+ const [fieldNo] = reader.tag();
82
+ switch (fieldNo) {
83
+ case 1:
84
+ key = readScalar(reader, field.K);
85
+ break;
86
+ case 2:
87
+ switch (field.V.kind) {
88
+ case "scalar":
89
+ val = readScalar(reader, field.V.T);
90
+ break;
91
+ case "enum":
92
+ val = reader.int32();
93
+ break;
94
+ case "message":
95
+ val = readMessageField(reader, field.V.T.create(), field.V.T.fields, options, undefined);
96
+ break;
97
+ }
98
+ break;
99
+ }
100
+ }
101
+ if (key === undefined) {
102
+ key = (0, scalar_js_1.scalarZeroValue)(field.K, protobuf_1.LongType.BIGINT);
103
+ }
104
+ if (typeof key != "string" && typeof key != "number") {
105
+ key = key.toString();
106
+ }
107
+ if (val === undefined) {
108
+ switch (field.V.kind) {
109
+ case "scalar":
110
+ val = (0, scalar_js_1.scalarZeroValue)(field.V.T, protobuf_1.LongType.BIGINT);
111
+ break;
112
+ case "enum":
113
+ val = field.V.T.values[0].no;
114
+ break;
115
+ case "message":
116
+ val = field.V.T.create();
117
+ break;
118
+ }
119
+ }
120
+ return [key, val];
121
+ }
122
+ exports.readMapEntry = readMapEntry;
123
+ function readScalar(reader, type) {
124
+ switch (type) {
125
+ case protobuf_1.ScalarType.STRING:
126
+ return reader.string();
127
+ case protobuf_1.ScalarType.BOOL:
128
+ return reader.bool();
129
+ case protobuf_1.ScalarType.DOUBLE:
130
+ return reader.double();
131
+ case protobuf_1.ScalarType.FLOAT:
132
+ return reader.float();
133
+ case protobuf_1.ScalarType.INT32:
134
+ return reader.int32();
135
+ case protobuf_1.ScalarType.INT64:
136
+ return reader.int64();
137
+ case protobuf_1.ScalarType.UINT64:
138
+ return reader.uint64();
139
+ case protobuf_1.ScalarType.FIXED64:
140
+ return reader.fixed64();
141
+ case protobuf_1.ScalarType.BYTES:
142
+ return reader.bytes();
143
+ case protobuf_1.ScalarType.FIXED32:
144
+ return reader.fixed32();
145
+ case protobuf_1.ScalarType.SFIXED32:
146
+ return reader.sfixed32();
147
+ case protobuf_1.ScalarType.SFIXED64:
148
+ return reader.sfixed64();
149
+ case protobuf_1.ScalarType.SINT64:
150
+ return reader.sint64();
151
+ case protobuf_1.ScalarType.UINT32:
152
+ return reader.uint32();
153
+ case protobuf_1.ScalarType.SINT32:
154
+ return reader.sint32();
155
+ }
156
+ }
157
+ exports.readScalar = readScalar;
158
+ // Read a scalar value, but return 64 bit integral types (int64, uint64,
159
+ // sint64, fixed64, sfixed64) as string instead of bigint.
160
+ function readScalarLTString(reader, type) {
161
+ const v = readScalar(reader, type);
162
+ return typeof v == "bigint" ? v.toString() : v;
163
+ }
164
+ exports.readScalarLTString = readScalarLTString;
165
+ // Read a message, avoiding MessageType.fromBinary() to re-use the
166
+ // BinaryReadOptions and the IBinaryReader.
167
+ function readMessageField(reader, message, fields, options, field) {
168
+ const delimited = field?.delimited;
169
+ readMessage(message, fields, reader, delimited ? field.no : reader.uint32(), // eslint-disable-line @typescript-eslint/strict-boolean-expressions
170
+ options, delimited);
171
+ return message;
172
+ }
173
+ // Default options for parsing binary data.
174
+ const readDefaults = {
175
+ readUnknownFields: true,
176
+ readerFactory: (bytes) => new protobuf_1.BinaryReader(bytes),
177
+ };
178
+ // Default options for serializing binary data.
179
+ const writeDefaults = {
180
+ writeUnknownFields: true,
181
+ writerFactory: () => new protobuf_1.BinaryWriter(),
182
+ };
183
+ function makeReadOptions(options) {
184
+ return options ? { ...readDefaults, ...options } : readDefaults;
185
+ }
186
+ exports.makeReadOptions = makeReadOptions;
187
+ function makeWriteOptions(options) {
188
+ return options ? { ...writeDefaults, ...options } : writeDefaults;
189
+ }
190
+ exports.makeWriteOptions = makeWriteOptions;
191
+ function readMessage(message, fields, reader, lengthOrEndTagFieldNo, options, delimitedMessageEncoding) {
192
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
193
+ const end = delimitedMessageEncoding
194
+ ? reader.len
195
+ : reader.pos + lengthOrEndTagFieldNo;
196
+ let fieldNo, wireType;
197
+ while (reader.pos < end) {
198
+ [fieldNo, wireType] = reader.tag();
199
+ if (wireType == protobuf_1.WireType.EndGroup) {
200
+ break;
201
+ }
202
+ const field = fields.find(fieldNo);
203
+ if (!field) {
204
+ const data = reader.skip(wireType);
205
+ if (options.readUnknownFields) {
206
+ (0, unknown_js_1.handleUnknownField)(message, fieldNo, wireType, data);
207
+ }
208
+ continue;
209
+ }
210
+ readField(message, reader, field, wireType, options);
211
+ }
212
+ if (delimitedMessageEncoding && // eslint-disable-line @typescript-eslint/strict-boolean-expressions
213
+ (wireType != protobuf_1.WireType.EndGroup || fieldNo !== lengthOrEndTagFieldNo)) {
214
+ throw new Error(`invalid end group tag`);
215
+ }
216
+ }
217
+ exports.readMessage = readMessage;
218
+ /**
219
+ * Serialize a message to binary data.
220
+ */
221
+ function writeMessage(message, fields, writer, options) {
222
+ for (const field of fields.byNumber()) {
223
+ if (!(0, field_js_1.isFieldSet)(field, message)) {
224
+ if (field.req) {
225
+ throw new Error(`cannot encode field ${field.name} to binary: required field not set`);
226
+ }
227
+ continue;
228
+ }
229
+ const value = field.oneof
230
+ ? message[field.oneof.localName].value
231
+ : message[field.localName];
232
+ writeField(field, value, writer, options);
233
+ }
234
+ if (options.writeUnknownFields) {
235
+ writeUnknownFields(message, writer);
236
+ }
237
+ }
238
+ exports.writeMessage = writeMessage;
239
+ function writeField(field, value, writer, options) {
240
+ (0, assert_js_1.assert)(value !== undefined);
241
+ const repeated = field.repeated;
242
+ switch (field.kind) {
243
+ case "scalar":
244
+ case "enum":
245
+ let scalarType = field.kind == "enum" ? protobuf_1.ScalarType.INT32 : field.T;
246
+ if (repeated) {
247
+ (0, assert_js_1.assert)(Array.isArray(value));
248
+ if (field.packed) {
249
+ writePacked(writer, scalarType, field.no, value);
250
+ }
251
+ else {
252
+ for (const item of value) {
253
+ writeScalar(writer, scalarType, field.no, item);
254
+ }
255
+ }
256
+ }
257
+ else {
258
+ writeScalar(writer, scalarType, field.no, value);
259
+ }
260
+ break;
261
+ case "message":
262
+ if (repeated) {
263
+ (0, assert_js_1.assert)(Array.isArray(value));
264
+ for (const item of value) {
265
+ writeMessageField(writer, options, field, item);
266
+ }
267
+ }
268
+ else {
269
+ writeMessageField(writer, options, field, value);
270
+ }
271
+ break;
272
+ case "map":
273
+ (0, assert_js_1.assert)(typeof value == "object" && value != null);
274
+ for (const [key, val] of Object.entries(value)) {
275
+ writeMapEntry(writer, options, field, key, val);
276
+ }
277
+ break;
278
+ }
279
+ }
280
+ function writeUnknownFields(message, writer) {
281
+ const m = message;
282
+ const c = m[unknown_js_1.unknownFieldsSymbol];
283
+ if (c) {
284
+ for (const f of c) {
285
+ writer.tag(f.no, f.wireType).raw(f.data);
286
+ }
287
+ }
288
+ }
289
+ // Value must not be undefined
290
+ function writeMessageField(writer, options, field, value) {
291
+ const message = (0, field_wrapper_js_1.wrapField)(field.T.fieldWrapper, value);
292
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
293
+ if (field.delimited)
294
+ writer
295
+ .tag(field.no, protobuf_1.WireType.StartGroup)
296
+ .raw(message.toBinary(options))
297
+ .tag(field.no, protobuf_1.WireType.EndGroup);
298
+ else
299
+ writer
300
+ .tag(field.no, protobuf_1.WireType.LengthDelimited)
301
+ .bytes(message.toBinary(options));
302
+ }
303
+ function writeScalar(writer, type, fieldNo, value) {
304
+ (0, assert_js_1.assert)(value !== undefined);
305
+ let [wireType, method] = scalarTypeInfo(type);
306
+ writer.tag(fieldNo, wireType)[method](value);
307
+ }
308
+ function writePacked(writer, type, fieldNo, value) {
309
+ if (!value.length) {
310
+ return;
311
+ }
312
+ writer.tag(fieldNo, protobuf_1.WireType.LengthDelimited).fork();
313
+ let [, method] = scalarTypeInfo(type);
314
+ for (let i = 0; i < value.length; i++) {
315
+ writer[method](value[i]);
316
+ }
317
+ writer.join();
318
+ }
319
+ /**
320
+ * Get information for writing a scalar value.
321
+ *
322
+ * Returns tuple:
323
+ * [0]: appropriate WireType
324
+ * [1]: name of the appropriate method of IBinaryWriter
325
+ * [2]: whether the given value is a default value for proto3 semantics
326
+ *
327
+ * If argument `value` is omitted, [2] is always false.
328
+ */
329
+ // TODO replace call-sites writeScalar() and writePacked(), then remove
330
+ function scalarTypeInfo(type) {
331
+ let wireType = protobuf_1.WireType.Varint;
332
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- INT32, UINT32, SINT32 are covered by the defaults
333
+ switch (type) {
334
+ case protobuf_1.ScalarType.BYTES:
335
+ case protobuf_1.ScalarType.STRING:
336
+ wireType = protobuf_1.WireType.LengthDelimited;
337
+ break;
338
+ case protobuf_1.ScalarType.DOUBLE:
339
+ case protobuf_1.ScalarType.FIXED64:
340
+ case protobuf_1.ScalarType.SFIXED64:
341
+ wireType = protobuf_1.WireType.Bit64;
342
+ break;
343
+ case protobuf_1.ScalarType.FIXED32:
344
+ case protobuf_1.ScalarType.SFIXED32:
345
+ case protobuf_1.ScalarType.FLOAT:
346
+ wireType = protobuf_1.WireType.Bit32;
347
+ break;
348
+ }
349
+ const method = protobuf_1.ScalarType[type].toLowerCase();
350
+ return [wireType, method];
351
+ }
352
+ function writeMapEntry(writer, options, field, key, value) {
353
+ writer.tag(field.no, protobuf_1.WireType.LengthDelimited);
354
+ writer.fork();
355
+ // javascript only allows number or string for object properties
356
+ // we convert from our representation to the protobuf type
357
+ let keyValue = key;
358
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we deliberately handle just the special cases for map keys
359
+ switch (field.K) {
360
+ case protobuf_1.ScalarType.INT32:
361
+ case protobuf_1.ScalarType.FIXED32:
362
+ case protobuf_1.ScalarType.UINT32:
363
+ case protobuf_1.ScalarType.SFIXED32:
364
+ case protobuf_1.ScalarType.SINT32:
365
+ keyValue = Number.parseInt(key);
366
+ break;
367
+ case protobuf_1.ScalarType.BOOL:
368
+ (0, assert_js_1.assert)(key == "true" || key == "false");
369
+ keyValue = key == "true";
370
+ break;
371
+ }
372
+ // write key, expecting key field number = 1
373
+ writeScalar(writer, field.K, 1, keyValue);
374
+ // write value, expecting value field number = 2
375
+ switch (field.V.kind) {
376
+ case "scalar":
377
+ writeScalar(writer, field.V.T, 2, value);
378
+ break;
379
+ case "enum":
380
+ writeScalar(writer, protobuf_1.ScalarType.INT32, 2, value);
381
+ break;
382
+ case "message":
383
+ (0, assert_js_1.assert)(value !== undefined);
384
+ writer.tag(2, protobuf_1.WireType.LengthDelimited).bytes(value.toBinary(options));
385
+ break;
386
+ }
387
+ writer.join();
388
+ }
389
+ exports.writeMapEntry = writeMapEntry;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.getNonEditionRuntime = void 0;
17
+ /**
18
+ * For syntax "editions", this function raises and error.
19
+ */
20
+ function getNonEditionRuntime(schema, file) {
21
+ if (file.syntax === "editions") {
22
+ throw new Error(`${file.proto.name ?? ""}: syntax "editions" is not supported`);
23
+ }
24
+ return schema.runtime[file.syntax];
25
+ }
26
+ exports.getNonEditionRuntime = getNonEditionRuntime;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.wrapField = void 0;
4
+ /**
5
+ * Wrap a primitive message field value in its corresponding wrapper
6
+ * message. This function is idempotent.
7
+ */
8
+ function wrapField(fieldWrapper, value) {
9
+ if (!fieldWrapper) {
10
+ return value;
11
+ }
12
+ return fieldWrapper.wrapField(value);
13
+ }
14
+ exports.wrapField = wrapField;