@bufbuild/protobuf 1.4.2 → 1.5.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 (47) hide show
  1. package/dist/cjs/binary-encoding.d.ts +3 -4
  2. package/dist/cjs/binary-encoding.js +3 -4
  3. package/dist/cjs/binary-format.d.ts +10 -1
  4. package/dist/cjs/create-descriptor-set.d.ts +19 -5
  5. package/dist/cjs/create-descriptor-set.js +157 -223
  6. package/dist/cjs/create-registry-from-desc.js +1 -0
  7. package/dist/cjs/descriptor-set.d.ts +47 -5
  8. package/dist/cjs/field.d.ts +36 -2
  9. package/dist/cjs/google/protobuf/any_pb.js +1 -1
  10. package/dist/cjs/google/protobuf/descriptor_pb.js +1 -1
  11. package/dist/cjs/google/protobuf/struct_pb.js +1 -1
  12. package/dist/cjs/google/protobuf/type_pb.js +1 -1
  13. package/dist/cjs/private/binary-format-common.d.ts +3 -2
  14. package/dist/cjs/private/binary-format-common.js +36 -15
  15. package/dist/cjs/private/binary-format-proto2.js +2 -2
  16. package/dist/cjs/private/binary-format-proto3.js +2 -2
  17. package/dist/cjs/private/feature-set.d.ts +21 -0
  18. package/dist/cjs/private/feature-set.js +107 -0
  19. package/dist/cjs/private/text-format.d.ts +4 -0
  20. package/dist/cjs/private/text-format.js +189 -0
  21. package/dist/cjs/proto-delimited.js +3 -2
  22. package/dist/cjs/proto2.js +7 -3
  23. package/dist/cjs/proto3.js +14 -10
  24. package/dist/esm/binary-encoding.d.ts +3 -4
  25. package/dist/esm/binary-encoding.js +3 -4
  26. package/dist/esm/binary-format.d.ts +10 -1
  27. package/dist/esm/create-descriptor-set.d.ts +19 -5
  28. package/dist/esm/create-descriptor-set.js +157 -222
  29. package/dist/esm/create-registry-from-desc.js +2 -1
  30. package/dist/esm/descriptor-set.d.ts +47 -5
  31. package/dist/esm/field.d.ts +36 -2
  32. package/dist/esm/google/protobuf/any_pb.js +1 -1
  33. package/dist/esm/google/protobuf/descriptor_pb.js +1 -1
  34. package/dist/esm/google/protobuf/struct_pb.js +1 -1
  35. package/dist/esm/google/protobuf/type_pb.js +1 -1
  36. package/dist/esm/private/binary-format-common.d.ts +3 -2
  37. package/dist/esm/private/binary-format-common.js +36 -15
  38. package/dist/esm/private/binary-format-proto2.js +2 -2
  39. package/dist/esm/private/binary-format-proto3.js +2 -2
  40. package/dist/esm/private/feature-set.d.ts +21 -0
  41. package/dist/esm/private/feature-set.js +103 -0
  42. package/dist/esm/private/text-format.d.ts +4 -0
  43. package/dist/esm/private/text-format.js +184 -0
  44. package/dist/esm/proto-delimited.js +3 -2
  45. package/dist/esm/proto2.js +7 -3
  46. package/dist/esm/proto3.js +14 -10
  47. package/package.json +11 -7
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ // Copyright 2021-2023 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.parseTextFormatScalarValue = exports.parseTextFormatEnumValue = void 0;
17
+ const assert_js_1 = require("./assert.js");
18
+ const field_js_1 = require("../field.js");
19
+ const proto_int64_js_1 = require("../proto-int64.js");
20
+ function parseTextFormatEnumValue(descEnum, value) {
21
+ const enumValue = descEnum.values.find((v) => v.name === value);
22
+ (0, assert_js_1.assert)(enumValue, `cannot parse ${descEnum.name} default value: ${value}`);
23
+ return enumValue.number;
24
+ }
25
+ exports.parseTextFormatEnumValue = parseTextFormatEnumValue;
26
+ function parseTextFormatScalarValue(type, value) {
27
+ switch (type) {
28
+ case field_js_1.ScalarType.STRING:
29
+ return value;
30
+ case field_js_1.ScalarType.BYTES: {
31
+ const u = unescapeBytesDefaultValue(value);
32
+ if (u === false) {
33
+ throw new Error(`cannot parse ${field_js_1.ScalarType[type]} default value: ${value}`);
34
+ }
35
+ return u;
36
+ }
37
+ case field_js_1.ScalarType.INT64:
38
+ case field_js_1.ScalarType.SFIXED64:
39
+ case field_js_1.ScalarType.SINT64:
40
+ return proto_int64_js_1.protoInt64.parse(value);
41
+ case field_js_1.ScalarType.UINT64:
42
+ case field_js_1.ScalarType.FIXED64:
43
+ return proto_int64_js_1.protoInt64.uParse(value);
44
+ case field_js_1.ScalarType.DOUBLE:
45
+ case field_js_1.ScalarType.FLOAT:
46
+ switch (value) {
47
+ case "inf":
48
+ return Number.POSITIVE_INFINITY;
49
+ case "-inf":
50
+ return Number.NEGATIVE_INFINITY;
51
+ case "nan":
52
+ return Number.NaN;
53
+ default:
54
+ return parseFloat(value);
55
+ }
56
+ case field_js_1.ScalarType.BOOL:
57
+ return value === "true";
58
+ case field_js_1.ScalarType.INT32:
59
+ case field_js_1.ScalarType.UINT32:
60
+ case field_js_1.ScalarType.SINT32:
61
+ case field_js_1.ScalarType.FIXED32:
62
+ case field_js_1.ScalarType.SFIXED32:
63
+ return parseInt(value, 10);
64
+ }
65
+ }
66
+ exports.parseTextFormatScalarValue = parseTextFormatScalarValue;
67
+ /**
68
+ * Parses a text-encoded default value (proto2) of a BYTES field.
69
+ */
70
+ function unescapeBytesDefaultValue(str) {
71
+ const b = [];
72
+ const input = {
73
+ tail: str,
74
+ c: "",
75
+ next() {
76
+ if (this.tail.length == 0) {
77
+ return false;
78
+ }
79
+ this.c = this.tail[0];
80
+ this.tail = this.tail.substring(1);
81
+ return true;
82
+ },
83
+ take(n) {
84
+ if (this.tail.length >= n) {
85
+ const r = this.tail.substring(0, n);
86
+ this.tail = this.tail.substring(n);
87
+ return r;
88
+ }
89
+ return false;
90
+ },
91
+ };
92
+ while (input.next()) {
93
+ switch (input.c) {
94
+ case "\\":
95
+ if (input.next()) {
96
+ switch (input.c) {
97
+ case "\\":
98
+ b.push(input.c.charCodeAt(0));
99
+ break;
100
+ case "b":
101
+ b.push(0x08);
102
+ break;
103
+ case "f":
104
+ b.push(0x0c);
105
+ break;
106
+ case "n":
107
+ b.push(0x0a);
108
+ break;
109
+ case "r":
110
+ b.push(0x0d);
111
+ break;
112
+ case "t":
113
+ b.push(0x09);
114
+ break;
115
+ case "v":
116
+ b.push(0x0b);
117
+ break;
118
+ case "0":
119
+ case "1":
120
+ case "2":
121
+ case "3":
122
+ case "4":
123
+ case "5":
124
+ case "6":
125
+ case "7": {
126
+ const s = input.c;
127
+ const t = input.take(2);
128
+ if (t === false) {
129
+ return false;
130
+ }
131
+ const n = parseInt(s + t, 8);
132
+ if (isNaN(n)) {
133
+ return false;
134
+ }
135
+ b.push(n);
136
+ break;
137
+ }
138
+ case "x": {
139
+ const s = input.c;
140
+ const t = input.take(2);
141
+ if (t === false) {
142
+ return false;
143
+ }
144
+ const n = parseInt(s + t, 16);
145
+ if (isNaN(n)) {
146
+ return false;
147
+ }
148
+ b.push(n);
149
+ break;
150
+ }
151
+ case "u": {
152
+ const s = input.c;
153
+ const t = input.take(4);
154
+ if (t === false) {
155
+ return false;
156
+ }
157
+ const n = parseInt(s + t, 16);
158
+ if (isNaN(n)) {
159
+ return false;
160
+ }
161
+ const chunk = new Uint8Array(4);
162
+ const view = new DataView(chunk.buffer);
163
+ view.setInt32(0, n, true);
164
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
165
+ break;
166
+ }
167
+ case "U": {
168
+ const s = input.c;
169
+ const t = input.take(8);
170
+ if (t === false) {
171
+ return false;
172
+ }
173
+ const tc = proto_int64_js_1.protoInt64.uEnc(s + t);
174
+ const chunk = new Uint8Array(8);
175
+ const view = new DataView(chunk.buffer);
176
+ view.setInt32(0, tc.lo, true);
177
+ view.setInt32(4, tc.hi, true);
178
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
179
+ break;
180
+ }
181
+ }
182
+ }
183
+ break;
184
+ default:
185
+ b.push(input.c.charCodeAt(0));
186
+ }
187
+ }
188
+ return new Uint8Array(b);
189
+ }
@@ -23,8 +23,9 @@ var __await = (this && this.__await) || function (v) { return this instanceof __
23
23
  var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
24
24
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
25
25
  var g = generator.apply(thisArg, _arguments || []), i, q = [];
26
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
27
- function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
26
+ return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
27
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
28
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
28
29
  function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
29
30
  function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
30
31
  function fulfill(value) { resume("next", value); }
@@ -57,7 +57,7 @@ exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_for
57
57
  } }));
58
58
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
59
59
  function normalizeFieldInfosProto2(fieldInfos) {
60
- var _a, _b, _c, _d;
60
+ var _a, _b, _c, _d, _e;
61
61
  const r = [];
62
62
  let o;
63
63
  for (const field of typeof fieldInfos == "function"
@@ -70,8 +70,6 @@ function normalizeFieldInfosProto2(fieldInfos) {
70
70
  if (field.kind == "scalar") {
71
71
  f.L = (_c = field.L) !== null && _c !== void 0 ? _c : field_js_2.LongType.BIGINT;
72
72
  }
73
- // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
74
- f.packed = (_d = field.packed) !== null && _d !== void 0 ? _d : false;
75
73
  // We do not surface options at this time
76
74
  // f.options = field.options ?? emptyReadonlyObject;
77
75
  if (field.oneof !== undefined) {
@@ -82,6 +80,12 @@ function normalizeFieldInfosProto2(fieldInfos) {
82
80
  f.oneof = o;
83
81
  o.addField(f);
84
82
  }
83
+ // proto2 specific:
84
+ if (field.kind == "message") {
85
+ f.delimited = (_d = field.delimited) !== null && _d !== void 0 ? _d : false;
86
+ }
87
+ // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
88
+ f.packed = (_e = field.packed) !== null && _e !== void 0 ? _e : false;
85
89
  r.push(f);
86
90
  }
87
91
  return r;
@@ -73,16 +73,6 @@ function normalizeFieldInfosProto3(fieldInfos) {
73
73
  if (field.kind == "scalar") {
74
74
  f.L = (_c = field.L) !== null && _c !== void 0 ? _c : field_js_1.LongType.BIGINT;
75
75
  }
76
- // From the proto3 language guide:
77
- // > In proto3, repeated fields of scalar numeric types are packed by default.
78
- // This information is incomplete - according to the conformance tests, BOOL
79
- // and ENUM are packed by default as well. This means only STRING and BYTES
80
- // are not packed by default, which makes sense because they are length-delimited.
81
- f.packed =
82
- (_d = field.packed) !== null && _d !== void 0 ? _d : (field.kind == "enum" ||
83
- (field.kind == "scalar" &&
84
- field.T != field_js_1.ScalarType.BYTES &&
85
- field.T != field_js_1.ScalarType.STRING));
86
76
  // We do not surface options at this time
87
77
  // f.options = field.options ?? emptyReadonlyObject;
88
78
  if (field.oneof !== undefined) {
@@ -93,6 +83,20 @@ function normalizeFieldInfosProto3(fieldInfos) {
93
83
  f.oneof = o;
94
84
  o.addField(f);
95
85
  }
86
+ // proto3 specific:
87
+ if (field.kind == "message") {
88
+ f.delimited = false;
89
+ }
90
+ // From the proto3 language guide:
91
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
92
+ // This information is incomplete - according to the conformance tests, BOOL
93
+ // and ENUM are packed by default as well. This means only STRING and BYTES
94
+ // are not packed by default, which makes sense because they are length-delimited.
95
+ f.packed =
96
+ (_d = field.packed) !== null && _d !== void 0 ? _d : (field.kind == "enum" ||
97
+ (field.kind == "scalar" &&
98
+ field.T != field_js_1.ScalarType.BYTES &&
99
+ field.T != field_js_1.ScalarType.STRING));
96
100
  r.push(f);
97
101
  }
98
102
  return r;
@@ -25,13 +25,12 @@ export declare enum WireType {
25
25
  */
26
26
  LengthDelimited = 2,
27
27
  /**
28
- * Used for groups
29
- * @deprecated
28
+ * Start of a tag-delimited aggregate, such as a proto2 group, or a message
29
+ * in editions with message_encoding = DELIMITED.
30
30
  */
31
31
  StartGroup = 3,
32
32
  /**
33
- * Used for groups
34
- * @deprecated
33
+ * End of a tag-delimited aggregate.
35
34
  */
36
35
  EndGroup = 4,
37
36
  /**
@@ -43,13 +43,12 @@ export var WireType;
43
43
  */
44
44
  WireType[WireType["LengthDelimited"] = 2] = "LengthDelimited";
45
45
  /**
46
- * Used for groups
47
- * @deprecated
46
+ * Start of a tag-delimited aggregate, such as a proto2 group, or a message
47
+ * in editions with message_encoding = DELIMITED.
48
48
  */
49
49
  WireType[WireType["StartGroup"] = 3] = "StartGroup";
50
50
  /**
51
- * Used for groups
52
- * @deprecated
51
+ * End of a tag-delimited aggregate.
53
52
  */
54
53
  WireType[WireType["EndGroup"] = 4] = "EndGroup";
55
54
  /**
@@ -16,8 +16,17 @@ export interface BinaryFormat {
16
16
  makeWriteOptions(options?: Partial<BinaryWriteOptions>): Readonly<BinaryWriteOptions>;
17
17
  /**
18
18
  * Parse a message from binary data, merging fields.
19
+ *
20
+ * Supports two message encodings:
21
+ * - length-prefixed: delimitedMessageEncoding is false or omitted, and
22
+ * lengthOrEndTagFieldNo is the expected length of the message in the reader.
23
+ * - delimited: delimitedMessageEncoding is true, and lengthOrEndTagFieldNo is
24
+ * the field number in a tag with wire type end-group signalling the end of
25
+ * the message in the reader.
26
+ *
27
+ * delimitedMessageEncoding is optional for backwards compatibility.
19
28
  */
20
- readMessage(message: Message, reader: IBinaryReader, length: number, options: BinaryReadOptions): void;
29
+ readMessage(message: Message, reader: IBinaryReader, lengthOrEndTagFieldNo: number, options: BinaryReadOptions, delimitedMessageEncoding?: boolean): void;
21
30
  /**
22
31
  * Serialize a message to binary data.
23
32
  */
@@ -1,5 +1,4 @@
1
- import type { FieldDescriptorProto } from "./google/protobuf/descriptor_pb.js";
2
- import { FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
1
+ import { FeatureSetDefaults, FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
3
2
  import type { DescriptorSet } from "./descriptor-set.js";
4
3
  /**
5
4
  * Create a DescriptorSet, a convenient interface for working with a set of
@@ -9,8 +8,23 @@ import type { DescriptorSet } from "./descriptor-set.js";
9
8
  * before any file that imports it. Protocol buffer compilers always produce
10
9
  * files in topological order.
11
10
  */
12
- export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array): DescriptorSet;
11
+ export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array, options?: CreateDescriptorSetOptions): DescriptorSet;
13
12
  /**
14
- * Get the default `packed` state of a repeated field.
13
+ * Options to createDescriptorSet()
15
14
  */
16
- export declare function isPackedFieldByDefault(proto: FieldDescriptorProto, syntax: "proto2" | "proto3"): boolean;
15
+ interface CreateDescriptorSetOptions {
16
+ /**
17
+ * Editions support language-specific features with extensions to
18
+ * google.protobuf.FeatureSet. They can define defaults, and specify on
19
+ * which targets the features can be set.
20
+ *
21
+ * To create a DescriptorSet that provides your language-specific features,
22
+ * you have to provide a google.protobuf.FeatureSetDefaults message in this
23
+ * option.
24
+ *
25
+ * The defaults can be generated with `protoc` - see the flag
26
+ * `--experimental_edition_defaults_out`.
27
+ */
28
+ featureSetDefaults?: FeatureSetDefaults;
29
+ }
30
+ export {};