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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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
38 changes: 31 additions & 7 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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 +617,39 @@ 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 withDirname =
node.arguments.length === 1 &&
node.arguments[0].type === 'Identifier' &&
node.arguments[0].name === '__dirname';

// handle case: require('node-gyp-build')(path.join(__dirname, '..'))
const withPathJoinDirname =
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 (knownBindings.__dirname.shadowDepth === 0 && (withDirname || withPathJoinDirname)) {

const pathJoinedDir = withPathJoinDirname
? path.join(dir, node.arguments[0].arguments[1].value)
: dir;

let resolved: string | undefined;
try {
// the pkg could be 'node-gyp-build' or '@aminya/node-gyp-build'
const pkgName = node.callee.arguments[0].value;
// 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)
}
catch (e) {
const nodeGypBuildPath = resolveFrom(pathJoinedDir, pkgName)
resolved = require(nodeGypBuildPath).path(pathJoinedDir)
} catch (e) {
try {
resolved = nodeGypBuild.path(dir);
resolved = nodeGypBuild.path(pathJoinedDir);
} 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;
4 changes: 2 additions & 2 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ 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
sortedFileList = sortedFileList.filter(file => {
if (file.endsWith('node-napi.node') || file.endsWith('node.napi.node')) {
if (file.includes('prebuilds') && file.endsWith('.node')) {
// 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