@aptre/protobuf-es-lite 0.2.11 → 0.2.12

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.
@@ -63,8 +63,18 @@ export const Value = createMessageType({
63
63
  typeName: "google.protobuf.Value",
64
64
  fields: [
65
65
  { no: 1, name: "null_value", kind: "enum", T: NullValue_Enum },
66
- { no: 2, name: "number_value", kind: "scalar", T: 1 /* ScalarType.DOUBLE */ },
67
- { no: 3, name: "string_value", kind: "scalar", T: 9 /* ScalarType.STRING */ },
66
+ {
67
+ no: 2,
68
+ name: "number_value",
69
+ kind: "scalar",
70
+ T: 1 /* ScalarType.DOUBLE */,
71
+ },
72
+ {
73
+ no: 3,
74
+ name: "string_value",
75
+ kind: "scalar",
76
+ T: 9 /* ScalarType.STRING */,
77
+ },
68
78
  { no: 4, name: "bool_value", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
69
79
  { no: 5, name: "struct_value", kind: "message", T: () => Struct },
70
80
  { no: 6, name: "list_value", kind: "message", T: () => ListValue },
@@ -74,7 +84,13 @@ export const Value = createMessageType({
74
84
  export const Struct = createMessageType({
75
85
  typeName: "google.protobuf.Struct",
76
86
  fields: [
77
- { no: 1, name: "fields", kind: "map", K: 9 /* ScalarType.STRING */, V: { kind: "message", T: () => Value } },
87
+ {
88
+ no: 1,
89
+ name: "fields",
90
+ kind: "map",
91
+ K: 9 /* ScalarType.STRING */,
92
+ V: { kind: "message", T: () => Value },
93
+ },
78
94
  ],
79
95
  packedByDefault: true,
80
96
  });
@@ -293,9 +293,20 @@ export const Field = createMessageType({
293
293
  { no: 6, name: "type_url", kind: "scalar", T: 9 /* ScalarType.STRING */ },
294
294
  { no: 7, name: "oneof_index", kind: "scalar", T: 5 /* ScalarType.INT32 */ },
295
295
  { no: 8, name: "packed", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
296
- { no: 9, name: "options", kind: "message", T: () => Option, repeated: true },
296
+ {
297
+ no: 9,
298
+ name: "options",
299
+ kind: "message",
300
+ T: () => Option,
301
+ repeated: true,
302
+ },
297
303
  { no: 10, name: "json_name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
298
- { no: 11, name: "default_value", kind: "scalar", T: 9 /* ScalarType.STRING */ },
304
+ {
305
+ no: 11,
306
+ name: "default_value",
307
+ kind: "scalar",
308
+ T: 9 /* ScalarType.STRING */,
309
+ },
299
310
  ],
300
311
  packedByDefault: true,
301
312
  });
@@ -304,8 +315,20 @@ export const Type = createMessageType({
304
315
  fields: [
305
316
  { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
306
317
  { no: 2, name: "fields", kind: "message", T: () => Field, repeated: true },
307
- { no: 3, name: "oneofs", kind: "scalar", T: 9 /* ScalarType.STRING */, repeated: true },
308
- { no: 4, name: "options", kind: "message", T: () => Option, repeated: true },
318
+ {
319
+ no: 3,
320
+ name: "oneofs",
321
+ kind: "scalar",
322
+ T: 9 /* ScalarType.STRING */,
323
+ repeated: true,
324
+ },
325
+ {
326
+ no: 4,
327
+ name: "options",
328
+ kind: "message",
329
+ T: () => Option,
330
+ repeated: true,
331
+ },
309
332
  { no: 5, name: "source_context", kind: "message", T: () => SourceContext },
310
333
  { no: 6, name: "syntax", kind: "enum", T: Syntax_Enum },
311
334
  { no: 7, name: "edition", kind: "scalar", T: 9 /* ScalarType.STRING */ },
@@ -317,7 +340,13 @@ export const EnumValue = createMessageType({
317
340
  fields: [
318
341
  { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
319
342
  { no: 2, name: "number", kind: "scalar", T: 5 /* ScalarType.INT32 */ },
320
- { no: 3, name: "options", kind: "message", T: () => Option, repeated: true },
343
+ {
344
+ no: 3,
345
+ name: "options",
346
+ kind: "message",
347
+ T: () => Option,
348
+ repeated: true,
349
+ },
321
350
  ],
322
351
  packedByDefault: true,
323
352
  });
@@ -325,8 +354,20 @@ export const Enum = createMessageType({
325
354
  typeName: "google.protobuf.Enum",
326
355
  fields: [
327
356
  { no: 1, name: "name", kind: "scalar", T: 9 /* ScalarType.STRING */ },
328
- { no: 2, name: "enumvalue", kind: "message", T: () => EnumValue, repeated: true },
329
- { no: 3, name: "options", kind: "message", T: () => Option, repeated: true },
357
+ {
358
+ no: 2,
359
+ name: "enumvalue",
360
+ kind: "message",
361
+ T: () => EnumValue,
362
+ repeated: true,
363
+ },
364
+ {
365
+ no: 3,
366
+ name: "options",
367
+ kind: "message",
368
+ T: () => Option,
369
+ repeated: true,
370
+ },
330
371
  { no: 4, name: "source_context", kind: "message", T: () => SourceContext },
331
372
  { no: 5, name: "syntax", kind: "enum", T: Syntax_Enum },
332
373
  { no: 6, name: "edition", kind: "scalar", T: 9 /* ScalarType.STRING */ },
package/dist/json.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { FieldInfo, FieldList } from "./field.js";
2
- import { IMessageTypeRegistry } from "./type-registry.js";
3
2
  /**
4
3
  * Options for parsing JSON data.
5
4
  */
@@ -10,10 +9,6 @@ export interface JsonReadOptions {
10
9
  * unrecognized enum string representations.
11
10
  */
12
11
  ignoreUnknownFields: boolean;
13
- /**
14
- * This option is required to read `google.protobuf.Any`
15
- */
16
- typeRegistry?: IMessageTypeRegistry;
17
12
  }
18
13
  /**
19
14
  * Options for serializing to JSON.
@@ -40,10 +35,6 @@ export interface JsonWriteOptions {
40
35
  * field name.
41
36
  */
42
37
  useProtoFieldName: boolean;
43
- /**
44
- * This option is required to write `google.protobuf.Any`
45
- */
46
- typeRegistry?: IMessageTypeRegistry;
47
38
  }
48
39
  /**
49
40
  * Options for serializing to JSON.
package/dist/json.js CHANGED
@@ -90,28 +90,6 @@ export function writeMessage(message, fields, options) {
90
90
  jsonValue;
91
91
  }
92
92
  }
93
- // TODO: handle extensions
94
- /*
95
- const registry = options.typeRegistry;
96
- if (registry?.findExtensionFor) {
97
- for (const uf of listUnknownFields(message)) {
98
- const ext = registry.findExtensionFor(typeName, uf.no);
99
- if (ext && hasExtension(message, ext)) {
100
- // We pass on the options as BinaryReadOptions, so that users can bring their own
101
- // binary reader factory if necessary.
102
- const value = getExtension(
103
- message,
104
- ext,
105
- options as Partial<BinaryReadOptions>,
106
- );
107
- const jsonValue = writeField(ext.field, value, options);
108
- if (jsonValue !== undefined) {
109
- json[ext.field.jsonName] = jsonValue;
110
- }
111
- }
112
- }
113
- }
114
- */
115
93
  }
116
94
  catch (e) {
117
95
  const m = field ?
package/dist/message.d.ts CHANGED
@@ -55,15 +55,15 @@ export interface MessageType<T extends Message<T>> {
55
55
  * If a message field is already present, it will be merged with the
56
56
  * new data.
57
57
  */
58
- fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): CompleteMessage<T>;
58
+ fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): T;
59
59
  /**
60
60
  * Parse a message from a JSON value.
61
61
  */
62
- fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): CompleteMessage<T>;
62
+ fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): T;
63
63
  /**
64
64
  * Parse a message from a JSON string.
65
65
  */
66
- fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): CompleteMessage<T>;
66
+ fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): T;
67
67
  /**
68
68
  * Returns true if the given arguments have equal field values, recursively.
69
69
  * Will also return true if both messages are `undefined` or `null`.
package/dist/message.js CHANGED
@@ -172,13 +172,13 @@ export function createMessageType(params) {
172
172
  return cloneMessage(a, fields);
173
173
  },
174
174
  fromBinary(bytes, options) {
175
- const message = this.create();
175
+ const message = {};
176
176
  const opt = makeBinaryReadOptions(options);
177
177
  readBinaryMessage(message, fields, opt.readerFactory(bytes), bytes.byteLength, opt, delimitedMessageEncoding);
178
178
  return message;
179
179
  },
180
180
  fromJson(jsonValue, options) {
181
- const message = this.create();
181
+ const message = {};
182
182
  const opts = makeJsonReadOptions(options);
183
183
  readJsonMessage(fields, typeName, jsonValue, opts, message);
184
184
  return message;
package/dist/partial.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { resolveMessageType } from "./field.js";
2
- import { isCompleteMessage } from "./is-message.js";
3
2
  import { ScalarType } from "./scalar.js";
4
3
  // applyPartialMessage applies a partial message to a message.
5
4
  export function applyPartialMessage(source, target, fields) {
@@ -9,7 +8,6 @@ export function applyPartialMessage(source, target, fields) {
9
8
  for (const member of fields.byMember()) {
10
9
  const localName = member.localName, t = target, s = source;
11
10
  if (s[localName] === undefined) {
12
- // TODO if source is a Message instance, we should use isFieldSet() here to support future field presence
13
11
  continue;
14
12
  }
15
13
  switch (member.kind) {
@@ -21,9 +19,8 @@ export function applyPartialMessage(source, target, fields) {
21
19
  const sourceField = member.findField(sk);
22
20
  let val = s[localName].value;
23
21
  if (sourceField?.kind == "message") {
24
- const messageType = resolveMessageType(sourceField.T);
25
- if (!isCompleteMessage(val, messageType.fields.list())) {
26
- val = messageType.create(val);
22
+ if (val === undefined) {
23
+ val = {};
27
24
  }
28
25
  }
29
26
  else if (sourceField &&
@@ -62,9 +59,9 @@ export function applyPartialMessage(source, target, fields) {
62
59
  for (const k of Object.keys(s[localName])) {
63
60
  let val = s[localName][k];
64
61
  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);
62
+ if (val === undefined) {
63
+ val = {};
64
+ }
68
65
  }
69
66
  t[localName][k] = val;
70
67
  }
@@ -74,7 +71,7 @@ export function applyPartialMessage(source, target, fields) {
74
71
  case "message":
75
72
  const mt = resolveMessageType(member.T);
76
73
  if (member.repeated) {
77
- t[localName] = s[localName].map((val) => isCompleteMessage(val, mt.fields.list()) ? val : mt.create(val));
74
+ t[localName] = s[localName].map((val) => val ?? {});
78
75
  }
79
76
  else {
80
77
  const val = s[localName];
@@ -89,9 +86,7 @@ export function applyPartialMessage(source, target, fields) {
89
86
  }
90
87
  }
91
88
  else {
92
- const messageType = mt;
93
- t[localName] =
94
- isCompleteMessage(val, messageType.fields.list()) ? val : (messageType.create(val));
89
+ t[localName] = val ?? {};
95
90
  }
96
91
  }
97
92
  break;
@@ -14,7 +14,7 @@
14
14
  import { createSchema } from "./ecmascript/schema.js";
15
15
  import { transpile } from "./ecmascript/transpile.js";
16
16
  import { parseParameter } from "./ecmascript/parameter.js";
17
- import { CodeGeneratorResponse, CodeGeneratorResponse_Feature, } from "../google/protobuf/compiler/plugin.pb.js";
17
+ import { CodeGeneratorResponse_Feature, } from "../google/protobuf/compiler/plugin.pb.js";
18
18
  import { protoInt64 } from "../proto-int64.js";
19
19
  /**
20
20
  * Create a new code generator plugin for ECMAScript.
@@ -103,7 +103,7 @@ function toResponse(files, supportsEditions) {
103
103
  supportedFeatures =
104
104
  supportedFeatures | CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS;
105
105
  }
106
- return CodeGeneratorResponse.create({
106
+ return {
107
107
  supportedFeatures: protoInt64.parse(supportedFeatures),
108
108
  file: files.map((f) => {
109
109
  if (f.preamble !== undefined) {
@@ -111,5 +111,5 @@ function toResponse(files, supportsEditions) {
111
111
  }
112
112
  return f;
113
113
  }),
114
- });
114
+ };
115
115
  }
@@ -12,12 +12,12 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  export class PluginOptionError extends Error {
15
+ name = "PluginOptionError";
15
16
  constructor(option, reason) {
16
17
  const detail = reason !== undefined ? reasonToString(reason) : "";
17
18
  super(detail.length > 0 ?
18
19
  `invalid option "${option}": ${detail}`
19
20
  : `invalid option "${option}"`);
20
- this.name = "PluginOptionError";
21
21
  }
22
22
  }
23
23
  export function reasonToString(reason) {
@@ -1,11 +1,7 @@
1
- //
2
-
3
1
  // @generated by protoc-gen-es-lite unknown with parameter "target=ts,ts_nocheck=false"
4
2
  // @generated from file example/example.proto (package example, syntax proto3)
5
3
  /* eslint-disable */
6
4
 
7
- //
8
-
9
5
  import type { MessageType, PartialFieldInfo } from "@aptre/protobuf-es-lite";
10
6
  import { createEnumType, createMessageType, Message, Timestamp } from "@aptre/protobuf-es-lite";
11
7
 
@@ -14,7 +10,6 @@ export const protobufPackage = "example";
14
10
  /**
15
11
  * ExampleEnum is an example enumeration.
16
12
  *
17
- *
18
13
  * @generated from enum example.ExampleEnum
19
14
  */
20
15
  export enum ExampleEnum {
@@ -44,30 +39,41 @@ export const ExampleEnum_Enum = createEnumType("example.ExampleEnum", [
44
39
  /**
45
40
  * EchoMsg is the message body for Echo.
46
41
  *
47
- *
48
42
  * @generated from message example.EchoMsg
49
43
  */
50
44
  export type EchoMsg = Message<{
51
45
  /**
52
- * @generated from field: string body = 1 [packed = false, default = "", jstype = JS_NORMAL];
46
+ * @generated from field: string body = 1;
53
47
  */
54
48
  body?: string;
55
49
  /**
56
- * @generated from field: google.protobuf.Timestamp ts = 2 [packed = false, default = , jstype = JS_NORMAL];
50
+ * @generated from field: google.protobuf.Timestamp ts = 2;
57
51
  */
58
52
  ts?: Timestamp;
59
53
  /**
60
- * @generated from field: example.ExampleEnum example_enum = 3 [packed = false, default = , jstype = JS_NORMAL];
54
+ * @generated from field: repeated google.protobuf.Timestamp timestamps = 5;
61
55
  */
62
- exampleEnum?: ExampleEnum;
63
- /**
64
- * @generated from field: string example_string = 4 [packed = false, default = "", jstype = JS_NORMAL];
65
- */
66
- exampleString?: string;
56
+ timestamps?: Timestamp[];
57
+
67
58
  /**
68
- * @generated from field: repeated google.protobuf.Timestamp timestamps = 5 [packed = false, default = , jstype = JS_NORMAL];
59
+ * @generated from oneof example.EchoMsg.demo
69
60
  */
70
- timestamps?: Timestamp[];
61
+ demo?: {
62
+ value?: undefined,
63
+ case: undefined
64
+ } | {
65
+ /**
66
+ * @generated from field: example.ExampleEnum example_enum = 3;
67
+ */
68
+ value: ExampleEnum;
69
+ case: "exampleEnum";
70
+ } | {
71
+ /**
72
+ * @generated from field: string example_string = 4;
73
+ */
74
+ value: string;
75
+ case: "exampleString";
76
+ };
71
77
 
72
78
  }>;
73
79
 
@@ -77,8 +83,8 @@ export const EchoMsg: MessageType<EchoMsg> = createMessageType(
77
83
  fields: [
78
84
  { no: 1, name: "body", kind: "scalar", T: 9 /* ScalarType.STRING */ },
79
85
  { no: 2, name: "ts", kind: "message", T: () => Timestamp },
80
- { no: 3, name: "example_enum", kind: "enum", T: ExampleEnum_Enum },
81
- { no: 4, name: "example_string", kind: "scalar", T: 9 /* ScalarType.STRING */ },
86
+ { no: 3, name: "example_enum", kind: "enum", T: ExampleEnum_Enum, oneof: "demo" },
87
+ { no: 4, name: "example_string", kind: "scalar", T: 9 /* ScalarType.STRING */, oneof: "demo" },
82
88
  { no: 5, name: "timestamps", kind: "message", T: () => Timestamp, repeated: true },
83
89
  ] as readonly PartialFieldInfo[],
84
90
  packedByDefault: true,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aptre/protobuf-es-lite",
3
3
  "description": "Lightweight Protobuf codegen for TypeScript and JavaScript.",
4
- "version": "0.2.11",
4
+ "version": "0.2.12",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "url": "git+ssh://git@github.com/aperturerobotics/protobuf-es-lite.git"
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2020",
4
- "module": "ES2020",
5
- "moduleResolution": "node",
3
+ "target": "es2022",
4
+ "module": "Node16",
5
+ "moduleResolution": "Node16",
6
6
  "esModuleInterop": false,
7
7
  "forceConsistentCasingInFileNames": true,
8
8
  "strict": true,
@@ -1,43 +0,0 @@
1
- import type { AnyMessage, Message, MessageType } from "./message.js";
2
- import type { EnumType } from "./enum.js";
3
- import type { ServiceType } from "./service-type.js";
4
- /**
5
- * IMessageTypeRegistry provides look-up for message types.
6
- *
7
- * You can conveniently create a registry using the createRegistry()
8
- * function:
9
- *
10
- * ```ts
11
- * import { createRegistry } from "@bufbuild/protobuf";
12
- * import { MyMessage, MyOtherMessage } from "./gen/my_message.pb.js";
13
- *
14
- * const reg: IMessageTypeRegistry = createRegistry(
15
- * MyMessage,
16
- * MyOtherMessage,
17
- * );
18
- * ```
19
- */
20
- export interface IMessageTypeRegistry {
21
- /**
22
- * Find a message type by its protobuf type name.
23
- */
24
- findMessage<T extends Message<T> = AnyMessage>(typeName: string): MessageType<T> | undefined;
25
- }
26
- /**
27
- * IEnumTypeRegistry provides look-up for enum types.
28
- */
29
- export interface IEnumTypeRegistry {
30
- /**
31
- * Find an enum type by its protobuf type name.
32
- */
33
- findEnum(typeName: string): EnumType | undefined;
34
- }
35
- /**
36
- * IServiceTypeRegistry provides look-up for service types.
37
- */
38
- export interface IServiceTypeRegistry {
39
- /**
40
- * Find a service type by its protobuf type name.
41
- */
42
- findService(typeName: string): ServiceType | undefined;
43
- }
@@ -1,14 +0,0 @@
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 {};