Skip to content

Commit 077ead2

Browse files
committed
Use package name and relative path when hashing
1 parent 7a97be9 commit 077ead2

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

packages/react-native-node-api-modules/src/node/babel-plugin/plugin.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { transformFileSync } from "@babel/core";
66

77
import { plugin } from "./plugin.js";
88
import { setupTempDirectory } from "../test-utils.js";
9+
import { getNodeApiRequireCallArgument } from "../path-utils.js";
910

1011
describe("plugin", () => {
1112
it("transforms require calls, regardless", (context) => {
@@ -37,10 +38,12 @@ describe("plugin", () => {
3738
`,
3839
});
3940

40-
const ADDON_1_REQUIRE_ARG =
41-
"@rpath/node-api-eb5c6d4b.framework/node-api-eb5c6d4b";
42-
const ADDON_2_REQUIRE_ARG =
43-
"@rpath/node-api-6c4e5fed.framework/node-api-6c4e5fed";
41+
const ADDON_1_REQUIRE_ARG = getNodeApiRequireCallArgument(
42+
path.join(tempDirectoryPath, "addon-1")
43+
);
44+
const ADDON_2_REQUIRE_ARG = getNodeApiRequireCallArgument(
45+
path.join(tempDirectoryPath, "addon-2")
46+
);
4447

4548
{
4649
const result = transformFileSync(

packages/react-native-node-api-modules/src/node/path-utils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, it } from "node:test";
33
import path from "node:path";
44

55
import {
6+
determineModuleContext,
67
isNodeApiModule,
78
replaceWithNodeExtension,
89
stripExtension,
@@ -53,3 +54,28 @@ describe("isNodeApiModule", () => {
5354
assert.equal(isNodeApiModule(path.join(tempDirectoryPath, "nope")), false);
5455
});
5556
});
57+
58+
describe("determineModuleContext", () => {
59+
it("works", (context) => {
60+
const tempDirectoryPath = setupTempDirectory(context, {
61+
"package.json": `{ "name": "my-package" }`,
62+
"sub-package/package.json": `{ "name": "my-sub-package" }`,
63+
});
64+
65+
{
66+
const { packageName, relativePath } = determineModuleContext(
67+
path.join(tempDirectoryPath, "some-dir/some-file.js")
68+
);
69+
assert.equal(packageName, "my-package");
70+
assert.equal(relativePath, "some-dir/some-file.js");
71+
}
72+
73+
{
74+
const { packageName, relativePath } = determineModuleContext(
75+
path.join(tempDirectoryPath, "sub-package/some-file.js")
76+
);
77+
assert.equal(packageName, "my-sub-package");
78+
assert.equal(relativePath, "some-file.js");
79+
}
80+
});
81+
});

packages/react-native-node-api-modules/src/node/path-utils.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from "node:assert/strict";
12
import path from "node:path";
23
import fs from "node:fs";
34
import crypto from "node:crypto";
@@ -37,13 +38,54 @@ export function replaceWithNodeExtension(modulePath: string) {
3738
});
3839
}
3940

41+
export type ModuleContext = {
42+
packageName: string;
43+
relativePath: string;
44+
};
45+
46+
/**
47+
* Traverse the filesystem upward to find a name for the package that which contains a file.
48+
*/
49+
export function determineModuleContext(
50+
modulePath: string,
51+
originalPath = modulePath
52+
): ModuleContext {
53+
const candidatePackageJsonPath = path.join(modulePath, "package.json");
54+
const parentDirectoryPath = path.dirname(modulePath);
55+
if (fs.existsSync(candidatePackageJsonPath)) {
56+
const packageJsonContent = fs.readFileSync(
57+
candidatePackageJsonPath,
58+
"utf8"
59+
);
60+
const packageJson = JSON.parse(packageJsonContent) as unknown;
61+
assert(
62+
typeof packageJson === "object" && packageJson !== null,
63+
"Expected package.json to be an object"
64+
);
65+
assert(
66+
"name" in packageJson && typeof packageJson.name === "string",
67+
"Expected package.json to have a name"
68+
);
69+
return {
70+
packageName: packageJson.name,
71+
relativePath: path.relative(modulePath, originalPath),
72+
};
73+
} else if (parentDirectoryPath === modulePath) {
74+
// We've reached the root of the filesystem
75+
throw new Error("Could not find containing package");
76+
} else {
77+
return determineModuleContext(parentDirectoryPath, originalPath);
78+
}
79+
}
80+
4081
export function hashNodeApiModulePath(modulePath: string) {
4182
// Transforming platform specific paths to a common path
4283
if (path.extname(modulePath) !== ".node") {
4384
return hashNodeApiModulePath(replaceWithNodeExtension(modulePath));
4485
}
86+
const { packageName, relativePath } = determineModuleContext(modulePath);
4587
const hash = crypto.createHash("sha256");
46-
hash.update(path.normalize(modulePath));
88+
hash.update(path.resolve(packageName, relativePath));
4789
const result = hash.digest("hex").slice(0, 8);
4890
return result;
4991
}

0 commit comments

Comments
 (0)