Skip to content

Commit 3e5f57d

Browse files
author
Mariusz Pasinski
committed
feat: add support for custom prefix resolvers
1 parent e539343 commit 3e5f57d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/react-native-node-api-modules/cpp/CxxNodeApiHostModule.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,19 @@ CxxNodeApiHostModule::requireNodeAddon(jsi::Runtime &rt,
154154
throw jsi::JSError(rt, "Invalid characters in `requiredFrom`. Only ASCII alphanumerics are allowed.");
155155
}
156156

157-
const std::string &libraryNameStr = requiredPath;
157+
// Check if this is a prefixed import (e.g. `node:fs/promises`)
158+
const auto [pathPrefix, strippedPath] = rpartition(requiredPath, ':');
159+
if (!pathPrefix.empty()) {
160+
// URL protocol or prefix detected, dispatch via custom resolver
161+
if (auto handler = prefixResolvers_.find(pathPrefix); prefixResolvers_.end() != handler) {
162+
// HACK: Smuggle the `pathPrefix` as new `requiredPackageName`
163+
return (handler->second)(rt, strippedPath, pathPrefix, requiredFrom);
164+
} else {
165+
throw jsi::JSError(rt, "Unsupported protocol or prefix \"" + pathPrefix + "\". Have you registered it?");
166+
}
167+
}
168+
169+
const std::string &libraryNameStr = strippedPath;
158170
auto [it, inserted] = nodeAddons_.emplace(libraryNameStr, NodeAddon());
159171
NodeAddon &addon = it->second;
160172

packages/react-native-node-api-modules/cpp/CxxNodeApiHostModule.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class JSI_EXPORT CxxNodeApiHostModule : public facebook::react::TurboModule {
4242
std::string generatedName;
4343
};
4444
std::unordered_map<std::string, NodeAddon> nodeAddons_;
45+
std::unordered_map<std::string, ResolverFunc> prefixResolvers_;
4546
using LoaderPolicy = PosixLoader; // FIXME: HACK: This is temporary workaround
4647
// for my lazyness (work on iOS and Android)
4748

0 commit comments

Comments
 (0)