()
| 24 | const PI_TUI_NATIVE_PATTERN = /native[\\/](?:win32|darwin)[\\/]prebuilds[\\/].+\.node$/; |
| 25 | |
| 26 | export function installNativeModuleHook(): void { |
| 27 | if (installed) return; |
| 28 | installed = true; |
| 29 | |
| 30 | const moduleBuiltin = nodeRequire('node:module') as ModuleWithLoad; |
| 31 | const originalLoad = moduleBuiltin._load; |
| 32 | if (originalLoad === undefined) return; |
| 33 | |
| 34 | moduleBuiltin._load = function loadWithNativeAssets( |
| 35 | this: unknown, |
| 36 | request: string, |
| 37 | parent: unknown, |
| 38 | isMain: boolean, |
| 39 | ): unknown { |
| 40 | if ( |
| 41 | typeof request === 'string' && |
| 42 | PI_TUI_NATIVE_PATTERN.test(request) && |
| 43 | !existsSync(request) |
| 44 | ) { |
| 45 | const pkgRoot = getNativePackageRoot('@moonshot-ai/pi-tui'); |
| 46 | if (pkgRoot !== null) { |
| 47 | const match = request.match(PI_TUI_NATIVE_PATTERN); |
| 48 | if (match !== null) { |
| 49 | const redirected = join(pkgRoot, match[0]); |
| 50 | return originalLoad.call(this, redirected, parent, isMain); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | return originalLoad.call(this, request, parent, isMain); |
| 55 | }; |
| 56 | } |
no test coverage detected