Skip to content

Commit

Permalink
fix #286: install script supports custom registry
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 25, 2020
1 parent 95cff1e commit 2241b21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

Previously `(0, this.fn)()` was incorrectly minified to `this.fn()`, which changes the value of `this` used for the function call. Now syntax like this is preserved during minification.

* Install script now respects the npm registry setting ([#286](https://github.com/evanw/esbuild/issues/286))

If you have configured npm to use a custom registry using `npm config set registry <url>` or by installing esbuild using `npm install --registry=<url> ...`, this custom registry URL should now be respected by the esbuild install script.

Specifically, the install script now uses the URL from the `npm_config_registry` environment variable if present instead of the default registry URL `https://registry.npmjs.org/`. Note that the URL must have both a protocol and a host name.

## 0.6.5

* Fix IIFE wrapper for ES5
Expand Down
49 changes: 36 additions & 13 deletions npm/esbuild/install.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const fs = require('fs');
const os = require('os');
const url = require('url');
const path = require('path');
const zlib = require('zlib');
const https = require('https');
const version = require('./package.json').version;
const binPath = path.join(__dirname, 'bin', 'esbuild');
const stampPath = path.join(__dirname, 'stamp.txt');

function die(text, err) {
err = err && err.message || err;
console.error(`error: ${text}${err ? ': ' + err : ''}`);
process.exit(1);
}

function installBinaryFromPackage(package, fromPath, toPath) {
// It turns out that some package managers (e.g. yarn) sometimes re-run the
// postinstall script for this package after we have already been installed.
Expand All @@ -17,26 +24,43 @@ function installBinaryFromPackage(package, fromPath, toPath) {
}

// Download the package from npm
let url = `https://registry.npmjs.org/${package}/-/${package}-${version}.tgz`
downloadURL(url, buffer => {
let pathname = `/${package}/-/${package}-${version}.tgz`;
let registry = url.parse('https://registry.npmjs.org/');
try {
let env = url.parse(process.env.npm_config_registry || '');
if (env.protocol !== null && env.host !== null) registry = env;
} catch (e) {
}
let packageURL = url.format({ protocol: registry.protocol, host: registry.host, pathname });
downloadURL(packageURL, (err, buffer) => {
if (err) die(`Failed to download ${JSON.stringify(packageURL)}`, err);

// Extract the binary executable from the package
fs.writeFileSync(toPath, extractFileFromTarGzip(buffer, fromPath));
fs.writeFileSync(toPath, extractFileFromTarGzip(packageURL, buffer, fromPath));

// Mark the operation as successful so this script is idempotent
fs.writeFileSync(stampPath, '');
});
}

function downloadURL(url, done) {
https.get(url, res => {
let chunks = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => done(Buffer.concat(chunks)));
}).on('error', err => { throw err; });
try {
https.get(url, res => {
let chunks = [];
res.on('data', chunk => chunks.push(chunk));
res.on('end', () => done(null, Buffer.concat(chunks)));
}).on('error', err => done(err));
} catch (err) {
done(err);
}
}

function extractFileFromTarGzip(buffer, file) {
buffer = zlib.unzipSync(buffer);
function extractFileFromTarGzip(url, buffer, file) {
try {
buffer = zlib.unzipSync(buffer);
} catch (err) {
die(`Invalid gzip data in archive from ${url}`, err);
}
let str = (i, n) => String.fromCharCode(...buffer.subarray(i, i + n)).replace(/\0.*$/, '');
let offset = 0;
while (offset < buffer.length) {
Expand All @@ -48,7 +72,7 @@ function extractFileFromTarGzip(buffer, file) {
offset += (size + 511) & ~511;
}
}
throw new Error(`Could not find ${JSON.stringify(file)}`)
die(`Could not find ${JSON.stringify(file)} in archive from ${url}`);
}

function installOnUnix(package) {
Expand Down Expand Up @@ -95,7 +119,6 @@ if (process.platform === 'win32' && os.arch() === 'x64') {
if (package) {
installOnUnix(package);
} else {
console.error(`error: Unsupported platform: ${key}`);
process.exit(1);
die(`Unsupported platform: ${key}`);
}
}

0 comments on commit 2241b21

Please sign in to comment.