Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
88 lines (77 loc) · 2.94 KB
/
Copy pathutils.ts
File metadata and controls
88 lines (77 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { getCurrentHub } from '@sentry/core';
import { getMainCarrier } from '@sentry/hub';
import { SpanContext } from '@sentry/types';
import * as domain from 'domain';
import * as fs from 'fs';
import * as path from 'path';
/**
* Recursively read the contents of a directory.
*
* @param targetDir Absolute or relative path of the directory to scan. All returned paths will be relative to this
* directory.
* @returns Array holding all relative paths
*/
export function deepReadDirSync(targetDir: string): string[] {
const targetDirAbsPath = path.resolve(targetDir);
if (!fs.existsSync(targetDirAbsPath)) {
throw new Error(`Cannot read contents of ${targetDirAbsPath}. Directory does not exist.`);
}
if (!fs.statSync(targetDirAbsPath).isDirectory()) {
throw new Error(`Cannot read contents of ${targetDirAbsPath}, because it is not a directory.`);
}
// This does the same thing as its containing function, `deepReadDirSync` (except that - purely for convenience - it
// deals in absolute paths rather than relative ones). We need this to be separate from the outer function to preserve
// the difference between `targetDirAbsPath` and `currentDirAbsPath`.
const deepReadCurrentDir = (currentDirAbsPath: string): string[] => {
return fs.readdirSync(currentDirAbsPath).reduce((absPaths: string[], itemName: string) => {
const itemAbsPath = path.join(currentDirAbsPath, itemName);
if (fs.statSync(itemAbsPath).isDirectory()) {
return [...absPaths, ...deepReadCurrentDir(itemAbsPath)];
}
return [...absPaths, itemAbsPath];
}, []);
};
return deepReadCurrentDir(targetDirAbsPath).map(absPath => path.relative(targetDirAbsPath, absPath));
}
type Trace<T> = (spanContext: SpanContext, fn: () => T) => Promise<T>
type DomainExt = ReturnType<typeof domain.create> & { parent?: DomainExt }
const branchHubAndTrace: Trace<unknown> = async (spanContext, fn) => new Promise((resolve, reject) => {
const d = domain.create() as DomainExt
// @ts-ignore
d.parent = (domain as unknown).active
d.on('error', reject)
d.run(async () => {
const parent = getCurrentHub().getScope()
const span = parent?.getSpan()?.startChild(spanContext)
parent?.setSpan(span)
try {
resolve(await fn())
} catch(e) {
span?.setStatus('unknown_error')
reject(e)
} finally {
span?.finish()
delete d.parent
}
})
})
const useScopeAndTrace: Trace<unknown> = async (spanContext, fn) => {
const span = getCurrentHub().getScope()?.getSpan()?.startChild(spanContext)
try {
return await fn()
} catch(e) {
span?.setStatus('unknown_error')
throw e
} finally {
span?.finish()
}
}
export const trace: Trace<unknown> = async (spanContext, fn) => {
const carrier = getMainCarrier()
const branchStrategy = carrier.__SENTRY__?.enableHubBranching
if (branchStrategy) {
return branchHubAndTrace(spanContext, fn)
} else {
return useScopeAndTrace(spanContext, fn)
}
}