|
1 | | -import { createRequire } from "node:module"; |
2 | | -import process from "node:process"; |
| 1 | +import {createRequire} from 'node:module' |
| 2 | +import process from 'node:process' |
3 | 3 |
|
4 | 4 | function shouldSkipNativeBinding(): boolean { |
5 | | - if (process.env["TNMSC_FORCE_NATIVE_BINDING"] === "1") return false; |
6 | | - 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 |
7 | 7 |
|
8 | | - return process.env["NODE_ENV"] === "test" || process.env["VITEST"] != null || process.env["VITEST_WORKER_ID"] != null; |
| 8 | + return process.env['NODE_ENV'] === 'test' || process.env['VITEST'] != null || process.env['VITEST_WORKER_ID'] != null |
9 | 9 | } |
10 | 10 |
|
11 | 11 | export function tryLoadNativeBinding<T extends object>(): T | undefined { |
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; |
| 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 |
16 | 16 |
|
17 | 17 | const suffixMap: Readonly<Record<string, string>> = { |
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; |
| 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 |
26 | 26 |
|
27 | 27 | try { |
28 | | - const _require = createRequire(import.meta.url); |
29 | | - const packageName = `@truenine/memory-sync-cli-${suffix}`; |
30 | | - 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` |
31 | 31 | const candidates = [ |
32 | 32 | packageName, |
33 | 33 | `${packageName}/${binaryFile}`, |
34 | 34 | `./${binaryFile}`, |
35 | 35 | `../npm/${suffix}`, |
36 | 36 | `../npm/${suffix}/${binaryFile}`, |
37 | 37 | `../../npm/${suffix}`, |
38 | | - `../../npm/${suffix}/${binaryFile}`, |
39 | | - ]; |
| 38 | + `../../npm/${suffix}/${binaryFile}` |
| 39 | + ] |
40 | 40 |
|
41 | 41 | for (const specifier of candidates) { |
42 | 42 | try { |
43 | | - const loaded = _require(specifier) as unknown; |
| 43 | + const loaded = _require(specifier) as unknown |
44 | 44 | const possibleBindings = [ |
45 | | - (loaded as { config?: unknown })?.config, |
46 | | - (loaded as { default?: { config?: unknown } })?.default?.config, |
47 | | - (loaded as { default?: unknown })?.default, |
48 | | - loaded, |
49 | | - ]; |
| 45 | + (loaded as {config?: unknown})?.config, |
| 46 | + (loaded as {default?: {config?: unknown}})?.default?.config, |
| 47 | + (loaded as {default?: unknown})?.default, |
| 48 | + loaded |
| 49 | + ] |
50 | 50 |
|
51 | 51 | for (const candidate of possibleBindings) { |
52 | | - if (candidate != null && typeof candidate === "object") return candidate as T; |
| 52 | + if (candidate != null && typeof candidate === 'object') return candidate as T |
53 | 53 | } |
54 | 54 | } catch {} |
55 | 55 | } |
56 | 56 | } catch {} |
57 | 57 |
|
58 | | - return void 0; |
| 58 | + return void 0 |
59 | 59 | } |
60 | 60 |
|
61 | 61 | export function getNativeBinding<T extends object>(): T | undefined { |
62 | | - return tryLoadNativeBinding<T>(); |
| 62 | + return tryLoadNativeBinding<T>() |
63 | 63 | } |
0 commit comments