Skip to content

Commit

Permalink
fix(analyze): node-gyp-build not including zeromq prebuilds (#392)
Browse files Browse the repository at this point in the history
- Fixes #391 

Three things I want to point out:

1. In zeromq the way node-gyp-build is called is by passing
`path.join(__dirname, "..")` instead of `__dirname` to the
`require("node-gyp-build")(...)` call.
2. In zeromq they use `@aminya/node-gyp-build` instead of
`node-gyp-build`, particular-li version `4.5.0-aminya.5`.
3. The zeromq version that's recommended for new project is the `6`
which is beta according to @aminya https://github.com/zeromq/zeromq.js.

After those changes it resolves OK.

```
❯ node ./node_modules/@vercel/nft/out/cli.js print ./node_modules/zeromq/lib/index.js
FILELIST:
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
node_modules/zeromq/prebuilds/darwin-x64/node.napi.glibc.node <---- It worked!

```

---------

Co-authored-by: Steven <[email protected]>
  • Loading branch information
eulersson and styfle authored Feb 13, 2024
1 parent 67d76d2 commit 0598a4c
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 17 deletions.
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"
]

0 comments on commit 0598a4c

Please sign in to comment.