-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ec9a6e
feat(node): Add a function to get the current execution context
timfish 14d2512
Rename
timfish 1a17959
Stack parser doesn't need to be configurable
timfish 5fd5c94
Lint
timfish b2cca9a
Merge branch 'develop' into timfish/feat/getExecutionContext
timfish 9600d66
Merge branch 'develop' into timfish/feat/getExecutionContext
timfish 0496899
Merge branch 'develop' into timfish/feat/getExecutionContext
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ines2020.string
is fully supported by all the Node versions we support.