|
1 | 1 | /* eslint-disable no-console */ |
2 | 2 | import type { AsyncLocalStorage } from 'node:async_hooks'; |
3 | 3 | import { spawnSync } from 'node:child_process'; |
4 | | -import * as fs from 'node:fs'; |
5 | | -import * as os from 'node:os'; |
6 | 4 | import * as path from 'node:path'; |
7 | | -import { env, versions } from 'node:process'; |
| 5 | +import { env } from 'node:process'; |
8 | 6 | import { threadId } from 'node:worker_threads'; |
9 | | -import * as libc from 'detect-libc'; |
10 | | -import { getAbi } from 'node-abi'; |
11 | | - |
12 | | -const stdlib = libc.familySync(); |
13 | | -const platform = process.env['BUILD_PLATFORM'] || os.platform(); |
14 | | -const arch = process.env['BUILD_ARCH'] || os.arch(); |
15 | | -const abi = getAbi(versions.node, 'node'); |
16 | | -const identifier = [platform, arch, stdlib, abi].filter(c => c !== undefined && c !== null).join('-'); |
| 7 | +import { abi, arch, identifier, platform, stdlib } from './constants'; |
| 8 | +import { copyBinary } from './copy-binary'; |
17 | 9 |
|
18 | 10 | type AsyncStorageArgs = { |
19 | 11 | /** The AsyncLocalStorage instance used to fetch the store */ |
@@ -56,33 +48,6 @@ interface Native { |
56 | 48 | getThreadsLastSeen(): Record<string, number>; |
57 | 49 | } |
58 | 50 |
|
59 | | -/** |
60 | | - * Copies the compiled binary from the build directory to the lib directory with the correct name based on the current platform and Node version. |
61 | | - * |
62 | | - * @hidden We only use this for copying the binary after building, it is not intended to be used by end users. |
63 | | - */ |
64 | | -export function copyBinary(): void { |
65 | | - const build = path.resolve(__dirname, '..', 'lib'); |
66 | | - if (!fs.existsSync(build)) { |
67 | | - fs.mkdirSync(build, { recursive: true }); |
68 | | - } |
69 | | - |
70 | | - if (!fs.existsSync(source)) { |
71 | | - console.log('Source file does not exist:', source); |
72 | | - process.exit(1); |
73 | | - } else { |
74 | | - if (fs.existsSync(target)) { |
75 | | - console.log('Target file already exists, overwriting it'); |
76 | | - fs.unlinkSync(target); |
77 | | - } |
78 | | - console.log('Copying', source, 'to', target); |
79 | | - fs.copyFileSync(source, target); |
80 | | - } |
81 | | -} |
82 | | - |
83 | | -const source = path.join(__dirname, '..', 'build', 'Release', 'stack-trace.node'); |
84 | | -const target = path.join(__dirname, '..', 'lib', `stack-trace-${identifier}.node`); |
85 | | - |
86 | 51 | function clean(err: Buffer): string { |
87 | 52 | return err.toString().trim(); |
88 | 53 | } |
@@ -308,7 +273,26 @@ function getNativeModule(): Native { |
308 | 273 |
|
309 | 274 | const native = getNativeModule(); |
310 | 275 |
|
| 276 | +/** |
| 277 | + * Registers the current thread with the native module. |
| 278 | + * |
| 279 | + * This should be called on every thread that you want to capture stack traces from. |
| 280 | + * |
| 281 | + * @param threadName The name of the thread |
| 282 | + * |
| 283 | + * threadName defaults to the `threadId` if not provided. |
| 284 | + */ |
311 | 285 | export function registerThread(threadName?: string): void; |
| 286 | +/** |
| 287 | + * Registers the current thread with the native module. |
| 288 | + * |
| 289 | + * This should be called on every thread that you want to capture stack traces from. |
| 290 | + * |
| 291 | + * @param storageOrThread Either the name of the thread, or an object containing an AsyncLocalStorage instance and optional storage key. |
| 292 | + * @param threadName The name of the thread, if the first argument is an object. |
| 293 | + * |
| 294 | + * threadName defaults to the `threadId` if not provided. |
| 295 | + */ |
312 | 296 | export function registerThread(storageOrThread: AsyncStorageArgs | string, threadName?: string): void; |
313 | 297 | /** |
314 | 298 | * Registers the current thread with the native module. |
|
0 commit comments