@yielded/auth-persistence 0.1.0-beta.10
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 +27 -0
- package/dist/Adapter.d.mts +30 -0
- package/dist/Adapter.mjs +25 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.mjs +7 -0
- package/dist/internal/PasswordPreparedPostconditions.d.mts +22 -0
- package/dist/internal/PasswordPreparedPostconditions.mjs +12 -0
- package/dist/internal/PasswordPreparedPostconditions.mjs.map +1 -0
- package/dist/internal/configuration.d.mts +174 -0
- package/dist/internal/configuration.mjs +7 -0
- package/dist/internal/configuration.mjs.map +1 -0
- package/dist/internal/email-kernel.d.mts +275 -0
- package/dist/internal/email-kernel.mjs +487 -0
- package/dist/internal/email-kernel.mjs.map +1 -0
- package/dist/internal/mapping-error.d.mts +10 -0
- package/dist/internal/mapping-error.mjs +30 -0
- package/dist/internal/mapping-error.mjs.map +1 -0
- package/dist/internal/models/common.d.mts +12 -0
- package/dist/internal/models/email-model.d.mts +353 -0
- package/dist/internal/models/email-model.mjs +21 -0
- package/dist/internal/models/email-model.mjs.map +1 -0
- package/dist/internal/models/passkey-model.d.mts +266 -0
- package/dist/internal/models/passkey-model.mjs +34 -0
- package/dist/internal/models/passkey-model.mjs.map +1 -0
- package/dist/internal/models/passkey-registration-ceremony-model.d.mts +49 -0
- package/dist/internal/models/passkey-registration-ceremony-model.mjs +18 -0
- package/dist/internal/models/passkey-registration-ceremony-model.mjs.map +1 -0
- package/dist/internal/models/passkey-write-model.d.mts +189 -0
- package/dist/internal/models/passkey-write-model.mjs +13 -0
- package/dist/internal/models/passkey-write-model.mjs.map +1 -0
- package/dist/internal/models/password-model.d.mts +328 -0
- package/dist/internal/models/password-model.mjs +23 -0
- package/dist/internal/models/password-model.mjs.map +1 -0
- package/dist/internal/models/proof-model.d.mts +285 -0
- package/dist/internal/models/proof-model.mjs +17 -0
- package/dist/internal/models/proof-model.mjs.map +1 -0
- package/dist/internal/models/session-model.d.mts +199 -0
- package/dist/internal/models/session-model.mjs +20 -0
- package/dist/internal/models/session-model.mjs.map +1 -0
- package/dist/internal/models/step-up-model.d.mts +66 -0
- package/dist/internal/models/step-up-model.mjs +9 -0
- package/dist/internal/models/step-up-model.mjs.map +1 -0
- package/dist/internal/oauth-app.d.mts +11 -0
- package/dist/internal/oauth-app.mjs +57 -0
- package/dist/internal/oauth-app.mjs.map +1 -0
- package/dist/internal/oauth-server.d.mts +10 -0
- package/dist/internal/oauth-server.mjs +66 -0
- package/dist/internal/oauth-server.mjs.map +1 -0
- package/dist/internal/passkey/admission.d.mts +6 -0
- package/dist/internal/passkey/admission.mjs +216 -0
- package/dist/internal/passkey/admission.mjs.map +1 -0
- package/dist/internal/passkey/credentials.d.mts +14 -0
- package/dist/internal/passkey/credentials.mjs +249 -0
- package/dist/internal/passkey/credentials.mjs.map +1 -0
- package/dist/internal/passkey/enrollment.mjs +138 -0
- package/dist/internal/passkey/enrollment.mjs.map +1 -0
- package/dist/internal/passkey/flow.d.mts +21 -0
- package/dist/internal/passkey/flow.mjs +452 -0
- package/dist/internal/passkey/flow.mjs.map +1 -0
- package/dist/internal/passkey/management.mjs +231 -0
- package/dist/internal/passkey/management.mjs.map +1 -0
- package/dist/internal/passkey/registration-ceremony.d.mts +9 -0
- package/dist/internal/passkey/registration-ceremony.mjs +129 -0
- package/dist/internal/passkey/registration-ceremony.mjs.map +1 -0
- package/dist/internal/passkey/registration-custody.d.mts +2 -0
- package/dist/internal/passkey/registration-custody.mjs +52 -0
- package/dist/internal/passkey/registration-custody.mjs.map +1 -0
- package/dist/internal/passkey/registration-write.mjs +211 -0
- package/dist/internal/passkey/registration-write.mjs.map +1 -0
- package/dist/internal/passkey/state.d.mts +10 -0
- package/dist/internal/passkey/state.mjs +234 -0
- package/dist/internal/passkey/state.mjs.map +1 -0
- package/dist/internal/passkey/target.d.mts +19 -0
- package/dist/internal/passkey/target.mjs +190 -0
- package/dist/internal/passkey/target.mjs.map +1 -0
- package/dist/internal/passkey/write-state.d.mts +17 -0
- package/dist/internal/passkey/write-state.mjs +345 -0
- package/dist/internal/passkey/write-state.mjs.map +1 -0
- package/dist/internal/passkey/write-target.mjs +192 -0
- package/dist/internal/passkey/write-target.mjs.map +1 -0
- package/dist/internal/passkey-kernel.d.mts +5592 -0
- package/dist/internal/passkey-kernel.mjs +49 -0
- package/dist/internal/passkey-kernel.mjs.map +1 -0
- package/dist/internal/passkeys.d.mts +7 -0
- package/dist/internal/passkeys.mjs +368 -0
- package/dist/internal/passkeys.mjs.map +1 -0
- package/dist/internal/password-kernel.d.mts +290 -0
- package/dist/internal/password-kernel.mjs +704 -0
- package/dist/internal/password-kernel.mjs.map +1 -0
- package/dist/internal/persistence.d.mts +25 -0
- package/dist/internal/persistence.mjs +291 -0
- package/dist/internal/persistence.mjs.map +1 -0
- package/dist/internal/phone-kernel.d.mts +162 -0
- package/dist/internal/phone-kernel.mjs +434 -0
- package/dist/internal/phone-kernel.mjs.map +1 -0
- package/dist/internal/proof-kernel.d.mts +93 -0
- package/dist/internal/proof-kernel.mjs +619 -0
- package/dist/internal/proof-kernel.mjs.map +1 -0
- package/dist/internal/query-operations.d.mts +61 -0
- package/dist/internal/registration-contract.d.mts +16 -0
- package/dist/internal/registration.mjs +66 -0
- package/dist/internal/registration.mjs.map +1 -0
- package/dist/internal/session-kernel.d.mts +48 -0
- package/dist/internal/session-kernel.mjs +737 -0
- package/dist/internal/session-kernel.mjs.map +1 -0
- package/dist/internal/sql-persistence.d.mts +10 -0
- package/dist/internal/sql-persistence.mjs +37 -0
- package/dist/internal/sql-persistence.mjs.map +1 -0
- package/dist/internal/sql-query.mjs +207 -0
- package/dist/internal/sql-query.mjs.map +1 -0
- package/dist/internal/sql-table.d.mts +36 -0
- package/dist/internal/sql-table.mjs +81 -0
- package/dist/internal/sql-table.mjs.map +1 -0
- package/dist/internal/standalone.d.mts +9 -0
- package/dist/internal/standalone.mjs +13 -0
- package/dist/internal/standalone.mjs.map +1 -0
- package/dist/internal/step-up-state.d.mts +49 -0
- package/dist/internal/step-up-state.mjs +32 -0
- package/dist/internal/step-up-state.mjs.map +1 -0
- package/dist/internal/storage-mapping.mjs +686 -0
- package/dist/internal/storage-mapping.mjs.map +1 -0
- package/dist/internal/storage-tables.d.mts +913 -0
- package/dist/internal/storage-tables.mjs +387 -0
- package/dist/internal/storage-tables.mjs.map +1 -0
- package/dist/internal/storage-validation.mjs +58 -0
- package/dist/internal/storage-validation.mjs.map +1 -0
- package/dist/internal/transaction-execution-kernel.d.mts +38 -0
- package/dist/internal/transaction-execution-kernel.mjs +116 -0
- package/dist/internal/transaction-execution-kernel.mjs.map +1 -0
- package/dist/internal/transaction-kernel.d.mts +77 -0
- package/dist/internal/transaction-kernel.mjs +260 -0
- package/dist/internal/transaction-kernel.mjs.map +1 -0
- package/package.json +1 -0
- package/src/Adapter.ts +291 -0
- package/src/index.ts +6 -0
- package/src/internal/PasswordPreparedPostconditions.ts +30 -0
- package/src/internal/configuration.ts +282 -0
- package/src/internal/email-kernel.ts +1535 -0
- package/src/internal/mapping-error.ts +36 -0
- package/src/internal/models/common.ts +11 -0
- package/src/internal/models/email-model.ts +559 -0
- package/src/internal/models/passkey-model.ts +390 -0
- package/src/internal/models/passkey-registration-ceremony-model.ts +72 -0
- package/src/internal/models/passkey-write-model.ts +297 -0
- package/src/internal/models/password-model.ts +513 -0
- package/src/internal/models/proof-model.ts +424 -0
- package/src/internal/models/session-model.ts +375 -0
- package/src/internal/models/step-up-model.ts +135 -0
- package/src/internal/oauth-app.ts +82 -0
- package/src/internal/oauth-server.ts +89 -0
- package/src/internal/passkey/admission.ts +361 -0
- package/src/internal/passkey/credentials.ts +511 -0
- package/src/internal/passkey/enrollment.ts +326 -0
- package/src/internal/passkey/flow.ts +1062 -0
- package/src/internal/passkey/management.ts +421 -0
- package/src/internal/passkey/registration-ceremony.ts +283 -0
- package/src/internal/passkey/registration-custody.ts +91 -0
- package/src/internal/passkey/registration-write.ts +426 -0
- package/src/internal/passkey/state.ts +353 -0
- package/src/internal/passkey/target.ts +586 -0
- package/src/internal/passkey/write-state.ts +681 -0
- package/src/internal/passkey/write-target.ts +452 -0
- package/src/internal/passkey-kernel.ts +104 -0
- package/src/internal/passkeys.ts +606 -0
- package/src/internal/password-kernel.ts +2124 -0
- package/src/internal/persistence.ts +588 -0
- package/src/internal/phone-kernel.ts +846 -0
- package/src/internal/proof-kernel.ts +1954 -0
- package/src/internal/query-operations.ts +81 -0
- package/src/internal/registration-contract.ts +22 -0
- package/src/internal/registration.ts +161 -0
- package/src/internal/session-kernel.ts +2217 -0
- package/src/internal/sql-persistence.ts +45 -0
- package/src/internal/sql-query.ts +298 -0
- package/src/internal/sql-table.ts +104 -0
- package/src/internal/standalone.ts +30 -0
- package/src/internal/step-up-state.ts +139 -0
- package/src/internal/storage-mapping.ts +708 -0
- package/src/internal/storage-tables.ts +408 -0
- package/src/internal/storage-validation.ts +117 -0
- package/src/internal/transaction-execution-kernel.ts +313 -0
- package/src/internal/transaction-kernel.ts +593 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
import { PersistenceMappingError } from "./mapping-error.mjs";
|
|
2
|
+
import { requiredProofConstraints } from "./models/proof-model.mjs";
|
|
3
|
+
import { CurrentPasswordPreparedTransaction, PasswordPreparedPostconditions } from "./PasswordPreparedPostconditions.mjs";
|
|
4
|
+
import { Cause, Context, DateTime, Effect, Option, Schema } from "effect";
|
|
5
|
+
import { CurrentCommitJournal, HookConfigurationError, LifecycleHooks, coordinateCommit, hasCommitScope } from "@yielded/auth/Hooks";
|
|
6
|
+
import { reportPersistenceFailure } from "@yielded/auth/Persistence";
|
|
7
|
+
import { ProofBinding, ProofPersistence, ProofRequestConflict, ProofUnavailable } from "@yielded/auth/Proofs";
|
|
8
|
+
//#region src/internal/proof-kernel.ts
|
|
9
|
+
var CurrentProofSql = class extends Context.Service()("effect-auth/CurrentProofSql") {};
|
|
10
|
+
const makeProofKernel = (operations) => {
|
|
11
|
+
const { and, eq, gte, inArray, isNull, lte, notExists, sql, column, updateValues } = operations;
|
|
12
|
+
const unavailable = () => ProofUnavailable.make({});
|
|
13
|
+
const translateFailure = (effect) => reportPersistenceFailure(effect, (error) => Schema.is(ProofUnavailable)(error) || Schema.is(HookConfigurationError)(error)).pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, () => unavailable()))));
|
|
14
|
+
const translateIssueFailure = (effect) => reportPersistenceFailure(effect, (error) => Schema.is(ProofUnavailable)(error) || Schema.is(ProofRequestConflict)(error) || Schema.is(HookConfigurationError)(error)).pipe(Effect.catchCause((cause) => Effect.failCause(Cause.map(cause, (error) => Schema.is(ProofRequestConflict)(error) ? error : unavailable()))));
|
|
15
|
+
const conflict = () => ProofRequestConflict.make({});
|
|
16
|
+
const validConstraints = (mapping) => Object.entries(requiredProofConstraints).every(([key, value]) => mapping.constraints[key] === value);
|
|
17
|
+
const selectRows = (query, locking) => locking && typeof query.for === "function" ? query.for("update") : query;
|
|
18
|
+
const allocateVersion = (mapping, mode) => {
|
|
19
|
+
if (mode === "synchronous") return mapping.allocateVersionSync === void 0 ? Effect.fail(unavailable()) : Effect.try({
|
|
20
|
+
try: mapping.allocateVersionSync,
|
|
21
|
+
catch: (cause) => new PersistenceMappingError({
|
|
22
|
+
operation: "mapping",
|
|
23
|
+
cause
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
if (mapping.allocateVersion !== void 0) return mapping.allocateVersion;
|
|
27
|
+
return mapping.allocateVersionSync === void 0 ? Effect.fail(unavailable()) : Effect.try({
|
|
28
|
+
try: mapping.allocateVersionSync,
|
|
29
|
+
catch: (cause) => new PersistenceMappingError({
|
|
30
|
+
operation: "mapping",
|
|
31
|
+
cause
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
const canonicalBinding = Schema.encodeSync(Schema.fromJsonString(ProofBinding));
|
|
36
|
+
const sameBinding = (left, right) => canonicalBinding(left) === canonicalBinding(right);
|
|
37
|
+
const requestColumns = (mapping) => ({
|
|
38
|
+
moduleId: column(mapping.request.table, mapping.request.moduleId),
|
|
39
|
+
requestId: column(mapping.request.table, mapping.request.requestId),
|
|
40
|
+
fingerprint: column(mapping.request.table, mapping.request.fingerprint),
|
|
41
|
+
proofId: column(mapping.request.table, mapping.request.proofId),
|
|
42
|
+
retentionUntil: column(mapping.request.table, mapping.request.retentionUntil)
|
|
43
|
+
});
|
|
44
|
+
const seriesColumns = (mapping) => ({
|
|
45
|
+
moduleId: column(mapping.series.table, mapping.series.moduleId),
|
|
46
|
+
purpose: column(mapping.series.table, mapping.series.purpose),
|
|
47
|
+
scopeKey: column(mapping.series.table, mapping.series.scopeKey),
|
|
48
|
+
activeProofId: column(mapping.series.table, mapping.series.activeProofId),
|
|
49
|
+
lastIssueAt: column(mapping.series.table, mapping.series.lastIssueAt),
|
|
50
|
+
version: column(mapping.series.table, mapping.series.version)
|
|
51
|
+
});
|
|
52
|
+
const generationColumns = (mapping) => ({
|
|
53
|
+
moduleId: column(mapping.generation.table, mapping.generation.moduleId),
|
|
54
|
+
purpose: column(mapping.generation.table, mapping.generation.purpose),
|
|
55
|
+
proofId: column(mapping.generation.table, mapping.generation.proofId),
|
|
56
|
+
requestId: column(mapping.generation.table, mapping.generation.requestId),
|
|
57
|
+
seriesKey: column(mapping.generation.table, mapping.generation.seriesKey),
|
|
58
|
+
deliveryId: column(mapping.generation.table, mapping.generation.deliveryId),
|
|
59
|
+
verifierKeyId: column(mapping.generation.table, mapping.generation.verifierKeyId),
|
|
60
|
+
verifierDigest: column(mapping.generation.table, mapping.generation.verifierDigest),
|
|
61
|
+
issuedAt: column(mapping.generation.table, mapping.generation.issuedAt),
|
|
62
|
+
expiresAt: column(mapping.generation.table, mapping.generation.expiresAt),
|
|
63
|
+
version: column(mapping.generation.table, mapping.generation.version),
|
|
64
|
+
state: column(mapping.generation.table, mapping.generation.state),
|
|
65
|
+
sendCount: column(mapping.generation.table, mapping.generation.sendCount),
|
|
66
|
+
deliveryState: column(mapping.generation.table, mapping.generation.deliveryState),
|
|
67
|
+
claimVersion: column(mapping.generation.table, mapping.generation.claimVersion),
|
|
68
|
+
claimDeadline: column(mapping.generation.table, mapping.generation.claimDeadline),
|
|
69
|
+
retryAt: column(mapping.generation.table, mapping.generation.retryAt),
|
|
70
|
+
deliveryRetryMillis: column(mapping.generation.table, mapping.generation.deliveryRetryMillis),
|
|
71
|
+
retentionUntil: column(mapping.generation.table, mapping.generation.retentionUntil)
|
|
72
|
+
});
|
|
73
|
+
const continuationColumns = (mapping) => ({
|
|
74
|
+
moduleId: column(mapping.continuation.table, mapping.continuation.moduleId),
|
|
75
|
+
purpose: column(mapping.continuation.table, mapping.continuation.purpose),
|
|
76
|
+
continuationId: column(mapping.continuation.table, mapping.continuation.continuationId),
|
|
77
|
+
digest: column(mapping.continuation.table, mapping.continuation.digest),
|
|
78
|
+
proofId: column(mapping.continuation.table, mapping.continuation.proofId),
|
|
79
|
+
seriesKey: column(mapping.continuation.table, mapping.continuation.seriesKey),
|
|
80
|
+
expiresAt: column(mapping.continuation.table, mapping.continuation.expiresAt),
|
|
81
|
+
consumed: column(mapping.continuation.table, mapping.continuation.consumed),
|
|
82
|
+
version: column(mapping.continuation.table, mapping.continuation.version),
|
|
83
|
+
retentionUntil: column(mapping.continuation.table, mapping.continuation.retentionUntil)
|
|
84
|
+
});
|
|
85
|
+
const scopeColumns = (mapping) => ({
|
|
86
|
+
moduleId: column(mapping.rateScope.table, mapping.rateScope.moduleId),
|
|
87
|
+
purpose: column(mapping.rateScope.table, mapping.rateScope.purpose),
|
|
88
|
+
action: column(mapping.rateScope.table, mapping.rateScope.action),
|
|
89
|
+
scopeKind: column(mapping.rateScope.table, mapping.rateScope.scopeKind),
|
|
90
|
+
scopeKey: column(mapping.rateScope.table, mapping.rateScope.scopeKey)
|
|
91
|
+
});
|
|
92
|
+
const abuseColumns = (mapping) => ({
|
|
93
|
+
moduleId: column(mapping.abuseEvent.table, mapping.abuseEvent.moduleId),
|
|
94
|
+
purpose: column(mapping.abuseEvent.table, mapping.abuseEvent.purpose),
|
|
95
|
+
action: column(mapping.abuseEvent.table, mapping.abuseEvent.action),
|
|
96
|
+
scopeKind: column(mapping.abuseEvent.table, mapping.abuseEvent.scopeKind),
|
|
97
|
+
scopeKey: column(mapping.abuseEvent.table, mapping.abuseEvent.scopeKey),
|
|
98
|
+
commandId: column(mapping.abuseEvent.table, mapping.abuseEvent.commandId),
|
|
99
|
+
occurredAt: column(mapping.abuseEvent.table, mapping.abuseEvent.occurredAt),
|
|
100
|
+
retentionUntil: column(mapping.abuseEvent.table, mapping.abuseEvent.retentionUntil)
|
|
101
|
+
});
|
|
102
|
+
const failureColumns = (mapping) => ({
|
|
103
|
+
moduleId: column(mapping.failureEvent.table, mapping.failureEvent.moduleId),
|
|
104
|
+
purpose: column(mapping.failureEvent.table, mapping.failureEvent.purpose),
|
|
105
|
+
seriesKey: column(mapping.failureEvent.table, mapping.failureEvent.seriesKey),
|
|
106
|
+
commandId: column(mapping.failureEvent.table, mapping.failureEvent.commandId),
|
|
107
|
+
occurredAt: column(mapping.failureEvent.table, mapping.failureEvent.occurredAt),
|
|
108
|
+
retentionUntil: column(mapping.failureEvent.table, mapping.failureEvent.retentionUntil)
|
|
109
|
+
});
|
|
110
|
+
const commandColumns = (mapping) => ({
|
|
111
|
+
moduleId: column(mapping.command.table, mapping.command.moduleId),
|
|
112
|
+
commandId: column(mapping.command.table, mapping.command.commandId),
|
|
113
|
+
kind: column(mapping.command.table, mapping.command.kind),
|
|
114
|
+
decision: column(mapping.command.table, mapping.command.decision),
|
|
115
|
+
retentionUntil: column(mapping.command.table, mapping.command.retentionUntil)
|
|
116
|
+
});
|
|
117
|
+
const nowMillis = DateTime.now.pipe(Effect.map(DateTime.toEpochMillis));
|
|
118
|
+
const lockAuthority = Effect.fn("DrizzleProof.lockAuthority")(function* (mapping, moduleId, purpose, binding, locking) {
|
|
119
|
+
const database = yield* CurrentProofSql;
|
|
120
|
+
const authority = mapping.authority;
|
|
121
|
+
let nativeSubjectId;
|
|
122
|
+
if (binding._tag !== "Identifier") {
|
|
123
|
+
if (authority.subject === void 0 || authority.credential === void 0 || authority.subjectId === void 0) return yield* unavailable();
|
|
124
|
+
nativeSubjectId = yield* authority.subjectId.toNative(binding.revision.subjectId);
|
|
125
|
+
const subject = authority.subject;
|
|
126
|
+
const row = (yield* selectRows(database.select().from(subject.table).where(eq(column(subject.table, subject.id), nativeSubjectId)).limit(1), locking))[0];
|
|
127
|
+
if (row === void 0 || !subject.isActiveStatus(row[subject.status]) || row[subject.securityRevision] !== binding.revision.securityRevision) return false;
|
|
128
|
+
}
|
|
129
|
+
const identifier = authority.identifier;
|
|
130
|
+
const identifierRows = yield* selectRows(database.select().from(identifier.table).where(and(eq(column(identifier.table, identifier.namespace), binding.identifier.namespace), eq(column(identifier.table, identifier.value), binding.identifier.value))), locking);
|
|
131
|
+
if (!identifier.isCurrent({
|
|
132
|
+
moduleId,
|
|
133
|
+
purpose,
|
|
134
|
+
binding,
|
|
135
|
+
...nativeSubjectId === void 0 ? {} : { nativeSubjectId }
|
|
136
|
+
}, identifierRows)) return false;
|
|
137
|
+
if (binding._tag === "Identifier") return true;
|
|
138
|
+
const credential = authority.credential;
|
|
139
|
+
const expected = [...binding.revision.credentials].sort((a, b) => a.credentialId.localeCompare(b.credentialId));
|
|
140
|
+
const rows = expected.length === 0 ? [] : yield* selectRows(database.select().from(credential.table).where(and(eq(column(credential.table, credential.subjectId), nativeSubjectId), inArray(column(credential.table, credential.credentialId), expected.map((item) => item.credentialId)))), locking);
|
|
141
|
+
if (rows.length !== expected.length) return false;
|
|
142
|
+
return rows.map((row) => ({
|
|
143
|
+
credentialId: row[credential.credentialId],
|
|
144
|
+
revision: row[credential.revision],
|
|
145
|
+
active: credential.status === void 0 || credential.isActiveStatus?.(row[credential.status]) === true
|
|
146
|
+
})).sort((a, b) => a.credentialId.localeCompare(b.credentialId)).every((item, index) => item.active && item.credentialId === expected[index]?.credentialId && item.revision === expected[index]?.revision);
|
|
147
|
+
});
|
|
148
|
+
const scopeEntries = (keys, binding, action, policy) => {
|
|
149
|
+
const suffix = action === "issue" ? "Issues" : "Attempts";
|
|
150
|
+
const simple = action === "issue" ? "issues" : "attempts";
|
|
151
|
+
const entries = [{
|
|
152
|
+
kind: "action",
|
|
153
|
+
key: "*",
|
|
154
|
+
budget: policy.abuse[`action${suffix}`]
|
|
155
|
+
}, {
|
|
156
|
+
kind: "identifier",
|
|
157
|
+
key: keys.identifier,
|
|
158
|
+
budget: policy.abuse[simple]
|
|
159
|
+
}];
|
|
160
|
+
if (binding._tag !== "Identifier") entries.push({
|
|
161
|
+
kind: "subject",
|
|
162
|
+
key: keys.subject,
|
|
163
|
+
budget: policy.abuse[`subject${suffix}`]
|
|
164
|
+
});
|
|
165
|
+
return entries;
|
|
166
|
+
};
|
|
167
|
+
const lockScopes = Effect.fn("DrizzleProof.lockScopes")(function* (mapping, configuration, moduleId, purpose, action, entries) {
|
|
168
|
+
const database = yield* CurrentProofSql;
|
|
169
|
+
const c = scopeColumns(mapping);
|
|
170
|
+
for (const entry of entries) {
|
|
171
|
+
const values = mapping.rateScope.encodeInsert({
|
|
172
|
+
moduleId,
|
|
173
|
+
purpose,
|
|
174
|
+
action,
|
|
175
|
+
scopeKind: entry.kind,
|
|
176
|
+
scopeKey: entry.key
|
|
177
|
+
});
|
|
178
|
+
const query = database.insert(mapping.rateScope.table).values(values);
|
|
179
|
+
yield* configuration.insertIfAbsent(query, mapping.rateScope.scopeKey, entry.key);
|
|
180
|
+
yield* selectRows(database.select().from(mapping.rateScope.table).where(and(eq(c.moduleId, moduleId), eq(c.purpose, purpose), eq(c.action, action), eq(c.scopeKind, entry.kind), eq(c.scopeKey, entry.key))).limit(1), configuration.locking);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
const admittedScopes = Effect.fn("DrizzleProof.admittedScopes")(function* (mapping, moduleId, purpose, action, entries, now) {
|
|
184
|
+
const database = yield* CurrentProofSql;
|
|
185
|
+
const c = abuseColumns(mapping);
|
|
186
|
+
const admitted = [];
|
|
187
|
+
for (const entry of entries) if ((yield* selectRows(database.select({ commandId: c.commandId }).from(mapping.abuseEvent.table).where(and(eq(c.moduleId, moduleId), eq(c.purpose, purpose), eq(c.action, action), eq(c.scopeKind, entry.kind), eq(c.scopeKey, entry.key), gte(c.occurredAt, mapping.encodeInstant(now - entry.budget.windowMillis)))).limit(entry.budget.limit), true)).length < entry.budget.limit) admitted.push(entry);
|
|
188
|
+
return admitted;
|
|
189
|
+
});
|
|
190
|
+
const insertScopeEvents = Effect.fn("Drizzle.insertScopeEvents")(function* (mapping, moduleId, purpose, action, commandId, entries, now, retentionUntil) {
|
|
191
|
+
const database = yield* CurrentProofSql;
|
|
192
|
+
return yield* Effect.forEach(entries, (entry) => database.insert(mapping.abuseEvent.table).values(mapping.abuseEvent.encodeInsert({
|
|
193
|
+
moduleId,
|
|
194
|
+
purpose,
|
|
195
|
+
action,
|
|
196
|
+
scopeKind: entry.kind,
|
|
197
|
+
scopeKey: entry.key,
|
|
198
|
+
commandId,
|
|
199
|
+
occurredAtMillis: now,
|
|
200
|
+
retentionUntilMillis: retentionUntil
|
|
201
|
+
})), { discard: true });
|
|
202
|
+
});
|
|
203
|
+
const ensureSeries = Effect.fn("DrizzleProof.ensureSeries")(function* (mapping, configuration, moduleId, purpose, scopeKey) {
|
|
204
|
+
const database = yield* CurrentProofSql;
|
|
205
|
+
const c = seriesColumns(mapping);
|
|
206
|
+
const initialVersion = yield* allocateVersion(mapping, configuration.mode);
|
|
207
|
+
const query = database.insert(mapping.series.table).values(mapping.series.encodeInsert({
|
|
208
|
+
moduleId,
|
|
209
|
+
purpose,
|
|
210
|
+
scopeKey,
|
|
211
|
+
version: initialVersion
|
|
212
|
+
}));
|
|
213
|
+
yield* configuration.insertIfAbsent(query, mapping.series.scopeKey, scopeKey);
|
|
214
|
+
return (yield* selectRows(database.select().from(mapping.series.table).where(and(eq(c.moduleId, moduleId), eq(c.purpose, purpose), eq(c.scopeKey, scopeKey))).limit(1), configuration.locking))[0] ?? (yield* unavailable());
|
|
215
|
+
});
|
|
216
|
+
const readGeneration = Effect.fn("Drizzle.readGeneration")(function* (mapping, moduleId, proofId, locking) {
|
|
217
|
+
const database = yield* CurrentProofSql;
|
|
218
|
+
const c = generationColumns(mapping);
|
|
219
|
+
return yield* selectRows(database.select().from(mapping.generation.table).where(and(eq(c.moduleId, moduleId), eq(c.proofId, proofId))).limit(1), locking);
|
|
220
|
+
});
|
|
221
|
+
const failureCount = Effect.fn("DrizzleProof.failureCount")(function* (mapping, moduleId, purpose, seriesKey, now, policy) {
|
|
222
|
+
const database = yield* CurrentProofSql;
|
|
223
|
+
const c = failureColumns(mapping);
|
|
224
|
+
return (yield* selectRows(database.select({ commandId: c.commandId }).from(mapping.failureEvent.table).where(and(eq(c.moduleId, moduleId), eq(c.purpose, purpose), eq(c.seriesKey, seriesKey), gte(c.occurredAt, mapping.encodeInstant(now - policy.abuse.attempts.windowMillis)))).limit(policy.maximumFailedAttempts), true)).length;
|
|
225
|
+
});
|
|
226
|
+
const commandRow = Effect.fn("Drizzle.commandRow")(function* (mapping, moduleId, commandId, locking) {
|
|
227
|
+
const database = yield* CurrentProofSql;
|
|
228
|
+
const c = commandColumns(mapping);
|
|
229
|
+
return yield* selectRows(database.select().from(mapping.command.table).where(and(eq(c.moduleId, moduleId), eq(c.commandId, commandId))).limit(1), locking);
|
|
230
|
+
});
|
|
231
|
+
const insertCommand = Effect.fn("Drizzle.insertCommand")(function* (mapping, moduleId, commandId, kind, decision, retentionUntil) {
|
|
232
|
+
return yield* (yield* CurrentProofSql).insert(mapping.command.table).values(mapping.command.encodeInsert({
|
|
233
|
+
moduleId,
|
|
234
|
+
commandId,
|
|
235
|
+
kind,
|
|
236
|
+
decision,
|
|
237
|
+
retentionUntilMillis: retentionUntil
|
|
238
|
+
}));
|
|
239
|
+
});
|
|
240
|
+
const owned = (database, mapping, configuration, body) => {
|
|
241
|
+
if (!validConstraints(mapping)) return Effect.fail(unavailable());
|
|
242
|
+
const run = coordinateCommit(() => database.transaction((transaction) => body.pipe(Effect.provideService(CurrentProofSql, transaction))), { mode: configuration.mode }).pipe(Effect.map((result) => result.value));
|
|
243
|
+
return configuration.coordinated !== true ? Effect.gen(function* () {
|
|
244
|
+
if (yield* hasCommitScope) return yield* unavailable();
|
|
245
|
+
yield* configuration.standaloneGuard;
|
|
246
|
+
return yield* run;
|
|
247
|
+
}) : run;
|
|
248
|
+
};
|
|
249
|
+
const makeSqlProofPersistence = Effect.fn("makeSqlProofPersistence")(function* (database, mapping, configuration) {
|
|
250
|
+
const hooks = yield* LifecycleHooks;
|
|
251
|
+
return ProofPersistence.of({
|
|
252
|
+
issue: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
253
|
+
const transaction = yield* CurrentProofSql;
|
|
254
|
+
const journal = yield* CurrentCommitJournal;
|
|
255
|
+
const keys = mapping.scopeKeys({
|
|
256
|
+
moduleId: input.record.moduleId,
|
|
257
|
+
purpose: input.record.purpose,
|
|
258
|
+
binding: input.record.binding
|
|
259
|
+
});
|
|
260
|
+
const authorityCurrent = yield* lockAuthority(mapping, input.record.moduleId, input.record.purpose, input.record.binding, configuration.locking);
|
|
261
|
+
const scopes = scopeEntries(keys, input.record.binding, "issue", input.policy);
|
|
262
|
+
yield* lockScopes(mapping, configuration, input.record.moduleId, input.record.purpose, "issue", scopes.slice(0, 1));
|
|
263
|
+
const actionNow = yield* nowMillis;
|
|
264
|
+
const actionOpen = (yield* admittedScopes(mapping, input.record.moduleId, input.record.purpose, "issue", scopes.slice(0, 1), actionNow)).length === 1;
|
|
265
|
+
if (actionOpen) yield* lockScopes(mapping, configuration, input.record.moduleId, input.record.purpose, "issue", scopes.slice(1));
|
|
266
|
+
const series = actionOpen ? yield* ensureSeries(mapping, configuration, input.record.moduleId, input.record.purpose, keys.series) : void 0;
|
|
267
|
+
const now = yield* nowMillis;
|
|
268
|
+
const retentionUntil = now + input.policy.requestRetentionMillis;
|
|
269
|
+
const rc = requestColumns(mapping);
|
|
270
|
+
let requestRows = yield* selectRows(transaction.select().from(mapping.request.table).where(and(eq(rc.moduleId, input.record.moduleId), eq(rc.requestId, input.record.requestId))).limit(1), configuration.locking);
|
|
271
|
+
let request = requestRows[0];
|
|
272
|
+
if (request !== void 0) {
|
|
273
|
+
if ((yield* mapping.decodeInstant(request[mapping.request.retentionUntil])) > now) {
|
|
274
|
+
if (request[mapping.request.fingerprint] !== input.record.fingerprint) return yield* conflict();
|
|
275
|
+
return prepare({
|
|
276
|
+
_tag: "Existing",
|
|
277
|
+
receipt: yield* mapping.request.decodeReceipt(request)
|
|
278
|
+
}, journal);
|
|
279
|
+
}
|
|
280
|
+
yield* transaction.delete(mapping.request.table).where(and(eq(rc.moduleId, input.record.moduleId), eq(rc.requestId, input.record.requestId), lte(rc.retentionUntil, mapping.encodeInstant(now))));
|
|
281
|
+
}
|
|
282
|
+
const requestInsert = mapping.request.encodeInsert({
|
|
283
|
+
record: input.record,
|
|
284
|
+
createdAtMillis: now,
|
|
285
|
+
retentionUntilMillis: retentionUntil
|
|
286
|
+
});
|
|
287
|
+
const requestQuery = transaction.insert(mapping.request.table).values(requestInsert);
|
|
288
|
+
yield* configuration.insertIfAbsent(requestQuery, mapping.request.requestId, input.record.requestId);
|
|
289
|
+
requestRows = yield* selectRows(transaction.select().from(mapping.request.table).where(and(eq(rc.moduleId, input.record.moduleId), eq(rc.requestId, input.record.requestId))).limit(1), configuration.locking);
|
|
290
|
+
request = requestRows[0];
|
|
291
|
+
if (request === void 0) return yield* unavailable();
|
|
292
|
+
if (request[mapping.request.fingerprint] !== input.record.fingerprint) return yield* conflict();
|
|
293
|
+
if (request[mapping.request.proofId] !== input.record.proofId) return prepare({
|
|
294
|
+
_tag: "Existing",
|
|
295
|
+
receipt: yield* mapping.request.decodeReceipt(request)
|
|
296
|
+
}, journal);
|
|
297
|
+
const admitted = actionOpen ? yield* admittedScopes(mapping, input.record.moduleId, input.record.purpose, "issue", scopes, now) : [];
|
|
298
|
+
const sc = seriesColumns(mapping);
|
|
299
|
+
const lastIssueAt = series?.[mapping.series.lastIssueAt] === null || series?.[mapping.series.lastIssueAt] === void 0 ? void 0 : yield* mapping.decodeInstant(series[mapping.series.lastIssueAt]);
|
|
300
|
+
const activeProofId = series?.[mapping.series.activeProofId];
|
|
301
|
+
const supersedesCurrent = input.supersedes === void 0 || input.supersedes === activeProofId;
|
|
302
|
+
const allowed = authorityCurrent && input.eligible && admitted.length === scopes.length && input.record.issuedAtMillis <= now && input.record.expiresAtMillis > now && supersedesCurrent && (lastIssueAt === void 0 || now - lastIssueAt >= input.policy.abuse.resendCooldownMillis);
|
|
303
|
+
const receipt = {
|
|
304
|
+
requestId: input.record.requestId,
|
|
305
|
+
reference: {
|
|
306
|
+
proofId: input.record.proofId,
|
|
307
|
+
purpose: input.record.purpose,
|
|
308
|
+
keyId: input.record.verifier.keyId
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
const prepared = prepare(allowed ? {
|
|
312
|
+
_tag: "Issued",
|
|
313
|
+
record: input.record
|
|
314
|
+
} : {
|
|
315
|
+
_tag: "Suppressed",
|
|
316
|
+
receipt
|
|
317
|
+
}, journal);
|
|
318
|
+
if (admitted.length > 0) yield* insertScopeEvents(mapping, input.record.moduleId, input.record.purpose, "issue", input.record.requestId, admitted, now, retentionUntil);
|
|
319
|
+
if (!allowed) return prepared;
|
|
320
|
+
if (activeProofId !== null && activeProofId !== void 0) yield* transaction.update(mapping.generation.table).set(updateValues([[mapping.generation.state, "superseded"]])).where(and(eq(generationColumns(mapping).moduleId, input.record.moduleId), eq(generationColumns(mapping).proofId, activeProofId), eq(generationColumns(mapping).state, "active")));
|
|
321
|
+
yield* transaction.insert(mapping.generation.table).values(mapping.generation.encodeInsert({
|
|
322
|
+
record: input.record,
|
|
323
|
+
seriesKey: keys.series,
|
|
324
|
+
retentionUntilMillis: retentionUntil,
|
|
325
|
+
state: "active",
|
|
326
|
+
deliveryState: "new",
|
|
327
|
+
policy: input.policy
|
|
328
|
+
}));
|
|
329
|
+
const nextVersion = yield* allocateVersion(mapping, configuration.mode);
|
|
330
|
+
yield* transaction.update(mapping.series.table).set(updateValues([
|
|
331
|
+
[mapping.series.activeProofId, input.record.proofId],
|
|
332
|
+
[mapping.series.lastIssueAt, mapping.encodeInstant(now)],
|
|
333
|
+
[mapping.series.version, nextVersion]
|
|
334
|
+
])).where(and(eq(sc.moduleId, input.record.moduleId), eq(sc.purpose, input.record.purpose), eq(sc.scopeKey, keys.series)));
|
|
335
|
+
return prepared;
|
|
336
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateIssueFailure),
|
|
337
|
+
attempt: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
338
|
+
const transaction = yield* CurrentProofSql;
|
|
339
|
+
const journal = yield* CurrentCommitJournal;
|
|
340
|
+
const keys = mapping.scopeKeys({
|
|
341
|
+
moduleId: input.moduleId,
|
|
342
|
+
purpose: input.purpose,
|
|
343
|
+
binding: input.binding
|
|
344
|
+
});
|
|
345
|
+
const authorityCurrent = yield* lockAuthority(mapping, input.moduleId, input.purpose, input.binding, configuration.locking);
|
|
346
|
+
const scopes = scopeEntries(keys, input.binding, "attempt", input.policy);
|
|
347
|
+
yield* lockScopes(mapping, configuration, input.moduleId, input.purpose, "attempt", scopes.slice(0, 1));
|
|
348
|
+
const actionNow = yield* nowMillis;
|
|
349
|
+
const actionOpen = (yield* admittedScopes(mapping, input.moduleId, input.purpose, "attempt", scopes.slice(0, 1), actionNow)).length === 1;
|
|
350
|
+
if (actionOpen) yield* lockScopes(mapping, configuration, input.moduleId, input.purpose, "attempt", scopes.slice(1));
|
|
351
|
+
const series = actionOpen ? yield* ensureSeries(mapping, configuration, input.moduleId, input.purpose, keys.series) : void 0;
|
|
352
|
+
const generation = (yield* readGeneration(mapping, input.moduleId, input.proofId, configuration.locking))[0];
|
|
353
|
+
const existingCommand = (yield* commandRow(mapping, input.moduleId, input.continuationId, configuration.locking))[0];
|
|
354
|
+
if (existingCommand !== void 0) {
|
|
355
|
+
if (existingCommand[mapping.command.decision] === "rejected") return prepare({ _tag: "Rejected" }, journal);
|
|
356
|
+
return yield* unavailable();
|
|
357
|
+
}
|
|
358
|
+
const now = yield* nowMillis;
|
|
359
|
+
const admitted = actionOpen ? yield* admittedScopes(mapping, input.moduleId, input.purpose, "attempt", scopes, now) : [];
|
|
360
|
+
const failures = yield* failureCount(mapping, input.moduleId, input.purpose, keys.series, now, input.policy);
|
|
361
|
+
const gc = generationColumns(mapping);
|
|
362
|
+
const activeProofId = series?.[mapping.series.activeProofId];
|
|
363
|
+
const storedBinding = generation === void 0 ? void 0 : yield* mapping.generation.decodeBinding(generation);
|
|
364
|
+
const candidateMatches = generation !== void 0 && input.candidate !== void 0 && generation[mapping.generation.verifierKeyId] === input.candidate.keyId && generation[mapping.generation.verifierDigest] === input.candidate.digest;
|
|
365
|
+
const currentGeneration = generation !== void 0 && generation[mapping.generation.purpose] === input.purpose && generation[mapping.generation.state] === "active" && activeProofId === input.proofId && storedBinding !== void 0 && sameBinding(storedBinding, input.binding) && Number(yield* mapping.decodeInstant(generation[mapping.generation.expiresAt])) > now;
|
|
366
|
+
const accepted = authorityCurrent && currentGeneration && candidateMatches && admitted.length === scopes.length && failures < input.policy.maximumFailedAttempts;
|
|
367
|
+
const retentionUntil = now + input.policy.requestRetentionMillis;
|
|
368
|
+
if (!accepted) {
|
|
369
|
+
const prepared = prepare({ _tag: "Rejected" }, journal);
|
|
370
|
+
yield* insertCommand(mapping, input.moduleId, input.continuationId, "attempt", "rejected", retentionUntil);
|
|
371
|
+
yield* insertScopeEvents(mapping, input.moduleId, input.purpose, "attempt", input.continuationId, admitted, now, retentionUntil);
|
|
372
|
+
if (admitted.length === scopes.length && currentGeneration && !candidateMatches && failures < input.policy.maximumFailedAttempts) yield* transaction.insert(mapping.failureEvent.table).values(mapping.failureEvent.encodeInsert({
|
|
373
|
+
moduleId: input.moduleId,
|
|
374
|
+
purpose: input.purpose,
|
|
375
|
+
seriesKey: keys.series,
|
|
376
|
+
commandId: input.continuationId,
|
|
377
|
+
occurredAtMillis: now,
|
|
378
|
+
retentionUntilMillis: retentionUntil
|
|
379
|
+
}));
|
|
380
|
+
return prepared;
|
|
381
|
+
}
|
|
382
|
+
const version = yield* allocateVersion(mapping, configuration.mode);
|
|
383
|
+
const expiresAt = Math.min(Number(yield* mapping.decodeInstant(generation[mapping.generation.expiresAt])), now + input.policy.continuationLifetimeMillis);
|
|
384
|
+
const continuation = {
|
|
385
|
+
moduleId: input.moduleId,
|
|
386
|
+
purpose: input.purpose,
|
|
387
|
+
continuationId: input.continuationId,
|
|
388
|
+
digest: input.continuationDigest,
|
|
389
|
+
proofId: input.proofId,
|
|
390
|
+
seriesKey: keys.series,
|
|
391
|
+
binding: input.binding,
|
|
392
|
+
expiresAtMillis: expiresAt,
|
|
393
|
+
version
|
|
394
|
+
};
|
|
395
|
+
const prepared = prepare({
|
|
396
|
+
_tag: "Accepted",
|
|
397
|
+
continuation: {
|
|
398
|
+
continuationId: input.continuationId,
|
|
399
|
+
purpose: input.purpose,
|
|
400
|
+
expiresAtMillis: expiresAt
|
|
401
|
+
}
|
|
402
|
+
}, journal);
|
|
403
|
+
yield* insertCommand(mapping, input.moduleId, input.continuationId, "attempt", "accepted", retentionUntil);
|
|
404
|
+
yield* insertScopeEvents(mapping, input.moduleId, input.purpose, "attempt", input.continuationId, scopes, now, retentionUntil);
|
|
405
|
+
yield* transaction.update(mapping.generation.table).set(updateValues([[mapping.generation.state, "consumed"]])).where(and(eq(gc.moduleId, input.moduleId), eq(gc.proofId, input.proofId), eq(gc.state, "active")));
|
|
406
|
+
yield* transaction.insert(mapping.continuation.table).values(mapping.continuation.encodeInsert({
|
|
407
|
+
...continuation,
|
|
408
|
+
retentionUntilMillis: retentionUntil
|
|
409
|
+
}));
|
|
410
|
+
return prepared;
|
|
411
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
412
|
+
complete: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
413
|
+
const journal = yield* CurrentCommitJournal;
|
|
414
|
+
return yield* completeIn(mapping, configuration, input, (decision) => prepare(decision, journal));
|
|
415
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
416
|
+
claimDelivery: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
417
|
+
const transaction = yield* CurrentProofSql;
|
|
418
|
+
const journal = yield* CurrentCommitJournal;
|
|
419
|
+
const row = (yield* readGeneration(mapping, input.moduleId, input.proofId, configuration.locking))[0];
|
|
420
|
+
const now = yield* nowMillis;
|
|
421
|
+
if (row === void 0) return prepare({ _tag: "Declined" }, journal);
|
|
422
|
+
const c = generationColumns(mapping);
|
|
423
|
+
const expiresAt = yield* mapping.decodeInstant(row[mapping.generation.expiresAt]);
|
|
424
|
+
const claimDeadline = row[mapping.generation.claimDeadline] === null || row[mapping.generation.claimDeadline] === void 0 ? void 0 : yield* mapping.decodeInstant(row[mapping.generation.claimDeadline]);
|
|
425
|
+
const retryAt = row[mapping.generation.retryAt] === null || row[mapping.generation.retryAt] === void 0 ? void 0 : yield* mapping.decodeInstant(row[mapping.generation.retryAt]);
|
|
426
|
+
let state = row[mapping.generation.deliveryState];
|
|
427
|
+
if (state === "claimed" && claimDeadline !== void 0 && claimDeadline <= now) {
|
|
428
|
+
state = "ambiguous";
|
|
429
|
+
yield* transaction.update(mapping.generation.table).set(updateValues([[mapping.generation.deliveryState, "ambiguous"]])).where(and(eq(c.moduleId, input.moduleId), eq(c.proofId, input.proofId)));
|
|
430
|
+
}
|
|
431
|
+
if (!(row[mapping.generation.version] === input.version && row[mapping.generation.deliveryId] === input.deliveryId && row[mapping.generation.state] === "active" && expiresAt > now && Number(row[mapping.generation.sendCount]) < input.policy.maximumDeliveryAttempts && (state === "new" || state === "ambiguous" && input.allowAmbiguousRetry && (retryAt === void 0 || retryAt <= now)))) return prepare({ _tag: "Declined" }, journal);
|
|
432
|
+
const claimVersion = yield* allocateVersion(mapping, configuration.mode);
|
|
433
|
+
const prepared = prepare({
|
|
434
|
+
_tag: "Claimed",
|
|
435
|
+
claimVersion
|
|
436
|
+
}, journal);
|
|
437
|
+
yield* transaction.update(mapping.generation.table).set(updateValues([
|
|
438
|
+
[mapping.generation.sendCount, Number(row[mapping.generation.sendCount]) + 1],
|
|
439
|
+
[mapping.generation.deliveryState, "claimed"],
|
|
440
|
+
[mapping.generation.claimVersion, claimVersion],
|
|
441
|
+
[mapping.generation.claimDeadline, mapping.encodeInstant(now + input.policy.deliveryClaimMillis)],
|
|
442
|
+
[mapping.generation.retryAt, mapping.encodeInstant(now + input.policy.deliveryRetryMillis)]
|
|
443
|
+
])).where(and(eq(c.moduleId, input.moduleId), eq(c.proofId, input.proofId)));
|
|
444
|
+
return prepared;
|
|
445
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
446
|
+
settleDelivery: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
447
|
+
const transaction = yield* CurrentProofSql;
|
|
448
|
+
const journal = yield* CurrentCommitJournal;
|
|
449
|
+
const initial = (yield* readGeneration(mapping, input.moduleId, input.proofId, false))[0];
|
|
450
|
+
if (initial === void 0) return prepare(void 0, journal);
|
|
451
|
+
const keys = initial[mapping.generation.seriesKey];
|
|
452
|
+
yield* ensureSeries(mapping, configuration, input.moduleId, initial[mapping.generation.purpose], keys);
|
|
453
|
+
const row = (yield* readGeneration(mapping, input.moduleId, input.proofId, configuration.locking))[0];
|
|
454
|
+
const prepared = prepare(void 0, journal);
|
|
455
|
+
if (row === void 0 || row[mapping.generation.version] !== input.version || row[mapping.generation.deliveryId] !== input.deliveryId || row[mapping.generation.claimVersion] !== input.claimVersion || row[mapping.generation.deliveryState] !== "claimed") return prepared;
|
|
456
|
+
const c = generationColumns(mapping);
|
|
457
|
+
const outcome = input.outcome;
|
|
458
|
+
const state = outcome._tag === "Accepted" ? "accepted" : outcome._tag === "DefiniteFailure" ? "failed" : "ambiguous";
|
|
459
|
+
const values = [[mapping.generation.deliveryState, state], [mapping.generation.claimDeadline, null]];
|
|
460
|
+
if (outcome._tag === "Ambiguous") values.push([mapping.generation.retryAt, mapping.encodeInstant((yield* nowMillis) + Number(row[mapping.generation.deliveryRetryMillis]))]);
|
|
461
|
+
if (outcome._tag === "DefiniteFailure") values.push([mapping.generation.state, "cancelled"]);
|
|
462
|
+
yield* transaction.update(mapping.generation.table).set(updateValues(values)).where(and(eq(c.moduleId, input.moduleId), eq(c.proofId, input.proofId)));
|
|
463
|
+
if (outcome._tag === "DefiniteFailure") {
|
|
464
|
+
const sc = seriesColumns(mapping);
|
|
465
|
+
yield* transaction.update(mapping.series.table).set(updateValues([[mapping.series.activeProofId, null]])).where(and(eq(sc.moduleId, input.moduleId), eq(sc.purpose, row[mapping.generation.purpose]), eq(sc.scopeKey, keys), eq(sc.activeProofId, input.proofId)));
|
|
466
|
+
}
|
|
467
|
+
return prepared;
|
|
468
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
469
|
+
cancel: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
470
|
+
const transaction = yield* CurrentProofSql;
|
|
471
|
+
const journal = yield* CurrentCommitJournal;
|
|
472
|
+
const authorityCurrent = yield* lockAuthority(mapping, input.moduleId, input.purpose, input.binding, configuration.locking);
|
|
473
|
+
const keys = mapping.scopeKeys({
|
|
474
|
+
moduleId: input.moduleId,
|
|
475
|
+
purpose: input.purpose,
|
|
476
|
+
binding: input.binding
|
|
477
|
+
});
|
|
478
|
+
const active = (yield* ensureSeries(mapping, configuration, input.moduleId, input.purpose, keys.series))[mapping.series.activeProofId];
|
|
479
|
+
const prepared = prepare(void 0, journal);
|
|
480
|
+
if (authorityCurrent && active !== void 0 && active !== null) {
|
|
481
|
+
const gc = generationColumns(mapping);
|
|
482
|
+
const sc = seriesColumns(mapping);
|
|
483
|
+
const row = (yield* readGeneration(mapping, input.moduleId, active, configuration.locking))[0];
|
|
484
|
+
const storedBinding = row === void 0 ? void 0 : yield* mapping.generation.decodeBinding(row);
|
|
485
|
+
if (storedBinding === void 0 || !sameBinding(storedBinding, input.binding)) return prepared;
|
|
486
|
+
yield* transaction.update(mapping.generation.table).set(updateValues([[mapping.generation.state, "cancelled"]])).where(and(eq(gc.moduleId, input.moduleId), eq(gc.proofId, active)));
|
|
487
|
+
yield* transaction.update(mapping.series.table).set(updateValues([[mapping.series.activeProofId, null]])).where(and(eq(sc.moduleId, input.moduleId), eq(sc.purpose, input.purpose), eq(sc.scopeKey, keys.series), eq(sc.activeProofId, active)));
|
|
488
|
+
}
|
|
489
|
+
return prepared;
|
|
490
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure),
|
|
491
|
+
cleanup: (input, prepare) => owned(database, mapping, configuration, Effect.gen(function* () {
|
|
492
|
+
const transaction = yield* CurrentProofSql;
|
|
493
|
+
const journal = yield* CurrentCommitJournal;
|
|
494
|
+
const now = yield* nowMillis;
|
|
495
|
+
const nativeNow = mapping.encodeInstant(now);
|
|
496
|
+
const gc = generationColumns(mapping);
|
|
497
|
+
const cc = continuationColumns(mapping);
|
|
498
|
+
const rc = requestColumns(mapping);
|
|
499
|
+
const ac = abuseColumns(mapping);
|
|
500
|
+
const fc = failureColumns(mapping);
|
|
501
|
+
const mc = commandColumns(mapping);
|
|
502
|
+
const sc = seriesColumns(mapping);
|
|
503
|
+
const rsc = scopeColumns(mapping);
|
|
504
|
+
let remaining = input.limit;
|
|
505
|
+
let hasMore = false;
|
|
506
|
+
const take = (query) => selectRows(query.limit(remaining + 1), configuration.locking).pipe(Effect.map((rows) => {
|
|
507
|
+
if (rows.length > remaining) hasMore = true;
|
|
508
|
+
const selected = rows.slice(0, remaining);
|
|
509
|
+
remaining -= selected.length;
|
|
510
|
+
return selected;
|
|
511
|
+
}));
|
|
512
|
+
const continuations = yield* take(transaction.select({ continuationId: cc.continuationId }).from(mapping.continuation.table).where(and(eq(cc.moduleId, input.moduleId), lte(cc.retentionUntil, nativeNow))).orderBy(cc.retentionUntil));
|
|
513
|
+
const generations = yield* take(transaction.select({ proofId: gc.proofId }).from(mapping.generation.table).where(and(eq(gc.moduleId, input.moduleId), lte(gc.retentionUntil, nativeNow))).orderBy(gc.retentionUntil));
|
|
514
|
+
const requests = yield* take(transaction.select({ requestId: rc.requestId }).from(mapping.request.table).where(and(eq(rc.moduleId, input.moduleId), lte(rc.retentionUntil, nativeNow))).orderBy(rc.retentionUntil));
|
|
515
|
+
const abuse = yield* take(transaction.select({
|
|
516
|
+
action: ac.action,
|
|
517
|
+
scopeKind: ac.scopeKind,
|
|
518
|
+
scopeKey: ac.scopeKey,
|
|
519
|
+
commandId: ac.commandId
|
|
520
|
+
}).from(mapping.abuseEvent.table).where(and(eq(ac.moduleId, input.moduleId), lte(ac.retentionUntil, nativeNow))).orderBy(ac.retentionUntil));
|
|
521
|
+
const failures = yield* take(transaction.select({
|
|
522
|
+
seriesKey: fc.seriesKey,
|
|
523
|
+
commandId: fc.commandId
|
|
524
|
+
}).from(mapping.failureEvent.table).where(and(eq(fc.moduleId, input.moduleId), lte(fc.retentionUntil, nativeNow))).orderBy(fc.retentionUntil));
|
|
525
|
+
const commands = yield* take(transaction.select({ commandId: mc.commandId }).from(mapping.command.table).where(and(eq(mc.moduleId, input.moduleId), lte(mc.retentionUntil, nativeNow))).orderBy(mc.retentionUntil));
|
|
526
|
+
const series = yield* take(transaction.select({
|
|
527
|
+
purpose: sc.purpose,
|
|
528
|
+
scopeKey: sc.scopeKey
|
|
529
|
+
}).from(mapping.series.table).where(and(eq(sc.moduleId, input.moduleId), isNull(sc.activeProofId), notExists(transaction.select({ one: sql`1` }).from(mapping.generation.table).where(and(eq(gc.moduleId, sc.moduleId), eq(gc.purpose, sc.purpose), eq(gc.seriesKey, sc.scopeKey)))), notExists(transaction.select({ one: sql`1` }).from(mapping.failureEvent.table).where(and(eq(fc.moduleId, sc.moduleId), eq(fc.purpose, sc.purpose), eq(fc.seriesKey, sc.scopeKey)))))));
|
|
530
|
+
const rateScopes = yield* take(transaction.select({
|
|
531
|
+
purpose: rsc.purpose,
|
|
532
|
+
action: rsc.action,
|
|
533
|
+
scopeKind: rsc.scopeKind,
|
|
534
|
+
scopeKey: rsc.scopeKey
|
|
535
|
+
}).from(mapping.rateScope.table).where(and(eq(rsc.moduleId, input.moduleId), notExists(transaction.select({ one: sql`1` }).from(mapping.abuseEvent.table).where(and(eq(ac.moduleId, rsc.moduleId), eq(ac.purpose, rsc.purpose), eq(ac.action, rsc.action), eq(ac.scopeKind, rsc.scopeKind), eq(ac.scopeKey, rsc.scopeKey)))))));
|
|
536
|
+
hasMore ||= generations.length > 0 || failures.length > 0 || abuse.length > 0;
|
|
537
|
+
const prepared = prepare({
|
|
538
|
+
removed: input.limit - remaining,
|
|
539
|
+
hasMore
|
|
540
|
+
}, journal);
|
|
541
|
+
if (continuations.length > 0) yield* transaction.delete(mapping.continuation.table).where(and(eq(cc.moduleId, input.moduleId), inArray(cc.continuationId, continuations.map((row) => row.continuationId)), lte(cc.retentionUntil, nativeNow)));
|
|
542
|
+
if (generations.length > 0) yield* transaction.update(mapping.series.table).set(updateValues([[mapping.series.activeProofId, null]])).where(and(eq(sc.moduleId, input.moduleId), inArray(sc.activeProofId, generations.map((row) => row.proofId))));
|
|
543
|
+
if (generations.length > 0) yield* transaction.delete(mapping.generation.table).where(and(eq(gc.moduleId, input.moduleId), inArray(gc.proofId, generations.map((row) => row.proofId)), lte(gc.retentionUntil, nativeNow)));
|
|
544
|
+
if (requests.length > 0) yield* transaction.delete(mapping.request.table).where(and(eq(rc.moduleId, input.moduleId), inArray(rc.requestId, requests.map((row) => row.requestId)), lte(rc.retentionUntil, nativeNow)));
|
|
545
|
+
for (const row of abuse) yield* transaction.delete(mapping.abuseEvent.table).where(and(eq(ac.moduleId, input.moduleId), eq(ac.action, row.action), eq(ac.scopeKind, row.scopeKind), eq(ac.scopeKey, row.scopeKey), eq(ac.commandId, row.commandId), lte(ac.retentionUntil, nativeNow)));
|
|
546
|
+
for (const row of failures) yield* transaction.delete(mapping.failureEvent.table).where(and(eq(fc.moduleId, input.moduleId), eq(fc.seriesKey, row.seriesKey), eq(fc.commandId, row.commandId), lte(fc.retentionUntil, nativeNow)));
|
|
547
|
+
if (commands.length > 0) yield* transaction.delete(mapping.command.table).where(and(eq(mc.moduleId, input.moduleId), inArray(mc.commandId, commands.map((row) => row.commandId)), lte(mc.retentionUntil, nativeNow)));
|
|
548
|
+
for (const row of series) yield* transaction.delete(mapping.series.table).where(and(eq(sc.moduleId, input.moduleId), eq(sc.purpose, row.purpose), eq(sc.scopeKey, row.scopeKey), isNull(sc.activeProofId), notExists(transaction.select({ one: sql`1` }).from(mapping.generation.table).where(and(eq(gc.moduleId, sc.moduleId), eq(gc.purpose, sc.purpose), eq(gc.seriesKey, sc.scopeKey))))));
|
|
549
|
+
for (const row of rateScopes) yield* transaction.delete(mapping.rateScope.table).where(and(eq(rsc.moduleId, input.moduleId), eq(rsc.purpose, row.purpose), eq(rsc.action, row.action), eq(rsc.scopeKind, row.scopeKind), eq(rsc.scopeKey, row.scopeKey), notExists(transaction.select({ one: sql`1` }).from(mapping.abuseEvent.table).where(and(eq(ac.moduleId, rsc.moduleId), eq(ac.purpose, rsc.purpose), eq(ac.action, rsc.action), eq(ac.scopeKind, rsc.scopeKind), eq(ac.scopeKey, rsc.scopeKey))))));
|
|
550
|
+
return prepared;
|
|
551
|
+
})).pipe(Effect.provideService(CurrentProofSql, database), Effect.provideService(LifecycleHooks, hooks), translateFailure)
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
const inspectCompletionIn = Effect.fn("DrizzleProof.inspectCompletionIn")(function* (mapping, configuration, input, ensure) {
|
|
555
|
+
const database = yield* CurrentProofSql;
|
|
556
|
+
const authorityCurrent = yield* lockAuthority(mapping, input.moduleId, input.purpose, input.binding, configuration.locking);
|
|
557
|
+
const keys = mapping.scopeKeys({
|
|
558
|
+
moduleId: input.moduleId,
|
|
559
|
+
purpose: input.purpose,
|
|
560
|
+
binding: input.binding
|
|
561
|
+
});
|
|
562
|
+
if (ensure) yield* ensureSeries(mapping, configuration, input.moduleId, input.purpose, keys.series);
|
|
563
|
+
const c = continuationColumns(mapping);
|
|
564
|
+
const row = (yield* selectRows(database.select().from(mapping.continuation.table).where(and(eq(c.moduleId, input.moduleId), eq(c.continuationId, input.continuationId))).limit(1), configuration.locking))[0];
|
|
565
|
+
const now = yield* nowMillis;
|
|
566
|
+
const record = row === void 0 ? void 0 : yield* mapping.continuation.decode(row);
|
|
567
|
+
return authorityCurrent && row !== void 0 && record !== void 0 && record.moduleId === input.moduleId && record.continuationId === input.continuationId && record.purpose === input.purpose && record.digest === input.continuationDigest && record.seriesKey === keys.series && sameBinding(record.binding, input.binding) && row[mapping.continuation.consumed] === false && record.expiresAtMillis > now ? {
|
|
568
|
+
record,
|
|
569
|
+
row,
|
|
570
|
+
columns: c
|
|
571
|
+
} : void 0;
|
|
572
|
+
});
|
|
573
|
+
/** Nonconsuming same-owner preflight; it never inserts a series or changes expiry. */
|
|
574
|
+
const checkProofCompletionIn = Effect.fn("Drizzle.checkProofCompletionIn")(function* (mapping, configuration, input) {
|
|
575
|
+
return yield* inspectCompletionIn(mapping, configuration, input, false).pipe(Effect.map((value) => value !== void 0));
|
|
576
|
+
});
|
|
577
|
+
const completeIn = Effect.fn("DrizzleProof.completeIn")(function* (mapping, configuration, input, prepare, protectedMutation) {
|
|
578
|
+
const database = yield* CurrentProofSql;
|
|
579
|
+
const accepted = yield* inspectCompletionIn(mapping, configuration, input, true);
|
|
580
|
+
if (accepted === void 0) return prepare("rejected");
|
|
581
|
+
const { record, row: original, columns: c } = accepted;
|
|
582
|
+
if (protectedMutation !== void 0 && !(yield* protectedMutation)) return yield* unavailable();
|
|
583
|
+
const value = prepare("completed");
|
|
584
|
+
yield* database.update(mapping.continuation.table).set(updateValues([[mapping.continuation.consumed, true]])).where(and(eq(c.moduleId, input.moduleId), eq(c.continuationId, input.continuationId), eq(c.digest, input.continuationDigest), eq(c.consumed, false)));
|
|
585
|
+
yield* insertCommand(mapping, input.moduleId, input.continuationDigest, "complete", "completed", record.expiresAtMillis);
|
|
586
|
+
const assertApplied = Effect.flatMap(CurrentPasswordPreparedTransaction, (currentTransaction) => Effect.gen(function* () {
|
|
587
|
+
const persisted = (yield* selectRows(currentTransaction.select().from(mapping.continuation.table).where(and(eq(c.moduleId, input.moduleId), eq(c.continuationId, input.continuationId))).limit(1), false))[0];
|
|
588
|
+
const command = (yield* commandRow(mapping, input.moduleId, input.continuationDigest, false))[0];
|
|
589
|
+
const decoded = persisted === void 0 ? void 0 : yield* mapping.continuation.decode(persisted);
|
|
590
|
+
const immutable = [
|
|
591
|
+
mapping.continuation.moduleId,
|
|
592
|
+
mapping.continuation.purpose,
|
|
593
|
+
mapping.continuation.continuationId,
|
|
594
|
+
mapping.continuation.digest,
|
|
595
|
+
mapping.continuation.proofId,
|
|
596
|
+
mapping.continuation.seriesKey,
|
|
597
|
+
mapping.continuation.version
|
|
598
|
+
];
|
|
599
|
+
if (persisted === void 0 || decoded === void 0 || !sameBinding(decoded.binding, record.binding) || persisted[mapping.continuation.consumed] !== true || immutable.some((key) => persisted[key] !== original[key]) || (yield* mapping.decodeInstant(persisted[mapping.continuation.expiresAt])) !== record.expiresAtMillis || (yield* mapping.decodeInstant(persisted[mapping.continuation.retentionUntil])) !== (yield* mapping.decodeInstant(original[mapping.continuation.retentionUntil])) || command === void 0 || command[mapping.command.kind] !== "complete" || command[mapping.command.decision] !== "completed" || (yield* mapping.decodeInstant(command[mapping.command.retentionUntil])) !== record.expiresAtMillis) return yield* unavailable();
|
|
600
|
+
if (record.expiresAtMillis <= (yield* nowMillis)) return yield* unavailable();
|
|
601
|
+
}).pipe(Effect.provideService(CurrentProofSql, currentTransaction)));
|
|
602
|
+
yield* assertApplied.pipe(Effect.provideService(CurrentPasswordPreparedTransaction, database));
|
|
603
|
+
const postconditions = yield* Effect.serviceOption(PasswordPreparedPostconditions);
|
|
604
|
+
if (Option.isSome(postconditions) && !postconditions.value.register(assertApplied.pipe(translateFailure))) return yield* unavailable();
|
|
605
|
+
return value;
|
|
606
|
+
});
|
|
607
|
+
return {
|
|
608
|
+
makeSqlProofPersistence,
|
|
609
|
+
checkProofCompletionIn,
|
|
610
|
+
completeProofPlanIn: Effect.fn("Drizzle.completeProofPlanIn")(function* (mapping, configuration, plan, protectedMutation, project) {
|
|
611
|
+
const journal = yield* CurrentCommitJournal;
|
|
612
|
+
return yield* completeIn(mapping, configuration, plan.input, (decision) => plan.prepare(decision, journal, project), protectedMutation);
|
|
613
|
+
})
|
|
614
|
+
};
|
|
615
|
+
};
|
|
616
|
+
//#endregion
|
|
617
|
+
export { CurrentProofSql, makeProofKernel };
|
|
618
|
+
|
|
619
|
+
//# sourceMappingURL=proof-kernel.mjs.map
|