Skip to content

Commit 5ff1a1a

Browse files
committed
add standlone protoc-gen-js npm package (@protocolbuffers/protoc-gen-js) for convenience
1 parent 5c70f21 commit 5ff1a1a

File tree

8 files changed

+206
-0
lines changed

8 files changed

+206
-0
lines changed

protoc_plugin/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
bin/protoc-gen-js*
3+
protocolbuffers-protoc-gen-js-*.tgz

protoc_plugin/LICENSE.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2025, Google Inc.
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
3. Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

protoc_plugin/bin/.gitkeep

Whitespace-only changes.

protoc_plugin/cli.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env node
2+
3+
const child_process = require("child_process");
4+
const PLUGIN = require("./");
5+
6+
child_process
7+
.spawn(PLUGIN, process.argv.slice(2), { stdio: "inherit" })
8+
.on("exit", (code) => process.exit(code));
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const fs = require('fs');
2+
const os = require('os');
3+
const path = require('path');
4+
const AdmZip = require('adm-zip');
5+
6+
const VERSION = '4.0.0';
7+
const BIN_DIR = path.join(__dirname, 'bin');
8+
9+
const SUPPORTED_PLATFORMS = [
10+
{ platform: 'linux', arch: 'x86_64' },
11+
{ platform: 'linux', arch: 'aarch64' },
12+
{ platform: 'osx', arch: 'x86_64' },
13+
{ platform: 'osx', arch: 'aarch64' },
14+
{ platform: 'windows', arch: 'x86_64' },
15+
];
16+
17+
function getDownloadUrl(platform, arch) {
18+
return `https://github.com/protocolbuffers/protobuf-javascript/releases/download/v${VERSION}/protobuf-javascript-${VERSION}-${platform}-${arch}.zip`;
19+
}
20+
21+
function unzip(zipFile, destDir, binaryName) {
22+
return new Promise((resolve, reject) => {
23+
const binaryPathInZip = `bin/${binaryName}`;
24+
const zip = new AdmZip(zipFile);
25+
const entry = zip.getEntry(binaryPathInZip);
26+
if (entry) {
27+
zip.extractEntryTo(entry, destDir, false, true);
28+
resolve();
29+
} else {
30+
reject(new Error(`Binary ${binaryPathInZip} not found in zip file.`));
31+
}
32+
});
33+
}
34+
35+
function getCurrentPlatform() {
36+
const platform = os.platform();
37+
if (platform === 'darwin') {
38+
return 'osx';
39+
}
40+
if (platform === 'win32') {
41+
return 'windows';
42+
}
43+
return 'linux';
44+
}
45+
46+
function getCurrentArch() {
47+
const arch = os.arch();
48+
if (arch === 'x64') {
49+
return 'x86_64';
50+
}
51+
if (arch === 'arm64') {
52+
return 'aarch64';
53+
}
54+
return arch;
55+
}
56+
57+
async function main() {
58+
try {
59+
await fs.promises.mkdir(BIN_DIR, {recursive: true});
60+
61+
const platform = getCurrentPlatform();
62+
const arch = getCurrentArch();
63+
64+
const supportedPlatform = SUPPORTED_PLATFORMS.find(p => p.platform === platform && p.arch === arch);
65+
66+
if (!supportedPlatform) {
67+
console.error(`Unsupported platform: ${platform} ${arch}`);
68+
process.exit(1);
69+
}
70+
71+
const downloadUrl = getDownloadUrl(supportedPlatform.platform, supportedPlatform.arch);
72+
const zipFile = path.join(__dirname, `google-protobuf-${supportedPlatform.platform}-${supportedPlatform.arch}.zip`);
73+
const isWindows = supportedPlatform.platform === 'windows';
74+
const binaryName = isWindows ? 'protoc-gen-js.exe' : 'protoc-gen-js';
75+
const binaryPath = path.join(BIN_DIR, binaryName);
76+
77+
console.log(`Downloading ${downloadUrl}`);
78+
const response = await fetch(downloadUrl);
79+
if (!response.ok) {
80+
throw new Error(
81+
`Failed to download: ${response.status} ${response.statusText}`
82+
);
83+
}
84+
const buffer = await response.arrayBuffer();
85+
await fs.promises.writeFile(zipFile, Buffer.from(buffer));
86+
87+
console.log('Unzipping...');
88+
await unzip(zipFile, BIN_DIR, binaryName);
89+
90+
await fs.promises.unlink(zipFile);
91+
92+
console.log(`Making ${binaryPath} executable...`);
93+
if (!isWindows) {
94+
await fs.promises.chmod(binaryPath, 0o755);
95+
}
96+
97+
console.log('Done!');
98+
} catch (err) {
99+
console.error(`Failed to install protoc-gen-js: ${err.message}`);
100+
process.exit(1);
101+
}
102+
}
103+
104+
main();

protoc_plugin/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const path = require("path");
2+
const BIN_DIR = path.resolve(__dirname, "bin");
3+
const EXT = process.platform === "win32" ? ".exe" : "";
4+
5+
module.exports = path.resolve(BIN_DIR, "protoc-gen-js" + EXT);

protoc_plugin/package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protoc_plugin/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@protocolbuffers/protoc-gen-js",
3+
"version": "4.0.0",
4+
"description": "Standalone distribution of the protoc-gen-js plugin for Protocol Buffers",
5+
"author": "Google Protocol Buffers Team",
6+
"license": "BSD-3-Clause",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/protocolbuffers/protobuf-javascript.git"
10+
},
11+
"type": "commonjs",
12+
"main": "index.js",
13+
"bin": {
14+
"protoc-gen-js": "cli.js"
15+
},
16+
"scripts": {
17+
"postinstall": "node download-protoc-gen-js.js"
18+
},
19+
"dependencies": {
20+
"adm-zip": "^0.5.16"
21+
},
22+
"engines": {
23+
"node": ">=18.0.0"
24+
}
25+
}

0 commit comments

Comments
 (0)