@fantastic.dev/better-stripe 0.97.1 → 0.99.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.d.ts +11 -0
  4. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.d.ts.map +1 -1
  5. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.js +39 -1
  6. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.js.map +1 -1
  7. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.d.ts +22 -0
  8. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.d.ts.map +1 -1
  9. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.js +13 -5
  10. package/dist/client/billing/renewalAttach/internal/c10/attachEvents.js.map +1 -1
  11. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.d.ts +17 -0
  12. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.js +140 -0
  14. package/dist/client/billing/renewalAttach/internal/operations/actorFailureControl.js.map +1 -0
  15. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.d.ts +61 -9
  16. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.js +41 -411
  18. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.d.ts +21 -0
  20. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.d.ts.map +1 -0
  21. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.js +36 -0
  22. package/dist/client/billing/renewalAttach/internal/operations/initialActorData.js.map +1 -0
  23. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.d.ts +141 -0
  24. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.d.ts.map +1 -1
  25. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.js +132 -182
  26. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.js.map +1 -1
  27. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.d.ts +1 -0
  28. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.d.ts.map +1 -1
  29. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.js +17 -5
  30. package/dist/client/billing/renewalAttach/internal/operations/ownedAwait.js.map +1 -1
  31. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.d.ts +35 -0
  32. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.js +46 -0
  34. package/dist/client/billing/renewalAttach/internal/operations/transportCapture.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.d.ts +63 -0
  36. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.d.ts.map +1 -0
  37. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.js +380 -0
  38. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.js.map +1 -0
  39. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +8 -0
  40. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -1
  41. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +25 -0
  42. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -1
  43. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +56 -0
  44. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts.map +1 -0
  45. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.js +49 -0
  46. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.js.map +1 -0
  47. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts +1 -0
  48. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts.map +1 -1
  49. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js +3 -0
  50. package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js.map +1 -1
  51. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +65 -0
  52. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts.map +1 -0
  53. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.js +162 -0
  54. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.js.map +1 -0
  55. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.d.ts +4 -0
  56. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.d.ts.map +1 -0
  57. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.js +87 -0
  58. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.js.map +1 -0
  59. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.d.ts +368 -0
  60. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.d.ts.map +1 -0
  61. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.js +106 -0
  62. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.js.map +1 -0
  63. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts +4 -0
  64. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts.map +1 -1
  65. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js +24 -3
  66. package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js.map +1 -1
  67. package/dist/client/billing/renewalAttach/internal/recovery/output.d.ts +1 -0
  68. package/dist/client/billing/renewalAttach/internal/recovery/output.d.ts.map +1 -1
  69. package/dist/client/billing/renewalAttach/internal/recovery/output.js +22 -8
  70. package/dist/client/billing/renewalAttach/internal/recovery/output.js.map +1 -1
  71. package/dist/client/billing/renewalAttach/internal/recovery/record.d.ts +1 -0
  72. package/dist/client/billing/renewalAttach/internal/recovery/record.d.ts.map +1 -1
  73. package/dist/client/billing/renewalAttach/internal/recovery/record.js +10 -2
  74. package/dist/client/billing/renewalAttach/internal/recovery/record.js.map +1 -1
  75. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.d.ts +3 -0
  76. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.d.ts.map +1 -1
  77. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.js +69 -156
  78. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.js.map +1 -1
  79. package/dist/client/billing/renewalAttach/internal/recovery/relation.d.ts +18 -0
  80. package/dist/client/billing/renewalAttach/internal/recovery/relation.d.ts.map +1 -1
  81. package/dist/client/billing/renewalAttach/internal/recovery/relation.js +15 -1
  82. package/dist/client/billing/renewalAttach/internal/recovery/relation.js.map +1 -1
  83. package/docs/renewal-context-reads.md +69 -8
  84. package/package.json +1 -1
  85. package/src/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.ts +45 -3
  86. package/src/client/billing/renewalAttach/internal/c10/attachEvents.ts +17 -5
  87. package/src/client/billing/renewalAttach/internal/operations/actorFailureControl.ts +165 -0
  88. package/src/client/billing/renewalAttach/internal/operations/fixedTransport.ts +48 -535
  89. package/src/client/billing/renewalAttach/internal/operations/initialActorData.ts +41 -0
  90. package/src/client/billing/renewalAttach/internal/operations/initialAttach.ts +168 -209
  91. package/src/client/billing/renewalAttach/internal/operations/ownedAwait.ts +23 -5
  92. package/src/client/billing/renewalAttach/internal/operations/transportCapture.ts +60 -0
  93. package/src/client/billing/renewalAttach/internal/operations/transportKernel.ts +500 -0
  94. package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +28 -0
  95. package/src/client/billing/renewalAttach/internal/producer/journalWorkFactory.ts +54 -0
  96. package/src/client/billing/renewalAttach/internal/producer/lineageAuthority.ts +3 -0
  97. package/src/client/billing/renewalAttach/internal/producer/operationFactory.ts +176 -0
  98. package/src/client/billing/renewalAttach/internal/producer/producerAcquisition.integration.test.ts +240 -0
  99. package/src/client/billing/renewalAttach/internal/producer/producerActor.integration.test.ts +526 -0
  100. package/src/client/billing/renewalAttach/internal/producer/producerRecovery.integration.test.ts +394 -0
  101. package/src/client/billing/renewalAttach/internal/producer/producerRecoveryControls.integration.test.ts +437 -0
  102. package/src/client/billing/renewalAttach/internal/producer/producerRecoveryIsolation.integration.test.ts +332 -0
  103. package/src/client/billing/renewalAttach/internal/producer/producerTransport.integration.test.ts +440 -0
  104. package/src/client/billing/renewalAttach/internal/producer/recoveryEntry.ts +109 -0
  105. package/src/client/billing/renewalAttach/internal/producer/recoveryFactory.ts +135 -0
  106. package/src/client/billing/renewalAttach/internal/producer/stockDelegateFactory.ts +29 -5
  107. package/src/client/billing/renewalAttach/internal/recovery/output.ts +34 -8
  108. package/src/client/billing/renewalAttach/internal/recovery/record.ts +16 -2
  109. package/src/client/billing/renewalAttach/internal/recovery/recoveryActor.ts +107 -188
  110. package/src/client/billing/renewalAttach/internal/recovery/relation.ts +17 -1
  111. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +82 -11
@@ -43,8 +43,8 @@ not migrate its old binding or authorize a retry.
43
43
 
44
44
  ## Internal ownership and transport groundwork
45
45
 
46
- The internal `renewalAttach/internal` modules keep one original owner identity
47
- and adapt the stock Stripe client's inherited `rawRequest` method without
46
+ The legacy `renewalAttach/internal` facades keep one dormant original Owner
47
+ identity and adapt the stock Stripe client's inherited `rawRequest` method without
48
48
  changing its receiver, arguments, result or thrown value. The owner checks the
49
49
  same original wall and monotonic time bounds and closes permanently on expiry,
50
50
  regression or retirement. These modules do not read a clock at import time.
@@ -113,8 +113,7 @@ Each callback rechecks both clocks and the original policy before rearming. The
113
113
  later final deadline is not a timer span limit. Event-loop suspension can delay
114
114
  callbacks. Every continuation therefore checks both clocks before
115
115
  returning a value. Retirement is irreversible and never extends either original
116
- cutoff. Legacy actors still use their separate dormant identity; extracting and
117
- wiring their per-invocation kernels remains required before runtime activation.
116
+ cutoff. Legacy actors retain their separate dormant identity and behavior.
118
117
 
119
118
  The private initial core can capture one attach-stage C10 compiler for its own
120
119
  lineage. This compiler takes the original raw source and actual acquired
@@ -127,8 +126,7 @@ Retiring one invocation rejects its pending channel without retiring another
127
126
  invocation's channel or removing already committed DATA. Stored events and C10
128
127
  append acknowledgements do not establish provider custody or a returned public
129
128
  operation acknowledgement. Legacy stage-channel facades retain their existing
130
- lazy Owner and behavior. Initial actor, acquisition and transport factory wiring
131
- remains required.
129
+ lazy Owner and behavior.
132
130
 
133
131
  The core's private material classifiers also retain the original journal source
134
132
  and canonical Owner. Retained classification accepts no replacement source.
@@ -138,8 +136,71 @@ original source. Both paths keep the existing DATA-consistency-only result and
138
136
  bounded snapshots. They do not qualify acquisition or provider custody. Pending
139
137
  reconstruction is owned by that invocation; retirement rejects its continuation
140
138
  without closing another invocation's classifier. Legacy classifier exports keep
141
- their lazy Owner and existing refusal behavior. The initial actor still needs
142
- to wire these classifiers to its per-invocation acquisition and transport.
139
+ their lazy Owner and existing refusal behavior.
140
+
141
+ The private initial actor now uses those classifiers, the canonical transport and
142
+ one acquisition reader for its lineage. Its operation retains one private origin
143
+ association and a single-use source slot. The transport captures the selected
144
+ SDK's original raw-request method and receiver, and receives the actual SDK
145
+ promise. Its fixed read plan permits the original subscription, target price,
146
+ billing customer and at most 40 original Coupon reads. It permits one attachment
147
+ POST. Callers cannot supply a replacement client, source, plan, Owner or journal
148
+ sink through the invocation core.
149
+
150
+ The actor retains the complete captured C3 receipt in the component journal at
151
+ sequence 2 before appending receipt event 3 or returning an acknowledgement.
152
+ Receipt retention alone is DATA. If the Owner retires while a committed retention
153
+ reply is pending, that DATA remains, but the actor refuses its acknowledgement.
154
+ Another invocation keeps its own client, Owner, journal and continuation.
155
+
156
+ Production Source class arrays remain empty. Empty admission returns before
157
+ constructing an Owner, loading Source data, invoking a client method or sampling
158
+ a clock, including when the
159
+ private actor is captured from a lineage that has no Owner yet. Tests admit
160
+ synthetic profiles in ordinary Node and Convex-test with controlled HTTP. They
161
+ do not qualify native execution or provider custody. Public scheduling, native
162
+ profile qualification, cross-process recovery and later operation stages remain
163
+ separate work.
164
+
165
+ The private core also captures one single-use explicit recovery actor. It keeps
166
+ actual initial attempt custody beside the initial actor, including its original
167
+ selected entry and predicates, reconstructed request and safely captured SDK
168
+ response when available. A stored or copied journal record cannot create this
169
+ custody. Recovery consumes its reservation before awaiting construction and
170
+ uses the same live Owner and original absolute deadlines.
171
+
172
+ A settled unknown POST without a safe response can use one explicitly invoked
173
+ same-request, same-key replay. It requires the committed dispatch event at head
174
+ 1, no known acknowledgement and no pending journal writes. The new transport
175
+ pair retains the original selected SDK and bounded read plan. Complete fresh
176
+ reads must preserve the original residual subscription, target, customer and
177
+ Coupon material. The observed schedule association may remain null or become a
178
+ validated non-null reference. That pre-POST observation remains unowned DATA;
179
+ it never substitutes for the original creation acknowledgement. Recovery checks
180
+ the complete journal frontier again after dispatch acknowledgement and before
181
+ its POST. This read fence cannot atomically lock journal state and a provider
182
+ request; another authorized writer may race the final read-to-call interval.
183
+ Immutable conflicting history still refuses acknowledgement. The legacy
184
+ recovery facade retains its existing association rules.
185
+
186
+ When the original live invocation already holds a safe response, recovery
187
+ resumes acknowledgement without another provider read or POST. It rebuilds C3
188
+ from that response and the original request, idempotently completes matching C10
189
+ events and receipt retention, and checks the original Owner before returning the
190
+ fully admitted receipt. Existing event 2 or 3 must match complete immutable
191
+ history. Conflicting DATA refuses.
192
+
193
+ Each initial or recovery channel captures its own original-journal sender and
194
+ keeps that sender's irreversible stop latch. Journal work tracking follows all
195
+ actual send and retention promises for the lineage. A C10
196
+ channel timeout can close its caller while the underlying mutation remains
197
+ pending and may later commit. Recovery refuses while that work is pending. It
198
+ also refuses an unattempted operation, a returned acknowledgement, changed
199
+ entry predicates, missing class relation or a retired Owner. Retiring one
200
+ invocation cannot retire another SDK root or return its receipt. These are
201
+ ordinary Node and Convex-test mechanics with controlled HTTP, not native or
202
+ provider qualification. Cross-process recovery cannot revive this Owner or
203
+ reconstruct actual live response custody from stored DATA.
143
204
 
144
205
  ## Component operation journal
145
206
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fantastic.dev/better-stripe",
3
- "version": "0.97.1",
3
+ "version": "0.99.0",
4
4
  "description": "Convex component for Stripe Accounts v2, subscriptions, metered billing and Connect marketplaces, with React workflows.",
5
5
  "keywords": [
6
6
  "stripe",
@@ -22,6 +22,10 @@ import {
22
22
  type RawReadTransport,
23
23
  } from "./rawWireSource.js";
24
24
  import { acquireCouponJoins } from "./rawCouponJoins.js";
25
+ import { ownerForInitialLineage } from "../producer/ownerFactory.js";
26
+ import { ownedAwaitForInitialLineage } from "../producer/ownedAwaitFactory.js";
27
+ import { operationForInitialLineage } from "../producer/operationFactory.js";
28
+ import { createInitialTransportForLineage } from "../operations/fixedTransport.js";
25
29
  const need: (x: unknown) => asserts x = (x) => {
26
30
  if (!x) throw Error("RAW_ACQUISITION_STOP");
27
31
  };
@@ -47,6 +51,44 @@ type RawSuccess = Readonly<{
47
51
  export type RawAcquisitionResult = typeof STOP | RawSuccess;
48
52
  export type RawConfigureAcquisitionResult =
49
53
  typeof STOP | (RawSuccess & Readonly<{ schedule: Record<string, unknown> }>);
54
+ type InitialReader = Readonly<{ acquire(): Promise<RawAcquisitionResult> }>;
55
+ const initialReaders = new WeakMap<object, Promise<InitialReader>>();
56
+
57
+ /** Fixed source, transport and expected material; no replacement inputs. */
58
+ export function acquisitionForInitialLineage(
59
+ token: object,
60
+ ): Promise<InitialReader> {
61
+ if (arguments.length !== 1) throw Error("RAW_ACQUISITION_STOP");
62
+ const owner = ownerForInitialLineage(token);
63
+ owner.check();
64
+ const owned = ownedAwaitForInitialLineage(token);
65
+ let pending = initialReaders.get(token);
66
+ if (!pending) {
67
+ pending = owned(async () => {
68
+ try {
69
+ const frame = await operationForInitialLineage(token);
70
+ const pair = await createInitialTransportForLineage(token);
71
+ owner.check();
72
+ const reader = createSelectedRawAcquisition(
73
+ owner,
74
+ captureTransport(pair.transport.readTransport),
75
+ );
76
+ return Object.freeze({
77
+ acquire() {
78
+ if (arguments.length !== 0) throw Error("RAW_ACQUISITION_STOP");
79
+ return owned(() => reader.acquire(frame.expected));
80
+ },
81
+ });
82
+ } catch (error) {
83
+ owner.retire();
84
+ throw error;
85
+ }
86
+ });
87
+ initialReaders.set(token, pending);
88
+ }
89
+ const captured = pending;
90
+ return owned(() => captured);
91
+ }
50
92
  const observed = z.union([
51
93
  z.null(),
52
94
  resourceId("sub_sched"),
@@ -167,18 +209,18 @@ function captureTransport(sourceTransport: RawReadTransport): RawReadTransport {
167
209
  return capturedTransport;
168
210
  }
169
211
 
170
- function createSelectedRawAcquisition(
212
+ export function createSelectedRawAcquisition(
171
213
  sourceOwner: typeof OWNER,
172
214
  sourceTransport: RawReadTransport,
173
215
  ): Readonly<{ acquire(rawExpected: unknown): Promise<RawAcquisitionResult> }>;
174
- function createSelectedRawAcquisition(
216
+ export function createSelectedRawAcquisition(
175
217
  sourceOwner: typeof OWNER,
176
218
  sourceTransport: RawReadTransport,
177
219
  fixedScheduleId: string,
178
220
  ): Readonly<{
179
221
  acquire(rawExpected: unknown): Promise<RawConfigureAcquisitionResult>;
180
222
  }>;
181
- function createSelectedRawAcquisition(
223
+ export function createSelectedRawAcquisition(
182
224
  sourceOwner: typeof OWNER,
183
225
  sourceTransport: RawReadTransport,
184
226
  fixedScheduleId: string | null = null,
@@ -11,6 +11,7 @@ import {
11
11
  import { materialize } from "./eventCodec.js";
12
12
  import { createStageChannel, createInitialStageChannel } from "./appendAck.js";
13
13
  import { readInitialLineage } from "../producer/lineageAuthority.js";
14
+ import { journalWorkForInitialLineage } from "../producer/journalWorkFactory.js";
14
15
  import { ownerForInitialLineage } from "../producer/ownerFactory.js";
15
16
  import { ownedAwaitForInitialLineage } from "../producer/ownedAwaitFactory.js";
16
17
  import {
@@ -22,16 +23,27 @@ export function createAttachEventCompiler(raw: unknown, options: unknown) {
22
23
  return attachEventCompiler(raw, createStageChannel(options));
23
24
  }
24
25
  const initialChannels = new WeakSet<object>();
26
+ const recoveryChannels = new WeakSet<object>();
27
+ export function createRecoveryAttachEventCompiler(token: object) {
28
+ return lineageAttachEventCompiler(token, recoveryChannels, arguments.length);
29
+ }
25
30
  /** Exact original call, source and journal; no replacement sink or Owner input. */
26
31
  export function createInitialAttachEventCompiler(token: object) {
32
+ return lineageAttachEventCompiler(token, initialChannels, arguments.length);
33
+ }
34
+ function lineageAttachEventCompiler(
35
+ token: object,
36
+ channels: WeakSet<object>,
37
+ arity: number,
38
+ ) {
27
39
  const owner = ownerForInitialLineage(token);
28
40
  owner.check();
29
- if (initialChannels.has(token)) throw Error("INITIAL_CHANNEL_CONSUMED");
30
- initialChannels.add(token);
31
- if (arguments.length !== 1) throw Error("INITIAL_CHANNEL_UNBOUND");
32
- const { reservation, journal } = readInitialLineage(token);
41
+ if (channels.has(token)) throw Error("INITIAL_CHANNEL_CONSUMED");
42
+ channels.add(token);
43
+ if (arity !== 1) throw Error("INITIAL_CHANNEL_UNBOUND");
44
+ const { reservation } = readInitialLineage(token);
33
45
  const source = JSON.parse(reservation.recordJSON);
34
- const send = journal.createC10Send(reservation);
46
+ const send = journalWorkForInitialLineage(token).captureSend();
35
47
  const owned = ownedAwaitForInitialLineage(token);
36
48
  owner.check();
37
49
  const channel = createInitialStageChannel(token, {
@@ -0,0 +1,165 @@
1
+ import type { AttachRequest } from "./types.js";
2
+ import type { Diagnostics } from "./providerFailure.js";
3
+ import type { createOwnedTransportWithAttemptCapability } from "./fixedTransport.js";
4
+ import { need, stop } from "./initialActorData.js";
5
+
6
+ /** Invocation-local failure projection; no transport, Owner or Source mint. */
7
+ export function captureActorFailureControl(
8
+ OWNER: { check(): unknown },
9
+ activePair: ReturnType<typeof createOwnedTransportWithAttemptCapability>,
10
+ request: AttachRequest,
11
+ ) {
12
+ const capability = activePair.capability,
13
+ takeQuarantine = capability.takeFulfilledQuarantine;
14
+ Object.freeze(request.payload);
15
+ Object.freeze(request.requestIdentity);
16
+ Object.freeze(request);
17
+ OWNER.check();
18
+ type Token = {
19
+ invocation: object;
20
+ phase: "read" | "post";
21
+ ordinal: number;
22
+ request: AttachRequest;
23
+ diagnostics: Diagnostics;
24
+ };
25
+ const tokens = new WeakMap<object, Token>(),
26
+ invocation = Object.freeze({});
27
+ let phase = "unused";
28
+ const tokenValue = (error: unknown) =>
29
+ error !== null && (typeof error === "object" || typeof error === "function")
30
+ ? tokens.get(error)
31
+ : undefined;
32
+ function consume(error: unknown, kind: "read" | "post") {
33
+ OWNER.check();
34
+ const value = tokenValue(error);
35
+ if (
36
+ error !== null &&
37
+ (typeof error === "object" || typeof error === "function")
38
+ )
39
+ tokens.delete(error);
40
+ if (
41
+ !value ||
42
+ value.invocation !== invocation ||
43
+ value.phase !== kind ||
44
+ phase !== (kind === "read" ? "failed" : "failed_post")
45
+ )
46
+ return null;
47
+ phase = kind === "read" ? "consumed" : "consumed_post";
48
+ const failure = Object.freeze({
49
+ category: "provider_rejected",
50
+ phase: kind,
51
+ ordinal: value.ordinal,
52
+ request: value.request,
53
+ diagnostics: value.diagnostics,
54
+ outcome: "unknown",
55
+ operationalAuthority: false,
56
+ });
57
+ const out = Error("INITIAL_PROVIDER_REJECTED");
58
+ Object.defineProperty(out, "failure", {
59
+ value: failure,
60
+ enumerable: false,
61
+ writable: false,
62
+ configurable: false,
63
+ });
64
+ return out;
65
+ }
66
+ return Object.freeze({
67
+ takePostQuarantine(...args: unknown[]) {
68
+ if (args.length !== 0) return null;
69
+ return Reflect.apply(takeQuarantine, capability, []) as object | null;
70
+ },
71
+ begin() {
72
+ OWNER.check();
73
+ if (phase !== "unused") stop();
74
+ if (capability.assertVirginAcquisition() !== true) {
75
+ phase = "closed";
76
+ stop();
77
+ }
78
+ OWNER.check();
79
+ phase = "acquiring";
80
+ },
81
+ complete() {
82
+ OWNER.check();
83
+ if (phase !== "acquiring") stop();
84
+ phase = "complete";
85
+ },
86
+ takeReadFailure() {
87
+ OWNER.check();
88
+ if (phase !== "acquiring") stop();
89
+ const record = capability.consumeReadCatch();
90
+ OWNER.check();
91
+ if (record === null) {
92
+ phase = "unsupported";
93
+ return null;
94
+ }
95
+ need(
96
+ record.phase === "read" &&
97
+ Number.isSafeInteger(record.ordinal) &&
98
+ record.ordinal >= 1 &&
99
+ record.ordinal <= 43,
100
+ );
101
+ const token = Object.freeze(Object.create(null)) as object;
102
+ tokens.set(
103
+ token,
104
+ Object.freeze({
105
+ invocation,
106
+ phase: "read",
107
+ ordinal: record.ordinal,
108
+ request,
109
+ diagnostics: record.diagnostics,
110
+ }),
111
+ );
112
+ phase = "failed";
113
+ return token;
114
+ },
115
+ beginPost() {
116
+ OWNER.check();
117
+ if (phase !== "complete") stop();
118
+ phase = "posting";
119
+ },
120
+ takePostFailure(actualReply: unknown) {
121
+ OWNER.check();
122
+ if (phase !== "posting") stop();
123
+ const record = capability.consumePostCatch(actualReply);
124
+ OWNER.check();
125
+ if (record === null) {
126
+ phase = "post_without_token";
127
+ return null;
128
+ }
129
+ need(
130
+ record.phase === "post" &&
131
+ Number.isSafeInteger(record.ordinal) &&
132
+ record.ordinal >= 1 &&
133
+ record.ordinal <= 44,
134
+ );
135
+ const token = Object.freeze(Object.create(null)) as object;
136
+ tokens.set(
137
+ token,
138
+ Object.freeze({
139
+ invocation,
140
+ phase: "post",
141
+ ordinal: record.ordinal,
142
+ request,
143
+ diagnostics: record.diagnostics,
144
+ }),
145
+ );
146
+ phase = "failed_post";
147
+ return token;
148
+ },
149
+ forward(error: unknown) {
150
+ OWNER.check();
151
+ const value = tokenValue(error);
152
+ return value &&
153
+ value.invocation === invocation &&
154
+ (phase === "failed" || phase === "failed_post")
155
+ ? (error as object)
156
+ : null;
157
+ },
158
+ consume(error: unknown) {
159
+ return consume(error, "read");
160
+ },
161
+ consumePost(error: unknown) {
162
+ return consume(error, "post");
163
+ },
164
+ });
165
+ }