@fantastic.dev/better-stripe 0.98.0 → 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 (93) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.d.ts +6 -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 +1 -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 +9 -11
  16. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.d.ts.map +1 -1
  17. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.js +3 -380
  18. package/dist/client/billing/renewalAttach/internal/operations/fixedTransport.js.map +1 -1
  19. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.d.ts +138 -0
  20. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.d.ts.map +1 -1
  21. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.js +64 -139
  22. package/dist/client/billing/renewalAttach/internal/operations/initialAttach.js.map +1 -1
  23. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.d.ts +63 -0
  24. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.d.ts.map +1 -0
  25. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.js +380 -0
  26. package/dist/client/billing/renewalAttach/internal/operations/transportKernel.js.map +1 -0
  27. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +2 -0
  28. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -1
  29. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +6 -0
  30. package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -1
  31. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts +56 -0
  32. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.d.ts.map +1 -0
  33. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.js +49 -0
  34. package/dist/client/billing/renewalAttach/internal/producer/journalWorkFactory.js.map +1 -0
  35. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts +1 -1
  36. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.d.ts.map +1 -1
  37. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.js +3 -2
  38. package/dist/client/billing/renewalAttach/internal/producer/operationFactory.js.map +1 -1
  39. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.d.ts +4 -0
  40. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.d.ts.map +1 -0
  41. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.js +87 -0
  42. package/dist/client/billing/renewalAttach/internal/producer/recoveryEntry.js.map +1 -0
  43. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.d.ts +368 -0
  44. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.d.ts.map +1 -0
  45. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.js +106 -0
  46. package/dist/client/billing/renewalAttach/internal/producer/recoveryFactory.js.map +1 -0
  47. package/dist/client/billing/renewalAttach/internal/recovery/output.d.ts +1 -0
  48. package/dist/client/billing/renewalAttach/internal/recovery/output.d.ts.map +1 -1
  49. package/dist/client/billing/renewalAttach/internal/recovery/output.js +22 -8
  50. package/dist/client/billing/renewalAttach/internal/recovery/output.js.map +1 -1
  51. package/dist/client/billing/renewalAttach/internal/recovery/record.d.ts +1 -0
  52. package/dist/client/billing/renewalAttach/internal/recovery/record.d.ts.map +1 -1
  53. package/dist/client/billing/renewalAttach/internal/recovery/record.js +10 -2
  54. package/dist/client/billing/renewalAttach/internal/recovery/record.js.map +1 -1
  55. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.d.ts +3 -0
  56. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.d.ts.map +1 -1
  57. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.js +69 -156
  58. package/dist/client/billing/renewalAttach/internal/recovery/recoveryActor.js.map +1 -1
  59. package/dist/client/billing/renewalAttach/internal/recovery/relation.d.ts +18 -0
  60. package/dist/client/billing/renewalAttach/internal/recovery/relation.d.ts.map +1 -1
  61. package/dist/client/billing/renewalAttach/internal/recovery/relation.js +15 -1
  62. package/dist/client/billing/renewalAttach/internal/recovery/relation.js.map +1 -1
  63. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +32 -32
  64. package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
  65. package/dist/component/balanceFunding/mutations.d.ts +16 -16
  66. package/dist/component/balanceFunding/preparationHandlers.d.ts +8 -8
  67. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  68. package/dist/component/earnings/triggers.d.ts +16 -16
  69. package/dist/component/payoutRelease/queries.d.ts +2 -2
  70. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  71. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  72. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  73. package/docs/renewal-context-reads.md +44 -3
  74. package/package.json +1 -1
  75. package/src/client/billing/renewalAttach/internal/acquisition/ownedAcquisition.ts +3 -3
  76. package/src/client/billing/renewalAttach/internal/c10/attachEvents.ts +17 -5
  77. package/src/client/billing/renewalAttach/internal/operations/actorFailureControl.ts +165 -0
  78. package/src/client/billing/renewalAttach/internal/operations/fixedTransport.ts +4 -498
  79. package/src/client/billing/renewalAttach/internal/operations/initialAttach.ts +83 -160
  80. package/src/client/billing/renewalAttach/internal/operations/transportKernel.ts +500 -0
  81. package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +6 -0
  82. package/src/client/billing/renewalAttach/internal/producer/journalWorkFactory.ts +54 -0
  83. package/src/client/billing/renewalAttach/internal/producer/operationFactory.ts +3 -2
  84. package/src/client/billing/renewalAttach/internal/producer/producerRecovery.integration.test.ts +394 -0
  85. package/src/client/billing/renewalAttach/internal/producer/producerRecoveryControls.integration.test.ts +437 -0
  86. package/src/client/billing/renewalAttach/internal/producer/producerRecoveryIsolation.integration.test.ts +332 -0
  87. package/src/client/billing/renewalAttach/internal/producer/recoveryEntry.ts +109 -0
  88. package/src/client/billing/renewalAttach/internal/producer/recoveryFactory.ts +135 -0
  89. package/src/client/billing/renewalAttach/internal/recovery/output.ts +34 -8
  90. package/src/client/billing/renewalAttach/internal/recovery/record.ts +16 -2
  91. package/src/client/billing/renewalAttach/internal/recovery/recoveryActor.ts +107 -188
  92. package/src/client/billing/renewalAttach/internal/recovery/relation.ts +17 -1
  93. package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +14 -1
@@ -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
+ }
@@ -1,509 +1,15 @@
1
- import { types } from "node:util";
2
- import { requiredJSONSnapshot } from "../material/requiredJson.js";
3
- import { rejectProxyJSON } from "../acquisition/rawWireSource.js";
4
- import {
5
- closed,
6
- captureRequestAndReads,
7
- need,
8
- stop,
9
- freezeJSON,
10
- type AttachRequest,
11
- type ReadRow,
12
- } from "./types.js";
13
- import {
14
- safeDiagnostics,
15
- opaqueRecord,
16
- type Diagnostics,
17
- } from "./providerFailure.js";
18
- import { equal } from "../../../renewalAggregate/shared.js";
1
+ import { build } from "./transportKernel.js";
2
+ export type { PostDisposition, Quarantine } from "./transportKernel.js";
3
+ import { closed, need } from "./types.js";
4
+ import { OWNER } from "../originalOwner.js";
19
5
  import {
20
6
  loadInitialOwnedTransportSource,
21
7
  loadRecoveryOwnedTransportSource,
22
8
  loadOwnedTransportSource,
23
9
  } from "./sourceOperations.js";
24
- import { OWNER } from "../originalOwner.js";
25
- import {
26
- captureLegacyTransportSource,
27
- captureInitialTransportSource,
28
- checkTransportOwnerResult as checkOwnerResult,
29
- } from "./transportCapture.js";
30
10
  import { ownerForInitialLineage } from "../producer/ownerFactory.js";
31
11
  import { ownedAwaitForInitialLineage } from "../producer/ownedAwaitFactory.js";
32
12
  import { operationForInitialLineage } from "../producer/operationFactory.js";
33
-
34
- function validRead(
35
- row: ReadRow,
36
- method: unknown,
37
- path: unknown,
38
- body: unknown,
39
- options: unknown,
40
- ) {
41
- need(row && method === row.method && path === row.path && body === undefined);
42
- const value = closed(options, ["apiVersion", "maxNetworkRetries"]);
43
- need(
44
- value.apiVersion === row.options.apiVersion &&
45
- value.maxNetworkRetries === 0,
46
- );
47
- }
48
-
49
- export type PostDisposition =
50
- | "NOT_STARTED"
51
- | "PENDING"
52
- | "REJECTED"
53
- | "FULFILLED_UNVERIFIED"
54
- | "FULFILLED_SAFE";
55
- type Attempt = Readonly<{
56
- generation: object;
57
- ordinal: number;
58
- kind: "read" | "post";
59
- resource: string;
60
- method: string;
61
- path: string;
62
- id: string;
63
- }>;
64
- export type Quarantine = Readonly<object>;
65
- type Rejection = Readonly<{
66
- phase: "read";
67
- ordinal: number;
68
- diagnostics: Diagnostics;
69
- }>;
70
- const notStarted = (reason: string) =>
71
- Object.freeze({ outcome: "not_started" as const, reason });
72
- const nativeResponsePrototype = Response.prototype,
73
- nativeStatusGetter = Object.getOwnPropertyDescriptor(
74
- nativeResponsePrototype,
75
- "status",
76
- )!.get!,
77
- apply = Reflect.apply;
78
-
79
- function capturePostReply(reply: unknown) {
80
- rejectProxyJSON(reply, true);
81
- need(reply !== null && typeof reply === "object" && !Array.isArray(reply));
82
- const proto = Object.getPrototypeOf(reply);
83
- need(proto === Object.prototype || proto === null);
84
- const meta = Object.getOwnPropertyDescriptor(reply, "lastResponse");
85
- need(
86
- meta &&
87
- !meta.enumerable &&
88
- "value" in meta &&
89
- meta.value !== null &&
90
- typeof meta.value === "object",
91
- );
92
- const metadata: object = meta.value;
93
- need(!types.isProxy(metadata));
94
- const codes: unknown[] = [];
95
- for (const key of ["status", "statusCode"]) {
96
- const d = Object.getOwnPropertyDescriptor(metadata, key);
97
- if (!d) continue;
98
- need("value" in d);
99
- if (d.value !== undefined) codes.push(d.value);
100
- }
101
- if (Object.getPrototypeOf(metadata) === nativeResponsePrototype)
102
- codes.push(apply(nativeStatusGetter, metadata, []));
103
- need(
104
- codes.length > 0 &&
105
- codes.every(
106
- (x) =>
107
- typeof x === "number" &&
108
- Number.isInteger(x) &&
109
- x >= 200 &&
110
- x < 300 &&
111
- x === codes[0],
112
- ),
113
- );
114
- const statusCode = codes[0];
115
- need(typeof statusCode === "number");
116
- const entries: [string, unknown][] = [];
117
- for (const key of Reflect.ownKeys(reply)) {
118
- need(typeof key === "string");
119
- if (key === "lastResponse") continue;
120
- const d = Object.getOwnPropertyDescriptor(reply, key);
121
- need(d && d.enumerable && "value" in d);
122
- entries.push([key, d.value]);
123
- }
124
- return Object.freeze({
125
- body: requiredJSONSnapshot(Object.fromEntries(entries)),
126
- statusCode,
127
- });
128
- }
129
-
130
- type Association = Readonly<{
131
- operation: unknown;
132
- readPlan: unknown;
133
- delegate: unknown;
134
- }>;
135
- function build(source: unknown, association?: Association, token?: object) {
136
- const b =
137
- token === undefined
138
- ? captureLegacyTransportSource(source)
139
- : captureInitialTransportSource(token, source);
140
- const generation = Object.freeze({});
141
- let ordinal = 0,
142
- index = 0,
143
- pending: Attempt | null = null,
144
- failedRead = false,
145
- consumedPost = false;
146
- let postState: PostDisposition = "NOT_STARTED",
147
- postAttempt: Attempt | null = null,
148
- rejectionSlot: { attempt: Attempt; record: Rejection } | null = null,
149
- lastFailedRead: Attempt | null = null,
150
- closedOwner = false,
151
- checking = false;
152
- function ownerGate() {
153
- if (closedOwner || checking) {
154
- closedOwner = true;
155
- stop();
156
- }
157
- checking = true;
158
- try {
159
- checkOwnerResult(Reflect.apply(b.check, b.owner, []));
160
- if (closedOwner) stop();
161
- } catch {
162
- closedOwner = true;
163
- stop();
164
- } finally {
165
- checking = false;
166
- }
167
- }
168
- function attempt(
169
- kind: "read" | "post",
170
- row: ReadRow | AttachRequest,
171
- ): Attempt {
172
- need(!pending && ordinal < 44);
173
- const value = Object.freeze({
174
- generation,
175
- ordinal: ++ordinal,
176
- kind,
177
- resource: kind === "read" && "resource" in row ? row.resource : "post",
178
- method: row.method,
179
- path: row.path,
180
- id: "id" in row ? row.id : b.request.payload.from_subscription,
181
- });
182
- pending = value;
183
- return value;
184
- }
185
- const own = (a: Attempt) => pending === a && a.generation === generation;
186
- const postCatchRecords = new WeakMap<
187
- object,
188
- Readonly<{ attempt: Attempt; diagnostics: Diagnostics }>
189
- >();
190
- let reservation = "unused",
191
- fulfilledSlot: Readonly<{
192
- attempt: Attempt;
193
- generation: object;
194
- quarantine: Quarantine;
195
- }> | null = null;
196
- async function rawRequest(
197
- method: unknown,
198
- path: unknown,
199
- body: unknown,
200
- options: unknown,
201
- ): Promise<unknown> {
202
- need(!failedRead && !consumedPost && !pending && index < b.plan.length);
203
- ownerGate();
204
- const row = b.plan[index];
205
- validRead(row, method, path, body, options);
206
- const a = attempt("read", row);
207
- let reply: unknown;
208
- try {
209
- reply = await Reflect.apply(b.send, b.delegate, [
210
- method,
211
- path,
212
- body,
213
- row.options,
214
- ]);
215
- } catch (error) {
216
- need(own(a));
217
- failedRead = true;
218
- lastFailedRead = a;
219
- let live = false;
220
- try {
221
- ownerGate();
222
- live = true;
223
- } catch {
224
- /* Retired errors are opaque. */
225
- }
226
- if (live && own(a))
227
- rejectionSlot = {
228
- attempt: a,
229
- record: opaqueRecord(
230
- {
231
- phase: "read",
232
- ordinal: a.ordinal,
233
- diagnostics: safeDiagnostics(error),
234
- } as const,
235
- "rawError",
236
- error,
237
- ),
238
- };
239
- pending = null;
240
- if (live) throw error;
241
- stop();
242
- }
243
- try {
244
- ownerGate();
245
- need(own(a));
246
- } catch {
247
- failedRead = true;
248
- pending = null;
249
- stop();
250
- }
251
- pending = null;
252
- index++;
253
- return reply;
254
- }
255
- async function postAttach(...args: unknown[]) {
256
- if (
257
- args.length ||
258
- consumedPost ||
259
- pending ||
260
- failedRead ||
261
- index !== b.plan.length
262
- )
263
- return notStarted("not_ready");
264
- try {
265
- ownerGate();
266
- } catch {
267
- return notStarted("owner_closed");
268
- }
269
- consumedPost = true;
270
- const request = b.request,
271
- a = attempt("post", request);
272
- postAttempt = a;
273
- postState = "PENDING";
274
- let reply: unknown;
275
- try {
276
- reply = await Reflect.apply(b.send, b.delegate, [
277
- request.method,
278
- request.path,
279
- request.payload,
280
- {
281
- apiVersion: request.apiVersion,
282
- idempotencyKey: request.key,
283
- maxNetworkRetries: 0,
284
- },
285
- ]);
286
- } catch (error) {
287
- postState = "REJECTED";
288
- let live = false;
289
- try {
290
- ownerGate();
291
- live = true;
292
- } catch {
293
- /* No late error reflection. */
294
- }
295
- const valid = own(a) && postAttempt === a,
296
- diagnostics =
297
- live && valid ? safeDiagnostics(error) : Object.freeze({});
298
- const record = opaqueRecord(
299
- {
300
- outcome: "provider_rejected",
301
- phase: "post",
302
- ordinal: a.ordinal,
303
- diagnostics,
304
- } as const,
305
- "rawError",
306
- error,
307
- );
308
- if (valid)
309
- postCatchRecords.set(
310
- record,
311
- Object.freeze({ attempt: a, diagnostics }),
312
- );
313
- pending = null;
314
- return record;
315
- }
316
- if (own(a) && postAttempt === a && a.generation === generation) {
317
- const q: object = Object.create(null);
318
- Object.defineProperty(q, "raw", {
319
- value: reply,
320
- enumerable: false,
321
- writable: false,
322
- configurable: false,
323
- });
324
- Object.freeze(q);
325
- fulfilledSlot = Object.freeze({ attempt: a, generation, quarantine: q });
326
- }
327
- postState = "FULFILLED_UNVERIFIED";
328
- try {
329
- ownerGate();
330
- need(own(a) && postAttempt === a);
331
- const admitted = capturePostReply(reply);
332
- ownerGate();
333
- pending = null;
334
- postState = "FULFILLED_SAFE";
335
- return Object.freeze({
336
- outcome: "fulfilled_safe" as const,
337
- body: freezeJSON(admitted.body),
338
- statusCode: admitted.statusCode,
339
- });
340
- } catch {
341
- pending = null;
342
- return opaqueRecord(
343
- { outcome: "fulfilled_unverified" as const },
344
- "rawResponse",
345
- reply,
346
- );
347
- }
348
- }
349
- const transport = Object.freeze({
350
- readTransport: Object.freeze({ rawRequest }),
351
- postAttach,
352
- takeReadRejection(...args: unknown[]) {
353
- need(args.length === 0);
354
- const slot = rejectionSlot;
355
- rejectionSlot = null;
356
- if (
357
- !slot ||
358
- slot.attempt !== lastFailedRead ||
359
- slot.attempt.generation !== generation ||
360
- slot.attempt.kind !== "read"
361
- )
362
- return null;
363
- return slot.record;
364
- },
365
- postDisposition(...args: unknown[]): PostDisposition {
366
- need(args.length === 0);
367
- return postState;
368
- },
369
- });
370
- const capability = Object.freeze({
371
- assertSourceOperationBinding(
372
- operation: unknown,
373
- request: unknown,
374
- readPlan: unknown,
375
- delegate: unknown,
376
- ) {
377
- ownerGate();
378
- if (
379
- !association ||
380
- association.operation !== operation ||
381
- association.readPlan !== readPlan ||
382
- association.delegate !== delegate
383
- )
384
- return false;
385
- const current = captureRequestAndReads(request, readPlan);
386
- const matches =
387
- equal(current.request, b.request) &&
388
- current.plan.length === b.plan.length &&
389
- current.plan.every((row, index) => {
390
- const captured = b.plan[index];
391
- return (
392
- row.resource === captured.resource &&
393
- row.ordinal === captured.ordinal &&
394
- row.method === captured.method &&
395
- row.path === captured.path &&
396
- row.body === captured.body &&
397
- row.id === captured.id &&
398
- row.options.apiVersion === captured.options.apiVersion &&
399
- row.options.maxNetworkRetries === captured.options.maxNetworkRetries
400
- );
401
- });
402
- ownerGate();
403
- return matches;
404
- },
405
- takeFulfilledQuarantine(...args: unknown[]) {
406
- if (
407
- args.length !== 0 ||
408
- pending !== null ||
409
- fulfilledSlot === null ||
410
- fulfilledSlot.attempt !== postAttempt ||
411
- fulfilledSlot.generation !== generation ||
412
- postAttempt === null ||
413
- postAttempt.generation !== generation ||
414
- postAttempt.kind !== "post" ||
415
- (postState !== "FULFILLED_SAFE" && postState !== "FULFILLED_UNVERIFIED")
416
- )
417
- return null;
418
- const q = fulfilledSlot.quarantine;
419
- fulfilledSlot = null;
420
- return q;
421
- },
422
- assertVirginAcquisition(...args: unknown[]) {
423
- need(args.length === 0);
424
- try {
425
- ownerGate();
426
- } catch {
427
- return false;
428
- }
429
- if (
430
- reservation !== "unused" ||
431
- ordinal !== 0 ||
432
- index !== 0 ||
433
- pending ||
434
- failedRead ||
435
- lastFailedRead ||
436
- rejectionSlot ||
437
- consumedPost ||
438
- postAttempt ||
439
- postState !== "NOT_STARTED"
440
- ) {
441
- reservation = "invalid";
442
- return false;
443
- }
444
- reservation = "reserved";
445
- return true;
446
- },
447
- consumeReadCatch(...args: unknown[]) {
448
- need(args.length === 0);
449
- try {
450
- ownerGate();
451
- } catch {
452
- return null;
453
- }
454
- const slot = rejectionSlot;
455
- if (
456
- reservation !== "reserved" ||
457
- !failedRead ||
458
- pending ||
459
- !slot ||
460
- slot.attempt !== lastFailedRead ||
461
- slot.attempt.generation !== generation ||
462
- slot.attempt.kind !== "read" ||
463
- slot.attempt.ordinal > 43
464
- )
465
- return null;
466
- rejectionSlot = null;
467
- return Object.freeze({
468
- phase: "read" as const,
469
- ordinal: slot.attempt.ordinal,
470
- diagnostics: slot.record.diagnostics,
471
- });
472
- },
473
- consumePostCatch(actualReply: unknown) {
474
- need(arguments.length === 1);
475
- try {
476
- ownerGate();
477
- } catch {
478
- return null;
479
- }
480
- const stored =
481
- actualReply !== null &&
482
- (typeof actualReply === "object" || typeof actualReply === "function")
483
- ? postCatchRecords.get(actualReply)
484
- : undefined;
485
- if (
486
- reservation !== "reserved" ||
487
- !stored ||
488
- stored.attempt !== postAttempt ||
489
- stored.attempt.generation !== generation ||
490
- stored.attempt.kind !== "post" ||
491
- postState !== "REJECTED" ||
492
- pending ||
493
- stored.attempt.ordinal > 44
494
- )
495
- return null;
496
- postCatchRecords.delete(actualReply as object);
497
- return Object.freeze({
498
- phase: "post" as const,
499
- ordinal: stored.attempt.ordinal,
500
- diagnostics: stored.diagnostics,
501
- });
502
- },
503
- });
504
- return Object.freeze({ transport, capability });
505
- }
506
-
507
13
  export function createOwnedTransportWithAttemptCapability() {
508
14
  need(arguments.length === 0);
509
15
  OWNER.check();