Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions protoc_plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
bin/protoc-gen-js*
protocolbuffers-protoc-gen-js-*.tgz
29 changes: 29 additions & 0 deletions protoc_plugin/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2025, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Empty file added protoc_plugin/bin/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions protoc_plugin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node

const child_process = require("child_process");
const PLUGIN = require("./");

child_process
.spawn(PLUGIN, process.argv.slice(2), { stdio: "inherit" })
.on("exit", (code) => process.exit(code));
111 changes: 111 additions & 0 deletions protoc_plugin/download-protoc-gen-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const AdmZip = require('adm-zip');

const VERSION = '4.0.0';
const BIN_DIR = path.join(__dirname, 'bin');

function getZipFilename(currentSystem) {
return `protobuf-javascript-${VERSION}-${currentSystem}.zip`;
}

function getDownloadUrl(currentSystem) {
return `https://github.com/protocolbuffers/protobuf-javascript/releases/download/v${VERSION}/${getZipFilename(currentSystem)}`;
}

function unzip(zipFile, destDir, binaryName) {
return new Promise((resolve, reject) => {
const zip = new AdmZip(zipFile);

// Paths may be inconsistent between platforms. On MacOS and Windows,
// the path seems to include the archive name while it doesn't on linux.
// There's only one thing named protoc-gen-js(.exe), so just look for
// that instead of trying to get the exact "entryName".
const entries = zip.getEntries();
let found = false;
for (const entry of entries) {
if (entry.name === binaryName) {
zip.extractEntryTo(entry, destDir, false, true);
found = true;
break;
}
}

if (found) {
resolve();
} else {
reject(new Error(`Binary ${binaryName} not found in zip file ${zipFile}`));
}
});
}

function getCurrentSystem() {
const platform = os.platform();
const arch = os.arch();

if (platform === 'darwin') {
if (arch === 'x64') {
return 'osx-x86_64';
}
if (arch === 'arm64') {
return 'osx-aarch_64';
}
} else if (platform === 'win32' && arch === 'x64') {
return 'win64';
} else if (platform === 'linux') {
if (arch === 'x64') {
return 'linux-x86_64';
}
if (arch === 'arm64') {
return 'linux-aarch_64';
}
if (arch === 's390x') {
return 'linux-s390_64';
}
}

console.error(`Unsupported platform: ${platform} ${arch}`);
process.exit(1);
}

async function main() {
try {
await fs.promises.mkdir(BIN_DIR, { recursive: true });
const currentSystem = getCurrentSystem();


const downloadUrl = getDownloadUrl(currentSystem);
const zipFile = path.join(__dirname, getZipFilename(currentSystem));
const isWindows = os.platform() === 'win32';
const binaryName = isWindows ? 'protoc-gen-js.exe' : 'protoc-gen-js';
const binaryPath = path.join(BIN_DIR, binaryName);

console.log(`Downloading ${downloadUrl}`);
const response = await fetch(downloadUrl);
if (!response.ok) {
throw new Error(
`Failed to download: ${response.status} ${response.statusText}`
);
}
const buffer = await response.arrayBuffer();
await fs.promises.writeFile(zipFile, Buffer.from(buffer));

console.log('Unzipping...');
await unzip(zipFile, BIN_DIR, binaryName);

await fs.promises.unlink(zipFile);

console.log(`Making ${binaryPath} executable...`);
if (!isWindows) {
await fs.promises.chmod(binaryPath, 0o755);
}

console.log('Done!');
} catch (err) {
console.error(`Failed to install protoc-gen-js: ${err.message}`);
process.exit(1);
}
}

main();
5 changes: 5 additions & 0 deletions protoc_plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const path = require("path");
const BIN_DIR = path.resolve(__dirname, "bin");
const EXT = process.platform === "win32" ? ".exe" : "";

module.exports = path.resolve(BIN_DIR, "protoc-gen-js" + EXT);
32 changes: 32 additions & 0 deletions protoc_plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions protoc_plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@protocolbuffers/protoc-gen-js",
"version": "4.0.0",
"description": "Standalone distribution of the protoc-gen-js plugin for Protocol Buffers",
"author": "Google Protocol Buffers Team",
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "https://github.com/protocolbuffers/protobuf-javascript.git"
},
"type": "commonjs",
"main": "index.js",
"bin": {
"protoc-gen-js": "cli.js"
},
"scripts": {
"postinstall": "node download-protoc-gen-js.js"
},
"dependencies": {
"adm-zip": "^0.5.16"
},
"engines": {
"node": ">=18.0.0"
}
}