@bufbuild/protobuf 1.6.0 → 1.7.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.
- package/dist/cjs/binary-encoding.js +2 -3
- package/dist/cjs/binary-format.d.ts +17 -0
- package/dist/cjs/binary-format.js +1 -1
- package/dist/cjs/codegen-info.d.ts +4 -4
- package/dist/cjs/codegen-info.js +2 -1
- package/dist/cjs/create-descriptor-set.d.ts +7 -1
- package/dist/cjs/create-descriptor-set.js +56 -50
- package/dist/cjs/create-registry-from-desc.d.ts +2 -2
- package/dist/cjs/create-registry-from-desc.js +53 -9
- package/dist/cjs/create-registry.d.ts +3 -2
- package/dist/cjs/create-registry.js +56 -37
- package/dist/cjs/descriptor-set.js +1 -1
- package/dist/cjs/enum.js +1 -1
- package/dist/cjs/extension-accessor.d.ts +35 -0
- package/dist/cjs/extension-accessor.js +108 -0
- package/dist/cjs/extension.d.ts +24 -0
- package/dist/cjs/extension.js +15 -0
- package/dist/cjs/field-list.js +1 -1
- package/dist/cjs/field.js +1 -1
- package/dist/cjs/google/protobuf/any_pb.js +1 -1
- package/dist/cjs/google/protobuf/api_pb.js +1 -1
- package/dist/cjs/google/protobuf/compiler/plugin_pb.js +1 -1
- package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
- package/dist/cjs/google/protobuf/duration_pb.js +1 -1
- package/dist/cjs/google/protobuf/empty_pb.js +1 -1
- package/dist/cjs/google/protobuf/field_mask_pb.js +1 -1
- package/dist/cjs/google/protobuf/source_context_pb.js +1 -1
- package/dist/cjs/google/protobuf/struct_pb.js +2 -2
- package/dist/cjs/google/protobuf/timestamp_pb.js +1 -1
- package/dist/cjs/google/protobuf/type_pb.js +2 -2
- package/dist/cjs/google/protobuf/wrappers_pb.js +1 -1
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +7 -2
- package/dist/cjs/json-format.d.ts +5 -5
- package/dist/cjs/json-format.js +1 -1
- package/dist/cjs/message-type.js +1 -1
- package/dist/cjs/message.js +1 -1
- package/dist/cjs/private/assert.js +1 -1
- package/dist/cjs/private/binary-format-common.d.ts +2 -3
- package/dist/cjs/private/binary-format-common.js +80 -78
- package/dist/cjs/private/binary-format-proto2.js +59 -64
- package/dist/cjs/private/binary-format-proto3.js +44 -40
- package/dist/cjs/private/enum.js +1 -1
- package/dist/cjs/private/extensions.d.ts +34 -0
- package/dist/cjs/private/extensions.js +86 -0
- package/dist/cjs/private/feature-set.d.ts +6 -8
- package/dist/cjs/private/feature-set.js +41 -42
- package/dist/cjs/private/field-list.js +1 -1
- package/dist/cjs/private/field-wrapper.d.ts +2 -2
- package/dist/cjs/private/field-wrapper.js +1 -1
- package/dist/cjs/private/field.js +1 -1
- package/dist/cjs/private/json-format-common.d.ts +3 -3
- package/dist/cjs/private/json-format-common.js +228 -173
- package/dist/cjs/private/json-format-proto2.js +3 -2
- package/dist/cjs/private/json-format-proto3.js +8 -6
- package/dist/cjs/private/message-type.js +1 -1
- package/dist/cjs/private/names.d.ts +2 -2
- package/dist/cjs/private/names.js +3 -2
- package/dist/cjs/private/options-map.js +1 -1
- package/dist/cjs/private/proto-runtime.d.ts +6 -0
- package/dist/cjs/private/proto-runtime.js +5 -1
- package/dist/cjs/private/reify-wkt.js +1 -1
- package/dist/cjs/private/scalars.js +1 -1
- package/dist/cjs/private/text-format.js +1 -1
- package/dist/cjs/private/util-common.js +1 -1
- package/dist/cjs/private/util.js +1 -1
- package/dist/cjs/proto-base64.js +1 -1
- package/dist/cjs/proto-delimited.js +1 -1
- package/dist/cjs/proto-double.js +1 -1
- package/dist/cjs/proto-int64.js +1 -1
- package/dist/cjs/proto2.js +1 -1
- package/dist/cjs/proto3.js +1 -1
- package/dist/cjs/service-type.js +1 -1
- package/dist/cjs/to-plain-message.js +1 -1
- package/dist/cjs/type-registry.d.ts +14 -0
- package/dist/cjs/type-registry.js +1 -1
- package/dist/esm/binary-encoding.js +2 -3
- package/dist/esm/binary-format.d.ts +17 -0
- package/dist/esm/binary-format.js +1 -1
- package/dist/esm/codegen-info.d.ts +4 -4
- package/dist/esm/codegen-info.js +2 -1
- package/dist/esm/create-descriptor-set.d.ts +7 -1
- package/dist/esm/create-descriptor-set.js +57 -51
- package/dist/esm/create-registry-from-desc.d.ts +2 -2
- package/dist/esm/create-registry-from-desc.js +53 -9
- package/dist/esm/create-registry.d.ts +3 -2
- package/dist/esm/create-registry.js +56 -37
- package/dist/esm/descriptor-set.js +1 -1
- package/dist/esm/enum.js +1 -1
- package/dist/esm/extension-accessor.d.ts +35 -0
- package/dist/esm/extension-accessor.js +101 -0
- package/dist/esm/extension.d.ts +24 -0
- package/dist/esm/extension.js +14 -0
- package/dist/esm/field-list.js +1 -1
- package/dist/esm/field.js +1 -1
- package/dist/esm/google/protobuf/any_pb.js +1 -1
- package/dist/esm/google/protobuf/api_pb.js +1 -1
- package/dist/esm/google/protobuf/compiler/plugin_pb.js +1 -1
- package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
- package/dist/esm/google/protobuf/duration_pb.js +1 -1
- package/dist/esm/google/protobuf/empty_pb.js +1 -1
- package/dist/esm/google/protobuf/field_mask_pb.js +1 -1
- package/dist/esm/google/protobuf/source_context_pb.js +1 -1
- package/dist/esm/google/protobuf/struct_pb.js +2 -2
- package/dist/esm/google/protobuf/timestamp_pb.js +1 -1
- package/dist/esm/google/protobuf/type_pb.js +2 -2
- package/dist/esm/google/protobuf/wrappers_pb.js +1 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/json-format.d.ts +5 -5
- package/dist/esm/json-format.js +1 -1
- package/dist/esm/message-type.js +1 -1
- package/dist/esm/message.js +1 -1
- package/dist/esm/private/assert.js +1 -1
- package/dist/esm/private/binary-format-common.d.ts +2 -3
- package/dist/esm/private/binary-format-common.js +80 -77
- package/dist/esm/private/binary-format-proto2.js +59 -64
- package/dist/esm/private/binary-format-proto3.js +44 -40
- package/dist/esm/private/enum.js +1 -1
- package/dist/esm/private/extensions.d.ts +34 -0
- package/dist/esm/private/extensions.js +81 -0
- package/dist/esm/private/feature-set.d.ts +6 -8
- package/dist/esm/private/feature-set.js +40 -41
- package/dist/esm/private/field-list.js +1 -1
- package/dist/esm/private/field-wrapper.d.ts +2 -2
- package/dist/esm/private/field-wrapper.js +1 -1
- package/dist/esm/private/field.js +1 -1
- package/dist/esm/private/json-format-common.d.ts +3 -3
- package/dist/esm/private/json-format-common.js +228 -173
- package/dist/esm/private/json-format-proto2.js +3 -2
- package/dist/esm/private/json-format-proto3.js +8 -6
- package/dist/esm/private/message-type.js +1 -1
- package/dist/esm/private/names.d.ts +2 -2
- package/dist/esm/private/names.js +3 -2
- package/dist/esm/private/options-map.js +1 -1
- package/dist/esm/private/proto-runtime.d.ts +6 -0
- package/dist/esm/private/proto-runtime.js +5 -1
- package/dist/esm/private/reify-wkt.js +1 -1
- package/dist/esm/private/scalars.js +1 -1
- package/dist/esm/private/text-format.js +1 -1
- package/dist/esm/private/util-common.js +1 -1
- package/dist/esm/private/util.js +1 -1
- package/dist/esm/proto-base64.js +1 -1
- package/dist/esm/proto-delimited.js +1 -1
- package/dist/esm/proto-double.js +1 -1
- package/dist/esm/proto-int64.js +1 -1
- package/dist/esm/proto2.js +1 -1
- package/dist/esm/proto3.js +1 -1
- package/dist/esm/service-type.js +1 -1
- package/dist/esm/to-plain-message.js +1 -1
- package/dist/esm/type-registry.d.ts +14 -0
- package/dist/esm/type-registry.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { assert } from "./private/assert.js";
|
|
15
|
+
import { createExtensionContainer, filterUnknownFields, } from "./private/extensions.js";
|
|
16
|
+
/**
|
|
17
|
+
* Retrieve an extension value from a message.
|
|
18
|
+
*
|
|
19
|
+
* The function never returns undefined. Use hasExtension() to check whether an
|
|
20
|
+
* extension is set. If the extension is not set, this function returns the
|
|
21
|
+
* default value (if one was specified in the protobuf source), or the zero value
|
|
22
|
+
* (for example `0` for numeric types, `[]` for repeated extension fields, and
|
|
23
|
+
* an empty message instance for message fields).
|
|
24
|
+
*
|
|
25
|
+
* Extensions are stored as unknown fields on a message. To mutate an extension
|
|
26
|
+
* value, make sure to store the new value with setExtension() after mutating.
|
|
27
|
+
*
|
|
28
|
+
* If the extension does not extend the given message, an error is raised.
|
|
29
|
+
*/
|
|
30
|
+
export function getExtension(message, extension, options) {
|
|
31
|
+
assertExtendee(extension, message);
|
|
32
|
+
const opt = extension.runtime.bin.makeReadOptions(options);
|
|
33
|
+
const ufs = filterUnknownFields(message.getType().runtime.bin.listUnknownFields(message), extension.field);
|
|
34
|
+
const [container, get] = createExtensionContainer(extension);
|
|
35
|
+
for (const uf of ufs) {
|
|
36
|
+
extension.runtime.bin.readField(container, opt.readerFactory(uf.data), extension.field, uf.wireType, opt);
|
|
37
|
+
}
|
|
38
|
+
return get();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Set an extension value on a message. If the message already has a value for
|
|
42
|
+
* this extension, the value is replaced.
|
|
43
|
+
*
|
|
44
|
+
* If the extension does not extend the given message, an error is raised.
|
|
45
|
+
*/
|
|
46
|
+
export function setExtension(message, extension, value, options) {
|
|
47
|
+
assertExtendee(extension, message);
|
|
48
|
+
const readOpt = extension.runtime.bin.makeReadOptions(options);
|
|
49
|
+
const writeOpt = extension.runtime.bin.makeWriteOptions(options);
|
|
50
|
+
if (hasExtension(message, extension)) {
|
|
51
|
+
const ufs = message
|
|
52
|
+
.getType()
|
|
53
|
+
.runtime.bin.listUnknownFields(message)
|
|
54
|
+
.filter((uf) => uf.no != extension.field.no);
|
|
55
|
+
message.getType().runtime.bin.discardUnknownFields(message);
|
|
56
|
+
for (const uf of ufs) {
|
|
57
|
+
message
|
|
58
|
+
.getType()
|
|
59
|
+
.runtime.bin.onUnknownField(message, uf.no, uf.wireType, uf.data);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const writer = writeOpt.writerFactory();
|
|
63
|
+
extension.runtime.bin.writeField(extension.field, value, writer, writeOpt);
|
|
64
|
+
const reader = readOpt.readerFactory(writer.finish());
|
|
65
|
+
while (reader.pos < reader.len) {
|
|
66
|
+
const [no, wireType] = reader.tag();
|
|
67
|
+
const data = reader.skip(wireType);
|
|
68
|
+
message.getType().runtime.bin.onUnknownField(message, no, wireType, data);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Remove an extension value from a message.
|
|
73
|
+
*
|
|
74
|
+
* If the extension does not extend the given message, an error is raised.
|
|
75
|
+
*/
|
|
76
|
+
export function clearExtension(message, extension) {
|
|
77
|
+
assertExtendee(extension, message);
|
|
78
|
+
if (hasExtension(message, extension)) {
|
|
79
|
+
const bin = message.getType().runtime.bin;
|
|
80
|
+
const ufs = bin
|
|
81
|
+
.listUnknownFields(message)
|
|
82
|
+
.filter((uf) => uf.no != extension.field.no);
|
|
83
|
+
bin.discardUnknownFields(message);
|
|
84
|
+
for (const uf of ufs) {
|
|
85
|
+
bin.onUnknownField(message, uf.no, uf.wireType, uf.data);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Check whether an extension is set on a message.
|
|
91
|
+
*/
|
|
92
|
+
export function hasExtension(message, extension) {
|
|
93
|
+
const messageType = message.getType();
|
|
94
|
+
return (extension.extendee.typeName === messageType.typeName &&
|
|
95
|
+
!!messageType.runtime.bin
|
|
96
|
+
.listUnknownFields(message)
|
|
97
|
+
.find((uf) => uf.no == extension.field.no));
|
|
98
|
+
}
|
|
99
|
+
function assertExtendee(extension, message) {
|
|
100
|
+
assert(extension.extendee.typeName == message.getType().typeName, `extension ${extension.typeName} can only be applied to message ${extension.extendee.typeName}`);
|
|
101
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { FieldInfo } from "./field.js";
|
|
2
|
+
import type { AnyMessage, Message } from "./message.js";
|
|
3
|
+
import type { MessageType } from "./message-type.js";
|
|
4
|
+
import type { ProtoRuntime } from "./private/proto-runtime.js";
|
|
5
|
+
export interface Extension<E extends Message<E> = AnyMessage, V = unknown> {
|
|
6
|
+
/**
|
|
7
|
+
* The fully qualified name of the extension.
|
|
8
|
+
*/
|
|
9
|
+
readonly typeName: string;
|
|
10
|
+
/**
|
|
11
|
+
* The message extended by this extension.
|
|
12
|
+
*/
|
|
13
|
+
readonly extendee: MessageType<E>;
|
|
14
|
+
/**
|
|
15
|
+
* Field information for this extension. Note that required fields, maps,
|
|
16
|
+
* oneof are not allowed in extensions. Behavior of "localName" property is
|
|
17
|
+
* undefined and must not be relied upon.
|
|
18
|
+
*/
|
|
19
|
+
readonly field: FieldInfo;
|
|
20
|
+
/**
|
|
21
|
+
* Provides serialization and other functionality.
|
|
22
|
+
*/
|
|
23
|
+
readonly runtime: ProtoRuntime;
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export {};
|
package/dist/esm/field-list.js
CHANGED
package/dist/esm/field.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// The messages in this file describe the definitions found in .proto files.
|
|
19
19
|
// A valid .proto file can be translated directly to a FileDescriptorProto
|
|
20
20
|
// without any other information (e.g. without reading its imports).
|
|
21
|
-
// @generated by protoc-gen-es v1.
|
|
21
|
+
// @generated by protoc-gen-es v1.7.1 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
22
22
|
// @generated from file google/protobuf/descriptor.proto (package google.protobuf, syntax proto2)
|
|
23
23
|
/* eslint-disable */
|
|
24
24
|
import { proto2 } from "../../proto2.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -11,7 +11,7 @@
|
|
|
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
|
-
// @generated by protoc-gen-es v1.
|
|
14
|
+
// @generated by protoc-gen-es v1.7.1 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
15
15
|
// @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
|
|
16
16
|
/* eslint-disable */
|
|
17
17
|
import { proto3 } from "../../proto3.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -11,7 +11,7 @@
|
|
|
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
|
-
// @generated by protoc-gen-es v1.
|
|
14
|
+
// @generated by protoc-gen-es v1.7.1 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
|
|
15
15
|
// @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
|
|
16
16
|
/* eslint-disable */
|
|
17
17
|
import { proto3 } from "../../proto3.js";
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type { FieldList } from "./field-list.js";
|
|
|
12
12
|
export { ScalarType, LongType } from "./field.js";
|
|
13
13
|
export type { MessageType } from "./message-type.js";
|
|
14
14
|
export type { EnumType, EnumValueInfo } from "./enum.js";
|
|
15
|
+
export type { Extension } from "./extension.js";
|
|
16
|
+
export { getExtension, setExtension, hasExtension, clearExtension, } from "./extension-accessor.js";
|
|
15
17
|
export type { ServiceType, MethodInfo, MethodInfoUnary, MethodInfoServerStreaming, MethodInfoClientStreaming, MethodInfoBiDiStreaming, } from "./service-type.js";
|
|
16
18
|
export { MethodKind, MethodIdempotency } from "./service-type.js";
|
|
17
19
|
export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
|
|
@@ -20,7 +22,7 @@ export type { BinaryFormat, BinaryWriteOptions, BinaryReadOptions, } from "./bin
|
|
|
20
22
|
export type { JsonFormat, JsonObject, JsonValue, JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions, } from "./json-format.js";
|
|
21
23
|
export type { DescriptorSet, AnyDesc, DescFile, DescEnum, DescEnumValue, DescMessage, DescOneof, DescField, DescService, DescMethod, DescExtension, DescComments, } from "./descriptor-set.js";
|
|
22
24
|
export { createDescriptorSet } from "./create-descriptor-set.js";
|
|
23
|
-
export type { IMessageTypeRegistry } from "./type-registry.js";
|
|
25
|
+
export type { IMessageTypeRegistry, IExtensionRegistry, } from "./type-registry.js";
|
|
24
26
|
export { createRegistry } from "./create-registry.js";
|
|
25
27
|
export { createRegistryFromDescriptors } from "./create-registry-from-desc.js";
|
|
26
28
|
export { toPlainMessage } from "./to-plain-message.js";
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -20,6 +20,7 @@ export { protoDelimited } from "./proto-delimited.js";
|
|
|
20
20
|
export { codegenInfo } from "./codegen-info.js";
|
|
21
21
|
export { Message } from "./message.js";
|
|
22
22
|
export { ScalarType, LongType } from "./field.js";
|
|
23
|
+
export { getExtension, setExtension, hasExtension, clearExtension, } from "./extension-accessor.js";
|
|
23
24
|
export { MethodKind, MethodIdempotency } from "./service-type.js";
|
|
24
25
|
export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
|
|
25
26
|
export { createDescriptorSet } from "./create-descriptor-set.js";
|
|
@@ -2,7 +2,7 @@ import type { Message } from "./message.js";
|
|
|
2
2
|
import type { MessageType } from "./message-type.js";
|
|
3
3
|
import type { ScalarType } from "./field.js";
|
|
4
4
|
import { LongType } from "./field.js";
|
|
5
|
-
import type { IMessageTypeRegistry } from "./type-registry.js";
|
|
5
|
+
import type { IExtensionRegistry, IMessageTypeRegistry } from "./type-registry.js";
|
|
6
6
|
/**
|
|
7
7
|
* JsonFormat is the contract for serializing messages to and from JSON.
|
|
8
8
|
* Implementations may be specific to a proto syntax, and can be reflection
|
|
@@ -52,10 +52,10 @@ export interface JsonReadOptions {
|
|
|
52
52
|
*/
|
|
53
53
|
ignoreUnknownFields: boolean;
|
|
54
54
|
/**
|
|
55
|
-
* This option is required to read `google.protobuf.Any`
|
|
55
|
+
* This option is required to read `google.protobuf.Any` and extensions
|
|
56
56
|
* from JSON format.
|
|
57
57
|
*/
|
|
58
|
-
typeRegistry?: IMessageTypeRegistry
|
|
58
|
+
typeRegistry?: IMessageTypeRegistry & Partial<IExtensionRegistry>;
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
61
|
* Options for serializing to JSON.
|
|
@@ -83,10 +83,10 @@ export interface JsonWriteOptions {
|
|
|
83
83
|
*/
|
|
84
84
|
useProtoFieldName: boolean;
|
|
85
85
|
/**
|
|
86
|
-
* This option is required to write `google.protobuf.Any`
|
|
86
|
+
* This option is required to write `google.protobuf.Any` and extensions
|
|
87
87
|
* to JSON format.
|
|
88
88
|
*/
|
|
89
|
-
typeRegistry?: IMessageTypeRegistry
|
|
89
|
+
typeRegistry?: IMessageTypeRegistry & Partial<IExtensionRegistry>;
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
92
|
* Options for serializing to JSON.
|
package/dist/esm/json-format.js
CHANGED
package/dist/esm/message-type.js
CHANGED
package/dist/esm/message.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IBinaryWriter } from "../binary-encoding.js";
|
|
2
2
|
import type { BinaryFormat, BinaryWriteOptions } from "../binary-format.js";
|
|
3
3
|
import type { FieldInfo } from "../field.js";
|
|
4
4
|
import { ScalarType } from "../field.js";
|
|
5
|
-
export declare function makeBinaryFormatCommon(): Omit<BinaryFormat, "writeMessage">;
|
|
6
|
-
export declare function readScalarLTString(reader: IBinaryReader, type: ScalarType): any;
|
|
5
|
+
export declare function makeBinaryFormatCommon(): Omit<BinaryFormat, "writeMessage" | "writeField">;
|
|
7
6
|
export declare function writeMapEntry(writer: IBinaryWriter, options: BinaryWriteOptions, field: FieldInfo & {
|
|
8
7
|
kind: "map";
|
|
9
8
|
}, key: any, value: any): void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright 2021-
|
|
1
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
2
2
|
//
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
4
|
// you may not use this file except in compliance with the License.
|
|
@@ -82,76 +82,80 @@ export function makeBinaryFormatCommon() {
|
|
|
82
82
|
}
|
|
83
83
|
continue;
|
|
84
84
|
}
|
|
85
|
-
|
|
86
|
-
if (field.oneof) {
|
|
87
|
-
target = target[field.oneof.localName];
|
|
88
|
-
if (target.case != localName) {
|
|
89
|
-
delete target.value;
|
|
90
|
-
}
|
|
91
|
-
target.case = localName;
|
|
92
|
-
localName = "value";
|
|
93
|
-
}
|
|
94
|
-
switch (field.kind) {
|
|
95
|
-
case "scalar":
|
|
96
|
-
case "enum":
|
|
97
|
-
const scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
|
|
98
|
-
let read = readScalar;
|
|
99
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
|
|
100
|
-
if (field.kind == "scalar" && field.L > 0) {
|
|
101
|
-
read = readScalarLTString;
|
|
102
|
-
}
|
|
103
|
-
if (repeated) {
|
|
104
|
-
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
|
|
105
|
-
if (wireType == WireType.LengthDelimited &&
|
|
106
|
-
scalarType != ScalarType.STRING &&
|
|
107
|
-
scalarType != ScalarType.BYTES) {
|
|
108
|
-
let e = reader.uint32() + reader.pos;
|
|
109
|
-
while (reader.pos < e) {
|
|
110
|
-
arr.push(read(reader, scalarType));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
arr.push(read(reader, scalarType));
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
target[localName] = read(reader, scalarType);
|
|
119
|
-
}
|
|
120
|
-
break;
|
|
121
|
-
case "message":
|
|
122
|
-
const messageType = field.T;
|
|
123
|
-
if (repeated) {
|
|
124
|
-
// safe to assume presence of array, oneof cannot contain repeated values
|
|
125
|
-
target[localName].push(readMessageField(reader, new messageType(), options, field));
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
if (target[localName] instanceof Message) {
|
|
129
|
-
readMessageField(reader, target[localName], options, field);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
target[localName] = readMessageField(reader, new messageType(), options, field);
|
|
133
|
-
if (messageType.fieldWrapper &&
|
|
134
|
-
!field.oneof &&
|
|
135
|
-
!field.repeated) {
|
|
136
|
-
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
case "map":
|
|
142
|
-
let [mapKey, mapVal] = readMapEntry(field, reader, options);
|
|
143
|
-
// safe to assume presence of map object, oneof cannot contain repeated values
|
|
144
|
-
target[localName][mapKey] = mapVal;
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
85
|
+
readField(message, reader, field, wireType, options);
|
|
147
86
|
}
|
|
148
87
|
if (delimitedMessageEncoding && // eslint-disable-line @typescript-eslint/strict-boolean-expressions
|
|
149
88
|
(wireType != WireType.EndGroup || fieldNo !== lengthOrEndTagFieldNo)) {
|
|
150
89
|
throw new Error(`invalid end group tag`);
|
|
151
90
|
}
|
|
152
91
|
},
|
|
92
|
+
readField,
|
|
153
93
|
};
|
|
154
94
|
}
|
|
95
|
+
function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
|
|
96
|
+
reader, field, wireType, options) {
|
|
97
|
+
let { repeated, localName } = field;
|
|
98
|
+
if (field.oneof) {
|
|
99
|
+
target = target[field.oneof.localName];
|
|
100
|
+
if (target.case != localName) {
|
|
101
|
+
delete target.value;
|
|
102
|
+
}
|
|
103
|
+
target.case = localName;
|
|
104
|
+
localName = "value";
|
|
105
|
+
}
|
|
106
|
+
switch (field.kind) {
|
|
107
|
+
case "scalar":
|
|
108
|
+
case "enum":
|
|
109
|
+
const scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
|
|
110
|
+
let read = readScalar;
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
|
|
112
|
+
if (field.kind == "scalar" && field.L > 0) {
|
|
113
|
+
read = readScalarLTString;
|
|
114
|
+
}
|
|
115
|
+
if (repeated) {
|
|
116
|
+
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
|
|
117
|
+
const isPacked = wireType == WireType.LengthDelimited &&
|
|
118
|
+
scalarType != ScalarType.STRING &&
|
|
119
|
+
scalarType != ScalarType.BYTES;
|
|
120
|
+
if (isPacked) {
|
|
121
|
+
let e = reader.uint32() + reader.pos;
|
|
122
|
+
while (reader.pos < e) {
|
|
123
|
+
arr.push(read(reader, scalarType));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
arr.push(read(reader, scalarType));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
target[localName] = read(reader, scalarType);
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
case "message":
|
|
135
|
+
const messageType = field.T;
|
|
136
|
+
if (repeated) {
|
|
137
|
+
// safe to assume presence of array, oneof cannot contain repeated values
|
|
138
|
+
target[localName].push(readMessageField(reader, new messageType(), options, field));
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
if (target[localName] instanceof Message) {
|
|
142
|
+
readMessageField(reader, target[localName], options, field);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
target[localName] = readMessageField(reader, new messageType(), options, field);
|
|
146
|
+
if (messageType.fieldWrapper && !field.oneof && !field.repeated) {
|
|
147
|
+
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
case "map":
|
|
153
|
+
let [mapKey, mapVal] = readMapEntry(field, reader, options);
|
|
154
|
+
// safe to assume presence of map object, oneof cannot contain repeated values
|
|
155
|
+
target[localName][mapKey] = mapVal;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
155
159
|
// Read a message, avoiding MessageType.fromBinary() to re-use the
|
|
156
160
|
// BinaryReadOptions and the IBinaryReader.
|
|
157
161
|
function readMessageField(reader, message, options, field) {
|
|
@@ -213,7 +217,7 @@ function readMapEntry(field, reader, options) {
|
|
|
213
217
|
}
|
|
214
218
|
// Read a scalar value, but return 64 bit integral types (int64, uint64,
|
|
215
219
|
// sint64, fixed64, sfixed64) as string instead of bigint.
|
|
216
|
-
|
|
220
|
+
function readScalarLTString(reader, type) {
|
|
217
221
|
const v = readScalar(reader, type);
|
|
218
222
|
return typeof v == "bigint" ? v.toString() : v;
|
|
219
223
|
}
|
|
@@ -288,20 +292,19 @@ export function writeMapEntry(writer, options, field, key, value) {
|
|
|
288
292
|
}
|
|
289
293
|
writer.join();
|
|
290
294
|
}
|
|
295
|
+
// Value must not be undefined
|
|
291
296
|
export function writeMessageField(writer, options, field, value) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
.bytes(message.toBinary(options));
|
|
304
|
-
}
|
|
297
|
+
const message = wrapField(field.T, value);
|
|
298
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
299
|
+
if (field === null || field === void 0 ? void 0 : field.delimited)
|
|
300
|
+
writer
|
|
301
|
+
.tag(field.no, WireType.StartGroup)
|
|
302
|
+
.raw(message.toBinary(options))
|
|
303
|
+
.tag(field.no, WireType.EndGroup);
|
|
304
|
+
else
|
|
305
|
+
writer
|
|
306
|
+
.tag(field.no, WireType.LengthDelimited)
|
|
307
|
+
.bytes(message.toBinary(options));
|
|
305
308
|
}
|
|
306
309
|
export function writeScalar(writer, type, fieldNo, value, emitIntrinsicDefault) {
|
|
307
310
|
let [wireType, method, isIntrinsicDefault] = scalarTypeInfo(type, value);
|