@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.
- package/README.md +6 -0
- package/dist/cjs/binary-encoding.js +434 -0
- package/dist/cjs/binary-format.js +2 -0
- package/dist/cjs/descriptor-registry.js +518 -0
- package/dist/cjs/descriptor-set.js +557 -0
- package/dist/cjs/enum.js +2 -0
- package/dist/cjs/field-list.js +2 -0
- package/dist/cjs/field.js +40 -0
- package/dist/cjs/google/protobuf/any_pb.js +280 -0
- package/dist/cjs/google/protobuf/api_pb.js +383 -0
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +334 -0
- package/dist/cjs/google/protobuf/descriptor_pb.js +2311 -0
- package/dist/cjs/google/protobuf/duration_pb.js +201 -0
- package/dist/cjs/google/protobuf/empty_pb.js +72 -0
- package/dist/cjs/google/protobuf/field_mask_pb.js +347 -0
- package/dist/cjs/google/protobuf/source_context_pb.js +74 -0
- package/dist/cjs/google/protobuf/struct_pb.js +288 -0
- package/dist/cjs/google/protobuf/timestamp_pb.js +271 -0
- package/dist/cjs/google/protobuf/type_pb.js +613 -0
- package/dist/cjs/google/protobuf/wrappers_pb.js +658 -0
- package/dist/cjs/google/varint.js +280 -0
- package/dist/cjs/index.js +148 -0
- package/dist/cjs/json-format.js +2 -0
- package/dist/cjs/message-type.js +2 -0
- package/dist/cjs/message.js +98 -0
- package/dist/cjs/private/assert.js +52 -0
- package/dist/cjs/private/base64.js +118 -0
- package/dist/cjs/private/binary-format-common.js +253 -0
- package/dist/cjs/private/binary-format-proto2.js +120 -0
- package/dist/cjs/private/binary-format-proto3.js +102 -0
- package/dist/cjs/private/enum.js +70 -0
- package/dist/cjs/private/field-list.js +62 -0
- package/dist/cjs/private/field-wrapper.js +25 -0
- package/dist/cjs/private/field.js +34 -0
- package/dist/cjs/private/json-format-common.js +482 -0
- package/dist/cjs/private/json-format-proto2.js +100 -0
- package/dist/cjs/private/json-format-proto3.js +103 -0
- package/dist/cjs/private/message-type.js +37 -0
- package/dist/cjs/private/names.js +101 -0
- package/dist/cjs/private/options-map.js +2 -0
- package/dist/cjs/private/proto-runtime.js +24 -0
- package/dist/cjs/private/scalars.js +138 -0
- package/dist/cjs/private/util-common.js +229 -0
- package/dist/cjs/private/util.js +2 -0
- package/dist/cjs/proto-int64.js +121 -0
- package/dist/cjs/proto2.js +86 -0
- package/dist/cjs/proto3.js +99 -0
- package/dist/cjs/service-type.js +43 -0
- package/dist/cjs/type-registry.js +42 -0
- package/dist/esm/binary-encoding.js +434 -0
- package/dist/esm/binary-format.js +1 -0
- package/dist/esm/descriptor-registry.js +521 -0
- package/dist/esm/descriptor-set.js +517 -0
- package/dist/esm/enum.js +1 -0
- package/dist/esm/field-list.js +1 -0
- package/dist/esm/field.js +37 -0
- package/dist/esm/google/protobuf/any_pb.js +276 -0
- package/dist/esm/google/protobuf/api_pb.js +350 -0
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +318 -0
- package/dist/esm/google/protobuf/descriptor_pb.js +2213 -0
- package/dist/esm/google/protobuf/duration_pb.js +197 -0
- package/dist/esm/google/protobuf/empty_pb.js +68 -0
- package/dist/esm/google/protobuf/field_mask_pb.js +343 -0
- package/dist/esm/google/protobuf/source_context_pb.js +70 -0
- package/dist/esm/google/protobuf/struct_pb.js +282 -0
- package/dist/esm/google/protobuf/timestamp_pb.js +267 -0
- package/dist/esm/google/protobuf/type_pb.js +569 -0
- package/dist/esm/google/protobuf/wrappers_pb.js +574 -0
- package/dist/esm/google/varint.js +265 -0
- package/dist/esm/index.js +24 -0
- package/dist/esm/json-format.js +1 -0
- package/dist/esm/message-type.js +1 -0
- package/dist/esm/message.js +94 -0
- package/dist/esm/private/assert.js +41 -0
- package/dist/esm/private/base64.js +113 -0
- package/dist/esm/private/binary-format-common.js +243 -0
- package/dist/esm/private/binary-format-proto2.js +87 -0
- package/dist/esm/private/binary-format-proto3.js +74 -0
- package/dist/esm/private/enum.js +64 -0
- package/dist/esm/private/field-list.js +58 -0
- package/dist/esm/private/field-wrapper.js +20 -0
- package/dist/esm/private/field.js +27 -0
- package/dist/esm/private/json-format-common.js +473 -0
- package/dist/esm/private/json-format-proto2.js +90 -0
- package/dist/esm/private/json-format-proto3.js +89 -0
- package/dist/esm/private/message-type.js +33 -0
- package/dist/esm/private/names.js +90 -0
- package/dist/esm/private/options-map.js +1 -0
- package/dist/esm/private/proto-runtime.js +15 -0
- package/dist/esm/private/scalars.js +129 -0
- package/dist/esm/private/util-common.js +207 -0
- package/dist/esm/private/util.js +1 -0
- package/dist/esm/proto-int64.js +118 -0
- package/dist/esm/proto2.js +77 -0
- package/dist/esm/proto3.js +90 -0
- package/dist/esm/service-type.js +37 -0
- package/dist/esm/type-registry.js +38 -0
- package/dist/types/binary-encoding.d.ts +421 -0
- package/dist/types/binary-format.d.ts +109 -0
- package/dist/types/descriptor-registry.d.ts +42 -0
- package/dist/types/descriptor-set.d.ts +171 -0
- package/dist/types/enum.d.ts +31 -0
- package/dist/types/field-list.d.ts +27 -0
- package/dist/types/field.d.ts +295 -0
- package/dist/types/google/protobuf/any_pb.d.ts +167 -0
- package/dist/types/google/protobuf/api_pb.d.ts +296 -0
- package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +294 -0
- package/dist/types/google/protobuf/descriptor_pb.d.ts +1959 -0
- package/dist/types/google/protobuf/duration_pb.d.ts +117 -0
- package/dist/types/google/protobuf/empty_pb.d.ts +44 -0
- package/dist/types/google/protobuf/field_mask_pb.d.ts +243 -0
- package/dist/types/google/protobuf/source_context_pb.d.ts +44 -0
- package/dist/types/google/protobuf/struct_pb.d.ts +205 -0
- package/dist/types/google/protobuf/timestamp_pb.d.ts +151 -0
- package/dist/types/google/protobuf/type_pb.d.ts +482 -0
- package/dist/types/google/protobuf/wrappers_pb.d.ts +379 -0
- package/dist/types/google/varint.d.ts +65 -0
- package/dist/types/index.d.ts +53 -0
- package/dist/types/json-format.d.ts +129 -0
- package/dist/types/message-type.d.ts +58 -0
- package/dist/types/message.d.ts +124 -0
- package/dist/types/private/assert.d.ts +19 -0
- package/dist/types/private/base64.d.ts +18 -0
- package/dist/types/private/binary-format-common.d.ts +38 -0
- package/dist/types/private/binary-format-proto2.d.ts +2 -0
- package/dist/types/private/binary-format-proto3.d.ts +2 -0
- package/dist/types/private/enum.d.ts +29 -0
- package/dist/types/private/field-list.d.ts +25 -0
- package/dist/types/private/field-wrapper.d.ts +27 -0
- package/dist/types/private/field.d.ts +15 -0
- package/dist/types/private/json-format-common.d.ts +30 -0
- package/dist/types/private/json-format-proto2.d.ts +2 -0
- package/dist/types/private/json-format-proto3.d.ts +2 -0
- package/dist/types/private/message-type.d.ts +15 -0
- package/dist/types/private/names.d.ts +19 -0
- package/dist/types/private/options-map.d.ts +7 -0
- package/dist/types/private/proto-runtime.d.ts +55 -0
- package/dist/types/private/scalars.d.ts +33 -0
- package/dist/types/private/util-common.d.ts +5 -0
- package/dist/types/private/util.d.ts +55 -0
- package/dist/types/proto-int64.d.ts +94 -0
- package/dist/types/proto2.d.ts +4 -0
- package/dist/types/proto3.d.ts +4 -0
- package/dist/types/service-type.d.ts +118 -0
- package/dist/types/type-registry.d.ts +37 -0
- package/package.json +31 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the JSON name for a protobuf field, exactly like protoc does.
|
|
3
|
+
*/
|
|
4
|
+
export function makeJsonName(protoName) {
|
|
5
|
+
return protoCamelCase(protoName);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Returns the local name of a field, exactly like protoc-gen-es does.
|
|
9
|
+
*/
|
|
10
|
+
export function makeFieldName(protoName, inOneof) {
|
|
11
|
+
const n = protoCamelCase(protoName);
|
|
12
|
+
if (inOneof) {
|
|
13
|
+
return n;
|
|
14
|
+
}
|
|
15
|
+
return rProp[n] ? n + escapeChar : n;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns the local name of a oneof group, exactly like protoc-gen-es does.
|
|
19
|
+
*/
|
|
20
|
+
export function makeOneofName(protoName) {
|
|
21
|
+
return makeFieldName(protoName, false);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Returns the local name of a rpc, exactly like protoc-gen-es does.
|
|
25
|
+
*/
|
|
26
|
+
export function makeMethodName(protoName) {
|
|
27
|
+
if (protoName.length == 0) {
|
|
28
|
+
return protoName;
|
|
29
|
+
}
|
|
30
|
+
return protoName[0].toLowerCase() + protoName.substring(1);
|
|
31
|
+
}
|
|
32
|
+
// Converts snake_case to protoCamelCase according to the convention
|
|
33
|
+
// used by protoc to convert a field name to a JSON name.
|
|
34
|
+
function protoCamelCase(snakeCase) {
|
|
35
|
+
let capNext = false;
|
|
36
|
+
const b = [];
|
|
37
|
+
for (let i = 0; i < snakeCase.length; i++) {
|
|
38
|
+
let c = snakeCase.charAt(i);
|
|
39
|
+
switch (c) {
|
|
40
|
+
case "_":
|
|
41
|
+
capNext = true;
|
|
42
|
+
break;
|
|
43
|
+
case "0":
|
|
44
|
+
case "1":
|
|
45
|
+
case "2":
|
|
46
|
+
case "3":
|
|
47
|
+
case "4":
|
|
48
|
+
case "5":
|
|
49
|
+
case "6":
|
|
50
|
+
case "7":
|
|
51
|
+
case "8":
|
|
52
|
+
case "9":
|
|
53
|
+
b.push(c);
|
|
54
|
+
capNext = false;
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
if (capNext) {
|
|
58
|
+
capNext = false;
|
|
59
|
+
c = c.toUpperCase();
|
|
60
|
+
}
|
|
61
|
+
b.push(c);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return b.join("");
|
|
66
|
+
}
|
|
67
|
+
// escapeChar must be appended to a reserved name.
|
|
68
|
+
// We choose '$' because it is invalid in proto identifiers.
|
|
69
|
+
const escapeChar = "$";
|
|
70
|
+
// Names that cannot be used for object properties.
|
|
71
|
+
// See buf_es/protoc-gen-es/internal/protoplugin/names.go
|
|
72
|
+
const rProp = {
|
|
73
|
+
// names reserved by JavaScript
|
|
74
|
+
constructor: true,
|
|
75
|
+
toString: true,
|
|
76
|
+
toJSON: true,
|
|
77
|
+
valueOf: true,
|
|
78
|
+
// names reserved by the runtime
|
|
79
|
+
getType: true,
|
|
80
|
+
clone: true,
|
|
81
|
+
equals: true,
|
|
82
|
+
fromBinary: true,
|
|
83
|
+
fromJson: true,
|
|
84
|
+
fromJsonString: true,
|
|
85
|
+
toBinary: true,
|
|
86
|
+
toJson: true,
|
|
87
|
+
toJsonString: true,
|
|
88
|
+
// names reserved by the runtime for the future
|
|
89
|
+
toObject: true,
|
|
90
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getEnumType, makeEnumType } from "./enum.js";
|
|
2
|
+
import { makeMessageType } from "./message-type.js";
|
|
3
|
+
export function makeProtoRuntime(syntax, json, bin, util) {
|
|
4
|
+
return {
|
|
5
|
+
syntax,
|
|
6
|
+
json,
|
|
7
|
+
bin,
|
|
8
|
+
util,
|
|
9
|
+
makeMessageType(typeName, fields, opt) {
|
|
10
|
+
return makeMessageType(this, typeName, fields, opt);
|
|
11
|
+
},
|
|
12
|
+
makeEnumType,
|
|
13
|
+
getEnumType,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { ScalarType } from "../field.js";
|
|
2
|
+
import { WireType } from "../binary-encoding.js";
|
|
3
|
+
import { protoInt64 } from "../proto-int64.js";
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if both scalar values are equal.
|
|
7
|
+
*/
|
|
8
|
+
export function scalarEquals(type, a, b) {
|
|
9
|
+
if (a === b) {
|
|
10
|
+
// This correctly matches equal values except BYTES and (possibly) 64-bit integers.
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
// Special case BYTES - we need to compare each byte individually
|
|
14
|
+
if (type == ScalarType.BYTES) {
|
|
15
|
+
if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
if (a.length !== b.length) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
for (let i = 0; i < a.length; i++) {
|
|
22
|
+
if (a[i] !== b[i]) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
// Special case 64-bit integers - we support number, string and bigint representation.
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
30
|
+
switch (type) {
|
|
31
|
+
case ScalarType.UINT64:
|
|
32
|
+
case ScalarType.FIXED64:
|
|
33
|
+
case ScalarType.INT64:
|
|
34
|
+
case ScalarType.SFIXED64:
|
|
35
|
+
case ScalarType.SINT64:
|
|
36
|
+
// Loose comparison will match between 0n, 0 and "0".
|
|
37
|
+
return a == b;
|
|
38
|
+
}
|
|
39
|
+
// Anything that hasn't been caught by strict comparison or special cased
|
|
40
|
+
// BYTES and 64-bit integers is not equal.
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns the default value for the given scalar type, following
|
|
45
|
+
* proto3 semantics.
|
|
46
|
+
*/
|
|
47
|
+
export function scalarDefaultValue(type) {
|
|
48
|
+
switch (type) {
|
|
49
|
+
case ScalarType.BOOL:
|
|
50
|
+
return false;
|
|
51
|
+
case ScalarType.UINT64:
|
|
52
|
+
case ScalarType.FIXED64:
|
|
53
|
+
case ScalarType.INT64:
|
|
54
|
+
case ScalarType.SFIXED64:
|
|
55
|
+
case ScalarType.SINT64:
|
|
56
|
+
return protoInt64.zero;
|
|
57
|
+
case ScalarType.DOUBLE:
|
|
58
|
+
case ScalarType.FLOAT:
|
|
59
|
+
return 0.0;
|
|
60
|
+
case ScalarType.BYTES:
|
|
61
|
+
return new Uint8Array(0);
|
|
62
|
+
case ScalarType.STRING:
|
|
63
|
+
return "";
|
|
64
|
+
default:
|
|
65
|
+
// Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
|
|
66
|
+
// We do not use individual cases to save a few bytes code size.
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get information for writing a scalar value.
|
|
72
|
+
*
|
|
73
|
+
* Returns tuple:
|
|
74
|
+
* [0]: appropriate WireType
|
|
75
|
+
* [1]: name of the appropriate method of IBinaryWriter
|
|
76
|
+
* [2]: whether the given value is a default value for proto3 semantics
|
|
77
|
+
*
|
|
78
|
+
* If argument `value` is omitted, [2] is always false.
|
|
79
|
+
*/
|
|
80
|
+
export function scalarTypeInfo(type, value) {
|
|
81
|
+
const isUndefined = value === undefined;
|
|
82
|
+
let wireType = WireType.Varint;
|
|
83
|
+
let isIntrinsicDefault = value === 0;
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- INT32, UINT32, SINT32 are covered by the defaults
|
|
85
|
+
switch (type) {
|
|
86
|
+
case ScalarType.STRING:
|
|
87
|
+
isIntrinsicDefault = isUndefined || !value.length;
|
|
88
|
+
wireType = WireType.LengthDelimited;
|
|
89
|
+
break;
|
|
90
|
+
case ScalarType.BOOL:
|
|
91
|
+
isIntrinsicDefault = value === false;
|
|
92
|
+
break;
|
|
93
|
+
case ScalarType.DOUBLE:
|
|
94
|
+
wireType = WireType.Bit64;
|
|
95
|
+
break;
|
|
96
|
+
case ScalarType.FLOAT:
|
|
97
|
+
wireType = WireType.Bit32;
|
|
98
|
+
break;
|
|
99
|
+
case ScalarType.INT64:
|
|
100
|
+
isIntrinsicDefault = isUndefined || value == 0;
|
|
101
|
+
break;
|
|
102
|
+
case ScalarType.UINT64:
|
|
103
|
+
isIntrinsicDefault = isUndefined || value == 0;
|
|
104
|
+
break;
|
|
105
|
+
case ScalarType.FIXED64:
|
|
106
|
+
isIntrinsicDefault = isUndefined || value == 0;
|
|
107
|
+
wireType = WireType.Bit64;
|
|
108
|
+
break;
|
|
109
|
+
case ScalarType.BYTES:
|
|
110
|
+
isIntrinsicDefault = isUndefined || !value.byteLength;
|
|
111
|
+
wireType = WireType.LengthDelimited;
|
|
112
|
+
break;
|
|
113
|
+
case ScalarType.FIXED32:
|
|
114
|
+
wireType = WireType.Bit32;
|
|
115
|
+
break;
|
|
116
|
+
case ScalarType.SFIXED32:
|
|
117
|
+
wireType = WireType.Bit32;
|
|
118
|
+
break;
|
|
119
|
+
case ScalarType.SFIXED64:
|
|
120
|
+
isIntrinsicDefault = isUndefined || value == 0;
|
|
121
|
+
wireType = WireType.Bit64;
|
|
122
|
+
break;
|
|
123
|
+
case ScalarType.SINT64:
|
|
124
|
+
isIntrinsicDefault = isUndefined || value == 0;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const method = ScalarType[type].toLowerCase();
|
|
128
|
+
return [wireType, method, isUndefined || isIntrinsicDefault];
|
|
129
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { setEnumType } from "./enum.js";
|
|
2
|
+
import { ScalarType } from "../field.js";
|
|
3
|
+
import { scalarEquals } from "./scalars.js";
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument,no-case-declarations */
|
|
5
|
+
export function makeUtilCommon() {
|
|
6
|
+
return {
|
|
7
|
+
setEnumType,
|
|
8
|
+
initPartial(source, target) {
|
|
9
|
+
if (source === undefined) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const type = target.getType();
|
|
13
|
+
for (const member of type.fields.byMember()) {
|
|
14
|
+
const localName = member.localName,
|
|
15
|
+
t = target,
|
|
16
|
+
s = source;
|
|
17
|
+
if (s[localName] === undefined) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
switch (member.kind) {
|
|
21
|
+
case "oneof":
|
|
22
|
+
const sk = s[localName].case;
|
|
23
|
+
if (sk === undefined) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const sourceField = member.findField(sk);
|
|
27
|
+
let val = s[localName].value;
|
|
28
|
+
if (
|
|
29
|
+
sourceField &&
|
|
30
|
+
sourceField.kind == "message" &&
|
|
31
|
+
!(val instanceof sourceField.T)
|
|
32
|
+
) {
|
|
33
|
+
val = new sourceField.T(val);
|
|
34
|
+
}
|
|
35
|
+
t[localName] = { case: sk, value: val };
|
|
36
|
+
break;
|
|
37
|
+
case "scalar":
|
|
38
|
+
case "enum":
|
|
39
|
+
t[localName] = s[localName];
|
|
40
|
+
break;
|
|
41
|
+
case "map":
|
|
42
|
+
switch (member.V.kind) {
|
|
43
|
+
case "scalar":
|
|
44
|
+
case "enum":
|
|
45
|
+
Object.assign(t[localName], s[localName]);
|
|
46
|
+
break;
|
|
47
|
+
case "message":
|
|
48
|
+
const messageType = member.V.T;
|
|
49
|
+
for (const k of Object.keys(s[localName])) {
|
|
50
|
+
let val = s[localName][k];
|
|
51
|
+
if (!messageType.fieldWrapper) {
|
|
52
|
+
// We only take partial input for messages that are not a wrapper type.
|
|
53
|
+
// For those messages, we recursively normalize the partial input.
|
|
54
|
+
val = new messageType(val);
|
|
55
|
+
}
|
|
56
|
+
t[localName][k] = val;
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case "message":
|
|
62
|
+
const mt = member.T;
|
|
63
|
+
if (member.repeated) {
|
|
64
|
+
t[localName] = s[localName].map((val) =>
|
|
65
|
+
val instanceof mt ? val : new mt(val)
|
|
66
|
+
);
|
|
67
|
+
} else if (s[localName] !== undefined) {
|
|
68
|
+
const val = s[localName];
|
|
69
|
+
if (mt.fieldWrapper) {
|
|
70
|
+
t[localName] = val;
|
|
71
|
+
} else {
|
|
72
|
+
t[localName] = val instanceof mt ? mt : new mt(val);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
equals(type, a, b) {
|
|
80
|
+
if (a === b) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if (!a || !b) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
return type.fields.byMember().every((m) => {
|
|
87
|
+
const va = a[m.localName];
|
|
88
|
+
const vb = b[m.localName];
|
|
89
|
+
if (m.repeated) {
|
|
90
|
+
if (va.length !== vb.length) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- repeated fields are never "map"
|
|
94
|
+
switch (m.kind) {
|
|
95
|
+
case "message":
|
|
96
|
+
return va.every((a, i) => m.T.equals(a, vb[i]));
|
|
97
|
+
case "scalar":
|
|
98
|
+
return va.every((a, i) => scalarEquals(m.T, a, vb[i]));
|
|
99
|
+
case "enum":
|
|
100
|
+
return va.every((a, i) =>
|
|
101
|
+
scalarEquals(ScalarType.INT32, a, vb[i])
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`repeated cannot contain ${m.kind}`);
|
|
105
|
+
}
|
|
106
|
+
switch (m.kind) {
|
|
107
|
+
case "message":
|
|
108
|
+
return m.T.equals(va, vb);
|
|
109
|
+
case "enum":
|
|
110
|
+
return scalarEquals(ScalarType.INT32, va, vb);
|
|
111
|
+
case "scalar":
|
|
112
|
+
return scalarEquals(m.T, va, vb);
|
|
113
|
+
case "oneof":
|
|
114
|
+
if (va.case !== vb.case) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const k = va.case,
|
|
118
|
+
s = m.findField(k);
|
|
119
|
+
if (s === undefined) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- oneof fields are never "map"
|
|
123
|
+
switch (s.kind) {
|
|
124
|
+
case "message":
|
|
125
|
+
return s.T.equals(va[k], vb[k]);
|
|
126
|
+
case "enum":
|
|
127
|
+
return scalarEquals(ScalarType.INT32, va, vb);
|
|
128
|
+
case "scalar":
|
|
129
|
+
return scalarEquals(s.T, va, vb);
|
|
130
|
+
}
|
|
131
|
+
throw new Error(`oneof cannot contain ${s.kind}`);
|
|
132
|
+
case "map":
|
|
133
|
+
const keys = Object.keys(va);
|
|
134
|
+
if (keys.some((k) => vb[k] === undefined)) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
switch (m.V.kind) {
|
|
138
|
+
case "message":
|
|
139
|
+
const messageType = m.V.T;
|
|
140
|
+
return keys.every((k) => messageType.equals(va[k], vb[k]));
|
|
141
|
+
case "enum":
|
|
142
|
+
return keys.every((k) =>
|
|
143
|
+
scalarEquals(ScalarType.INT32, va[k], vb[k])
|
|
144
|
+
);
|
|
145
|
+
case "scalar":
|
|
146
|
+
const scalarType = m.V.T;
|
|
147
|
+
return keys.every((k) =>
|
|
148
|
+
scalarEquals(scalarType, va[k], vb[k])
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
clone(message) {
|
|
156
|
+
const type = message.getType(),
|
|
157
|
+
target = new type(),
|
|
158
|
+
any = target;
|
|
159
|
+
for (const member of type.fields.byMember()) {
|
|
160
|
+
const source = message[member.localName];
|
|
161
|
+
let copy;
|
|
162
|
+
if (member.repeated) {
|
|
163
|
+
copy = source.map((e) => cloneSingularField(member, e));
|
|
164
|
+
} else if (member.kind == "map") {
|
|
165
|
+
copy = any[member.localName];
|
|
166
|
+
for (const [key, v] of Object.entries(source)) {
|
|
167
|
+
copy[key] = cloneSingularField(member.V, v);
|
|
168
|
+
}
|
|
169
|
+
} else if (member.kind == "oneof") {
|
|
170
|
+
const f = member.findField(source.case);
|
|
171
|
+
copy = f
|
|
172
|
+
? { case: source.case, value: cloneSingularField(f, source.value) }
|
|
173
|
+
: { case: undefined };
|
|
174
|
+
} else {
|
|
175
|
+
copy = cloneSingularField(member, source);
|
|
176
|
+
}
|
|
177
|
+
any[member.localName] = copy;
|
|
178
|
+
}
|
|
179
|
+
return target;
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
// clone a single field value - i.e. the element type of repeated fields, the value type of maps
|
|
184
|
+
function cloneSingularField(field, value) {
|
|
185
|
+
if (value === undefined) {
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- unmatched "map" is unsupported
|
|
189
|
+
switch (field.kind) {
|
|
190
|
+
case "enum":
|
|
191
|
+
return value;
|
|
192
|
+
case "scalar":
|
|
193
|
+
if (field.T === ScalarType.BYTES) {
|
|
194
|
+
const c = new Uint8Array(value.byteLength);
|
|
195
|
+
c.set(value);
|
|
196
|
+
return c;
|
|
197
|
+
}
|
|
198
|
+
return value;
|
|
199
|
+
case "message":
|
|
200
|
+
if (field.T.fieldWrapper) {
|
|
201
|
+
return field.T.fieldWrapper.unwrapField(
|
|
202
|
+
field.T.fieldWrapper.wrapField(value).clone()
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
return value.clone();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { int64fromString, int64toString } from "./google/varint.js";
|
|
2
|
+
function makeInt64Support() {
|
|
3
|
+
const dv = new DataView(new ArrayBuffer(8));
|
|
4
|
+
// note that Safari 14 implements BigInt, but not the DataView methods
|
|
5
|
+
const ok =
|
|
6
|
+
globalThis.BigInt !== undefined && // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- conditional for BigInt is very much necessary
|
|
7
|
+
typeof dv.getBigInt64 === "function" &&
|
|
8
|
+
typeof dv.getBigUint64 === "function" &&
|
|
9
|
+
typeof dv.setBigInt64 === "function" &&
|
|
10
|
+
typeof dv.setBigUint64 === "function";
|
|
11
|
+
if (ok) {
|
|
12
|
+
const MIN = BigInt("-9223372036854775808"),
|
|
13
|
+
MAX = BigInt("9223372036854775807"),
|
|
14
|
+
UMIN = BigInt("0"),
|
|
15
|
+
UMAX = BigInt("18446744073709551615");
|
|
16
|
+
return {
|
|
17
|
+
zero: BigInt(0),
|
|
18
|
+
supported: true,
|
|
19
|
+
parse(value) {
|
|
20
|
+
const bi = typeof value == "bigint" ? value : BigInt(value);
|
|
21
|
+
if (bi > MAX || bi < MIN) {
|
|
22
|
+
throw new Error(`int64 invalid: ${value}`);
|
|
23
|
+
}
|
|
24
|
+
return bi;
|
|
25
|
+
},
|
|
26
|
+
uParse(value) {
|
|
27
|
+
const bi = typeof value == "bigint" ? value : BigInt(value);
|
|
28
|
+
if (bi > UMAX || bi < UMIN) {
|
|
29
|
+
throw new Error(`uint64 invalid: ${value}`);
|
|
30
|
+
}
|
|
31
|
+
return bi;
|
|
32
|
+
},
|
|
33
|
+
enc(value) {
|
|
34
|
+
dv.setBigInt64(0, this.parse(value), true);
|
|
35
|
+
return {
|
|
36
|
+
lo: dv.getInt32(0, true),
|
|
37
|
+
hi: dv.getInt32(4, true),
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
uEnc(value) {
|
|
41
|
+
dv.setBigInt64(0, this.uParse(value), true);
|
|
42
|
+
return {
|
|
43
|
+
lo: dv.getInt32(0, true),
|
|
44
|
+
hi: dv.getInt32(4, true),
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
dec(lo, hi) {
|
|
48
|
+
dv.setInt32(0, lo, true);
|
|
49
|
+
dv.setInt32(4, hi, true);
|
|
50
|
+
return dv.getBigInt64(0, true);
|
|
51
|
+
},
|
|
52
|
+
uDec(lo, hi) {
|
|
53
|
+
dv.setInt32(0, lo, true);
|
|
54
|
+
dv.setInt32(4, hi, true);
|
|
55
|
+
return dv.getBigUint64(0, true);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
zero: "0",
|
|
61
|
+
supported: false,
|
|
62
|
+
parse(value) {
|
|
63
|
+
if (!/^-?[0-9]+$/.test(value)) {
|
|
64
|
+
throw new Error(`int64 invalid: ${value}`);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
},
|
|
68
|
+
uParse(value) {
|
|
69
|
+
if (!/^-?[0-9]+$/.test(value)) {
|
|
70
|
+
throw new Error(`uint64 invalid: ${value}`);
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
},
|
|
74
|
+
enc(value) {
|
|
75
|
+
if (typeof value == "string") {
|
|
76
|
+
if (!/^-?[0-9]+$/.test(value)) {
|
|
77
|
+
throw new Error(`int64 invalid: ${value}`);
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
value = value.toString(10);
|
|
81
|
+
}
|
|
82
|
+
const [, lo, hi] = int64fromString(value);
|
|
83
|
+
return { lo, hi };
|
|
84
|
+
},
|
|
85
|
+
uEnc(value) {
|
|
86
|
+
if (typeof value == "string") {
|
|
87
|
+
if (!/^-?[0-9]+$/.test(value)) {
|
|
88
|
+
throw new Error(`uint64 invalid: ${value}`);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
value = value.toString(10);
|
|
92
|
+
}
|
|
93
|
+
const [minus, lo, hi] = int64fromString(value);
|
|
94
|
+
if (minus) {
|
|
95
|
+
throw new Error(`uint64 invalid: ${value}`);
|
|
96
|
+
}
|
|
97
|
+
return { lo, hi };
|
|
98
|
+
},
|
|
99
|
+
dec(lo, hi) {
|
|
100
|
+
const minus = (hi & 0x80000000) !== 0;
|
|
101
|
+
if (minus) {
|
|
102
|
+
// negate
|
|
103
|
+
hi = ~hi;
|
|
104
|
+
if (lo) {
|
|
105
|
+
lo = ~lo + 1;
|
|
106
|
+
} else {
|
|
107
|
+
hi += 1;
|
|
108
|
+
}
|
|
109
|
+
return "-" + int64toString(lo, hi);
|
|
110
|
+
}
|
|
111
|
+
return int64toString(lo, hi);
|
|
112
|
+
},
|
|
113
|
+
uDec(lo, hi) {
|
|
114
|
+
return int64toString(lo, hi);
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export const protoInt64 = makeInt64Support();
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { makeProtoRuntime } from "./private/proto-runtime.js";
|
|
2
|
+
import { makeBinaryFormatProto2 } from "./private/binary-format-proto2.js";
|
|
3
|
+
import { makeUtilCommon } from "./private/util-common.js";
|
|
4
|
+
import { InternalFieldList } from "./private/field-list.js";
|
|
5
|
+
import { InternalOneofInfo } from "./private/field.js";
|
|
6
|
+
import { makeFieldName, makeJsonName } from "./private/names.js";
|
|
7
|
+
import { makeJsonFormatProto2 } from "./private/json-format-proto2.js";
|
|
8
|
+
/**
|
|
9
|
+
* Provides functionality for messages defined with the proto2 syntax.
|
|
10
|
+
*/
|
|
11
|
+
export const proto2 = makeProtoRuntime(
|
|
12
|
+
"proto2",
|
|
13
|
+
makeJsonFormatProto2(),
|
|
14
|
+
makeBinaryFormatProto2(),
|
|
15
|
+
{
|
|
16
|
+
...makeUtilCommon(),
|
|
17
|
+
newFieldList(fields) {
|
|
18
|
+
return new InternalFieldList(fields, normalizeFieldInfosProto2);
|
|
19
|
+
},
|
|
20
|
+
initFields(target) {
|
|
21
|
+
for (const member of target.getType().fields.byMember()) {
|
|
22
|
+
const name = member.localName,
|
|
23
|
+
t = target;
|
|
24
|
+
if (member.repeated) {
|
|
25
|
+
t[name] = [];
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
switch (member.kind) {
|
|
29
|
+
case "oneof":
|
|
30
|
+
t[name] = { case: undefined };
|
|
31
|
+
break;
|
|
32
|
+
case "map":
|
|
33
|
+
t[name] = {};
|
|
34
|
+
break;
|
|
35
|
+
case "scalar":
|
|
36
|
+
case "enum":
|
|
37
|
+
case "message":
|
|
38
|
+
// In contrast to proto3, enum and scalar fields have no intrinsic default value,
|
|
39
|
+
// only an optional explicit default value.
|
|
40
|
+
// Unlike proto3 intrinsic default values, proto2 explicit default values are not
|
|
41
|
+
// set on construction, because they are not omitted on the wire. If we did set
|
|
42
|
+
// default values on construction, a deserialize-serialize round-trip would add
|
|
43
|
+
// fields to a message.
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
/* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
|
|
51
|
+
function normalizeFieldInfosProto2(fieldInfos) {
|
|
52
|
+
const r = [];
|
|
53
|
+
let o;
|
|
54
|
+
for (const field of typeof fieldInfos == "function"
|
|
55
|
+
? fieldInfos()
|
|
56
|
+
: fieldInfos) {
|
|
57
|
+
const f = field;
|
|
58
|
+
f.localName = makeFieldName(field.name, field.oneof !== undefined);
|
|
59
|
+
f.jsonName = field.jsonName ?? makeJsonName(field.name);
|
|
60
|
+
f.repeated = field.repeated ?? false;
|
|
61
|
+
// In contrast to proto3, repeated fields are unpacked except when explicitly specified.
|
|
62
|
+
f.packed = field.packed ?? false;
|
|
63
|
+
// We do not surface options at this time
|
|
64
|
+
// f.options = field.options ?? emptyReadonlyObject;
|
|
65
|
+
if (field.oneof !== undefined) {
|
|
66
|
+
const ooname =
|
|
67
|
+
typeof field.oneof == "string" ? field.oneof : field.oneof.name;
|
|
68
|
+
if (!o || o.name != ooname) {
|
|
69
|
+
o = new InternalOneofInfo(ooname);
|
|
70
|
+
}
|
|
71
|
+
f.oneof = o;
|
|
72
|
+
o.addField(f);
|
|
73
|
+
}
|
|
74
|
+
r.push(f);
|
|
75
|
+
}
|
|
76
|
+
return r;
|
|
77
|
+
}
|