@bufbuild/protobuf 0.0.8 → 0.1.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.
- package/README.md +34 -4
- package/dist/cjs/codegen-info.js +60 -0
- package/dist/cjs/create-descriptor-set.js +952 -0
- package/dist/cjs/create-registry-from-desc.js +270 -0
- package/dist/cjs/create-registry.js +75 -0
- package/dist/cjs/descriptor-set.js +0 -436
- package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
- package/dist/cjs/google/protobuf/empty_pb.js +0 -1
- package/dist/cjs/google/protobuf/struct_pb.js +1 -1
- package/dist/cjs/google/protobuf/type_pb.js +1 -1
- package/dist/cjs/google/varint.js +85 -37
- package/dist/cjs/index.js +11 -7
- package/dist/cjs/private/enum.js +22 -31
- package/dist/cjs/private/field-wrapper.js +30 -1
- package/dist/cjs/private/field.js +1 -1
- package/dist/cjs/private/json-format-common.js +6 -6
- package/dist/cjs/private/names.js +168 -29
- package/dist/cjs/proto-int64.js +23 -40
- package/dist/cjs/proto2.js +2 -2
- package/dist/cjs/proto3.js +2 -2
- package/dist/cjs/type-registry.js +0 -87
- package/dist/esm/codegen-info.js +57 -0
- package/dist/esm/create-descriptor-set.js +947 -0
- package/dist/esm/create-registry-from-desc.js +266 -0
- package/dist/esm/create-registry.js +71 -0
- package/dist/esm/descriptor-set.js +1 -434
- package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
- package/dist/esm/google/protobuf/empty_pb.js +0 -1
- package/dist/esm/google/protobuf/struct_pb.js +1 -1
- package/dist/esm/google/protobuf/type_pb.js +1 -1
- package/dist/esm/google/varint.js +81 -34
- package/dist/esm/index.js +6 -3
- package/dist/esm/private/enum.js +22 -31
- package/dist/esm/private/field-wrapper.js +28 -0
- package/dist/esm/private/field.js +2 -2
- package/dist/esm/private/json-format-common.js +6 -6
- package/dist/esm/private/names.js +163 -24
- package/dist/esm/proto-int64.js +24 -41
- package/dist/esm/proto2.js +3 -3
- package/dist/esm/proto3.js +3 -3
- package/dist/esm/type-registry.js +1 -85
- package/dist/types/codegen-info.d.ts +19 -0
- package/dist/types/create-descriptor-set.d.ts +16 -0
- package/dist/types/create-registry-from-desc.d.ts +15 -0
- package/dist/types/create-registry.d.ts +8 -0
- package/dist/types/descriptor-set.d.ts +554 -108
- package/dist/types/enum.d.ts +9 -5
- package/dist/types/google/protobuf/descriptor_pb.d.ts +6 -6
- package/dist/types/google/protobuf/empty_pb.d.ts +0 -1
- package/dist/types/google/varint.d.ts +21 -9
- package/dist/types/index.d.ts +6 -3
- package/dist/types/message.d.ts +3 -1
- package/dist/types/private/enum.d.ts +4 -8
- package/dist/types/private/field-wrapper.d.ts +7 -0
- package/dist/types/private/message-type.d.ts +1 -4
- package/dist/types/private/names.d.ts +26 -8
- package/dist/types/private/proto-runtime.d.ts +2 -2
- package/dist/types/private/util.d.ts +1 -6
- package/dist/types/proto-int64.d.ts +14 -13
- package/dist/types/type-registry.d.ts +0 -38
- package/package.json +6 -10
- package/dist/cjs/descriptor-registry.js +0 -460
- package/dist/esm/descriptor-registry.js +0 -456
- package/dist/protobuf.cjs +0 -2
- package/dist/protobuf.cjs.map +0 -1
- package/dist/protobuf.esm.js +0 -2
- package/dist/protobuf.esm.js.map +0 -1
- package/dist/protobuf.modern.js +0 -2
- package/dist/protobuf.modern.js.map +0 -1
- package/dist/types/descriptor-registry.d.ts +0 -43
package/dist/esm/private/enum.js
CHANGED
|
@@ -27,35 +27,30 @@ export function getEnumType(enumObject) {
|
|
|
27
27
|
/**
|
|
28
28
|
* Sets reflection information on a generated enum.
|
|
29
29
|
*/
|
|
30
|
-
export function setEnumType(enumObject, typeName, values
|
|
31
|
-
// We do not surface options at this time
|
|
32
|
-
// opt?: {
|
|
33
|
-
// options?: { readonly [extensionName: string]: JsonValue };
|
|
34
|
-
// },
|
|
35
|
-
) {
|
|
30
|
+
export function setEnumType(enumObject, typeName, values, opt) {
|
|
36
31
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
|
|
37
|
-
enumObject[enumTypeSymbol] = makeEnumType(typeName, values)
|
|
32
|
+
enumObject[enumTypeSymbol] = makeEnumType(typeName, values.map((v) => ({
|
|
33
|
+
no: v.no,
|
|
34
|
+
name: v.name,
|
|
35
|
+
localName: enumObject[v.no],
|
|
36
|
+
})), opt);
|
|
38
37
|
}
|
|
39
|
-
// We do not surface options at this time
|
|
40
|
-
// export type PartialEnumValue = Omit<EnumValueInfo, 'options'> & Partial<Pick<EnumValueInfo, "options">>;
|
|
41
38
|
/**
|
|
42
39
|
* Create a new EnumType with the given values.
|
|
43
40
|
*/
|
|
44
|
-
export function makeEnumType(typeName, values
|
|
45
|
-
//
|
|
46
|
-
|
|
47
|
-
// options?: { readonly [extensionName: string]: JsonValue };
|
|
48
|
-
// },
|
|
49
|
-
) {
|
|
41
|
+
export function makeEnumType(typeName, values,
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
43
|
+
_opt) {
|
|
50
44
|
const names = Object.create(null);
|
|
51
45
|
const numbers = Object.create(null);
|
|
52
46
|
const normalValues = [];
|
|
53
47
|
for (const value of values) {
|
|
54
48
|
// We do not surface options at this time
|
|
55
49
|
// const value: EnumValueInfo = {...v, options: v.options ?? emptyReadonlyObject};
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
const n = normalizeEnumValue(value);
|
|
51
|
+
normalValues.push(n);
|
|
52
|
+
names[value.name] = n;
|
|
53
|
+
numbers[value.no] = n;
|
|
59
54
|
}
|
|
60
55
|
return {
|
|
61
56
|
typeName,
|
|
@@ -72,25 +67,21 @@ export function makeEnumType(typeName, values
|
|
|
72
67
|
}
|
|
73
68
|
/**
|
|
74
69
|
* Create a new enum object with the given values.
|
|
70
|
+
* Sets reflection information.
|
|
75
71
|
*/
|
|
76
72
|
export function makeEnum(typeName, values, opt) {
|
|
77
73
|
const enumObject = {};
|
|
78
74
|
for (const value of values) {
|
|
79
|
-
const
|
|
80
|
-
enumObject[
|
|
81
|
-
enumObject[
|
|
75
|
+
const n = normalizeEnumValue(value);
|
|
76
|
+
enumObject[n.localName] = n.no;
|
|
77
|
+
enumObject[n.no] = n.localName;
|
|
82
78
|
}
|
|
83
|
-
setEnumType(enumObject, typeName, values);
|
|
79
|
+
setEnumType(enumObject, typeName, values, opt);
|
|
84
80
|
return enumObject;
|
|
85
81
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (sharedPrefix === undefined) {
|
|
90
|
-
return value.name;
|
|
82
|
+
function normalizeEnumValue(value) {
|
|
83
|
+
if ("localName" in value) {
|
|
84
|
+
return value;
|
|
91
85
|
}
|
|
92
|
-
|
|
93
|
-
return value.name;
|
|
94
|
-
}
|
|
95
|
-
return value.name.substring(sharedPrefix.length);
|
|
86
|
+
return Object.assign(Object.assign({}, value), { localName: value.name });
|
|
96
87
|
}
|
|
@@ -11,6 +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
|
+
import { ScalarType } from "../field.js";
|
|
14
15
|
/**
|
|
15
16
|
* Wrap field values whose message type has a wrapper.
|
|
16
17
|
*/
|
|
@@ -29,3 +30,30 @@ export function wrapField(type, value) {
|
|
|
29
30
|
export function unwrapField(type, value) {
|
|
30
31
|
return type.fieldWrapper ? type.fieldWrapper.unwrapField(value) : value;
|
|
31
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* If the given field uses one of the well-known wrapper types, return
|
|
35
|
+
* the base type it wraps.
|
|
36
|
+
*/
|
|
37
|
+
export function getUnwrappedFieldType(field) {
|
|
38
|
+
if (field.kind !== "message_field") {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (field.repeated) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
if (field.oneof != undefined) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return wktWrapperToScalarType[field.message.typeName];
|
|
48
|
+
}
|
|
49
|
+
const wktWrapperToScalarType = {
|
|
50
|
+
"google.protobuf.DoubleValue": ScalarType.DOUBLE,
|
|
51
|
+
"google.protobuf.FloatValue": ScalarType.FLOAT,
|
|
52
|
+
"google.protobuf.Int64Value": ScalarType.INT64,
|
|
53
|
+
"google.protobuf.UInt64Value": ScalarType.UINT64,
|
|
54
|
+
"google.protobuf.Int32Value": ScalarType.INT32,
|
|
55
|
+
"google.protobuf.UInt32Value": ScalarType.UINT32,
|
|
56
|
+
"google.protobuf.BoolValue": ScalarType.BOOL,
|
|
57
|
+
"google.protobuf.StringValue": ScalarType.STRING,
|
|
58
|
+
"google.protobuf.BytesValue": ScalarType.BYTES,
|
|
59
|
+
};
|
|
@@ -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
|
-
import {
|
|
14
|
+
import { localOneofName } from "./names.js";
|
|
15
15
|
import { assert } from "./assert.js";
|
|
16
16
|
export class InternalOneofInfo {
|
|
17
17
|
constructor(name) {
|
|
@@ -22,7 +22,7 @@ export class InternalOneofInfo {
|
|
|
22
22
|
this.default = undefined;
|
|
23
23
|
this.fields = [];
|
|
24
24
|
this.name = name;
|
|
25
|
-
this.localName =
|
|
25
|
+
this.localName = localOneofName(name);
|
|
26
26
|
}
|
|
27
27
|
addField(field) {
|
|
28
28
|
assert(field.oneof === this, `field ${field.name} not one of ${this.name}`);
|
|
@@ -73,12 +73,12 @@ export function makeJsonFormatCommon(makeWriteField) {
|
|
|
73
73
|
continue;
|
|
74
74
|
}
|
|
75
75
|
if (!Array.isArray(jsonValue)) {
|
|
76
|
-
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON:
|
|
76
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`);
|
|
77
77
|
}
|
|
78
78
|
const targetArray = target[localName];
|
|
79
79
|
for (const jsonItem of jsonValue) {
|
|
80
80
|
if (jsonItem === null) {
|
|
81
|
-
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON:
|
|
81
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonItem)}`);
|
|
82
82
|
}
|
|
83
83
|
let val;
|
|
84
84
|
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- "map" is invalid for repeated fields
|
|
@@ -96,7 +96,7 @@ export function makeJsonFormatCommon(makeWriteField) {
|
|
|
96
96
|
val = readScalar(field.T, jsonItem);
|
|
97
97
|
}
|
|
98
98
|
catch (e) {
|
|
99
|
-
let m = `cannot decode field ${type.typeName}.${field.name} from JSON:
|
|
99
|
+
let m = `cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonItem)}`;
|
|
100
100
|
if (e instanceof Error && e.message.length > 0) {
|
|
101
101
|
m += `: ${e.message}`;
|
|
102
102
|
}
|
|
@@ -134,7 +134,7 @@ export function makeJsonFormatCommon(makeWriteField) {
|
|
|
134
134
|
val = readScalar(field.V.T, jsonMapValue);
|
|
135
135
|
}
|
|
136
136
|
catch (e) {
|
|
137
|
-
let m = `cannot decode map value for field ${type.typeName}.${field.name} from JSON:
|
|
137
|
+
let m = `cannot decode map value for field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`;
|
|
138
138
|
if (e instanceof Error && e.message.length > 0) {
|
|
139
139
|
m += `: ${e.message}`;
|
|
140
140
|
}
|
|
@@ -152,7 +152,7 @@ export function makeJsonFormatCommon(makeWriteField) {
|
|
|
152
152
|
: jsonMapKey).toString()] = val;
|
|
153
153
|
}
|
|
154
154
|
catch (e) {
|
|
155
|
-
let m = `cannot decode map key for field ${type.typeName}.${field.name} from JSON:
|
|
155
|
+
let m = `cannot decode map key for field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`;
|
|
156
156
|
if (e instanceof Error && e.message.length > 0) {
|
|
157
157
|
m += `: ${e.message}`;
|
|
158
158
|
}
|
|
@@ -187,7 +187,7 @@ export function makeJsonFormatCommon(makeWriteField) {
|
|
|
187
187
|
target[localName] = readScalar(field.T, jsonValue);
|
|
188
188
|
}
|
|
189
189
|
catch (e) {
|
|
190
|
-
let m = `cannot decode field ${type.typeName}.${field.name} from JSON:
|
|
190
|
+
let m = `cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`;
|
|
191
191
|
if (e instanceof Error && e.message.length > 0) {
|
|
192
192
|
m += `: ${e.message}`;
|
|
193
193
|
}
|
|
@@ -12,38 +12,113 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
/**
|
|
15
|
-
* Returns the
|
|
15
|
+
* Returns the name of a protobuf element in generated code.
|
|
16
|
+
*
|
|
17
|
+
* Field names - including oneofs - are converted to lowerCamelCase. For
|
|
18
|
+
* messages, enumerations and services, the package name is stripped from
|
|
19
|
+
* the type name. For nested messages and enumerations, the names are joined
|
|
20
|
+
* with an underscore. For methods, the first character is made lowercase.
|
|
16
21
|
*/
|
|
17
|
-
export function
|
|
18
|
-
|
|
22
|
+
export function localName(desc) {
|
|
23
|
+
switch (desc.kind) {
|
|
24
|
+
case "enum_field":
|
|
25
|
+
case "message_field":
|
|
26
|
+
case "map_field":
|
|
27
|
+
case "scalar_field":
|
|
28
|
+
return localFieldName(desc.name, desc.oneof !== undefined);
|
|
29
|
+
case "oneof":
|
|
30
|
+
return localOneofName(desc.name);
|
|
31
|
+
case "enum":
|
|
32
|
+
case "message":
|
|
33
|
+
case "service": {
|
|
34
|
+
const pkg = desc.file.proto.package;
|
|
35
|
+
const offset = pkg === undefined ? 0 : pkg.length + 1;
|
|
36
|
+
const name = desc.typeName.substring(offset).replace(/\./g, "_");
|
|
37
|
+
if (reservedIdent[name]) {
|
|
38
|
+
return name + "$";
|
|
39
|
+
}
|
|
40
|
+
return name;
|
|
41
|
+
}
|
|
42
|
+
case "enum_value": {
|
|
43
|
+
const sharedPrefix = desc.parent.sharedPrefix;
|
|
44
|
+
if (sharedPrefix === undefined) {
|
|
45
|
+
return desc.name;
|
|
46
|
+
}
|
|
47
|
+
const name = desc.name.substring(sharedPrefix.length);
|
|
48
|
+
if (reservedObjectProperties[name]) {
|
|
49
|
+
return name + "$";
|
|
50
|
+
}
|
|
51
|
+
return name;
|
|
52
|
+
}
|
|
53
|
+
case "rpc": {
|
|
54
|
+
let name = desc.name;
|
|
55
|
+
if (name.length == 0) {
|
|
56
|
+
return name;
|
|
57
|
+
}
|
|
58
|
+
name = name[0].toLowerCase() + name.substring(1);
|
|
59
|
+
if (reservedObjectProperties[name]) {
|
|
60
|
+
return name + "$";
|
|
61
|
+
}
|
|
62
|
+
return name;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
19
65
|
}
|
|
20
66
|
/**
|
|
21
|
-
* Returns the
|
|
67
|
+
* Returns the name of a field in generated code.
|
|
22
68
|
*/
|
|
23
|
-
export function
|
|
24
|
-
|
|
69
|
+
export function localFieldName(protoName, inOneof) {
|
|
70
|
+
let name = protoCamelCase(protoName);
|
|
25
71
|
if (inOneof) {
|
|
26
|
-
|
|
72
|
+
// oneof member names are not properties, but values of the `case` property.
|
|
73
|
+
return name;
|
|
27
74
|
}
|
|
28
|
-
|
|
75
|
+
if (reservedObjectProperties[name] || reservedMessageProperties[name]) {
|
|
76
|
+
name = name + "$";
|
|
77
|
+
}
|
|
78
|
+
return name;
|
|
29
79
|
}
|
|
30
80
|
/**
|
|
31
|
-
* Returns the
|
|
81
|
+
* Returns the name of a oneof group in generated code.
|
|
32
82
|
*/
|
|
33
|
-
export function
|
|
34
|
-
return
|
|
83
|
+
export function localOneofName(protoName) {
|
|
84
|
+
return localFieldName(protoName, false);
|
|
35
85
|
}
|
|
36
86
|
/**
|
|
37
|
-
* Returns the
|
|
87
|
+
* Returns the JSON name for a protobuf field, exactly like protoc does.
|
|
38
88
|
*/
|
|
39
|
-
export
|
|
40
|
-
|
|
41
|
-
|
|
89
|
+
export const fieldJsonName = protoCamelCase;
|
|
90
|
+
/**
|
|
91
|
+
* Finds a prefix shared by enum values, for example `MY_ENUM_` for
|
|
92
|
+
* `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`.
|
|
93
|
+
*/
|
|
94
|
+
export function findEnumSharedPrefix(enumName, valueNames) {
|
|
95
|
+
const prefix = camelToSnakeCase(enumName) + "_";
|
|
96
|
+
for (const name of valueNames) {
|
|
97
|
+
if (!name.toLowerCase().startsWith(prefix)) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
const shortName = name.substring(prefix.length);
|
|
101
|
+
if (shortName.length == 0) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
if (/^\d/.test(shortName)) {
|
|
105
|
+
// identifiers must not start with numbers
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
42
108
|
}
|
|
43
|
-
return
|
|
109
|
+
return prefix;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Converts lowerCamelCase or UpperCamelCase into lower_snake_case.
|
|
113
|
+
* This is used to find shared prefixes in an enum.
|
|
114
|
+
*/
|
|
115
|
+
function camelToSnakeCase(camel) {
|
|
116
|
+
return (camel.substring(0, 1) + camel.substring(1).replace(/[A-Z]/g, (c) => "_" + c)).toLowerCase();
|
|
44
117
|
}
|
|
45
|
-
|
|
46
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Converts snake_case to protoCamelCase according to the convention
|
|
120
|
+
* used by protoc to convert a field name to a JSON name.
|
|
121
|
+
*/
|
|
47
122
|
function protoCamelCase(snakeCase) {
|
|
48
123
|
let capNext = false;
|
|
49
124
|
const b = [];
|
|
@@ -77,17 +152,81 @@ function protoCamelCase(snakeCase) {
|
|
|
77
152
|
}
|
|
78
153
|
return b.join("");
|
|
79
154
|
}
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
const
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
155
|
+
// Names that cannot be used for identifiers, such as class names,
|
|
156
|
+
// but _can_ be used for object properties.
|
|
157
|
+
const reservedIdent = {
|
|
158
|
+
// ECMAScript 2015 keywords
|
|
159
|
+
break: true,
|
|
160
|
+
case: true,
|
|
161
|
+
catch: true,
|
|
162
|
+
class: true,
|
|
163
|
+
const: true,
|
|
164
|
+
continue: true,
|
|
165
|
+
debugger: true,
|
|
166
|
+
default: true,
|
|
167
|
+
delete: true,
|
|
168
|
+
do: true,
|
|
169
|
+
else: true,
|
|
170
|
+
export: true,
|
|
171
|
+
extends: true,
|
|
172
|
+
false: true,
|
|
173
|
+
finally: true,
|
|
174
|
+
for: true,
|
|
175
|
+
function: true,
|
|
176
|
+
if: true,
|
|
177
|
+
import: true,
|
|
178
|
+
in: true,
|
|
179
|
+
instanceof: true,
|
|
180
|
+
new: true,
|
|
181
|
+
null: true,
|
|
182
|
+
return: true,
|
|
183
|
+
super: true,
|
|
184
|
+
switch: true,
|
|
185
|
+
this: true,
|
|
186
|
+
throw: true,
|
|
187
|
+
true: true,
|
|
188
|
+
try: true,
|
|
189
|
+
typeof: true,
|
|
190
|
+
var: true,
|
|
191
|
+
void: true,
|
|
192
|
+
while: true,
|
|
193
|
+
with: true,
|
|
194
|
+
yield: true,
|
|
195
|
+
// ECMAScript 2015 future reserved keywords
|
|
196
|
+
enum: true,
|
|
197
|
+
implements: true,
|
|
198
|
+
interface: true,
|
|
199
|
+
let: true,
|
|
200
|
+
package: true,
|
|
201
|
+
private: true,
|
|
202
|
+
protected: true,
|
|
203
|
+
public: true,
|
|
204
|
+
static: true,
|
|
205
|
+
// Class name cannot be 'Object' when targeting ES5 with module CommonJS
|
|
206
|
+
Object: true,
|
|
207
|
+
// TypeScript keywords that cannot be used for types (as opposed to variables)
|
|
208
|
+
bigint: true,
|
|
209
|
+
number: true,
|
|
210
|
+
boolean: true,
|
|
211
|
+
string: true,
|
|
212
|
+
object: true,
|
|
213
|
+
// Identifiers reserved for the runtime, so we can generate legible code
|
|
214
|
+
globalThis: true,
|
|
215
|
+
Uint8Array: true,
|
|
216
|
+
Partial: true,
|
|
217
|
+
};
|
|
218
|
+
// Names that cannot be used for object properties because they are reserved
|
|
219
|
+
// by built-in JavaScript properties.
|
|
220
|
+
const reservedObjectProperties = {
|
|
86
221
|
// names reserved by JavaScript
|
|
87
222
|
constructor: true,
|
|
88
223
|
toString: true,
|
|
89
224
|
toJSON: true,
|
|
90
225
|
valueOf: true,
|
|
226
|
+
};
|
|
227
|
+
// Names that cannot be used for object properties because they are reserved
|
|
228
|
+
// by the runtime.
|
|
229
|
+
const reservedMessageProperties = {
|
|
91
230
|
// names reserved by the runtime
|
|
92
231
|
getType: true,
|
|
93
232
|
clone: true,
|
package/dist/esm/proto-int64.js
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
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
|
-
import {
|
|
14
|
+
import { assert } from "./private/assert.js";
|
|
15
|
+
import { int64FromString, int64ToString, uInt64ToString, } from "./google/varint.js";
|
|
15
16
|
function makeInt64Support() {
|
|
16
17
|
const dv = new DataView(new ArrayBuffer(8));
|
|
17
18
|
// note that Safari 14 implements BigInt, but not the DataView methods
|
|
@@ -19,7 +20,10 @@ function makeInt64Support() {
|
|
|
19
20
|
typeof dv.getBigInt64 === "function" &&
|
|
20
21
|
typeof dv.getBigUint64 === "function" &&
|
|
21
22
|
typeof dv.setBigInt64 === "function" &&
|
|
22
|
-
typeof dv.setBigUint64 === "function"
|
|
23
|
+
typeof dv.setBigUint64 === "function" &&
|
|
24
|
+
(typeof process != "object" ||
|
|
25
|
+
typeof process.env != "object" ||
|
|
26
|
+
process.env.BUF_BIGINT_DISABLE !== "1");
|
|
23
27
|
if (ok) {
|
|
24
28
|
const MIN = BigInt("-9223372036854775808"), MAX = BigInt("9223372036854775807"), UMIN = BigInt("0"), UMAX = BigInt("18446744073709551615");
|
|
25
29
|
return {
|
|
@@ -65,65 +69,44 @@ function makeInt64Support() {
|
|
|
65
69
|
},
|
|
66
70
|
};
|
|
67
71
|
}
|
|
72
|
+
const assertInt64String = (value) => assert(/^-?[0-9]+$/.test(value), `int64 invalid: ${value}`);
|
|
73
|
+
const assertUInt64String = (value) => assert(/^[0-9]+$/.test(value), `uint64 invalid: ${value}`);
|
|
68
74
|
return {
|
|
69
75
|
zero: "0",
|
|
70
76
|
supported: false,
|
|
71
77
|
parse(value) {
|
|
72
|
-
if (
|
|
73
|
-
|
|
78
|
+
if (typeof value != "string") {
|
|
79
|
+
value = value.toString();
|
|
74
80
|
}
|
|
81
|
+
assertInt64String(value);
|
|
75
82
|
return value;
|
|
76
83
|
},
|
|
77
84
|
uParse(value) {
|
|
78
|
-
if (
|
|
79
|
-
|
|
85
|
+
if (typeof value != "string") {
|
|
86
|
+
value = value.toString();
|
|
80
87
|
}
|
|
88
|
+
assertUInt64String(value);
|
|
81
89
|
return value;
|
|
82
90
|
},
|
|
83
91
|
enc(value) {
|
|
84
|
-
if (typeof value
|
|
85
|
-
|
|
86
|
-
throw new Error(`int64 invalid: ${value}`);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
value = value.toString(10);
|
|
92
|
+
if (typeof value != "string") {
|
|
93
|
+
value = value.toString();
|
|
91
94
|
}
|
|
92
|
-
|
|
93
|
-
return
|
|
95
|
+
assertInt64String(value);
|
|
96
|
+
return int64FromString(value);
|
|
94
97
|
},
|
|
95
98
|
uEnc(value) {
|
|
96
|
-
if (typeof value
|
|
97
|
-
|
|
98
|
-
throw new Error(`uint64 invalid: ${value}`);
|
|
99
|
-
}
|
|
99
|
+
if (typeof value != "string") {
|
|
100
|
+
value = value.toString();
|
|
100
101
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
const [minus, lo, hi] = int64fromString(value);
|
|
105
|
-
if (minus) {
|
|
106
|
-
throw new Error(`uint64 invalid: ${value}`);
|
|
107
|
-
}
|
|
108
|
-
return { lo, hi };
|
|
102
|
+
assertUInt64String(value);
|
|
103
|
+
return int64FromString(value);
|
|
109
104
|
},
|
|
110
105
|
dec(lo, hi) {
|
|
111
|
-
|
|
112
|
-
if (minus) {
|
|
113
|
-
// negate
|
|
114
|
-
hi = ~hi;
|
|
115
|
-
if (lo) {
|
|
116
|
-
lo = ~lo + 1;
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
hi += 1;
|
|
120
|
-
}
|
|
121
|
-
return ("-" + int64toString(lo, hi));
|
|
122
|
-
}
|
|
123
|
-
return int64toString(lo, hi);
|
|
106
|
+
return int64ToString(lo, hi);
|
|
124
107
|
},
|
|
125
108
|
uDec(lo, hi) {
|
|
126
|
-
return
|
|
109
|
+
return uInt64ToString(lo, hi);
|
|
127
110
|
},
|
|
128
111
|
};
|
|
129
112
|
}
|
package/dist/esm/proto2.js
CHANGED
|
@@ -16,7 +16,7 @@ import { makeBinaryFormatProto2 } from "./private/binary-format-proto2.js";
|
|
|
16
16
|
import { makeUtilCommon } from "./private/util-common.js";
|
|
17
17
|
import { InternalFieldList } from "./private/field-list.js";
|
|
18
18
|
import { InternalOneofInfo } from "./private/field.js";
|
|
19
|
-
import {
|
|
19
|
+
import { localFieldName, fieldJsonName } from "./private/names.js";
|
|
20
20
|
import { makeJsonFormatProto2 } from "./private/json-format-proto2.js";
|
|
21
21
|
/**
|
|
22
22
|
* Provides functionality for messages defined with the proto2 syntax.
|
|
@@ -60,8 +60,8 @@ function normalizeFieldInfosProto2(fieldInfos) {
|
|
|
60
60
|
? fieldInfos()
|
|
61
61
|
: fieldInfos) {
|
|
62
62
|
const f = field;
|
|
63
|
-
f.localName =
|
|
64
|
-
f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a :
|
|
63
|
+
f.localName = localFieldName(field.name, field.oneof !== undefined);
|
|
64
|
+
f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
|
|
65
65
|
f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
|
|
66
66
|
// In contrast to proto3, repeated fields are unpacked except when explicitly specified.
|
|
67
67
|
f.packed = (_c = field.packed) !== null && _c !== void 0 ? _c : false;
|
package/dist/esm/proto3.js
CHANGED
|
@@ -19,7 +19,7 @@ import { InternalFieldList } from "./private/field-list.js";
|
|
|
19
19
|
import { scalarDefaultValue } from "./private/scalars.js";
|
|
20
20
|
import { ScalarType } from "./field.js";
|
|
21
21
|
import { InternalOneofInfo } from "./private/field.js";
|
|
22
|
-
import {
|
|
22
|
+
import { localFieldName, fieldJsonName } from "./private/names.js";
|
|
23
23
|
/**
|
|
24
24
|
* Provides functionality for messages defined with the proto3 syntax.
|
|
25
25
|
*/
|
|
@@ -64,8 +64,8 @@ function normalizeFieldInfosProto3(fieldInfos) {
|
|
|
64
64
|
? fieldInfos()
|
|
65
65
|
: fieldInfos) {
|
|
66
66
|
const f = field;
|
|
67
|
-
f.localName =
|
|
68
|
-
f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a :
|
|
67
|
+
f.localName = localFieldName(field.name, field.oneof !== undefined);
|
|
68
|
+
f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : fieldJsonName(field.name);
|
|
69
69
|
f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
|
|
70
70
|
// From the proto3 language guide:
|
|
71
71
|
// > In proto3, repeated fields of scalar numeric types are packed by default.
|
|
@@ -11,88 +11,4 @@
|
|
|
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
|
-
|
|
15
|
-
* TypeRegistry is a simple registry for all message, enum, or service types.
|
|
16
|
-
*/
|
|
17
|
-
export class TypeRegistry {
|
|
18
|
-
constructor() {
|
|
19
|
-
this.messages = {};
|
|
20
|
-
this.enums = {};
|
|
21
|
-
this.services = {};
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Find a message type by its protobuf type name.
|
|
25
|
-
*/
|
|
26
|
-
findMessage(typeName) {
|
|
27
|
-
return this.messages[typeName];
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Find an enum type by its protobuf type name.
|
|
31
|
-
*/
|
|
32
|
-
findEnum(typeName) {
|
|
33
|
-
return this.enums[typeName];
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Find a service type by its protobuf type name.
|
|
37
|
-
*/
|
|
38
|
-
findService(typeName) {
|
|
39
|
-
return this.services[typeName];
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a new TypeRegistry from the given types.
|
|
43
|
-
*/
|
|
44
|
-
static from(...types) {
|
|
45
|
-
const registry = new TypeRegistry();
|
|
46
|
-
for (const type of types) {
|
|
47
|
-
registry.add(type);
|
|
48
|
-
}
|
|
49
|
-
return registry;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* @deprecated use TypeRegistry.from()
|
|
53
|
-
*/
|
|
54
|
-
static fromIterable(types) {
|
|
55
|
-
return TypeRegistry.from(...types);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* @deprecated use TypeRegistry.from()
|
|
59
|
-
*/
|
|
60
|
-
static fromTypes(...types) {
|
|
61
|
-
return TypeRegistry.from(...types);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Add a type to the registry. For messages, the types used in message
|
|
65
|
-
* fields are added recursively. For services, the message types used
|
|
66
|
-
* for requests and responses are added recursively.
|
|
67
|
-
*/
|
|
68
|
-
add(type) {
|
|
69
|
-
if ("fields" in type) {
|
|
70
|
-
if (!this.findMessage(type.typeName)) {
|
|
71
|
-
this.messages[type.typeName] = type;
|
|
72
|
-
for (const field of type.fields.list()) {
|
|
73
|
-
if (field.kind == "message") {
|
|
74
|
-
this.add(field.T);
|
|
75
|
-
}
|
|
76
|
-
else if (field.kind == "map" && field.V.kind == "message") {
|
|
77
|
-
this.add(field.V.T);
|
|
78
|
-
}
|
|
79
|
-
else if (field.kind == "enum") {
|
|
80
|
-
this.add(field.T);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else if ("methods" in type) {
|
|
86
|
-
if (!this.findService(type.typeName)) {
|
|
87
|
-
this.services[type.typeName] = type;
|
|
88
|
-
for (const method of Object.values(type.methods)) {
|
|
89
|
-
this.add(method.I);
|
|
90
|
-
this.add(method.O);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
this.enums[type.typeName] = type;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { localName } from "./private/names.js";
|
|
2
|
+
import { getUnwrappedFieldType } from "./private/field-wrapper.js";
|
|
3
|
+
import { scalarDefaultValue } from "./private/scalars.js";
|
|
4
|
+
interface CodegenInfo {
|
|
5
|
+
readonly packageName: string;
|
|
6
|
+
readonly localName: typeof localName;
|
|
7
|
+
readonly symbols: Record<RuntimeSymbolName, RuntimeSymbolInfo>;
|
|
8
|
+
readonly getUnwrappedFieldType: typeof getUnwrappedFieldType;
|
|
9
|
+
readonly wktSourceFiles: readonly string[];
|
|
10
|
+
readonly scalarDefaultValue: typeof scalarDefaultValue;
|
|
11
|
+
}
|
|
12
|
+
declare type RuntimeSymbolName = "proto2" | "proto3" | "Message" | "PartialMessage" | "PlainMessage" | "FieldList" | "MessageType" | "BinaryReadOptions" | "BinaryWriteOptions" | "JsonReadOptions" | "JsonWriteOptions" | "JsonValue" | "JsonObject" | "protoInt64" | "ScalarType" | "MethodKind" | "MethodIdempotency";
|
|
13
|
+
declare type RuntimeSymbolInfo = {
|
|
14
|
+
typeOnly: boolean;
|
|
15
|
+
publicImportPath: string;
|
|
16
|
+
privateImportPath: string;
|
|
17
|
+
};
|
|
18
|
+
export declare const codegenInfo: CodegenInfo;
|
|
19
|
+
export {};
|