@aptre/protobuf-es-lite 0.2.14 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +10 -15
  3. package/dist/binary.d.ts +13 -10
  4. package/dist/binary.js +24 -23
  5. package/dist/codegen-info.d.ts +8 -0
  6. package/dist/codegen-info.js +12 -4
  7. package/dist/create-descriptor-set.js +1 -1
  8. package/dist/descriptor-set.d.ts +8 -9
  9. package/dist/google/protobuf/any.pb.d.ts +55 -2
  10. package/dist/google/protobuf/any.pb.js +74 -4
  11. package/dist/google/protobuf/api.pb.js +13 -10
  12. package/dist/google/protobuf/descriptor.pb.js +125 -93
  13. package/dist/google/protobuf/duration.pb.d.ts +7 -2
  14. package/dist/google/protobuf/duration.pb.js +54 -4
  15. package/dist/google/protobuf/empty.pb.js +1 -0
  16. package/dist/google/protobuf/source_context.pb.js +3 -2
  17. package/dist/google/protobuf/struct.pb.d.ts +17 -4
  18. package/dist/google/protobuf/struct.pb.js +104 -8
  19. package/dist/google/protobuf/timestamp.pb.d.ts +8 -2
  20. package/dist/google/protobuf/timestamp.pb.js +54 -4
  21. package/dist/google/protobuf/type.pb.js +21 -16
  22. package/dist/google/protobuf/wrappers.pb.d.ts +47 -10
  23. package/dist/google/protobuf/wrappers.pb.js +190 -19
  24. package/dist/index.d.ts +5 -1
  25. package/dist/index.js +4 -1
  26. package/dist/json.d.ts +30 -4
  27. package/dist/json.js +17 -16
  28. package/dist/message.d.ts +8 -15
  29. package/dist/message.js +75 -71
  30. package/dist/protoc-gen-es-lite/typescript.d.ts +2 -2
  31. package/dist/protoc-gen-es-lite/typescript.js +323 -26
  32. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +1 -5
  33. package/dist/protoplugin/ecmascript/reify-wkt.js +0 -10
  34. package/dist/type-registry.d.ts +43 -0
  35. package/dist/type-registry.js +14 -0
  36. package/example/example.pb.ts +6 -7
  37. package/package.json +3 -1
@@ -37,68 +37,239 @@
37
37
  // the ability to detect presence on individual elements.
38
38
  // These wrappers have no meaningful use within a map or a oneof since
39
39
  // individual entries of a map or fields of a oneof can already detect presence.
40
- import { createMessageType } from "../../index.js";
40
+ import { createMessageType, jsonReadScalar, jsonWriteScalar, LongType, ScalarType } from "../../index.js";
41
41
  export const protobufPackage = "google.protobuf";
42
+ // DoubleValue_Wkt contains the well-known-type overrides for DoubleValue.
43
+ const DoubleValue_Wkt = {
44
+ toJson(msg, _options) {
45
+ return jsonWriteScalar(ScalarType.DOUBLE, msg.value);
46
+ },
47
+ fromJson(json, _options) {
48
+ try {
49
+ return { value: jsonReadScalar(ScalarType.DOUBLE, json, LongType.BIGINT) };
50
+ }
51
+ catch (e) {
52
+ let m = `cannot decode message google.protobuf.DoubleValue from JSON"`;
53
+ if (e instanceof Error && e.message.length > 0) {
54
+ m += `: ${e.message}`;
55
+ }
56
+ throw new Error(m);
57
+ }
58
+ },
59
+ };
60
+ // DoubleValue contains the message type declaration for DoubleValue.
42
61
  export const DoubleValue = createMessageType({
43
62
  typeName: "google.protobuf.DoubleValue",
44
63
  fields: [
45
- { no: 1, name: "value", kind: "scalar", T: 1 /* ScalarType.DOUBLE */ },
64
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.DOUBLE },
46
65
  ],
47
66
  packedByDefault: true,
48
- });
67
+ }, DoubleValue_Wkt);
68
+ // FloatValue_Wkt contains the well-known-type overrides for FloatValue.
69
+ const FloatValue_Wkt = {
70
+ toJson(msg, _options) {
71
+ return jsonWriteScalar(ScalarType.FLOAT, msg.value);
72
+ },
73
+ fromJson(json, _options) {
74
+ try {
75
+ return { value: jsonReadScalar(ScalarType.FLOAT, json, LongType.BIGINT) };
76
+ }
77
+ catch (e) {
78
+ let m = `cannot decode message google.protobuf.FloatValue from JSON"`;
79
+ if (e instanceof Error && e.message.length > 0) {
80
+ m += `: ${e.message}`;
81
+ }
82
+ throw new Error(m);
83
+ }
84
+ },
85
+ };
86
+ // FloatValue contains the message type declaration for FloatValue.
49
87
  export const FloatValue = createMessageType({
50
88
  typeName: "google.protobuf.FloatValue",
51
89
  fields: [
52
- { no: 1, name: "value", kind: "scalar", T: 2 /* ScalarType.FLOAT */ },
90
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.FLOAT },
53
91
  ],
54
92
  packedByDefault: true,
55
- });
93
+ }, FloatValue_Wkt);
94
+ // Int64Value_Wkt contains the well-known-type overrides for Int64Value.
95
+ const Int64Value_Wkt = {
96
+ toJson(msg, _options) {
97
+ return jsonWriteScalar(ScalarType.INT64, msg.value);
98
+ },
99
+ fromJson(json, _options) {
100
+ try {
101
+ return { value: jsonReadScalar(ScalarType.INT64, json, LongType.BIGINT) };
102
+ }
103
+ catch (e) {
104
+ let m = `cannot decode message google.protobuf.Int64Value from JSON"`;
105
+ if (e instanceof Error && e.message.length > 0) {
106
+ m += `: ${e.message}`;
107
+ }
108
+ throw new Error(m);
109
+ }
110
+ },
111
+ };
112
+ // Int64Value contains the message type declaration for Int64Value.
56
113
  export const Int64Value = createMessageType({
57
114
  typeName: "google.protobuf.Int64Value",
58
115
  fields: [
59
- { no: 1, name: "value", kind: "scalar", T: 3 /* ScalarType.INT64 */ },
116
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.INT64 },
60
117
  ],
61
118
  packedByDefault: true,
62
- });
119
+ }, Int64Value_Wkt);
120
+ // UInt64Value_Wkt contains the well-known-type overrides for UInt64Value.
121
+ const UInt64Value_Wkt = {
122
+ toJson(msg, _options) {
123
+ return jsonWriteScalar(ScalarType.UINT64, msg.value);
124
+ },
125
+ fromJson(json, _options) {
126
+ try {
127
+ return { value: jsonReadScalar(ScalarType.UINT64, json, LongType.BIGINT) };
128
+ }
129
+ catch (e) {
130
+ let m = `cannot decode message google.protobuf.UInt64Value from JSON"`;
131
+ if (e instanceof Error && e.message.length > 0) {
132
+ m += `: ${e.message}`;
133
+ }
134
+ throw new Error(m);
135
+ }
136
+ },
137
+ };
138
+ // UInt64Value contains the message type declaration for UInt64Value.
63
139
  export const UInt64Value = createMessageType({
64
140
  typeName: "google.protobuf.UInt64Value",
65
141
  fields: [
66
- { no: 1, name: "value", kind: "scalar", T: 4 /* ScalarType.UINT64 */ },
142
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.UINT64 },
67
143
  ],
68
144
  packedByDefault: true,
69
- });
145
+ }, UInt64Value_Wkt);
146
+ // Int32Value_Wkt contains the well-known-type overrides for Int32Value.
147
+ const Int32Value_Wkt = {
148
+ toJson(msg, _options) {
149
+ return jsonWriteScalar(ScalarType.INT32, msg.value);
150
+ },
151
+ fromJson(json, _options) {
152
+ try {
153
+ return { value: jsonReadScalar(ScalarType.INT32, json, LongType.BIGINT) };
154
+ }
155
+ catch (e) {
156
+ let m = `cannot decode message google.protobuf.Int32Value from JSON"`;
157
+ if (e instanceof Error && e.message.length > 0) {
158
+ m += `: ${e.message}`;
159
+ }
160
+ throw new Error(m);
161
+ }
162
+ },
163
+ };
164
+ // Int32Value contains the message type declaration for Int32Value.
70
165
  export const Int32Value = createMessageType({
71
166
  typeName: "google.protobuf.Int32Value",
72
167
  fields: [
73
- { no: 1, name: "value", kind: "scalar", T: 5 /* ScalarType.INT32 */ },
168
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.INT32 },
74
169
  ],
75
170
  packedByDefault: true,
76
- });
171
+ }, Int32Value_Wkt);
172
+ // UInt32Value_Wkt contains the well-known-type overrides for UInt32Value.
173
+ const UInt32Value_Wkt = {
174
+ toJson(msg, _options) {
175
+ return jsonWriteScalar(ScalarType.UINT32, msg.value);
176
+ },
177
+ fromJson(json, _options) {
178
+ try {
179
+ return { value: jsonReadScalar(ScalarType.UINT32, json, LongType.BIGINT) };
180
+ }
181
+ catch (e) {
182
+ let m = `cannot decode message google.protobuf.UInt32Value from JSON"`;
183
+ if (e instanceof Error && e.message.length > 0) {
184
+ m += `: ${e.message}`;
185
+ }
186
+ throw new Error(m);
187
+ }
188
+ },
189
+ };
190
+ // UInt32Value contains the message type declaration for UInt32Value.
77
191
  export const UInt32Value = createMessageType({
78
192
  typeName: "google.protobuf.UInt32Value",
79
193
  fields: [
80
- { no: 1, name: "value", kind: "scalar", T: 13 /* ScalarType.UINT32 */ },
194
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.UINT32 },
81
195
  ],
82
196
  packedByDefault: true,
83
- });
197
+ }, UInt32Value_Wkt);
198
+ // BoolValue_Wkt contains the well-known-type overrides for BoolValue.
199
+ const BoolValue_Wkt = {
200
+ toJson(msg, _options) {
201
+ return jsonWriteScalar(ScalarType.BOOL, msg.value);
202
+ },
203
+ fromJson(json, _options) {
204
+ try {
205
+ return { value: jsonReadScalar(ScalarType.BOOL, json, LongType.BIGINT) };
206
+ }
207
+ catch (e) {
208
+ let m = `cannot decode message google.protobuf.BoolValue from JSON"`;
209
+ if (e instanceof Error && e.message.length > 0) {
210
+ m += `: ${e.message}`;
211
+ }
212
+ throw new Error(m);
213
+ }
214
+ },
215
+ };
216
+ // BoolValue contains the message type declaration for BoolValue.
84
217
  export const BoolValue = createMessageType({
85
218
  typeName: "google.protobuf.BoolValue",
86
219
  fields: [
87
- { no: 1, name: "value", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
220
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.BOOL },
88
221
  ],
89
222
  packedByDefault: true,
90
- });
223
+ }, BoolValue_Wkt);
224
+ // StringValue_Wkt contains the well-known-type overrides for StringValue.
225
+ const StringValue_Wkt = {
226
+ toJson(msg, _options) {
227
+ return jsonWriteScalar(ScalarType.STRING, msg.value);
228
+ },
229
+ fromJson(json, _options) {
230
+ try {
231
+ return { value: jsonReadScalar(ScalarType.STRING, json, LongType.BIGINT) };
232
+ }
233
+ catch (e) {
234
+ let m = `cannot decode message google.protobuf.StringValue from JSON"`;
235
+ if (e instanceof Error && e.message.length > 0) {
236
+ m += `: ${e.message}`;
237
+ }
238
+ throw new Error(m);
239
+ }
240
+ },
241
+ };
242
+ // StringValue contains the message type declaration for StringValue.
91
243
  export const StringValue = createMessageType({
92
244
  typeName: "google.protobuf.StringValue",
93
245
  fields: [
94
- { no: 1, name: "value", kind: "scalar", T: 9 /* ScalarType.STRING */ },
246
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.STRING },
95
247
  ],
96
248
  packedByDefault: true,
97
- });
249
+ }, StringValue_Wkt);
250
+ // BytesValue_Wkt contains the well-known-type overrides for BytesValue.
251
+ const BytesValue_Wkt = {
252
+ toJson(msg, _options) {
253
+ return jsonWriteScalar(ScalarType.BYTES, msg.value);
254
+ },
255
+ fromJson(json, _options) {
256
+ try {
257
+ return { value: jsonReadScalar(ScalarType.BYTES, json, LongType.BIGINT) };
258
+ }
259
+ catch (e) {
260
+ let m = `cannot decode message google.protobuf.BytesValue from JSON"`;
261
+ if (e instanceof Error && e.message.length > 0) {
262
+ m += `: ${e.message}`;
263
+ }
264
+ throw new Error(m);
265
+ }
266
+ },
267
+ };
268
+ // BytesValue contains the message type declaration for BytesValue.
98
269
  export const BytesValue = createMessageType({
99
270
  typeName: "google.protobuf.BytesValue",
100
271
  fields: [
101
- { no: 1, name: "value", kind: "scalar", T: 12 /* ScalarType.BYTES */ },
272
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.BYTES },
102
273
  ],
103
274
  packedByDefault: true,
104
- });
275
+ }, BytesValue_Wkt);
package/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ export { Message, CompleteMessage, AnyMessage, MessageType, Field, compareMessag
2
2
  export { ServiceType, MethodInfo, MethodInfoUnary, MethodInfoServerStreaming, MethodInfoClientStreaming, MethodInfoBiDiStreaming, MethodKind, MethodIdempotency, } from "./service-type.js";
3
3
  export { isCompleteMessage, isCompleteField } from "./is-message.js";
4
4
  export { newFieldList, FieldList, PartialFieldInfo, FieldInfo, OneofInfo, fieldJsonName, } from "./field.js";
5
- export { scalarEquals, scalarZeroValue, isScalarZeroValue } from "./scalar.js";
5
+ export { applyPartialMessage } from "./partial.js";
6
+ export { scalarEquals, scalarZeroValue, isScalarZeroValue, ScalarType, ScalarValue, LongType, } from "./scalar.js";
6
7
  export { createEnumType, normalizeEnumValue } from "./enum.js";
7
8
  export { localName, localFieldName, localOneofName, findEnumSharedPrefix, camelToSnakeCase, protoCamelCase, safeObjectProperty, safeIdentifier, } from "./names.js";
8
9
  export { int64FromString, int64ToString, uInt64ToString } from "./varint.js";
@@ -11,3 +12,6 @@ export { protoBase64 } from "./proto-base64.js";
11
12
  export { protoDouble } from "./proto-double.js";
12
13
  export { Timestamp, Duration, Any, Empty, DoubleValue, FloatValue, Int64Value, UInt64Value, Int32Value, UInt32Value, BoolValue, StringValue, BytesValue, Value, NullValue, ListValue, Struct, } from "./google/index.js";
13
14
  export { DescComments, AnyDesc, DescEnum, DescEnumValue, DescExtension, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, DescriptorSet, } from "./descriptor-set.js";
15
+ export { jsonReadEnum, jsonReadField, jsonReadMapKey, jsonReadScalar, jsonReadMessage, jsonWriteEnum, jsonWriteField, jsonWriteScalar, jsonWriteMessage, jsonDebugValue, JsonValue, JsonObject, JsonReadOptions, jsonMakeReadOptions, JsonWriteOptions, JsonWriteStringOptions, jsonMakeWriteOptions, } from "./json.js";
16
+ export { binaryReadField, binaryReadMapEntry, binaryReadScalar, binaryReadScalarLTString, binaryReadMessage, binaryWriteField, binaryWriteScalar, binaryWritePacked, binaryWriteMapEntry, binaryWriteMessage, binaryMakeReadOptions, binaryMakeWriteOptions, BinaryReadOptions, BinaryWriteOptions, } from "./binary.js";
17
+ export type { IMessageTypeRegistry, IServiceTypeRegistry, IEnumTypeRegistry, } from "./type-registry.js";
package/dist/index.js CHANGED
@@ -2,7 +2,8 @@ export { compareMessages, createMessageType, } from "./message.js";
2
2
  export { MethodKind, MethodIdempotency, } from "./service-type.js";
3
3
  export { isCompleteMessage, isCompleteField } from "./is-message.js";
4
4
  export { newFieldList, FieldList, fieldJsonName, } from "./field.js";
5
- export { scalarEquals, scalarZeroValue, isScalarZeroValue } from "./scalar.js";
5
+ export { applyPartialMessage } from "./partial.js";
6
+ export { scalarEquals, scalarZeroValue, isScalarZeroValue, ScalarType, LongType, } from "./scalar.js";
6
7
  export { createEnumType, normalizeEnumValue } from "./enum.js";
7
8
  export { localName, localFieldName, localOneofName, findEnumSharedPrefix, camelToSnakeCase, protoCamelCase, safeObjectProperty, safeIdentifier, } from "./names.js";
8
9
  export { int64FromString, int64ToString, uInt64ToString } from "./varint.js";
@@ -10,3 +11,5 @@ export { protoInt64 } from "./proto-int64.js";
10
11
  export { protoBase64 } from "./proto-base64.js";
11
12
  export { protoDouble } from "./proto-double.js";
12
13
  export { Timestamp, Duration, Any, Empty, DoubleValue, FloatValue, Int64Value, UInt64Value, Int32Value, UInt32Value, BoolValue, StringValue, BytesValue, Value, NullValue, ListValue, Struct, } from "./google/index.js";
14
+ export { jsonReadEnum, jsonReadField, jsonReadMapKey, jsonReadScalar, jsonReadMessage, jsonWriteEnum, jsonWriteField, jsonWriteScalar, jsonWriteMessage, jsonDebugValue, jsonMakeReadOptions, jsonMakeWriteOptions, } from "./json.js";
15
+ export { binaryReadField, binaryReadMapEntry, binaryReadScalar, binaryReadScalarLTString, binaryReadMessage, binaryWriteField, binaryWriteScalar, binaryWritePacked, binaryWriteMapEntry, binaryWriteMessage, binaryMakeReadOptions, binaryMakeWriteOptions, } from "./binary.js";
package/dist/json.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import { FieldInfo, FieldList } from "./field.js";
2
+ import { LongType, ScalarType, ScalarValue } from "./scalar.js";
3
+ import { EnumType } from "./enum.js";
4
+ import { IMessageTypeRegistry } from "./type-registry.js";
2
5
  /**
3
6
  * Options for parsing JSON data.
4
7
  */
@@ -9,6 +12,10 @@ export interface JsonReadOptions {
9
12
  * unrecognized enum string representations.
10
13
  */
11
14
  ignoreUnknownFields: boolean;
15
+ /**
16
+ * This option is required to read `google.protobuf.Any` from JSON format.
17
+ */
18
+ typeRegistry?: IMessageTypeRegistry;
12
19
  }
13
20
  /**
14
21
  * Options for serializing to JSON.
@@ -35,6 +42,10 @@ export interface JsonWriteOptions {
35
42
  * field name.
36
43
  */
37
44
  useProtoFieldName: boolean;
45
+ /**
46
+ * This option is required to write `google.protobuf.Any` to JSON format.
47
+ */
48
+ typeRegistry?: IMessageTypeRegistry;
38
49
  }
39
50
  /**
40
51
  * Options for serializing to JSON.
@@ -58,11 +69,26 @@ export type JsonValue = number | string | boolean | null | JsonObject | JsonValu
58
69
  export type JsonObject = {
59
70
  [k: string]: JsonValue;
60
71
  };
61
- export declare function makeReadOptions(options?: Partial<JsonReadOptions>): Readonly<JsonReadOptions>;
62
- export declare function makeWriteOptions(options?: Partial<JsonWriteStringOptions>): Readonly<JsonWriteStringOptions>;
63
- export declare function readMessage<T>(fields: FieldList, typeName: string, json: JsonValue, options: JsonReadOptions, message: T): T;
64
- export declare function writeMessage<T>(message: T, fields: FieldList, options: JsonWriteOptions): JsonValue;
72
+ declare function makeReadOptions(options?: Partial<JsonReadOptions>): Readonly<JsonReadOptions>;
73
+ declare function makeWriteOptions(options?: Partial<JsonWriteStringOptions>): Readonly<JsonWriteStringOptions>;
74
+ declare function jsonDebugValue(json: unknown): string;
75
+ declare function readMessage<T>(fields: FieldList, typeName: string, json: JsonValue, options: JsonReadOptions, message: T): T;
76
+ declare function writeMessage<T>(message: T, fields: FieldList, options: JsonWriteOptions): JsonValue;
77
+ declare function readField(target: Record<string, unknown>, jsonValue: JsonValue, field: FieldInfo, options: JsonReadOptions): void;
78
+ declare const tokenNull: unique symbol;
79
+ declare const tokenIgnoredUnknownEnum: unique symbol;
80
+ declare function readEnum(type: EnumType, json: JsonValue, ignoreUnknownFields: boolean, nullAsZeroValue: false): number | typeof tokenIgnoredUnknownEnum | typeof tokenNull;
81
+ declare function readEnum(type: EnumType, json: JsonValue, ignoreUnknownFields: boolean, nullAsZeroValue: true): number | typeof tokenIgnoredUnknownEnum;
82
+ declare function readScalar(type: ScalarType, json: JsonValue | null | undefined): ScalarValue;
83
+ declare function readScalar(type: ScalarType, json: JsonValue | null | undefined, longType: LongType): ScalarValue;
84
+ declare function readScalar(type: ScalarType, json: JsonValue | null | undefined, longType: LongType, nullAsZeroValue: true): ScalarValue;
85
+ declare function readScalar(type: ScalarType, json: JsonValue | null | undefined, longType: LongType, nullAsZeroValue: false): ScalarValue | typeof tokenNull;
86
+ declare function readMapKey(type: ScalarType, json: JsonValue): string;
65
87
  /**
66
88
  * Resets the field, so that isFieldSet() will return false.
67
89
  */
68
90
  export declare function clearField(field: FieldInfo, target: Record<string, any>): void;
91
+ declare function writeField(field: FieldInfo, value: unknown, options: JsonWriteOptions): JsonValue | undefined;
92
+ declare function writeScalar(type: ScalarType, value: unknown): string | number | boolean;
93
+ declare function writeEnum(type: EnumType, value: unknown, enumAsInteger: boolean): string | number | null;
94
+ export { readEnum as jsonReadEnum, readField as jsonReadField, readMapKey as jsonReadMapKey, readScalar as jsonReadScalar, readMessage as jsonReadMessage, writeEnum as jsonWriteEnum, writeField as jsonWriteField, writeScalar as jsonWriteScalar, writeMessage as jsonWriteMessage, makeReadOptions as jsonMakeReadOptions, makeWriteOptions as jsonMakeWriteOptions, jsonDebugValue, };
package/dist/json.js CHANGED
@@ -15,13 +15,13 @@ const jsonWriteDefaults = {
15
15
  useProtoFieldName: false,
16
16
  prettySpaces: 0,
17
17
  };
18
- export function makeReadOptions(options) {
18
+ function makeReadOptions(options) {
19
19
  return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
20
20
  }
21
- export function makeWriteOptions(options) {
21
+ function makeWriteOptions(options) {
22
22
  return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
23
23
  }
24
- function debugJsonValue(json) {
24
+ function jsonDebugValue(json) {
25
25
  if (json === null) {
26
26
  return "null";
27
27
  }
@@ -34,9 +34,9 @@ function debugJsonValue(json) {
34
34
  return String(json);
35
35
  }
36
36
  }
37
- export function readMessage(fields, typeName, json, options, message) {
37
+ function readMessage(fields, typeName, json, options, message) {
38
38
  if (json == null || Array.isArray(json) || typeof json != "object") {
39
- throw new Error(`cannot decode message ${typeName} from JSON: ${debugJsonValue(json)}`);
39
+ throw new Error(`cannot decode message ${typeName} from JSON: ${jsonDebugValue(json)}`);
40
40
  }
41
41
  const oneofSeen = new Map();
42
42
  for (const [jsonKey, jsonValue] of Object.entries(json)) {
@@ -64,7 +64,7 @@ export function readMessage(fields, typeName, json, options, message) {
64
64
  }
65
65
  return message;
66
66
  }
67
- export function writeMessage(message, fields, options) {
67
+ function writeMessage(message, fields, options) {
68
68
  const json = {};
69
69
  let field;
70
70
  try {
@@ -110,7 +110,7 @@ function readField(target, jsonValue, field, options) {
110
110
  return;
111
111
  }
112
112
  if (!Array.isArray(jsonValue)) {
113
- throw new Error(`cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`);
113
+ throw new Error(`cannot decode field ${field.name} from JSON: ${jsonDebugValue(jsonValue)}`);
114
114
  }
115
115
  var targetArray = target[localName];
116
116
  if (!Array.isArray(targetArray)) {
@@ -118,7 +118,7 @@ function readField(target, jsonValue, field, options) {
118
118
  }
119
119
  for (const jsonItem of jsonValue) {
120
120
  if (jsonItem === null) {
121
- throw new Error(`cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonItem)}`);
121
+ throw new Error(`cannot decode field ${field.name} from JSON: ${jsonDebugValue(jsonItem)}`);
122
122
  }
123
123
  switch (field.kind) {
124
124
  case "message":
@@ -136,7 +136,7 @@ function readField(target, jsonValue, field, options) {
136
136
  targetArray.push(readScalar(field.T, jsonItem, field.L, true));
137
137
  }
138
138
  catch (e) {
139
- let m = `cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonItem)}`;
139
+ let m = `cannot decode field ${field.name} from JSON: ${jsonDebugValue(jsonItem)}`;
140
140
  if (e instanceof Error && e.message.length > 0) {
141
141
  m += `: ${e.message}`;
142
142
  }
@@ -151,7 +151,7 @@ function readField(target, jsonValue, field, options) {
151
151
  return;
152
152
  }
153
153
  if (typeof jsonValue != "object" || Array.isArray(jsonValue)) {
154
- throw new Error(`cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`);
154
+ throw new Error(`cannot decode field ${field.name} from JSON: ${jsonDebugValue(jsonValue)}`);
155
155
  }
156
156
  var targetMap = target[localName];
157
157
  if (typeof targetMap !== "object") {
@@ -166,7 +166,7 @@ function readField(target, jsonValue, field, options) {
166
166
  key = readMapKey(field.K, jsonMapKey);
167
167
  }
168
168
  catch (e) {
169
- let m = `cannot decode map key for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
169
+ let m = `cannot decode map key for field ${field.name} from JSON: ${jsonDebugValue(jsonValue)}`;
170
170
  if (e instanceof Error && e.message.length > 0) {
171
171
  m += `: ${e.message}`;
172
172
  }
@@ -188,7 +188,7 @@ function readField(target, jsonValue, field, options) {
188
188
  targetMap[key] = readScalar(field.V.T, jsonMapValue, LongType.BIGINT, true);
189
189
  }
190
190
  catch (e) {
191
- let m = `cannot decode map value for field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
191
+ let m = `cannot decode map value for field ${field.name} from JSON: ${jsonDebugValue(jsonValue)}`;
192
192
  if (e instanceof Error && e.message.length > 0) {
193
193
  m += `: ${e.message}`;
194
194
  }
@@ -242,7 +242,7 @@ function readField(target, jsonValue, field, options) {
242
242
  }
243
243
  }
244
244
  catch (e) {
245
- let m = `cannot decode field ${field.name} from JSON: ${debugJsonValue(jsonValue)}`;
245
+ let m = `cannot decode field ${field.name} from JSON: ${jsonDebugValue(jsonValue)}`;
246
246
  if (e instanceof Error && e.message.length > 0) {
247
247
  m += `: ${e.message}`;
248
248
  }
@@ -278,10 +278,10 @@ function readEnum(type, json, ignoreUnknownFields, nullAsZeroValue) {
278
278
  }
279
279
  break;
280
280
  }
281
- throw new Error(`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`);
281
+ throw new Error(`cannot decode enum ${type.typeName} from JSON: ${jsonDebugValue(json)}`);
282
282
  }
283
- function readScalar(type, json, longType, nullAsZeroValue) {
284
- if (json === null) {
283
+ function readScalar(type, json, longType = LongType.BIGINT, nullAsZeroValue = true) {
284
+ if (json == null) {
285
285
  if (nullAsZeroValue) {
286
286
  return scalarZeroValue(type, longType);
287
287
  }
@@ -577,3 +577,4 @@ function writeEnum(type, value, enumAsInteger) {
577
577
  const val = type.findNumber(value);
578
578
  return val?.name ?? value; // if we don't know the enum value, just return the number
579
579
  }
580
+ export { readEnum as jsonReadEnum, readField as jsonReadField, readMapKey as jsonReadMapKey, readScalar as jsonReadScalar, readMessage as jsonReadMessage, writeEnum as jsonWriteEnum, writeField as jsonWriteField, writeScalar as jsonWriteScalar, writeMessage as jsonWriteMessage, makeReadOptions as jsonMakeReadOptions, makeWriteOptions as jsonMakeWriteOptions, jsonDebugValue, };
package/dist/message.d.ts CHANGED
@@ -55,15 +55,15 @@ export interface MessageType<T extends Message<T> = AnyMessage> {
55
55
  * If a message field is already present, it will be merged with the
56
56
  * new data.
57
57
  */
58
- fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): T;
58
+ fromBinary(bytes: Uint8Array | null | undefined, options?: Partial<BinaryReadOptions>): T;
59
59
  /**
60
60
  * Parse a message from a JSON value.
61
61
  */
62
- fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): T;
62
+ fromJson(jsonValue: JsonValue | null | undefined, options?: Partial<JsonReadOptions>): T;
63
63
  /**
64
64
  * Parse a message from a JSON string.
65
65
  */
66
- fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): T;
66
+ fromJsonString(jsonString: string | null | undefined, options?: Partial<JsonReadOptions>): T;
67
67
  /**
68
68
  * Returns true if the given arguments have equal field values, recursively.
69
69
  * Will also return true if both messages are `undefined` or `null`.
@@ -83,7 +83,7 @@ export interface MessageType<T extends Message<T> = AnyMessage> {
83
83
  */
84
84
  toJsonString(a: Message<T>, options?: Partial<JsonWriteStringOptions>): string;
85
85
  }
86
- export interface MessageTypeParams<T extends Message<T>> extends Pick<MessageType<T>, "typeName" | "fieldWrapper"> {
86
+ export type MessageTypeParams<T extends Message<T>> = Pick<MessageType<T>, "fieldWrapper" | "typeName"> & {
87
87
  /**
88
88
  * Fields contains the list of message fields.
89
89
  */
@@ -98,23 +98,16 @@ export interface MessageTypeParams<T extends Message<T>> extends Pick<MessageTyp
98
98
  * delimited fields (proto3) or with (proto2 legacy).
99
99
  */
100
100
  delimitedMessageEncoding?: boolean;
101
- /**
102
- * Serialize the message to a JSON value, a JavaScript value that can be
103
- * passed to JSON.stringify().
104
- *
105
- * When passed as MessageTypeParams this will override the default serializer behavior.
106
- */
107
- toJson?: MessageType<T>["toJson"];
108
- }
109
- export declare function compareMessages<T extends Message<T>>(fields: FieldList, a: T | undefined | null, b: T | undefined | null): boolean;
110
- export declare function cloneMessage<T extends Message<T>>(message: T, fields: FieldList): T;
101
+ };
111
102
  /**
112
103
  * createMessageType creates a new message type.
113
104
  *
114
105
  * The argument `packedByDefault` specifies whether fields that do not specify
115
106
  * `packed` should be packed (proto3) or unpacked (proto2).
116
107
  */
117
- export declare function createMessageType<T extends Message<T>>(params: MessageTypeParams<T>): MessageType<T>;
108
+ export declare function createMessageType<T extends Message<T>, E extends Record<string, Function> = {}>(params: MessageTypeParams<T>, exts?: E): MessageType<T> & E;
109
+ export declare function compareMessages<T extends Message<T>>(fields: FieldList, a: T | undefined | null, b: T | undefined | null): boolean;
110
+ export declare function cloneMessage<T extends Message<T>>(message: T, fields: FieldList): T;
118
111
  /**
119
112
  * createMessage recursively builds a message filled with zero values based on the given FieldList.
120
113
  */