@aptre/protobuf-es-lite 0.1.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.
@@ -0,0 +1,247 @@
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.createMessageType = exports.cloneMessage = exports.compareMessages = void 0;
17
+ const protobuf_1 = require("@bufbuild/protobuf");
18
+ const field_js_1 = require("./field.js");
19
+ const partial_js_1 = require("./partial.js");
20
+ const scalar_js_1 = require("./scalar.js");
21
+ const binary_js_1 = require("./binary.js");
22
+ const json_js_1 = require("./json.js");
23
+ // compareMessages compares two messages for equality.
24
+ function compareMessages(fields, a, b) {
25
+ if (a === b) {
26
+ return true;
27
+ }
28
+ if (!a || !b) {
29
+ return false;
30
+ }
31
+ return fields.byMember().every((m) => {
32
+ const va = a[m.localName];
33
+ const vb = b[m.localName];
34
+ if (m.repeated) {
35
+ if (va.length !== vb.length) {
36
+ return false;
37
+ }
38
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- repeated fields are never "map"
39
+ switch (m.kind) {
40
+ case "message":
41
+ return va.every((a, i) => m.T.equals(a, vb[i]));
42
+ case "scalar":
43
+ return va.every((a, i) => (0, scalar_js_1.scalarEquals)(m.T, a, vb[i]));
44
+ case "enum":
45
+ return va.every((a, i) => (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, a, vb[i]));
46
+ }
47
+ throw new Error(`repeated cannot contain ${m.kind}`);
48
+ }
49
+ switch (m.kind) {
50
+ case "message":
51
+ return m.T.equals(va, vb);
52
+ case "enum":
53
+ return (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, va, vb);
54
+ case "scalar":
55
+ return (0, scalar_js_1.scalarEquals)(m.T, va, vb);
56
+ case "oneof":
57
+ if (va.case !== vb.case) {
58
+ return false;
59
+ }
60
+ const s = m.findField(va.case);
61
+ if (s === undefined) {
62
+ return true;
63
+ }
64
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- oneof fields are never "map"
65
+ switch (s.kind) {
66
+ case "message":
67
+ return s.T.equals(va.value, vb.value);
68
+ case "enum":
69
+ return (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, va.value, vb.value);
70
+ case "scalar":
71
+ return (0, scalar_js_1.scalarEquals)(s.T, va.value, vb.value);
72
+ }
73
+ throw new Error(`oneof cannot contain ${s.kind}`);
74
+ case "map":
75
+ const keys = Object.keys(va).concat(Object.keys(vb));
76
+ switch (m.V.kind) {
77
+ case "message":
78
+ const messageType = m.V.T;
79
+ return keys.every((k) => messageType.equals(va[k], vb[k]));
80
+ case "enum":
81
+ return keys.every((k) => (0, scalar_js_1.scalarEquals)(protobuf_1.ScalarType.INT32, va[k], vb[k]));
82
+ case "scalar":
83
+ const scalarType = m.V.T;
84
+ return keys.every((k) => (0, scalar_js_1.scalarEquals)(scalarType, va[k], vb[k]));
85
+ }
86
+ }
87
+ });
88
+ }
89
+ exports.compareMessages = compareMessages;
90
+ // clone a single field value - i.e. the element type of repeated fields, the value type of maps
91
+ function cloneSingularField(value, fieldInfo) {
92
+ if (value === undefined) {
93
+ return value;
94
+ }
95
+ if (fieldInfo.kind === "message") {
96
+ return cloneMessage(value, fieldInfo.T.fields);
97
+ }
98
+ if (fieldInfo.kind === "oneof") {
99
+ if (value.case === undefined) {
100
+ return undefined;
101
+ }
102
+ const selectedField = fieldInfo.findField(value.case);
103
+ if (!selectedField) {
104
+ throw new Error(`Invalid oneof case "${value.case}" for ${fieldInfo.name}`);
105
+ }
106
+ return {
107
+ case: value.case,
108
+ value: cloneSingularField(value.value, selectedField),
109
+ };
110
+ }
111
+ if (value instanceof Uint8Array) {
112
+ const c = new Uint8Array(value.byteLength);
113
+ c.set(value);
114
+ return c;
115
+ }
116
+ return value;
117
+ }
118
+ // TODO use isFieldSet() here to support future field presence
119
+ function cloneMessage(message, fields) {
120
+ const clone = {};
121
+ for (const member of fields.byMember()) {
122
+ const source = message[member.localName];
123
+ let copy;
124
+ if (member.repeated) {
125
+ copy = source.map((v) => cloneSingularField(v, member));
126
+ }
127
+ else if (member.kind == "map") {
128
+ copy = {};
129
+ for (const [key, v] of Object.entries(source)) {
130
+ copy[key] = cloneSingularField(v, member);
131
+ }
132
+ }
133
+ else if (member.kind == "oneof") {
134
+ const f = member.findField(source.case);
135
+ copy = f
136
+ ? { case: source.case, value: cloneSingularField(source.value, member) }
137
+ : { case: undefined };
138
+ }
139
+ else {
140
+ copy = cloneSingularField(source, member);
141
+ }
142
+ clone[member.localName] = copy;
143
+ }
144
+ return clone;
145
+ }
146
+ exports.cloneMessage = cloneMessage;
147
+ /**
148
+ * createMessageType creates a new message type.
149
+ *
150
+ * The argument `packedByDefault` specifies whether fields that do not specify
151
+ * `packed` should be packed (proto3) or unpacked (proto2).
152
+ */
153
+ function createMessageType(params) {
154
+ const { fields: fieldsSource, typeName, packedByDefault, delimitedMessageEncoding, fieldWrapper, } = params;
155
+ const fields = (0, field_js_1.newFieldList)(fieldsSource, packedByDefault);
156
+ const mt = {
157
+ typeName,
158
+ fields,
159
+ fieldWrapper,
160
+ create(partial) {
161
+ const message = createMessage(fields);
162
+ (0, partial_js_1.applyPartialMessage)(partial, message, fields);
163
+ return message;
164
+ },
165
+ equals(a, b) {
166
+ return compareMessages(fields, a, b);
167
+ },
168
+ clone(a) {
169
+ if (a == null) {
170
+ return a;
171
+ }
172
+ return cloneMessage(a, fields);
173
+ },
174
+ fromBinary(bytes, options) {
175
+ const message = this.create();
176
+ const opt = (0, binary_js_1.makeReadOptions)(options);
177
+ (0, binary_js_1.readMessage)(message, fields, opt.readerFactory(bytes), bytes.byteLength, opt, delimitedMessageEncoding);
178
+ return message;
179
+ },
180
+ fromJson(jsonValue, options) {
181
+ const message = this.create();
182
+ const opts = (0, json_js_1.makeReadOptions)(options);
183
+ (0, json_js_1.readMessage)(fields, typeName, jsonValue, opts, message);
184
+ return message;
185
+ },
186
+ fromJsonString(jsonString, options) {
187
+ let json;
188
+ try {
189
+ json = JSON.parse(jsonString);
190
+ }
191
+ catch (e) {
192
+ throw new Error(`cannot decode ${typeName} from JSON: ${e instanceof Error ? e.message : String(e)}`);
193
+ }
194
+ return this.fromJson(json, options);
195
+ },
196
+ toBinary(a, options) {
197
+ const opt = (0, binary_js_1.makeWriteOptions)(options);
198
+ const writer = opt.writerFactory();
199
+ (0, binary_js_1.writeMessage)(a, fields, writer, opt);
200
+ return writer.finish();
201
+ },
202
+ toJson(a, options) {
203
+ const opt = (0, json_js_1.makeWriteOptions)(options);
204
+ return (0, json_js_1.writeMessage)(a, fields, opt);
205
+ },
206
+ toJsonString(a, options) {
207
+ const value = this.toJson(a, options);
208
+ return JSON.stringify(value, null, options?.prettySpaces ?? 0);
209
+ },
210
+ };
211
+ if (params.toJson) {
212
+ mt.toJson = params.toJson;
213
+ }
214
+ return mt;
215
+ }
216
+ exports.createMessageType = createMessageType;
217
+ /**
218
+ * createMessage recursively builds a message filled with zero values based on the given FieldList.
219
+ */
220
+ function createMessage(fields) {
221
+ const message = {};
222
+ for (const field of fields.list()) {
223
+ const fieldKind = field.kind;
224
+ switch (fieldKind) {
225
+ case "scalar":
226
+ message[field.localName] = (field.T == protobuf_1.ScalarType.BOOL ? false : 0);
227
+ break;
228
+ case "enum":
229
+ message[field.localName] = 0;
230
+ break;
231
+ case "message":
232
+ if (field.oneof) {
233
+ // leave oneofs undefined
234
+ message[field.localName] = undefined;
235
+ continue;
236
+ }
237
+ message[field.localName] = createMessage(field.T.fields);
238
+ break;
239
+ case "map":
240
+ message[field.localName] = {};
241
+ break;
242
+ default:
243
+ field;
244
+ }
245
+ }
246
+ return message;
247
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyPartialMessage = void 0;
4
+ const protobuf_1 = require("@bufbuild/protobuf");
5
+ const is_message_js_1 = require("./is-message.js");
6
+ // applyPartialMessage applies a partial message to a message.
7
+ function applyPartialMessage(source, target, fields) {
8
+ if (source === undefined) {
9
+ return;
10
+ }
11
+ for (const member of fields.byMember()) {
12
+ const localName = member.localName, t = target, s = source;
13
+ if (s[localName] === undefined) {
14
+ // TODO if source is a Message instance, we should use isFieldSet() here to support future field presence
15
+ continue;
16
+ }
17
+ switch (member.kind) {
18
+ case "oneof":
19
+ const sk = s[localName].case;
20
+ if (sk === undefined) {
21
+ continue;
22
+ }
23
+ const sourceField = member.findField(sk);
24
+ let val = s[localName].value;
25
+ if (sourceField &&
26
+ sourceField.kind == "message" &&
27
+ !(0, is_message_js_1.isCompleteMessage)(val, sourceField.T.fields.list())) {
28
+ val = sourceField.T.create(val);
29
+ }
30
+ else if (sourceField &&
31
+ sourceField.kind === "scalar" &&
32
+ sourceField.T === protobuf_1.ScalarType.BYTES) {
33
+ val = toU8Arr(val);
34
+ }
35
+ t[localName] = { case: sk, value: val };
36
+ break;
37
+ case "scalar":
38
+ case "enum":
39
+ let copy = s[localName];
40
+ if (member.T === protobuf_1.ScalarType.BYTES) {
41
+ copy = member.repeated
42
+ ? copy.map(toU8Arr)
43
+ : toU8Arr(copy);
44
+ }
45
+ t[localName] = copy;
46
+ break;
47
+ case "map":
48
+ switch (member.V.kind) {
49
+ case "scalar":
50
+ case "enum":
51
+ if (member.V.T === protobuf_1.ScalarType.BYTES) {
52
+ for (const [k, v] of Object.entries(s[localName])) {
53
+ t[localName][k] = toU8Arr(v);
54
+ }
55
+ }
56
+ else {
57
+ Object.assign(t[localName], s[localName]);
58
+ }
59
+ break;
60
+ case "message":
61
+ const messageType = member.V.T;
62
+ for (const k of Object.keys(s[localName])) {
63
+ let val = s[localName][k];
64
+ if (!messageType.fieldWrapper) {
65
+ // We only take partial input for messages that are not a wrapper type.
66
+ // For those messages, we recursively normalize the partial input.
67
+ val = messageType.create(val);
68
+ }
69
+ t[localName][k] = val;
70
+ }
71
+ break;
72
+ }
73
+ break;
74
+ case "message":
75
+ const mt = member.T;
76
+ if (member.repeated) {
77
+ t[localName] = s[localName].map((val) => (0, is_message_js_1.isCompleteMessage)(val, mt.fields.list()) ? val : mt.create(val));
78
+ }
79
+ else {
80
+ const val = s[localName];
81
+ if (mt.fieldWrapper) {
82
+ if (
83
+ // We can't use BytesValue.typeName as that will create a circular import
84
+ mt.typeName === "google.protobuf.BytesValue") {
85
+ t[localName] = toU8Arr(val);
86
+ }
87
+ else {
88
+ t[localName] = val;
89
+ }
90
+ }
91
+ else {
92
+ t[localName] = (0, is_message_js_1.isCompleteMessage)(val, mt.fields.list())
93
+ ? val
94
+ : mt.create(val);
95
+ }
96
+ }
97
+ break;
98
+ }
99
+ }
100
+ }
101
+ exports.applyPartialMessage = applyPartialMessage;
102
+ // converts any ArrayLike<number> to Uint8Array if necessary.
103
+ function toU8Arr(input) {
104
+ return input instanceof Uint8Array ? input : new Uint8Array(input);
105
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ // Copyright 2024 Aperture Robotics, LLC.
3
+ // Copyright 2021-2024 Buf Technologies, Inc.
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.protocGenEsLite = void 0;
18
+ const protoplugin_1 = require("@bufbuild/protoplugin");
19
+ const typescript_js_1 = require("./typescript.js");
20
+ // import { version } from "../package.json";
21
+ exports.protocGenEsLite = (0, protoplugin_1.createEcmaScriptPlugin)({
22
+ name: "protoc-gen-es-lite",
23
+ version: `unknown`,
24
+ generateTs: typescript_js_1.generateTs,
25
+ });
package/dist/scalar.js ADDED
@@ -0,0 +1,104 @@
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.isScalarZeroValue = exports.scalarZeroValue = exports.scalarEquals = void 0;
17
+ const protobuf_1 = require("@bufbuild/protobuf");
18
+ /**
19
+ * Returns true if both scalar values are equal.
20
+ */
21
+ function scalarEquals(type, a, b) {
22
+ if (a === b) {
23
+ // This correctly matches equal values except BYTES and (possibly) 64-bit integers.
24
+ return true;
25
+ }
26
+ // Special case BYTES - we need to compare each byte individually
27
+ if (type == protobuf_1.ScalarType.BYTES) {
28
+ if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array)) {
29
+ return false;
30
+ }
31
+ if (a.length !== b.length) {
32
+ return false;
33
+ }
34
+ for (let i = 0; i < a.length; i++) {
35
+ if (a[i] !== b[i]) {
36
+ return false;
37
+ }
38
+ }
39
+ return true;
40
+ }
41
+ // Special case 64-bit integers - we support number, string and bigint representation.
42
+ // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
43
+ switch (type) {
44
+ case protobuf_1.ScalarType.UINT64:
45
+ case protobuf_1.ScalarType.FIXED64:
46
+ case protobuf_1.ScalarType.INT64:
47
+ case protobuf_1.ScalarType.SFIXED64:
48
+ case protobuf_1.ScalarType.SINT64:
49
+ // Loose comparison will match between 0n, 0 and "0".
50
+ return a == b;
51
+ }
52
+ // Anything that hasn't been caught by strict comparison or special cased
53
+ // BYTES and 64-bit integers is not equal.
54
+ return false;
55
+ }
56
+ exports.scalarEquals = scalarEquals;
57
+ /**
58
+ * Returns the zero value for the given scalar type.
59
+ */
60
+ function scalarZeroValue(type, longType) {
61
+ switch (type) {
62
+ case protobuf_1.ScalarType.BOOL:
63
+ return false;
64
+ case protobuf_1.ScalarType.UINT64:
65
+ case protobuf_1.ScalarType.FIXED64:
66
+ case protobuf_1.ScalarType.INT64:
67
+ case protobuf_1.ScalarType.SFIXED64:
68
+ case protobuf_1.ScalarType.SINT64:
69
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison -- acceptable since it's covered by tests
70
+ return (longType == 0 ? protobuf_1.protoInt64.zero : "0");
71
+ case protobuf_1.ScalarType.DOUBLE:
72
+ case protobuf_1.ScalarType.FLOAT:
73
+ return 0.0;
74
+ case protobuf_1.ScalarType.BYTES:
75
+ return new Uint8Array(0);
76
+ case protobuf_1.ScalarType.STRING:
77
+ return "";
78
+ default:
79
+ // Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
80
+ // We do not use individual cases to save a few bytes code size.
81
+ return 0;
82
+ }
83
+ }
84
+ exports.scalarZeroValue = scalarZeroValue;
85
+ /**
86
+ * Returns true for a zero-value. For example, an integer has the zero-value `0`,
87
+ * a boolean is `false`, a string is `""`, and bytes is an empty Uint8Array.
88
+ *
89
+ * In proto3, zero-values are not written to the wire, unless the field is
90
+ * optional or repeated.
91
+ */
92
+ function isScalarZeroValue(type, value) {
93
+ switch (type) {
94
+ case protobuf_1.ScalarType.BOOL:
95
+ return value === false;
96
+ case protobuf_1.ScalarType.STRING:
97
+ return value === "";
98
+ case protobuf_1.ScalarType.BYTES:
99
+ return value instanceof Uint8Array && !value.byteLength;
100
+ default:
101
+ return value == 0; // Loose comparison matches 0n, 0 and "0"
102
+ }
103
+ }
104
+ exports.isScalarZeroValue = isScalarZeroValue;
@@ -0,0 +1,177 @@
1
+ "use strict";
2
+ // Copyright 2024 Aperture Robotics, LLC.
3
+ // Copyright 2021-2024 Buf Technologies, Inc.
4
+ //
5
+ // Licensed under the Apache License, Version 2.0 (the "License");
6
+ // you may not use this file except in compliance with the License.
7
+ // You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing, software
12
+ // distributed under the License is distributed on an "AS IS" BASIS,
13
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ // See the License for the specific language governing permissions and
15
+ // limitations under the License.
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.getFieldInfoLiteral = exports.generateFieldInfo = exports.generateTs = void 0;
18
+ const protobuf_1 = require("@bufbuild/protobuf");
19
+ const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
20
+ const editions_js_1 = require("./editions.js");
21
+ const util_js_1 = require("./util.js");
22
+ const libPkg = "@aptre/protobuf-es-lite";
23
+ const MessageImport = (0, ecmascript_1.createImportSymbol)("Message", libPkg);
24
+ const MessageTypeImport = (0, ecmascript_1.createImportSymbol)("MessageType", libPkg);
25
+ const CreateMessageTypeImport = (0, ecmascript_1.createImportSymbol)("createMessageType", libPkg);
26
+ function generateTs(schema) {
27
+ for (const file of schema.files) {
28
+ const f = schema.generateFile(file.name + "_pb.ts");
29
+ f.preamble(file);
30
+ f.print(`export const protobufPackage = "${file.proto.package}";`);
31
+ f.print();
32
+ for (const enumeration of file.enums) {
33
+ generateEnum(f, enumeration);
34
+ }
35
+ for (const message of file.messages) {
36
+ generateMessage(schema, f, message);
37
+ }
38
+ // We do not generate anything for services or extensions
39
+ }
40
+ }
41
+ exports.generateTs = generateTs;
42
+ // prettier-ignore
43
+ function generateEnum(f, enumeration) {
44
+ f.print(f.jsDoc(enumeration));
45
+ f.print(f.exportDecl("enum", enumeration), " {");
46
+ for (const value of enumeration.values) {
47
+ if (enumeration.values.indexOf(value) > 0) {
48
+ f.print();
49
+ }
50
+ f.print(f.jsDoc(value, " "));
51
+ f.print(" ", (0, ecmascript_1.localName)(value), " = ", value.number, ",");
52
+ }
53
+ f.print("}");
54
+ f.print();
55
+ f.print("// ", enumeration, "_Name maps the enum names to the values.");
56
+ f.print(f.exportDecl("const", enumeration.name + "_Name"), " = {");
57
+ for (const value of enumeration.values) {
58
+ f.print(" ", (0, ecmascript_1.localName)(value), ": ", enumeration, ".", (0, ecmascript_1.localName)(value), ",");
59
+ }
60
+ f.print("};");
61
+ f.print();
62
+ }
63
+ // prettier-ignore
64
+ function generateMessage(schema, f, message) {
65
+ // check if we support this runtime
66
+ (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
67
+ f.print(f.jsDoc(message));
68
+ f.print(f.exportDecl("interface", message), " extends ", MessageImport, "<", message, "> {");
69
+ for (const member of message.members) {
70
+ switch (member.kind) {
71
+ case "field":
72
+ generateField(f, member);
73
+ break;
74
+ }
75
+ f.print();
76
+ }
77
+ f.print("}");
78
+ f.print();
79
+ f.print(f.exportDecl("const", message), ": ", MessageTypeImport, "<", message, "> = ", CreateMessageTypeImport, "(");
80
+ f.print(" {");
81
+ f.print(" typeName: ", f.string(message.typeName), ",");
82
+ f.print(" fields: [");
83
+ for (const field of message.fields) {
84
+ generateFieldInfo(f, field);
85
+ }
86
+ f.print(" ],");
87
+ f.print(" packedByDefault: ", message.file.proto.syntax === "proto3", ",");
88
+ f.print(" },");
89
+ f.print(" true,");
90
+ f.print(");");
91
+ f.print();
92
+ for (const nestedEnum of message.nestedEnums) {
93
+ generateEnum(f, nestedEnum);
94
+ }
95
+ for (const nestedMessage of message.nestedMessages) {
96
+ generateMessage(schema, f, nestedMessage);
97
+ }
98
+ }
99
+ // prettier-ignore
100
+ function generateField(f, field) {
101
+ f.print(f.jsDoc(field, " "));
102
+ const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
103
+ f.print(" ", (0, ecmascript_1.localName)(field), ": ", typing, ";");
104
+ }
105
+ // prettier-ignore
106
+ function generateFieldInfo(f, field) {
107
+ f.print(" ", getFieldInfoLiteral(field), ",");
108
+ }
109
+ exports.generateFieldInfo = generateFieldInfo;
110
+ // prettier-ignore
111
+ function getFieldInfoLiteral(field) {
112
+ const e = [];
113
+ e.push("{ no: ", field.number, `, `);
114
+ if (field.kind == "field") {
115
+ e.push(`name: "`, field.name, `", `);
116
+ if (field.jsonName !== undefined) {
117
+ e.push(`jsonName: "`, field.jsonName, `", `);
118
+ }
119
+ }
120
+ switch (field.fieldKind) {
121
+ case "scalar":
122
+ e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
123
+ if (field.longType != protobuf_1.LongType.BIGINT) {
124
+ e.push(`L: `, field.longType, ` /* LongType.`, protobuf_1.LongType[field.longType], ` */, `);
125
+ }
126
+ break;
127
+ case "map":
128
+ e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
129
+ switch (field.mapValue.kind) {
130
+ case "scalar":
131
+ e.push(`V: {kind: "scalar", T: `, field.mapValue.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapValue.scalar], ` */}, `);
132
+ break;
133
+ case "message":
134
+ e.push(`V: {kind: "message", T: `, field.mapValue.message, `}, `);
135
+ break;
136
+ case "enum":
137
+ e.push(`V: {kind: "enum", T: `, field.mapValue.enum, `}, `);
138
+ break;
139
+ }
140
+ break;
141
+ case "message":
142
+ e.push(`kind: "message", T: `, field.message, `, `);
143
+ if (field.proto.type === protobuf_1.FieldDescriptorProto_Type.GROUP) {
144
+ e.push(`delimited: true, `);
145
+ }
146
+ break;
147
+ case "enum":
148
+ e.push(`kind: "enum", T: `, field.enum, `, `);
149
+ break;
150
+ }
151
+ if (field.repeated) {
152
+ e.push(`repeated: true, `);
153
+ if (field.packed !== field.packedByDefault) {
154
+ e.push(`packed: `, field.packed, `, `);
155
+ }
156
+ }
157
+ if (field.optional) {
158
+ e.push(`opt: true, `);
159
+ }
160
+ else if (field.proto.label === protobuf_1.FieldDescriptorProto_Label.REQUIRED) {
161
+ e.push(`req: true, `);
162
+ }
163
+ const defaultValue = (0, util_js_1.getFieldDefaultValueExpression)(field);
164
+ if (defaultValue !== undefined) {
165
+ e.push(`default: `, defaultValue, `, `);
166
+ }
167
+ if (field.oneof) {
168
+ e.push(`oneof: "`, field.oneof.name, `", `);
169
+ }
170
+ const lastE = e[e.length - 1];
171
+ if (typeof lastE == "string" && lastE.endsWith(", ")) {
172
+ e[e.length - 1] = lastE.substring(0, lastE.length - 2);
173
+ }
174
+ e.push(" }");
175
+ return e;
176
+ }
177
+ exports.getFieldInfoLiteral = getFieldInfoLiteral;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleUnknownField = exports.listUnknownFields = exports.unknownFieldsSymbol = void 0;
4
+ // unknownFieldsSymbol is the symbol used for unknown fields.
5
+ exports.unknownFieldsSymbol = Symbol("@aptre/protobuf-es-lite/unknown-fields");
6
+ function listUnknownFields(message) {
7
+ return message[exports.unknownFieldsSymbol] ?? [];
8
+ }
9
+ exports.listUnknownFields = listUnknownFields;
10
+ // handleUnknownField stores an unknown field.
11
+ function handleUnknownField(message, no, wireType, data) {
12
+ if (typeof message !== "object") {
13
+ return;
14
+ }
15
+ const m = message;
16
+ if (!Array.isArray(m[exports.unknownFieldsSymbol])) {
17
+ m[exports.unknownFieldsSymbol] = [];
18
+ }
19
+ m[exports.unknownFieldsSymbol].push({ no, wireType, data });
20
+ }
21
+ exports.handleUnknownField = handleUnknownField;