@@ -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
153153CxxNodeApiHostModule::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
163163jsi::Value
164164CxxNodeApiHostModule::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
199196jsi::Value
200197CxxNodeApiHostModule::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);
0 commit comments