@watchtowerobservability/node 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/README.md +186 -3
  2. package/dist/cjs/appsec.d.ts +95 -0
  3. package/dist/cjs/appsec.js +206 -0
  4. package/dist/cjs/iast.d.ts +27 -0
  5. package/dist/cjs/iast.js +138 -0
  6. package/dist/cjs/index.d.ts +3 -0
  7. package/dist/cjs/index.js +14 -1
  8. package/dist/cjs/integrations/amqp.d.ts +13 -0
  9. package/dist/cjs/integrations/amqp.js +96 -0
  10. package/dist/cjs/integrations/bullmq.d.ts +19 -0
  11. package/dist/cjs/integrations/bullmq.js +83 -0
  12. package/dist/cjs/integrations/cache.d.ts +13 -0
  13. package/dist/cjs/integrations/cache.js +51 -0
  14. package/dist/cjs/integrations/db.d.ts +62 -0
  15. package/dist/cjs/integrations/db.js +288 -0
  16. package/dist/cjs/integrations/duckdb.d.ts +22 -0
  17. package/dist/cjs/integrations/duckdb.js +66 -0
  18. package/dist/cjs/integrations/graphile-worker.d.ts +22 -0
  19. package/dist/cjs/integrations/graphile-worker.js +50 -0
  20. package/dist/cjs/integrations/level.d.ts +18 -0
  21. package/dist/cjs/integrations/level.js +71 -0
  22. package/dist/cjs/integrations/lmdb.d.ts +26 -0
  23. package/dist/cjs/integrations/lmdb.js +78 -0
  24. package/dist/cjs/integrations/messaging.d.ts +17 -0
  25. package/dist/cjs/integrations/messaging.js +73 -0
  26. package/dist/cjs/integrations/mqtt.d.ts +7 -0
  27. package/dist/cjs/integrations/mqtt.js +41 -0
  28. package/dist/cjs/integrations/nats.d.ts +17 -0
  29. package/dist/cjs/integrations/nats.js +73 -0
  30. package/dist/cjs/integrations/orm.d.ts +47 -0
  31. package/dist/cjs/integrations/orm.js +93 -0
  32. package/dist/cjs/integrations/pg-boss.d.ts +8 -0
  33. package/dist/cjs/integrations/pg-boss.js +63 -0
  34. package/dist/cjs/integrations/pools.d.ts +41 -0
  35. package/dist/cjs/integrations/pools.js +94 -0
  36. package/dist/cjs/integrations/postgres.d.ts +12 -0
  37. package/dist/cjs/integrations/postgres.js +92 -0
  38. package/dist/cjs/integrations/sqlite.d.ts +36 -0
  39. package/dist/cjs/integrations/sqlite.js +125 -0
  40. package/dist/cjs/pprof.d.ts +6 -0
  41. package/dist/cjs/pprof.js +88 -0
  42. package/dist/cjs/profiling.d.ts +4 -1
  43. package/dist/cjs/profiling.js +18 -8
  44. package/dist/cjs/sql.d.ts +19 -0
  45. package/dist/cjs/sql.js +332 -0
  46. package/dist/cjs/test-plan.d.ts +10 -1
  47. package/dist/cjs/test-plan.js +91 -1
  48. package/dist/esm/appsec.d.ts +95 -0
  49. package/dist/esm/appsec.js +193 -0
  50. package/dist/esm/iast.d.ts +27 -0
  51. package/dist/esm/iast.js +130 -0
  52. package/dist/esm/index.d.ts +3 -0
  53. package/dist/esm/index.js +14 -1
  54. package/dist/esm/integrations/amqp.d.ts +13 -0
  55. package/dist/esm/integrations/amqp.js +92 -0
  56. package/dist/esm/integrations/bullmq.d.ts +19 -0
  57. package/dist/esm/integrations/bullmq.js +79 -0
  58. package/dist/esm/integrations/cache.d.ts +13 -0
  59. package/dist/esm/integrations/cache.js +46 -0
  60. package/dist/esm/integrations/db.d.ts +62 -0
  61. package/dist/esm/integrations/db.js +239 -0
  62. package/dist/esm/integrations/duckdb.d.ts +22 -0
  63. package/dist/esm/integrations/duckdb.js +29 -0
  64. package/dist/esm/integrations/graphile-worker.d.ts +22 -0
  65. package/dist/esm/integrations/graphile-worker.js +46 -0
  66. package/dist/esm/integrations/level.d.ts +18 -0
  67. package/dist/esm/integrations/level.js +35 -0
  68. package/dist/esm/integrations/lmdb.d.ts +26 -0
  69. package/dist/esm/integrations/lmdb.js +42 -0
  70. package/dist/esm/integrations/messaging.d.ts +17 -0
  71. package/dist/esm/integrations/messaging.js +64 -0
  72. package/dist/esm/integrations/mqtt.d.ts +7 -0
  73. package/dist/esm/integrations/mqtt.js +38 -0
  74. package/dist/esm/integrations/nats.d.ts +17 -0
  75. package/dist/esm/integrations/nats.js +70 -0
  76. package/dist/esm/integrations/orm.d.ts +47 -0
  77. package/dist/esm/integrations/orm.js +86 -0
  78. package/dist/esm/integrations/pg-boss.d.ts +8 -0
  79. package/dist/esm/integrations/pg-boss.js +60 -0
  80. package/dist/esm/integrations/pools.d.ts +41 -0
  81. package/dist/esm/integrations/pools.js +89 -0
  82. package/dist/esm/integrations/postgres.d.ts +12 -0
  83. package/dist/esm/integrations/postgres.js +55 -0
  84. package/dist/esm/integrations/sqlite.d.ts +36 -0
  85. package/dist/esm/integrations/sqlite.js +84 -0
  86. package/dist/esm/pprof.d.ts +6 -0
  87. package/dist/esm/pprof.js +85 -0
  88. package/dist/esm/profiling.d.ts +4 -1
  89. package/dist/esm/profiling.js +17 -8
  90. package/dist/esm/sql.d.ts +19 -0
  91. package/dist/esm/sql.js +324 -0
  92. package/dist/esm/test-plan.d.ts +10 -1
  93. package/dist/esm/test-plan.js +89 -1
  94. package/package.json +171 -1
@@ -10,13 +10,17 @@
10
10
  // - a test the project has never seen is retried up to `efd_retries` times (early flake detection).
11
11
  // The reporters upload the results as JUnit (`POST /v1/projects/{prj}/test-runs`) when
12
12
  // WT_CI_RUN_ID is set, which feeds flaky-test detection and auto-quarantine.
13
+ // With WT_TIA_RECORD=1 (and WT_API_KEY) each test file's istanbul coverage (Vitest
14
+ // `coverage.provider: 'istanbul'`, Jest `--coverage`) is uploaded as the per-test coverage map
15
+ // (`PUT /v1/projects/{prj}/tests/coverage-map`): every test of the file gets the files the file
16
+ // executed plus the test file itself, which Test Impact Analysis then uses to skip tests.
13
17
  //
14
18
  // Ids are `<suite>::<file relative to cwd> > <describe>... > <test>`, suite = WT_TEST_SUITE or
15
19
  // the framework name. Vitest:
16
20
  // globalSetup: ['./wt-global.mjs'] // export { default } from '@watchtowerobservability/node/test-plan'
17
21
  // setupFiles: ['./wt-setup.ts'] // import { beforeAll, afterEach } from 'vitest';
18
22
  // // import { vitestSetup } from '@watchtowerobservability/node/test-plan';
19
- // // vitestSetup({ beforeAll, afterEach });
23
+ // // vitestSetup({ beforeAll, afterEach, afterAll });
20
24
  // Jest: globalSetup as above, and testEnvironment: './wt-env.js' with
21
25
  // module.exports = require('@watchtowerobservability/node/test-plan').jestEnvironment(require('jest-environment-node').TestEnvironment);
22
26
  import { execFileSync } from 'node:child_process';
@@ -120,8 +124,24 @@ export function decider(plan) {
120
124
  return d;
121
125
  };
122
126
  }
127
+ /** Ids of the tests in `t` that ran (a skipped test executed nothing, so it gets no coverage). */
128
+ function ranIds(t, suite, file, names, out) {
129
+ for (const c of t.tasks || []) {
130
+ const ran = c.mode !== 'skip' && c.mode !== 'todo' && !!c.result?.state && c.result.state !== 'skip';
131
+ if (c.tasks)
132
+ ranIds(c, suite, file, [...names, c.name], out);
133
+ else if (ran)
134
+ out.push(testId(suite, file, [...names, c.name]));
135
+ }
136
+ return out;
137
+ }
123
138
  /** Applies the plan in a Vitest setup file. Decisions go to `task.meta.wt` for the reporter. */
124
139
  export function vitestSetup(hooks) {
140
+ if (process.env.WT_TIA_RECORD === '1' && hooks.afterAll) {
141
+ const suite = suiteName('vitest');
142
+ const g = globalThis;
143
+ hooks.afterAll((file) => uploadCoverage(file.filepath, ranIds(file, suite, file.filepath, [], []), g.__VITEST_COVERAGE__ || g.__coverage__));
144
+ }
125
145
  const plan = loadPlan();
126
146
  if (!plan)
127
147
  return;
@@ -165,6 +185,7 @@ export function jestEnvironment(Base) {
165
185
  wtDecide = decider(loadPlan());
166
186
  wtSuite = suiteName('jest');
167
187
  wtRetrySaved = [];
188
+ wtRan = [];
168
189
  // any[]: see JestEnvBase
169
190
  constructor(...args) {
170
191
  super(...args);
@@ -180,6 +201,12 @@ export function jestEnvironment(Base) {
180
201
  if (super.handleTestEvent)
181
202
  await super.handleTestEvent(event, state);
182
203
  const t = event.test;
204
+ if (event.name === 'test_done' && t && process.env.WT_TIA_RECORD === '1')
205
+ this.wtRan.push(this.wtId(t));
206
+ if (event.name === 'run_finish' && process.env.WT_TIA_RECORD === '1') {
207
+ await uploadCoverage(this.wtFile, this.wtRan, this.global.__coverage__);
208
+ return;
209
+ }
183
210
  if (event.name === 'test_start' && t) {
184
211
  if (this.wtDecide(this.wtId(t)).skip)
185
212
  t.mode = 'skip';
@@ -246,3 +273,64 @@ export async function uploadResults(client, suite, tests) {
246
273
  return false;
247
274
  }
248
275
  }
276
+ // ---------------------------------------------------------------- Test Impact Analysis recording
277
+ let rootCache;
278
+ /** The repository top level (git diff paths are relative to it), else the working directory. */
279
+ function repoRoot() {
280
+ if (rootCache === undefined) {
281
+ try {
282
+ rootCache = execFileSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8', timeout: 10_000 }).trim() || process.cwd();
283
+ }
284
+ catch (_e) {
285
+ rootCache = process.cwd();
286
+ }
287
+ }
288
+ return rootCache;
289
+ }
290
+ const repoRel = (f, root) => relative(root, f).split(sep).join('/');
291
+ /** Repository-relative files with an executed statement in an istanbul coverage object. */
292
+ export function coveredFiles(cov, root = repoRoot()) {
293
+ const out = new Set();
294
+ if (!cov || typeof cov !== 'object')
295
+ return [];
296
+ for (const [key, fc] of Object.entries(cov)) {
297
+ if (!fc || !fc.s || !Object.values(fc.s).some((n) => n > 0))
298
+ continue;
299
+ const f = repoRel(fc.path || key, root);
300
+ if (f && !f.startsWith('..'))
301
+ out.add(f);
302
+ }
303
+ return [...out].sort();
304
+ }
305
+ /**
306
+ * Uploads the coverage of one test file for its tests (`suite::rest` ids); never throws.
307
+ * Needs WT_API_URL, WT_PROJECT_ID and WT_API_KEY (the coverage map needs a project API key).
308
+ */
309
+ export async function uploadCoverage(file, ids, cov, fetchImpl = globalThis.fetch) {
310
+ const env = process.env;
311
+ const [api, project, key] = [env.WT_API_URL, env.WT_PROJECT_ID, env.WT_API_KEY];
312
+ if (!api || !project || !key || ids.length === 0)
313
+ return false;
314
+ const root = repoRoot();
315
+ const files = coveredFiles(cov, root);
316
+ if (file) {
317
+ const self = repoRel(file, root);
318
+ if (self && !self.startsWith('..') && !files.includes(self))
319
+ files.push(self);
320
+ }
321
+ const tests = [...new Set(ids)].slice(0, 20_000).map((id) => {
322
+ const i = id.indexOf('::');
323
+ return { suite: i < 0 ? '' : id.slice(0, i), test: i < 0 ? id : id.slice(i + 2), files: files.slice(0, 2000) };
324
+ });
325
+ try {
326
+ const res = await fetchImpl(`${api.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(project)}/tests/coverage-map`, {
327
+ method: 'PUT',
328
+ headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json' },
329
+ body: JSON.stringify({ sha: (env.WT_SHA || env.GITHUB_SHA || env.CI_COMMIT_SHA || '').slice(0, 64), tests }),
330
+ });
331
+ return res.ok;
332
+ }
333
+ catch (_e) {
334
+ return false;
335
+ }
336
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watchtowerobservability/node",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/watchtowerobservability/watchtower-node.git"
@@ -27,6 +27,26 @@
27
27
  "default": "./dist/cjs/index.js"
28
28
  }
29
29
  },
30
+ "./appsec": {
31
+ "import": {
32
+ "types": "./dist/esm/appsec.d.ts",
33
+ "default": "./dist/esm/appsec.js"
34
+ },
35
+ "require": {
36
+ "types": "./dist/cjs/appsec.d.ts",
37
+ "default": "./dist/cjs/appsec.js"
38
+ }
39
+ },
40
+ "./iast": {
41
+ "import": {
42
+ "types": "./dist/esm/iast.d.ts",
43
+ "default": "./dist/esm/iast.js"
44
+ },
45
+ "require": {
46
+ "types": "./dist/cjs/iast.d.ts",
47
+ "default": "./dist/cjs/iast.js"
48
+ }
49
+ },
30
50
  "./probes": {
31
51
  "import": {
32
52
  "types": "./dist/esm/probes.d.ts",
@@ -127,6 +147,156 @@
127
147
  "default": "./dist/cjs/integrations/prisma.js"
128
148
  }
129
149
  },
150
+ "./bullmq": {
151
+ "import": {
152
+ "types": "./dist/esm/integrations/bullmq.d.ts",
153
+ "default": "./dist/esm/integrations/bullmq.js"
154
+ },
155
+ "require": {
156
+ "types": "./dist/cjs/integrations/bullmq.d.ts",
157
+ "default": "./dist/cjs/integrations/bullmq.js"
158
+ }
159
+ },
160
+ "./postgres": {
161
+ "import": {
162
+ "types": "./dist/esm/integrations/postgres.d.ts",
163
+ "default": "./dist/esm/integrations/postgres.js"
164
+ },
165
+ "require": {
166
+ "types": "./dist/cjs/integrations/postgres.d.ts",
167
+ "default": "./dist/cjs/integrations/postgres.js"
168
+ }
169
+ },
170
+ "./sql": {
171
+ "import": {
172
+ "types": "./dist/esm/sql.d.ts",
173
+ "default": "./dist/esm/sql.js"
174
+ },
175
+ "require": {
176
+ "types": "./dist/cjs/sql.d.ts",
177
+ "default": "./dist/cjs/sql.js"
178
+ }
179
+ },
180
+ "./sqlite": {
181
+ "import": {
182
+ "types": "./dist/esm/integrations/sqlite.d.ts",
183
+ "default": "./dist/esm/integrations/sqlite.js"
184
+ },
185
+ "require": {
186
+ "types": "./dist/cjs/integrations/sqlite.d.ts",
187
+ "default": "./dist/cjs/integrations/sqlite.js"
188
+ }
189
+ },
190
+ "./duckdb": {
191
+ "import": {
192
+ "types": "./dist/esm/integrations/duckdb.d.ts",
193
+ "default": "./dist/esm/integrations/duckdb.js"
194
+ },
195
+ "require": {
196
+ "types": "./dist/cjs/integrations/duckdb.d.ts",
197
+ "default": "./dist/cjs/integrations/duckdb.js"
198
+ }
199
+ },
200
+ "./lmdb": {
201
+ "import": {
202
+ "types": "./dist/esm/integrations/lmdb.d.ts",
203
+ "default": "./dist/esm/integrations/lmdb.js"
204
+ },
205
+ "require": {
206
+ "types": "./dist/cjs/integrations/lmdb.d.ts",
207
+ "default": "./dist/cjs/integrations/lmdb.js"
208
+ }
209
+ },
210
+ "./level": {
211
+ "import": {
212
+ "types": "./dist/esm/integrations/level.d.ts",
213
+ "default": "./dist/esm/integrations/level.js"
214
+ },
215
+ "require": {
216
+ "types": "./dist/cjs/integrations/level.d.ts",
217
+ "default": "./dist/cjs/integrations/level.js"
218
+ }
219
+ },
220
+ "./cache": {
221
+ "import": {
222
+ "types": "./dist/esm/integrations/cache.d.ts",
223
+ "default": "./dist/esm/integrations/cache.js"
224
+ },
225
+ "require": {
226
+ "types": "./dist/cjs/integrations/cache.d.ts",
227
+ "default": "./dist/cjs/integrations/cache.js"
228
+ }
229
+ },
230
+ "./pools": {
231
+ "import": {
232
+ "types": "./dist/esm/integrations/pools.d.ts",
233
+ "default": "./dist/esm/integrations/pools.js"
234
+ },
235
+ "require": {
236
+ "types": "./dist/cjs/integrations/pools.d.ts",
237
+ "default": "./dist/cjs/integrations/pools.js"
238
+ }
239
+ },
240
+ "./orm": {
241
+ "import": {
242
+ "types": "./dist/esm/integrations/orm.d.ts",
243
+ "default": "./dist/esm/integrations/orm.js"
244
+ },
245
+ "require": {
246
+ "types": "./dist/cjs/integrations/orm.d.ts",
247
+ "default": "./dist/cjs/integrations/orm.js"
248
+ }
249
+ },
250
+ "./amqp": {
251
+ "import": {
252
+ "types": "./dist/esm/integrations/amqp.d.ts",
253
+ "default": "./dist/esm/integrations/amqp.js"
254
+ },
255
+ "require": {
256
+ "types": "./dist/cjs/integrations/amqp.d.ts",
257
+ "default": "./dist/cjs/integrations/amqp.js"
258
+ }
259
+ },
260
+ "./nats": {
261
+ "import": {
262
+ "types": "./dist/esm/integrations/nats.d.ts",
263
+ "default": "./dist/esm/integrations/nats.js"
264
+ },
265
+ "require": {
266
+ "types": "./dist/cjs/integrations/nats.d.ts",
267
+ "default": "./dist/cjs/integrations/nats.js"
268
+ }
269
+ },
270
+ "./mqtt": {
271
+ "import": {
272
+ "types": "./dist/esm/integrations/mqtt.d.ts",
273
+ "default": "./dist/esm/integrations/mqtt.js"
274
+ },
275
+ "require": {
276
+ "types": "./dist/cjs/integrations/mqtt.d.ts",
277
+ "default": "./dist/cjs/integrations/mqtt.js"
278
+ }
279
+ },
280
+ "./pg-boss": {
281
+ "import": {
282
+ "types": "./dist/esm/integrations/pg-boss.d.ts",
283
+ "default": "./dist/esm/integrations/pg-boss.js"
284
+ },
285
+ "require": {
286
+ "types": "./dist/cjs/integrations/pg-boss.d.ts",
287
+ "default": "./dist/cjs/integrations/pg-boss.js"
288
+ }
289
+ },
290
+ "./graphile-worker": {
291
+ "import": {
292
+ "types": "./dist/esm/integrations/graphile-worker.d.ts",
293
+ "default": "./dist/esm/integrations/graphile-worker.js"
294
+ },
295
+ "require": {
296
+ "types": "./dist/cjs/integrations/graphile-worker.d.ts",
297
+ "default": "./dist/cjs/integrations/graphile-worker.js"
298
+ }
299
+ },
130
300
  "./lambda": {
131
301
  "import": {
132
302
  "types": "./dist/esm/integrations/lambda.d.ts",