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