@aptre/protobuf-es-lite 0.1.0

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/dist/json.js ADDED
@@ -0,0 +1,624 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clearField = exports.writeMessage = exports.readMessage = exports.makeWriteOptions = exports.makeReadOptions = void 0;
4
+ const protobuf_1 = require("@bufbuild/protobuf");
5
+ const field_js_1 = require("./field.js");
6
+ const assert_js_1 = require("./assert.js");
7
+ const scalar_js_1 = require("./scalar.js");
8
+ const field_wrapper_js_1 = require("./field-wrapper.js");
9
+ // Default options for parsing JSON.
10
+ const jsonReadDefaults = {
11
+ ignoreUnknownFields: false,
12
+ };
13
+ // Default options for serializing to JSON.
14
+ const jsonWriteDefaults = {
15
+ emitDefaultValues: false,
16
+ enumAsInteger: false,
17
+ useProtoFieldName: false,
18
+ prettySpaces: 0,
19
+ };
20
+ function makeReadOptions(options) {
21
+ return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
22
+ }
23
+ exports.makeReadOptions = makeReadOptions;
24
+ function makeWriteOptions(options) {
25
+ return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
26
+ }
27
+ exports.makeWriteOptions = makeWriteOptions;
28
+ function debugJsonValue(json) {
29
+ if (json === null) {
30
+ return "null";
31
+ }
32
+ switch (typeof json) {
33
+ case "object":
34
+ return Array.isArray(json) ? "array" : "object";
35
+ case "string":
36
+ return json.length > 100 ? "string" : `"${json.split('"').join('\\"')}"`;
37
+ default:
38
+ return String(json);
39
+ }
40
+ }
41
+ function readMessage(fields, typeName, json, options, message) {
42
+ if (json == null || Array.isArray(json) || typeof json != "object") {
43
+ throw new Error(`cannot decode message ${typeName} from JSON: ${debugJsonValue(json)}`);
44
+ }
45
+ const oneofSeen = new Map();
46
+ const registry = options.typeRegistry;
47
+ for (const [jsonKey, jsonValue] of Object.entries(json)) {
48
+ const field = fields.findJsonName(jsonKey);
49
+ if (field) {
50
+ if (field.oneof) {
51
+ if (jsonValue === null && field.kind == "scalar") {
52
+ // see conformance test Required.Proto3.JsonInput.OneofFieldNull{First,Second}
53
+ continue;
54
+ }
55
+ const seen = oneofSeen.get(field.oneof);
56
+ if (seen !== undefined) {
57
+ throw new Error(`cannot decode message ${typeName} from JSON: multiple keys for oneof "${field.oneof.name}" present: "${seen}", "${jsonKey}"`);
58
+ }
59
+ oneofSeen.set(field.oneof, jsonKey);
60
+ }
61
+ readField(message, jsonValue, field, options);
62
+ }
63
+ else {
64
+ let found = false;
65
+ if (registry?.findExtension &&
66
+ jsonKey.startsWith("[") &&
67
+ jsonKey.endsWith("]")) {
68
+ // TODO: handle extension
69
+ /*
70
+ const ext = registry.findExtension(
71
+ jsonKey.substring(1, jsonKey.length - 1),
72
+ );
73
+ if (ext && ext.extendee.typeName == typeName) {
74
+ found = true;
75
+ const [container, get] = createExtensionContainer(ext);
76
+ readField(container, jsonValue, ext.field, options, ext);
77
+ // We pass on the options as BinaryReadOptions/BinaryWriteOptions,
78
+ // so that users can bring their own binary reader and writer factories
79
+ // if necessary.
80
+ setExtension(
81
+ message,
82
+ ext as Extension<T>,
83
+ get(),
84
+ options as Partial<BinaryReadOptions & BinaryWriteOptions>,
85
+ );
86
+ }
87
+ */
88
+ }
89
+ if (!found && !options.ignoreUnknownFields) {
90
+ throw new Error(`cannot decode message ${typeName} from JSON: key "${jsonKey}" is unknown`);
91
+ }
92
+ }
93
+ }
94
+ return message;
95
+ }
96
+ exports.readMessage = readMessage;
97
+ function writeMessage(message, fields, options) {
98
+ const json = {};
99
+ let field;
100
+ try {
101
+ for (field of fields.byNumber()) {
102
+ if (!(0, field_js_1.isFieldSet)(field, message)) {
103
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
104
+ if (field.req) {
105
+ throw `required field not set`;
106
+ }
107
+ if (!options.emitDefaultValues) {
108
+ continue;
109
+ }
110
+ if (!canEmitFieldDefaultValue(field)) {
111
+ continue;
112
+ }
113
+ }
114
+ const value = field.oneof
115
+ ? message[field.oneof.localName].value
116
+ : message[field.localName];
117
+ const jsonValue = writeField(field, value, options);
118
+ if (jsonValue !== undefined) {
119
+ json[options.useProtoFieldName ? field.name : field.jsonName] =
120
+ jsonValue;
121
+ }
122
+ }
123
+ // TODO: handle extensions
124
+ /*
125
+ const registry = options.typeRegistry;
126
+ if (registry?.findExtensionFor) {
127
+ for (const uf of listUnknownFields(message)) {
128
+ const ext = registry.findExtensionFor(typeName, uf.no);
129
+ if (ext && hasExtension(message, ext)) {
130
+ // We pass on the options as BinaryReadOptions, so that users can bring their own
131
+ // binary reader factory if necessary.
132
+ const value = getExtension(
133
+ message,
134
+ ext,
135
+ options as Partial<BinaryReadOptions>,
136
+ );
137
+ const jsonValue = writeField(ext.field, value, options);
138
+ if (jsonValue !== undefined) {
139
+ json[ext.field.jsonName] = jsonValue;
140
+ }
141
+ }
142
+ }
143
+ }
144
+ */
145
+ }
146
+ catch (e) {
147
+ const m = field
148
+ ? `cannot encode field ${field.name} to JSON`
149
+ : `cannot encode message to JSON`;
150
+ const r = e instanceof Error ? e.message : String(e);
151
+ throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
152
+ }
153
+ return json;
154
+ }
155
+ exports.writeMessage = writeMessage;
156
+ // Read a JSON value for a field.
157
+ // The "parentType" argument is only used to provide context in errors.
158
+ function readField(target, jsonValue, field, options) {
159
+ let localName = field.localName;
160
+ if (field.repeated) {
161
+ (0, assert_js_1.assert)(field.kind != "map");
162
+ if (jsonValue === null) {
163
+ return;
164
+ }
165
+ if (!Array.isArray(jsonValue)) {
166
+ throw new Error(`cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`);
167
+ }
168
+ const targetArray = target[localName];
169
+ for (const jsonItem of jsonValue) {
170
+ if (jsonItem === null) {
171
+ throw new Error(`cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonItem)}`);
172
+ }
173
+ switch (field.kind) {
174
+ case "message":
175
+ targetArray.push(field.T.fromJson(jsonItem, options));
176
+ break;
177
+ case "enum":
178
+ const enumValue = readEnum(field.T, jsonItem, options.ignoreUnknownFields, true);
179
+ if (enumValue !== tokenIgnoredUnknownEnum) {
180
+ targetArray.push(enumValue);
181
+ }
182
+ break;
183
+ case "scalar":
184
+ try {
185
+ targetArray.push(readScalar(field.T, jsonItem, field.L, true));
186
+ }
187
+ catch (e) {
188
+ let m = `cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonItem)}`;
189
+ if (e instanceof Error && e.message.length > 0) {
190
+ m += `: ${e.message}`;
191
+ }
192
+ throw new Error(m);
193
+ }
194
+ break;
195
+ }
196
+ }
197
+ }
198
+ else if (field.kind == "map") {
199
+ if (jsonValue === null) {
200
+ return;
201
+ }
202
+ if (typeof jsonValue != "object" || Array.isArray(jsonValue)) {
203
+ throw new Error(`cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`);
204
+ }
205
+ const targetMap = target[localName];
206
+ for (const [jsonMapKey, jsonMapValue] of Object.entries(jsonValue)) {
207
+ if (jsonMapValue === null) {
208
+ throw new Error(`cannot decode field ${field.name} from JSON: map value null`);
209
+ }
210
+ let key;
211
+ try {
212
+ key = readMapKey(field.K, jsonMapKey);
213
+ }
214
+ catch (e) {
215
+ let m = `cannot decode map key for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
216
+ if (e instanceof Error && e.message.length > 0) {
217
+ m += `: ${e.message}`;
218
+ }
219
+ throw new Error(m);
220
+ }
221
+ switch (field.V.kind) {
222
+ case "message":
223
+ targetMap[key] = field.V.T.fromJson(jsonMapValue, options);
224
+ break;
225
+ case "enum":
226
+ const enumValue = readEnum(field.V.T, jsonMapValue, options.ignoreUnknownFields, true);
227
+ if (enumValue !== tokenIgnoredUnknownEnum) {
228
+ targetMap[key] = enumValue;
229
+ }
230
+ break;
231
+ case "scalar":
232
+ try {
233
+ targetMap[key] = readScalar(field.V.T, jsonMapValue, protobuf_1.LongType.BIGINT, true);
234
+ }
235
+ catch (e) {
236
+ let m = `cannot decode map value for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
237
+ if (e instanceof Error && e.message.length > 0) {
238
+ m += `: ${e.message}`;
239
+ }
240
+ throw new Error(m);
241
+ }
242
+ break;
243
+ }
244
+ }
245
+ }
246
+ else {
247
+ if (field.oneof) {
248
+ target = target[field.oneof.localName] = { case: localName };
249
+ localName = "value";
250
+ }
251
+ switch (field.kind) {
252
+ case "message":
253
+ const messageType = field.T;
254
+ if (jsonValue === null &&
255
+ messageType.typeName != "google.protobuf.Value") {
256
+ return;
257
+ }
258
+ let currentValue = target[localName];
259
+ target[localName] = currentValue = messageType.fromJson(jsonValue, options);
260
+ if (messageType.fieldWrapper && !field.oneof) {
261
+ target[localName] = messageType.fieldWrapper.unwrapField(currentValue);
262
+ }
263
+ break;
264
+ case "enum":
265
+ const enumValue = readEnum(field.T, jsonValue, options.ignoreUnknownFields, false);
266
+ switch (enumValue) {
267
+ case tokenNull:
268
+ clearField(field, target);
269
+ break;
270
+ case tokenIgnoredUnknownEnum:
271
+ break;
272
+ default:
273
+ target[localName] = enumValue;
274
+ break;
275
+ }
276
+ break;
277
+ case "scalar":
278
+ try {
279
+ const scalarValue = readScalar(field.T, jsonValue, field.L, false);
280
+ switch (scalarValue) {
281
+ case tokenNull:
282
+ clearField(field, target);
283
+ break;
284
+ default:
285
+ target[localName] = scalarValue;
286
+ break;
287
+ }
288
+ }
289
+ catch (e) {
290
+ let m = `cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
291
+ if (e instanceof Error && e.message.length > 0) {
292
+ m += `: ${e.message}`;
293
+ }
294
+ throw new Error(m);
295
+ }
296
+ break;
297
+ }
298
+ }
299
+ }
300
+ const tokenNull = Symbol();
301
+ const tokenIgnoredUnknownEnum = Symbol();
302
+ function readEnum(type, json, ignoreUnknownFields, nullAsZeroValue) {
303
+ if (json === null) {
304
+ if (type.typeName == "google.protobuf.NullValue") {
305
+ return 0; // google.protobuf.NullValue.NULL_VALUE = 0
306
+ }
307
+ return nullAsZeroValue ? type.values[0].no : tokenNull;
308
+ }
309
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
310
+ switch (typeof json) {
311
+ case "number":
312
+ if (Number.isInteger(json)) {
313
+ return json;
314
+ }
315
+ break;
316
+ case "string":
317
+ const value = type.findName(json);
318
+ if (value !== undefined) {
319
+ return value.no;
320
+ }
321
+ if (ignoreUnknownFields) {
322
+ return tokenIgnoredUnknownEnum;
323
+ }
324
+ break;
325
+ }
326
+ throw new Error(`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`);
327
+ }
328
+ function readScalar(type, json, longType, nullAsZeroValue) {
329
+ if (json === null) {
330
+ if (nullAsZeroValue) {
331
+ return (0, scalar_js_1.scalarZeroValue)(type, longType);
332
+ }
333
+ return tokenNull;
334
+ }
335
+ // every valid case in the switch below returns, and every fall
336
+ // through is regarded as a failure.
337
+ switch (type) {
338
+ // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
339
+ // Either numbers or strings are accepted. Exponent notation is also accepted.
340
+ case protobuf_1.ScalarType.DOUBLE:
341
+ case protobuf_1.ScalarType.FLOAT:
342
+ if (json === "NaN")
343
+ return Number.NaN;
344
+ if (json === "Infinity")
345
+ return Number.POSITIVE_INFINITY;
346
+ if (json === "-Infinity")
347
+ return Number.NEGATIVE_INFINITY;
348
+ if (json === "") {
349
+ // empty string is not a number
350
+ break;
351
+ }
352
+ if (typeof json == "string" && json.trim().length !== json.length) {
353
+ // extra whitespace
354
+ break;
355
+ }
356
+ if (typeof json != "string" && typeof json != "number") {
357
+ break;
358
+ }
359
+ const float = Number(json);
360
+ if (Number.isNaN(float)) {
361
+ // not a number
362
+ break;
363
+ }
364
+ if (!Number.isFinite(float)) {
365
+ // infinity and -infinity are handled by string representation above, so this is an error
366
+ break;
367
+ }
368
+ if (type == protobuf_1.ScalarType.FLOAT)
369
+ (0, assert_js_1.assertFloat32)(float);
370
+ return float;
371
+ // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
372
+ case protobuf_1.ScalarType.INT32:
373
+ case protobuf_1.ScalarType.FIXED32:
374
+ case protobuf_1.ScalarType.SFIXED32:
375
+ case protobuf_1.ScalarType.SINT32:
376
+ case protobuf_1.ScalarType.UINT32:
377
+ let int32;
378
+ if (typeof json == "number")
379
+ int32 = json;
380
+ else if (typeof json == "string" && json.length > 0) {
381
+ if (json.trim().length === json.length)
382
+ int32 = Number(json);
383
+ }
384
+ if (int32 === undefined)
385
+ break;
386
+ if (type == protobuf_1.ScalarType.UINT32 || type == protobuf_1.ScalarType.FIXED32)
387
+ (0, assert_js_1.assertUInt32)(int32);
388
+ else
389
+ (0, assert_js_1.assertInt32)(int32);
390
+ return int32;
391
+ // int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
392
+ case protobuf_1.ScalarType.INT64:
393
+ case protobuf_1.ScalarType.SFIXED64:
394
+ case protobuf_1.ScalarType.SINT64:
395
+ if (typeof json != "number" && typeof json != "string")
396
+ break;
397
+ const long = protobuf_1.protoInt64.parse(json);
398
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
399
+ return longType ? long.toString() : long;
400
+ case protobuf_1.ScalarType.FIXED64:
401
+ case protobuf_1.ScalarType.UINT64:
402
+ if (typeof json != "number" && typeof json != "string")
403
+ break;
404
+ const uLong = protobuf_1.protoInt64.uParse(json);
405
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
406
+ return longType ? uLong.toString() : uLong;
407
+ // bool:
408
+ case protobuf_1.ScalarType.BOOL:
409
+ if (typeof json !== "boolean")
410
+ break;
411
+ return json;
412
+ // string:
413
+ case protobuf_1.ScalarType.STRING:
414
+ if (typeof json !== "string") {
415
+ break;
416
+ }
417
+ // A string must always contain UTF-8 encoded or 7-bit ASCII.
418
+ // We validate with encodeURIComponent, which appears to be the fastest widely available option.
419
+ try {
420
+ encodeURIComponent(json);
421
+ }
422
+ catch (e) {
423
+ throw new Error("invalid UTF8");
424
+ }
425
+ return json;
426
+ // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
427
+ // Either standard or URL-safe base64 encoding with/without paddings are accepted.
428
+ case protobuf_1.ScalarType.BYTES:
429
+ if (json === "")
430
+ return new Uint8Array(0);
431
+ if (typeof json !== "string")
432
+ break;
433
+ return protobuf_1.protoBase64.dec(json);
434
+ }
435
+ throw new Error();
436
+ }
437
+ function readMapKey(type, json) {
438
+ if (type === protobuf_1.ScalarType.BOOL) {
439
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
440
+ switch (json) {
441
+ case "true":
442
+ json = true;
443
+ break;
444
+ case "false":
445
+ json = false;
446
+ break;
447
+ }
448
+ }
449
+ return readScalar(type, json, protobuf_1.LongType.BIGINT, true).toString();
450
+ }
451
+ /**
452
+ * Resets the field, so that isFieldSet() will return false.
453
+ */
454
+ function clearField(field, target) {
455
+ const localName = field.localName;
456
+ const implicitPresence = !field.opt && !field.req;
457
+ if (field.repeated) {
458
+ target[localName] = [];
459
+ }
460
+ else if (field.oneof) {
461
+ target[field.oneof.localName] = { case: undefined };
462
+ }
463
+ else {
464
+ switch (field.kind) {
465
+ case "map":
466
+ target[localName] = {};
467
+ break;
468
+ case "enum":
469
+ target[localName] = implicitPresence ? field.T.values[0].no : undefined;
470
+ break;
471
+ case "scalar":
472
+ target[localName] = implicitPresence
473
+ ? (0, scalar_js_1.scalarZeroValue)(field.T, field.L)
474
+ : undefined;
475
+ break;
476
+ case "message":
477
+ target[localName] = undefined;
478
+ break;
479
+ }
480
+ }
481
+ }
482
+ exports.clearField = clearField;
483
+ // Decide whether an unset field should be emitted with JSON write option `emitDefaultValues`
484
+ function canEmitFieldDefaultValue(field) {
485
+ if (field.repeated || field.kind == "map") {
486
+ // maps are {}, repeated fields are []
487
+ return true;
488
+ }
489
+ if (field.oneof) {
490
+ // oneof fields are never emitted
491
+ return false;
492
+ }
493
+ if (field.kind == "message") {
494
+ // singular message field are allowed to emit JSON null, but we do not
495
+ return false;
496
+ }
497
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
498
+ if (field.opt || field.req) {
499
+ // the field uses explicit presence, so we cannot emit a zero value
500
+ return false;
501
+ }
502
+ return true;
503
+ }
504
+ function writeField(field, value, options) {
505
+ if (field.kind == "map") {
506
+ (0, assert_js_1.assert)(typeof value == "object" && value != null);
507
+ const jsonObj = {};
508
+ const entries = Object.entries(value);
509
+ switch (field.V.kind) {
510
+ case "scalar":
511
+ for (const [entryKey, entryValue] of entries) {
512
+ jsonObj[entryKey.toString()] = writeScalar(field.V.T, entryValue); // JSON standard allows only (double quoted) string as property key
513
+ }
514
+ break;
515
+ case "message":
516
+ for (const [entryKey, entryValue] of entries) {
517
+ // JSON standard allows only (double quoted) string as property key
518
+ jsonObj[entryKey.toString()] = field.V.T.toJson(entryValue, options);
519
+ }
520
+ break;
521
+ case "enum":
522
+ const enumType = field.V.T;
523
+ for (const [entryKey, entryValue] of entries) {
524
+ // JSON standard allows only (double quoted) string as property key
525
+ jsonObj[entryKey.toString()] = writeEnum(enumType, entryValue, options.enumAsInteger);
526
+ }
527
+ break;
528
+ }
529
+ return options.emitDefaultValues || entries.length > 0
530
+ ? jsonObj
531
+ : undefined;
532
+ }
533
+ if (field.repeated) {
534
+ (0, assert_js_1.assert)(Array.isArray(value));
535
+ const jsonArr = [];
536
+ switch (field.kind) {
537
+ case "scalar":
538
+ for (let i = 0; i < value.length; i++) {
539
+ jsonArr.push(writeScalar(field.T, value[i]));
540
+ }
541
+ break;
542
+ case "enum":
543
+ for (let i = 0; i < value.length; i++) {
544
+ jsonArr.push(writeEnum(field.T, value[i], options.enumAsInteger));
545
+ }
546
+ break;
547
+ case "message":
548
+ for (let i = 0; i < value.length; i++) {
549
+ jsonArr.push(value[i].toJson(options));
550
+ }
551
+ break;
552
+ }
553
+ return options.emitDefaultValues || jsonArr.length > 0
554
+ ? jsonArr
555
+ : undefined;
556
+ }
557
+ switch (field.kind) {
558
+ case "scalar":
559
+ return writeScalar(field.T, value);
560
+ case "enum":
561
+ return writeEnum(field.T, value, options.enumAsInteger);
562
+ case "message":
563
+ return (0, field_wrapper_js_1.wrapField)(field.T.fieldWrapper, value).toJson(options);
564
+ }
565
+ }
566
+ function writeScalar(type, value) {
567
+ switch (type) {
568
+ // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
569
+ case protobuf_1.ScalarType.INT32:
570
+ case protobuf_1.ScalarType.SFIXED32:
571
+ case protobuf_1.ScalarType.SINT32:
572
+ case protobuf_1.ScalarType.FIXED32:
573
+ case protobuf_1.ScalarType.UINT32:
574
+ (0, assert_js_1.assert)(typeof value == "number");
575
+ return value;
576
+ // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
577
+ // Either numbers or strings are accepted. Exponent notation is also accepted.
578
+ case protobuf_1.ScalarType.FLOAT:
579
+ // assertFloat32(value);
580
+ case protobuf_1.ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
581
+ (0, assert_js_1.assert)(typeof value == "number");
582
+ if (Number.isNaN(value))
583
+ return "NaN";
584
+ if (value === Number.POSITIVE_INFINITY)
585
+ return "Infinity";
586
+ if (value === Number.NEGATIVE_INFINITY)
587
+ return "-Infinity";
588
+ return value;
589
+ // string:
590
+ case protobuf_1.ScalarType.STRING:
591
+ (0, assert_js_1.assert)(typeof value == "string");
592
+ return value;
593
+ // bool:
594
+ case protobuf_1.ScalarType.BOOL:
595
+ (0, assert_js_1.assert)(typeof value == "boolean");
596
+ return value;
597
+ // JSON value will be a decimal string. Either numbers or strings are accepted.
598
+ case protobuf_1.ScalarType.UINT64:
599
+ case protobuf_1.ScalarType.FIXED64:
600
+ case protobuf_1.ScalarType.INT64:
601
+ case protobuf_1.ScalarType.SFIXED64:
602
+ case protobuf_1.ScalarType.SINT64:
603
+ (0, assert_js_1.assert)(typeof value == "bigint" ||
604
+ typeof value == "string" ||
605
+ typeof value == "number");
606
+ return value.toString();
607
+ // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
608
+ // Either standard or URL-safe base64 encoding with/without paddings are accepted.
609
+ case protobuf_1.ScalarType.BYTES:
610
+ (0, assert_js_1.assert)(value instanceof Uint8Array);
611
+ return protobuf_1.protoBase64.enc(value);
612
+ }
613
+ }
614
+ function writeEnum(type, value, enumAsInteger) {
615
+ (0, assert_js_1.assert)(typeof value == "number");
616
+ if (type.typeName == "google.protobuf.NullValue") {
617
+ return null;
618
+ }
619
+ if (enumAsInteger) {
620
+ return value;
621
+ }
622
+ const val = type.findNumber(value);
623
+ return val?.name ?? value; // if we don't know the enum value, just return the number
624
+ }