Skip to content

Commit

Permalink
feat(helper): display addtional info while loading bindings failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Jun 8, 2021
1 parent 7c84df2 commit cfaaf27
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
8 changes: 4 additions & 4 deletions packages/bcrypt/src/lib_bcrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,15 @@ mod tests {
fn forbid_null_bytes() {
fn assert_invalid_password(password: &[u8]) {
match hash(password, DEFAULT_COST) {
Ok(_) => panic!(format!(
Ok(_) => panic!(
"NULL bytes must be forbidden, but {:?} is allowed.",
password
)),
),
Err(BcryptError::InvalidPassword) => {}
Err(e) => panic!(format!(
Err(e) => panic!(
"NULL bytes are forbidden but error differs: {} for {:?}.",
e, password
)),
),
}
}
assert_invalid_password(b"\0");
Expand Down
2 changes: 0 additions & 2 deletions packages/deno-lint/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { platform } = require('os')

const { loadBinding } = require('@node-rs/helper')

const binding = loadBinding(__dirname, 'deno-lint', '@node-rs/deno-lint')
Expand Down
38 changes: 29 additions & 9 deletions packages/helper/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'fs'
import { existsSync, readdirSync } from 'fs'
import { platform, arch } from 'os'
import { join } from 'path'

Expand All @@ -9,25 +9,45 @@ const PlatformName = platform()

export function loadBinding(dirname: string, filename = 'index', packageName?: string) {
const triples = platformArchTriples[PlatformName][ArchName]
let additionalErrorMsg = ''
for (const triple of triples) {
// resolve in node_modules
if (packageName) {
try {
return require(require.resolve(`${packageName}-${triple.platformArchABI}`, { paths: [dirname] }))
// eslint-disable-next-line no-empty
} catch (e) {}
} catch (e) {
if (e?.code !== 'MODULE_NOT_FOUND') {
try {
const pkgPath = require.resolve(`${packageName}-${triple.platformArchABI}`, { paths: [dirname] })
additionalErrorMsg += `file: ${pkgPath} existed but error occurred while require it: ${e.message ?? e} \n`
// eslint-disable-next-line no-empty
} catch {}
}
}
}
const localFilePath = join(dirname, `${filename}.${triple.platformArchABI}.node`)
if (existsSync(localFilePath)) {
return require(localFilePath)
}
}

const errorMsg = `Can not find node binding files from ${
packageName ? triples.map((triple) => `${packageName}-${triple.platformArchABI}`).join(', ') : ''
} ${packageName ? 'and ' : ''}${triples
.map((triple) => join(dirname, `${filename}.${triple.platformArchABI}.node`))
.join(', ')}`
let packageList = ''

throw new TypeError(errorMsg)
if (packageName) {
try {
// @swc/core => core
// awesome-package => awesome-package
const packageNameWithoutNamespace = packageName.split('/').pop()!
packageList = readdirSync(join(require.resolve(packageName, { paths: [dirname] }), '..', '..'))
.filter((d) => d !== packageNameWithoutNamespace && d.startsWith(packageNameWithoutNamespace))
.join(', ')
// eslint-disable-next-line no-empty
} catch {}
}

const errorMsg = `Can not load bindings${additionalErrorMsg ? ', ' + additionalErrorMsg : '\n'}${
packageList ? 'Installed packages: [' + packageList + ']' : ''
}`

throw new Error(errorMsg)
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4908,6 +4908,7 @@ minipass-fetch@^1.3.0, minipass-fetch@^1.3.2:
resolved "https://registry.npmjs.org/minipass-fetch/-/minipass-fetch-1.3.3.tgz#34c7cea038c817a8658461bf35174551dce17a0a"
integrity sha512-akCrLDWfbdAWkMLBxJEeWTdNsjML+dt5YgOI4gJ53vuO0vrmYQkUPxa6j6V65s9CcePIr2SSWqjT2EcrNseryQ==
dependencies:
encoding "^0.1.12"
minipass "^3.1.0"
minipass-sized "^1.0.3"
minizlib "^2.0.0"
Expand Down

0 comments on commit cfaaf27

Please sign in to comment.