@cortexkit/common-auth 0.11.7 → 0.12.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.
@@ -50,6 +50,11 @@ export class PullScheduler {
50
50
  const progress = { writes: 0 };
51
51
  try {
52
52
  const request = await withTransaction(ctx, locks, progress, { operation: 'pull', rowId: id }, async (tx) => {
53
+ // Even a skipped capture must not repair another row on a reserved
54
+ // row's behalf. Decide reservation before pool-wide torn repair.
55
+ if (tx.reservation(id) !== undefined)
56
+ return undefined;
57
+ await tx.completeTorn();
53
58
  const row = tx.row(id);
54
59
  // A row that would pull but for its unbound credential refuses, so
55
60
  // the failure hook hears of it instead of the pull vanishing.
@@ -78,7 +83,7 @@ export class PullScheduler {
78
83
  : {}),
79
84
  reason,
80
85
  };
81
- });
86
+ }, { completeTorn: false });
82
87
  await locks.releaseAll();
83
88
  if (!request) {
84
89
  this.firedAtLoad.delete(id);
@@ -4,11 +4,13 @@ import { assertNotInsideHook, runInsideHook } from './hooks.js';
4
4
  import { IDENTITY_CONTRADICTED_REASON_PREFIX, recordIdentityIn, } from './identity.js';
5
5
  import { runOperation, withTransaction } from './mutate.js';
6
6
  import { acceptProviderState, mergedProviderState } from './provider-state.js';
7
+ import { assertNotReserved } from './reserved.js';
7
8
  import { rotateIn } from './rows.js';
8
9
  import { readRow, refusal, requireBound, rowLockSpec, } from './runtime.js';
9
10
  import { rotationStamp, rotationStampUntrusted, rowLockKey, } from './schema.js';
10
11
  import { applyTransition } from './torn.js';
11
12
  function requireRefreshable(id, row) {
13
+ assertNotReserved('refresh', id, row?.staged);
12
14
  if (!row)
13
15
  throw refusal('refresh', id, 'unknown-row', `no row ${id} in the pool`);
14
16
  if (row.invalid)
@@ -0,0 +1,3 @@
1
+ import { type PoolOperation } from './errors.js';
2
+ /** A stored marker reserves the row even when the rest of its entry is invalid. */
3
+ export declare function assertNotReserved(operation: PoolOperation, id: string, reservation: unknown): void;
@@ -0,0 +1,13 @@
1
+ import { PoolOperationError } from './errors.js';
2
+ /** A stored marker reserves the row even when the rest of its entry is invalid. */
3
+ export function assertNotReserved(operation, id, reservation) {
4
+ if (reservation !== undefined)
5
+ throw new PoolOperationError({
6
+ operation,
7
+ rowId: id,
8
+ kind: 'row-staged',
9
+ phase: operation === 'pull' ? 'pull' : 'before-first-write',
10
+ retryable: false,
11
+ message: `row ${id} is reserved for roster publication`,
12
+ });
13
+ }
@@ -4,7 +4,7 @@ import { type Transaction } from './mutate.js';
4
4
  import { type ProviderStateWrite, type RowTransitionMutator, type UpdateProviderStateResult } from './provider-state.js';
5
5
  import type { PoolLockSpec } from './refresh-lock.js';
6
6
  import { type StoreRuntime } from './runtime.js';
7
- import { type CredentialBinding, type PoolCredential, type PoolRow, type RotateCredential, type StoredCredential } from './schema.js';
7
+ import { type CredentialBinding, type PoolCredential, type PoolRow, type RotateCredential, type StagedStamp, type StoredCredential } from './schema.js';
8
8
  import { type StampedTransition } from './torn.js';
9
9
  export type FailureHook = (rowId: string, error: PoolOperationError) => void | Promise<void>;
10
10
  export interface RowOperationOptions {
@@ -20,6 +20,33 @@ export interface RowOperationOptions {
20
20
  */
21
21
  extraLocks?: readonly PoolLockSpec[];
22
22
  }
23
+ export interface RowProjection {
24
+ id: string;
25
+ type: 'oauth' | 'api';
26
+ label?: string;
27
+ enabled: boolean;
28
+ disabledReason?: string;
29
+ identity?: string;
30
+ credentialEpoch?: number;
31
+ stamp?: PoolRow['stamp'];
32
+ staged?: {
33
+ reservation: string;
34
+ };
35
+ torn?: true;
36
+ invalid?: 'roster' | 'entry';
37
+ }
38
+ export interface ProtectView {
39
+ id: string;
40
+ row?: RowProjection;
41
+ rows: RowProjection[];
42
+ config: Readonly<Record<string, unknown>>;
43
+ }
44
+ export type ProtectFn = (view: ProtectView) => string | undefined | Promise<string | undefined>;
45
+ export interface AddOptions extends RowOperationOptions {
46
+ onExisting?: 'rotate' | 'refuse' | 'stage-duplicate';
47
+ protect?: ProtectFn;
48
+ }
49
+ export declare function protectIn(tx: Transaction, id: string, protect?: ProtectFn): Promise<void>;
23
50
  /** Options of `replace` and `rotate`. Without `attribution` a call behaves as before. */
24
51
  export interface RowWriteOptions extends RowOperationOptions {
25
52
  /**
@@ -55,6 +82,10 @@ export interface RemoveView {
55
82
  state: Readonly<Record<string, unknown>>;
56
83
  }
57
84
  export interface RemoveOptions extends RowToggleOptions {
85
+ /** Exact reservation required to remove a row that is still staged. */
86
+ staged?: {
87
+ reservation: string;
88
+ };
58
89
  /**
59
90
  * The credential the removal was decided for (since 0.11.7). Checked under
60
91
  * every lock, before `protect` and before anything is written or repaired:
@@ -119,6 +150,12 @@ export interface AddInput {
119
150
  credential: PoolCredential;
120
151
  identity?: string;
121
152
  label?: string;
153
+ disabled?: {
154
+ reason: string;
155
+ };
156
+ stage?: {
157
+ reservation: string;
158
+ };
122
159
  /**
123
160
  * Provider state for the credential, written in the same state write as
124
161
  * the credential (needs the store's provider-state codec). On an `add`
@@ -141,20 +178,19 @@ export interface CredentialWriteInput {
141
178
  export type AddResult = {
142
179
  /** The row holding the credential; an existing row's id on a re-add. */
143
180
  id: string;
144
- outcome: 'added' | 'added-disabled' | 'completed' | 'rotated';
181
+ outcome: 'added' | 'added-disabled' | 'completed' | 'rotated' | 'exists';
145
182
  credential: StoredCredential;
183
+ credentialEpoch: number;
146
184
  };
147
185
  /**
148
- * Writes a credential into the state file (one write), stamped with the
149
- * credential epoch the row's entry holds in `tx` (1 without an entry) and a
150
- * binding: for a replace, the one the config is about to get (and the stamp
151
- * is marked as a replace's); for every other write, the row's config as it
152
- * stands in `tx` with the identity the operation is about to record
153
- * (`identity`, see `bindingInTx`). A rotation is the same lineage: no epoch
154
- * bump, no identity or quota change. An API key must belong to the endpoint
155
- * the row holds in `tx` (see `onRowEndpoint`).
186
+ * Build the credential and its state entry without writing. The stamp records
187
+ * the credential's epoch, identity and API endpoint so readers can verify that
188
+ * they belong together. A replacement records the new epoch and account; a
189
+ * rotation keeps the current epoch, account and quota. Pending writes are read
190
+ * as completed when choosing those fields, even while disk config is behind.
191
+ * Endpoint checks and provider-state encoding finish here, before any repair.
156
192
  */
157
- export declare function rotateIn(rt: StoreRuntime, tx: Transaction, id: string, given: RotateCredential, extra?: {
193
+ declare function planRotationIn(rt: StoreRuntime, tx: Transaction, id: string, given: RotateCredential, extra?: {
158
194
  stamp?: number;
159
195
  clearErrors?: boolean;
160
196
  binding?: CredentialBinding;
@@ -162,8 +198,17 @@ export declare function rotateIn(rt: StoreRuntime, tx: Transaction, id: string,
162
198
  providerState?: ProviderStateWrite;
163
199
  /** Config transition persisted with the successor credential for crash recovery. */
164
200
  transition?: StampedTransition;
165
- }): Promise<StoredCredential>;
166
- export declare function addRow(rt: StoreRuntime, input: AddInput, options?: RowOperationOptions): Promise<AddResult>;
201
+ staged?: StagedStamp;
202
+ }): {
203
+ stored: StoredCredential;
204
+ account: Record<string, unknown>;
205
+ };
206
+ /** Validate the endpoint and encode provider state and its stamp before committing state. */
207
+ export declare function rotateIn(rt: StoreRuntime, tx: Transaction, id: string, given: RotateCredential, extra?: Parameters<typeof planRotationIn>[4]): Promise<StoredCredential>;
208
+ export declare function validateAttribution(operation: PoolOperationError['operation'], id: string, fence: Attribution | undefined): void;
209
+ /** Exact recorded identity, including absence, is part of a credential fence. */
210
+ export declare function assertRowAttribution(operation: PoolOperationError['operation'], id: string, row: PoolRow | undefined, fence: Attribution): void;
211
+ export declare function addRow(rt: StoreRuntime, input: AddInput, options?: AddOptions): Promise<AddResult>;
167
212
  export declare function replaceRow(rt: StoreRuntime, id: string, credential: PoolCredential, input?: CredentialWriteInput, options?: RowWriteOptions): Promise<{
168
213
  id: string;
169
214
  credential: StoredCredential;
@@ -178,6 +223,7 @@ export declare function rotateRow(rt: StoreRuntime, id: string, credential: Rota
178
223
  * `attribution` nor `providerState` behaves exactly as it did before 0.7.0.
179
224
  */
180
225
  export interface RowTransitionOptions extends RowToggleOptions {
226
+ protect?: ProtectFn;
181
227
  /**
182
228
  * The credential epoch and recorded identity the caller's evidence for the
183
229
  * transition was obtained under (as `recordQuota`'s attribution: an
@@ -239,6 +285,8 @@ export declare function enableRow(rt: StoreRuntime, id: string, options?: RowTra
239
285
  * past it (see `nextAddEpochIn`).
240
286
  */
241
287
  export declare function removeRow(rt: StoreRuntime, id: string, options?: RemoveOptions): Promise<RemoveResult>;
288
+ /** An attributed removal of a roster row: bound, not torn, same epoch and identity. */
289
+ export declare function assertBoundRowAttribution(id: string, row: PoolRow, fence: Attribution, operation?: PoolOperationError['operation']): void;
242
290
  /**
243
291
  * Sets the roster order in one config write. `ids` must name every roster id
244
292
  * exactly once; anything else refuses (`invalid-order`) before writing. The
@@ -262,3 +310,4 @@ export declare function recordRowIdentity(rt: StoreRuntime, id: string, identity
262
310
  id: string;
263
311
  disabled: string[];
264
312
  }>;
313
+ export {};