Skip to content

Commit 0895bed

Browse files
committed
Instrument better-sqlite3
1 parent 796a7d7 commit 0895bed

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

library/sinks/BetterSQLite3.ts

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/* eslint-disable max-lines-per-function */
12
import { getContext } from "../agent/Context";
23
import { Hooks } from "../agent/hooks/Hooks";
4+
import type { PackageFunctionInstrumentationInstruction } from "../agent/hooks/instrumentation/types";
35
import { InterceptorResult } from "../agent/hooks/InterceptorResult";
46
import { wrapExport } from "../agent/hooks/wrapExport";
57
import { Wrapper } from "../agent/Wrapper";
@@ -65,26 +67,66 @@ export class BetterSQLite3 implements Wrapper {
6567
const sqlFunctions = ["prepare", "exec", "pragma"];
6668
const fsPathFunctions = ["backup", "loadExtension"];
6769

68-
hooks
70+
const pkg = hooks
6971
.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+
});
89131
}
90132
}

0 commit comments

Comments
 (0)