openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 7.9 kB
JavaScript
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{t as escapeRegExp}from"./regexp-DAJp1ywh.mjs";import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{t as PLUGIN_SOURCE_CAPTURE_PREFIX}from"./plugin-source-capture-path-BpBJjNIB.mjs";import Module,{createRequire}from"node:module";import fs from"node:fs";import{fileURLToPath,pathToFileURL}from"node:url";import path from"node:path";
//!src/plugins/native-module-require.ts
function useNodeModuleHooks(){return!process.versions.bun}const PLUGIN_SOURCE_MODULE_EXTENSIONS=[`.ts`,`.tsx`,`.mts`,`.cts`,`.mtsx`,`.ctsx`];function isPluginSourceModulePath(e){return PLUGIN_SOURCE_MODULE_EXTENSIONS.includes(path.extname(e).toLowerCase())}const nativeModuleLoadFailures=/* @__PURE__ */ new Map;const moduleWithResolver=Module;const bunRuntimeOnResolveProbe=resolveGlobalSingleton(Symbol.for(`openclaw.bunRuntimeOnResolveProbe`),()=>({tested:false,supported:false}));function supportsBunRuntimeOnResolveTargets(){if(bunRuntimeOnResolveProbe.tested)return bunRuntimeOnResolveProbe.supported;bunRuntimeOnResolveProbe.tested=true;const e=globalThis.Bun;if(!e?.resolveSync)return false;const t=`openclaw-bun-runtime-onresolve-probe`;const n=fileURLToPath(import.meta.url);let r=false;try{e.plugin({name:t,setup(e){e.onResolve({filter:/^openclaw-bun-runtime-onresolve-probe$/u,namespace:`file`},()=>{r=true;return{path:n,namespace:`file`}})}});bunRuntimeOnResolveProbe.supported=e.resolveSync(t,path.dirname(n))===n&&r}catch{bunRuntimeOnResolveProbe.supported=false}return bunRuntimeOnResolveProbe.supported}const capturedModuleResolvers=resolveGlobalSingleton(Symbol.for(`openclaw.capturedModuleResolvers`),()=>({installed:false,loaderInstalled:false,resolving:false,owners:/* @__PURE__ */ new Set}));function resolveCapturedPluginModule(e){if(capturedModuleResolvers.resolving)return;capturedModuleResolvers.resolving=true;try{for(const t of capturedModuleResolvers.owners){const n=e(t);if(n)return n}}finally{capturedModuleResolvers.resolving=false}}function installCapturedPluginModuleResolver(e){if(e){e.plugin({name:`openclaw-plugin-source-capture`,setup(t){const n=new RegExp(`^(?!${escapeRegExp(e.main)}(?![\\s\\S]))`);t.onResolve({filter:n,namespace:`file`},({path:e,importer:t,kind:n})=>{const r=resolveCapturedPluginModule(r=>r.prepare(e,t,n));return r?{path:r,namespace:`file`}:void 0})}});return}const t=moduleWithResolver[`_resolveFilename`];moduleWithResolver[`_resolveFilename`]=(e,n,r,i)=>{const a=n?.filename;return(a?resolveCapturedPluginModule(o=>o.resolve(e,a,()=>t(e,n,r,i))):void 0)??t(e,n,r,i)}}function installCapturedPluginModuleLoader(e){e.plugin({name:`openclaw-plugin-source-jsx`,setup(e){e.onLoad({filter:new RegExp(`${PLUGIN_SOURCE_CAPTURE_PREFIX}[^/\\\\]+[/\\\\].*\\.[cm]?[jt]sx$`,`u`),namespace:`file`},({path:e})=>resolveCapturedPluginModule(t=>t.load?.(e))??{contents:fs.readFileSync(e,`utf8`),loader:e.toLowerCase().endsWith(`.jsx`)?`jsx`:`tsx`})}})}function registerCapturedPluginModuleResolver(e){const t=globalThis.Bun;if(!capturedModuleResolvers.installed){installCapturedPluginModuleResolver(t);capturedModuleResolvers.installed=true}if(e.load&&t&&!capturedModuleResolvers.loaderInstalled){installCapturedPluginModuleLoader(t);capturedModuleResolvers.loaderInstalled=true}capturedModuleResolvers.owners.add(e);return()=>{capturedModuleResolvers.owners.delete(e)}}function isJavaScriptModulePath(e){return[`.js`,`.mjs`,`.cjs`].includes(path.extname(e).toLowerCase())}function resolvePluginLoaderTryNative(e,t){const n=[`.js`,`.mjs`,`.cjs`,`.json`].includes(path.extname(e).trim().toLowerCase());if(e.replace(/\\/g,`/`).includes(`/dist/extensions/`))return n;return n||t?.preferBuiltDist===true&&e.includes(`${path.sep}dist${path.sep}`)}function isMissingTargetModuleError(e,t){if(e.code!==`MODULE_NOT_FOUND`||typeof e.message!==`string`)return false;const n=e.message.split(`
`,1)[0]??``;return n.includes(`'${t}'`)||n.includes(`"${t}"`)}function isSourceTransformFallbackError(e,t){if(!e||typeof e!==`object`||!(`code`in e))return false;const n=e.code;return n===`ERR_REQUIRE_ESM`||n===`ERR_REQUIRE_ASYNC_MODULE`||n===`ERR_REQUIRE_ESM_RACE_CONDITION`||n===`ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX`||n===`ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING`||n===`ERR_UNKNOWN_FILE_EXTENSION`||isMissingTargetModuleError(e,t)}function tryNativeRequireJavaScriptModule(e,t={}){const n=toNativeRequirePath(e);const r=Boolean(process.versions.bun)&&isPluginSourceModulePath(n);if(!isJavaScriptModulePath(n)&&!r)return{ok:false};return tryNativeRequireModule(e,t)}function tryNativeRequireModule(e,t={}){const n=toNativeRequirePath(e);const r=createRequire(import.meta.url);if(isPluginSourceModulePath(n)&&!process.features.typescript&&typeof r.extensions?.[path.extname(n)]!==`function`)return{ok:false};let i;try{i=withNativeRequireAliases(t.aliasMap,t.hostRoot,()=>r.resolve(n));const e=withNativeRequireAliases(t.aliasMap,t.hostRoot,()=>r(n));nativeModuleLoadFailures.delete(i);return{ok:true,moduleExport:e}}catch(e){const r=e&&typeof e===`object`?Reflect.get(e,`code`):void 0;if(i!==void 0&&nativeModuleLoadFailures.has(i)&&(r===`ERR_REQUIRE_ESM_RACE_CONDITION`||r===`ERR_INTERNAL_ASSERTION`))throw nativeModuleLoadFailures.get(i);if(isSourceTransformFallbackError(e,n)||i===void 0&&t.fallbackOnMissingDependency===true&&(r===`MODULE_NOT_FOUND`||r===`ERR_MODULE_NOT_FOUND`))return{ok:false};if(i!==void 0)nativeModuleLoadFailures.set(i,e);throw e}}function clearPluginSourceCaptureModuleCache(e){const t=pathToFileURL(e+path.sep).href;const n=createRequire(import.meta.url).cache;for(const r of Object.keys(n))if(r.startsWith(t)||path.isAbsolute(r)&&isPathInside(e,r))delete n[r];for(const t of nativeModuleLoadFailures.keys())if(isPathInside(e,t))nativeModuleLoadFailures.delete(t)}function clearPluginModuleRequireCache(e,t){const n=createRequire(import.meta.url);const r=/* @__PURE__ */ new Set;const clear=e=>{if(r.has(e)||!isPathInside(t,e))return;r.add(e);for(const t of n.cache[e]?.children??[])clear(t.id);delete n.cache[e]};clear(e)}function toNativeRequirePath(e){try{return/^file:\/\//iu.test(e)?fileURLToPath(e):e}catch{return e}}function withNativeRequireAliases(e,t,n){if(!e&&!t||!moduleWithResolver[`_resolveFilename`])return n();const r=typeof e===`function`?e:t=>e?.[t];const i=t?fs.realpathSync.native(t):void 0;const resolveHostPath=e=>{if(!t||!i||t===i||!path.isAbsolute(e))return e;const n=fs.realpathSync.native(e);if(!isPathInside(i,n))return e;return path.join(t,path.relative(i,n))};const a=moduleWithResolver[`_resolveFilename`];const o=useNodeModuleHooks()?Module.registerHooks({resolve(e,n,a){const o=n.parentURL?.startsWith(`file:`)?fileURLToPath(n.parentURL):void 0;const s=r(e,o);if(s)return{shortCircuit:true,url:pathToFileURL(s).href};let c;try{c=a(e,n)}catch(t){if(o&&isPluginSourceModulePath(o)&&e.startsWith(`.`)&&/\.[cm]?js$/u.test(e)&&hasErrnoCode(t,`ERR_MODULE_NOT_FOUND`)){const r=new URL(e.replace(/\.([cm]?)js$/u,`.$1ts`),n.parentURL);if(fs.existsSync(fileURLToPath(r)))c=a(r.href,n);else throw t}else throw t}if(!t||t===i||!c.url.startsWith(`file:`))return c;const l=new URL(c.url);if(l.search||l.hash)return c;return{...c,url:pathToFileURL(resolveHostPath(fileURLToPath(l))).href}}}):void 0;moduleWithResolver[`_resolveFilename`]=((e,t,n,i)=>{const o=r(e,t?.filename);if(o)return path.normalize(o);return resolveHostPath(a(e,t,n,i))});try{return n()}finally{moduleWithResolver[`_resolveFilename`]=a;o?.deregister()}}export{isPluginSourceModulePath as a,supportsBunRuntimeOnResolveTargets as c,useNodeModuleHooks as d,isJavaScriptModulePath as i,tryNativeRequireJavaScriptModule as l,clearPluginModuleRequireCache as n,registerCapturedPluginModuleResolver as o,clearPluginSourceCaptureModuleCache as r,resolvePluginLoaderTryNative as s,PLUGIN_SOURCE_MODULE_EXTENSIONS as t,tryNativeRequireModule as u};