@bufbuild/protobuf 0.0.1-alpha.1 → 0.0.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 +21 -1
- package/dist/cjs/binary-encoding.js +402 -410
- package/dist/cjs/descriptor-registry.js +407 -449
- package/dist/cjs/descriptor-set.js +416 -513
- package/dist/cjs/field.js +30 -30
- package/dist/cjs/google/protobuf/any_pb.js +122 -140
- package/dist/cjs/google/protobuf/api_pb.js +177 -224
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +125 -227
- package/dist/cjs/google/protobuf/descriptor_pb.js +1029 -1888
- package/dist/cjs/google/protobuf/duration_pb.js +72 -88
- package/dist/cjs/google/protobuf/empty_pb.js +17 -17
- package/dist/cjs/google/protobuf/field_mask_pb.js +77 -94
- package/dist/cjs/google/protobuf/source_context_pb.js +25 -25
- package/dist/cjs/google/protobuf/struct_pb.js +144 -181
- package/dist/cjs/google/protobuf/timestamp_pb.js +86 -123
- package/dist/cjs/google/protobuf/type_pb.js +406 -462
- package/dist/cjs/google/protobuf/wrappers_pb.js +407 -470
- package/dist/cjs/index.js +24 -122
- package/dist/cjs/message.js +81 -89
- package/dist/cjs/private/assert.js +20 -26
- package/dist/cjs/private/base64.js +79 -78
- package/dist/cjs/private/binary-format-common.js +195 -208
- package/dist/cjs/private/binary-format-proto2.js +75 -109
- package/dist/cjs/private/binary-format-proto3.js +57 -90
- package/dist/cjs/private/enum.js +38 -43
- package/dist/cjs/private/field-list.js +51 -50
- package/dist/cjs/private/field-wrapper.js +8 -10
- package/dist/cjs/private/field.js +22 -25
- package/dist/cjs/private/goog-varint.js +275 -0
- package/dist/cjs/private/json-format-common.js +414 -440
- package/dist/cjs/private/json-format-proto2.js +78 -89
- package/dist/cjs/private/json-format-proto3.js +69 -93
- package/dist/cjs/private/message-type.js +26 -27
- package/dist/cjs/private/names.js +58 -62
- package/dist/cjs/private/proto-runtime.js +11 -16
- package/dist/cjs/private/scalars.js +100 -103
- package/dist/cjs/private/util-common.js +180 -210
- package/dist/cjs/proto-int64.js +111 -112
- package/dist/cjs/proto2.js +50 -63
- package/dist/cjs/proto3.js +61 -74
- package/dist/cjs/service-type.js +14 -17
- package/dist/cjs/type-registry.js +33 -31
- package/dist/esm/binary-encoding.js +401 -414
- package/dist/esm/descriptor-registry.js +409 -458
- package/dist/esm/descriptor-set.js +417 -478
- package/dist/esm/field.js +29 -29
- package/dist/esm/google/protobuf/any_pb.js +122 -140
- package/dist/esm/google/protobuf/api_pb.js +177 -197
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +123 -216
- package/dist/esm/google/protobuf/descriptor_pb.js +1013 -1804
- package/dist/esm/google/protobuf/duration_pb.js +72 -88
- package/dist/esm/google/protobuf/empty_pb.js +17 -17
- package/dist/esm/google/protobuf/field_mask_pb.js +77 -94
- package/dist/esm/google/protobuf/source_context_pb.js +25 -25
- package/dist/esm/google/protobuf/struct_pb.js +143 -180
- package/dist/esm/google/protobuf/timestamp_pb.js +86 -123
- package/dist/esm/google/protobuf/type_pb.js +401 -421
- package/dist/esm/google/protobuf/wrappers_pb.js +406 -397
- package/dist/esm/index.js +1 -1
- package/dist/esm/message.js +81 -89
- package/dist/esm/private/assert.js +19 -21
- package/dist/esm/private/base64.js +79 -78
- package/dist/esm/private/binary-format-common.js +196 -207
- package/dist/esm/private/binary-format-proto2.js +76 -81
- package/dist/esm/private/binary-format-proto3.js +58 -67
- package/dist/esm/private/enum.js +38 -43
- package/dist/esm/private/field-list.js +51 -50
- package/dist/esm/private/field-wrapper.js +8 -10
- package/dist/esm/private/field.js +22 -22
- package/dist/esm/private/goog-varint.js +266 -0
- package/dist/esm/private/json-format-common.js +414 -435
- package/dist/esm/private/json-format-proto2.js +76 -81
- package/dist/esm/private/json-format-proto3.js +66 -80
- package/dist/esm/private/message-type.js +27 -28
- package/dist/esm/private/names.js +57 -57
- package/dist/esm/private/proto-runtime.js +11 -11
- package/dist/esm/private/scalars.js +99 -99
- package/dist/esm/private/util-common.js +180 -192
- package/dist/esm/proto-int64.js +111 -112
- package/dist/esm/proto2.js +50 -57
- package/dist/esm/proto3.js +61 -68
- package/dist/esm/service-type.js +12 -12
- package/dist/esm/type-registry.js +33 -31
- package/dist/types/binary-encoding.d.ts +398 -398
- package/dist/types/binary-format.d.ts +69 -91
- package/dist/types/descriptor-registry.d.ts +23 -29
- package/dist/types/descriptor-set.d.ts +107 -128
- package/dist/types/enum.d.ts +21 -21
- package/dist/types/field-list.d.ts +21 -21
- package/dist/types/field.d.ts +191 -231
- package/dist/types/google/protobuf/any_pb.d.ts +56 -77
- package/dist/types/google/protobuf/api_pb.d.ts +144 -187
- package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +195 -257
- package/dist/types/google/protobuf/descriptor_pb.d.ts +1360 -1737
- package/dist/types/google/protobuf/duration_pb.d.ts +30 -50
- package/dist/types/google/protobuf/empty_pb.d.ts +9 -28
- package/dist/types/google/protobuf/field_mask_pb.d.ts +17 -37
- package/dist/types/google/protobuf/source_context_pb.d.ts +16 -35
- package/dist/types/google/protobuf/struct_pb.d.ts +64 -115
- package/dist/types/google/protobuf/timestamp_pb.d.ts +31 -51
- package/dist/types/google/protobuf/type_pb.d.ts +355 -422
- package/dist/types/google/protobuf/wrappers_pb.d.ts +181 -297
- package/dist/types/index.d.ts +4 -27
- package/dist/types/json-format.d.ts +70 -89
- package/dist/types/message-type.d.ts +38 -46
- package/dist/types/message.d.ts +73 -99
- package/dist/types/private/assert.d.ts +1 -4
- package/dist/types/private/binary-format-common.d.ts +6 -32
- package/dist/types/private/enum.d.ts +4 -11
- package/dist/types/private/field-list.d.ts +14 -21
- package/dist/types/private/field-wrapper.d.ts +5 -11
- package/dist/types/private/field.d.ts +12 -12
- package/dist/types/{google/varint.d.ts → private/goog-varint.d.ts} +6 -13
- package/dist/types/private/json-format-common.d.ts +5 -27
- package/dist/types/private/message-type.d.ts +3 -8
- package/dist/types/private/names.d.ts +1 -4
- package/dist/types/private/options-map.d.ts +1 -1
- package/dist/types/private/proto-runtime.d.ts +29 -42
- package/dist/types/private/scalars.d.ts +5 -12
- package/dist/types/private/util-common.d.ts +1 -4
- package/dist/types/private/util.d.ts +29 -41
- package/dist/types/proto-int64.d.ts +42 -42
- package/dist/types/service-type.d.ts +37 -57
- package/dist/types/type-registry.d.ts +13 -15
- package/package.json +2 -2
- package/dist/cjs/google/varint.js +0 -280
- package/dist/esm/google/varint.js +0 -265
|
@@ -6,468 +6,447 @@ import { base64decode, base64encode } from "./base64.js";
|
|
|
6
6
|
/* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
|
|
7
7
|
// Default options for parsing JSON.
|
|
8
8
|
const jsonReadDefaults = {
|
|
9
|
-
|
|
9
|
+
ignoreUnknownFields: false,
|
|
10
10
|
};
|
|
11
11
|
// Default options for serializing to JSON.
|
|
12
12
|
const jsonWriteDefaults = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
emitDefaultValues: false,
|
|
14
|
+
enumAsInteger: false,
|
|
15
|
+
useProtoFieldName: false,
|
|
16
|
+
prettySpaces: 0,
|
|
17
17
|
};
|
|
18
18
|
function makeReadOptions(options) {
|
|
19
|
-
|
|
19
|
+
return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
|
|
20
20
|
}
|
|
21
21
|
function makeWriteOptions(options) {
|
|
22
|
-
|
|
22
|
+
return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
|
|
23
23
|
}
|
|
24
24
|
export function makeJsonFormatCommon(makeWriteField) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
`cannot decode message ${type.typeName} from JSON: ${this.debug(
|
|
33
|
-
json
|
|
34
|
-
)}`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
message = message ?? new type();
|
|
38
|
-
const oneofSeen = {};
|
|
39
|
-
for (const [jsonKey, jsonValue] of Object.entries(json)) {
|
|
40
|
-
const field = type.fields.findJsonName(jsonKey);
|
|
41
|
-
if (!field) {
|
|
42
|
-
if (!options.ignoreUnknownFields) {
|
|
43
|
-
throw new Error(
|
|
44
|
-
`cannot decode message ${type.typeName} from JSON: key "${jsonKey}" is unknown`
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
let localName = field.localName;
|
|
50
|
-
let target = message;
|
|
51
|
-
if (field.oneof) {
|
|
52
|
-
if (jsonValue === null && field.kind == "scalar") {
|
|
53
|
-
// see conformance test Required.Proto3.JsonInput.OneofFieldNull{First,Second}
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
const seen = oneofSeen[field.oneof.localName];
|
|
57
|
-
if (seen) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`cannot decode message ${type.typeName} from JSON: multiple keys for oneof "${field.oneof.name}" present: "${seen}", "${jsonKey}"`
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
oneofSeen[field.oneof.localName] = jsonKey;
|
|
63
|
-
target = target[field.oneof.localName] = { case: localName };
|
|
64
|
-
localName = "value";
|
|
65
|
-
}
|
|
66
|
-
if (field.repeated) {
|
|
67
|
-
if (jsonValue === null) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (!Array.isArray(jsonValue)) {
|
|
71
|
-
throw new Error(
|
|
72
|
-
`cannot decode field ${type.typeName}.${
|
|
73
|
-
field.name
|
|
74
|
-
} from JSON: "${this.debug(jsonValue)}"`
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
const targetArray = target[localName];
|
|
78
|
-
for (const jsonItem of jsonValue) {
|
|
79
|
-
if (jsonItem === null) {
|
|
80
|
-
throw new Error(
|
|
81
|
-
`cannot decode field ${type.typeName}.${
|
|
82
|
-
field.name
|
|
83
|
-
} from JSON: "${this.debug(jsonItem)}"`
|
|
84
|
-
);
|
|
25
|
+
const writeField = makeWriteField(writeEnum, writeScalar);
|
|
26
|
+
return {
|
|
27
|
+
makeReadOptions,
|
|
28
|
+
makeWriteOptions,
|
|
29
|
+
readMessage(type, json, options, message) {
|
|
30
|
+
if (json == null || Array.isArray(json) || typeof json != "object") {
|
|
31
|
+
throw new Error(`cannot decode message ${type.typeName} from JSON: ${this.debug(json)}`);
|
|
85
32
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
break;
|
|
96
|
-
case "scalar":
|
|
97
|
-
try {
|
|
98
|
-
val = readScalar(field.T, jsonItem);
|
|
99
|
-
} catch (e) {
|
|
100
|
-
let m = `cannot decode field ${type.typeName}.${
|
|
101
|
-
field.name
|
|
102
|
-
} from JSON: "${this.debug(jsonItem)}"`;
|
|
103
|
-
if (e instanceof Error && e.message.length > 0) {
|
|
104
|
-
m += `: ${e.message}`;
|
|
105
|
-
}
|
|
106
|
-
throw new Error(m);
|
|
33
|
+
message = message ?? new type();
|
|
34
|
+
const oneofSeen = {};
|
|
35
|
+
for (const [jsonKey, jsonValue] of Object.entries(json)) {
|
|
36
|
+
const field = type.fields.findJsonName(jsonKey);
|
|
37
|
+
if (!field) {
|
|
38
|
+
if (!options.ignoreUnknownFields) {
|
|
39
|
+
throw new Error(`cannot decode message ${type.typeName} from JSON: key "${jsonKey}" is unknown`);
|
|
40
|
+
}
|
|
41
|
+
continue;
|
|
107
42
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
43
|
+
let localName = field.localName;
|
|
44
|
+
let target = message;
|
|
45
|
+
if (field.oneof) {
|
|
46
|
+
if (jsonValue === null && field.kind == "scalar") {
|
|
47
|
+
// see conformance test Required.Proto3.JsonInput.OneofFieldNull{First,Second}
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const seen = oneofSeen[field.oneof.localName];
|
|
51
|
+
if (seen) {
|
|
52
|
+
throw new Error(`cannot decode message ${type.typeName} from JSON: multiple keys for oneof "${field.oneof.name}" present: "${seen}", "${jsonKey}"`);
|
|
53
|
+
}
|
|
54
|
+
oneofSeen[field.oneof.localName] = jsonKey;
|
|
55
|
+
target = target[field.oneof.localName] = { case: localName };
|
|
56
|
+
localName = "value";
|
|
57
|
+
}
|
|
58
|
+
if (field.repeated) {
|
|
59
|
+
if (jsonValue === null) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!Array.isArray(jsonValue)) {
|
|
63
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`);
|
|
64
|
+
}
|
|
65
|
+
const targetArray = target[localName];
|
|
66
|
+
for (const jsonItem of jsonValue) {
|
|
67
|
+
if (jsonItem === null) {
|
|
68
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonItem)}"`);
|
|
69
|
+
}
|
|
70
|
+
let val;
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- "map" is invalid for repeated fields
|
|
72
|
+
switch (field.kind) {
|
|
73
|
+
case "message":
|
|
74
|
+
val = field.T.fromJson(jsonItem, options);
|
|
75
|
+
break;
|
|
76
|
+
case "enum":
|
|
77
|
+
val = readEnum(field.T, jsonItem, options.ignoreUnknownFields);
|
|
78
|
+
if (val === undefined)
|
|
79
|
+
continue;
|
|
80
|
+
break;
|
|
81
|
+
case "scalar":
|
|
82
|
+
try {
|
|
83
|
+
val = readScalar(field.T, jsonItem);
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
let m = `cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonItem)}"`;
|
|
87
|
+
if (e instanceof Error && e.message.length > 0) {
|
|
88
|
+
m += `: ${e.message}`;
|
|
89
|
+
}
|
|
90
|
+
throw new Error(m);
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
targetArray.push(val);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (field.kind == "map") {
|
|
98
|
+
if (jsonValue === null) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (Array.isArray(jsonValue) || typeof jsonValue != "object") {
|
|
102
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: ${this.debug(jsonValue)}`);
|
|
103
|
+
}
|
|
104
|
+
const targetMap = target[localName];
|
|
105
|
+
for (const [jsonMapKey, jsonMapValue] of Object.entries(jsonValue)) {
|
|
106
|
+
if (jsonMapValue === null) {
|
|
107
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: map value null`);
|
|
108
|
+
}
|
|
109
|
+
let val;
|
|
110
|
+
switch (field.V.kind) {
|
|
111
|
+
case "message":
|
|
112
|
+
val = field.V.T.fromJson(jsonMapValue, options);
|
|
113
|
+
break;
|
|
114
|
+
case "enum":
|
|
115
|
+
val = readEnum(field.V.T, jsonMapValue, options.ignoreUnknownFields);
|
|
116
|
+
if (val === undefined)
|
|
117
|
+
continue;
|
|
118
|
+
break;
|
|
119
|
+
case "scalar":
|
|
120
|
+
try {
|
|
121
|
+
val = readScalar(field.V.T, jsonMapValue);
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
let m = `cannot decode map value for field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
|
|
125
|
+
if (e instanceof Error && e.message.length > 0) {
|
|
126
|
+
m += `: ${e.message}`;
|
|
127
|
+
}
|
|
128
|
+
throw new Error(m);
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
targetMap[readScalar(field.K, field.K == ScalarType.BOOL
|
|
134
|
+
? jsonMapKey == "true"
|
|
135
|
+
? true
|
|
136
|
+
: jsonMapKey == "false"
|
|
137
|
+
? false
|
|
138
|
+
: jsonMapKey
|
|
139
|
+
: jsonMapKey).toString()] = val;
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
let m = `cannot decode map key for field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
|
|
143
|
+
if (e instanceof Error && e.message.length > 0) {
|
|
144
|
+
m += `: ${e.message}`;
|
|
145
|
+
}
|
|
146
|
+
throw new Error(m);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
switch (field.kind) {
|
|
152
|
+
case "message":
|
|
153
|
+
const messageType = field.T;
|
|
154
|
+
if (jsonValue === null &&
|
|
155
|
+
messageType.typeName != "google.protobuf.Value") {
|
|
156
|
+
if (field.oneof) {
|
|
157
|
+
throw new Error(`cannot decode field ${type.typeName}.${field.name} from JSON: null is invalid for oneof field "${jsonKey}"`);
|
|
158
|
+
}
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const targetMessage = target[localName] === undefined
|
|
162
|
+
? new messageType()
|
|
163
|
+
: wrapField(messageType, target[localName]);
|
|
164
|
+
target[localName] = unwrapField(messageType, targetMessage.fromJson(jsonValue, options));
|
|
165
|
+
break;
|
|
166
|
+
case "enum":
|
|
167
|
+
const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields);
|
|
168
|
+
if (enumValue !== undefined) {
|
|
169
|
+
target[localName] = enumValue;
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
case "scalar":
|
|
173
|
+
try {
|
|
174
|
+
target[localName] = readScalar(field.T, jsonValue);
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
let m = `cannot decode field ${type.typeName}.${field.name} from JSON: "${this.debug(jsonValue)}"`;
|
|
178
|
+
if (e instanceof Error && e.message.length > 0) {
|
|
179
|
+
m += `: ${e.message}`;
|
|
180
|
+
}
|
|
181
|
+
throw new Error(m);
|
|
182
|
+
}
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
154
185
|
}
|
|
155
|
-
break;
|
|
156
186
|
}
|
|
187
|
+
return message;
|
|
188
|
+
},
|
|
189
|
+
writeMessage(message, options) {
|
|
190
|
+
const type = message.getType();
|
|
191
|
+
const json = {};
|
|
192
|
+
let field;
|
|
157
193
|
try {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
} else {
|
|
181
|
-
switch (field.kind) {
|
|
182
|
-
case "message":
|
|
183
|
-
const messageType = field.T;
|
|
184
|
-
if (
|
|
185
|
-
jsonValue === null &&
|
|
186
|
-
messageType.typeName != "google.protobuf.Value"
|
|
187
|
-
) {
|
|
188
|
-
if (field.oneof) {
|
|
189
|
-
throw new Error(
|
|
190
|
-
`cannot decode field ${type.typeName}.${field.name} from JSON: null is invalid for oneof field "${jsonKey}"`
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
const targetMessage =
|
|
196
|
-
target[localName] === undefined
|
|
197
|
-
? new messageType()
|
|
198
|
-
: wrapField(messageType, target[localName]);
|
|
199
|
-
target[localName] = unwrapField(
|
|
200
|
-
messageType,
|
|
201
|
-
targetMessage.fromJson(jsonValue, options)
|
|
202
|
-
);
|
|
203
|
-
break;
|
|
204
|
-
case "enum":
|
|
205
|
-
const enumValue = readEnum(
|
|
206
|
-
field.T,
|
|
207
|
-
jsonValue,
|
|
208
|
-
options.ignoreUnknownFields
|
|
209
|
-
);
|
|
210
|
-
if (enumValue !== undefined) {
|
|
211
|
-
target[localName] = enumValue;
|
|
212
|
-
}
|
|
213
|
-
break;
|
|
214
|
-
case "scalar":
|
|
215
|
-
try {
|
|
216
|
-
target[localName] = readScalar(field.T, jsonValue);
|
|
217
|
-
} catch (e) {
|
|
218
|
-
let m = `cannot decode field ${type.typeName}.${
|
|
219
|
-
field.name
|
|
220
|
-
} from JSON: "${this.debug(jsonValue)}"`;
|
|
221
|
-
if (e instanceof Error && e.message.length > 0) {
|
|
222
|
-
m += `: ${e.message}`;
|
|
194
|
+
for (const member of type.fields.byMember()) {
|
|
195
|
+
let jsonValue;
|
|
196
|
+
if (member.kind == "oneof") {
|
|
197
|
+
const oneof = message[member.localName];
|
|
198
|
+
if (oneof.value === undefined) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
field = member.findField(oneof.case);
|
|
202
|
+
if (!field) {
|
|
203
|
+
throw "oneof case not found: " + oneof.case;
|
|
204
|
+
}
|
|
205
|
+
jsonValue = writeField(field, oneof.value, options);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
field = member;
|
|
209
|
+
jsonValue = writeField(field, message[field.localName], options);
|
|
210
|
+
}
|
|
211
|
+
if (jsonValue !== undefined) {
|
|
212
|
+
json[options.useProtoFieldName ? field.name : field.jsonName] =
|
|
213
|
+
jsonValue;
|
|
214
|
+
}
|
|
223
215
|
}
|
|
224
|
-
throw new Error(m);
|
|
225
|
-
}
|
|
226
|
-
break;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return message;
|
|
231
|
-
},
|
|
232
|
-
writeMessage(message, options) {
|
|
233
|
-
const type = message.getType();
|
|
234
|
-
const json = {};
|
|
235
|
-
let field;
|
|
236
|
-
try {
|
|
237
|
-
for (const member of type.fields.byMember()) {
|
|
238
|
-
let jsonValue;
|
|
239
|
-
if (member.kind == "oneof") {
|
|
240
|
-
const oneof = message[member.localName];
|
|
241
|
-
if (oneof.value === undefined) {
|
|
242
|
-
continue;
|
|
243
216
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
217
|
+
catch (e) {
|
|
218
|
+
const m = field
|
|
219
|
+
? `cannot encode field ${type.typeName}.${field.name} to JSON`
|
|
220
|
+
: `cannot encode message ${type.typeName} to JSON`;
|
|
221
|
+
const r = e instanceof Error ? e.message : String(e);
|
|
222
|
+
throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
|
|
247
223
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
json[options.useProtoFieldName ? field.name : field.jsonName] =
|
|
255
|
-
jsonValue;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
} catch (e) {
|
|
259
|
-
const m = field
|
|
260
|
-
? `cannot encode field ${type.typeName}.${field.name} to JSON`
|
|
261
|
-
: `cannot encode message ${type.typeName} to JSON`;
|
|
262
|
-
const r = e instanceof Error ? e.message : String(e);
|
|
263
|
-
throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
|
|
264
|
-
}
|
|
265
|
-
return json;
|
|
266
|
-
},
|
|
267
|
-
readScalar,
|
|
268
|
-
writeScalar,
|
|
269
|
-
debug: debugJsonValue,
|
|
270
|
-
};
|
|
224
|
+
return json;
|
|
225
|
+
},
|
|
226
|
+
readScalar,
|
|
227
|
+
writeScalar,
|
|
228
|
+
debug: debugJsonValue,
|
|
229
|
+
};
|
|
271
230
|
}
|
|
272
231
|
function debugJsonValue(json) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
232
|
+
if (json === null) {
|
|
233
|
+
return "null";
|
|
234
|
+
}
|
|
235
|
+
switch (typeof json) {
|
|
236
|
+
case "object":
|
|
237
|
+
return Array.isArray(json) ? "array" : "object";
|
|
238
|
+
case "string":
|
|
239
|
+
return json.length > 100 ? "string" : `"${json.split('"').join('\\"')}"`;
|
|
240
|
+
default:
|
|
241
|
+
return json.toString();
|
|
242
|
+
}
|
|
284
243
|
}
|
|
285
244
|
// May throw an error. If the error message is non-blank, it should be shown.
|
|
286
245
|
// It is up to the caller to provide context.
|
|
287
246
|
function readScalar(type, json) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
247
|
+
// every valid case in the switch below returns, and every fall
|
|
248
|
+
// through is regarded as a failure.
|
|
249
|
+
switch (type) {
|
|
250
|
+
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
251
|
+
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
252
|
+
case ScalarType.DOUBLE:
|
|
253
|
+
case ScalarType.FLOAT:
|
|
254
|
+
if (json === null)
|
|
255
|
+
return 0.0;
|
|
256
|
+
if (json === "NaN")
|
|
257
|
+
return Number.NaN;
|
|
258
|
+
if (json === "Infinity")
|
|
259
|
+
return Number.POSITIVE_INFINITY;
|
|
260
|
+
if (json === "-Infinity")
|
|
261
|
+
return Number.NEGATIVE_INFINITY;
|
|
262
|
+
if (json === "") {
|
|
263
|
+
// empty string is not a number
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
if (typeof json == "string" && json.trim().length !== json.length) {
|
|
267
|
+
// extra whitespace
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
if (typeof json != "string" && typeof json != "number") {
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
const float = Number(json);
|
|
274
|
+
if (Number.isNaN(float)) {
|
|
275
|
+
// not a number
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
if (!Number.isFinite(float)) {
|
|
279
|
+
// infinity and -infinity are handled by string representation above, so this is an error
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
if (type == ScalarType.FLOAT)
|
|
283
|
+
assertFloat32(float);
|
|
284
|
+
return float;
|
|
285
|
+
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
286
|
+
case ScalarType.INT32:
|
|
287
|
+
case ScalarType.FIXED32:
|
|
288
|
+
case ScalarType.SFIXED32:
|
|
289
|
+
case ScalarType.SINT32:
|
|
290
|
+
case ScalarType.UINT32:
|
|
291
|
+
if (json === null)
|
|
292
|
+
return 0;
|
|
293
|
+
let int32;
|
|
294
|
+
if (typeof json == "number")
|
|
295
|
+
int32 = json;
|
|
296
|
+
else if (typeof json == "string" && json.length > 0) {
|
|
297
|
+
if (json.trim().length === json.length)
|
|
298
|
+
int32 = Number(json);
|
|
299
|
+
}
|
|
300
|
+
if (int32 === undefined)
|
|
301
|
+
break;
|
|
302
|
+
if (type == ScalarType.UINT32)
|
|
303
|
+
assertUInt32(int32);
|
|
304
|
+
else
|
|
305
|
+
assertInt32(int32);
|
|
306
|
+
return int32;
|
|
307
|
+
// int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
308
|
+
case ScalarType.INT64:
|
|
309
|
+
case ScalarType.SFIXED64:
|
|
310
|
+
case ScalarType.SINT64:
|
|
311
|
+
if (json === null)
|
|
312
|
+
return protoInt64.zero;
|
|
313
|
+
if (typeof json != "number" && typeof json != "string")
|
|
314
|
+
break;
|
|
315
|
+
return protoInt64.parse(json);
|
|
316
|
+
case ScalarType.FIXED64:
|
|
317
|
+
case ScalarType.UINT64:
|
|
318
|
+
if (json === null)
|
|
319
|
+
return protoInt64.zero;
|
|
320
|
+
if (typeof json != "number" && typeof json != "string")
|
|
321
|
+
break;
|
|
322
|
+
return protoInt64.uParse(json);
|
|
323
|
+
// bool:
|
|
324
|
+
case ScalarType.BOOL:
|
|
325
|
+
if (json === null)
|
|
326
|
+
return false;
|
|
327
|
+
if (typeof json !== "boolean")
|
|
328
|
+
break;
|
|
329
|
+
return json;
|
|
330
|
+
// string:
|
|
331
|
+
case ScalarType.STRING:
|
|
332
|
+
if (json === null)
|
|
333
|
+
return "";
|
|
334
|
+
if (typeof json !== "string") {
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
// A string must always contain UTF-8 encoded or 7-bit ASCII.
|
|
338
|
+
// We validate with encodeURIComponent, which appears to be the fastest widely available option.
|
|
339
|
+
try {
|
|
340
|
+
encodeURIComponent(json);
|
|
341
|
+
}
|
|
342
|
+
catch (e) {
|
|
343
|
+
throw new Error("invalid UTF8");
|
|
344
|
+
}
|
|
345
|
+
return json;
|
|
346
|
+
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
347
|
+
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
348
|
+
case ScalarType.BYTES:
|
|
349
|
+
if (json === null || json === "")
|
|
350
|
+
return new Uint8Array(0);
|
|
351
|
+
if (typeof json !== "string")
|
|
352
|
+
break;
|
|
353
|
+
return base64decode(json);
|
|
354
|
+
}
|
|
355
|
+
throw new Error();
|
|
376
356
|
}
|
|
377
357
|
function readEnum(type, json, ignoreUnknownFields) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`
|
|
398
|
-
);
|
|
358
|
+
if (json === null) {
|
|
359
|
+
// proto3 requires 0 to be default value for all enums
|
|
360
|
+
return 0;
|
|
361
|
+
}
|
|
362
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
363
|
+
switch (typeof json) {
|
|
364
|
+
case "number":
|
|
365
|
+
if (Number.isInteger(json)) {
|
|
366
|
+
return json;
|
|
367
|
+
}
|
|
368
|
+
break;
|
|
369
|
+
case "string":
|
|
370
|
+
const value = type.findName(json);
|
|
371
|
+
if (value || ignoreUnknownFields) {
|
|
372
|
+
return value?.no;
|
|
373
|
+
}
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
throw new Error(`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`);
|
|
399
377
|
}
|
|
400
378
|
function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
379
|
+
if (value === undefined) {
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
if (value === 0 && !emitIntrinsicDefault) {
|
|
383
|
+
// proto3 requires 0 to be default value for all enums
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
if (enumAsInteger) {
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
if (type.typeName == "google.protobuf.NullValue") {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
const val = type.findNumber(value);
|
|
393
|
+
return val?.name ?? value; // if we don't know the enum value, just return the number
|
|
416
394
|
}
|
|
417
395
|
function writeScalar(type, value, emitIntrinsicDefault) {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
396
|
+
if (value === undefined) {
|
|
397
|
+
return undefined;
|
|
398
|
+
}
|
|
399
|
+
switch (type) {
|
|
400
|
+
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
401
|
+
case ScalarType.INT32:
|
|
402
|
+
case ScalarType.SFIXED32:
|
|
403
|
+
case ScalarType.SINT32:
|
|
404
|
+
case ScalarType.FIXED32:
|
|
405
|
+
case ScalarType.UINT32:
|
|
406
|
+
assert(typeof value == "number");
|
|
407
|
+
return value != 0 || emitIntrinsicDefault ? value : undefined;
|
|
408
|
+
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
409
|
+
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
410
|
+
case ScalarType.FLOAT:
|
|
411
|
+
// assertFloat32(value);
|
|
412
|
+
case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
|
|
413
|
+
assert(typeof value == "number");
|
|
414
|
+
if (Number.isNaN(value))
|
|
415
|
+
return "NaN";
|
|
416
|
+
if (value === Number.POSITIVE_INFINITY)
|
|
417
|
+
return "Infinity";
|
|
418
|
+
if (value === Number.NEGATIVE_INFINITY)
|
|
419
|
+
return "-Infinity";
|
|
420
|
+
return value !== 0 || emitIntrinsicDefault ? value : undefined;
|
|
421
|
+
// string:
|
|
422
|
+
case ScalarType.STRING:
|
|
423
|
+
assert(typeof value == "string");
|
|
424
|
+
return value.length > 0 || emitIntrinsicDefault ? value : undefined;
|
|
425
|
+
// bool:
|
|
426
|
+
case ScalarType.BOOL:
|
|
427
|
+
assert(typeof value == "boolean");
|
|
428
|
+
return value || emitIntrinsicDefault ? value : undefined;
|
|
429
|
+
// JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
430
|
+
case ScalarType.UINT64:
|
|
431
|
+
case ScalarType.FIXED64:
|
|
432
|
+
case ScalarType.INT64:
|
|
433
|
+
case ScalarType.SFIXED64:
|
|
434
|
+
case ScalarType.SINT64:
|
|
435
|
+
assert(typeof value == "bigint" ||
|
|
436
|
+
typeof value == "string" ||
|
|
437
|
+
typeof value == "number");
|
|
438
|
+
// We use implicit conversion with `value != 0` to catch both 0n and "0"
|
|
439
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
440
|
+
// @ts-ignore
|
|
441
|
+
return emitIntrinsicDefault || value != 0
|
|
442
|
+
? value.toString(10)
|
|
443
|
+
: undefined;
|
|
444
|
+
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
445
|
+
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
446
|
+
case ScalarType.BYTES:
|
|
447
|
+
assert(value instanceof Uint8Array);
|
|
448
|
+
return emitIntrinsicDefault || value.byteLength > 0
|
|
449
|
+
? base64encode(value)
|
|
450
|
+
: undefined;
|
|
451
|
+
}
|
|
473
452
|
}
|