Skip to content

Commit d5bf7b4

Browse files
author
Mariusz Pasinski
committed
chore: rename requiredFrom param to originalId
1 parent f4130a1 commit d5bf7b4

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

packages/host/cpp/CxxNodeApiHostModule.cpp

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
140140
const jsi::Value args[], size_t count) {
141141
auto &thisModule = static_cast<CxxNodeApiHostModule &>(turboModule);
142142
if (3 == count) {
143-
// Must be `requireNodeAddon(requiredPath: string, requiredPackageName: string, requiredFrom: string)`
143+
// Must be `requireNodeAddon(requiredPath: string, requiredPackageName: string, originalId: string)`
144144
return thisModule.requireNodeAddon(rt,
145145
args[0].asString(rt),
146146
args[1].asString(rt),
@@ -153,25 +153,22 @@ jsi::Value
153153
CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
154154
const jsi::String &requiredPath,
155155
const jsi::String &requiredPackageName,
156-
const jsi::String &requiredFrom) {
156+
const jsi::String &originalId) {
157157
return requireNodeAddon(rt,
158158
requiredPath.utf8(rt),
159159
requiredPackageName.utf8(rt),
160-
requiredFrom.utf8(rt));
160+
originalId.utf8(rt));
161161
}
162162

163163
jsi::Value
164164
CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
165165
const std::string &requiredPath,
166166
const std::string &requiredPackageName,
167-
const std::string &requiredFrom) {
167+
const std::string &originalId) {
168168
// Ensure that user-supplied inputs contain only allowed characters
169169
if (!isModulePathLike(requiredPath)) {
170170
throw jsi::JSError(rt, "Invalid characters in `requiredPath`. Only ASCII alphanumerics are allowed.");
171171
}
172-
if (!isModulePathLike(requiredFrom)) {
173-
throw jsi::JSError(rt, "Invalid characters in `requiredFrom`. Only ASCII alphanumerics are allowed.");
174-
}
175172

176173
// Check if this is a prefixed import (e.g. `node:fs/promises`)
177174
const auto [pathPrefix, strippedPath] = rpartition(requiredPath, ':');
@@ -180,7 +177,7 @@ CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
180177
std::string pathPrefixCopy(pathPrefix); // HACK: Need explicit cast to `std::string`
181178
if (auto handler = prefixResolvers_.find(pathPrefixCopy); prefixResolvers_.end() != handler) {
182179
// HACK: Smuggle the `pathPrefix` as new `requiredPackageName`
183-
return (handler->second)(rt, strippedPath, pathPrefix, requiredFrom);
180+
return (handler->second)(rt, strippedPath, pathPrefix, originalId);
184181
} else {
185182
throw jsi::JSError(rt, "Unsupported protocol or prefix \"" + pathPrefixCopy + "\". Have you registered it?");
186183
}
@@ -189,45 +186,39 @@ CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
189186
// Check, if this package has been overridden
190187
if (auto handler = packageOverrides_.find(requiredPackageName); packageOverrides_.end() != handler) {
191188
// This package has a custom resolver, invoke it
192-
return (handler->second)(rt, strippedPath, requiredPackageName, requiredFrom);
189+
return (handler->second)(rt, strippedPath, requiredPackageName, originalId);
193190
}
194191

195-
// Otherwise, "requiredPath" must be a "relative specifier" or a "bare specifier"
196-
return resolveRelativePath(rt, strippedPath, requiredPackageName, requiredFrom);
192+
// Otherwise, "requiredPath" must be a package-relative specifier
193+
return resolveRelativePath(rt, strippedPath, requiredPackageName, originalId);
197194
}
198195

199196
jsi::Value
200197
CxxNodeApiHostModule::resolveRelativePath(facebook::jsi::Runtime &rt,
201198
const std::string_view &requiredPath,
202199
const std::string_view &requiredPackageName,
203-
const std::string_view &requiredFrom) {
204-
// "Rebase" the relative path to get a proper package-relative path
205-
const auto requiredFromDirParts = makeParentPath(requiredFrom);
206-
const auto requiredPathParts = explodePath(requiredPath);
207-
const std::string mergedSubpath = implodePath(joinPath(requiredFromDirParts, requiredPathParts));
208-
if (!isModulePathLike(mergedSubpath)) {
209-
throw jsi::JSError(rt, "Computed subpath is invalid. Check `requiredPath` and `requiredFrom`.");
210-
}
211-
if (!startsWith(mergedSubpath, "./")) {
212-
throw jsi::JSError(rt, "Subpath must be relative and cannot leave its package root.");
200+
const std::string_view &originalId) {
201+
if (!startsWith(requiredPath, "./")) {
202+
throw jsi::JSError(rt, "requiredPath must be relative and cannot leave its package root.");
213203
}
214204

215-
// Check whether (`requiredPackageName`, `mergedSubpath`) is already cached
205+
// Check whether (`requiredPackageName`, `requiredPath`) is already cached
216206
// NOTE: Cache must to be `jsi::Runtime`-local
217207
auto [exports, isCached] = lookupRequireCache(rt,
218208
requiredPackageName,
219-
mergedSubpath);
209+
requiredPath);
220210

221211
if (!isCached) {
222212
// Ask the global addon registry to load given Node-API addon.
223213
// If other runtime loaded it already, the OS will return the same pointer.
224214
// NOTE: This method might try multiple platform-specific paths.
225215
const std::string packageNameCopy(requiredPackageName);
226-
auto &addon = g_platformAddonRegistry.loadAddon(packageNameCopy, mergedSubpath);
216+
const std::string requiredPathCopy(requiredPath);
217+
auto &addon = g_platformAddonRegistry.loadAddon(packageNameCopy, requiredPathCopy);
227218

228219
// Create a `napi_env` and initialize the addon
229220
exports = g_platformAddonRegistry.instantiateAddonInRuntime(rt, addon);
230-
updateRequireCache(rt, requiredPackageName, mergedSubpath, exports);
221+
updateRequireCache(rt, requiredPackageName, requiredPath, exports);
231222
}
232223

233224
return std::move(exports);

packages/host/cpp/CxxNodeApiHostModule.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ class JSI_EXPORT CxxNodeApiHostModule : public facebook::react::TurboModule {
2929
facebook::jsi::Value requireNodeAddon(facebook::jsi::Runtime &rt,
3030
const facebook::jsi::String &requiredPath,
3131
const facebook::jsi::String &requiredPackageName,
32-
const facebook::jsi::String &requiredFrom);
32+
const facebook::jsi::String &originalId);
3333
facebook::jsi::Value requireNodeAddon(facebook::jsi::Runtime &rt,
3434
const std::string &requiredPath,
3535
const std::string &requiredPackageName,
36-
const std::string &requiredFrom);
36+
const std::string &originalId);
3737

3838
facebook::jsi::Value resolveRelativePath(facebook::jsi::Runtime &rt,
3939
const std::string_view &requiredPath,
4040
const std::string_view &requiredPackageName,
41-
const std::string_view &requiredFrom);
41+
const std::string_view &originalId);
4242

4343
std::pair<facebook::jsi::Value, bool>
4444
lookupRequireCache(facebook::jsi::Runtime &rt,

packages/host/src/react-native/NativeNodeApiHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { TurboModule } from "react-native";
22
import { TurboModuleRegistry } from "react-native";
33

44
export interface Spec extends TurboModule {
5-
requireNodeAddon(requiredPath: string, packageName?: string, requiredFrom?: string): void;
5+
requireNodeAddon(requiredPath: string, packageName?: string, originalId?: string): void;
66
}
77

88
export default TurboModuleRegistry.getEnforcing<Spec>("NodeApiHost");

0 commit comments

Comments
 (0)