Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Add a function to resolve entry point type #14352

Merged
merged 7 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions packages/node/src/utils/entry-point.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { resolve } from 'node:path';
import type { StackParser } from '@sentry/types';

export interface ProcessInterface {
execArgv: string[];
argv: string[];
cwd: () => string;
}

export interface ProcessArgs {
appPath: string;
importPaths: string[];
requirePaths: string[];
}

/**
* Parses the process arguments to determine the app path, import paths, and require paths.
*/
export function parseProcessPaths(proc: ProcessInterface): ProcessArgs {
const { execArgv, argv, cwd: getCwd } = proc;
const cwd = getCwd();
const appPath = resolve(cwd, argv[1] || '');

const joinedArgs = execArgv.join(' ');
const importPaths = Array.from(joinedArgs.matchAll(/--import[ =](\S+)/g)).map(e => resolve(cwd, e[1] || ''));
const requirePaths = Array.from(joinedArgs.matchAll(/--require[ =](\S+)/g)).map(e => resolve(cwd, e[1] || ''));

return { appPath, importPaths, requirePaths };
}

/**
* Gets the current entry point type.
*
* `app` means this function was most likely called via the app entry point.
* `import` means this function was most likely called from an --import cli arg.
* `require` means this function was most likely called from a --require cli arg.
* `unknown` means we couldn't determine for sure.
*/
export function getEntryPointType(
stackParser: StackParser,
proc: ProcessInterface = process,
): 'import' | 'require' | 'app' | 'unknown' {
const filenames = stackParser(new Error().stack || '')
.map(f => f.filename)
.filter(Boolean) as string[];

const { appPath, importPaths, requirePaths } = parseProcessPaths(proc);

const output = [];

if (appPath && filenames.includes(appPath)) {
output.push('app');
}

if (importPaths.some(p => filenames.includes(p))) {
output.push('import');
}

if (requirePaths.some(p => filenames.includes(p))) {
output.push('require');
}

// We only only return anything other than 'unknown' if we only got one match.
if (output.length === 1) {
return output[0] as 'import' | 'require' | 'app';
}

return 'unknown';
}
103 changes: 103 additions & 0 deletions packages/node/test/utils/entry-point.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { defaultStackParser } from '../../src';
import type { ProcessArgs, ProcessInterface } from '../../src/utils/entry-point';
import { getEntryPointType, parseProcessPaths } from '../../src/utils/entry-point';

const PROCESS_ARG_TESTS: [ProcessInterface, ProcessArgs][] = [
[
{ cwd: () => '/user/tim/docs', argv: ['/bin/node', 'app.js'], execArgv: ['--import', './something.js'] },
{ appPath: '/user/tim/docs/app.js', importPaths: ['/user/tim/docs/something.js'], requirePaths: [] },
],
[
{
cwd: () => '/user/tim/docs',
argv: ['/bin/node', 'app.js'],
execArgv: ['--import', './something.js', '--import=./else.js'],
},
{
appPath: '/user/tim/docs/app.js',
importPaths: ['/user/tim/docs/something.js', '/user/tim/docs/else.js'],
requirePaths: [],
},
],
[
{
cwd: () => '/user/tim/docs',
argv: ['/bin/node', 'app.js'],
execArgv: ['--require', './something.js', '--import=else.js'],
},
{
appPath: '/user/tim/docs/app.js',
importPaths: ['/user/tim/docs/else.js'],
requirePaths: ['/user/tim/docs/something.js'],
},
],
[
{
cwd: () => '/user/tim/docs',
argv: ['/bin/node', 'app.js'],
execArgv: ['--require=here/something.js'],
},
{
appPath: '/user/tim/docs/app.js',
importPaths: [],
requirePaths: ['/user/tim/docs/here/something.js'],
},
],
];

describe('getEntryPointType', () => {
it.each(PROCESS_ARG_TESTS)('parseProcessArgs', (input, output) => {
const result = parseProcessPaths(input);
expect(result).toStrictEqual(output);
});

it('app absolute', () => {
const ctx = getEntryPointType(defaultStackParser, {
cwd: () => __dirname,
argv: ['/bin/node', __filename],
execArgv: [],
});

expect(ctx).toEqual('app');
});

it('app relative', () => {
const ctx = getEntryPointType(defaultStackParser, {
cwd: () => __dirname,
argv: ['/bin/node', 'entry-point.test.ts'],
execArgv: [],
});

expect(ctx).toEqual('app');
});

it('import absolute', () => {
const ctx = getEntryPointType(defaultStackParser, {
cwd: () => __dirname,
argv: ['/bin/node', 'app.ts'],
execArgv: ['--import', __filename],
});

expect(ctx).toEqual('import');
});

it('import relative', () => {
const ctx = getEntryPointType(defaultStackParser, {
cwd: () => __dirname,
argv: ['/bin/node', 'app.ts'],
execArgv: ['--import', './entry-point.test.ts'],
});

expect(ctx).toEqual('import');
});

it('require relative', () => {
const ctx = getEntryPointType(defaultStackParser, {
cwd: () => __dirname,
argv: ['/bin/node', 'app.ts'],
execArgv: ['--require', './entry-point.test.ts'],
});

expect(ctx).toEqual('require');
});
});
2 changes: 1 addition & 1 deletion packages/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"include": ["src/**/*"],

"compilerOptions": {
"lib": ["es2018"],
"lib": ["es2018", "es2020.string"],
Copy link
Collaborator Author

@timfish timfish Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use string.matchAll and it turns out everything in es2020.string is fully supported by all the Node versions we support.

"module": "Node16"
}
}
Loading