@bufbuild/protobuf 2.10.2 → 2.12.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/cjs/clone.js +1 -1
- package/dist/cjs/codegenv1/enum.js +1 -1
- package/dist/cjs/codegenv1/extension.js +1 -1
- package/dist/cjs/codegenv1/file.js +1 -1
- package/dist/cjs/codegenv1/index.js +1 -1
- package/dist/cjs/codegenv1/message.js +1 -1
- package/dist/cjs/codegenv1/service.js +1 -1
- package/dist/cjs/codegenv1/symbols.js +1 -1
- package/dist/cjs/codegenv1/types.js +1 -1
- package/dist/cjs/codegenv2/boot.js +1 -1
- package/dist/cjs/codegenv2/embed.js +1 -1
- package/dist/cjs/codegenv2/enum.js +1 -1
- package/dist/cjs/codegenv2/extension.js +1 -1
- package/dist/cjs/codegenv2/file.js +1 -1
- package/dist/cjs/codegenv2/index.js +1 -1
- package/dist/cjs/codegenv2/message.js +1 -1
- package/dist/cjs/codegenv2/restore-json-names.js +1 -1
- package/dist/cjs/codegenv2/scalar.js +1 -1
- package/dist/cjs/codegenv2/service.js +1 -1
- package/dist/cjs/codegenv2/symbols.js +1 -1
- package/dist/cjs/codegenv2/types.js +1 -1
- package/dist/cjs/create.js +1 -1
- package/dist/cjs/descriptors.d.ts +12 -5
- package/dist/cjs/descriptors.js +1 -1
- package/dist/cjs/equals.js +1 -1
- package/dist/cjs/extensions.js +1 -1
- package/dist/cjs/fields.js +1 -1
- package/dist/cjs/from-binary.js +8 -8
- package/dist/cjs/from-json.d.ts +1 -1
- package/dist/cjs/from-json.js +81 -89
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/is-message.js +1 -1
- package/dist/cjs/json-value.js +1 -1
- package/dist/cjs/merge.js +1 -1
- package/dist/cjs/proto-int64.js +1 -1
- package/dist/cjs/reflect/error.js +1 -1
- package/dist/cjs/reflect/guard.js +1 -1
- package/dist/cjs/reflect/index.js +1 -1
- package/dist/cjs/reflect/names.d.ts +11 -0
- package/dist/cjs/reflect/names.js +15 -1
- package/dist/cjs/reflect/nested-types.js +1 -1
- package/dist/cjs/reflect/path.d.ts +1 -1
- package/dist/cjs/reflect/path.js +1 -1
- package/dist/cjs/reflect/reflect-check.js +1 -1
- package/dist/cjs/reflect/reflect-types.js +1 -1
- package/dist/cjs/reflect/reflect.js +1 -1
- package/dist/cjs/reflect/scalar.js +1 -1
- package/dist/cjs/reflect/unsafe.js +1 -1
- package/dist/cjs/registry.js +25 -2
- package/dist/cjs/to-binary.js +1 -1
- package/dist/cjs/to-json.d.ts +1 -1
- package/dist/cjs/to-json.js +12 -15
- package/dist/cjs/types.d.ts +1 -1
- package/dist/cjs/types.js +1 -1
- package/dist/cjs/wire/base64-encoding.js +1 -1
- package/dist/cjs/wire/binary-encoding.d.ts +9 -7
- package/dist/cjs/wire/binary-encoding.js +19 -9
- package/dist/cjs/wire/index.js +1 -1
- package/dist/cjs/wire/size-delimited.js +1 -1
- package/dist/cjs/wire/text-encoding.d.ts +4 -2
- package/dist/cjs/wire/text-encoding.js +9 -2
- package/dist/cjs/wire/text-format.js +1 -1
- package/dist/cjs/wkt/any.js +1 -1
- package/dist/cjs/wkt/duration.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/any_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/api_pb.d.ts +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/api_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +2 -2
- package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/cpp_features_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +37 -23
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +2 -2
- package/dist/cjs/wkt/gen/google/protobuf/duration_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/empty_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.d.ts +32 -36
- package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/go_features_pb.d.ts +65 -0
- package/dist/cjs/wkt/gen/google/protobuf/go_features_pb.js +39 -3
- package/dist/cjs/wkt/gen/google/protobuf/java_features_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/struct_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.d.ts +5 -5
- package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/type_pb.d.ts +3 -3
- package/dist/cjs/wkt/gen/google/protobuf/type_pb.js +1 -1
- package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.js +1 -1
- package/dist/cjs/wkt/index.js +1 -1
- package/dist/cjs/wkt/timestamp.js +1 -1
- package/dist/cjs/wkt/wrappers.d.ts +10 -0
- package/dist/cjs/wkt/wrappers.js +25 -1
- package/dist/esm/clone.js +1 -1
- package/dist/esm/codegenv1/enum.js +1 -1
- package/dist/esm/codegenv1/extension.js +1 -1
- package/dist/esm/codegenv1/file.js +1 -1
- package/dist/esm/codegenv1/index.js +1 -1
- package/dist/esm/codegenv1/message.js +1 -1
- package/dist/esm/codegenv1/service.js +1 -1
- package/dist/esm/codegenv1/symbols.js +1 -1
- package/dist/esm/codegenv1/types.js +1 -1
- package/dist/esm/codegenv2/boot.js +1 -1
- package/dist/esm/codegenv2/embed.js +1 -1
- package/dist/esm/codegenv2/enum.js +1 -1
- package/dist/esm/codegenv2/extension.js +1 -1
- package/dist/esm/codegenv2/file.js +1 -1
- package/dist/esm/codegenv2/index.js +1 -1
- package/dist/esm/codegenv2/message.js +1 -1
- package/dist/esm/codegenv2/restore-json-names.js +1 -1
- package/dist/esm/codegenv2/scalar.js +1 -1
- package/dist/esm/codegenv2/service.js +1 -1
- package/dist/esm/codegenv2/symbols.js +1 -1
- package/dist/esm/codegenv2/types.js +1 -1
- package/dist/esm/create.js +1 -1
- package/dist/esm/descriptors.d.ts +12 -5
- package/dist/esm/descriptors.js +1 -1
- package/dist/esm/equals.js +1 -1
- package/dist/esm/extensions.js +1 -1
- package/dist/esm/fields.js +1 -1
- package/dist/esm/from-binary.js +8 -8
- package/dist/esm/from-json.d.ts +1 -1
- package/dist/esm/from-json.js +82 -90
- package/dist/esm/index.js +1 -1
- package/dist/esm/is-message.js +1 -1
- package/dist/esm/json-value.js +1 -1
- package/dist/esm/merge.js +1 -1
- package/dist/esm/proto-int64.js +1 -1
- package/dist/esm/reflect/error.js +1 -1
- package/dist/esm/reflect/guard.js +1 -1
- package/dist/esm/reflect/index.js +1 -1
- package/dist/esm/reflect/names.d.ts +11 -0
- package/dist/esm/reflect/names.js +14 -1
- package/dist/esm/reflect/nested-types.js +1 -1
- package/dist/esm/reflect/path.d.ts +1 -1
- package/dist/esm/reflect/path.js +1 -1
- package/dist/esm/reflect/reflect-check.js +1 -1
- package/dist/esm/reflect/reflect-types.js +1 -1
- package/dist/esm/reflect/reflect.js +1 -1
- package/dist/esm/reflect/scalar.js +1 -1
- package/dist/esm/reflect/unsafe.js +1 -1
- package/dist/esm/registry.js +25 -2
- package/dist/esm/to-binary.js +1 -1
- package/dist/esm/to-json.d.ts +1 -1
- package/dist/esm/to-json.js +14 -17
- package/dist/esm/types.d.ts +1 -1
- package/dist/esm/types.js +1 -1
- package/dist/esm/wire/base64-encoding.js +1 -1
- package/dist/esm/wire/binary-encoding.d.ts +9 -7
- package/dist/esm/wire/binary-encoding.js +19 -9
- package/dist/esm/wire/index.js +1 -1
- package/dist/esm/wire/size-delimited.js +1 -1
- package/dist/esm/wire/text-encoding.d.ts +4 -2
- package/dist/esm/wire/text-encoding.js +9 -2
- package/dist/esm/wire/text-format.js +1 -1
- package/dist/esm/wkt/any.js +1 -1
- package/dist/esm/wkt/duration.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/any_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/api_pb.d.ts +1 -1
- package/dist/esm/wkt/gen/google/protobuf/api_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +2 -2
- package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/cpp_features_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +37 -23
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +2 -2
- package/dist/esm/wkt/gen/google/protobuf/duration_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/empty_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.d.ts +32 -36
- package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/go_features_pb.d.ts +65 -0
- package/dist/esm/wkt/gen/google/protobuf/go_features_pb.js +38 -2
- package/dist/esm/wkt/gen/google/protobuf/java_features_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/source_context_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/struct_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.d.ts +5 -5
- package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/type_pb.d.ts +3 -3
- package/dist/esm/wkt/gen/google/protobuf/type_pb.js +1 -1
- package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.js +1 -1
- package/dist/esm/wkt/index.js +1 -1
- package/dist/esm/wkt/timestamp.js +1 -1
- package/dist/esm/wkt/wrappers.d.ts +10 -0
- package/dist/esm/wkt/wrappers.js +24 -1
- package/package.json +1 -1
package/dist/esm/from-json.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2026 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -17,9 +17,9 @@ import { create } from "./create.js";
|
|
|
17
17
|
import { reflect } from "./reflect/reflect.js";
|
|
18
18
|
import { FieldError, isFieldError } from "./reflect/error.js";
|
|
19
19
|
import { formatVal } from "./reflect/reflect-check.js";
|
|
20
|
-
import {
|
|
20
|
+
import { protoSnakeCase } from "./reflect/names.js";
|
|
21
21
|
import { base64Decode } from "./wire/base64-encoding.js";
|
|
22
|
-
import { isWrapperDesc, anyPack, ListValueSchema, NullValue, StructSchema, ValueSchema, } from "./wkt/index.js";
|
|
22
|
+
import { hasCustomJsonRepresentation, isWrapperDesc, anyPack, ListValueSchema, NullValue, StructSchema, ValueSchema, } from "./wkt/index.js";
|
|
23
23
|
import { createExtensionContainer, setExtension } from "./extensions.js";
|
|
24
24
|
// Default options for parsing JSON.
|
|
25
25
|
const jsonReadDefaults = {
|
|
@@ -93,11 +93,7 @@ export function mergeFromJson(schema, target, json, options) {
|
|
|
93
93
|
* Parses an enum value from JSON.
|
|
94
94
|
*/
|
|
95
95
|
export function enumFromJson(descEnum, json) {
|
|
96
|
-
|
|
97
|
-
if (val === tokenIgnoredUnknownEnum) {
|
|
98
|
-
throw new Error(`cannot decode ${descEnum} from JSON: ${formatVal(json)}`);
|
|
99
|
-
}
|
|
100
|
-
return val;
|
|
96
|
+
return readEnum(descEnum, json, false);
|
|
101
97
|
}
|
|
102
98
|
/**
|
|
103
99
|
* Is the given value a JSON enum value?
|
|
@@ -105,6 +101,18 @@ export function enumFromJson(descEnum, json) {
|
|
|
105
101
|
export function isEnumJson(descEnum, value) {
|
|
106
102
|
return undefined !== descEnum.values.find((v) => v.name === value);
|
|
107
103
|
}
|
|
104
|
+
const messageJsonFields = new WeakMap();
|
|
105
|
+
function getJsonField(desc, jsonKey) {
|
|
106
|
+
var _a;
|
|
107
|
+
if (!messageJsonFields.has(desc)) {
|
|
108
|
+
const jsonNames = new Map();
|
|
109
|
+
for (const field of desc.fields) {
|
|
110
|
+
jsonNames.set(field.name, field).set(field.jsonName, field);
|
|
111
|
+
}
|
|
112
|
+
messageJsonFields.set(desc, jsonNames);
|
|
113
|
+
}
|
|
114
|
+
return (_a = messageJsonFields.get(desc)) === null || _a === void 0 ? void 0 : _a.get(jsonKey);
|
|
115
|
+
}
|
|
108
116
|
function readMessage(msg, json, opts) {
|
|
109
117
|
var _a;
|
|
110
118
|
if (tryWktFromJson(msg, json, opts)) {
|
|
@@ -114,12 +122,8 @@ function readMessage(msg, json, opts) {
|
|
|
114
122
|
throw new Error(`cannot decode ${msg.desc} from JSON: ${formatVal(json)}`);
|
|
115
123
|
}
|
|
116
124
|
const oneofSeen = new Map();
|
|
117
|
-
const jsonNames = new Map();
|
|
118
|
-
for (const field of msg.desc.fields) {
|
|
119
|
-
jsonNames.set(field.name, field).set(field.jsonName, field);
|
|
120
|
-
}
|
|
121
125
|
for (const [jsonKey, jsonValue] of Object.entries(json)) {
|
|
122
|
-
const field =
|
|
126
|
+
const field = getJsonField(msg.desc, jsonKey);
|
|
123
127
|
if (field) {
|
|
124
128
|
if (field.oneof) {
|
|
125
129
|
if (jsonValue === null && field.fieldKind == "scalar") {
|
|
@@ -170,6 +174,20 @@ function readField(msg, field, json, opts) {
|
|
|
170
174
|
break;
|
|
171
175
|
}
|
|
172
176
|
}
|
|
177
|
+
function readListOrMapItem(field, json, opts) {
|
|
178
|
+
if (field.scalar && json !== null) {
|
|
179
|
+
return scalarFromJson(field, json);
|
|
180
|
+
}
|
|
181
|
+
if (field.message && !isResetSentinelNullValue(field, json)) {
|
|
182
|
+
const msgValue = reflect(field.message);
|
|
183
|
+
readMessage(msgValue, json, opts);
|
|
184
|
+
return msgValue;
|
|
185
|
+
}
|
|
186
|
+
if (field.enum && !isResetSentinelNullValue(field, json)) {
|
|
187
|
+
return readEnum(field.enum, json, opts.ignoreUnknownFields);
|
|
188
|
+
}
|
|
189
|
+
throw new FieldError(field, `${field.fieldKind === "list" ? "list item" : "map value"} must not be null`);
|
|
190
|
+
}
|
|
173
191
|
function readMapField(map, json, opts) {
|
|
174
192
|
if (json === null) {
|
|
175
193
|
return;
|
|
@@ -179,28 +197,11 @@ function readMapField(map, json, opts) {
|
|
|
179
197
|
throw new FieldError(field, "expected object, got " + formatVal(json));
|
|
180
198
|
}
|
|
181
199
|
for (const [jsonMapKey, jsonMapValue] of Object.entries(json)) {
|
|
182
|
-
if (jsonMapValue === null && !isSafeNullValueInListOrMap(field)) {
|
|
183
|
-
throw new FieldError(field, "map value must not be null");
|
|
184
|
-
}
|
|
185
|
-
let value;
|
|
186
|
-
switch (field.mapKind) {
|
|
187
|
-
case "message":
|
|
188
|
-
const msgValue = reflect(field.message);
|
|
189
|
-
readMessage(msgValue, jsonMapValue, opts);
|
|
190
|
-
value = msgValue;
|
|
191
|
-
break;
|
|
192
|
-
case "enum":
|
|
193
|
-
value = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true);
|
|
194
|
-
if (value === tokenIgnoredUnknownEnum) {
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
break;
|
|
198
|
-
case "scalar":
|
|
199
|
-
value = scalarFromJson(field, jsonMapValue, true);
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
200
|
const key = mapKeyFromJson(field.mapKey, jsonMapKey);
|
|
203
|
-
|
|
201
|
+
const value = readListOrMapItem(field, jsonMapValue, opts);
|
|
202
|
+
if (value !== tokenIgnoredUnknownEnum) {
|
|
203
|
+
map.set(key, value);
|
|
204
|
+
}
|
|
204
205
|
}
|
|
205
206
|
}
|
|
206
207
|
function readListField(list, json, opts) {
|
|
@@ -212,34 +213,14 @@ function readListField(list, json, opts) {
|
|
|
212
213
|
throw new FieldError(field, "expected Array, got " + formatVal(json));
|
|
213
214
|
}
|
|
214
215
|
for (const jsonItem of json) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
switch (field.listKind) {
|
|
219
|
-
case "message":
|
|
220
|
-
const msgValue = reflect(field.message);
|
|
221
|
-
readMessage(msgValue, jsonItem, opts);
|
|
222
|
-
list.add(msgValue);
|
|
223
|
-
break;
|
|
224
|
-
case "enum":
|
|
225
|
-
const enumValue = readEnum(field.enum, jsonItem, opts.ignoreUnknownFields, true);
|
|
226
|
-
if (enumValue !== tokenIgnoredUnknownEnum) {
|
|
227
|
-
list.add(enumValue);
|
|
228
|
-
}
|
|
229
|
-
break;
|
|
230
|
-
case "scalar":
|
|
231
|
-
list.add(scalarFromJson(field, jsonItem, true));
|
|
232
|
-
break;
|
|
216
|
+
const value = readListOrMapItem(field, jsonItem, opts);
|
|
217
|
+
if (value !== tokenIgnoredUnknownEnum) {
|
|
218
|
+
list.add(value);
|
|
233
219
|
}
|
|
234
220
|
}
|
|
235
221
|
}
|
|
236
|
-
function isSafeNullValueInListOrMap(field) {
|
|
237
|
-
var _a, _b;
|
|
238
|
-
return (((_a = field.message) === null || _a === void 0 ? void 0 : _a.typeName) == "google.protobuf.Value" ||
|
|
239
|
-
((_b = field.enum) === null || _b === void 0 ? void 0 : _b.typeName) == "google.protobuf.NullValue");
|
|
240
|
-
}
|
|
241
222
|
function readMessageField(msg, field, json, opts) {
|
|
242
|
-
if (
|
|
223
|
+
if (isResetSentinelNullValue(field, json)) {
|
|
243
224
|
msg.clear(field);
|
|
244
225
|
return;
|
|
245
226
|
}
|
|
@@ -248,30 +229,44 @@ function readMessageField(msg, field, json, opts) {
|
|
|
248
229
|
msg.set(field, msgValue);
|
|
249
230
|
}
|
|
250
231
|
function readEnumField(msg, field, json, opts) {
|
|
251
|
-
|
|
252
|
-
if (enumValue === tokenNull) {
|
|
232
|
+
if (isResetSentinelNullValue(field, json)) {
|
|
253
233
|
msg.clear(field);
|
|
234
|
+
return;
|
|
254
235
|
}
|
|
255
|
-
|
|
236
|
+
const enumValue = readEnum(field.enum, json, opts.ignoreUnknownFields);
|
|
237
|
+
if (enumValue !== tokenIgnoredUnknownEnum) {
|
|
256
238
|
msg.set(field, enumValue);
|
|
257
239
|
}
|
|
258
240
|
}
|
|
259
241
|
function readScalarField(msg, field, json) {
|
|
260
|
-
|
|
261
|
-
if (scalarValue === tokenNull) {
|
|
242
|
+
if (json === null) {
|
|
262
243
|
msg.clear(field);
|
|
263
244
|
}
|
|
264
245
|
else {
|
|
265
|
-
msg.set(field,
|
|
246
|
+
msg.set(field, scalarFromJson(field, json));
|
|
266
247
|
}
|
|
267
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Indicates whether a value is a sentinel for reseting a field.
|
|
251
|
+
*
|
|
252
|
+
* For this to be true, the value must be a JSON null and the field must not
|
|
253
|
+
* permit a present, Protobuf-serializable null.
|
|
254
|
+
*
|
|
255
|
+
* Only message google.protobuf.Value and enum google.protobuf.NullValue fields
|
|
256
|
+
* permit Protobuf-serializable nulls.
|
|
257
|
+
*
|
|
258
|
+
* Note that field-resetting sentinel nulls are not permitted in lists and maps.
|
|
259
|
+
*/
|
|
260
|
+
function isResetSentinelNullValue(field, json) {
|
|
261
|
+
var _a, _b;
|
|
262
|
+
return (json === null &&
|
|
263
|
+
((_a = field.message) === null || _a === void 0 ? void 0 : _a.typeName) != "google.protobuf.Value" &&
|
|
264
|
+
((_b = field.enum) === null || _b === void 0 ? void 0 : _b.typeName) != "google.protobuf.NullValue");
|
|
265
|
+
}
|
|
268
266
|
const tokenIgnoredUnknownEnum = Symbol();
|
|
269
|
-
function readEnum(desc, json, ignoreUnknownFields
|
|
267
|
+
function readEnum(desc, json, ignoreUnknownFields) {
|
|
270
268
|
if (json === null) {
|
|
271
|
-
|
|
272
|
-
return 0; // google.protobuf.NullValue.NULL_VALUE = 0
|
|
273
|
-
}
|
|
274
|
-
return nullAsZeroValue ? desc.values[0].number : tokenNull;
|
|
269
|
+
return desc.values[0].number;
|
|
275
270
|
}
|
|
276
271
|
switch (typeof json) {
|
|
277
272
|
case "number":
|
|
@@ -291,14 +286,13 @@ function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
|
|
|
291
286
|
}
|
|
292
287
|
throw new Error(`cannot decode ${desc} from JSON: ${formatVal(json)}`);
|
|
293
288
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
}
|
|
289
|
+
/**
|
|
290
|
+
* Try to parse a JSON value to a scalar value for the reflect API.
|
|
291
|
+
*
|
|
292
|
+
* Returns the input if the JSON value cannot be converted. Raises a FieldError
|
|
293
|
+
* if conversion would be ambiguous.
|
|
294
|
+
*/
|
|
295
|
+
function scalarFromJson(field, json) {
|
|
302
296
|
// int64, sfixed64, sint64, fixed64, uint64: Reflect supports string and number.
|
|
303
297
|
// string, bool: Supported by reflect.
|
|
304
298
|
switch (field.scalar) {
|
|
@@ -371,24 +365,24 @@ function scalarFromJson(field, json, nullAsZeroValue) {
|
|
|
371
365
|
*
|
|
372
366
|
* Returns the input if the JSON value cannot be converted.
|
|
373
367
|
*/
|
|
374
|
-
function mapKeyFromJson(type,
|
|
368
|
+
function mapKeyFromJson(type, jsonString) {
|
|
375
369
|
switch (type) {
|
|
376
370
|
case ScalarType.BOOL:
|
|
377
|
-
switch (
|
|
371
|
+
switch (jsonString) {
|
|
378
372
|
case "true":
|
|
379
373
|
return true;
|
|
380
374
|
case "false":
|
|
381
375
|
return false;
|
|
382
376
|
}
|
|
383
|
-
return
|
|
377
|
+
return jsonString;
|
|
384
378
|
case ScalarType.INT32:
|
|
385
379
|
case ScalarType.FIXED32:
|
|
386
380
|
case ScalarType.UINT32:
|
|
387
381
|
case ScalarType.SFIXED32:
|
|
388
382
|
case ScalarType.SINT32:
|
|
389
|
-
return int32FromJson(
|
|
383
|
+
return int32FromJson(jsonString);
|
|
390
384
|
default:
|
|
391
|
-
return
|
|
385
|
+
return jsonString;
|
|
392
386
|
}
|
|
393
387
|
}
|
|
394
388
|
/**
|
|
@@ -459,7 +453,7 @@ function tryWktFromJson(msg, jsonValue, opts) {
|
|
|
459
453
|
msg.clear(valueField);
|
|
460
454
|
}
|
|
461
455
|
else {
|
|
462
|
-
msg.set(valueField, scalarFromJson(valueField, jsonValue
|
|
456
|
+
msg.set(valueField, scalarFromJson(valueField, jsonValue));
|
|
463
457
|
}
|
|
464
458
|
return true;
|
|
465
459
|
}
|
|
@@ -489,7 +483,7 @@ function anyFromJson(any, json, opts) {
|
|
|
489
483
|
throw new Error(`cannot decode message ${any.$typeName} from JSON: ${typeUrl} is not in the type registry`);
|
|
490
484
|
}
|
|
491
485
|
const msg = reflect(desc);
|
|
492
|
-
if (
|
|
486
|
+
if (hasCustomJsonRepresentation(desc) &&
|
|
493
487
|
Object.prototype.hasOwnProperty.call(json, "value")) {
|
|
494
488
|
const value = json.value;
|
|
495
489
|
readMessage(msg, value, opts);
|
|
@@ -557,14 +551,12 @@ function fieldMaskFromJson(fieldMask, json) {
|
|
|
557
551
|
if (json === "") {
|
|
558
552
|
return;
|
|
559
553
|
}
|
|
560
|
-
|
|
561
|
-
if (
|
|
554
|
+
fieldMask.paths = json.split(",").map((path) => {
|
|
555
|
+
if (path.includes("_")) {
|
|
562
556
|
throw new Error(`cannot decode message ${fieldMask.$typeName} from JSON: path names must be lowerCamelCase`);
|
|
563
557
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
}
|
|
567
|
-
fieldMask.paths = json.split(",").map(camelToSnake);
|
|
558
|
+
return protoSnakeCase(path);
|
|
559
|
+
});
|
|
568
560
|
}
|
|
569
561
|
function structFromJson(struct, json) {
|
|
570
562
|
if (typeof json != "object" || json == null || Array.isArray(json)) {
|
package/dist/esm/index.js
CHANGED
package/dist/esm/is-message.js
CHANGED
package/dist/esm/json-value.js
CHANGED
package/dist/esm/merge.js
CHANGED
package/dist/esm/proto-int64.js
CHANGED
|
@@ -9,8 +9,19 @@ export declare function qualifiedName(desc: AnyDesc): string;
|
|
|
9
9
|
/**
|
|
10
10
|
* Converts snake_case to protoCamelCase according to the convention
|
|
11
11
|
* used by protoc to convert a field name to a JSON name.
|
|
12
|
+
*
|
|
13
|
+
* See https://protobuf.com/docs/language-spec#default-json-names
|
|
14
|
+
*
|
|
15
|
+
* The function protoSnakeCase provides the reverse.
|
|
12
16
|
*/
|
|
13
17
|
export declare function protoCamelCase(snakeCase: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Converts protoCamelCase to snake_case.
|
|
20
|
+
*
|
|
21
|
+
* This function is the reverse of function protoCamelCase. Note that some names
|
|
22
|
+
* are not reversible - for example, "foo__bar" -> "fooBar" -> "foo_bar".
|
|
23
|
+
*/
|
|
24
|
+
export declare function protoSnakeCase(lowerCamelCase: string): string;
|
|
14
25
|
/**
|
|
15
26
|
* Escapes names that are reserved for ECMAScript built-in object properties.
|
|
16
27
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2026 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -41,6 +41,10 @@ export function qualifiedName(desc) {
|
|
|
41
41
|
/**
|
|
42
42
|
* Converts snake_case to protoCamelCase according to the convention
|
|
43
43
|
* used by protoc to convert a field name to a JSON name.
|
|
44
|
+
*
|
|
45
|
+
* See https://protobuf.com/docs/language-spec#default-json-names
|
|
46
|
+
*
|
|
47
|
+
* The function protoSnakeCase provides the reverse.
|
|
44
48
|
*/
|
|
45
49
|
export function protoCamelCase(snakeCase) {
|
|
46
50
|
let capNext = false;
|
|
@@ -75,6 +79,15 @@ export function protoCamelCase(snakeCase) {
|
|
|
75
79
|
}
|
|
76
80
|
return b.join("");
|
|
77
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Converts protoCamelCase to snake_case.
|
|
84
|
+
*
|
|
85
|
+
* This function is the reverse of function protoCamelCase. Note that some names
|
|
86
|
+
* are not reversible - for example, "foo__bar" -> "fooBar" -> "foo_bar".
|
|
87
|
+
*/
|
|
88
|
+
export function protoSnakeCase(lowerCamelCase) {
|
|
89
|
+
return lowerCamelCase.replace(/[A-Z]/g, (letter) => "_" + letter.toLowerCase());
|
|
90
|
+
}
|
|
78
91
|
/**
|
|
79
92
|
* Names that cannot be used for object properties because they are reserved
|
|
80
93
|
* by built-in JavaScript properties.
|
|
@@ -89,7 +89,7 @@ export declare function buildPath(schema: DescMessage): PathBuilder;
|
|
|
89
89
|
* paths that refer to an extension.
|
|
90
90
|
*/
|
|
91
91
|
export declare function parsePath(schema: DescMessage, path: string, options?: {
|
|
92
|
-
registry?: Registry;
|
|
92
|
+
registry?: Registry | undefined;
|
|
93
93
|
}): Path;
|
|
94
94
|
/**
|
|
95
95
|
* Stringify a path.
|
package/dist/esm/reflect/path.js
CHANGED
package/dist/esm/registry.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2026 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -191,6 +191,8 @@ function initBaseRegistry(inputs) {
|
|
|
191
191
|
const EDITION_PROTO2 = 998;
|
|
192
192
|
// bootstrap-inject google.protobuf.Edition.EDITION_PROTO3: const $name: Edition.$localName = $number;
|
|
193
193
|
const EDITION_PROTO3 = 999;
|
|
194
|
+
// bootstrap-inject google.protobuf.Edition.EDITION_UNSTABLE: const $name: Edition.$localName = $number;
|
|
195
|
+
const EDITION_UNSTABLE = 9999;
|
|
194
196
|
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_STRING: const $name: FieldDescriptorProto_Type.$localName = $number;
|
|
195
197
|
const TYPE_STRING = 9;
|
|
196
198
|
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_GROUP: const $name: FieldDescriptorProto_Type.$localName = $number;
|
|
@@ -221,9 +223,11 @@ const PACKED = 1;
|
|
|
221
223
|
const DELIMITED = 2;
|
|
222
224
|
// bootstrap-inject google.protobuf.FeatureSet.EnumType.OPEN: const $name: FeatureSet_EnumType.$localName = $number;
|
|
223
225
|
const OPEN = 1;
|
|
226
|
+
// bootstrap-inject google.protobuf.FeatureSet.Utf8Validation.VERIFY: const $name: FeatureSet_Utf8Validation.$localName = $number;
|
|
227
|
+
const VERIFY = 2;
|
|
224
228
|
// biome-ignore format: want this to read well
|
|
225
229
|
// bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2024: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition;
|
|
226
|
-
// generated from protoc
|
|
230
|
+
// generated from protoc v34.0
|
|
227
231
|
export const minimumEdition = 998, maximumEdition = 1001;
|
|
228
232
|
const featureDefaults = {
|
|
229
233
|
// EDITION_PROTO2
|
|
@@ -562,6 +566,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
|
|
|
562
566
|
message: undefined,
|
|
563
567
|
enum: undefined,
|
|
564
568
|
presence: getFieldPresence(proto, oneof, isExtension, parentOrFile),
|
|
569
|
+
utf8Validation: isUtf8Validated(proto, parentOrFile),
|
|
565
570
|
listKind: undefined,
|
|
566
571
|
mapKind: undefined,
|
|
567
572
|
mapKey: undefined,
|
|
@@ -690,6 +695,12 @@ function getFileEdition(proto) {
|
|
|
690
695
|
case "proto3":
|
|
691
696
|
return EDITION_PROTO3;
|
|
692
697
|
case "editions":
|
|
698
|
+
// EDITION_UNSTABLE is a sandbox for in-development features. Collapse
|
|
699
|
+
// it to maximumEdition so SupportedEdition and feature resolution do
|
|
700
|
+
// not leak the test-only edition to users.
|
|
701
|
+
if (proto.edition === EDITION_UNSTABLE) {
|
|
702
|
+
return maximumEdition;
|
|
703
|
+
}
|
|
693
704
|
if (proto.edition in featureDefaults) {
|
|
694
705
|
return proto.edition;
|
|
695
706
|
}
|
|
@@ -885,6 +896,18 @@ function isDelimitedEncoding(proto, parent) {
|
|
|
885
896
|
parent,
|
|
886
897
|
}));
|
|
887
898
|
}
|
|
899
|
+
/**
|
|
900
|
+
* Reject invalid UTF-8 when reading string fields from the binary wire format?
|
|
901
|
+
* Driven by the resolved `utf8_validation` feature: VERIFY (proto3 / editions
|
|
902
|
+
* 2023+ default) enforces; NONE (proto2 default) does not.
|
|
903
|
+
*/
|
|
904
|
+
function isUtf8Validated(proto, parent) {
|
|
905
|
+
return (VERIFY ==
|
|
906
|
+
resolveFeature("utf8Validation", {
|
|
907
|
+
proto,
|
|
908
|
+
parent,
|
|
909
|
+
}));
|
|
910
|
+
}
|
|
888
911
|
function resolveFeature(name, ref) {
|
|
889
912
|
var _a, _b;
|
|
890
913
|
const featureSet = (_a = ref.proto.options) === null || _a === void 0 ? void 0 : _a.features;
|
package/dist/esm/to-binary.js
CHANGED
package/dist/esm/to-json.d.ts
CHANGED
package/dist/esm/to-json.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2026 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { ScalarType, } from "./descriptors.js";
|
|
15
|
-
import { protoCamelCase } from "./reflect/names.js";
|
|
15
|
+
import { protoCamelCase, protoSnakeCase } from "./reflect/names.js";
|
|
16
16
|
import { reflect } from "./reflect/reflect.js";
|
|
17
17
|
import { anyUnpack } from "./wkt/index.js";
|
|
18
|
-
import { isWrapperDesc } from "./wkt/wrappers.js";
|
|
18
|
+
import { hasCustomJsonRepresentation, isWrapperDesc } from "./wkt/wrappers.js";
|
|
19
19
|
import { base64Encode } from "./wire/index.js";
|
|
20
20
|
import { createExtensionContainer, getExtension } from "./extensions.js";
|
|
21
21
|
import { checkField, formatVal } from "./reflect/reflect-check.js";
|
|
@@ -221,10 +221,12 @@ function scalarToJson(field, value) {
|
|
|
221
221
|
case ScalarType.INT64:
|
|
222
222
|
case ScalarType.SFIXED64:
|
|
223
223
|
case ScalarType.SINT64:
|
|
224
|
-
if (typeof value
|
|
225
|
-
|
|
224
|
+
if (typeof value == "bigint" ||
|
|
225
|
+
typeof value == "string" ||
|
|
226
|
+
(typeof value == "number" && Number.isInteger(value))) {
|
|
227
|
+
return value.toString();
|
|
226
228
|
}
|
|
227
|
-
|
|
229
|
+
throw new Error(`cannot encode ${field} to JSON: ${(_e = checkField(field, value)) === null || _e === void 0 ? void 0 : _e.message}`);
|
|
228
230
|
// bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
|
|
229
231
|
// Either standard or URL-safe base64 encoding with/without paddings are accepted.
|
|
230
232
|
case ScalarType.BYTES:
|
|
@@ -281,13 +283,10 @@ function anyToJson(val, opts) {
|
|
|
281
283
|
if (!desc || !message) {
|
|
282
284
|
throw new Error(`cannot encode message ${val.$typeName} to JSON: "${val.typeUrl}" is not in the type registry`);
|
|
283
285
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
typeof json !== "object") {
|
|
289
|
-
json = { value: json };
|
|
290
|
-
}
|
|
286
|
+
const reflected = reflect(desc, message);
|
|
287
|
+
const json = hasCustomJsonRepresentation(desc)
|
|
288
|
+
? { value: tryWktToJson(reflected, opts) }
|
|
289
|
+
: reflectToJson(reflected, opts);
|
|
291
290
|
json["@type"] = val.typeUrl;
|
|
292
291
|
return json;
|
|
293
292
|
}
|
|
@@ -320,10 +319,8 @@ function durationToJson(val) {
|
|
|
320
319
|
function fieldMaskToJson(val) {
|
|
321
320
|
return val.paths
|
|
322
321
|
.map((p) => {
|
|
323
|
-
if (p
|
|
324
|
-
throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "`
|
|
325
|
-
p +
|
|
326
|
-
'" is irreversible');
|
|
322
|
+
if (protoSnakeCase(protoCamelCase(p)) !== p) {
|
|
323
|
+
throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "${p}" is irreversible`);
|
|
327
324
|
}
|
|
328
325
|
return protoCamelCase(p);
|
|
329
326
|
})
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export type Message<TypeName extends string = string> = {
|
|
|
15
15
|
/**
|
|
16
16
|
* Unknown fields and extensions stored on the message.
|
|
17
17
|
*/
|
|
18
|
-
$unknown?: UnknownField[];
|
|
18
|
+
$unknown?: UnknownField[] | undefined;
|
|
19
19
|
};
|
|
20
20
|
/**
|
|
21
21
|
* Extract the message type from a message descriptor.
|
package/dist/esm/types.js
CHANGED