@bufbuild/protoc-gen-es 0.2.1 → 0.4.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/README.md CHANGED
@@ -121,14 +121,29 @@ Multiple values can be given by separating them with `+`, for example
121
121
  `target=js+dts`.
122
122
 
123
123
  By default, we generate JavaScript and TypeScript declaration files, which
124
- produces the smallest code size. If you prefer to generate TypeScript, use
125
- `target=ts`.
124
+ produces the smallest code size and is the most compatible with various
125
+ bundler configurations. If you prefer to generate TypeScript, use `target=ts`.
126
+
127
+ ### `import_extension=.js`
128
+
129
+ By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
130
+ (and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
131
+ uses a `.js` file extensions in import paths, even in TypeScript files.
132
+
133
+ This is unintuitive, but necessary for [ECMAScript modules in Node.js](https://www.typescriptlang.org/docs/handbook/esm-node.html).
134
+ Unfortunately, not all bundlers and tools have caught up yet, and Deno
135
+ requires `.ts`. With this plugin option, you can replace `.js` extensions
136
+ in import paths with the given value. For example, set
137
+
138
+ - `import_extension=none` to remove the `.js` extension
139
+ - `import_extension=.ts` to replace the `.js` extension with `.ts`
140
+
126
141
 
127
142
  ### `keep_empty_files=true`
128
143
 
129
- By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
130
- (and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
131
- omit empty files from the plugin output. This option disables pruning of
132
- empty files, to allow for smooth interoperation with Bazel and similar
144
+ By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
145
+ (and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
146
+ omit empty files from the plugin output. This option disables pruning of
147
+ empty files, to allow for smooth interoperation with Bazel and similar
133
148
  tooling that requires all output files to be declared ahead of time.
134
149
  Unless you use Bazel, it is very unlikely that you need this option.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.2.1"
23
+ "@bufbuild/protoplugin": "0.4.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.2.1"
26
+ "@bufbuild/protobuf": "0.4.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -122,14 +122,16 @@ function generateWktMethods(schema, f, message) {
122
122
  if (ref === undefined) {
123
123
  return;
124
124
  }
125
- const { Message, MessageType, } = schema.runtime;
125
+ const { Message, MessageType, IMessageTypeRegistry } = schema.runtime;
126
126
  switch (ref.typeName) {
127
127
  case "google.protobuf.Any":
128
128
  f.print(" packFrom(message: ", Message, "): void;");
129
129
  f.print();
130
130
  f.print(" unpackTo(target: ", Message, "): boolean;");
131
131
  f.print();
132
- f.print(" is(type: ", MessageType, "): boolean;");
132
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
133
+ f.print();
134
+ f.print(" is(type: ", MessageType, " | string): boolean;");
133
135
  f.print();
134
136
  f.print(" private typeNameToUrl(name: string): string;");
135
137
  f.print();
@@ -185,7 +187,7 @@ function generateWktStaticMethods(schema, f, message) {
185
187
  case "google.protobuf.BytesValue": {
186
188
  const { typing } = (0, ecmascript_1.getFieldTyping)(ref.value, f);
187
189
  f.print(" static readonly fieldWrapper: {");
188
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, ",");
190
+ f.print(" wrapField(value: ", typing, "): ", message, ",");
189
191
  f.print(" unwrapField(value: ", message, "): ", typing, ",");
190
192
  f.print(" };");
191
193
  f.print();
@@ -213,8 +213,29 @@ function generateWktMethods(schema, f, message) {
213
213
  f.print(" return true;");
214
214
  f.print("};");
215
215
  f.print();
216
+ f.print(message, ".prototype.unpack = function unpack(registry) {");
217
+ f.print(" if (this.", (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
218
+ f.print(" return undefined;");
219
+ f.print(" }");
220
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), "));");
221
+ f.print(" if (!messageType) {");
222
+ f.print(" return undefined;");
223
+ f.print(" }");
224
+ f.print(" return messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
225
+ f.print(" }");
226
+ f.print();
216
227
  f.print(message, ".prototype.is = function is(type) {");
217
- f.print(" return this.", (0, ecmascript_1.localName)(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
228
+ f.print(" if (this.typeUrl === '') {");
229
+ f.print(" return false;");
230
+ f.print(" }");
231
+ f.print(" const name = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
232
+ f.print(" let typeName = '';");
233
+ f.print(" if (typeof type === 'string') {");
234
+ f.print(" typeName = type;");
235
+ f.print(" } else {");
236
+ f.print(" typeName = type.typeName;");
237
+ f.print(" }");
238
+ f.print(" return name === typeName;");
218
239
  f.print("};");
219
240
  f.print();
220
241
  f.print(message, ".prototype.typeNameToUrl = function typeNameToUrl(name) {");
@@ -541,7 +562,7 @@ function generateWktStaticMethods(schema, f, message) {
541
562
  case "google.protobuf.BytesValue": {
542
563
  f.print(message, ".fieldWrapper = {");
543
564
  f.print(" wrapField(value) {");
544
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});");
565
+ f.print(" return new ", message, "({value});");
545
566
  f.print(" },");
546
567
  f.print(" unwrapField(value) {");
547
568
  f.print(" return value.", (0, ecmascript_1.localName)(ref.value), ";");
@@ -152,7 +152,7 @@ function generateWktMethods(schema, f, message) {
152
152
  if (ref === undefined) {
153
153
  return;
154
154
  }
155
- const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, ScalarType: rtScalarType, protoInt64, } = schema.runtime;
155
+ const { Message, JsonValue, JsonReadOptions, JsonWriteOptions, JsonObject, MessageType, IMessageTypeRegistry, ScalarType: rtScalarType, protoInt64, } = schema.runtime;
156
156
  const protoN = schema.runtime[message.file.syntax];
157
157
  switch (ref.typeName) {
158
158
  case "google.protobuf.Any":
@@ -214,8 +214,29 @@ function generateWktMethods(schema, f, message) {
214
214
  f.print(" return true;");
215
215
  f.print(" }");
216
216
  f.print();
217
- f.print(" is(type: ", MessageType, "): boolean {");
218
- f.print(" return this.", (0, ecmascript_1.localName)(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
217
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): ", Message, " | undefined {");
218
+ f.print(" if (this.", (0, ecmascript_1.localName)(ref.typeUrl), ` === "") {`);
219
+ f.print(" return undefined;");
220
+ f.print(" }");
221
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), "));");
222
+ f.print(" if (!messageType) {");
223
+ f.print(" return undefined;");
224
+ f.print(" }");
225
+ f.print(" return messageType.fromBinary(this.", (0, ecmascript_1.localName)(ref.value), ");");
226
+ f.print(" }");
227
+ f.print();
228
+ f.print(" is(type: ", MessageType, " | string): boolean {");
229
+ f.print(" if (this.typeUrl === '') {");
230
+ f.print(" return false;");
231
+ f.print(" }");
232
+ f.print(" const name = this.typeUrlToName(this.", (0, ecmascript_1.localName)(ref.typeUrl), ");");
233
+ f.print(" let typeName = '';");
234
+ f.print(" if (typeof type === 'string') {");
235
+ f.print(" typeName = type;");
236
+ f.print(" } else {");
237
+ f.print(" typeName = type.typeName;");
238
+ f.print(" }");
239
+ f.print(" return name === typeName;");
219
240
  f.print(" }");
220
241
  f.print();
221
242
  f.print(" private typeNameToUrl(name: string): string {");
@@ -542,8 +563,8 @@ function generateWktStaticMethods(schema, f, message) {
542
563
  case "google.protobuf.BytesValue": {
543
564
  const { typing } = (0, ecmascript_1.getFieldTyping)(ref.value, f);
544
565
  f.print(" static readonly fieldWrapper = {");
545
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, " {");
546
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});");
566
+ f.print(" wrapField(value: ", typing, "): ", message, " {");
567
+ f.print(" return new ", message, "({value});");
547
568
  f.print(" },");
548
569
  f.print(" unwrapField(value: ", message, "): ", typing, " {");
549
570
  f.print(" return value.", (0, ecmascript_1.localName)(ref.value), ";");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.2.1"
23
+ "@bufbuild/protoplugin": "0.4.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.2.1"
26
+ "@bufbuild/protobuf": "0.4.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -152,6 +152,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
152
152
  const {
153
153
  Message,
154
154
  MessageType,
155
+ IMessageTypeRegistry
155
156
  } = schema.runtime;
156
157
  switch (ref.typeName) {
157
158
  case "google.protobuf.Any":
@@ -159,7 +160,9 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
159
160
  f.print();
160
161
  f.print(" unpackTo(target: ", Message, "): boolean;");
161
162
  f.print();
162
- f.print(" is(type: ", MessageType, "): boolean;");
163
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): Message | undefined;");
164
+ f.print();
165
+ f.print(" is(type: ", MessageType, " | string): boolean;");
163
166
  f.print();
164
167
  f.print(" private typeNameToUrl(name: string): string;");
165
168
  f.print();
@@ -216,7 +219,7 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
216
219
  case "google.protobuf.BytesValue": {
217
220
  const {typing} = getFieldTyping(ref.value, f);
218
221
  f.print(" static readonly fieldWrapper: {")
219
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, ",")
222
+ f.print(" wrapField(value: ", typing, "): ", message, ",")
220
223
  f.print(" unwrapField(value: ", message, "): ", typing, ",")
221
224
  f.print(" };")
222
225
  f.print()
package/src/javascript.ts CHANGED
@@ -226,8 +226,29 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
226
226
  f.print(" return true;");
227
227
  f.print("};");
228
228
  f.print();
229
+ f.print(message, ".prototype.unpack = function unpack(registry) {")
230
+ f.print(" if (this.", localName(ref.typeUrl), ` === "") {`);
231
+ f.print(" return undefined;");
232
+ f.print(" }");
233
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", localName(ref.typeUrl), "));");
234
+ f.print(" if (!messageType) {");
235
+ f.print(" return undefined;");
236
+ f.print(" }");
237
+ f.print(" return messageType.fromBinary(this.", localName(ref.value), ");");
238
+ f.print(" }");
239
+ f.print();
229
240
  f.print(message, ".prototype.is = function is(type) {")
230
- f.print(" return this.", localName(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
241
+ f.print(" if (this.typeUrl === '') {");
242
+ f.print(" return false;");
243
+ f.print(" }");
244
+ f.print(" const name = this.typeUrlToName(this.", localName(ref.typeUrl), ");");
245
+ f.print(" let typeName = '';");
246
+ f.print(" if (typeof type === 'string') {");
247
+ f.print(" typeName = type;");
248
+ f.print(" } else {");
249
+ f.print(" typeName = type.typeName;");
250
+ f.print(" }");
251
+ f.print(" return name === typeName;");
231
252
  f.print("};");
232
253
  f.print();
233
254
  f.print(message, ".prototype.typeNameToUrl = function typeNameToUrl(name) {")
@@ -557,7 +578,7 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
557
578
  case "google.protobuf.BytesValue": {
558
579
  f.print(message, ".fieldWrapper = {")
559
580
  f.print(" wrapField(value) {")
560
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});")
581
+ f.print(" return new ", message, "({value});")
561
582
  f.print(" },")
562
583
  f.print(" unwrapField(value) {")
563
584
  f.print(" return value.", localName(ref.value), ";")
package/src/typescript.ts CHANGED
@@ -186,6 +186,7 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
186
186
  JsonWriteOptions,
187
187
  JsonObject,
188
188
  MessageType,
189
+ IMessageTypeRegistry,
189
190
  ScalarType: rtScalarType,
190
191
  protoInt64,
191
192
  } = schema.runtime;
@@ -250,8 +251,29 @@ function generateWktMethods(schema: Schema, f: GeneratedFile, message: DescMessa
250
251
  f.print(" return true;");
251
252
  f.print(" }");
252
253
  f.print();
253
- f.print(" is(type: ", MessageType, "): boolean {");
254
- f.print(" return this.", localName(ref.typeUrl), " === this.typeNameToUrl(type.typeName);");
254
+ f.print(" unpack(registry: ", IMessageTypeRegistry, "): ", Message, " | undefined {");
255
+ f.print(" if (this.", localName(ref.typeUrl), ` === "") {`);
256
+ f.print(" return undefined;");
257
+ f.print(" }");
258
+ f.print(" const messageType = registry.findMessage(this.typeUrlToName(this.", localName(ref.typeUrl), "));");
259
+ f.print(" if (!messageType) {");
260
+ f.print(" return undefined;");
261
+ f.print(" }");
262
+ f.print(" return messageType.fromBinary(this.", localName(ref.value), ");");
263
+ f.print(" }");
264
+ f.print();
265
+ f.print(" is(type: ", MessageType, " | string): boolean {");
266
+ f.print(" if (this.typeUrl === '') {");
267
+ f.print(" return false;");
268
+ f.print(" }");
269
+ f.print(" const name = this.typeUrlToName(this.", localName(ref.typeUrl), ");");
270
+ f.print(" let typeName = '';");
271
+ f.print(" if (typeof type === 'string') {");
272
+ f.print(" typeName = type;");
273
+ f.print(" } else {");
274
+ f.print(" typeName = type.typeName;");
275
+ f.print(" }");
276
+ f.print(" return name === typeName;");
255
277
  f.print(" }");
256
278
  f.print();
257
279
  f.print(" private typeNameToUrl(name: string): string {");
@@ -581,8 +603,8 @@ function generateWktStaticMethods(schema: Schema, f: GeneratedFile, message: Des
581
603
  case "google.protobuf.BytesValue": {
582
604
  const {typing} = getFieldTyping(ref.value, f);
583
605
  f.print(" static readonly fieldWrapper = {")
584
- f.print(" wrapField(value: ", typing, " | ", message, "): ", message, " {")
585
- f.print(" return value instanceof ", message, " ? value : new ", message, "({value});")
606
+ f.print(" wrapField(value: ", typing, "): ", message, " {")
607
+ f.print(" return new ", message, "({value});")
586
608
  f.print(" },")
587
609
  f.print(" unwrapField(value: ", message, "): ", typing, " {")
588
610
  f.print(" return value.", localName(ref.value), ";")