protons 2.0.1 → 3.0.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/LICENSE +3 -20
- package/README.md +31 -122
- package/dist/bin/protons.js +34 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +206 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +149 -70
- package/src/index.ts +282 -0
- package/.aegir.js +0 -11
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/open_an_issue.md +0 -19
- package/.github/config.yml +0 -68
- package/.travis.yml +0 -42
- package/CHANGELOG.md +0 -83
- package/bench/bench.proto.js +0 -30
- package/bench/index.js +0 -57
- package/example.js +0 -19
- package/example.proto +0 -4
- package/src/compile/decode.js +0 -330
- package/src/compile/encode.js +0 -133
- package/src/compile/encoding-length.js +0 -102
- package/src/compile/encodings/bool.js +0 -21
- package/src/compile/encodings/bytes.js +0 -42
- package/src/compile/encodings/double.js +0 -21
- package/src/compile/encodings/encoder.js +0 -14
- package/src/compile/encodings/fixed32.js +0 -21
- package/src/compile/encodings/fixed64.js +0 -25
- package/src/compile/encodings/float.js +0 -21
- package/src/compile/encodings/index.js +0 -22
- package/src/compile/encodings/int32.js +0 -22
- package/src/compile/encodings/int64.js +0 -49
- package/src/compile/encodings/sfixed32.js +0 -21
- package/src/compile/encodings/sint64.js +0 -19
- package/src/compile/encodings/string.js +0 -41
- package/src/compile/encodings/varint.js +0 -19
- package/src/compile/index.js +0 -165
- package/src/compile/utils.js +0 -5
- package/src/index.js +0 -39
- package/test/basic.spec.js +0 -109
- package/test/booleans.spec.js +0 -37
- package/test/bytes.spec.js +0 -36
- package/test/corrupted.spec.js +0 -50
- package/test/custom-types.spec.js +0 -55
- package/test/defaults.spec.js +0 -44
- package/test/enums.spec.js +0 -21
- package/test/float.spec.js +0 -28
- package/test/integers.spec.js +0 -72
- package/test/map.spec.js +0 -35
- package/test/nan.spec.js +0 -28
- package/test/nested.spec.js +0 -70
- package/test/notpacked.spec.js +0 -33
- package/test/oneof.spec.js +0 -59
- package/test/optional.spec.js +0 -65
- package/test/packed.spec.js +0 -61
- package/test/repeated.spec.js +0 -74
- package/test/strings.spec.js +0 -45
- package/test/test.proto.js +0 -134
- package/test/utf-8.spec.js +0 -21
package/LICENSE
CHANGED
|
@@ -1,21 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
This project is dual licensed under MIT and Apache-2.0.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
all copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
-
THE SOFTWARE.
|
|
3
|
+
MIT: https://www.opensource.org/licenses/mit
|
|
4
|
+
Apache-2.0: https://www.apache.org/licenses/license-2.0
|
package/README.md
CHANGED
|
@@ -1,153 +1,62 @@
|
|
|
1
1
|
# protons <!-- omit in toc -->
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://travis-ci.org/ipfs/protons)
|
|
3
|
+
> Generate typescript from .proto files
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
>
|
|
8
|
-
> Forked from [protocol-buffers](https://github.com/mafintosh/protocol-buffers).
|
|
5
|
+
## Table of contents <!-- omit in toc -->
|
|
9
6
|
|
|
10
|
-
## Lead Maintainer <!-- omit in toc -->
|
|
11
|
-
|
|
12
|
-
[Alex Potsides](https://github.com/achingbrain)
|
|
13
|
-
|
|
14
|
-
## Table of Contents <!-- omit in toc -->
|
|
15
|
-
|
|
16
|
-
- [Install](#install)
|
|
17
7
|
- [Usage](#usage)
|
|
18
|
-
- [
|
|
19
|
-
- [Performance](#performance)
|
|
20
|
-
- [Leveldb encoding compatibility](#leveldb-encoding-compatibility)
|
|
8
|
+
- [Contribute](#contribute)
|
|
21
9
|
- [License](#license)
|
|
22
10
|
|
|
23
|
-
##
|
|
24
|
-
|
|
25
|
-
```sh
|
|
26
|
-
> npm install protons
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Usage
|
|
30
|
-
|
|
31
|
-
Assuming the following `test.proto` file exists
|
|
32
|
-
|
|
33
|
-
```proto
|
|
34
|
-
enum FOO {
|
|
35
|
-
BAR = 1;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
message Test {
|
|
39
|
-
required float num = 1;
|
|
40
|
-
required string payload = 2;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
message AnotherOne {
|
|
44
|
-
repeated FOO list = 1;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
message WithOptional {
|
|
48
|
-
optional string payload = 1;
|
|
49
|
-
}
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
Use the above proto file to encode/decode messages by doing
|
|
53
|
-
|
|
54
|
-
``` js
|
|
55
|
-
const protons = require('protons')
|
|
56
|
-
|
|
57
|
-
// pass a proto file as a buffer/string or pass a parsed protobuf-schema object
|
|
58
|
-
const messages = protons(fs.readFileSync('test.proto'))
|
|
11
|
+
## Install
|
|
59
12
|
|
|
60
|
-
|
|
61
|
-
num: 42,
|
|
62
|
-
payload: 'hello world'
|
|
63
|
-
})
|
|
13
|
+
To use this project, add `protons` as a development dependency and `protons-runtime` as a runtime dependency.
|
|
64
14
|
|
|
65
|
-
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
To decode a message use `Test.decode`
|
|
15
|
+
`protons` contains the code to compile `.proto` files to `.ts` files and `protons-runtime` contains the code to do serialization/deserialization to `Uint8Array`s during application execution.
|
|
69
16
|
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
|
|
17
|
+
```console
|
|
18
|
+
$ npm install --save-dev protons
|
|
19
|
+
$ npm install --save protons-runtime
|
|
73
20
|
```
|
|
74
21
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
``` js
|
|
78
|
-
const buf = messages.AnotherOne.encode({
|
|
79
|
-
list: [
|
|
80
|
-
messages.FOO.BAR
|
|
81
|
-
]
|
|
82
|
-
})
|
|
83
|
-
```
|
|
22
|
+
## Usage
|
|
84
23
|
|
|
85
|
-
|
|
24
|
+
First generate your `.ts` files:
|
|
86
25
|
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
list: [
|
|
90
|
-
messages.SomeMessage.NESTED_ENUM.VALUE
|
|
91
|
-
]
|
|
92
|
-
})
|
|
26
|
+
```console
|
|
27
|
+
$ protons ./path/to/foo.proto ./path/to/output.ts
|
|
93
28
|
```
|
|
94
29
|
|
|
95
|
-
|
|
96
|
-
available types etc.
|
|
97
|
-
|
|
98
|
-
## Properties
|
|
99
|
-
|
|
100
|
-
Decoded object properties can be interacted with using accessor methods:
|
|
101
|
-
|
|
102
|
-
```javascript
|
|
103
|
-
const obj = messages.WithOptional.decode(messages.WithOptional.encode({}))
|
|
30
|
+
Then run tsc over them as normal:
|
|
104
31
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
obj.setPayload('hello world')
|
|
108
|
-
obj.getPayload() // 'hello world'
|
|
109
|
-
obj.clearPayload()
|
|
110
|
-
obj.getPayload() // undefined
|
|
32
|
+
```console
|
|
33
|
+
$ tsc
|
|
111
34
|
```
|
|
112
35
|
|
|
113
|
-
|
|
36
|
+
In your code import the generated classes and use them to transform to/from bytes:
|
|
114
37
|
|
|
115
|
-
|
|
38
|
+
```js
|
|
39
|
+
import { Foo } from './foo.js'
|
|
116
40
|
|
|
117
|
-
|
|
41
|
+
const foo = {
|
|
42
|
+
message: 'hello world'
|
|
43
|
+
}
|
|
118
44
|
|
|
119
|
-
|
|
45
|
+
const encoded = Foo.encode(foo)
|
|
46
|
+
const decoded = Foo.decode(encoded)
|
|
120
47
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
JSON (decode) x 619,564 ops/sec ±1.60% (94 runs sampled)
|
|
124
|
-
JSON (encode + decode) x 308,635 ops/sec ±1.74% (92 runs sampled)
|
|
125
|
-
protocol-buffers@4.1.0 (encode) x 693,570 ops/sec ±1.55% (92 runs sampled)
|
|
126
|
-
protocol-buffers@4.1.0 (decode) x 1,894,031 ops/sec ±1.61% (93 runs sampled)
|
|
127
|
-
protocol-buffers@4.1.0 (encode + decode) x 444,229 ops/sec ±1.50% (93 runs sampled)
|
|
128
|
-
protons@1.0.1 (encode) x 435,058 ops/sec ±1.46% (91 runs sampled)
|
|
129
|
-
protons@1.0.1 (decode) x 29,548 ops/sec ±3.29% (78 runs sampled)
|
|
130
|
-
protons@1.0.1 (encode + decode) x 27,042 ops/sec ±4.41% (80 runs sampled)
|
|
48
|
+
console.info(decoded.message)
|
|
49
|
+
// 'hello world'
|
|
131
50
|
```
|
|
132
51
|
|
|
133
|
-
|
|
52
|
+
## Contribute
|
|
134
53
|
|
|
135
|
-
|
|
54
|
+
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/protons/issues)!
|
|
136
55
|
|
|
137
|
-
|
|
138
|
-
This means you can pass them as `valueEncoding` and `keyEncoding`.
|
|
56
|
+
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
|
139
57
|
|
|
140
|
-
|
|
141
|
-
const level = require('level')
|
|
142
|
-
const db = level('db')
|
|
143
|
-
|
|
144
|
-
db.put('hello', {payload:'world'}, {valueEncoding:messages.Test}, (err) => {
|
|
145
|
-
db.get('hello', {valueEncoding:messages.Test}, (err, message) => {
|
|
146
|
-
console.log(message)
|
|
147
|
-
})
|
|
148
|
-
})
|
|
149
|
-
```
|
|
58
|
+
[](https://github.com/ipfs/community/blob/master/contributing.md)
|
|
150
59
|
|
|
151
60
|
## License
|
|
152
61
|
|
|
153
|
-
MIT
|
|
62
|
+
[Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) © Protocol Labs
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
import meow from 'meow';
|
|
3
|
+
import { generate } from '../src/index.js';
|
|
4
|
+
async function main() {
|
|
5
|
+
const cli = meow(`
|
|
6
|
+
Usage
|
|
7
|
+
$ protons source
|
|
8
|
+
|
|
9
|
+
Options
|
|
10
|
+
--output, -o Path to a directory to write transpiled typescript files into
|
|
11
|
+
|
|
12
|
+
Examples
|
|
13
|
+
$ protons ./path/to/file.proto
|
|
14
|
+
`, {
|
|
15
|
+
importMeta: import.meta,
|
|
16
|
+
flags: {
|
|
17
|
+
output: {
|
|
18
|
+
type: 'string',
|
|
19
|
+
alias: 'o'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (cli.input.length === 0) {
|
|
24
|
+
throw new Error('source must be specified');
|
|
25
|
+
}
|
|
26
|
+
for (const source of cli.input) {
|
|
27
|
+
await generate(source, cli.flags);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
main().catch(err => {
|
|
31
|
+
console.error(err); // eslint-disable-line no-console
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
|
34
|
+
//# sourceMappingURL=protons.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAoQA,wBAAsB,QAAQ,CAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,iBAqBzD"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { main as pbjs } from 'protobufjs/cli/pbjs.js';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
import fs from 'fs/promises';
|
|
5
|
+
function pathWithExtension(input, extension) {
|
|
6
|
+
return path.join(path.dirname(input), path.basename(input).split('.').slice(0, -1).join('.') + extension);
|
|
7
|
+
}
|
|
8
|
+
const types = {
|
|
9
|
+
double: 'number',
|
|
10
|
+
float: 'number',
|
|
11
|
+
int32: 'number',
|
|
12
|
+
int64: 'bigint',
|
|
13
|
+
uint32: 'number',
|
|
14
|
+
uint64: 'bigint',
|
|
15
|
+
sint32: 'number',
|
|
16
|
+
sint64: 'bigint',
|
|
17
|
+
fixed32: 'number',
|
|
18
|
+
fixed64: 'bigint',
|
|
19
|
+
sfixed32: 'number',
|
|
20
|
+
sfixed64: 'bigint',
|
|
21
|
+
bool: 'boolean',
|
|
22
|
+
string: 'string',
|
|
23
|
+
bytes: 'Uint8Array'
|
|
24
|
+
};
|
|
25
|
+
function findTypeName(typeName, classDef, moduleDef) {
|
|
26
|
+
if (types[typeName] != null) {
|
|
27
|
+
return types[typeName];
|
|
28
|
+
}
|
|
29
|
+
if (isEnumDef(classDef)) {
|
|
30
|
+
throw new Error('Could not find type in enum');
|
|
31
|
+
}
|
|
32
|
+
if (classDef.nested?.[typeName] != null) {
|
|
33
|
+
return `${classDef.fullName}.${typeName}`;
|
|
34
|
+
}
|
|
35
|
+
if (classDef.parent != null) {
|
|
36
|
+
return findTypeName(typeName, classDef.parent, moduleDef);
|
|
37
|
+
}
|
|
38
|
+
if (moduleDef.globals[typeName] != null) {
|
|
39
|
+
return typeName;
|
|
40
|
+
}
|
|
41
|
+
throw new Error(`Could not resolve type name "${typeName}"`);
|
|
42
|
+
}
|
|
43
|
+
function findDef(typeName, classDef, moduleDef) {
|
|
44
|
+
if (isEnumDef(classDef)) {
|
|
45
|
+
throw new Error('Could not find type in enum');
|
|
46
|
+
}
|
|
47
|
+
if (classDef.nested?.[typeName] != null) {
|
|
48
|
+
return classDef.nested?.[typeName];
|
|
49
|
+
}
|
|
50
|
+
if (classDef.parent != null) {
|
|
51
|
+
return findDef(typeName, classDef.parent, moduleDef);
|
|
52
|
+
}
|
|
53
|
+
if (moduleDef.globals[typeName] != null) {
|
|
54
|
+
return moduleDef.globals[typeName];
|
|
55
|
+
}
|
|
56
|
+
throw new Error(`Could not resolve type name "${typeName}"`);
|
|
57
|
+
}
|
|
58
|
+
const encoders = {
|
|
59
|
+
bool: 'bool',
|
|
60
|
+
double: 'double',
|
|
61
|
+
bytes: 'bytes',
|
|
62
|
+
fixed32: 'fixed32',
|
|
63
|
+
fixed64: 'fixed64',
|
|
64
|
+
float: 'float',
|
|
65
|
+
int32: 'int32',
|
|
66
|
+
int64: 'int64',
|
|
67
|
+
sint32: 'sint32',
|
|
68
|
+
sint64: 'sint64',
|
|
69
|
+
string: 'string',
|
|
70
|
+
uint32: 'uint32',
|
|
71
|
+
uint64: 'uint64',
|
|
72
|
+
sfixed32: 'sfixed32',
|
|
73
|
+
sfixed64: 'sfixed64'
|
|
74
|
+
};
|
|
75
|
+
function isEnumDef(obj) {
|
|
76
|
+
return obj.values != null;
|
|
77
|
+
}
|
|
78
|
+
function defineFields(fields, messageDef, moduleDef) {
|
|
79
|
+
return Object.entries(fields).map(([fieldName, fieldDef]) => {
|
|
80
|
+
const isArray = fieldDef.rule === 'repeated';
|
|
81
|
+
const isOptional = !isArray && fieldDef.options?.proto3_optional === true;
|
|
82
|
+
return `${fieldName}${isOptional ? '?' : ''}: ${findTypeName(fieldDef.type, messageDef, moduleDef)}${isArray ? '[]' : ''}`;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function compileMessage(messageDef, moduleDef) {
|
|
86
|
+
if (isEnumDef(messageDef)) {
|
|
87
|
+
moduleDef.imports.add('enumeration');
|
|
88
|
+
return `
|
|
89
|
+
export enum ${messageDef.name} {
|
|
90
|
+
${Object.entries(messageDef.values).map(([enumValueName, enumValue]) => {
|
|
91
|
+
return `${enumValueName} = '${enumValueName}'`;
|
|
92
|
+
}).join(',\n ').trim()}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export namespace ${messageDef.name} {
|
|
96
|
+
export const codec = enumeration<typeof ${messageDef.name}>(${messageDef.name})
|
|
97
|
+
}
|
|
98
|
+
`;
|
|
99
|
+
}
|
|
100
|
+
let nested = '';
|
|
101
|
+
if (messageDef.nested != null) {
|
|
102
|
+
nested = '\n';
|
|
103
|
+
nested += Object.values(messageDef.nested)
|
|
104
|
+
.map(def => compileMessage(def, moduleDef).trim())
|
|
105
|
+
.join('\n')
|
|
106
|
+
.split('\n')
|
|
107
|
+
.map(line => line.trim() === '' ? '' : ` ${line}`)
|
|
108
|
+
.join('\n');
|
|
109
|
+
nested += '\n';
|
|
110
|
+
}
|
|
111
|
+
const fields = messageDef.fields ?? {};
|
|
112
|
+
// import relevant modules
|
|
113
|
+
moduleDef.imports.add('encodeMessage');
|
|
114
|
+
moduleDef.imports.add('decodeMessage');
|
|
115
|
+
moduleDef.imports.add('message');
|
|
116
|
+
return `
|
|
117
|
+
export interface ${messageDef.name} {
|
|
118
|
+
${defineFields(fields, messageDef, moduleDef)
|
|
119
|
+
.join('\n ')
|
|
120
|
+
.trim()}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export namespace ${messageDef.name} {${nested}
|
|
124
|
+
export const codec = message<${messageDef.name}>({
|
|
125
|
+
${Object.entries(fields)
|
|
126
|
+
.map(([name, fieldDef]) => {
|
|
127
|
+
let codec = encoders[fieldDef.type];
|
|
128
|
+
if (codec == null) {
|
|
129
|
+
const def = findDef(fieldDef.type, messageDef, moduleDef);
|
|
130
|
+
if (isEnumDef(def)) {
|
|
131
|
+
moduleDef.imports.add('enumeration');
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
moduleDef.imports.add('message');
|
|
135
|
+
}
|
|
136
|
+
const typeName = findTypeName(fieldDef.type, messageDef, moduleDef);
|
|
137
|
+
codec = `${typeName}.codec`;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
moduleDef.imports.add(codec);
|
|
141
|
+
}
|
|
142
|
+
return `${fieldDef.id}: { name: '${name}', codec: ${codec}${fieldDef.options?.proto3_optional === true ? ', optional: true' : ''}${fieldDef.rule === 'repeated' ? ', repeats: true' : ''} }`;
|
|
143
|
+
}).join(',\n ')}
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
export const encode = (obj: ${messageDef.name}): Uint8Array => {
|
|
147
|
+
return encodeMessage(obj, ${messageDef.name}.codec)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export const decode = (buf: Uint8Array): ${messageDef.name} => {
|
|
151
|
+
return decodeMessage(buf, ${messageDef.name}.codec)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
`;
|
|
155
|
+
}
|
|
156
|
+
function defineModule(def) {
|
|
157
|
+
const moduleDef = {
|
|
158
|
+
imports: new Set(),
|
|
159
|
+
types: new Set(),
|
|
160
|
+
compiled: [],
|
|
161
|
+
globals: {}
|
|
162
|
+
};
|
|
163
|
+
const defs = def.nested;
|
|
164
|
+
if (defs == null) {
|
|
165
|
+
throw new Error('No top-level messages found in protobuf');
|
|
166
|
+
}
|
|
167
|
+
function defineMessage(defs, parent) {
|
|
168
|
+
for (const className of Object.keys(defs)) {
|
|
169
|
+
const classDef = defs[className];
|
|
170
|
+
classDef.name = className;
|
|
171
|
+
classDef.parent = parent;
|
|
172
|
+
classDef.fullName = parent == null ? className : `${parent.fullName}.${className}`;
|
|
173
|
+
if (classDef.nested != null) {
|
|
174
|
+
defineMessage(classDef.nested, classDef);
|
|
175
|
+
}
|
|
176
|
+
if (parent == null) {
|
|
177
|
+
moduleDef.globals[className] = classDef;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
defineMessage(defs);
|
|
182
|
+
for (const className of Object.keys(defs)) {
|
|
183
|
+
const classDef = defs[className];
|
|
184
|
+
moduleDef.compiled.push(compileMessage(classDef, moduleDef));
|
|
185
|
+
}
|
|
186
|
+
return moduleDef;
|
|
187
|
+
}
|
|
188
|
+
export async function generate(source, flags) {
|
|
189
|
+
// convert .protobuf to .json
|
|
190
|
+
const json = await promisify(pbjs)(['-t', 'json', source]);
|
|
191
|
+
if (json == null) {
|
|
192
|
+
throw new Error(`Could not convert ${source} to intermediate JSON format`);
|
|
193
|
+
}
|
|
194
|
+
const def = JSON.parse(json);
|
|
195
|
+
const moduleDef = defineModule(def);
|
|
196
|
+
const content = `
|
|
197
|
+
/* eslint-disable import/export */
|
|
198
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
199
|
+
|
|
200
|
+
import { ${Array.from(moduleDef.imports).join(', ')} } from 'protons-runtime'
|
|
201
|
+
|
|
202
|
+
${moduleDef.compiled.map(str => str.trim()).join('\n\n').trim()}
|
|
203
|
+
`.trim();
|
|
204
|
+
await fs.writeFile(pathWithExtension(source, '.ts'), content + '\n');
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,IAAI,EAAE,MAAM,wBAAwB,CAAA;AACrD,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAA;AAChC,OAAO,EAAE,MAAM,aAAa,CAAA;AAE5B,SAAS,iBAAiB,CAAE,KAAa,EAA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