@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,473 @@
|
|
|
1
|
+
import { unwrapField, wrapField } from "./field-wrapper.js";
|
|
2
|
+
import { ScalarType } from "../field.js";
|
|
3
|
+
import { assert, assertFloat32, assertInt32, assertUInt32 } from "./assert.js";
|
|
4
|
+
import { protoInt64 } from "../proto-int64.js";
|
|
5
|
+
import { base64decode, base64encode } from "./base64.js";
|
|
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
|
+
// Default options for parsing JSON.
|
|
8
|
+
const jsonReadDefaults = {
|
|
9
|
+
ignoreUnknownFields: false,
|
|
10
|
+
};
|
|
11
|
+
// Default options for serializing to JSON.
|
|
12
|
+
const jsonWriteDefaults = {
|
|
13
|
+
emitDefaultValues: false,
|
|
14
|
+
enumAsInteger: false,
|
|
15
|
+
useProtoFieldName: false,
|
|
16
|
+
prettySpaces: 0,
|
|
17
|
+
};
|
|
18
|
+
function makeReadOptions(options) {
|
|
19
|
+
return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
|
|
20
|
+
}
|
|
21
|
+
function makeWriteOptions(options) {
|
|
22
|
+
return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
|
|
23
|
+
}
|
|
24
|
+
export function makeJsonFormatCommon(makeWriteField) {
|
|
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(
|
|
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
|
+
);
|
|
85
|
+
}
|
|
86
|
+
let val;
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- "map" is invalid for repeated fields
|
|
88
|
+
switch (field.kind) {
|
|
89
|
+
case "message":
|
|
90
|
+
val = field.T.fromJson(jsonItem, options);
|
|
91
|
+
break;
|
|
92
|
+
case "enum":
|
|
93
|
+
val = readEnum(field.T, jsonItem, options.ignoreUnknownFields);
|
|
94
|
+
if (val === undefined) continue;
|
|
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);
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
targetArray.push(val);
|
|
111
|
+
}
|
|
112
|
+
} else if (field.kind == "map") {
|
|
113
|
+
if (jsonValue === null) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (Array.isArray(jsonValue) || typeof jsonValue != "object") {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`cannot decode field ${type.typeName}.${
|
|
119
|
+
field.name
|
|
120
|
+
} from JSON: ${this.debug(jsonValue)}`
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const targetMap = target[localName];
|
|
124
|
+
for (const [jsonMapKey, jsonMapValue] of Object.entries(jsonValue)) {
|
|
125
|
+
if (jsonMapValue === null) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`cannot decode field ${type.typeName}.${field.name} from JSON: map value null`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
let val;
|
|
131
|
+
switch (field.V.kind) {
|
|
132
|
+
case "message":
|
|
133
|
+
val = field.V.T.fromJson(jsonMapValue, options);
|
|
134
|
+
break;
|
|
135
|
+
case "enum":
|
|
136
|
+
val = readEnum(
|
|
137
|
+
field.V.T,
|
|
138
|
+
jsonMapValue,
|
|
139
|
+
options.ignoreUnknownFields
|
|
140
|
+
);
|
|
141
|
+
if (val === undefined) continue;
|
|
142
|
+
break;
|
|
143
|
+
case "scalar":
|
|
144
|
+
try {
|
|
145
|
+
val = readScalar(field.V.T, jsonMapValue);
|
|
146
|
+
} catch (e) {
|
|
147
|
+
let m = `cannot decode map value for field ${type.typeName}.${
|
|
148
|
+
field.name
|
|
149
|
+
} from JSON: "${this.debug(jsonValue)}"`;
|
|
150
|
+
if (e instanceof Error && e.message.length > 0) {
|
|
151
|
+
m += `: ${e.message}`;
|
|
152
|
+
}
|
|
153
|
+
throw new Error(m);
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
targetMap[
|
|
159
|
+
readScalar(
|
|
160
|
+
field.K,
|
|
161
|
+
field.K == ScalarType.BOOL
|
|
162
|
+
? jsonMapKey == "true"
|
|
163
|
+
? true
|
|
164
|
+
: jsonMapKey == "false"
|
|
165
|
+
? false
|
|
166
|
+
: jsonMapKey
|
|
167
|
+
: jsonMapKey
|
|
168
|
+
).toString()
|
|
169
|
+
] = val;
|
|
170
|
+
} catch (e) {
|
|
171
|
+
let m = `cannot decode map key for field ${type.typeName}.${
|
|
172
|
+
field.name
|
|
173
|
+
} from JSON: "${this.debug(jsonValue)}"`;
|
|
174
|
+
if (e instanceof Error && e.message.length > 0) {
|
|
175
|
+
m += `: ${e.message}`;
|
|
176
|
+
}
|
|
177
|
+
throw new Error(m);
|
|
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}`;
|
|
223
|
+
}
|
|
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
|
+
}
|
|
244
|
+
field = member.findField(oneof.case);
|
|
245
|
+
if (!field) {
|
|
246
|
+
throw "oneof case not found: " + oneof.case;
|
|
247
|
+
}
|
|
248
|
+
jsonValue = writeField(field, oneof.value, options);
|
|
249
|
+
} else {
|
|
250
|
+
field = member;
|
|
251
|
+
jsonValue = writeField(field, message[field.localName], options);
|
|
252
|
+
}
|
|
253
|
+
if (jsonValue !== undefined) {
|
|
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
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function debugJsonValue(json) {
|
|
273
|
+
if (json === null) {
|
|
274
|
+
return "null";
|
|
275
|
+
}
|
|
276
|
+
switch (typeof json) {
|
|
277
|
+
case "object":
|
|
278
|
+
return Array.isArray(json) ? "array" : "object";
|
|
279
|
+
case "string":
|
|
280
|
+
return json.length > 100 ? "string" : `"${json.split('"').join('\\"')}"`;
|
|
281
|
+
default:
|
|
282
|
+
return json.toString();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// May throw an error. If the error message is non-blank, it should be shown.
|
|
286
|
+
// It is up to the caller to provide context.
|
|
287
|
+
function readScalar(type, json) {
|
|
288
|
+
// every valid case in the switch below returns, and every fall
|
|
289
|
+
// through is regarded as a failure.
|
|
290
|
+
switch (type) {
|
|
291
|
+
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
292
|
+
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
293
|
+
case ScalarType.DOUBLE:
|
|
294
|
+
case ScalarType.FLOAT:
|
|
295
|
+
if (json === null) return 0.0;
|
|
296
|
+
if (json === "NaN") return Number.NaN;
|
|
297
|
+
if (json === "Infinity") return Number.POSITIVE_INFINITY;
|
|
298
|
+
if (json === "-Infinity") return Number.NEGATIVE_INFINITY;
|
|
299
|
+
if (json === "") {
|
|
300
|
+
// empty string is not a number
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
if (typeof json == "string" && json.trim().length !== json.length) {
|
|
304
|
+
// extra whitespace
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
if (typeof json != "string" && typeof json != "number") {
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
const float = Number(json);
|
|
311
|
+
if (Number.isNaN(float)) {
|
|
312
|
+
// not a number
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
if (!Number.isFinite(float)) {
|
|
316
|
+
// infinity and -infinity are handled by string representation above, so this is an error
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
if (type == ScalarType.FLOAT) assertFloat32(float);
|
|
320
|
+
return float;
|
|
321
|
+
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
322
|
+
case ScalarType.INT32:
|
|
323
|
+
case ScalarType.FIXED32:
|
|
324
|
+
case ScalarType.SFIXED32:
|
|
325
|
+
case ScalarType.SINT32:
|
|
326
|
+
case ScalarType.UINT32:
|
|
327
|
+
if (json === null) return 0;
|
|
328
|
+
let int32;
|
|
329
|
+
if (typeof json == "number") int32 = json;
|
|
330
|
+
else if (typeof json == "string" && json.length > 0) {
|
|
331
|
+
if (json.trim().length === json.length) int32 = Number(json);
|
|
332
|
+
}
|
|
333
|
+
if (int32 === undefined) break;
|
|
334
|
+
if (type == ScalarType.UINT32) assertUInt32(int32);
|
|
335
|
+
else assertInt32(int32);
|
|
336
|
+
return int32;
|
|
337
|
+
// int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
338
|
+
case ScalarType.INT64:
|
|
339
|
+
case ScalarType.SFIXED64:
|
|
340
|
+
case ScalarType.SINT64:
|
|
341
|
+
if (json === null) return protoInt64.zero;
|
|
342
|
+
if (typeof json != "number" && typeof json != "string") break;
|
|
343
|
+
return protoInt64.parse(json);
|
|
344
|
+
case ScalarType.FIXED64:
|
|
345
|
+
case ScalarType.UINT64:
|
|
346
|
+
if (json === null) return protoInt64.zero;
|
|
347
|
+
if (typeof json != "number" && typeof json != "string") break;
|
|
348
|
+
return protoInt64.uParse(json);
|
|
349
|
+
// bool:
|
|
350
|
+
case ScalarType.BOOL:
|
|
351
|
+
if (json === null) return false;
|
|
352
|
+
if (typeof json !== "boolean") break;
|
|
353
|
+
return json;
|
|
354
|
+
// string:
|
|
355
|
+
case ScalarType.STRING:
|
|
356
|
+
if (json === null) return "";
|
|
357
|
+
if (typeof json !== "string") {
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
// A string must always contain UTF-8 encoded or 7-bit ASCII.
|
|
361
|
+
// We validate with encodeURIComponent, which appears to be the fastest widely available option.
|
|
362
|
+
try {
|
|
363
|
+
encodeURIComponent(json);
|
|
364
|
+
} catch (e) {
|
|
365
|
+
throw new Error("invalid UTF8");
|
|
366
|
+
}
|
|
367
|
+
return json;
|
|
368
|
+
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
369
|
+
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
370
|
+
case ScalarType.BYTES:
|
|
371
|
+
if (json === null || json === "") return new Uint8Array(0);
|
|
372
|
+
if (typeof json !== "string") break;
|
|
373
|
+
return base64decode(json);
|
|
374
|
+
}
|
|
375
|
+
throw new Error();
|
|
376
|
+
}
|
|
377
|
+
function readEnum(type, json, ignoreUnknownFields) {
|
|
378
|
+
if (json === null) {
|
|
379
|
+
// proto3 requires 0 to be default value for all enums
|
|
380
|
+
return 0;
|
|
381
|
+
}
|
|
382
|
+
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
383
|
+
switch (typeof json) {
|
|
384
|
+
case "number":
|
|
385
|
+
if (Number.isInteger(json)) {
|
|
386
|
+
return json;
|
|
387
|
+
}
|
|
388
|
+
break;
|
|
389
|
+
case "string":
|
|
390
|
+
const value = type.findName(json);
|
|
391
|
+
if (value || ignoreUnknownFields) {
|
|
392
|
+
return value?.no;
|
|
393
|
+
}
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
throw new Error(
|
|
397
|
+
`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
|
|
401
|
+
if (value === undefined) {
|
|
402
|
+
return value;
|
|
403
|
+
}
|
|
404
|
+
if (value === 0 && !emitIntrinsicDefault) {
|
|
405
|
+
// proto3 requires 0 to be default value for all enums
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
if (enumAsInteger) {
|
|
409
|
+
return value;
|
|
410
|
+
}
|
|
411
|
+
if (type.typeName == "google.protobuf.NullValue") {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
const val = type.findNumber(value);
|
|
415
|
+
return val?.name ?? value; // if we don't know the enum value, just return the number
|
|
416
|
+
}
|
|
417
|
+
function writeScalar(type, value, emitIntrinsicDefault) {
|
|
418
|
+
if (value === undefined) {
|
|
419
|
+
return undefined;
|
|
420
|
+
}
|
|
421
|
+
switch (type) {
|
|
422
|
+
// int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
|
|
423
|
+
case ScalarType.INT32:
|
|
424
|
+
case ScalarType.SFIXED32:
|
|
425
|
+
case ScalarType.SINT32:
|
|
426
|
+
case ScalarType.FIXED32:
|
|
427
|
+
case ScalarType.UINT32:
|
|
428
|
+
assert(typeof value == "number");
|
|
429
|
+
return value != 0 || emitIntrinsicDefault ? value : undefined;
|
|
430
|
+
// float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
|
|
431
|
+
// Either numbers or strings are accepted. Exponent notation is also accepted.
|
|
432
|
+
case ScalarType.FLOAT:
|
|
433
|
+
// assertFloat32(value);
|
|
434
|
+
case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
|
|
435
|
+
assert(typeof value == "number");
|
|
436
|
+
if (Number.isNaN(value)) return "NaN";
|
|
437
|
+
if (value === Number.POSITIVE_INFINITY) return "Infinity";
|
|
438
|
+
if (value === Number.NEGATIVE_INFINITY) return "-Infinity";
|
|
439
|
+
return value !== 0 || emitIntrinsicDefault ? value : undefined;
|
|
440
|
+
// string:
|
|
441
|
+
case ScalarType.STRING:
|
|
442
|
+
assert(typeof value == "string");
|
|
443
|
+
return value.length > 0 || emitIntrinsicDefault ? value : undefined;
|
|
444
|
+
// bool:
|
|
445
|
+
case ScalarType.BOOL:
|
|
446
|
+
assert(typeof value == "boolean");
|
|
447
|
+
return value || emitIntrinsicDefault ? value : undefined;
|
|
448
|
+
// JSON value will be a decimal string. Either numbers or strings are accepted.
|
|
449
|
+
case ScalarType.UINT64:
|
|
450
|
+
case ScalarType.FIXED64:
|
|
451
|
+
case ScalarType.INT64:
|
|
452
|
+
case ScalarType.SFIXED64:
|
|
453
|
+
case ScalarType.SINT64:
|
|
454
|
+
assert(
|
|
455
|
+
typeof value == "bigint" ||
|
|
456
|
+
typeof value == "string" ||
|
|
457
|
+
typeof value == "number"
|
|
458
|
+
);
|
|
459
|
+
// We use implicit conversion with `value != 0` to catch both 0n and "0"
|
|
460
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
461
|
+
// @ts-ignore
|
|
462
|
+
return emitIntrinsicDefault || value != 0
|
|
463
|
+
? value.toString(10)
|
|
464
|
+
: undefined;
|
|
465
|
+
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
466
|
+
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
467
|
+
case ScalarType.BYTES:
|
|
468
|
+
assert(value instanceof Uint8Array);
|
|
469
|
+
return emitIntrinsicDefault || value.byteLength > 0
|
|
470
|
+
? base64encode(value)
|
|
471
|
+
: undefined;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { wrapField } from "./field-wrapper.js";
|
|
2
|
+
import { assert } from "./assert.js";
|
|
3
|
+
import { makeJsonFormatCommon } from "./json-format-common.js";
|
|
4
|
+
/* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
|
|
5
|
+
export function makeJsonFormatProto2() {
|
|
6
|
+
return makeJsonFormatCommon((writeEnum, writeScalar) => {
|
|
7
|
+
return function writeField(field, value, options) {
|
|
8
|
+
if (field.kind == "map") {
|
|
9
|
+
const jsonObj = {};
|
|
10
|
+
switch (field.V.kind) {
|
|
11
|
+
case "scalar":
|
|
12
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
13
|
+
const val = writeScalar(field.V.T, entryValue, true);
|
|
14
|
+
assert(val !== undefined);
|
|
15
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
16
|
+
}
|
|
17
|
+
break;
|
|
18
|
+
case "message":
|
|
19
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
20
|
+
// JSON standard allows only (double quoted) string as property key
|
|
21
|
+
jsonObj[entryKey.toString()] = entryValue.toJson(options);
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
case "enum":
|
|
25
|
+
const enumType = field.V.T;
|
|
26
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
27
|
+
assert(entryValue === undefined || typeof entryValue == "number");
|
|
28
|
+
const val = writeEnum(
|
|
29
|
+
enumType,
|
|
30
|
+
entryValue,
|
|
31
|
+
true,
|
|
32
|
+
options.enumAsInteger
|
|
33
|
+
);
|
|
34
|
+
assert(val !== undefined);
|
|
35
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return options.emitDefaultValues || Object.keys(jsonObj).length > 0
|
|
40
|
+
? jsonObj
|
|
41
|
+
: undefined;
|
|
42
|
+
} else if (field.repeated) {
|
|
43
|
+
const jsonArr = [];
|
|
44
|
+
switch (field.kind) {
|
|
45
|
+
case "scalar":
|
|
46
|
+
for (let i = 0; i < value.length; i++) {
|
|
47
|
+
jsonArr.push(writeScalar(field.T, value[i], true));
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
case "enum":
|
|
51
|
+
for (let i = 0; i < value.length; i++) {
|
|
52
|
+
jsonArr.push(
|
|
53
|
+
writeEnum(field.T, value[i], true, options.enumAsInteger)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
case "message":
|
|
58
|
+
for (let i = 0; i < value.length; i++) {
|
|
59
|
+
jsonArr.push(value[i].toJson(options));
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
return options.emitDefaultValues || jsonArr.length > 0
|
|
64
|
+
? jsonArr
|
|
65
|
+
: undefined;
|
|
66
|
+
} else {
|
|
67
|
+
// In contrast to proto3, we raise an error if a non-optional (proto2 required)
|
|
68
|
+
// field is missing a value.
|
|
69
|
+
if (value === undefined) {
|
|
70
|
+
if (!field.oneof && !field.opt) {
|
|
71
|
+
throw `required field not set`;
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
switch (field.kind) {
|
|
76
|
+
case "scalar":
|
|
77
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
78
|
+
// Explicit default values are not special cased either.
|
|
79
|
+
return writeScalar(field.T, value, true);
|
|
80
|
+
case "enum":
|
|
81
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
82
|
+
// Explicit default values are not special cased either.
|
|
83
|
+
return writeEnum(field.T, value, true, options.enumAsInteger);
|
|
84
|
+
case "message":
|
|
85
|
+
return wrapField(field.T, value).toJson(options);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { wrapField } from "./field-wrapper.js";
|
|
2
|
+
import { assert } from "./assert.js";
|
|
3
|
+
import { makeJsonFormatCommon } from "./json-format-common.js";
|
|
4
|
+
/* eslint-disable no-case-declarations, @typescript-eslint/restrict-plus-operands,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
|
|
5
|
+
export function makeJsonFormatProto3() {
|
|
6
|
+
return makeJsonFormatCommon((writeEnum, writeScalar) => {
|
|
7
|
+
return function writeField(field, value, options) {
|
|
8
|
+
if (field.kind == "map") {
|
|
9
|
+
const jsonObj = {};
|
|
10
|
+
switch (field.V.kind) {
|
|
11
|
+
case "scalar":
|
|
12
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
13
|
+
const val = writeScalar(field.V.T, entryValue, true);
|
|
14
|
+
assert(val !== undefined);
|
|
15
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
16
|
+
}
|
|
17
|
+
break;
|
|
18
|
+
case "message":
|
|
19
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
20
|
+
// JSON standard allows only (double quoted) string as property key
|
|
21
|
+
jsonObj[entryKey.toString()] = entryValue.toJson(options);
|
|
22
|
+
}
|
|
23
|
+
break;
|
|
24
|
+
case "enum":
|
|
25
|
+
const enumType = field.V.T;
|
|
26
|
+
for (const [entryKey, entryValue] of Object.entries(value)) {
|
|
27
|
+
assert(entryValue === undefined || typeof entryValue == "number");
|
|
28
|
+
const val = writeEnum(
|
|
29
|
+
enumType,
|
|
30
|
+
entryValue,
|
|
31
|
+
true,
|
|
32
|
+
options.enumAsInteger
|
|
33
|
+
);
|
|
34
|
+
assert(val !== undefined);
|
|
35
|
+
jsonObj[entryKey.toString()] = val; // JSON standard allows only (double quoted) string as property key
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return options.emitDefaultValues || Object.keys(jsonObj).length > 0
|
|
40
|
+
? jsonObj
|
|
41
|
+
: undefined;
|
|
42
|
+
} else if (field.repeated) {
|
|
43
|
+
const jsonArr = [];
|
|
44
|
+
switch (field.kind) {
|
|
45
|
+
case "scalar":
|
|
46
|
+
for (let i = 0; i < value.length; i++) {
|
|
47
|
+
jsonArr.push(writeScalar(field.T, value[i], true));
|
|
48
|
+
}
|
|
49
|
+
break;
|
|
50
|
+
case "enum":
|
|
51
|
+
for (let i = 0; i < value.length; i++) {
|
|
52
|
+
jsonArr.push(
|
|
53
|
+
writeEnum(field.T, value[i], true, options.enumAsInteger)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
break;
|
|
57
|
+
case "message":
|
|
58
|
+
for (let i = 0; i < value.length; i++) {
|
|
59
|
+
jsonArr.push(wrapField(field.T, value[i]).toJson(options));
|
|
60
|
+
}
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
return options.emitDefaultValues || jsonArr.length > 0
|
|
64
|
+
? jsonArr
|
|
65
|
+
: undefined;
|
|
66
|
+
} else {
|
|
67
|
+
switch (field.kind) {
|
|
68
|
+
case "scalar":
|
|
69
|
+
return writeScalar(
|
|
70
|
+
field.T,
|
|
71
|
+
value,
|
|
72
|
+
!!field.oneof || field.opt || options.emitDefaultValues
|
|
73
|
+
);
|
|
74
|
+
case "enum":
|
|
75
|
+
return writeEnum(
|
|
76
|
+
field.T,
|
|
77
|
+
value,
|
|
78
|
+
!!field.oneof || field.opt || options.emitDefaultValues,
|
|
79
|
+
options.enumAsInteger
|
|
80
|
+
);
|
|
81
|
+
case "message":
|
|
82
|
+
return value !== undefined
|
|
83
|
+
? wrapField(field.T, value).toJson(options)
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Message } from "../message.js";
|
|
2
|
+
/**
|
|
3
|
+
* Create a new message type using the given runtime.
|
|
4
|
+
*/
|
|
5
|
+
export function makeMessageType(runtime, typeName, fields, opt) {
|
|
6
|
+
const localName =
|
|
7
|
+
opt?.localName ?? typeName.substring(typeName.lastIndexOf(".") + 1);
|
|
8
|
+
const type = {
|
|
9
|
+
[localName]: function (data) {
|
|
10
|
+
runtime.util.initFields(this);
|
|
11
|
+
runtime.util.initPartial(data, this);
|
|
12
|
+
},
|
|
13
|
+
}[localName];
|
|
14
|
+
Object.setPrototypeOf(type.prototype, new Message());
|
|
15
|
+
Object.assign(type, {
|
|
16
|
+
runtime,
|
|
17
|
+
typeName,
|
|
18
|
+
fields: runtime.util.newFieldList(fields),
|
|
19
|
+
fromBinary(bytes, options) {
|
|
20
|
+
return new type().fromBinary(bytes, options);
|
|
21
|
+
},
|
|
22
|
+
fromJson(jsonValue, options) {
|
|
23
|
+
return new type().fromJson(jsonValue, options);
|
|
24
|
+
},
|
|
25
|
+
fromJsonString(jsonString, options) {
|
|
26
|
+
return new type().fromJsonString(jsonString, options);
|
|
27
|
+
},
|
|
28
|
+
equals(a, b) {
|
|
29
|
+
return runtime.util.equals(type, a, b);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return type;
|
|
33
|
+
}
|