Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ nodeLinker: node-modules
npmAuditRegistry: "https://registry.npmjs.org"

yarnPath: .yarn/releases/yarn-4.18.1.cjs

npmPreapprovedPackages:
- '@napi-rs/*'
- '@emnapi/*'
- '@oxc-node/*'
- emnapi
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ export declare class Builder {
}

/**
* This type implements JavaScript's iterable iterator protocol.
* On runtimes with `Iterator` helpers, its prototype also inherits those helpers.
* This type extends JavaScript's `Iterator`, and so has the iterator helper
* methods. It may extend the upcoming TypeScript `Iterator` class in the future.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_methods
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_and_iterable_protocols
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-6.html#iterator-helper-methods
*/
export declare class Entries {

Expand Down
59 changes: 58 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ const { readFileSync } = require('fs')
let nativeBinding = null
// Which artifact actually loaded. The WASI fallback chain overwrites it with
// the flavor it resolved; the late native retry below leaves it alone because
// it only runs while no WASI candidate has been loaded.
// it only runs while no WASI candidate has been loaded. The take-over check
// after the load-failure block can also set it.
let __napiLoadedBindingTarget = 'native'
// What this loader's own `requireNative()` calls returned. The check after
// the load-failure block compares it with `nativeBinding` to tell a binding
// this loader loaded from one that code outside it put in its place.
let __napiRequiredNativeBinding = null
const loadErrors = []

const isMusl = () => {
Expand Down Expand Up @@ -269,6 +274,22 @@ function requireNative() {
} catch (e) {
loadErrors.push(e)
}
} else if (process.arch === 'ppc64') {
try {
return require('./tar.freebsd-ppc64.node')
} catch (e) {
loadErrors.push(e)
}
try {
const binding = require('@napi-rs/tar-freebsd-ppc64')
const bindingPackageVersion = require('@napi-rs/tar-freebsd-ppc64/package.json').version
if (bindingPackageVersion !== '1.1.1' && process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0') {
throw new Error(`Native binding package version mismatch, expected 1.1.1 but got ${bindingPackageVersion}. You can reinstall dependencies to fix this issue.`)
}
return binding
} catch (e) {
loadErrors.push(e)
}
} else {
loadErrors.push(new Error(`Unsupported architecture on FreeBSD: ${process.arch}`))
}
Expand Down Expand Up @@ -585,6 +606,7 @@ const forceWasi =

if (!forceWasi) {
nativeBinding = requireNative()
__napiRequiredNativeBinding = nativeBinding
}

if (!nativeBinding || forceWasi) {
Expand Down Expand Up @@ -686,6 +708,7 @@ if (!nativeBinding || forceWasi) {
!__napiWasiFlavorRequested
) {
nativeBinding = requireNative()
__napiRequiredNativeBinding = nativeBinding
}
if ((forceWasiError || __napiWasiFlavorRequested) && !wasiBindingLoaded) {
const error = new Error(
Expand Down Expand Up @@ -713,6 +736,40 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

// A package can patch its own fallback in front of the load-failure check
// above, e.g. a WebContainer download through `napi-postinstall/fallback`.
// That fallback swaps `nativeBinding` for a generated WASI loader, which has
// already stamped its own flavor, while the target above still says 'native'.
// Take that flavor over, or the CommonJS stamp throws a conflict and the ESM
// export reports the wrong artifact. Three limits keep the conflict check:
// - only a binding this loader did not load itself, so a native addon whose
// `#[napi(module_exports)]` hook claims the name is still rejected;
// - only an own data property, read through its descriptor, so no addon
// getter runs here, and it must read back the same, as the stamp reads it;
// - only a flavor in `__napiWasiFlavors`, the ones this loader's own WASI
// chain can report ('wasm32-wasi' for a package without a WASI target).
// This is metadata and never fails a load: a throwing Proxy trap keeps 'native'.
if (
__napiLoadedBindingTarget === 'native' &&
nativeBinding !== __napiRequiredNativeBinding
) {
try {
const __napiBindingTargetDescriptor = Object.getOwnPropertyDescriptor(
nativeBinding,
'__napiBindingTarget',
)
if (
__napiBindingTargetDescriptor &&
__napiWasiFlavors.indexOf(__napiBindingTargetDescriptor.value) !== -1 &&
nativeBinding.__napiBindingTarget === __napiBindingTargetDescriptor.value
) {
__napiLoadedBindingTarget = __napiBindingTargetDescriptor.value
}
} catch {
// keep 'native'
}
}

function __napiStampBindingTarget(exportsObject, target) {
if (
Object.prototype.hasOwnProperty.call(exportsObject, '__napiBindingTarget')
Expand Down
34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@
"version": "napi version"
},
"devDependencies": {
"@emnapi/core": "2.0.0-alpha.6",
"@emnapi/runtime": "2.0.0-alpha.6",
"@napi-rs/cli": "^3.10.8",
"@napi-rs/lzma": "^1.4.5",
"@napi-rs/wasm-runtime": "^1.2.5",
"@oxc-node/core": "^1.0.0",
"@emnapi/core": "2.0.0-beta.3",
"@emnapi/runtime": "2.0.0-beta.3",
"@napi-rs/cli": "^3.10.10",
"@napi-rs/lzma": "^1.5.1",
"@napi-rs/wasm-runtime": "^1.2.7",
"@oxc-node/core": "^1.0.1",
"@taplo/cli": "^0.7.0",
"@types/node": "^26.1.1",
"@types/node": "^26.6.6",
"@types/tar": "^7.0.87",
"ava": "^8.0.1",
"chalk": "^6.0.0",
"emnapi": "2.0.0-alpha.6",
"ava": "^8.3.0",
"chalk": "^6.0.1",
"emnapi": "2.0.0-beta.3",
"husky": "^9.1.7",
"lint-staged": "^17.0.8",
"npm-run-all2": "^9.0.2",
"oxlint": "^1.73.0",
"prettier": "^3.9.5",
"tar": "^7.5.20",
"tinybench": "^6.0.2",
"typescript": "^7.0.0"
"lint-staged": "^17.6.0",
"npm-run-all2": "^9.0.4",
"oxlint": "^1.90.0",
"prettier": "^3.9.10",
"tar": "^7.5.22",
"tinybench": "^6.2.3",
"typescript": "^7.0.2"
},
"lint-staged": {
"*.@(js|ts|tsx)": [
Expand Down
Loading
Loading