@bufbuild/protobuf 0.0.1-alpha.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 (146) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/binary-encoding.js +434 -0
  3. package/dist/cjs/binary-format.js +2 -0
  4. package/dist/cjs/descriptor-registry.js +518 -0
  5. package/dist/cjs/descriptor-set.js +557 -0
  6. package/dist/cjs/enum.js +2 -0
  7. package/dist/cjs/field-list.js +2 -0
  8. package/dist/cjs/field.js +40 -0
  9. package/dist/cjs/google/protobuf/any_pb.js +280 -0
  10. package/dist/cjs/google/protobuf/api_pb.js +383 -0
  11. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +334 -0
  12. package/dist/cjs/google/protobuf/descriptor_pb.js +2311 -0
  13. package/dist/cjs/google/protobuf/duration_pb.js +201 -0
  14. package/dist/cjs/google/protobuf/empty_pb.js +72 -0
  15. package/dist/cjs/google/protobuf/field_mask_pb.js +347 -0
  16. package/dist/cjs/google/protobuf/source_context_pb.js +74 -0
  17. package/dist/cjs/google/protobuf/struct_pb.js +288 -0
  18. package/dist/cjs/google/protobuf/timestamp_pb.js +271 -0
  19. package/dist/cjs/google/protobuf/type_pb.js +613 -0
  20. package/dist/cjs/google/protobuf/wrappers_pb.js +658 -0
  21. package/dist/cjs/google/varint.js +280 -0
  22. package/dist/cjs/index.js +148 -0
  23. package/dist/cjs/json-format.js +2 -0
  24. package/dist/cjs/message-type.js +2 -0
  25. package/dist/cjs/message.js +98 -0
  26. package/dist/cjs/private/assert.js +52 -0
  27. package/dist/cjs/private/base64.js +118 -0
  28. package/dist/cjs/private/binary-format-common.js +253 -0
  29. package/dist/cjs/private/binary-format-proto2.js +120 -0
  30. package/dist/cjs/private/binary-format-proto3.js +102 -0
  31. package/dist/cjs/private/enum.js +70 -0
  32. package/dist/cjs/private/field-list.js +62 -0
  33. package/dist/cjs/private/field-wrapper.js +25 -0
  34. package/dist/cjs/private/field.js +34 -0
  35. package/dist/cjs/private/json-format-common.js +482 -0
  36. package/dist/cjs/private/json-format-proto2.js +100 -0
  37. package/dist/cjs/private/json-format-proto3.js +103 -0
  38. package/dist/cjs/private/message-type.js +37 -0
  39. package/dist/cjs/private/names.js +101 -0
  40. package/dist/cjs/private/options-map.js +2 -0
  41. package/dist/cjs/private/proto-runtime.js +24 -0
  42. package/dist/cjs/private/scalars.js +138 -0
  43. package/dist/cjs/private/util-common.js +229 -0
  44. package/dist/cjs/private/util.js +2 -0
  45. package/dist/cjs/proto-int64.js +121 -0
  46. package/dist/cjs/proto2.js +86 -0
  47. package/dist/cjs/proto3.js +99 -0
  48. package/dist/cjs/service-type.js +43 -0
  49. package/dist/cjs/type-registry.js +42 -0
  50. package/dist/esm/binary-encoding.js +434 -0
  51. package/dist/esm/binary-format.js +1 -0
  52. package/dist/esm/descriptor-registry.js +521 -0
  53. package/dist/esm/descriptor-set.js +517 -0
  54. package/dist/esm/enum.js +1 -0
  55. package/dist/esm/field-list.js +1 -0
  56. package/dist/esm/field.js +37 -0
  57. package/dist/esm/google/protobuf/any_pb.js +276 -0
  58. package/dist/esm/google/protobuf/api_pb.js +350 -0
  59. package/dist/esm/google/protobuf/compiler/plugin_pb.js +318 -0
  60. package/dist/esm/google/protobuf/descriptor_pb.js +2213 -0
  61. package/dist/esm/google/protobuf/duration_pb.js +197 -0
  62. package/dist/esm/google/protobuf/empty_pb.js +68 -0
  63. package/dist/esm/google/protobuf/field_mask_pb.js +343 -0
  64. package/dist/esm/google/protobuf/source_context_pb.js +70 -0
  65. package/dist/esm/google/protobuf/struct_pb.js +282 -0
  66. package/dist/esm/google/protobuf/timestamp_pb.js +267 -0
  67. package/dist/esm/google/protobuf/type_pb.js +569 -0
  68. package/dist/esm/google/protobuf/wrappers_pb.js +574 -0
  69. package/dist/esm/google/varint.js +265 -0
  70. package/dist/esm/index.js +24 -0
  71. package/dist/esm/json-format.js +1 -0
  72. package/dist/esm/message-type.js +1 -0
  73. package/dist/esm/message.js +94 -0
  74. package/dist/esm/private/assert.js +41 -0
  75. package/dist/esm/private/base64.js +113 -0
  76. package/dist/esm/private/binary-format-common.js +243 -0
  77. package/dist/esm/private/binary-format-proto2.js +87 -0
  78. package/dist/esm/private/binary-format-proto3.js +74 -0
  79. package/dist/esm/private/enum.js +64 -0
  80. package/dist/esm/private/field-list.js +58 -0
  81. package/dist/esm/private/field-wrapper.js +20 -0
  82. package/dist/esm/private/field.js +27 -0
  83. package/dist/esm/private/json-format-common.js +473 -0
  84. package/dist/esm/private/json-format-proto2.js +90 -0
  85. package/dist/esm/private/json-format-proto3.js +89 -0
  86. package/dist/esm/private/message-type.js +33 -0
  87. package/dist/esm/private/names.js +90 -0
  88. package/dist/esm/private/options-map.js +1 -0
  89. package/dist/esm/private/proto-runtime.js +15 -0
  90. package/dist/esm/private/scalars.js +129 -0
  91. package/dist/esm/private/util-common.js +207 -0
  92. package/dist/esm/private/util.js +1 -0
  93. package/dist/esm/proto-int64.js +118 -0
  94. package/dist/esm/proto2.js +77 -0
  95. package/dist/esm/proto3.js +90 -0
  96. package/dist/esm/service-type.js +37 -0
  97. package/dist/esm/type-registry.js +38 -0
  98. package/dist/types/binary-encoding.d.ts +421 -0
  99. package/dist/types/binary-format.d.ts +109 -0
  100. package/dist/types/descriptor-registry.d.ts +42 -0
  101. package/dist/types/descriptor-set.d.ts +171 -0
  102. package/dist/types/enum.d.ts +31 -0
  103. package/dist/types/field-list.d.ts +27 -0
  104. package/dist/types/field.d.ts +295 -0
  105. package/dist/types/google/protobuf/any_pb.d.ts +167 -0
  106. package/dist/types/google/protobuf/api_pb.d.ts +296 -0
  107. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +294 -0
  108. package/dist/types/google/protobuf/descriptor_pb.d.ts +1959 -0
  109. package/dist/types/google/protobuf/duration_pb.d.ts +117 -0
  110. package/dist/types/google/protobuf/empty_pb.d.ts +44 -0
  111. package/dist/types/google/protobuf/field_mask_pb.d.ts +243 -0
  112. package/dist/types/google/protobuf/source_context_pb.d.ts +44 -0
  113. package/dist/types/google/protobuf/struct_pb.d.ts +205 -0
  114. package/dist/types/google/protobuf/timestamp_pb.d.ts +151 -0
  115. package/dist/types/google/protobuf/type_pb.d.ts +482 -0
  116. package/dist/types/google/protobuf/wrappers_pb.d.ts +379 -0
  117. package/dist/types/google/varint.d.ts +65 -0
  118. package/dist/types/index.d.ts +53 -0
  119. package/dist/types/json-format.d.ts +129 -0
  120. package/dist/types/message-type.d.ts +58 -0
  121. package/dist/types/message.d.ts +124 -0
  122. package/dist/types/private/assert.d.ts +19 -0
  123. package/dist/types/private/base64.d.ts +18 -0
  124. package/dist/types/private/binary-format-common.d.ts +38 -0
  125. package/dist/types/private/binary-format-proto2.d.ts +2 -0
  126. package/dist/types/private/binary-format-proto3.d.ts +2 -0
  127. package/dist/types/private/enum.d.ts +29 -0
  128. package/dist/types/private/field-list.d.ts +25 -0
  129. package/dist/types/private/field-wrapper.d.ts +27 -0
  130. package/dist/types/private/field.d.ts +15 -0
  131. package/dist/types/private/json-format-common.d.ts +30 -0
  132. package/dist/types/private/json-format-proto2.d.ts +2 -0
  133. package/dist/types/private/json-format-proto3.d.ts +2 -0
  134. package/dist/types/private/message-type.d.ts +15 -0
  135. package/dist/types/private/names.d.ts +19 -0
  136. package/dist/types/private/options-map.d.ts +7 -0
  137. package/dist/types/private/proto-runtime.d.ts +55 -0
  138. package/dist/types/private/scalars.d.ts +33 -0
  139. package/dist/types/private/util-common.d.ts +5 -0
  140. package/dist/types/private/util.d.ts +55 -0
  141. package/dist/types/proto-int64.d.ts +94 -0
  142. package/dist/types/proto2.d.ts +4 -0
  143. package/dist/types/proto3.d.ts +4 -0
  144. package/dist/types/service-type.d.ts +118 -0
  145. package/dist/types/type-registry.d.ts +37 -0
  146. package/package.json +31 -0
@@ -0,0 +1,124 @@
1
+ import type { BinaryReadOptions, BinaryWriteOptions } from "./binary-format.js";
2
+ import type {
3
+ JsonReadOptions,
4
+ JsonValue,
5
+ JsonWriteOptions,
6
+ JsonWriteStringOptions,
7
+ } from "./json-format.js";
8
+ import type { MessageType } from "./message-type.js";
9
+ /**
10
+ * AnyMessage is an interface implemented by all messages. If you need to
11
+ * handle messages of unknown type, this interface provides a convenient
12
+ * index signature to access fields with message["fieldname"].
13
+ */
14
+ export interface AnyMessage extends Message<AnyMessage> {
15
+ [k: string]: any;
16
+ }
17
+ /**
18
+ * Message is the base class of every message, generated, or created at
19
+ * runtime.
20
+ *
21
+ * It is _not_ safe to extend this class. If you want to create a message at
22
+ * run time, use proto3.makeMessageType().
23
+ */
24
+ export declare class Message<T extends Message<T> = AnyMessage> {
25
+ /**
26
+ * Compare with a message of the same type.
27
+ */
28
+ equals(other: T | PlainMessage<T> | undefined): boolean;
29
+ /**
30
+ * Create a deep copy.
31
+ */
32
+ clone(): T;
33
+ /**
34
+ * Parse from binary data, merging fields.
35
+ *
36
+ * Repeated fields are appended. Map entries are added, overwriting
37
+ * existing keys.
38
+ *
39
+ * If a message field is already present, it will be merged with the
40
+ * new data.
41
+ */
42
+ fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): this;
43
+ /**
44
+ * Parse a message from a JSON value.
45
+ */
46
+ fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): this;
47
+ /**
48
+ * Parse a message from a JSON string.
49
+ */
50
+ fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): this;
51
+ /**
52
+ * Serialize the message to binary data.
53
+ */
54
+ toBinary(options?: Partial<BinaryWriteOptions>): Uint8Array;
55
+ /**
56
+ * Serialize the message to a JSON value, a JavaScript value that can be
57
+ * passed to JSON.stringify().
58
+ */
59
+ toJson(options?: Partial<JsonWriteOptions>): JsonValue;
60
+ /**
61
+ * Serialize the message to a JSON string.
62
+ */
63
+ toJsonString(options?: Partial<JsonWriteStringOptions>): string;
64
+ /**
65
+ * Retrieve the MessageType of this message - a singleton that represents
66
+ * the protobuf message declaration and provides metadata for reflection-
67
+ * based operations.
68
+ */
69
+ getType(): MessageType<T>;
70
+ }
71
+ /**
72
+ * PlainMessage<T> strips all methods from a message, leaving only fields
73
+ * and oneof groups.
74
+ */
75
+ export declare type PlainMessage<T extends Message> = {
76
+ [P in keyof T as T[P] extends Function ? never : P]: T[P];
77
+ };
78
+ /**
79
+ * PartialMessage<T> constructs a type from a message. The resulting type
80
+ * only contains the protobuf field members of the message, and all of them
81
+ * are optional.
82
+ *
83
+ * PartialMessage is similar to the built-in type Partial<T>, but recursive,
84
+ * and respects `oneof` groups.
85
+ */
86
+ export declare type PartialMessage<T extends Message> = {
87
+ [P in keyof T as T[P] extends Function ? never : P]?: PartialField<T[P]>;
88
+ };
89
+ declare type PartialField<F> = F extends
90
+ | Date
91
+ | Uint8Array
92
+ | bigint
93
+ | boolean
94
+ | string
95
+ | number
96
+ ? F
97
+ : F extends Array<infer U>
98
+ ? Array<PartialField<U>>
99
+ : F extends ReadonlyArray<infer U>
100
+ ? ReadonlyArray<PartialField<U>>
101
+ : F extends OneofSelectedMessage<infer C, infer V>
102
+ ? {
103
+ case: C;
104
+ value: PartialMessage<V>;
105
+ }
106
+ : F extends {
107
+ case: string | undefined;
108
+ value?: unknown;
109
+ }
110
+ ? F
111
+ : F extends Message
112
+ ? PartialMessage<F>
113
+ : F extends {
114
+ [key: string | number]: Message<infer U>;
115
+ }
116
+ ? {
117
+ [key: string | number]: PartialMessage<U>;
118
+ }
119
+ : F;
120
+ declare type OneofSelectedMessage<K extends string, M extends Message> = {
121
+ case: K;
122
+ value: M;
123
+ };
124
+ export {};
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Assert that condition is truthy or throw error (with message)
3
+ */
4
+ export declare function assert(
5
+ condition: unknown,
6
+ msg?: string
7
+ ): asserts condition;
8
+ /**
9
+ * Assert a valid signed protobuf 32-bit integer.
10
+ */
11
+ export declare function assertInt32(arg: unknown): asserts arg is number;
12
+ /**
13
+ * Assert a valid unsigned protobuf 32-bit integer.
14
+ */
15
+ export declare function assertUInt32(arg: unknown): asserts arg is number;
16
+ /**
17
+ * Assert a valid protobuf float value.
18
+ */
19
+ export declare function assertFloat32(arg: unknown): asserts arg is number;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Decodes a base64 string to a byte array.
3
+ *
4
+ * - ignores white-space, including line breaks and tabs
5
+ * - allows inner padding (can decode concatenated base64 strings)
6
+ * - does not require padding
7
+ * - understands base64url encoding:
8
+ * "-" instead of "+",
9
+ * "_" instead of "/",
10
+ * no padding
11
+ */
12
+ export declare function base64decode(base64Str: string): Uint8Array;
13
+ /**
14
+ * Encodes a byte array to a base64 string.
15
+ * Adds padding at the end.
16
+ * Does not insert newlines.
17
+ */
18
+ export declare function base64encode(bytes: Uint8Array): string;
@@ -0,0 +1,38 @@
1
+ import { IBinaryWriter } from "../binary-encoding.js";
2
+ import type { BinaryWriteOptions } from "../binary-format.js";
3
+ import type { BinaryFormat } from "../binary-format.js";
4
+ import { FieldInfo, ScalarType } from "../field.js";
5
+ import type { MessageType } from "../message-type.js";
6
+ export declare function makeBinaryFormatCommon(): Omit<
7
+ BinaryFormat,
8
+ "writeMessage"
9
+ >;
10
+ export declare function writeMapEntry(
11
+ writer: IBinaryWriter,
12
+ options: BinaryWriteOptions,
13
+ field: FieldInfo & {
14
+ kind: "map";
15
+ },
16
+ key: any,
17
+ value: any
18
+ ): void;
19
+ export declare function writeMessageField(
20
+ writer: IBinaryWriter,
21
+ options: BinaryWriteOptions,
22
+ type: MessageType,
23
+ fieldNo: number,
24
+ value: any
25
+ ): void;
26
+ export declare function writeScalar(
27
+ writer: IBinaryWriter,
28
+ type: ScalarType,
29
+ fieldNo: number,
30
+ value: any,
31
+ emitIntrinsicDefault: boolean
32
+ ): void;
33
+ export declare function writePacked(
34
+ writer: IBinaryWriter,
35
+ type: ScalarType,
36
+ fieldNo: number,
37
+ value: any[]
38
+ ): void;
@@ -0,0 +1,2 @@
1
+ import type { BinaryFormat } from "../binary-format.js";
2
+ export declare function makeBinaryFormatProto2(): BinaryFormat;
@@ -0,0 +1,2 @@
1
+ import type { BinaryFormat } from "../binary-format.js";
2
+ export declare function makeBinaryFormatProto3(): BinaryFormat;
@@ -0,0 +1,29 @@
1
+ import type { EnumType, EnumValueInfo } from "../enum.js";
2
+ /**
3
+ * Represents a generated enum.
4
+ */
5
+ export interface EnumObject {
6
+ [key: number]: string;
7
+ [k: string]: number | string;
8
+ }
9
+ /**
10
+ * Get reflection information from a generated enum.
11
+ * If this function is called on something other than a generated
12
+ * enum, it raises an error.
13
+ */
14
+ export declare function getEnumType(enumObject: EnumObject): EnumType;
15
+ /**
16
+ * Sets reflection information on a generated enum.
17
+ */
18
+ export declare function setEnumType(
19
+ enumObject: EnumObject,
20
+ typeName: string,
21
+ values: EnumValueInfo[]
22
+ ): void;
23
+ /**
24
+ * Create a new EnumType with the given values.
25
+ */
26
+ export declare function makeEnumType(
27
+ typeName: string,
28
+ values: EnumValueInfo[]
29
+ ): EnumType;
@@ -0,0 +1,25 @@
1
+ import type { FieldInfo, OneofInfo, PartialFieldInfo } from "../field.js";
2
+ import type { FieldList } from "../field-list.js";
3
+ export declare type FieldListSource =
4
+ | readonly PartialFieldInfo[]
5
+ | readonly FieldInfo[]
6
+ | (() => readonly PartialFieldInfo[])
7
+ | (() => readonly FieldInfo[]);
8
+ export declare class InternalFieldList implements FieldList {
9
+ private readonly _fields;
10
+ private readonly _normalizer;
11
+ private all?;
12
+ private numbersAsc?;
13
+ private jsonNames?;
14
+ private numbers?;
15
+ private members?;
16
+ constructor(
17
+ fields: FieldListSource,
18
+ normalizer: (p: FieldListSource) => FieldInfo[]
19
+ );
20
+ findJsonName(jsonName: string): FieldInfo | undefined;
21
+ find(fieldNo: number): FieldInfo | undefined;
22
+ list(): readonly FieldInfo[];
23
+ byNumber(): readonly FieldInfo[];
24
+ byMember(): readonly (FieldInfo | OneofInfo)[];
25
+ }
@@ -0,0 +1,27 @@
1
+ import type { Message } from "../message.js";
2
+ import type { MessageType } from "../message-type.js";
3
+ /**
4
+ * A field wrapper unwraps a message to a primitive value that is more
5
+ * ergonomic for use as a message field.
6
+ *
7
+ * Note that this feature exists for google/protobuf/{wrappers,struct}.proto
8
+ * and cannot be used to arbitrarily modify types in generated code.
9
+ */
10
+ export interface FieldWrapper<T extends Message<T> = any, U = any> {
11
+ wrapField(value: U): T;
12
+ unwrapField(value: T): U;
13
+ }
14
+ /**
15
+ * Wrap field values whose message type has a wrapper.
16
+ */
17
+ export declare function wrapField<T extends Message<T>>(
18
+ type: MessageType<T>,
19
+ value: any
20
+ ): T;
21
+ /**
22
+ * Unwrap field values whose message type has a wrapper.
23
+ */
24
+ export declare function unwrapField<T extends Message<T>>(
25
+ type: MessageType<T>,
26
+ value: T
27
+ ): any;
@@ -0,0 +1,15 @@
1
+ import type { FieldInfo, OneofInfo } from "../field.js";
2
+ export declare class InternalOneofInfo implements OneofInfo {
3
+ readonly kind = "oneof";
4
+ readonly name: string;
5
+ readonly localName: string;
6
+ readonly repeated = false;
7
+ readonly packed = false;
8
+ readonly opt = false;
9
+ readonly default: undefined;
10
+ readonly fields: FieldInfo[];
11
+ private _lookup?;
12
+ constructor(name: string);
13
+ addField(field: FieldInfo): void;
14
+ findField(localName: string): FieldInfo | undefined;
15
+ }
@@ -0,0 +1,30 @@
1
+ import type {
2
+ JsonFormat,
3
+ JsonValue,
4
+ JsonWriteOptions,
5
+ } from "../json-format.js";
6
+ import { FieldInfo, ScalarType } from "../field.js";
7
+ import type { EnumType } from "../enum.js";
8
+ declare type JsonFormatWriteFieldFn = (
9
+ field: FieldInfo,
10
+ value: any,
11
+ options: JsonWriteOptions
12
+ ) => JsonValue | undefined;
13
+ export declare function makeJsonFormatCommon(
14
+ makeWriteField: (
15
+ writeEnumFn: typeof writeEnum,
16
+ writeScalarFn: typeof writeScalar
17
+ ) => JsonFormatWriteFieldFn
18
+ ): JsonFormat;
19
+ declare function writeEnum(
20
+ type: EnumType,
21
+ value: number | undefined,
22
+ emitIntrinsicDefault: boolean,
23
+ enumAsInteger: boolean
24
+ ): JsonValue | undefined;
25
+ declare function writeScalar(
26
+ type: ScalarType,
27
+ value: any,
28
+ emitIntrinsicDefault: boolean
29
+ ): JsonValue | undefined;
30
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { JsonFormat } from "../json-format.js";
2
+ export declare function makeJsonFormatProto2(): JsonFormat;
@@ -0,0 +1,2 @@
1
+ import type { JsonFormat } from "../json-format.js";
2
+ export declare function makeJsonFormatProto3(): JsonFormat;
@@ -0,0 +1,15 @@
1
+ import { AnyMessage, Message } from "../message.js";
2
+ import type { FieldListSource } from "./field-list.js";
3
+ import type { MessageType } from "../message-type.js";
4
+ import type { ProtoRuntime } from "./proto-runtime.js";
5
+ /**
6
+ * Create a new message type using the given runtime.
7
+ */
8
+ export declare function makeMessageType<T extends Message<T> = AnyMessage>(
9
+ runtime: ProtoRuntime,
10
+ typeName: string,
11
+ fields: FieldListSource,
12
+ opt?: {
13
+ localName?: string;
14
+ }
15
+ ): MessageType<T>;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Returns the JSON name for a protobuf field, exactly like protoc does.
3
+ */
4
+ export declare function makeJsonName(protoName: string): string;
5
+ /**
6
+ * Returns the local name of a field, exactly like protoc-gen-es does.
7
+ */
8
+ export declare function makeFieldName(
9
+ protoName: string,
10
+ inOneof: boolean
11
+ ): string;
12
+ /**
13
+ * Returns the local name of a oneof group, exactly like protoc-gen-es does.
14
+ */
15
+ export declare function makeOneofName(protoName: string): string;
16
+ /**
17
+ * Returns the local name of a rpc, exactly like protoc-gen-es does.
18
+ */
19
+ export declare function makeMethodName(protoName: string): string;
@@ -0,0 +1,7 @@
1
+ import type { JsonValue } from "../json-format";
2
+ /**
3
+ *
4
+ */
5
+ export declare type OptionsMap = {
6
+ readonly [extensionName: string]: JsonValue;
7
+ };
@@ -0,0 +1,55 @@
1
+ import type { JsonFormat, JsonValue } from "../json-format.js";
2
+ import type { BinaryFormat } from "../binary-format.js";
3
+ import type { AnyMessage } from "../message.js";
4
+ import type { Message } from "../message.js";
5
+ import type { EnumType, EnumValueInfo } from "../enum.js";
6
+ import type { MessageType } from "../message-type.js";
7
+ import type { FieldListSource } from "./field-list.js";
8
+ import type { EnumObject } from "./enum.js";
9
+ import type { Util } from "./util.js";
10
+ /**
11
+ * A facade that provides serialization and other internal functionality.
12
+ */
13
+ export interface ProtoRuntime {
14
+ readonly syntax: string;
15
+ readonly json: JsonFormat;
16
+ readonly bin: BinaryFormat;
17
+ readonly util: Util;
18
+ /**
19
+ * Create a message type at runtime, without generating code.
20
+ */
21
+ makeMessageType<T extends Message<T> = AnyMessage>(
22
+ typeName: string,
23
+ fields: FieldListSource,
24
+ opt?: {
25
+ localName?: string;
26
+ options?: {
27
+ readonly [extensionName: string]: JsonValue;
28
+ };
29
+ }
30
+ ): MessageType<T>;
31
+ /**
32
+ * Create an enum type at runtime, without generating code.
33
+ */
34
+ makeEnumType(
35
+ typeName: string,
36
+ values: EnumValueInfo[],
37
+ opt?: {
38
+ options?: {
39
+ readonly [extensionName: string]: JsonValue;
40
+ };
41
+ }
42
+ ): EnumType;
43
+ /**
44
+ * Get reflection information from a generated enum.
45
+ * If this function is called on something other than a generated
46
+ * enum, it raises an error.
47
+ */
48
+ getEnumType(enumObject: EnumObject): EnumType;
49
+ }
50
+ export declare function makeProtoRuntime(
51
+ syntax: string,
52
+ json: JsonFormat,
53
+ bin: BinaryFormat,
54
+ util: Util
55
+ ): ProtoRuntime;
@@ -0,0 +1,33 @@
1
+ import { ScalarType } from "../field.js";
2
+ import { IBinaryWriter, WireType } from "../binary-encoding.js";
3
+ /**
4
+ * Returns true if both scalar values are equal.
5
+ */
6
+ export declare function scalarEquals(
7
+ type: ScalarType,
8
+ a: string | boolean | number | bigint | Uint8Array | undefined,
9
+ b: string | boolean | number | bigint | Uint8Array | undefined
10
+ ): boolean;
11
+ /**
12
+ * Returns the default value for the given scalar type, following
13
+ * proto3 semantics.
14
+ */
15
+ export declare function scalarDefaultValue(type: ScalarType): any;
16
+ /**
17
+ * Get information for writing a scalar value.
18
+ *
19
+ * Returns tuple:
20
+ * [0]: appropriate WireType
21
+ * [1]: name of the appropriate method of IBinaryWriter
22
+ * [2]: whether the given value is a default value for proto3 semantics
23
+ *
24
+ * If argument `value` is omitted, [2] is always false.
25
+ */
26
+ export declare function scalarTypeInfo(
27
+ type: ScalarType,
28
+ value?: any
29
+ ): [
30
+ WireType,
31
+ Exclude<keyof IBinaryWriter, "tag" | "raw" | "fork" | "join" | "finish">,
32
+ boolean
33
+ ];
@@ -0,0 +1,5 @@
1
+ import type { Util } from "./util.js";
2
+ export declare function makeUtilCommon(): Omit<
3
+ Util,
4
+ "newFieldList" | "initFields"
5
+ >;
@@ -0,0 +1,55 @@
1
+ import type { FieldListSource } from "./field-list.js";
2
+ import type { FieldList } from "../field-list.js";
3
+ import type { EnumObject } from "./enum.js";
4
+ import type { JsonValue } from "../json-format.js";
5
+ import type { Message, PartialMessage, PlainMessage } from "../message.js";
6
+ import type { MessageType } from "../message-type.js";
7
+ import type { EnumValueInfo } from "../enum.js";
8
+ /**
9
+ * Provides utilities used by generated code.
10
+ * All methods are internal and are not safe to use, they may break with a
11
+ * future release.
12
+ */
13
+ export interface Util {
14
+ /**
15
+ * Create a field list
16
+ */
17
+ newFieldList(fields: FieldListSource): FieldList;
18
+ /**
19
+ * Sets reflection information on a generated enum.
20
+ */
21
+ setEnumType(
22
+ enumObject: EnumObject,
23
+ typeName: string,
24
+ values: EnumValueInfo[],
25
+ opt?: {
26
+ options?: {
27
+ readonly [extensionName: string]: JsonValue;
28
+ };
29
+ }
30
+ ): void;
31
+ /**
32
+ * Set default field values on the target message.
33
+ */
34
+ initFields(target: Message): void;
35
+ /**
36
+ * Set specified field values on the target message, recursively.
37
+ */
38
+ initPartial<T extends Message<T>>(
39
+ source: PartialMessage<T> | undefined,
40
+ target: T
41
+ ): void;
42
+ /**
43
+ * Compares two messages of the same type recursively.
44
+ * Will also return true if both messages are `undefined`.
45
+ */
46
+ equals<T extends Message<T>>(
47
+ type: MessageType,
48
+ a: T | PlainMessage<T> | undefined,
49
+ b: T | PlainMessage<T> | undefined
50
+ ): boolean;
51
+ /**
52
+ * Create a deep copy.
53
+ */
54
+ clone<T extends Message<T>>(message: T): T;
55
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * We use the `bigint` primitive to represent 64-bit integral types. If bigint
3
+ * is unavailable, we fall back to a string representation, which means that
4
+ * all values typed as `bigint` will actually be strings.
5
+ *
6
+ * If your code is intended to run in an environment where bigint may be
7
+ * unavailable, it must handle both the bigint and the string representation.
8
+ * For presenting values, this is straight-forward with implicit or explicit
9
+ * conversion to string:
10
+ *
11
+ * ```ts
12
+ * let el = document.createElement("span");
13
+ * el.innerText = message.int64Field; // assuming a protobuf int64 field
14
+ *
15
+ * console.log(`int64: ${message.int64Field}`);
16
+ *
17
+ * let str: string = message.int64Field.toString();
18
+ * ```
19
+ *
20
+ * If you need to manipulate 64-bit integral values and are sure the values
21
+ * can be safely represented as an IEEE-754 double precision number, you can
22
+ * convert to a JavaScript Number:
23
+ *
24
+ * ```ts
25
+ * console.log(message.int64Field.toString())
26
+ * let num = Number(message.int64Field);
27
+ * num = num + 1;
28
+ * message.int64Field = protoInt64.parse(num);
29
+ * ```
30
+ *
31
+ * If you need to manipulate 64-bit integral values that are outside the
32
+ * range of safe representation as Number, you can work with the two's
33
+ * complement directly. The following example negates a field value:
34
+ *
35
+ * ```ts
36
+ * let t = protoInt64.enc(message.int64Field);
37
+ * t.hi = ~t.hi;
38
+ * if (t.lo) {
39
+ * t.lo = ~t.lo + 1;
40
+ * } else {
41
+ * t.hi += 1;
42
+ * }
43
+ * message.int64Field = protoInt64.dec(t.lo, t.hi);
44
+ * ```
45
+ *
46
+ * There are several 3rd party libraries that provide arithmetic operations
47
+ * on a two's complement, for example the npm package "long".
48
+ */
49
+ interface Int64Support {
50
+ /**
51
+ * 0n if bigint is available, "0" if unavailable.
52
+ */
53
+ readonly zero: bigint;
54
+ /**
55
+ * Is bigint available?
56
+ */
57
+ readonly supported: boolean;
58
+ /**
59
+ * Parse a signed 64-bit integer.
60
+ * Returns a bigint if available, a string otherwise.
61
+ */
62
+ parse(value: string | number | bigint): bigint;
63
+ /**
64
+ * Parse an unsigned 64-bit integer.
65
+ * Returns a bigint if available, a string otherwise.
66
+ */
67
+ uParse(value: string | number | bigint): bigint;
68
+ /**
69
+ * Convert a signed 64-bit integral value to a two's complement.
70
+ */
71
+ enc(value: string | number | bigint): {
72
+ lo: number;
73
+ hi: number;
74
+ };
75
+ /**
76
+ * Convert an unsigned 64-bit integral value to a two's complement.
77
+ */
78
+ uEnc(value: string | number | bigint): {
79
+ lo: number;
80
+ hi: number;
81
+ };
82
+ /**
83
+ * Convert a two's complement to a signed 64-bit integral value.
84
+ * Returns a bigint if available, a string otherwise.
85
+ */
86
+ dec(lo: number, hi: number): bigint;
87
+ /**
88
+ * Convert a two's complement to an unsigned 64-bit integral value.
89
+ * Returns a bigint if available, a string otherwise.
90
+ */
91
+ uDec(lo: number, hi: number): bigint;
92
+ }
93
+ export declare const protoInt64: Int64Support;
94
+ export {};
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Provides functionality for messages defined with the proto2 syntax.
3
+ */
4
+ export declare const proto2: import("./private/proto-runtime.js").ProtoRuntime;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Provides functionality for messages defined with the proto3 syntax.
3
+ */
4
+ export declare const proto3: import("./private/proto-runtime.js").ProtoRuntime;