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