Skip to content

Commit 1cdd51b

Browse files
authored
add support for oxc parsing in the rewriter (#7619)
1 parent 2c69381 commit 1cdd51b

File tree

6 files changed

+342
-122
lines changed

6 files changed

+342
-122
lines changed

LICENSE-3rdparty.csv

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,56 @@
55
"@datadog/native-iast-taint-tracking","https://github.com/DataDog/dd-native-iast-taint-tracking-js","['Apache-2.0']","['Datadog Inc.']"
66
"@datadog/native-metrics","https://github.com/DataDog/dd-native-metrics-js","['Apache-2.0']","['Datadog Inc.']"
77
"@datadog/openfeature-node-server","https://github.com/DataDog/openfeature-js-client","['Apache-2.0']","['DataDog']"
8-
"@datadog/pprof","https://github.com/DataDog/pprof-nodejs","['Apache-2.0']","['Google Inc.']"
9-
"@datadog/sketches-js","https://github.com/DataDog/sketches-js","['Apache-2.0']","['DataDog']"
10-
"@datadog/wasm-js-rewriter","https://github.com/DataDog/dd-wasm-js-rewriter","['Apache-2.0']","['Datadog Inc.']"
11-
"@isaacs/ttlcache","https://github.com/isaacs/ttlcache","['BlueOak-1.0.0']","['Isaac Z. Schlueter']"
12-
"@jsep-plugin/assignment","https://github.com/EricSmekens/jsep","['MIT']","['Shelly']"
13-
"@jsep-plugin/regex","https://github.com/EricSmekens/jsep","['MIT']","['Shelly']"
14-
"@opentelemetry/api","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
15-
"@opentelemetry/api-logs","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
16-
"@opentelemetry/core","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
17-
"@opentelemetry/resources","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
18-
"@opentelemetry/semantic-conventions","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
19-
"@protobufjs/aspromise","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
20-
"@protobufjs/base64","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
21-
"@protobufjs/codegen","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
8+
"@datadog/pprof","https://github.com/DataDog/pprof-nodejs","['Apache-2.0']","['Google Inc.']"
9+
"@datadog/sketches-js","https://github.com/DataDog/sketches-js","['Apache-2.0']","['DataDog']"
10+
"@datadog/wasm-js-rewriter","https://github.com/DataDog/dd-wasm-js-rewriter","['Apache-2.0']","['Datadog Inc.']"
11+
"@emnapi/core","https://github.com/toyobayashi/emnapi","['MIT']","['toyobayashi']"
12+
"@emnapi/runtime","https://github.com/toyobayashi/emnapi","['MIT']","['toyobayashi']"
13+
"@emnapi/wasi-threads","https://github.com/toyobayashi/emnapi","['MIT']","['toyobayashi']"
14+
"@isaacs/ttlcache","https://github.com/isaacs/ttlcache","['BlueOak-1.0.0']","['Isaac Z. Schlueter']"
15+
"@jsep-plugin/assignment","https://github.com/EricSmekens/jsep","['MIT']","['Shelly']"
16+
"@jsep-plugin/regex","https://github.com/EricSmekens/jsep","['MIT']","['Shelly']"
17+
"@napi-rs/wasm-runtime","https://github.com/napi-rs/napi-rs","['MIT']","['LongYinan']"
18+
"@opentelemetry/api","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
19+
"@opentelemetry/api-logs","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
20+
"@opentelemetry/core","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
21+
"@opentelemetry/resources","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
22+
"@opentelemetry/semantic-conventions","https://github.com/open-telemetry/opentelemetry-js","['Apache-2.0']","['OpenTelemetry Authors']"
23+
"@oxc-parser/binding-android-arm-eabi","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
24+
"@oxc-parser/binding-android-arm64","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
25+
"@oxc-parser/binding-darwin-arm64","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
26+
"@oxc-parser/binding-darwin-x64","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
27+
"@oxc-parser/binding-freebsd-x64","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
28+
"@oxc-parser/binding-linux-arm-gnueabihf","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
29+
"@oxc-parser/binding-linux-arm-musleabihf","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
30+
"@oxc-parser/binding-linux-arm64-gnu","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
31+
"@oxc-parser/binding-linux-arm64-musl","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
32+
"@oxc-parser/binding-linux-ppc64-gnu","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
33+
"@oxc-parser/binding-linux-riscv64-gnu","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
34+
"@oxc-parser/binding-linux-riscv64-musl","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
35+
"@oxc-parser/binding-linux-s390x-gnu","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
36+
"@oxc-parser/binding-linux-x64-gnu","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
37+
"@oxc-parser/binding-linux-x64-musl","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
38+
"@oxc-parser/binding-openharmony-arm64","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
39+
"@oxc-parser/binding-wasm32-wasi","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
40+
"@oxc-parser/binding-win32-arm64-msvc","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
41+
"@oxc-parser/binding-win32-ia32-msvc","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
42+
"@oxc-parser/binding-win32-x64-msvc","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
43+
"@oxc-project/types","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
44+
"@protobufjs/aspromise","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
45+
"@protobufjs/base64","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
46+
"@protobufjs/codegen","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
2247
"@protobufjs/eventemitter","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
2348
"@protobufjs/fetch","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
2449
"@protobufjs/float","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
2550
"@protobufjs/inquire","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
26-
"@protobufjs/path","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
27-
"@protobufjs/pool","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
28-
"@protobufjs/utf8","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
29-
"@types/node","https://github.com/DefinitelyTyped/DefinitelyTyped","['MIT']","['DefinitelyTyped']"
30-
"acorn","https://github.com/acornjs/acorn","['MIT']","['acornjs']"
31-
"acorn-import-attributes","https://github.com/xtuc/acorn-import-attributes","['MIT']","['Sven Sauleau']"
51+
"@protobufjs/path","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
52+
"@protobufjs/pool","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
53+
"@protobufjs/utf8","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
54+
"@tybys/wasm-util","https://github.com/toyobayashi/wasm-util","['MIT']","['toyobayashi']"
55+
"@types/node","https://github.com/DefinitelyTyped/DefinitelyTyped","['MIT']","['DefinitelyTyped']"
56+
"acorn","https://github.com/acornjs/acorn","['MIT']","['acornjs']"
57+
"acorn-import-attributes","https://github.com/xtuc/acorn-import-attributes","['MIT']","['Sven Sauleau']"
3258
"argparse","https://github.com/nodeca/argparse","['Python-2.0']","['nodeca']"
3359
"astring","https://github.com/davidbonnet/astring","['MIT']","['David Bonnet']"
3460
"cjs-module-lexer","https://github.com/nodejs/cjs-module-lexer","['MIT']","['Guy Bedford']"
@@ -55,23 +81,25 @@
5581
"meriyah","https://github.com/meriyah/meriyah","['ISC']","['Kenny F.']"
5682
"module-details-from-path","https://github.com/watson/module-details-from-path","['MIT']","['Thomas Watson']"
5783
"mutexify","https://github.com/mafintosh/mutexify","['MIT']","['Mathias Buus']"
58-
"node-addon-api","https://github.com/nodejs/node-addon-api","['MIT']","['nodejs']"
59-
"node-gyp-build","https://github.com/prebuild/node-gyp-build","['MIT']","['Mathias Buus']"
60-
"opentracing","https://github.com/opentracing/opentracing-javascript","['Apache-2.0']","['opentracing']"
61-
"p-limit","https://github.com/sindresorhus/p-limit","['MIT']","['Sindre Sorhus']"
62-
"path-to-regexp","https://github.com/pillarjs/path-to-regexp","['MIT']","['pillarjs']"
63-
"pprof-format","https://github.com/DataDog/pprof-format","['MIT']","['Datadog Inc.']"
84+
"node-addon-api","https://github.com/nodejs/node-addon-api","['MIT']","['nodejs']"
85+
"node-gyp-build","https://github.com/prebuild/node-gyp-build","['MIT']","['Mathias Buus']"
86+
"opentracing","https://github.com/opentracing/opentracing-javascript","['Apache-2.0']","['opentracing']"
87+
"oxc-parser","https://github.com/oxc-project/oxc","['MIT']","['Boshen and oxc contributors']"
88+
"p-limit","https://github.com/sindresorhus/p-limit","['MIT']","['Sindre Sorhus']"
89+
"path-to-regexp","https://github.com/pillarjs/path-to-regexp","['MIT']","['pillarjs']"
90+
"pprof-format","https://github.com/DataDog/pprof-format","['MIT']","['Datadog Inc.']"
6491
"protobufjs","https://github.com/protobufjs/protobuf.js","['BSD-3-Clause']","['Daniel Wirtz']"
6592
"queue-tick","https://github.com/mafintosh/queue-tick","['MIT']","['Mathias Buus']"
6693
"retry","https://github.com/tim-kos/node-retry","['MIT']","['Tim Koschützki']"
6794
"rfdc","https://github.com/davidmarkclements/rfdc","['MIT']","['David Mark Clements']"
6895
"semifies","https://github.com/holepunchto/semifies","['Apache-2.0']","['Holepunch Inc']"
6996
"shell-quote","https://github.com/ljharb/shell-quote","['MIT']","['James Halliday']"
70-
"source-map","https://github.com/mozilla/source-map","['BSD-3-Clause']","['Nick Fitzgerald']"
71-
"spark-md5","https://github.com/satazor/js-spark-md5","['(WTFPL OR MIT)']","['André Cruz']"
72-
"tlhunter-sorted-set","https://github.com/tlhunter/node-sorted-set","['MIT']","['Thomas Hunter II']"
73-
"ttl-set","https://github.com/watson/ttl-set","['MIT']","['Thomas Watson']"
74-
"undici-types","https://github.com/nodejs/undici","['MIT']","['nodejs']"
75-
"yocto-queue","https://github.com/sindresorhus/yocto-queue","['MIT']","['Sindre Sorhus']"
97+
"source-map","https://github.com/mozilla/source-map","['BSD-3-Clause']","['Nick Fitzgerald']"
98+
"spark-md5","https://github.com/satazor/js-spark-md5","['(WTFPL OR MIT)']","['André Cruz']"
99+
"tlhunter-sorted-set","https://github.com/tlhunter/node-sorted-set","['MIT']","['Thomas Hunter II']"
100+
"tslib","https://github.com/microsoft/tslib","['0BSD']","['Microsoft Corp.']"
101+
"ttl-set","https://github.com/watson/ttl-set","['MIT']","['Thomas Watson']"
102+
"undici-types","https://github.com/nodejs/undici","['MIT']","['nodejs']"
103+
"yocto-queue","https://github.com/sindresorhus/yocto-queue","['MIT']","['Sindre Sorhus']"
76104
"aws-lambda-nodejs-runtime-interface-client","https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/blob/v2.1.0/src/utils/UserFunction.ts","['Apache-2.0']","['Amazon.com Inc. or its affiliates']"
77105
"is-git-url","https://github.com/jonschlinkert/is-git-url/blob/396965ffabf2f46656c8af4c47bef1d69f09292e/index.js#L9C15-L9C87","['MIT']","['Jon Schlinkert']"

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@
143143
"@datadog/pprof": "5.13.4",
144144
"@datadog/wasm-js-rewriter": "5.0.1",
145145
"@opentelemetry/api": ">=1.0.0 <1.10.0",
146-
"@opentelemetry/api-logs": "<1.0.0"
146+
"@opentelemetry/api-logs": "<1.0.0",
147+
"oxc-parser": "^0.115.0"
147148
},
148149
"devDependencies": {
149150
"@babel/helpers": "^7.28.6",
Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,74 @@
11
'use strict'
22

3-
let meriyah
4-
let astring
5-
let esquery
3+
const log = require('../../../../dd-trace/src/log')
64

7-
module.exports = {
8-
parse: (...args) => {
9-
meriyah ??= require('../../../../../vendor/dist/meriyah')
5+
// eslint-disable-next-line camelcase, no-undef
6+
const runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
7+
8+
const compiler = {
9+
parse: (sourceText, options) => {
10+
try {
11+
// TODO: Figure out ESBuild `createRequire` issue and remove this hack.
12+
const oxc = runtimeRequire(['oxc', 'parser'].join('-'))
13+
14+
compiler.parse = (sourceText, options) => {
15+
const { program, errors } = oxc.parseSync('index.js', sourceText, {
16+
...options,
17+
preserveParens: false,
18+
})
19+
20+
if (errors?.length > 0) throw errors[0]
21+
22+
return program
23+
}
24+
} catch (e) {
25+
log.error(e)
26+
27+
// Fallback for when OXC is not available.
28+
const meriyah = require('../../../../../vendor/dist/meriyah')
1029

11-
return meriyah.parse(...args)
30+
compiler.parse = (sourceText, { range, sourceType } = {}) => {
31+
return meriyah.parse(sourceText.toString(), {
32+
loc: range,
33+
ranges: range,
34+
module: sourceType === 'module',
35+
})
36+
}
37+
}
38+
39+
return compiler.parse(sourceText, options)
1240
},
1341

1442
generate: (...args) => {
15-
astring ??= require('../../../../../vendor/dist/astring')
43+
const astring = require('../../../../../vendor/dist/astring')
44+
45+
compiler.generate = astring.generate
1646

17-
return astring.generate(...args)
47+
return compiler.generate(...args)
1848
},
1949

2050
traverse: (ast, query, visitor) => {
21-
esquery ??= require('../../../../../vendor/dist/esquery').default
51+
const esquery = require('../../../../../vendor/dist/esquery').default
2252

23-
const selector = esquery.parse(query)
53+
compiler.traverse = (ast, query, visitor) => {
54+
return esquery.traverse(ast, esquery.parse(query), visitor)
55+
}
2456

25-
return esquery.traverse(ast, selector, visitor)
57+
return compiler.traverse(ast, query, visitor)
2658
},
2759

2860
query: (ast, query) => {
29-
esquery ??= require('../../../../../vendor/dist/esquery').default
61+
const esquery = require('../../../../../vendor/dist/esquery').default
62+
63+
compiler.query = esquery.query
3064

31-
return esquery.query(ast, query)
65+
return compiler.query(ast, query)
3266
},
3367
}
68+
69+
module.exports = {
70+
parse: (...args) => compiler.parse(...args),
71+
generate: (...args) => compiler.generate(...args),
72+
traverse: (...args) => compiler.traverse(...args),
73+
query: (...args) => compiler.query(...args),
74+
}

packages/datadog-instrumentations/src/helpers/rewriter/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ let SourceMapGenerator
5656
function rewrite (content, filename, format) {
5757
if (!content) return content
5858

59+
const sourceType = format === 'module' ? 'module' : 'script'
60+
5961
try {
6062
let ast
6163

@@ -68,10 +70,10 @@ function rewrite (content, filename, format) {
6870
if (!filename.endsWith(`${name}/${filePath}`)) continue
6971
if (!satisfies(filename, filePath, versionRange)) continue
7072

71-
ast ??= parse(content.toString(), { loc: true, ranges: true, module: format === 'module' })
73+
ast ??= parse(content.toString(), { range: true, sourceType })
7274

7375
const query = astQuery || fromFunctionQuery(functionQuery)
74-
const state = { ...inst, format, functionQuery }
76+
const state = { ...inst, sourceType, functionQuery }
7577

7678
traverse(ast, query, (...args) => transform(state, ...args))
7779
}

packages/datadog-instrumentations/src/helpers/rewriter/transforms.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ const tracingChannelPredicate = (node) => (
88
)
99

1010
const transforms = module.exports = {
11-
tracingChannelImport ({ format }, node) {
11+
tracingChannelImport ({ sourceType }, node) {
1212
if (node.body.some(tracingChannelPredicate)) return
1313

1414
const index = node.body.findIndex(child => child.directive === 'use strict')
15-
const code = format === 'module'
15+
const code = sourceType === 'module'
1616
? 'import { tracingChannel as tr_ch_apm_tracingChannel } from "diagnostics_channel"'
1717
: 'const {tracingChannel: tr_ch_apm_tracingChannel} = require("diagnostics_channel")'
1818

19-
node.body.splice(index + 1, 0, parse(code, { module: format === 'module' }).body[0])
19+
node.body.splice(index + 1, 0, parse(code, { sourceType }).body[0])
2020
},
2121

2222
tracingChannelDeclaration (state, node) {

0 commit comments

Comments
 (0)