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