|
1 | | -import {createRequire} from 'node:module' |
2 | | -import process from 'node:process' |
3 | | - |
4 | | -declare global { |
5 | | - interface GlobalThis { |
6 | | - __TNMSC_TEST_NATIVE_BINDING__?: object |
7 | | - } |
8 | | -} |
| 1 | +import { createRequire } from "node:module"; |
| 2 | +import process from "node:process"; |
9 | 3 |
|
10 | 4 | function shouldSkipNativeBinding(): boolean { |
11 | | - if (process.env['TNMSC_FORCE_NATIVE_BINDING'] === '1') return false |
12 | | - if (process.env['TNMSC_DISABLE_NATIVE_BINDING'] === '1') return true |
| 5 | + if (process.env["TNMSC_FORCE_NATIVE_BINDING"] === "1") return false; |
| 6 | + if (process.env["TNMSC_DISABLE_NATIVE_BINDING"] === "1") return true; |
13 | 7 |
|
14 | | - return process.env['NODE_ENV'] === 'test' |
15 | | - || process.env['VITEST'] != null |
16 | | - || process.env['VITEST_WORKER_ID'] != null |
| 8 | + return process.env["NODE_ENV"] === "test" || process.env["VITEST"] != null || process.env["VITEST_WORKER_ID"] != null; |
17 | 9 | } |
18 | 10 |
|
19 | 11 | export function tryLoadNativeBinding<T extends object>(): T | undefined { |
20 | | - const testBinding: unknown = globalThis.__TNMSC_TEST_NATIVE_BINDING__ |
21 | | - if (testBinding != null && typeof testBinding === 'object') return testBinding as T |
22 | | - if (shouldSkipNativeBinding()) return void 0 |
| 12 | + const testGlobals = globalThis as typeof globalThis & { __TNMSC_TEST_NATIVE_BINDING__?: object }; |
| 13 | + const testBinding: unknown = testGlobals.__TNMSC_TEST_NATIVE_BINDING__; |
| 14 | + if (testBinding != null && typeof testBinding === "object") return testBinding as T; |
| 15 | + if (shouldSkipNativeBinding()) return void 0; |
23 | 16 |
|
24 | 17 | const suffixMap: Readonly<Record<string, string>> = { |
25 | | - 'win32-x64': 'win32-x64-msvc', |
26 | | - 'linux-x64': 'linux-x64-gnu', |
27 | | - 'linux-arm64': 'linux-arm64-gnu', |
28 | | - 'darwin-arm64': 'darwin-arm64', |
29 | | - 'darwin-x64': 'darwin-x64' |
30 | | - } |
31 | | - const suffix = suffixMap[`${process.platform}-${process.arch}`] |
32 | | - if (suffix == null) return void 0 |
| 18 | + "win32-x64": "win32-x64-msvc", |
| 19 | + "linux-x64": "linux-x64-gnu", |
| 20 | + "linux-arm64": "linux-arm64-gnu", |
| 21 | + "darwin-arm64": "darwin-arm64", |
| 22 | + "darwin-x64": "darwin-x64", |
| 23 | + }; |
| 24 | + const suffix = suffixMap[`${process.platform}-${process.arch}`]; |
| 25 | + if (suffix == null) return void 0; |
33 | 26 |
|
34 | 27 | try { |
35 | | - const _require = createRequire(import.meta.url) |
36 | | - const packageName = `@truenine/memory-sync-cli-${suffix}` |
37 | | - const binaryFile = `napi-memory-sync-cli.${suffix}.node` |
| 28 | + const _require = createRequire(import.meta.url); |
| 29 | + const packageName = `@truenine/memory-sync-cli-${suffix}`; |
| 30 | + const binaryFile = `napi-memory-sync-cli.${suffix}.node`; |
38 | 31 | const candidates = [ |
39 | 32 | packageName, |
40 | 33 | `${packageName}/${binaryFile}`, |
41 | 34 | `./${binaryFile}`, |
42 | 35 | `../npm/${suffix}`, |
43 | 36 | `../npm/${suffix}/${binaryFile}`, |
44 | 37 | `../../npm/${suffix}`, |
45 | | - `../../npm/${suffix}/${binaryFile}` |
46 | | - ] |
| 38 | + `../../npm/${suffix}/${binaryFile}`, |
| 39 | + ]; |
47 | 40 |
|
48 | 41 | for (const specifier of candidates) { |
49 | 42 | try { |
50 | | - const loaded = _require(specifier) as unknown |
| 43 | + const loaded = _require(specifier) as unknown; |
51 | 44 | const possibleBindings = [ |
52 | | - (loaded as {config?: unknown})?.config, |
53 | | - (loaded as {default?: {config?: unknown}})?.default?.config, |
54 | | - (loaded as {default?: unknown})?.default, |
55 | | - loaded |
56 | | - ] |
| 45 | + (loaded as { config?: unknown })?.config, |
| 46 | + (loaded as { default?: { config?: unknown } })?.default?.config, |
| 47 | + (loaded as { default?: unknown })?.default, |
| 48 | + loaded, |
| 49 | + ]; |
57 | 50 |
|
58 | 51 | for (const candidate of possibleBindings) { |
59 | | - if (candidate != null && typeof candidate === 'object') return candidate as T |
| 52 | + if (candidate != null && typeof candidate === "object") return candidate as T; |
60 | 53 | } |
61 | | - } |
62 | | - catch {} |
| 54 | + } catch {} |
63 | 55 | } |
64 | | - } |
65 | | - catch { |
66 | | - } |
| 56 | + } catch {} |
67 | 57 |
|
68 | | - return void 0 |
| 58 | + return void 0; |
69 | 59 | } |
70 | 60 |
|
71 | 61 | export function getNativeBinding<T extends object>(): T | undefined { |
72 | | - return tryLoadNativeBinding<T>() |
| 62 | + return tryLoadNativeBinding<T>(); |
73 | 63 | } |
0 commit comments