@bufbuild/protobuf 2.0.0-alpha.2 → 2.0.0-alpha.3

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 (143) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/clone.d.ts +1 -1
  3. package/dist/cjs/clone.js +2 -2
  4. package/dist/cjs/codegenv1/boot.d.ts +1 -1
  5. package/dist/cjs/codegenv1/boot.js +3 -5
  6. package/dist/cjs/codegenv1/embed.d.ts +1 -1
  7. package/dist/cjs/codegenv1/embed.js +39 -34
  8. package/dist/cjs/codegenv1/enum.d.ts +1 -1
  9. package/dist/cjs/codegenv1/extension.d.ts +1 -1
  10. package/dist/cjs/codegenv1/file.d.ts +1 -1
  11. package/dist/cjs/codegenv1/file.js +5 -7
  12. package/dist/cjs/codegenv1/index.d.ts +1 -0
  13. package/dist/cjs/codegenv1/index.js +1 -0
  14. package/dist/cjs/codegenv1/message.d.ts +1 -1
  15. package/dist/cjs/codegenv1/scalar.d.ts +5 -0
  16. package/dist/cjs/codegenv1/scalar.js +39 -0
  17. package/dist/cjs/codegenv1/service.d.ts +3 -3
  18. package/dist/cjs/codegenv1/types.d.ts +7 -9
  19. package/dist/cjs/create.d.ts +1 -1
  20. package/dist/cjs/create.js +6 -5
  21. package/dist/cjs/{desc-types.d.ts → descriptors.d.ts} +39 -7
  22. package/dist/cjs/descriptors.js +53 -0
  23. package/dist/cjs/equals.d.ts +1 -1
  24. package/dist/cjs/extensions.d.ts +1 -1
  25. package/dist/cjs/extensions.js +4 -3
  26. package/dist/cjs/fields.d.ts +1 -1
  27. package/dist/cjs/from-binary.d.ts +2 -2
  28. package/dist/cjs/from-binary.js +22 -22
  29. package/dist/cjs/from-json.d.ts +4 -4
  30. package/dist/cjs/from-json.js +200 -181
  31. package/dist/cjs/index.d.ts +2 -1
  32. package/dist/cjs/index.js +2 -1
  33. package/dist/cjs/is-message.d.ts +1 -1
  34. package/dist/cjs/proto-int64.d.ts +4 -1
  35. package/dist/cjs/proto-int64.js +16 -6
  36. package/dist/cjs/reflect/error.d.ts +1 -1
  37. package/dist/cjs/reflect/guard.d.ts +1 -1
  38. package/dist/cjs/reflect/index.d.ts +0 -1
  39. package/dist/cjs/reflect/index.js +0 -1
  40. package/dist/cjs/reflect/names.d.ts +1 -1
  41. package/dist/cjs/reflect/names.js +1 -1
  42. package/dist/cjs/reflect/nested-types.d.ts +1 -1
  43. package/dist/cjs/reflect/reflect-check.d.ts +10 -1
  44. package/dist/cjs/reflect/reflect-check.js +126 -9
  45. package/dist/cjs/reflect/reflect-types.d.ts +4 -4
  46. package/dist/cjs/reflect/reflect.d.ts +1 -1
  47. package/dist/cjs/reflect/reflect.js +28 -27
  48. package/dist/cjs/reflect/scalar.d.ts +3 -55
  49. package/dist/cjs/reflect/scalar.js +26 -220
  50. package/dist/cjs/reflect/unsafe.d.ts +1 -1
  51. package/dist/cjs/reflect/unsafe.js +1 -1
  52. package/dist/cjs/{reflect/registry.d.ts → registry.d.ts} +2 -2
  53. package/dist/cjs/{reflect/registry.js → registry.js} +54 -36
  54. package/dist/cjs/to-binary.d.ts +1 -1
  55. package/dist/cjs/to-binary.js +27 -27
  56. package/dist/cjs/to-json.d.ts +7 -6
  57. package/dist/cjs/to-json.js +57 -44
  58. package/dist/cjs/types.d.ts +1 -1
  59. package/dist/cjs/wire/base64-encoding.js +2 -2
  60. package/dist/cjs/wire/binary-encoding.d.ts +20 -0
  61. package/dist/cjs/wire/binary-encoding.js +54 -8
  62. package/dist/cjs/wire/size-delimited.d.ts +1 -1
  63. package/dist/cjs/wire/text-format.d.ts +1 -2
  64. package/dist/cjs/wire/text-format.js +21 -19
  65. package/dist/cjs/wkt/any.d.ts +3 -3
  66. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
  67. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
  68. package/dist/cjs/wkt/wrappers.d.ts +1 -1
  69. package/dist/esm/clone.d.ts +1 -1
  70. package/dist/esm/clone.js +1 -1
  71. package/dist/esm/codegenv1/boot.d.ts +1 -1
  72. package/dist/esm/codegenv1/boot.js +3 -5
  73. package/dist/esm/codegenv1/embed.d.ts +1 -1
  74. package/dist/esm/codegenv1/embed.js +9 -4
  75. package/dist/esm/codegenv1/enum.d.ts +1 -1
  76. package/dist/esm/codegenv1/extension.d.ts +1 -1
  77. package/dist/esm/codegenv1/file.d.ts +1 -1
  78. package/dist/esm/codegenv1/file.js +4 -6
  79. package/dist/esm/codegenv1/index.d.ts +1 -0
  80. package/dist/esm/codegenv1/index.js +1 -0
  81. package/dist/esm/codegenv1/message.d.ts +1 -1
  82. package/dist/esm/codegenv1/scalar.d.ts +5 -0
  83. package/dist/esm/codegenv1/scalar.js +35 -0
  84. package/dist/esm/codegenv1/service.d.ts +3 -3
  85. package/dist/esm/codegenv1/types.d.ts +7 -9
  86. package/dist/esm/create.d.ts +1 -1
  87. package/dist/esm/create.js +4 -3
  88. package/dist/esm/{desc-types.d.ts → descriptors.d.ts} +39 -7
  89. package/dist/esm/descriptors.js +50 -0
  90. package/dist/esm/equals.d.ts +1 -1
  91. package/dist/esm/extensions.d.ts +1 -1
  92. package/dist/esm/extensions.js +4 -3
  93. package/dist/esm/fields.d.ts +1 -1
  94. package/dist/esm/from-binary.d.ts +2 -2
  95. package/dist/esm/from-binary.js +4 -4
  96. package/dist/esm/from-json.d.ts +4 -4
  97. package/dist/esm/from-json.js +193 -174
  98. package/dist/esm/index.d.ts +2 -1
  99. package/dist/esm/index.js +2 -1
  100. package/dist/esm/is-message.d.ts +1 -1
  101. package/dist/esm/proto-int64.d.ts +4 -1
  102. package/dist/esm/proto-int64.js +16 -6
  103. package/dist/esm/reflect/error.d.ts +1 -1
  104. package/dist/esm/reflect/guard.d.ts +1 -1
  105. package/dist/esm/reflect/index.d.ts +0 -1
  106. package/dist/esm/reflect/index.js +0 -1
  107. package/dist/esm/reflect/names.d.ts +1 -1
  108. package/dist/esm/reflect/names.js +1 -1
  109. package/dist/esm/reflect/nested-types.d.ts +1 -1
  110. package/dist/esm/reflect/reflect-check.d.ts +10 -1
  111. package/dist/esm/reflect/reflect-check.js +119 -2
  112. package/dist/esm/reflect/reflect-types.d.ts +4 -4
  113. package/dist/esm/reflect/reflect-types.js +1 -0
  114. package/dist/esm/reflect/reflect.d.ts +1 -1
  115. package/dist/esm/reflect/reflect.js +10 -9
  116. package/dist/esm/reflect/scalar.d.ts +3 -55
  117. package/dist/esm/reflect/scalar.js +14 -205
  118. package/dist/esm/reflect/unsafe.d.ts +1 -1
  119. package/dist/esm/reflect/unsafe.js +1 -1
  120. package/dist/esm/{reflect/registry.d.ts → registry.d.ts} +2 -2
  121. package/dist/esm/{reflect/registry.js → registry.js} +44 -26
  122. package/dist/esm/to-binary.d.ts +1 -1
  123. package/dist/esm/to-binary.js +1 -1
  124. package/dist/esm/to-json.d.ts +7 -6
  125. package/dist/esm/to-json.js +42 -29
  126. package/dist/esm/types.d.ts +1 -1
  127. package/dist/esm/wire/base64-encoding.js +2 -2
  128. package/dist/esm/wire/binary-encoding.d.ts +20 -0
  129. package/dist/esm/wire/binary-encoding.js +47 -1
  130. package/dist/esm/wire/size-delimited.d.ts +1 -1
  131. package/dist/esm/wire/text-format.d.ts +1 -2
  132. package/dist/esm/wire/text-format.js +5 -3
  133. package/dist/esm/wkt/any.d.ts +3 -3
  134. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
  135. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
  136. package/dist/esm/wkt/wrappers.d.ts +1 -1
  137. package/package.json +1 -1
  138. package/dist/cjs/desc-types.js +0 -15
  139. package/dist/cjs/reflect/assert.d.ts +0 -16
  140. package/dist/cjs/reflect/assert.js +0 -59
  141. package/dist/esm/desc-types.js +0 -14
  142. package/dist/esm/reflect/assert.d.ts +0 -16
  143. package/dist/esm/reflect/assert.js +0 -52
@@ -14,16 +14,16 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.mergeFromJson = exports.fromJson = exports.mergeFromJsonString = exports.fromJsonString = void 0;
17
- const assert_js_1 = require("./reflect/assert.js");
17
+ /* eslint-disable no-case-declarations,@typescript-eslint/restrict-template-expressions */
18
+ const descriptors_js_1 = require("./descriptors.js");
18
19
  const proto_int64_js_1 = require("./proto-int64.js");
19
20
  const create_js_1 = require("./create.js");
20
21
  const reflect_js_1 = require("./reflect/reflect.js");
22
+ const error_js_1 = require("./reflect/error.js");
21
23
  const reflect_check_js_1 = require("./reflect/reflect-check.js");
22
24
  const scalar_js_1 = require("./reflect/scalar.js");
23
25
  const base64_encoding_js_1 = require("./wire/base64-encoding.js");
24
- const text_encoding_js_1 = require("./wire/text-encoding.js");
25
26
  const index_js_1 = require("./wkt/index.js");
26
- const wrappers_js_1 = require("./wkt/wrappers.js");
27
27
  const extensions_js_1 = require("./extensions.js");
28
28
  // Default options for parsing JSON.
29
29
  const jsonReadDefaults = {
@@ -49,7 +49,7 @@ exports.fromJsonString = fromJsonString;
49
49
  * new data.
50
50
  */
51
51
  function mergeFromJsonString(messageDesc, target, json, options) {
52
- return mergeFromJson(messageDesc, target, parseJsonString(json, messageDesc.typeName), options);
52
+ mergeFromJson(messageDesc, target, parseJsonString(json, messageDesc.typeName), options);
53
53
  }
54
54
  exports.mergeFromJsonString = mergeFromJsonString;
55
55
  /**
@@ -57,7 +57,18 @@ exports.mergeFromJsonString = mergeFromJsonString;
57
57
  */
58
58
  function fromJson(messageDesc, json, options) {
59
59
  const msg = (0, reflect_js_1.reflect)(messageDesc);
60
- readMessage(msg, json, makeReadOptions(options));
60
+ try {
61
+ readMessage(msg, json, makeReadOptions(options));
62
+ }
63
+ catch (e) {
64
+ if ((0, error_js_1.isFieldError)(e)) {
65
+ // @ts-expect-error we use the ES2022 error CTOR option "cause" for better stack traces
66
+ throw new Error(`cannot decode ${e.field()} from JSON: ${e.message}`, {
67
+ cause: e,
68
+ });
69
+ }
70
+ throw e;
71
+ }
61
72
  return msg.message;
62
73
  }
63
74
  exports.fromJson = fromJson;
@@ -71,8 +82,18 @@ exports.fromJson = fromJson;
71
82
  * new data.
72
83
  */
73
84
  function mergeFromJson(messageDesc, target, json, options) {
74
- readMessage((0, reflect_js_1.reflect)(messageDesc, target), json, makeReadOptions(options));
75
- return target;
85
+ try {
86
+ readMessage((0, reflect_js_1.reflect)(messageDesc, target), json, makeReadOptions(options));
87
+ }
88
+ catch (e) {
89
+ if ((0, error_js_1.isFieldError)(e)) {
90
+ // @ts-expect-error we use the ES2022 error CTOR option "cause" for better stack traces
91
+ throw new Error(`cannot decode ${e.field()} from JSON: ${e.message}`, {
92
+ cause: e,
93
+ });
94
+ }
95
+ throw e;
96
+ }
76
97
  }
77
98
  exports.mergeFromJson = mergeFromJson;
78
99
  function readMessage(msg, json, opts) {
@@ -81,7 +102,7 @@ function readMessage(msg, json, opts) {
81
102
  return;
82
103
  }
83
104
  if (json == null || Array.isArray(json) || typeof json != "object") {
84
- throw new Error(`cannot decode message ${msg.desc.typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
105
+ throw new Error(`cannot decode ${msg.desc} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
85
106
  }
86
107
  const oneofSeen = new Map();
87
108
  const jsonNames = new Map();
@@ -98,9 +119,9 @@ function readMessage(msg, json, opts) {
98
119
  }
99
120
  const seen = oneofSeen.get(field.oneof);
100
121
  if (seen !== undefined) {
101
- throw new Error(`cannot decode message ${msg.desc.typeName} from JSON: multiple keys for oneof "${field.oneof.name}" present: "${seen}", "${jsonKey}"`);
122
+ throw new error_js_1.FieldError(field.oneof, `oneof set multiple times by ${seen.name} and ${field.name}`);
102
123
  }
103
- oneofSeen.set(field.oneof, jsonKey);
124
+ oneofSeen.set(field.oneof, field);
104
125
  }
105
126
  readField(msg, field, jsonValue, opts);
106
127
  }
@@ -115,7 +136,7 @@ function readMessage(msg, json, opts) {
115
136
  (0, extensions_js_1.setExtension)(msg.message, extension, get());
116
137
  }
117
138
  if (!extension && !opts.ignoreUnknownFields) {
118
- throw new Error(`cannot decode message ${msg.desc.typeName} from JSON: key "${jsonKey}" is unknown`);
139
+ throw new Error(`cannot decode ${msg.desc} from JSON: key "${jsonKey}" is unknown`);
119
140
  }
120
141
  }
121
142
  }
@@ -144,45 +165,42 @@ function readMapField(msg, field, json, opts) {
144
165
  return;
145
166
  }
146
167
  if (typeof json != "object" || Array.isArray(json)) {
147
- throw new Error(`cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
168
+ throw new error_js_1.FieldError(field, "expected object, got " + (0, reflect_check_js_1.formatVal)(json));
148
169
  }
149
170
  for (const [jsonMapKey, jsonMapValue] of Object.entries(json)) {
150
171
  if (jsonMapValue === null) {
151
- throw new Error(`cannot decode field ${msg.desc.typeName}.${field.name} from JSON: map value null`);
152
- }
153
- let key;
154
- try {
155
- key = readMapKey(field.mapKey, jsonMapKey);
156
- }
157
- catch (e) {
158
- let m = `cannot decode map key for field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(jsonMapKey)}`;
159
- if (e instanceof Error && e.message.length > 0) {
160
- m += `: ${e.message}`;
161
- }
162
- throw new Error(m);
172
+ throw new error_js_1.FieldError(field, "map value must not be null");
163
173
  }
174
+ const key = mapKeyFromJson(field.mapKey, jsonMapKey);
164
175
  switch (field.mapKind) {
165
176
  case "message":
166
177
  const msgValue = (0, reflect_js_1.reflect)(field.message);
167
178
  readMessage(msgValue, jsonMapValue, opts);
168
- msg.setMapEntry(field, key, msgValue);
179
+ // TODO fix types
180
+ // @ts-expect-error TODO
181
+ const err = msg.setMapEntry(field, key, msgValue);
182
+ if (err) {
183
+ throw err;
184
+ }
169
185
  break;
170
186
  case "enum":
171
187
  const enumValue = readEnum(field.enum, jsonMapValue, opts.ignoreUnknownFields, true);
172
188
  if (enumValue !== tokenIgnoredUnknownEnum) {
173
- msg.setMapEntry(field, key, enumValue);
189
+ // TODO fix types
190
+ // @ts-expect-error TODO
191
+ const err = msg.setMapEntry(field, key, enumValue);
192
+ if (err) {
193
+ throw err;
194
+ }
174
195
  }
175
196
  break;
176
197
  case "scalar":
177
- try {
178
- msg.setMapEntry(field, key, readScalar(field.scalar, jsonMapValue, true));
179
- }
180
- catch (e) {
181
- let m = `cannot decode map value for field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(jsonMapValue)}`;
182
- if (e instanceof Error && e.message.length > 0) {
183
- m += `: ${e.message}`;
184
- }
185
- throw new Error(m);
198
+ const err2 = msg.setMapEntry(field,
199
+ // TODO fix types
200
+ // @ts-expect-error TODO
201
+ key, scalarFromJson(field, jsonMapValue, true));
202
+ if (err2) {
203
+ throw err2;
186
204
  }
187
205
  break;
188
206
  }
@@ -193,11 +211,11 @@ function readListField(msg, field, json, opts) {
193
211
  return;
194
212
  }
195
213
  if (!Array.isArray(json)) {
196
- throw new Error(`cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
214
+ throw new error_js_1.FieldError(field, "expected Array, got " + (0, reflect_check_js_1.formatVal)(json));
197
215
  }
198
216
  for (const jsonItem of json) {
199
217
  if (jsonItem === null) {
200
- throw new Error(`cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(jsonItem)}`);
218
+ throw new error_js_1.FieldError(field, "list item must not be null");
201
219
  }
202
220
  switch (field.listKind) {
203
221
  case "message":
@@ -212,15 +230,9 @@ function readListField(msg, field, json, opts) {
212
230
  }
213
231
  break;
214
232
  case "scalar":
215
- try {
216
- msg.addListItem(field, readScalar(field.scalar, jsonItem, true));
217
- }
218
- catch (e) {
219
- let m = `cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(jsonItem)}`;
220
- if (e instanceof Error && e.message.length > 0) {
221
- m += `: ${e.message}`;
222
- }
223
- throw new Error(m);
233
+ const err = msg.addListItem(field, scalarFromJson(field, jsonItem, true));
234
+ if (err) {
235
+ throw err;
224
236
  }
225
237
  break;
226
238
  }
@@ -245,36 +257,18 @@ function readEnumField(msg, field, json, opts) {
245
257
  }
246
258
  }
247
259
  function readScalarField(msg, field, json) {
248
- try {
249
- const scalarValue = readScalar(field.scalar, json, false);
250
- if (scalarValue === tokenNull) {
251
- msg.clear(field);
252
- }
253
- else {
254
- msg.set(field, scalarValue);
255
- }
256
- }
257
- catch (e) {
258
- let m = `cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`;
259
- if (e instanceof Error && e.message.length > 0) {
260
- m += `: ${e.message}`;
261
- }
262
- throw new Error(m);
260
+ const scalarValue = scalarFromJson(field, json, false);
261
+ if (scalarValue === tokenNull) {
262
+ msg.clear(field);
263
263
  }
264
- }
265
- function readMapKey(type, json) {
266
- if (type === scalar_js_1.ScalarType.BOOL) {
267
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
268
- switch (json) {
269
- case "true":
270
- json = true;
271
- break;
272
- case "false":
273
- json = false;
274
- break;
264
+ else {
265
+ // TODO fix type error
266
+ // @ts-expect-error TODO
267
+ const err = msg.set(field, scalarValue);
268
+ if (err) {
269
+ throw err;
275
270
  }
276
271
  }
277
- return readScalar(type, json, true);
278
272
  }
279
273
  const tokenIgnoredUnknownEnum = Symbol();
280
274
  function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
@@ -301,116 +295,143 @@ function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
301
295
  }
302
296
  break;
303
297
  }
304
- throw new Error(`cannot decode enum ${desc.typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
298
+ throw new Error(`cannot decode ${desc} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
305
299
  }
306
300
  const tokenNull = Symbol();
307
- function readScalar(type, json, nullAsZeroValue) {
301
+ function scalarFromJson(field, json, nullAsZeroValue) {
308
302
  if (json === null) {
309
303
  if (nullAsZeroValue) {
310
- return (0, scalar_js_1.scalarZeroValue)(type, scalar_js_1.LongType.BIGINT);
304
+ return (0, scalar_js_1.scalarZeroValue)(field.scalar, false);
311
305
  }
312
306
  return tokenNull;
313
307
  }
314
- // every valid case in the switch below returns, and every fall
315
- // through is regarded as a failure.
316
- switch (type) {
308
+ // int64, sfixed64, sint64, fixed64, uint64: Reflect supports string and number.
309
+ // string, bool: Supported by reflect.
310
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
311
+ switch (field.scalar) {
317
312
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
318
313
  // Either numbers or strings are accepted. Exponent notation is also accepted.
319
- case scalar_js_1.ScalarType.DOUBLE:
320
- case scalar_js_1.ScalarType.FLOAT:
314
+ case descriptors_js_1.ScalarType.DOUBLE:
315
+ case descriptors_js_1.ScalarType.FLOAT:
321
316
  if (json === "NaN")
322
- return Number.NaN;
317
+ return NaN;
323
318
  if (json === "Infinity")
324
319
  return Number.POSITIVE_INFINITY;
325
320
  if (json === "-Infinity")
326
321
  return Number.NEGATIVE_INFINITY;
327
- if (json === "") {
328
- // empty string is not a number
329
- break;
330
- }
331
- if (typeof json == "string" && json.trim().length !== json.length) {
332
- // extra whitespace
333
- break;
334
- }
335
- if (typeof json != "string" && typeof json != "number") {
336
- break;
337
- }
338
- const float = Number(json);
339
- if (Number.isNaN(float)) {
340
- // not a number
322
+ if (typeof json == "number") {
323
+ if (isNaN(json)) {
324
+ // NaN must be encoded with string constants
325
+ throw new error_js_1.FieldError(field, "unexpected NaN number");
326
+ }
327
+ if (!isFinite(json)) {
328
+ // Infinity must be encoded with string constants
329
+ throw new error_js_1.FieldError(field, "unexpected infinite number");
330
+ }
341
331
  break;
342
332
  }
343
- if (!Number.isFinite(float)) {
344
- // infinity and -infinity are handled by string representation above, so this is an error
345
- break;
333
+ if (typeof json == "string") {
334
+ if (json === "") {
335
+ // empty string is not a number
336
+ break;
337
+ }
338
+ if (json.trim().length !== json.length) {
339
+ // extra whitespace
340
+ break;
341
+ }
342
+ const float = Number(json);
343
+ if (!isFinite(float)) {
344
+ // Infinity and NaN must be encoded with string constants
345
+ break;
346
+ }
347
+ return float;
346
348
  }
347
- if (type == scalar_js_1.ScalarType.FLOAT)
348
- (0, assert_js_1.assertFloat32)(float);
349
- return float;
349
+ break;
350
350
  // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
351
- case scalar_js_1.ScalarType.INT32:
352
- case scalar_js_1.ScalarType.FIXED32:
353
- case scalar_js_1.ScalarType.SFIXED32:
354
- case scalar_js_1.ScalarType.SINT32:
355
- case scalar_js_1.ScalarType.UINT32:
356
- let int32;
357
- if (typeof json == "number")
358
- int32 = json;
359
- else if (typeof json == "string" && json.length > 0) {
360
- if (json.trim().length === json.length)
361
- int32 = Number(json);
362
- }
363
- if (int32 === undefined)
364
- break;
365
- if (type == scalar_js_1.ScalarType.UINT32 || type == scalar_js_1.ScalarType.FIXED32)
366
- (0, assert_js_1.assertUInt32)(int32);
367
- else
368
- (0, assert_js_1.assertInt32)(int32);
369
- return int32;
370
- // int64, fixed64, uint64: JSON value will be a decimal string. Either numbers or strings are accepted.
371
- case scalar_js_1.ScalarType.INT64:
372
- case scalar_js_1.ScalarType.SFIXED64:
373
- case scalar_js_1.ScalarType.SINT64:
374
- if (typeof json != "number" && typeof json != "string")
375
- break;
376
- return proto_int64_js_1.protoInt64.parse(json);
377
- case scalar_js_1.ScalarType.FIXED64:
378
- case scalar_js_1.ScalarType.UINT64:
379
- if (typeof json != "number" && typeof json != "string")
380
- break;
381
- return proto_int64_js_1.protoInt64.uParse(json);
382
- // bool:
383
- case scalar_js_1.ScalarType.BOOL:
384
- if (typeof json !== "boolean")
385
- break;
386
- return json;
387
- // string:
388
- case scalar_js_1.ScalarType.STRING:
389
- if (typeof json !== "string") {
390
- break;
351
+ case descriptors_js_1.ScalarType.INT32:
352
+ case descriptors_js_1.ScalarType.FIXED32:
353
+ case descriptors_js_1.ScalarType.SFIXED32:
354
+ case descriptors_js_1.ScalarType.SINT32:
355
+ case descriptors_js_1.ScalarType.UINT32:
356
+ return int32FromJson(json);
357
+ // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
358
+ // Either standard or URL-safe base64 encoding with/without paddings are accepted.
359
+ case descriptors_js_1.ScalarType.BYTES:
360
+ if (typeof json == "string") {
361
+ if (json === "") {
362
+ return new Uint8Array(0);
363
+ }
364
+ try {
365
+ return (0, base64_encoding_js_1.base64Decode)(json);
366
+ }
367
+ catch (e) {
368
+ const message = e instanceof Error ? e.message : String(e);
369
+ throw new error_js_1.FieldError(field, message);
370
+ }
391
371
  }
392
- // A string must always contain UTF-8 encoded or 7-bit ASCII.
393
- if (!(0, text_encoding_js_1.getTextEncoding)().checkUtf8(json)) {
394
- throw new Error("invalid UTF8");
372
+ break;
373
+ }
374
+ return json;
375
+ }
376
+ /**
377
+ * Try to parse a JSON value to a map key for the reflect API.
378
+ *
379
+ * Returns the input if the JSON value cannot be converted.
380
+ */
381
+ function mapKeyFromJson(type, json) {
382
+ switch (type) {
383
+ case descriptors_js_1.ScalarType.BOOL:
384
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
385
+ switch (json) {
386
+ case "true":
387
+ return true;
388
+ case "false":
389
+ return false;
395
390
  }
396
391
  return json;
397
- // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
398
- // Either standard or URL-safe base64 encoding with/without paddings are accepted.
399
- case scalar_js_1.ScalarType.BYTES:
400
- if (json === "")
401
- return new Uint8Array(0);
402
- if (typeof json !== "string")
403
- break;
404
- return (0, base64_encoding_js_1.base64Decode)(json);
392
+ case descriptors_js_1.ScalarType.INT32:
393
+ case descriptors_js_1.ScalarType.FIXED32:
394
+ case descriptors_js_1.ScalarType.UINT32:
395
+ case descriptors_js_1.ScalarType.SFIXED32:
396
+ case descriptors_js_1.ScalarType.SINT32:
397
+ return int32FromJson(json);
398
+ default:
399
+ return json;
405
400
  }
406
- throw new Error();
401
+ }
402
+ /**
403
+ * Try to parse a JSON value to a 32-bit integer for the reflect API.
404
+ *
405
+ * Returns the input if the JSON value cannot be converted.
406
+ */
407
+ function int32FromJson(json) {
408
+ if (typeof json == "string") {
409
+ if (json === "") {
410
+ // empty string is not a number
411
+ return json;
412
+ }
413
+ if (json.trim().length !== json.length) {
414
+ // extra whitespace
415
+ return json;
416
+ }
417
+ const num = Number(json);
418
+ if (Number.isNaN(num)) {
419
+ // not a number
420
+ return json;
421
+ }
422
+ return num;
423
+ }
424
+ return json;
407
425
  }
408
426
  function parseJsonString(jsonString, typeName) {
409
427
  try {
410
428
  return JSON.parse(jsonString);
411
429
  }
412
430
  catch (e) {
413
- throw new Error(`cannot decode ${typeName} from JSON: ${e instanceof Error ? e.message : String(e)}`);
431
+ const message = e instanceof Error ? e.message : String(e);
432
+ throw new Error(`cannot decode message ${typeName} from JSON: ${message}`,
433
+ // @ts-expect-error we use the ES2022 error CTOR option "cause" for better stack traces
434
+ { cause: e });
414
435
  }
415
436
  }
416
437
  function tryWktFromJson(msg, jsonValue, opts) {
@@ -440,13 +461,16 @@ function tryWktFromJson(msg, jsonValue, opts) {
440
461
  listValueFromJson(msg.message, jsonValue);
441
462
  return true;
442
463
  default:
443
- if ((0, wrappers_js_1.isWrapperDesc)(msg.desc)) {
464
+ if ((0, index_js_1.isWrapperDesc)(msg.desc)) {
444
465
  const valueField = msg.desc.fields[0];
445
466
  if (jsonValue === null) {
446
467
  msg.clear(valueField);
447
468
  }
448
469
  else {
449
- msg.set(valueField, readScalar(valueField.scalar, jsonValue, true));
470
+ const err = msg.set(valueField, scalarFromJson(valueField, jsonValue, true));
471
+ if (err) {
472
+ throw err;
473
+ }
450
474
  }
451
475
  return true;
452
476
  }
@@ -456,7 +480,7 @@ function tryWktFromJson(msg, jsonValue, opts) {
456
480
  function anyFromJson(any, json, opts) {
457
481
  var _a;
458
482
  if (json === null || Array.isArray(json) || typeof json != "object") {
459
- throw new Error(`cannot decode message ${any.$typeName} from JSON: expected object but got ${json === null ? "null" : Array.isArray(json) ? "array" : typeof json}`);
483
+ throw new Error(`cannot decode message ${any.$typeName} from JSON: expected object but got ${(0, reflect_check_js_1.formatVal)(json)}`);
460
484
  }
461
485
  if (Object.keys(json).length == 0) {
462
486
  return;
@@ -465,7 +489,13 @@ function anyFromJson(any, json, opts) {
465
489
  if (typeof typeUrl != "string" || typeUrl == "") {
466
490
  throw new Error(`cannot decode message ${any.$typeName} from JSON: "@type" is empty`);
467
491
  }
468
- const typeName = typeUrlToName(typeUrl), desc = (_a = opts.registry) === null || _a === void 0 ? void 0 : _a.getMessage(typeName);
492
+ const typeName = typeUrl.includes("/")
493
+ ? typeUrl.substring(typeUrl.lastIndexOf("/") + 1)
494
+ : typeUrl;
495
+ if (!typeName.length) {
496
+ throw new Error(`cannot decode message ${any.$typeName} from JSON: "@type" is invalid`);
497
+ }
498
+ const desc = (_a = opts.registry) === null || _a === void 0 ? void 0 : _a.getMessage(typeName);
469
499
  if (!desc) {
470
500
  throw new Error(`cannot decode message ${any.$typeName} from JSON: ${typeUrl} is not in the type registry`);
471
501
  }
@@ -484,17 +514,17 @@ function anyFromJson(any, json, opts) {
484
514
  }
485
515
  function timestampFromJson(timestamp, json) {
486
516
  if (typeof json !== "string") {
487
- throw new Error(`cannot decode ${timestamp.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
517
+ throw new Error(`cannot decode message ${timestamp.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
488
518
  }
489
519
  const matches = json.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/);
490
520
  if (!matches) {
491
- throw new Error(`cannot decode ${timestamp.$typeName} from JSON: invalid RFC 3339 string`);
521
+ throw new Error(`cannot decode message ${timestamp.$typeName} from JSON: invalid RFC 3339 string`);
492
522
  }
493
523
  const ms = Date.parse(
494
524
  //prettier-ignore
495
525
  matches[1] + "-" + matches[2] + "-" + matches[3] + "T" + matches[4] + ":" + matches[5] + ":" + matches[6] + (matches[8] ? matches[8] : "Z"));
496
526
  if (Number.isNaN(ms)) {
497
- throw new Error(`cannot decode ${timestamp.$typeName} from JSON: invalid RFC 3339 string`);
527
+ throw new Error(`cannot decode message ${timestamp.$typeName} from JSON: invalid RFC 3339 string`);
498
528
  }
499
529
  if (ms < Date.parse("0001-01-01T00:00:00Z") ||
500
530
  ms > Date.parse("9999-12-31T23:59:59Z")) {
@@ -510,15 +540,15 @@ function timestampFromJson(timestamp, json) {
510
540
  }
511
541
  function durationFromJson(duration, json) {
512
542
  if (typeof json !== "string") {
513
- throw new Error(`cannot decode ${duration.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
543
+ throw new Error(`cannot decode message ${duration.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
514
544
  }
515
545
  const match = json.match(/^(-?[0-9]+)(?:\.([0-9]+))?s/);
516
546
  if (match === null) {
517
- throw new Error(`cannot decode ${duration.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
547
+ throw new Error(`cannot decode message ${duration.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
518
548
  }
519
549
  const longSeconds = Number(match[1]);
520
550
  if (longSeconds > 315576000000 || longSeconds < -315576000000) {
521
- throw new Error(`cannot decode ${duration.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
551
+ throw new Error(`cannot decode message ${duration.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
522
552
  }
523
553
  duration.seconds = proto_int64_js_1.protoInt64.parse(longSeconds);
524
554
  if (typeof match[2] !== "string") {
@@ -532,14 +562,14 @@ function durationFromJson(duration, json) {
532
562
  }
533
563
  function fieldMaskFromJson(fieldMask, json) {
534
564
  if (typeof json !== "string") {
535
- throw new Error(`cannot decode ${fieldMask.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
565
+ throw new Error(`cannot decode message ${fieldMask.$typeName} from JSON: ${(0, reflect_check_js_1.formatVal)(json)}`);
536
566
  }
537
567
  if (json === "") {
538
568
  return;
539
569
  }
540
570
  function camelToSnake(str) {
541
571
  if (str.includes("_")) {
542
- throw new Error(`cannot decode ${fieldMask.$typeName} from JSON: path names must be lowerCamelCase`);
572
+ throw new Error(`cannot decode message ${fieldMask.$typeName} from JSON: path names must be lowerCamelCase`);
543
573
  }
544
574
  const sc = str.replace(/[A-Z]/g, (letter) => "_" + letter.toLowerCase());
545
575
  return sc[0] === "_" ? sc.substring(1) : sc;
@@ -548,7 +578,7 @@ function fieldMaskFromJson(fieldMask, json) {
548
578
  }
549
579
  function structFromJson(struct, json) {
550
580
  if (typeof json != "object" || json == null || Array.isArray(json)) {
551
- throw new Error(`cannot decode ${struct.$typeName} from JSON ${(0, reflect_check_js_1.formatVal)(json)}`);
581
+ throw new Error(`cannot decode message ${struct.$typeName} from JSON ${(0, reflect_check_js_1.formatVal)(json)}`);
552
582
  }
553
583
  for (const [k, v] of Object.entries(json)) {
554
584
  const parsedV = (0, create_js_1.create)(index_js_1.ValueDesc);
@@ -583,13 +613,13 @@ function valueFromJson(value, json) {
583
613
  }
584
614
  break;
585
615
  default:
586
- throw new Error(`cannot decode ${value.$typeName} from JSON ${(0, reflect_check_js_1.formatVal)(json)}`);
616
+ throw new Error(`cannot decode message ${value.$typeName} from JSON ${(0, reflect_check_js_1.formatVal)(json)}`);
587
617
  }
588
618
  return value;
589
619
  }
590
620
  function listValueFromJson(listValue, json) {
591
621
  if (!Array.isArray(json)) {
592
- throw new Error(`cannot decode ${listValue.$typeName} from JSON ${(0, reflect_check_js_1.formatVal)(json)}`);
622
+ throw new Error(`cannot decode message ${listValue.$typeName} from JSON ${(0, reflect_check_js_1.formatVal)(json)}`);
593
623
  }
594
624
  for (const e of json) {
595
625
  const value = (0, create_js_1.create)(index_js_1.ValueDesc);
@@ -597,14 +627,3 @@ function listValueFromJson(listValue, json) {
597
627
  listValue.values.push(value);
598
628
  }
599
629
  }
600
- function typeUrlToName(url) {
601
- if (!url.length) {
602
- throw new Error(`invalid type url: ${url}`);
603
- }
604
- const slash = url.lastIndexOf("/");
605
- const name = slash >= 0 ? url.substring(slash + 1) : url;
606
- if (!name.length) {
607
- throw new Error(`invalid type url: ${url}`);
608
- }
609
- return name;
610
- }
@@ -2,9 +2,10 @@ export * from "./types.js";
2
2
  export * from "./is-message.js";
3
3
  export * from "./create.js";
4
4
  export * from "./clone.js";
5
- export * from "./desc-types.js";
5
+ export * from "./descriptors.js";
6
6
  export * from "./equals.js";
7
7
  export * from "./fields.js";
8
+ export * from "./registry.js";
8
9
  export type { JsonValue, JsonObject } from "./json-value.js";
9
10
  export { toBinary } from "./to-binary.js";
10
11
  export type { BinaryWriteOptions } from "./to-binary.js";
package/dist/cjs/index.js CHANGED
@@ -32,9 +32,10 @@ __exportStar(require("./types.js"), exports);
32
32
  __exportStar(require("./is-message.js"), exports);
33
33
  __exportStar(require("./create.js"), exports);
34
34
  __exportStar(require("./clone.js"), exports);
35
- __exportStar(require("./desc-types.js"), exports);
35
+ __exportStar(require("./descriptors.js"), exports);
36
36
  __exportStar(require("./equals.js"), exports);
37
37
  __exportStar(require("./fields.js"), exports);
38
+ __exportStar(require("./registry.js"), exports);
38
39
  var to_binary_js_1 = require("./to-binary.js");
39
40
  Object.defineProperty(exports, "toBinary", { enumerable: true, get: function () { return to_binary_js_1.toBinary; } });
40
41
  var from_binary_js_1 = require("./from-binary.js");
@@ -1,5 +1,5 @@
1
1
  import type { MessageShape } from "./types.js";
2
- import type { DescMessage } from "./desc-types.js";
2
+ import type { DescMessage } from "./descriptors.js";
3
3
  /**
4
4
  * Determine whether the given `arg` is a message.
5
5
  * If `desc` is set, determine whether `arg` is this specific message.
@@ -1,3 +1,7 @@
1
+ /**
2
+ * Int64Support for the current environment.
3
+ */
4
+ export declare const protoInt64: Int64Support;
1
5
  /**
2
6
  * We use the `bigint` primitive to represent 64-bit integral types. If bigint
3
7
  * is unavailable, we fall back to a string representation, which means that
@@ -91,5 +95,4 @@ interface Int64Support {
91
95
  */
92
96
  uDec(lo: number, hi: number): bigint;
93
97
  }
94
- export declare const protoInt64: Int64Support;
95
98
  export {};