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

fix(analyze): node-gyp-build not including zeromq prebuilds #392

Merged
124 changes: 117 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"out"
],
"dependencies": {
"@aminya/node-gyp-build": "4.5.0-aminya.5",
"@mapbox/node-pre-gyp": "^1.0.5",
"@rollup/pluginutils": "^4.0.0",
"acorn": "^8.6.0",
Expand Down Expand Up @@ -128,7 +129,8 @@
"vm2": "^3.9.18",
"vue": "^2.6.10",
"vue-server-renderer": "^2.6.10",
"when": "^3.7.8"
"when": "^3.7.8",
"zeromq": "6.0.0-beta.19"
},
"engines": {
"node": ">=16"
Expand Down
54 changes: 49 additions & 5 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import resolve from './resolve-dependency.js';
//@ts-ignore
import nodeGypBuild from 'node-gyp-build';
//@ts-ignore
import aminyaNodeGypBuild from '@aminya/node-gyp-build';
//@ts-ignore
import mapboxPregyp from '@mapbox/node-pre-gyp';
import { Job } from './node-file-trace';
import { fileURLToPath, pathToFileURL, URL } from 'url';
Expand Down Expand Up @@ -127,6 +129,9 @@ const staticModules = Object.assign(Object.create(null), {
'node-gyp-build': {
default: NODE_GYP_BUILD
},
'@aminya/node-gyp-build': {
default: NODE_GYP_BUILD
},
'nbind': {
init: NBIND_INIT,
default: {
Expand Down Expand Up @@ -614,18 +619,57 @@ export default async function analyze(id: string, code: string, job: Job): Promi
}
break;
case NODE_GYP_BUILD:
if (node.arguments.length === 1 && node.arguments[0].type === 'Identifier' &&
node.arguments[0].name === '__dirname' && knownBindings.__dirname.shadowDepth === 0) {
// handle case: require("node-gyp-build")(__dirname)
const calledWithDirnameDirectly =
node.arguments.length === 1 &&
node.arguments[0].type === 'Identifier' &&
node.arguments[0].name === '__dirname';

// handle case: require("node-gyp-build")(path.join(__dirname, ".."))
const calledWithPathJoinDirname =
node.arguments.length === 1 &&
node.arguments[0].callee?.object?.name === "path" &&
node.arguments[0].callee?.property?.name === "join" &&
node.arguments[0].arguments.length === 2 &&
node.arguments[0].arguments[0].type === 'Identifier' &&
node.arguments[0].arguments[0].name === '__dirname' &&
node.arguments[0].arguments[1].type === 'Literal'

if ((calledWithDirnameDirectly || calledWithPathJoinDirname) &&
knownBindings.__dirname.shadowDepth === 0) {

// Resolve the path in the case of passing path.join(__dirname, "..") instead of __dirname.
const pathJoinedDir =
calledWithPathJoinDirname ? path.join(dir, node.arguments[0].arguments[1].value) : dir;

// zeromq.js uses @aminya's fork of node-gyp-build, consider it.
const validNodeGypBuildForkNames = ['node-gyp-build', '@aminya/node-gyp-build'];
if (node.callee.arguments[0].type !== 'Literal' ||
!validNodeGypBuildForkNames.includes(node.callee.arguments[0].value)) {
throw new Error("Unknown node-gyp-build fork name");
eulersson marked this conversation as resolved.
Show resolved Hide resolved
}

const nodeGypBuildForkName: 'node-gyp-build' | '@aminya/node-gyp-build' = node.callee.arguments[0].value;

let resolved: string | undefined;
try {
// use installed version of node-gyp-build since resolving
// binaries can differ among versions
const nodeGypBuildPath = resolveFrom(dir, 'node-gyp-build')
resolved = require(nodeGypBuildPath).path(dir)
const nodeGypBuildPath = resolveFrom(pathJoinedDir, nodeGypBuildForkName)
resolved = require(nodeGypBuildPath).path(pathJoinedDir)
}
catch (e) {
try {
resolved = nodeGypBuild.path(dir);
switch (nodeGypBuildForkName) {
case "node-gyp-build":
resolved = nodeGypBuild.path(pathJoinedDir);
break;
case "@aminya/node-gyp-build":
resolved = aminyaNodeGypBuild.path(pathJoinedDir);
break;
default:
throw new Error("Unknown node-gyp-build fork name");
eulersson marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (e) {}
}
if (resolved) {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/zeromq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const zmq = require("zeromq");
const sock = new zmq.Push;
8 changes: 6 additions & 2 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,14 @@ for (const { testName, isRoot } of unitTests) {
}
let sortedFileList = [...fileList].sort()

if (testName === 'microtime-node-gyp') {
if (testName === 'microtime-node-gyp' || testName === 'zeromq-node-gyp') {
let foundMatchingBinary = false
const isBinaryFnMap = {
'microtime-node-gyp': file => file.endsWith('node-napi.node') || file.endsWith('node.napi.node'),
'zeromq-node-gyp': file => file.endsWith('node.napi.glibc.node') || file.endsWith('node.napi.musl.node'),
}
sortedFileList = sortedFileList.filter(file => {
if (file.endsWith('node-napi.node') || file.endsWith('node.napi.node')) {
if (isBinaryFnMap[testName](file)) {
// remove from fileList for expected checking
// as it will differ per platform
foundMatchingBinary = true
Expand Down
2 changes: 2 additions & 0 deletions test/unit/zeromq-node-gyp/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const zeromq = require('zeromq');
const sock = new zmq.Push;
11 changes: 11 additions & 0 deletions test/unit/zeromq-node-gyp/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
"node_modules/@aminya/node-gyp-build/index.js",
"node_modules/@aminya/node-gyp-build/package.json",
"node_modules/zeromq/lib/draft.js",
"node_modules/zeromq/lib/index.js",
"node_modules/zeromq/lib/native.js",
"node_modules/zeromq/lib/util.js",
"node_modules/zeromq/package.json",
"package.json",
"test/unit/zeromq-node-gyp/input.js"
]
Loading