@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,184 @@
1
+ // Copyright 2021-2023 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { assert } from "./assert.js";
15
+ import { ScalarType } from "../field.js";
16
+ import { protoInt64 } from "../proto-int64.js";
17
+ export function parseTextFormatEnumValue(descEnum, value) {
18
+ const enumValue = descEnum.values.find((v) => v.name === value);
19
+ assert(enumValue, `cannot parse ${descEnum.name} default value: ${value}`);
20
+ return enumValue.number;
21
+ }
22
+ export function parseTextFormatScalarValue(type, value) {
23
+ switch (type) {
24
+ case ScalarType.STRING:
25
+ return value;
26
+ case ScalarType.BYTES: {
27
+ const u = unescapeBytesDefaultValue(value);
28
+ if (u === false) {
29
+ throw new Error(`cannot parse ${ScalarType[type]} default value: ${value}`);
30
+ }
31
+ return u;
32
+ }
33
+ case ScalarType.INT64:
34
+ case ScalarType.SFIXED64:
35
+ case ScalarType.SINT64:
36
+ return protoInt64.parse(value);
37
+ case ScalarType.UINT64:
38
+ case ScalarType.FIXED64:
39
+ return protoInt64.uParse(value);
40
+ case ScalarType.DOUBLE:
41
+ case ScalarType.FLOAT:
42
+ switch (value) {
43
+ case "inf":
44
+ return Number.POSITIVE_INFINITY;
45
+ case "-inf":
46
+ return Number.NEGATIVE_INFINITY;
47
+ case "nan":
48
+ return Number.NaN;
49
+ default:
50
+ return parseFloat(value);
51
+ }
52
+ case ScalarType.BOOL:
53
+ return value === "true";
54
+ case ScalarType.INT32:
55
+ case ScalarType.UINT32:
56
+ case ScalarType.SINT32:
57
+ case ScalarType.FIXED32:
58
+ case ScalarType.SFIXED32:
59
+ return parseInt(value, 10);
60
+ }
61
+ }
62
+ /**
63
+ * Parses a text-encoded default value (proto2) of a BYTES field.
64
+ */
65
+ function unescapeBytesDefaultValue(str) {
66
+ const b = [];
67
+ const input = {
68
+ tail: str,
69
+ c: "",
70
+ next() {
71
+ if (this.tail.length == 0) {
72
+ return false;
73
+ }
74
+ this.c = this.tail[0];
75
+ this.tail = this.tail.substring(1);
76
+ return true;
77
+ },
78
+ take(n) {
79
+ if (this.tail.length >= n) {
80
+ const r = this.tail.substring(0, n);
81
+ this.tail = this.tail.substring(n);
82
+ return r;
83
+ }
84
+ return false;
85
+ },
86
+ };
87
+ while (input.next()) {
88
+ switch (input.c) {
89
+ case "\\":
90
+ if (input.next()) {
91
+ switch (input.c) {
92
+ case "\\":
93
+ b.push(input.c.charCodeAt(0));
94
+ break;
95
+ case "b":
96
+ b.push(0x08);
97
+ break;
98
+ case "f":
99
+ b.push(0x0c);
100
+ break;
101
+ case "n":
102
+ b.push(0x0a);
103
+ break;
104
+ case "r":
105
+ b.push(0x0d);
106
+ break;
107
+ case "t":
108
+ b.push(0x09);
109
+ break;
110
+ case "v":
111
+ b.push(0x0b);
112
+ break;
113
+ case "0":
114
+ case "1":
115
+ case "2":
116
+ case "3":
117
+ case "4":
118
+ case "5":
119
+ case "6":
120
+ case "7": {
121
+ const s = input.c;
122
+ const t = input.take(2);
123
+ if (t === false) {
124
+ return false;
125
+ }
126
+ const n = parseInt(s + t, 8);
127
+ if (isNaN(n)) {
128
+ return false;
129
+ }
130
+ b.push(n);
131
+ break;
132
+ }
133
+ case "x": {
134
+ const s = input.c;
135
+ const t = input.take(2);
136
+ if (t === false) {
137
+ return false;
138
+ }
139
+ const n = parseInt(s + t, 16);
140
+ if (isNaN(n)) {
141
+ return false;
142
+ }
143
+ b.push(n);
144
+ break;
145
+ }
146
+ case "u": {
147
+ const s = input.c;
148
+ const t = input.take(4);
149
+ if (t === false) {
150
+ return false;
151
+ }
152
+ const n = parseInt(s + t, 16);
153
+ if (isNaN(n)) {
154
+ return false;
155
+ }
156
+ const chunk = new Uint8Array(4);
157
+ const view = new DataView(chunk.buffer);
158
+ view.setInt32(0, n, true);
159
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3]);
160
+ break;
161
+ }
162
+ case "U": {
163
+ const s = input.c;
164
+ const t = input.take(8);
165
+ if (t === false) {
166
+ return false;
167
+ }
168
+ const tc = protoInt64.uEnc(s + t);
169
+ const chunk = new Uint8Array(8);
170
+ const view = new DataView(chunk.buffer);
171
+ view.setInt32(0, tc.lo, true);
172
+ view.setInt32(4, tc.hi, true);
173
+ b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]);
174
+ break;
175
+ }
176
+ }
177
+ }
178
+ break;
179
+ default:
180
+ b.push(input.c.charCodeAt(0));
181
+ }
182
+ }
183
+ return new Uint8Array(b);
184
+ }
@@ -22,8 +22,9 @@ var __await = (this && this.__await) || function (v) { return this instanceof __
22
22
  var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
23
23
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
24
24
  var g = generator.apply(thisArg, _arguments || []), i, q = [];
25
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
26
- 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); }); }; }
25
+ return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
26
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
27
+ 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]); } }
27
28
  function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
28
29
  function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
29
30
  function fulfill(value) { resume("next", value); }
@@ -54,7 +54,7 @@ export const proto2 = makeProtoRuntime("proto2", makeJsonFormatProto2(), makeBin
54
54
  } }));
55
55
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
56
56
  function normalizeFieldInfosProto2(fieldInfos) {
57
- var _a, _b, _c, _d;
57
+ var _a, _b, _c, _d, _e;
58
58
  const r = [];
59
59
  let o;
60
60
  for (const field of typeof fieldInfos == "function"
@@ -67,8 +67,6 @@ function normalizeFieldInfosProto2(fieldInfos) {
67
67
  if (field.kind == "scalar") {
68
68
  f.L = (_c = field.L) !== null && _c !== void 0 ? _c : LongType.BIGINT;
69
69
  }
70
- // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
71
- f.packed = (_d = field.packed) !== null && _d !== void 0 ? _d : false;
72
70
  // We do not surface options at this time
73
71
  // f.options = field.options ?? emptyReadonlyObject;
74
72
  if (field.oneof !== undefined) {
@@ -79,6 +77,12 @@ function normalizeFieldInfosProto2(fieldInfos) {
79
77
  f.oneof = o;
80
78
  o.addField(f);
81
79
  }
80
+ // proto2 specific:
81
+ if (field.kind == "message") {
82
+ f.delimited = (_d = field.delimited) !== null && _d !== void 0 ? _d : false;
83
+ }
84
+ // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
85
+ f.packed = (_e = field.packed) !== null && _e !== void 0 ? _e : false;
82
86
  r.push(f);
83
87
  }
84
88
  return r;
@@ -70,16 +70,6 @@ function normalizeFieldInfosProto3(fieldInfos) {
70
70
  if (field.kind == "scalar") {
71
71
  f.L = (_c = field.L) !== null && _c !== void 0 ? _c : LongType.BIGINT;
72
72
  }
73
- // From the proto3 language guide:
74
- // > In proto3, repeated fields of scalar numeric types are packed by default.
75
- // This information is incomplete - according to the conformance tests, BOOL
76
- // and ENUM are packed by default as well. This means only STRING and BYTES
77
- // are not packed by default, which makes sense because they are length-delimited.
78
- f.packed =
79
- (_d = field.packed) !== null && _d !== void 0 ? _d : (field.kind == "enum" ||
80
- (field.kind == "scalar" &&
81
- field.T != ScalarType.BYTES &&
82
- field.T != ScalarType.STRING));
83
73
  // We do not surface options at this time
84
74
  // f.options = field.options ?? emptyReadonlyObject;
85
75
  if (field.oneof !== undefined) {
@@ -90,6 +80,20 @@ function normalizeFieldInfosProto3(fieldInfos) {
90
80
  f.oneof = o;
91
81
  o.addField(f);
92
82
  }
83
+ // proto3 specific:
84
+ if (field.kind == "message") {
85
+ f.delimited = false;
86
+ }
87
+ // From the proto3 language guide:
88
+ // > In proto3, repeated fields of scalar numeric types are packed by default.
89
+ // This information is incomplete - according to the conformance tests, BOOL
90
+ // and ENUM are packed by default as well. This means only STRING and BYTES
91
+ // are not packed by default, which makes sense because they are length-delimited.
92
+ f.packed =
93
+ (_d = field.packed) !== null && _d !== void 0 ? _d : (field.kind == "enum" ||
94
+ (field.kind == "scalar" &&
95
+ field.T != ScalarType.BYTES &&
96
+ field.T != ScalarType.STRING));
93
97
  r.push(f);
94
98
  }
95
99
  return r;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "1.4.2",
3
+ "version": "1.5.1",
4
4
  "license": "(Apache-2.0 AND BSD-3-Clause)",
5
5
  "description": "A complete implementation of Protocol Buffers in TypeScript, suitable for web browsers and Node.js.",
6
6
  "repository": {
@@ -14,10 +14,10 @@
14
14
  "build:cjs": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs --declaration --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
15
15
  "build:esm": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/esm",
16
16
  "build:proxy": "node ../../scripts/gen-esm-proxy.mjs .",
17
- "prebootstrap": "rm -rf .tmp && mkdir -p .tmp/google/protobuf && cp -rp src/google/protobuf/* .tmp/google/protobuf",
18
- "bootstrap": "protoc --es_out=src --es_opt=bootstrap_wkt=true,ts_nocheck=false,target=ts --proto_path $(upstream-include wkt) $(upstream-files wkt)",
19
- "postbootstrap": "license-header src/google/protobuf",
20
- "bootstrap-diff": "diff >/dev/null -r .tmp/google/protobuf src/google/protobuf && cp -rp .tmp/google/protobuf/* src/google/protobuf",
17
+ "bootstrap:featureset-defaults": "upstream-inject-feature-defaults src/private/feature-set.ts",
18
+ "prebootstrap:wkt": "rm -rf .tmp && mkdir -p .tmp/google/protobuf && cp -rp src/google/protobuf/* .tmp/google/protobuf",
19
+ "bootstrap:wkt": "protoc --es_out=src --es_opt=bootstrap_wkt=true,ts_nocheck=false,target=ts --proto_path $(upstream-include wkt) $(upstream-files wkt) && license-header src/google/protobuf",
20
+ "postbootstrap:wkt": "diff >/dev/null -r .tmp/google/protobuf src/google/protobuf && cp -rp .tmp/google/protobuf/* src/google/protobuf || true",
21
21
  "attw": "attw --pack"
22
22
  },
23
23
  "type": "module",
@@ -25,9 +25,13 @@
25
25
  "main": "./dist/cjs/index.js",
26
26
  "exports": {
27
27
  ".": {
28
+ "node": {
29
+ "import": "./dist/proxy/index.js",
30
+ "require": "./dist/cjs/index.js"
31
+ },
28
32
  "module": "./dist/esm/index.js",
29
- "require": "./dist/cjs/index.js",
30
- "import": "./dist/proxy/index.js"
33
+ "import": "./dist/esm/index.js",
34
+ "require": "./dist/cjs/index.js"
31
35
  }
32
36
  },
33
37
  "devDependencies": {