protobufjs 8.6.2 → 8.6.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/LICENSE +39 -39
- package/README.md +441 -441
- package/dist/light/protobuf.js +8582 -8454
- package/dist/light/protobuf.js.map +1 -1
- package/dist/light/protobuf.min.js +3 -3
- package/dist/light/protobuf.min.js.map +1 -1
- package/dist/minimal/protobuf.js +3071 -2999
- package/dist/minimal/protobuf.js.map +1 -1
- package/dist/minimal/protobuf.min.js +3 -3
- package/dist/minimal/protobuf.min.js.map +1 -1
- package/dist/protobuf.js +10486 -10358
- package/dist/protobuf.js.map +1 -1
- package/dist/protobuf.min.js +3 -3
- package/dist/protobuf.min.js.map +1 -1
- package/ext/README.md +70 -70
- package/ext/debug/README.md +4 -4
- package/ext/debug/index.js +71 -71
- package/ext/descriptor/README.md +5 -5
- package/ext/descriptor/index.d.ts +2 -2
- package/ext/descriptor/index.js +2 -2
- package/ext/descriptor.d.ts +87 -87
- package/ext/descriptor.js +1354 -1354
- package/ext/protojson.LICENSE +201 -201
- package/ext/protojson.d.ts +20 -20
- package/ext/protojson.js +945 -925
- package/ext/textformat.d.ts +19 -19
- package/ext/textformat.js +1256 -1256
- package/google/LICENSE +27 -27
- package/google/README.md +1 -1
- package/google/api/annotations.json +82 -82
- package/google/api/annotations.proto +10 -10
- package/google/api/http.json +85 -85
- package/google/api/http.proto +30 -30
- package/google/protobuf/api.json +117 -117
- package/google/protobuf/api.proto +33 -33
- package/google/protobuf/compiler/plugin.json +126 -126
- package/google/protobuf/compiler/plugin.proto +47 -47
- package/google/protobuf/descriptor.json +1381 -1381
- package/google/protobuf/descriptor.proto +534 -534
- package/google/protobuf/source_context.json +19 -19
- package/google/protobuf/source_context.proto +7 -7
- package/google/protobuf/type.json +201 -201
- package/google/protobuf/type.proto +89 -89
- package/index.d.ts +30 -0
- package/index.js +4 -4
- package/light.d.ts +2 -2
- package/light.js +3 -3
- package/minimal.d.ts +2 -2
- package/minimal.js +4 -4
- package/package.json +93 -93
- package/src/common.js +399 -399
- package/src/converter.js +344 -344
- package/src/decoder.js +214 -208
- package/src/encoder.js +111 -111
- package/src/enum.js +231 -231
- package/src/field.js +497 -497
- package/src/index-light.js +104 -104
- package/src/index-minimal.js +35 -36
- package/src/index.js +12 -12
- package/src/mapfield.js +136 -136
- package/src/message.js +137 -137
- package/src/method.js +175 -175
- package/src/namespace.js +566 -565
- package/src/object.js +382 -382
- package/src/oneof.js +225 -225
- package/src/parse.js +1068 -1068
- package/src/reader.js +558 -543
- package/src/reader_buffer.js +72 -72
- package/src/root.js +416 -416
- package/src/roots.js +18 -18
- package/src/rpc/service.js +148 -148
- package/src/rpc.js +36 -36
- package/src/service.js +198 -198
- package/src/tokenize.js +421 -421
- package/src/type.js +654 -655
- package/src/types.js +196 -196
- package/src/typescript.js +25 -25
- package/src/util/aspromise.d.ts +13 -13
- package/src/util/aspromise.js +52 -52
- package/src/util/base64.d.ts +32 -32
- package/src/util/base64.js +146 -146
- package/src/util/codegen.d.ts +31 -31
- package/src/util/codegen.js +113 -113
- package/src/util/eventemitter.d.ts +45 -45
- package/src/util/eventemitter.js +86 -86
- package/src/util/fetch.d.ts +56 -56
- package/src/util/fetch.js +112 -112
- package/src/util/float.d.ts +83 -83
- package/src/util/float.js +335 -335
- package/src/util/fs.js +11 -11
- package/src/util/longbits.js +200 -200
- package/src/util/minimal.js +515 -515
- package/src/util/path.d.ts +22 -22
- package/src/util/path.js +100 -72
- package/src/util/patterns.js +7 -7
- package/src/util/pool.d.ts +32 -32
- package/src/util/pool.js +48 -48
- package/src/util/utf8.d.ts +24 -24
- package/src/util/utf8.js +188 -130
- package/src/util.js +264 -242
- package/src/verifier.js +180 -180
- package/src/wrappers.js +106 -106
- package/src/writer.js +495 -495
- package/src/writer_buffer.js +102 -102
- package/tsconfig.json +6 -6
package/ext/protojson.js
CHANGED
|
@@ -1,925 +1,945 @@
|
|
|
1
|
-
// Copyright 2021 Google LLC
|
|
2
|
-
// Copyright 2026 The protobuf.js Authors
|
|
3
|
-
//
|
|
4
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
// you may not use this file except in compliance with the License.
|
|
6
|
-
// You may obtain a copy of the License at
|
|
7
|
-
//
|
|
8
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
//
|
|
10
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
// See the License for the specific language governing permissions and
|
|
14
|
-
// limitations under the License.
|
|
15
|
-
|
|
16
|
-
// Derived from proto3-json-serializer v3.0.4 and modified by The protobuf.js Authors.
|
|
17
|
-
|
|
18
|
-
"use strict";
|
|
19
|
-
var protobuf = require("../light");
|
|
20
|
-
|
|
21
|
-
/* global BigInt */
|
|
22
|
-
|
|
23
|
-
var Type = protobuf.Type,
|
|
24
|
-
Enum = protobuf.Enum,
|
|
25
|
-
util = protobuf.util;
|
|
26
|
-
|
|
27
|
-
var protojson = protobuf.protojson = module.exports = {};
|
|
28
|
-
|
|
29
|
-
function isExtension(field) {
|
|
30
|
-
return Boolean(field.declaringField) || field.name.charAt(0) === ".";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function extensionName(field) {
|
|
34
|
-
var df = field.declaringField || field,
|
|
35
|
-
full = df.fullName.charAt(0) === "." ? df.fullName.slice(1) : df.fullName;
|
|
36
|
-
return "[" + (df.protoName ? full.replace(/[^.]+$/, df.protoName) : full) + "]";
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function indexField(index, key, field, type) {
|
|
40
|
-
var existing = index[key];
|
|
41
|
-
if (existing !== undefined && existing !== field)
|
|
42
|
-
throw Error(type.fullName + ": duplicate ProtoJSON field name " + JSON.stringify(key));
|
|
43
|
-
index[key] = field;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function fieldsByJsonName(type) {
|
|
47
|
-
if (type._fieldsByJsonName)
|
|
48
|
-
return type._fieldsByJsonName;
|
|
49
|
-
var index = Object.create(null),
|
|
50
|
-
fields = type.fieldsArray,
|
|
51
|
-
i = 0;
|
|
52
|
-
for (; i < fields.length; ++i) {
|
|
53
|
-
var field = fields[i].resolve();
|
|
54
|
-
if (isExtension(field))
|
|
55
|
-
indexField(index, extensionName(field), field, type);
|
|
56
|
-
else {
|
|
57
|
-
indexField(index, field.name, field, type);
|
|
58
|
-
indexField(index, field.jsonName, field, type);
|
|
59
|
-
indexField(index, field.protoName, field, type);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
type._fieldsByJsonName = index;
|
|
63
|
-
return index;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// --- reading scalars ---
|
|
67
|
-
|
|
68
|
-
var INT_RANGE = {
|
|
69
|
-
int32: ["-2147483648", "2147483647"],
|
|
70
|
-
sint32: ["-2147483648", "2147483647"],
|
|
71
|
-
sfixed32: ["-2147483648", "2147483647"],
|
|
72
|
-
uint32: ["0", "4294967295"],
|
|
73
|
-
fixed32: ["0", "4294967295"],
|
|
74
|
-
int64: ["-9223372036854775808", "9223372036854775807"],
|
|
75
|
-
sint64: ["-9223372036854775808", "9223372036854775807"],
|
|
76
|
-
sfixed64: ["-9223372036854775808", "9223372036854775807"],
|
|
77
|
-
uint64: ["0", "18446744073709551615"],
|
|
78
|
-
fixed64: ["0", "18446744073709551615"]
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
var LONG_TYPE = { int64: 1, uint64: 1, sint64: 1, fixed64: 1, sfixed64: 1 };
|
|
82
|
-
|
|
83
|
-
var MAX_FLOAT = 3.4028234663852886e38;
|
|
84
|
-
var hasBigInt = typeof BigInt !== "undefined";
|
|
85
|
-
|
|
86
|
-
var NUMERIC_RE = /^[+-]?(?:[0-9]*\.[0-9]+|[0-9]+\.?)(?:[eE][+-]?[0-9]+)?$/;
|
|
87
|
-
|
|
88
|
-
var SKIP = {};
|
|
89
|
-
|
|
90
|
-
function invalid(name, value, what) {
|
|
91
|
-
return Error(name + ": " + what + ": " + JSON.stringify(value));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function parseIntegerString(value, type, name) {
|
|
95
|
-
var str;
|
|
96
|
-
if (typeof value === "number") {
|
|
97
|
-
if (!isFinite(value) || Math.floor(value) !== value)
|
|
98
|
-
throw invalid(name, value, "not an integer");
|
|
99
|
-
str = numberToIntString(value);
|
|
100
|
-
} else if (typeof value === "string") {
|
|
101
|
-
if (value.length === 0 || /^\s|\s$/.test(value))
|
|
102
|
-
throw invalid(name, value, "invalid integer");
|
|
103
|
-
if (/^[+-]?[0-9]+$/.test(value))
|
|
104
|
-
str = value;
|
|
105
|
-
else if (NUMERIC_RE.test(value)) {
|
|
106
|
-
var num = Number(value);
|
|
107
|
-
if (!isFinite(num) || Math.floor(num) !== num)
|
|
108
|
-
throw invalid(name, value, "not an integer");
|
|
109
|
-
str = numberToIntString(num);
|
|
110
|
-
} else
|
|
111
|
-
throw invalid(name, value, "invalid integer");
|
|
112
|
-
} else
|
|
113
|
-
throw invalid(name, value, "expected integer (number or string)");
|
|
114
|
-
|
|
115
|
-
var range = INT_RANGE[type];
|
|
116
|
-
if (LONG_TYPE[type]) {
|
|
117
|
-
if (hasBigInt) {
|
|
118
|
-
var big = BigInt(str);
|
|
119
|
-
if (big < BigInt(range[0]) || big > BigInt(range[1]))
|
|
120
|
-
throw invalid(name, value, "out of range for " + type);
|
|
121
|
-
}
|
|
122
|
-
} else if (Number(str) < Number(range[0]) || Number(str) > Number(range[1]))
|
|
123
|
-
throw invalid(name, value, "out of range for " + type);
|
|
124
|
-
return str;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function parseMapIntegerKey(key, type, name) {
|
|
128
|
-
var unsigned = type === "uint32" || type === "fixed32" || type === "uint64" || type === "fixed64";
|
|
129
|
-
if (!(unsigned ? /^[0-9]+$/ : /^-?[0-9]+$/).test(key))
|
|
130
|
-
throw invalid(name, key, "invalid " + type + " map key");
|
|
131
|
-
if (hasBigInt) {
|
|
132
|
-
var big = BigInt(key),
|
|
133
|
-
range = INT_RANGE[type];
|
|
134
|
-
if (big < BigInt(range[0]) || big > BigInt(range[1]))
|
|
135
|
-
throw invalid(name, key, "out of range for " + type + " map key");
|
|
136
|
-
return big.toString();
|
|
137
|
-
}
|
|
138
|
-
parseIntegerString(key, type, name);
|
|
139
|
-
if (LONG_TYPE[type]) {
|
|
140
|
-
var normalized = key.replace(/^-?0+(?=\d)/, key.charAt(0) === "-" ? "-" : "");
|
|
141
|
-
return normalized === "-0" ? "0" : normalized;
|
|
142
|
-
}
|
|
143
|
-
return String(Number(key));
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function numberToIntString(num) {
|
|
147
|
-
if (num >= -9007199254740991 && num <= 9007199254740991)
|
|
148
|
-
return String(num);
|
|
149
|
-
return num.toFixed(0);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function parseFloat32Or64(value, isFloat, name) {
|
|
153
|
-
var num;
|
|
154
|
-
if (typeof value === "number") {
|
|
155
|
-
if (!isFinite(value))
|
|
156
|
-
throw invalid(name, value, "number out of range");
|
|
157
|
-
num = value;
|
|
158
|
-
} else if (value === "NaN")
|
|
159
|
-
return NaN;
|
|
160
|
-
else if (value === "Infinity")
|
|
161
|
-
return Infinity;
|
|
162
|
-
else if (value === "-Infinity")
|
|
163
|
-
return -Infinity;
|
|
164
|
-
else if (typeof value === "string") {
|
|
165
|
-
if (value.length === 0 || /^\s|\s$/.test(value) || !NUMERIC_RE.test(value))
|
|
166
|
-
throw invalid(name, value, "invalid number");
|
|
167
|
-
num = Number(value);
|
|
168
|
-
if (!isFinite(num))
|
|
169
|
-
throw invalid(name, value, "invalid number");
|
|
170
|
-
} else
|
|
171
|
-
throw invalid(name, value, "expected number");
|
|
172
|
-
if (isFloat && Math.abs(num) > MAX_FLOAT)
|
|
173
|
-
throw invalid(name, value, "out of range for float");
|
|
174
|
-
return num;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function validateUtf16(value, name) {
|
|
178
|
-
for (var i = 0; i < value.length; ++i) {
|
|
179
|
-
var c = value.charCodeAt(i);
|
|
180
|
-
if (c >= 0xD800 && c <= 0xDBFF) {
|
|
181
|
-
var next = value.charCodeAt(i + 1);
|
|
182
|
-
if (!(next >= 0xDC00 && next <= 0xDFFF))
|
|
183
|
-
throw invalid(name, value, "unpaired high surrogate");
|
|
184
|
-
++i;
|
|
185
|
-
} else if (c >= 0xDC00 && c <= 0xDFFF)
|
|
186
|
-
throw invalid(name, value, "unpaired low surrogate");
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function parseBytes(value, name) {
|
|
191
|
-
if (typeof value !== "string")
|
|
192
|
-
throw invalid(name, value, "expected base64 string");
|
|
193
|
-
var s = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
194
|
-
while (s.length % 4)
|
|
195
|
-
s += "=";
|
|
196
|
-
var buffer = util.newBuffer(util.base64.length(s));
|
|
197
|
-
util.base64.decode(s, buffer, 0);
|
|
198
|
-
return buffer;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function longFromString(str, unsigned) {
|
|
202
|
-
return util.Long ? util.Long.fromString(str, unsigned) : parseInt(str, 10);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function readScalar(type, value, name) {
|
|
206
|
-
switch (type) {
|
|
207
|
-
case "int32": case "sint32": case "sfixed32":
|
|
208
|
-
case "uint32": case "fixed32":
|
|
209
|
-
return Number(parseIntegerString(value, type, name));
|
|
210
|
-
case "int64": case "sint64": case "sfixed64":
|
|
211
|
-
return longFromString(parseIntegerString(value, type, name), false);
|
|
212
|
-
case "uint64": case "fixed64":
|
|
213
|
-
return longFromString(parseIntegerString(value, type, name), true);
|
|
214
|
-
case "float":
|
|
215
|
-
return parseFloat32Or64(value, true, name);
|
|
216
|
-
case "double":
|
|
217
|
-
return parseFloat32Or64(value, false, name);
|
|
218
|
-
case "bool":
|
|
219
|
-
if (typeof value !== "boolean")
|
|
220
|
-
throw invalid(name, value, "expected boolean");
|
|
221
|
-
return value;
|
|
222
|
-
case "string":
|
|
223
|
-
if (typeof value !== "string")
|
|
224
|
-
throw invalid(name, value, "expected string");
|
|
225
|
-
validateUtf16(value, name);
|
|
226
|
-
return value;
|
|
227
|
-
case "bytes":
|
|
228
|
-
return parseBytes(value, name);
|
|
229
|
-
default:
|
|
230
|
-
throw Error(name + ": unsupported scalar type " + type);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function readEnum(enm, value, name, options) {
|
|
235
|
-
if (typeof value === "string") {
|
|
236
|
-
var num = enm.values[value];
|
|
237
|
-
if (num !== undefined)
|
|
238
|
-
return num;
|
|
239
|
-
if (options.ignoreUnknownFields)
|
|
240
|
-
return SKIP;
|
|
241
|
-
throw invalid(name, value, "unknown enum value");
|
|
242
|
-
}
|
|
243
|
-
if (typeof value === "number") {
|
|
244
|
-
if (!isFinite(value) || Math.floor(value) !== value)
|
|
245
|
-
throw invalid(name, value, "invalid enum number");
|
|
246
|
-
return value;
|
|
247
|
-
}
|
|
248
|
-
if (value === null && enm.fullName === ".google.protobuf.NullValue")
|
|
249
|
-
return 0;
|
|
250
|
-
throw invalid(name, value, "expected enum string or number");
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function readMapKey(field, key) {
|
|
254
|
-
switch (field.keyType) {
|
|
255
|
-
case "bool":
|
|
256
|
-
if (key !== "true" && key !== "false")
|
|
257
|
-
throw invalid(field.fullName, key, "invalid bool map key");
|
|
258
|
-
return key;
|
|
259
|
-
case "string":
|
|
260
|
-
validateUtf16(key, field.fullName);
|
|
261
|
-
return key;
|
|
262
|
-
case "int32": case "sint32": case "sfixed32":
|
|
263
|
-
case "uint32": case "fixed32":
|
|
264
|
-
case "int64": case "sint64": case "sfixed64":
|
|
265
|
-
case "uint64": case "fixed64":
|
|
266
|
-
return parseMapIntegerKey(key, field.keyType, field.fullName);
|
|
267
|
-
default:
|
|
268
|
-
throw Error(field.fullName + ": unsupported map key type " + field.keyType);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// --- reading messages ---
|
|
273
|
-
|
|
274
|
-
function readField(field, value, options, depth) {
|
|
275
|
-
if (field.map) {
|
|
276
|
-
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
277
|
-
throw invalid(field.fullName, value, "expected object for map");
|
|
278
|
-
var map = Object.create(null), k;
|
|
279
|
-
for (k in value)
|
|
280
|
-
if (hasOwn(value, k)) {
|
|
281
|
-
var mk = readMapKey(field, k),
|
|
282
|
-
mv = readSingular(field, value[k], options, depth);
|
|
283
|
-
if (mv !== SKIP) {
|
|
284
|
-
if (hasOwn(map, mk))
|
|
285
|
-
throw invalid(field.fullName, k, "duplicate map key");
|
|
286
|
-
map[mk] = mv;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
return map;
|
|
290
|
-
}
|
|
291
|
-
if (field.repeated) {
|
|
292
|
-
if (!Array.isArray(value))
|
|
293
|
-
throw invalid(field.fullName, value, "expected array");
|
|
294
|
-
var arr = [], i = 0;
|
|
295
|
-
for (; i < value.length; ++i) {
|
|
296
|
-
if (value[i] === null && !isValueType(field.resolvedType))
|
|
297
|
-
throw invalid(field.fullName, null, "null element");
|
|
298
|
-
var el = readSingular(field, value[i], options, depth);
|
|
299
|
-
if (el !== SKIP)
|
|
300
|
-
arr.push(el);
|
|
301
|
-
}
|
|
302
|
-
return arr;
|
|
303
|
-
}
|
|
304
|
-
return readSingular(field, value, options, depth);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function readSingular(field, value, options, depth) {
|
|
308
|
-
if (field.resolvedType instanceof Type)
|
|
309
|
-
return readMessage(field.resolvedType, value, options, depth + 1);
|
|
310
|
-
if (field.resolvedType instanceof Enum)
|
|
311
|
-
return readEnum(field.resolvedType, value, field.fullName, options);
|
|
312
|
-
return readScalar(field.type, value, field.fullName);
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
function isValueType(type) {
|
|
316
|
-
return type instanceof Type && type.fullName === ".google.protobuf.Value";
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
function isImplicitDefault(field, value) {
|
|
320
|
-
if (field.hasPresence || field.repeated || field.map || field.resolvedType instanceof Type)
|
|
321
|
-
return false;
|
|
322
|
-
if (field.resolvedType instanceof Enum)
|
|
323
|
-
return value === 0;
|
|
324
|
-
switch (field.type) {
|
|
325
|
-
case "bool": return value === false;
|
|
326
|
-
case "string": return value === "";
|
|
327
|
-
case "bytes": return value == null || value.length === 0;
|
|
328
|
-
default: return longToNumber(value) === 0;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function readMessage(type, value, options, depth) {
|
|
333
|
-
if (depth > util.recursionLimit)
|
|
334
|
-
throw Error("max depth exceeded");
|
|
335
|
-
|
|
336
|
-
var wkt = WKT_FROM[type.fullName];
|
|
337
|
-
if (wkt)
|
|
338
|
-
return wkt(type, value, options, depth);
|
|
339
|
-
|
|
340
|
-
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
341
|
-
throw invalid(type.fullName, value, "expected object");
|
|
342
|
-
|
|
343
|
-
var index = fieldsByJsonName(type),
|
|
344
|
-
out = {},
|
|
345
|
-
seenFields = Object.create(null),
|
|
346
|
-
seenOneofs = Object.create(null),
|
|
347
|
-
key;
|
|
348
|
-
|
|
349
|
-
for (key in value) {
|
|
350
|
-
if (!hasOwn(value, key))
|
|
351
|
-
continue;
|
|
352
|
-
var field = index[key];
|
|
353
|
-
if (field === undefined) {
|
|
354
|
-
if (options.ignoreUnknownFields)
|
|
355
|
-
continue;
|
|
356
|
-
throw invalid(type.fullName, key, "unknown field");
|
|
357
|
-
}
|
|
358
|
-
if (seenFields[field.name])
|
|
359
|
-
throw invalid(type.fullName, key, "duplicate field");
|
|
360
|
-
seenFields[field.name] = true;
|
|
361
|
-
var fv = value[key], fieldValue;
|
|
362
|
-
if (fv === null) {
|
|
363
|
-
if (isValueType(field.resolvedType))
|
|
364
|
-
fieldValue = { nullValue: 0 };
|
|
365
|
-
else if (field.resolvedType instanceof Enum && field.resolvedType.fullName === ".google.protobuf.NullValue")
|
|
366
|
-
fieldValue = 0;
|
|
367
|
-
else
|
|
368
|
-
continue;
|
|
369
|
-
} else
|
|
370
|
-
fieldValue = readField(field, fv, options, depth);
|
|
371
|
-
if (fieldValue === SKIP)
|
|
372
|
-
continue;
|
|
373
|
-
if (field.partOf) {
|
|
374
|
-
if (seenOneofs[field.partOf.name])
|
|
375
|
-
throw Error(type.fullName + ": multiple values for oneof " + field.partOf.name);
|
|
376
|
-
seenOneofs[field.partOf.name] = true;
|
|
377
|
-
}
|
|
378
|
-
if (!isImplicitDefault(field, fieldValue))
|
|
379
|
-
out[field.name] = fieldValue;
|
|
380
|
-
}
|
|
381
|
-
return out;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// --- writing messages ---
|
|
385
|
-
|
|
386
|
-
function hasOwn(o, k) {
|
|
387
|
-
return o != null && Object.prototype.hasOwnProperty.call(o, k);
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
function setOwn(o, k, v) {
|
|
391
|
-
if (k === "__proto__")
|
|
392
|
-
util.makeProp(o, k);
|
|
393
|
-
o[k] = v;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
function wktFieldName(type, name) {
|
|
397
|
-
var field = fieldsByJsonName(type)[name];
|
|
398
|
-
return field ? field.name : name;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function wktFieldValue(type, message, name) {
|
|
402
|
-
var field = fieldsByJsonName(type)[name];
|
|
403
|
-
if (!field)
|
|
404
|
-
return message && message[name];
|
|
405
|
-
if (hasOwn(message, field.name))
|
|
406
|
-
return message[field.name];
|
|
407
|
-
if (hasOwn(message, field.protoName))
|
|
408
|
-
return message[field.protoName];
|
|
409
|
-
if (hasOwn(message, field.jsonName))
|
|
410
|
-
return message[field.jsonName];
|
|
411
|
-
return undefined;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
function writeScalar(type, value) {
|
|
415
|
-
switch (type) {
|
|
416
|
-
case "int64": case "sint64": case "sfixed64":
|
|
417
|
-
case "uint64": case "fixed64":
|
|
418
|
-
return value == null ? "0" : String(value);
|
|
419
|
-
case "float": case "double":
|
|
420
|
-
return typeof value === "number" && !isFinite(value) ? String(value) : value;
|
|
421
|
-
case "bytes":
|
|
422
|
-
return value == null ? "" : util.base64.encode(value, 0, value.length);
|
|
423
|
-
default:
|
|
424
|
-
return value;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
function writeSingular(field, value, options, depth) {
|
|
429
|
-
if (field.resolvedType instanceof Type)
|
|
430
|
-
return toJsonValue(field.resolvedType, value, options, depth + 1);
|
|
431
|
-
if (field.resolvedType instanceof Enum) {
|
|
432
|
-
if (field.resolvedType.fullName === ".google.protobuf.NullValue")
|
|
433
|
-
return null;
|
|
434
|
-
var name = field.resolvedType.valuesById[value];
|
|
435
|
-
return name === undefined ? value : name;
|
|
436
|
-
}
|
|
437
|
-
return writeScalar(field.type, value);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
function toJsonValue(type, message, options, depth) {
|
|
441
|
-
if (depth > util.recursionLimit)
|
|
442
|
-
throw Error("max depth exceeded");
|
|
443
|
-
|
|
444
|
-
var wkt = WKT_TO[type.fullName];
|
|
445
|
-
if (wkt)
|
|
446
|
-
return wkt(type, message, options, depth);
|
|
447
|
-
|
|
448
|
-
var out = {},
|
|
449
|
-
fields = type.fieldsArray,
|
|
450
|
-
i = 0;
|
|
451
|
-
for (; i < fields.length; ++i) {
|
|
452
|
-
var field = fields[i].resolve(),
|
|
453
|
-
value = message[field.name];
|
|
454
|
-
if (value == null)
|
|
455
|
-
continue;
|
|
456
|
-
var key = isExtension(field) ? extensionName(field) : field.jsonName;
|
|
457
|
-
if (field.map) {
|
|
458
|
-
var mk = Object.keys(value);
|
|
459
|
-
if (!mk.length)
|
|
460
|
-
continue;
|
|
461
|
-
var longKey = LONG_TYPE[field.keyType],
|
|
462
|
-
unsignedKey = field.keyType === "uint64" || field.keyType === "fixed64",
|
|
463
|
-
mo = {}, ki = 0;
|
|
464
|
-
for (; ki < mk.length; ++ki) {
|
|
465
|
-
var outKey = longKey ? util.longFromKey(mk[ki], unsignedKey).toString() : mk[ki];
|
|
466
|
-
setOwn(mo, outKey, writeSingular(field, value[mk[ki]], options, depth));
|
|
467
|
-
}
|
|
468
|
-
setOwn(out, key, mo);
|
|
469
|
-
} else if (field.repeated) {
|
|
470
|
-
if (!value.length)
|
|
471
|
-
continue;
|
|
472
|
-
var arr = new Array(value.length), j = 0;
|
|
473
|
-
for (; j < value.length; ++j)
|
|
474
|
-
arr[j] = writeSingular(field, value[j], options, depth);
|
|
475
|
-
setOwn(out, key, arr);
|
|
476
|
-
} else {
|
|
477
|
-
if (!hasOwn(message, field.name) || isImplicitDefault(field, value))
|
|
478
|
-
continue;
|
|
479
|
-
setOwn(out, key, writeSingular(field, value, options, depth));
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
return out;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
// --- well-known types ---
|
|
486
|
-
|
|
487
|
-
function longToNumber(value) {
|
|
488
|
-
if (value == null) return 0;
|
|
489
|
-
if (typeof value === "number") return value;
|
|
490
|
-
if (typeof value.toNumber === "function") return value.toNumber();
|
|
491
|
-
return Number(value) || 0;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
function nanosToString(nanos) {
|
|
495
|
-
var str = String(nanos < 0 ? -nanos : nanos);
|
|
496
|
-
while (str.length < 9) str = "0" + str;
|
|
497
|
-
while (str.length > 3 && str.slice(str.length - 3) === "000") str = str.slice(0, str.length - 3);
|
|
498
|
-
return str;
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
function fracToNanos(frac) {
|
|
502
|
-
while (frac.length < 9) frac += "0";
|
|
503
|
-
return parseInt(frac.slice(0, 9), 10);
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
function daysInMonth(year, month) {
|
|
507
|
-
switch (month) {
|
|
508
|
-
case 2:
|
|
509
|
-
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
510
|
-
case 4:
|
|
511
|
-
case 6:
|
|
512
|
-
case 9:
|
|
513
|
-
case 11:
|
|
514
|
-
return 30;
|
|
515
|
-
default:
|
|
516
|
-
return 31;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
function camelToSnake(path) {
|
|
521
|
-
return path.replace(/[A-Z]/g, function ($0) { return "_" + $0.toLowerCase(); });
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
var WKT_FROM = {};
|
|
525
|
-
var WKT_TO = {};
|
|
526
|
-
|
|
527
|
-
WKT_FROM[".google.protobuf.Duration"] = function (type, value) {
|
|
528
|
-
if (typeof value !== "string")
|
|
529
|
-
throw invalid(type.fullName, value, "expected duration string");
|
|
530
|
-
var match = /^(-)?([0-9]+)(?:\.([0-9]{1,9}))?s$/.exec(value);
|
|
531
|
-
if (!match)
|
|
532
|
-
throw invalid(type.fullName, value, "invalid duration");
|
|
533
|
-
var sign = match[1] ? -1 : 1,
|
|
534
|
-
seconds = parseInt(match[2], 10) * sign || 0,
|
|
535
|
-
nanos = match[3] ? fracToNanos(match[3]) * sign || 0 : 0;
|
|
536
|
-
if (seconds > 315576000000 || seconds < -315576000000)
|
|
537
|
-
throw invalid(type.fullName, value, "duration out of range");
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
nanos =
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
if (
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (
|
|
563
|
-
throw invalid(type.fullName, value, "invalid timestamp
|
|
564
|
-
var
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
return
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
function
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
validateUtf16(
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
return { fields: structFieldsFromJson(
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
function
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
return
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
if (
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
return
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
return
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
if (
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
var
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
if (
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
return
|
|
760
|
-
};
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
++i;
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
*
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
*
|
|
840
|
-
* @
|
|
841
|
-
* @
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
*
|
|
856
|
-
* @
|
|
857
|
-
* @
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
*
|
|
872
|
-
* @
|
|
873
|
-
* @
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
/**
|
|
907
|
-
*
|
|
908
|
-
* @param {
|
|
909
|
-
* @param {IProtoJsonOptions} [options] Conversion options
|
|
910
|
-
* @returns {
|
|
911
|
-
*/
|
|
912
|
-
Type.prototype.
|
|
913
|
-
return protojson.
|
|
914
|
-
};
|
|
915
|
-
|
|
916
|
-
/**
|
|
917
|
-
*
|
|
918
|
-
* @param {
|
|
919
|
-
* @param {IProtoJsonOptions} [options] Conversion options
|
|
920
|
-
* @returns {
|
|
921
|
-
*/
|
|
922
|
-
Type.prototype.
|
|
923
|
-
return protojson.
|
|
924
|
-
};
|
|
925
|
-
|
|
1
|
+
// Copyright 2021 Google LLC
|
|
2
|
+
// Copyright 2026 The protobuf.js Authors
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
|
|
16
|
+
// Derived from proto3-json-serializer v3.0.4 and modified by The protobuf.js Authors.
|
|
17
|
+
|
|
18
|
+
"use strict";
|
|
19
|
+
var protobuf = require("../light");
|
|
20
|
+
|
|
21
|
+
/* global BigInt */
|
|
22
|
+
|
|
23
|
+
var Type = protobuf.Type,
|
|
24
|
+
Enum = protobuf.Enum,
|
|
25
|
+
util = protobuf.util;
|
|
26
|
+
|
|
27
|
+
var protojson = protobuf.protojson = module.exports = {};
|
|
28
|
+
|
|
29
|
+
function isExtension(field) {
|
|
30
|
+
return Boolean(field.declaringField) || field.name.charAt(0) === ".";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function extensionName(field) {
|
|
34
|
+
var df = field.declaringField || field,
|
|
35
|
+
full = df.fullName.charAt(0) === "." ? df.fullName.slice(1) : df.fullName;
|
|
36
|
+
return "[" + (df.protoName ? full.replace(/[^.]+$/, df.protoName) : full) + "]";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function indexField(index, key, field, type) {
|
|
40
|
+
var existing = index[key];
|
|
41
|
+
if (existing !== undefined && existing !== field)
|
|
42
|
+
throw Error(type.fullName + ": duplicate ProtoJSON field name " + JSON.stringify(key));
|
|
43
|
+
index[key] = field;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function fieldsByJsonName(type) {
|
|
47
|
+
if (type._fieldsByJsonName)
|
|
48
|
+
return type._fieldsByJsonName;
|
|
49
|
+
var index = Object.create(null),
|
|
50
|
+
fields = type.fieldsArray,
|
|
51
|
+
i = 0;
|
|
52
|
+
for (; i < fields.length; ++i) {
|
|
53
|
+
var field = fields[i].resolve();
|
|
54
|
+
if (isExtension(field))
|
|
55
|
+
indexField(index, extensionName(field), field, type);
|
|
56
|
+
else {
|
|
57
|
+
indexField(index, field.name, field, type);
|
|
58
|
+
indexField(index, field.jsonName, field, type);
|
|
59
|
+
indexField(index, field.protoName, field, type);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
type._fieldsByJsonName = index;
|
|
63
|
+
return index;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// --- reading scalars ---
|
|
67
|
+
|
|
68
|
+
var INT_RANGE = {
|
|
69
|
+
int32: ["-2147483648", "2147483647"],
|
|
70
|
+
sint32: ["-2147483648", "2147483647"],
|
|
71
|
+
sfixed32: ["-2147483648", "2147483647"],
|
|
72
|
+
uint32: ["0", "4294967295"],
|
|
73
|
+
fixed32: ["0", "4294967295"],
|
|
74
|
+
int64: ["-9223372036854775808", "9223372036854775807"],
|
|
75
|
+
sint64: ["-9223372036854775808", "9223372036854775807"],
|
|
76
|
+
sfixed64: ["-9223372036854775808", "9223372036854775807"],
|
|
77
|
+
uint64: ["0", "18446744073709551615"],
|
|
78
|
+
fixed64: ["0", "18446744073709551615"]
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
var LONG_TYPE = { int64: 1, uint64: 1, sint64: 1, fixed64: 1, sfixed64: 1 };
|
|
82
|
+
|
|
83
|
+
var MAX_FLOAT = 3.4028234663852886e38;
|
|
84
|
+
var hasBigInt = typeof BigInt !== "undefined";
|
|
85
|
+
|
|
86
|
+
var NUMERIC_RE = /^[+-]?(?:[0-9]*\.[0-9]+|[0-9]+\.?)(?:[eE][+-]?[0-9]+)?$/;
|
|
87
|
+
|
|
88
|
+
var SKIP = {};
|
|
89
|
+
|
|
90
|
+
function invalid(name, value, what) {
|
|
91
|
+
return Error(name + ": " + what + ": " + JSON.stringify(value));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseIntegerString(value, type, name) {
|
|
95
|
+
var str;
|
|
96
|
+
if (typeof value === "number") {
|
|
97
|
+
if (!isFinite(value) || Math.floor(value) !== value)
|
|
98
|
+
throw invalid(name, value, "not an integer");
|
|
99
|
+
str = numberToIntString(value);
|
|
100
|
+
} else if (typeof value === "string") {
|
|
101
|
+
if (value.length === 0 || /^\s|\s$/.test(value))
|
|
102
|
+
throw invalid(name, value, "invalid integer");
|
|
103
|
+
if (/^[+-]?[0-9]+$/.test(value))
|
|
104
|
+
str = value;
|
|
105
|
+
else if (NUMERIC_RE.test(value)) {
|
|
106
|
+
var num = Number(value);
|
|
107
|
+
if (!isFinite(num) || Math.floor(num) !== num)
|
|
108
|
+
throw invalid(name, value, "not an integer");
|
|
109
|
+
str = numberToIntString(num);
|
|
110
|
+
} else
|
|
111
|
+
throw invalid(name, value, "invalid integer");
|
|
112
|
+
} else
|
|
113
|
+
throw invalid(name, value, "expected integer (number or string)");
|
|
114
|
+
|
|
115
|
+
var range = INT_RANGE[type];
|
|
116
|
+
if (LONG_TYPE[type]) {
|
|
117
|
+
if (hasBigInt) {
|
|
118
|
+
var big = BigInt(str);
|
|
119
|
+
if (big < BigInt(range[0]) || big > BigInt(range[1]))
|
|
120
|
+
throw invalid(name, value, "out of range for " + type);
|
|
121
|
+
}
|
|
122
|
+
} else if (Number(str) < Number(range[0]) || Number(str) > Number(range[1]))
|
|
123
|
+
throw invalid(name, value, "out of range for " + type);
|
|
124
|
+
return str;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function parseMapIntegerKey(key, type, name) {
|
|
128
|
+
var unsigned = type === "uint32" || type === "fixed32" || type === "uint64" || type === "fixed64";
|
|
129
|
+
if (!(unsigned ? /^[0-9]+$/ : /^-?[0-9]+$/).test(key))
|
|
130
|
+
throw invalid(name, key, "invalid " + type + " map key");
|
|
131
|
+
if (hasBigInt) {
|
|
132
|
+
var big = BigInt(key),
|
|
133
|
+
range = INT_RANGE[type];
|
|
134
|
+
if (big < BigInt(range[0]) || big > BigInt(range[1]))
|
|
135
|
+
throw invalid(name, key, "out of range for " + type + " map key");
|
|
136
|
+
return big.toString();
|
|
137
|
+
}
|
|
138
|
+
parseIntegerString(key, type, name);
|
|
139
|
+
if (LONG_TYPE[type]) {
|
|
140
|
+
var normalized = key.replace(/^-?0+(?=\d)/, key.charAt(0) === "-" ? "-" : "");
|
|
141
|
+
return normalized === "-0" ? "0" : normalized;
|
|
142
|
+
}
|
|
143
|
+
return String(Number(key));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function numberToIntString(num) {
|
|
147
|
+
if (num >= -9007199254740991 && num <= 9007199254740991)
|
|
148
|
+
return String(num);
|
|
149
|
+
return num.toFixed(0);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function parseFloat32Or64(value, isFloat, name) {
|
|
153
|
+
var num;
|
|
154
|
+
if (typeof value === "number") {
|
|
155
|
+
if (!isFinite(value))
|
|
156
|
+
throw invalid(name, value, "number out of range");
|
|
157
|
+
num = value;
|
|
158
|
+
} else if (value === "NaN")
|
|
159
|
+
return NaN;
|
|
160
|
+
else if (value === "Infinity")
|
|
161
|
+
return Infinity;
|
|
162
|
+
else if (value === "-Infinity")
|
|
163
|
+
return -Infinity;
|
|
164
|
+
else if (typeof value === "string") {
|
|
165
|
+
if (value.length === 0 || /^\s|\s$/.test(value) || !NUMERIC_RE.test(value))
|
|
166
|
+
throw invalid(name, value, "invalid number");
|
|
167
|
+
num = Number(value);
|
|
168
|
+
if (!isFinite(num))
|
|
169
|
+
throw invalid(name, value, "invalid number");
|
|
170
|
+
} else
|
|
171
|
+
throw invalid(name, value, "expected number");
|
|
172
|
+
if (isFloat && Math.abs(num) > MAX_FLOAT)
|
|
173
|
+
throw invalid(name, value, "out of range for float");
|
|
174
|
+
return num;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function validateUtf16(value, name) {
|
|
178
|
+
for (var i = 0; i < value.length; ++i) {
|
|
179
|
+
var c = value.charCodeAt(i);
|
|
180
|
+
if (c >= 0xD800 && c <= 0xDBFF) {
|
|
181
|
+
var next = value.charCodeAt(i + 1);
|
|
182
|
+
if (!(next >= 0xDC00 && next <= 0xDFFF))
|
|
183
|
+
throw invalid(name, value, "unpaired high surrogate");
|
|
184
|
+
++i;
|
|
185
|
+
} else if (c >= 0xDC00 && c <= 0xDFFF)
|
|
186
|
+
throw invalid(name, value, "unpaired low surrogate");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function parseBytes(value, name) {
|
|
191
|
+
if (typeof value !== "string")
|
|
192
|
+
throw invalid(name, value, "expected base64 string");
|
|
193
|
+
var s = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
194
|
+
while (s.length % 4)
|
|
195
|
+
s += "=";
|
|
196
|
+
var buffer = util.newBuffer(util.base64.length(s));
|
|
197
|
+
util.base64.decode(s, buffer, 0);
|
|
198
|
+
return buffer;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function longFromString(str, unsigned) {
|
|
202
|
+
return util.Long ? util.Long.fromString(str, unsigned) : parseInt(str, 10);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function readScalar(type, value, name) {
|
|
206
|
+
switch (type) {
|
|
207
|
+
case "int32": case "sint32": case "sfixed32":
|
|
208
|
+
case "uint32": case "fixed32":
|
|
209
|
+
return Number(parseIntegerString(value, type, name));
|
|
210
|
+
case "int64": case "sint64": case "sfixed64":
|
|
211
|
+
return longFromString(parseIntegerString(value, type, name), false);
|
|
212
|
+
case "uint64": case "fixed64":
|
|
213
|
+
return longFromString(parseIntegerString(value, type, name), true);
|
|
214
|
+
case "float":
|
|
215
|
+
return parseFloat32Or64(value, true, name);
|
|
216
|
+
case "double":
|
|
217
|
+
return parseFloat32Or64(value, false, name);
|
|
218
|
+
case "bool":
|
|
219
|
+
if (typeof value !== "boolean")
|
|
220
|
+
throw invalid(name, value, "expected boolean");
|
|
221
|
+
return value;
|
|
222
|
+
case "string":
|
|
223
|
+
if (typeof value !== "string")
|
|
224
|
+
throw invalid(name, value, "expected string");
|
|
225
|
+
validateUtf16(value, name);
|
|
226
|
+
return value;
|
|
227
|
+
case "bytes":
|
|
228
|
+
return parseBytes(value, name);
|
|
229
|
+
default:
|
|
230
|
+
throw Error(name + ": unsupported scalar type " + type);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function readEnum(enm, value, name, options) {
|
|
235
|
+
if (typeof value === "string") {
|
|
236
|
+
var num = enm.values[value];
|
|
237
|
+
if (num !== undefined)
|
|
238
|
+
return num;
|
|
239
|
+
if (options.ignoreUnknownFields)
|
|
240
|
+
return SKIP;
|
|
241
|
+
throw invalid(name, value, "unknown enum value");
|
|
242
|
+
}
|
|
243
|
+
if (typeof value === "number") {
|
|
244
|
+
if (!isFinite(value) || Math.floor(value) !== value)
|
|
245
|
+
throw invalid(name, value, "invalid enum number");
|
|
246
|
+
return value;
|
|
247
|
+
}
|
|
248
|
+
if (value === null && enm.fullName === ".google.protobuf.NullValue")
|
|
249
|
+
return 0;
|
|
250
|
+
throw invalid(name, value, "expected enum string or number");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function readMapKey(field, key) {
|
|
254
|
+
switch (field.keyType) {
|
|
255
|
+
case "bool":
|
|
256
|
+
if (key !== "true" && key !== "false")
|
|
257
|
+
throw invalid(field.fullName, key, "invalid bool map key");
|
|
258
|
+
return key;
|
|
259
|
+
case "string":
|
|
260
|
+
validateUtf16(key, field.fullName);
|
|
261
|
+
return key;
|
|
262
|
+
case "int32": case "sint32": case "sfixed32":
|
|
263
|
+
case "uint32": case "fixed32":
|
|
264
|
+
case "int64": case "sint64": case "sfixed64":
|
|
265
|
+
case "uint64": case "fixed64":
|
|
266
|
+
return parseMapIntegerKey(key, field.keyType, field.fullName);
|
|
267
|
+
default:
|
|
268
|
+
throw Error(field.fullName + ": unsupported map key type " + field.keyType);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// --- reading messages ---
|
|
273
|
+
|
|
274
|
+
function readField(field, value, options, depth) {
|
|
275
|
+
if (field.map) {
|
|
276
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
277
|
+
throw invalid(field.fullName, value, "expected object for map");
|
|
278
|
+
var map = Object.create(null), k;
|
|
279
|
+
for (k in value)
|
|
280
|
+
if (hasOwn(value, k)) {
|
|
281
|
+
var mk = readMapKey(field, k),
|
|
282
|
+
mv = readSingular(field, value[k], options, depth);
|
|
283
|
+
if (mv !== SKIP) {
|
|
284
|
+
if (hasOwn(map, mk))
|
|
285
|
+
throw invalid(field.fullName, k, "duplicate map key");
|
|
286
|
+
map[mk] = mv;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return map;
|
|
290
|
+
}
|
|
291
|
+
if (field.repeated) {
|
|
292
|
+
if (!Array.isArray(value))
|
|
293
|
+
throw invalid(field.fullName, value, "expected array");
|
|
294
|
+
var arr = [], i = 0;
|
|
295
|
+
for (; i < value.length; ++i) {
|
|
296
|
+
if (value[i] === null && !isValueType(field.resolvedType))
|
|
297
|
+
throw invalid(field.fullName, null, "null element");
|
|
298
|
+
var el = readSingular(field, value[i], options, depth);
|
|
299
|
+
if (el !== SKIP)
|
|
300
|
+
arr.push(el);
|
|
301
|
+
}
|
|
302
|
+
return arr;
|
|
303
|
+
}
|
|
304
|
+
return readSingular(field, value, options, depth);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function readSingular(field, value, options, depth) {
|
|
308
|
+
if (field.resolvedType instanceof Type)
|
|
309
|
+
return readMessage(field.resolvedType, value, options, depth + 1);
|
|
310
|
+
if (field.resolvedType instanceof Enum)
|
|
311
|
+
return readEnum(field.resolvedType, value, field.fullName, options);
|
|
312
|
+
return readScalar(field.type, value, field.fullName);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function isValueType(type) {
|
|
316
|
+
return type instanceof Type && type.fullName === ".google.protobuf.Value";
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function isImplicitDefault(field, value) {
|
|
320
|
+
if (field.hasPresence || field.repeated || field.map || field.resolvedType instanceof Type)
|
|
321
|
+
return false;
|
|
322
|
+
if (field.resolvedType instanceof Enum)
|
|
323
|
+
return value === 0;
|
|
324
|
+
switch (field.type) {
|
|
325
|
+
case "bool": return value === false;
|
|
326
|
+
case "string": return value === "";
|
|
327
|
+
case "bytes": return value == null || value.length === 0;
|
|
328
|
+
default: return longToNumber(value) === 0;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function readMessage(type, value, options, depth) {
|
|
333
|
+
if (depth > util.recursionLimit)
|
|
334
|
+
throw Error("max depth exceeded");
|
|
335
|
+
|
|
336
|
+
var wkt = WKT_FROM[type.fullName];
|
|
337
|
+
if (wkt)
|
|
338
|
+
return wkt(type, value, options, depth);
|
|
339
|
+
|
|
340
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
341
|
+
throw invalid(type.fullName, value, "expected object");
|
|
342
|
+
|
|
343
|
+
var index = fieldsByJsonName(type),
|
|
344
|
+
out = {},
|
|
345
|
+
seenFields = Object.create(null),
|
|
346
|
+
seenOneofs = Object.create(null),
|
|
347
|
+
key;
|
|
348
|
+
|
|
349
|
+
for (key in value) {
|
|
350
|
+
if (!hasOwn(value, key))
|
|
351
|
+
continue;
|
|
352
|
+
var field = index[key];
|
|
353
|
+
if (field === undefined) {
|
|
354
|
+
if (options.ignoreUnknownFields)
|
|
355
|
+
continue;
|
|
356
|
+
throw invalid(type.fullName, key, "unknown field");
|
|
357
|
+
}
|
|
358
|
+
if (seenFields[field.name])
|
|
359
|
+
throw invalid(type.fullName, key, "duplicate field");
|
|
360
|
+
seenFields[field.name] = true;
|
|
361
|
+
var fv = value[key], fieldValue;
|
|
362
|
+
if (fv === null) {
|
|
363
|
+
if (isValueType(field.resolvedType))
|
|
364
|
+
fieldValue = { nullValue: 0 };
|
|
365
|
+
else if (field.resolvedType instanceof Enum && field.resolvedType.fullName === ".google.protobuf.NullValue")
|
|
366
|
+
fieldValue = 0;
|
|
367
|
+
else
|
|
368
|
+
continue;
|
|
369
|
+
} else
|
|
370
|
+
fieldValue = readField(field, fv, options, depth);
|
|
371
|
+
if (fieldValue === SKIP)
|
|
372
|
+
continue;
|
|
373
|
+
if (field.partOf) {
|
|
374
|
+
if (seenOneofs[field.partOf.name])
|
|
375
|
+
throw Error(type.fullName + ": multiple values for oneof " + field.partOf.name);
|
|
376
|
+
seenOneofs[field.partOf.name] = true;
|
|
377
|
+
}
|
|
378
|
+
if (!isImplicitDefault(field, fieldValue))
|
|
379
|
+
out[field.name] = fieldValue;
|
|
380
|
+
}
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// --- writing messages ---
|
|
385
|
+
|
|
386
|
+
function hasOwn(o, k) {
|
|
387
|
+
return o != null && Object.prototype.hasOwnProperty.call(o, k);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function setOwn(o, k, v) {
|
|
391
|
+
if (k === "__proto__")
|
|
392
|
+
util.makeProp(o, k);
|
|
393
|
+
o[k] = v;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function wktFieldName(type, name) {
|
|
397
|
+
var field = fieldsByJsonName(type)[name];
|
|
398
|
+
return field ? field.name : name;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function wktFieldValue(type, message, name) {
|
|
402
|
+
var field = fieldsByJsonName(type)[name];
|
|
403
|
+
if (!field)
|
|
404
|
+
return message && message[name];
|
|
405
|
+
if (hasOwn(message, field.name))
|
|
406
|
+
return message[field.name];
|
|
407
|
+
if (hasOwn(message, field.protoName))
|
|
408
|
+
return message[field.protoName];
|
|
409
|
+
if (hasOwn(message, field.jsonName))
|
|
410
|
+
return message[field.jsonName];
|
|
411
|
+
return undefined;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function writeScalar(type, value) {
|
|
415
|
+
switch (type) {
|
|
416
|
+
case "int64": case "sint64": case "sfixed64":
|
|
417
|
+
case "uint64": case "fixed64":
|
|
418
|
+
return value == null ? "0" : String(value);
|
|
419
|
+
case "float": case "double":
|
|
420
|
+
return typeof value === "number" && !isFinite(value) ? String(value) : value;
|
|
421
|
+
case "bytes":
|
|
422
|
+
return value == null ? "" : util.base64.encode(value, 0, value.length);
|
|
423
|
+
default:
|
|
424
|
+
return value;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function writeSingular(field, value, options, depth) {
|
|
429
|
+
if (field.resolvedType instanceof Type)
|
|
430
|
+
return toJsonValue(field.resolvedType, value, options, depth + 1);
|
|
431
|
+
if (field.resolvedType instanceof Enum) {
|
|
432
|
+
if (field.resolvedType.fullName === ".google.protobuf.NullValue")
|
|
433
|
+
return null;
|
|
434
|
+
var name = field.resolvedType.valuesById[value];
|
|
435
|
+
return name === undefined ? value : name;
|
|
436
|
+
}
|
|
437
|
+
return writeScalar(field.type, value);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function toJsonValue(type, message, options, depth) {
|
|
441
|
+
if (depth > util.recursionLimit)
|
|
442
|
+
throw Error("max depth exceeded");
|
|
443
|
+
|
|
444
|
+
var wkt = WKT_TO[type.fullName];
|
|
445
|
+
if (wkt)
|
|
446
|
+
return wkt(type, message, options, depth);
|
|
447
|
+
|
|
448
|
+
var out = {},
|
|
449
|
+
fields = type.fieldsArray,
|
|
450
|
+
i = 0;
|
|
451
|
+
for (; i < fields.length; ++i) {
|
|
452
|
+
var field = fields[i].resolve(),
|
|
453
|
+
value = message[field.name];
|
|
454
|
+
if (value == null)
|
|
455
|
+
continue;
|
|
456
|
+
var key = isExtension(field) ? extensionName(field) : field.jsonName;
|
|
457
|
+
if (field.map) {
|
|
458
|
+
var mk = Object.keys(value);
|
|
459
|
+
if (!mk.length)
|
|
460
|
+
continue;
|
|
461
|
+
var longKey = LONG_TYPE[field.keyType],
|
|
462
|
+
unsignedKey = field.keyType === "uint64" || field.keyType === "fixed64",
|
|
463
|
+
mo = {}, ki = 0;
|
|
464
|
+
for (; ki < mk.length; ++ki) {
|
|
465
|
+
var outKey = longKey ? util.longFromKey(mk[ki], unsignedKey).toString() : mk[ki];
|
|
466
|
+
setOwn(mo, outKey, writeSingular(field, value[mk[ki]], options, depth));
|
|
467
|
+
}
|
|
468
|
+
setOwn(out, key, mo);
|
|
469
|
+
} else if (field.repeated) {
|
|
470
|
+
if (!value.length)
|
|
471
|
+
continue;
|
|
472
|
+
var arr = new Array(value.length), j = 0;
|
|
473
|
+
for (; j < value.length; ++j)
|
|
474
|
+
arr[j] = writeSingular(field, value[j], options, depth);
|
|
475
|
+
setOwn(out, key, arr);
|
|
476
|
+
} else {
|
|
477
|
+
if (!hasOwn(message, field.name) || isImplicitDefault(field, value))
|
|
478
|
+
continue;
|
|
479
|
+
setOwn(out, key, writeSingular(field, value, options, depth));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return out;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// --- well-known types ---
|
|
486
|
+
|
|
487
|
+
function longToNumber(value) {
|
|
488
|
+
if (value == null) return 0;
|
|
489
|
+
if (typeof value === "number") return value;
|
|
490
|
+
if (typeof value.toNumber === "function") return value.toNumber();
|
|
491
|
+
return Number(value) || 0;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function nanosToString(nanos) {
|
|
495
|
+
var str = String(nanos < 0 ? -nanos : nanos);
|
|
496
|
+
while (str.length < 9) str = "0" + str;
|
|
497
|
+
while (str.length > 3 && str.slice(str.length - 3) === "000") str = str.slice(0, str.length - 3);
|
|
498
|
+
return str;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function fracToNanos(frac) {
|
|
502
|
+
while (frac.length < 9) frac += "0";
|
|
503
|
+
return parseInt(frac.slice(0, 9), 10);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function daysInMonth(year, month) {
|
|
507
|
+
switch (month) {
|
|
508
|
+
case 2:
|
|
509
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
510
|
+
case 4:
|
|
511
|
+
case 6:
|
|
512
|
+
case 9:
|
|
513
|
+
case 11:
|
|
514
|
+
return 30;
|
|
515
|
+
default:
|
|
516
|
+
return 31;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function camelToSnake(path) {
|
|
521
|
+
return path.replace(/[A-Z]/g, function ($0) { return "_" + $0.toLowerCase(); });
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
var WKT_FROM = {};
|
|
525
|
+
var WKT_TO = {};
|
|
526
|
+
|
|
527
|
+
WKT_FROM[".google.protobuf.Duration"] = function (type, value) {
|
|
528
|
+
if (typeof value !== "string")
|
|
529
|
+
throw invalid(type.fullName, value, "expected duration string");
|
|
530
|
+
var match = /^(-)?([0-9]+)(?:\.([0-9]{1,9}))?s$/.exec(value);
|
|
531
|
+
if (!match)
|
|
532
|
+
throw invalid(type.fullName, value, "invalid duration");
|
|
533
|
+
var sign = match[1] ? -1 : 1,
|
|
534
|
+
seconds = parseInt(match[2], 10) * sign || 0,
|
|
535
|
+
nanos = match[3] ? fracToNanos(match[3]) * sign || 0 : 0;
|
|
536
|
+
if (seconds > 315576000000 || seconds < -315576000000)
|
|
537
|
+
throw invalid(type.fullName, value, "duration out of range");
|
|
538
|
+
var message = {};
|
|
539
|
+
if (seconds !== 0)
|
|
540
|
+
message.seconds = seconds;
|
|
541
|
+
if (nanos !== 0)
|
|
542
|
+
message.nanos = nanos;
|
|
543
|
+
return message;
|
|
544
|
+
};
|
|
545
|
+
WKT_TO[".google.protobuf.Duration"] = function (type, message) {
|
|
546
|
+
var seconds = longToNumber(message.seconds),
|
|
547
|
+
nanos = message.nanos || 0;
|
|
548
|
+
if (seconds > 315576000000 || seconds < -315576000000)
|
|
549
|
+
throw Error("google.protobuf.Duration out of range");
|
|
550
|
+
if (nanos > 999999999 || nanos < -999999999 || seconds && nanos && seconds < 0 !== nanos < 0)
|
|
551
|
+
throw Error("google.protobuf.Duration nanos invalid");
|
|
552
|
+
var result = (seconds < 0 || nanos < 0 ? "-" : "") + Math.abs(seconds);
|
|
553
|
+
if (nanos)
|
|
554
|
+
result += "." + nanosToString(nanos);
|
|
555
|
+
return result + "s";
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
WKT_FROM[".google.protobuf.Timestamp"] = function (type, value) {
|
|
559
|
+
if (typeof value !== "string")
|
|
560
|
+
throw invalid(type.fullName, value, "expected timestamp string");
|
|
561
|
+
var match = /^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(?:\.(\d{1,9}))?(Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/.exec(value);
|
|
562
|
+
if (!match)
|
|
563
|
+
throw invalid(type.fullName, value, "invalid timestamp");
|
|
564
|
+
var year = parseInt(match[1], 10),
|
|
565
|
+
month = parseInt(match[2], 10),
|
|
566
|
+
day = parseInt(match[3], 10);
|
|
567
|
+
if (day > daysInMonth(year, month))
|
|
568
|
+
throw invalid(type.fullName, value, "invalid timestamp date");
|
|
569
|
+
var millis = new Date(value).getTime();
|
|
570
|
+
if (isNaN(millis))
|
|
571
|
+
throw invalid(type.fullName, value, "invalid timestamp");
|
|
572
|
+
var seconds = Math.floor(millis / 1000),
|
|
573
|
+
nanos = match[7] ? fracToNanos(match[7]) : 0;
|
|
574
|
+
if (seconds < -62135596800 || seconds > 253402300799)
|
|
575
|
+
throw invalid(type.fullName, value, "timestamp out of range");
|
|
576
|
+
var message = {};
|
|
577
|
+
if (seconds !== 0)
|
|
578
|
+
message.seconds = seconds;
|
|
579
|
+
if (nanos !== 0)
|
|
580
|
+
message.nanos = nanos;
|
|
581
|
+
return message;
|
|
582
|
+
};
|
|
583
|
+
WKT_TO[".google.protobuf.Timestamp"] = function (type, message) {
|
|
584
|
+
var seconds = longToNumber(message.seconds),
|
|
585
|
+
nanos = message.nanos || 0;
|
|
586
|
+
if (seconds < -62135596800 || seconds > 253402300799)
|
|
587
|
+
throw Error("google.protobuf.Timestamp out of range");
|
|
588
|
+
if (nanos < 0 || nanos > 999999999)
|
|
589
|
+
throw Error("google.protobuf.Timestamp nanos out of range");
|
|
590
|
+
var iso = new Date(seconds * 1000).toISOString();
|
|
591
|
+
return nanos
|
|
592
|
+
? iso.replace(/\.\d+Z$/, "." + nanosToString(nanos) + "Z")
|
|
593
|
+
: iso.replace(/\.\d+Z$/, "Z");
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
WKT_FROM[".google.protobuf.FieldMask"] = function (type, value) {
|
|
597
|
+
if (typeof value !== "string")
|
|
598
|
+
throw invalid(type.fullName, value, "expected field mask string");
|
|
599
|
+
if (value.indexOf("_") !== -1)
|
|
600
|
+
throw invalid(type.fullName, value, "field mask path must be lowerCamelCase");
|
|
601
|
+
var paths = value.length ? value.split(",") : [],
|
|
602
|
+
i = 0;
|
|
603
|
+
for (; i < paths.length; ++i)
|
|
604
|
+
paths[i] = camelToSnake(paths[i]);
|
|
605
|
+
return { paths: paths };
|
|
606
|
+
};
|
|
607
|
+
WKT_TO[".google.protobuf.FieldMask"] = function (type, message) {
|
|
608
|
+
var paths = message.paths || [],
|
|
609
|
+
out = [],
|
|
610
|
+
i = 0;
|
|
611
|
+
for (; i < paths.length; ++i) {
|
|
612
|
+
var camel = util.jsonName(paths[i]);
|
|
613
|
+
if (camelToSnake(camel) !== paths[i])
|
|
614
|
+
throw Error("google.protobuf.FieldMask path does not round-trip: " + paths[i]);
|
|
615
|
+
out.push(camel);
|
|
616
|
+
}
|
|
617
|
+
return out.join(",");
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
["DoubleValue", "FloatValue", "Int64Value", "UInt64Value", "Int32Value",
|
|
621
|
+
"UInt32Value", "BoolValue", "StringValue", "BytesValue"].forEach(function (name) {
|
|
622
|
+
var fullName = ".google.protobuf." + name;
|
|
623
|
+
WKT_FROM[fullName] = function (type, value, options, depth) {
|
|
624
|
+
var field = type.fields.value.resolve(),
|
|
625
|
+
fieldValue = readSingular(field, value, options, depth),
|
|
626
|
+
message = {};
|
|
627
|
+
if (!isImplicitDefault(field, fieldValue))
|
|
628
|
+
message.value = fieldValue;
|
|
629
|
+
return message;
|
|
630
|
+
};
|
|
631
|
+
WKT_TO[fullName] = function (type, message, options, depth) {
|
|
632
|
+
var field = type.fields.value.resolve(),
|
|
633
|
+
value = wktFieldValue(type, message, "value");
|
|
634
|
+
return writeSingular(field, value === undefined ? field.defaultValue : value, options, depth);
|
|
635
|
+
};
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
function valueFromJson(json, depth) {
|
|
639
|
+
if (depth > util.recursionLimit)
|
|
640
|
+
throw Error("max depth exceeded");
|
|
641
|
+
if (json === null)
|
|
642
|
+
return { nullValue: 0 };
|
|
643
|
+
switch (typeof json) {
|
|
644
|
+
case "number":
|
|
645
|
+
if (!isFinite(json))
|
|
646
|
+
throw Error("google.protobuf.Value cannot hold a non-finite number");
|
|
647
|
+
return { numberValue: json };
|
|
648
|
+
case "string":
|
|
649
|
+
validateUtf16(json, "google.protobuf.Value.string_value");
|
|
650
|
+
return { stringValue: json };
|
|
651
|
+
case "boolean":
|
|
652
|
+
return { boolValue: json };
|
|
653
|
+
}
|
|
654
|
+
if (Array.isArray(json)) {
|
|
655
|
+
var values = new Array(json.length), i = 0;
|
|
656
|
+
for (; i < json.length; ++i)
|
|
657
|
+
values[i] = valueFromJson(json[i], depth + 1);
|
|
658
|
+
return { listValue: { values: values } };
|
|
659
|
+
}
|
|
660
|
+
return { structValue: { fields: structFieldsFromJson(json, depth + 1) } };
|
|
661
|
+
}
|
|
662
|
+
function structFieldsFromJson(json, depth) {
|
|
663
|
+
var fields = Object.create(null), k;
|
|
664
|
+
for (k in json)
|
|
665
|
+
if (hasOwn(json, k)) {
|
|
666
|
+
validateUtf16(k, "google.protobuf.Struct.fields");
|
|
667
|
+
fields[k] = valueFromJson(json[k], depth);
|
|
668
|
+
}
|
|
669
|
+
return fields;
|
|
670
|
+
}
|
|
671
|
+
WKT_FROM[".google.protobuf.Value"] = function (type, value, options, depth) {
|
|
672
|
+
return valueFromJson(value, depth);
|
|
673
|
+
};
|
|
674
|
+
WKT_FROM[".google.protobuf.Struct"] = function (type, value, options, depth) {
|
|
675
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
676
|
+
throw invalid(type.fullName, value, "google.protobuf.Struct must be an object");
|
|
677
|
+
return { fields: structFieldsFromJson(value, depth + 1) };
|
|
678
|
+
};
|
|
679
|
+
WKT_FROM[".google.protobuf.ListValue"] = function (type, value, options, depth) {
|
|
680
|
+
if (!Array.isArray(value))
|
|
681
|
+
throw invalid(type.fullName, value, "google.protobuf.ListValue must be an array");
|
|
682
|
+
var values = new Array(value.length), i = 0;
|
|
683
|
+
for (; i < value.length; ++i)
|
|
684
|
+
values[i] = valueFromJson(value[i], depth + 1);
|
|
685
|
+
return { values: values };
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
function valueToJson(message, options, depth) {
|
|
689
|
+
if (depth > util.recursionLimit)
|
|
690
|
+
throw Error("max depth exceeded");
|
|
691
|
+
if (message == null || hasOwn(message, "nullValue"))
|
|
692
|
+
return null;
|
|
693
|
+
if (hasOwn(message, "numberValue")) {
|
|
694
|
+
if (typeof message.numberValue === "number" && !isFinite(message.numberValue))
|
|
695
|
+
throw Error("google.protobuf.Value cannot hold a non-finite number");
|
|
696
|
+
return message.numberValue;
|
|
697
|
+
}
|
|
698
|
+
if (hasOwn(message, "stringValue")) return message.stringValue;
|
|
699
|
+
if (hasOwn(message, "boolValue")) return message.boolValue;
|
|
700
|
+
if (hasOwn(message, "structValue")) return structToJson(message.structValue, options, depth + 1);
|
|
701
|
+
if (hasOwn(message, "listValue")) return listToJson(message.listValue, options, depth + 1);
|
|
702
|
+
return null;
|
|
703
|
+
}
|
|
704
|
+
function structToJson(message, options, depth) {
|
|
705
|
+
var out = {}, fields = message && message.fields, k;
|
|
706
|
+
if (fields)
|
|
707
|
+
for (k in fields)
|
|
708
|
+
if (hasOwn(fields, k))
|
|
709
|
+
setOwn(out, k, valueToJson(fields[k], options, depth));
|
|
710
|
+
return out;
|
|
711
|
+
}
|
|
712
|
+
function listToJson(message, options, depth) {
|
|
713
|
+
var values = message && message.values;
|
|
714
|
+
if (!values) return [];
|
|
715
|
+
var arr = new Array(values.length), i = 0;
|
|
716
|
+
for (; i < values.length; ++i)
|
|
717
|
+
arr[i] = valueToJson(values[i], options, depth);
|
|
718
|
+
return arr;
|
|
719
|
+
}
|
|
720
|
+
WKT_TO[".google.protobuf.Value"] = function (type, message, options, depth) {
|
|
721
|
+
return valueToJson(message, options, depth);
|
|
722
|
+
};
|
|
723
|
+
WKT_TO[".google.protobuf.Struct"] = function (type, message, options, depth) {
|
|
724
|
+
return structToJson(message, options, depth);
|
|
725
|
+
};
|
|
726
|
+
WKT_TO[".google.protobuf.ListValue"] = function (type, message, options, depth) {
|
|
727
|
+
return listToJson(message, options, depth);
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
WKT_FROM[".google.protobuf.Any"] = function (type, value, options, depth) {
|
|
731
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
732
|
+
throw invalid(type.fullName, value, "google.protobuf.Any must be an object");
|
|
733
|
+
var typeUrl = value["@type"];
|
|
734
|
+
if (typeUrl === undefined)
|
|
735
|
+
return {};
|
|
736
|
+
if (typeof typeUrl !== "string")
|
|
737
|
+
throw Error("google.protobuf.Any @type must be a string");
|
|
738
|
+
var name = typeUrl.substring(typeUrl.lastIndexOf("/") + 1),
|
|
739
|
+
msgType = type.root.lookupType(name),
|
|
740
|
+
custom = WKT_FROM[msgType.fullName] !== undefined,
|
|
741
|
+
body;
|
|
742
|
+
if (custom)
|
|
743
|
+
body = value.value;
|
|
744
|
+
else {
|
|
745
|
+
body = {};
|
|
746
|
+
for (var k in value)
|
|
747
|
+
if (hasOwn(value, k) && k !== "@type")
|
|
748
|
+
setOwn(body, k, value[k]);
|
|
749
|
+
}
|
|
750
|
+
var inner = readMessage(msgType, body, options, depth + 1);
|
|
751
|
+
var url = typeUrl.charAt(0) === "." ? typeUrl.slice(1) : typeUrl,
|
|
752
|
+
out = {};
|
|
753
|
+
if (url.indexOf("/") === -1)
|
|
754
|
+
url = "/" + url;
|
|
755
|
+
out[wktFieldName(type, "type_url")] = url;
|
|
756
|
+
var bytes = msgType.encode(inner).finish();
|
|
757
|
+
if (bytes.length)
|
|
758
|
+
out[wktFieldName(type, "value")] = bytes;
|
|
759
|
+
return out;
|
|
760
|
+
};
|
|
761
|
+
WKT_TO[".google.protobuf.Any"] = function (type, message, options, depth) {
|
|
762
|
+
var typeUrl = wktFieldValue(type, message, "type_url");
|
|
763
|
+
if (!typeUrl)
|
|
764
|
+
return {};
|
|
765
|
+
var name = typeUrl.substring(typeUrl.lastIndexOf("/") + 1),
|
|
766
|
+
msgType = type.root.lookupType(name),
|
|
767
|
+
value = wktFieldValue(type, message, "value"),
|
|
768
|
+
decoded = msgType.decode(value || util.emptyArray),
|
|
769
|
+
body = toJsonValue(msgType, decoded, options, depth + 1),
|
|
770
|
+
result;
|
|
771
|
+
if (WKT_TO[msgType.fullName])
|
|
772
|
+
result = { "@type": typeUrl, "value": body };
|
|
773
|
+
else {
|
|
774
|
+
result = { "@type": typeUrl };
|
|
775
|
+
for (var k in body)
|
|
776
|
+
if (hasOwn(body, k))
|
|
777
|
+
setOwn(result, k, body[k]);
|
|
778
|
+
}
|
|
779
|
+
return result;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
// --- duplicate keys ---
|
|
783
|
+
|
|
784
|
+
// JSON.parse keeps the last duplicate key, but ProtoJSON rejects duplicates.
|
|
785
|
+
function checkDuplicateKeys(str) {
|
|
786
|
+
var stack = [],
|
|
787
|
+
expectKey = false,
|
|
788
|
+
i = 0,
|
|
789
|
+
n = str.length;
|
|
790
|
+
while (i < n) {
|
|
791
|
+
var c = str.charAt(i);
|
|
792
|
+
if (c === "{") {
|
|
793
|
+
stack.push(Object.create(null));
|
|
794
|
+
expectKey = true;
|
|
795
|
+
++i;
|
|
796
|
+
} else if (c === "[") {
|
|
797
|
+
stack.push(null);
|
|
798
|
+
expectKey = false;
|
|
799
|
+
++i;
|
|
800
|
+
} else if (c === "}" || c === "]") {
|
|
801
|
+
stack.pop();
|
|
802
|
+
expectKey = false;
|
|
803
|
+
++i;
|
|
804
|
+
} else if (c === ":") {
|
|
805
|
+
expectKey = false;
|
|
806
|
+
++i;
|
|
807
|
+
} else if (c === ",") {
|
|
808
|
+
expectKey = stack.length > 0 && stack[stack.length - 1] !== null;
|
|
809
|
+
++i;
|
|
810
|
+
} else if (c === "\"") {
|
|
811
|
+
var start = i++;
|
|
812
|
+
while (i < n) {
|
|
813
|
+
var ch = str.charAt(i++);
|
|
814
|
+
if (ch === "\\") ++i;
|
|
815
|
+
else if (ch === "\"") break;
|
|
816
|
+
}
|
|
817
|
+
if (expectKey) {
|
|
818
|
+
var seen = stack[stack.length - 1],
|
|
819
|
+
name = JSON.parse(str.slice(start, i));
|
|
820
|
+
if (seen[name])
|
|
821
|
+
throw Error("duplicate key in JSON object: " + JSON.stringify(name));
|
|
822
|
+
seen[name] = true;
|
|
823
|
+
expectKey = false;
|
|
824
|
+
}
|
|
825
|
+
} else
|
|
826
|
+
++i;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// --- public API ---
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* ProtoJSON conversion options.
|
|
834
|
+
* @interface IProtoJsonOptions
|
|
835
|
+
* @property {boolean} [ignoreUnknownFields=false] Ignores unknown object members and unrecognized enum names while parsing.
|
|
836
|
+
*/
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* Parses a message from an already-parsed ProtoJSON value using the specified reflected type.
|
|
840
|
+
* @function fromJson
|
|
841
|
+
* @name fromJson
|
|
842
|
+
* @param {$protobuf.Type} type Reflected message type
|
|
843
|
+
* @param {*} json Already-parsed ProtoJSON value
|
|
844
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
845
|
+
* @returns {$protobuf.Message<{}>} Message instance
|
|
846
|
+
*/
|
|
847
|
+
protojson.fromJson = function fromJson(type, json, options) {
|
|
848
|
+
if (!(type instanceof Type))
|
|
849
|
+
throw TypeError("type must be a Type");
|
|
850
|
+
type.root.resolveAll();
|
|
851
|
+
return type.create(readMessage(type, json, options || {}, 0));
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Parses a message from ProtoJSON text using the specified reflected type.
|
|
856
|
+
* @function fromJsonString
|
|
857
|
+
* @name fromJsonString
|
|
858
|
+
* @param {$protobuf.Type} type Reflected message type
|
|
859
|
+
* @param {string} json ProtoJSON text
|
|
860
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
861
|
+
* @returns {$protobuf.Message<{}>} Message instance
|
|
862
|
+
*/
|
|
863
|
+
protojson.fromJsonString = function fromJsonString(type, json, options) {
|
|
864
|
+
if (typeof json !== "string")
|
|
865
|
+
throw TypeError("json must be a string");
|
|
866
|
+
checkDuplicateKeys(json);
|
|
867
|
+
return protojson.fromJson(type, JSON.parse(json), options);
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Formats a message as ProtoJSON using the specified reflected type.
|
|
872
|
+
* @function toJson
|
|
873
|
+
* @name toJson
|
|
874
|
+
* @param {$protobuf.Type} type Reflected message type
|
|
875
|
+
* @param {$protobuf.Message<{}>|Object.<string,*>} message Message instance or plain object
|
|
876
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
877
|
+
* @returns {*} ProtoJSON value (object, array, string, number, boolean or null)
|
|
878
|
+
*/
|
|
879
|
+
protojson.toJson = function toJson(type, message, options) {
|
|
880
|
+
if (!(type instanceof Type))
|
|
881
|
+
throw TypeError("type must be a Type");
|
|
882
|
+
type.root.resolveAll();
|
|
883
|
+
return toJsonValue(type, message, options || {}, 0);
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Formats a message as ProtoJSON text using the specified reflected type.
|
|
888
|
+
* @function toJsonString
|
|
889
|
+
* @name toJsonString
|
|
890
|
+
* @param {$protobuf.Type} type Reflected message type
|
|
891
|
+
* @param {$protobuf.Message<{}>|Object.<string,*>} message Message instance or plain object
|
|
892
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
893
|
+
* @returns {string} ProtoJSON text
|
|
894
|
+
*/
|
|
895
|
+
protojson.toJsonString = function toJsonString(type, message, options) {
|
|
896
|
+
return JSON.stringify(protojson.toJson(type, message, options));
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
/**
|
|
900
|
+
* Installs reflected {@link Type} convenience methods.
|
|
901
|
+
* @function install
|
|
902
|
+
* @name install
|
|
903
|
+
* @returns {undefined}
|
|
904
|
+
*/
|
|
905
|
+
protojson.install = function install() {
|
|
906
|
+
/**
|
|
907
|
+
* Parses a message of this type from an already-parsed ProtoJSON value. Convenience for {@link protojson.fromJson}.
|
|
908
|
+
* @param {*} json Already-parsed ProtoJSON value
|
|
909
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
910
|
+
* @returns {Message<{}>} Message instance
|
|
911
|
+
*/
|
|
912
|
+
Type.prototype.fromJson = function fromJson(json, options) {
|
|
913
|
+
return protojson.fromJson(this, json, options);
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* Parses a message of this type from ProtoJSON text. Convenience for {@link protojson.fromJsonString}.
|
|
918
|
+
* @param {string} json ProtoJSON text
|
|
919
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
920
|
+
* @returns {Message<{}>} Message instance
|
|
921
|
+
*/
|
|
922
|
+
Type.prototype.fromJsonString = function fromJsonString(json, options) {
|
|
923
|
+
return protojson.fromJsonString(this, json, options);
|
|
924
|
+
};
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* Formats a message of this type as ProtoJSON. Convenience for {@link protojson.toJson}.
|
|
928
|
+
* @param {Message<{}>|Object.<string,*>} message Message instance or plain object
|
|
929
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
930
|
+
* @returns {*} ProtoJSON value
|
|
931
|
+
*/
|
|
932
|
+
Type.prototype.toJson = function toJson(message, options) {
|
|
933
|
+
return protojson.toJson(this, message, options);
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
/**
|
|
937
|
+
* Formats a message of this type as ProtoJSON text. Convenience for {@link protojson.toJsonString}.
|
|
938
|
+
* @param {Message<{}>|Object.<string,*>} message Message instance or plain object
|
|
939
|
+
* @param {IProtoJsonOptions} [options] Conversion options
|
|
940
|
+
* @returns {string} ProtoJSON text
|
|
941
|
+
*/
|
|
942
|
+
Type.prototype.toJsonString = function toJsonString(message, options) {
|
|
943
|
+
return protojson.toJsonString(this, message, options);
|
|
944
|
+
};
|
|
945
|
+
};
|