Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esbuild-wasm: don't create a new exit0 native module for each run #719

Merged
merged 1 commit into from
Jan 30, 2021
Merged
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
19 changes: 14 additions & 5 deletions npm/esbuild-wasm/bin/esbuild
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Forward to the automatically-generated WebAssembly loader from the Go compiler

const crypto = require('crypto');
const path = require('path');
const zlib = require('zlib');
const fs = require('fs');
Expand Down Expand Up @@ -41,12 +42,20 @@ process.on('exit', code => {
// Otherwise if the exit code is zero, try to fall back to a binary N-API module that
// calls the operating system's "exit(0)" function.
const nativeModule = `${process.platform}-${os.arch()}-${os.endianness()}.node`;
const data = require('../exit0')[nativeModule];
if (data) {
const base64 = require('../exit0')[nativeModule];
if (base64) {
try {
const tempFile = path.join(os.tmpdir(), `${Math.random().toString(36).slice(2)}-${nativeModule}`);
fs.writeFileSync(tempFile, zlib.inflateRawSync(Buffer.from(data, 'base64')));
require(tempFile);
const data = zlib.inflateRawSync(Buffer.from(base64, 'base64'));
const hash = crypto.createHash('sha256').update(base64).digest().toString('hex').slice(0, 16);
const tempFile = path.join(os.tmpdir(), `${hash}-${nativeModule}`);
try {
if (fs.readFileSync(tempFile).equals(data)) {
require(tempFile);
}
} finally {
fs.writeFileSync(tempFile, data);
require(tempFile);
}
} catch (e) {
}
}
Expand Down