@aptre/protobuf-es-lite 0.2.15 → 0.3.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 (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/descriptor-set.d.ts +8 -9
  8. package/dist/field-wrapper.d.ts +2 -2
  9. package/dist/google/protobuf/any.pb.d.ts +55 -2
  10. package/dist/google/protobuf/any.pb.js +85 -4
  11. package/dist/google/protobuf/api.pb.js +34 -13
  12. package/dist/google/protobuf/descriptor.pb.js +1045 -161
  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 +162 -12
  19. package/dist/google/protobuf/timestamp.pb.d.ts +8 -2
  20. package/dist/google/protobuf/timestamp.pb.js +68 -4
  21. package/dist/google/protobuf/type.pb.js +62 -21
  22. package/dist/google/protobuf/wrappers.pb.d.ts +47 -10
  23. package/dist/google/protobuf/wrappers.pb.js +280 -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 +5 -12
  29. package/dist/message.js +75 -77
  30. package/dist/protoc-gen-es-lite/typescript.d.ts +2 -2
  31. package/dist/protoc-gen-es-lite/typescript.js +348 -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 +56 -32
  37. package/package.json +3 -1
@@ -1,4 +1,4 @@
1
- import type { MessageType } from "../../index.js";
1
+ import type { JsonReadOptions, JsonValue, JsonWriteOptions, MessageType } from "../../index.js";
2
2
  import { Message } from "../../index.js";
3
3
  export declare const protobufPackage = "google.protobuf";
4
4
  /**
@@ -18,7 +18,11 @@ export type DoubleValue = Message<{
18
18
  */
19
19
  value?: number;
20
20
  }>;
21
- export declare const DoubleValue: MessageType<DoubleValue>;
21
+ declare const DoubleValue_Wkt: {
22
+ toJson(msg: DoubleValue, _options?: Partial<JsonWriteOptions>): JsonValue;
23
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): DoubleValue;
24
+ };
25
+ export declare const DoubleValue: MessageType<DoubleValue> & typeof DoubleValue_Wkt;
22
26
  /**
23
27
  * Wrapper message for `float`.
24
28
  *
@@ -36,7 +40,11 @@ export type FloatValue = Message<{
36
40
  */
37
41
  value?: number;
38
42
  }>;
39
- export declare const FloatValue: MessageType<FloatValue>;
43
+ declare const FloatValue_Wkt: {
44
+ toJson(msg: FloatValue, _options?: Partial<JsonWriteOptions>): JsonValue;
45
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): FloatValue;
46
+ };
47
+ export declare const FloatValue: MessageType<FloatValue> & typeof FloatValue_Wkt;
40
48
  /**
41
49
  * Wrapper message for `int64`.
42
50
  *
@@ -54,7 +62,11 @@ export type Int64Value = Message<{
54
62
  */
55
63
  value?: bigint;
56
64
  }>;
57
- export declare const Int64Value: MessageType<Int64Value>;
65
+ declare const Int64Value_Wkt: {
66
+ toJson(msg: Int64Value, _options?: Partial<JsonWriteOptions>): JsonValue;
67
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): Int64Value;
68
+ };
69
+ export declare const Int64Value: MessageType<Int64Value> & typeof Int64Value_Wkt;
58
70
  /**
59
71
  * Wrapper message for `uint64`.
60
72
  *
@@ -72,7 +84,11 @@ export type UInt64Value = Message<{
72
84
  */
73
85
  value?: bigint;
74
86
  }>;
75
- export declare const UInt64Value: MessageType<UInt64Value>;
87
+ declare const UInt64Value_Wkt: {
88
+ toJson(msg: UInt64Value, _options?: Partial<JsonWriteOptions>): JsonValue;
89
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): UInt64Value;
90
+ };
91
+ export declare const UInt64Value: MessageType<UInt64Value> & typeof UInt64Value_Wkt;
76
92
  /**
77
93
  * Wrapper message for `int32`.
78
94
  *
@@ -90,7 +106,11 @@ export type Int32Value = Message<{
90
106
  */
91
107
  value?: number;
92
108
  }>;
93
- export declare const Int32Value: MessageType<Int32Value>;
109
+ declare const Int32Value_Wkt: {
110
+ toJson(msg: Int32Value, _options?: Partial<JsonWriteOptions>): JsonValue;
111
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): Int32Value;
112
+ };
113
+ export declare const Int32Value: MessageType<Int32Value> & typeof Int32Value_Wkt;
94
114
  /**
95
115
  * Wrapper message for `uint32`.
96
116
  *
@@ -108,7 +128,11 @@ export type UInt32Value = Message<{
108
128
  */
109
129
  value?: number;
110
130
  }>;
111
- export declare const UInt32Value: MessageType<UInt32Value>;
131
+ declare const UInt32Value_Wkt: {
132
+ toJson(msg: UInt32Value, _options?: Partial<JsonWriteOptions>): JsonValue;
133
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): UInt32Value;
134
+ };
135
+ export declare const UInt32Value: MessageType<UInt32Value> & typeof UInt32Value_Wkt;
112
136
  /**
113
137
  * Wrapper message for `bool`.
114
138
  *
@@ -126,7 +150,11 @@ export type BoolValue = Message<{
126
150
  */
127
151
  value?: boolean;
128
152
  }>;
129
- export declare const BoolValue: MessageType<BoolValue>;
153
+ declare const BoolValue_Wkt: {
154
+ toJson(msg: BoolValue, _options?: Partial<JsonWriteOptions>): JsonValue;
155
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): BoolValue;
156
+ };
157
+ export declare const BoolValue: MessageType<BoolValue> & typeof BoolValue_Wkt;
130
158
  /**
131
159
  * Wrapper message for `string`.
132
160
  *
@@ -144,7 +172,11 @@ export type StringValue = Message<{
144
172
  */
145
173
  value?: string;
146
174
  }>;
147
- export declare const StringValue: MessageType<StringValue>;
175
+ declare const StringValue_Wkt: {
176
+ toJson(msg: StringValue, _options?: Partial<JsonWriteOptions>): JsonValue;
177
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): StringValue;
178
+ };
179
+ export declare const StringValue: MessageType<StringValue> & typeof StringValue_Wkt;
148
180
  /**
149
181
  * Wrapper message for `bytes`.
150
182
  *
@@ -162,4 +194,9 @@ export type BytesValue = Message<{
162
194
  */
163
195
  value?: Uint8Array;
164
196
  }>;
165
- export declare const BytesValue: MessageType<BytesValue>;
197
+ declare const BytesValue_Wkt: {
198
+ toJson(msg: BytesValue, _options?: Partial<JsonWriteOptions>): JsonValue;
199
+ fromJson(json: JsonValue | null | undefined, _options?: Partial<JsonReadOptions>): BytesValue;
200
+ };
201
+ export declare const BytesValue: MessageType<BytesValue> & typeof BytesValue_Wkt;
202
+ export {};
@@ -37,68 +37,329 @@
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 {
50
+ value: jsonReadScalar(ScalarType.DOUBLE, json, LongType.BIGINT),
51
+ };
52
+ }
53
+ catch (e) {
54
+ let m = `cannot decode message google.protobuf.DoubleValue from JSON"`;
55
+ if (e instanceof Error && e.message.length > 0) {
56
+ m += `: ${e.message}`;
57
+ }
58
+ throw new Error(m);
59
+ }
60
+ },
61
+ };
62
+ // DoubleValue contains the message type declaration for DoubleValue.
42
63
  export const DoubleValue = createMessageType({
43
64
  typeName: "google.protobuf.DoubleValue",
44
65
  fields: [
45
- { no: 1, name: "value", kind: "scalar", T: 1 /* ScalarType.DOUBLE */ },
66
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.DOUBLE },
46
67
  ],
47
68
  packedByDefault: true,
48
- });
69
+ fieldWrapper: {
70
+ wrapField(value) {
71
+ return DoubleValue.create({ value: value ?? undefined });
72
+ },
73
+ unwrapField(msg) {
74
+ return msg.value;
75
+ },
76
+ },
77
+ }, DoubleValue_Wkt);
78
+ // FloatValue_Wkt contains the well-known-type overrides for FloatValue.
79
+ const FloatValue_Wkt = {
80
+ toJson(msg, _options) {
81
+ return jsonWriteScalar(ScalarType.FLOAT, msg.value);
82
+ },
83
+ fromJson(json, _options) {
84
+ try {
85
+ return {
86
+ value: jsonReadScalar(ScalarType.FLOAT, json, LongType.BIGINT),
87
+ };
88
+ }
89
+ catch (e) {
90
+ let m = `cannot decode message google.protobuf.FloatValue from JSON"`;
91
+ if (e instanceof Error && e.message.length > 0) {
92
+ m += `: ${e.message}`;
93
+ }
94
+ throw new Error(m);
95
+ }
96
+ },
97
+ };
98
+ // FloatValue contains the message type declaration for FloatValue.
49
99
  export const FloatValue = createMessageType({
50
100
  typeName: "google.protobuf.FloatValue",
51
101
  fields: [
52
- { no: 1, name: "value", kind: "scalar", T: 2 /* ScalarType.FLOAT */ },
102
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.FLOAT },
53
103
  ],
54
104
  packedByDefault: true,
55
- });
105
+ fieldWrapper: {
106
+ wrapField(value) {
107
+ return FloatValue.create({ value: value ?? undefined });
108
+ },
109
+ unwrapField(msg) {
110
+ return msg.value;
111
+ },
112
+ },
113
+ }, FloatValue_Wkt);
114
+ // Int64Value_Wkt contains the well-known-type overrides for Int64Value.
115
+ const Int64Value_Wkt = {
116
+ toJson(msg, _options) {
117
+ return jsonWriteScalar(ScalarType.INT64, msg.value);
118
+ },
119
+ fromJson(json, _options) {
120
+ try {
121
+ return {
122
+ value: jsonReadScalar(ScalarType.INT64, json, LongType.BIGINT),
123
+ };
124
+ }
125
+ catch (e) {
126
+ let m = `cannot decode message google.protobuf.Int64Value from JSON"`;
127
+ if (e instanceof Error && e.message.length > 0) {
128
+ m += `: ${e.message}`;
129
+ }
130
+ throw new Error(m);
131
+ }
132
+ },
133
+ };
134
+ // Int64Value contains the message type declaration for Int64Value.
56
135
  export const Int64Value = createMessageType({
57
136
  typeName: "google.protobuf.Int64Value",
58
137
  fields: [
59
- { no: 1, name: "value", kind: "scalar", T: 3 /* ScalarType.INT64 */ },
138
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.INT64 },
60
139
  ],
61
140
  packedByDefault: true,
62
- });
141
+ fieldWrapper: {
142
+ wrapField(value) {
143
+ return Int64Value.create({ value: value ?? undefined });
144
+ },
145
+ unwrapField(msg) {
146
+ return msg.value;
147
+ },
148
+ },
149
+ }, Int64Value_Wkt);
150
+ // UInt64Value_Wkt contains the well-known-type overrides for UInt64Value.
151
+ const UInt64Value_Wkt = {
152
+ toJson(msg, _options) {
153
+ return jsonWriteScalar(ScalarType.UINT64, msg.value);
154
+ },
155
+ fromJson(json, _options) {
156
+ try {
157
+ return {
158
+ value: jsonReadScalar(ScalarType.UINT64, json, LongType.BIGINT),
159
+ };
160
+ }
161
+ catch (e) {
162
+ let m = `cannot decode message google.protobuf.UInt64Value from JSON"`;
163
+ if (e instanceof Error && e.message.length > 0) {
164
+ m += `: ${e.message}`;
165
+ }
166
+ throw new Error(m);
167
+ }
168
+ },
169
+ };
170
+ // UInt64Value contains the message type declaration for UInt64Value.
63
171
  export const UInt64Value = createMessageType({
64
172
  typeName: "google.protobuf.UInt64Value",
65
173
  fields: [
66
- { no: 1, name: "value", kind: "scalar", T: 4 /* ScalarType.UINT64 */ },
174
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.UINT64 },
67
175
  ],
68
176
  packedByDefault: true,
69
- });
177
+ fieldWrapper: {
178
+ wrapField(value) {
179
+ return UInt64Value.create({ value: value ?? undefined });
180
+ },
181
+ unwrapField(msg) {
182
+ return msg.value;
183
+ },
184
+ },
185
+ }, UInt64Value_Wkt);
186
+ // Int32Value_Wkt contains the well-known-type overrides for Int32Value.
187
+ const Int32Value_Wkt = {
188
+ toJson(msg, _options) {
189
+ return jsonWriteScalar(ScalarType.INT32, msg.value);
190
+ },
191
+ fromJson(json, _options) {
192
+ try {
193
+ return {
194
+ value: jsonReadScalar(ScalarType.INT32, json, LongType.BIGINT),
195
+ };
196
+ }
197
+ catch (e) {
198
+ let m = `cannot decode message google.protobuf.Int32Value from JSON"`;
199
+ if (e instanceof Error && e.message.length > 0) {
200
+ m += `: ${e.message}`;
201
+ }
202
+ throw new Error(m);
203
+ }
204
+ },
205
+ };
206
+ // Int32Value contains the message type declaration for Int32Value.
70
207
  export const Int32Value = createMessageType({
71
208
  typeName: "google.protobuf.Int32Value",
72
209
  fields: [
73
- { no: 1, name: "value", kind: "scalar", T: 5 /* ScalarType.INT32 */ },
210
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.INT32 },
74
211
  ],
75
212
  packedByDefault: true,
76
- });
213
+ fieldWrapper: {
214
+ wrapField(value) {
215
+ return Int32Value.create({ value: value ?? undefined });
216
+ },
217
+ unwrapField(msg) {
218
+ return msg.value;
219
+ },
220
+ },
221
+ }, Int32Value_Wkt);
222
+ // UInt32Value_Wkt contains the well-known-type overrides for UInt32Value.
223
+ const UInt32Value_Wkt = {
224
+ toJson(msg, _options) {
225
+ return jsonWriteScalar(ScalarType.UINT32, msg.value);
226
+ },
227
+ fromJson(json, _options) {
228
+ try {
229
+ return {
230
+ value: jsonReadScalar(ScalarType.UINT32, json, LongType.BIGINT),
231
+ };
232
+ }
233
+ catch (e) {
234
+ let m = `cannot decode message google.protobuf.UInt32Value 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
+ // UInt32Value contains the message type declaration for UInt32Value.
77
243
  export const UInt32Value = createMessageType({
78
244
  typeName: "google.protobuf.UInt32Value",
79
245
  fields: [
80
- { no: 1, name: "value", kind: "scalar", T: 13 /* ScalarType.UINT32 */ },
246
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.UINT32 },
81
247
  ],
82
248
  packedByDefault: true,
83
- });
249
+ fieldWrapper: {
250
+ wrapField(value) {
251
+ return UInt32Value.create({ value: value ?? undefined });
252
+ },
253
+ unwrapField(msg) {
254
+ return msg.value;
255
+ },
256
+ },
257
+ }, UInt32Value_Wkt);
258
+ // BoolValue_Wkt contains the well-known-type overrides for BoolValue.
259
+ const BoolValue_Wkt = {
260
+ toJson(msg, _options) {
261
+ return jsonWriteScalar(ScalarType.BOOL, msg.value);
262
+ },
263
+ fromJson(json, _options) {
264
+ try {
265
+ return {
266
+ value: jsonReadScalar(ScalarType.BOOL, json, LongType.BIGINT),
267
+ };
268
+ }
269
+ catch (e) {
270
+ let m = `cannot decode message google.protobuf.BoolValue from JSON"`;
271
+ if (e instanceof Error && e.message.length > 0) {
272
+ m += `: ${e.message}`;
273
+ }
274
+ throw new Error(m);
275
+ }
276
+ },
277
+ };
278
+ // BoolValue contains the message type declaration for BoolValue.
84
279
  export const BoolValue = createMessageType({
85
280
  typeName: "google.protobuf.BoolValue",
86
281
  fields: [
87
- { no: 1, name: "value", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
282
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.BOOL },
88
283
  ],
89
284
  packedByDefault: true,
90
- });
285
+ fieldWrapper: {
286
+ wrapField(value) {
287
+ return BoolValue.create({ value: value ?? undefined });
288
+ },
289
+ unwrapField(msg) {
290
+ return msg.value;
291
+ },
292
+ },
293
+ }, BoolValue_Wkt);
294
+ // StringValue_Wkt contains the well-known-type overrides for StringValue.
295
+ const StringValue_Wkt = {
296
+ toJson(msg, _options) {
297
+ return jsonWriteScalar(ScalarType.STRING, msg.value);
298
+ },
299
+ fromJson(json, _options) {
300
+ try {
301
+ return {
302
+ value: jsonReadScalar(ScalarType.STRING, json, LongType.BIGINT),
303
+ };
304
+ }
305
+ catch (e) {
306
+ let m = `cannot decode message google.protobuf.StringValue from JSON"`;
307
+ if (e instanceof Error && e.message.length > 0) {
308
+ m += `: ${e.message}`;
309
+ }
310
+ throw new Error(m);
311
+ }
312
+ },
313
+ };
314
+ // StringValue contains the message type declaration for StringValue.
91
315
  export const StringValue = createMessageType({
92
316
  typeName: "google.protobuf.StringValue",
93
317
  fields: [
94
- { no: 1, name: "value", kind: "scalar", T: 9 /* ScalarType.STRING */ },
318
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.STRING },
95
319
  ],
96
320
  packedByDefault: true,
97
- });
321
+ fieldWrapper: {
322
+ wrapField(value) {
323
+ return StringValue.create({ value: value ?? undefined });
324
+ },
325
+ unwrapField(msg) {
326
+ return msg.value;
327
+ },
328
+ },
329
+ }, StringValue_Wkt);
330
+ // BytesValue_Wkt contains the well-known-type overrides for BytesValue.
331
+ const BytesValue_Wkt = {
332
+ toJson(msg, _options) {
333
+ return jsonWriteScalar(ScalarType.BYTES, msg.value);
334
+ },
335
+ fromJson(json, _options) {
336
+ try {
337
+ return {
338
+ value: jsonReadScalar(ScalarType.BYTES, json, LongType.BIGINT),
339
+ };
340
+ }
341
+ catch (e) {
342
+ let m = `cannot decode message google.protobuf.BytesValue from JSON"`;
343
+ if (e instanceof Error && e.message.length > 0) {
344
+ m += `: ${e.message}`;
345
+ }
346
+ throw new Error(m);
347
+ }
348
+ },
349
+ };
350
+ // BytesValue contains the message type declaration for BytesValue.
98
351
  export const BytesValue = createMessageType({
99
352
  typeName: "google.protobuf.BytesValue",
100
353
  fields: [
101
- { no: 1, name: "value", kind: "scalar", T: 12 /* ScalarType.BYTES */ },
354
+ { no: 1, name: "value", kind: "scalar", T: ScalarType.BYTES },
102
355
  ],
103
356
  packedByDefault: true,
104
- });
357
+ fieldWrapper: {
358
+ wrapField(value) {
359
+ return BytesValue.create({ value: value ?? undefined });
360
+ },
361
+ unwrapField(msg) {
362
+ return msg.value;
363
+ },
364
+ },
365
+ }, 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, };