|
| 1 | +/* eslint-disable max-lines-per-function */ |
1 | 2 | import { getContext } from "../agent/Context";
|
2 | 3 | import { Hooks } from "../agent/hooks/Hooks";
|
| 4 | +import type { PackageFunctionInstrumentationInstruction } from "../agent/hooks/instrumentation/types"; |
3 | 5 | import { InterceptorResult } from "../agent/hooks/InterceptorResult";
|
4 | 6 | import { wrapExport } from "../agent/hooks/wrapExport";
|
5 | 7 | import { Wrapper } from "../agent/Wrapper";
|
@@ -65,26 +67,66 @@ export class BetterSQLite3 implements Wrapper {
|
65 | 67 | const sqlFunctions = ["prepare", "exec", "pragma"];
|
66 | 68 | const fsPathFunctions = ["backup", "loadExtension"];
|
67 | 69 |
|
68 |
| - hooks |
| 70 | + const pkg = hooks |
69 | 71 | .addPackage("better-sqlite3")
|
70 |
| - .withVersion("^11.0.0 || ^10.0.0 || ^9.0.0 || ^8.0.0") |
71 |
| - .onRequire((exports, pkgInfo) => { |
72 |
| - for (const func of sqlFunctions) { |
73 |
| - wrapExport(exports.prototype, func, pkgInfo, { |
74 |
| - kind: "sql_op", |
75 |
| - inspectArgs: (args) => { |
76 |
| - return this.inspectQuery(`better-sqlite3.${func}`, args); |
77 |
| - }, |
78 |
| - }); |
79 |
| - } |
80 |
| - for (const func of fsPathFunctions) { |
81 |
| - wrapExport(exports.prototype, func, pkgInfo, { |
82 |
| - kind: "sql_op", |
83 |
| - inspectArgs: (args) => { |
84 |
| - return this.inspectPath(`better-sqlite3.${func}`, args); |
85 |
| - }, |
86 |
| - }); |
87 |
| - } |
88 |
| - }); |
| 72 | + .withVersion("^11.0.0 || ^10.0.0 || ^9.0.0 || ^8.0.0"); |
| 73 | + |
| 74 | + pkg.onRequire((exports, pkgInfo) => { |
| 75 | + for (const func of sqlFunctions) { |
| 76 | + wrapExport(exports.prototype, func, pkgInfo, { |
| 77 | + kind: "sql_op", |
| 78 | + inspectArgs: (args) => { |
| 79 | + return this.inspectQuery(`better-sqlite3.${func}`, args); |
| 80 | + }, |
| 81 | + }); |
| 82 | + } |
| 83 | + for (const func of fsPathFunctions) { |
| 84 | + wrapExport(exports.prototype, func, pkgInfo, { |
| 85 | + kind: "fs_op", |
| 86 | + inspectArgs: (args) => { |
| 87 | + return this.inspectPath(`better-sqlite3.${func}`, args); |
| 88 | + }, |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + const wrapperFunctionsInstructions: PackageFunctionInstrumentationInstruction[] = |
| 94 | + sqlFunctions.map((func) => ({ |
| 95 | + name: `exports.${func}`, |
| 96 | + operationKind: "sql_op", |
| 97 | + nodeType: "FunctionAssignment", |
| 98 | + inspectArgs: (args) => { |
| 99 | + return this.inspectQuery(`better-sqlite3.${func}`, args); |
| 100 | + }, |
| 101 | + })); |
| 102 | + |
| 103 | + wrapperFunctionsInstructions.push({ |
| 104 | + name: "exports.loadExtension", |
| 105 | + operationKind: "fs_op", |
| 106 | + nodeType: "FunctionAssignment", |
| 107 | + inspectArgs: (args) => { |
| 108 | + return this.inspectPath("better-sqlite3.loadExtension", args); |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + pkg.addFileInstrumentation({ |
| 113 | + path: "lib/methods/wrappers.js", |
| 114 | + functions: wrapperFunctionsInstructions, |
| 115 | + }); |
| 116 | + |
| 117 | + // Add backup instrumentation |
| 118 | + pkg.addFileInstrumentation({ |
| 119 | + path: "lib/methods/backup.js", |
| 120 | + functions: [ |
| 121 | + { |
| 122 | + name: "module.exports", |
| 123 | + operationKind: "fs_op", |
| 124 | + nodeType: "FunctionAssignment", |
| 125 | + inspectArgs: (args) => { |
| 126 | + return this.inspectPath("better-sqlite3.backup", args); |
| 127 | + }, |
| 128 | + }, |
| 129 | + ], |
| 130 | + }); |
89 | 131 | }
|
90 | 132 | }
|
0 commit comments