Skip to content

Commit

Permalink
Add nearby resolution for zeit/pkg (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilbayes authored and mafintosh committed Nov 3, 2019
1 parent e18b05e commit f3f0e2f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ without having to compile on install time AND will work in both node and electro

Users can override `node-gyp-build` and force compiling by doing `npm install --build-from-source`.

Prebuilds will be attempted loaded from `MODULE_PATH/prebuilds/...` and then next `EXEC_PATH/prebuilds/...` (the latter allowing use with `zeit/pkg`)

This comment has been minimized.

Copy link
@jwerle

jwerle Nov 26, 2019

Is this documented anywhere else besides this README file?
We should get an example going with utp-native, sodium-native, et al running in a pkg compiled binary that loads prebuilds from EXEC_PATH/prebuilds cc @emilbayes

This comment has been minimized.

Copy link
@emilbayes

emilbayes Nov 27, 2019

Author Contributor

It's documented on pkg that it want's to load that way, but there is a collision with multiple native deps atm


## Supported prebuild names

If so desired you can bundle more specific flavors, for example `musl` builds to support Alpine, or targeting a numbered ARM architecture version.
Expand Down
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ load.path = function (dir) {
if (debug) return debug
}

// Find most specific flavor first
var prebuilds = path.join(dir, 'prebuilds', platform + '-' + arch)
var parsed = readdirSync(prebuilds).map(parseTags)
var candidates = parsed.filter(matchTags(runtime, abi))
var winner = candidates.sort(compareTags(runtime))[0]
if (winner) return path.join(prebuilds, winner.file)
var prebuild = resolve(dir)
if (prebuild) return prebuild

var nearby = resolve(path.dirname(process.execPath))
if (nearby) return nearby

var target = [
'platform=' + platform,
Expand All @@ -54,6 +53,15 @@ load.path = function (dir) {
].filter(Boolean).join(' ')

throw new Error('No native build was found for ' + target)

function resolve (dir) {
// Find most specific flavor first
var prebuilds = path.join(dir, 'prebuilds', platform + '-' + arch)
var parsed = readdirSync(prebuilds).map(parseTags)
var candidates = parsed.filter(matchTags(runtime, abi))
var winner = candidates.sort(compareTags(runtime))[0]
if (winner) return path.join(prebuilds, winner.file)
}
}

function readdirSync (dir) {
Expand Down

0 comments on commit f3f0e2f

Please sign in to comment.