Skip to content

Commit 3734567

Browse files
committed
Add GitHub token support for private repo installs
1 parent 7abfb5a commit 3734567

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

scripts/postinstall.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,40 @@ function getVersion() {
4040
return pkg.version;
4141
}
4242

43-
function httpsGet(url) {
43+
function getGitHubToken() {
44+
// Check environment variables (in order of preference)
45+
return (
46+
process.env.GITHUB_TOKEN ||
47+
process.env.GH_TOKEN ||
48+
process.env.GITHUB_PAT ||
49+
null
50+
);
51+
}
52+
53+
function httpsGet(url, token = null) {
4454
return new Promise((resolve, reject) => {
55+
const headers = { "User-Agent": "relay-dedup-installer" };
56+
57+
// Add auth header for private repos
58+
if (token) {
59+
headers["Authorization"] = `token ${token}`;
60+
}
61+
62+
// For GitHub release assets, we need to accept the binary
63+
if (url.includes("github.com") && url.includes("/releases/download/")) {
64+
headers["Accept"] = "application/octet-stream";
65+
}
66+
4567
https
46-
.get(url, { headers: { "User-Agent": "relay-dedup-installer" } }, (res) => {
47-
// Follow redirects
68+
.get(url, { headers }, (res) => {
69+
// Follow redirects (pass token only for same-origin redirects)
4870
if (res.statusCode === 301 || res.statusCode === 302) {
49-
return httpsGet(res.headers.location).then(resolve).catch(reject);
71+
const redirectUrl = res.headers.location;
72+
// Don't pass token to external domains (like S3)
73+
const sameOrigin = redirectUrl.includes("github.com");
74+
return httpsGet(redirectUrl, sameOrigin ? token : null)
75+
.then(resolve)
76+
.catch(reject);
5077
}
5178
if (res.statusCode !== 200) {
5279
reject(new Error(`HTTP ${res.statusCode}: ${url}`));
@@ -64,14 +91,18 @@ function httpsGet(url) {
6491
async function downloadBinary() {
6592
const target = getTarget();
6693
const version = getVersion();
94+
const token = getGitHubToken();
6795
const assetName = `${BINARY_NAME}-${target}.tar.gz`;
6896
const url = `https://github.com/${REPO}/releases/download/v${version}/${assetName}`;
6997

7098
console.log(`Downloading ${BINARY_NAME} v${version} for ${getPlatformKey()}...`);
7199
console.log(` ${url}`);
100+
if (token) {
101+
console.log(` (using GitHub token for authentication)`);
102+
}
72103

73104
try {
74-
const tarGz = await httpsGet(url);
105+
const tarGz = await httpsGet(url, token);
75106

76107
// Extract tar.gz
77108
const tar = zlib.gunzipSync(tarGz);
@@ -93,6 +124,11 @@ async function downloadBinary() {
93124
if (error.message.includes("404")) {
94125
console.error(`\nError: No prebuilt binary found for ${getPlatformKey()}`);
95126
console.error(`Release v${version} may not exist or may not have binaries yet.`);
127+
if (!token) {
128+
console.error(`\nThis is a private repo - you need to set GITHUB_TOKEN:`);
129+
console.error(` export GITHUB_TOKEN=ghp_your_token_here`);
130+
console.error(` pnpm install`);
131+
}
96132
console.error(`\nYou can build from source with: cargo build --release`);
97133
} else {
98134
console.error(`\nError downloading binary: ${error.message}`);

0 commit comments

Comments
 (0)