From 70ea0f299bb16f2da8ef8edacdde752b15b06683 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 17 Jul 2025 22:54:56 +0200 Subject: [PATCH] Fix PAM module loading path for bundled npm package (#396) --- .../services/authenticate-pam-loader.ts | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/web/src/server/services/authenticate-pam-loader.ts b/web/src/server/services/authenticate-pam-loader.ts index d1b01e29..ba2af783 100644 --- a/web/src/server/services/authenticate-pam-loader.ts +++ b/web/src/server/services/authenticate-pam-loader.ts @@ -27,7 +27,10 @@ if (fs.existsSync(seaPamPath) || fs.existsSync(seaNativePamPath)) { const possiblePaths = [ seaPamPath, seaNativePamPath, - path.join(__dirname, '..', '..', '..', 'native', 'authenticate_pam.node'), + // Try different parent levels for native directory + ...[1, 2, 3].map((levels) => + path.join(__dirname, ...Array(levels).fill('..'), 'native', 'authenticate_pam.node') + ), ]; let loaded = false; @@ -77,27 +80,38 @@ if (fs.existsSync(seaPamPath) || fs.existsSync(seaNativePamPath)) { // If normal require failed, try the optional-modules location if (!loaded) { - const optionalModulePath = path.join( - __dirname, - '..', - '..', - '..', + // Try different parent directory levels for various contexts: + // 1 level up: bundled context (dist-npm/lib/) + // 3 levels up: development context (src/server/services/) + // 2 levels up: alternative bundled location + const parentLevels = [1, 3, 2]; + const modulePath = [ 'optional-modules', 'authenticate-pam', 'build', 'Release', - 'authenticate_pam.node' - ); - if (fs.existsSync(optionalModulePath)) { - try { - const nativeModule = loadNativeModule(optionalModulePath); - if (nativeModule.authenticate) { - authenticate = nativeModule.authenticate; - loaded = true; - console.log('Loaded authenticate-pam from optional-modules location'); + 'authenticate_pam.node', + ]; + + for (const levels of parentLevels) { + const pathSegments = [__dirname, ...Array(levels).fill('..'), ...modulePath]; + const optionalModulePath = path.join(...pathSegments); + + if (fs.existsSync(optionalModulePath)) { + try { + const nativeModule = loadNativeModule(optionalModulePath); + if (nativeModule.authenticate) { + authenticate = nativeModule.authenticate; + loaded = true; + console.log( + 'Loaded authenticate-pam from optional-modules location:', + optionalModulePath + ); + break; + } + } catch (_loadError) { + // Continue to next path } - } catch (_loadError) { - // Continue to stub } } }