|
1 | 1 | import type { SyslogOptions, SyslogFacilityType, SyslogLevelType, SyslogOptionType } from './types/index.js'; |
2 | 2 | import { SyslogFacility, SyslogLevel, SyslogOption } from './types/index.js'; |
| 3 | +import { createRequire } from 'module'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import { dirname } from 'path'; |
3 | 6 |
|
4 | 7 | /** |
5 | 8 | * Native module loader with fallback |
6 | 9 | * @internal |
7 | 10 | */ |
8 | | -declare const require: (id: string) => any; |
9 | | -declare const __dirname: string; |
| 11 | +const require = createRequire(import.meta.url); |
| 12 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
10 | 13 |
|
11 | 14 | function loadNativeModule() { |
12 | | - // Check if we're in a test environment and mock is available |
| 15 | + // Return mock in test environment |
13 | 16 | if (typeof process !== 'undefined' && (process.env.NODE_ENV === 'test' || process.env.VITEST === 'true')) { |
14 | | - try { |
15 | | - // Try to load the mock from the global test setup |
16 | | - if ((globalThis as any).__MOCK_SYSLOG__) { |
17 | | - return (globalThis as any).__MOCK_SYSLOG__; |
18 | | - } |
19 | | - } catch { |
20 | | - // Continue with normal loading if mock not available |
| 17 | + if ((globalThis as any).__MOCK_SYSLOG__) { |
| 18 | + return (globalThis as any).__MOCK_SYSLOG__; |
21 | 19 | } |
22 | 20 | } |
23 | 21 |
|
| 22 | + // Use node-gyp-build (handles path resolution automatically) |
24 | 23 | try { |
25 | | - // Try to load the precompiled binary first |
26 | | - return require('../lib/binding/syslog_native.node'); |
| 24 | + return require('node-gyp-build')(__dirname); |
27 | 25 | } catch (error) { |
28 | | - try { |
29 | | - // Fallback to the source module |
30 | | - return require('node-gyp-build')(__dirname); |
31 | | - } catch (fallbackError) { |
32 | | - throw new Error( |
33 | | - `Failed to load native syslog module. This package only supports Linux x64/ARM64.\n` + |
34 | | - `Original error: ${error instanceof Error ? error.message : String(error)}\n` + |
35 | | - `Fallback error: ${fallbackError instanceof Error ? fallbackError.message : String(fallbackError)}` |
36 | | - ); |
37 | | - } |
| 26 | + throw new Error( |
| 27 | + `Failed to load native syslog module. This package only supports Linux x64/ARM64.\n` + |
| 28 | + `Error: ${error instanceof Error ? error.message : String(error)}` |
| 29 | + ); |
38 | 30 | } |
39 | 31 | } |
40 | 32 |
|
@@ -85,7 +77,7 @@ export class Syslog { |
85 | 77 | */ |
86 | 78 | constructor(options: SyslogOptions = {}) { |
87 | 79 | this.native = loadNativeModule(); |
88 | | - this.ident = options.ident || (typeof require !== 'undefined' && (require as any).main?.filename) || 'node'; |
| 80 | + this.ident = options.ident || (typeof process !== 'undefined' && process.argv[1]) || 'node'; |
89 | 81 | this.facility = options.facility ?? SyslogFacility.LOG_USER; |
90 | 82 | this.options = options.options ?? SyslogOption.LOG_PID; |
91 | 83 |
|
|
0 commit comments