Skip to content
Closed
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
11 changes: 11 additions & 0 deletions .changeset/loud-pandas-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"react-native-node-api": patch
---

Fixed the Babel plugin rewriting `require('./foo')` to load a Node-API addon
even when a same-named `foo.js`/`.cjs`/`.mjs`/`.json` file exists alongside it
— that source file is what Node's own `require()` resolves to, so the addon
was never reachable at runtime through that specific call, only through an
explicit `require('./foo.node')`.

Also exported `escapeBundleIdentifier` from the package's `node` entrypoint.
3 changes: 2 additions & 1 deletion packages/host/src/node/babel-plugin/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ describe("plugin", () => {
itTransforms("and does not touch required JS files", {
files: {
"package.json": `{ "name": "my-package" }`,
// TODO: Add a ./my-addon.node to make this test complete
"my-addon.js": "// Some JS file",
"my-addon.apple.node/my-addon.node":
"// This is supposed to be a binary file",
"index.js": `
const addon = require('./my-addon');
console.log(addon);
Expand Down
1 change: 1 addition & 0 deletions packages/host/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export {
createXCframework,
createUniversalAppleLibrary,
determineXCFrameworkFilename,
escapeBundleIdentifier,
} from "./prebuilds/apple.js";

export {
Expand Down
12 changes: 12 additions & 0 deletions packages/host/src/node/path-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@ export type NamingStrategy = {
// Cache mapping package directory to package name across calls
const packageNameCache = new Map<string, string>();

// Extensions Node's own require() resolves before ever trying `.node`.
const COLLIDING_SOURCE_EXTENSIONS = [".js", ".cjs", ".mjs", ".json"];

/**
* @param modulePath Batch-scans the path to the module to check (must be extensionless or end in .node)
* @returns True if a platform specific prebuild exists for the module path, warns on unreadable modules.
* @throws If the parent directory cannot be read, or if a detected module is unreadable.
* TODO: Consider checking for a specific platform extension.
*/
export function isNodeApiModule(modulePath: string): boolean {
if (
!modulePath.endsWith(".node") &&
COLLIDING_SOURCE_EXTENSIONS.some((extension) =>
fs.existsSync(modulePath + extension),
)
) {
// An explicit require('./foo.node') has no such ambiguity to defer to.
return false;
}
{
// HACK: Take a shortcut (if applicable): existing `.node` files are addons
try {
Expand Down
4 changes: 3 additions & 1 deletion packages/node-addon-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"weak-node-api": "workspace:*"
},
"dependencies": {
"@expo/plist": "0.4.7",
"assert": "^2.1.0",
"react-native-node-api": "workspace:*"
"react-native-node-api": "workspace:*",
"zod": "^4.1.11"
}
}
42 changes: 40 additions & 2 deletions packages/node-addon-examples/scripts/verify-prebuilds.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ import fs from "node:fs";
import assert from "node:assert/strict";
import path from "node:path";

import plistModule from "@expo/plist";
import { escapeBundleIdentifier } from "react-native-node-api";
import { z } from "zod";

import { DIRS } from "./cmake-projects.mjs";

// @expo/plist is CJS with an `export default`; under Node's ESM/CJS interop
// the default import binds to the whole `module.exports`, which nests the
// real API one `.default` deeper.
const plist = plistModule.default;

const FrameworkInfoPlistSchema = z.object({
CFBundleExecutable: z.string(),
CFBundleIdentifier: z.string(),
});

const EXPECTED_ANDROID_ARCHS = ["armeabi-v7a", "arm64-v8a", "x86_64", "x86"];

const EXPECTED_XCFRAMEWORK_PLATFORMS = [
Expand Down Expand Up @@ -37,6 +51,27 @@ async function verifyAndroidPrebuild(dirent: fs.Dirent) {
}
}

async function verifyFrameworkInfoPlist(
infoPlistPath: string,
libraryName: string,
) {
const contents = await fs.promises.readFile(infoPlistPath, "utf8");
const parsed = FrameworkInfoPlistSchema.parse(plist.parse(contents));
assert.equal(
parsed.CFBundleExecutable,
libraryName,
`Unexpected CFBundleExecutable in ${infoPlistPath}`,
);
assert.equal(
parsed.CFBundleIdentifier,
// Mirrors the default writeFrameworkInfoPlist derives in
// packages/host/src/node/prebuilds/apple.ts, since none of the
// examples pass --apple-bundle-identifier.
escapeBundleIdentifier(`com.callstackincubator.node-api.${libraryName}`),
`Unexpected CFBundleIdentifier in ${infoPlistPath}`,
);
}

async function verifyApplePrebuild(dirent: fs.Dirent) {
console.log("Verifying Apple prebuild", dirent.name, "in", dirent.parentPath);
for (const arch of EXPECTED_XCFRAMEWORK_PLATFORMS) {
Expand Down Expand Up @@ -65,8 +100,11 @@ async function verifyApplePrebuild(dirent: fs.Dirent) {
"Expected only directory and files in framework",
);
if (file.name === "Info.plist") {
// TODO: Verify the contents of the Info.plist file
continue;
const libraryName = path.basename(frameworkDir, ".framework");
await verifyFrameworkInfoPlist(
path.join(frameworkDir, file.name),
libraryName,
);
} else {
assert(
!file.name.endsWith(".node"),
Expand Down
26 changes: 8 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading