@aptre/protobuf-es-lite 0.2.9 → 0.2.11

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.
Files changed (75) hide show
  1. package/bin/protoc-gen-es-lite +2 -2
  2. package/dist/assert.js +4 -11
  3. package/dist/binary-encoding.js +30 -35
  4. package/dist/binary.js +84 -96
  5. package/dist/codegen-info.js +12 -15
  6. package/dist/create-descriptor-set.js +113 -117
  7. package/dist/descriptor-set.js +1 -2
  8. package/dist/enum.js +2 -7
  9. package/dist/feature-set.js +16 -20
  10. package/dist/field-wrapper.js +12 -17
  11. package/dist/field.js +18 -27
  12. package/dist/google/index.js +9 -41
  13. package/dist/google/protobuf/any.pb.d.ts +2 -2
  14. package/dist/google/protobuf/any.pb.js +3 -6
  15. package/dist/google/protobuf/api.pb.d.ts +2 -2
  16. package/dist/google/protobuf/api.pb.js +14 -17
  17. package/dist/google/protobuf/compiler/plugin.pb.d.ts +2 -2
  18. package/dist/google/protobuf/compiler/plugin.pb.js +15 -18
  19. package/dist/google/protobuf/descriptor.pb.d.ts +19 -19
  20. package/dist/google/protobuf/descriptor.pb.js +156 -160
  21. package/dist/google/protobuf/duration.pb.d.ts +2 -2
  22. package/dist/google/protobuf/duration.pb.js +3 -6
  23. package/dist/google/protobuf/empty.pb.d.ts +2 -2
  24. package/dist/google/protobuf/empty.pb.js +3 -6
  25. package/dist/google/protobuf/source_context.pb.d.ts +2 -2
  26. package/dist/google/protobuf/source_context.pb.js +3 -6
  27. package/dist/google/protobuf/struct.pb.d.ts +3 -3
  28. package/dist/google/protobuf/struct.pb.js +13 -16
  29. package/dist/google/protobuf/timestamp.pb.d.ts +2 -2
  30. package/dist/google/protobuf/timestamp.pb.js +3 -6
  31. package/dist/google/protobuf/type.pb.d.ts +5 -5
  32. package/dist/google/protobuf/type.pb.js +31 -34
  33. package/dist/google/protobuf/wrappers.pb.d.ts +2 -2
  34. package/dist/google/protobuf/wrappers.pb.js +11 -14
  35. package/dist/index.d.ts +1 -0
  36. package/dist/index.js +12 -60
  37. package/dist/is-message.js +9 -16
  38. package/dist/json.js +72 -80
  39. package/dist/message.js +48 -55
  40. package/dist/names.js +12 -23
  41. package/dist/partial.js +13 -17
  42. package/dist/proto-base64.js +1 -4
  43. package/dist/proto-double.js +1 -4
  44. package/dist/proto-int64.js +9 -12
  45. package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.js +4 -7
  46. package/dist/protoc-gen-es-lite/typescript.js +27 -36
  47. package/dist/protoplugin/create-es-plugin.js +13 -17
  48. package/dist/protoplugin/ecmascript/file-preamble.js +5 -9
  49. package/dist/protoplugin/ecmascript/generated-file.js +16 -20
  50. package/dist/protoplugin/ecmascript/import-path.js +13 -20
  51. package/dist/protoplugin/ecmascript/import-symbol.js +1 -5
  52. package/dist/protoplugin/ecmascript/index.js +2 -7
  53. package/dist/protoplugin/ecmascript/jsdoc.js +1 -5
  54. package/dist/protoplugin/ecmascript/opaque-printables.js +1 -2
  55. package/dist/protoplugin/ecmascript/parameter.js +10 -14
  56. package/dist/protoplugin/ecmascript/reify-wkt.js +12 -16
  57. package/dist/protoplugin/ecmascript/runtime-imports.js +5 -9
  58. package/dist/protoplugin/ecmascript/schema.js +16 -20
  59. package/dist/protoplugin/ecmascript/target.js +1 -2
  60. package/dist/protoplugin/ecmascript/transpile.js +15 -42
  61. package/dist/protoplugin/error.js +3 -9
  62. package/dist/protoplugin/index.js +2 -7
  63. package/dist/protoplugin/plugin.js +1 -2
  64. package/dist/protoplugin/run-node.js +6 -10
  65. package/dist/scalar.js +9 -15
  66. package/dist/service-type.js +4 -7
  67. package/dist/text-format.js +25 -30
  68. package/dist/type-registry.js +1 -2
  69. package/dist/unknown.js +7 -12
  70. package/dist/util.d.ts +1 -1
  71. package/dist/util.js +49 -55
  72. package/dist/varint.js +7 -17
  73. package/example/example.pb.ts +1 -1
  74. package/package.json +10 -7
  75. package/tsconfig.base.json +2 -2
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const {runNodeJs} = require("../dist/protoplugin");
4
- const {protocGenEsLite} = require("../dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.js");
3
+ import {runNodeJs} from "../dist/protoplugin/index.js";
4
+ import {protocGenEsLite} from "../dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.js";
5
5
 
6
6
  runNodeJs(protocGenEsLite);
package/dist/assert.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  // Copyright 2021-2024 Buf Technologies, Inc.
3
2
  //
4
3
  // Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,43 +11,38 @@
12
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
12
  // See the License for the specific language governing permissions and
14
13
  // limitations under the License.
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.assertFloat32 = exports.assertUInt32 = exports.assertInt32 = exports.assert = void 0;
17
14
  /**
18
15
  * Assert that condition is truthy or throw error (with message)
19
16
  */
20
- function assert(condition, msg) {
17
+ export function assert(condition, msg) {
21
18
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
22
19
  if (!condition) {
23
20
  throw new Error(msg);
24
21
  }
25
22
  }
26
- exports.assert = assert;
27
23
  const FLOAT32_MAX = 3.4028234663852886e38, FLOAT32_MIN = -3.4028234663852886e38, UINT32_MAX = 0xffffffff, INT32_MAX = 0x7fffffff, INT32_MIN = -0x80000000;
28
24
  /**
29
25
  * Assert a valid signed protobuf 32-bit integer.
30
26
  */
31
- function assertInt32(arg) {
27
+ export function assertInt32(arg) {
32
28
  if (typeof arg !== "number")
33
29
  throw new Error("invalid int 32: " + typeof arg);
34
30
  if (!Number.isInteger(arg) || arg > INT32_MAX || arg < INT32_MIN)
35
31
  throw new Error("invalid int 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
36
32
  }
37
- exports.assertInt32 = assertInt32;
38
33
  /**
39
34
  * Assert a valid unsigned protobuf 32-bit integer.
40
35
  */
41
- function assertUInt32(arg) {
36
+ export function assertUInt32(arg) {
42
37
  if (typeof arg !== "number")
43
38
  throw new Error("invalid uint 32: " + typeof arg);
44
39
  if (!Number.isInteger(arg) || arg > UINT32_MAX || arg < 0)
45
40
  throw new Error("invalid uint 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
46
41
  }
47
- exports.assertUInt32 = assertUInt32;
48
42
  /**
49
43
  * Assert a valid protobuf float value.
50
44
  */
51
- function assertFloat32(arg) {
45
+ export function assertFloat32(arg) {
52
46
  if (typeof arg !== "number")
53
47
  throw new Error("invalid float 32: " + typeof arg);
54
48
  if (!Number.isFinite(arg))
@@ -56,4 +50,3 @@ function assertFloat32(arg) {
56
50
  if (arg > FLOAT32_MAX || arg < FLOAT32_MIN)
57
51
  throw new Error("invalid float 32: " + arg); // eslint-disable-line @typescript-eslint/restrict-plus-operands -- we want the implicit conversion to string
58
52
  }
59
- exports.assertFloat32 = assertFloat32;
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  // Copyright 2021-2024 Buf Technologies, Inc.
3
2
  //
4
3
  // Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,11 +11,9 @@
12
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
12
  // See the License for the specific language governing permissions and
14
13
  // limitations under the License.
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.BinaryReader = exports.BinaryWriter = exports.WireType = void 0;
17
- const varint_js_1 = require("./varint.js");
18
- const assert_js_1 = require("./assert.js");
19
- const proto_int64_js_1 = require("./proto-int64.js");
14
+ import { varint32read, varint32write, varint64read, varint64write, } from "./varint.js";
15
+ import { assertFloat32, assertInt32, assertUInt32 } from "./assert.js";
16
+ import { protoInt64 } from "./proto-int64.js";
20
17
  /* eslint-disable prefer-const,no-case-declarations,@typescript-eslint/restrict-plus-operands */
21
18
  /**
22
19
  * Protobuf binary format wire types.
@@ -26,7 +23,7 @@ const proto_int64_js_1 = require("./proto-int64.js");
26
23
  *
27
24
  * See https://developers.google.com/protocol-buffers/docs/encoding#structure
28
25
  */
29
- var WireType;
26
+ export var WireType;
30
27
  (function (WireType) {
31
28
  /**
32
29
  * Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
@@ -59,8 +56,8 @@ var WireType;
59
56
  * Always 4 bytes with little-endian byte order.
60
57
  */
61
58
  WireType[WireType["Bit32"] = 5] = "Bit32";
62
- })(WireType || (exports.WireType = WireType = {}));
63
- class BinaryWriter {
59
+ })(WireType || (WireType = {}));
60
+ export class BinaryWriter {
64
61
  constructor(textEncoder) {
65
62
  /**
66
63
  * Previous fork states.
@@ -141,7 +138,7 @@ class BinaryWriter {
141
138
  * Write a `uint32` value, an unsigned 32 bit varint.
142
139
  */
143
140
  uint32(value) {
144
- (0, assert_js_1.assertUInt32)(value);
141
+ assertUInt32(value);
145
142
  // write value as varint 32, inlined for speed
146
143
  while (value > 0x7f) {
147
144
  this.buf.push((value & 0x7f) | 0x80);
@@ -154,8 +151,8 @@ class BinaryWriter {
154
151
  * Write a `int32` value, a signed 32 bit varint.
155
152
  */
156
153
  int32(value) {
157
- (0, assert_js_1.assertInt32)(value);
158
- (0, varint_js_1.varint32write)(value, this.buf);
154
+ assertInt32(value);
155
+ varint32write(value, this.buf);
159
156
  return this;
160
157
  }
161
158
  /**
@@ -184,7 +181,7 @@ class BinaryWriter {
184
181
  * Write a `float` value, 32-bit floating point number.
185
182
  */
186
183
  float(value) {
187
- (0, assert_js_1.assertFloat32)(value);
184
+ assertFloat32(value);
188
185
  let chunk = new Uint8Array(4);
189
186
  new DataView(chunk.buffer).setFloat32(0, value, true);
190
187
  return this.raw(chunk);
@@ -201,7 +198,7 @@ class BinaryWriter {
201
198
  * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
202
199
  */
203
200
  fixed32(value) {
204
- (0, assert_js_1.assertUInt32)(value);
201
+ assertUInt32(value);
205
202
  let chunk = new Uint8Array(4);
206
203
  new DataView(chunk.buffer).setUint32(0, value, true);
207
204
  return this.raw(chunk);
@@ -210,7 +207,7 @@ class BinaryWriter {
210
207
  * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
211
208
  */
212
209
  sfixed32(value) {
213
- (0, assert_js_1.assertInt32)(value);
210
+ assertInt32(value);
214
211
  let chunk = new Uint8Array(4);
215
212
  new DataView(chunk.buffer).setInt32(0, value, true);
216
213
  return this.raw(chunk);
@@ -219,17 +216,17 @@ class BinaryWriter {
219
216
  * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
220
217
  */
221
218
  sint32(value) {
222
- (0, assert_js_1.assertInt32)(value);
219
+ assertInt32(value);
223
220
  // zigzag encode
224
221
  value = ((value << 1) ^ (value >> 31)) >>> 0;
225
- (0, varint_js_1.varint32write)(value, this.buf);
222
+ varint32write(value, this.buf);
226
223
  return this;
227
224
  }
228
225
  /**
229
226
  * Write a `fixed64` value, a signed, fixed-length 64-bit integer.
230
227
  */
231
228
  sfixed64(value) {
232
- let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = proto_int64_js_1.protoInt64.enc(value);
229
+ let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = protoInt64.enc(value);
233
230
  view.setInt32(0, tc.lo, true);
234
231
  view.setInt32(4, tc.hi, true);
235
232
  return this.raw(chunk);
@@ -238,7 +235,7 @@ class BinaryWriter {
238
235
  * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
239
236
  */
240
237
  fixed64(value) {
241
- let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = proto_int64_js_1.protoInt64.uEnc(value);
238
+ let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = protoInt64.uEnc(value);
242
239
  view.setInt32(0, tc.lo, true);
243
240
  view.setInt32(4, tc.hi, true);
244
241
  return this.raw(chunk);
@@ -247,37 +244,36 @@ class BinaryWriter {
247
244
  * Write a `int64` value, a signed 64-bit varint.
248
245
  */
249
246
  int64(value) {
250
- let tc = proto_int64_js_1.protoInt64.enc(value);
251
- (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
247
+ let tc = protoInt64.enc(value);
248
+ varint64write(tc.lo, tc.hi, this.buf);
252
249
  return this;
253
250
  }
254
251
  /**
255
252
  * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
256
253
  */
257
254
  sint64(value) {
258
- let tc = proto_int64_js_1.protoInt64.enc(value),
255
+ let tc = protoInt64.enc(value),
259
256
  // zigzag encode
260
257
  sign = tc.hi >> 31, lo = (tc.lo << 1) ^ sign, hi = ((tc.hi << 1) | (tc.lo >>> 31)) ^ sign;
261
- (0, varint_js_1.varint64write)(lo, hi, this.buf);
258
+ varint64write(lo, hi, this.buf);
262
259
  return this;
263
260
  }
264
261
  /**
265
262
  * Write a `uint64` value, an unsigned 64-bit varint.
266
263
  */
267
264
  uint64(value) {
268
- let tc = proto_int64_js_1.protoInt64.uEnc(value);
269
- (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
265
+ let tc = protoInt64.uEnc(value);
266
+ varint64write(tc.lo, tc.hi, this.buf);
270
267
  return this;
271
268
  }
272
269
  }
273
- exports.BinaryWriter = BinaryWriter;
274
- class BinaryReader {
270
+ export class BinaryReader {
275
271
  constructor(buf, textDecoder) {
276
- this.varint64 = varint_js_1.varint64read; // dirty cast for `this`
272
+ this.varint64 = varint64read; // dirty cast for `this`
277
273
  /**
278
274
  * Read a `uint32` field, an unsigned 32 bit varint.
279
275
  */
280
- this.uint32 = varint_js_1.varint32read; // dirty cast for `this` and access to protected `buf`
276
+ this.uint32 = varint32read; // dirty cast for `this` and access to protected `buf`
281
277
  this.buf = buf;
282
278
  this.len = buf.length;
283
279
  this.pos = 0;
@@ -356,13 +352,13 @@ class BinaryReader {
356
352
  * Read a `int64` field, a signed 64-bit varint.
357
353
  */
358
354
  int64() {
359
- return proto_int64_js_1.protoInt64.dec(...this.varint64());
355
+ return protoInt64.dec(...this.varint64());
360
356
  }
361
357
  /**
362
358
  * Read a `uint64` field, an unsigned 64-bit varint.
363
359
  */
364
360
  uint64() {
365
- return proto_int64_js_1.protoInt64.uDec(...this.varint64());
361
+ return protoInt64.uDec(...this.varint64());
366
362
  }
367
363
  /**
368
364
  * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
@@ -373,7 +369,7 @@ class BinaryReader {
373
369
  let s = -(lo & 1);
374
370
  lo = ((lo >>> 1) | ((hi & 1) << 31)) ^ s;
375
371
  hi = (hi >>> 1) ^ s;
376
- return proto_int64_js_1.protoInt64.dec(lo, hi);
372
+ return protoInt64.dec(lo, hi);
377
373
  }
378
374
  /**
379
375
  * Read a `bool` field, a variant.
@@ -398,13 +394,13 @@ class BinaryReader {
398
394
  * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
399
395
  */
400
396
  fixed64() {
401
- return proto_int64_js_1.protoInt64.uDec(this.sfixed32(), this.sfixed32());
397
+ return protoInt64.uDec(this.sfixed32(), this.sfixed32());
402
398
  }
403
399
  /**
404
400
  * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
405
401
  */
406
402
  sfixed64() {
407
- return proto_int64_js_1.protoInt64.dec(this.sfixed32(), this.sfixed32());
403
+ return protoInt64.dec(this.sfixed32(), this.sfixed32());
408
404
  }
409
405
  /**
410
406
  * Read a `float` field, 32-bit floating point number.
@@ -434,4 +430,3 @@ class BinaryReader {
434
430
  return this.textDecoder.decode(this.bytes());
435
431
  }
436
432
  }
437
- exports.BinaryReader = BinaryReader;
package/dist/binary.js CHANGED
@@ -1,14 +1,11 @@
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 field_js_1 = require("./field.js");
5
- const is_message_js_1 = require("./is-message.js");
6
- const unknown_js_1 = require("./unknown.js");
7
- const field_wrapper_js_1 = require("./field-wrapper.js");
8
- const scalar_js_1 = require("./scalar.js");
9
- const assert_js_1 = require("./assert.js");
10
- const binary_encoding_js_1 = require("./binary-encoding.js");
11
- function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
1
+ import { isFieldSet, resolveMessageType, } from "./field.js";
2
+ import { isCompleteMessage } from "./is-message.js";
3
+ import { handleUnknownField, unknownFieldsSymbol } from "./unknown.js";
4
+ import { wrapField } from "./field-wrapper.js";
5
+ import { LongType, ScalarType, scalarZeroValue, } from "./scalar.js";
6
+ import { assert } from "./assert.js";
7
+ import { BinaryReader, BinaryWriter, WireType, } from "./binary-encoding.js";
8
+ export function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
12
9
  reader, field, wireType, options) {
13
10
  let { repeated, localName } = field;
14
11
  if (field.oneof) {
@@ -26,7 +23,7 @@ reader, field, wireType, options) {
26
23
  switch (field.kind) {
27
24
  case "scalar":
28
25
  case "enum":
29
- const scalarType = field.kind == "enum" ? scalar_js_1.ScalarType.INT32 : field.T;
26
+ const scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
30
27
  let read = readScalar;
31
28
  // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
32
29
  if (field.kind == "scalar" && field.L > 0) {
@@ -37,9 +34,9 @@ reader, field, wireType, options) {
37
34
  if (!Array.isArray(tgtArr)) {
38
35
  tgtArr = target[localName] = [];
39
36
  }
40
- const isPacked = wireType == binary_encoding_js_1.WireType.LengthDelimited &&
41
- scalarType != scalar_js_1.ScalarType.STRING &&
42
- scalarType != scalar_js_1.ScalarType.BYTES;
37
+ const isPacked = wireType == WireType.LengthDelimited &&
38
+ scalarType != ScalarType.STRING &&
39
+ scalarType != ScalarType.BYTES;
43
40
  if (isPacked) {
44
41
  let e = reader.uint32() + reader.pos;
45
42
  while (reader.pos < e) {
@@ -65,7 +62,7 @@ reader, field, wireType, options) {
65
62
  tgtArr.push(readMessageField(reader, messageType.create(), messageType.fields, options, field));
66
63
  }
67
64
  else {
68
- if ((0, is_message_js_1.isCompleteMessage)(target[localName], messageType.fields.list())) {
65
+ if (isCompleteMessage(target[localName], messageType.fields.list())) {
69
66
  readMessageField(reader, target[localName], messageType.fields, options, field);
70
67
  }
71
68
  else {
@@ -86,9 +83,8 @@ reader, field, wireType, options) {
86
83
  break;
87
84
  }
88
85
  }
89
- exports.readField = readField;
90
86
  // Read a map field, expecting key field = 1, value field = 2
91
- function readMapEntry(field, reader, options) {
87
+ export function readMapEntry(field, reader, options) {
92
88
  const length = reader.uint32(), end = reader.pos + length;
93
89
  let key, val;
94
90
  while (reader.pos < end) {
@@ -106,7 +102,7 @@ function readMapEntry(field, reader, options) {
106
102
  val = reader.int32();
107
103
  break;
108
104
  case "message":
109
- const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
105
+ const messageType = resolveMessageType(field.V.T);
110
106
  val = readMessageField(reader, messageType.create(), messageType.fields, options, undefined);
111
107
  break;
112
108
  }
@@ -114,7 +110,7 @@ function readMapEntry(field, reader, options) {
114
110
  }
115
111
  }
116
112
  if (key === undefined) {
117
- key = (0, scalar_js_1.scalarZeroValue)(field.K, scalar_js_1.LongType.BIGINT);
113
+ key = scalarZeroValue(field.K, LongType.BIGINT);
118
114
  }
119
115
  if (typeof key != "string" && typeof key != "number") {
120
116
  key = key.toString();
@@ -123,61 +119,58 @@ function readMapEntry(field, reader, options) {
123
119
  const fieldKind = field.V.kind;
124
120
  switch (fieldKind) {
125
121
  case "scalar":
126
- val = (0, scalar_js_1.scalarZeroValue)(field.V.T, scalar_js_1.LongType.BIGINT);
122
+ val = scalarZeroValue(field.V.T, LongType.BIGINT);
127
123
  break;
128
124
  case "enum":
129
125
  val = field.V.T.values[0].no;
130
126
  break;
131
127
  case "message":
132
- val = (0, field_js_1.resolveMessageType)(field.V.T).create();
128
+ val = resolveMessageType(field.V.T).create();
133
129
  break;
134
130
  }
135
131
  }
136
132
  return [key, val];
137
133
  }
138
- exports.readMapEntry = readMapEntry;
139
- function readScalar(reader, type) {
134
+ export function readScalar(reader, type) {
140
135
  switch (type) {
141
- case scalar_js_1.ScalarType.STRING:
136
+ case ScalarType.STRING:
142
137
  return reader.string();
143
- case scalar_js_1.ScalarType.BOOL:
138
+ case ScalarType.BOOL:
144
139
  return reader.bool();
145
- case scalar_js_1.ScalarType.DOUBLE:
140
+ case ScalarType.DOUBLE:
146
141
  return reader.double();
147
- case scalar_js_1.ScalarType.FLOAT:
142
+ case ScalarType.FLOAT:
148
143
  return reader.float();
149
- case scalar_js_1.ScalarType.INT32:
144
+ case ScalarType.INT32:
150
145
  return reader.int32();
151
- case scalar_js_1.ScalarType.INT64:
146
+ case ScalarType.INT64:
152
147
  return reader.int64();
153
- case scalar_js_1.ScalarType.UINT64:
148
+ case ScalarType.UINT64:
154
149
  return reader.uint64();
155
- case scalar_js_1.ScalarType.FIXED64:
150
+ case ScalarType.FIXED64:
156
151
  return reader.fixed64();
157
- case scalar_js_1.ScalarType.BYTES:
152
+ case ScalarType.BYTES:
158
153
  return reader.bytes();
159
- case scalar_js_1.ScalarType.FIXED32:
154
+ case ScalarType.FIXED32:
160
155
  return reader.fixed32();
161
- case scalar_js_1.ScalarType.SFIXED32:
156
+ case ScalarType.SFIXED32:
162
157
  return reader.sfixed32();
163
- case scalar_js_1.ScalarType.SFIXED64:
158
+ case ScalarType.SFIXED64:
164
159
  return reader.sfixed64();
165
- case scalar_js_1.ScalarType.SINT64:
160
+ case ScalarType.SINT64:
166
161
  return reader.sint64();
167
- case scalar_js_1.ScalarType.UINT32:
162
+ case ScalarType.UINT32:
168
163
  return reader.uint32();
169
- case scalar_js_1.ScalarType.SINT32:
164
+ case ScalarType.SINT32:
170
165
  return reader.sint32();
171
166
  }
172
167
  }
173
- exports.readScalar = readScalar;
174
168
  // Read a scalar value, but return 64 bit integral types (int64, uint64,
175
169
  // sint64, fixed64, sfixed64) as string instead of bigint.
176
- function readScalarLTString(reader, type) {
170
+ export function readScalarLTString(reader, type) {
177
171
  const v = readScalar(reader, type);
178
172
  return typeof v == "bigint" ? v.toString() : v;
179
173
  }
180
- exports.readScalarLTString = readScalarLTString;
181
174
  // Read a message, avoiding MessageType.fromBinary() to re-use the
182
175
  // BinaryReadOptions and the IBinaryReader.
183
176
  function readMessageField(reader, message, fields, options, field) {
@@ -189,52 +182,49 @@ function readMessageField(reader, message, fields, options, field) {
189
182
  // Default options for parsing binary data.
190
183
  const readDefaults = {
191
184
  readUnknownFields: true,
192
- readerFactory: (bytes) => new binary_encoding_js_1.BinaryReader(bytes),
185
+ readerFactory: (bytes) => new BinaryReader(bytes),
193
186
  };
194
187
  // Default options for serializing binary data.
195
188
  const writeDefaults = {
196
189
  writeUnknownFields: true,
197
- writerFactory: () => new binary_encoding_js_1.BinaryWriter(),
190
+ writerFactory: () => new BinaryWriter(),
198
191
  };
199
- function makeReadOptions(options) {
192
+ export function makeReadOptions(options) {
200
193
  return options ? { ...readDefaults, ...options } : readDefaults;
201
194
  }
202
- exports.makeReadOptions = makeReadOptions;
203
- function makeWriteOptions(options) {
195
+ export function makeWriteOptions(options) {
204
196
  return options ? { ...writeDefaults, ...options } : writeDefaults;
205
197
  }
206
- exports.makeWriteOptions = makeWriteOptions;
207
- function readMessage(message, fields, reader, lengthOrEndTagFieldNo, options, delimitedMessageEncoding) {
198
+ export function readMessage(message, fields, reader, lengthOrEndTagFieldNo, options, delimitedMessageEncoding) {
208
199
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
209
200
  const end = delimitedMessageEncoding ? reader.len : reader.pos + lengthOrEndTagFieldNo;
210
201
  let fieldNo, wireType;
211
202
  while (reader.pos < end) {
212
203
  [fieldNo, wireType] = reader.tag();
213
- if (wireType == binary_encoding_js_1.WireType.EndGroup) {
204
+ if (wireType == WireType.EndGroup) {
214
205
  break;
215
206
  }
216
207
  const field = fields.find(fieldNo);
217
208
  if (!field) {
218
209
  const data = reader.skip(wireType);
219
210
  if (options.readUnknownFields) {
220
- (0, unknown_js_1.handleUnknownField)(message, fieldNo, wireType, data);
211
+ handleUnknownField(message, fieldNo, wireType, data);
221
212
  }
222
213
  continue;
223
214
  }
224
215
  readField(message, reader, field, wireType, options);
225
216
  }
226
217
  if (delimitedMessageEncoding && // eslint-disable-line @typescript-eslint/strict-boolean-expressions
227
- (wireType != binary_encoding_js_1.WireType.EndGroup || fieldNo !== lengthOrEndTagFieldNo)) {
218
+ (wireType != WireType.EndGroup || fieldNo !== lengthOrEndTagFieldNo)) {
228
219
  throw new Error(`invalid end group tag`);
229
220
  }
230
221
  }
231
- exports.readMessage = readMessage;
232
222
  /**
233
223
  * Serialize a message to binary data.
234
224
  */
235
- function writeMessage(message, fields, writer, options) {
225
+ export function writeMessage(message, fields, writer, options) {
236
226
  for (const field of fields.byNumber()) {
237
- if (!(0, field_js_1.isFieldSet)(field, message)) {
227
+ if (!isFieldSet(field, message)) {
238
228
  if (field.req) {
239
229
  throw new Error(`cannot encode field ${field.name} to binary: required field not set`);
240
230
  }
@@ -249,16 +239,15 @@ function writeMessage(message, fields, writer, options) {
249
239
  writeUnknownFields(message, writer);
250
240
  }
251
241
  }
252
- exports.writeMessage = writeMessage;
253
242
  function writeField(field, value, writer, options) {
254
- (0, assert_js_1.assert)(value !== undefined);
243
+ assert(value !== undefined);
255
244
  const repeated = field.repeated;
256
245
  switch (field.kind) {
257
246
  case "scalar":
258
247
  case "enum":
259
- let scalarType = field.kind == "enum" ? scalar_js_1.ScalarType.INT32 : field.T;
248
+ let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
260
249
  if (repeated) {
261
- (0, assert_js_1.assert)(Array.isArray(value));
250
+ assert(Array.isArray(value));
262
251
  if (field.packed) {
263
252
  writePacked(writer, scalarType, field.no, value);
264
253
  }
@@ -274,7 +263,7 @@ function writeField(field, value, writer, options) {
274
263
  break;
275
264
  case "message":
276
265
  if (repeated) {
277
- (0, assert_js_1.assert)(Array.isArray(value));
266
+ assert(Array.isArray(value));
278
267
  for (const item of value) {
279
268
  writeMessageField(writer, options, field, item);
280
269
  }
@@ -284,7 +273,7 @@ function writeField(field, value, writer, options) {
284
273
  }
285
274
  break;
286
275
  case "map":
287
- (0, assert_js_1.assert)(typeof value == "object" && value != null);
276
+ assert(typeof value == "object" && value != null);
288
277
  for (const [key, val] of Object.entries(value)) {
289
278
  writeMapEntry(writer, options, field, key, val);
290
279
  }
@@ -293,7 +282,7 @@ function writeField(field, value, writer, options) {
293
282
  }
294
283
  function writeUnknownFields(message, writer) {
295
284
  const m = message;
296
- const c = m[unknown_js_1.unknownFieldsSymbol];
285
+ const c = m[unknownFieldsSymbol];
297
286
  if (c) {
298
287
  for (const f of c) {
299
288
  writer.tag(f.no, f.wireType).raw(f.data);
@@ -302,21 +291,21 @@ function writeUnknownFields(message, writer) {
302
291
  }
303
292
  // Value must not be undefined
304
293
  function writeMessageField(writer, options, field, value) {
305
- const messageType = (0, field_js_1.resolveMessageType)(field.T);
306
- const message = (0, field_wrapper_js_1.wrapField)(messageType.fieldWrapper, value);
294
+ const messageType = resolveMessageType(field.T);
295
+ const message = wrapField(messageType.fieldWrapper, value);
307
296
  // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
308
297
  if (field.delimited)
309
298
  writer
310
- .tag(field.no, binary_encoding_js_1.WireType.StartGroup)
299
+ .tag(field.no, WireType.StartGroup)
311
300
  .raw(messageType.toBinary(message, options))
312
- .tag(field.no, binary_encoding_js_1.WireType.EndGroup);
301
+ .tag(field.no, WireType.EndGroup);
313
302
  else
314
303
  writer
315
- .tag(field.no, binary_encoding_js_1.WireType.LengthDelimited)
304
+ .tag(field.no, WireType.LengthDelimited)
316
305
  .bytes(messageType.toBinary(message, options));
317
306
  }
318
307
  function writeScalar(writer, type, fieldNo, value) {
319
- (0, assert_js_1.assert)(value !== undefined);
308
+ assert(value !== undefined);
320
309
  let [wireType, method] = scalarTypeInfo(type);
321
310
  writer.tag(fieldNo, wireType)[method](value);
322
311
  }
@@ -324,7 +313,7 @@ function writePacked(writer, type, fieldNo, value) {
324
313
  if (!value.length) {
325
314
  return;
326
315
  }
327
- writer.tag(fieldNo, binary_encoding_js_1.WireType.LengthDelimited).fork();
316
+ writer.tag(fieldNo, WireType.LengthDelimited).fork();
328
317
  let [, method] = scalarTypeInfo(type);
329
318
  for (let i = 0; i < value.length; i++) {
330
319
  writer[method](value[i]);
@@ -343,44 +332,44 @@ function writePacked(writer, type, fieldNo, value) {
343
332
  */
344
333
  // TODO replace call-sites writeScalar() and writePacked(), then remove
345
334
  function scalarTypeInfo(type) {
346
- let wireType = binary_encoding_js_1.WireType.Varint;
335
+ let wireType = WireType.Varint;
347
336
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- INT32, UINT32, SINT32 are covered by the defaults
348
337
  switch (type) {
349
- case scalar_js_1.ScalarType.BYTES:
350
- case scalar_js_1.ScalarType.STRING:
351
- wireType = binary_encoding_js_1.WireType.LengthDelimited;
338
+ case ScalarType.BYTES:
339
+ case ScalarType.STRING:
340
+ wireType = WireType.LengthDelimited;
352
341
  break;
353
- case scalar_js_1.ScalarType.DOUBLE:
354
- case scalar_js_1.ScalarType.FIXED64:
355
- case scalar_js_1.ScalarType.SFIXED64:
356
- wireType = binary_encoding_js_1.WireType.Bit64;
342
+ case ScalarType.DOUBLE:
343
+ case ScalarType.FIXED64:
344
+ case ScalarType.SFIXED64:
345
+ wireType = WireType.Bit64;
357
346
  break;
358
- case scalar_js_1.ScalarType.FIXED32:
359
- case scalar_js_1.ScalarType.SFIXED32:
360
- case scalar_js_1.ScalarType.FLOAT:
361
- wireType = binary_encoding_js_1.WireType.Bit32;
347
+ case ScalarType.FIXED32:
348
+ case ScalarType.SFIXED32:
349
+ case ScalarType.FLOAT:
350
+ wireType = WireType.Bit32;
362
351
  break;
363
352
  }
364
- const method = scalar_js_1.ScalarType[type].toLowerCase();
353
+ const method = ScalarType[type].toLowerCase();
365
354
  return [wireType, method];
366
355
  }
367
- function writeMapEntry(writer, options, field, key, value) {
368
- writer.tag(field.no, binary_encoding_js_1.WireType.LengthDelimited);
356
+ export function writeMapEntry(writer, options, field, key, value) {
357
+ writer.tag(field.no, WireType.LengthDelimited);
369
358
  writer.fork();
370
359
  // javascript only allows number or string for object properties
371
360
  // we convert from our representation to the protobuf type
372
361
  let keyValue = key;
373
362
  // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- we deliberately handle just the special cases for map keys
374
363
  switch (field.K) {
375
- case scalar_js_1.ScalarType.INT32:
376
- case scalar_js_1.ScalarType.FIXED32:
377
- case scalar_js_1.ScalarType.UINT32:
378
- case scalar_js_1.ScalarType.SFIXED32:
379
- case scalar_js_1.ScalarType.SINT32:
364
+ case ScalarType.INT32:
365
+ case ScalarType.FIXED32:
366
+ case ScalarType.UINT32:
367
+ case ScalarType.SFIXED32:
368
+ case ScalarType.SINT32:
380
369
  keyValue = Number.parseInt(key);
381
370
  break;
382
- case scalar_js_1.ScalarType.BOOL:
383
- (0, assert_js_1.assert)(key == "true" || key == "false");
371
+ case ScalarType.BOOL:
372
+ assert(key == "true" || key == "false");
384
373
  keyValue = key == "true";
385
374
  break;
386
375
  }
@@ -392,16 +381,15 @@ function writeMapEntry(writer, options, field, key, value) {
392
381
  writeScalar(writer, field.V.T, 2, value);
393
382
  break;
394
383
  case "enum":
395
- writeScalar(writer, scalar_js_1.ScalarType.INT32, 2, value);
384
+ writeScalar(writer, ScalarType.INT32, 2, value);
396
385
  break;
397
386
  case "message":
398
- (0, assert_js_1.assert)(value !== undefined);
399
- const messageType = (0, field_js_1.resolveMessageType)(field.V.T);
387
+ assert(value !== undefined);
388
+ const messageType = resolveMessageType(field.V.T);
400
389
  writer
401
- .tag(2, binary_encoding_js_1.WireType.LengthDelimited)
390
+ .tag(2, WireType.LengthDelimited)
402
391
  .bytes(messageType.toBinary(value, options));
403
392
  break;
404
393
  }
405
394
  writer.join();
406
395
  }
407
- exports.writeMapEntry = writeMapEntry;