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

Binary tools cache & load from buffer #29

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 20 additions & 5 deletions scripts/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@ if (fs.existsSync(util.bindir)) {
process.exit(0);
}

var platform = process.platform + "-" + process.arch,
var platform = process.platform + "-" + process.arch,
isWindows = /^win32/.test(platform),
temp = tmp.fileSync({ prefix: "wa-tools-" }),
file = fs.createWriteStream(temp.name),
archive = "tools-" + platform + (isWindows ? ".zip" : ".tar.gz");
temp = tmp.fileSync({ prefix: "wa-tools-" }),
file = fs.createWriteStream(temp.name),
archive = "tools-" + platform + (isWindows ? ".zip" : ".tar.gz");

function cachedDownload(callback) {
var tmpDir = ['','tmp'].join(path.sep);
var cacheDir = [tmpDir,'webassembly',pkg.tools].join(path.sep);
var cacheFile = [cacheDir,archive].join(path.sep);
if (fs.existsSync(cacheFile)) return callback(null,cacheFile);
if (!fs.existsSync(tmpDir)) return download(callback);
fs.mkdirSync(cacheDir, { recursive: true });

download(function(err, filename) {
if (err) return callback(err);
fs.copyFileSync(filename,cacheFile);
return callback(null,filename);
});
}

function download(callback) {
var req = https.get("https://github.com/dcodeIO/webassembly/releases/download/" + pkg.tools + "/" + archive, res => {
Expand Down Expand Up @@ -62,7 +77,7 @@ function install(file, callback) {

util.printLogo("Setup");
process.stdout.write(chalk.white.bold("Downloading binaries for " + platform + " ...") + "\n");
download(function(err, file) {
cachedDownload(function(err, file) {
if (err) {
process.stderr.write("\n");
throw err;
Expand Down
31 changes: 23 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ var getOwnPropertyNames = Object.getOwnPropertyNames;

/**
* Loads a WebAssembly.
* @param {string} file File name
* @param {LoadOptions} [options] Options
*
* @param {Buffer} assembly WebAssembly buffer
* @param {LoadOptions} [options] Options
*
* @returns {Promise.<IModule>} Promise resolving to the instantiated module
*/
function load(file, options) {
function load_buffer(assembly, options) {

/**
* Options as used by {@link load}.
Expand Down Expand Up @@ -198,16 +200,15 @@ function load(file, options) {
// Add default exit listeners if not explicitly imported

if (!env._abort)
env._abort = errno => { throw Error("abnormal abort in " + file + ": " + errno); };
env._abort = errno => { throw Error("abnormal abort: " + errno); };
if (!env._exit)
env._exit = code => { if (code) throw Error("abnormal exit in " + file + ": " + code); }
env._exit = code => { if (code) throw Error("abnormal exit: " + code); }

// Finally, fetch the assembly and instantiate it

env._grow = grow;

return (typeof fetch === "function" && fetch || fetch_node)(file)
.then(result => result.arrayBuffer())
return Promise.resolve(Buffer.from(assembly))
.then(buffer => WebAssembly.instantiate(buffer, { env: env }))
.then(module => {
var instance = module.instance;
Expand All @@ -218,7 +219,21 @@ function load(file, options) {
});
}

exports.load = load;
exports.load_buffer = load_buffer;

/**
* Loads a WebAssembly.
*
* @param {string} filename The file to load
* @param {LoadOptions} [options] Options
*
* @returns {Promise.<IModule>} Promise resolving to the instantiated module
*/
exports.load = function( filename, options ) {
return ('function' === typeof fetch && fetch || fetch_node)(filename)
.then(result => result.arrayBuffer())
.then(buffer => load_buffer(buffer, options));
};

// Internal fetch API polyfill for node that doesn't trigger webpack
var fs;
Expand Down