@aptre/protobuf-es-lite 0.1.0 → 0.1.2

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.
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Assert that condition is truthy or throw error (with message)
3
+ */
4
+ export declare function assert(condition: unknown, msg?: string): asserts condition;
5
+ /**
6
+ * Assert a valid signed protobuf 32-bit integer.
7
+ */
8
+ export declare function assertInt32(arg: unknown): asserts arg is number;
9
+ /**
10
+ * Assert a valid unsigned protobuf 32-bit integer.
11
+ */
12
+ export declare function assertUInt32(arg: unknown): asserts arg is number;
13
+ /**
14
+ * Assert a valid protobuf float value.
15
+ */
16
+ export declare function assertFloat32(arg: unknown): asserts arg is number;
@@ -0,0 +1,28 @@
1
+ import { BinaryReadOptions, BinaryWriteOptions, IBinaryReader, IBinaryWriter, ScalarType, ScalarValue, WireType } from "@bufbuild/protobuf";
2
+ import { FieldInfo, FieldList } from "./field.js";
3
+ export declare function readField(target: Record<string, any>, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
4
+ reader: IBinaryReader, field: FieldInfo, wireType: WireType, options: BinaryReadOptions): void;
5
+ /**
6
+ * AnyMessage is an interface implemented by all messages. If you need to
7
+ * handle messages of unknown type, this interface provides a convenient
8
+ * index signature to access fields with message["fieldname"].
9
+ */
10
+ type AnyMessage = {
11
+ [k: string]: any;
12
+ };
13
+ export declare function readMapEntry(field: FieldInfo & {
14
+ kind: "map";
15
+ }, reader: IBinaryReader, options: BinaryReadOptions): [string | number, ScalarValue | AnyMessage];
16
+ export declare function readScalar(reader: IBinaryReader, type: ScalarType): ScalarValue;
17
+ export declare function readScalarLTString(reader: IBinaryReader, type: ScalarType): Exclude<ScalarValue, bigint>;
18
+ export declare function makeReadOptions(options?: Partial<BinaryReadOptions>): Readonly<BinaryReadOptions>;
19
+ export declare function makeWriteOptions(options?: Partial<BinaryWriteOptions>): Readonly<BinaryWriteOptions>;
20
+ export declare function readMessage<T>(message: T, fields: FieldList, reader: IBinaryReader, lengthOrEndTagFieldNo: number, options: BinaryReadOptions, delimitedMessageEncoding?: boolean): void;
21
+ /**
22
+ * Serialize a message to binary data.
23
+ */
24
+ export declare function writeMessage<T>(message: T, fields: FieldList, writer: IBinaryWriter, options: BinaryWriteOptions): void;
25
+ export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
26
+ kind: "map";
27
+ }, key: string, value: any): void;
28
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { ImportSymbol, Schema } from "@bufbuild/protoplugin/ecmascript";
2
+ import { DescFile } from "@bufbuild/protobuf";
3
+ /**
4
+ * For syntax "editions", this function raises and error.
5
+ */
6
+ export declare function getNonEditionRuntime(schema: Schema, file: DescFile): ImportSymbol;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * A field wrapper unwraps a message to a primitive value that is more
3
+ * ergonomic for use as a message field.
4
+ *
5
+ * Note that this feature exists for google/protobuf/wrappers.proto
6
+ * and cannot be used to arbitrarily modify types in generated code.
7
+ */
8
+ export interface FieldWrapper<T = any, U = any> {
9
+ wrapField(value: U): T;
10
+ unwrapField(value: T): U;
11
+ }
12
+ /**
13
+ * Wrap a primitive message field value in its corresponding wrapper
14
+ * message. This function is idempotent.
15
+ */
16
+ export declare function wrapField<T>(fieldWrapper: FieldWrapper<T> | undefined, value: any): T;
@@ -0,0 +1,393 @@
1
+ import { EnumType, LongType, ScalarType } from "@bufbuild/protobuf";
2
+ import { MessageType } from "./message.js";
3
+ /**
4
+ * FieldInfo describes a field of a protobuf message for runtime reflection. We
5
+ * distinguish between the following kinds of fields:
6
+ *
7
+ * - "scalar": string, bool, float, int32, etc. The scalar type is "T".
8
+ * - "enum": The field was declared with an enum type. The enum type is "T".
9
+ * - "message": The field was declared with a message type. The message type is "T".
10
+ * - "map": The field was declared with map<K,V>. The key type is "K", the value type is "V".
11
+ *
12
+ * Every field always has the following properties:
13
+ *
14
+ * - "no": The field number of the protobuf field.
15
+ * - "name": The original name of the protobuf field.
16
+ * - "localName": The name of the field as used in generated code.
17
+ * - "jsonName": The name for JSON serialization / deserialization.
18
+ * - "opt": Whether the field is optional.
19
+ * - "req": Whether the field is required (a legacy proto2 feature).
20
+ * - "repeated": Whether the field is repeated.
21
+ * - "packed": Whether the repeated field is packed.
22
+ *
23
+ * Additionally, fields may have the following properties:
24
+ *
25
+ * - "oneof": If the field is member of a oneof group.
26
+ * - "default": Only proto2: An explicit default value.
27
+ * - "delimited": Only proto2: Use the tag-delimited group encoding.
28
+ */
29
+ export type FieldInfo = fiRules<fiScalar> | fiRules<fiEnum> | fiRules<fiMessage> | fiRules<fiMap>;
30
+ /**
31
+ * Provides convenient access to field information of a message type.
32
+ */
33
+ export declare class FieldList {
34
+ private readonly _fields;
35
+ private readonly _normalizer;
36
+ private all?;
37
+ private numbersAsc?;
38
+ private jsonNames?;
39
+ private numbers?;
40
+ private members?;
41
+ constructor(fields: FieldListSource, normalizer: (p: FieldListSource) => FieldInfo[]);
42
+ /**
43
+ * Find field information by field name or json_name.
44
+ */
45
+ findJsonName(jsonName: string): FieldInfo | undefined;
46
+ /**
47
+ * Find field information by proto field number.
48
+ */
49
+ find(fieldNo: number): FieldInfo | undefined;
50
+ /**
51
+ * Return field information in the order they appear in the source.
52
+ */
53
+ list(): readonly FieldInfo[];
54
+ /**
55
+ * Return field information ordered by field number ascending.
56
+ */
57
+ byNumber(): readonly FieldInfo[];
58
+ /**
59
+ * In order of appearance in the source, list fields and
60
+ * oneof groups.
61
+ */
62
+ byMember(): readonly (FieldInfo | OneofInfo)[];
63
+ }
64
+ /**
65
+ * Version of `FieldInfo` that allows the following properties
66
+ * to be omitted:
67
+ *
68
+ * - "localName", "jsonName": can be omitted if equal to lowerCamelCase(name)
69
+ * - "opt": Can be omitted if false.
70
+ * - "repeated": Can be omitted if false.
71
+ * - "packed": Can be omitted if equal to the standard packing of the field.
72
+ */
73
+ export type PartialFieldInfo = fiPartialRules<fiScalar> | fiPartialRules<fiEnum> | fiPartialRules<fiMessage> | fiPartialRules<fiMap>;
74
+ export type FieldListSource = readonly PartialFieldInfo[] | readonly FieldInfo[] | (() => readonly PartialFieldInfo[]) | (() => readonly FieldInfo[]);
75
+ export declare function newFieldList(fields: FieldListSource, packedByDefault: boolean): FieldList;
76
+ export declare function protoCamelCase(snakeCase: string): string;
77
+ /**
78
+ * Returns true if the field is set.
79
+ */
80
+ export declare function isFieldSet(field: FieldInfo, target: Record<string, any>): boolean;
81
+ /**
82
+ * Returns the JSON name for a protobuf field, exactly like protoc does.
83
+ */
84
+ export declare const fieldJsonName: typeof protoCamelCase;
85
+ /**
86
+ * Returns the name of a field in generated code.
87
+ */
88
+ export declare function localFieldName(protoName: string, inOneof: boolean): string;
89
+ /**
90
+ * Names that cannot be used for object properties because they are reserved
91
+ * by built-in JavaScript properties.
92
+ */
93
+ export declare const safeObjectProperty: (name: string) => string;
94
+ /**
95
+ * Returns the name of a oneof group in generated code.
96
+ */
97
+ export declare function localOneofName(protoName: string): string;
98
+ export declare class InternalOneofInfo implements OneofInfo {
99
+ readonly kind = "oneof";
100
+ readonly name: string;
101
+ readonly localName: string;
102
+ readonly repeated = false;
103
+ readonly packed = false;
104
+ readonly opt = false;
105
+ readonly req = false;
106
+ readonly default: undefined;
107
+ readonly fields: FieldInfo[];
108
+ private _lookup?;
109
+ constructor(name: string);
110
+ addField(field: FieldInfo): void;
111
+ findField(localName: string): FieldInfo | undefined;
112
+ }
113
+ /**
114
+ * Convert a collection of field info to an array of normalized FieldInfo.
115
+ *
116
+ * The argument `packedByDefault` specifies whether fields that do not specify
117
+ * `packed` should be packed (proto3) or unpacked (proto2).
118
+ */
119
+ export declare function normalizeFieldInfos(fieldInfos: FieldListSource, packedByDefault: boolean): FieldInfo[];
120
+ /**
121
+ * Provides convenient access to field information of a oneof.
122
+ */
123
+ export interface OneofInfo {
124
+ readonly kind: "oneof";
125
+ readonly name: string;
126
+ readonly localName: string;
127
+ readonly repeated: false;
128
+ readonly packed: false;
129
+ readonly opt: false;
130
+ readonly req: false;
131
+ readonly default: undefined;
132
+ readonly delimited?: undefined;
133
+ readonly fields: readonly FieldInfo[];
134
+ /**
135
+ * Return field information by local name.
136
+ */
137
+ findField(localName: string): FieldInfo | undefined;
138
+ }
139
+ interface fiShared {
140
+ /**
141
+ * The field number of the .proto field.
142
+ */
143
+ readonly no: number;
144
+ /**
145
+ * The original name of the .proto field.
146
+ */
147
+ readonly name: string;
148
+ /**
149
+ * The name of the field as used in generated code.
150
+ */
151
+ readonly localName: string;
152
+ /**
153
+ * The name for JSON serialization / deserialization.
154
+ */
155
+ readonly jsonName: string;
156
+ /**
157
+ * The `oneof` group, if this field belongs to one.
158
+ */
159
+ readonly oneof?: OneofInfo | undefined;
160
+ }
161
+ interface fiScalar extends fiShared {
162
+ readonly kind: "scalar";
163
+ /**
164
+ * Scalar type of the field.
165
+ */
166
+ readonly T: ScalarType;
167
+ /**
168
+ * JavaScript representation of 64 bit integral types (int64, uint64,
169
+ * sint64, fixed64, sfixed64).
170
+ *
171
+ * By default, this is LongType.BIGINT. Generated code will use the BigInt
172
+ * primitive.
173
+ *
174
+ * With LongType.STRING, generated code will use the String primitive instead.
175
+ * This can be specified per field with the option `[jstype = JS_STRING]`:
176
+ *
177
+ * ```protobuf
178
+ * uint64 field_a = 1; // BigInt
179
+ * uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
180
+ * uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
181
+ * uint64 field_b = 2 [jstype = JS_STRING]; // String
182
+ * ```
183
+ *
184
+ * This property is ignored for other scalar types.
185
+ */
186
+ readonly L: LongType;
187
+ /**
188
+ * Is the field repeated?
189
+ */
190
+ readonly repeated: boolean;
191
+ /**
192
+ * Is this repeated field packed?
193
+ * BYTES and STRING can never be packed, since they are length-delimited.
194
+ * Other types can be packed with the field option "packed".
195
+ * For proto3, fields are packed by default.
196
+ */
197
+ readonly packed: boolean;
198
+ /**
199
+ * Is the field optional?
200
+ */
201
+ readonly opt: boolean;
202
+ /**
203
+ * Is the field required? A legacy proto2 feature.
204
+ */
205
+ readonly req: boolean;
206
+ /**
207
+ * Only proto2: An explicit default value.
208
+ */
209
+ readonly default: number | boolean | string | bigint | Uint8Array | undefined;
210
+ /**
211
+ * Serialize this message with the delimited format, also known as group
212
+ * encoding, as opposed to the standard length prefix.
213
+ *
214
+ * Only valid for message fields.
215
+ */
216
+ readonly delimited?: undefined;
217
+ }
218
+ interface fiMessage extends fiShared {
219
+ readonly kind: "message";
220
+ /**
221
+ * Message handler for the field.
222
+ */
223
+ readonly T: MessageType;
224
+ /**
225
+ * Is the field repeated?
226
+ */
227
+ readonly repeated: boolean;
228
+ /**
229
+ * Is this repeated field packed? Never true for messages.
230
+ */
231
+ readonly packed: false;
232
+ /**
233
+ * Is the field required? A legacy proto2 feature.
234
+ */
235
+ readonly req: boolean;
236
+ /**
237
+ * An explicit default value (only proto2). Never set for messages.
238
+ */
239
+ readonly default: undefined;
240
+ /**
241
+ * Serialize this message with the delimited format, also known as group
242
+ * encoding, as opposed to the standard length prefix.
243
+ *
244
+ * Only valid for message fields.
245
+ */
246
+ readonly delimited?: boolean;
247
+ }
248
+ interface fiEnum extends fiShared {
249
+ readonly kind: "enum";
250
+ /**
251
+ * Enum type information for the field.
252
+ */
253
+ readonly T: EnumType;
254
+ /**
255
+ * Is the field repeated?
256
+ */
257
+ readonly repeated: boolean;
258
+ /**
259
+ * Is this repeated field packed?
260
+ * Repeated enums can be packed with the field option "packed".
261
+ * For proto3, they are packed by default.
262
+ */
263
+ readonly packed: boolean;
264
+ /**
265
+ * Is the field optional?
266
+ */
267
+ readonly opt: boolean;
268
+ /**
269
+ * Is the field required? A legacy proto2 feature.
270
+ */
271
+ readonly req: boolean;
272
+ /**
273
+ * Only proto2: An explicit default value.
274
+ */
275
+ readonly default: number | undefined;
276
+ /**
277
+ * Serialize this message with the delimited format, also known as group
278
+ * encoding, as opposed to the standard length prefix.
279
+ *
280
+ * Only valid for message fields.
281
+ */
282
+ readonly delimited?: undefined;
283
+ }
284
+ interface fiMap extends fiShared {
285
+ readonly kind: "map";
286
+ /**
287
+ * Map key type.
288
+ *
289
+ * The key_type can be any integral or string type
290
+ * (so, any scalar type except for floating point
291
+ * types and bytes)
292
+ */
293
+ readonly K: Exclude<ScalarType, ScalarType.FLOAT | ScalarType.DOUBLE | ScalarType.BYTES>;
294
+ /**
295
+ * Map value type. Can be scalar, enum, or message.
296
+ */
297
+ readonly V: {
298
+ readonly kind: "scalar";
299
+ readonly T: ScalarType;
300
+ } | {
301
+ readonly kind: "enum";
302
+ readonly T: EnumType;
303
+ } | {
304
+ readonly kind: "message";
305
+ readonly T: MessageType;
306
+ };
307
+ /**
308
+ * Is the field repeated? Never true for maps.
309
+ */
310
+ readonly repeated: false;
311
+ /**
312
+ * Is this repeated field packed? Never true for maps.
313
+ */
314
+ readonly packed: false;
315
+ /**
316
+ * An explicit default value (only proto2). Never set for maps.
317
+ */
318
+ readonly default: undefined;
319
+ /**
320
+ * Serialize this message with the delimited format, also known as group
321
+ * encoding, as opposed to the standard length prefix.
322
+ *
323
+ * Only valid for message fields.
324
+ */
325
+ readonly delimited?: undefined;
326
+ }
327
+ type fiRules<T> = Omit<T, "oneof" | "repeat" | "repeated" | "packed" | "opt" | "req"> & ({
328
+ readonly repeated: false;
329
+ readonly packed: false;
330
+ readonly opt: false;
331
+ readonly req: boolean;
332
+ readonly oneof: undefined;
333
+ } | {
334
+ readonly repeated: false;
335
+ readonly packed: false;
336
+ readonly opt: true;
337
+ readonly req: false;
338
+ readonly oneof: undefined;
339
+ } | {
340
+ readonly repeated: boolean;
341
+ readonly packed: boolean;
342
+ readonly opt: false;
343
+ readonly req: boolean;
344
+ readonly oneof: undefined;
345
+ } | {
346
+ readonly repeated: false;
347
+ readonly packed: false;
348
+ readonly opt: false;
349
+ readonly req: false;
350
+ readonly oneof: OneofInfo;
351
+ });
352
+ type fiPartialRules<T extends fiScalar | fiMap | fiEnum | fiMessage> = Omit<T, "jsonName" | "localName" | "oneof" | "repeat" | "repeated" | "packed" | "opt" | "req" | "default" | "L" | "delimited"> & ({
353
+ readonly jsonName?: string;
354
+ readonly repeated?: false;
355
+ readonly packed?: false;
356
+ readonly opt?: false;
357
+ readonly req?: boolean;
358
+ readonly oneof?: undefined;
359
+ default?: T["default"];
360
+ L?: LongType;
361
+ delimited?: boolean;
362
+ } | {
363
+ readonly jsonName?: string;
364
+ readonly repeated?: false;
365
+ readonly packed?: false;
366
+ readonly opt: true;
367
+ readonly req?: false;
368
+ readonly oneof?: undefined;
369
+ default?: T["default"];
370
+ L?: LongType;
371
+ delimited?: boolean;
372
+ } | {
373
+ readonly jsonName?: string;
374
+ readonly repeated?: boolean;
375
+ readonly packed?: boolean;
376
+ readonly opt?: false;
377
+ readonly req?: boolean;
378
+ readonly oneof?: undefined;
379
+ default?: T["default"];
380
+ L?: LongType;
381
+ delimited?: boolean;
382
+ } | {
383
+ readonly jsonName?: string;
384
+ readonly repeated?: false;
385
+ readonly packed?: false;
386
+ readonly opt?: false;
387
+ readonly req?: false;
388
+ readonly oneof: string;
389
+ default?: T["default"];
390
+ L?: LongType;
391
+ delimited?: boolean;
392
+ });
393
+ export {};
@@ -0,0 +1,3 @@
1
+ export { Message, AnyMessage, PartialMessage, MessageType, compareMessages, createMessageType, } from "./message.js";
2
+ export { newFieldList, FieldList, PartialFieldInfo, FieldInfo, OneofInfo, fieldJsonName, localFieldName, localOneofName, } from "./field.js";
3
+ export { scalarEquals, scalarZeroValue, isScalarZeroValue } from "./scalar.js";
@@ -0,0 +1,6 @@
1
+ import { FieldInfo } from "./field.js";
2
+ import { AnyMessage, Message, PartialMessage } from "./message.js";
3
+ /**
4
+ * Check whether the given partial has all fields present recursively.
5
+ */
6
+ export declare function isCompleteMessage<T extends Message<T> = AnyMessage>(arg: PartialMessage<T>, fields: readonly FieldInfo[]): arg is T;
package/dist/json.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { JsonReadOptions, JsonValue, JsonWriteOptions, JsonWriteStringOptions } from "@bufbuild/protobuf";
2
+ import { FieldInfo, FieldList } from "./field.js";
3
+ export declare function makeReadOptions(options?: Partial<JsonReadOptions>): Readonly<JsonReadOptions>;
4
+ export declare function makeWriteOptions(options?: Partial<JsonWriteStringOptions>): Readonly<JsonWriteStringOptions>;
5
+ export declare function readMessage<T>(fields: FieldList, typeName: string, json: JsonValue, options: JsonReadOptions, message: T): T;
6
+ export declare function writeMessage<T>(message: T, fields: FieldList, options: JsonWriteOptions): JsonValue;
7
+ /**
8
+ * Resets the field, so that isFieldSet() will return false.
9
+ */
10
+ export declare function clearField(field: FieldInfo, target: Record<string, any>): void;
@@ -0,0 +1,137 @@
1
+ import { BinaryReadOptions, JsonReadOptions, JsonValue, MessageType as BufMessageType, BinaryWriteOptions, JsonWriteOptions, JsonWriteStringOptions } from "@bufbuild/protobuf";
2
+ import { FieldList, FieldListSource } from "./field.js";
3
+ /**
4
+ * Message is the base type of every message.
5
+ */
6
+ export interface Message<T extends Message<T>> {
7
+ }
8
+ /**
9
+ * AnyMessage is an interface implemented by all messages. If you need to
10
+ * handle messages of unknown type, this interface provides a convenient
11
+ * index signature to access fields with message["fieldname"].
12
+ */
13
+ export interface AnyMessage extends Message<AnyMessage> {
14
+ [k: string]: any;
15
+ }
16
+ /**
17
+ * PartialMessage<T> constructs a type from a message. The resulting type
18
+ * only contains the protobuf field members of the message, and all of them
19
+ * are optional.
20
+ *
21
+ * Note that the optionality of the fields is the only difference between
22
+ * PartialMessage and PlainMessage.
23
+ *
24
+ * PartialMessage is similar to the built-in type Partial<T>, but recursive,
25
+ * and respects `oneof` groups.
26
+ */
27
+ export type PartialMessage<T extends Message<T>> = {
28
+ [P in keyof T as T[P] extends Function ? never : P]?: PartialField<T[P]>;
29
+ };
30
+ export type PartialField<F> = F extends (Date | Uint8Array | bigint | boolean | string | number) ? F : F extends Array<infer U> ? Array<PartialField<U>> : F extends ReadonlyArray<infer U> ? ReadonlyArray<PartialField<U>> : F extends Message<infer U> ? PartialMessage<U> : F extends OneofSelectedMessage<infer C, infer V> ? {
31
+ case: C;
32
+ value: PartialMessage<V>;
33
+ } : F extends {
34
+ case: string | undefined;
35
+ value?: unknown;
36
+ } ? F : F extends {
37
+ [key: string | number]: Message<infer U>;
38
+ } ? {
39
+ [key: string | number]: PartialMessage<U>;
40
+ } : F;
41
+ type OneofSelectedMessage<K extends string, M extends Message<M>> = {
42
+ case: K;
43
+ value: M;
44
+ };
45
+ /**
46
+ * MessageType represents a protobuf message declaration. It provides:
47
+ * - a constructor that produces an instance of the message
48
+ * - metadata for reflection-based operations
49
+ * - common functionality like serialization
50
+ */
51
+ export interface MessageType<T extends Message<T> = AnyMessage> extends Pick<BufMessageType, "fieldWrapper"> {
52
+ /**
53
+ * The fully qualified name of the message.
54
+ */
55
+ readonly typeName: string;
56
+ /**
57
+ * Field metadata.
58
+ */
59
+ readonly fields: FieldList;
60
+ /**
61
+ * Create a new instance of this message with zero values for fields.
62
+ */
63
+ create(partial?: PartialMessage<T>): T;
64
+ /**
65
+ * Create a deep copy.
66
+ */
67
+ clone(a: T | undefined | null): T | undefined | null;
68
+ /**
69
+ * Parse from binary data, merging fields.
70
+ *
71
+ * Repeated fields are appended. Map entries are added, overwriting
72
+ * existing keys.
73
+ *
74
+ * If a message field is already present, it will be merged with the
75
+ * new data.
76
+ */
77
+ fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): T;
78
+ /**
79
+ * Parse a message from a JSON value.
80
+ */
81
+ fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): T;
82
+ /**
83
+ * Parse a message from a JSON string.
84
+ */
85
+ fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): T;
86
+ /**
87
+ * Returns true if the given arguments have equal field values, recursively.
88
+ * Will also return true if both messages are `undefined` or `null`.
89
+ */
90
+ equals(a: T | undefined | null, b: T | undefined | null): boolean;
91
+ /**
92
+ * Serialize the message to binary data.
93
+ */
94
+ toBinary(a: T, options?: Partial<BinaryWriteOptions>): Uint8Array;
95
+ /**
96
+ * Serialize the message to a JSON value, a JavaScript value that can be
97
+ * passed to JSON.stringify().
98
+ */
99
+ toJson(a: T, options?: Partial<JsonWriteOptions>): JsonValue;
100
+ /**
101
+ * Serialize the message to a JSON string.
102
+ */
103
+ toJsonString(a: T, options?: Partial<JsonWriteStringOptions>): string;
104
+ }
105
+ export interface MessageTypeParams<T extends Message<T>> extends Pick<MessageType<T>, "typeName" | "fieldWrapper"> {
106
+ /**
107
+ * Fields contains the list of message fields.
108
+ */
109
+ fields: FieldListSource;
110
+ /**
111
+ * `packedByDefault` specifies whether fields that do not specify `packed`
112
+ * should be packed (proto3) or unpacked (proto2).
113
+ */
114
+ packedByDefault: boolean;
115
+ /**
116
+ * `delimitedMessageEncoding` specifies whether fields are encoded without
117
+ * delimited fields (proto3) or with (proto2 legacy).
118
+ */
119
+ delimitedMessageEncoding?: boolean;
120
+ /**
121
+ * Serialize the message to a JSON value, a JavaScript value that can be
122
+ * passed to JSON.stringify().
123
+ *
124
+ * When passed as MessageTypeParams this will override the default serializer behavior.
125
+ */
126
+ toJson?: MessageType<T>["toJson"];
127
+ }
128
+ export declare function compareMessages<T extends Message<T>>(fields: FieldList, a: T | undefined | null, b: T | undefined | null): boolean;
129
+ export declare function cloneMessage<T extends Message<T>>(message: T, fields: FieldList): T;
130
+ /**
131
+ * createMessageType creates a new message type.
132
+ *
133
+ * The argument `packedByDefault` specifies whether fields that do not specify
134
+ * `packed` should be packed (proto3) or unpacked (proto2).
135
+ */
136
+ export declare function createMessageType<T extends Message<T>>(params: MessageTypeParams<T>): MessageType<T>;
137
+ export {};
@@ -0,0 +1,3 @@
1
+ import { FieldList } from "./field.js";
2
+ import { Message, PartialMessage } from "./message.js";
3
+ export declare function applyPartialMessage<T extends Message<T>>(source: PartialMessage<T> | undefined, target: T, fields: FieldList): void;
@@ -0,0 +1 @@
1
+ export declare const protocGenEsLite: import("@bufbuild/protoplugin").Plugin;
@@ -0,0 +1,17 @@
1
+ import { LongType, ScalarType, ScalarValue } from "@bufbuild/protobuf";
2
+ /**
3
+ * Returns true if both scalar values are equal.
4
+ */
5
+ export declare function scalarEquals(type: ScalarType, a: string | boolean | number | bigint | Uint8Array | undefined, b: string | boolean | number | bigint | Uint8Array | undefined): boolean;
6
+ /**
7
+ * Returns the zero value for the given scalar type.
8
+ */
9
+ export declare function scalarZeroValue<T extends ScalarType, L extends LongType>(type: T, longType: L): ScalarValue<T, L>;
10
+ /**
11
+ * Returns true for a zero-value. For example, an integer has the zero-value `0`,
12
+ * a boolean is `false`, a string is `""`, and bytes is an empty Uint8Array.
13
+ *
14
+ * In proto3, zero-values are not written to the wire, unless the field is
15
+ * optional or repeated.
16
+ */
17
+ export declare function isScalarZeroValue(type: ScalarType, value: unknown): boolean;
@@ -0,0 +1,5 @@
1
+ import type { DescExtension, DescField } from "@bufbuild/protobuf";
2
+ import type { GeneratedFile, Printable, Schema } from "@bufbuild/protoplugin/ecmascript";
3
+ export declare function generateTs(schema: Schema): void;
4
+ export declare function generateFieldInfo(f: GeneratedFile, field: DescField | DescExtension): void;
5
+ export declare function getFieldInfoLiteral(field: DescField | DescExtension): Printable;
@@ -86,7 +86,6 @@ function generateMessage(schema, f, message) {
86
86
  f.print(" ],");
87
87
  f.print(" packedByDefault: ", message.file.proto.syntax === "proto3", ",");
88
88
  f.print(" },");
89
- f.print(" true,");
90
89
  f.print(");");
91
90
  f.print();
92
91
  for (const nestedEnum of message.nestedEnums) {
@@ -0,0 +1,8 @@
1
+ import { WireType } from "@bufbuild/protobuf";
2
+ export declare const unknownFieldsSymbol: unique symbol;
3
+ export declare function listUnknownFields(message: any): ReadonlyArray<{
4
+ no: number;
5
+ wireType: WireType;
6
+ data: Uint8Array;
7
+ }>;
8
+ export declare function handleUnknownField(message: any, no: number, wireType: WireType, data: Uint8Array): void;
package/dist/util.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { DescExtension, DescField } from "@bufbuild/protobuf";
2
+ import type { Printable } from "@bufbuild/protoplugin/ecmascript";
3
+ export declare function getFieldTypeInfo(field: DescField | DescExtension): {
4
+ typing: Printable;
5
+ optional: boolean;
6
+ typingInferrableFromZeroValue: boolean;
7
+ };
8
+ /**
9
+ * Return a printable expression for the default value of a field.
10
+ * Only applicable for singular scalar and enum fields.
11
+ */
12
+ export declare function getFieldDefaultValueExpression(field: DescField | DescExtension, enumAs?: "enum_value_as_is" | "enum_value_as_integer" | "enum_value_as_cast_integer"): Printable | undefined;
13
+ /**
14
+ * Return a printable expression for the zero value of a field.
15
+ *
16
+ * Returns either:
17
+ * - empty array literal for repeated fields
18
+ * - empty object literal for maps
19
+ * - undefined for message fields
20
+ * - an enums first value
21
+ * - scalar zero value
22
+ */
23
+ export declare function getFieldZeroValueExpression(field: DescField | DescExtension, enumAs?: "enum_value_as_is" | "enum_value_as_integer" | "enum_value_as_cast_integer"): Printable | undefined;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aptre/protobuf-es-lite",
3
3
  "description": "Lightweight Protobuf codegen for TypeScript and JavaScript.",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"
@@ -21,6 +21,7 @@
21
21
  "protoc-gen-es-lite": "bin/protoc-gen-es-lite"
22
22
  },
23
23
  "main": "./dist/index.js",
24
+ "types": "./dist/index.d.ts",
24
25
  "exports": {
25
26
  ".": {
26
27
  "import": "./dist/index.js",
@@ -18,6 +18,7 @@
18
18
  "moduleResolution": "Node16",
19
19
  "module": "Node16",
20
20
  "verbatimModuleSyntax": true,
21
- "skipLibCheck": false
21
+ "skipLibCheck": false,
22
+ "declaration": true
22
23
  }
23
24
  }