protoc 1.0.3 → 1.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.
- package/README.md +34 -26
- package/bin/protoc.js +19 -0
- package/index.js +130 -109
- package/package.json +16 -8
- package/protoc.js +5 -5
- package/scripts/postinstall.js +58 -46
- package/scripts/test.js +7 -6
- package/.npmignore +0 -2
package/README.md
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
|
-
# Protocol Buffers for Node
|
|
2
|
-
A wrapper in Node for the compiled protoc from https://github.com/
|
|
3
|
-
|
|
4
|
-
## Version
|
|
5
|
-
It's currently using Protocol Buffers `v3.
|
|
6
|
-
|
|
7
|
-
## Platforms
|
|
8
|
-
Google only provides binary files for Windows, Linux and OSX in x86_64 and x86_32.
|
|
9
|
-
|
|
10
|
-
## Examples
|
|
11
|
-
There's currently no documentation. Hopefully this example will help.
|
|
12
|
-
|
|
13
|
-
```JavaScript
|
|
14
|
-
var protoc = require("protoc");
|
|
15
|
-
|
|
16
|
-
protoc.library(["path/to/file.proto", "path/to/file2.proto"], function(err, files) {
|
|
17
|
-
if (err)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
# Protocol Buffers for Node
|
|
2
|
+
A wrapper in Node for the compiled protoc from https://github.com/protocolbuffers/protobuf.
|
|
3
|
+
|
|
4
|
+
## Version
|
|
5
|
+
It's currently using Protocol Buffers `v3.20.3`.
|
|
6
|
+
|
|
7
|
+
## Platforms
|
|
8
|
+
Google only provides binary files for Windows, Linux and OSX in x86_64 and x86_32.
|
|
9
|
+
|
|
10
|
+
## Examples
|
|
11
|
+
There's currently no documentation. Hopefully this example will help.
|
|
12
|
+
|
|
13
|
+
```JavaScript
|
|
14
|
+
var protoc = require("protoc");
|
|
15
|
+
|
|
16
|
+
protoc.library(["path/to/file.proto", "path/to/file2.proto"], function(err, files) {
|
|
17
|
+
if (err) {
|
|
18
|
+
console.error(err);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Handle the JavaScript Vinyl files.
|
|
23
|
+
// These files can be used in Google Closure Compiler,
|
|
24
|
+
// but they require the files in
|
|
25
|
+
// https://github.com/google/protobuf/tree/master/js
|
|
26
|
+
|
|
27
|
+
// ...
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
It's also possible to directly call the protoc binary file:
|
|
32
|
+
```
|
|
33
|
+
npx protoc --help
|
|
34
|
+
```
|
package/bin/protoc.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { protoc } = require("../");
|
|
4
|
+
|
|
5
|
+
protoc(process.argv.slice(2), {}, (err, stdout, stderr) => {
|
|
6
|
+
if (err) {
|
|
7
|
+
console.error(err);
|
|
8
|
+
process.exit(1);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (stdout) {
|
|
13
|
+
console.info(stdout);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (stderr) {
|
|
17
|
+
console.error(stderr);
|
|
18
|
+
}
|
|
19
|
+
})
|
package/index.js
CHANGED
|
@@ -1,109 +1,130 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.protoc = function(args, options, callback) {
|
|
11
|
-
cp.execFile(protoc, args, options, callback);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
exports.closure = function(files, options, callback) {
|
|
15
|
-
if (!callback) {
|
|
16
|
-
callback = options;
|
|
17
|
-
options = null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
options.imports = options.imports || [];
|
|
21
|
-
options.outputPath = options.outputPath || "./";
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"--js_out=one_output_file_per_input_file,binary:."
|
|
29
|
-
];
|
|
30
|
-
|
|
31
|
-
for (var i = 0; i < options.imports.length; i++) {
|
|
32
|
-
args.push("-I", path.join(relative, options.imports[i]));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
for (var i = 0; i < files.length; i++) {
|
|
36
|
-
args.push(path.join(relative, files[i]));
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
mkdirp(options.outputPath, function(err) {
|
|
40
|
-
if (err)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const cp = require("child_process");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const protoc = require("./protoc.js");
|
|
5
|
+
const Vinyl = require("vinyl");
|
|
6
|
+
const uuid = require("uuid");
|
|
7
|
+
const mkdirp = require("mkdirp");
|
|
8
|
+
const glob = require("glob");
|
|
9
|
+
|
|
10
|
+
exports.protoc = function(args, options, callback) {
|
|
11
|
+
cp.execFile(protoc, args, options, callback);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.closure = function(files, options, callback) {
|
|
15
|
+
if (!callback) {
|
|
16
|
+
callback = options;
|
|
17
|
+
options = null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
options.imports = options.imports || [];
|
|
21
|
+
options.outputPath = options.outputPath || "./";
|
|
22
|
+
|
|
23
|
+
const cwd = process.cwd();
|
|
24
|
+
const absoluteOutputPath = path.resolve(cwd, options.outputPath);
|
|
25
|
+
const relative = path.relative(absoluteOutputPath, cwd);
|
|
26
|
+
|
|
27
|
+
const args = [
|
|
28
|
+
"--js_out=one_output_file_per_input_file,binary:."
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
for (var i = 0; i < options.imports.length; i++) {
|
|
32
|
+
args.push("-I", path.join(relative, options.imports[i]));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
for (var i = 0; i < files.length; i++) {
|
|
36
|
+
args.push(path.join(relative, files[i]));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
mkdirp(options.outputPath, function(err) {
|
|
40
|
+
if (err) {
|
|
41
|
+
callback(err);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.protoc(args, {
|
|
46
|
+
"cwd": options.outputPath
|
|
47
|
+
}, callback);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Converts .proto files to .js files that can be used in Google Closure
|
|
53
|
+
* Compiler.
|
|
54
|
+
* The generated .js files require the files in
|
|
55
|
+
* https://github.com/protocolbuffers/protobuf/tree/v3.20.3/js.
|
|
56
|
+
* @param {?Array<string>} files the proto files.
|
|
57
|
+
* @param {?function(?Error, ?Array<Vinyl>)} callback the callback method.
|
|
58
|
+
*/
|
|
59
|
+
exports.library = function(files, callback) {
|
|
60
|
+
var dirpath = "tmp";
|
|
61
|
+
var filename = uuid.v4();
|
|
62
|
+
var jsFile = path.join(dirpath, filename);
|
|
63
|
+
mkdirp("tmp", function(err) {
|
|
64
|
+
if (err) {
|
|
65
|
+
callback(err);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
exports.protoc(["--js_out=library=" + jsFile + ",binary:."].concat(files), function(err, stdout, stderr) {
|
|
70
|
+
if (err) {
|
|
71
|
+
callback(err);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (fs.existsSync(jsFile + ".js")) {
|
|
76
|
+
fs.readFile(jsFile + ".js", function(err, contents) {
|
|
77
|
+
if (err) {
|
|
78
|
+
callback(err);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fs.unlink(jsFile + ".js", function(err) {
|
|
83
|
+
if (err) {
|
|
84
|
+
callback(err);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
fs.rmdir(dirpath, function() {
|
|
89
|
+
callback(null, [new Vinyl({
|
|
90
|
+
"cwd": "/",
|
|
91
|
+
"base": "/",
|
|
92
|
+
"path": filename + ".js",
|
|
93
|
+
"contents": contents
|
|
94
|
+
})]);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
} else {
|
|
99
|
+
glob("**/*.js", {
|
|
100
|
+
"cwd": jsFile
|
|
101
|
+
}, function(err, matches) {
|
|
102
|
+
if (err) {
|
|
103
|
+
callback(err, null);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
var files = matches.map(function(match) {
|
|
108
|
+
return new Vinyl({
|
|
109
|
+
"cwd": "/",
|
|
110
|
+
"base": "/",
|
|
111
|
+
"path": match,
|
|
112
|
+
"contents": fs.readFileSync(path.join(jsFile, match))
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
rimraf(jsFile, function(err) {
|
|
117
|
+
if (err) {
|
|
118
|
+
callback(err);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fs.rmdir(dirpath, function() {
|
|
123
|
+
callback(null, files);
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
};
|
package/package.json
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "protoc",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A tool for converting proto buffers (.proto) files into javascript files usable in Closure Compiler with Closure Library.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"postinstall": "node scripts/postinstall.js",
|
|
8
8
|
"test": "node scripts/test.js"
|
|
9
9
|
},
|
|
10
|
+
"bin": {
|
|
11
|
+
"protoc": "./bin/protoc.sh"
|
|
12
|
+
},
|
|
10
13
|
"author": "Jeppe Rune Mortensen <jepperm@gmail.com>",
|
|
11
14
|
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/YePpHa/node-protoc.git"
|
|
18
|
+
},
|
|
12
19
|
"dependencies": {
|
|
13
|
-
"glob": "^7.
|
|
14
|
-
"mkdirp": "^0.5.
|
|
15
|
-
"node-
|
|
16
|
-
"request": "^2.
|
|
17
|
-
"rimraf": "^
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
+
"glob": "^7.2.3",
|
|
21
|
+
"mkdirp": "^0.5.6",
|
|
22
|
+
"node-fetch": "^3.2.10",
|
|
23
|
+
"request": "^2.88.2",
|
|
24
|
+
"rimraf": "^3.0.2",
|
|
25
|
+
"unzipper": "^0.10.11",
|
|
26
|
+
"uuid": "^9.0.0",
|
|
27
|
+
"vinyl": "^2.2.1"
|
|
20
28
|
}
|
|
21
29
|
}
|
package/protoc.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module.exports = path.join(protoc_bin, "protoc" + (process.platform === "win32" ? ".exe" : ""));
|
|
1
|
+
const path = require("path");
|
|
2
|
+
|
|
3
|
+
const protoc_bin = path.join(__dirname, "protoc", "bin");
|
|
4
|
+
|
|
5
|
+
module.exports = path.join(protoc_bin, "protoc" + (process.platform === "win32" ? ".exe" : ""));
|
package/scripts/postinstall.js
CHANGED
|
@@ -1,46 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"darwin_x86_64":
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const unzip = require("unzipper");
|
|
4
|
+
const mkdirp = require("mkdirp");
|
|
5
|
+
const protoc = require("../protoc.js");
|
|
6
|
+
|
|
7
|
+
const protoVersion = "3.20.3";
|
|
8
|
+
|
|
9
|
+
const releases = {
|
|
10
|
+
"win32_x86_32": `https://github.com/protocolbuffers/protobuf/releases/download/v${protoVersion}/protoc-${protoVersion}-win32.zip`,
|
|
11
|
+
"win32_x86_64": `https://github.com/protocolbuffers/protobuf/releases/download/v${protoVersion}/protoc-${protoVersion}-win32.zip`,
|
|
12
|
+
"linux_x86_32": `https://github.com/protocolbuffers/protobuf/releases/download/v${protoVersion}/protoc-${protoVersion}-linux-x86_32.zip`,
|
|
13
|
+
"linux_x86_64": `https://github.com/protocolbuffers/protobuf/releases/download/v${protoVersion}/protoc-${protoVersion}-linux-x86_64.zip`,
|
|
14
|
+
"darwin_x86_64": `https://github.com/protocolbuffers/protobuf/releases/download/v${protoVersion}/protoc-${protoVersion}-osx-x86_64.zip`
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const platform = process.platform;
|
|
18
|
+
const arch = process.arch === "x64" ? "x86_64" : "x86_32";
|
|
19
|
+
const release = platform + "_" + arch;
|
|
20
|
+
const protocDirectory = path.join(__dirname, "..", "protoc");
|
|
21
|
+
|
|
22
|
+
(async () => {
|
|
23
|
+
if (!releases[release]) {
|
|
24
|
+
throw new Error(`Unsupported platform: ${release}. Was not able to find a proper protoc version.`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fs.rmSync(protocDirectory, { recursive: true, force: true });
|
|
28
|
+
|
|
29
|
+
const fetch = await import("node-fetch");
|
|
30
|
+
const response = await fetch.default(releases[release]);
|
|
31
|
+
response
|
|
32
|
+
.body
|
|
33
|
+
.pipe(unzip.Parse())
|
|
34
|
+
.on("entry", entry => {
|
|
35
|
+
const isFile = "File" === entry.type;
|
|
36
|
+
const isDir = "Directory" === entry.type;
|
|
37
|
+
const fullpath = path.join(protocDirectory, entry.path);
|
|
38
|
+
const directory = isDir ? fullpath : path.dirname(fullpath);
|
|
39
|
+
|
|
40
|
+
mkdirp(directory, err => {
|
|
41
|
+
if (err) {
|
|
42
|
+
throw err;
|
|
43
|
+
}
|
|
44
|
+
if (isFile) {
|
|
45
|
+
entry.pipe(fs.createWriteStream(fullpath))
|
|
46
|
+
.on("finish", function() {
|
|
47
|
+
if (protoc === fullpath) {
|
|
48
|
+
fs.chmod(fullpath, 0o755, function(err) {
|
|
49
|
+
if (err) {
|
|
50
|
+
throw err;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
})();
|
package/scripts/test.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
protoc.library(["protoc/include/google/protobuf/timestamp.proto"], function(err, files) {
|
|
3
|
-
if (err)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
1
|
+
const protoc = require("../index.js");
|
|
2
|
+
protoc.library(["protoc/include/google/protobuf/timestamp.proto"], function(err, files) {
|
|
3
|
+
if (err) {
|
|
4
|
+
console.error(err);
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
});
|
package/.npmignore
DELETED