@bufbuild/protobuf 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/binary-encoding.js +434 -0
  3. package/dist/cjs/binary-format.js +2 -0
  4. package/dist/cjs/descriptor-registry.js +518 -0
  5. package/dist/cjs/descriptor-set.js +557 -0
  6. package/dist/cjs/enum.js +2 -0
  7. package/dist/cjs/field-list.js +2 -0
  8. package/dist/cjs/field.js +40 -0
  9. package/dist/cjs/google/protobuf/any_pb.js +280 -0
  10. package/dist/cjs/google/protobuf/api_pb.js +383 -0
  11. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +334 -0
  12. package/dist/cjs/google/protobuf/descriptor_pb.js +2311 -0
  13. package/dist/cjs/google/protobuf/duration_pb.js +201 -0
  14. package/dist/cjs/google/protobuf/empty_pb.js +72 -0
  15. package/dist/cjs/google/protobuf/field_mask_pb.js +347 -0
  16. package/dist/cjs/google/protobuf/source_context_pb.js +74 -0
  17. package/dist/cjs/google/protobuf/struct_pb.js +288 -0
  18. package/dist/cjs/google/protobuf/timestamp_pb.js +271 -0
  19. package/dist/cjs/google/protobuf/type_pb.js +613 -0
  20. package/dist/cjs/google/protobuf/wrappers_pb.js +658 -0
  21. package/dist/cjs/google/varint.js +280 -0
  22. package/dist/cjs/index.js +148 -0
  23. package/dist/cjs/json-format.js +2 -0
  24. package/dist/cjs/message-type.js +2 -0
  25. package/dist/cjs/message.js +98 -0
  26. package/dist/cjs/private/assert.js +52 -0
  27. package/dist/cjs/private/base64.js +118 -0
  28. package/dist/cjs/private/binary-format-common.js +253 -0
  29. package/dist/cjs/private/binary-format-proto2.js +120 -0
  30. package/dist/cjs/private/binary-format-proto3.js +102 -0
  31. package/dist/cjs/private/enum.js +70 -0
  32. package/dist/cjs/private/field-list.js +62 -0
  33. package/dist/cjs/private/field-wrapper.js +25 -0
  34. package/dist/cjs/private/field.js +34 -0
  35. package/dist/cjs/private/json-format-common.js +482 -0
  36. package/dist/cjs/private/json-format-proto2.js +100 -0
  37. package/dist/cjs/private/json-format-proto3.js +103 -0
  38. package/dist/cjs/private/message-type.js +37 -0
  39. package/dist/cjs/private/names.js +101 -0
  40. package/dist/cjs/private/options-map.js +2 -0
  41. package/dist/cjs/private/proto-runtime.js +24 -0
  42. package/dist/cjs/private/scalars.js +138 -0
  43. package/dist/cjs/private/util-common.js +229 -0
  44. package/dist/cjs/private/util.js +2 -0
  45. package/dist/cjs/proto-int64.js +121 -0
  46. package/dist/cjs/proto2.js +86 -0
  47. package/dist/cjs/proto3.js +99 -0
  48. package/dist/cjs/service-type.js +43 -0
  49. package/dist/cjs/type-registry.js +42 -0
  50. package/dist/esm/binary-encoding.js +434 -0
  51. package/dist/esm/binary-format.js +1 -0
  52. package/dist/esm/descriptor-registry.js +521 -0
  53. package/dist/esm/descriptor-set.js +517 -0
  54. package/dist/esm/enum.js +1 -0
  55. package/dist/esm/field-list.js +1 -0
  56. package/dist/esm/field.js +37 -0
  57. package/dist/esm/google/protobuf/any_pb.js +276 -0
  58. package/dist/esm/google/protobuf/api_pb.js +350 -0
  59. package/dist/esm/google/protobuf/compiler/plugin_pb.js +318 -0
  60. package/dist/esm/google/protobuf/descriptor_pb.js +2213 -0
  61. package/dist/esm/google/protobuf/duration_pb.js +197 -0
  62. package/dist/esm/google/protobuf/empty_pb.js +68 -0
  63. package/dist/esm/google/protobuf/field_mask_pb.js +343 -0
  64. package/dist/esm/google/protobuf/source_context_pb.js +70 -0
  65. package/dist/esm/google/protobuf/struct_pb.js +282 -0
  66. package/dist/esm/google/protobuf/timestamp_pb.js +267 -0
  67. package/dist/esm/google/protobuf/type_pb.js +569 -0
  68. package/dist/esm/google/protobuf/wrappers_pb.js +574 -0
  69. package/dist/esm/google/varint.js +265 -0
  70. package/dist/esm/index.js +24 -0
  71. package/dist/esm/json-format.js +1 -0
  72. package/dist/esm/message-type.js +1 -0
  73. package/dist/esm/message.js +94 -0
  74. package/dist/esm/private/assert.js +41 -0
  75. package/dist/esm/private/base64.js +113 -0
  76. package/dist/esm/private/binary-format-common.js +243 -0
  77. package/dist/esm/private/binary-format-proto2.js +87 -0
  78. package/dist/esm/private/binary-format-proto3.js +74 -0
  79. package/dist/esm/private/enum.js +64 -0
  80. package/dist/esm/private/field-list.js +58 -0
  81. package/dist/esm/private/field-wrapper.js +20 -0
  82. package/dist/esm/private/field.js +27 -0
  83. package/dist/esm/private/json-format-common.js +473 -0
  84. package/dist/esm/private/json-format-proto2.js +90 -0
  85. package/dist/esm/private/json-format-proto3.js +89 -0
  86. package/dist/esm/private/message-type.js +33 -0
  87. package/dist/esm/private/names.js +90 -0
  88. package/dist/esm/private/options-map.js +1 -0
  89. package/dist/esm/private/proto-runtime.js +15 -0
  90. package/dist/esm/private/scalars.js +129 -0
  91. package/dist/esm/private/util-common.js +207 -0
  92. package/dist/esm/private/util.js +1 -0
  93. package/dist/esm/proto-int64.js +118 -0
  94. package/dist/esm/proto2.js +77 -0
  95. package/dist/esm/proto3.js +90 -0
  96. package/dist/esm/service-type.js +37 -0
  97. package/dist/esm/type-registry.js +38 -0
  98. package/dist/types/binary-encoding.d.ts +421 -0
  99. package/dist/types/binary-format.d.ts +109 -0
  100. package/dist/types/descriptor-registry.d.ts +42 -0
  101. package/dist/types/descriptor-set.d.ts +171 -0
  102. package/dist/types/enum.d.ts +31 -0
  103. package/dist/types/field-list.d.ts +27 -0
  104. package/dist/types/field.d.ts +295 -0
  105. package/dist/types/google/protobuf/any_pb.d.ts +167 -0
  106. package/dist/types/google/protobuf/api_pb.d.ts +296 -0
  107. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +294 -0
  108. package/dist/types/google/protobuf/descriptor_pb.d.ts +1959 -0
  109. package/dist/types/google/protobuf/duration_pb.d.ts +117 -0
  110. package/dist/types/google/protobuf/empty_pb.d.ts +44 -0
  111. package/dist/types/google/protobuf/field_mask_pb.d.ts +243 -0
  112. package/dist/types/google/protobuf/source_context_pb.d.ts +44 -0
  113. package/dist/types/google/protobuf/struct_pb.d.ts +205 -0
  114. package/dist/types/google/protobuf/timestamp_pb.d.ts +151 -0
  115. package/dist/types/google/protobuf/type_pb.d.ts +482 -0
  116. package/dist/types/google/protobuf/wrappers_pb.d.ts +379 -0
  117. package/dist/types/google/varint.d.ts +65 -0
  118. package/dist/types/index.d.ts +53 -0
  119. package/dist/types/json-format.d.ts +129 -0
  120. package/dist/types/message-type.d.ts +58 -0
  121. package/dist/types/message.d.ts +124 -0
  122. package/dist/types/private/assert.d.ts +19 -0
  123. package/dist/types/private/base64.d.ts +18 -0
  124. package/dist/types/private/binary-format-common.d.ts +38 -0
  125. package/dist/types/private/binary-format-proto2.d.ts +2 -0
  126. package/dist/types/private/binary-format-proto3.d.ts +2 -0
  127. package/dist/types/private/enum.d.ts +29 -0
  128. package/dist/types/private/field-list.d.ts +25 -0
  129. package/dist/types/private/field-wrapper.d.ts +27 -0
  130. package/dist/types/private/field.d.ts +15 -0
  131. package/dist/types/private/json-format-common.d.ts +30 -0
  132. package/dist/types/private/json-format-proto2.d.ts +2 -0
  133. package/dist/types/private/json-format-proto3.d.ts +2 -0
  134. package/dist/types/private/message-type.d.ts +15 -0
  135. package/dist/types/private/names.d.ts +19 -0
  136. package/dist/types/private/options-map.d.ts +7 -0
  137. package/dist/types/private/proto-runtime.d.ts +55 -0
  138. package/dist/types/private/scalars.d.ts +33 -0
  139. package/dist/types/private/util-common.d.ts +5 -0
  140. package/dist/types/private/util.d.ts +55 -0
  141. package/dist/types/proto-int64.d.ts +94 -0
  142. package/dist/types/proto2.d.ts +4 -0
  143. package/dist/types/proto3.d.ts +4 -0
  144. package/dist/types/service-type.d.ts +118 -0
  145. package/dist/types/type-registry.d.ts +37 -0
  146. package/package.json +31 -0
@@ -0,0 +1,90 @@
1
+ import { makeProtoRuntime } from "./private/proto-runtime.js";
2
+ import { makeBinaryFormatProto3 } from "./private/binary-format-proto3.js";
3
+ import { makeJsonFormatProto3 } from "./private/json-format-proto3.js";
4
+ import { makeUtilCommon } from "./private/util-common.js";
5
+ import { InternalFieldList } from "./private/field-list.js";
6
+ import { scalarDefaultValue } from "./private/scalars.js";
7
+ import { ScalarType } from "./field.js";
8
+ import { InternalOneofInfo } from "./private/field.js";
9
+ import { makeFieldName, makeJsonName } from "./private/names.js";
10
+ /**
11
+ * Provides functionality for messages defined with the proto3 syntax.
12
+ */
13
+ export const proto3 = makeProtoRuntime(
14
+ "proto3",
15
+ makeJsonFormatProto3(),
16
+ makeBinaryFormatProto3(),
17
+ {
18
+ ...makeUtilCommon(),
19
+ newFieldList(fields) {
20
+ return new InternalFieldList(fields, normalizeFieldInfosProto3);
21
+ },
22
+ initFields(target) {
23
+ for (const member of target.getType().fields.byMember()) {
24
+ if (member.opt) {
25
+ continue;
26
+ }
27
+ const name = member.localName,
28
+ t = target;
29
+ if (member.repeated) {
30
+ t[name] = [];
31
+ continue;
32
+ }
33
+ switch (member.kind) {
34
+ case "oneof":
35
+ t[name] = { case: undefined };
36
+ break;
37
+ case "enum":
38
+ t[name] = 0;
39
+ break;
40
+ case "map":
41
+ t[name] = {};
42
+ break;
43
+ case "scalar":
44
+ t[name] = scalarDefaultValue(member.T); // eslint-disable-line @typescript-eslint/no-unsafe-assignment
45
+ break;
46
+ case "message":
47
+ // message fields are always optional in proto3
48
+ break;
49
+ }
50
+ }
51
+ },
52
+ }
53
+ );
54
+ /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
55
+ function normalizeFieldInfosProto3(fieldInfos) {
56
+ const r = [];
57
+ let o;
58
+ for (const field of typeof fieldInfos == "function"
59
+ ? fieldInfos()
60
+ : fieldInfos) {
61
+ const f = field;
62
+ f.localName = makeFieldName(field.name, field.oneof !== undefined);
63
+ f.jsonName = field.jsonName ?? makeJsonName(field.name);
64
+ f.repeated = field.repeated ?? false;
65
+ // From the proto3 language guide:
66
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
67
+ // This information is incomplete - according to the conformance tests, BOOL
68
+ // and ENUM are packed by default as well. This means only STRING and BYTES
69
+ // are not packed by default, which makes sense because they are length-delimited.
70
+ f.packed =
71
+ field.packed ??
72
+ (field.kind == "enum" ||
73
+ (field.kind == "scalar" &&
74
+ field.T != ScalarType.BYTES &&
75
+ field.T != ScalarType.STRING));
76
+ // We do not surface options at this time
77
+ // f.options = field.options ?? emptyReadonlyObject;
78
+ if (field.oneof !== undefined) {
79
+ const ooname =
80
+ typeof field.oneof == "string" ? field.oneof : field.oneof.name;
81
+ if (!o || o.name != ooname) {
82
+ o = new InternalOneofInfo(ooname);
83
+ }
84
+ f.oneof = o;
85
+ o.addField(f);
86
+ }
87
+ r.push(f);
88
+ }
89
+ return r;
90
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * MethodKind represents the four method types that can be declared in
3
+ * protobuf with the `stream` keyword:
4
+ *
5
+ * 1. Unary: rpc (Input) returns (Output)
6
+ * 2. ServerStreaming: rpc (Input) returns (stream Output)
7
+ * 3. ClientStreaming: rpc (stream Input) returns (Output)
8
+ * 4. BiDiStreaming: rpc (stream Input) returns (stream Output)
9
+ */
10
+ export var MethodKind;
11
+ (function (MethodKind) {
12
+ MethodKind[(MethodKind["Unary"] = 0)] = "Unary";
13
+ MethodKind[(MethodKind["ServerStreaming"] = 1)] = "ServerStreaming";
14
+ MethodKind[(MethodKind["ClientStreaming"] = 2)] = "ClientStreaming";
15
+ MethodKind[(MethodKind["BiDiStreaming"] = 3)] = "BiDiStreaming";
16
+ })(MethodKind || (MethodKind = {}));
17
+ /**
18
+ * Is this method side-effect-free (or safe in HTTP parlance), or just
19
+ * idempotent, or neither? HTTP based RPC implementation may choose GET verb
20
+ * for safe methods, and PUT verb for idempotent methods instead of the
21
+ * default POST.
22
+ *
23
+ * This enum matches the protobuf enum google.protobuf.MethodOptions.IdempotencyLevel,
24
+ * defined in the well-known type google/protobuf/descriptor.proto, but
25
+ * drops UNKNOWN.
26
+ */
27
+ export var MethodIdempotency;
28
+ (function (MethodIdempotency) {
29
+ /**
30
+ * Idempotent, no side effects.
31
+ */
32
+ MethodIdempotency[(MethodIdempotency["NoSideEffects"] = 1)] = "NoSideEffects";
33
+ /**
34
+ * Idempotent, but may have side effects.
35
+ */
36
+ MethodIdempotency[(MethodIdempotency["Idempotent"] = 2)] = "Idempotent";
37
+ })(MethodIdempotency || (MethodIdempotency = {}));
@@ -0,0 +1,38 @@
1
+ /**
2
+ * TypeRegistry is a basic type registry
3
+ */
4
+ export class TypeRegistry {
5
+ constructor() {
6
+ this.messages = {};
7
+ this.enums = {};
8
+ this.services = {};
9
+ }
10
+ findMessage(typeName) {
11
+ return this.messages[typeName];
12
+ }
13
+ findEnum(typeName) {
14
+ return this.enums[typeName];
15
+ }
16
+ findService(typeName) {
17
+ return this.services[typeName];
18
+ }
19
+ add(type) {
20
+ if ("fields" in type) {
21
+ this.messages[type.typeName] = type;
22
+ } else if ("methods" in type) {
23
+ this.services[type.typeName] = type;
24
+ } else {
25
+ this.enums[type.typeName] = type;
26
+ }
27
+ }
28
+ static fromIterable(types) {
29
+ const r = new TypeRegistry();
30
+ for (const t of types) {
31
+ r.add(t);
32
+ }
33
+ return r;
34
+ }
35
+ static fromTypes(...types) {
36
+ return TypeRegistry.fromIterable(types);
37
+ }
38
+ }
@@ -0,0 +1,421 @@
1
+ /**
2
+ * Protobuf binary format wire types.
3
+ *
4
+ * A wire type provides just enough information to find the length of the
5
+ * following value.
6
+ *
7
+ * See https://developers.google.com/protocol-buffers/docs/encoding#structure
8
+ */
9
+ export declare enum WireType {
10
+ /**
11
+ * Used for int32, int64, uint32, uint64, sint32, sint64, bool, enum
12
+ */
13
+ Varint = 0,
14
+ /**
15
+ * Used for fixed64, sfixed64, double.
16
+ * Always 8 bytes with little-endian byte order.
17
+ */
18
+ Bit64 = 1,
19
+ /**
20
+ * Used for string, bytes, embedded messages, packed repeated fields
21
+ *
22
+ * Only repeated numeric types (types which use the varint, 32-bit,
23
+ * or 64-bit wire types) can be packed. In proto3, such fields are
24
+ * packed by default.
25
+ */
26
+ LengthDelimited = 2,
27
+ /**
28
+ * Used for groups
29
+ * @deprecated
30
+ */
31
+ StartGroup = 3,
32
+ /**
33
+ * Used for groups
34
+ * @deprecated
35
+ */
36
+ EndGroup = 4,
37
+ /**
38
+ * Used for fixed32, sfixed32, float.
39
+ * Always 4 bytes with little-endian byte order.
40
+ */
41
+ Bit32 = 5,
42
+ }
43
+ declare type TextEncoderLike = {
44
+ encode(input?: string): Uint8Array;
45
+ };
46
+ declare type TextDecoderLike = {
47
+ decode(input?: Uint8Array): string;
48
+ };
49
+ export interface IBinaryReader {
50
+ /**
51
+ * Current position.
52
+ */
53
+ readonly pos: number;
54
+ /**
55
+ * Number of bytes available in this reader.
56
+ */
57
+ readonly len: number;
58
+ /**
59
+ * Reads a tag - field number and wire type.
60
+ */
61
+ tag(): [number, WireType];
62
+ /**
63
+ * Skip one element on the wire and return the skipped data.
64
+ */
65
+ skip(wireType: WireType): Uint8Array;
66
+ /**
67
+ * Read a `int32` field, a signed 32 bit varint.
68
+ */
69
+ uint32(): number;
70
+ /**
71
+ * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
72
+ */
73
+ int32(): number;
74
+ /**
75
+ * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
76
+ */
77
+ sint32(): number;
78
+ /**
79
+ * Read a `int64` field, a signed 64-bit varint.
80
+ */
81
+ int64(): bigint | string;
82
+ /**
83
+ * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
84
+ */
85
+ sint64(): bigint | string;
86
+ /**
87
+ * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
88
+ */
89
+ sfixed64(): bigint | string;
90
+ /**
91
+ * Read a `uint64` field, an unsigned 64-bit varint.
92
+ */
93
+ uint64(): bigint | string;
94
+ /**
95
+ * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
96
+ */
97
+ fixed64(): bigint | string;
98
+ /**
99
+ * Read a `bool` field, a variant.
100
+ */
101
+ bool(): boolean;
102
+ /**
103
+ * Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
104
+ */
105
+ fixed32(): number;
106
+ /**
107
+ * Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
108
+ */
109
+ sfixed32(): number;
110
+ /**
111
+ * Read a `float` field, 32-bit floating point number.
112
+ */
113
+ float(): number;
114
+ /**
115
+ * Read a `double` field, a 64-bit floating point number.
116
+ */
117
+ double(): number;
118
+ /**
119
+ * Read a `bytes` field, length-delimited arbitrary data.
120
+ */
121
+ bytes(): Uint8Array;
122
+ /**
123
+ * Read a `string` field, length-delimited data converted to UTF-8 text.
124
+ */
125
+ string(): string;
126
+ }
127
+ export interface IBinaryWriter {
128
+ /**
129
+ * Return all bytes written and reset this writer.
130
+ */
131
+ finish(): Uint8Array;
132
+ /**
133
+ * Start a new fork for length-delimited data like a message
134
+ * or a packed repeated field.
135
+ *
136
+ * Must be joined later with `join()`.
137
+ */
138
+ fork(): IBinaryWriter;
139
+ /**
140
+ * Join the last fork. Write its length and bytes, then
141
+ * return to the previous state.
142
+ */
143
+ join(): IBinaryWriter;
144
+ /**
145
+ * Writes a tag (field number and wire type).
146
+ *
147
+ * Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`
148
+ *
149
+ * Generated code should compute the tag ahead of time and call `uint32()`.
150
+ */
151
+ tag(fieldNo: number, type: WireType): IBinaryWriter;
152
+ /**
153
+ * Write a chunk of raw bytes.
154
+ */
155
+ raw(chunk: Uint8Array): IBinaryWriter;
156
+ /**
157
+ * Write a `uint32` value, an unsigned 32 bit varint.
158
+ */
159
+ uint32(value: number): IBinaryWriter;
160
+ /**
161
+ * Write a `int32` value, a signed 32 bit varint.
162
+ */
163
+ int32(value: number): IBinaryWriter;
164
+ /**
165
+ * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
166
+ */
167
+ sint32(value: number): IBinaryWriter;
168
+ /**
169
+ * Write a `int64` value, a signed 64-bit varint.
170
+ */
171
+ int64(value: string | number | bigint): IBinaryWriter;
172
+ /**
173
+ * Write a `uint64` value, an unsigned 64-bit varint.
174
+ */
175
+ uint64(value: string | number | bigint): IBinaryWriter;
176
+ /**
177
+ * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
178
+ */
179
+ sint64(value: string | number | bigint): IBinaryWriter;
180
+ /**
181
+ * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
182
+ */
183
+ fixed64(value: string | number | bigint): IBinaryWriter;
184
+ /**
185
+ * Write a `fixed64` value, a signed, fixed-length 64-bit integer.
186
+ */
187
+ sfixed64(value: string | number | bigint): IBinaryWriter;
188
+ /**
189
+ * Write a `bool` value, a variant.
190
+ */
191
+ bool(value: boolean): IBinaryWriter;
192
+ /**
193
+ * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
194
+ */
195
+ fixed32(value: number): IBinaryWriter;
196
+ /**
197
+ * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
198
+ */
199
+ sfixed32(value: number): IBinaryWriter;
200
+ /**
201
+ * Write a `float` value, 32-bit floating point number.
202
+ */
203
+ float(value: number): IBinaryWriter;
204
+ /**
205
+ * Write a `double` value, a 64-bit floating point number.
206
+ */
207
+ double(value: number): IBinaryWriter;
208
+ /**
209
+ * Write a `bytes` value, length-delimited arbitrary data.
210
+ */
211
+ bytes(value: Uint8Array): IBinaryWriter;
212
+ /**
213
+ * Write a `string` value, length-delimited data converted to UTF-8 text.
214
+ */
215
+ string(value: string): IBinaryWriter;
216
+ }
217
+ export declare class BinaryWriter implements IBinaryWriter {
218
+ /**
219
+ * We cannot allocate a buffer for the entire output
220
+ * because we don't know it's size.
221
+ *
222
+ * So we collect smaller chunks of known size and
223
+ * concat them later.
224
+ *
225
+ * Use `raw()` to push data to this array. It will flush
226
+ * `buf` first.
227
+ */
228
+ private chunks;
229
+ /**
230
+ * A growing buffer for byte values. If you don't know
231
+ * the size of the data you are writing, push to this
232
+ * array.
233
+ */
234
+ protected buf: number[];
235
+ /**
236
+ * Previous fork states.
237
+ */
238
+ private stack;
239
+ /**
240
+ * Text encoder instance to convert UTF-8 to bytes.
241
+ */
242
+ private readonly textEncoder;
243
+ constructor(textEncoder?: TextEncoderLike);
244
+ /**
245
+ * Return all bytes written and reset this writer.
246
+ */
247
+ finish(): Uint8Array;
248
+ /**
249
+ * Start a new fork for length-delimited data like a message
250
+ * or a packed repeated field.
251
+ *
252
+ * Must be joined later with `join()`.
253
+ */
254
+ fork(): IBinaryWriter;
255
+ /**
256
+ * Join the last fork. Write its length and bytes, then
257
+ * return to the previous state.
258
+ */
259
+ join(): IBinaryWriter;
260
+ /**
261
+ * Writes a tag (field number and wire type).
262
+ *
263
+ * Equivalent to `uint32( (fieldNo << 3 | type) >>> 0 )`.
264
+ *
265
+ * Generated code should compute the tag ahead of time and call `uint32()`.
266
+ */
267
+ tag(fieldNo: number, type: WireType): IBinaryWriter;
268
+ /**
269
+ * Write a chunk of raw bytes.
270
+ */
271
+ raw(chunk: Uint8Array): IBinaryWriter;
272
+ /**
273
+ * Write a `uint32` value, an unsigned 32 bit varint.
274
+ */
275
+ uint32(value: number): IBinaryWriter;
276
+ /**
277
+ * Write a `int32` value, a signed 32 bit varint.
278
+ */
279
+ int32(value: number): IBinaryWriter;
280
+ /**
281
+ * Write a `bool` value, a variant.
282
+ */
283
+ bool(value: boolean): IBinaryWriter;
284
+ /**
285
+ * Write a `bytes` value, length-delimited arbitrary data.
286
+ */
287
+ bytes(value: Uint8Array): IBinaryWriter;
288
+ /**
289
+ * Write a `string` value, length-delimited data converted to UTF-8 text.
290
+ */
291
+ string(value: string): IBinaryWriter;
292
+ /**
293
+ * Write a `float` value, 32-bit floating point number.
294
+ */
295
+ float(value: number): IBinaryWriter;
296
+ /**
297
+ * Write a `double` value, a 64-bit floating point number.
298
+ */
299
+ double(value: number): IBinaryWriter;
300
+ /**
301
+ * Write a `fixed32` value, an unsigned, fixed-length 32-bit integer.
302
+ */
303
+ fixed32(value: number): IBinaryWriter;
304
+ /**
305
+ * Write a `sfixed32` value, a signed, fixed-length 32-bit integer.
306
+ */
307
+ sfixed32(value: number): IBinaryWriter;
308
+ /**
309
+ * Write a `sint32` value, a signed, zigzag-encoded 32-bit varint.
310
+ */
311
+ sint32(value: number): IBinaryWriter;
312
+ /**
313
+ * Write a `fixed64` value, a signed, fixed-length 64-bit integer.
314
+ */
315
+ sfixed64(value: string | number | bigint): IBinaryWriter;
316
+ /**
317
+ * Write a `fixed64` value, an unsigned, fixed-length 64 bit integer.
318
+ */
319
+ fixed64(value: string | number | bigint): IBinaryWriter;
320
+ /**
321
+ * Write a `int64` value, a signed 64-bit varint.
322
+ */
323
+ int64(value: string | number | bigint): IBinaryWriter;
324
+ /**
325
+ * Write a `sint64` value, a signed, zig-zag-encoded 64-bit varint.
326
+ */
327
+ sint64(value: string | number | bigint): IBinaryWriter;
328
+ /**
329
+ * Write a `uint64` value, an unsigned 64-bit varint.
330
+ */
331
+ uint64(value: string | number | bigint): IBinaryWriter;
332
+ }
333
+ export declare class BinaryReader implements IBinaryReader {
334
+ /**
335
+ * Current position.
336
+ */
337
+ pos: number;
338
+ /**
339
+ * Number of bytes available in this reader.
340
+ */
341
+ readonly len: number;
342
+ private readonly buf;
343
+ private readonly view;
344
+ private readonly textDecoder;
345
+ constructor(buf: Uint8Array, textDecoder?: TextDecoderLike);
346
+ /**
347
+ * Reads a tag - field number and wire type.
348
+ */
349
+ tag(): [number, WireType];
350
+ /**
351
+ * Skip one element on the wire and return the skipped data.
352
+ * Supports WireType.StartGroup since v2.0.0-alpha.23.
353
+ */
354
+ skip(wireType: WireType): Uint8Array;
355
+ protected varint64: () => [number, number];
356
+ /**
357
+ * Throws error if position in byte array is out of range.
358
+ */
359
+ protected assertBounds(): void;
360
+ /**
361
+ * Read a `uint32` field, an unsigned 32 bit varint.
362
+ */
363
+ uint32: () => number;
364
+ /**
365
+ * Read a `int32` field, a signed 32 bit varint.
366
+ */
367
+ int32(): number;
368
+ /**
369
+ * Read a `sint32` field, a signed, zigzag-encoded 32-bit varint.
370
+ */
371
+ sint32(): number;
372
+ /**
373
+ * Read a `int64` field, a signed 64-bit varint.
374
+ */
375
+ int64(): bigint | string;
376
+ /**
377
+ * Read a `uint64` field, an unsigned 64-bit varint.
378
+ */
379
+ uint64(): bigint | string;
380
+ /**
381
+ * Read a `sint64` field, a signed, zig-zag-encoded 64-bit varint.
382
+ */
383
+ sint64(): bigint | string;
384
+ /**
385
+ * Read a `bool` field, a variant.
386
+ */
387
+ bool(): boolean;
388
+ /**
389
+ * Read a `fixed32` field, an unsigned, fixed-length 32-bit integer.
390
+ */
391
+ fixed32(): number;
392
+ /**
393
+ * Read a `sfixed32` field, a signed, fixed-length 32-bit integer.
394
+ */
395
+ sfixed32(): number;
396
+ /**
397
+ * Read a `fixed64` field, an unsigned, fixed-length 64 bit integer.
398
+ */
399
+ fixed64(): bigint | string;
400
+ /**
401
+ * Read a `fixed64` field, a signed, fixed-length 64-bit integer.
402
+ */
403
+ sfixed64(): bigint | string;
404
+ /**
405
+ * Read a `float` field, 32-bit floating point number.
406
+ */
407
+ float(): number;
408
+ /**
409
+ * Read a `double` field, a 64-bit floating point number.
410
+ */
411
+ double(): number;
412
+ /**
413
+ * Read a `bytes` field, length-delimited arbitrary data.
414
+ */
415
+ bytes(): Uint8Array;
416
+ /**
417
+ * Read a `string` field, length-delimited data converted to UTF-8 text.
418
+ */
419
+ string(): string;
420
+ }
421
+ export {};
@@ -0,0 +1,109 @@
1
+ import type { Message } from "./message.js";
2
+ import type {
3
+ IBinaryReader,
4
+ IBinaryWriter,
5
+ WireType,
6
+ } from "./binary-encoding.js";
7
+ /**
8
+ * BinaryFormat is the contract for serializing messages to and from binary
9
+ * data. Implementations may be specific to a proto syntax, and can be
10
+ * reflection based, or delegate to speed optimized generated code.
11
+ */
12
+ export interface BinaryFormat {
13
+ /**
14
+ * Provide options for parsing binary data.
15
+ */
16
+ makeReadOptions(
17
+ options?: Partial<BinaryReadOptions>
18
+ ): Readonly<BinaryReadOptions>;
19
+ /**
20
+ * Provide options for serializing binary data.
21
+ */
22
+ makeWriteOptions(
23
+ options?: Partial<BinaryWriteOptions>
24
+ ): Readonly<BinaryWriteOptions>;
25
+ /**
26
+ * Parse a message from binary data, merging fields.
27
+ */
28
+ readMessage<T extends Message>(
29
+ message: T,
30
+ reader: IBinaryReader,
31
+ length: number,
32
+ options: BinaryReadOptions
33
+ ): void;
34
+ /**
35
+ * Serialize a message to binary data.
36
+ */
37
+ writeMessage(
38
+ message: Message,
39
+ writer: IBinaryWriter,
40
+ options: BinaryWriteOptions
41
+ ): void;
42
+ /**
43
+ * Retrieve the unknown fields for the given message.
44
+ *
45
+ * Unknown fields are well-formed protocol buffer serialized data for
46
+ * fields that the parserdoes not recognize.
47
+ *
48
+ * For more details see https://developers.google.com/protocol-buffers/docs/proto3#unknowns
49
+ */
50
+ listUnknownFields(message: Message): ReadonlyArray<{
51
+ no: number;
52
+ wireType: WireType;
53
+ data: Uint8Array;
54
+ }>;
55
+ /**
56
+ * Discard unknown fields for the given message.
57
+ */
58
+ discardUnknownFields(message: Message): void;
59
+ /**
60
+ * Retrieve the unknown fields for the given message and write them to
61
+ * the given writer. This method is called when a message is serialized,
62
+ * so the fields that are unknown to the parser persist through a round
63
+ * trip.
64
+ */
65
+ writeUnknownFields(message: Message, writer: IBinaryWriter): void;
66
+ /**
67
+ * Store an unknown field for the given message. The parser will use this
68
+ * method if it does not recognize a field, unless the option
69
+ * `readUnknownFields` has been disabled.
70
+ */
71
+ onUnknownField(
72
+ message: Message,
73
+ no: number,
74
+ wireType: WireType,
75
+ data: Uint8Array
76
+ ): void;
77
+ }
78
+ /**
79
+ * Options for parsing binary data.
80
+ */
81
+ export interface BinaryReadOptions {
82
+ /**
83
+ * Retain unknown fields during parsing? The default behavior is to retain
84
+ * unknown fields and include them in the serialized output.
85
+ *
86
+ * For more details see https://developers.google.com/protocol-buffers/docs/proto3#unknowns
87
+ */
88
+ readUnknownFields: boolean;
89
+ /**
90
+ * Allows to use a custom implementation to decode binary data.
91
+ */
92
+ readerFactory: (bytes: Uint8Array) => IBinaryReader;
93
+ }
94
+ /**
95
+ * Options for serializing to binary data.
96
+ */
97
+ export interface BinaryWriteOptions {
98
+ /**
99
+ * Include unknown fields in the serialized output? The default behavior
100
+ * is to retain unknown fields and include them in the serialized output.
101
+ *
102
+ * For more details see https://developers.google.com/protocol-buffers/docs/proto3#unknowns
103
+ */
104
+ writeUnknownFields: boolean;
105
+ /**
106
+ * Allows to use a custom implementation to encode binary data.
107
+ */
108
+ writerFactory: () => IBinaryWriter;
109
+ }