Skip to content

Commit 3ba75a5

Browse files
committed
oops commonjs
More fixes
1 parent c62c0f2 commit 3ba75a5

5 files changed

Lines changed: 71 additions & 43 deletions

File tree

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"sentry"
1717
],
1818
"scripts": {
19+
"install": "node -p \"'@sentry/node-native-stacktrace@' + require('./package.json').version\"",
1920
"lint": "yarn lint:eslint && yarn lint:clang",
2021
"lint:eslint": "eslint . --format stylish",
2122
"lint:clang": "node scripts/clang-format.mjs",
@@ -24,15 +25,16 @@
2425
"fix:clang": "node scripts/clang-format.mjs --fix",
2526
"build": "yarn clean && yarn build:lib && yarn build:bindings:configure && yarn build:bindings",
2627
"build:lib": "tsc",
27-
"build:copy-binary": "node -e \"import { copyBinary } from './lib/index.js'; copyBinary();\"",
28+
"build:copy-binary": "node -e \"const { copyBinary } = require('./lib/copy-binary.js'); copyBinary();\"",
2829
"build:bindings:configure": "node-gyp configure",
2930
"build:bindings:configure:arm64": "node-gyp configure --arch=arm64 --target_arch=arm64",
30-
"build:bindings": "node-gyp build && yarn build:copy-binary",
31-
"build:bindings:arm64": "node-gyp build --arch=arm64 && yarn build:copy-binary",
31+
"build:bindings": "yarn build:lib && node-gyp build && yarn build:copy-binary",
32+
"build:bindings:arm64": "yarn build:lib && node-gyp build --arch=arm64 && yarn build:copy-binary",
3233
"build:tarball": "npm pack",
3334
"clean": "node-gyp clean && rm -rf lib && rm -rf build && rm -f *.tgz",
3435
"test": "node ./test/prepare.mjs && vitest run --poolOptions.forks.singleFork --silent=false --disable-console-intercept"
3536
},
37+
"gypfile": false,
3638
"engines": {
3739
"node": ">=18"
3840
},

src/constants.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import * as os from 'node:os';
3+
import { versions } from 'node:process';
4+
import * as libc from 'detect-libc';
5+
import { getAbi } from 'node-abi';
6+
7+
export const stdlib = libc.familySync();
8+
export const platform = process.env['BUILD_PLATFORM'] || os.platform();
9+
export const arch = process.env['BUILD_ARCH'] || os.arch();
10+
export const abi = getAbi(versions.node, 'node');
11+
export const identifier = [platform, arch, stdlib, abi].filter(c => c !== undefined && c !== null).join('-');

src/copy-binary.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-disable no-console */
2+
import * as fs from 'node:fs';
3+
import * as path from 'node:path';
4+
import { identifier } from './constants';
5+
6+
const source = path.join(__dirname, '..', 'build', 'Release', 'stack-trace.node');
7+
const target = path.join(__dirname, '..', 'lib', `stack-trace-${identifier}.node`);
8+
9+
/**
10+
* Copies the compiled binary from the build directory to the lib directory with the correct name based on the current platform and Node version.
11+
*
12+
* @hidden We only use this for copying the binary after building, it is not intended to be used by end users.
13+
*/
14+
export function copyBinary(): void {
15+
const build = path.resolve(__dirname, '..', 'lib');
16+
if (!fs.existsSync(build)) {
17+
fs.mkdirSync(build, { recursive: true });
18+
}
19+
20+
if (!fs.existsSync(source)) {
21+
console.log('Source file does not exist:', source);
22+
process.exit(1);
23+
} else {
24+
if (fs.existsSync(target)) {
25+
console.log('Target file already exists, overwriting it');
26+
fs.unlinkSync(target);
27+
}
28+
console.log('Copying', source, 'to', target);
29+
fs.copyFileSync(source, target);
30+
}
31+
}

src/index.ts

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
/* eslint-disable no-console */
22
import type { AsyncLocalStorage } from 'node:async_hooks';
33
import { spawnSync } from 'node:child_process';
4-
import * as fs from 'node:fs';
5-
import * as os from 'node:os';
64
import * as path from 'node:path';
7-
import { env, versions } from 'node:process';
5+
import { env } from 'node:process';
86
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';
179

1810
type AsyncStorageArgs = {
1911
/** The AsyncLocalStorage instance used to fetch the store */
@@ -56,33 +48,6 @@ interface Native {
5648
getThreadsLastSeen(): Record<string, number>;
5749
}
5850

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-
8651
function clean(err: Buffer): string {
8752
return err.toString().trim();
8853
}
@@ -308,7 +273,26 @@ function getNativeModule(): Native {
308273

309274
const native = getNativeModule();
310275

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+
*/
311285
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+
*/
312296
export function registerThread(storageOrThread: AsyncStorageArgs | string, threadName?: string): void;
313297
/**
314298
* Registers the current thread with the native module.

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"compilerOptions": {
33
"target": "ES2020",
4-
"module": "node16",
4+
"module": "commonjs",
55
"declaration": true,
66
"outDir": "lib",
77
"strict": true,
88
"skipLibCheck": true,
99
"forceConsistentCasingInFileNames": true,
10-
"moduleResolution": "node16",
10+
"moduleResolution": "node",
1111
"noEmit": false,
1212
"sourceMap": true,
1313
"declarationMap": true,

0 commit comments

Comments
 (0)