@fantastic.dev/better-stripe 0.106.0 → 0.107.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 (28) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/observation/history.d.ts +41 -0
  4. package/dist/client/billing/renewalAttach/internal/observation/history.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAttach/internal/observation/history.js +161 -0
  6. package/dist/client/billing/renewalAttach/internal/observation/history.js.map +1 -0
  7. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.d.ts +55 -0
  8. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.js +83 -0
  10. package/dist/client/billing/renewalAttach/internal/observation/historyEvents.js.map +1 -0
  11. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  12. package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
  13. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  14. package/dist/component/balanceFunding/mutations.d.ts +27 -27
  15. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  16. package/dist/component/earnings/triggers.d.ts +16 -16
  17. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  18. package/dist/component/payoutRelease/queries.d.ts +2 -2
  19. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  20. package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
  21. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  22. package/docs/renewal-context-reads.md +35 -0
  23. package/package.json +1 -1
  24. package/src/client/billing/renewalAttach/internal/observation/history.integration.test.ts +79 -0
  25. package/src/client/billing/renewalAttach/internal/observation/history.ts +200 -0
  26. package/src/client/billing/renewalAttach/internal/observation/historyControls.integration.test.ts +238 -0
  27. package/src/client/billing/renewalAttach/internal/observation/historyEvents.ts +114 -0
  28. package/src/client/billing/renewalAttach/internal/test-fixtures/historySnapshot.ts +47 -0
@@ -0,0 +1,238 @@
1
+ import { expect } from "vitest";
2
+ import { configureAdmissionCase } from "../test-fixtures/producerConfigureAdmission.js";
3
+ import {
4
+ historySnapshot,
5
+ historyPrefix,
6
+ type HistorySnapshot,
7
+ } from "../test-fixtures/historySnapshot.js";
8
+ import { captureRetainedObservationHistory } from "./history.js";
9
+ import { canonicalJournalEvent } from "../../../../../shared/renewalOperationJournal.js";
10
+ import { renewalSnapshot } from "../../../renewalReplacementSnapshot.js";
11
+
12
+ // Mutable controlled fixture DATA only, never the detached production reader.
13
+ function object(value: unknown): Record<string, unknown> {
14
+ if (!value || typeof value !== "object" || Array.isArray(value))
15
+ throw Error("FIXTURE_OBJECT");
16
+ return value as Record<string, unknown>;
17
+ }
18
+
19
+ function rewriteRecord(
20
+ s: HistorySnapshot,
21
+ change: (r: Record<string, unknown>) => void,
22
+ ) {
23
+ const record = object(JSON.parse(s.configure.operation.recordJSON));
24
+ change(record);
25
+ const json = JSON.stringify(renewalSnapshot(record));
26
+ s.configure.operation.recordJSON = json;
27
+ s.configure.attempt.reservation.recordJSON = json;
28
+ }
29
+ async function rewriteEvent(
30
+ s: HistorySnapshot,
31
+ change: (r: Record<string, unknown>) => void,
32
+ ) {
33
+ const event = s.configure.events[1]!;
34
+ const data = object(JSON.parse(event!.dataJSON));
35
+ change(data);
36
+ const canonical = await canonicalJournalEvent(2, JSON.stringify(data));
37
+ Object.assign(event!, {
38
+ binding: canonical.binding,
39
+ dataJSON: canonical.json,
40
+ });
41
+ s.configure.attempt.events[1].binding = canonical.binding;
42
+ }
43
+
44
+ configureAdmissionCase(
45
+ "rejects inconsistent full history, response, C9 and immutable parent relations",
46
+ historySnapshot,
47
+ async (_h, full) => {
48
+ const controls: [string, (s: HistorySnapshot) => void | Promise<void>][] = [
49
+ [
50
+ "foreign parent",
51
+ (s) =>
52
+ rewriteRecord(s, (r) => {
53
+ object(r.parentAttachment).operationId = "foreign";
54
+ }),
55
+ ],
56
+ [
57
+ "raw original material",
58
+ (s) =>
59
+ rewriteRecord(s, (r) => {
60
+ object(
61
+ object(object(r.configureRecord).originalBundle).wireTargetPrice,
62
+ ).metadata = { changed: true };
63
+ }),
64
+ ],
65
+ [
66
+ "account",
67
+ (s) =>
68
+ rewriteRecord(s, (r) => {
69
+ object(r.account).livemode = true;
70
+ }),
71
+ ],
72
+ [
73
+ "deadline",
74
+ (s) =>
75
+ rewriteRecord(s, (r) => {
76
+ const p = object(r.timingPolicy);
77
+ p.endWallMs = Number(p.endWallMs) + 1;
78
+ }),
79
+ ],
80
+ [
81
+ "basis",
82
+ (s) => {
83
+ object(object(s.configure.operation).initialBasis).basisJSON = "{}";
84
+ },
85
+ ],
86
+ [
87
+ "missing bound basis",
88
+ (s) => {
89
+ Object.assign(s.configure.operation, { initialBasis: null });
90
+ },
91
+ ],
92
+ [
93
+ "descriptor mismatch",
94
+ (s) => {
95
+ s.configure.attempt.events[0].eventId = "other-event";
96
+ },
97
+ ],
98
+ [
99
+ "binding mismatch",
100
+ (s) => {
101
+ s.configure.events[0]!.binding = "wrong";
102
+ s.configure.attempt.events[0].binding = "wrong";
103
+ },
104
+ ],
105
+ [
106
+ "response request",
107
+ async (s) =>
108
+ rewriteEvent(s, (r) => {
109
+ object(r.request).key = "different-key";
110
+ }),
111
+ ],
112
+ [
113
+ "response body",
114
+ async (s) =>
115
+ rewriteEvent(s, (r) => {
116
+ object(r.body).id = "sub_sched_foreign";
117
+ }),
118
+ ],
119
+ [
120
+ "contradictory C9",
121
+ (s) => {
122
+ const ack = s.configure.acknowledgement!;
123
+ const r = JSON.parse(ack.receiptJSON);
124
+ r.providerSnapshot.metadata = { changed: true };
125
+ ack.receiptJSON = JSON.stringify(renewalSnapshot(r));
126
+ },
127
+ ],
128
+ [
129
+ "missing head3 retention",
130
+ (s) => {
131
+ s.configure.acknowledgement = null;
132
+ },
133
+ ],
134
+ [
135
+ "retention source alias",
136
+ (s) => {
137
+ s.configure.acknowledgement!.retention.receiptPayloadId =
138
+ s.configure.attempt.reservation.payloadId;
139
+ },
140
+ ],
141
+ [
142
+ "retention event alias",
143
+ (s) => {
144
+ s.configure.acknowledgement!.retention.receiptPayloadId =
145
+ s.configure.attempt.events[2].payloadId;
146
+ },
147
+ ],
148
+ [
149
+ "retention event identity alias",
150
+ (s) => {
151
+ s.configure.acknowledgement!.retention.receiptPayloadId =
152
+ s.configure.attempt.events[1].eventId;
153
+ },
154
+ ],
155
+ [
156
+ "parent receipt alias",
157
+ (s) => {
158
+ s.parent.acknowledgement!.retention.receiptPayloadId =
159
+ s.parent.attempt.reservation.payloadId;
160
+ },
161
+ ],
162
+ [
163
+ "cross stage payload alias",
164
+ (s) => {
165
+ s.configure.acknowledgement!.retention.receiptPayloadId =
166
+ s.parent.acknowledgement!.retention.receiptPayloadId;
167
+ },
168
+ ],
169
+ [
170
+ "parent event missing",
171
+ (s) => {
172
+ s.parent.events.pop();
173
+ s.parent.attempt.events.pop();
174
+ },
175
+ ],
176
+ [
177
+ "extra stage key",
178
+ (s) => {
179
+ Object.assign(s.configure, { callback: null });
180
+ },
181
+ ],
182
+ ];
183
+ for (const [name, change] of controls) {
184
+ const s = structuredClone(full);
185
+ await change(s);
186
+ let refused = false;
187
+ try {
188
+ await captureRetainedObservationHistory(s);
189
+ } catch {
190
+ refused = true;
191
+ }
192
+ expect(refused, name).toBe(true);
193
+ }
194
+ const absent = historyPrefix(full, 2);
195
+ await rewriteEvent(absent, (r) => {
196
+ object(r.body).id = "sub_sched_foreign";
197
+ });
198
+ await expect(
199
+ captureRetainedObservationHistory(absent),
200
+ "foreign response without retained C9",
201
+ ).rejects.toThrow();
202
+ },
203
+ );
204
+
205
+ configureAdmissionCase(
206
+ "rejects proxy traps, getters, hidden keys, sparse arrays and oversized graphs before awaits",
207
+ historySnapshot,
208
+ async (_h, full) => {
209
+ let touched = 0;
210
+ const proxy = new Proxy(full, {
211
+ ownKeys() {
212
+ touched++;
213
+ throw Error("TRAP");
214
+ },
215
+ });
216
+ await expect(captureRetainedObservationHistory(proxy)).rejects.toThrow();
217
+ expect(touched).toBe(0);
218
+ const getter = { ...full };
219
+ Object.defineProperty(getter, "parent", {
220
+ enumerable: true,
221
+ get() {
222
+ touched++;
223
+ return full.parent;
224
+ },
225
+ });
226
+ await expect(captureRetainedObservationHistory(getter)).rejects.toThrow();
227
+ expect(touched).toBe(0);
228
+ const hidden = structuredClone(full);
229
+ Object.defineProperty(hidden.configure, "secret", { value: true });
230
+ await expect(captureRetainedObservationHistory(hidden)).rejects.toThrow();
231
+ const sparse = structuredClone(full);
232
+ delete sparse.configure.events[0];
233
+ await expect(captureRetainedObservationHistory(sparse)).rejects.toThrow();
234
+ const huge = structuredClone(full);
235
+ huge.configure.operation.recordJSON = "x".repeat(500001);
236
+ await expect(captureRetainedObservationHistory(huge)).rejects.toThrow();
237
+ },
238
+ );
@@ -0,0 +1,114 @@
1
+ import { canonicalJournalEvent } from "../../../../../shared/renewalOperationJournal.js";
2
+ import { renewalSnapshot } from "../../../renewalReplacementSnapshot.js";
3
+ import { closed, freeze } from "../c10/ownData.js";
4
+ import {
5
+ captureAttachDispatch,
6
+ captureAttachResponse,
7
+ captureAttachReceipt,
8
+ captureConfigureDispatch,
9
+ captureConfigureResponse,
10
+ captureConfigureReceipt,
11
+ } from "../c10/eventCapture.js";
12
+ import {
13
+ captureOperationalReservation,
14
+ captureParentHistory,
15
+ captureConfigureHistory,
16
+ matchConfigureDATA,
17
+ object,
18
+ text,
19
+ requireConfigure as need,
20
+ } from "../producer/configureJournalData.js";
21
+
22
+ /** Complete bounded query DATA, never a class journal or actual ACK. */
23
+ export function captureHistoryStage(
24
+ raw: unknown,
25
+ stage: "attach" | "configure",
26
+ ) {
27
+ const x = closed(raw, ["operation", "attempt", "events", "acknowledgement"]);
28
+ const attempt = closed(x.attempt, ["reservation", "events"]);
29
+ const reservation = captureOperationalReservation(attempt.reservation, stage);
30
+ need(Array.isArray(attempt.events) && Array.isArray(x.events));
31
+ const count = attempt.events.length;
32
+ need(count <= 3 && x.events.length === count);
33
+ const descriptors =
34
+ stage === "attach"
35
+ ? captureParentHistory(x.attempt, reservation)
36
+ : count === 0
37
+ ? (matchConfigureDATA(x.attempt, { reservation, events: [] }), [])
38
+ : captureConfigureHistory(x.attempt, reservation, count);
39
+ const allPayloads = [
40
+ reservation.payloadId,
41
+ ...descriptors.map((e) => e.payloadId),
42
+ ];
43
+ need(new Set(allPayloads).size === allPayloads.length);
44
+ const events = x.events.map((raw, index) => {
45
+ const event = closed(raw, [...Object.keys(descriptors[index]), "dataJSON"]);
46
+ const dataJSON = text(event.dataJSON);
47
+ need(dataJSON.length <= 500000);
48
+ const parsed: unknown = JSON.parse(dataJSON);
49
+ const captures =
50
+ stage === "attach"
51
+ ? [captureAttachDispatch, captureAttachResponse, captureAttachReceipt]
52
+ : [
53
+ captureConfigureDispatch,
54
+ captureConfigureResponse,
55
+ captureConfigureReceipt,
56
+ ];
57
+ captures[index](parsed);
58
+ matchConfigureDATA(raw, { ...descriptors[index], dataJSON });
59
+ return { descriptor: descriptors[index], dataJSON, data: object(parsed) };
60
+ });
61
+ return {
62
+ operation: x.operation,
63
+ reservation,
64
+ events,
65
+ acknowledgement: x.acknowledgement,
66
+ };
67
+ }
68
+ export async function verifyHistoryEvents(
69
+ stage: ReturnType<typeof captureHistoryStage>,
70
+ expected: readonly unknown[],
71
+ ) {
72
+ need(stage.events.length === expected.length);
73
+ for (let index = 0; index < expected.length; index++) {
74
+ const event = stage.events[index];
75
+ matchConfigureDATA(event.data, expected[index]);
76
+ const canonical = await canonicalJournalEvent(index + 1, event.dataJSON);
77
+ need(
78
+ canonical.json === event.dataJSON &&
79
+ canonical.binding === event.descriptor.binding,
80
+ );
81
+ }
82
+ }
83
+ export function verifyHistoryRetention(
84
+ stage: ReturnType<typeof captureHistoryStage>,
85
+ receipt: unknown,
86
+ ) {
87
+ need(stage.events.length >= 2);
88
+ const ack = closed(stage.acknowledgement, ["retention", "receiptJSON"]);
89
+ const retention = object(ack.retention),
90
+ r = object(receipt);
91
+ const receiptPayloadId = text(retention.receiptPayloadId);
92
+ need(
93
+ receiptPayloadId !== stage.reservation.payloadId &&
94
+ stage.events.every((e) => e.descriptor.payloadId !== receiptPayloadId),
95
+ );
96
+ const [dispatch, response] = stage.events;
97
+ matchConfigureDATA(JSON.parse(text(ack.receiptJSON)), receipt);
98
+ need(JSON.stringify(renewalSnapshot(receipt)) === ack.receiptJSON);
99
+ matchConfigureDATA(ack.retention, {
100
+ status: "retained",
101
+ operationId: stage.reservation.operationId,
102
+ schemaVersion: 1,
103
+ stage: stage.reservation.stage,
104
+ attemptId: stage.reservation.attemptId,
105
+ sourcePayloadId: stage.reservation.payloadId,
106
+ dispatchEventId: dispatch.descriptor.eventId,
107
+ dispatchPayloadId: dispatch.descriptor.payloadId,
108
+ responseEventId: response.descriptor.eventId,
109
+ responsePayloadId: response.descriptor.payloadId,
110
+ receiptPayloadId,
111
+ receiptBinding: r.binding,
112
+ });
113
+ return freeze({ retention: ack.retention, receiptJSON: ack.receiptJSON });
114
+ }
@@ -0,0 +1,47 @@
1
+ import assert from "node:assert/strict";
2
+ import { prepareAdmittedConfigure } from "./producerConfigureAdmission.js";
3
+ import { captureAdmittedConfigureWrite } from "../producer/configureWrite.js";
4
+
5
+ export async function historySnapshot(
6
+ h: Parameters<typeof prepareAdmittedConfigure>[0],
7
+ ) {
8
+ const { child, token } = await prepareAdmittedConfigure(h);
9
+ await captureAdmittedConfigureWrite(token).invoke();
10
+ async function read(journal: typeof child.journal) {
11
+ const operation = await journal.loadAuthorizedOperation();
12
+ assert(operation?.activeAttempt);
13
+ const attempt = await journal.readAttempt({
14
+ attemptId: operation.activeAttempt.attemptId,
15
+ });
16
+ assert(attempt);
17
+ const events = await Promise.all(
18
+ attempt.events.map((event) =>
19
+ journal.readStageEvent({
20
+ attemptId: attempt.reservation.attemptId,
21
+ sequence: event.sequence,
22
+ }),
23
+ ),
24
+ );
25
+ const acknowledgement = await journal.readProviderAcknowledgement();
26
+ return { operation, attempt, events, acknowledgement };
27
+ }
28
+ const snapshot = structuredClone({
29
+ parent: await read(h.journal),
30
+ configure: await read(child.journal),
31
+ });
32
+ return snapshot;
33
+ }
34
+ export type HistorySnapshot = Awaited<ReturnType<typeof historySnapshot>>;
35
+
36
+ /** A prefix of already recorded real events, without inventing new evidence. */
37
+ export function historyPrefix(
38
+ full: HistorySnapshot,
39
+ head: number,
40
+ retained = false,
41
+ ) {
42
+ const copy = structuredClone(full);
43
+ copy.configure.attempt.events.splice(head);
44
+ copy.configure.events.splice(head);
45
+ if (!retained) copy.configure.acknowledgement = null;
46
+ return copy;
47
+ }