@bufbuild/protobuf 1.6.0 → 1.7.0
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 +1 -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 +3 -3
- package/dist/cjs/private/feature-set.js +27 -32
- 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.js +202 -156
- package/dist/cjs/private/json-format-proto2.js +2 -1
- package/dist/cjs/private/json-format-proto3.js +7 -5
- 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 +1 -1
- package/dist/esm/create-descriptor-set.js +56 -50
- 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 +3 -3
- package/dist/esm/private/feature-set.js +27 -32
- 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.js +202 -156
- package/dist/esm/private/json-format-proto2.js +2 -1
- package/dist/esm/private/json-format-proto3.js +7 -5
- 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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Copyright 2021-
|
|
2
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
3
3
|
//
|
|
4
4
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
// you may not use this file except in compliance with the License.
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.writePacked = exports.writeScalar = exports.writeMessageField = exports.writeMapEntry = exports.
|
|
16
|
+
exports.writePacked = exports.writeScalar = exports.writeMessageField = exports.writeMapEntry = exports.makeBinaryFormatCommon = void 0;
|
|
17
17
|
const binary_encoding_js_1 = require("../binary-encoding.js");
|
|
18
18
|
const message_js_1 = require("../message.js");
|
|
19
19
|
const field_js_1 = require("../field.js");
|
|
@@ -85,77 +85,81 @@ function makeBinaryFormatCommon() {
|
|
|
85
85
|
}
|
|
86
86
|
continue;
|
|
87
87
|
}
|
|
88
|
-
|
|
89
|
-
if (field.oneof) {
|
|
90
|
-
target = target[field.oneof.localName];
|
|
91
|
-
if (target.case != localName) {
|
|
92
|
-
delete target.value;
|
|
93
|
-
}
|
|
94
|
-
target.case = localName;
|
|
95
|
-
localName = "value";
|
|
96
|
-
}
|
|
97
|
-
switch (field.kind) {
|
|
98
|
-
case "scalar":
|
|
99
|
-
case "enum":
|
|
100
|
-
const scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
101
|
-
let read = readScalar;
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
|
|
103
|
-
if (field.kind == "scalar" && field.L > 0) {
|
|
104
|
-
read = readScalarLTString;
|
|
105
|
-
}
|
|
106
|
-
if (repeated) {
|
|
107
|
-
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
|
|
108
|
-
if (wireType == binary_encoding_js_1.WireType.LengthDelimited &&
|
|
109
|
-
scalarType != field_js_1.ScalarType.STRING &&
|
|
110
|
-
scalarType != field_js_1.ScalarType.BYTES) {
|
|
111
|
-
let e = reader.uint32() + reader.pos;
|
|
112
|
-
while (reader.pos < e) {
|
|
113
|
-
arr.push(read(reader, scalarType));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
arr.push(read(reader, scalarType));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
target[localName] = read(reader, scalarType);
|
|
122
|
-
}
|
|
123
|
-
break;
|
|
124
|
-
case "message":
|
|
125
|
-
const messageType = field.T;
|
|
126
|
-
if (repeated) {
|
|
127
|
-
// safe to assume presence of array, oneof cannot contain repeated values
|
|
128
|
-
target[localName].push(readMessageField(reader, new messageType(), options, field));
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
if (target[localName] instanceof message_js_1.Message) {
|
|
132
|
-
readMessageField(reader, target[localName], options, field);
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
target[localName] = readMessageField(reader, new messageType(), options, field);
|
|
136
|
-
if (messageType.fieldWrapper &&
|
|
137
|
-
!field.oneof &&
|
|
138
|
-
!field.repeated) {
|
|
139
|
-
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
break;
|
|
144
|
-
case "map":
|
|
145
|
-
let [mapKey, mapVal] = readMapEntry(field, reader, options);
|
|
146
|
-
// safe to assume presence of map object, oneof cannot contain repeated values
|
|
147
|
-
target[localName][mapKey] = mapVal;
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
88
|
+
readField(message, reader, field, wireType, options);
|
|
150
89
|
}
|
|
151
90
|
if (delimitedMessageEncoding && // eslint-disable-line @typescript-eslint/strict-boolean-expressions
|
|
152
91
|
(wireType != binary_encoding_js_1.WireType.EndGroup || fieldNo !== lengthOrEndTagFieldNo)) {
|
|
153
92
|
throw new Error(`invalid end group tag`);
|
|
154
93
|
}
|
|
155
94
|
},
|
|
95
|
+
readField,
|
|
156
96
|
};
|
|
157
97
|
}
|
|
158
98
|
exports.makeBinaryFormatCommon = makeBinaryFormatCommon;
|
|
99
|
+
function readField(target, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
|
|
100
|
+
reader, field, wireType, options) {
|
|
101
|
+
let { repeated, localName } = field;
|
|
102
|
+
if (field.oneof) {
|
|
103
|
+
target = target[field.oneof.localName];
|
|
104
|
+
if (target.case != localName) {
|
|
105
|
+
delete target.value;
|
|
106
|
+
}
|
|
107
|
+
target.case = localName;
|
|
108
|
+
localName = "value";
|
|
109
|
+
}
|
|
110
|
+
switch (field.kind) {
|
|
111
|
+
case "scalar":
|
|
112
|
+
case "enum":
|
|
113
|
+
const scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
114
|
+
let read = readScalar;
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
|
|
116
|
+
if (field.kind == "scalar" && field.L > 0) {
|
|
117
|
+
read = readScalarLTString;
|
|
118
|
+
}
|
|
119
|
+
if (repeated) {
|
|
120
|
+
let arr = target[localName]; // safe to assume presence of array, oneof cannot contain repeated values
|
|
121
|
+
const isPacked = wireType == binary_encoding_js_1.WireType.LengthDelimited &&
|
|
122
|
+
scalarType != field_js_1.ScalarType.STRING &&
|
|
123
|
+
scalarType != field_js_1.ScalarType.BYTES;
|
|
124
|
+
if (isPacked) {
|
|
125
|
+
let e = reader.uint32() + reader.pos;
|
|
126
|
+
while (reader.pos < e) {
|
|
127
|
+
arr.push(read(reader, scalarType));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
arr.push(read(reader, scalarType));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
target[localName] = read(reader, scalarType);
|
|
136
|
+
}
|
|
137
|
+
break;
|
|
138
|
+
case "message":
|
|
139
|
+
const messageType = field.T;
|
|
140
|
+
if (repeated) {
|
|
141
|
+
// safe to assume presence of array, oneof cannot contain repeated values
|
|
142
|
+
target[localName].push(readMessageField(reader, new messageType(), options, field));
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (target[localName] instanceof message_js_1.Message) {
|
|
146
|
+
readMessageField(reader, target[localName], options, field);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
target[localName] = readMessageField(reader, new messageType(), options, field);
|
|
150
|
+
if (messageType.fieldWrapper && !field.oneof && !field.repeated) {
|
|
151
|
+
target[localName] = messageType.fieldWrapper.unwrapField(target[localName]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
case "map":
|
|
157
|
+
let [mapKey, mapVal] = readMapEntry(field, reader, options);
|
|
158
|
+
// safe to assume presence of map object, oneof cannot contain repeated values
|
|
159
|
+
target[localName][mapKey] = mapVal;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
159
163
|
// Read a message, avoiding MessageType.fromBinary() to re-use the
|
|
160
164
|
// BinaryReadOptions and the IBinaryReader.
|
|
161
165
|
function readMessageField(reader, message, options, field) {
|
|
@@ -221,7 +225,6 @@ function readScalarLTString(reader, type) {
|
|
|
221
225
|
const v = readScalar(reader, type);
|
|
222
226
|
return typeof v == "bigint" ? v.toString() : v;
|
|
223
227
|
}
|
|
224
|
-
exports.readScalarLTString = readScalarLTString;
|
|
225
228
|
// Does not use scalarTypeInfo() for better performance.
|
|
226
229
|
function readScalar(reader, type) {
|
|
227
230
|
switch (type) {
|
|
@@ -294,20 +297,19 @@ function writeMapEntry(writer, options, field, key, value) {
|
|
|
294
297
|
writer.join();
|
|
295
298
|
}
|
|
296
299
|
exports.writeMapEntry = writeMapEntry;
|
|
300
|
+
// Value must not be undefined
|
|
297
301
|
function writeMessageField(writer, options, field, value) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
.bytes(message.toBinary(options));
|
|
310
|
-
}
|
|
302
|
+
const message = (0, field_wrapper_js_1.wrapField)(field.T, value);
|
|
303
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
304
|
+
if (field === null || field === void 0 ? void 0 : field.delimited)
|
|
305
|
+
writer
|
|
306
|
+
.tag(field.no, binary_encoding_js_1.WireType.StartGroup)
|
|
307
|
+
.raw(message.toBinary(options))
|
|
308
|
+
.tag(field.no, binary_encoding_js_1.WireType.EndGroup);
|
|
309
|
+
else
|
|
310
|
+
writer
|
|
311
|
+
.tag(field.no, binary_encoding_js_1.WireType.LengthDelimited)
|
|
312
|
+
.bytes(message.toBinary(options));
|
|
311
313
|
}
|
|
312
314
|
exports.writeMessageField = writeMessageField;
|
|
313
315
|
function writeScalar(writer, type, fieldNo, value, emitIntrinsicDefault) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Copyright 2021-
|
|
2
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
3
3
|
//
|
|
4
4
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
// you may not use this file except in compliance with the License.
|
|
@@ -18,74 +18,28 @@ const field_js_1 = require("../field.js");
|
|
|
18
18
|
const binary_format_common_js_1 = require("./binary-format-common.js");
|
|
19
19
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, no-case-declarations, prefer-const */
|
|
20
20
|
function makeBinaryFormatProto2() {
|
|
21
|
-
return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), {
|
|
21
|
+
return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeField,
|
|
22
|
+
writeMessage(message, writer, options) {
|
|
22
23
|
const type = message.getType();
|
|
23
24
|
let field;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
if (oneof.case !== localName) {
|
|
31
|
-
continue; // field is not selected, skip
|
|
32
|
-
}
|
|
33
|
-
value = oneof.value;
|
|
25
|
+
for (field of type.fields.byNumber()) {
|
|
26
|
+
let value, localName = field.localName;
|
|
27
|
+
if (field.oneof) {
|
|
28
|
+
const oneof = message[field.oneof.localName];
|
|
29
|
+
if (oneof.case !== localName) {
|
|
30
|
+
continue; // field is not selected, skip
|
|
34
31
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
switch (field.kind) {
|
|
44
|
-
case "scalar":
|
|
45
|
-
case "enum":
|
|
46
|
-
let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
47
|
-
if (repeated) {
|
|
48
|
-
if (field.packed) {
|
|
49
|
-
(0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
for (const item of value) {
|
|
53
|
-
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
if (value !== undefined) {
|
|
59
|
-
// In contrast to proto3, we do not skip intrinsic default values.
|
|
60
|
-
// Explicit default values are not special cased either.
|
|
61
|
-
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, true);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
break;
|
|
65
|
-
case "message":
|
|
66
|
-
if (repeated) {
|
|
67
|
-
for (const item of value) {
|
|
68
|
-
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, item);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, value);
|
|
73
|
-
}
|
|
74
|
-
break;
|
|
75
|
-
case "map":
|
|
76
|
-
for (const [key, val] of Object.entries(value)) {
|
|
77
|
-
(0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
|
|
78
|
-
}
|
|
79
|
-
break;
|
|
32
|
+
value = oneof.value;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
value = message[localName];
|
|
36
|
+
// In contrast to proto3, we raise an error if a non-optional (proto2 required)
|
|
37
|
+
// field is missing a value.
|
|
38
|
+
if (value === undefined && !field.oneof && !field.opt) {
|
|
39
|
+
throw new Error(`cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary: required field not set`);
|
|
80
40
|
}
|
|
81
41
|
}
|
|
82
|
-
|
|
83
|
-
catch (e) {
|
|
84
|
-
let m = field
|
|
85
|
-
? `cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary`
|
|
86
|
-
: `cannot encode message ${type.typeName} to binary`;
|
|
87
|
-
let r = e instanceof Error ? e.message : String(e);
|
|
88
|
-
throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
|
|
42
|
+
writeField(field, value, writer, options);
|
|
89
43
|
}
|
|
90
44
|
if (options.writeUnknownFields) {
|
|
91
45
|
this.writeUnknownFields(message, writer);
|
|
@@ -94,3 +48,44 @@ function makeBinaryFormatProto2() {
|
|
|
94
48
|
} });
|
|
95
49
|
}
|
|
96
50
|
exports.makeBinaryFormatProto2 = makeBinaryFormatProto2;
|
|
51
|
+
// TODO field presence: merge this function with proto3
|
|
52
|
+
function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
|
|
53
|
+
writer, options) {
|
|
54
|
+
const repeated = field.repeated;
|
|
55
|
+
switch (field.kind) {
|
|
56
|
+
case "scalar":
|
|
57
|
+
case "enum":
|
|
58
|
+
let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
59
|
+
if (repeated) {
|
|
60
|
+
if (field.packed) {
|
|
61
|
+
(0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
for (const item of value) {
|
|
65
|
+
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else if (value !== undefined) {
|
|
70
|
+
// In contrast to proto3, we do not skip intrinsic default values.
|
|
71
|
+
// Explicit default values are not special cased either.
|
|
72
|
+
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, true);
|
|
73
|
+
}
|
|
74
|
+
break;
|
|
75
|
+
case "message":
|
|
76
|
+
if (repeated) {
|
|
77
|
+
for (const item of value) {
|
|
78
|
+
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, item);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (value !== undefined) {
|
|
82
|
+
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, value);
|
|
83
|
+
}
|
|
84
|
+
break;
|
|
85
|
+
case "map":
|
|
86
|
+
for (const [key, val] of Object.entries(value)) {
|
|
87
|
+
(0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Copyright 2021-
|
|
2
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
3
3
|
//
|
|
4
4
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
// you may not use this file except in compliance with the License.
|
|
@@ -18,11 +18,11 @@ const field_js_1 = require("../field.js");
|
|
|
18
18
|
const binary_format_common_js_1 = require("./binary-format-common.js");
|
|
19
19
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, prefer-const, no-case-declarations */
|
|
20
20
|
function makeBinaryFormatProto3() {
|
|
21
|
-
return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), {
|
|
21
|
+
return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeField,
|
|
22
|
+
writeMessage(message, writer, options) {
|
|
22
23
|
const type = message.getType();
|
|
23
24
|
for (const field of type.fields.byNumber()) {
|
|
24
|
-
let value,
|
|
25
|
-
repeated = field.repeated, localName = field.localName;
|
|
25
|
+
let value, localName = field.localName;
|
|
26
26
|
if (field.oneof) {
|
|
27
27
|
const oneof = message[field.oneof.localName];
|
|
28
28
|
if (oneof.case !== localName) {
|
|
@@ -33,42 +33,7 @@ function makeBinaryFormatProto3() {
|
|
|
33
33
|
else {
|
|
34
34
|
value = message[localName];
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
case "scalar":
|
|
38
|
-
case "enum":
|
|
39
|
-
let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
40
|
-
if (repeated) {
|
|
41
|
-
if (field.packed) {
|
|
42
|
-
(0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
for (const item of value) {
|
|
46
|
-
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
if (value !== undefined) {
|
|
52
|
-
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, !!field.oneof || field.opt);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
break;
|
|
56
|
-
case "message":
|
|
57
|
-
if (repeated) {
|
|
58
|
-
for (const item of value) {
|
|
59
|
-
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, item);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, value);
|
|
64
|
-
}
|
|
65
|
-
break;
|
|
66
|
-
case "map":
|
|
67
|
-
for (const [key, val] of Object.entries(value)) {
|
|
68
|
-
(0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
|
|
69
|
-
}
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
36
|
+
writeField(field, value, writer, options);
|
|
72
37
|
}
|
|
73
38
|
if (options.writeUnknownFields) {
|
|
74
39
|
this.writeUnknownFields(message, writer);
|
|
@@ -77,3 +42,42 @@ function makeBinaryFormatProto3() {
|
|
|
77
42
|
} });
|
|
78
43
|
}
|
|
79
44
|
exports.makeBinaryFormatProto3 = makeBinaryFormatProto3;
|
|
45
|
+
// TODO field presence: merge this function with proto2
|
|
46
|
+
function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
|
|
47
|
+
writer, options) {
|
|
48
|
+
const repeated = field.repeated;
|
|
49
|
+
switch (field.kind) {
|
|
50
|
+
case "scalar":
|
|
51
|
+
case "enum":
|
|
52
|
+
let scalarType = field.kind == "enum" ? field_js_1.ScalarType.INT32 : field.T;
|
|
53
|
+
if (repeated) {
|
|
54
|
+
if (field.packed) {
|
|
55
|
+
(0, binary_format_common_js_1.writePacked)(writer, scalarType, field.no, value);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
for (const item of value) {
|
|
59
|
+
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, item, true);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else if (value !== undefined) {
|
|
64
|
+
(0, binary_format_common_js_1.writeScalar)(writer, scalarType, field.no, value, !!field.oneof || field.opt);
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
case "message":
|
|
68
|
+
if (repeated) {
|
|
69
|
+
for (const item of value) {
|
|
70
|
+
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, item);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else if (value !== undefined) {
|
|
74
|
+
(0, binary_format_common_js_1.writeMessageField)(writer, options, field, value);
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case "map":
|
|
78
|
+
for (const [key, val] of Object.entries(value)) {
|
|
79
|
+
(0, binary_format_common_js_1.writeMapEntry)(writer, options, field, key, val);
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
package/dist/cjs/private/enum.js
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Extension } from "../extension.js";
|
|
2
|
+
import type { AnyMessage, Message } from "../message.js";
|
|
3
|
+
import type { FieldInfo, OneofInfo, PartialFieldInfo } from "../field.js";
|
|
4
|
+
import { WireType } from "../binary-encoding.js";
|
|
5
|
+
import type { ProtoRuntime } from "./proto-runtime.js";
|
|
6
|
+
import type { MessageType } from "../message-type.js";
|
|
7
|
+
export type ExtensionFieldSource = extensionFieldRules<FieldInfo> | extensionFieldRules<PartialFieldInfo> | (() => extensionFieldRules<FieldInfo>) | (() => extensionFieldRules<PartialFieldInfo>);
|
|
8
|
+
type extensionFieldRules<T extends FieldInfo | PartialFieldInfo> = T extends {
|
|
9
|
+
kind: "map";
|
|
10
|
+
} ? never : T extends {
|
|
11
|
+
oneof: string;
|
|
12
|
+
} ? never : T extends {
|
|
13
|
+
oneof: OneofInfo;
|
|
14
|
+
} ? never : Omit<T, "name"> & Partial<Pick<T, "name">>;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new extension using the given runtime.
|
|
17
|
+
*/
|
|
18
|
+
export declare function makeExtension<E extends Message<E> = AnyMessage, V = unknown>(runtime: ProtoRuntime, typeName: string, extendee: MessageType<E>, field: ExtensionFieldSource): Extension<E, V>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a container that allows us to read extension fields into it with the
|
|
21
|
+
* same logic as regular fields.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createExtensionContainer<E extends Message<E> = AnyMessage, V = unknown>(extension: Extension<E, V>): [Record<string, V>, () => V];
|
|
24
|
+
type UnknownField = {
|
|
25
|
+
no: number;
|
|
26
|
+
wireType: WireType;
|
|
27
|
+
data: Uint8Array;
|
|
28
|
+
};
|
|
29
|
+
type UnknownFields = ReadonlyArray<UnknownField>;
|
|
30
|
+
/**
|
|
31
|
+
* Helper to filter unknown fields, optimized based on field type.
|
|
32
|
+
*/
|
|
33
|
+
export declare function filterUnknownFields(unknownFields: UnknownFields, field: Pick<FieldInfo, "no" | "kind" | "repeated">): UnknownField[];
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2021-2024 Buf Technologies, Inc.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.filterUnknownFields = exports.createExtensionContainer = exports.makeExtension = void 0;
|
|
17
|
+
const scalars_js_1 = require("./scalars.js");
|
|
18
|
+
/**
|
|
19
|
+
* Create a new extension using the given runtime.
|
|
20
|
+
*/
|
|
21
|
+
function makeExtension(runtime, typeName, extendee, field) {
|
|
22
|
+
let fi;
|
|
23
|
+
return {
|
|
24
|
+
typeName,
|
|
25
|
+
extendee,
|
|
26
|
+
get field() {
|
|
27
|
+
if (!fi) {
|
|
28
|
+
const i = (typeof field == "function" ? field() : field);
|
|
29
|
+
i.name = typeName.split(".").pop();
|
|
30
|
+
i.jsonName = `[${typeName}]`;
|
|
31
|
+
fi = runtime.util.newFieldList([i]).list()[0];
|
|
32
|
+
}
|
|
33
|
+
return fi;
|
|
34
|
+
},
|
|
35
|
+
runtime,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
exports.makeExtension = makeExtension;
|
|
39
|
+
/**
|
|
40
|
+
* Create a container that allows us to read extension fields into it with the
|
|
41
|
+
* same logic as regular fields.
|
|
42
|
+
*/
|
|
43
|
+
function createExtensionContainer(extension) {
|
|
44
|
+
const localName = extension.field.localName;
|
|
45
|
+
const container = Object.create(null);
|
|
46
|
+
container[localName] = initExtensionField(extension);
|
|
47
|
+
return [container, () => container[localName]];
|
|
48
|
+
}
|
|
49
|
+
exports.createExtensionContainer = createExtensionContainer;
|
|
50
|
+
function initExtensionField(ext) {
|
|
51
|
+
const field = ext.field;
|
|
52
|
+
if (field.repeated) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
if (field.default !== undefined) {
|
|
56
|
+
return field.default;
|
|
57
|
+
}
|
|
58
|
+
switch (field.kind) {
|
|
59
|
+
case "enum":
|
|
60
|
+
return field.T.values[0].no;
|
|
61
|
+
case "scalar":
|
|
62
|
+
return (0, scalars_js_1.scalarDefaultValue)(field.T, field.L);
|
|
63
|
+
case "message":
|
|
64
|
+
// eslint-disable-next-line no-case-declarations
|
|
65
|
+
const T = field.T, value = new T();
|
|
66
|
+
return T.fieldWrapper ? T.fieldWrapper.unwrapField(value) : value;
|
|
67
|
+
case "map":
|
|
68
|
+
throw "map fields are not allowed to be extensions";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Helper to filter unknown fields, optimized based on field type.
|
|
73
|
+
*/
|
|
74
|
+
function filterUnknownFields(unknownFields, field) {
|
|
75
|
+
if (!field.repeated && (field.kind == "enum" || field.kind == "scalar")) {
|
|
76
|
+
// singular scalar fields do not merge, we pick the last
|
|
77
|
+
for (let i = unknownFields.length - 1; i >= 0; --i) {
|
|
78
|
+
if (unknownFields[i].no == field.no) {
|
|
79
|
+
return [unknownFields[i]];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
return unknownFields.filter((uf) => uf.no === field.no);
|
|
85
|
+
}
|
|
86
|
+
exports.filterUnknownFields = filterUnknownFields;
|
|
@@ -8,14 +8,14 @@ export declare const featureSetDefaults: FeatureSetDefaults;
|
|
|
8
8
|
*/
|
|
9
9
|
export type MergedFeatureSet = FeatureSet & Required<FeatureSet>;
|
|
10
10
|
/**
|
|
11
|
-
* A function that resolves features
|
|
11
|
+
* A function that resolves features.
|
|
12
12
|
*
|
|
13
13
|
* If no feature set is provided, the default feature set for the edition is
|
|
14
14
|
* returned. If features are provided, they are merged into the edition default
|
|
15
15
|
* features.
|
|
16
16
|
*/
|
|
17
|
-
export type FeatureResolverFn = (
|
|
17
|
+
export type FeatureResolverFn = (a?: FeatureSet, b?: FeatureSet) => MergedFeatureSet;
|
|
18
18
|
/**
|
|
19
19
|
* Create an edition feature resolver with the given feature set defaults.
|
|
20
20
|
*/
|
|
21
|
-
export declare function createFeatureResolver(compiledFeatureSetDefaults: FeatureSetDefaults): FeatureResolverFn;
|
|
21
|
+
export declare function createFeatureResolver(compiledFeatureSetDefaults: FeatureSetDefaults, edition: Edition): FeatureResolverFn;
|