From 4537686d775c18fe9e31139b28f5bb9b5649e235 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 19 Apr 2025 17:06:01 +0200 Subject: [PATCH 01/12] feat: Remove yaml configuration --- Cargo.lock | 613 ++++++++++-------- Cargo.toml | 14 +- src/config.rs | 118 +--- src/error.rs | 10 - src/function_query.rs | 66 +- src/instrumentation.rs | 6 +- src/lib.rs | 32 +- tests/class_method_cjs/instrumentations.yml | 12 - tests/class_method_cjs/mod.rs | 26 + tests/common/mod.rs | 35 +- tests/constructor_cjs/instrumentations.yml | 12 - tests/constructor_cjs/mod.rs | 26 + tests/constructor_mjs/instrumentations.yml | 12 - tests/constructor_mjs/mod.rs | 26 + tests/decl_cjs/instrumentations.yml | 13 - tests/decl_cjs/mod.rs | 26 + tests/decl_mjs/instrumentations.yml | 12 - tests/decl_mjs/mod.rs | 26 + tests/expr_cjs/instrumentations.yml | 12 - tests/expr_cjs/mod.rs | 26 + tests/expr_mjs/instrumentations.yml | 12 - tests/expr_mjs/mod.rs | 26 + tests/index_cjs/instrumentations.yml | 13 - tests/index_cjs/mod.rs | 26 + tests/instrumentor_test.rs | 58 +- .../instrumentations.yml | 22 - tests/multiple_class_method_cjs/mod.rs | 45 ++ tests/multiple_load_cjs/instrumentations.yml | 12 - tests/multiple_load_cjs/mod.rs | 26 + tests/no_index_cjs/instrumentations.yml | 12 - tests/no_index_cjs/mod.js | 9 - tests/no_index_cjs/test.js | 17 - tests/object_method_cjs/instrumentations.yml | 12 - tests/object_method_cjs/mod.rs | 26 + tests/polyfill_cjs/instrumentations.yml | 13 - tests/polyfill_cjs/mod.rs | 29 + tests/polyfill_mjs/instrumentations.yml | 13 - tests/polyfill_mjs/mod.rs | 29 + 38 files changed, 773 insertions(+), 750 deletions(-) delete mode 100644 tests/class_method_cjs/instrumentations.yml create mode 100644 tests/class_method_cjs/mod.rs delete mode 100644 tests/constructor_cjs/instrumentations.yml create mode 100644 tests/constructor_cjs/mod.rs delete mode 100644 tests/constructor_mjs/instrumentations.yml create mode 100644 tests/constructor_mjs/mod.rs delete mode 100644 tests/decl_cjs/instrumentations.yml create mode 100644 tests/decl_cjs/mod.rs delete mode 100644 tests/decl_mjs/instrumentations.yml create mode 100644 tests/decl_mjs/mod.rs delete mode 100644 tests/expr_cjs/instrumentations.yml create mode 100644 tests/expr_cjs/mod.rs delete mode 100644 tests/expr_mjs/instrumentations.yml create mode 100644 tests/expr_mjs/mod.rs delete mode 100644 tests/index_cjs/instrumentations.yml create mode 100644 tests/index_cjs/mod.rs delete mode 100644 tests/multiple_class_method_cjs/instrumentations.yml create mode 100644 tests/multiple_class_method_cjs/mod.rs delete mode 100644 tests/multiple_load_cjs/instrumentations.yml create mode 100644 tests/multiple_load_cjs/mod.rs delete mode 100644 tests/no_index_cjs/instrumentations.yml delete mode 100644 tests/no_index_cjs/mod.js delete mode 100644 tests/no_index_cjs/test.js delete mode 100644 tests/object_method_cjs/instrumentations.yml create mode 100644 tests/object_method_cjs/mod.rs delete mode 100644 tests/polyfill_cjs/instrumentations.yml create mode 100644 tests/polyfill_cjs/mod.rs delete mode 100644 tests/polyfill_mjs/instrumentations.yml create mode 100644 tests/polyfill_mjs/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 77726b5..a1c7d33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,15 +73,9 @@ checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "anyhow" -version = "1.0.96" +version = "1.0.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b964d184e89d9b6b67dd2715bc8e74cf3107fb2b529990c90cf517326150bf4" - -[[package]] -name = "arraydeque" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" +checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" [[package]] name = "arrayvec" @@ -97,9 +91,9 @@ checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" [[package]] name = "assert_cmd" -version = "2.0.16" +version = "2.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1835b7f27878de8525dc71410b5a31cdcc5f230aed5ba5df968e09c201b23d" +checksum = "2bd389a4b2970a01282ee455294913c0a43724daedcd1a24c3eb0ec1c1320b66" dependencies = [ "anstyle", "bstr", @@ -125,9 +119,9 @@ dependencies = [ [[package]] name = "auto_impl" -version = "1.2.1" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e12882f59de5360c748c4cbf569a042d5fb0eb515f7bea9c1f470b47f6ffbd73" +checksum = "ffdcb70bdbc4d478427380519163274ac86e52916e10f0a8889adf0f96d3fee7" dependencies = [ "proc-macro2", "quote", @@ -142,9 +136,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "base64" -version = "0.21.7" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "base64-simd" @@ -166,9 +160,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" [[package]] name = "bitvec" @@ -193,9 +187,9 @@ dependencies = [ [[package]] name = "browserslist-rs" -version = "0.17.0" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74c973b79d9b6b89854493185ab760c6ef8e54bcfad10ad4e33991e46b374ac8" +checksum = "2f95aff901882c66e4b642f3f788ceee152ef44f8a5ef12cb1ddee5479c483be" dependencies = [ "ahash", "chrono", @@ -210,9 +204,9 @@ dependencies = [ [[package]] name = "bstr" -version = "1.11.3" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "531a9155a481e2ee699d4f98f43c0ca4ff8ee1bfd55c31e9e98fb29d2b176fe0" +checksum = "234113d19d0d7d613b40e86fb654acf958910802bcceab913a4f9e7cda03b1a4" dependencies = [ "memchr", "regex-automata 0.4.9", @@ -259,9 +253,9 @@ checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce" [[package]] name = "bytes" -version = "1.10.0" +version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "camino" @@ -289,7 +283,7 @@ checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037" dependencies = [ "camino", "cargo-platform", - "semver 1.0.25", + "semver 1.0.26", "serde", "serde_json", "thiserror 1.0.69", @@ -297,16 +291,16 @@ dependencies = [ [[package]] name = "cargo_metadata" -version = "0.19.1" +version = "0.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8769706aad5d996120af43197bf46ef6ad0fda35216b4505f926a365a232d924" +checksum = "dd5eb614ed4c27c5d706420e4320fbe3216ab31fa1c33cd8246ac36dae4479ba" dependencies = [ "camino", "cargo-platform", - "semver 1.0.25", + "semver 1.0.26", "serde", "serde_json", - "thiserror 2.0.11", + "thiserror 2.0.12", ] [[package]] @@ -320,9 +314,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.15" +version = "1.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c736e259eea577f443d5c86c304f9f4ae0295c43f3ba05c21f1d66b5f06001af" +checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362" dependencies = [ "shlex", ] @@ -385,9 +379,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.10" +version = "0.20.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" dependencies = [ "darling_core", "darling_macro", @@ -395,9 +389,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.10" +version = "0.20.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" dependencies = [ "fnv", "ident_case", @@ -409,9 +403,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.10" +version = "0.20.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" dependencies = [ "darling_core", "quote", @@ -433,9 +427,9 @@ dependencies = [ [[package]] name = "data-encoding" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "575f75dfd25738df5b91b8e43e14d44bda14637a58fae779fd2b064f8bf3e010" +checksum = "2a2330da5de22e8a3cb63252ce2abb30116bf5265e89c0e01bc17015ce30a476" [[package]] name = "debugid" @@ -525,18 +519,9 @@ checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" [[package]] name = "either" -version = "1.14.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7914353092ddf589ad78f25c5c1c21b7f80b0ff8621e7c814c3485b5306da9d" - -[[package]] -name = "encoding_rs" -version = "0.8.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3" -dependencies = [ - "cfg-if", -] +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "equivalent" @@ -546,9 +531,9 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "errno" -version = "0.3.10" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +checksum = "976dd42dc7e85965fe702eb8164f21f450704bdde31faefd6471dba214cb594e" dependencies = [ "libc", "windows-sys", @@ -562,9 +547,9 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" [[package]] name = "fixedbitset" -version = "0.4.2" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" [[package]] name = "fnv" @@ -572,12 +557,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "foldhash" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" - [[package]] name = "form_urlencoded" version = "1.2.1" @@ -627,14 +606,14 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +checksum = "73fea8450eea4bac3940448fb7ae50d91f034f941199fcd9d909a5a07aa455f0" dependencies = [ "cfg-if", "libc", - "wasi 0.13.3+wasi-0.2.2", - "windows-targets", + "r-efi", + "wasi 0.14.2+wasi-0.2.4", ] [[package]] @@ -667,18 +646,6 @@ name = "hashbrown" version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" -dependencies = [ - "foldhash", -] - -[[package]] -name = "hashlink" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" -dependencies = [ - "hashbrown 0.15.2", -] [[package]] name = "heck" @@ -714,14 +681,15 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.61" +version = "0.1.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8" dependencies = [ "android_system_properties", "core-foundation-sys", "iana-time-zone-haiku", "js-sys", + "log", "wasm-bindgen", "windows-core", ] @@ -776,9 +744,9 @@ dependencies = [ [[package]] name = "icu_locid_transform_data" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc8ff3388f852bede6b579ad4e978ab004f139284d7b28715f773507b946f6e" +checksum = "7515e6d781098bf9f7205ab3fc7e9709d34554ae0b21ddbcb5febfa4bc7df11d" [[package]] name = "icu_normalizer" @@ -800,9 +768,9 @@ dependencies = [ [[package]] name = "icu_normalizer_data" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8cafbf7aa791e9b22bec55a167906f9e1215fd475cd22adfcf660e03e989516" +checksum = "c5e8338228bdc8ab83303f16b797e177953730f601a96c25d10cb3ab0daa0cb7" [[package]] name = "icu_properties" @@ -821,9 +789,9 @@ dependencies = [ [[package]] name = "icu_properties_data" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67a8effbc3dd3e4ba1afa8ad918d5684b8868b3b26500753effea8d2eed19569" +checksum = "85fb8799753b75aee8d2a21d7c14d9f38921b54b3dbda10f5a3c7a7b82dba5e2" [[package]] name = "icu_provider" @@ -888,9 +856,9 @@ checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" [[package]] name = "indexmap" -version = "2.7.1" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e" dependencies = [ "equivalent", "hashbrown 0.15.2", @@ -920,9 +888,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" @@ -951,15 +919,15 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.170" +version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" +checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[package]] name = "linux-raw-sys" -version = "0.4.15" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" [[package]] name = "litemap" @@ -979,9 +947,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.26" +version = "0.4.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" +checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94" [[package]] name = "lru" @@ -1018,7 +986,7 @@ dependencies = [ "owo-colors", "textwrap", "thiserror 1.0.69", - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -1146,9 +1114,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.3" +version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "orchestrion-js" @@ -1160,7 +1128,6 @@ dependencies = [ "swc_core", "swc_ecma_parser", "swc_ecma_visit", - "yaml-rust2", ] [[package]] @@ -1181,6 +1148,25 @@ version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1036865bb9422d3300cf723f657c2851d0e9ab12567854b1f4eba3d77decf564" +[[package]] +name = "par-core" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b506ab63a8bd3cd38858c7bfc2d078a189dc3210c7f8c9be1bbaf50c082a0ae" +dependencies = [ + "once_cell", +] + +[[package]] +name = "par-iter" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a5b20f31e9ba82bfcbbb54a67aa40be6cebec9f668ba5753be138f9523c531a" +dependencies = [ + "either", + "par-core", +] + [[package]] name = "parking_lot" version = "0.12.3" @@ -1230,9 +1216,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" [[package]] name = "petgraph" -version = "0.6.5" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" +checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772" dependencies = [ "fixedbitset", "indexmap", @@ -1315,9 +1301,9 @@ dependencies = [ [[package]] name = "preset_env_base" -version = "2.0.1" +version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07852df2dda2f0ab8c3407a6fd19e9389563af11c20f6c299bd07ff9fc96d6ae" +checksum = "b06c1ead1873928228f01ffafe4800c3accb27d019c034626c54698408e36bfb" dependencies = [ "anyhow", "browserslist-rs", @@ -1325,7 +1311,7 @@ dependencies = [ "from_variant", "once_cell", "rustc-hash 2.1.1", - "semver 1.0.25", + "semver 1.0.26", "serde", "st-map", "tracing", @@ -1343,9 +1329,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.93" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] @@ -1381,13 +1367,19 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.38" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] +[[package]] +name = "r-efi" +version = "5.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74765f6d916ee2faa39bc8e68e4f3ed8949b48cccdac59983d287a7cb71ce9c5" + [[package]] name = "radium" version = "0.7.0" @@ -1426,9 +1418,9 @@ checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" [[package]] name = "redox_syscall" -version = "0.5.9" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b568323e98e49e2a0899dcee453dd679fae22d69adf9b11dd508d1549b7e2f" +checksum = "d2f103c6d277498fbceb16e84d317e2a400f160f46904d5f5410848c829511a3" dependencies = [ "bitflags", ] @@ -1545,9 +1537,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.44" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +checksum = "d97817398dd4bb2e6da002002db259209759911da105da92bec29ccb12cf58bf" dependencies = [ "bitflags", "errno", @@ -1558,15 +1550,15 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" +checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" [[package]] name = "ryu" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "ryu-js" @@ -1597,9 +1589,9 @@ dependencies = [ [[package]] name = "semver" -version = "1.0.25" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f79dfe2d285b0488816f30e700a7438c5a73d816b5b7d3ac72fbc48b0d185e03" +checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" dependencies = [ "serde", ] @@ -1612,18 +1604,18 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.218" +version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.218" +version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", @@ -1632,9 +1624,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.139" +version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ "itoa", "memchr", @@ -1708,9 +1700,9 @@ checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d" [[package]] name = "smallvec" -version = "1.14.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" +checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9" [[package]] name = "smartstring" @@ -1760,9 +1752,9 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stacker" -version = "0.1.19" +version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9156ebd5870ef293bfb43f91c7a74528d363ec0d424afe24160ed5a4343d08a" +checksum = "601f9201feb9b09c00266478bf459952b9ef9a6b94edb2f21eba14ab681a60a9" dependencies = [ "cc", "cfg-if", @@ -1808,9 +1800,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "swc" -version = "16.1.0" +version = "21.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5972bd6b22d5fe9790913b4dcb579e117f7e18ed37d209d8dbe8dbb46ee64efe" +checksum = "3809091d5035036db41f5212697b10e492a6c97d50bf76839ac52481547a0528" dependencies = [ "anyhow", "base64", @@ -1820,6 +1812,8 @@ dependencies = [ "jsonc-parser", "lru", "once_cell", + "par-core", + "par-iter", "parking_lot", "pathdiff", "regex", @@ -1901,9 +1895,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "8.0.0" +version = "8.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26fbd21a1179166b5635d4b7a6b5930cf34b803a7361e0297b04f84dc820db04" +checksum = "7d96ac5d021c7c20acb3073940b4ee59b62989a705f855783c4a452e0737a2e6" dependencies = [ "anyhow", "ast_node", @@ -1928,15 +1922,15 @@ dependencies = [ "swc_visit", "termcolor", "tracing", - "unicode-width", + "unicode-width 0.1.14", "url", ] [[package]] name = "swc_compiler_base" -version = "13.0.0" +version = "18.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cc6e0eaba5b6410fda9c3189145616b3146ac14328ffc000cfe8319e0dea530" +checksum = "3516918cdce803f6c175aeefefbf3a05b4064eadc2a772f98da63d4282f85497" dependencies = [ "anyhow", "base64", @@ -1987,9 +1981,9 @@ dependencies = [ [[package]] name = "swc_core" -version = "16.2.3" +version = "22.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0890fd720ce6a3df52a174f623351634fe8486d0137226de5eb7ea7eef211f3" +checksum = "750935e8c0a2c23b69968d8c8f65963750fb20c9803311e3d3ed248340fbac97" dependencies = [ "once_cell", "swc_allocator", @@ -2003,22 +1997,25 @@ dependencies = [ "swc_plugin", "swc_plugin_macro", "swc_plugin_proxy", + "swc_transform_common", "vergen", ] [[package]] name = "swc_ecma_ast" -version = "8.0.0" +version = "8.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c66db1e9b31f0f91ee0964aba014b4d2dfdc6c558732d106d762b43bedad2c4a" +checksum = "4062a54522a9c02d2b68cc09282774b87121cd48693b0e67ae8c18b31b709866" dependencies = [ "bitflags", "bytecheck", "is-macro", "num-bigint", + "once_cell", "phf", "rancor", "rkyv", + "rustc-hash 2.1.1", "scoped-tls", "serde", "string_enum", @@ -2030,9 +2027,9 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "8.0.1" +version = "10.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "874889c00e41e5ae487886ff4af2533944584e8b479bc469a3f9708cab7ecdb7" +checksum = "b85453d346d0642f296c2b3aa204886a6ae2b9652262c3468d6f4556c1ed020d" dependencies = [ "ascii", "compact_str", @@ -2053,9 +2050,9 @@ dependencies = [ [[package]] name = "swc_ecma_codegen_macros" -version = "1.0.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ac2ff0957329e0dfcde86a1ac465382e189bf42a5989720d3476bea78eaa31a" +checksum = "e99e1931669a67c83e2c2b4375674f6901d1480994a76aa75b23f1389e6c5076" dependencies = [ "proc-macro2", "quote", @@ -2065,9 +2062,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_bugfixes" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fbf52155fac8dbf8b13cf412da46e81f8bbe57467334a4e9434837f7bd61506" +checksum = "5e908297dfe18472b82b391ae444a72dbd63c4b5f2823eba52c1bf7972903952" dependencies = [ "rustc-hash 2.1.1", "swc_atoms", @@ -2083,9 +2080,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_common" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09054aad2b52da3e6cf72089237700ff43fc5e6ab3ee1c521583c2c549522a38" +checksum = "bb2d5902317bbf8e8c1944e63f19057e6dff1fb60a8a73f33bb26bdb2d365662" dependencies = [ "swc_common", "swc_ecma_ast", @@ -2096,9 +2093,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2015" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "124d5fdcdc9973b7dba1eb18874c5a7a40b9fadb32bc7c5e2fc4f30c69129fa1" +checksum = "fb1efa640c57cbc4eaa40625275a86ff99a29cd0f4997668c88117e86390e821" dependencies = [ "arrayvec", "indexmap", @@ -2123,9 +2120,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2016" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d557bc5bc9242e07d16e5d42fb1882856d9bafcd26eab77ba124b9e68444e83" +checksum = "b5b890417e8080d460e1962c73d58f94cca5b27c5ec89f8ba37a114c7dd6a76b" dependencies = [ "swc_atoms", "swc_common", @@ -2140,9 +2137,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2017" -version = "11.0.0" +version = "12.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ce9c93d6a0780fa84eab113083259228557bcd7ce3d27e0479e0d8de143012b" +checksum = "d7681f9e322245d0f0640db74c109f8a74cf9beaa32efe5e01e12d55afd1286d" dependencies = [ "serde", "swc_atoms", @@ -2158,9 +2155,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2018" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b926094b18e30780c231032ce8ad6240842d0b0cca01938c61370b67ed8911fc" +checksum = "a41fe86e2a237f1b87ed4d34c20a3721665328fc8f1b8e5e6bdeb022ce52f148" dependencies = [ "serde", "swc_atoms", @@ -2177,9 +2174,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2019" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "658ab6efb4ff84a91429d90f5add80c1cf19d9d720cb8e0a47863fc2628e3564" +checksum = "e06197f2f74f2a6366cfbf68d4de4feabf42bd2532413c71347ba7cdbe964c40" dependencies = [ "swc_atoms", "swc_common", @@ -2193,9 +2190,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2020" -version = "12.0.0" +version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3e0eac482a0ed60af3b7de78ca85e664095dfbd96a21dbafc8dff43e2f13b66" +checksum = "92568d138eec2894c644fbf865401778026b42b45fa1073739b732cd66d55b42" dependencies = [ "serde", "swc_atoms", @@ -2211,9 +2208,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2021" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89dd2f25812eab659bd088c2ace9837d5b6f064e7a184f27d7199d5aae493b20" +checksum = "6b38614b689a8ed0b4cda05bee30a7f908ea621db6010888f407be282884ecbe" dependencies = [ "swc_atoms", "swc_common", @@ -2227,9 +2224,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es2022" -version = "12.0.0" +version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa9e163b2badafc208995f771524492f8b003e52b82e0dff6c11fcb06662dc99" +checksum = "2923bf7ce2236f36aef951bd204ec115a17af421cdc696ff526c9ba22983533f" dependencies = [ "rustc-hash 2.1.1", "swc_atoms", @@ -2247,9 +2244,9 @@ dependencies = [ [[package]] name = "swc_ecma_compat_es3" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e5bce31592c053191996262d502f219a23edd53ae87ae7f54204bbdd94e5fcc" +checksum = "cf4b386df40a8b1d0a71eb54b5766ce483bb4f9311c4df931035542a39341861" dependencies = [ "swc_common", "swc_ecma_ast", @@ -2262,9 +2259,9 @@ dependencies = [ [[package]] name = "swc_ecma_ext_transforms" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "111d812c5e61ffc4f2e18573b0f09bcd870463b7eaa0a0419014d88cc7fc084b" +checksum = "0064bdc27ebff66cb92e596b13e9c0e13c671c56b327c0083c200e4793c8db2b" dependencies = [ "phf", "swc_atoms", @@ -2274,14 +2271,40 @@ dependencies = [ "swc_ecma_visit", ] +[[package]] +name = "swc_ecma_lexer" +version = "11.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0d9ed10e3efa2230d0b3d0ad63c2e67d9b40c3892f31a865ad14d6fa881e0e9" +dependencies = [ + "arrayvec", + "bitflags", + "either", + "new_debug_unreachable", + "num-bigint", + "num-traits", + "phf", + "rustc-hash 2.1.1", + "serde", + "smallvec", + "smartstring", + "stacker", + "swc_atoms", + "swc_common", + "swc_ecma_ast", + "tracing", + "typed-arena", +] + [[package]] name = "swc_ecma_lints" -version = "11.0.1" +version = "12.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39c61cc0dd2b072152de9cdb6eaf6cb57e979b359ed8d0eb86ce8c3450308b2e" +checksum = "d86c9a647230352f00452699472e16fa76ec54a9e4acfe7fb8c0c93ec3d0ee07" dependencies = [ "auto_impl", "dashmap", + "par-core", "parking_lot", "regex", "rustc-hash 2.1.1", @@ -2292,7 +2315,6 @@ dependencies = [ "swc_ecma_ast", "swc_ecma_utils", "swc_ecma_visit", - "swc_parallel", ] [[package]] @@ -2320,15 +2342,18 @@ dependencies = [ [[package]] name = "swc_ecma_minifier" -version = "12.0.1" +version = "16.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc1b838f90630f1c620113c3517eed44f1b36b09274ada47ed7e4cb4ec10899" +checksum = "4f1923c07f9858a1bcb79030c2c959d92a1fd1acb40189eccec714108233146f" dependencies = [ "arrayvec", + "bitflags", "indexmap", "num-bigint", "num_cpus", "once_cell", + "par-core", + "par-iter", "parking_lot", "phf", "radix_fmt", @@ -2349,17 +2374,18 @@ dependencies = [ "swc_ecma_usage_analyzer", "swc_ecma_utils", "swc_ecma_visit", - "swc_parallel", "swc_timer", "tracing", ] [[package]] name = "swc_ecma_parser" -version = "10.0.0" +version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9e336f2b460882df2c132328b3c29ab3e680e1db681a05ec3e406940d98320a" +checksum = "398f4105e5fa3dedcefc58d068a5b2d8330cd4a36c4ae1ef2da83ed0a3e3c61f" dependencies = [ + "arrayvec", + "bitflags", "either", "new_debug_unreachable", "num-bigint", @@ -2373,15 +2399,16 @@ dependencies = [ "swc_atoms", "swc_common", "swc_ecma_ast", + "swc_ecma_lexer", "tracing", "typed-arena", ] [[package]] name = "swc_ecma_preset_env" -version = "14.0.0" +version = "16.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71fa37f57f20e5641bdc5b728955e254b7cbc4dcc4313f6e8e088dd9ec62775d" +checksum = "dcdb997223f2c92bb31278cf25b37398209fe5ce6a5cf276cf0cdc264386124b" dependencies = [ "anyhow", "dashmap", @@ -2389,7 +2416,7 @@ dependencies = [ "once_cell", "preset_env_base", "rustc-hash 2.1.1", - "semver 1.0.25", + "semver 1.0.26", "serde", "serde_json", "st-map", @@ -2404,9 +2431,9 @@ dependencies = [ [[package]] name = "swc_ecma_quote_macros" -version = "10.0.0" +version = "11.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4822fc4052681e583cbac5f823351d38de3347b59edd56bebface11dc05e863" +checksum = "26132f0851c46a258f954cc00ca6c71fe6ab4520f6fde722e6e8a200c61f6c83" dependencies = [ "anyhow", "proc-macro2", @@ -2422,9 +2449,9 @@ dependencies = [ [[package]] name = "swc_ecma_testing" -version = "8.0.0" +version = "9.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e72a43b7acd904fa0c6d244a72aeda66febbc5a9720975481cb836d6804b604" +checksum = "cb63358ab7094db21eb0c90eba89161bbe3c35e39c27f414ecdc9f4ffc8bc601" dependencies = [ "anyhow", "hex", @@ -2435,10 +2462,11 @@ dependencies = [ [[package]] name = "swc_ecma_transforms" -version = "14.0.0" +version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243295ffdee377b2ee66c94145017f0f60f28f70353d1c02047c9e409f7cd0e2" +checksum = "13aeeeb6ba750d144d49d96f900063706e8e4ff45d63d1ccde0ce5f441bcee6a" dependencies = [ + "par-core", "swc_atoms", "swc_common", "swc_ecma_ast", @@ -2455,14 +2483,15 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_base" -version = "11.1.1" +version = "12.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53f823fb2ba61099c06f1557f4d7bc3a957147f2e39f92419204682aa62b46fc" +checksum = "b46e3a36213d78fb4233e596b8a5c81c6cdafe02d03d780eed006c983aa0a724" dependencies = [ "better_scoped_tls", "bitflags", "indexmap", "once_cell", + "par-core", "phf", "rustc-hash 2.1.1", "serde", @@ -2473,15 +2502,14 @@ dependencies = [ "swc_ecma_parser", "swc_ecma_utils", "swc_ecma_visit", - "swc_parallel", "tracing", ] [[package]] name = "swc_ecma_transforms_classes" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2111a904b8f3c5dd63f56e7c8048851fcd8f748691a162a5d19a5da49f4a9d35" +checksum = "d871bbd46d14d032a48c14096abd778a8a87831638343f28b581c3025daa7086" dependencies = [ "swc_atoms", "swc_common", @@ -2493,14 +2521,15 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_compat" -version = "12.0.0" +version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9e25a5cc997638fd050e5e1ddccb49688300f13940ade79ee9bbe584158697b" +checksum = "dbfdfb50bd6db7991105f371b23ebb7cc79d48f43f53866a9a55dfbf7cfacd36" dependencies = [ "arrayvec", "indexmap", "is-macro", "num-bigint", + "par-core", "serde", "smallvec", "swc_atoms", @@ -2541,9 +2570,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_module" -version = "12.0.0" +version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2911ff76a54c74eaf29518f15861326595f8f9d98a3086edcad30d3d46bea957" +checksum = "d0cf50886962aa3d7d20317a486971b91002a930b236c1e4af1f1050280b4070" dependencies = [ "Inflector", "anyhow", @@ -2569,13 +2598,14 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_optimization" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93e98cb0e4e10a839c553d610082b4b920a430019a0150067ac415e6049f12b2" +checksum = "6646a0a5e3662a2a86369a42f5203f1c92584c37502f9b79d4d10613db0c1fb3" dependencies = [ "dashmap", "indexmap", "once_cell", + "par-core", "petgraph", "rustc-hash 2.1.1", "serde_json", @@ -2587,15 +2617,14 @@ dependencies = [ "swc_ecma_transforms_macros", "swc_ecma_utils", "swc_ecma_visit", - "swc_fast_graph", "tracing", ] [[package]] name = "swc_ecma_transforms_proposal" -version = "11.0.1" +version = "12.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3072700bb4401fffed5a152248d0d173a41da94584a4267355fa6772538880b" +checksum = "5265158f5134b7b37dd2d53e7730921b8b5f567f6baddcc52129c2eb55927214" dependencies = [ "either", "rustc-hash 2.1.1", @@ -2613,9 +2642,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_react" -version = "12.0.0" +version = "13.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "311218980029ea376a1f53292418d852d50b461d15d9d39f830abf0d1c3bdd6c" +checksum = "d8e7635afe1e1e798d61ff3107b8d27e437e61f243dd226a47fb10724693be66" dependencies = [ "base64", "dashmap", @@ -2639,9 +2668,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_testing" -version = "11.0.0" +version = "15.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dce5bbd4417919ff4e7a15500c18d88fc135e57b512f3ebd59cf82439d80ef9" +checksum = "3f1e112d74cbf146d419b4df60de430fd8db4fef99df0443d7a96a3b30bd5878" dependencies = [ "ansi_term", "anyhow", @@ -2666,9 +2695,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_typescript" -version = "12.0.0" +version = "13.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19c32ebb3dd1942d35142de60c9dc0f3c034d26567de7eb8b3ad6de426f5b0e9" +checksum = "cec3c91a2c37372746ebc5608e30b7c2c3af60216768b59ec6413ee2bfe44c29" dependencies = [ "once_cell", "rustc-hash 2.1.1", @@ -2685,10 +2714,11 @@ dependencies = [ [[package]] name = "swc_ecma_usage_analyzer" -version = "12.0.0" +version = "13.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed536f224b5dd9c6c9431c3fca48f572324c485a15d865fa84a22a639e6be59" +checksum = "50907e1e2f522ddbe5f94bea99f11a219cc1486812d5e55e502dbe5e0f0acc35" dependencies = [ + "bitflags", "indexmap", "rustc-hash 2.1.1", "swc_atoms", @@ -2702,20 +2732,21 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "11.0.0" +version = "12.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "721dc779e7de200da96ac4002c710bc32c988e3e1ebf62b39d32bf99f14d9765" +checksum = "71d6c8ba7d987dcc254f05ad2c23e7a6ec3f259611af2923a8c1a0602556cd21" dependencies = [ "indexmap", "num_cpus", "once_cell", + "par-core", + "par-iter", "rustc-hash 2.1.1", "ryu-js", "swc_atoms", "swc_common", "swc_ecma_ast", "swc_ecma_visit", - "swc_parallel", "tracing", "unicode-id", ] @@ -2748,26 +2779,17 @@ dependencies = [ [[package]] name = "swc_error_reporters" -version = "9.0.0" +version = "10.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10ad5f4690758cedc202cf0f4c9d2369372c6692307f65bd40031de494662cfa" +checksum = "499cf6a20e6acb36f15e22cca18dadc108d7046ae062840b7371ae02eac4dfde" dependencies = [ "anyhow", "miette", "once_cell", "parking_lot", - "swc_common", -] - -[[package]] -name = "swc_fast_graph" -version = "9.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd24b9798b0538803d0a69cffa5f5e051087fa2bd0d23e5a2f05d32edf9ab671" -dependencies = [ - "indexmap", - "petgraph", - "rustc-hash 2.1.1", + "serde", + "serde_derive", + "serde_json", "swc_common", ] @@ -2794,15 +2816,6 @@ dependencies = [ "swc_common", ] -[[package]] -name = "swc_parallel" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5f75f1094d69174ef628e3665fff0f81d58e9f568802e3c90d332c72b0b6026" -dependencies = [ - "once_cell", -] - [[package]] name = "swc_plugin" version = "1.0.0" @@ -2814,9 +2827,9 @@ dependencies = [ [[package]] name = "swc_plugin_macro" -version = "1.0.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0917ccfdcd3fa6cf41bdacef2388702a3b274f9ea708d930e1e8db37c7c3e1c6" +checksum = "ace467dfafbbdf3aecff786b8605b35db57d945e92fd88800569aa2cba0cdf61" dependencies = [ "proc-macro2", "quote", @@ -2862,23 +2875,25 @@ dependencies = [ [[package]] name = "swc_transform_common" -version = "1.0.1" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79319c2165695896119f0cb22847dedfb0bd7f77acd98dbc5bc1f081105db6f3" +checksum = "e40bbeef964d6edd66081a31bbfeef913bb0be536e398392f99e8e91b7da63eb" dependencies = [ "better_scoped_tls", "once_cell", "rustc-hash 2.1.1", "serde", "serde_json", + "swc_common", ] [[package]] name = "swc_typescript" -version = "10.0.0" +version = "11.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8facec2d8504b0e38195d60de884056d0d2365ffdc78cd2300ee595fcf9c625d" +checksum = "e8538a8b2e8d8a3ebbf58fe7f933d7b4bb01a291fbd7356352ea255cc15bbc70" dependencies = [ + "bitflags", "petgraph", "rustc-hash 2.1.1", "swc_atoms", @@ -2901,9 +2916,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.98" +version = "2.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" dependencies = [ "proc-macro2", "quote", @@ -2929,13 +2944,12 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "tempfile" -version = "3.17.1" +version = "3.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" +checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" dependencies = [ - "cfg-if", "fastrand", - "getrandom 0.3.1", + "getrandom 0.3.2", "once_cell", "rustix", "windows-sys", @@ -2958,9 +2972,9 @@ checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" [[package]] name = "testing" -version = "8.0.0" +version = "9.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d32ddc0e2ebd072cbffe7424087267da990708a5bc3ae29e075904468450275" +checksum = "e32a1c95775a4077dbfc66d9d6e33576c142bd9bff457289d124037a79f72786" dependencies = [ "ansi_term", "cargo_metadata 0.18.1", @@ -2996,12 +3010,12 @@ dependencies = [ [[package]] name = "textwrap" -version = "0.16.1" +version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" +checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" dependencies = [ "unicode-linebreak", - "unicode-width", + "unicode-width 0.2.0", ] [[package]] @@ -3015,11 +3029,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.11" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" dependencies = [ - "thiserror-impl 2.0.11", + "thiserror-impl 2.0.12", ] [[package]] @@ -3035,9 +3049,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.11" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ "proc-macro2", "quote", @@ -3066,9 +3080,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.8.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8" +checksum = "09b3661f17e86524eccd4371ab0429194e0d7c008abb45f7a7495b1719463c71" dependencies = [ "tinyvec_macros", ] @@ -3176,9 +3190,9 @@ checksum = "2f322b60f6b9736017344fa0635d64be2f458fbc04eef65f6be22976dd1ffd5b" [[package]] name = "unicode-ident" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "unicode-linebreak" @@ -3192,6 +3206,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "url" version = "2.5.4" @@ -3217,9 +3237,9 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" [[package]] name = "uuid" -version = "1.15.1" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0f540e3240398cce6128b64ba83fdbdd86129c16a3aa1a3a252efd66eb3d587" +checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9" [[package]] name = "valuable" @@ -3229,12 +3249,12 @@ checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" [[package]] name = "vergen" -version = "9.0.4" +version = "9.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0d2f179f8075b805a43a2a21728a46f0cc2921b3c58695b28fa8817e103cd9a" +checksum = "6b2bf58be11fc9414104c6d3a2e464163db5ef74b12296bda593cac37b6e4777" dependencies = [ "anyhow", - "cargo_metadata 0.19.1", + "cargo_metadata 0.19.2", "derive_builder", "regex", "rustversion", @@ -3275,9 +3295,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasi" -version = "0.13.3+wasi-0.2.2" +version = "0.14.2+wasi-0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3" dependencies = [ "wit-bindgen-rt", ] @@ -3373,18 +3393,62 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-core" -version = "0.52.0" +version = "0.61.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980" dependencies = [ - "windows-targets", + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] name = "windows-link" -version = "0.1.0" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38" + +[[package]] +name = "windows-result" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dccfd733ce2b1753b03b6d3c65edf020262ea35e20ccdf3e288043e6dd620e3" +checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97" +dependencies = [ + "windows-link", +] [[package]] name = "windows-sys" @@ -3470,9 +3534,9 @@ dependencies = [ [[package]] name = "wit-bindgen-rt" -version = "0.33.0" +version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" dependencies = [ "bitflags", ] @@ -3498,17 +3562,6 @@ dependencies = [ "tap", ] -[[package]] -name = "yaml-rust2" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "232bdb534d65520716bef0bbb205ff8f2db72d807b19c0bc3020853b92a0cd4b" -dependencies = [ - "arraydeque", - "encoding_rs", - "hashlink", -] - [[package]] name = "yansi" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index f203e86..b7e4d6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,17 +9,15 @@ license = "Apache-2.0" # crate-type = ["cdylib"] # This was originally set as above, but commented to run tests in tests folder. - [profile.release] lto = true [dependencies] -nodejs-semver = "4.1.0" -swc = "16.1.0" -swc_core = { version = "16.2.3", features = ["ecma_plugin_transform","ecma_quote"] } -swc_ecma_parser = "10.0.0" -swc_ecma_visit = { version = "8.0.0", features = ["path"] } -yaml-rust2 = "0.10.0" +nodejs-semver = "4" +swc = "21" +swc_core = { version = "22", features = ["ecma_plugin_transform","ecma_quote"] } +swc_ecma_parser = "11" +swc_ecma_visit = { version = "8", features = ["path"] } [dev-dependencies] -assert_cmd = "2.0.16" +assert_cmd = "2" diff --git a/src/config.rs b/src/config.rs index 3cd5799..38488fd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,30 +2,9 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ -use std::path::PathBuf; - -use nodejs_semver::{Range, Version}; - -use crate::error::OrchestrionError; use crate::function_query::FunctionQuery; - -use yaml_rust2::{Yaml, YamlLoader}; - -macro_rules! get_str { - ($property:expr, $name:expr) => { - $property[$name] - .as_str() - .ok_or(format!("Invalid config: '{}' must be a string", $name))? - }; -} - -macro_rules! get_arr { - ($property:expr, $name:expr) => { - $property[$name] - .as_vec() - .ok_or(format!("Invalid config: '{}' must be a array", $name))? - }; -} +use nodejs_semver::{Range, Version}; +use std::path::PathBuf; #[derive(Clone, Debug)] pub enum InstrumentationOperator { @@ -36,6 +15,7 @@ pub enum InstrumentationOperator { } impl InstrumentationOperator { + #[must_use] pub fn as_str(&self) -> &'static str { match self { InstrumentationOperator::Callback => "traceCallback", @@ -44,19 +24,9 @@ impl InstrumentationOperator { InstrumentationOperator::Async => "traceAsync", } } - - pub fn from_str(s: &str) -> Option { - match s { - "traceCallback" => Some(InstrumentationOperator::Callback), - "tracePromise" => Some(InstrumentationOperator::Promise), - "traceSync" => Some(InstrumentationOperator::Sync), - "traceAsync" => Some(InstrumentationOperator::Async), - _ => None, - } - } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct InstrumentationConfig { pub module_name: String, pub version_range: Range, @@ -66,85 +36,43 @@ pub struct InstrumentationConfig { pub channel_name: String, } +#[derive(Debug, Clone)] pub struct Config { pub instrumentations: Vec, pub dc_module: String, } impl Config { - pub fn from_yaml_data(yaml_str: &str) -> Result { - let docs = YamlLoader::load_from_str(yaml_str)?; - let doc = &docs[0]; - - let version = doc["version"] - .as_i64() - .ok_or("Invalid config: 'version' must be a number")?; - if version != 1 { - return Err("Invalid config version".into()); + #[must_use] + pub fn new(instrumentations: Vec, dc_module: String) -> Self { + Self { + instrumentations, + dc_module, } - - let dc_module = doc["dc_module"].as_str().unwrap_or("diagnostics_channel"); - - let configs = InstrumentationConfig::from_yaml(doc)?; - - Ok(Config { - instrumentations: configs, - dc_module: dc_module.to_string(), - }) } -} - -impl InstrumentationConfig { - pub fn from_yaml(doc: &Yaml) -> Result, OrchestrionError> { - let instrumentations = get_arr!(doc, "instrumentations"); - let mut configs = Vec::new(); - for instr in instrumentations { - instr - .as_hash() - .ok_or("Invalid config: 'instrumentations' must be a array of objects")?; - configs.push(instr.try_into()?); + #[must_use] + pub fn new_single_with_default_dc_module(instrumentation: InstrumentationConfig) -> Self { + Self { + instrumentations: vec![instrumentation], + dc_module: "diagnostics_channel".to_string(), } - - Ok(configs) } +} +impl InstrumentationConfig { + #[must_use] pub fn matches(&self, module_name: &str, version: &str, file_path: &PathBuf) -> bool { let version: Version = match version.parse() { Ok(v) => v, - Err(_) => return false, + Err(e) => { + println!("Failed to parse version {version}: {e}"); + return false; + } }; + self.module_name == module_name && version.satisfies(&self.version_range) && self.file_path == *file_path } } - -impl TryFrom<&Yaml> for InstrumentationConfig { - type Error = OrchestrionError; - - fn try_from(instr: &Yaml) -> Result { - let module_name = get_str!(instr, "module_name"); - let version_range = get_str!(instr, "version_range"); - let version_range: Range = version_range - .parse() - .map_err(|_| format!("Invalid version range: {version_range}"))?; - let file_path = PathBuf::from(get_str!(instr, "file_path")); - if instr["function_query"].as_hash().is_none() { - return Err("Invalid config: 'function_query' must be a object".into()); - } - let function_query = (&instr["function_query"]).try_into()?; - let operator = InstrumentationOperator::from_str(get_str!(instr, "operator")) - .unwrap_or(InstrumentationOperator::Sync); - let channel_name = get_str!(instr, "channel_name"); - - Ok(InstrumentationConfig { - module_name: module_name.to_string(), - version_range, - file_path, - function_query, - operator, - channel_name: channel_name.to_string(), - }) - } -} diff --git a/src/error.rs b/src/error.rs index 4ed1c20..70fb8b6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,9 +6,7 @@ use std::fmt::{self, Display, Formatter}; #[derive(Debug)] pub enum OrchestrionError { - InvalidVersionRange(String), IoError(std::io::Error), - YamlParseError(yaml_rust2::ScanError), StrError(String), } @@ -18,12 +16,6 @@ impl From for OrchestrionError { } } -impl From for OrchestrionError { - fn from(e: yaml_rust2::ScanError) -> Self { - OrchestrionError::YamlParseError(e) - } -} - impl From for OrchestrionError { fn from(s: String) -> Self { OrchestrionError::StrError(s) @@ -39,9 +31,7 @@ impl From<&str> for OrchestrionError { impl Display for OrchestrionError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { - OrchestrionError::InvalidVersionRange(s) => write!(f, "Invalid version range: {s}"), OrchestrionError::IoError(e) => write!(f, "IO error: {e}"), - OrchestrionError::YamlParseError(e) => write!(f, "YAML parse error: {e}"), OrchestrionError::StrError(s) => write!(f, "String error: {s}"), } } diff --git a/src/function_query.rs b/src/function_query.rs index 5890428..ee7260c 100644 --- a/src/function_query.rs +++ b/src/function_query.rs @@ -2,37 +2,16 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ -use crate::error::OrchestrionError; use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; -use yaml_rust2::Yaml; -macro_rules! get_str { - ($property:expr, $name:expr) => { - $property[$name] - .as_str() - .ok_or(format!("Invalid config: '{}' must be a string", $name))? - }; -} - -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum FunctionType { FunctionDeclaration, FunctionExpression, Method, } -impl FunctionType { - pub fn from_str(s: &str) -> Option { - match s { - "decl" => Some(FunctionType::FunctionDeclaration), - "expr" => Some(FunctionType::FunctionExpression), - "method" => Some(FunctionType::Method), - _ => None, - } - } -} - -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum FunctionKind { Sync, Async, @@ -41,14 +20,17 @@ pub enum FunctionKind { } impl FunctionKind { + #[must_use] pub fn is_async(&self) -> bool { matches!(self, FunctionKind::Async | FunctionKind::AsyncGenerator) } + #[must_use] pub fn is_generator(&self) -> bool { matches!(self, FunctionKind::Generator | FunctionKind::AsyncGenerator) } + #[must_use] pub fn matches(&self, func: &Function) -> bool { match self { FunctionKind::Sync => !func.is_async && !func.is_generator, @@ -57,19 +39,9 @@ impl FunctionKind { FunctionKind::AsyncGenerator => func.is_async && func.is_generator, } } - - pub fn from_str(s: &str) -> Option { - match s { - "sync" => Some(FunctionKind::Sync), - "async" => Some(FunctionKind::Async), - "generator" => Some(FunctionKind::Generator), - "async generator" => Some(FunctionKind::AsyncGenerator), - _ => None, - } - } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct FunctionQuery { pub name: String, pub class: Option, @@ -113,29 +85,3 @@ impl FunctionQuery { self.maybe_increment_count(matches_except_count, count) } } - -impl TryFrom<&Yaml> for FunctionQuery { - type Error = OrchestrionError; - - fn try_from(query: &Yaml) -> Result { - let typ = get_str!(query, "type"); - let kind = get_str!(query, "kind"); - let name = get_str!(query, "name"); - let class = query["class"] - .as_str() - .map(std::string::ToString::to_string); - let index: usize = query["index"].as_i64().unwrap_or(0).try_into().unwrap_or(0); - - Ok(FunctionQuery { - name: name.to_string(), - class, - typ: FunctionType::from_str(typ).ok_or(format!( - "Invalid config: 'type' must be one of 'decl', 'expr', or 'method', got '{typ}'" - ))?, - kind: FunctionKind::from_str(kind).ok_or(format!( - "Invalid config: 'kind' must be one of 'sync', 'async', 'generator', or 'async generator', got '{kind}'" - ))?, - index, - }) - } -} diff --git a/src/instrumentation.rs b/src/instrumentation.rs index b02523a..ed1bf2d 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -27,6 +27,7 @@ macro_rules! ident { /// /// [`Instrumentation`]: Instrumentation /// [`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html +#[derive(Debug)] pub struct Instrumentation { config: InstrumentationConfig, count: usize, @@ -34,7 +35,8 @@ pub struct Instrumentation { } impl Instrumentation { - pub(crate) fn new(config: InstrumentationConfig) -> Self { + #[must_use] + pub fn new(config: InstrumentationConfig) -> Self { Self { config, count: 0, @@ -230,7 +232,7 @@ impl Instrumentation { .function_query .class .as_ref() - .map_or(true, |class| node.ident.sym.as_ref() == class); + .is_none_or(|class| node.ident.sym.as_ref() == class); true } diff --git a/src/lib.rs b/src/lib.rs index 2d7d7c4..d027f03 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,27 +19,27 @@ * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ use std::path::PathBuf; -use std::str::FromStr; - -use swc_core::ecma::{ - ast::{ - AssignExpr, ClassDecl, ClassMethod, Constructor, FnDecl, MethodProp, Module, Script, Str, - VarDecl, +use swc_core::{ + ecma::{ + ast::{ + AssignExpr, ClassDecl, ClassMethod, Constructor, FnDecl, MethodProp, Module, Script, + Str, VarDecl, + }, + visit::{VisitMut, VisitMutWith}, }, - visit::{VisitMut, VisitMutWith}, + quote, }; -use swc_core::quote; mod error; -use error::OrchestrionError; mod config; -use config::Config; +pub use config::*; mod instrumentation; pub use instrumentation::*; mod function_query; +pub use function_query::*; /// This struct is responsible for managing all instrumentations. It's created from a YAML string /// via the [`FromStr`] trait. See tests for examples, but by-and-large this just means you can @@ -52,7 +52,7 @@ pub struct Instrumentor { } impl Instrumentor { - fn new(config: Config) -> Self { + pub fn new(config: Config) -> Self { Self { instrumentations: config .instrumentations @@ -75,18 +75,12 @@ impl Instrumentor { .instrumentations .iter_mut() .filter(|instr| instr.matches(module_name, version, file_path)); - InstrumentationVisitor::new(instrumentations, self.dc_module.as_ref()) - } -} - -impl FromStr for Instrumentor { - type Err = OrchestrionError; - fn from_str(s: &str) -> Result { - Config::from_yaml_data(s).map(Self::new) + InstrumentationVisitor::new(instrumentations, self.dc_module.as_ref()) } } +#[derive(Debug)] pub struct InstrumentationVisitor<'a> { instrumentations: Vec<&'a mut Instrumentation>, dc_module: &'a str, diff --git a/tests/class_method_cjs/instrumentations.yml b/tests/class_method_cjs/instrumentations.yml deleted file mode 100644 index 2b8591c..0000000 --- a/tests/class_method_cjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - class: Undici - name: fetch - type: method - kind: async - operator: tracePromise - channel_name: Undici_fetch diff --git a/tests/class_method_cjs/mod.rs b/tests/class_method_cjs/mod.rs new file mode 100644 index 0000000..6769d2d --- /dev/null +++ b/tests/class_method_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn class_method_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/class_method_cjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "fetch".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "Undici_fetch".to_string(), + }), + ); +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 09137d3..3b09324 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -83,41 +83,30 @@ fn transpile( .unwrap() } -pub fn init_instrumentor(test_name: &str) -> Instrumentor { - let mut file = get_dir(test_name); - file.push("instrumentations.yml"); - let yaml = std::fs::read_to_string(file).unwrap(); - yaml.parse().unwrap() -} +pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) { + let test_file = PathBuf::from(test_file); + let test_dir = test_file.parent().expect("Couldn't find test directory"); -fn get_dir(test_name: &str) -> PathBuf { - let mut dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - dir.push("tests"); - dir.push(test_name); - dir -} + let file_path = test_dir.join("index.mjs"); + let mut instrumentor = Instrumentor::new(config); + let mut instrumentations = + instrumentor.get_matching_instrumentations("undici", "0.0.1", &file_path); -pub fn transpile_and_test( - test_name: &str, - mjs: bool, - instrumentations: &mut InstrumentationVisitor, -) { - let dir = get_dir(test_name); let extension = if mjs { "mjs" } else { "js" }; - let instrumentable = dir.join(format!("mod.{}", extension)); + let instrumentable = test_dir.join(format!("mod.{}", extension)); let mut file = std::fs::File::open(&instrumentable).unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); - let result = transpile(&contents, IsModule::Bool(mjs), instrumentations); + let result = transpile(&contents, IsModule::Bool(mjs), &mut instrumentations); - let instrumented_file = dir.join(format!("instrumented.{}", extension)); + let instrumented_file = test_dir.join(format!("instrumented.{}", extension)); let mut file = std::fs::File::create(&instrumented_file).unwrap(); file.write_all(result.as_bytes()).unwrap(); - let test_file = dir.join(format!("test.{}", extension)); + let test_file = format!("test.{}", extension); Command::new("node") - .current_dir(dir) + .current_dir(test_dir) .stdout(std::process::Stdio::inherit()) .stderr(std::process::Stdio::inherit()) .arg(&test_file) diff --git a/tests/constructor_cjs/instrumentations.yml b/tests/constructor_cjs/instrumentations.yml deleted file mode 100644 index 5687267..0000000 --- a/tests/constructor_cjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: constructor - type: method - kind: sync - class: Undici - operator: traceSync - channel_name: Undici_constructor diff --git a/tests/constructor_cjs/mod.rs b/tests/constructor_cjs/mod.rs new file mode 100644 index 0000000..a742def --- /dev/null +++ b/tests/constructor_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn constructor_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/constructor_cjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "constructor".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Sync, + index: 0, + }, + operator: InstrumentationOperator::Sync, + channel_name: "Undici_constructor".to_string(), + }), + ); +} diff --git a/tests/constructor_mjs/instrumentations.yml b/tests/constructor_mjs/instrumentations.yml deleted file mode 100644 index 8ffcea7..0000000 --- a/tests/constructor_mjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: constructor - type: method - kind: sync - class: Undici - operator: traceSync - channel_name: Undici_constructor \ No newline at end of file diff --git a/tests/constructor_mjs/mod.rs b/tests/constructor_mjs/mod.rs new file mode 100644 index 0000000..adccf36 --- /dev/null +++ b/tests/constructor_mjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn constructor_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/constructor_mjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "constructor".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Sync, + index: 0, + }, + operator: InstrumentationOperator::Sync, + channel_name: "Undici_constructor".to_string(), + }), + ); +} diff --git a/tests/decl_cjs/instrumentations.yml b/tests/decl_cjs/instrumentations.yml deleted file mode 100644 index 6a9beac..0000000 --- a/tests/decl_cjs/instrumentations.yml +++ /dev/null @@ -1,13 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: decl - kind: async - index: 0 - operator: tracePromise - channel_name: fetch_decl - diff --git a/tests/decl_cjs/mod.rs b/tests/decl_cjs/mod.rs new file mode 100644 index 0000000..f338e2b --- /dev/null +++ b/tests/decl_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn decl_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/decl_cjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::FunctionDeclaration, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "fetch_decl".to_string(), + }), + ); +} diff --git a/tests/decl_mjs/instrumentations.yml b/tests/decl_mjs/instrumentations.yml deleted file mode 100644 index c24a0e2..0000000 --- a/tests/decl_mjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: decl - kind: async - index: 0 - operator: tracePromise - channel_name: fetch_decl diff --git a/tests/decl_mjs/mod.rs b/tests/decl_mjs/mod.rs new file mode 100644 index 0000000..12472ca --- /dev/null +++ b/tests/decl_mjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn decl_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/decl_mjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::FunctionDeclaration, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "fetch_decl".to_string(), + }), + ); +} diff --git a/tests/expr_cjs/instrumentations.yml b/tests/expr_cjs/instrumentations.yml deleted file mode 100644 index 3d2b837..0000000 --- a/tests/expr_cjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: expr - kind: async - index: 0 - operator: tracePromise - channel_name: fetch_expr diff --git a/tests/expr_cjs/mod.rs b/tests/expr_cjs/mod.rs new file mode 100644 index 0000000..3cbbd5c --- /dev/null +++ b/tests/expr_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn expr_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/expr_cjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::FunctionExpression, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "fetch_expr".to_string(), + }), + ); +} diff --git a/tests/expr_mjs/instrumentations.yml b/tests/expr_mjs/instrumentations.yml deleted file mode 100644 index 3d2b837..0000000 --- a/tests/expr_mjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: expr - kind: async - index: 0 - operator: tracePromise - channel_name: fetch_expr diff --git a/tests/expr_mjs/mod.rs b/tests/expr_mjs/mod.rs new file mode 100644 index 0000000..7eea046 --- /dev/null +++ b/tests/expr_mjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn expr_mjs() { + transpile_and_test( + file!(), + true, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/expr_mjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::FunctionExpression, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "fetch_expr".to_string(), + }), + ); +} diff --git a/tests/index_cjs/instrumentations.yml b/tests/index_cjs/instrumentations.yml deleted file mode 100644 index 725dda5..0000000 --- a/tests/index_cjs/instrumentations.yml +++ /dev/null @@ -1,13 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - class: Undici - name: fetch - type: method - kind: async - index: 2 - operator: tracePromise - channel_name: Undici_fetch diff --git a/tests/index_cjs/mod.rs b/tests/index_cjs/mod.rs new file mode 100644 index 0000000..d2405fb --- /dev/null +++ b/tests/index_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn index_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/index_cjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "fetch".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Async, + index: 2, + }, + operator: InstrumentationOperator::Promise, + channel_name: "Undici_fetch".to_string(), + }), + ); +} diff --git a/tests/instrumentor_test.rs b/tests/instrumentor_test.rs index 0dc02e4..ec5b9fd 100644 --- a/tests/instrumentor_test.rs +++ b/tests/instrumentor_test.rs @@ -2,50 +2,18 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc. **/ -use std::path::PathBuf; - mod common; -use common::{init_instrumentor, transpile_and_test}; - -macro_rules! make_test { - ($name:ident, $mjs:literal) => { - #[test] - fn $name() { - let file_path = PathBuf::from("index.mjs"); - let mut instrumentor = init_instrumentor(stringify!($name)); - let mut instrumentations = - instrumentor.get_matching_instrumentations("undici", "0.0.1", &file_path); - - transpile_and_test(stringify!($name), $mjs, &mut instrumentations); - - // It has to work twice, since we might use the same instrumentor on multiple files - transpile_and_test(stringify!($name), $mjs, &mut instrumentations); - } - }; -} - -make_test!(decl_mjs, true); - -make_test!(decl_cjs, false); - -make_test!(expr_mjs, true); - -make_test!(expr_cjs, false); - -make_test!(class_method_cjs, false); - -make_test!(multiple_class_method_cjs, false); - -make_test!(object_method_cjs, false); - -make_test!(constructor_cjs, false); - -make_test!(constructor_mjs, true); - -make_test!(polyfill_mjs, true); - -make_test!(polyfill_cjs, false); - -make_test!(index_cjs, false); -make_test!(no_index_cjs, false); +mod class_method_cjs; +mod constructor_cjs; +mod constructor_mjs; +mod decl_cjs; +mod decl_mjs; +mod expr_cjs; +mod expr_mjs; +mod index_cjs; +mod multiple_class_method_cjs; +mod multiple_load_cjs; +mod object_method_cjs; +mod polyfill_cjs; +mod polyfill_mjs; diff --git a/tests/multiple_class_method_cjs/instrumentations.yml b/tests/multiple_class_method_cjs/instrumentations.yml deleted file mode 100644 index c68c482..0000000 --- a/tests/multiple_class_method_cjs/instrumentations.yml +++ /dev/null @@ -1,22 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - class: Undici - name: fetch1 - type: method - kind: async - operator: tracePromise - channel_name: Undici_fetch1 - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - class: Undici - name: fetch2 - type: method - kind: async - operator: tracePromise - channel_name: Undici_fetch2 diff --git a/tests/multiple_class_method_cjs/mod.rs b/tests/multiple_class_method_cjs/mod.rs new file mode 100644 index 0000000..6d41eef --- /dev/null +++ b/tests/multiple_class_method_cjs/mod.rs @@ -0,0 +1,45 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn multiple_class_method_cjs() { + transpile_and_test( + file!(), + false, + Config::new( + vec![ + InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/multiple_class_method_cjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "fetch1".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "Undici_fetch1".to_string(), + }, + InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/multiple_class_method_cjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "fetch2".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "Undici_fetch2".to_string(), + }, + ], + "diagnostics_channel".to_string(), + ), + ); +} diff --git a/tests/multiple_load_cjs/instrumentations.yml b/tests/multiple_load_cjs/instrumentations.yml deleted file mode 100644 index 2b8591c..0000000 --- a/tests/multiple_load_cjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - class: Undici - name: fetch - type: method - kind: async - operator: tracePromise - channel_name: Undici_fetch diff --git a/tests/multiple_load_cjs/mod.rs b/tests/multiple_load_cjs/mod.rs new file mode 100644 index 0000000..6c00205 --- /dev/null +++ b/tests/multiple_load_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn multiple_load_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/multiple_load_cjs/index.mjs"), + function_query: FunctionQuery { + class: Some("Undici".to_string()), + name: "fetch".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "Undici_fetch".to_string(), + }), + ); +} diff --git a/tests/no_index_cjs/instrumentations.yml b/tests/no_index_cjs/instrumentations.yml deleted file mode 100644 index acabc37..0000000 --- a/tests/no_index_cjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: decl - kind: async - # intentionally omitting index - operator: tracePromise - channel_name: fetch_no_index \ No newline at end of file diff --git a/tests/no_index_cjs/mod.js b/tests/no_index_cjs/mod.js deleted file mode 100644 index c977331..0000000 --- a/tests/no_index_cjs/mod.js +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. - * This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc. - **/ -async function fetch(url) { - return 42; -} - -module.exports = { fetch }; diff --git a/tests/no_index_cjs/test.js b/tests/no_index_cjs/test.js deleted file mode 100644 index 8c88c1c..0000000 --- a/tests/no_index_cjs/test.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. - * This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2025 Datadog, Inc. - **/ -const { fetch } = require('./instrumented.js'); -const { assert, getContext } = require('../common/preamble.js'); -const context = getContext('orchestrion:undici:fetch_no_index'); -(async () => { - const result = await fetch('https://example.com'); - assert.strictEqual(result, 42); - assert.deepStrictEqual(context, { - start: true, - end: true, - asyncStart: 42, - asyncEnd: 42 - }); -})(); diff --git a/tests/object_method_cjs/instrumentations.yml b/tests/object_method_cjs/instrumentations.yml deleted file mode 100644 index 8ac53ed..0000000 --- a/tests/object_method_cjs/instrumentations.yml +++ /dev/null @@ -1,12 +0,0 @@ -version: 1 -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: method - kind: async - index: 0 - operator: tracePromise - channel_name: Undici_fetch diff --git a/tests/object_method_cjs/mod.rs b/tests/object_method_cjs/mod.rs new file mode 100644 index 0000000..f3f0949 --- /dev/null +++ b/tests/object_method_cjs/mod.rs @@ -0,0 +1,26 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn object_method_cjs() { + transpile_and_test( + file!(), + false, + Config::new_single_with_default_dc_module(InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/object_method_cjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::Method, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "Undici_fetch".to_string(), + }), + ); +} diff --git a/tests/polyfill_cjs/instrumentations.yml b/tests/polyfill_cjs/instrumentations.yml deleted file mode 100644 index a17f90b..0000000 --- a/tests/polyfill_cjs/instrumentations.yml +++ /dev/null @@ -1,13 +0,0 @@ -version: 1 -dc_module: ./polyfill.js -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: decl - kind: async - index: 0 - operator: tracePromise - channel_name: fetch_decl diff --git a/tests/polyfill_cjs/mod.rs b/tests/polyfill_cjs/mod.rs new file mode 100644 index 0000000..92f0ec5 --- /dev/null +++ b/tests/polyfill_cjs/mod.rs @@ -0,0 +1,29 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn polyfill_cjs() { + transpile_and_test( + file!(), + false, + Config::new( + vec![InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/polyfill_cjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::FunctionDeclaration, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "fetch_decl".to_string(), + }], + "./polyfill.js".to_string(), + ), + ); +} diff --git a/tests/polyfill_mjs/instrumentations.yml b/tests/polyfill_mjs/instrumentations.yml deleted file mode 100644 index a17f90b..0000000 --- a/tests/polyfill_mjs/instrumentations.yml +++ /dev/null @@ -1,13 +0,0 @@ -version: 1 -dc_module: ./polyfill.js -instrumentations: - - module_name: undici - version_range: ">=0.0.1" - file_path: index.mjs - function_query: - name: fetch - type: decl - kind: async - index: 0 - operator: tracePromise - channel_name: fetch_decl diff --git a/tests/polyfill_mjs/mod.rs b/tests/polyfill_mjs/mod.rs new file mode 100644 index 0000000..dbe0337 --- /dev/null +++ b/tests/polyfill_mjs/mod.rs @@ -0,0 +1,29 @@ +use crate::common::*; +use nodejs_semver::Range; +use orchestrion_js::*; +use std::path::PathBuf; + +#[test] +fn polyfill_mjs() { + transpile_and_test( + file!(), + true, + Config::new( + vec![InstrumentationConfig { + module_name: "undici".to_string(), + version_range: Range::parse(">=0.0.1").unwrap(), + file_path: PathBuf::from("tests/polyfill_mjs/index.mjs"), + function_query: FunctionQuery { + class: None, + name: "fetch".to_string(), + typ: FunctionType::FunctionDeclaration, + kind: FunctionKind::Async, + index: 0, + }, + operator: InstrumentationOperator::Promise, + channel_name: "fetch_decl".to_string(), + }], + "./polyfill.js".to_string(), + ), + ); +} From 7545db36401227ce0e5e89ae272657b987a9da9e Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 24 Apr 2025 11:58:12 +0200 Subject: [PATCH 02/12] update LICENSE-3rdparty.csv --- LICENSE-3rdparty.csv | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/LICENSE-3rdparty.csv b/LICENSE-3rdparty.csv index 8ba88c1..0d0cc60 100644 --- a/LICENSE-3rdparty.csv +++ b/LICENSE-3rdparty.csv @@ -7,11 +7,10 @@ android-tzdata,https://github.com/RumovZ/android-tzdata,MIT OR Apache-2.0,RumovZ android_system_properties,https://github.com/nical/android_system_properties,MIT OR Apache-2.0,Nicolas Silva ansi_term,https://github.com/ogham/rust-ansi-term,MIT,"ogham@bsago.me, Ryan Scheel (Havvy) , Josh Triplett " anyhow,https://github.com/dtolnay/anyhow,MIT OR Apache-2.0,David Tolnay -arraydeque,https://github.com/andylokandy/arraydeque,MIT OR Apache-2.0,andylokandy arrayvec,https://github.com/bluss/arrayvec,MIT OR Apache-2.0,bluss ascii,https://github.com/tomprogrammer/rust-ascii,Apache-2.0 OR MIT,"Thomas Bahn , Torbjørn Birch Moltu , Simon Sapin " auto_impl,https://github.com/auto-impl-rs/auto_impl,MIT OR Apache-2.0,"Ashley Mannix , Lukas Kalbertodt " -base64,https://github.com/marshallpierce/rust-base64,MIT OR Apache-2.0,"Alice Maz , Marshall Pierce " +base64,https://github.com/marshallpierce/rust-base64,MIT OR Apache-2.0,Marshall Pierce base64-simd,https://github.com/Nugine/simd,MIT,The base64-simd Authors bitflags,https://github.com/bitflags/bitflags,MIT OR Apache-2.0,The Rust Project Developers bitvec,https://github.com/bitvecto-rs/bitvec,MIT,The bitvec Authors @@ -39,18 +38,15 @@ difference,https://github.com/johannhof/difference.rs,MIT,Johann Hofmann either,https://github.com/rayon-rs/either,MIT OR Apache-2.0,bluss -encoding_rs,https://github.com/hsivonen/encoding_rs,(Apache-2.0 OR MIT) AND BSD-3-Clause,Henri Sivonen equivalent,https://github.com/indexmap-rs/equivalent,Apache-2.0 OR MIT,The equivalent Authors -errno,https://github.com/lambda-fairy/rust-errno,MIT OR Apache-2.0,Chris Wong +errno,https://github.com/lambda-fairy/rust-errno,MIT OR Apache-2.0,"Chris Wong , Dan Gohman " fastrand,https://github.com/smol-rs/fastrand,Apache-2.0 OR MIT,Stjepan Glavina fixedbitset,https://github.com/petgraph/fixedbitset,MIT OR Apache-2.0,bluss -foldhash,https://github.com/orlp/foldhash,Zlib,Orson Peters funty,https://github.com/myrrlyn/funty,MIT,myrrlyn generic-array,https://github.com/fizyk20/generic-array,MIT,"Bartłomiej Kamiński , Aaron Trent " getrandom,https://github.com/rust-random/getrandom,MIT OR Apache-2.0,The Rand Project Developers glob,https://github.com/rust-lang/glob,MIT OR Apache-2.0,The Rust Project Developers hashbrown,https://github.com/rust-lang/hashbrown,MIT OR Apache-2.0,Amanieu d'Antras -hashlink,https://github.com/kyren/hashlink,MIT OR Apache-2.0,kyren heck,https://github.com/withoutboats/heck,MIT OR Apache-2.0,The heck Authors hermit-abi,https://github.com/hermit-os/hermit-rs,MIT OR Apache-2.0,Stefan Lankes hex,https://github.com/KokaKiwi/rust-hex,MIT OR Apache-2.0,KokaKiwi @@ -98,6 +94,8 @@ once_cell,https://github.com/matklad/once_cell,MIT OR Apache-2.0,Aleksey Kladov outref,https://github.com/Nugine/outref,MIT,The outref Authors overload,https://github.com/danaugrs/overload,MIT,Daniel Salvadori owo-colors,https://github.com/owo-colors/owo-colors,MIT,jam1garner <8260240+jam1garner@users.noreply.github.com> +par-core,https://github.com/dudykr/ddbase,Apache-2.0,강동윤 +par-iter,https://github.com/dudykr/ddbase,Apache-2.0,The par-iter Authors parking_lot,https://github.com/Amanieu/parking_lot,MIT OR Apache-2.0,Amanieu d'Antras path-clean,https://github.com/danreeves/path-clean,MIT OR Apache-2.0,Dan Reeves pathdiff,https://github.com/Manishearth/pathdiff,MIT OR Apache-2.0,Manish Goregaokar @@ -109,6 +107,7 @@ proc-macro2,https://github.com/dtolnay/proc-macro2,MIT OR Apache-2.0,"David Toln psm,https://github.com/rust-lang/stacker,MIT OR Apache-2.0,Simonas Kazlauskas ptr_meta,https://github.com/rkyv/ptr_meta,MIT,David Koloski quote,https://github.com/dtolnay/quote,MIT OR Apache-2.0,David Tolnay +r-efi,https://github.com/r-efi/r-efi,MIT OR Apache-2.0 OR LGPL-2.1-or-later,The r-efi Authors radium,https://github.com/bitvecto-rs/radium,MIT,"Nika Layzell , myrrlyn " radix_fmt,https://gitlab.com/Boiethios/radix_fmt_rs,Apache-2.0,Félix rancor,https://github.com/rkyv/rancor,MIT,David Koloski @@ -192,7 +191,11 @@ wasm-bindgen-shared,https://github.com/rustwasm/wasm-bindgen/tree/master/crates/ winapi,https://github.com/retep998/winapi-rs,MIT OR Apache-2.0,Peter Atashian winapi-util,https://github.com/BurntSushi/winapi-util,Unlicense OR MIT,Andrew Gallant windows-core,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft +windows-implement,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft +windows-interface,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft windows-link,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft +windows-result,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft +windows-strings,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft windows-sys,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft windows-targets,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft windows_aarch64_gnullvm,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft @@ -204,11 +207,10 @@ windows_x86_64_gnu,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Mic windows_x86_64_gnullvm,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft windows_x86_64_msvc,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft winnow,https://github.com/winnow-rs/winnow,MIT,The winnow Authors -wit-bindgen-rt,https://github.com/bytecodealliance/wasi-rs,Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT,The wit-bindgen-rt Authors +wit-bindgen-rt,https://github.com/bytecodealliance/wit-bindgen,Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT,The wit-bindgen-rt Authors write16,https://github.com/hsivonen/write16,Apache-2.0 OR MIT,The write16 Authors writeable,https://github.com/unicode-org/icu4x,Unicode-3.0,The ICU4X Project Developers wyz,https://github.com/myrrlyn/wyz,MIT,myrrlyn -yaml-rust2,https://github.com/Ethiraric/yaml-rust2,MIT OR Apache-2.0,"Yuheng Chen , Ethiraric , David Aguilar " yansi,https://github.com/SergioBenitez/yansi,MIT OR Apache-2.0,Sergio Benitez yoke,https://github.com/unicode-org/icu4x,Unicode-3.0,Manish Goregaokar yoke-derive,https://github.com/unicode-org/icu4x,Unicode-3.0,Manish Goregaokar From edfb9e4909c5c70a29208b976d6e68405d07910f Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 26 Apr 2025 20:20:38 +0200 Subject: [PATCH 03/12] feat: Combine config enums --- src/config.rs | 84 ++++++------ src/function_query.rs | 170 +++++++++++++++++++++---- src/instrumentation.rs | 15 +-- tests/class_method_cjs/mod.rs | 21 +-- tests/constructor_cjs/mod.rs | 21 +-- tests/constructor_mjs/mod.rs | 21 +-- tests/decl_cjs/mod.rs | 21 +-- tests/decl_mjs/mod.rs | 21 +-- tests/expr_cjs/mod.rs | 21 +-- tests/expr_mjs/mod.rs | 21 +-- tests/index_cjs/mod.rs | 20 +-- tests/multiple_class_method_cjs/mod.rs | 52 +++----- tests/multiple_load_cjs/mod.rs | 21 +-- tests/object_method_cjs/mod.rs | 21 +-- tests/polyfill_cjs/mod.rs | 23 +--- tests/polyfill_mjs/mod.rs | 23 +--- 16 files changed, 282 insertions(+), 294 deletions(-) diff --git a/src/config.rs b/src/config.rs index 38488fd..de8085c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,39 +3,62 @@ * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ use crate::function_query::FunctionQuery; -use nodejs_semver::{Range, Version}; +use nodejs_semver::{Range, SemverError, Version}; use std::path::PathBuf; -#[derive(Clone, Debug)] -pub enum InstrumentationOperator { - Callback, - Promise, - Sync, - Async, +#[derive(Debug, Clone)] +pub struct ModuleMatcher { + pub name: String, + pub version_range: Range, + pub file_path: PathBuf, } -impl InstrumentationOperator { +impl ModuleMatcher { + /// Creates a new `ModuleMatcher` instance. + /// # Errors + /// Returns an error if the version range cannot be parsed. + pub fn new(name: &str, version_range: &str, file_path: &str) -> Result { + Ok(Self { + name: name.to_string(), + version_range: Range::parse(version_range)?, + file_path: PathBuf::from(file_path), + }) + } + #[must_use] - pub fn as_str(&self) -> &'static str { - match self { - InstrumentationOperator::Callback => "traceCallback", - InstrumentationOperator::Promise => "tracePromise", - InstrumentationOperator::Sync => "traceSync", - InstrumentationOperator::Async => "traceAsync", - } + pub fn matches(&self, module_name: &str, version: &str, file_path: &PathBuf) -> bool { + let version: Version = match version.parse() { + Ok(v) => v, + Err(e) => { + println!("Failed to parse version {version}: {e}"); + return false; + } + }; + + self.name == module_name + && version.satisfies(&self.version_range) + && self.file_path == *file_path } } #[derive(Debug, Clone)] pub struct InstrumentationConfig { - pub module_name: String, - pub version_range: Range, - pub file_path: PathBuf, + pub module: ModuleMatcher, pub function_query: FunctionQuery, - pub operator: InstrumentationOperator, pub channel_name: String, } +impl InstrumentationConfig { + #[must_use] + pub fn new(module: ModuleMatcher, function_query: FunctionQuery, channel_name: &str) -> Self { + Self { + module, + function_query, + channel_name: channel_name.to_string(), + } + } +} + #[derive(Debug, Clone)] pub struct Config { pub instrumentations: Vec, @@ -44,35 +67,22 @@ pub struct Config { impl Config { #[must_use] - pub fn new(instrumentations: Vec, dc_module: String) -> Self { + pub fn new(instrumentations: Vec, dc_module: Option) -> Self { Self { instrumentations, - dc_module, + dc_module: dc_module.unwrap_or_else(|| "diagnostics_channel".to_string()), } } #[must_use] - pub fn new_single_with_default_dc_module(instrumentation: InstrumentationConfig) -> Self { - Self { - instrumentations: vec![instrumentation], - dc_module: "diagnostics_channel".to_string(), - } + pub fn new_single(instrumentation: InstrumentationConfig) -> Self { + Self::new(vec![instrumentation], None) } } impl InstrumentationConfig { #[must_use] pub fn matches(&self, module_name: &str, version: &str, file_path: &PathBuf) -> bool { - let version: Version = match version.parse() { - Ok(v) => v, - Err(e) => { - println!("Failed to parse version {version}: {e}"); - return false; - } - }; - - self.module_name == module_name - && version.satisfies(&self.version_range) - && self.file_path == *file_path + self.module.matches(module_name, version, file_path) } } diff --git a/src/function_query.rs b/src/function_query.rs index ee7260c..2f7a28a 100644 --- a/src/function_query.rs +++ b/src/function_query.rs @@ -5,7 +5,7 @@ use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; #[derive(Debug, Clone)] -pub enum FunctionType { +pub(crate) enum FunctionType { FunctionDeclaration, FunctionExpression, Method, @@ -15,19 +15,12 @@ pub enum FunctionType { pub enum FunctionKind { Sync, Async, - Generator, - AsyncGenerator, } impl FunctionKind { #[must_use] pub fn is_async(&self) -> bool { - matches!(self, FunctionKind::Async | FunctionKind::AsyncGenerator) - } - - #[must_use] - pub fn is_generator(&self) -> bool { - matches!(self, FunctionKind::Generator | FunctionKind::AsyncGenerator) + matches!(self, FunctionKind::Async) } #[must_use] @@ -35,25 +28,148 @@ impl FunctionKind { match self { FunctionKind::Sync => !func.is_async && !func.is_generator, FunctionKind::Async => func.is_async && !func.is_generator, - FunctionKind::Generator => !func.is_async && func.is_generator, - FunctionKind::AsyncGenerator => func.is_async && func.is_generator, + } + } + + #[must_use] + pub fn tracing_operator(&self) -> &'static str { + match self { + FunctionKind::Sync => "traceSync", + FunctionKind::Async => "tracePromise", } } } #[derive(Debug, Clone)] -pub struct FunctionQuery { - pub name: String, - pub class: Option, - pub typ: FunctionType, - pub kind: FunctionKind, - pub index: usize, +pub enum FunctionQuery { + ClassConstructor { + class_name: String, + index: usize, + }, + ClassMethod { + class_name: String, + method_name: String, + kind: FunctionKind, + index: usize, + }, + ObjectMethod { + method_name: String, + kind: FunctionKind, + index: usize, + }, + FunctionDeclaration { + function_name: String, + kind: FunctionKind, + index: usize, + }, + FunctionExpression { + expression_name: String, + kind: FunctionKind, + index: usize, + }, } impl FunctionQuery { + #[must_use] + pub fn class_constructor(class_name: &str) -> Self { + FunctionQuery::ClassConstructor { + class_name: class_name.to_string(), + index: 0, + } + } + + #[must_use] + pub fn class_method(class_name: &str, method_name: &str, kind: FunctionKind) -> Self { + FunctionQuery::ClassMethod { + class_name: class_name.to_string(), + method_name: method_name.to_string(), + kind, + index: 0, + } + } + + #[must_use] + pub fn object_method(method_name: &str, kind: FunctionKind) -> Self { + FunctionQuery::ObjectMethod { + method_name: method_name.to_string(), + kind, + index: 0, + } + } + + #[must_use] + pub fn function_declaration(function_name: &str, kind: FunctionKind) -> Self { + FunctionQuery::FunctionDeclaration { + function_name: function_name.to_string(), + kind, + index: 0, + } + } + + #[must_use] + pub fn function_expression(expression_name: &str, kind: FunctionKind) -> Self { + FunctionQuery::FunctionExpression { + expression_name: expression_name.to_string(), + kind, + index: 0, + } + } + + pub(crate) fn kind(&self) -> &FunctionKind { + match self { + FunctionQuery::ClassConstructor { .. } => &FunctionKind::Sync, + FunctionQuery::ClassMethod { kind, .. } + | FunctionQuery::ObjectMethod { kind, .. } + | FunctionQuery::FunctionDeclaration { kind, .. } + | FunctionQuery::FunctionExpression { kind, .. } => kind, + } + } + + pub(crate) fn name(&self) -> &str { + match self { + FunctionQuery::ClassConstructor { .. } => "constructor", + FunctionQuery::ClassMethod { method_name, .. } + | FunctionQuery::ObjectMethod { method_name, .. } => method_name, + FunctionQuery::FunctionDeclaration { function_name, .. } => function_name, + FunctionQuery::FunctionExpression { + expression_name, .. + } => expression_name, + } + } + + pub(crate) fn typ(&self) -> FunctionType { + match self { + FunctionQuery::ClassConstructor { .. } + | FunctionQuery::ClassMethod { .. } + | FunctionQuery::ObjectMethod { .. } => FunctionType::Method, + FunctionQuery::FunctionDeclaration { .. } => FunctionType::FunctionDeclaration, + FunctionQuery::FunctionExpression { .. } => FunctionType::FunctionExpression, + } + } + + #[must_use] + pub(crate) fn index(&self) -> usize { + match self { + FunctionQuery::ClassConstructor { index, .. } + | FunctionQuery::ClassMethod { index, .. } + | FunctionQuery::ObjectMethod { index, .. } + | FunctionQuery::FunctionDeclaration { index, .. } + | FunctionQuery::FunctionExpression { index, .. } => *index, + } + } + + #[must_use] + pub(crate) fn class_name(&self) -> Option<&str> { + match self { + FunctionQuery::ClassConstructor { class_name, .. } + | FunctionQuery::ClassMethod { class_name, .. } => Some(class_name), + _ => None, + } + } + fn maybe_increment_count(&self, matches_except_count: bool, count: &mut usize) -> bool { if matches_except_count { - if self.index == *count { + if self.index() == *count { true } else { *count += 1; @@ -65,23 +181,23 @@ impl FunctionQuery { } pub fn matches_decl(&self, func: &FnDecl, count: &mut usize) -> bool { - let matches_except_count = matches!(self.typ, FunctionType::FunctionDeclaration) - && self.kind.matches(&func.function) - && func.ident.sym == self.name; + let matches_except_count = matches!(self.typ(), FunctionType::FunctionDeclaration) + && self.kind().matches(&func.function) + && func.ident.sym == self.name(); self.maybe_increment_count(matches_except_count, count) } pub fn matches_expr(&self, func: &FnExpr, count: &mut usize, name: &str) -> bool { - let matches_except_count = matches!(self.typ, FunctionType::FunctionExpression) - && self.kind.matches(&func.function) - && name == self.name; + let matches_except_count = matches!(self.typ(), FunctionType::FunctionExpression) + && self.kind().matches(&func.function) + && name == self.name(); self.maybe_increment_count(matches_except_count, count) } pub fn matches_method(&self, func: &Function, count: &mut usize, name: &str) -> bool { - let matches_except_count = matches!(self.typ, FunctionType::Method) - && self.kind.matches(func) - && name == self.name; + let matches_except_count = matches!(self.typ(), FunctionType::Method) + && self.kind().matches(func) + && name == self.name(); self.maybe_increment_count(matches_except_count, count) } } diff --git a/src/instrumentation.rs b/src/instrumentation.rs index ed1bf2d..f2d0247 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -53,8 +53,8 @@ impl Instrumentation { ArrowExpr { params: vec![], body: Box::new(body.into()), - is_async: self.config.function_query.kind.is_async(), - is_generator: self.config.function_query.kind.is_generator(), + is_async: self.config.function_query.kind().is_async(), + is_generator: false, type_params: None, return_type: None, span: Span::default(), @@ -68,7 +68,7 @@ impl Instrumentation { span: Span::default(), value: format!( "orchestrion:{}:{}", - self.config.module_name, self.config.channel_name + self.config.module.name, self.config.channel_name ) .into(), raw: None, @@ -99,7 +99,7 @@ impl Instrumentation { let trace_ident = ident!(format!( "tr_ch_apm${}.{}", &self.config.channel_name, - self.config.operator.as_str() + self.config.function_query.kind().tracing_operator() )); body.stmts = vec![ @@ -230,8 +230,7 @@ impl Instrumentation { self.is_correct_class = self .config .function_query - .class - .as_ref() + .class_name() .is_none_or(|class| node.ident.sym.as_ref() == class); true } @@ -261,11 +260,11 @@ impl Instrumentation { } pub fn visit_mut_constructor(&mut self, node: &mut Constructor) -> bool { - if !self.is_correct_class || self.config.function_query.name != "constructor" { + if !self.is_correct_class || self.config.function_query.name() != "constructor" { return false; } - if self.count == self.config.function_query.index && node.body.is_some() { + if self.count == self.config.function_query.index() && node.body.is_some() { if let Some(body) = node.body.as_mut() { self.insert_constructor_tracing(body); } diff --git a/tests/class_method_cjs/mod.rs b/tests/class_method_cjs/mod.rs index 6769d2d..e67beaa 100644 --- a/tests/class_method_cjs/mod.rs +++ b/tests/class_method_cjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn class_method_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/class_method_cjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "fetch".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "Undici_fetch".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/class_method_cjs/index.mjs").unwrap(), + FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async), + "Undici_fetch", + )), ); } diff --git a/tests/constructor_cjs/mod.rs b/tests/constructor_cjs/mod.rs index a742def..a41b7f3 100644 --- a/tests/constructor_cjs/mod.rs +++ b/tests/constructor_cjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn constructor_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/constructor_cjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "constructor".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Sync, - index: 0, - }, - operator: InstrumentationOperator::Sync, - channel_name: "Undici_constructor".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/constructor_cjs/index.mjs").unwrap(), + FunctionQuery::class_constructor("Undici"), + "Undici_constructor", + )), ); } diff --git a/tests/constructor_mjs/mod.rs b/tests/constructor_mjs/mod.rs index adccf36..e618d18 100644 --- a/tests/constructor_mjs/mod.rs +++ b/tests/constructor_mjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn constructor_mjs() { transpile_and_test( file!(), true, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/constructor_mjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "constructor".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Sync, - index: 0, - }, - operator: InstrumentationOperator::Sync, - channel_name: "Undici_constructor".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/constructor_mjs/index.mjs").unwrap(), + FunctionQuery::class_constructor("Undici"), + "Undici_constructor", + )), ); } diff --git a/tests/decl_cjs/mod.rs b/tests/decl_cjs/mod.rs index f338e2b..8a15de3 100644 --- a/tests/decl_cjs/mod.rs +++ b/tests/decl_cjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn decl_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/decl_cjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::FunctionDeclaration, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "fetch_decl".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/decl_cjs/index.mjs").unwrap(), + FunctionQuery::function_declaration("fetch", FunctionKind::Async), + "fetch_decl", + )), ); } diff --git a/tests/decl_mjs/mod.rs b/tests/decl_mjs/mod.rs index 12472ca..1344bd4 100644 --- a/tests/decl_mjs/mod.rs +++ b/tests/decl_mjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn decl_mjs() { transpile_and_test( file!(), true, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/decl_mjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::FunctionDeclaration, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "fetch_decl".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/decl_mjs/index.mjs").unwrap(), + FunctionQuery::function_declaration("fetch", FunctionKind::Async), + "fetch_decl", + )), ); } diff --git a/tests/expr_cjs/mod.rs b/tests/expr_cjs/mod.rs index 3cbbd5c..3bbca82 100644 --- a/tests/expr_cjs/mod.rs +++ b/tests/expr_cjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn expr_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/expr_cjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::FunctionExpression, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "fetch_expr".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/expr_cjs/index.mjs").unwrap(), + FunctionQuery::function_expression("fetch", FunctionKind::Async), + "fetch_expr", + )), ); } diff --git a/tests/expr_mjs/mod.rs b/tests/expr_mjs/mod.rs index 7eea046..ee45bd7 100644 --- a/tests/expr_mjs/mod.rs +++ b/tests/expr_mjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn expr_mjs() { transpile_and_test( file!(), true, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/expr_mjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::FunctionExpression, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "fetch_expr".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/expr_mjs/index.mjs").unwrap(), + FunctionQuery::function_expression("fetch", FunctionKind::Async), + "fetch_expr", + )), ); } diff --git a/tests/index_cjs/mod.rs b/tests/index_cjs/mod.rs index d2405fb..6d2290c 100644 --- a/tests/index_cjs/mod.rs +++ b/tests/index_cjs/mod.rs @@ -1,26 +1,20 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn index_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/index_cjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "fetch".to_string(), - typ: FunctionType::Method, + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/index_cjs/index.mjs").unwrap(), + FunctionQuery::ClassMethod { + class_name: "Undici".to_string(), + method_name: "fetch".to_string(), kind: FunctionKind::Async, index: 2, }, - operator: InstrumentationOperator::Promise, - channel_name: "Undici_fetch".to_string(), - }), + "Undici_fetch", + )), ); } diff --git a/tests/multiple_class_method_cjs/mod.rs b/tests/multiple_class_method_cjs/mod.rs index 6d41eef..54273ff 100644 --- a/tests/multiple_class_method_cjs/mod.rs +++ b/tests/multiple_class_method_cjs/mod.rs @@ -1,7 +1,5 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn multiple_class_method_cjs() { @@ -10,36 +8,28 @@ fn multiple_class_method_cjs() { false, Config::new( vec![ - InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/multiple_class_method_cjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "fetch1".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "Undici_fetch1".to_string(), - }, - InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/multiple_class_method_cjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "fetch2".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "Undici_fetch2".to_string(), - }, + InstrumentationConfig::new( + ModuleMatcher::new( + "undici", + ">=0.0.1", + "tests/multiple_class_method_cjs/index.mjs", + ) + .unwrap(), + FunctionQuery::class_method("Undici", "fetch1", FunctionKind::Async), + "Undici_fetch1", + ), + InstrumentationConfig::new( + ModuleMatcher::new( + "undici", + ">=0.0.1", + "tests/multiple_class_method_cjs/index.mjs", + ) + .unwrap(), + FunctionQuery::class_method("Undici", "fetch2", FunctionKind::Async), + "Undici_fetch2", + ), ], - "diagnostics_channel".to_string(), + None, ), ); } diff --git a/tests/multiple_load_cjs/mod.rs b/tests/multiple_load_cjs/mod.rs index 6c00205..2e264e7 100644 --- a/tests/multiple_load_cjs/mod.rs +++ b/tests/multiple_load_cjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn multiple_load_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/multiple_load_cjs/index.mjs"), - function_query: FunctionQuery { - class: Some("Undici".to_string()), - name: "fetch".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "Undici_fetch".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/multiple_load_cjs/index.mjs").unwrap(), + FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async), + "Undici_fetch", + )), ); } diff --git a/tests/object_method_cjs/mod.rs b/tests/object_method_cjs/mod.rs index f3f0949..9a902f6 100644 --- a/tests/object_method_cjs/mod.rs +++ b/tests/object_method_cjs/mod.rs @@ -1,26 +1,15 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn object_method_cjs() { transpile_and_test( file!(), false, - Config::new_single_with_default_dc_module(InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/object_method_cjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::Method, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "Undici_fetch".to_string(), - }), + Config::new_single(InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/object_method_cjs/index.mjs").unwrap(), + FunctionQuery::object_method("fetch", FunctionKind::Async), + "Undici_fetch", + )), ); } diff --git a/tests/polyfill_cjs/mod.rs b/tests/polyfill_cjs/mod.rs index 92f0ec5..1854bf6 100644 --- a/tests/polyfill_cjs/mod.rs +++ b/tests/polyfill_cjs/mod.rs @@ -1,7 +1,5 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn polyfill_cjs() { @@ -9,21 +7,12 @@ fn polyfill_cjs() { file!(), false, Config::new( - vec![InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/polyfill_cjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::FunctionDeclaration, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "fetch_decl".to_string(), - }], - "./polyfill.js".to_string(), + vec![InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/polyfill_cjs/index.mjs").unwrap(), + FunctionQuery::function_declaration("fetch", FunctionKind::Async), + "fetch_decl", + )], + Some("./polyfill.js".to_string()), ), ); } diff --git a/tests/polyfill_mjs/mod.rs b/tests/polyfill_mjs/mod.rs index dbe0337..38d7159 100644 --- a/tests/polyfill_mjs/mod.rs +++ b/tests/polyfill_mjs/mod.rs @@ -1,7 +1,5 @@ use crate::common::*; -use nodejs_semver::Range; use orchestrion_js::*; -use std::path::PathBuf; #[test] fn polyfill_mjs() { @@ -9,21 +7,12 @@ fn polyfill_mjs() { file!(), true, Config::new( - vec![InstrumentationConfig { - module_name: "undici".to_string(), - version_range: Range::parse(">=0.0.1").unwrap(), - file_path: PathBuf::from("tests/polyfill_mjs/index.mjs"), - function_query: FunctionQuery { - class: None, - name: "fetch".to_string(), - typ: FunctionType::FunctionDeclaration, - kind: FunctionKind::Async, - index: 0, - }, - operator: InstrumentationOperator::Promise, - channel_name: "fetch_decl".to_string(), - }], - "./polyfill.js".to_string(), + vec![InstrumentationConfig::new( + ModuleMatcher::new("undici", ">=0.0.1", "tests/polyfill_mjs/index.mjs").unwrap(), + FunctionQuery::function_declaration("fetch", FunctionKind::Async), + "fetch_decl", + )], + Some("./polyfill.js".to_string()), ), ); } From 3f1eba9c92507361547a7f062306f2acd4d3b843 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 26 Apr 2025 22:09:12 +0200 Subject: [PATCH 04/12] Simplify test module matching --- tests/class_method_cjs/mod.rs | 2 +- tests/common/mod.rs | 11 +++++++++-- tests/constructor_cjs/mod.rs | 2 +- tests/constructor_mjs/mod.rs | 2 +- tests/decl_cjs/mod.rs | 2 +- tests/decl_mjs/mod.rs | 2 +- tests/expr_cjs/mod.rs | 2 +- tests/expr_mjs/mod.rs | 2 +- tests/index_cjs/mod.rs | 2 +- tests/multiple_class_method_cjs/mod.rs | 14 ++------------ tests/multiple_load_cjs/mod.rs | 2 +- tests/object_method_cjs/mod.rs | 2 +- tests/polyfill_cjs/mod.rs | 2 +- tests/polyfill_mjs/mod.rs | 2 +- 14 files changed, 23 insertions(+), 26 deletions(-) diff --git a/tests/class_method_cjs/mod.rs b/tests/class_method_cjs/mod.rs index e67beaa..38345b9 100644 --- a/tests/class_method_cjs/mod.rs +++ b/tests/class_method_cjs/mod.rs @@ -7,7 +7,7 @@ fn class_method_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/class_method_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async), "Undici_fetch", )), diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 3b09324..6554d66 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -83,14 +83,17 @@ fn transpile( .unwrap() } +static TEST_MODULE_NAME: &'static str = "undici"; +static TEST_MODULE_PATH: &'static str = "index.mjs"; + pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) { let test_file = PathBuf::from(test_file); let test_dir = test_file.parent().expect("Couldn't find test directory"); - let file_path = test_dir.join("index.mjs"); + let file_path = PathBuf::from("index.mjs"); let mut instrumentor = Instrumentor::new(config); let mut instrumentations = - instrumentor.get_matching_instrumentations("undici", "0.0.1", &file_path); + instrumentor.get_matching_instrumentations(TEST_MODULE_NAME, "0.0.1", &file_path); let extension = if mjs { "mjs" } else { "js" }; let instrumentable = test_dir.join(format!("mod.{}", extension)); @@ -113,3 +116,7 @@ pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) { .assert() .success(); } + +pub fn test_module_matcher() -> ModuleMatcher { + ModuleMatcher::new(TEST_MODULE_NAME, ">=0.0.1", TEST_MODULE_PATH).unwrap() +} diff --git a/tests/constructor_cjs/mod.rs b/tests/constructor_cjs/mod.rs index a41b7f3..3e24e4c 100644 --- a/tests/constructor_cjs/mod.rs +++ b/tests/constructor_cjs/mod.rs @@ -7,7 +7,7 @@ fn constructor_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/constructor_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::class_constructor("Undici"), "Undici_constructor", )), diff --git a/tests/constructor_mjs/mod.rs b/tests/constructor_mjs/mod.rs index e618d18..78ebc56 100644 --- a/tests/constructor_mjs/mod.rs +++ b/tests/constructor_mjs/mod.rs @@ -7,7 +7,7 @@ fn constructor_mjs() { file!(), true, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/constructor_mjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::class_constructor("Undici"), "Undici_constructor", )), diff --git a/tests/decl_cjs/mod.rs b/tests/decl_cjs/mod.rs index 8a15de3..3850e87 100644 --- a/tests/decl_cjs/mod.rs +++ b/tests/decl_cjs/mod.rs @@ -7,7 +7,7 @@ fn decl_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/decl_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), "fetch_decl", )), diff --git a/tests/decl_mjs/mod.rs b/tests/decl_mjs/mod.rs index 1344bd4..51adf8d 100644 --- a/tests/decl_mjs/mod.rs +++ b/tests/decl_mjs/mod.rs @@ -7,7 +7,7 @@ fn decl_mjs() { file!(), true, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/decl_mjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), "fetch_decl", )), diff --git a/tests/expr_cjs/mod.rs b/tests/expr_cjs/mod.rs index 3bbca82..e8febf5 100644 --- a/tests/expr_cjs/mod.rs +++ b/tests/expr_cjs/mod.rs @@ -7,7 +7,7 @@ fn expr_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/expr_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::function_expression("fetch", FunctionKind::Async), "fetch_expr", )), diff --git a/tests/expr_mjs/mod.rs b/tests/expr_mjs/mod.rs index ee45bd7..0a32ddb 100644 --- a/tests/expr_mjs/mod.rs +++ b/tests/expr_mjs/mod.rs @@ -7,7 +7,7 @@ fn expr_mjs() { file!(), true, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/expr_mjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::function_expression("fetch", FunctionKind::Async), "fetch_expr", )), diff --git a/tests/index_cjs/mod.rs b/tests/index_cjs/mod.rs index 6d2290c..371426b 100644 --- a/tests/index_cjs/mod.rs +++ b/tests/index_cjs/mod.rs @@ -7,7 +7,7 @@ fn index_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/index_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::ClassMethod { class_name: "Undici".to_string(), method_name: "fetch".to_string(), diff --git a/tests/multiple_class_method_cjs/mod.rs b/tests/multiple_class_method_cjs/mod.rs index 54273ff..d148f20 100644 --- a/tests/multiple_class_method_cjs/mod.rs +++ b/tests/multiple_class_method_cjs/mod.rs @@ -9,22 +9,12 @@ fn multiple_class_method_cjs() { Config::new( vec![ InstrumentationConfig::new( - ModuleMatcher::new( - "undici", - ">=0.0.1", - "tests/multiple_class_method_cjs/index.mjs", - ) - .unwrap(), + test_module_matcher(), FunctionQuery::class_method("Undici", "fetch1", FunctionKind::Async), "Undici_fetch1", ), InstrumentationConfig::new( - ModuleMatcher::new( - "undici", - ">=0.0.1", - "tests/multiple_class_method_cjs/index.mjs", - ) - .unwrap(), + test_module_matcher(), FunctionQuery::class_method("Undici", "fetch2", FunctionKind::Async), "Undici_fetch2", ), diff --git a/tests/multiple_load_cjs/mod.rs b/tests/multiple_load_cjs/mod.rs index 2e264e7..3abad2f 100644 --- a/tests/multiple_load_cjs/mod.rs +++ b/tests/multiple_load_cjs/mod.rs @@ -7,7 +7,7 @@ fn multiple_load_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/multiple_load_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async), "Undici_fetch", )), diff --git a/tests/object_method_cjs/mod.rs b/tests/object_method_cjs/mod.rs index 9a902f6..b69ab1e 100644 --- a/tests/object_method_cjs/mod.rs +++ b/tests/object_method_cjs/mod.rs @@ -7,7 +7,7 @@ fn object_method_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/object_method_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::object_method("fetch", FunctionKind::Async), "Undici_fetch", )), diff --git a/tests/polyfill_cjs/mod.rs b/tests/polyfill_cjs/mod.rs index 1854bf6..5e4ed46 100644 --- a/tests/polyfill_cjs/mod.rs +++ b/tests/polyfill_cjs/mod.rs @@ -8,7 +8,7 @@ fn polyfill_cjs() { false, Config::new( vec![InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/polyfill_cjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), "fetch_decl", )], diff --git a/tests/polyfill_mjs/mod.rs b/tests/polyfill_mjs/mod.rs index 38d7159..13978a5 100644 --- a/tests/polyfill_mjs/mod.rs +++ b/tests/polyfill_mjs/mod.rs @@ -8,7 +8,7 @@ fn polyfill_mjs() { true, Config::new( vec![InstrumentationConfig::new( - ModuleMatcher::new("undici", ">=0.0.1", "tests/polyfill_mjs/index.mjs").unwrap(), + test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), "fetch_decl", )], From 2a69112d616e0327f730c936177325ab4797b5c5 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 28 Apr 2025 01:09:32 +0200 Subject: [PATCH 05/12] feat: Add wasm module build --- .gitignore | 1 + Cargo.lock | 137 ++++++++++++++++++++++++++++++--------- Cargo.toml | 20 ++++-- README.md | 21 ++++-- package.json | 25 +++++++ src/config.rs | 10 ++- src/function_query.rs | 28 ++++++-- src/instrumentation.rs | 2 +- src/lib.rs | 118 ++++++++++++++++++++++++++------- src/wasm.rs | 52 +++++++++++++++ tests/common/mod.rs | 6 +- tests/tests.mjs | 36 ++++++++++ tests/tests.mjs.snapshot | 3 + 13 files changed, 385 insertions(+), 74 deletions(-) create mode 100644 package.json create mode 100644 src/wasm.rs create mode 100644 tests/tests.mjs create mode 100644 tests/tests.mjs.snapshot diff --git a/.gitignore b/.gitignore index 5253332..772ea44 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ ^target/ target tests/*/instrumented.* +pkg/ diff --git a/Cargo.lock b/Cargo.lock index a1c7d33..4a4e1a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,7 +19,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" dependencies = [ "cfg-if", - "getrandom 0.2.15", + "getrandom 0.2.16", "once_cell", "serde", "version_check", @@ -314,9 +314,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.19" +version = "1.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e3a13707ac958681c13b39b458c073d0d9bc8a22cb1b2f4c8e55eb72c13f362" +checksum = "04da6a0d40b948dfc4fa8f5bbf402b0fc1a64a28dbf7d12ffd683550f2c1b63a" dependencies = [ "shlex", ] @@ -595,13 +595,15 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.0+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -622,6 +624,19 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" +[[package]] +name = "gloo-utils" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5555354113b18c547c1d3a98fbf7fb32a9ff4f6fa112ce823a21641a0ba3aa" +dependencies = [ + "js-sys", + "serde", + "serde_json", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "hashbrown" version = "0.13.2" @@ -977,23 +992,22 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miette" -version = "7.5.0" +version = "7.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a955165f87b37fd1862df2a59547ac542c77ef6d17c666f619d1ad22dd89484" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" dependencies = [ "cfg-if", "miette-derive", "owo-colors", "textwrap", - "thiserror 1.0.69", "unicode-width 0.1.14", ] [[package]] name = "miette-derive" -version = "7.5.0" +version = "7.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf45bf44ab49be92fd1227a3be6fc6f617f1a337c06af54981048574d8783147" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" dependencies = [ "proc-macro2", "quote", @@ -1008,18 +1022,18 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "munge" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0091202c98cf06da46c279fdf50cccb6b1c43b4521abdf6a27b4c7e71d5d9d7" +checksum = "9e22e7961c873e8b305b176d2a4e1d41ce7ba31bc1c52d2a107a89568ec74c55" dependencies = [ "munge_macro", ] [[package]] name = "munge_macro" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "734799cf91479720b2f970c61a22850940dd91e27d4f02b1c6fc792778df2459" +checksum = "0ac7d860b767c6398e88fe93db73ce53eb496057aa6895ffa4d60cb02e1d1c6b" dependencies = [ "proc-macro2", "quote", @@ -1040,6 +1054,7 @@ checksum = "54a541e66989ad860689e1994447f22ab670d604068ed11376894ada03890c11" dependencies = [ "bytecount", "miette", + "serde", "thiserror 1.0.69", "winnow", ] @@ -1123,11 +1138,15 @@ name = "orchestrion-js" version = "0.1.0" dependencies = [ "assert_cmd", + "getrandom 0.2.16", "nodejs-semver", + "serde", "swc", "swc_core", "swc_ecma_parser", "swc_ecma_visit", + "tsify", + "wasm-bindgen", ] [[package]] @@ -1150,9 +1169,9 @@ checksum = "1036865bb9422d3300cf723f657c2851d0e9ab12567854b1f4eba3d77decf564" [[package]] name = "par-core" -version = "1.0.3" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b506ab63a8bd3cd38858c7bfc2d078a189dc3210c7f8c9be1bbaf50c082a0ae" +checksum = "757892557993c69e82f9de0f9051e87144278aa342f03bf53617bbf044554484" dependencies = [ "once_cell", ] @@ -1338,9 +1357,9 @@ dependencies = [ [[package]] name = "psm" -version = "0.1.25" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f58e5423e24c18cc840e1c98370b3993c6649cd1678b4d24318bcf0a083cbe88" +checksum = "6e944464ec8536cd1beb0bbfd96987eb5e3b72f2ecdafdc5c769a37f1fa2ae1f" dependencies = [ "cc", ] @@ -1611,6 +1630,17 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_derive" version = "1.0.219" @@ -1622,6 +1652,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_derive_internals" +version = "0.29.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "serde_json" version = "1.0.140" @@ -1752,9 +1793,9 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stacker" -version = "0.1.20" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "601f9201feb9b09c00266478bf459952b9ef9a6b94edb2f21eba14ab681a60a9" +checksum = "cddb07e32ddb770749da91081d8d0ac3a16f1a569a18b20348cd371f5dead06b" dependencies = [ "cc", "cfg-if", @@ -1895,9 +1936,9 @@ dependencies = [ [[package]] name = "swc_common" -version = "8.1.0" +version = "8.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d96ac5d021c7c20acb3073940b4ee59b62989a705f855783c4a452e0737a2e6" +checksum = "4f8c8e4348383e4154f8d384cdad7e48f5d6d3daef78af376ac4e5ddbbf60c88" dependencies = [ "anyhow", "ast_node", @@ -1981,9 +2022,9 @@ dependencies = [ [[package]] name = "swc_core" -version = "22.5.0" +version = "22.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "750935e8c0a2c23b69968d8c8f65963750fb20c9803311e3d3ed248340fbac97" +checksum = "39b5f70fe5f8db1d3977aae3f2ecb64bae55d0e4f5ec93d372219f2ecf58e372" dependencies = [ "once_cell", "swc_allocator", @@ -2622,9 +2663,9 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_proposal" -version = "12.0.1" +version = "12.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5265158f5134b7b37dd2d53e7730921b8b5f567f6baddcc52129c2eb55927214" +checksum = "08a2b5e62d2badf805aba6e976518cceb28273a108cb9ab9f339c55483edc92c" dependencies = [ "either", "rustc-hash 2.1.1", @@ -2732,9 +2773,9 @@ dependencies = [ [[package]] name = "swc_ecma_utils" -version = "12.0.0" +version = "12.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d6c8ba7d987dcc254f05ad2c23e7a6ec3f259611af2923a8c1a0602556cd21" +checksum = "a7c499ba586b784be6dfbdd76ebd3cfdbabaf43a5bda162a11fe7dd326670b62" dependencies = [ "indexmap", "num_cpus", @@ -2916,9 +2957,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.100" +version = "2.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" +checksum = "8ce2b7fc941b3a24138a0a7cf8e858bfc6a992e7978a068a5c760deb0ed43caf" dependencies = [ "proc-macro2", "quote", @@ -3164,6 +3205,32 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "tsify" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2ec91b85e6c6592ed28636cb1dd1fac377ecbbeb170ff1d79f97aac5e38926d" +dependencies = [ + "gloo-utils", + "serde", + "serde-wasm-bindgen", + "serde_json", + "tsify-macros", + "wasm-bindgen", +] + +[[package]] +name = "tsify-macros" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a324606929ad11628a19206d7853807481dcaecd6c08be70a235930b8241955" +dependencies = [ + "proc-macro2", + "quote", + "serde_derive_internals", + "syn", +] + [[package]] name = "typed-arena" version = "2.0.2" @@ -3360,6 +3427,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "web-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index b7e4d6f..51ec9a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,18 +6,28 @@ rust-version = "1.84.1" license = "Apache-2.0" [lib] -# crate-type = ["cdylib"] -# This was originally set as above, but commented to run tests in tests folder. +crate-type = ["cdylib", "rlib"] -[profile.release] -lto = true +[features] +wasm = ["wasm-bindgen", "tsify", "getrandom"] [dependencies] -nodejs-semver = "4" +nodejs-semver = { version = "4", features = ["serde"] } swc = "21" swc_core = { version = "22", features = ["ecma_plugin_transform","ecma_quote"] } swc_ecma_parser = "11" swc_ecma_visit = { version = "8", features = ["path"] } +serde = { version = "1", features = ["derive"] } + +# wasm +wasm-bindgen = { version = "0.2", optional = true } +tsify = { version='0.5', features = ["js"], optional = true} +# we need this to enable the js feature +getrandom = { version = "*", features = ["js"], optional = true } [dev-dependencies] assert_cmd = "2" + +[profile.release] +lto = true +opt-level = "s" \ No newline at end of file diff --git a/README.md b/README.md index 5788e0c..500ec06 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,21 @@ # Orchestion-JS -Orchestrion is a library for instrumenting Node.js libraries at build or load time. -It provides [`VisitMut`] implementations for SWC's AST nodes, which can be used to insert tracing code into matching functions. -It's entirely configurable via a YAML string, and can be used in SWC plugins, or anything else that mutates JavaScript ASTs using SWC. +Orchestrion is a library for instrumenting Node.js libraries at build or load +time. + +It provides `VisitMut` implementations for SWC's AST nodes, which can be used to +insert tracing code into matching functions. It can be used in SWC plugins, or +anything else that mutates JavaScript ASTs using SWC. + +Orchestrion can also be built as a JavaScript module, which can be used from +Node.js. + +To build the JavaScript module, ensure you have +[Rust installed](https://www.rust-lang.org/tools/install) and then run: + +```bash +npm install && npm run build +``` ## Contributing @@ -11,5 +24,3 @@ See CONTRIBUTING.md ## License See LICENSE - -[`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html diff --git a/package.json b/package.json new file mode 100644 index 0000000..e12f7f3 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "orchestrion-js", + "version": "0.1.0", + "license": "Apache-2.0", + "files": [ + "./pkg/orchestrion_js_bg.wasm", + "./pkg/orchestrion_js.js", + "./pkg/orchestrion_js.d.ts", + "LICENSE-3rdparty.csv", + "LICENSE", + "NOTICE" + ], + "main": "./pkg/orchestrion_js.js", + "types": "./pkg/orchestrion_js.d.ts", + "scripts": { + "build": "wasm-pack build --target nodejs --release -- --features wasm", + "test": "node ./tests/tests.mjs" + }, + "dependencies": { + "wasm-pack": "^0.13.1" + }, + "volta": { + "node": "22.15.0" + } +} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index de8085c..a23784e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,9 +4,12 @@ **/ use crate::function_query::FunctionQuery; use nodejs_semver::{Range, SemverError, Version}; +use serde::{Deserialize, Serialize}; use std::path::PathBuf; -#[derive(Debug, Clone)] +#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct ModuleMatcher { pub name: String, pub version_range: Range, @@ -41,7 +44,10 @@ impl ModuleMatcher { } } -#[derive(Debug, Clone)] +#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))] +#[serde(rename_all = "camelCase")] pub struct InstrumentationConfig { pub module: ModuleMatcher, pub function_query: FunctionQuery, diff --git a/src/function_query.rs b/src/function_query.rs index 2f7a28a..e39a210 100644 --- a/src/function_query.rs +++ b/src/function_query.rs @@ -2,6 +2,7 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ +use serde::{Deserialize, Serialize}; use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; #[derive(Debug, Clone)] @@ -11,7 +12,8 @@ pub(crate) enum FunctionType { Method, } -#[derive(Debug, Clone)] +#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] +#[derive(Debug, Clone, Serialize, Deserialize)] pub enum FunctionKind { Sync, Async, @@ -40,31 +42,45 @@ impl FunctionKind { } } -#[derive(Debug, Clone)] +#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(untagged, rename_all_fields = "camelCase")] pub enum FunctionQuery { - ClassConstructor { - class_name: String, - index: usize, - }, + // The order here matters because this enum is untagged, serde will try + // choose the first variant that matches the data. ClassMethod { class_name: String, method_name: String, kind: FunctionKind, + #[serde(default)] + #[cfg_attr(feature = "wasm", tsify(optional))] + index: usize, + }, + ClassConstructor { + class_name: String, + #[serde(default)] + #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, ObjectMethod { method_name: String, kind: FunctionKind, + #[serde(default)] + #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, FunctionDeclaration { function_name: String, kind: FunctionKind, + #[serde(default)] + #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, FunctionExpression { expression_name: String, kind: FunctionKind, + #[serde(default)] + #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, } diff --git a/src/instrumentation.rs b/src/instrumentation.rs index f2d0247..3f55bbe 100644 --- a/src/instrumentation.rs +++ b/src/instrumentation.rs @@ -27,7 +27,7 @@ macro_rules! ident { /// /// [`Instrumentation`]: Instrumentation /// [`VisitMut`]: https://rustdoc.swc.rs/swc_core/ecma/visit/trait.VisitMut.html -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Instrumentation { config: InstrumentationConfig, count: usize, diff --git a/src/lib.rs b/src/lib.rs index d027f03..e328f95 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,17 +18,23 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ -use std::path::PathBuf; +use std::{error::Error, path::PathBuf, sync::Arc}; +use swc::{ + config::{IsModule, SourceMapsConfig}, + try_with_handler, Compiler, HandlerOpts, PrintArgs, +}; use swc_core::{ + common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping}, ecma::{ ast::{ - AssignExpr, ClassDecl, ClassMethod, Constructor, FnDecl, MethodProp, Module, Script, - Str, VarDecl, + AssignExpr, ClassDecl, ClassMethod, Constructor, EsVersion, FnDecl, MethodProp, Module, + Script, Str, VarDecl, }, visit::{VisitMut, VisitMutWith}, }, quote, }; +use swc_ecma_parser::{EsSyntax, Syntax}; mod error; @@ -41,6 +47,9 @@ pub use instrumentation::*; mod function_query; pub use function_query::*; +#[cfg(feature = "wasm")] +pub mod wasm; + /// This struct is responsible for managing all instrumentations. It's created from a YAML string /// via the [`FromStr`] trait. See tests for examples, but by-and-large this just means you can /// call `.parse()` on a YAML string to get an `Instrumentor` instance, if it's valid. @@ -65,37 +74,102 @@ impl Instrumentor { /// For a given module name, version, and file path within the module, return all /// `Instrumentation` instances that match. - pub fn get_matching_instrumentations<'a>( - &'a mut self, - module_name: &'a str, - version: &'a str, - file_path: &'a PathBuf, - ) -> InstrumentationVisitor<'a> { + #[must_use] + pub fn get_matching_instrumentations( + &self, + module_name: &str, + version: &str, + file_path: &PathBuf, + ) -> InstrumentationVisitor { let instrumentations = self .instrumentations - .iter_mut() + .iter() .filter(|instr| instr.matches(module_name, version, file_path)); - InstrumentationVisitor::new(instrumentations, self.dc_module.as_ref()) + InstrumentationVisitor::new(instrumentations, &self.dc_module) } } #[derive(Debug)] -pub struct InstrumentationVisitor<'a> { - instrumentations: Vec<&'a mut Instrumentation>, - dc_module: &'a str, +pub struct InstrumentationVisitor { + instrumentations: Vec, + dc_module: String, } -impl<'a> InstrumentationVisitor<'a> { - fn new(instrumentations: I, dc_module: &'a str) -> Self +impl InstrumentationVisitor { + fn new<'b, I>(instrumentations: I, dc_module: &str) -> Self where - I: Iterator + 'a, + I: Iterator, { Self { - instrumentations: instrumentations.collect(), - dc_module, + instrumentations: instrumentations.cloned().collect(), + dc_module: dc_module.to_string(), } } + + #[must_use] + pub fn has_instrumentations(&self) -> bool { + !self.instrumentations.is_empty() + } + + /// Transform the given JavaScript code. + /// # Errors + /// Returns an error if the transformation fails. + pub fn transform( + &mut self, + contents: &str, + is_module: IsModule, + ) -> Result> { + let compiler = Compiler::new(Arc::new(swc_core::common::SourceMap::new( + FilePathMapping::empty(), + ))); + + #[allow(clippy::redundant_closure_for_method_calls)] + Ok(try_with_handler( + compiler.cm.clone(), + HandlerOpts { + color: ColorConfig::Never, + skip_filename: false, + }, + |handler| { + let source_file = compiler.cm.new_source_file( + Arc::new(FileName::Real(PathBuf::from("index.mjs"))), + contents.to_string(), + ); + + let program = compiler + .parse_js( + source_file.clone(), + handler, + EsVersion::latest(), + Syntax::Es(EsSyntax { + explicit_resource_management: true, + import_attributes: true, + decorators: true, + ..Default::default() + }), + is_module, + Some(&compiler.comments() as &dyn Comments), + ) + .map(|mut program| { + program.visit_mut_with(self); + program + })?; + let result = compiler.print( + &program, + PrintArgs { + source_file_name: None, + source_map: SourceMapsConfig::Bool(false), + comments: None, + emit_source_map_columns: false, + ..Default::default() + }, + )?; + Ok(result.code) + }, + ) + .map_err(|e| e.to_pretty_error())?) + } } macro_rules! visit_with_all { @@ -119,14 +193,14 @@ macro_rules! visit_with_all_fn { }; } -impl VisitMut for InstrumentationVisitor<'_> { +impl VisitMut for InstrumentationVisitor { fn visit_mut_module(&mut self, item: &mut Module) { let mut line = quote!( "import { tracingChannel as tr_ch_apm_tracingChannel } from 'dc';" as ModuleItem, ); if let Some(module_decl) = line.as_mut_module_decl() { if let Some(import) = module_decl.as_mut_import() { - import.src = Box::new(Str::from(self.dc_module)); + import.src = Box::new(Str::from(self.dc_module.as_ref())); item.body.insert(0, line); } } @@ -139,7 +213,7 @@ impl VisitMut for InstrumentationVisitor<'_> { fn visit_mut_script(&mut self, item: &mut Script) { let import = quote!( "const { tracingChannel: tr_ch_apm_tracingChannel } = require($dc);" as Stmt, - dc: Expr = self.dc_module.into(), + dc: Expr = self.dc_module.clone().into(), ); item.body.insert(get_script_start_index(item), import); visit_with_all!(self, visit_mut_script, item); diff --git a/src/wasm.rs b/src/wasm.rs new file mode 100644 index 0000000..485eecc --- /dev/null +++ b/src/wasm.rs @@ -0,0 +1,52 @@ +use crate::*; +use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; + +#[wasm_bindgen] +pub struct InstrumentationMatcher(Instrumentor); + +#[wasm_bindgen] +impl InstrumentationMatcher { + #[wasm_bindgen(js_name = "getTransformer")] + pub fn get_transformer( + &mut self, + module_name: &str, + version: &str, + file_path: &str, + ) -> Option { + let instrumentations = + self.0 + .get_matching_instrumentations(module_name, version, &PathBuf::from(file_path)); + + if instrumentations.has_instrumentations() { + Some(Transformer(instrumentations)) + } else { + None + } + } +} + +#[wasm_bindgen] +pub struct Transformer(InstrumentationVisitor); + +#[wasm_bindgen] +impl Transformer { + #[wasm_bindgen] + pub fn transform(&mut self, contents: &str, is_module: bool) -> Result { + let is_module = if is_module { + IsModule::Bool(true) + } else { + IsModule::Bool(false) + }; + self.0 + .transform(contents, is_module) + .map_err(|e| JsValue::from_str(&e.to_string())) + } +} + +#[wasm_bindgen] +pub fn create( + configs: Vec, + dc_module: Option, +) -> InstrumentationMatcher { + InstrumentationMatcher(Instrumentor::new(Config::new(configs, dc_module))) +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 6554d66..706d25b 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -83,15 +83,15 @@ fn transpile( .unwrap() } -static TEST_MODULE_NAME: &'static str = "undici"; -static TEST_MODULE_PATH: &'static str = "index.mjs"; +static TEST_MODULE_NAME: &str = "undici"; +static TEST_MODULE_PATH: &str = "index.mjs"; pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) { let test_file = PathBuf::from(test_file); let test_dir = test_file.parent().expect("Couldn't find test directory"); let file_path = PathBuf::from("index.mjs"); - let mut instrumentor = Instrumentor::new(config); + let instrumentor = Instrumentor::new(config); let mut instrumentations = instrumentor.get_matching_instrumentations(TEST_MODULE_NAME, "0.0.1", &file_path); diff --git a/tests/tests.mjs b/tests/tests.mjs new file mode 100644 index 0000000..aa15dc2 --- /dev/null +++ b/tests/tests.mjs @@ -0,0 +1,36 @@ +import test from "node:test"; +import { create } from "../pkg/orchestrion_js.js"; + +test("test the wasm", async (t) => { + const transformer = create([ + { + module: { name: "one", versionRange: ">=1", filePath: "index.js" }, + functionQuery: { className: "Up" }, + channelName: "up:constructor", + }, + { + module: { name: "one", versionRange: ">=1", filePath: "index.js" }, + functionQuery: { + className: "Up", + methodName: "fetch", + kind: "Sync", + }, + channelName: "up:fetch", + }, + ]); + + const matchedTransforms = transformer.getTransformer( + "one", + "1.0.0", + "index.js", + ); + + t.assert.ok(matchedTransforms); + + const output = matchedTransforms.transform( + "export class Up { constructor() {console.log('constructor')} fetch() {console.log('fetch')} }", + true, + ); + + t.assert.snapshot(output); +}); diff --git a/tests/tests.mjs.snapshot b/tests/tests.mjs.snapshot new file mode 100644 index 0000000..e35ec6e --- /dev/null +++ b/tests/tests.mjs.snapshot @@ -0,0 +1,3 @@ +exports[`test the wasm 1`] = ` +"import { tracingChannel as tr_ch_apm_tracingChannel } from \\"diagnostics_channel\\";\\nconst tr_ch_apm$up:fetch = tr_ch_apm_tracingChannel(\\"orchestrion:one:up:fetch\\");\\nconst tr_ch_apm$up:constructor = tr_ch_apm_tracingChannel(\\"orchestrion:one:up:constructor\\");\\nexport class Up {\\n constructor(){\\n const tr_ch_apm_ctx$up:constructor = {\\n arguments\\n };\\n try {\\n if (tr_ch_apm$up:constructor.hasSubscribers) {\\n tr_ch_apm$up:constructor.start.publish(tr_ch_apm_ctx$up:constructor);\\n }\\n console.log('constructor');\\n } catch (tr_ch_err) {\\n if (tr_ch_apm$up:constructor.hasSubscribers) {\\n tr_ch_apm_ctx$up:constructor.error = tr_ch_err;\\n try {\\n tr_ch_apm_ctx$up:constructor.self = this;\\n } catch (refErr) {}\\n tr_ch_apm$up:constructor.error.publish(tr_ch_apm_ctx$up:constructor);\\n }\\n throw tr_ch_err;\\n } finally{\\n if (tr_ch_apm$up:constructor.hasSubscribers) {\\n tr_ch_apm_ctx$up:constructor.self = this;\\n tr_ch_apm$up:constructor.end.publish(tr_ch_apm_ctx$up:constructor);\\n }\\n }\\n }\\n fetch() {\\n const traced = ()=>{\\n console.log('fetch');\\n };\\n if (!tr_ch_apm$up:fetch.hasSubscribers) return traced();\\n return tr_ch_apm$up:fetch.traceSync(traced, {\\n arguments,\\n self: this\\n });\\n }\\n}\\n" +`; From 64f80d4d0f2020dae54a721b2a4352a687782dbc Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 29 Apr 2025 18:22:27 +0200 Subject: [PATCH 06/12] remove duplicate code --- tests/common/mod.rs | 80 +++------------------------------------------ 1 file changed, 4 insertions(+), 76 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 706d25b..b86ed47 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -7,81 +7,7 @@ use orchestrion_js::*; use std::io::prelude::*; use std::path::PathBuf; use std::process::Command; -use std::sync::Arc; -use swc::{ - config::{IsModule, SourceMapsConfig}, - try_with_handler, Compiler, HandlerOpts, PrintArgs, -}; -use swc_core::common::{comments::Comments, errors::ColorConfig, FileName, FilePathMapping}; -use swc_core::ecma::ast::EsVersion; -use swc_ecma_parser::{EsSyntax, Syntax}; -use swc_ecma_visit::VisitMutWith; - -fn print_result(original: &str, modified: &str) { - println!( - "\n - == === Original === == - \n{}\n\n\n - == === Modified === == - \n{}\n\n", - original, modified - ); -} - -fn transpile( - contents: &str, - is_module: IsModule, - instrumentation: &mut InstrumentationVisitor, -) -> String { - let compiler = Compiler::new(Arc::new(swc_core::common::SourceMap::new( - FilePathMapping::empty(), - ))); - try_with_handler( - compiler.cm.clone(), - HandlerOpts { - color: ColorConfig::Never, - skip_filename: false, - }, - |handler| { - let source_file = compiler.cm.new_source_file( - Arc::new(FileName::Real(PathBuf::from("index.mjs"))), - contents.to_string(), - ); - - let program = compiler - .parse_js( - source_file.to_owned(), - handler, - EsVersion::latest(), - Syntax::Es(EsSyntax { - explicit_resource_management: true, - import_attributes: true, - decorators: true, - ..Default::default() - }), - is_module, - Some(&compiler.comments() as &dyn Comments), - ) - .map(|mut program| { - program.visit_mut_with(instrumentation); - program - }) - .unwrap(); - let result = compiler - .print( - &program, - PrintArgs { - source_file_name: None, - source_map: SourceMapsConfig::Bool(false), - comments: None, - emit_source_map_columns: false, - ..Default::default() - }, - ) - .unwrap(); - - print_result(contents, &result.code); - Ok(result.code) - }, - ) - .unwrap() -} +use swc::config::IsModule; static TEST_MODULE_NAME: &str = "undici"; static TEST_MODULE_PATH: &str = "index.mjs"; @@ -101,7 +27,9 @@ pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) { let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); - let result = transpile(&contents, IsModule::Bool(mjs), &mut instrumentations); + let result = instrumentations + .transform(&contents, IsModule::Bool(mjs)) + .unwrap(); let instrumented_file = test_dir.join(format!("instrumented.{}", extension)); let mut file = std::fs::File::create(&instrumented_file).unwrap(); From 8c3eaac55b9f39859559f85708a2f7fc63a9cbed Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 29 Apr 2025 21:18:23 +0200 Subject: [PATCH 07/12] reorder `InstrumentationConfig::new` args --- src/config.rs | 6 +++--- tests/class_method_cjs/mod.rs | 2 +- tests/constructor_cjs/mod.rs | 2 +- tests/constructor_mjs/mod.rs | 2 +- tests/decl_cjs/mod.rs | 2 +- tests/decl_mjs/mod.rs | 2 +- tests/expr_cjs/mod.rs | 2 +- tests/expr_mjs/mod.rs | 2 +- tests/index_cjs/mod.rs | 2 +- tests/multiple_class_method_cjs/mod.rs | 4 ++-- tests/multiple_load_cjs/mod.rs | 2 +- tests/object_method_cjs/mod.rs | 2 +- tests/polyfill_cjs/mod.rs | 2 +- tests/polyfill_mjs/mod.rs | 2 +- 14 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/config.rs b/src/config.rs index de8085c..771108c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -43,18 +43,18 @@ impl ModuleMatcher { #[derive(Debug, Clone)] pub struct InstrumentationConfig { + pub channel_name: String, pub module: ModuleMatcher, pub function_query: FunctionQuery, - pub channel_name: String, } impl InstrumentationConfig { #[must_use] - pub fn new(module: ModuleMatcher, function_query: FunctionQuery, channel_name: &str) -> Self { + pub fn new(channel_name: &str, module: ModuleMatcher, function_query: FunctionQuery) -> Self { Self { + channel_name: channel_name.to_string(), module, function_query, - channel_name: channel_name.to_string(), } } } diff --git a/tests/class_method_cjs/mod.rs b/tests/class_method_cjs/mod.rs index 38345b9..3e1e913 100644 --- a/tests/class_method_cjs/mod.rs +++ b/tests/class_method_cjs/mod.rs @@ -7,9 +7,9 @@ fn class_method_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "Undici_fetch", test_module_matcher(), FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async), - "Undici_fetch", )), ); } diff --git a/tests/constructor_cjs/mod.rs b/tests/constructor_cjs/mod.rs index 3e24e4c..fb1c3d5 100644 --- a/tests/constructor_cjs/mod.rs +++ b/tests/constructor_cjs/mod.rs @@ -7,9 +7,9 @@ fn constructor_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "Undici_constructor", test_module_matcher(), FunctionQuery::class_constructor("Undici"), - "Undici_constructor", )), ); } diff --git a/tests/constructor_mjs/mod.rs b/tests/constructor_mjs/mod.rs index 78ebc56..10cce6a 100644 --- a/tests/constructor_mjs/mod.rs +++ b/tests/constructor_mjs/mod.rs @@ -7,9 +7,9 @@ fn constructor_mjs() { file!(), true, Config::new_single(InstrumentationConfig::new( + "Undici_constructor", test_module_matcher(), FunctionQuery::class_constructor("Undici"), - "Undici_constructor", )), ); } diff --git a/tests/decl_cjs/mod.rs b/tests/decl_cjs/mod.rs index 3850e87..db5254b 100644 --- a/tests/decl_cjs/mod.rs +++ b/tests/decl_cjs/mod.rs @@ -7,9 +7,9 @@ fn decl_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "fetch_decl", test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), - "fetch_decl", )), ); } diff --git a/tests/decl_mjs/mod.rs b/tests/decl_mjs/mod.rs index 51adf8d..d186fff 100644 --- a/tests/decl_mjs/mod.rs +++ b/tests/decl_mjs/mod.rs @@ -7,9 +7,9 @@ fn decl_mjs() { file!(), true, Config::new_single(InstrumentationConfig::new( + "fetch_decl", test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), - "fetch_decl", )), ); } diff --git a/tests/expr_cjs/mod.rs b/tests/expr_cjs/mod.rs index e8febf5..2a67c4f 100644 --- a/tests/expr_cjs/mod.rs +++ b/tests/expr_cjs/mod.rs @@ -7,9 +7,9 @@ fn expr_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "fetch_expr", test_module_matcher(), FunctionQuery::function_expression("fetch", FunctionKind::Async), - "fetch_expr", )), ); } diff --git a/tests/expr_mjs/mod.rs b/tests/expr_mjs/mod.rs index 0a32ddb..51cf40f 100644 --- a/tests/expr_mjs/mod.rs +++ b/tests/expr_mjs/mod.rs @@ -7,9 +7,9 @@ fn expr_mjs() { file!(), true, Config::new_single(InstrumentationConfig::new( + "fetch_expr", test_module_matcher(), FunctionQuery::function_expression("fetch", FunctionKind::Async), - "fetch_expr", )), ); } diff --git a/tests/index_cjs/mod.rs b/tests/index_cjs/mod.rs index 371426b..d387580 100644 --- a/tests/index_cjs/mod.rs +++ b/tests/index_cjs/mod.rs @@ -7,6 +7,7 @@ fn index_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "Undici_fetch", test_module_matcher(), FunctionQuery::ClassMethod { class_name: "Undici".to_string(), @@ -14,7 +15,6 @@ fn index_cjs() { kind: FunctionKind::Async, index: 2, }, - "Undici_fetch", )), ); } diff --git a/tests/multiple_class_method_cjs/mod.rs b/tests/multiple_class_method_cjs/mod.rs index d148f20..1319696 100644 --- a/tests/multiple_class_method_cjs/mod.rs +++ b/tests/multiple_class_method_cjs/mod.rs @@ -9,14 +9,14 @@ fn multiple_class_method_cjs() { Config::new( vec![ InstrumentationConfig::new( + "Undici_fetch1", test_module_matcher(), FunctionQuery::class_method("Undici", "fetch1", FunctionKind::Async), - "Undici_fetch1", ), InstrumentationConfig::new( + "Undici_fetch2", test_module_matcher(), FunctionQuery::class_method("Undici", "fetch2", FunctionKind::Async), - "Undici_fetch2", ), ], None, diff --git a/tests/multiple_load_cjs/mod.rs b/tests/multiple_load_cjs/mod.rs index 3abad2f..539ecf7 100644 --- a/tests/multiple_load_cjs/mod.rs +++ b/tests/multiple_load_cjs/mod.rs @@ -7,9 +7,9 @@ fn multiple_load_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "Undici_fetch", test_module_matcher(), FunctionQuery::class_method("Undici", "fetch", FunctionKind::Async), - "Undici_fetch", )), ); } diff --git a/tests/object_method_cjs/mod.rs b/tests/object_method_cjs/mod.rs index b69ab1e..ddc0468 100644 --- a/tests/object_method_cjs/mod.rs +++ b/tests/object_method_cjs/mod.rs @@ -7,9 +7,9 @@ fn object_method_cjs() { file!(), false, Config::new_single(InstrumentationConfig::new( + "Undici_fetch", test_module_matcher(), FunctionQuery::object_method("fetch", FunctionKind::Async), - "Undici_fetch", )), ); } diff --git a/tests/polyfill_cjs/mod.rs b/tests/polyfill_cjs/mod.rs index 5e4ed46..226879e 100644 --- a/tests/polyfill_cjs/mod.rs +++ b/tests/polyfill_cjs/mod.rs @@ -8,9 +8,9 @@ fn polyfill_cjs() { false, Config::new( vec![InstrumentationConfig::new( + "fetch_decl", test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), - "fetch_decl", )], Some("./polyfill.js".to_string()), ), diff --git a/tests/polyfill_mjs/mod.rs b/tests/polyfill_mjs/mod.rs index 13978a5..9031133 100644 --- a/tests/polyfill_mjs/mod.rs +++ b/tests/polyfill_mjs/mod.rs @@ -8,9 +8,9 @@ fn polyfill_mjs() { true, Config::new( vec![InstrumentationConfig::new( + "fetch_decl", test_module_matcher(), FunctionQuery::function_declaration("fetch", FunctionKind::Async), - "fetch_decl", )], Some("./polyfill.js".to_string()), ), From 23752224f792cc2f232fe683fb0242fd9a941d2b Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 30 Apr 2025 11:37:20 +0200 Subject: [PATCH 08/12] Test wasm in CI --- .github/workflows/ci.yml | 6 +++ .gitignore | 2 + tests/tests.mjs | 96 ++++++++++++++++++++++++++++------------ 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9fa7cb3..847086b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,6 +14,10 @@ jobs: run: rustup toolchain install ${{ env.RUST_VERSION }} --component clippy --component rustfmt && rustup default ${{ env.RUST_VERSION }} - name: Cache uses: Swatinem/rust-cache@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version-file: 'package.json' - name: Build run: cargo build --all - name: Run tests @@ -22,6 +26,8 @@ jobs: run: cargo clippy --all -- -D warnings - name: Run fmt run: cargo fmt --all -- --check + - name: Build and Test wasm + run: npm install && npm run build && npm test - name: Install license tool run: cargo install dd-rust-license-tool - name: Run license tool diff --git a/.gitignore b/.gitignore index 772ea44..56fbe1c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ target tests/*/instrumented.* pkg/ +node_modules/ +package-lock.json \ No newline at end of file diff --git a/tests/tests.mjs b/tests/tests.mjs index aa15dc2..3c6637f 100644 --- a/tests/tests.mjs +++ b/tests/tests.mjs @@ -1,36 +1,74 @@ -import test from "node:test"; import { create } from "../pkg/orchestrion_js.js"; +import * as assert from "node:assert"; -test("test the wasm", async (t) => { - const transformer = create([ - { - module: { name: "one", versionRange: ">=1", filePath: "index.js" }, - functionQuery: { className: "Up" }, - channelName: "up:constructor", +const instrumentor = create([ + { + channelName: "up:constructor", + module: { name: "one", versionRange: ">=1", filePath: "index.js" }, + functionQuery: { className: "Up" }, + }, + { + channelName: "up:fetch", + module: { name: "one", versionRange: ">=1", filePath: "index.js" }, + functionQuery: { + className: "Up", + methodName: "fetch", + kind: "Sync", }, - { - module: { name: "one", versionRange: ">=1", filePath: "index.js" }, - functionQuery: { - className: "Up", - methodName: "fetch", - kind: "Sync", - }, - channelName: "up:fetch", - }, - ]); + }, +]); - const matchedTransforms = transformer.getTransformer( - "one", - "1.0.0", - "index.js", - ); +const matchedTransforms = instrumentor.getTransformer( + "one", + "1.0.0", + "index.js", +); - t.assert.ok(matchedTransforms); +assert.ok(matchedTransforms); - const output = matchedTransforms.transform( - "export class Up { constructor() {console.log('constructor')} fetch() {console.log('fetch')} }", - true, - ); +const output = matchedTransforms.transform( + "export class Up { constructor() {console.log('constructor')} fetch() {console.log('fetch')} }", + true, +); - t.assert.snapshot(output); -}); +assert.strictEqual(output, `import { tracingChannel as tr_ch_apm_tracingChannel } from "diagnostics_channel"; +const tr_ch_apm$up:fetch = tr_ch_apm_tracingChannel("orchestrion:one:up:fetch"); +const tr_ch_apm$up:constructor = tr_ch_apm_tracingChannel("orchestrion:one:up:constructor"); +export class Up { + constructor(){ + const tr_ch_apm_ctx$up:constructor = { + arguments + }; + try { + if (tr_ch_apm$up:constructor.hasSubscribers) { + tr_ch_apm$up:constructor.start.publish(tr_ch_apm_ctx$up:constructor); + } + console.log('constructor'); + } catch (tr_ch_err) { + if (tr_ch_apm$up:constructor.hasSubscribers) { + tr_ch_apm_ctx$up:constructor.error = tr_ch_err; + try { + tr_ch_apm_ctx$up:constructor.self = this; + } catch (refErr) {} + tr_ch_apm$up:constructor.error.publish(tr_ch_apm_ctx$up:constructor); + } + throw tr_ch_err; + } finally{ + if (tr_ch_apm$up:constructor.hasSubscribers) { + tr_ch_apm_ctx$up:constructor.self = this; + tr_ch_apm$up:constructor.end.publish(tr_ch_apm_ctx$up:constructor); + } + } + } + fetch() { + const traced = ()=>{ + console.log('fetch'); + }; + if (!tr_ch_apm$up:fetch.hasSubscribers) return traced(); + return tr_ch_apm$up:fetch.traceSync(traced, { + arguments, + self: this + }); + } +} +`); From ddc0f246f461bf69ceec5aeb33260d20f5a7e9de Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Fri, 9 May 2025 13:49:54 +0200 Subject: [PATCH 09/12] Fix PR issues --- README.md | 21 ++++++++------------- package.json | 1 - src/wasm.rs | 6 +----- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 500ec06..f823fbb 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,16 @@ # Orchestion-JS -Orchestrion is a library for instrumenting Node.js libraries at build or load -time. +Orchestrion is a library for instrumenting Node.js libraries at build or load time. -It provides `VisitMut` implementations for SWC's AST nodes, which can be used to -insert tracing code into matching functions. It can be used in SWC plugins, or -anything else that mutates JavaScript ASTs using SWC. +It provides `VisitMut` implementations for SWC's AST nodes, which can be used to insert tracing code into matching functions. +It can be used in SWC plugins, or anything else that mutates JavaScript ASTs using SWC. -Orchestrion can also be built as a JavaScript module, which can be used from -Node.js. +Orchestrion can also be built as a JavaScript module, which can be used from Node.js. -To build the JavaScript module, ensure you have -[Rust installed](https://www.rust-lang.org/tools/install) and then run: - -```bash -npm install && npm run build -``` +To build the JavaScript module: +- Ensure you have [Rust installed](https://www.rust-lang.org/tools/install) +- Install the wasm toolchain `rustup target add wasm32-unknown-unknown --toolchain stable` +- Install dependencies and build the module `npm install && npm run build` ## Contributing diff --git a/package.json b/package.json index e12f7f3..603d28d 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "./pkg/orchestrion_js_bg.wasm", "./pkg/orchestrion_js.js", "./pkg/orchestrion_js.d.ts", - "LICENSE-3rdparty.csv", "LICENSE", "NOTICE" ], diff --git a/src/wasm.rs b/src/wasm.rs index 485eecc..9fcb5be 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -32,11 +32,7 @@ pub struct Transformer(InstrumentationVisitor); impl Transformer { #[wasm_bindgen] pub fn transform(&mut self, contents: &str, is_module: bool) -> Result { - let is_module = if is_module { - IsModule::Bool(true) - } else { - IsModule::Bool(false) - }; + let is_module = IsModule::Bool(is_module); self.0 .transform(contents, is_module) .map_err(|e| JsValue::from_str(&e.to_string())) From c1af536fd4ca3c0fb06ac444a32ddba9ce59118b Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 27 May 2025 17:58:28 +0200 Subject: [PATCH 10/12] Split wasm and serde features --- Cargo.toml | 9 ++++++--- src/config.rs | 29 ++++++++++++++++++++++------- src/function_query.rs | 22 +++++++++++++--------- tests/common/mod.rs | 3 --- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 51ec9a7..ca3d343 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,8 @@ license = "Apache-2.0" crate-type = ["cdylib", "rlib"] [features] -wasm = ["wasm-bindgen", "tsify", "getrandom"] +serde = ["serde/derive"] +wasm = ['serde', "wasm-bindgen", "tsify", "getrandom"] [dependencies] nodejs-semver = { version = "4", features = ["serde"] } @@ -17,9 +18,11 @@ swc = "21" swc_core = { version = "22", features = ["ecma_plugin_transform","ecma_quote"] } swc_ecma_parser = "11" swc_ecma_visit = { version = "8", features = ["path"] } -serde = { version = "1", features = ["derive"] } -# wasm +# serde feature +serde = { version = "1", features = ["derive"], optional = true } + +# wasm feature wasm-bindgen = { version = "0.2", optional = true } tsify = { version='0.5', features = ["js"], optional = true} # we need this to enable the js feature diff --git a/src/config.rs b/src/config.rs index b59a565..202b090 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,12 +4,15 @@ **/ use crate::function_query::FunctionQuery; use nodejs_semver::{Range, SemverError, Version}; -use serde::{Deserialize, Serialize}; use std::path::PathBuf; #[cfg_attr(feature = "wasm", derive(tsify::Tsify))] -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] +#[derive(Debug, Clone)] pub struct ModuleMatcher { pub name: String, pub version_range: Range, @@ -44,10 +47,17 @@ impl ModuleMatcher { } } -#[cfg_attr(feature = "wasm", derive(tsify::Tsify))] -#[derive(Debug, Clone, Serialize, Deserialize)] -#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))] -#[serde(rename_all = "camelCase")] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] +#[cfg_attr( + feature = "wasm", + derive(tsify::Tsify), + tsify(into_wasm_abi, from_wasm_abi) +)] +#[derive(Debug, Clone)] pub struct InstrumentationConfig { pub channel_name: String, pub module: ModuleMatcher, @@ -65,6 +75,11 @@ impl InstrumentationConfig { } } +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(rename_all = "camelCase") +)] #[derive(Debug, Clone)] pub struct Config { pub instrumentations: Vec, diff --git a/src/function_query.rs b/src/function_query.rs index e39a210..99d4d2f 100644 --- a/src/function_query.rs +++ b/src/function_query.rs @@ -2,7 +2,6 @@ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. * This product includes software developed at Datadog (/). Copyright 2025 Datadog, Inc. **/ -use serde::{Deserialize, Serialize}; use swc_core::ecma::ast::{FnDecl, FnExpr, Function}; #[derive(Debug, Clone)] @@ -13,7 +12,8 @@ pub(crate) enum FunctionType { } #[cfg_attr(feature = "wasm", derive(tsify::Tsify))] -#[derive(Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone)] pub enum FunctionKind { Sync, Async, @@ -43,8 +43,12 @@ impl FunctionKind { } #[cfg_attr(feature = "wasm", derive(tsify::Tsify))] -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(untagged, rename_all_fields = "camelCase")] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(untagged, rename_all_fields = "camelCase") +)] +#[derive(Debug, Clone)] pub enum FunctionQuery { // The order here matters because this enum is untagged, serde will try // choose the first variant that matches the data. @@ -52,34 +56,34 @@ pub enum FunctionQuery { class_name: String, method_name: String, kind: FunctionKind, - #[serde(default)] + #[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, ClassConstructor { class_name: String, - #[serde(default)] + #[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, ObjectMethod { method_name: String, kind: FunctionKind, - #[serde(default)] + #[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, FunctionDeclaration { function_name: String, kind: FunctionKind, - #[serde(default)] + #[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, FunctionExpression { expression_name: String, kind: FunctionKind, - #[serde(default)] + #[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "wasm", tsify(optional))] index: usize, }, diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 902d3d8..b86ed47 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -12,9 +12,6 @@ use swc::config::IsModule; static TEST_MODULE_NAME: &str = "undici"; static TEST_MODULE_PATH: &str = "index.mjs"; -static TEST_MODULE_NAME: &'static str = "undici"; -static TEST_MODULE_PATH: &'static str = "index.mjs"; - pub fn transpile_and_test(test_file: &str, mjs: bool, config: Config) { let test_file = PathBuf::from(test_file); let test_dir = test_file.parent().expect("Couldn't find test directory"); From dc89f9fcf7e4bad72bff0f0ab1afce743c2d7532 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sun, 1 Jun 2025 15:28:35 +0100 Subject: [PATCH 11/12] Run Ci for pull requests --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 847086b..814a68f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ name: CI -on: [push] +on: [push, pull_request] env: CARGO_TERM_COLOR: always RUST_VERSION: 1.84.1 From 17c97d35dd5d89f1d3eeaa6abafc9b6399b1a772 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 2 Jun 2025 11:00:09 +0100 Subject: [PATCH 12/12] Remove unused snapshot --- tests/tests.mjs.snapshot | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 tests/tests.mjs.snapshot diff --git a/tests/tests.mjs.snapshot b/tests/tests.mjs.snapshot deleted file mode 100644 index e35ec6e..0000000 --- a/tests/tests.mjs.snapshot +++ /dev/null @@ -1,3 +0,0 @@ -exports[`test the wasm 1`] = ` -"import { tracingChannel as tr_ch_apm_tracingChannel } from \\"diagnostics_channel\\";\\nconst tr_ch_apm$up:fetch = tr_ch_apm_tracingChannel(\\"orchestrion:one:up:fetch\\");\\nconst tr_ch_apm$up:constructor = tr_ch_apm_tracingChannel(\\"orchestrion:one:up:constructor\\");\\nexport class Up {\\n constructor(){\\n const tr_ch_apm_ctx$up:constructor = {\\n arguments\\n };\\n try {\\n if (tr_ch_apm$up:constructor.hasSubscribers) {\\n tr_ch_apm$up:constructor.start.publish(tr_ch_apm_ctx$up:constructor);\\n }\\n console.log('constructor');\\n } catch (tr_ch_err) {\\n if (tr_ch_apm$up:constructor.hasSubscribers) {\\n tr_ch_apm_ctx$up:constructor.error = tr_ch_err;\\n try {\\n tr_ch_apm_ctx$up:constructor.self = this;\\n } catch (refErr) {}\\n tr_ch_apm$up:constructor.error.publish(tr_ch_apm_ctx$up:constructor);\\n }\\n throw tr_ch_err;\\n } finally{\\n if (tr_ch_apm$up:constructor.hasSubscribers) {\\n tr_ch_apm_ctx$up:constructor.self = this;\\n tr_ch_apm$up:constructor.end.publish(tr_ch_apm_ctx$up:constructor);\\n }\\n }\\n }\\n fetch() {\\n const traced = ()=>{\\n console.log('fetch');\\n };\\n if (!tr_ch_apm$up:fetch.hasSubscribers) return traced();\\n return tr_ch_apm$up:fetch.traceSync(traced, {\\n arguments,\\n self: this\\n });\\n }\\n}\\n" -`;