@bufbuild/protobuf 2.0.0-alpha.2 → 2.0.0-alpha.4
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 +1 -1
- package/dist/cjs/clone.d.ts +1 -1
- package/dist/cjs/clone.js +9 -6
- package/dist/cjs/codegenv1/boot.d.ts +1 -1
- package/dist/cjs/codegenv1/boot.js +3 -5
- package/dist/cjs/codegenv1/embed.d.ts +1 -1
- package/dist/cjs/codegenv1/embed.js +39 -34
- package/dist/cjs/codegenv1/enum.d.ts +1 -1
- package/dist/cjs/codegenv1/extension.d.ts +1 -1
- package/dist/cjs/codegenv1/file.d.ts +1 -1
- package/dist/cjs/codegenv1/file.js +5 -7
- package/dist/cjs/codegenv1/index.d.ts +1 -0
- package/dist/cjs/codegenv1/index.js +1 -0
- package/dist/cjs/codegenv1/message.d.ts +1 -1
- package/dist/cjs/codegenv1/scalar.d.ts +5 -0
- package/dist/cjs/codegenv1/scalar.js +39 -0
- package/dist/cjs/codegenv1/service.d.ts +3 -3
- package/dist/cjs/codegenv1/types.d.ts +7 -9
- package/dist/cjs/create.d.ts +1 -1
- package/dist/cjs/create.js +6 -5
- package/dist/cjs/{desc-types.d.ts → descriptors.d.ts} +39 -7
- package/dist/cjs/descriptors.js +53 -0
- package/dist/cjs/equals.d.ts +1 -1
- package/dist/cjs/extensions.d.ts +1 -1
- package/dist/cjs/extensions.js +4 -3
- package/dist/cjs/fields.d.ts +1 -1
- package/dist/cjs/from-binary.d.ts +1 -1
- package/dist/cjs/from-binary.js +32 -29
- package/dist/cjs/from-json.d.ts +2 -2
- package/dist/cjs/from-json.js +201 -188
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/is-message.d.ts +1 -1
- package/dist/cjs/proto-int64.d.ts +4 -1
- package/dist/cjs/proto-int64.js +16 -6
- package/dist/cjs/reflect/error.d.ts +1 -1
- package/dist/cjs/reflect/guard.d.ts +1 -1
- package/dist/cjs/reflect/guard.js +4 -4
- package/dist/cjs/reflect/index.d.ts +0 -1
- package/dist/cjs/reflect/index.js +0 -1
- package/dist/cjs/reflect/names.d.ts +1 -1
- package/dist/cjs/reflect/names.js +1 -1
- package/dist/cjs/reflect/nested-types.d.ts +1 -1
- package/dist/cjs/reflect/reflect-check.d.ts +10 -1
- package/dist/cjs/reflect/reflect-check.js +126 -9
- package/dist/cjs/reflect/reflect-types.d.ts +3 -41
- package/dist/cjs/reflect/reflect.d.ts +1 -1
- package/dist/cjs/reflect/reflect.js +42 -60
- package/dist/cjs/reflect/scalar.d.ts +3 -55
- package/dist/cjs/reflect/scalar.js +26 -220
- package/dist/cjs/reflect/unsafe.d.ts +1 -17
- package/dist/cjs/reflect/unsafe.js +2 -20
- package/dist/cjs/{reflect/registry.d.ts → registry.d.ts} +2 -2
- package/dist/cjs/{reflect/registry.js → registry.js} +54 -36
- package/dist/cjs/to-binary.d.ts +1 -1
- package/dist/cjs/to-binary.js +27 -27
- package/dist/cjs/to-json.d.ts +7 -6
- package/dist/cjs/to-json.js +57 -44
- package/dist/cjs/types.d.ts +1 -1
- package/dist/cjs/wire/base64-encoding.js +2 -2
- package/dist/cjs/wire/binary-encoding.d.ts +20 -0
- package/dist/cjs/wire/binary-encoding.js +75 -8
- package/dist/cjs/wire/size-delimited.d.ts +1 -1
- package/dist/cjs/wire/text-format.d.ts +1 -2
- package/dist/cjs/wire/text-format.js +21 -19
- package/dist/cjs/wkt/any.d.ts +2 -2
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
- package/dist/cjs/wkt/wrappers.d.ts +1 -1
- package/dist/esm/clone.d.ts +1 -1
- package/dist/esm/clone.js +8 -5
- package/dist/esm/codegenv1/boot.d.ts +1 -1
- package/dist/esm/codegenv1/boot.js +3 -5
- package/dist/esm/codegenv1/embed.d.ts +1 -1
- package/dist/esm/codegenv1/embed.js +9 -4
- package/dist/esm/codegenv1/enum.d.ts +1 -1
- package/dist/esm/codegenv1/extension.d.ts +1 -1
- package/dist/esm/codegenv1/file.d.ts +1 -1
- package/dist/esm/codegenv1/file.js +4 -6
- package/dist/esm/codegenv1/index.d.ts +1 -0
- package/dist/esm/codegenv1/index.js +1 -0
- package/dist/esm/codegenv1/message.d.ts +1 -1
- package/dist/esm/codegenv1/scalar.d.ts +5 -0
- package/dist/esm/codegenv1/scalar.js +35 -0
- package/dist/esm/codegenv1/service.d.ts +3 -3
- package/dist/esm/codegenv1/types.d.ts +7 -9
- package/dist/esm/create.d.ts +1 -1
- package/dist/esm/create.js +4 -3
- package/dist/esm/{desc-types.d.ts → descriptors.d.ts} +39 -7
- package/dist/esm/descriptors.js +50 -0
- package/dist/esm/equals.d.ts +1 -1
- package/dist/esm/extensions.d.ts +1 -1
- package/dist/esm/extensions.js +4 -3
- package/dist/esm/fields.d.ts +1 -1
- package/dist/esm/from-binary.d.ts +1 -1
- package/dist/esm/from-binary.js +14 -11
- package/dist/esm/from-json.d.ts +2 -2
- package/dist/esm/from-json.js +194 -181
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/is-message.d.ts +1 -1
- package/dist/esm/proto-int64.d.ts +4 -1
- package/dist/esm/proto-int64.js +16 -6
- package/dist/esm/reflect/error.d.ts +1 -1
- package/dist/esm/reflect/guard.d.ts +1 -1
- package/dist/esm/reflect/guard.js +4 -4
- package/dist/esm/reflect/index.d.ts +0 -1
- package/dist/esm/reflect/index.js +0 -1
- package/dist/esm/reflect/names.d.ts +1 -1
- package/dist/esm/reflect/names.js +1 -1
- package/dist/esm/reflect/nested-types.d.ts +1 -1
- package/dist/esm/reflect/reflect-check.d.ts +10 -1
- package/dist/esm/reflect/reflect-check.js +119 -2
- package/dist/esm/reflect/reflect-types.d.ts +3 -41
- package/dist/esm/reflect/reflect-types.js +1 -0
- package/dist/esm/reflect/reflect.d.ts +1 -1
- package/dist/esm/reflect/reflect.js +25 -43
- package/dist/esm/reflect/scalar.d.ts +3 -55
- package/dist/esm/reflect/scalar.js +14 -205
- package/dist/esm/reflect/unsafe.d.ts +1 -17
- package/dist/esm/reflect/unsafe.js +1 -17
- package/dist/esm/{reflect/registry.d.ts → registry.d.ts} +2 -2
- package/dist/esm/{reflect/registry.js → registry.js} +44 -26
- package/dist/esm/to-binary.d.ts +1 -1
- package/dist/esm/to-binary.js +1 -1
- package/dist/esm/to-json.d.ts +7 -6
- package/dist/esm/to-json.js +42 -29
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/wire/base64-encoding.js +2 -2
- package/dist/esm/wire/binary-encoding.d.ts +20 -0
- package/dist/esm/wire/binary-encoding.js +68 -1
- package/dist/esm/wire/size-delimited.d.ts +1 -1
- package/dist/esm/wire/text-format.d.ts +1 -2
- package/dist/esm/wire/text-format.js +5 -3
- package/dist/esm/wkt/any.d.ts +2 -2
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
- package/dist/esm/wkt/wrappers.d.ts +1 -1
- package/package.json +1 -1
- package/dist/cjs/desc-types.js +0 -15
- package/dist/cjs/reflect/assert.d.ts +0 -16
- package/dist/cjs/reflect/assert.js +0 -59
- package/dist/esm/desc-types.js +0 -14
- package/dist/esm/reflect/assert.d.ts +0 -16
- package/dist/esm/reflect/assert.js +0 -52
package/dist/esm/proto-int64.js
CHANGED
|
@@ -11,8 +11,11 @@
|
|
|
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 { assert } from "./reflect/assert.js";
|
|
15
14
|
import { int64FromString, int64ToString, uInt64ToString, } from "./wire/varint.js";
|
|
15
|
+
/**
|
|
16
|
+
* Int64Support for the current environment.
|
|
17
|
+
*/
|
|
18
|
+
export const protoInt64 = /*@__PURE__*/ makeInt64Support();
|
|
16
19
|
function makeInt64Support() {
|
|
17
20
|
const dv = new DataView(new ArrayBuffer(8));
|
|
18
21
|
// note that Safari 14 implements BigInt, but not the DataView methods
|
|
@@ -32,14 +35,14 @@ function makeInt64Support() {
|
|
|
32
35
|
parse(value) {
|
|
33
36
|
const bi = typeof value == "bigint" ? value : BigInt(value);
|
|
34
37
|
if (bi > MAX || bi < MIN) {
|
|
35
|
-
throw new Error(`int64
|
|
38
|
+
throw new Error(`invalid int64: ${value}`);
|
|
36
39
|
}
|
|
37
40
|
return bi;
|
|
38
41
|
},
|
|
39
42
|
uParse(value) {
|
|
40
43
|
const bi = typeof value == "bigint" ? value : BigInt(value);
|
|
41
44
|
if (bi > UMAX || bi < UMIN) {
|
|
42
|
-
throw new Error(`uint64
|
|
45
|
+
throw new Error(`invalid uint64: ${value}`);
|
|
43
46
|
}
|
|
44
47
|
return bi;
|
|
45
48
|
},
|
|
@@ -69,8 +72,6 @@ function makeInt64Support() {
|
|
|
69
72
|
},
|
|
70
73
|
};
|
|
71
74
|
}
|
|
72
|
-
const assertInt64String = (value) => assert(/^-?[0-9]+$/.test(value), `int64 invalid: ${value}`);
|
|
73
|
-
const assertUInt64String = (value) => assert(/^[0-9]+$/.test(value), `uint64 invalid: ${value}`);
|
|
74
75
|
return {
|
|
75
76
|
zero: "0",
|
|
76
77
|
supported: false,
|
|
@@ -110,4 +111,13 @@ function makeInt64Support() {
|
|
|
110
111
|
},
|
|
111
112
|
};
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
+
function assertInt64String(value) {
|
|
115
|
+
if (!/^-?[0-9]+$/.test(value)) {
|
|
116
|
+
throw new Error("invalid int64: " + value);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function assertUInt64String(value) {
|
|
120
|
+
if (!/^[0-9]+$/.test(value)) {
|
|
121
|
+
throw new Error("invalid uint64: " + value);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Message } from "../types.js";
|
|
2
2
|
import type { ScalarValue } from "./scalar.js";
|
|
3
3
|
import type { ReflectList, ReflectMap, ReflectMessage } from "./reflect-types.js";
|
|
4
|
-
import type { DescField, DescMessage } from "../
|
|
4
|
+
import type { DescField, DescMessage } from "../descriptors.js";
|
|
5
5
|
export declare function isObject(arg: unknown): arg is Record<string, unknown>;
|
|
6
6
|
export declare function isOneofADT(arg: unknown): arg is OneofADT;
|
|
7
7
|
export type OneofADT = {
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { unsafeLocal } from "./unsafe.js";
|
|
15
|
-
import { isMessage } from "../is-message.js";
|
|
16
15
|
export function isObject(arg) {
|
|
17
16
|
return arg !== null && typeof arg == "object" && !Array.isArray(arg);
|
|
18
17
|
}
|
|
@@ -64,7 +63,8 @@ export function isReflectMap(arg, field) {
|
|
|
64
63
|
export function isReflectMessage(arg, messageDesc) {
|
|
65
64
|
return (isObject(arg) &&
|
|
66
65
|
unsafeLocal in arg &&
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
"desc" in arg &&
|
|
67
|
+
isObject(arg.desc) &&
|
|
68
|
+
arg.desc.kind === "message" &&
|
|
69
|
+
(messageDesc === undefined || arg.desc.typeName == messageDesc.typeName));
|
|
70
70
|
}
|
|
@@ -3,6 +3,5 @@ export * from "./names.js";
|
|
|
3
3
|
export * from "./nested-types.js";
|
|
4
4
|
export * from "./reflect.js";
|
|
5
5
|
export * from "./reflect-types.js";
|
|
6
|
-
export * from "./registry.js";
|
|
7
6
|
export * from "./scalar.js";
|
|
8
7
|
export { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
|
|
@@ -16,6 +16,5 @@ export * from "./names.js";
|
|
|
16
16
|
export * from "./nested-types.js";
|
|
17
17
|
export * from "./reflect.js";
|
|
18
18
|
export * from "./reflect-types.js";
|
|
19
|
-
export * from "./registry.js";
|
|
20
19
|
export * from "./scalar.js";
|
|
21
20
|
export { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
|
|
@@ -6,6 +6,6 @@ export declare function protoCamelCase(snakeCase: string): string;
|
|
|
6
6
|
/**
|
|
7
7
|
* Escapes names that are reserved for ECMAScript built-in object properties.
|
|
8
8
|
*
|
|
9
|
-
* Also see safeIdentifier() from @bufbuild/protoplugin
|
|
9
|
+
* Also see safeIdentifier() from @bufbuild/protoplugin.
|
|
10
10
|
*/
|
|
11
11
|
export declare function safeObjectProperty(name: string): string;
|
|
@@ -62,7 +62,7 @@ const reservedObjectProperties = new Set([
|
|
|
62
62
|
/**
|
|
63
63
|
* Escapes names that are reserved for ECMAScript built-in object properties.
|
|
64
64
|
*
|
|
65
|
-
* Also see safeIdentifier() from @bufbuild/protoplugin
|
|
65
|
+
* Also see safeIdentifier() from @bufbuild/protoplugin.
|
|
66
66
|
*/
|
|
67
67
|
export function safeObjectProperty(name) {
|
|
68
68
|
return reservedObjectProperties.has(name) ? name + "$" : name;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage, DescService } from "../
|
|
1
|
+
import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage, DescService } from "../descriptors.js";
|
|
2
2
|
/**
|
|
3
3
|
* Iterate over all types - enumerations, extensions, services, messages -
|
|
4
4
|
* and enumerations, extensions and messages nested in messages.
|
|
@@ -1,9 +1,18 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DescField } from "../descriptors.js";
|
|
2
2
|
import { FieldError } from "./error.js";
|
|
3
|
+
/**
|
|
4
|
+
* Check whether the given field value is valid for the reflect API.
|
|
5
|
+
*/
|
|
3
6
|
export declare function checkField(field: DescField, value: unknown): FieldError | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Check whether the given list item is valid for the reflect API.
|
|
9
|
+
*/
|
|
4
10
|
export declare function checkListItem(field: DescField & {
|
|
5
11
|
fieldKind: "list";
|
|
6
12
|
}, index: number, value: unknown): FieldError | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Check whether the given map key and value are valid for the reflect API.
|
|
15
|
+
*/
|
|
7
16
|
export declare function checkMapEntry(field: DescField & {
|
|
8
17
|
fieldKind: "map";
|
|
9
18
|
}, key: unknown, value: unknown): FieldError | undefined;
|
|
@@ -11,11 +11,16 @@
|
|
|
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,
|
|
15
|
-
import { checkScalarValue } from "./scalar.js";
|
|
14
|
+
import { ScalarType, } from "../descriptors.js";
|
|
16
15
|
import { isMessage } from "../is-message.js";
|
|
17
16
|
import { FieldError } from "./error.js";
|
|
18
17
|
import { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
|
|
18
|
+
import { FLOAT32_MAX, FLOAT32_MIN, INT32_MAX, INT32_MIN, UINT32_MAX, } from "../wire/binary-encoding.js";
|
|
19
|
+
import { getTextEncoding } from "../wire/text-encoding.js";
|
|
20
|
+
import { protoInt64 } from "../proto-int64.js";
|
|
21
|
+
/**
|
|
22
|
+
* Check whether the given field value is valid for the reflect API.
|
|
23
|
+
*/
|
|
19
24
|
export function checkField(field, value) {
|
|
20
25
|
const check = field.fieldKind == "list"
|
|
21
26
|
? isReflectList(value, field)
|
|
@@ -39,6 +44,9 @@ export function checkField(field, value) {
|
|
|
39
44
|
}
|
|
40
45
|
return new FieldError(field, reason);
|
|
41
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Check whether the given list item is valid for the reflect API.
|
|
49
|
+
*/
|
|
42
50
|
export function checkListItem(field, index, value) {
|
|
43
51
|
const check = checkSingular(field, value);
|
|
44
52
|
if (check !== true) {
|
|
@@ -46,6 +54,9 @@ export function checkListItem(field, index, value) {
|
|
|
46
54
|
}
|
|
47
55
|
return undefined;
|
|
48
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Check whether the given map key and value are valid for the reflect API.
|
|
59
|
+
*/
|
|
49
60
|
export function checkMapEntry(field, key, value) {
|
|
50
61
|
const checkKey = checkScalarValue(key, field.mapKey);
|
|
51
62
|
if (checkKey !== true) {
|
|
@@ -69,6 +80,84 @@ function checkSingular(field, value) {
|
|
|
69
80
|
}
|
|
70
81
|
return isReflectMessage(value, field.message);
|
|
71
82
|
}
|
|
83
|
+
function checkScalarValue(value, scalar) {
|
|
84
|
+
switch (scalar) {
|
|
85
|
+
case ScalarType.DOUBLE:
|
|
86
|
+
return typeof value == "number";
|
|
87
|
+
case ScalarType.FLOAT:
|
|
88
|
+
if (typeof value != "number") {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (Number.isNaN(value) || !Number.isFinite(value)) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if (value > FLOAT32_MAX || value < FLOAT32_MIN) {
|
|
95
|
+
return `${value.toFixed()} out of range`;
|
|
96
|
+
}
|
|
97
|
+
return true;
|
|
98
|
+
case ScalarType.INT32:
|
|
99
|
+
case ScalarType.SFIXED32:
|
|
100
|
+
case ScalarType.SINT32:
|
|
101
|
+
// signed
|
|
102
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (value > INT32_MAX || value < INT32_MIN) {
|
|
106
|
+
return `${value.toFixed()} out of range`;
|
|
107
|
+
}
|
|
108
|
+
return true;
|
|
109
|
+
case ScalarType.FIXED32:
|
|
110
|
+
case ScalarType.UINT32:
|
|
111
|
+
// unsigned
|
|
112
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
if (value > UINT32_MAX || value < 0) {
|
|
116
|
+
return `${value.toFixed()} out of range`;
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
case ScalarType.BOOL:
|
|
120
|
+
return typeof value == "boolean";
|
|
121
|
+
case ScalarType.STRING:
|
|
122
|
+
if (typeof value != "string") {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return getTextEncoding().checkUtf8(value) || "invalid UTF8";
|
|
126
|
+
case ScalarType.BYTES:
|
|
127
|
+
return value instanceof Uint8Array;
|
|
128
|
+
case ScalarType.INT64:
|
|
129
|
+
case ScalarType.SFIXED64:
|
|
130
|
+
case ScalarType.SINT64:
|
|
131
|
+
// signed
|
|
132
|
+
if (typeof value != "string" &&
|
|
133
|
+
typeof value !== "bigint" &&
|
|
134
|
+
typeof value !== "number") {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
protoInt64.parse(value);
|
|
139
|
+
}
|
|
140
|
+
catch (e) {
|
|
141
|
+
return `${value} out of range`;
|
|
142
|
+
}
|
|
143
|
+
return true;
|
|
144
|
+
case ScalarType.FIXED64:
|
|
145
|
+
case ScalarType.UINT64:
|
|
146
|
+
// unsigned
|
|
147
|
+
if (typeof value != "string" &&
|
|
148
|
+
typeof value !== "bigint" &&
|
|
149
|
+
typeof value !== "number") {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
protoInt64.uParse(value);
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
return `${value} out of range`;
|
|
157
|
+
}
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
72
161
|
function reasonSingular(field, val, details) {
|
|
73
162
|
details =
|
|
74
163
|
typeof details == "string" ? `: ${details}` : `, got ${formatVal(val)}`;
|
|
@@ -141,3 +230,31 @@ function formatReflectMap(field) {
|
|
|
141
230
|
return `ReflectMap (${ScalarType[field.mapKey]}, ${ScalarType[field.scalar]})`;
|
|
142
231
|
}
|
|
143
232
|
}
|
|
233
|
+
function scalarTypeDescription(scalar) {
|
|
234
|
+
switch (scalar) {
|
|
235
|
+
case ScalarType.STRING:
|
|
236
|
+
return "string";
|
|
237
|
+
case ScalarType.BOOL:
|
|
238
|
+
return "boolean";
|
|
239
|
+
case ScalarType.INT64:
|
|
240
|
+
case ScalarType.SINT64:
|
|
241
|
+
case ScalarType.SFIXED64:
|
|
242
|
+
return "bigint (int64)";
|
|
243
|
+
case ScalarType.UINT64:
|
|
244
|
+
case ScalarType.FIXED64:
|
|
245
|
+
return "bigint (uint64)";
|
|
246
|
+
case ScalarType.BYTES:
|
|
247
|
+
return "Uint8Array";
|
|
248
|
+
case ScalarType.DOUBLE:
|
|
249
|
+
return "number (float64)";
|
|
250
|
+
case ScalarType.FLOAT:
|
|
251
|
+
return "number (float32)";
|
|
252
|
+
case ScalarType.FIXED32:
|
|
253
|
+
case ScalarType.UINT32:
|
|
254
|
+
return "number (uint32)";
|
|
255
|
+
case ScalarType.INT32:
|
|
256
|
+
case ScalarType.SFIXED32:
|
|
257
|
+
case ScalarType.SINT32:
|
|
258
|
+
return "number (int32)";
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DescField, type DescMessage, type DescOneof } from "../descriptors.js";
|
|
2
2
|
import { FieldError } from "./error.js";
|
|
3
3
|
import { unsafeLocal } from "./unsafe.js";
|
|
4
4
|
import type { Message, UnknownField } from "../types.js";
|
|
5
|
-
import type {
|
|
5
|
+
import type { ScalarValue } from "./scalar.js";
|
|
6
6
|
/**
|
|
7
7
|
* ReflectMessage provides dynamic access and manipulation of a message.
|
|
8
8
|
*/
|
|
@@ -109,18 +109,6 @@ export interface ReflectMessage {
|
|
|
109
109
|
* a valid value. To reset a field, use clear().
|
|
110
110
|
*/
|
|
111
111
|
set<Field extends DescField>(field: Field, value: ReflectSetValue<Field>): FieldError | undefined;
|
|
112
|
-
/**
|
|
113
|
-
* Add an item to a list field.
|
|
114
|
-
*/
|
|
115
|
-
addListItem<Field extends DescField & {
|
|
116
|
-
fieldKind: "list";
|
|
117
|
-
}>(field: Field, value: ReflectAddListItemValue<Field>): FieldError | undefined;
|
|
118
|
-
/**
|
|
119
|
-
* Set a map entry.
|
|
120
|
-
*/
|
|
121
|
-
setMapEntry<Field extends DescField & {
|
|
122
|
-
fieldKind: "map";
|
|
123
|
-
}>(field: Field, key: MapEntryKey, value: ReflectSetMapEntryValue<Field>): FieldError | undefined;
|
|
124
112
|
/**
|
|
125
113
|
* Returns the unknown fields of the message.
|
|
126
114
|
*/
|
|
@@ -230,7 +218,7 @@ export type ReflectGetValue<Field extends DescField = DescField> = (Field extend
|
|
|
230
218
|
} ? ReflectMap<MapEntryKey, enumVal> : Field extends {
|
|
231
219
|
mapKind: "scalar";
|
|
232
220
|
scalar: infer T;
|
|
233
|
-
} ? ReflectMap<MapEntryKey, ScalarValue<T
|
|
221
|
+
} ? ReflectMap<MapEntryKey, ScalarValue<T>> : never) : Field extends {
|
|
234
222
|
fieldKind: "list";
|
|
235
223
|
} ? ReflectList : Field extends {
|
|
236
224
|
fieldKind: "enum";
|
|
@@ -255,30 +243,4 @@ export type ReflectSetValue<Field extends DescField = DescField> = (Field extend
|
|
|
255
243
|
fieldKind: "scalar";
|
|
256
244
|
scalar: infer T;
|
|
257
245
|
} ? ScalarValue<T> : never);
|
|
258
|
-
/**
|
|
259
|
-
* The type of the "value" argument of ReflectMessage.addListItem()
|
|
260
|
-
*/
|
|
261
|
-
export type ReflectAddListItemValue<Field extends DescField & {
|
|
262
|
-
fieldKind: "list";
|
|
263
|
-
}> = (Field extends {
|
|
264
|
-
listKind: "scalar";
|
|
265
|
-
scalar: infer T;
|
|
266
|
-
} ? ScalarValue<T> : Field extends {
|
|
267
|
-
listKind: "enum";
|
|
268
|
-
} ? enumVal : Field extends {
|
|
269
|
-
listKind: "message";
|
|
270
|
-
} ? ReflectMessage : never);
|
|
271
|
-
/**
|
|
272
|
-
* The type of the "value" argument of ReflectMessage.setMapEntry()
|
|
273
|
-
*/
|
|
274
|
-
export type ReflectSetMapEntryValue<Field extends DescField & {
|
|
275
|
-
fieldKind: "map";
|
|
276
|
-
}> = (Field extends {
|
|
277
|
-
mapKind: "enum";
|
|
278
|
-
} ? enumVal : Field extends {
|
|
279
|
-
mapKind: "message";
|
|
280
|
-
} ? ReflectMessage : Field extends {
|
|
281
|
-
mapKind: "scalar";
|
|
282
|
-
scalar: infer T;
|
|
283
|
-
} ? ScalarValue<T, LongType.BIGINT> : never);
|
|
284
246
|
export {};
|
|
@@ -11,5 +11,6 @@
|
|
|
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 {} from "../descriptors.js";
|
|
14
15
|
import { FieldError } from "./error.js";
|
|
15
16
|
import { unsafeLocal } from "./unsafe.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type DescField, type DescMessage } from "../descriptors.js";
|
|
2
2
|
import type { MessageShape } from "../types.js";
|
|
3
3
|
import type { MapEntryKey, ReflectList, ReflectMap, ReflectMessage } from "./reflect-types.js";
|
|
4
4
|
/**
|
|
@@ -11,12 +11,13 @@
|
|
|
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 "../descriptors.js";
|
|
14
15
|
import { checkField, checkListItem, checkMapEntry } from "./reflect-check.js";
|
|
15
16
|
import { FieldError } from "./error.js";
|
|
16
|
-
import {
|
|
17
|
+
import { unsafeClear, unsafeGet, unsafeIsSet, unsafeLocal, unsafeOneofCase, unsafeSet, } from "./unsafe.js";
|
|
17
18
|
import { create } from "../create.js";
|
|
18
19
|
import { isWrapper, isWrapperDesc } from "../wkt/wrappers.js";
|
|
19
|
-
import {
|
|
20
|
+
import { scalarZeroValue } from "./scalar.js";
|
|
20
21
|
import { protoInt64 } from "../proto-int64.js";
|
|
21
22
|
import { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
|
|
22
23
|
/**
|
|
@@ -37,6 +38,8 @@ class ReflectMessageImpl {
|
|
|
37
38
|
constructor(messageDesc, message,
|
|
38
39
|
// TODO either remove this option, or support it in reflect-list and reflect-map as well
|
|
39
40
|
check = true) {
|
|
41
|
+
this.lists = new Map();
|
|
42
|
+
this.maps = new Map();
|
|
40
43
|
this.check = check;
|
|
41
44
|
this.desc = messageDesc;
|
|
42
45
|
this.message = this[unsafeLocal] = message !== null && message !== void 0 ? message : create(messageDesc);
|
|
@@ -67,11 +70,19 @@ class ReflectMessageImpl {
|
|
|
67
70
|
let value = unsafeGet(this.message, field);
|
|
68
71
|
switch (field.fieldKind) {
|
|
69
72
|
case "list":
|
|
70
|
-
|
|
73
|
+
// eslint-disable-next-line no-case-declarations
|
|
74
|
+
let list = this.lists.get(field);
|
|
75
|
+
if (!list || list[unsafeLocal] !== value) {
|
|
76
|
+
this.lists.set(field, (list = new ReflectListImpl(field, value, this.check)));
|
|
77
|
+
}
|
|
78
|
+
return list;
|
|
71
79
|
case "map":
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
80
|
+
// eslint-disable-next-line no-case-declarations
|
|
81
|
+
let map = this.maps.get(field);
|
|
82
|
+
if (!map || map[unsafeLocal] !== value) {
|
|
83
|
+
this.maps.set(field, (map = new ReflectMapImpl(field, value, this.check)));
|
|
84
|
+
}
|
|
85
|
+
return map;
|
|
75
86
|
case "message":
|
|
76
87
|
if (value !== undefined &&
|
|
77
88
|
!field.oneof &&
|
|
@@ -84,7 +95,7 @@ class ReflectMessageImpl {
|
|
|
84
95
|
return new ReflectMessageImpl(field.message, value, this.check);
|
|
85
96
|
case "scalar":
|
|
86
97
|
return (value === undefined
|
|
87
|
-
? scalarZeroValue(field.scalar,
|
|
98
|
+
? scalarZeroValue(field.scalar, false)
|
|
88
99
|
: longToReflect(field, value));
|
|
89
100
|
case "enum":
|
|
90
101
|
return (value !== null && value !== void 0 ? value : field.enum.values[0].number);
|
|
@@ -98,7 +109,7 @@ class ReflectMessageImpl {
|
|
|
98
109
|
return err;
|
|
99
110
|
}
|
|
100
111
|
}
|
|
101
|
-
let local
|
|
112
|
+
let local;
|
|
102
113
|
if (isReflectMap(value) || isReflectList(value)) {
|
|
103
114
|
local = value[unsafeLocal];
|
|
104
115
|
}
|
|
@@ -112,30 +123,6 @@ class ReflectMessageImpl {
|
|
|
112
123
|
unsafeSet(this.message, field, local);
|
|
113
124
|
return undefined;
|
|
114
125
|
}
|
|
115
|
-
addListItem(field, value) {
|
|
116
|
-
assertOwn(this.message, field);
|
|
117
|
-
assertKind(field, "list");
|
|
118
|
-
if (this.check) {
|
|
119
|
-
if (checkListItem(field, 0, value)) {
|
|
120
|
-
const arr = unsafeGet(this.message, field);
|
|
121
|
-
return checkListItem(field, arr.length, value);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
unsafeAddListItem(this.message, field, listItemToLocal(field, value));
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
setMapEntry(field, key, value) {
|
|
128
|
-
assertOwn(this.message, field);
|
|
129
|
-
assertKind(field, "map");
|
|
130
|
-
if (this.check) {
|
|
131
|
-
const err = checkMapEntry(field, key, value);
|
|
132
|
-
if (err) {
|
|
133
|
-
return err;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
unsafeSetMapEntry(this.message, field, mapKeyToLocal(key), mapValueToLocal(field, value));
|
|
137
|
-
return undefined;
|
|
138
|
-
}
|
|
139
126
|
getUnknown() {
|
|
140
127
|
return this.message.$unknown;
|
|
141
128
|
}
|
|
@@ -143,11 +130,6 @@ class ReflectMessageImpl {
|
|
|
143
130
|
this.message.$unknown = value;
|
|
144
131
|
}
|
|
145
132
|
}
|
|
146
|
-
function assertKind(field, kind) {
|
|
147
|
-
if (field.fieldKind != kind) {
|
|
148
|
-
throw new FieldError(field, `${field.toString()} is ${field.fieldKind}`, "ForeignFieldError");
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
133
|
function assertOwn(owner, member) {
|
|
152
134
|
if (member.parent.typeName !== owner.$typeName) {
|
|
153
135
|
throw new FieldError(member, `cannot use ${member.toString()} with message ${owner.$typeName}`, "ForeignFieldError");
|
|
@@ -384,16 +366,16 @@ function longToReflect(field, value) {
|
|
|
384
366
|
case ScalarType.INT64:
|
|
385
367
|
case ScalarType.SFIXED64:
|
|
386
368
|
case ScalarType.SINT64:
|
|
387
|
-
if ("
|
|
388
|
-
field.
|
|
369
|
+
if ("longAsString" in field &&
|
|
370
|
+
field.longAsString &&
|
|
389
371
|
typeof value == "string") {
|
|
390
372
|
value = protoInt64.parse(value);
|
|
391
373
|
}
|
|
392
374
|
break;
|
|
393
375
|
case ScalarType.FIXED64:
|
|
394
376
|
case ScalarType.UINT64:
|
|
395
|
-
if ("
|
|
396
|
-
field.
|
|
377
|
+
if ("longAsString" in field &&
|
|
378
|
+
field.longAsString &&
|
|
397
379
|
typeof value == "string") {
|
|
398
380
|
value = protoInt64.uParse(value);
|
|
399
381
|
}
|
|
@@ -407,7 +389,7 @@ function longToLocal(field, value) {
|
|
|
407
389
|
case ScalarType.INT64:
|
|
408
390
|
case ScalarType.SFIXED64:
|
|
409
391
|
case ScalarType.SINT64:
|
|
410
|
-
if ("
|
|
392
|
+
if ("longAsString" in field && field.longAsString) {
|
|
411
393
|
value = String(value);
|
|
412
394
|
}
|
|
413
395
|
else if (typeof value == "string" || typeof value == "number") {
|
|
@@ -416,7 +398,7 @@ function longToLocal(field, value) {
|
|
|
416
398
|
break;
|
|
417
399
|
case ScalarType.FIXED64:
|
|
418
400
|
case ScalarType.UINT64:
|
|
419
|
-
if ("
|
|
401
|
+
if ("longAsString" in field && field.longAsString) {
|
|
420
402
|
value = String(value);
|
|
421
403
|
}
|
|
422
404
|
else if (typeof value == "string" || typeof value == "number") {
|
|
@@ -1,56 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
* Scalar value types. This is a subset of field types declared by protobuf
|
|
3
|
-
* enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
|
|
4
|
-
* are omitted, but the numerical values are identical.
|
|
5
|
-
*/
|
|
6
|
-
export declare enum ScalarType {
|
|
7
|
-
DOUBLE = 1,
|
|
8
|
-
FLOAT = 2,
|
|
9
|
-
INT64 = 3,
|
|
10
|
-
UINT64 = 4,
|
|
11
|
-
INT32 = 5,
|
|
12
|
-
FIXED64 = 6,
|
|
13
|
-
FIXED32 = 7,
|
|
14
|
-
BOOL = 8,
|
|
15
|
-
STRING = 9,
|
|
16
|
-
BYTES = 12,
|
|
17
|
-
UINT32 = 13,
|
|
18
|
-
SFIXED32 = 15,
|
|
19
|
-
SFIXED64 = 16,
|
|
20
|
-
SINT32 = 17,// Uses ZigZag encoding.
|
|
21
|
-
SINT64 = 18
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* JavaScript representation of fields with 64 bit integral types (int64, uint64,
|
|
25
|
-
* sint64, fixed64, sfixed64).
|
|
26
|
-
*
|
|
27
|
-
* This is a subset of google.protobuf.FieldOptions.JSType, which defines JS_NORMAL,
|
|
28
|
-
* JS_STRING, and JS_NUMBER. Protobuf-ES uses BigInt by default, but will use
|
|
29
|
-
* String if `[jstype = JS_STRING]` is specified.
|
|
30
|
-
*
|
|
31
|
-
* ```protobuf
|
|
32
|
-
* uint64 field_a = 1; // BigInt
|
|
33
|
-
* uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
|
|
34
|
-
* uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
|
|
35
|
-
* uint64 field_b = 2 [jstype = JS_STRING]; // String
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
export declare enum LongType {
|
|
39
|
-
/**
|
|
40
|
-
* Use JavaScript BigInt.
|
|
41
|
-
*/
|
|
42
|
-
BIGINT = 0,
|
|
43
|
-
/**
|
|
44
|
-
* Use JavaScript String.
|
|
45
|
-
*
|
|
46
|
-
* Field option `[jstype = JS_STRING]`.
|
|
47
|
-
*/
|
|
48
|
-
STRING = 1
|
|
49
|
-
}
|
|
1
|
+
import { ScalarType } from "../descriptors.js";
|
|
50
2
|
/**
|
|
51
3
|
* ScalarValue maps from a scalar field type to a TypeScript value type.
|
|
52
4
|
*/
|
|
53
|
-
export type ScalarValue<T = ScalarType,
|
|
5
|
+
export type ScalarValue<T = ScalarType, LongAsString extends boolean = false> = T extends ScalarType.STRING ? string : T extends ScalarType.INT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.SINT32 ? number : T extends ScalarType.FIXED32 ? number : T extends ScalarType.SFIXED32 ? number : T extends ScalarType.FLOAT ? number : T extends ScalarType.DOUBLE ? number : T extends ScalarType.INT64 ? LongAsString extends true ? string : bigint : T extends ScalarType.SINT64 ? LongAsString extends true ? string : bigint : T extends ScalarType.SFIXED64 ? LongAsString extends true ? string : bigint : T extends ScalarType.UINT64 ? LongAsString extends true ? string : bigint : T extends ScalarType.FIXED64 ? LongAsString extends true ? string : bigint : T extends ScalarType.BOOL ? boolean : T extends ScalarType.BYTES ? Uint8Array : never;
|
|
54
6
|
/**
|
|
55
7
|
* Returns true if both scalar values are equal.
|
|
56
8
|
*/
|
|
@@ -58,7 +10,7 @@ export declare function scalarEquals(type: ScalarType, a: string | boolean | num
|
|
|
58
10
|
/**
|
|
59
11
|
* Returns the zero value for the given scalar type.
|
|
60
12
|
*/
|
|
61
|
-
export declare function scalarZeroValue<T extends ScalarType,
|
|
13
|
+
export declare function scalarZeroValue<T extends ScalarType, LongAsString extends boolean>(type: T, longAsString: LongAsString): ScalarValue<T, LongAsString>;
|
|
62
14
|
/**
|
|
63
15
|
* Returns true for a zero-value. For example, an integer has the zero-value `0`,
|
|
64
16
|
* a boolean is `false`, a string is `""`, and bytes is an empty Uint8Array.
|
|
@@ -67,7 +19,3 @@ export declare function scalarZeroValue<T extends ScalarType, L extends LongType
|
|
|
67
19
|
* optional or repeated.
|
|
68
20
|
*/
|
|
69
21
|
export declare function isScalarZeroValue(type: ScalarType, value: unknown): boolean;
|
|
70
|
-
export type InvalidScalarValueErr = false | "invalid UTF8" | `${string} out of range`;
|
|
71
|
-
export declare function checkScalarValue(value: unknown, scalar: ScalarType): true | InvalidScalarValueErr;
|
|
72
|
-
export declare function scalarTypeDescription(scalar: ScalarType): string;
|
|
73
|
-
export declare function scalarTypeScriptType(scalar: ScalarType, long: LongType): "string" | "boolean" | "bigint" | "bigint | string" | "Uint8Array" | "number";
|