-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathloader-hook.mjs
More file actions
77 lines (63 loc) · 2.57 KB
/
loader-hook.mjs
File metadata and controls
77 lines (63 loc) · 2.57 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
import { initialize as origInitialize, load as origLoad, resolve } from 'import-in-the-middle/hook.mjs'
import regexpEscapeModule from './vendor/dist/escape-string-regexp/index.js'
import hooks from './packages/datadog-instrumentations/src/helpers/hooks.js'
import configHelper from './packages/dd-trace/src/config/helper.js'
import * as rewriterLoader from './packages/datadog-instrumentations/src/helpers/rewriter/loader.mjs'
import { isRelativeRequire } from './packages/datadog-instrumentations/src/helpers/shared-utils.js'
// This file must support Node.js 12.0.0 syntax
const regexpEscape = regexpEscapeModule.default
// For some reason `getEnvironmentVariable` is not otherwise available to ESM.
const env = configHelper.getEnvironmentVariable
function initialize (data = {}) {
if (data.include == null) data.include = []
if (data.exclude == null) data.exclude = []
addInstrumentations(data)
addSecurityControls(data)
addExclusions(data)
return origInitialize(data)
}
function load (url, context, nextLoad) {
return rewriterLoader.load(url, context, (url, context) => origLoad(url, context, nextLoad))
}
function addInstrumentations (data) {
const instrumentations = Object.keys(hooks)
for (const moduleName of instrumentations) {
// Skip instrumentation hooks with relative module names
// since there is no current business need of instrumenting imports outside of the node_modules folder
if (!isRelativeRequire(moduleName)) {
data.include.push(new RegExp(`node_modules/${moduleName}/(?!node_modules).+`), moduleName)
}
}
}
function addSecurityControls (data) {
const raw = env('DD_IAST_SECURITY_CONTROLS_CONFIGURATION')
if (!raw) return
// Parse `;`-separated entries and take the 3rd `:`-separated segment.
// Expected form (per entry): `<...>:<...>:<subpath>:<...>`
const entries = raw.split(';')
for (const entry of entries) {
if (entry) {
const first = entry.indexOf(':')
if (first === -1) continue
const second = entry.indexOf(':', first + 1)
if (second === -1) continue
const third = entry.indexOf(':', second + 1)
const subpath = entry.slice(second + 1, third === -1 ? undefined : third).trim()
if (subpath) {
data.include.push(new RegExp(regexpEscape(subpath)))
}
}
}
}
function addExclusions (data) {
data.exclude.push(...iitmExclusions)
}
export const iitmExclusions = [
/middle/,
/langsmith/,
/openai\/_shims/,
/openai\/resources\/chat\/completions\/messages/,
/openai\/agents-core\/dist\/shims/,
/@anthropic-ai\/sdk\/_shims/,
]
export { initialize, load, resolve }