Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Add cross-platform support for dot-node files. (#1066)
Browse files Browse the repository at this point in the history
* feat: use prebuild-install to fetch platform specific node files

* fix: adjust test results to remove minor differences in error output

* fix: remove copy-paste from tests as it just hangs
  • Loading branch information
david-mohr authored Mar 19, 2021
1 parent 55cda5f commit 3b947c8
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 26 deletions.
12 changes: 0 additions & 12 deletions dictionary/copy-paste.js

This file was deleted.

61 changes: 60 additions & 1 deletion lib/producer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { STORE_BLOB, STORE_CONTENT, snapshotify } from '../prelude/common.js';
import { STORE_BLOB, STORE_CONTENT, isDotNODE, snapshotify } from '../prelude/common.js';
import { log, wasReported } from './log.js';
import Multistream from 'multistream';
import assert from 'assert';
import { execSync } from 'child_process';
import { fabricateTwice } from './fabricator.js';
import fs from 'fs';
import intoStream from 'into-stream';
import path from 'path';
import streamMeter from 'stream-meter';

function discoverPlaceholder (binaryBuffer, searchString, padder) {
Expand Down Expand Up @@ -74,6 +76,53 @@ function makePreludeBufferFromPrelude (prelude) {
);
}

function findPackageJson (nodeFile) {
let dir = nodeFile;
while (dir !== '/') {
dir = path.dirname(dir);
if (fs.existsSync(path.join(dir, 'package.json'))) {
break;
}
}
if (dir === '/') {
throw new Error(`package.json not found for "${nodeFile}"`);
}
return dir;
}

const platform = {
macos: 'darwin',
win: 'win32',
linux: 'linux'
};

function nativePrebuildInstall (target, nodeFile) {
const prebuild = path.join(__dirname, '../node_modules/.bin/prebuild-install');
const dir = findPackageJson(nodeFile);
// parse the target node version from the binaryPath
const nodeVersion = path.basename(target.binaryPath).split('-')[1];
if (!/^v[0-9]+\.[0-9]+\.[0-9]+$/.test(nodeVersion)) {
throw new Error(`Couldn't find node version, instead got: ${nodeVersion}`);
}
// prebuild-install will overwrite the target .node file. Instead, we're
// going to:
// * Take a backup
// * run prebuild
// * move the prebuild to a new name with a platform/version extension
// * put the backed up file back
const nativeFile = `${nodeFile}.${target.platform}.${nodeVersion}`;
if (fs.existsSync(nativeFile)) {
return nativeFile;
}
if (!fs.existsSync(`${nodeFile}.bak`)) {
fs.copyFileSync(nodeFile, `${nodeFile}.bak`);
}
execSync(`${prebuild} -t ${nodeVersion} --platform ${platform[target.platform]} --arch ${target.arch}`, { cwd: dir });
fs.copyFileSync(nodeFile, nativeFile);
fs.copyFileSync(`${nodeFile}.bak`, nodeFile);
return nativeFile;
}

export default function ({ backpack, bakes, slash, target }) {
return new Promise((resolve, reject) => {
if (!Buffer.alloc) {
Expand Down Expand Up @@ -166,6 +215,16 @@ export default function ({ backpack, bakes, slash, target }) {
}

assert.strictEqual(stripe.store, STORE_CONTENT); // others must be buffers from walker
if (isDotNODE(stripe.file)) {
try {
const platformFile = nativePrebuildInstall(target, stripe.file);
if (fs.existsSync(platformFile)) {
return cb(undefined, pipeToNewMeter(fs.createReadStream(platformFile)));
}
} catch (err) {
log.debug(`prebuild-install failed[${stripe.file}]:`, err);
}
}
return cb(undefined, pipeToNewMeter(fs.createReadStream(stripe.file)));
} else {
assert(false, 'producer: bad stripe');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"minimist": "^1.2.5",
"multistream": "^2.1.1",
"pkg-fetch": "^2.6.9",
"prebuild-install": "6.0.1",
"progress": "^2.0.3",
"resolve": "^1.15.1",
"stream-meter": "^1.0.4"
Expand Down
11 changes: 11 additions & 0 deletions test/test-50-fs-runtime-layer-2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ right = right.split('\n');
// right may have less lines, premature exit,
// less trusted, so using left.length here
for (let i = 0; i < left.length; i += 1) {
if (/is out of range/.test(right[i])) {
let rval = right[i].replace('>= 0 && ', '');
if (left[i] === rval) {
right[i] = rval;
} else {
rval = right[i].replace(/ && <= [0-9]/, '');
if (left[i] === rval) {
right[i] = rval;
}
}
}
assert.strictEqual(left[i], right[i]);
}

Expand Down
6 changes: 0 additions & 6 deletions test/test-79-npm/copy-paste/copy-paste.js

This file was deleted.

1 change: 0 additions & 1 deletion test/test-79-npm/copy-paste/package.json

This file was deleted.

Loading

0 comments on commit 3b947c8

Please sign in to comment.