@bufbuild/protoc-gen-es 0.1.0 → 0.2.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 +74 -26
- package/dist/cjs/package.json +3 -3
- package/dist/cjs/src/declaration.js +15 -7
- package/dist/cjs/src/javascript.js +20 -12
- package/dist/cjs/src/match-wkt.js +16 -16
- package/dist/cjs/src/protoc-gen-es-plugin.js +4 -16
- package/dist/cjs/src/typescript.js +15 -7
- package/package.json +3 -3
- package/src/declaration.ts +19 -8
- package/src/javascript.ts +24 -13
- package/src/match-wkt.ts +30 -30
- package/src/protoc-gen-es-plugin.ts +10 -27
- package/src/typescript.ts +19 -8
package/README.md
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
# @bufbuild/protoc-gen-es
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
## Protocol Buffers for ECMAScript
|
|
7
|
-
|
|
8
|
-
A complete implementation of [Protocol Buffers](https://developers.google.com/protocol-buffers)
|
|
9
|
-
in TypeScript, suitable for web browsers and Node.js.
|
|
10
|
-
|
|
11
|
-
For example, the following definition:
|
|
3
|
+
The code generator for Protocol Buffers for ECMAScript. For example, the
|
|
4
|
+
following definition:
|
|
12
5
|
|
|
13
6
|
```protobuf
|
|
14
7
|
message Person {
|
|
@@ -31,45 +24,92 @@ pete = Person.fromBinary(bytes);
|
|
|
31
24
|
pete = Person.fromJsonString('{"name": "pete", "id": 123}');
|
|
32
25
|
```
|
|
33
26
|
|
|
34
|
-
Learn more at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es).
|
|
27
|
+
Learn more about the project at [github.com/bufbuild/protobuf-es](https://github.com/bufbuild/protobuf-es).
|
|
35
28
|
|
|
36
29
|
|
|
37
30
|
## Installation
|
|
38
31
|
|
|
39
|
-
|
|
32
|
+
`protoc-gen-es` is a code generator plugin for Protocol Buffer compilers,
|
|
33
|
+
like [buf](https://github.com/bufbuild/buf) and [protoc](https://github.com/protocolbuffers/protobuf/releases).
|
|
34
|
+
It generates base types - messages and enumerations - from your Protocol Buffer
|
|
35
|
+
schema. The generated code requires the runtime library [@bufbuild/protobuf](https://www.npmjs.com/package/@bufbuild/protobuf).
|
|
36
|
+
|
|
37
|
+
To install the plugin and the runtime library, run:
|
|
40
38
|
|
|
41
39
|
```shell
|
|
42
|
-
npm install @bufbuild/protoc-gen-es
|
|
40
|
+
npm install --save-dev @bufbuild/protoc-gen-es
|
|
41
|
+
npm install @bufbuild/protobuf
|
|
43
42
|
```
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
We use peer dependencies to ensure that code generator and runtime library are
|
|
45
|
+
compatible with each other. Note that yarn and pnpm only emit a warning in this case.
|
|
47
46
|
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
|
|
48
|
+
## Generating code
|
|
49
|
+
|
|
50
|
+
### With buf
|
|
51
|
+
|
|
52
|
+
Add a new configuration file `buf.gen.yaml`:
|
|
53
|
+
|
|
54
|
+
```yaml
|
|
55
|
+
# buf.gen.yaml defines a local generation template.
|
|
56
|
+
# For details, see https://docs.buf.build/configuration/v1/buf-gen-yaml
|
|
57
|
+
version: v1
|
|
58
|
+
plugins:
|
|
59
|
+
# This will invoke protoc-gen-es and write output to src/gen
|
|
60
|
+
- name: es
|
|
61
|
+
out: src/gen
|
|
62
|
+
opt: target=ts
|
|
50
63
|
```
|
|
51
64
|
|
|
52
|
-
|
|
65
|
+
Add the following script to your `package.json`:
|
|
53
66
|
|
|
54
|
-
```
|
|
55
|
-
|
|
67
|
+
```json
|
|
68
|
+
{
|
|
69
|
+
"name": "your-package",
|
|
70
|
+
"version": "1.0.0",
|
|
71
|
+
"scripts": {
|
|
72
|
+
"generate": "buf generate"
|
|
73
|
+
},
|
|
74
|
+
// ...
|
|
75
|
+
}
|
|
56
76
|
```
|
|
57
77
|
|
|
58
|
-
|
|
59
|
-
where yarn stores the executable, run `yarn bin protoc-gen-es` (it is "unplugged"
|
|
60
|
-
automatically).
|
|
78
|
+
To generate code for all protobuf files within your project, simply run:
|
|
61
79
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
80
|
+
```bash
|
|
81
|
+
npm run generate
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Note that `buf` can generate from various [inputs](https://docs.buf.build/reference/inputs),
|
|
85
|
+
not just local protobuf files.
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### With protoc
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
PATH=$PATH:$(pwd)/node_modules/.bin \
|
|
92
|
+
protoc -I . \
|
|
93
|
+
--es_out src/gen \
|
|
94
|
+
--es_opt target=ts \
|
|
95
|
+
a.proto b.proto c.proto
|
|
65
96
|
```
|
|
66
97
|
|
|
98
|
+
Note that we are adding `node_modules/.bin` to the `$PATH`, so that the protocol
|
|
99
|
+
buffer compiler can find them. This happens automatically with npm scripts.
|
|
100
|
+
|
|
101
|
+
Since yarn v2 and above does not use a `node_modules` directory, you need to
|
|
102
|
+
change the variable a bit:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
PATH=$(dirname $(yarn bin protoc-gen-es)):$PATH
|
|
106
|
+
```
|
|
67
107
|
|
|
68
108
|
## Plugin options
|
|
69
109
|
|
|
70
110
|
### `target`
|
|
71
111
|
|
|
72
|
-
This option controls whether the plugin generates JavaScript, TypeScript,
|
|
112
|
+
This option controls whether the plugin generates JavaScript, TypeScript,
|
|
73
113
|
or TypeScript declaration files.
|
|
74
114
|
|
|
75
115
|
Possible values:
|
|
@@ -84,3 +124,11 @@ By default, we generate JavaScript and TypeScript declaration files, which
|
|
|
84
124
|
produces the smallest code size. If you prefer to generate TypeScript, use
|
|
85
125
|
`target=ts`.
|
|
86
126
|
|
|
127
|
+
### `keep_empty_files=true`
|
|
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
|
+
omit empty files from the plugin output. This option disables pruning of
|
|
132
|
+
empty files, to allow for smooth interoperation with Bazel and similar
|
|
133
|
+
tooling that requires all output files to be declared ahead of time.
|
|
134
|
+
Unless you use Bazel, it is very unlikely that you need this option.
|
package/dist/cjs/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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.
|
|
23
|
+
"@bufbuild/protoplugin": "0.2.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@bufbuild/protobuf": "0.
|
|
26
|
+
"@bufbuild/protobuf": "0.2.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependenciesMeta": {
|
|
29
29
|
"@bufbuild/protobuf": {
|
|
@@ -13,15 +13,23 @@
|
|
|
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.
|
|
16
|
+
exports.generateDts = void 0;
|
|
17
17
|
const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
|
|
18
18
|
const match_wkt_js_1 = require("./match-wkt.js");
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
function generateDts(schema) {
|
|
20
|
+
for (const file of schema.files) {
|
|
21
|
+
const f = schema.generateFile(file.name + "_pb.d.ts");
|
|
22
|
+
f.preamble(file);
|
|
23
|
+
for (const enumeration of file.enums) {
|
|
24
|
+
generateEnum(schema, f, enumeration);
|
|
25
|
+
}
|
|
26
|
+
for (const message of file.messages) {
|
|
27
|
+
generateMessage(schema, f, message);
|
|
28
|
+
}
|
|
29
|
+
// We do not generate anything for services, and we do not support extensions at this time
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.generateDts = generateDts;
|
|
25
33
|
// prettier-ignore
|
|
26
34
|
function generateEnum(schema, f, enumeration) {
|
|
27
35
|
f.print((0, ecmascript_1.makeJsDoc)(enumeration));
|
|
@@ -13,16 +13,24 @@
|
|
|
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.generateFieldInfo = exports.
|
|
16
|
+
exports.generateFieldInfo = exports.generateJs = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
18
|
const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
|
|
19
19
|
const match_wkt_js_1 = require("./match-wkt.js");
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
function generateJs(schema) {
|
|
21
|
+
for (const file of schema.files) {
|
|
22
|
+
const f = schema.generateFile(file.name + "_pb.js");
|
|
23
|
+
f.preamble(file);
|
|
24
|
+
for (const enumeration of file.enums) {
|
|
25
|
+
generateEnum(schema, f, enumeration);
|
|
26
|
+
}
|
|
27
|
+
for (const message of file.messages) {
|
|
28
|
+
generateMessage(schema, f, message);
|
|
29
|
+
}
|
|
30
|
+
// We do not generate anything for services, and we do not support extensions at this time
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.generateJs = generateJs;
|
|
26
34
|
// prettier-ignore
|
|
27
35
|
function generateEnum(schema, f, enumeration) {
|
|
28
36
|
const protoN = schema.runtime[enumeration.file.syntax];
|
|
@@ -88,11 +96,11 @@ function generateFieldInfo(schema, f, field) {
|
|
|
88
96
|
if (field.jsonName !== undefined) {
|
|
89
97
|
e.push(`jsonName: "`, field.jsonName, `", `);
|
|
90
98
|
}
|
|
91
|
-
switch (field.
|
|
92
|
-
case "
|
|
99
|
+
switch (field.fieldKind) {
|
|
100
|
+
case "scalar":
|
|
93
101
|
e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
|
|
94
102
|
break;
|
|
95
|
-
case "
|
|
103
|
+
case "map":
|
|
96
104
|
e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
|
|
97
105
|
switch (field.mapValue.kind) {
|
|
98
106
|
case "scalar":
|
|
@@ -106,10 +114,10 @@ function generateFieldInfo(schema, f, field) {
|
|
|
106
114
|
break;
|
|
107
115
|
}
|
|
108
116
|
break;
|
|
109
|
-
case "
|
|
117
|
+
case "message":
|
|
110
118
|
e.push(`kind: "message", T: `, field.message, `, `);
|
|
111
119
|
break;
|
|
112
|
-
case "
|
|
120
|
+
case "enum":
|
|
113
121
|
e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
|
|
114
122
|
break;
|
|
115
123
|
}
|
|
@@ -19,10 +19,10 @@ function matchWkt(message) {
|
|
|
19
19
|
switch (message.typeName) {
|
|
20
20
|
case "google.protobuf.Any": {
|
|
21
21
|
const typeUrl = message.fields.find((f) => f.number == 1 &&
|
|
22
|
-
f.
|
|
22
|
+
f.fieldKind == "scalar" &&
|
|
23
23
|
f.scalar === protobuf_1.ScalarType.STRING);
|
|
24
24
|
const value = message.fields.find((f) => f.number == 2 &&
|
|
25
|
-
f.
|
|
25
|
+
f.fieldKind == "scalar" &&
|
|
26
26
|
f.scalar === protobuf_1.ScalarType.BYTES);
|
|
27
27
|
if (typeUrl && value) {
|
|
28
28
|
return {
|
|
@@ -35,10 +35,10 @@ function matchWkt(message) {
|
|
|
35
35
|
}
|
|
36
36
|
case "google.protobuf.Timestamp": {
|
|
37
37
|
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
38
|
-
f.
|
|
38
|
+
f.fieldKind == "scalar" &&
|
|
39
39
|
f.scalar === protobuf_1.ScalarType.INT64);
|
|
40
40
|
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
41
|
-
f.
|
|
41
|
+
f.fieldKind == "scalar" &&
|
|
42
42
|
f.scalar === protobuf_1.ScalarType.INT32);
|
|
43
43
|
if (seconds && nanos) {
|
|
44
44
|
return {
|
|
@@ -51,10 +51,10 @@ function matchWkt(message) {
|
|
|
51
51
|
}
|
|
52
52
|
case "google.protobuf.Duration": {
|
|
53
53
|
const seconds = message.fields.find((f) => f.number == 1 &&
|
|
54
|
-
f.
|
|
54
|
+
f.fieldKind == "scalar" &&
|
|
55
55
|
f.scalar === protobuf_1.ScalarType.INT64);
|
|
56
56
|
const nanos = message.fields.find((f) => f.number == 2 &&
|
|
57
|
-
f.
|
|
57
|
+
f.fieldKind == "scalar" &&
|
|
58
58
|
f.scalar === protobuf_1.ScalarType.INT32);
|
|
59
59
|
if (seconds && nanos) {
|
|
60
60
|
return {
|
|
@@ -67,7 +67,7 @@ function matchWkt(message) {
|
|
|
67
67
|
}
|
|
68
68
|
case "google.protobuf.Struct": {
|
|
69
69
|
const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
|
|
70
|
-
if ((fields === null || fields === void 0 ? void 0 : fields.
|
|
70
|
+
if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
|
|
71
71
|
fields.mapValue.kind !== "message" ||
|
|
72
72
|
fields.mapValue.message.typeName !== "google.protobuf.Value") {
|
|
73
73
|
break;
|
|
@@ -77,29 +77,29 @@ function matchWkt(message) {
|
|
|
77
77
|
case "google.protobuf.Value": {
|
|
78
78
|
const kind = message.oneofs.find((o) => o.name === "kind");
|
|
79
79
|
const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
|
|
80
|
-
if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.
|
|
80
|
+
if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
|
|
81
81
|
nullValue.enum.typeName !== "google.protobuf.NullValue") {
|
|
82
82
|
return undefined;
|
|
83
83
|
}
|
|
84
84
|
const numberValue = message.fields.find((f) => f.number == 2 &&
|
|
85
|
-
f.
|
|
85
|
+
f.fieldKind == "scalar" &&
|
|
86
86
|
f.scalar === protobuf_1.ScalarType.DOUBLE &&
|
|
87
87
|
f.oneof === kind);
|
|
88
88
|
const stringValue = message.fields.find((f) => f.number == 3 &&
|
|
89
|
-
f.
|
|
89
|
+
f.fieldKind == "scalar" &&
|
|
90
90
|
f.scalar === protobuf_1.ScalarType.STRING &&
|
|
91
91
|
f.oneof === kind);
|
|
92
92
|
const boolValue = message.fields.find((f) => f.number == 4 &&
|
|
93
|
-
f.
|
|
93
|
+
f.fieldKind == "scalar" &&
|
|
94
94
|
f.scalar === protobuf_1.ScalarType.BOOL &&
|
|
95
95
|
f.oneof === kind);
|
|
96
96
|
const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
|
|
97
|
-
if ((structValue === null || structValue === void 0 ? void 0 : structValue.
|
|
97
|
+
if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
|
|
98
98
|
structValue.message.typeName !== "google.protobuf.Struct") {
|
|
99
99
|
return undefined;
|
|
100
100
|
}
|
|
101
101
|
const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
|
|
102
|
-
if ((listValue === null || listValue === void 0 ? void 0 : listValue.
|
|
102
|
+
if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
|
|
103
103
|
listValue.message.typeName !== "google.protobuf.ListValue") {
|
|
104
104
|
return undefined;
|
|
105
105
|
}
|
|
@@ -119,7 +119,7 @@ function matchWkt(message) {
|
|
|
119
119
|
}
|
|
120
120
|
case "google.protobuf.ListValue": {
|
|
121
121
|
const values = message.fields.find((f) => f.number == 1 && f.repeated);
|
|
122
|
-
if ((values === null || values === void 0 ? void 0 : values.
|
|
122
|
+
if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
|
|
123
123
|
values.message.typeName !== "google.protobuf.Value") {
|
|
124
124
|
break;
|
|
125
125
|
}
|
|
@@ -127,7 +127,7 @@ function matchWkt(message) {
|
|
|
127
127
|
}
|
|
128
128
|
case "google.protobuf.FieldMask": {
|
|
129
129
|
const paths = message.fields.find((f) => f.number == 1 &&
|
|
130
|
-
f.
|
|
130
|
+
f.fieldKind == "scalar" &&
|
|
131
131
|
f.scalar === protobuf_1.ScalarType.STRING &&
|
|
132
132
|
f.repeated);
|
|
133
133
|
if (paths) {
|
|
@@ -148,7 +148,7 @@ function matchWkt(message) {
|
|
|
148
148
|
if (!value) {
|
|
149
149
|
break;
|
|
150
150
|
}
|
|
151
|
-
if (value.
|
|
151
|
+
if (value.fieldKind !== "scalar") {
|
|
152
152
|
break;
|
|
153
153
|
}
|
|
154
154
|
return { typeName: message.typeName, value };
|
|
@@ -17,24 +17,12 @@ exports.protocGenEs = void 0;
|
|
|
17
17
|
const protoplugin_1 = require("@bufbuild/protoplugin");
|
|
18
18
|
const typescript_js_1 = require("./typescript.js");
|
|
19
19
|
const javascript_js_1 = require("./javascript.js");
|
|
20
|
-
const
|
|
20
|
+
const declaration_js_1 = require("./declaration.js");
|
|
21
21
|
const package_json_1 = require("../package.json");
|
|
22
22
|
exports.protocGenEs = (0, protoplugin_1.createEcmaScriptPlugin)({
|
|
23
23
|
name: "protoc-gen-es",
|
|
24
24
|
version: `v${String(package_json_1.version)}`,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
for (const file of schema.files) {
|
|
29
|
-
const f = schema.generateFile(file.name + target.extension);
|
|
30
|
-
f.preamble(file);
|
|
31
|
-
for (const enumeration of file.enums) {
|
|
32
|
-
target.generateEnum(schema, f, enumeration);
|
|
33
|
-
}
|
|
34
|
-
for (const message of file.messages) {
|
|
35
|
-
target.generateMessage(schema, f, message);
|
|
36
|
-
}
|
|
37
|
-
// We do not generate anything for services, and we do not support extensions at this time
|
|
38
|
-
}
|
|
39
|
-
}
|
|
25
|
+
generateTs: typescript_js_1.generateTs,
|
|
26
|
+
generateJs: javascript_js_1.generateJs,
|
|
27
|
+
generateDts: declaration_js_1.generateDts,
|
|
40
28
|
});
|
|
@@ -13,18 +13,26 @@
|
|
|
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.
|
|
16
|
+
exports.generateTs = void 0;
|
|
17
17
|
const protobuf_1 = require("@bufbuild/protobuf");
|
|
18
18
|
const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
|
|
19
19
|
const match_wkt_js_1 = require("./match-wkt.js");
|
|
20
20
|
const javascript_js_1 = require("./javascript.js");
|
|
21
21
|
const ecmascript_2 = require("@bufbuild/protoplugin/ecmascript");
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
function generateTs(schema) {
|
|
23
|
+
for (const file of schema.files) {
|
|
24
|
+
const f = schema.generateFile(file.name + "_pb.ts");
|
|
25
|
+
f.preamble(file);
|
|
26
|
+
for (const enumeration of file.enums) {
|
|
27
|
+
generateEnum(schema, f, enumeration);
|
|
28
|
+
}
|
|
29
|
+
for (const message of file.messages) {
|
|
30
|
+
generateMessage(schema, f, message);
|
|
31
|
+
}
|
|
32
|
+
// We do not generate anything for services, and we do not support extensions at this time
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.generateTs = generateTs;
|
|
28
36
|
// prettier-ignore
|
|
29
37
|
function generateEnum(schema, f, enumeration) {
|
|
30
38
|
const protoN = schema.runtime[enumeration.file.syntax];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bufbuild/protoc-gen-es",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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.
|
|
23
|
+
"@bufbuild/protoplugin": "0.2.0"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@bufbuild/protobuf": "0.
|
|
26
|
+
"@bufbuild/protobuf": "0.2.0"
|
|
27
27
|
},
|
|
28
28
|
"peerDependenciesMeta": {
|
|
29
29
|
"@bufbuild/protobuf": {
|
package/src/declaration.ts
CHANGED
|
@@ -18,7 +18,11 @@ import type {
|
|
|
18
18
|
DescMessage,
|
|
19
19
|
DescOneof,
|
|
20
20
|
} from "@bufbuild/protobuf";
|
|
21
|
-
import type {
|
|
21
|
+
import type {
|
|
22
|
+
GeneratedFile,
|
|
23
|
+
Printable,
|
|
24
|
+
Schema,
|
|
25
|
+
} from "@bufbuild/protoplugin/ecmascript";
|
|
22
26
|
import {
|
|
23
27
|
getFieldTyping,
|
|
24
28
|
literalString,
|
|
@@ -27,12 +31,19 @@ import {
|
|
|
27
31
|
} from "@bufbuild/protoplugin/ecmascript";
|
|
28
32
|
import { matchWkt } from "./match-wkt.js";
|
|
29
33
|
|
|
30
|
-
export
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
export function generateDts(schema: Schema) {
|
|
35
|
+
for (const file of schema.files) {
|
|
36
|
+
const f = schema.generateFile(file.name + "_pb.d.ts");
|
|
37
|
+
f.preamble(file);
|
|
38
|
+
for (const enumeration of file.enums) {
|
|
39
|
+
generateEnum(schema, f, enumeration);
|
|
40
|
+
}
|
|
41
|
+
for (const message of file.messages) {
|
|
42
|
+
generateMessage(schema, f, message);
|
|
43
|
+
}
|
|
44
|
+
// We do not generate anything for services, and we do not support extensions at this time
|
|
45
|
+
}
|
|
46
|
+
}
|
|
36
47
|
|
|
37
48
|
// prettier-ignore
|
|
38
49
|
function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
|
|
@@ -120,7 +131,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
|
|
|
120
131
|
|
|
121
132
|
function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
|
|
122
133
|
f.print(makeJsDoc(field, " "));
|
|
123
|
-
const e:
|
|
134
|
+
const e: Printable = [];
|
|
124
135
|
e.push(" ", localName(field));
|
|
125
136
|
const { typing, optional } = getFieldTyping(field, f);
|
|
126
137
|
if (optional) {
|
package/src/javascript.ts
CHANGED
|
@@ -14,7 +14,11 @@
|
|
|
14
14
|
|
|
15
15
|
import type { DescEnum, DescField, DescMessage } from "@bufbuild/protobuf";
|
|
16
16
|
import { proto2, proto3, ScalarType } from "@bufbuild/protobuf";
|
|
17
|
-
import type {
|
|
17
|
+
import type {
|
|
18
|
+
GeneratedFile,
|
|
19
|
+
Printable,
|
|
20
|
+
Schema,
|
|
21
|
+
} from "@bufbuild/protoplugin/ecmascript";
|
|
18
22
|
import {
|
|
19
23
|
getFieldExplicitDefaultValue,
|
|
20
24
|
literalString,
|
|
@@ -23,12 +27,19 @@ import {
|
|
|
23
27
|
} from "@bufbuild/protoplugin/ecmascript";
|
|
24
28
|
import { matchWkt } from "./match-wkt.js";
|
|
25
29
|
|
|
26
|
-
export
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
export function generateJs(schema: Schema) {
|
|
31
|
+
for (const file of schema.files) {
|
|
32
|
+
const f = schema.generateFile(file.name + "_pb.js");
|
|
33
|
+
f.preamble(file);
|
|
34
|
+
for (const enumeration of file.enums) {
|
|
35
|
+
generateEnum(schema, f, enumeration);
|
|
36
|
+
}
|
|
37
|
+
for (const message of file.messages) {
|
|
38
|
+
generateMessage(schema, f, message);
|
|
39
|
+
}
|
|
40
|
+
// We do not generate anything for services, and we do not support extensions at this time
|
|
41
|
+
}
|
|
42
|
+
}
|
|
32
43
|
|
|
33
44
|
// prettier-ignore
|
|
34
45
|
function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
|
|
@@ -90,16 +101,16 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
|
|
|
90
101
|
// prettier-ignore
|
|
91
102
|
export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescField) {
|
|
92
103
|
const protoN = schema.runtime[field.parent.file.syntax];
|
|
93
|
-
const e:
|
|
104
|
+
const e: Printable = [];
|
|
94
105
|
e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
|
|
95
106
|
if (field.jsonName !== undefined) {
|
|
96
107
|
e.push(`jsonName: "`, field.jsonName, `", `);
|
|
97
108
|
}
|
|
98
|
-
switch (field.
|
|
99
|
-
case "
|
|
109
|
+
switch (field.fieldKind) {
|
|
110
|
+
case "scalar":
|
|
100
111
|
e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, ScalarType[field.scalar], ` */, `);
|
|
101
112
|
break;
|
|
102
|
-
case "
|
|
113
|
+
case "map":
|
|
103
114
|
e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, ScalarType[field.mapKey], ` */, `);
|
|
104
115
|
switch (field.mapValue.kind) {
|
|
105
116
|
case "scalar":
|
|
@@ -113,10 +124,10 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
|
|
|
113
124
|
break;
|
|
114
125
|
}
|
|
115
126
|
break;
|
|
116
|
-
case "
|
|
127
|
+
case "message":
|
|
117
128
|
e.push(`kind: "message", T: `, field.message, `, `);
|
|
118
129
|
break;
|
|
119
|
-
case "
|
|
130
|
+
case "enum":
|
|
120
131
|
e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
|
|
121
132
|
break;
|
|
122
133
|
}
|
package/src/match-wkt.ts
CHANGED
|
@@ -37,21 +37,21 @@ type DescWkt =
|
|
|
37
37
|
}
|
|
38
38
|
| {
|
|
39
39
|
typeName: "google.protobuf.Struct";
|
|
40
|
-
fields: DescField & {
|
|
40
|
+
fields: DescField & { fieldKind: "map" };
|
|
41
41
|
}
|
|
42
42
|
| {
|
|
43
43
|
typeName: "google.protobuf.Value";
|
|
44
44
|
kind: DescOneof;
|
|
45
|
-
nullValue: DescField & {
|
|
45
|
+
nullValue: DescField & { fieldKind: "enum" };
|
|
46
46
|
numberValue: DescField;
|
|
47
47
|
stringValue: DescField;
|
|
48
48
|
boolValue: DescField;
|
|
49
|
-
structValue: DescField & {
|
|
50
|
-
listValue: DescField & {
|
|
49
|
+
structValue: DescField & { fieldKind: "message" };
|
|
50
|
+
listValue: DescField & { fieldKind: "message" };
|
|
51
51
|
}
|
|
52
52
|
| {
|
|
53
53
|
typeName: "google.protobuf.ListValue";
|
|
54
|
-
values: DescField & {
|
|
54
|
+
values: DescField & { fieldKind: "message" };
|
|
55
55
|
}
|
|
56
56
|
| {
|
|
57
57
|
typeName: "google.protobuf.FieldMask";
|
|
@@ -59,39 +59,39 @@ type DescWkt =
|
|
|
59
59
|
}
|
|
60
60
|
| {
|
|
61
61
|
typeName: "google.protobuf.DoubleValue";
|
|
62
|
-
value: DescField & {
|
|
62
|
+
value: DescField & { fieldKind: "scalar" };
|
|
63
63
|
}
|
|
64
64
|
| {
|
|
65
65
|
typeName: "google.protobuf.FloatValue";
|
|
66
|
-
value: DescField & {
|
|
66
|
+
value: DescField & { fieldKind: "scalar" };
|
|
67
67
|
}
|
|
68
68
|
| {
|
|
69
69
|
typeName: "google.protobuf.Int64Value";
|
|
70
|
-
value: DescField & {
|
|
70
|
+
value: DescField & { fieldKind: "scalar" };
|
|
71
71
|
}
|
|
72
72
|
| {
|
|
73
73
|
typeName: "google.protobuf.UInt64Value";
|
|
74
|
-
value: DescField & {
|
|
74
|
+
value: DescField & { fieldKind: "scalar" };
|
|
75
75
|
}
|
|
76
76
|
| {
|
|
77
77
|
typeName: "google.protobuf.Int32Value";
|
|
78
|
-
value: DescField & {
|
|
78
|
+
value: DescField & { fieldKind: "scalar" };
|
|
79
79
|
}
|
|
80
80
|
| {
|
|
81
81
|
typeName: "google.protobuf.UInt32Value";
|
|
82
|
-
value: DescField & {
|
|
82
|
+
value: DescField & { fieldKind: "scalar" };
|
|
83
83
|
}
|
|
84
84
|
| {
|
|
85
85
|
typeName: "google.protobuf.BoolValue";
|
|
86
|
-
value: DescField & {
|
|
86
|
+
value: DescField & { fieldKind: "scalar" };
|
|
87
87
|
}
|
|
88
88
|
| {
|
|
89
89
|
typeName: "google.protobuf.StringValue";
|
|
90
|
-
value: DescField & {
|
|
90
|
+
value: DescField & { fieldKind: "scalar" };
|
|
91
91
|
}
|
|
92
92
|
| {
|
|
93
93
|
typeName: "google.protobuf.BytesValue";
|
|
94
|
-
value: DescField & {
|
|
94
|
+
value: DescField & { fieldKind: "scalar" };
|
|
95
95
|
};
|
|
96
96
|
|
|
97
97
|
export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
@@ -100,13 +100,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
100
100
|
const typeUrl = message.fields.find(
|
|
101
101
|
(f) =>
|
|
102
102
|
f.number == 1 &&
|
|
103
|
-
f.
|
|
103
|
+
f.fieldKind == "scalar" &&
|
|
104
104
|
f.scalar === ScalarType.STRING
|
|
105
105
|
);
|
|
106
106
|
const value = message.fields.find(
|
|
107
107
|
(f) =>
|
|
108
108
|
f.number == 2 &&
|
|
109
|
-
f.
|
|
109
|
+
f.fieldKind == "scalar" &&
|
|
110
110
|
f.scalar === ScalarType.BYTES
|
|
111
111
|
);
|
|
112
112
|
if (typeUrl && value) {
|
|
@@ -122,13 +122,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
122
122
|
const seconds = message.fields.find(
|
|
123
123
|
(f) =>
|
|
124
124
|
f.number == 1 &&
|
|
125
|
-
f.
|
|
125
|
+
f.fieldKind == "scalar" &&
|
|
126
126
|
f.scalar === ScalarType.INT64
|
|
127
127
|
);
|
|
128
128
|
const nanos = message.fields.find(
|
|
129
129
|
(f) =>
|
|
130
130
|
f.number == 2 &&
|
|
131
|
-
f.
|
|
131
|
+
f.fieldKind == "scalar" &&
|
|
132
132
|
f.scalar === ScalarType.INT32
|
|
133
133
|
);
|
|
134
134
|
if (seconds && nanos) {
|
|
@@ -144,13 +144,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
144
144
|
const seconds = message.fields.find(
|
|
145
145
|
(f) =>
|
|
146
146
|
f.number == 1 &&
|
|
147
|
-
f.
|
|
147
|
+
f.fieldKind == "scalar" &&
|
|
148
148
|
f.scalar === ScalarType.INT64
|
|
149
149
|
);
|
|
150
150
|
const nanos = message.fields.find(
|
|
151
151
|
(f) =>
|
|
152
152
|
f.number == 2 &&
|
|
153
|
-
f.
|
|
153
|
+
f.fieldKind == "scalar" &&
|
|
154
154
|
f.scalar === ScalarType.INT32
|
|
155
155
|
);
|
|
156
156
|
if (seconds && nanos) {
|
|
@@ -165,7 +165,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
165
165
|
case "google.protobuf.Struct": {
|
|
166
166
|
const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
|
|
167
167
|
if (
|
|
168
|
-
fields?.
|
|
168
|
+
fields?.fieldKind !== "map" ||
|
|
169
169
|
fields.mapValue.kind !== "message" ||
|
|
170
170
|
fields.mapValue.message.typeName !== "google.protobuf.Value"
|
|
171
171
|
) {
|
|
@@ -179,7 +179,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
179
179
|
(f) => f.number == 1 && f.oneof === kind
|
|
180
180
|
);
|
|
181
181
|
if (
|
|
182
|
-
nullValue?.
|
|
182
|
+
nullValue?.fieldKind !== "enum" ||
|
|
183
183
|
nullValue.enum.typeName !== "google.protobuf.NullValue"
|
|
184
184
|
) {
|
|
185
185
|
return undefined;
|
|
@@ -187,21 +187,21 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
187
187
|
const numberValue = message.fields.find(
|
|
188
188
|
(f) =>
|
|
189
189
|
f.number == 2 &&
|
|
190
|
-
f.
|
|
190
|
+
f.fieldKind == "scalar" &&
|
|
191
191
|
f.scalar === ScalarType.DOUBLE &&
|
|
192
192
|
f.oneof === kind
|
|
193
193
|
);
|
|
194
194
|
const stringValue = message.fields.find(
|
|
195
195
|
(f) =>
|
|
196
196
|
f.number == 3 &&
|
|
197
|
-
f.
|
|
197
|
+
f.fieldKind == "scalar" &&
|
|
198
198
|
f.scalar === ScalarType.STRING &&
|
|
199
199
|
f.oneof === kind
|
|
200
200
|
);
|
|
201
201
|
const boolValue = message.fields.find(
|
|
202
202
|
(f) =>
|
|
203
203
|
f.number == 4 &&
|
|
204
|
-
f.
|
|
204
|
+
f.fieldKind == "scalar" &&
|
|
205
205
|
f.scalar === ScalarType.BOOL &&
|
|
206
206
|
f.oneof === kind
|
|
207
207
|
);
|
|
@@ -209,7 +209,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
209
209
|
(f) => f.number == 5 && f.oneof === kind
|
|
210
210
|
);
|
|
211
211
|
if (
|
|
212
|
-
structValue?.
|
|
212
|
+
structValue?.fieldKind !== "message" ||
|
|
213
213
|
structValue.message.typeName !== "google.protobuf.Struct"
|
|
214
214
|
) {
|
|
215
215
|
return undefined;
|
|
@@ -218,7 +218,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
218
218
|
(f) => f.number == 6 && f.oneof === kind
|
|
219
219
|
);
|
|
220
220
|
if (
|
|
221
|
-
listValue?.
|
|
221
|
+
listValue?.fieldKind !== "message" ||
|
|
222
222
|
listValue.message.typeName !== "google.protobuf.ListValue"
|
|
223
223
|
) {
|
|
224
224
|
return undefined;
|
|
@@ -240,7 +240,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
240
240
|
case "google.protobuf.ListValue": {
|
|
241
241
|
const values = message.fields.find((f) => f.number == 1 && f.repeated);
|
|
242
242
|
if (
|
|
243
|
-
values?.
|
|
243
|
+
values?.fieldKind != "message" ||
|
|
244
244
|
values.message.typeName !== "google.protobuf.Value"
|
|
245
245
|
) {
|
|
246
246
|
break;
|
|
@@ -251,7 +251,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
251
251
|
const paths = message.fields.find(
|
|
252
252
|
(f) =>
|
|
253
253
|
f.number == 1 &&
|
|
254
|
-
f.
|
|
254
|
+
f.fieldKind == "scalar" &&
|
|
255
255
|
f.scalar === ScalarType.STRING &&
|
|
256
256
|
f.repeated
|
|
257
257
|
);
|
|
@@ -275,7 +275,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
|
|
|
275
275
|
if (!value) {
|
|
276
276
|
break;
|
|
277
277
|
}
|
|
278
|
-
if (value.
|
|
278
|
+
if (value.fieldKind !== "scalar") {
|
|
279
279
|
break;
|
|
280
280
|
}
|
|
281
281
|
return { typeName: message.typeName, value };
|
|
@@ -13,32 +13,15 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
15
|
import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
16
|
+
import { generateTs } from "./typescript.js";
|
|
17
|
+
import { generateJs } from "./javascript.js";
|
|
18
|
+
import { generateDts } from "./declaration.js";
|
|
19
19
|
import { version } from "../package.json";
|
|
20
20
|
|
|
21
|
-
export const protocGenEs = createEcmaScriptPlugin(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
schema.targets.includes(gen.target)
|
|
29
|
-
);
|
|
30
|
-
for (const target of targets) {
|
|
31
|
-
for (const file of schema.files) {
|
|
32
|
-
const f = schema.generateFile(file.name + target.extension);
|
|
33
|
-
f.preamble(file);
|
|
34
|
-
for (const enumeration of file.enums) {
|
|
35
|
-
target.generateEnum(schema, f, enumeration);
|
|
36
|
-
}
|
|
37
|
-
for (const message of file.messages) {
|
|
38
|
-
target.generateMessage(schema, f, message);
|
|
39
|
-
}
|
|
40
|
-
// We do not generate anything for services, and we do not support extensions at this time
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
);
|
|
21
|
+
export const protocGenEs = createEcmaScriptPlugin({
|
|
22
|
+
name: "protoc-gen-es",
|
|
23
|
+
version: `v${String(version)}`,
|
|
24
|
+
generateTs,
|
|
25
|
+
generateJs,
|
|
26
|
+
generateDts,
|
|
27
|
+
});
|
package/src/typescript.ts
CHANGED
|
@@ -19,7 +19,11 @@ import type {
|
|
|
19
19
|
DescOneof,
|
|
20
20
|
} from "@bufbuild/protobuf";
|
|
21
21
|
import { ScalarType } from "@bufbuild/protobuf";
|
|
22
|
-
import type {
|
|
22
|
+
import type {
|
|
23
|
+
GeneratedFile,
|
|
24
|
+
Printable,
|
|
25
|
+
Schema,
|
|
26
|
+
} from "@bufbuild/protoplugin/ecmascript";
|
|
23
27
|
import {
|
|
24
28
|
localName,
|
|
25
29
|
getFieldIntrinsicDefaultValue,
|
|
@@ -30,12 +34,19 @@ import { matchWkt } from "./match-wkt.js";
|
|
|
30
34
|
import { generateFieldInfo } from "./javascript.js";
|
|
31
35
|
import { literalString } from "@bufbuild/protoplugin/ecmascript";
|
|
32
36
|
|
|
33
|
-
export
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
export function generateTs(schema: Schema) {
|
|
38
|
+
for (const file of schema.files) {
|
|
39
|
+
const f = schema.generateFile(file.name + "_pb.ts");
|
|
40
|
+
f.preamble(file);
|
|
41
|
+
for (const enumeration of file.enums) {
|
|
42
|
+
generateEnum(schema, f, enumeration);
|
|
43
|
+
}
|
|
44
|
+
for (const message of file.messages) {
|
|
45
|
+
generateMessage(schema, f, message);
|
|
46
|
+
}
|
|
47
|
+
// We do not generate anything for services, and we do not support extensions at this time
|
|
48
|
+
}
|
|
49
|
+
}
|
|
39
50
|
|
|
40
51
|
// prettier-ignore
|
|
41
52
|
function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
|
|
@@ -145,7 +156,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
|
|
|
145
156
|
|
|
146
157
|
function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
|
|
147
158
|
f.print(makeJsDoc(field, " "));
|
|
148
|
-
const e:
|
|
159
|
+
const e: Printable = [];
|
|
149
160
|
e.push(" ", localName(field));
|
|
150
161
|
const { defaultValue, typingInferrable } =
|
|
151
162
|
getFieldIntrinsicDefaultValue(field);
|