Skip to content

Commit

Permalink
feat(rari-npm): add initial support for npm
Browse files Browse the repository at this point in the history
Also renames the workflows
  • Loading branch information
fiji-flo committed Oct 21, 2024
1 parent 45f66b1 commit e6ba05b
Show file tree
Hide file tree
Showing 14 changed files with 1,183 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ship binaries
name: build-and-upload-binaries

permissions:
contents: write
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: publish-npm

on:
workflow_run:
workflows: [build-and-upload-binaries]
types: [completed]

jobs:
on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- run: echo 'This would run npm publish.'
File renamed without changes.
3 changes: 3 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ See LICENSE.MPL-2.0 file for more information.

This project includes code from Comrak licensed under the BSD 2-Clause License.
See LICENSE.BSD-2-Clause file for more information.

This project includes code from vscode-ripgrep licensed under the MIT License.
See LICENSE.MIT file for more information.
13 changes: 13 additions & 0 deletions LICENSE.MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
vscode-ripgrep

Copyright (c) Microsoft Corporation

All rights reserved.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions rari-npm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bin
275 changes: 275 additions & 0 deletions rari-npm/lib/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
import { platform, tmpdir } from "os";
import { getProxyForUrl } from "proxy-from-env";
import * as https from "https";
import { createWriteStream } from "fs";
import { unlink, access, constants, mkdir, chmod } from "fs/promises";
import path from "node:path";

import { x } from "tar";
import extract from "extract-zip";

import * as packageJson from "../package.json" with { type: "json" };

const tmpDir = path.join(tmpdir(), `rari-cache-${packageJson.version}`);
const isWindows = platform() === "win32";

const REPO = "mdn/rari";

/**
* This function is adapted from vscode-ripgrep (https://github.com/microsoft/vscode-ripgrep)
* Copyright (c) Microsoft, licensed under the MIT License
*
* @param {string} url
*/
function isGithubUrl(url) {
return URL.parse(url)?.hostname === "api.github.com";
}

/**
* @param {string} path
*/
export async function exists(path) {
try {
await access(path, constants.F_OK);
return true;
} catch {
return false;
}
}

/**
* This function is adapted from vscode-ripgrep (https://github.com/microsoft/vscode-ripgrep)
* Copyright (c) Microsoft, licensed under the MIT License
*
* @param {string} url
* @param {import("fs").PathLike} dest
* @param {any} opts
*/
export async function do_download(url, dest, opts) {
const proxy = getProxyForUrl(URL.parse(url));
if (proxy !== "") {
const HttpsProxyAgent = await import("https-proxy-agent");
opts = {
...opts,
agent: new HttpsProxyAgent.HttpsProxyAgent(proxy),
proxy,
};
}

if (opts.headers && opts.headers.authorization && !isGithubUrl(url)) {
delete opts.headers.authorization;
}

return await new Promise((resolve, reject) => {
console.log(`Download options: ${JSON.stringify(opts)}`);
const outFile = createWriteStream(dest);

https
.get(url, opts, (response) => {
console.log("statusCode: " + response.statusCode);
if (response.statusCode === 302 && response.headers.location) {
console.log("Following redirect to: " + response.headers.location);
return do_download(response.headers.location, dest, opts).then(
resolve,
reject,
);
} else if (response.statusCode !== 200) {
reject(new Error("Download failed with " + response.statusCode));
return;
}

response.pipe(outFile);
outFile.on("finish", () => {
resolve(null);
});
})
.on("error", async (err) => {
await unlink(dest);
reject(err);
});
});
}

/**
* This function is adapted from vscode-ripgrep (https://github.com/microsoft/vscode-ripgrep)
* Copyright (c) Microsoft, licensed under the MIT License
*
* @param {string} _url
* @param {any} opts
*/
function get(_url, opts) {
console.log(`GET ${_url}`);

const proxy = getProxyForUrl(URL.parse(_url));
if (proxy !== "") {
var HttpsProxyAgent = require("https-proxy-agent");
opts = {
...opts,
agent: new HttpsProxyAgent.HttpsProxyAgent(proxy),
};
}

return new Promise((resolve, reject) => {
let result = "";
https.get(_url, opts, (response) => {
if (response.statusCode !== 200) {
reject(new Error("Request failed: " + response.statusCode));
}

response.on("data", (d) => {
result += d.toString();
});

response.on("end", () => {
resolve(result);
});

response.on("error", (e) => {
reject(e);
});
});
});
}

/**
* @param {string} repo
* @param {string} tag
*/
function getApiUrl(repo, tag) {
return `https://api.github.com/repos/${repo}/releases/tags/${tag}`;
}

/**
* This function is adapted from vscode-ripgrep (https://github.com/microsoft/vscode-ripgrep)
* Copyright (c) Microsoft, licensed under the MIT License
*
* @param {{ force: boolean; token: string | undefined; version: string; }} opts
* @param {string} assetName
* @param {string} downloadFolder
*/
async function getAssetFromGithubApi(opts, assetName, downloadFolder) {
const assetDownloadPath = path.join(downloadFolder, assetName);

// We can just use the cached binary
if (!opts.force && (await exists(assetDownloadPath))) {
console.log("Using cached download: " + assetDownloadPath);
return assetDownloadPath;
}

const downloadOpts = {
headers: {
"user-agent": "rari-npm",
},
};

if (opts.token) {
downloadOpts.headers.authorization = `token ${opts.token}`;
}

console.log(`Finding release for ${opts.version}`);
const release = await get(getApiUrl(REPO, opts.version), downloadOpts);
let jsonRelease;
try {
jsonRelease = JSON.parse(release);
} catch (e) {
throw new Error("Malformed API response: " + e.stack);
}

if (!jsonRelease.assets) {
throw new Error("Bad API response: " + JSON.stringify(release));
}

const asset = jsonRelease.assets.find((a) => a.name === assetName);
if (!asset) {
throw new Error("Asset not found with name: " + assetName);
}

console.log(`Downloading from ${asset.url}`);
console.log(`Downloading to ${assetDownloadPath}`);

downloadOpts.headers.accept = "application/octet-stream";
await do_download(asset.url, assetDownloadPath, downloadOpts);
}

/**
* @param {string} packedFilePath
* @param {string} destinationDir
*/
async function unpack(packedFilePath, destinationDir) {
const rari_name = "rari";
if (isWindows) {
await extract(packedFilePath, { dir: destinationDir });
} else {
await x({ cwd: destinationDir, file: packedFilePath });
}

const expectedName = path.join(destinationDir, rari_name);
if (await exists(expectedName)) {
return expectedName;
}

if (await exists(expectedName + ".exe")) {
return expectedName + ".exe";
}

throw new Error(
`Expecting ${rari_name} or ${rari_name}.exe unzipped into ${destinationDir}, didn't find one.`,
);
}

/**
* This function is adapted from vscode-ripgrep (https://github.com/microsoft/vscode-ripgrep)
* Copyright (c) Microsoft, licensed under the MIT License
*
* @param {{
version: string;
token: string | undefined;
target: string;
destDir: string;
force: boolean;
}} options
*/
export async function download(options) {
if (!options.version) {
return Promise.reject(new Error("Missing version"));
}

if (!options.target) {
return Promise.reject(new Error("Missing target"));
}

const extension = isWindows ? ".zip" : ".tar.gz";
const assetName = ["rari", options.target].join("-") + extension;

if (!(await exists(tmpDir))) {
await mkdir(tmpDir);
}

const assetDownloadPath = path.join(tmpDir, assetName);
try {
await getAssetFromGithubApi(options, assetName, tmpDir);
} catch (e) {
console.log("Deleting invalid download cache");
try {
await unlink(assetDownloadPath);
} catch (e) {}

throw e;
}

console.log(`Unpacking to ${options.destDir}`);
try {
const destinationPath = await unpack(assetDownloadPath, options.destDir);
if (!isWindows) {
await chmod(destinationPath, "755");
}
} catch (e) {
console.log("Deleting invalid download");

try {
await unlink(assetDownloadPath);
} catch (e) {}

throw e;
}
}
1 change: 1 addition & 0 deletions rari-npm/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const rariBin: string;
6 changes: 6 additions & 0 deletions rari-npm/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { join } from "node:path";

export const rariBin = join(
import.meta.dirname,
`../bin/rari${process.platform === "win32" ? ".exe" : ""}`,
);
69 changes: 69 additions & 0 deletions rari-npm/lib/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { mkdir } from "fs/promises";
import { arch, platform } from "os";
import { join } from "path";

import { download, exists } from "./download.js";

const VERSION = "v0.0.12";
const BIN_PATH = join(import.meta.dirname, "../bin");
const FORCE = JSON.parse(process.env.FORCE || "false");
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;

const TARGET_LOOKUP = {
arm64: {
darwin: "aarch64-apple-darwin",
linux: "aarch64-unknown-linux-musl",
win32: "arch64-pc-windows-msvc",
},
x64: {
darwin: "x86_64-apple-darwin",
linux: "x86_64-unknown-linux-musl",
win32: "x86_64-pc-windows-msvc",
},
};

async function getTarget() {
const architecture = process.env.npm_config_arch || arch();

const target = TARGET_LOOKUP[architecture]?.[platform()];
if (!target) {
throw new Error("Unknown platform: " + platform());
}
return target;
}

/**
* This function is adapted from vscode-ripgrep (https://github.com/microsoft/vscode-ripgrep)
* Copyright (c) Microsoft, licensed under the MIT License
*
*/
async function main() {
const binPathExists = await exists(BIN_PATH);
if (!FORCE && binPathExists) {
console.log(
`${BIN_PATH} already exists, exiting use FORCE=true to force install`,
);
process.exit(0);
}

if (!binPathExists) {
await mkdir(BIN_PATH);
}

const target = await getTarget();
const options = {
version: VERSION,
token: GITHUB_TOKEN,
target,
destDir: BIN_PATH,
force: FORCE,
};
try {
await download(options);
} catch (err) {
console.error(`Downloading rari failed: ${err.stack}`);
process.exit(1);
}
}

await main();
Loading

0 comments on commit e6ba05b

Please sign in to comment.