@decionis/agentsafe 0.3.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.
- package/README.md +181 -121
- package/dist/Cli.js +5 -0
- package/dist/Cli.js.map +1 -1
- package/dist/TrustedExecutor.d.ts.map +1 -1
- package/dist/TrustedExecutor.js +1 -0
- package/dist/TrustedExecutor.js.map +1 -1
- package/dist/adapters/EffectAwareGrantVerifier.d.ts +5 -2
- package/dist/adapters/EffectAwareGrantVerifier.d.ts.map +1 -1
- package/dist/adapters/EffectAwareGrantVerifier.js +9 -0
- package/dist/adapters/EffectAwareGrantVerifier.js.map +1 -1
- package/dist/adapters/banking/BankingAction.d.ts +2 -2
- package/dist/adapters/cards/CardHoldSeal.d.ts +29 -0
- package/dist/adapters/cards/CardHoldSeal.d.ts.map +1 -0
- package/dist/adapters/cards/CardHoldSeal.js +87 -0
- package/dist/adapters/cards/CardHoldSeal.js.map +1 -0
- package/dist/adapters/cards/CardHoldStore.d.ts +102 -0
- package/dist/adapters/cards/CardHoldStore.d.ts.map +1 -0
- package/dist/adapters/cards/CardHoldStore.js +9 -0
- package/dist/adapters/cards/CardHoldStore.js.map +1 -0
- package/dist/adapters/cards/CardHolds.d.ts +19 -0
- package/dist/adapters/cards/CardHolds.d.ts.map +1 -0
- package/dist/adapters/cards/CardHolds.js +57 -0
- package/dist/adapters/cards/CardHolds.js.map +1 -0
- package/dist/adapters/cards/HeldCardGrants.d.ts +41 -42
- package/dist/adapters/cards/HeldCardGrants.d.ts.map +1 -1
- package/dist/adapters/cards/HeldCardGrants.js +112 -22
- package/dist/adapters/cards/HeldCardGrants.js.map +1 -1
- package/dist/adapters/cards/PostgresCardHolds.d.ts +95 -0
- package/dist/adapters/cards/PostgresCardHolds.d.ts.map +1 -0
- package/dist/adapters/cards/PostgresCardHolds.js +411 -0
- package/dist/adapters/cards/PostgresCardHolds.js.map +1 -0
- package/dist/cli/Cards.d.ts +5 -0
- package/dist/cli/Cards.d.ts.map +1 -0
- package/dist/cli/Cards.js +48 -0
- package/dist/cli/Cards.js.map +1 -0
- package/dist/cli/Help.d.ts.map +1 -1
- package/dist/cli/Help.js +1 -0
- package/dist/cli/Help.js.map +1 -1
- package/dist/config/ConfigKeys.d.ts +2 -2
- package/dist/config/ConfigKeys.d.ts.map +1 -1
- package/dist/config/ConfigKeys.js +12 -0
- package/dist/config/ConfigKeys.js.map +1 -1
- package/dist/config/ExecutorConfig.d.ts +53 -0
- package/dist/config/ExecutorConfig.d.ts.map +1 -1
- package/dist/config/ExecutorConfig.js +88 -0
- package/dist/config/ExecutorConfig.js.map +1 -1
- package/dist/http/Routes.d.ts +7 -0
- package/dist/http/Routes.d.ts.map +1 -1
- package/dist/http/Routes.js +7 -1
- package/dist/http/Routes.js.map +1 -1
- package/dist/identity/Authenticator.d.ts.map +1 -1
- package/dist/identity/Authenticator.js +5 -2
- package/dist/identity/Authenticator.js.map +1 -1
- package/dist/identity/PrincipalsFile.d.ts +3 -1
- package/dist/identity/PrincipalsFile.d.ts.map +1 -1
- package/dist/identity/PrincipalsFile.js +1 -0
- package/dist/identity/PrincipalsFile.js.map +1 -1
- package/dist/incident/SecurityEvents.d.ts +9 -0
- package/dist/incident/SecurityEvents.d.ts.map +1 -1
- package/dist/incident/SecurityEvents.js +3 -0
- package/dist/incident/SecurityEvents.js.map +1 -1
- package/dist/service/EscalationResolver.d.ts +30 -6
- package/dist/service/EscalationResolver.d.ts.map +1 -1
- package/dist/service/EscalationResolver.js +2 -0
- package/dist/service/EscalationResolver.js.map +1 -1
- package/dist/service/Requests.d.ts +17 -0
- package/dist/service/Requests.d.ts.map +1 -1
- package/dist/service/TrustedExecutorService.d.ts +77 -4
- package/dist/service/TrustedExecutorService.d.ts.map +1 -1
- package/dist/service/TrustedExecutorService.js +451 -56
- package/dist/service/TrustedExecutorService.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,18 +1,28 @@
|
|
|
1
|
+
import { CARD_VELOCITY_WINDOW_MS, cardHoldKey, } from "./CardHoldStore.js";
|
|
2
|
+
export { CARD_VELOCITY_WINDOW_MS } from "./CardHoldStore.js";
|
|
1
3
|
/**
|
|
2
4
|
* Grants held for a card authorization that has not happened yet, keyed by
|
|
3
|
-
* the card
|
|
5
|
+
* the card and the merchant, and the authorizations that claimed one, keyed
|
|
4
6
|
* by the issuer's authorization id. In memory and bounded on purpose: a hold
|
|
5
7
|
* lives no longer than its grant, which the authority caps in minutes, and a
|
|
6
8
|
* restart that loses one loses only a grant nobody claimed, which expires
|
|
7
|
-
* with no effect. A claimed authorization is already in the journal.
|
|
9
|
+
* with no effect. A claimed authorization is already in the journal. A
|
|
10
|
+
* deployment with more than one replica, or one that must keep its holds
|
|
11
|
+
* across a restart, uses the shared store instead.
|
|
8
12
|
*
|
|
9
|
-
* One card has at most one spendable hold. An agent that wants
|
|
10
|
-
* purchase on the same card waits for the
|
|
11
|
-
* expire, so an authorization can never be
|
|
13
|
+
* One card has at most one spendable hold per merchant. An agent that wants
|
|
14
|
+
* a second purchase at the same merchant on the same card waits for the
|
|
15
|
+
* first to be authorised or to expire, so an authorization can never be
|
|
16
|
+
* matched to the wrong one of two; purchases at different merchants hold in
|
|
17
|
+
* parallel, because the authorization names its merchant.
|
|
12
18
|
*/
|
|
13
19
|
export class HeldCardGrants {
|
|
20
|
+
kind = "memory";
|
|
21
|
+
static MAX_ACTIVITY_PER_CARD = 100;
|
|
14
22
|
holds = new Map();
|
|
15
23
|
claims = new Map();
|
|
24
|
+
/** When each card was last held for and claimed, most recently touched card last. */
|
|
25
|
+
activity = new Map();
|
|
16
26
|
maxEntries;
|
|
17
27
|
claimRetentionMs;
|
|
18
28
|
clock;
|
|
@@ -21,34 +31,57 @@ export class HeldCardGrants {
|
|
|
21
31
|
this.claimRetentionMs = options.claimRetentionMs ?? 15 * 60_000;
|
|
22
32
|
this.clock = options.clock ?? (() => Date.now());
|
|
23
33
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const grant = this.grant(cardTokenRef);
|
|
27
|
-
return grant !== undefined && !grant.used && grant.expiresAtMs > this.clock();
|
|
28
|
-
}
|
|
29
|
-
/** The hold for a card, spent, expired or neither, until it is forgotten. */
|
|
30
|
-
grant(cardTokenRef) {
|
|
31
|
-
this.sweep();
|
|
32
|
-
return this.holds.get(cardTokenRef);
|
|
34
|
+
async spendable(key) {
|
|
35
|
+
return this.spendableNow(key);
|
|
33
36
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (this.
|
|
37
|
+
async hold(grant) {
|
|
38
|
+
const key = cardHoldKey(grant);
|
|
39
|
+
if (this.spendableNow(grant))
|
|
37
40
|
return false;
|
|
38
|
-
this.holds.delete(
|
|
41
|
+
this.holds.delete(key);
|
|
39
42
|
if (this.holds.size >= this.maxEntries)
|
|
40
43
|
return false;
|
|
41
|
-
this.holds.set(
|
|
44
|
+
this.holds.set(key, grant);
|
|
45
|
+
this.note(grant.cardTokenRef, "hold");
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
async grant(key) {
|
|
49
|
+
this.sweep();
|
|
50
|
+
const grant = this.holds.get(cardHoldKey(key));
|
|
51
|
+
if (grant === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
return {
|
|
54
|
+
cardTokenRef: grant.cardTokenRef,
|
|
55
|
+
merchantId: grant.merchantId,
|
|
56
|
+
amountMinor: grant.amountMinor,
|
|
57
|
+
currency: grant.currency,
|
|
58
|
+
mcc: grant.mcc,
|
|
59
|
+
expiresAtMs: grant.expiresAtMs,
|
|
60
|
+
used: grant.used,
|
|
61
|
+
intentId: grant.held.captured.intent.intentId,
|
|
62
|
+
open: async () => grant,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Spent before anything is awaited by the caller: the first to ask gets it, later askers find it used. */
|
|
66
|
+
async spend(grant) {
|
|
67
|
+
if (grant.used || this.holds.get(cardHoldKey(grant)) !== grant)
|
|
68
|
+
return false;
|
|
69
|
+
grant.used = true;
|
|
42
70
|
return true;
|
|
43
71
|
}
|
|
44
|
-
claimed(authorizationId) {
|
|
72
|
+
async claimed(authorizationId) {
|
|
45
73
|
this.sweep();
|
|
46
74
|
return this.claims.get(authorizationId);
|
|
47
75
|
}
|
|
48
76
|
/** Remembers a claim for its result; a full store forgets the oldest, which the journal still holds. */
|
|
49
|
-
recordClaim(authorizationId, claim) {
|
|
77
|
+
async recordClaim(authorizationId, claim) {
|
|
78
|
+
// The claim material is the shared store's concern; here the claim stays in the process.
|
|
50
79
|
const record = {
|
|
51
|
-
|
|
80
|
+
grant: claim.grant,
|
|
81
|
+
request: claim.request,
|
|
82
|
+
authorization: claim.authorization,
|
|
83
|
+
leaseExpiresAtMs: claim.leaseExpiresAtMs,
|
|
84
|
+
answer: claim.answer,
|
|
52
85
|
retainUntilMs: this.clock() + this.claimRetentionMs,
|
|
53
86
|
settled: false,
|
|
54
87
|
};
|
|
@@ -57,11 +90,68 @@ export class HeldCardGrants {
|
|
|
57
90
|
this.claims.delete(oldest);
|
|
58
91
|
}
|
|
59
92
|
this.claims.set(authorizationId, record);
|
|
93
|
+
this.note(claim.grant.cardTokenRef, "claim");
|
|
60
94
|
return record;
|
|
61
95
|
}
|
|
96
|
+
async amend(authorizationId, request) {
|
|
97
|
+
const claim = this.claims.get(authorizationId);
|
|
98
|
+
if (claim === undefined || claim.settled)
|
|
99
|
+
return false;
|
|
100
|
+
claim.request = request;
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
async settle(authorizationId) {
|
|
104
|
+
const claim = this.claims.get(authorizationId);
|
|
105
|
+
if (claim === undefined || claim.settled)
|
|
106
|
+
return false;
|
|
107
|
+
claim.settled = true;
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The burst a policy wants to see: how many purchases were held for this
|
|
112
|
+
* card and how many authorizations claimed one, over the window. The
|
|
113
|
+
* policy language counts nothing over time, so the executor counts here
|
|
114
|
+
* and puts the numbers in the intent's context; a card reference never
|
|
115
|
+
* leaves with them.
|
|
116
|
+
*/
|
|
117
|
+
async velocity(cardTokenRef, windowMs = CARD_VELOCITY_WINDOW_MS) {
|
|
118
|
+
const since = this.clock() - windowMs;
|
|
119
|
+
const recent = (this.activity.get(cardTokenRef) ?? []).filter((entry) => entry.at > since);
|
|
120
|
+
const holds = recent.filter((entry) => entry.kind === "hold").length;
|
|
121
|
+
return { window_seconds: Math.round(windowMs / 1_000), holds, claims: recent.length - holds };
|
|
122
|
+
}
|
|
123
|
+
async ready() {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
async cleanup() {
|
|
127
|
+
this.sweep();
|
|
128
|
+
}
|
|
129
|
+
async close() {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
62
132
|
get size() {
|
|
63
133
|
return { holds: this.holds.size, claims: this.claims.size };
|
|
64
134
|
}
|
|
135
|
+
spendableNow(key) {
|
|
136
|
+
this.sweep();
|
|
137
|
+
const grant = this.holds.get(cardHoldKey(key));
|
|
138
|
+
return grant !== undefined && !grant.used && grant.expiresAtMs > this.clock();
|
|
139
|
+
}
|
|
140
|
+
/** Remembers one hold or claim on a card, bounded per card and across cards. */
|
|
141
|
+
note(cardTokenRef, kind) {
|
|
142
|
+
const now = this.clock();
|
|
143
|
+
const entries = (this.activity.get(cardTokenRef) ?? []).filter((entry) => entry.at > now - CARD_VELOCITY_WINDOW_MS);
|
|
144
|
+
entries.push({ at: now, kind });
|
|
145
|
+
if (entries.length > HeldCardGrants.MAX_ACTIVITY_PER_CARD) {
|
|
146
|
+
entries.splice(0, entries.length - HeldCardGrants.MAX_ACTIVITY_PER_CARD);
|
|
147
|
+
}
|
|
148
|
+
// Re-inserted so the map's order is recency, and the stalest card goes first.
|
|
149
|
+
this.activity.delete(cardTokenRef);
|
|
150
|
+
this.activity.set(cardTokenRef, entries);
|
|
151
|
+
if (this.activity.size > this.maxEntries) {
|
|
152
|
+
this.activity.delete(this.activity.keys().next().value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
65
155
|
sweep() {
|
|
66
156
|
const now = this.clock();
|
|
67
157
|
// An expired hold is kept as long as a claim would be, so an authorization
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HeldCardGrants.js","sourceRoot":"","sources":["../../../src/adapters/cards/HeldCardGrants.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"HeldCardGrants.js","sourceRoot":"","sources":["../../../src/adapters/cards/HeldCardGrants.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,WAAW,GAQZ,MAAM,oBAAoB,CAAC;AAS5B,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAU7D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,cAAc;IACT,IAAI,GAAG,QAAiB,CAAC;IACjC,MAAM,CAAU,qBAAqB,GAAG,GAAG,CAAC;IACnC,KAAK,GAAG,IAAI,GAAG,EAAyB,CAAC;IACzC,MAAM,GAAG,IAAI,GAAG,EAAoC,CAAC;IACtE,qFAAqF;IACpE,QAAQ,GAAG,IAAI,GAAG,EAGhC,CAAC;IACa,UAAU,CAAS;IACnB,gBAAgB,CAAS;IACzB,KAAK,CAAe;IAErC,YAAmB,UAAiC,EAAE;QACpD,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,MAAM,CAAC;QAC/C,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,EAAE,GAAG,MAAM,CAAC;QAChE,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,GAAW,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAC3D,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,GAAgB;QACrC,OAAO,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,KAAoB;QACpC,MAAM,GAAG,GAAG,WAAW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AC,UAAU,EAAE,CAAC;YACzC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAe,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;IAEO,KAAK;QACX,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QACzB,2EAA2E;QAC3E,6EAA6E;QAC7E,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACtC,IAAI,KAAK,CAAC,WAAW,GAAG,IAAI,CAAC,gBAAgB,IAAI,GAAG;gBAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC/E,CAAC;QACD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACvC,IAAI,KAAK,CAAC,aAAa,IAAI,GAAG;gBAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { SqlClient } from "@decionis/agent-safe-pipeline";
|
|
2
|
+
import type { CardHoldSeal } from "./CardHoldSeal.js";
|
|
3
|
+
import { type CardClaimInput, type CardHoldAdoption, type CardHoldKey, type CardHoldStore, type CardVelocity, type ClaimedCardAuthorization, type HeldCardGrant, type HeldCardGrantRecord } from "./CardHoldStore.js";
|
|
4
|
+
import type { CardAuthorizationRequest } from "./CardPurchase.js";
|
|
5
|
+
export declare const DEFAULT_CARD_HOLDS_TABLE = "agentsafe_card";
|
|
6
|
+
declare const CLAIM_NOT_ADOPTED = "CLAIM_NOT_ADOPTED";
|
|
7
|
+
export declare class CardHoldStoreError extends Error {
|
|
8
|
+
readonly code: typeof CLAIM_NOT_ADOPTED | "CARD_HOLDS_UNAVAILABLE";
|
|
9
|
+
constructor(code: typeof CLAIM_NOT_ADOPTED | "CARD_HOLDS_UNAVAILABLE");
|
|
10
|
+
}
|
|
11
|
+
export interface PostgresCardHoldsOptions {
|
|
12
|
+
readonly client: SqlClient;
|
|
13
|
+
/** Whose holds these are: one deployment. Two deployments on one database never see each other's. */
|
|
14
|
+
readonly namespace: string;
|
|
15
|
+
/** The tables' prefix, optionally schema-qualified; `agentsafe_card` by default. */
|
|
16
|
+
readonly table?: string;
|
|
17
|
+
/** Seals what the rows keep of a grant and a claim; the key stays in the process. */
|
|
18
|
+
readonly seal: CardHoldSeal;
|
|
19
|
+
/** How this process admits a hold or a claim another replica made. */
|
|
20
|
+
readonly adopt: CardHoldAdoption;
|
|
21
|
+
/** This replica's name in a row, for an operator reading the table; random when absent. */
|
|
22
|
+
readonly replica?: string;
|
|
23
|
+
/** How long a claimed authorization waits for its result before it is forgotten. */
|
|
24
|
+
readonly claimRetentionMs?: number;
|
|
25
|
+
readonly clock?: () => number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Holds and claims shared by every replica, and kept across a restart. A
|
|
29
|
+
* hold is one row, unique on the card and the merchant; `hold` takes the
|
|
30
|
+
* key when its row is absent, spent or expired, in one statement, so of two
|
|
31
|
+
* purchases racing for the same card at the same merchant the database lets
|
|
32
|
+
* one hold. `spend` sets `used` on the row that is still unspent and
|
|
33
|
+
* unexpired, in one statement, so of two authorizations racing for the
|
|
34
|
+
* same hold the database lets one claim: the same race rule as in memory,
|
|
35
|
+
* decided by the database instead of by one process's memory.
|
|
36
|
+
*
|
|
37
|
+
* What a row keeps of the grant, and later of the claim, is sealed under
|
|
38
|
+
* the deployment's hold key, so the database holds no bearer credential in
|
|
39
|
+
* clear; the columns outside the seal are references, times and flags. A
|
|
40
|
+
* hold another replica made is admitted here on first use, through the
|
|
41
|
+
* executor's own checks, and a claim another replica made is finalized here
|
|
42
|
+
* with the material that replica sealed. The operator creates the tables
|
|
43
|
+
* (`PostgresCardHolds.schema()`); the executor's role needs SELECT, INSERT,
|
|
44
|
+
* UPDATE and DELETE on them and nothing else.
|
|
45
|
+
*/
|
|
46
|
+
export declare class PostgresCardHolds implements CardHoldStore {
|
|
47
|
+
readonly kind: "postgres";
|
|
48
|
+
private readonly client;
|
|
49
|
+
private readonly namespace;
|
|
50
|
+
private readonly holds;
|
|
51
|
+
private readonly claims;
|
|
52
|
+
private readonly activity;
|
|
53
|
+
private readonly seal;
|
|
54
|
+
private readonly adopt;
|
|
55
|
+
private readonly replica;
|
|
56
|
+
private readonly claimRetentionMs;
|
|
57
|
+
private readonly clock;
|
|
58
|
+
/** The handles this process holds, by key, each valid for the intent it was made for. */
|
|
59
|
+
private readonly localHolds;
|
|
60
|
+
private readonly localClaims;
|
|
61
|
+
/** The claim material each claim was recorded with here, for re-sealing an amendment. */
|
|
62
|
+
private readonly materials;
|
|
63
|
+
constructor(options: PostgresCardHoldsOptions);
|
|
64
|
+
/** The DDL an operator runs once, before the first replica starts. */
|
|
65
|
+
static schema(table?: string): string;
|
|
66
|
+
spendable(key: CardHoldKey): Promise<boolean>;
|
|
67
|
+
hold(grant: HeldCardGrant): Promise<boolean>;
|
|
68
|
+
grant(key: CardHoldKey): Promise<HeldCardGrantRecord | undefined>;
|
|
69
|
+
spend(grant: HeldCardGrant): Promise<boolean>;
|
|
70
|
+
claimed(authorizationId: string): Promise<ClaimedCardAuthorization | undefined>;
|
|
71
|
+
recordClaim(authorizationId: string, claim: CardClaimInput): Promise<ClaimedCardAuthorization>;
|
|
72
|
+
amend(authorizationId: string, request: CardAuthorizationRequest): Promise<boolean>;
|
|
73
|
+
settle(authorizationId: string): Promise<boolean>;
|
|
74
|
+
velocity(cardTokenRef: string, windowMs?: number): Promise<CardVelocity>;
|
|
75
|
+
/** Throws unless every table exists with the columns this store uses. */
|
|
76
|
+
ready(): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Forgets what lapsed: a hold a claim's retention after its expiry (so a
|
|
79
|
+
* late authorization is still told its grant expired), a claim after its
|
|
80
|
+
* retention, and activity older than the velocity window plus a day.
|
|
81
|
+
*/
|
|
82
|
+
cleanup(): Promise<void>;
|
|
83
|
+
close(): Promise<void>;
|
|
84
|
+
private note;
|
|
85
|
+
private sealClaim;
|
|
86
|
+
/** The material a claim was recorded with here, kept only in the row; re-read for an amendment. */
|
|
87
|
+
private materialOf;
|
|
88
|
+
private holdIdentity;
|
|
89
|
+
private claimIdentity;
|
|
90
|
+
private iso;
|
|
91
|
+
private static holdRow;
|
|
92
|
+
private static claimRow;
|
|
93
|
+
}
|
|
94
|
+
export {};
|
|
95
|
+
//# sourceMappingURL=PostgresCardHolds.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PostgresCardHolds.d.ts","sourceRoot":"","sources":["../../../src/adapters/cards/PostgresCardHolds.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAIV,SAAS,EAEV,MAAM,+BAA+B,CAAC;AAEvC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAKL,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,wBAAwB,EAC7B,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACzB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,wBAAwB,EAAgB,MAAM,mBAAmB,CAAC;AAEhF,eAAO,MAAM,wBAAwB,mBAAmB,CAAC;AAKzD,QAAA,MAAM,iBAAiB,sBAAsB,CAAC;AAM9C,qBAAa,kBAAmB,SAAQ,KAAK;aACR,IAAI,EAAE,OAAO,iBAAiB,GAAG,wBAAwB;gBAAzD,IAAI,EAAE,OAAO,iBAAiB,GAAG,wBAAwB;CAI7F;AAED,MAAM,WAAW,wBAAwB;IACvC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,qGAAqG;IACrG,QAAQ,CAAC,SAAS,EAAE,MAAM,C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