@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
@@ -0,0 +1,332 @@
1
+ "use strict";
2
+ // Shared SQL normaliser and statement hash. A byte-for-byte port of the worker's
3
+ // services/worker/src/sqlnorm.rs (`normalize_with`, xxh3_64), so `db.query.hash` from the SDK equals
4
+ // the `wt.db.query_hash` the server stamps. Algorithm: docs/product/instrumentation/sql-normalization.md;
5
+ // vectors: testdata/sql_normalize_vectors.json. Change both sides together.
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.isPgDialect = exports.MAX_STATEMENT_BYTES = void 0;
8
+ exports.normalizeSql = normalizeSql;
9
+ exports.normalizeAndHash = normalizeAndHash;
10
+ exports.querySummary = querySummary;
11
+ exports.xxh3 = xxh3;
12
+ /** Longest statement looked at, in UTF-8 bytes; the rest is dropped. */
13
+ exports.MAX_STATEMENT_BYTES = 8 * 1024;
14
+ const MAX_DOLLAR_TAG = 32;
15
+ const ALNUM = /^[\p{Alphabetic}\p{N}]$/u; // Rust char::is_alphanumeric
16
+ const SPACE = /^\p{White_Space}$/u; // Rust char::is_whitespace
17
+ const isAlnum = (c) => ALNUM.test(c);
18
+ const isWord = (c) => c === '_' || c === '$' || ALNUM.test(c);
19
+ const isDigit = (c) => c !== undefined && c >= '0' && c <= '9';
20
+ /** Code points of the first 8 KiB of UTF-8, cut back to a whole character. */
21
+ function cap(s) {
22
+ const out = [];
23
+ let bytes = 0;
24
+ for (const c of s) {
25
+ const cp = c.codePointAt(0);
26
+ bytes += cp < 0x80 ? 1 : cp < 0x800 ? 2 : cp < 0x10000 ? 3 : 4;
27
+ if (bytes > exports.MAX_STATEMENT_BYTES)
28
+ break;
29
+ out.push(c);
30
+ }
31
+ return out;
32
+ }
33
+ const startsWith = (cs, at, tag) => tag.every((t, k) => cs[at + k] === t);
34
+ function dollarQuoted(cs, i) {
35
+ let tagLen = -1;
36
+ for (let k = 0; k < MAX_DOLLAR_TAG + 1 && i + 1 + k < cs.length; k++) {
37
+ if (cs[i + 1 + k] === '$') {
38
+ tagLen = k;
39
+ break;
40
+ }
41
+ }
42
+ if (tagLen < 0)
43
+ return undefined;
44
+ const tag = cs.slice(i, i + tagLen + 2);
45
+ if (!tag.slice(1, tagLen + 1).every((c) => isAlnum(c) || c === '_') || isDigit(tag[1]))
46
+ return undefined;
47
+ for (let j = i + tag.length; j < cs.length; j++)
48
+ if (startsWith(cs, j, tag))
49
+ return j + tag.length;
50
+ return cs.length;
51
+ }
52
+ function token(cs, i, pg, out) {
53
+ const c = cs[i];
54
+ if (c === "'") {
55
+ i++;
56
+ while (i < cs.length) {
57
+ if (cs[i] === "'") {
58
+ if (cs[i + 1] !== "'")
59
+ break;
60
+ i++;
61
+ }
62
+ i++;
63
+ }
64
+ out.push('?');
65
+ // a quote right after a backslash is ambiguous (MySQL escapes): mask to the end
66
+ return i < cs.length && cs[i - 1] === '\\' ? cs.length : i + 1;
67
+ }
68
+ if (c === '"' || c === '`') {
69
+ let close = cs.indexOf(c, i + 1);
70
+ if (close < 0)
71
+ close = cs.length;
72
+ const body = cs.slice(i + 1, close);
73
+ const ident = c === '"' && pg && close < cs.length && body.length >= 1 && body.length <= 64
74
+ && body.every((b) => isAlnum(b) || b === '_' || b === '$');
75
+ if (ident)
76
+ out.push(...cs.slice(i, close + 1));
77
+ else
78
+ out.push('?');
79
+ return close + 1;
80
+ }
81
+ if (c === '$') {
82
+ if (isDigit(cs[i + 1])) {
83
+ i++;
84
+ while (isDigit(cs[i]))
85
+ i++;
86
+ out.push('?');
87
+ return i;
88
+ }
89
+ const end = dollarQuoted(cs, i);
90
+ if (end !== undefined) {
91
+ out.push('?');
92
+ return end;
93
+ }
94
+ }
95
+ if (isDigit(c)) {
96
+ while (i < cs.length && (isWord(cs[i]) || cs[i] === '.'))
97
+ i++;
98
+ out.push('?');
99
+ }
100
+ else if (isWord(c)) {
101
+ while (i < cs.length && isWord(cs[i]))
102
+ out.push(cs[i++]);
103
+ }
104
+ else {
105
+ out.push(c);
106
+ i++;
107
+ }
108
+ return i;
109
+ }
110
+ const enc = new TextEncoder();
111
+ const dec = new TextDecoder();
112
+ const SP = 0x20;
113
+ /** After `in`: optional spaces, `(`, `?` items split by commas, `)`. Returns [open, end]. */
114
+ function inList(b, i) {
115
+ const skip = () => { while (b[i] === SP)
116
+ i++; };
117
+ skip();
118
+ const open = i;
119
+ if (b[i] !== 0x28)
120
+ return undefined;
121
+ i++;
122
+ for (;;) {
123
+ skip();
124
+ if (b[i] !== 0x3f)
125
+ return undefined;
126
+ i++;
127
+ skip();
128
+ if (b[i] === 0x2c)
129
+ i++;
130
+ else if (b[i] === 0x29)
131
+ return [open, i + 1];
132
+ else
133
+ return undefined;
134
+ }
135
+ }
136
+ /** `in (?, ?, ?)` -> `in (?+)`. On UTF-8 bytes like the Rust: the word check reads the previous byte as Latin-1. */
137
+ function collapseIn(s) {
138
+ const out = [];
139
+ let i = 0;
140
+ while (i < s.length) {
141
+ const atWord = i === 0 || !isWord(String.fromCharCode(s[i - 1]));
142
+ const span = atWord && i + 2 <= s.length && (s[i] | 0x20) === 0x69 && (s[i + 1] | 0x20) === 0x6e
143
+ ? inList(s, i + 2) : undefined;
144
+ if (span) {
145
+ for (let k = i; k < span[0]; k++)
146
+ out.push(s[k]);
147
+ out.push(0x28, 0x3f, 0x2b, 0x29);
148
+ i = span[1];
149
+ }
150
+ else {
151
+ out.push(s[i++]);
152
+ }
153
+ }
154
+ return Uint8Array.from(out);
155
+ }
156
+ function normalizeBytes(stmt, pg) {
157
+ const cs = cap(stmt);
158
+ const out = [];
159
+ let space = false;
160
+ let i = 0;
161
+ while (i < cs.length) {
162
+ const c = cs[i];
163
+ const next = cs[i + 1];
164
+ if (SPACE.test(c)) {
165
+ space = true;
166
+ i++;
167
+ }
168
+ else if (c === '/' && next === '*') {
169
+ let depth = 1;
170
+ i += 2;
171
+ while (i < cs.length && depth > 0) {
172
+ if (cs[i] === '/' && cs[i + 1] === '*') {
173
+ depth++;
174
+ i += 2;
175
+ }
176
+ else if (cs[i] === '*' && cs[i + 1] === '/') {
177
+ depth--;
178
+ i += 2;
179
+ }
180
+ else
181
+ i++;
182
+ }
183
+ space = true;
184
+ }
185
+ else if ((c === '-' && next === '-') || (c === '#' && !pg)) {
186
+ while (i < cs.length && cs[i] !== '\n')
187
+ i++;
188
+ space = true;
189
+ }
190
+ else {
191
+ if (space && out.length)
192
+ out.push(' ');
193
+ space = false;
194
+ i = token(cs, i, pg, out);
195
+ }
196
+ }
197
+ return collapseIn(enc.encode(out.join('')));
198
+ }
199
+ /**
200
+ * Literal-free statement text: strings, numbers and `$1` become `?`, comments go, whitespace
201
+ * collapses, `IN (?, ?)` becomes `IN (?+)`. `pg` keeps short `"identifiers"` and the `#` operator.
202
+ */
203
+ function normalizeSql(stmt, pg = false) {
204
+ return dec.decode(normalizeBytes(stmt, pg));
205
+ }
206
+ /** `[normalized text, decimal xxh3_64 of its UTF-8 bytes]`: the `db.query.hash` spans carry. */
207
+ function normalizeAndHash(stmt, pg = false) {
208
+ const b = normalizeBytes(stmt, pg);
209
+ return [dec.decode(b), xxh3(b).toString()];
210
+ }
211
+ /** Systems whose dialect keeps `"identifier"` quotes and the `#` operator (as the worker decides). */
212
+ const isPgDialect = (system) => system === 'postgresql' || system === 'cockroachdb' || system === 'redshift';
213
+ exports.isPgDialect = isPgDialect;
214
+ const OP = /^\s*(?:with\b[\s\S]*?\)\s*)?([a-z]+)/i;
215
+ const TARGET = /\b(?:from|into|update|join|table)\s+((?:"[^"]+"|[\w$]+)(?:\.(?:"[^"]+"|[\w$]+))?)/i;
216
+ /** `SELECT users`-style summary: the operation and first table, never values or the statement text. */
217
+ function querySummary(text, fallback = 'postgres') {
218
+ const operation = text?.match(OP)?.[1]?.toUpperCase();
219
+ const target = text?.match(TARGET)?.[1]?.replace(/"/g, '');
220
+ return { operation, target, summary: [operation ?? fallback, target].filter(Boolean).join(' ') };
221
+ }
222
+ // ---- XXH3-64, seed 0, default secret (port of the xxHash reference; BigInt is fine at <= 8 KiB) ----
223
+ const M64 = (1n << 64n) - 1n;
224
+ const P32_1 = 0x9e3779b1n, P32_2 = 0x85ebca77n, P32_3 = 0xc2b2ae3dn;
225
+ const P64_1 = 0x9e3779b185ebca87n, P64_2 = 0xc2b2ae3d27d4eb4fn, P64_3 = 0x165667b19e3779f9n;
226
+ const P64_4 = 0x85ebca77c2b2ae63n, P64_5 = 0x27d4eb2f165667c5n;
227
+ const SECRET = Uint8Array.from(Buffer.from('b8fe6c3923a44bbe7c01812cf721ad1cded46de9839097db7240a4a4b7b3671fcb79e64eccc0e578825ad07dccff7221'
228
+ + 'b8084674f743248ee03590e6813a264c3c2852bb91c300cb88d0658b1b532ea371644897a20df94e3819ef46a9deacd8'
229
+ + 'a8fa763fe39c343ff9dcbbc7c70b4f1d8a51e04bcdb45931c89f7ec9d9787364eac5ac8334d3ebc3c581a0fffa1363eb'
230
+ + '170ddd51b7f0da49d316552629d4689e2b16be587d47a1fc8ff8b8d17ad031ce45cb3a8f95160428afd7fbcabb4b407e', 'hex'));
231
+ const r64 = (b, o) => new DataView(b.buffer, b.byteOffset + o, 8).getBigUint64(0, true);
232
+ const r32 = (b, o) => BigInt(new DataView(b.buffer, b.byteOffset + o, 4).getUint32(0, true));
233
+ const mul = (a, b) => (a * b) & M64;
234
+ const add = (a, b) => (a + b) & M64;
235
+ const rotl = (x, r) => ((x << r) | (x >> (64n - r))) & M64;
236
+ const swap64 = (x) => {
237
+ let r = 0n;
238
+ for (let k = 0; k < 8; k++) {
239
+ r = (r << 8n) | (x & 0xffn);
240
+ x >>= 8n;
241
+ }
242
+ return r;
243
+ };
244
+ const fold = (a, b) => { const p = a * b; return (p ^ (p >> 64n)) & M64; };
245
+ const avalanche = (h) => { h ^= h >> 37n; h = mul(h, 0x165667919e3779f9n); return h ^ (h >> 32n); };
246
+ const avalanche64 = (h) => {
247
+ h ^= h >> 33n;
248
+ h = mul(h, P64_2);
249
+ h ^= h >> 29n;
250
+ h = mul(h, P64_3);
251
+ return h ^ (h >> 32n);
252
+ };
253
+ const mix16 = (p, o, s) => fold(r64(p, o) ^ r64(SECRET, s), r64(p, o + 8) ^ r64(SECRET, s + 8));
254
+ function accumulate512(acc, p, o, s) {
255
+ for (let i = 0; i < 8; i++) {
256
+ const v = r64(p, o + 8 * i);
257
+ const k = v ^ r64(SECRET, s + 8 * i);
258
+ acc[i ^ 1] = add(acc[i ^ 1], v);
259
+ acc[i] = add(acc[i], (k & 0xffffffffn) * (k >> 32n));
260
+ }
261
+ }
262
+ function long(p) {
263
+ const len = p.length;
264
+ const acc = [P32_3, P64_1, P64_2, P64_3, P64_4, P32_2, P64_5, P32_1];
265
+ const stripesPerBlock = (SECRET.length - 64) / 8;
266
+ const block = 64 * stripesPerBlock;
267
+ const blocks = Math.floor((len - 1) / block);
268
+ for (let n = 0; n < blocks; n++) {
269
+ for (let s = 0; s < stripesPerBlock; s++)
270
+ accumulate512(acc, p, n * block + s * 64, s * 8);
271
+ for (let i = 0; i < 8; i++) {
272
+ let a = acc[i];
273
+ a ^= a >> 47n;
274
+ a ^= r64(SECRET, SECRET.length - 64 + 8 * i);
275
+ acc[i] = mul(a, P32_1);
276
+ }
277
+ }
278
+ const stripes = Math.floor((len - 1 - block * blocks) / 64);
279
+ for (let s = 0; s < stripes; s++)
280
+ accumulate512(acc, p, blocks * block + s * 64, s * 8);
281
+ accumulate512(acc, p, len - 64, SECRET.length - 64 - 7);
282
+ let r = mul(BigInt(len), P64_1);
283
+ for (let i = 0; i < 4; i++)
284
+ r = add(r, fold(acc[2 * i] ^ r64(SECRET, 11 + 16 * i), acc[2 * i + 1] ^ r64(SECRET, 19 + 16 * i)));
285
+ return avalanche(r);
286
+ }
287
+ /** XXH3_64bits(input), seed 0. */
288
+ function xxh3(p) {
289
+ const len = p.length;
290
+ const L = BigInt(len);
291
+ if (len === 0)
292
+ return avalanche64(r64(SECRET, 56) ^ r64(SECRET, 64));
293
+ if (len <= 3) {
294
+ const combined = (BigInt(p[0]) << 16n) | (BigInt(p[len >> 1]) << 24n) | BigInt(p[len - 1]) | (L << 8n);
295
+ return avalanche64(combined ^ (r32(SECRET, 0) ^ r32(SECRET, 4)));
296
+ }
297
+ if (len <= 8) {
298
+ const keyed = (r32(p, len - 4) + (r32(p, 0) << 32n)) ^ (r64(SECRET, 8) ^ r64(SECRET, 16));
299
+ let h = keyed ^ rotl(keyed, 49n) ^ rotl(keyed, 24n);
300
+ h = mul(h, 0x9fb21c651e98df25n);
301
+ h ^= add(h >> 35n, L);
302
+ h = mul(h, 0x9fb21c651e98df25n);
303
+ return h ^ (h >> 28n);
304
+ }
305
+ if (len <= 16) {
306
+ const lo = r64(p, 0) ^ (r64(SECRET, 24) ^ r64(SECRET, 32));
307
+ const hi = r64(p, len - 8) ^ (r64(SECRET, 40) ^ r64(SECRET, 48));
308
+ return avalanche(add(add(add(L, swap64(lo)), hi), fold(lo, hi)));
309
+ }
310
+ if (len <= 128) {
311
+ let acc = mul(L, P64_1);
312
+ if (len > 32) {
313
+ if (len > 64) {
314
+ if (len > 96)
315
+ acc = add(add(acc, mix16(p, 48, 96)), mix16(p, len - 64, 112));
316
+ acc = add(add(acc, mix16(p, 32, 64)), mix16(p, len - 48, 80));
317
+ }
318
+ acc = add(add(acc, mix16(p, 16, 32)), mix16(p, len - 32, 48));
319
+ }
320
+ return avalanche(add(add(acc, mix16(p, 0, 0)), mix16(p, len - 16, 16)));
321
+ }
322
+ if (len <= 240) {
323
+ let acc = mul(L, P64_1);
324
+ for (let i = 0; i < 8; i++)
325
+ acc = add(acc, mix16(p, 16 * i, 16 * i));
326
+ acc = avalanche(acc);
327
+ for (let i = 8; i < Math.floor(len / 16); i++)
328
+ acc = add(acc, mix16(p, 16 * i, 16 * (i - 8) + 3));
329
+ return avalanche(add(acc, mix16(p, len - 16, 136 - 17)));
330
+ }
331
+ return long(p);
332
+ }
@@ -46,13 +46,15 @@ export interface PlanTask {
46
46
  }[];
47
47
  };
48
48
  }
49
- /** Vitest's `beforeAll`/`afterEach`, passed in so this package does not import Vitest. */
49
+ /** Vitest's `beforeAll`/`afterEach`/`afterAll`, passed in so this package does not import Vitest. */
50
50
  export interface VitestHooks {
51
51
  beforeAll(fn: (suite: PlanTask) => unknown): void;
52
52
  afterEach(fn: (ctx: {
53
53
  task: PlanTask;
54
54
  skip(note?: string): void;
55
55
  }) => unknown): void;
56
+ /** needed for WT_TIA_RECORD=1 */
57
+ afterAll?(fn: (suite: PlanTask) => unknown): void;
56
58
  }
57
59
  /** Applies the plan in a Vitest setup file. Decisions go to `task.meta.wt` for the reporter. */
58
60
  export declare function vitestSetup(hooks: VitestHooks): void;
@@ -90,4 +92,11 @@ export declare function jestEnvironment<B extends JestEnvBase>(Base: B): B;
90
92
  export declare function toJunit(suite: string, tests: TestRecord[]): string;
91
93
  /** Posts the results as JUnit when WT_CI_RUN_ID is set; never throws. */
92
94
  export declare function uploadResults(client: Client | null, suite: string, tests: TestRecord[]): Promise<boolean>;
95
+ /** Repository-relative files with an executed statement in an istanbul coverage object. */
96
+ export declare function coveredFiles(cov: unknown, root?: string): string[];
97
+ /**
98
+ * Uploads the coverage of one test file for its tests (`suite::rest` ids); never throws.
99
+ * Needs WT_API_URL, WT_PROJECT_ID and WT_API_KEY (the coverage map needs a project API key).
100
+ */
101
+ export declare function uploadCoverage(file: string | undefined, ids: string[], cov: unknown, fetchImpl?: typeof globalThis.fetch): Promise<boolean>;
93
102
  export {};
@@ -11,6 +11,8 @@ exports.vitestSetup = vitestSetup;
11
11
  exports.jestEnvironment = jestEnvironment;
12
12
  exports.toJunit = toJunit;
13
13
  exports.uploadResults = uploadResults;
14
+ exports.coveredFiles = coveredFiles;
15
+ exports.uploadCoverage = uploadCoverage;
14
16
  // Test optimization for Vitest and Jest, the same contract as the pytest plugin. Opt in with
15
17
  // WT_TEST_OPTIMIZATION=1 (plus WT_INGEST_KEY, WT_PROJECT_ID and WT_API_URL; WT_API_KEY, when set,
16
18
  // authenticates the project API calls instead of the ingest key):
@@ -23,13 +25,17 @@ exports.uploadResults = uploadResults;
23
25
  // - a test the project has never seen is retried up to `efd_retries` times (early flake detection).
24
26
  // The reporters upload the results as JUnit (`POST /v1/projects/{prj}/test-runs`) when
25
27
  // WT_CI_RUN_ID is set, which feeds flaky-test detection and auto-quarantine.
28
+ // With WT_TIA_RECORD=1 (and WT_API_KEY) each test file's istanbul coverage (Vitest
29
+ // `coverage.provider: 'istanbul'`, Jest `--coverage`) is uploaded as the per-test coverage map
30
+ // (`PUT /v1/projects/{prj}/tests/coverage-map`): every test of the file gets the files the file
31
+ // executed plus the test file itself, which Test Impact Analysis then uses to skip tests.
26
32
  //
27
33
  // Ids are `<suite>::<file relative to cwd> > <describe>... > <test>`, suite = WT_TEST_SUITE or
28
34
  // the framework name. Vitest:
29
35
  // globalSetup: ['./wt-global.mjs'] // export { default } from '@watchtowerobservability/node/test-plan'
30
36
  // setupFiles: ['./wt-setup.ts'] // import { beforeAll, afterEach } from 'vitest';
31
37
  // // import { vitestSetup } from '@watchtowerobservability/node/test-plan';
32
- // // vitestSetup({ beforeAll, afterEach });
38
+ // // vitestSetup({ beforeAll, afterEach, afterAll });
33
39
  // Jest: globalSetup as above, and testEnvironment: './wt-env.js' with
34
40
  // module.exports = require('@watchtowerobservability/node/test-plan').jestEnvironment(require('jest-environment-node').TestEnvironment);
35
41
  const node_child_process_1 = require("node:child_process");
@@ -134,8 +140,24 @@ function decider(plan) {
134
140
  return d;
135
141
  };
136
142
  }
143
+ /** Ids of the tests in `t` that ran (a skipped test executed nothing, so it gets no coverage). */
144
+ function ranIds(t, suite, file, names, out) {
145
+ for (const c of t.tasks || []) {
146
+ const ran = c.mode !== 'skip' && c.mode !== 'todo' && !!c.result?.state && c.result.state !== 'skip';
147
+ if (c.tasks)
148
+ ranIds(c, suite, file, [...names, c.name], out);
149
+ else if (ran)
150
+ out.push(testId(suite, file, [...names, c.name]));
151
+ }
152
+ return out;
153
+ }
137
154
  /** Applies the plan in a Vitest setup file. Decisions go to `task.meta.wt` for the reporter. */
138
155
  function vitestSetup(hooks) {
156
+ if (process.env.WT_TIA_RECORD === '1' && hooks.afterAll) {
157
+ const suite = (0, exports.suiteName)('vitest');
158
+ const g = globalThis;
159
+ hooks.afterAll((file) => uploadCoverage(file.filepath, ranIds(file, suite, file.filepath, [], []), g.__VITEST_COVERAGE__ || g.__coverage__));
160
+ }
139
161
  const plan = loadPlan();
140
162
  if (!plan)
141
163
  return;
@@ -179,6 +201,7 @@ function jestEnvironment(Base) {
179
201
  wtDecide = decider(loadPlan());
180
202
  wtSuite = (0, exports.suiteName)('jest');
181
203
  wtRetrySaved = [];
204
+ wtRan = [];
182
205
  // any[]: see JestEnvBase
183
206
  constructor(...args) {
184
207
  super(...args);
@@ -194,6 +217,12 @@ function jestEnvironment(Base) {
194
217
  if (super.handleTestEvent)
195
218
  await super.handleTestEvent(event, state);
196
219
  const t = event.test;
220
+ if (event.name === 'test_done' && t && process.env.WT_TIA_RECORD === '1')
221
+ this.wtRan.push(this.wtId(t));
222
+ if (event.name === 'run_finish' && process.env.WT_TIA_RECORD === '1') {
223
+ await uploadCoverage(this.wtFile, this.wtRan, this.global.__coverage__);
224
+ return;
225
+ }
197
226
  if (event.name === 'test_start' && t) {
198
227
  if (this.wtDecide(this.wtId(t)).skip)
199
228
  t.mode = 'skip';
@@ -260,3 +289,64 @@ async function uploadResults(client, suite, tests) {
260
289
  return false;
261
290
  }
262
291
  }
292
+ // ---------------------------------------------------------------- Test Impact Analysis recording
293
+ let rootCache;
294
+ /** The repository top level (git diff paths are relative to it), else the working directory. */
295
+ function repoRoot() {
296
+ if (rootCache === undefined) {
297
+ try {
298
+ rootCache = (0, node_child_process_1.execFileSync)('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8', timeout: 10_000 }).trim() || process.cwd();
299
+ }
300
+ catch (_e) {
301
+ rootCache = process.cwd();
302
+ }
303
+ }
304
+ return rootCache;
305
+ }
306
+ const repoRel = (f, root) => (0, node_path_1.relative)(root, f).split(node_path_1.sep).join('/');
307
+ /** Repository-relative files with an executed statement in an istanbul coverage object. */
308
+ function coveredFiles(cov, root = repoRoot()) {
309
+ const out = new Set();
310
+ if (!cov || typeof cov !== 'object')
311
+ return [];
312
+ for (const [key, fc] of Object.entries(cov)) {
313
+ if (!fc || !fc.s || !Object.values(fc.s).some((n) => n > 0))
314
+ continue;
315
+ const f = repoRel(fc.path || key, root);
316
+ if (f && !f.startsWith('..'))
317
+ out.add(f);
318
+ }
319
+ return [...out].sort();
320
+ }
321
+ /**
322
+ * Uploads the coverage of one test file for its tests (`suite::rest` ids); never throws.
323
+ * Needs WT_API_URL, WT_PROJECT_ID and WT_API_KEY (the coverage map needs a project API key).
324
+ */
325
+ async function uploadCoverage(file, ids, cov, fetchImpl = globalThis.fetch) {
326
+ const env = process.env;
327
+ const [api, project, key] = [env.WT_API_URL, env.WT_PROJECT_ID, env.WT_API_KEY];
328
+ if (!api || !project || !key || ids.length === 0)
329
+ return false;
330
+ const root = repoRoot();
331
+ const files = coveredFiles(cov, root);
332
+ if (file) {
333
+ const self = repoRel(file, root);
334
+ if (self && !self.startsWith('..') && !files.includes(self))
335
+ files.push(self);
336
+ }
337
+ const tests = [...new Set(ids)].slice(0, 20_000).map((id) => {
338
+ const i = id.indexOf('::');
339
+ return { suite: i < 0 ? '' : id.slice(0, i), test: i < 0 ? id : id.slice(i + 2), files: files.slice(0, 2000) };
340
+ });
341
+ try {
342
+ const res = await fetchImpl(`${api.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(project)}/tests/coverage-map`, {
343
+ method: 'PUT',
344
+ headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json' },
345
+ body: JSON.stringify({ sha: (env.WT_SHA || env.GITHUB_SHA || env.CI_COMMIT_SHA || '').slice(0, 64), tests }),
346
+ });
347
+ return res.ok;
348
+ }
349
+ catch (_e) {
350
+ return false;
351
+ }
352
+ }
@@ -0,0 +1,95 @@
1
+ export type Field = 'path' | 'query' | 'user_agent' | 'body' | 'headers';
2
+ export interface AppsecRule {
3
+ id: string;
4
+ name?: string;
5
+ category: string;
6
+ severity?: string;
7
+ fields: Field[];
8
+ pattern: string;
9
+ }
10
+ export interface DenyEntry {
11
+ value: string;
12
+ /** Unix seconds; null: no expiry. */
13
+ expires_at?: number | null;
14
+ }
15
+ export interface Ruleset {
16
+ mode: 'monitor' | 'block';
17
+ rules: AppsecRule[];
18
+ denylist?: {
19
+ ip?: DenyEntry[];
20
+ user?: DenyEntry[];
21
+ };
22
+ poll_s?: number;
23
+ }
24
+ /** One request as the inspector sees it (decoded). */
25
+ export interface Inspected {
26
+ path: string;
27
+ query?: string;
28
+ user_agent?: string;
29
+ body?: string;
30
+ headers?: string;
31
+ ip?: string;
32
+ user?: string;
33
+ }
34
+ export interface Decision {
35
+ /** `allow`: nothing matched; `monitor`: matched, let through; `block`: answer 403. */
36
+ action: 'allow' | 'monitor' | 'block';
37
+ rule_id?: string;
38
+ category?: string;
39
+ match?: string;
40
+ }
41
+ /** Rule id recorded for denylist blocks (the worker reports them as their own signal). */
42
+ export declare const DENYLIST = "denylist";
43
+ /** Replace the active ruleset (what the poller calls; tests may call it directly). */
44
+ export declare function setRuleset(r: Ruleset | null): void;
45
+ /** Whether `id` is on the user denylist (call after authentication to refuse a listed user). */
46
+ export declare const isUserBlocked: (id: string, now?: number) => boolean;
47
+ /** Judge one request. Denylisted callers are blocked in either mode. */
48
+ export declare function inspect(req: Inspected, now?: number): Decision;
49
+ type Headers = Record<string, string | string[] | undefined>;
50
+ /** The slice of a Node request the inspector reads. */
51
+ export interface NodeRequest {
52
+ url?: string;
53
+ originalUrl?: string;
54
+ headers?: Headers;
55
+ socket?: {
56
+ remoteAddress?: string;
57
+ };
58
+ body?: unknown;
59
+ }
60
+ /** The slice of a Node response used to answer 403. */
61
+ export interface NodeResponse {
62
+ statusCode: number;
63
+ setHeader?(name: string, value: string): unknown;
64
+ end(body?: string): unknown;
65
+ }
66
+ /** Client IP: first `X-Forwarded-For` hop, else `X-Real-IP`, else the socket peer.
67
+ * shortcut: trusts the forwarding headers; run behind a proxy that overwrites them. */
68
+ export declare function clientIp(req: NodeRequest): string | undefined;
69
+ /** Decode a Node request into the inspector's fields. */
70
+ export declare function fromNode(req: NodeRequest, user?: string): Inspected;
71
+ /** Record a decision on the active span. */
72
+ export declare function annotate(d: Decision, ip?: string): void;
73
+ /** Inspect and, when blocked, answer 403. Returns true when the request was answered. */
74
+ export declare function handleRequest(req: NodeRequest, res: NodeResponse, user?: string): boolean;
75
+ /** Connect-style middleware; `user(req)` names the caller when the app already knows it. */
76
+ export declare function appsecMiddleware(o?: {
77
+ user?: (req: NodeRequest) => string | undefined;
78
+ }): (req: NodeRequest, res: NodeResponse, next: (err?: unknown) => void) => void;
79
+ /** Mark the current request as a login attempt; failed ones feed the account-takeover rules. */
80
+ export declare function trackLogin(login: string, success: boolean): void;
81
+ export interface AppsecOptions {
82
+ /** API base; default `WT_API_URL` or https://api.watchtower.dev */
83
+ apiUrl?: string;
84
+ /** Ingestion key with scope `probes:read`; default `WT_INGEST_KEY`. */
85
+ key?: string;
86
+ /** Default `WT_SERVICE` or `OTEL_SERVICE_NAME`. */
87
+ service?: string;
88
+ fetch?: typeof fetch;
89
+ }
90
+ /** Fetch the ruleset now and keep polling; `stop()` ends the polling. */
91
+ export declare function startAppsec(o?: AppsecOptions): {
92
+ ready: Promise<void>;
93
+ stop(): void;
94
+ };
95
+ export {};