@bufbuild/protobuf 1.4.2 → 1.5.0

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.
@@ -18,7 +18,7 @@
18
18
  // The messages in this file describe the definitions found in .proto files.
19
19
  // A valid .proto file can be translated directly to a FileDescriptorProto
20
20
  // without any other information (e.g. without reading its imports).
21
- // @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
21
+ // @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
22
22
  // @generated from file google/protobuf/descriptor.proto (package google.protobuf, syntax proto2)
23
23
  /* eslint-disable */
24
24
  import { proto2 } from "../../proto2.js";
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- // @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -11,7 +11,7 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- // @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -0,0 +1,21 @@
1
+ import { Edition, FeatureSet, FeatureSetDefaults } from "../google/protobuf/descriptor_pb.js";
2
+ /**
3
+ * Static edition feature defaults supported by @bufbuild/protobuf.
4
+ */
5
+ export declare const featureSetDefaults: FeatureSetDefaults;
6
+ /**
7
+ * A merged google.protobuf.FeaturesSet, with all fields guaranteed to be set.
8
+ */
9
+ export type MergedFeatureSet = FeatureSet & Required<FeatureSet>;
10
+ /**
11
+ * A function that resolves features for the given edition.
12
+ *
13
+ * If no feature set is provided, the default feature set for the edition is
14
+ * returned. If features are provided, they are merged into the edition default
15
+ * features.
16
+ */
17
+ export type FeatureResolverFn = (edition: Edition, a?: FeatureSet, b?: FeatureSet) => MergedFeatureSet;
18
+ /**
19
+ * Create an edition feature resolver with the given feature set defaults.
20
+ */
21
+ export declare function createFeatureResolver(compiledFeatureSetDefaults: FeatureSetDefaults): FeatureResolverFn;
@@ -0,0 +1,103 @@
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 { Edition, FeatureSet, FeatureSetDefaults, } from "../google/protobuf/descriptor_pb.js";
15
+ import { protoBase64 } from "../proto-base64.js";
16
+ /**
17
+ * Static edition feature defaults supported by @bufbuild/protobuf.
18
+ */
19
+ export const featureSetDefaults = FeatureSetDefaults.fromBinary(protoBase64.dec(
20
+ /*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiABKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/));
21
+ /**
22
+ * Create an edition feature resolver with the given feature set defaults.
23
+ */
24
+ export function createFeatureResolver(compiledFeatureSetDefaults) {
25
+ const min = compiledFeatureSetDefaults.minimumEdition;
26
+ const max = compiledFeatureSetDefaults.maximumEdition;
27
+ if (min === undefined ||
28
+ max === undefined ||
29
+ compiledFeatureSetDefaults.defaults.some((d) => d.edition === undefined)) {
30
+ throw new Error("Invalid FeatureSetDefaults");
31
+ }
32
+ const defaultsBinByEdition = new Map();
33
+ return (edition, ...rest) => {
34
+ var _a, _b;
35
+ let defaultsBin = defaultsBinByEdition.get(edition);
36
+ if (defaultsBin === undefined) {
37
+ if (edition < min) {
38
+ throw new Error(`Edition ${Edition[edition]} is earlier than the minimum supported edition ${Edition[min]}`);
39
+ }
40
+ if (max < edition) {
41
+ throw new Error(`Edition ${Edition[edition]} is later than the maximum supported edition ${Edition[max]}`);
42
+ }
43
+ let highestMatch = undefined;
44
+ for (const c of compiledFeatureSetDefaults.defaults) {
45
+ const e = (_a = c.edition) !== null && _a !== void 0 ? _a : 0;
46
+ if (e > edition) {
47
+ continue;
48
+ }
49
+ if (highestMatch !== undefined && highestMatch.e > e) {
50
+ continue;
51
+ }
52
+ highestMatch = {
53
+ e,
54
+ f: (_b = c.features) !== null && _b !== void 0 ? _b : new FeatureSet(),
55
+ };
56
+ }
57
+ if (highestMatch === undefined) {
58
+ throw new Error(`No valid default found for edition ${Edition[edition]}`);
59
+ }
60
+ defaultsBin = highestMatch.f.toBinary();
61
+ defaultsBinByEdition.set(edition, defaultsBin);
62
+ }
63
+ const f = FeatureSet.fromBinary(defaultsBin);
64
+ for (const c of rest) {
65
+ if (c !== undefined) {
66
+ f.fromBinary(c.toBinary());
67
+ }
68
+ }
69
+ if (!validateMergedFeatures(f)) {
70
+ throw new Error(`Invalid FeatureSet for edition ${Edition[edition]}`);
71
+ }
72
+ return f;
73
+ };
74
+ }
75
+ // When protoc generates google.protobuf.FeatureSetDefaults, it ensures that
76
+ // fields are not repeated or required, do not use oneof, and have a default
77
+ // value.
78
+ //
79
+ // When features for an element are resolved, features of the element and its
80
+ // parents are merged into the default FeatureSet for the edition. Because unset
81
+ // fields in the FeatureSet of an element do not unset the default FeatureSet
82
+ // values, a resolved FeatureSet is guaranteed to have all fields set. This is
83
+ // also the case for extensions to FeatureSet that a user might provide, and for
84
+ // features from the future.
85
+ //
86
+ // We cannot exhaustively validate correctness of FeatureSetDefaults at runtime
87
+ // without knowing the schema: If no value for a feature is provided, we do not
88
+ // know that it exists at all.
89
+ //
90
+ // As a sanity check, we validate that all fields known to our version of
91
+ // FeatureSet are set.
92
+ function validateMergedFeatures(featureSet) {
93
+ for (const fi of FeatureSet.fields.list()) {
94
+ const v = featureSet[fi.localName];
95
+ if (v === undefined) {
96
+ return false;
97
+ }
98
+ if (fi.kind == "enum" && v === 0) {
99
+ return false;
100
+ }
101
+ }
102
+ return true;
103
+ }
@@ -0,0 +1,4 @@
1
+ import type { DescEnum } from "../descriptor-set.js";
2
+ import { ScalarType } from "../field.js";
3
+ export declare function parseTextFormatEnumValue(descEnum: DescEnum, value: string): number;
4
+ export declare function parseTextFormatScalarValue(type: ScalarType, value: string): number | boolean | string | bigint | Uint8Array;
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protobuf",
3
- "version": "1.4.2",
3
+ "version": "1.5.0",
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",