@bufbuild/protobuf 2.0.0-alpha.1 → 2.0.0-alpha.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.
- package/dist/cjs/codegenv1/embed.d.ts +2 -2
- package/dist/cjs/codegenv1/embed.js +25 -25
- package/dist/cjs/codegenv1/enum.js +2 -4
- package/dist/cjs/codegenv1/restore-json-names.d.ts +3 -0
- package/dist/cjs/codegenv1/restore-json-names.js +3 -0
- package/dist/cjs/codegenv1/symbols.d.ts +9 -0
- package/dist/cjs/codegenv1/symbols.js +9 -0
- package/dist/cjs/codegenv1/types.d.ts +55 -2
- package/dist/cjs/create.js +7 -18
- package/dist/cjs/desc-types.d.ts +27 -10
- package/dist/cjs/extensions.d.ts +21 -1
- package/dist/cjs/extensions.js +38 -7
- package/dist/cjs/fields.d.ts +17 -16
- package/dist/cjs/fields.js +17 -10
- package/dist/cjs/from-binary.js +9 -19
- package/dist/cjs/from-json.js +9 -13
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/reflect/names.d.ts +0 -12
- package/dist/cjs/reflect/names.js +1 -39
- package/dist/cjs/reflect/nested-types.d.ts +11 -1
- package/dist/cjs/reflect/nested-types.js +34 -1
- package/dist/cjs/reflect/reflect-types.d.ts +158 -10
- package/dist/cjs/reflect/reflect.d.ts +11 -2
- package/dist/cjs/reflect/reflect.js +306 -281
- package/dist/cjs/reflect/registry.js +50 -41
- package/dist/cjs/reflect/unsafe.js +38 -43
- package/dist/cjs/to-binary.js +1 -3
- package/dist/cjs/to-json.js +7 -27
- package/dist/cjs/wire/base64-encoding.js +2 -2
- package/dist/cjs/wire/binary-encoding.d.ts +5 -3
- package/dist/cjs/wire/binary-encoding.js +16 -10
- package/dist/cjs/wkt/gen/google/protobuf/any_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/any_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/api_pb.d.ts +15 -0
- package/dist/cjs/wkt/gen/google/protobuf/api_pb.js +15 -6
- package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +22 -0
- package/dist/cjs/wkt/gen/google/protobuf/compiler/plugin_pb.js +22 -9
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +186 -0
- package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +186 -83
- package/dist/cjs/wkt/gen/google/protobuf/duration_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/duration_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/empty_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/empty_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/field_mask_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/source_context_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/struct_pb.d.ts +18 -0
- package/dist/cjs/wkt/gen/google/protobuf/struct_pb.js +18 -7
- package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.d.ts +7 -0
- package/dist/cjs/wkt/gen/google/protobuf/timestamp_pb.js +7 -2
- package/dist/cjs/wkt/gen/google/protobuf/type_pb.d.ts +32 -0
- package/dist/cjs/wkt/gen/google/protobuf/type_pb.js +32 -13
- package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.d.ts +39 -0
- package/dist/cjs/wkt/gen/google/protobuf/wrappers_pb.js +39 -18
- package/dist/esm/codegenv1/embed.d.ts +2 -2
- package/dist/esm/codegenv1/embed.js +25 -25
- package/dist/esm/codegenv1/enum.js +2 -4
- package/dist/esm/codegenv1/restore-json-names.d.ts +3 -0
- package/dist/esm/codegenv1/restore-json-names.js +3 -0
- package/dist/esm/codegenv1/symbols.d.ts +9 -0
- package/dist/esm/codegenv1/symbols.js +9 -0
- package/dist/esm/codegenv1/types.d.ts +55 -2
- package/dist/esm/create.js +7 -18
- package/dist/esm/desc-types.d.ts +27 -10
- package/dist/esm/extensions.d.ts +21 -1
- package/dist/esm/extensions.js +35 -6
- package/dist/esm/fields.d.ts +17 -16
- package/dist/esm/fields.js +17 -10
- package/dist/esm/from-binary.js +9 -19
- package/dist/esm/from-json.js +11 -15
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/reflect/names.d.ts +0 -12
- package/dist/esm/reflect/names.js +0 -37
- package/dist/esm/reflect/nested-types.d.ts +11 -1
- package/dist/esm/reflect/nested-types.js +32 -0
- package/dist/esm/reflect/reflect-types.d.ts +158 -10
- package/dist/esm/reflect/reflect.d.ts +11 -2
- package/dist/esm/reflect/reflect.js +304 -279
- package/dist/esm/reflect/registry.js +50 -41
- package/dist/esm/reflect/unsafe.js +38 -43
- package/dist/esm/to-binary.js +1 -3
- package/dist/esm/to-json.js +7 -27
- package/dist/esm/wire/base64-encoding.js +2 -2
- package/dist/esm/wire/binary-encoding.d.ts +5 -3
- package/dist/esm/wire/binary-encoding.js +16 -10
- package/dist/esm/wkt/gen/google/protobuf/any_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/any_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/api_pb.d.ts +15 -0
- package/dist/esm/wkt/gen/google/protobuf/api_pb.js +15 -6
- package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.d.ts +22 -0
- package/dist/esm/wkt/gen/google/protobuf/compiler/plugin_pb.js +22 -9
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +186 -0
- package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +186 -83
- package/dist/esm/wkt/gen/google/protobuf/duration_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/duration_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/empty_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/empty_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/field_mask_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/source_context_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/source_context_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/struct_pb.d.ts +18 -0
- package/dist/esm/wkt/gen/google/protobuf/struct_pb.js +18 -7
- package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.d.ts +7 -0
- package/dist/esm/wkt/gen/google/protobuf/timestamp_pb.js +7 -2
- package/dist/esm/wkt/gen/google/protobuf/type_pb.d.ts +32 -0
- package/dist/esm/wkt/gen/google/protobuf/type_pb.js +32 -13
- package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.d.ts +39 -0
- package/dist/esm/wkt/gen/google/protobuf/wrappers_pb.js +39 -18
- package/package.json +2 -2
|
@@ -13,6 +13,9 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { protoCamelCase } from "../reflect/names.js";
|
|
15
15
|
import { unsafeIsSetExplicit } from "../reflect/unsafe.js";
|
|
16
|
+
/**
|
|
17
|
+
* @private
|
|
18
|
+
*/
|
|
16
19
|
export function restoreJsonNames(message) {
|
|
17
20
|
for (const f of message.field) {
|
|
18
21
|
if (!unsafeIsSetExplicit(f, "jsonName")) {
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @private
|
|
3
|
+
*/
|
|
1
4
|
export declare const packageName = "@bufbuild/protobuf";
|
|
5
|
+
/**
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
2
8
|
export declare const wktPublicImportPaths: Readonly<Record<string, string>>;
|
|
9
|
+
/**
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
3
12
|
export declare const symbols: {
|
|
4
13
|
readonly isMessage: {
|
|
5
14
|
readonly typeOnly: false;
|
|
@@ -11,7 +11,13 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
14
17
|
export const packageName = "@bufbuild/protobuf";
|
|
18
|
+
/**
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
15
21
|
export const wktPublicImportPaths = {
|
|
16
22
|
"google/protobuf/compiler/plugin.proto": packageName + "/wkt",
|
|
17
23
|
"google/protobuf/any.proto": packageName + "/wkt",
|
|
@@ -26,6 +32,9 @@ export const wktPublicImportPaths = {
|
|
|
26
32
|
"google/protobuf/type.proto": packageName + "/wkt",
|
|
27
33
|
"google/protobuf/wrappers.proto": packageName + "/wkt",
|
|
28
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
29
38
|
// prettier-ignore
|
|
30
39
|
export const symbols = {
|
|
31
40
|
isMessage: { typeOnly: false, bootstrapWktFrom: "../../is-message.js", from: packageName },
|
|
@@ -1,10 +1,52 @@
|
|
|
1
1
|
import type { Message } from "../types.js";
|
|
2
|
-
import type { DescEnum, DescExtension, DescFile, DescMessage, DescService } from "../desc-types.js";
|
|
2
|
+
import type { DescEnum, DescExtension, DescField, DescFile, DescMessage, DescService } from "../desc-types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Describes a protobuf source file.
|
|
5
|
+
*
|
|
6
|
+
* @private
|
|
7
|
+
*/
|
|
3
8
|
export type GenDescFile = DescFile;
|
|
4
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Describes a message declaration in a protobuf source file.
|
|
11
|
+
*
|
|
12
|
+
* This type is identical to DescMessage, but carries additional type
|
|
13
|
+
* information.
|
|
14
|
+
*
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
17
|
+
export type GenDescMessage<RuntimeShape extends Message> = Omit<DescMessage, "field"> & {
|
|
18
|
+
field: Record<MessageFieldNames<RuntimeShape>, DescField>;
|
|
19
|
+
} & brand<RuntimeShape>;
|
|
20
|
+
/**
|
|
21
|
+
* Describes an enumeration in a protobuf source file.
|
|
22
|
+
*
|
|
23
|
+
* This type is identical to DescEnum, but carries additional type
|
|
24
|
+
* information.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
5
28
|
export type GenDescEnum<RuntimeShape> = DescEnum & brand<RuntimeShape>;
|
|
29
|
+
/**
|
|
30
|
+
* Describes an extension in a protobuf source file.
|
|
31
|
+
*
|
|
32
|
+
* This type is identical to DescExtension, but carries additional type
|
|
33
|
+
* information.
|
|
34
|
+
*
|
|
35
|
+
* @private
|
|
36
|
+
*/
|
|
6
37
|
export type GenDescExtension<Extendee extends Message = Message, RuntimeShape = unknown> = DescExtension & brand<Extendee, RuntimeShape>;
|
|
38
|
+
/**
|
|
39
|
+
* Describes a service declaration in a protobuf source file.
|
|
40
|
+
*
|
|
41
|
+
* This type is identical to DescService, but carries additional type
|
|
42
|
+
* information.
|
|
43
|
+
*
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
7
46
|
export type GenDescService<RuntimeShape extends GenDescServiceShape> = DescService & brand<RuntimeShape>;
|
|
47
|
+
/**
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
8
50
|
export type GenDescServiceShape = {
|
|
9
51
|
[localName: string]: {
|
|
10
52
|
kind: "unary" | "server_streaming" | "client_streaming" | "bidi_streaming";
|
|
@@ -16,4 +58,15 @@ declare class brand<A, B = unknown> {
|
|
|
16
58
|
protected a: A | boolean;
|
|
17
59
|
protected b: B | boolean;
|
|
18
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Union of the property names of all fields, including oneof members.
|
|
63
|
+
* For an anonymous message (no generated message shape), it's simply a string.
|
|
64
|
+
*/
|
|
65
|
+
type MessageFieldNames<T extends Message> = Message extends T ? string : Exclude<keyof {
|
|
66
|
+
[P in keyof T as P extends ("$typeName" | "$unknown") ? never : T[P] extends Oneof<infer K> ? K : P]-?: true;
|
|
67
|
+
}, number | symbol>;
|
|
68
|
+
type Oneof<K extends string> = {
|
|
69
|
+
case: K | undefined;
|
|
70
|
+
value?: unknown;
|
|
71
|
+
};
|
|
19
72
|
export {};
|
package/dist/esm/create.js
CHANGED
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
import { isMessage } from "./is-message.js";
|
|
15
|
-
import { localName } from "./reflect/names.js";
|
|
16
15
|
import { LongType, ScalarType, scalarZeroValue } from "./reflect/scalar.js";
|
|
17
16
|
import { FieldError } from "./reflect/error.js";
|
|
18
17
|
import { isObject } from "./reflect/guard.js";
|
|
@@ -20,8 +19,6 @@ import { unsafeGet, unsafeOneofCase, unsafeSet } from "./reflect/unsafe.js";
|
|
|
20
19
|
import { isWrapperDesc } from "./wkt/wrappers.js";
|
|
21
20
|
// bootstrap-inject google.protobuf.Edition.EDITION_PROTO3: const $name: Edition.$localName = $number;
|
|
22
21
|
const EDITION_PROTO3 = 999;
|
|
23
|
-
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.EXPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
24
|
-
const EXPLICIT = 1;
|
|
25
22
|
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
|
|
26
23
|
const IMPLICIT = 2;
|
|
27
24
|
/**
|
|
@@ -45,7 +42,7 @@ export function create(messageDesc, init) {
|
|
|
45
42
|
*/
|
|
46
43
|
function initMessage(messageDesc, message, init) {
|
|
47
44
|
for (const member of messageDesc.members) {
|
|
48
|
-
let value = init[localName
|
|
45
|
+
let value = init[member.localName];
|
|
49
46
|
if (value == null) {
|
|
50
47
|
// intentionally ignore undefined and null
|
|
51
48
|
continue;
|
|
@@ -143,17 +140,9 @@ function createZeroMessage(desc) {
|
|
|
143
140
|
// the `optional` keyword generate an optional property.
|
|
144
141
|
msg = {};
|
|
145
142
|
for (const member of desc.members) {
|
|
146
|
-
if (member.kind == "
|
|
147
|
-
|
|
148
|
-
member.fieldKind == "enum" ||
|
|
149
|
-
member.fieldKind == "message") {
|
|
150
|
-
if (member.presence == EXPLICIT) {
|
|
151
|
-
// skips message fields and optional scalar/enum
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
143
|
+
if (member.kind == "oneof" || member.presence == IMPLICIT) {
|
|
144
|
+
msg[member.localName] = createZeroField(member);
|
|
155
145
|
}
|
|
156
|
-
msg[localName(member)] = createZeroField(member);
|
|
157
146
|
}
|
|
158
147
|
}
|
|
159
148
|
else {
|
|
@@ -180,12 +169,12 @@ function createZeroMessage(desc) {
|
|
|
180
169
|
continue;
|
|
181
170
|
}
|
|
182
171
|
if (member.presence == IMPLICIT) {
|
|
183
|
-
// implicit presence tracks field presence by zero values
|
|
184
|
-
//
|
|
172
|
+
// implicit presence tracks field presence by zero values - e.g. 0, false, "", are unset, 1, true, "x" are set.
|
|
173
|
+
// message, map, list fields are mutable, and also have IMPLICIT presence.
|
|
185
174
|
continue;
|
|
186
175
|
}
|
|
187
176
|
members.add(member);
|
|
188
|
-
prototype[localName
|
|
177
|
+
prototype[member.localName] = createZeroField(member);
|
|
189
178
|
}
|
|
190
179
|
messagePrototypes.set(desc, { prototype, members });
|
|
191
180
|
}
|
|
@@ -204,7 +193,7 @@ function createZeroMessage(desc) {
|
|
|
204
193
|
}
|
|
205
194
|
}
|
|
206
195
|
}
|
|
207
|
-
msg[localName
|
|
196
|
+
msg[member.localName] = createZeroField(member);
|
|
208
197
|
}
|
|
209
198
|
}
|
|
210
199
|
msg.$typeName = desc.typeName;
|
package/dist/esm/desc-types.d.ts
CHANGED
|
@@ -108,6 +108,10 @@ export interface DescEnumValue {
|
|
|
108
108
|
* The name of the enumeration value, as specified in the protobuf source.
|
|
109
109
|
*/
|
|
110
110
|
readonly name: string;
|
|
111
|
+
/**
|
|
112
|
+
* A safe and idiomatic name for the value in a TypeScript enum.
|
|
113
|
+
*/
|
|
114
|
+
readonly localName: string;
|
|
111
115
|
/**
|
|
112
116
|
* The enumeration this value belongs to.
|
|
113
117
|
*/
|
|
@@ -152,6 +156,10 @@ export interface DescMessage {
|
|
|
152
156
|
* group.
|
|
153
157
|
*/
|
|
154
158
|
readonly fields: DescField[];
|
|
159
|
+
/**
|
|
160
|
+
* All fields of this message by their "localName".
|
|
161
|
+
*/
|
|
162
|
+
readonly field: Record<string, DescField>;
|
|
155
163
|
/**
|
|
156
164
|
* Oneof groups declared for this message.
|
|
157
165
|
* This does not include synthetic oneofs for proto3 optionals.
|
|
@@ -195,6 +203,10 @@ type descFieldCommon = descFieldAndExtensionShared & {
|
|
|
195
203
|
* The message this field is declared on.
|
|
196
204
|
*/
|
|
197
205
|
readonly parent: DescMessage;
|
|
206
|
+
/**
|
|
207
|
+
* A safe and idiomatic name for the field as a property in ECMAScript.
|
|
208
|
+
*/
|
|
209
|
+
readonly localName: string;
|
|
198
210
|
};
|
|
199
211
|
/**
|
|
200
212
|
* Describes an extension in a protobuf source file.
|
|
@@ -240,6 +252,11 @@ interface descFieldAndExtensionShared {
|
|
|
240
252
|
* Marked as deprecated in the protobuf source.
|
|
241
253
|
*/
|
|
242
254
|
readonly deprecated: boolean;
|
|
255
|
+
/**
|
|
256
|
+
* Presence of the field.
|
|
257
|
+
* See https://protobuf.dev/programming-guides/field_presence/
|
|
258
|
+
*/
|
|
259
|
+
readonly presence: SupportedFieldPresence;
|
|
243
260
|
/**
|
|
244
261
|
* The compiler-generated descriptor.
|
|
245
262
|
*/
|
|
@@ -256,15 +273,6 @@ type descFieldSingularCommon = {
|
|
|
256
273
|
* This does not include synthetic oneofs for proto3 optionals.
|
|
257
274
|
*/
|
|
258
275
|
readonly oneof: DescOneof | undefined;
|
|
259
|
-
/**
|
|
260
|
-
* Whether this field was declared with `optional` in the protobuf source.
|
|
261
|
-
*/
|
|
262
|
-
readonly optional: boolean;
|
|
263
|
-
/**
|
|
264
|
-
* Presence of the field.
|
|
265
|
-
* See https://protobuf.dev/programming-guides/field_presence/
|
|
266
|
-
*/
|
|
267
|
-
readonly presence: SupportedFieldPresence;
|
|
268
276
|
};
|
|
269
277
|
type descFieldScalar<T extends ScalarType = ScalarType> = T extends T ? {
|
|
270
278
|
readonly fieldKind: "scalar";
|
|
@@ -414,7 +422,8 @@ type descFieldMapCommon<T extends ScalarType = ScalarType> = T extends Exclude<S
|
|
|
414
422
|
readonly oneof: undefined;
|
|
415
423
|
/**
|
|
416
424
|
* Encode the map entry message delimited (a.k.a. proto2 group encoding),
|
|
417
|
-
* or length-prefixed?
|
|
425
|
+
* or length-prefixed? As of Edition 2023, this is always false for map fields,
|
|
426
|
+
* and also applies to map values, if they are messages.
|
|
418
427
|
*/
|
|
419
428
|
readonly delimitedEncoding: false;
|
|
420
429
|
} : never;
|
|
@@ -472,6 +481,10 @@ export interface DescOneof {
|
|
|
472
481
|
* The name of the oneof group, as specified in the protobuf source.
|
|
473
482
|
*/
|
|
474
483
|
readonly name: string;
|
|
484
|
+
/**
|
|
485
|
+
* A safe and idiomatic name for the oneof group as a property in ECMAScript.
|
|
486
|
+
*/
|
|
487
|
+
readonly localName: string;
|
|
475
488
|
/**
|
|
476
489
|
* The message this oneof group was declared in.
|
|
477
490
|
*/
|
|
@@ -532,6 +545,10 @@ export interface DescMethod {
|
|
|
532
545
|
* The name of the RPC, as specified in the protobuf source.
|
|
533
546
|
*/
|
|
534
547
|
readonly name: string;
|
|
548
|
+
/**
|
|
549
|
+
* A safe and idiomatic name for the RPC as a method in ECMAScript.
|
|
550
|
+
*/
|
|
551
|
+
readonly localName: string;
|
|
535
552
|
/**
|
|
536
553
|
* The parent service.
|
|
537
554
|
*/
|
package/dist/esm/extensions.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { DescExtension, DescField } from "./desc-types.js";
|
|
1
|
+
import type { AnyDesc, DescEnum, DescEnumValue, DescExtension, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService } from "./desc-types.js";
|
|
2
2
|
import type { ReflectMessage } from "./reflect/reflect-types.js";
|
|
3
3
|
import type { Extendee, ExtensionValueShape } from "./types.js";
|
|
4
|
+
import type { EnumOptions, EnumValueOptions, FieldOptions, FileOptions, MessageOptions, MethodOptions, OneofOptions, ServiceOptions } from "./wkt/gen/google/protobuf/descriptor_pb.js";
|
|
4
5
|
/**
|
|
5
6
|
* Retrieve an extension value from a message.
|
|
6
7
|
*
|
|
@@ -33,7 +34,26 @@ export declare function clearExtension<Desc extends DescExtension>(message: Exte
|
|
|
33
34
|
* Check whether an extension is set on a message.
|
|
34
35
|
*/
|
|
35
36
|
export declare function hasExtension<Desc extends DescExtension>(message: Extendee<Desc>, extension: Desc): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Check whether an option is set on a descriptor.
|
|
39
|
+
*
|
|
40
|
+
* Options are extensions to the `google.protobuf.*Options` messages defined in
|
|
41
|
+
* google/protobuf/descriptor.proto. This function gets the option message from
|
|
42
|
+
* the descriptor, and calls hasExtension().
|
|
43
|
+
*/
|
|
44
|
+
export declare function hasOption<Ext extends DescExtension, Desc extends DescForOptionExtension<Ext>>(element: Desc, option: Ext): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Retrieve an option value from a descriptor.
|
|
47
|
+
*
|
|
48
|
+
* Options are extensions to the `google.protobuf.*Options` messages defined in
|
|
49
|
+
* google/protobuf/descriptor.proto. This function gets the option message from
|
|
50
|
+
* the descriptor, and calls getExtension(). Same as getExtension(), this
|
|
51
|
+
* function never returns undefined.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getOption<Ext extends DescExtension, Desc extends DescForOptionExtension<Ext>>(element: Desc, option: Ext): ExtensionValueShape<Ext>;
|
|
54
|
+
type DescForOptionExtension<Ext extends DescExtension> = Extendee<Ext> extends FileOptions ? DescFile : Extendee<Ext> extends EnumOptions ? DescEnum : Extendee<Ext> extends EnumValueOptions ? DescEnumValue : Extendee<Ext> extends MessageOptions ? DescMessage : Extendee<Ext> extends MessageOptions ? DescEnum : Extendee<Ext> extends FieldOptions ? DescField | DescExtension : Extendee<Ext> extends OneofOptions ? DescOneof : Extendee<Ext> extends ServiceOptions ? DescService : Extendee<Ext> extends EnumOptions ? DescEnum : Extendee<Ext> extends MethodOptions ? DescMethod : AnyDesc;
|
|
36
55
|
/**
|
|
37
56
|
* @private
|
|
38
57
|
*/
|
|
39
58
|
export declare function createExtensionContainer<Desc extends DescExtension>(extension: Desc, value?: ExtensionValueShape<Desc>): [ReflectMessage, DescField, () => ExtensionValueShape<Desc>];
|
|
59
|
+
export {};
|
package/dist/esm/extensions.js
CHANGED
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
import { assert } from "./reflect/assert.js";
|
|
15
15
|
import { create } from "./create.js";
|
|
16
16
|
import { readField } from "./from-binary.js";
|
|
17
|
-
import { localName } from "./reflect/names.js";
|
|
18
17
|
import { reflect } from "./reflect/reflect.js";
|
|
19
18
|
import { scalarZeroValue } from "./reflect/scalar.js";
|
|
20
19
|
import { writeField } from "./to-binary.js";
|
|
@@ -61,7 +60,7 @@ export function setExtension(message, extension, value) {
|
|
|
61
60
|
const reader = new BinaryReader(writer.finish());
|
|
62
61
|
while (reader.pos < reader.len) {
|
|
63
62
|
const [no, wireType] = reader.tag();
|
|
64
|
-
const data = reader.skip(wireType);
|
|
63
|
+
const data = reader.skip(wireType, no);
|
|
65
64
|
ufs.push({ no, wireType, data });
|
|
66
65
|
}
|
|
67
66
|
message.$unknown = ufs;
|
|
@@ -86,6 +85,36 @@ export function hasExtension(message, extension) {
|
|
|
86
85
|
return (extension.extendee.typeName === message.$typeName &&
|
|
87
86
|
!!((_a = message.$unknown) === null || _a === void 0 ? void 0 : _a.find((uf) => uf.no === extension.number)));
|
|
88
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Check whether an option is set on a descriptor.
|
|
90
|
+
*
|
|
91
|
+
* Options are extensions to the `google.protobuf.*Options` messages defined in
|
|
92
|
+
* google/protobuf/descriptor.proto. This function gets the option message from
|
|
93
|
+
* the descriptor, and calls hasExtension().
|
|
94
|
+
*/
|
|
95
|
+
export function hasOption(element, option) {
|
|
96
|
+
const message = element.proto.options;
|
|
97
|
+
if (!message) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return hasExtension(message, option);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Retrieve an option value from a descriptor.
|
|
104
|
+
*
|
|
105
|
+
* Options are extensions to the `google.protobuf.*Options` messages defined in
|
|
106
|
+
* google/protobuf/descriptor.proto. This function gets the option message from
|
|
107
|
+
* the descriptor, and calls getExtension(). Same as getExtension(), this
|
|
108
|
+
* function never returns undefined.
|
|
109
|
+
*/
|
|
110
|
+
export function getOption(element, option) {
|
|
111
|
+
const message = element.proto.options;
|
|
112
|
+
if (!message) {
|
|
113
|
+
const [, , get] = createExtensionContainer(option);
|
|
114
|
+
return get();
|
|
115
|
+
}
|
|
116
|
+
return getExtension(message, option);
|
|
117
|
+
}
|
|
89
118
|
function filterUnknownFields(unknownFields, extension) {
|
|
90
119
|
if (unknownFields === undefined)
|
|
91
120
|
return [];
|
|
@@ -104,15 +133,15 @@ function filterUnknownFields(unknownFields, extension) {
|
|
|
104
133
|
* @private
|
|
105
134
|
*/
|
|
106
135
|
export function createExtensionContainer(extension, value) {
|
|
107
|
-
const
|
|
136
|
+
const localName = extension.typeName;
|
|
137
|
+
const field = Object.assign(Object.assign({}, extension), { kind: "field", parent: extension.extendee, localName });
|
|
108
138
|
const desc = Object.assign(Object.assign({}, extension.extendee), { fields: [field], members: [field], oneofs: [] });
|
|
109
|
-
const
|
|
110
|
-
const container = create(desc, value !== undefined ? { [fieldName]: value } : undefined);
|
|
139
|
+
const container = create(desc, value !== undefined ? { [localName]: value } : undefined);
|
|
111
140
|
return [
|
|
112
141
|
reflect(desc, container),
|
|
113
142
|
field,
|
|
114
143
|
() => {
|
|
115
|
-
const value = container[
|
|
144
|
+
const value = container[localName];
|
|
116
145
|
if (value === undefined) {
|
|
117
146
|
// Only message fields are undefined, rest will have a zero value.
|
|
118
147
|
const desc = extension.message;
|
package/dist/esm/fields.d.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { DescMessage } from "./desc-types.js";
|
|
1
|
+
import type { MessageShape } from "./types.js";
|
|
2
|
+
import type { DescField, DescMessage } from "./desc-types.js";
|
|
3
3
|
/**
|
|
4
4
|
* Returns true if the field is set.
|
|
5
|
+
*
|
|
6
|
+
* - Scalar and enum fields with implicit presence (proto3):
|
|
7
|
+
* Set if not a zero value.
|
|
8
|
+
*
|
|
9
|
+
* - Scalar and enum fields with explicit presence (proto2, oneof):
|
|
10
|
+
* Set if a value was set when creating or parsing the message, or when a
|
|
11
|
+
* value was assigned to the field's property.
|
|
12
|
+
*
|
|
13
|
+
* - Message fields:
|
|
14
|
+
* Set if the property is not undefined.
|
|
15
|
+
*
|
|
16
|
+
* - List and map fields:
|
|
17
|
+
* Set if not empty.
|
|
5
18
|
*/
|
|
6
|
-
export declare function isFieldSet<Desc extends DescMessage>(
|
|
19
|
+
export declare function isFieldSet<Desc extends DescMessage>(message: MessageShape<Desc>, field: DescField): boolean;
|
|
7
20
|
/**
|
|
8
21
|
* Resets the field, so that isFieldSet() will return false.
|
|
9
22
|
*/
|
|
10
|
-
export declare function clearField<Desc extends DescMessage>(
|
|
11
|
-
/**
|
|
12
|
-
* Union of the property names of all fields, including oneof members.
|
|
13
|
-
* For an anonymous message (no generated message shape), it's simply a string.
|
|
14
|
-
*/
|
|
15
|
-
type MessageFieldNames<T extends Message> = Message extends T ? string : Exclude<keyof {
|
|
16
|
-
[P in keyof T as P extends ("$typeName" | "$unknown") ? never : T[P] extends Oneof<infer K> ? K : P]-?: true;
|
|
17
|
-
}, number | symbol>;
|
|
18
|
-
type Oneof<K extends string> = {
|
|
19
|
-
case: K | undefined;
|
|
20
|
-
value?: unknown;
|
|
21
|
-
};
|
|
22
|
-
export {};
|
|
23
|
+
export declare function clearField<Desc extends DescMessage>(message: MessageShape<Desc>, field: DescField): void;
|
package/dist/esm/fields.js
CHANGED
|
@@ -11,24 +11,31 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import { localName } from "./reflect/names.js";
|
|
15
14
|
import { unsafeClear, unsafeIsSet } from "./reflect/unsafe.js";
|
|
16
15
|
/**
|
|
17
16
|
* Returns true if the field is set.
|
|
17
|
+
*
|
|
18
|
+
* - Scalar and enum fields with implicit presence (proto3):
|
|
19
|
+
* Set if not a zero value.
|
|
20
|
+
*
|
|
21
|
+
* - Scalar and enum fields with explicit presence (proto2, oneof):
|
|
22
|
+
* Set if a value was set when creating or parsing the message, or when a
|
|
23
|
+
* value was assigned to the field's property.
|
|
24
|
+
*
|
|
25
|
+
* - Message fields:
|
|
26
|
+
* Set if the property is not undefined.
|
|
27
|
+
*
|
|
28
|
+
* - List and map fields:
|
|
29
|
+
* Set if not empty.
|
|
18
30
|
*/
|
|
19
|
-
export function isFieldSet(
|
|
20
|
-
|
|
21
|
-
if (field) {
|
|
22
|
-
return unsafeIsSet(message, field);
|
|
23
|
-
}
|
|
24
|
-
return false;
|
|
31
|
+
export function isFieldSet(message, field) {
|
|
32
|
+
return (field.parent.typeName == message.$typeName && unsafeIsSet(message, field));
|
|
25
33
|
}
|
|
26
34
|
/**
|
|
27
35
|
* Resets the field, so that isFieldSet() will return false.
|
|
28
36
|
*/
|
|
29
|
-
export function clearField(
|
|
30
|
-
|
|
31
|
-
if (field) {
|
|
37
|
+
export function clearField(message, field) {
|
|
38
|
+
if (field.parent.typeName == message.$typeName) {
|
|
32
39
|
unsafeClear(message, field);
|
|
33
40
|
}
|
|
34
41
|
}
|
package/dist/esm/from-binary.js
CHANGED
|
@@ -57,12 +57,12 @@ function readMessage(message, reader, options, delimited, lengthOrDelimitedField
|
|
|
57
57
|
const unknownFields = (_a = message.getUnknown()) !== null && _a !== void 0 ? _a : [];
|
|
58
58
|
while (reader.pos < end) {
|
|
59
59
|
[fieldNo, wireType] = reader.tag();
|
|
60
|
-
if (wireType == WireType.EndGroup) {
|
|
60
|
+
if (delimited && wireType == WireType.EndGroup) {
|
|
61
61
|
break;
|
|
62
62
|
}
|
|
63
63
|
const field = message.findNumber(fieldNo);
|
|
64
64
|
if (!field) {
|
|
65
|
-
const data = reader.skip(wireType);
|
|
65
|
+
const data = reader.skip(wireType, fieldNo);
|
|
66
66
|
if (options.readUnknownFields) {
|
|
67
67
|
unknownFields.push({ no: fieldNo, wireType, data });
|
|
68
68
|
}
|
|
@@ -85,7 +85,7 @@ function readMessage(message, reader, options, delimited, lengthOrDelimitedField
|
|
|
85
85
|
export function readField(message, reader, field, wireType, options) {
|
|
86
86
|
switch (field.fieldKind) {
|
|
87
87
|
case "scalar":
|
|
88
|
-
message.set(field, readScalar(reader, field.scalar
|
|
88
|
+
message.set(field, readScalar(reader, field.scalar));
|
|
89
89
|
break;
|
|
90
90
|
case "enum":
|
|
91
91
|
message.set(field, readScalar(reader, ScalarType.INT32));
|
|
@@ -97,14 +97,12 @@ export function readField(message, reader, field, wireType, options) {
|
|
|
97
97
|
readListField(message, reader, options, field, wireType);
|
|
98
98
|
break;
|
|
99
99
|
case "map":
|
|
100
|
-
|
|
101
|
-
const [key, val] = readMapEntry(field, reader, options);
|
|
102
|
-
message.setMapEntry(field, key, val);
|
|
100
|
+
readMapEntry(message, field, reader, options);
|
|
103
101
|
break;
|
|
104
102
|
}
|
|
105
103
|
}
|
|
106
104
|
// Read a map field, expecting key field = 1, value field = 2
|
|
107
|
-
function readMapEntry(field, reader, options) {
|
|
105
|
+
function readMapEntry(message, field, reader, options) {
|
|
108
106
|
let key, val;
|
|
109
107
|
const end = reader.pos + reader.uint32();
|
|
110
108
|
while (reader.pos < end) {
|
|
@@ -144,7 +142,7 @@ function readMapEntry(field, reader, options) {
|
|
|
144
142
|
break;
|
|
145
143
|
}
|
|
146
144
|
}
|
|
147
|
-
|
|
145
|
+
message.setMapEntry(field, key, val);
|
|
148
146
|
}
|
|
149
147
|
function readListField(message, reader, options, field, wireType) {
|
|
150
148
|
var _a;
|
|
@@ -153,17 +151,16 @@ function readListField(message, reader, options, field, wireType) {
|
|
|
153
151
|
return;
|
|
154
152
|
}
|
|
155
153
|
const scalarType = (_a = field.scalar) !== null && _a !== void 0 ? _a : ScalarType.INT32;
|
|
156
|
-
const longType = field.listKind == "scalar" ? field.longType : undefined;
|
|
157
154
|
const packed = wireType == WireType.LengthDelimited &&
|
|
158
155
|
scalarType != ScalarType.STRING &&
|
|
159
156
|
scalarType != ScalarType.BYTES;
|
|
160
157
|
if (!packed) {
|
|
161
|
-
message.addListItem(field, readScalar(reader, scalarType
|
|
158
|
+
message.addListItem(field, readScalar(reader, scalarType));
|
|
162
159
|
return;
|
|
163
160
|
}
|
|
164
161
|
const e = reader.uint32() + reader.pos;
|
|
165
162
|
while (reader.pos < e) {
|
|
166
|
-
message.addListItem(field, readScalar(reader, scalarType
|
|
163
|
+
message.addListItem(field, readScalar(reader, scalarType));
|
|
167
164
|
}
|
|
168
165
|
}
|
|
169
166
|
function readMessageField(reader, options, field, mergeMessage) {
|
|
@@ -172,14 +169,7 @@ function readMessageField(reader, options, field, mergeMessage) {
|
|
|
172
169
|
readMessage(message, reader, options, delimited, delimited ? field.number : reader.uint32());
|
|
173
170
|
return message;
|
|
174
171
|
}
|
|
175
|
-
function readScalar(reader, type
|
|
176
|
-
let v = readScalarValue(reader, type);
|
|
177
|
-
if (longType === LongType.STRING) {
|
|
178
|
-
v = typeof v == "bigint" ? v.toString() : v;
|
|
179
|
-
}
|
|
180
|
-
return v;
|
|
181
|
-
}
|
|
182
|
-
function readScalarValue(reader, type) {
|
|
172
|
+
function readScalar(reader, type) {
|
|
183
173
|
switch (type) {
|
|
184
174
|
case ScalarType.STRING:
|
|
185
175
|
return reader.string();
|
package/dist/esm/from-json.js
CHANGED
|
@@ -16,10 +16,10 @@ import { protoInt64 } from "./proto-int64.js";
|
|
|
16
16
|
import { create } from "./create.js";
|
|
17
17
|
import { reflect } from "./reflect/reflect.js";
|
|
18
18
|
import { formatVal } from "./reflect/reflect-check.js";
|
|
19
|
-
import {
|
|
19
|
+
import { LongType, ScalarType, scalarZeroValue, } from "./reflect/scalar.js";
|
|
20
20
|
import { base64Decode } from "./wire/base64-encoding.js";
|
|
21
21
|
import { getTextEncoding } from "./wire/text-encoding.js";
|
|
22
|
-
import { ListValueDesc, NullValue, StructDesc, ValueDesc,
|
|
22
|
+
import { anyPack, ListValueDesc, NullValue, StructDesc, ValueDesc, } from "./wkt/index.js";
|
|
23
23
|
import { isWrapperDesc } from "./wkt/wrappers.js";
|
|
24
24
|
import { createExtensionContainer, setExtension } from "./extensions.js";
|
|
25
25
|
// Default options for parsing JSON.
|
|
@@ -168,7 +168,7 @@ function readMapField(msg, field, json, opts) {
|
|
|
168
168
|
break;
|
|
169
169
|
case "scalar":
|
|
170
170
|
try {
|
|
171
|
-
msg.setMapEntry(field, key, readScalar(field.scalar, jsonMapValue,
|
|
171
|
+
msg.setMapEntry(field, key, readScalar(field.scalar, jsonMapValue, true));
|
|
172
172
|
}
|
|
173
173
|
catch (e) {
|
|
174
174
|
let m = `cannot decode map value for field ${msg.desc.typeName}.${field.name} from JSON: ${formatVal(jsonMapValue)}`;
|
|
@@ -206,7 +206,7 @@ function readListField(msg, field, json, opts) {
|
|
|
206
206
|
break;
|
|
207
207
|
case "scalar":
|
|
208
208
|
try {
|
|
209
|
-
msg.addListItem(field, readScalar(field.scalar, jsonItem,
|
|
209
|
+
msg.addListItem(field, readScalar(field.scalar, jsonItem, true));
|
|
210
210
|
}
|
|
211
211
|
catch (e) {
|
|
212
212
|
let m = `cannot decode field ${msg.desc.typeName}.${field.name} from JSON: ${formatVal(jsonItem)}`;
|
|
@@ -239,7 +239,7 @@ function readEnumField(msg, field, json, opts) {
|
|
|
239
239
|
}
|
|
240
240
|
function readScalarField(msg, field, json) {
|
|
241
241
|
try {
|
|
242
|
-
const scalarValue = readScalar(field.scalar, json,
|
|
242
|
+
const scalarValue = readScalar(field.scalar, json, false);
|
|
243
243
|
if (scalarValue === tokenNull) {
|
|
244
244
|
msg.clear(field);
|
|
245
245
|
}
|
|
@@ -267,7 +267,7 @@ function readMapKey(type, json) {
|
|
|
267
267
|
break;
|
|
268
268
|
}
|
|
269
269
|
}
|
|
270
|
-
return readScalar(type, json,
|
|
270
|
+
return readScalar(type, json, true);
|
|
271
271
|
}
|
|
272
272
|
const tokenIgnoredUnknownEnum = Symbol();
|
|
273
273
|
function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
|
|
@@ -297,10 +297,10 @@ function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
|
|
|
297
297
|
throw new Error(`cannot decode enum ${desc.typeName} from JSON: ${formatVal(json)}`);
|
|
298
298
|
}
|
|
299
299
|
const tokenNull = Symbol();
|
|
300
|
-
function readScalar(type, json,
|
|
300
|
+
function readScalar(type, json, nullAsZeroValue) {
|
|
301
301
|
if (json === null) {
|
|
302
302
|
if (nullAsZeroValue) {
|
|
303
|
-
return scalarZeroValue(type,
|
|
303
|
+
return scalarZeroValue(type, LongType.BIGINT);
|
|
304
304
|
}
|
|
305
305
|
return tokenNull;
|
|
306
306
|
}
|
|
@@ -366,16 +366,12 @@ function readScalar(type, json, longType, nullAsZeroValue) {
|
|
|
366
366
|
case ScalarType.SINT64:
|
|
367
367
|
if (typeof json != "number" && typeof json != "string")
|
|
368
368
|
break;
|
|
369
|
-
|
|
370
|
-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
371
|
-
return longType ? long.toString() : long;
|
|
369
|
+
return protoInt64.parse(json);
|
|
372
370
|
case ScalarType.FIXED64:
|
|
373
371
|
case ScalarType.UINT64:
|
|
374
372
|
if (typeof json != "number" && typeof json != "string")
|
|
375
373
|
break;
|
|
376
|
-
|
|
377
|
-
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
378
|
-
return longType ? uLong.toString() : uLong;
|
|
374
|
+
return protoInt64.uParse(json);
|
|
379
375
|
// bool:
|
|
380
376
|
case ScalarType.BOOL:
|
|
381
377
|
if (typeof json !== "boolean")
|
|
@@ -443,7 +439,7 @@ function tryWktFromJson(msg, jsonValue, opts) {
|
|
|
443
439
|
msg.clear(valueField);
|
|
444
440
|
}
|
|
445
441
|
else {
|
|
446
|
-
msg.set(valueField, readScalar(valueField.scalar, jsonValue,
|
|
442
|
+
msg.set(valueField, readScalar(valueField.scalar, jsonValue, true));
|
|
447
443
|
}
|
|
448
444
|
return true;
|
|
449
445
|
}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -12,5 +12,5 @@ export { fromBinary, mergeFromBinary } from "./from-binary.js";
|
|
|
12
12
|
export type { BinaryReadOptions } from "./from-binary.js";
|
|
13
13
|
export * from "./to-json.js";
|
|
14
14
|
export * from "./from-json.js";
|
|
15
|
-
export { hasExtension, getExtension, setExtension, clearExtension, } from "./extensions.js";
|
|
15
|
+
export { hasExtension, getExtension, setExtension, clearExtension, hasOption, getOption, } from "./extensions.js";
|
|
16
16
|
export * from "./proto-int64.js";
|
package/dist/esm/index.js
CHANGED
|
@@ -22,5 +22,5 @@ export { toBinary } from "./to-binary.js";
|
|
|
22
22
|
export { fromBinary, mergeFromBinary } from "./from-binary.js";
|
|
23
23
|
export * from "./to-json.js";
|
|
24
24
|
export * from "./from-json.js";
|
|
25
|
-
export { hasExtension, getExtension, setExtension, clearExtension, } from "./extensions.js";
|
|
25
|
+
export { hasExtension, getExtension, setExtension, clearExtension, hasOption, getOption, } from "./extensions.js";
|
|
26
26
|
export * from "./proto-int64.js";
|