@reprova/sdk 0.4.0 → 0.6.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.
- package/README.md +170 -8
- package/dist/cjs/context.js +28 -0
- package/dist/cjs/distributionBaselinePoller.js +87 -0
- package/dist/cjs/distributionDrift.js +170 -0
- package/dist/cjs/drizzle.js +204 -0
- package/dist/cjs/frameworkAdapter.js +2 -0
- package/dist/cjs/httpFetch.js +59 -0
- package/dist/cjs/idempotency.js +132 -0
- package/dist/cjs/idempotencySettingsPoller.js +71 -0
- package/dist/cjs/index.js +96 -0
- package/dist/cjs/invariantInference.js +90 -0
- package/dist/cjs/invariantRules.js +142 -0
- package/dist/cjs/knex.js +86 -0
- package/dist/cjs/kysely.js +175 -0
- package/dist/cjs/legacyPrisma.js +130 -0
- package/dist/cjs/mikroorm.js +74 -0
- package/dist/cjs/mongoSchemaReporter.js +85 -0
- package/dist/cjs/mongoose.js +335 -0
- package/dist/cjs/nPlusOne.js +0 -0
- package/dist/cjs/nest.js +136 -0
- package/dist/cjs/nextjs.js +180 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/prisma.js +363 -0
- package/dist/cjs/proto.gen.js +4 -0
- package/dist/cjs/registry.js +100 -0
- package/dist/cjs/retryStorm.js +70 -0
- package/dist/cjs/rulesPoller.js +77 -0
- package/dist/cjs/sdk.js +1231 -0
- package/dist/cjs/sequelize.js +108 -0
- package/dist/cjs/shape.js +45 -0
- package/dist/cjs/stack.js +51 -0
- package/dist/cjs/transport.js +71 -0
- package/dist/cjs/typeorm.js +200 -0
- package/dist/context.d.ts +32 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +18 -0
- package/dist/context.js.map +1 -1
- package/dist/distributionBaselinePoller.d.ts +36 -0
- package/dist/distributionBaselinePoller.d.ts.map +1 -0
- package/dist/distributionBaselinePoller.js +84 -0
- package/dist/distributionBaselinePoller.js.map +1 -0
- package/dist/distributionDrift.d.ts +41 -0
- package/dist/distributionDrift.d.ts.map +1 -0
- package/dist/distributionDrift.js +161 -0
- package/dist/distributionDrift.js.map +1 -0
- package/dist/drizzle.d.ts +22 -0
- package/dist/drizzle.d.ts.map +1 -1
- package/dist/drizzle.js +129 -1
- package/dist/drizzle.js.map +1 -1
- package/dist/frameworkAdapter.d.ts +5 -0
- package/dist/frameworkAdapter.d.ts.map +1 -0
- package/dist/frameworkAdapter.js +2 -0
- package/dist/frameworkAdapter.js.map +1 -0
- package/dist/httpFetch.d.ts +13 -0
- package/dist/httpFetch.d.ts.map +1 -0
- package/dist/httpFetch.js +54 -0
- package/dist/httpFetch.js.map +1 -0
- package/dist/idempotency.d.ts +26 -0
- package/dist/idempotency.d.ts.map +1 -0
- package/dist/idempotency.js +124 -0
- package/dist/idempotency.js.map +1 -0
- package/dist/idempotencySettingsPoller.d.ts +23 -0
- package/dist/idempotencySettingsPoller.d.ts.map +1 -0
- package/dist/idempotencySettingsPoller.js +68 -0
- package/dist/idempotencySettingsPoller.js.map +1 -0
- package/dist/index.d.ts +33 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -4
- package/dist/index.js.map +1 -1
- package/dist/invariantInference.d.ts +28 -0
- package/dist/invariantInference.d.ts.map +1 -0
- package/dist/invariantInference.js +87 -0
- package/dist/invariantInference.js.map +1 -0
- package/dist/invariantRules.d.ts +16 -0
- package/dist/invariantRules.d.ts.map +1 -0
- package/dist/invariantRules.js +104 -0
- package/dist/invariantRules.js.map +1 -0
- package/dist/knex.d.ts.map +1 -1
- package/dist/knex.js +1 -0
- package/dist/knex.js.map +1 -1
- package/dist/kysely.d.ts.map +1 -1
- package/dist/kysely.js +2 -1
- package/dist/kysely.js.map +1 -1
- package/dist/legacyPrisma.d.ts +6 -0
- package/dist/legacyPrisma.d.ts.map +1 -0
- package/dist/legacyPrisma.js +128 -0
- package/dist/legacyPrisma.js.map +1 -0
- package/dist/mikroorm.d.ts +2 -0
- package/dist/mikroorm.d.ts.map +1 -0
- package/dist/mikroorm.js +72 -0
- package/dist/mikroorm.js.map +1 -0
- package/dist/mongoSchemaReporter.d.ts +23 -0
- package/dist/mongoSchemaReporter.d.ts.map +1 -0
- package/dist/mongoSchemaReporter.js +82 -0
- package/dist/mongoSchemaReporter.js.map +1 -0
- package/dist/mongoose.d.ts +48 -0
- package/dist/mongoose.d.ts.map +1 -0
- package/dist/mongoose.js +331 -0
- package/dist/mongoose.js.map +1 -0
- package/dist/nPlusOne.d.ts +52 -0
- package/dist/nPlusOne.d.ts.map +1 -0
- package/dist/nPlusOne.js +0 -0
- package/dist/nPlusOne.js.map +1 -0
- package/dist/nest.d.ts +31 -0
- package/dist/nest.d.ts.map +1 -0
- package/dist/nest.js +133 -0
- package/dist/nest.js.map +1 -0
- package/dist/nextjs.d.ts +27 -0
- package/dist/nextjs.d.ts.map +1 -0
- package/dist/nextjs.js +178 -0
- package/dist/nextjs.js.map +1 -0
- package/dist/prisma.d.ts +21 -5
- package/dist/prisma.d.ts.map +1 -1
- package/dist/prisma.js +247 -22
- package/dist/prisma.js.map +1 -1
- package/dist/proto.gen.d.ts +19 -1
- package/dist/proto.gen.d.ts.map +1 -1
- package/dist/registry.d.ts +1 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +24 -0
- package/dist/registry.js.map +1 -1
- package/dist/retryStorm.d.ts +42 -0
- package/dist/retryStorm.d.ts.map +1 -0
- package/dist/retryStorm.js +65 -0
- package/dist/retryStorm.js.map +1 -0
- package/dist/rulesPoller.d.ts +26 -0
- package/dist/rulesPoller.d.ts.map +1 -0
- package/dist/rulesPoller.js +74 -0
- package/dist/rulesPoller.js.map +1 -0
- package/dist/sdk.d.ts +53 -6
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +663 -34
- package/dist/sdk.js.map +1 -1
- package/dist/sequelize.d.ts.map +1 -1
- package/dist/sequelize.js +4 -3
- package/dist/sequelize.js.map +1 -1
- package/dist/stack.d.ts.map +1 -1
- package/dist/stack.js +6 -3
- package/dist/stack.js.map +1 -1
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +2 -1
- package/dist/transport.js.map +1 -1
- package/dist/typeorm.d.ts +1 -0
- package/dist/typeorm.d.ts.map +1 -1
- package/dist/typeorm.js +133 -1
- package/dist/typeorm.js.map +1 -1
- package/package.json +20 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency.d.ts","sourceRoot":"","sources":["../src/idempotency.ts"],"names":[],"mappings":"AAsBA,MAAM,WAAW,mBAAmB;IAClC,iBAAiB,EAAE,OAAO,CAAC;IAC3B,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,sBAAsB,EAAE,OAAO,CAAC;IAChC,wBAAwB,EAAE,MAAM,CAAC;CAClC;AAKD,eAAO,MAAM,4BAA4B,EAAE,mBAO1C,CAAC;AAMF,qBAAa,sBAAuB,SAAQ,KAAK;CAAG;AACpD,qBAAa,qBAAsB,SAAQ,KAAK;CAAG;AAcnD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAEtD;AAQD,qBAAa,iBAAiB;IAI1B,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,KAAK;IAJf,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA0D;gBAGpE,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM;IAMvB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAO/C,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,SAAa,GAAG,OAAO;IAa1E,IAAI,IAAI,IAAI,MAAM,CAEjB;CACF;AAKD,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,GAAG,SAAS,CAM7G;AAUD,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAOnG"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Idempotency/consistency checks: two independent, domain-agnostic
|
|
2
|
+
// detectors for "silent 200" bugs, unlike business-invariant rules — neither
|
|
3
|
+
// needs a dashboard-authored condition tree, since neither depends on
|
|
4
|
+
// knowing anything about the app's data model.
|
|
5
|
+
//
|
|
6
|
+
// 1. Idempotency-key mismatch: the app itself asserts two requests are "the
|
|
7
|
+
// same operation" by sending an identical value in a known header (e.g.
|
|
8
|
+
// Stripe-style Idempotency-Key). If the response differs between them,
|
|
9
|
+
// that's unambiguously a bug — no heuristic tuning needed, the app told
|
|
10
|
+
// us these should match.
|
|
11
|
+
// 2. Duplicate-read mismatch: the SAME GET (route + query + caller
|
|
12
|
+
// identity) repeated within a short window returning a different
|
|
13
|
+
// result. Real false-positive risk exists here (data can legitimately
|
|
14
|
+
// change between two reads), so it's scoped tight: a short window turns
|
|
15
|
+
// it into "did two near-simultaneous reads of what should be the same
|
|
16
|
+
// snapshot disagree" (a race-condition/double-fetch signal), not general
|
|
17
|
+
// staleness detection — and it's skipped entirely when no caller
|
|
18
|
+
// identity is available, since an identity-less GET trivially disagrees
|
|
19
|
+
// across different callers.
|
|
20
|
+
import { createHash } from 'node:crypto';
|
|
21
|
+
// Mirrors the control plane's idempotency_settings table defaults — used
|
|
22
|
+
// before the first successful poll, and if polling never succeeds at all
|
|
23
|
+
// (e.g. an older control plane without this endpoint yet).
|
|
24
|
+
export const DEFAULT_IDEMPOTENCY_SETTINGS = {
|
|
25
|
+
key_check_enabled: true,
|
|
26
|
+
header_names: ['Idempotency-Key', 'X-Idempotency-Key'],
|
|
27
|
+
ttl_ms: 86_400_000,
|
|
28
|
+
max_keys: 100_000,
|
|
29
|
+
duplicate_read_enabled: false,
|
|
30
|
+
duplicate_read_window_ms: 500,
|
|
31
|
+
};
|
|
32
|
+
// One reusable Error class per detector, mirroring BusinessInvariantViolation
|
|
33
|
+
// — per-endpoint identity comes from capture()'s errorType override, not a
|
|
34
|
+
// per-instance subclass, so many distinct endpoints share one class without
|
|
35
|
+
// collapsing into a single issue.
|
|
36
|
+
export class IdempotencyKeyMismatch extends Error {
|
|
37
|
+
}
|
|
38
|
+
export class DuplicateReadMismatch extends Error {
|
|
39
|
+
}
|
|
40
|
+
// Deterministic regardless of key insertion order, so { a:1, b:2 } and
|
|
41
|
+
// { b:2, a:1 } hash identically — otherwise two genuinely-equivalent
|
|
42
|
+
// responses serialized in a different key order would look like a mismatch
|
|
43
|
+
// that isn't one.
|
|
44
|
+
function stableStringify(value) {
|
|
45
|
+
if (value === null || typeof value !== 'object')
|
|
46
|
+
return JSON.stringify(value);
|
|
47
|
+
if (Array.isArray(value))
|
|
48
|
+
return `[${value.map(stableStringify).join(',')}]`;
|
|
49
|
+
const obj = value;
|
|
50
|
+
const keys = Object.keys(obj).sort();
|
|
51
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`).join(',')}}`;
|
|
52
|
+
}
|
|
53
|
+
export function hashResponseBody(body) {
|
|
54
|
+
return createHash('sha256').update(stableStringify(body)).digest('hex');
|
|
55
|
+
}
|
|
56
|
+
// Bounded signature -> last-seen-hash cache with TTL + LRU eviction, shared
|
|
57
|
+
// by both detectors — the underlying mechanism ("has this signature's hash
|
|
58
|
+
// changed within its window") is identical for both; only what goes into
|
|
59
|
+
// the signature and how long it's remembered differs. Map iteration order
|
|
60
|
+
// is insertion order, and delete-then-reinsert on every access keeps it in
|
|
61
|
+
// least-recently-used order, so eviction always drops the LRU entry.
|
|
62
|
+
export class HashMismatchCache {
|
|
63
|
+
maxKeys;
|
|
64
|
+
ttlMs;
|
|
65
|
+
map = new Map();
|
|
66
|
+
constructor(maxKeys, ttlMs) {
|
|
67
|
+
this.maxKeys = maxKeys;
|
|
68
|
+
this.ttlMs = ttlMs;
|
|
69
|
+
}
|
|
70
|
+
// Settings can change on every poll — reconfigure in place rather than
|
|
71
|
+
// recreating the cache, so a TTL/cap edit doesn't silently drop everything
|
|
72
|
+
// currently tracked.
|
|
73
|
+
configure(maxKeys, ttlMs) {
|
|
74
|
+
this.maxKeys = maxKeys;
|
|
75
|
+
this.ttlMs = ttlMs;
|
|
76
|
+
}
|
|
77
|
+
// Records `hash` for `signature`, returning true if a live (non-expired)
|
|
78
|
+
// prior hash for the same signature exists and differs from this one.
|
|
79
|
+
checkAndRecord(signature, hash, now = Date.now()) {
|
|
80
|
+
const existing = this.map.get(signature);
|
|
81
|
+
this.map.delete(signature); // re-inserted below either way, refreshing LRU order
|
|
82
|
+
const mismatch = !!existing && existing.expiresAt > now && existing.hash !== hash;
|
|
83
|
+
this.map.set(signature, { hash, expiresAt: now + this.ttlMs });
|
|
84
|
+
while (this.map.size > this.maxKeys) {
|
|
85
|
+
const oldest = this.map.keys().next().value;
|
|
86
|
+
if (oldest === undefined)
|
|
87
|
+
break;
|
|
88
|
+
this.map.delete(oldest);
|
|
89
|
+
}
|
|
90
|
+
return mismatch;
|
|
91
|
+
}
|
|
92
|
+
get size() {
|
|
93
|
+
return this.map.size;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Checks each configured header name case-insensitively — Node normalizes
|
|
97
|
+
// incoming header names to lowercase, and captureHeaders (stack.ts) passes
|
|
98
|
+
// that through unchanged, so ctx.request.headers keys are already lowercase.
|
|
99
|
+
export function findIdempotencyKey(headers, headerNames) {
|
|
100
|
+
for (const name of headerNames) {
|
|
101
|
+
const value = headers[name.toLowerCase()];
|
|
102
|
+
if (value)
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
// A small, conventional set of claim keys checked in order — not exhaustive,
|
|
108
|
+
// just enough to cover common shapes (Auth0/JWT `sub`, a plain `id`,
|
|
109
|
+
// Passport-style `userId`/`user_id`). Returns undefined, never a guess, when
|
|
110
|
+
// none match: duplicate-read detection is skipped entirely without an
|
|
111
|
+
// identity, rather than risk comparing two different callers' data and
|
|
112
|
+
// calling the difference a bug.
|
|
113
|
+
const IDENTITY_CLAIM_KEYS = ['sub', 'id', 'userId', 'user_id'];
|
|
114
|
+
export function extractIdentity(authClaims) {
|
|
115
|
+
if (!authClaims)
|
|
116
|
+
return undefined;
|
|
117
|
+
for (const key of IDENTITY_CLAIM_KEYS) {
|
|
118
|
+
const value = authClaims[key];
|
|
119
|
+
if (typeof value === 'string' || typeof value === 'number')
|
|
120
|
+
return String(value);
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=idempotency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotency.js","sourceRoot":"","sources":["../src/idempotency.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,6EAA6E;AAC7E,sEAAsE;AACtE,+CAA+C;AAC/C,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,0EAA0E;AAC1E,2EAA2E;AAC3E,4BAA4B;AAC5B,mEAAmE;AACnE,oEAAoE;AACpE,yEAAyE;AACzE,2EAA2E;AAC3E,yEAAyE;AACzE,4EAA4E;AAC5E,oEAAoE;AACpE,2EAA2E;AAC3E,+BAA+B;AAE/B,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAWzC,yEAAyE;AACzE,yEAAyE;AACzE,2DAA2D;AAC3D,MAAM,CAAC,MAAM,4BAA4B,GAAwB;IAC/D,iBAAiB,EAAE,IAAI;IACvB,YAAY,EAAE,CAAC,iBAAiB,EAAE,mBAAmB,CAAC;IACtD,MAAM,EAAE,UAAU;IAClB,QAAQ,EAAE,OAAO;IACjB,sBAAsB,EAAE,KAAK;IAC7B,wBAAwB,EAAE,GAAG;CAC9B,CAAC;AAEF,8EAA8E;AAC9E,2EAA2E;AAC3E,4EAA4E;AAC5E,kCAAkC;AAClC,MAAM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AAI,WAAW,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;QAC1C,IAAI,KAAK;YAAE,OAAO,KAAK,CAAC;IAC1B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,6EAA6E;AAC7E,qEAAqE;AACrE,6EAA6E;AAC7E,sEAAsE;AACtE,uEAAuE;AACvE,gCAAgC;AAChC,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;AAE/D,MAAM,UAAU,eAAe,CAAC,UAA+C;IAC7E,IAAI,CAAC,UAAU;QAAE,OAAO,SAAS,CAAC;IAClC,KAAK,MAAM,GAAG,IAAI,mBAAmB,EAAE,CAAC;QACtC,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { IdempotencySettings } from './idempotency.js';
|
|
2
|
+
interface IdempotencySettingsPollerOptions {
|
|
3
|
+
endpoint: string;
|
|
4
|
+
apiKey: string;
|
|
5
|
+
intervalMs?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare class IdempotencySettingsPoller {
|
|
8
|
+
private settings;
|
|
9
|
+
private timer;
|
|
10
|
+
private lastWarnAt;
|
|
11
|
+
private readonly endpoint;
|
|
12
|
+
private readonly apiKey;
|
|
13
|
+
private readonly intervalMs;
|
|
14
|
+
constructor(opts: IdempotencySettingsPollerOptions);
|
|
15
|
+
start(): void;
|
|
16
|
+
stop(): void;
|
|
17
|
+
getSettings(): IdempotencySettings;
|
|
18
|
+
refresh(): Promise<void>;
|
|
19
|
+
seed(settings: IdempotencySettings): void;
|
|
20
|
+
private warnOnce;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=idempotencySettingsPoller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotencySettingsPoller.d.ts","sourceRoot":"","sources":["../src/idempotencySettingsPoller.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAG5D,UAAU,gCAAgC;IACxC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAUD,qBAAa,yBAAyB;IACpC,OAAO,CAAC,QAAQ,CAAqD;IACrE,OAAO,CAAC,KAAK,CAA+C;IAC5D,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,IAAI,EAAE,gCAAgC;IAMlD,KAAK,IAAI,IAAI;IAMb,IAAI,IAAI,IAAI;IAOZ,WAAW,IAAI,mBAAmB;IAI5B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAoB9B,IAAI,CAAC,QAAQ,EAAE,mBAAmB,GAAG,IAAI;IAIzC,OAAO,CAAC,QAAQ;CAOjB"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DEFAULT_IDEMPOTENCY_SETTINGS } from './idempotency.js';
|
|
2
|
+
import { httpFetch } from './httpFetch.js';
|
|
3
|
+
const WARN_INTERVAL_MS = 60_000;
|
|
4
|
+
// Same start()/stop()/setInterval+unref shape as RulesPoller — periodically
|
|
5
|
+
// fetches the tenant's dashboard-configured idempotency-check settings so a
|
|
6
|
+
// toggle flipped in the dashboard reaches a running process without a
|
|
7
|
+
// redeploy. Falls back to DEFAULT_IDEMPOTENCY_SETTINGS before the first
|
|
8
|
+
// successful poll and keeps the last-known-good settings on any failure,
|
|
9
|
+
// never throwing and never leaving the detectors unconfigured.
|
|
10
|
+
export class IdempotencySettingsPoller {
|
|
11
|
+
settings = DEFAULT_IDEMPOTENCY_SETTINGS;
|
|
12
|
+
timer = null;
|
|
13
|
+
lastWarnAt = 0;
|
|
14
|
+
endpoint;
|
|
15
|
+
apiKey;
|
|
16
|
+
intervalMs;
|
|
17
|
+
constructor(opts) {
|
|
18
|
+
this.endpoint = opts.endpoint;
|
|
19
|
+
this.apiKey = opts.apiKey;
|
|
20
|
+
this.intervalMs = opts.intervalMs ?? 60_000;
|
|
21
|
+
}
|
|
22
|
+
start() {
|
|
23
|
+
void this.refresh(); // don't wait a full interval before settings are usable
|
|
24
|
+
this.timer = setInterval(() => void this.refresh(), this.intervalMs);
|
|
25
|
+
if (this.timer.unref)
|
|
26
|
+
this.timer.unref(); // don't block process exit
|
|
27
|
+
}
|
|
28
|
+
stop() {
|
|
29
|
+
if (this.timer) {
|
|
30
|
+
clearInterval(this.timer);
|
|
31
|
+
this.timer = null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
getSettings() {
|
|
35
|
+
return this.settings;
|
|
36
|
+
}
|
|
37
|
+
async refresh() {
|
|
38
|
+
try {
|
|
39
|
+
const res = await httpFetch(this.endpoint, {
|
|
40
|
+
headers: { 'X-Reprova-Key': this.apiKey },
|
|
41
|
+
});
|
|
42
|
+
if (!res.ok) {
|
|
43
|
+
this.warnOnce(`reprova: idempotency settings fetch returned ${res.status}`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.seed((await res.json()));
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
this.warnOnce(`reprova: idempotency settings fetch error: ${err.message}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Applies an already-fetched settings object directly, with no network
|
|
53
|
+
// call — used by replay mode to seed a one-time snapshot the CLI fetched
|
|
54
|
+
// ahead of time (there's no live control plane to poll from inside a
|
|
55
|
+
// replayed app), so checkIdempotencyIfAny (sdk.ts) sees exactly the same
|
|
56
|
+
// shape it would from a real refresh().
|
|
57
|
+
seed(settings) {
|
|
58
|
+
this.settings = settings;
|
|
59
|
+
}
|
|
60
|
+
warnOnce(msg) {
|
|
61
|
+
const now = Date.now();
|
|
62
|
+
if (now - this.lastWarnAt > WARN_INTERVAL_MS) {
|
|
63
|
+
this.lastWarnAt = now;
|
|
64
|
+
console.warn(msg);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=idempotencySettingsPoller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"idempotencySettingsPoller.js","sourceRoot":"","sources":["../src/idempotencySettingsPoller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AAEhE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAQ3C,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEhC,4EAA4E;AAC5E,4EAA4E;AAC5E,sEAAsE;AACtE,wEAAwE;AACxE,yEAAyE;AACzE,+DAA+D;AAC/D,MAAM,OAAO,yBAAyB;IAC5B,QAAQ,GAAwB,4BAA4B,CAAC;IAC7D,KAAK,GAA0C,IAAI,CAAC;IACpD,UAAU,GAAG,CAAC,CAAC;IACN,QAAQ,CAAS;IACjB,MAAM,CAAS;IACf,UAAU,CAAS;IAEpC,YAAY,IAAsC;QAChD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,MAAM,CAAC;IAC9C,CAAC;IAED,KAAK;QACH,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,wDAAwD;QAC7E,IAAI,CAAC,K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AE,CAAC;QACvB,IAAI,GAAG,GAAG,IAAI,CAAC,UAAU,GAAG,gBAAgB,EAAE,CAAC;YAC7C,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
export { Reprova, wrapResponse, forwardAsyncErrors, replayErrorHandler } from './sdk.js';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export type { FrameworkAdapter } from './frameworkAdapter.js';
|
|
3
|
+
export { contextStorage, currentContext, setAuthClaims } from './context.js';
|
|
4
|
+
export { createPrismaExtension, recordPrismaOperation } from './prisma.js';
|
|
5
|
+
export type { DMMF, DMMFModel, DMMFField, PrismaRecordHooks } from './prisma.js';
|
|
6
|
+
export { instrumentLegacyPrismaClient } from './legacyPrisma.js';
|
|
7
|
+
export type { LegacyPrismaOptions } from './legacyPrisma.js';
|
|
4
8
|
export { createKyselyPlugin } from './kysely.js';
|
|
5
9
|
export type { KyselyPluginOptions, KyselyFootprintPlugin } from './kysely.js';
|
|
6
10
|
export { installSequelizeHooks } from './sequelize.js';
|
|
7
11
|
export type { SequelizeAdapterOptions } from './sequelize.js';
|
|
8
|
-
export { installTypeOrmSubscriber } from './typeorm.js';
|
|
12
|
+
export { installTypeOrmSubscriber, installTypeOrmQueryLogger } from './typeorm.js';
|
|
13
|
+
export { installMikroOrmSubscriber } from './mikroorm.js';
|
|
14
|
+
export { reprovaMongoosePlugin, discoverMongooseSchema } from './mongoose.js';
|
|
15
|
+
export type { DiscoveredMongoModel, DiscoveredMongoEdge, MongooseInstanceLike } from './mongoose.js';
|
|
16
|
+
export { MongoSchemaReporter } from './mongoSchemaReporter.js';
|
|
9
17
|
export { installKnexHooks } from './knex.js';
|
|
10
18
|
export type { KnexAdapterOptions } from './knex.js';
|
|
11
|
-
export {
|
|
12
|
-
export type {
|
|
19
|
+
export { createNestExceptionFilter, createNestContextInterceptor } from './nest.js';
|
|
20
|
+
export type { ReprovaNestFilter, NestExceptionFilterOptions, ArgumentsHostLike, NestAppLike, CallHandlerLike, ReprovaNestInterceptor, NestContextInterceptorOptions, } from './nest.js';
|
|
21
|
+
export { createNextAdapter } from './nextjs.js';
|
|
22
|
+
export type { NextAdapterOptions, ReprovaNextAdapter, ReprovaTrpcMiddleware, TrpcMiddlewareResultLike, TrpcMiddlewareParamsLike } from './nextjs.js';
|
|
23
|
+
export { createDrizzleLogger, createDrizzleCache, createDrizzleFootprint } from './drizzle.js';
|
|
24
|
+
export type { DrizzleLoggerOptions, DrizzleLogger, DrizzleCache, DrizzleCacheOptions, DrizzleFootprintOptions, } from './drizzle.js';
|
|
13
25
|
export type { ReprovaOptions } from './sdk.js';
|
|
14
|
-
export type { ReprovaContext, FootprintQuery, OutboundCall, RequestSnapshot } from './context.js';
|
|
26
|
+
export type { ReprovaContext, FootprintQuery, OutboundCall, RequestSnapshot, BusinessInvariantHit, WriteCountDriftHit } from './context.js';
|
|
27
|
+
export { detectNPlusOne, describeNPlusOne, PotentialNPlusOneQuery } from './nPlusOne.js';
|
|
28
|
+
export type { NPlusOneFinding } from './nPlusOne.js';
|
|
29
|
+
export { detectRetryStorm, describeRetryStorm, PotentialRetryStorm } from './retryStorm.js';
|
|
30
|
+
export type { RetryStormFinding } from './retryStorm.js';
|
|
31
|
+
export { compileInvariant, evaluateInvariant, operationMatchesHook, BusinessInvariantViolation } from './invariantRules.js';
|
|
32
|
+
export type { InvariantRule, CompiledInvariant } from './invariantRules.js';
|
|
33
|
+
export { RulesPoller } from './rulesPoller.js';
|
|
34
|
+
export { DEFAULT_IDEMPOTENCY_SETTINGS, HashMismatchCache, IdempotencyKeyMismatch, DuplicateReadMismatch, findIdempotencyKey, extractIdentity, hashResponseBody, } from './idempotency.js';
|
|
35
|
+
export type { IdempotencySettings } from './idempotency.js';
|
|
36
|
+
export { IdempotencySettingsPoller } from './idempotencySettingsPoller.js';
|
|
37
|
+
export { InferenceReporter } from './invariantInference.js';
|
|
38
|
+
export type { InferredInvariantObservation } from './invariantInference.js';
|
|
39
|
+
export { ValueDistributionDrift, WriteCountDrift, newWelfordState, updateWelfordState, isDrift, extractTopLevelNumericFields, DistributionDriftReporter, } from './distributionDrift.js';
|
|
40
|
+
export type { WelfordState, DistributionBaseline } from './distributionDrift.js';
|
|
41
|
+
export { DistributionBaselinePoller } from './distributionBaselinePoller.js';
|
|
15
42
|
export * from './proto.gen.js';
|
|
16
43
|
export declare const SDK_VERSION = "0.1.0";
|
|
17
44
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AACzF,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AACzF,YAAY,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC3E,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACjF,OAAO,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AACjE,YAAY,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,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AAU,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
export { Reprova, wrapResponse, forwardAsyncErrors, replayErrorHandler } from './sdk.js';
|
|
2
|
-
export { contextStorage, currentContext } from './context.js';
|
|
3
|
-
export { createPrismaExtension } from './prisma.js';
|
|
2
|
+
export { contextStorage, currentContext, setAuthClaims } from './context.js';
|
|
3
|
+
export { createPrismaExtension, recordPrismaOperation } from './prisma.js';
|
|
4
|
+
export { instrumentLegacyPrismaClient } from './legacyPrisma.js';
|
|
4
5
|
export { createKyselyPlugin } from './kysely.js';
|
|
5
6
|
export { installSequelizeHooks } from './sequelize.js';
|
|
6
|
-
export { installTypeOrmSubscriber } from './typeorm.js';
|
|
7
|
+
export { installTypeOrmSubscriber, installTypeOrmQueryLogger } from './typeorm.js';
|
|
8
|
+
export { installMikroOrmSubscriber } from './mikroorm.js';
|
|
9
|
+
export { reprovaMongoosePlugin, discoverMongooseSchema } from './mongoose.js';
|
|
10
|
+
export { MongoSchemaReporter } from './mongoSchemaReporter.js';
|
|
7
11
|
export { installKnexHooks } from './knex.js';
|
|
8
|
-
export {
|
|
12
|
+
export { createNestExceptionFilter, createNestContextInterceptor } from './nest.js';
|
|
13
|
+
export { createNextAdapter } from './nextjs.js';
|
|
14
|
+
export { createDrizzleLogger, createDrizzleCache, createDrizzleFootprint } from './drizzle.js';
|
|
15
|
+
export { detectNPlusOne, describeNPlusOne, PotentialNPlusOneQuery } from './nPlusOne.js';
|
|
16
|
+
export { detectRetryStorm, describeRetryStorm, PotentialRetryStorm } from './retryStorm.js';
|
|
17
|
+
export { compileInvariant, evaluateInvariant, operationMatchesHook, BusinessInvariantViolation } from './invariantRules.js';
|
|
18
|
+
export { RulesPoller } from './rulesPoller.js';
|
|
19
|
+
export { DEFAULT_IDEMPOTENCY_SETTINGS, HashMismatchCache, IdempotencyKeyMismatch, DuplicateReadMismatch, findIdempotencyKey, extractIdentity, hashResponseBody, } from './idempotency.js';
|
|
20
|
+
export { IdempotencySettingsPoller } from './idempotencySettingsPoller.js';
|
|
21
|
+
export { InferenceReporter } from './invariantInference.js';
|
|
22
|
+
export { ValueDistributionDrift, WriteCountDrift, newWelfordState, updateWelfordState, isDrift, extractTopLevelNumericFields, DistributionDriftReporter, } from './distributionDrift.js';
|
|
23
|
+
export { DistributionBaselinePoller } from './distributionBaselinePoller.js';
|
|
9
24
|
export * from './proto.gen.js';
|
|
10
25
|
export const SDK_VERSION = '0.1.0';
|
|
11
26
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEzF,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7E,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAE3E,OAAO,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAC;AAEjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,OAAO,EAAE,wBAAwB,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC;AACnF,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAE9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/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|
8
|
+
interface ReporterOptions {
|
|
9
|
+
endpoint: string;
|
|
10
|
+
apiKey: string;
|
|
11
|
+
flushIntervalMs?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class InferenceReporter {
|
|
14
|
+
private counts;
|
|
15
|
+
private timer;
|
|
16
|
+
private lastWarnAt;
|
|
17
|
+
private readonly endpoint;
|
|
18
|
+
private readonly apiKey;
|
|
19
|
+
private readonly flushIntervalMs;
|
|
20
|
+
constructor(opts: ReporterOptions);
|
|
21
|
+
start(): void;
|
|
22
|
+
stop(): void;
|
|
23
|
+
record(model: string, field: string, relation: string, held: boolean): void;
|
|
24
|
+
flush(): Promise<void>;
|
|
25
|
+
private warnOnce;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=invariantInference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariantInference.d.ts","sourceRoot":"","sources":["../src/invariantInference.ts"],"names":[],"mappings":"AAoBA,MAAM,WAAW,4BAA4B;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,eAAe;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAOD,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAwG;IACtH,OAAO,CAAC,KAAK,CAA+C;IAC5D,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;gBAE7B,IAAI,EAAE,eAAe;IAMjC,KAAK,IAAI,IAAI;IAKb,IAAI,IAAI,IAAI;IAOZ,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI;IAWrE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAsB5B,OAAO,CAAC,QAAQ;CAOjB"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Invariant inference, v1 slice: instead of hand-authoring a
|
|
2
|
+
// business-invariant-rule condition tree, passively observe whether a
|
|
3
|
+
// same-named numeric field on a parent row equals the sum of that same
|
|
4
|
+
// field across an already-included to-many relation (Invoice.total vs
|
|
5
|
+
// sum(lineItems.total)) — and report pass/fail counts to the control plane.
|
|
6
|
+
// Once a candidate has been observed enough times at a high enough pass
|
|
7
|
+
// rate, the dashboard offers it as a one-click "confirm" that creates a
|
|
8
|
+
// normal business_invariant_rule — no new evaluation mechanism needed once
|
|
9
|
+
// confirmed, it becomes an ordinary dashboard-authored rule.
|
|
10
|
+
//
|
|
11
|
+
// Deliberately scoped down from the general idea: only the same-field-name
|
|
12
|
+
// shape (not every numeric-field-pair combination, which would be
|
|
13
|
+
// combinatorial and noisy), only `sum` (no `count`), and no dashboard-
|
|
14
|
+
// configurable thresholds yet — see PROGRESS.md for the fuller design this
|
|
15
|
+
// is a first slice of.
|
|
16
|
+
import { httpFetch } from './httpFetch.js';
|
|
17
|
+
const WARN_INTERVAL_MS = 60_000;
|
|
18
|
+
// Reports observation deltas, not cumulative totals — the control plane
|
|
19
|
+
// owns the running (checked_count, held_count) per candidate; the SDK only
|
|
20
|
+
// ever adds to it. That makes a dropped/failed flush harmless to correctness
|
|
21
|
+
// (the next flush's delta is unaffected by a prior one failing) at the cost
|
|
22
|
+
// of only ever undercounting on a genuine send failure, never overcounting.
|
|
23
|
+
export class InferenceReporter {
|
|
24
|
+
counts = new Map();
|
|
25
|
+
timer = null;
|
|
26
|
+
lastWarnAt = 0;
|
|
27
|
+
endpoint;
|
|
28
|
+
apiKey;
|
|
29
|
+
flushIntervalMs;
|
|
30
|
+
constructor(opts) {
|
|
31
|
+
this.endpoint = opts.endpoint;
|
|
32
|
+
this.apiKey = opts.apiKey;
|
|
33
|
+
this.flushIntervalMs = opts.flushIntervalMs ?? 30_000;
|
|
34
|
+
}
|
|
35
|
+
start() {
|
|
36
|
+
this.timer = setInterval(() => void this.flush(), this.flushIntervalMs);
|
|
37
|
+
if (this.timer.unref)
|
|
38
|
+
this.timer.unref(); // don't block process exit
|
|
39
|
+
}
|
|
40
|
+
stop() {
|
|
41
|
+
if (this.timer) {
|
|
42
|
+
clearInterval(this.timer);
|
|
43
|
+
this.timer = null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
record(model, field, relation, held) {
|
|
47
|
+
const key = `${model}\x00${field}\x00${relation}`;
|
|
48
|
+
let c = this.counts.get(key);
|
|
49
|
+
if (!c) {
|
|
50
|
+
c = { model, field, relation, checked: 0, held: 0 };
|
|
51
|
+
this.counts.set(key, c);
|
|
52
|
+
}
|
|
53
|
+
c.checked++;
|
|
54
|
+
if (held)
|
|
55
|
+
c.held++;
|
|
56
|
+
}
|
|
57
|
+
async flush() {
|
|
58
|
+
if (this.counts.size === 0)
|
|
59
|
+
return;
|
|
60
|
+
const observations = [...this.counts.values()];
|
|
61
|
+
this.counts.clear();
|
|
62
|
+
try {
|
|
63
|
+
const res = await httpFetch(this.endpoint, {
|
|
64
|
+
method: 'POST',
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
'X-Reprova-Key': this.apiKey,
|
|
68
|
+
},
|
|
69
|
+
body: JSON.stringify({ observations }),
|
|
70
|
+
});
|
|
71
|
+
if (!res.ok) {
|
|
72
|
+
this.warnOnce(`reprova: inferred-invariant observation report returned ${res.status}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
this.warnOnce(`reprova: inferred-invariant observation report error: ${err.message}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
warnOnce(msg) {
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
if (now - this.lastWarnAt > WARN_INTERVAL_MS) {
|
|
82
|
+
this.lastWarnAt = now;
|
|
83
|
+
console.warn(msg);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=invariantInference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariantInference.js","sourceRoot":"","sources":["../src/invariantInference.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,sEAAsE;AACtE,uEAAuE;AACvE,sEAAsE;AACtE,4EAA4E;AAC5E,wEAAwE;AACxE,wEAAwE;AACxE,2EAA2E;AAC3E,6DAA6D;AAC7D,EAAE;AACF,2EAA2E;AAC3E,kEAAkE;AAClE,uEAAuE;AACvE,2EAA2E;AAC3E,uBAAuB;AAEvB,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAgBhC,wEAAwE;AACxE,2EAA2E;AAC3E,6EAA6E;AAC7E,4EAA4E;AAC5E,4EAA4E;AAC5E,MAAM,OAAO,iBAAiB;IACpB,MAAM,GAAG,IAAI,GAAG,EAA6F,CAAC;IAC9G,KAAK,GAA0C,IAAI,CAAC;IACpD,UAAU,GAAG,CAAC,CAAC;IACN,QAAQ,CAAS;IACjB,MAAM,CAAS;IACf,eAAe,CAAS;IAEzC,YAAY,IAAqB;QAC/B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,IAAI,MAAM,CAAC;IACxD,CAAC;IAED,KAAK;QACH,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,KAAK,IAAI,CAAC,KAAK,EAAE,EAA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,kBAAkB;oBAClC,eAAe,EAAE,IAAI,CAAC,MAAM;iBAC7B;gBACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;aACvC,CAAC,CAAC;YACH,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,IAAI,CAAC,QAAQ,CAAC,2DAA2D,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;YACzF,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,QAAQ,CAAC,yDAA0D,GAAa,CAAC,OAAO,EAAE,CAAC,CAAC;QACnG,CAAC;IACH,CAAC;IAEO,QAAQ,CAAC,GAAW;QAC1B,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,GAAG,GAAG,IAAI,CAAC,UAAU,GAAG,gBAAgB,EAAE,CAAC;YAC7C,IAAI,CAAC,UAAU,GAAG,GAAG,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface InvariantRule {
|
|
2
|
+
id: string;
|
|
3
|
+
model: string;
|
|
4
|
+
when_hook: 'after_read' | 'after_write';
|
|
5
|
+
expr: string;
|
|
6
|
+
message: string;
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function operationMatchesHook(op: string, whenHook: InvariantRule['when_hook']): boolean;
|
|
10
|
+
export type CompiledInvariant = (row: Record<string, unknown>) => unknown;
|
|
11
|
+
export declare function compileInvariant(rule: Pick<InvariantRule, 'id' | 'expr'>): Promise<CompiledInvariant | null>;
|
|
12
|
+
export declare function evaluateInvariant(compiled: CompiledInvariant, row: Record<string, unknown>): boolean | undefined;
|
|
13
|
+
export declare class BusinessInvariantViolation extends Error {
|
|
14
|
+
constructor(message: string);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=invariantRules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariantRules.d.ts","sourceRoot":"","sources":["../src/invariantRules.ts"],"names":[],"mappings":"AA4DA,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,YAAY,GAAG,aAAa,CAAC;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CAClB;AASD,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,CAAC,WAAW,CAAC,GAAG,OAAO,CAE9F;AAED,MAAM,MAAM,iBAAiB,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC;AAc1E,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,aAAa,EAAE,IAAI,GAAG,MAAM,CAAC,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAalH;AAOD,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,GAAG,SAAS,CAQhH;AAOD,qBAAa,0BAA2B,SAAQ,KAAK;gBACvC,OAAO,EAAE,MAAM;CAG5B"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// Dashboard-authored business-invariant rules: CEL (Common Expression
|
|
2
|
+
// Language) expressions evaluated in-process against real Prisma query
|
|
3
|
+
// results (not just the footprint's PKs/counts — actual field values), so
|
|
4
|
+
// bugs that never throw and never return a 5xx (an invoice total that
|
|
5
|
+
// silently drifted from its line items, negative inventory, an order marked
|
|
6
|
+
// shipped with no shipment row) can still produce a reproduction.
|
|
7
|
+
//
|
|
8
|
+
// Replaces an earlier JSON condition-tree evaluator (see git history) — CEL
|
|
9
|
+
// gives the same same-row-comparison-and-relation-aggregate expressiveness
|
|
10
|
+
// with a much friendlier authoring surface (`total ==
|
|
11
|
+
// lineItems.map(x, x.total).sum()` instead of a nested op/field/right tree),
|
|
12
|
+
// using the actively-maintained, zero-dependency @marcbachmann/cel-js.
|
|
13
|
+
//
|
|
14
|
+
// `expr` is the INVARIANT itself — what should always be true — not the
|
|
15
|
+
// violation condition: a rule fires when `expr` evaluates to exactly
|
|
16
|
+
// `false`. This is the opposite polarity from the old JSON tree (which
|
|
17
|
+
// encoded the violation, e.g. `not: eq(...)`), but reads naturally in CEL
|
|
18
|
+
// and matches how an admin would actually describe the rule out loud.
|
|
19
|
+
//
|
|
20
|
+
// A field or relation the app's own query didn't `include` simply isn't a
|
|
21
|
+
// key on the row object, which CEL reports as a runtime "no such attribute"
|
|
22
|
+
// error rather than `false` — evaluateInvariant treats any evaluation error,
|
|
23
|
+
// and any non-boolean result (an author mistake, e.g. forgetting a
|
|
24
|
+
// comparison operator), as "can't evaluate" and skips, never firing a false
|
|
25
|
+
// violation on data the query simply didn't load. Same principle
|
|
26
|
+
// `invariantInference.ts`/`distributionDrift.ts` use: never guess.
|
|
27
|
+
let envPromise = null;
|
|
28
|
+
function loadEnv() {
|
|
29
|
+
if (!envPromise) {
|
|
30
|
+
envPromise = import('@marcbachmann/cel-js')
|
|
31
|
+
.then(({ Environment }) => new Environment({ unlistedVariablesAreDyn: true }).registerFunction({
|
|
32
|
+
name: 'sum',
|
|
33
|
+
receiverType: 'list',
|
|
34
|
+
returnType: 'double',
|
|
35
|
+
handler: (list) => list.reduce((a, b) => a + b, 0),
|
|
36
|
+
params: [],
|
|
37
|
+
}))
|
|
38
|
+
.catch(() => null); // can't load the CEL engine at all — every rule silently never fires, never crashes the host app
|
|
39
|
+
}
|
|
40
|
+
return envPromise;
|
|
41
|
+
}
|
|
42
|
+
const READ_OPS = new Set(['findUnique', 'findUniqueOrThrow', 'findFirst', 'findFirstOrThrow', 'findMany']);
|
|
43
|
+
const WRITE_OPS = new Set(['create', 'createMany', 'update', 'updateMany', 'upsert', 'delete', 'deleteMany']);
|
|
44
|
+
// Whether a Prisma operation name belongs to the read/write class a rule's
|
|
45
|
+
// when_hook asks to run on. Anything neither (e.g. $queryRaw, aggregate,
|
|
46
|
+
// count, groupBy) never matches — a rule needs real field values on a real
|
|
47
|
+
// row, which those don't uniformly provide.
|
|
48
|
+
export function operationMatchesHook(op, whenHook) {
|
|
49
|
+
return whenHook === 'after_read' ? READ_OPS.has(op) : WRITE_OPS.has(op);
|
|
50
|
+
}
|
|
51
|
+
// Compiled programs are cached by (rule id, expr) so an edited rule
|
|
52
|
+
// recompiles (new expr under the same id misses the cache) while an
|
|
53
|
+
// unchanged one is parsed exactly once for the life of the process — CEL
|
|
54
|
+
// parsing is not free, and this runs on every matching query result.
|
|
55
|
+
// Invalid CEL syntax caches a `null` miss rather than re-attempting the
|
|
56
|
+
// same broken parse on every request. A CEL-engine load failure (see
|
|
57
|
+
// loadEnv's own doc comment) is deliberately NOT cached here — that's a
|
|
58
|
+
// process-wide condition already memoized by envPromise itself, not a
|
|
59
|
+
// per-rule outcome, so a later rule with valid syntax isn't punished by an
|
|
60
|
+
// earlier one's transient load timing.
|
|
61
|
+
const compileCache = new Map();
|
|
62
|
+
export async function compileInvariant(rule) {
|
|
63
|
+
const key = `${rule.id}\x00${rule.expr}`;
|
|
64
|
+
if (compileCache.has(key))
|
|
65
|
+
return compileCache.get(key);
|
|
66
|
+
const env = await loadEnv();
|
|
67
|
+
if (!env)
|
|
68
|
+
return null;
|
|
69
|
+
let compiled;
|
|
70
|
+
try {
|
|
71
|
+
compiled = env.parse(rule.expr);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
compiled = null; // invalid CEL — never crash the host app on a bad admin-authored rule
|
|
75
|
+
}
|
|
76
|
+
compileCache.set(key, compiled);
|
|
77
|
+
return compiled;
|
|
78
|
+
}
|
|
79
|
+
// Runs a compiled invariant against one row. Returns `false` only for a
|
|
80
|
+
// genuine, cleanly-evaluated violation; `undefined` for anything else
|
|
81
|
+
// (evaluation error from a missing field/relation, or a non-boolean result
|
|
82
|
+
// from a malformed expression) so the caller never mistakes "couldn't tell"
|
|
83
|
+
// for "violated".
|
|
84
|
+
export function evaluateInvariant(compiled, row) {
|
|
85
|
+
let result;
|
|
86
|
+
try {
|
|
87
|
+
result = compiled(row);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
return typeof result === 'boolean' ? result : undefined;
|
|
93
|
+
}
|
|
94
|
+
// One reusable Error class for every rule violation. Per-rule identity comes
|
|
95
|
+
// from capture()'s extra.errorType override (see sdk.ts), not from a
|
|
96
|
+
// per-rule subclass — many rules can share this class without collapsing
|
|
97
|
+
// into one issue, since fingerprinting keys off errorType, not the class
|
|
98
|
+
// name, once errorType is supplied explicitly.
|
|
99
|
+
export class BusinessInvariantViolation extends Error {
|
|
100
|
+
constructor(message) {
|
|
101
|
+
super(message);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=invariantRules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"invariantRules.js","sourceRoot":"","sources":["../src/invariantRules.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,uEAAuE;AACvE,0EAA0E;AAC1E,sEAAsE;AACtE,4EAA4E;AAC5E,kEAAkE;AAClE,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,sDAAsD;AACtD,6EAA6E;AAC7E,uEAAuE;AACvE,EAAE;AACF,wEAAwE;AACxE,qEAAqE;AACrE,uEAAuE;AACvE,0EAA0E;AAC1E,sEAAsE;AACtE,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,6EAA6E;AAC7E,mEAAmE;AACnE,4EAA4E;AAC5E,iEAAiE;AACjE,mEAAmE;AAgBnE,IAAI,UAAU,GAA0C,IAAI,CAAC;AAE7D,SAAS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BAA2B,SAAQ,KAAK;IACnD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF"}
|
package/dist/knex.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"knex.d.ts","sourceRoot":"","sources":["../src/knex.ts"],"names":[],"mappings":"AAuBA,MAAM,WAAW,kBAAkB;IAEjC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,EAAE,CAAC;CAC1C;AAsCD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,GAAE,kBAAuB,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"knex.d.ts","sourceRoot":"","sources":["../src/knex.ts"],"names":[],"mappings":"AAuBA,MAAM,WAAW,kBAAkB;IAEjC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,EAAE,CAAC;CAC1C;AAsCD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,GAAE,kBAAuB,GAAG,IAAI,CAsCnF"}
|