@aptre/protobuf-es-lite 0.2.7 → 0.2.8

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 (121) hide show
  1. package/.ignore +1 -0
  2. package/README.md +23 -24
  3. package/bin/protoc-gen-es-lite +3 -3
  4. package/dist/binary-encoding.d.ts +420 -0
  5. package/dist/binary-encoding.js +437 -0
  6. package/dist/binary.d.ts +35 -2
  7. package/dist/binary.js +64 -65
  8. package/dist/codegen-info.d.ts +43 -0
  9. package/dist/codegen-info.js +72 -0
  10. package/dist/create-descriptor-set.d.ts +36 -0
  11. package/dist/create-descriptor-set.js +936 -0
  12. package/dist/descriptor-set.d.ts +672 -0
  13. package/dist/{editions.js → descriptor-set.js} +0 -11
  14. package/dist/enum.d.ts +35 -1
  15. package/dist/feature-set.d.ts +19 -0
  16. package/dist/feature-set.js +103 -0
  17. package/dist/field-wrapper.d.ts +7 -3
  18. package/dist/field-wrapper.js +30 -1
  19. package/dist/field.d.ts +8 -19
  20. package/dist/field.js +15 -117
  21. package/dist/google/index.d.ts +9 -0
  22. package/dist/google/index.js +41 -0
  23. package/dist/google/protobuf/any.pb.d.ts +139 -0
  24. package/dist/google/protobuf/any.pb.js +42 -0
  25. package/dist/google/protobuf/api.pb.d.ts +252 -0
  26. package/dist/google/protobuf/api.pb.js +70 -0
  27. package/dist/google/protobuf/compiler/plugin.pb.d.ts +240 -0
  28. package/dist/google/protobuf/compiler/plugin.pb.js +203 -0
  29. package/dist/google/protobuf/descriptor.pb.d.ts +2132 -0
  30. package/dist/google/protobuf/descriptor.pb.js +1080 -0
  31. package/dist/google/protobuf/duration.pb.d.ts +92 -0
  32. package/dist/google/protobuf/duration.pb.js +42 -0
  33. package/dist/google/protobuf/empty.pb.d.ts +18 -0
  34. package/dist/google/protobuf/empty.pb.js +39 -0
  35. package/dist/google/protobuf/source_context.pb.d.ts +21 -0
  36. package/dist/google/protobuf/source_context.pb.js +41 -0
  37. package/dist/google/protobuf/struct.pb.d.ts +121 -0
  38. package/dist/google/protobuf/struct.pb.js +83 -0
  39. package/dist/google/protobuf/timestamp.pb.d.ts +121 -0
  40. package/dist/google/protobuf/timestamp.pb.js +42 -0
  41. package/dist/google/protobuf/type.pb.d.ts +464 -0
  42. package/dist/google/protobuf/type.pb.js +338 -0
  43. package/dist/google/protobuf/wrappers.pb.d.ts +183 -0
  44. package/dist/google/protobuf/wrappers.pb.js +107 -0
  45. package/dist/index.d.ts +8 -3
  46. package/dist/index.js +38 -8
  47. package/dist/is-message.d.ts +10 -2
  48. package/dist/is-message.js +74 -3
  49. package/dist/json.d.ts +68 -1
  50. package/dist/json.js +57 -81
  51. package/dist/message.d.ts +30 -61
  52. package/dist/message.js +61 -20
  53. package/dist/names.d.ts +43 -0
  54. package/dist/names.js +276 -0
  55. package/dist/partial.js +14 -15
  56. package/dist/proto-base64.d.ts +18 -0
  57. package/dist/proto-base64.js +128 -0
  58. package/dist/proto-double.d.ts +5 -0
  59. package/dist/proto-double.js +29 -0
  60. package/dist/proto-int64.d.ts +95 -0
  61. package/dist/proto-int64.js +116 -0
  62. package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.d.ts +1 -0
  63. package/dist/{protoc-gen-es-lite-plugin.js → protoc-gen-es-lite/protoc-gen-es-lite-plugin.js} +2 -2
  64. package/dist/{typescript.d.ts → protoc-gen-es-lite/typescript.d.ts} +3 -2
  65. package/dist/{typescript.js → protoc-gen-es-lite/typescript.js} +46 -34
  66. package/dist/protoplugin/create-es-plugin.d.ts +83 -0
  67. package/dist/protoplugin/create-es-plugin.js +119 -0
  68. package/dist/protoplugin/ecmascript/file-preamble.d.ts +2 -0
  69. package/dist/protoplugin/ecmascript/file-preamble.js +74 -0
  70. package/dist/protoplugin/ecmascript/generated-file.d.ts +116 -0
  71. package/dist/protoplugin/ecmascript/generated-file.js +407 -0
  72. package/dist/protoplugin/ecmascript/import-path.d.ts +65 -0
  73. package/dist/protoplugin/ecmascript/import-path.js +156 -0
  74. package/dist/protoplugin/ecmascript/import-symbol.d.ts +39 -0
  75. package/dist/protoplugin/ecmascript/import-symbol.js +37 -0
  76. package/dist/protoplugin/ecmascript/index.d.ts +7 -0
  77. package/dist/protoplugin/ecmascript/index.js +20 -0
  78. package/dist/protoplugin/ecmascript/jsdoc.d.ts +14 -0
  79. package/dist/protoplugin/ecmascript/jsdoc.js +93 -0
  80. package/dist/protoplugin/ecmascript/opaque-printables.d.ts +27 -0
  81. package/dist/protoplugin/ecmascript/opaque-printables.js +15 -0
  82. package/dist/protoplugin/ecmascript/parameter.d.ts +13 -0
  83. package/dist/protoplugin/ecmascript/parameter.js +161 -0
  84. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +100 -0
  85. package/dist/protoplugin/ecmascript/reify-wkt.js +170 -0
  86. package/dist/protoplugin/ecmascript/runtime-imports.d.ts +6 -0
  87. package/dist/protoplugin/ecmascript/runtime-imports.js +28 -0
  88. package/dist/protoplugin/ecmascript/schema.d.ts +43 -0
  89. package/dist/protoplugin/ecmascript/schema.js +73 -0
  90. package/dist/protoplugin/ecmascript/target.d.ts +4 -0
  91. package/dist/protoplugin/ecmascript/target.js +15 -0
  92. package/dist/protoplugin/ecmascript/transpile.d.ts +2 -0
  93. package/dist/protoplugin/ecmascript/transpile.js +149 -0
  94. package/dist/protoplugin/error.d.ts +6 -0
  95. package/dist/protoplugin/error.js +43 -0
  96. package/dist/protoplugin/index.d.ts +8 -0
  97. package/dist/protoplugin/index.js +20 -0
  98. package/dist/protoplugin/plugin.d.ts +18 -0
  99. package/dist/protoplugin/plugin.js +15 -0
  100. package/dist/protoplugin/run-node.d.ts +12 -0
  101. package/dist/protoplugin/run-node.js +83 -0
  102. package/dist/scalar.d.ts +53 -1
  103. package/dist/scalar.js +87 -22
  104. package/dist/service-type.d.ts +95 -0
  105. package/dist/service-type.js +53 -0
  106. package/dist/text-format.d.ts +4 -0
  107. package/dist/text-format.js +189 -0
  108. package/dist/type-registry.d.ts +43 -0
  109. package/dist/type-registry.js +15 -0
  110. package/dist/unknown.d.ts +1 -1
  111. package/dist/util.d.ts +2 -2
  112. package/dist/util.js +46 -44
  113. package/dist/varint.d.ts +70 -0
  114. package/dist/varint.js +324 -0
  115. package/example/example.pb.ts +87 -0
  116. package/example/example.proto +1 -0
  117. package/example/tsconfig.json +8 -2
  118. package/package.json +19 -12
  119. package/tsconfig.json +4 -4
  120. package/dist/editions.d.ts +0 -6
  121. package/dist/protoc-gen-es-lite-plugin.d.ts +0 -1
@@ -0,0 +1,437 @@
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.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");
20
+ /* eslint-disable prefer-const,no-case-declarations,@typescript-eslint/restrict-plus-operands */
21
+ /**
22
+ * Protobuf binary format wire types.
23
+ *
24
+ * A wire type provides just enough information to find the length of the
25
+ * following value.
26
+ *
27
+ * See https://developers.google.com/protocol-buffers/docs/encoding#structure
28
+ */
29
+ var WireType;
30
+ (function (WireType) {
31
+ /**
32
+ * Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
33
+ */
34
+ WireType[WireType["Varint"] = 0] = "Varint";
35
+ /**
36
+ * Used for fixed64, sfixed64, double.
37
+ * Always 8 bytes with little-endian byte order.
38
+ */
39
+ WireType[WireType["Bit64"] = 1] = "Bit64";
40
+ /**
41
+ * Used for string, bytes, embedded messages, packed repeated fields
42
+ *
43
+ * Only repeated numeric types (types which use the varint, 32-bit,
44
+ * or 64-bit wire types) can be packed. In proto3, such fields are
45
+ * packed by default.
46
+ */
47
+ WireType[WireType["LengthDelimited"] = 2] = "LengthDelimited";
48
+ /**
49
+ * Start of a tag-delimited aggregate, such as a proto2 group, or a message
50
+ * in editions with message_encoding = DELIMITED.
51
+ */
52
+ WireType[WireType["StartGroup"] = 3] = "StartGroup";
53
+ /**
54
+ * End of a tag-delimited aggregate.
55
+ */
56
+ WireType[WireType["EndGroup"] = 4] = "EndGroup";
57
+ /**
58
+ * Used for fixed32, sfixed32, float.
59
+ * Always 4 bytes with little-endian byte order.
60
+ */
61
+ WireType[WireType["Bit32"] = 5] = "Bit32";
62
+ })(WireType || (exports.WireType = WireType = {}));
63
+ class BinaryWriter {
64
+ constructor(textEncoder) {
65
+ /**
66
+ * Previous fork states.
67
+ */
68
+ this.stack = [];
69
+ this.textEncoder = textEncoder ?? new TextEncoder();
70
+ this.chunks = [];
71
+ this.buf = [];
72
+ }
73
+ /**
74
+ * Return all bytes written and reset this writer.
75
+ */
76
+ finish() {
77
+ this.chunks.push(new Uint8Array(this.buf)); // flush the buffer
78
+ let len = 0;
79
+ for (let i = 0; i < this.chunks.length; i++)
80
+ len += this.chunks[i].length;
81
+ let bytes = new Uint8Array(len);
82
+ let offset = 0;
83
+ for (let i = 0; i < this.chunks.length; i++) {
84
+ bytes.set(this.chunks[i], offset);
85
+ offset += this.chunks[i].length;
86
+ }
87
+ this.chunks = [];
88
+ return bytes;
89
+ }
90
+ /**
91
+ * Start a new fork for length-delimited data like a message
92
+ * or a packed repeated field.
93
+ *
94
+ * Must be joined later with `join()`.
95
+ */
96
+ fork() {
97
+ this.stack.push({ chunks: this.chunks, buf: this.buf });
98
+ this.chunks = [];
99
+ this.buf = [];
100
+ return this;
101
+ }
102
+ /**
103
+ * Join the last fork. Write its length and bytes, then
104
+ * return to the previous state.
105
+ */
106
+ join() {
107
+ // get chunk of fork
108
+ let chunk = this.finish();
109
+ // restore previous state
110
+ let prev = this.stack.pop();
111
+ if (!prev)
112
+ throw new Error("invalid state, fork stack empty");
113
+ this.chunks = prev.chunks;
114
+ this.buf = prev.buf;
115
+ // write length of chunk as varint
116
+ this.uint32(chunk.byteLength);
117
+ return this.raw(chunk);
118
+ }
119
+ /**
120
+ * Writes a tag (field number and wire type).
121
+ *
122
+ * Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
123
+ *
124
+ * Generated code should compute the tag ahead of time and call `uint32()`.
125
+ */
126
+ tag(fieldNo, type) {
127
+ return this.uint32(((fieldNo << 3) | type) >>> 0);
128
+ }
129
+ /**
130
+ * Write a chunk of raw bytes.
131
+ */
132
+ raw(chunk) {
133
+ if (this.buf.length) {
134
+ this.chunks.push(new Uint8Array(this.buf));
135
+ this.buf = [];
136
+ }
137
+ this.chunks.push(chunk);
138
+ return this;
139
+ }
140
+ /**
141
+ * Write a `uint32` value, an unsigned 32 bit varint.
142
+ */
143
+ uint32(value) {
144
+ (0, assert_js_1.assertUInt32)(value);
145
+ // write value as varint 32, inlined for speed
146
+ while (value > 0x7f) {
147
+ this.buf.push((value & 0x7f) | 0x80);
148
+ value = value >>> 7;
149
+ }
150
+ this.buf.push(value);
151
+ return this;
152
+ }
153
+ /**
154
+ * Write a `int32` value, a signed 32 bit varint.
155
+ */
156
+ int32(value) {
157
+ (0, assert_js_1.assertInt32)(value);
158
+ (0, varint_js_1.varint32write)(value, this.buf);
159
+ return this;
160
+ }
161
+ /**
162
+ * Write a `bool` value, a variant.
163
+ */
164
+ bool(value) {
165
+ this.buf.push(value ? 1 : 0);
166
+ return this;
167
+ }
168
+ /**
169
+ * Write a `bytes` value, length-delimited arbitrary data.
170
+ */
171
+ bytes(value) {
172
+ this.uint32(value.byteLength); // write length of chunk as varint
173
+ return this.raw(value);
174
+ }
175
+ /**
176
+ * Write a `string` value, length-delimited data converted to UTF-8 text.
177
+ */
178
+ string(value) {
179
+ let chunk = this.textEncoder.encode(value);
180
+ this.uint32(chunk.byteLength); // write length of chunk as varint
181
+ return this.raw(chunk);
182
+ }
183
+ /**
184
+ * Write a `float` value, 32-bit floating point number.
185
+ */
186
+ float(value) {
187
+ (0, assert_js_1.assertFloat32)(value);
188
+ let chunk = new Uint8Array(4);
189
+ new DataView(chunk.buffer).setFloat32(0, value, true);
190
+ return this.raw(chunk);
191
+ }
192
+ /**
193
+ * Write a `double` value, a 64-bit floating point number.
194
+ */
195
+ double(value) {
196
+ let chunk = new Uint8Array(8);
197
+ new DataView(chunk.buffer).setFloat64(0, value, true);
198
+ return this.raw(chunk);
199
+ }
200
+ /**
201
+ * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
202
+ */
203
+ fixed32(value) {
204
+ (0, assert_js_1.assertUInt32)(value);
205
+ let chunk = new Uint8Array(4);
206
+ new DataView(chunk.buffer).setUint32(0, value, true);
207
+ return this.raw(chunk);
208
+ }
209
+ /**
210
+ * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
211
+ */
212
+ sfixed32(value) {
213
+ (0, assert_js_1.assertInt32)(value);
214
+ let chunk = new Uint8Array(4);
215
+ new DataView(chunk.buffer).setInt32(0, value, true);
216
+ return this.raw(chunk);
217
+ }
218
+ /**
219
+ * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
220
+ */
221
+ sint32(value) {
222
+ (0, assert_js_1.assertInt32)(value);
223
+ // zigzag encode
224
+ value = ((value << 1) ^ (value >> 31)) >>> 0;
225
+ (0, varint_js_1.varint32write)(value, this.buf);
226
+ return this;
227
+ }
228
+ /**
229
+ * Write a `fixed64` value, a signed, fixed-length 64-bit integer.
230
+ */
231
+ sfixed64(value) {
232
+ let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = proto_int64_js_1.protoInt64.enc(value);
233
+ view.setInt32(0, tc.lo, true);
234
+ view.setInt32(4, tc.hi, true);
235
+ return this.raw(chunk);
236
+ }
237
+ /**
238
+ * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
239
+ */
240
+ fixed64(value) {
241
+ let chunk = new Uint8Array(8), view = new DataView(chunk.buffer), tc = proto_int64_js_1.protoInt64.uEnc(value);
242
+ view.setInt32(0, tc.lo, true);
243
+ view.setInt32(4, tc.hi, true);
244
+ return this.raw(chunk);
245
+ }
246
+ /**
247
+ * Write a `int64` value, a signed 64-bit varint.
248
+ */
249
+ int64(value) {
250
+ let tc = proto_int64_js_1.protoInt64.enc(value);
251
+ (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
252
+ return this;
253
+ }
254
+ /**
255
+ * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
256
+ */
257
+ sint64(value) {
258
+ let tc = proto_int64_js_1.protoInt64.enc(value),
259
+ // zigzag encode
260
+ 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);
262
+ return this;
263
+ }
264
+ /**
265
+ * Write a `uint64` value, an unsigned 64-bit varint.
266
+ */
267
+ uint64(value) {
268
+ let tc = proto_int64_js_1.protoInt64.uEnc(value);
269
+ (0, varint_js_1.varint64write)(tc.lo, tc.hi, this.buf);
270
+ return this;
271
+ }
272
+ }
273
+ exports.BinaryWriter = BinaryWriter;
274
+ class BinaryReader {
275
+ constructor(buf, textDecoder) {
276
+ this.varint64 = varint_js_1.varint64read; // dirty cast for `this`
277
+ /**
278
+ * Read a `uint32` field, an unsigned 32 bit varint.
279
+ */
280
+ this.uint32 = varint_js_1.varint32read; // dirty cast for `this` and access to protected `buf`
281
+ this.buf = buf;
282
+ this.len = buf.length;
283
+ this.pos = 0;
284
+ this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
285
+ this.textDecoder = textDecoder ?? new TextDecoder();
286
+ }
287
+ /**
288
+ * Reads a tag - field number and wire type.
289
+ */
290
+ tag() {
291
+ let tag = this.uint32(), fieldNo = tag >>> 3, wireType = tag & 7;
292
+ if (fieldNo <= 0 || wireType < 0 || wireType > 5)
293
+ throw new Error("illegal tag: field no " + fieldNo + " wire type " + wireType);
294
+ return [fieldNo, wireType];
295
+ }
296
+ /**
297
+ * Skip one element on the wire and return the skipped data.
298
+ * Supports WireType.StartGroup since v2.0.0-alpha.23.
299
+ */
300
+ skip(wireType) {
301
+ let start = this.pos;
302
+ switch (wireType) {
303
+ case WireType.Varint:
304
+ while (this.buf[this.pos++] & 0x80) {
305
+ // ignore
306
+ }
307
+ break;
308
+ // eslint-disable-next-line
309
+ // @ts-ignore TS7029: Fallthrough case in switch
310
+ case WireType.Bit64:
311
+ this.pos += 4;
312
+ // eslint-disable-next-line
313
+ // @ts-ignore TS7029: Fallthrough case in switch
314
+ case WireType.Bit32:
315
+ this.pos += 4;
316
+ break;
317
+ case WireType.LengthDelimited:
318
+ let len = this.uint32();
319
+ this.pos += len;
320
+ break;
321
+ case WireType.StartGroup:
322
+ // TODO check for matching field numbers in StartGroup / EndGroup tags
323
+ let t;
324
+ while ((t = this.tag()[1]) !== WireType.EndGroup) {
325
+ this.skip(t);
326
+ }
327
+ break;
328
+ default:
329
+ throw new Error("cant skip wire type " + wireType);
330
+ }
331
+ this.assertBounds();
332
+ return this.buf.subarray(start, this.pos);
333
+ }
334
+ /**
335
+ * Throws error if position in byte array is out of range.
336
+ */
337
+ assertBounds() {
338
+ if (this.pos > this.len)
339
+ throw new RangeError("premature EOF");
340
+ }
341
+ /**
342
+ * Read a `int32` field, a signed 32 bit varint.
343
+ */
344
+ int32() {
345
+ return this.uint32() | 0;
346
+ }
347
+ /**
348
+ * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
349
+ */
350
+ sint32() {
351
+ let zze = this.uint32();
352
+ // decode zigzag
353
+ return (zze >>> 1) ^ -(zze & 1);
354
+ }
355
+ /**
356
+ * Read a `int64` field, a signed 64-bit varint.
357
+ */
358
+ int64() {
359
+ return proto_int64_js_1.protoInt64.dec(...this.varint64());
360
+ }
361
+ /**
362
+ * Read a `uint64` field, an unsigned 64-bit varint.
363
+ */
364
+ uint64() {
365
+ return proto_int64_js_1.protoInt64.uDec(...this.varint64());
366
+ }
367
+ /**
368
+ * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
369
+ */
370
+ sint64() {
371
+ let [lo, hi] = this.varint64();
372
+ // decode zig zag
373
+ let s = -(lo & 1);
374
+ lo = ((lo >>> 1) | ((hi & 1) << 31)) ^ s;
375
+ hi = (hi >>> 1) ^ s;
376
+ return proto_int64_js_1.protoInt64.dec(lo, hi);
377
+ }
378
+ /**
379
+ * Read a `bool` field, a variant.
380
+ */
381
+ bool() {
382
+ let [lo, hi] = this.varint64();
383
+ return lo !== 0 || hi !== 0;
384
+ }
385
+ /**
386
+ * Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
387
+ */
388
+ fixed32() {
389
+ return this.view.getUint32((this.pos += 4) - 4, true);
390
+ }
391
+ /**
392
+ * Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
393
+ */
394
+ sfixed32() {
395
+ return this.view.getInt32((this.pos += 4) - 4, true);
396
+ }
397
+ /**
398
+ * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
399
+ */
400
+ fixed64() {
401
+ return proto_int64_js_1.protoInt64.uDec(this.sfixed32(), this.sfixed32());
402
+ }
403
+ /**
404
+ * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
405
+ */
406
+ sfixed64() {
407
+ return proto_int64_js_1.protoInt64.dec(this.sfixed32(), this.sfixed32());
408
+ }
409
+ /**
410
+ * Read a `float` field, 32-bit floating point number.
411
+ */
412
+ float() {
413
+ return this.view.getFloat32((this.pos += 4) - 4, true);
414
+ }
415
+ /**
416
+ * Read a `double` field, a 64-bit floating point number.
417
+ */
418
+ double() {
419
+ return this.view.getFloat64((this.pos += 8) - 8, true);
420
+ }
421
+ /**
422
+ * Read a `bytes` field, length-delimited arbitrary data.
423
+ */
424
+ bytes() {
425
+ let len = this.uint32(), start = this.pos;
426
+ this.pos += len;
427
+ this.assertBounds();
428
+ return this.buf.subarray(start, start + len);
429
+ }
430
+ /**
431
+ * Read a `string` field, length-delimited data converted to UTF-8 text.
432
+ */
433
+ string() {
434
+ return this.textDecoder.decode(this.bytes());
435
+ }
436
+ }
437
+ exports.BinaryReader = BinaryReader;
package/dist/binary.d.ts CHANGED
@@ -1,5 +1,38 @@
1
- import { BinaryReadOptions, BinaryWriteOptions, IBinaryReader, IBinaryWriter, ScalarType, ScalarValue, WireType } from "@bufbuild/protobuf";
2
1
  import { FieldInfo, FieldList } from "./field.js";
2
+ import { ScalarType, ScalarValue } from "./scalar.js";
3
+ import { IBinaryReader, IBinaryWriter, WireType } from "./binary-encoding.js";
4
+ /**
5
+ * Options for parsing binary data.
6
+ */
7
+ export interface BinaryReadOptions {
8
+ /**
9
+ * Retain unknown fields during parsing? The default behavior is to retain
10
+ * unknown fields and include them in the serialized output.
11
+ *
12
+ * For more details see https://developers.google.com/protocol-buffers/docs/proto3#unknowns
13
+ */
14
+ readUnknownFields: boolean;
15
+ /**
16
+ * Allows to use a custom implementation to decode binary data.
17
+ */
18
+ readerFactory: (bytes: Uint8Array) => IBinaryReader;
19
+ }
20
+ /**
21
+ * Options for serializing to binary data.
22
+ */
23
+ export interface BinaryWriteOptions {
24
+ /**
25
+ * Include unknown fields in the serialized output? The default behavior
26
+ * is to retain unknown fields and include them in the serialized output.
27
+ *
28
+ * For more details see https://developers.google.com/protocol-buffers/docs/proto3#unknowns
29
+ */
30
+ writeUnknownFields: boolean;
31
+ /**
32
+ * Allows to use a custom implementation to encode binary data.
33
+ */
34
+ writerFactory: () => IBinaryWriter;
35
+ }
3
36
  export declare function readField(target: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
4
37
  reader: IBinaryReader, field: FieldInfo, wireType: WireType, options: BinaryReadOptions): void;
5
38
  /**
@@ -12,7 +45,7 @@ type AnyMessage = {
12
45
  };
13
46
  export declare function readMapEntry(field: FieldInfo & {
14
47
  kind: "map";
15
- }, reader: IBinaryReader, options: BinaryReadOptions): [string | number, ScalarValue | AnyMessage];
48
+ }, reader: IBinaryReader, options: BinaryReadOptions): [string | number, ScalarValue | AnyMessage | undefined];
16
49
  export declare function readScalar(reader: IBinaryReader, type: ScalarType): ScalarValue;
17
50
  export declare function readScalarLTString(reader: IBinaryReader, type: ScalarType): Exclude<ScalarValue, bigint>;
18
51
  export declare function makeReadOptions(options?: Partial<BinaryReadOptions>): Readonly<BinaryReadOptions>;