@fantastic.dev/better-stripe 0.88.0 → 0.89.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.
- package/CHANGELOG.md +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAggregate/readMaterial.d.ts.map +1 -1
- package/dist/client/billing/renewalAggregate/readMaterial.js +4 -3
- package/dist/client/billing/renewalAggregate/readMaterial.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts +27 -0
- package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/originalOwner.js +236 -0
- package/dist/client/billing/renewalAttach/internal/originalOwner.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts +5 -0
- package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/sourceBindings.js +9 -0
- package/dist/client/billing/renewalAttach/internal/sourceBindings.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts +6 -0
- package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js +35 -0
- package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js.map +1 -0
- package/dist/client/billing/renewalPriceReplacement.d.ts.map +1 -1
- package/dist/client/billing/renewalPriceReplacement.js +7 -3
- package/dist/client/billing/renewalPriceReplacement.js.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
- package/docs/renewal-context-reads.md +25 -0
- package/docs/renewal-phase-assembly.md +7 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAggregate/readMaterial.ts +5 -3
- package/src/client/billing/renewalAttach/internal/originalOwner.test.ts +258 -0
- package/src/client/billing/renewalAttach/internal/originalOwner.ts +280 -0
- package/src/client/billing/renewalAttach/internal/sourceBindings.test.ts +13 -0
- package/src/client/billing/renewalAttach/internal/sourceBindings.ts +8 -0
- package/src/client/billing/renewalAttach/internal/stockRawDelegate.test.ts +156 -0
- package/src/client/billing/renewalAttach/internal/stockRawDelegate.ts +43 -0
- package/src/client/billing/renewalPriceReplacement.ts +7 -3
- package/src/client/billing/renewalV2/rawRetention.test.ts +293 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/** Internal owner mechanics; Source origin and production bindings remain unbound. */
|
|
2
|
+
import { types } from "node:util";
|
|
3
|
+
import { loadOwnedOriginalSource } from "./sourceBindings.js";
|
|
4
|
+
|
|
5
|
+
export interface OriginalBasis {
|
|
6
|
+
readonly startWallMs: number;
|
|
7
|
+
readonly startNs: bigint;
|
|
8
|
+
readonly workEndWallMs: number;
|
|
9
|
+
readonly workEndNs: bigint;
|
|
10
|
+
readonly endWallMs: number;
|
|
11
|
+
readonly endNs: bigint;
|
|
12
|
+
}
|
|
13
|
+
export interface OriginalSample {
|
|
14
|
+
readonly wallMs: number;
|
|
15
|
+
readonly ns: bigint;
|
|
16
|
+
readonly nowSeconds: number;
|
|
17
|
+
readonly retired: false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function stop(): never {
|
|
21
|
+
throw new Error("ORIGINAL_OWNER_STOP");
|
|
22
|
+
}
|
|
23
|
+
function need(value: unknown): asserts value {
|
|
24
|
+
if (!value) stop();
|
|
25
|
+
}
|
|
26
|
+
const isProxy = types.isProxy;
|
|
27
|
+
const u64 = 18446744073709551615n;
|
|
28
|
+
const ms = (value: unknown): value is number =>
|
|
29
|
+
typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
30
|
+
const ns = (value: unknown): value is bigint =>
|
|
31
|
+
typeof value === "bigint" && value >= 0n && value <= u64;
|
|
32
|
+
function own<K extends string>(
|
|
33
|
+
value: unknown,
|
|
34
|
+
names: readonly K[],
|
|
35
|
+
): Record<K, unknown> {
|
|
36
|
+
need(!isProxy(value));
|
|
37
|
+
need(value !== null && typeof value === "object" && !Array.isArray(value));
|
|
38
|
+
const prototype = Object.getPrototypeOf(value);
|
|
39
|
+
need(prototype === Object.prototype || prototype === null);
|
|
40
|
+
const keys = Reflect.ownKeys(value);
|
|
41
|
+
need(
|
|
42
|
+
keys.length === names.length &&
|
|
43
|
+
keys.every((key) => typeof key === "string" && names.includes(key as K)),
|
|
44
|
+
);
|
|
45
|
+
const result = Object.create(null) as Record<K, unknown>;
|
|
46
|
+
for (const key of names) {
|
|
47
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
48
|
+
need(
|
|
49
|
+
descriptor &&
|
|
50
|
+
descriptor.enumerable &&
|
|
51
|
+
Object.prototype.hasOwnProperty.call(descriptor, "value"),
|
|
52
|
+
);
|
|
53
|
+
Object.defineProperty(result, key, {
|
|
54
|
+
value: descriptor.value,
|
|
55
|
+
enumerable: true,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
interface Context {
|
|
61
|
+
readonly basis: Readonly<OriginalBasis>;
|
|
62
|
+
readonly policy: Readonly<{
|
|
63
|
+
firstDispatchedAt: number;
|
|
64
|
+
periodStart: number;
|
|
65
|
+
periodEnd: number;
|
|
66
|
+
}>;
|
|
67
|
+
readonly sample: (...args: never[]) => unknown;
|
|
68
|
+
readonly clock: object;
|
|
69
|
+
readonly originAssociation: object;
|
|
70
|
+
}
|
|
71
|
+
let state: string = "unused";
|
|
72
|
+
let context: Context | null = null;
|
|
73
|
+
let checking = false;
|
|
74
|
+
let last: Readonly<OriginalSample> | null = null;
|
|
75
|
+
let detachClock: (() => void) | null = null;
|
|
76
|
+
let detached = false;
|
|
77
|
+
const waiters = new Set<{ fn: () => void; active: boolean }>();
|
|
78
|
+
// Source registration can synchronously reenter and close the latch.
|
|
79
|
+
const isClosed = () => state === "closed";
|
|
80
|
+
function detach() {
|
|
81
|
+
if (!detached && detachClock) {
|
|
82
|
+
detached = true;
|
|
83
|
+
try {
|
|
84
|
+
Reflect.apply(detachClock, undefined, []);
|
|
85
|
+
} catch {
|
|
86
|
+
/* Closing remains irreversible. */
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function notify(fn: () => void) {
|
|
91
|
+
try {
|
|
92
|
+
Reflect.apply(fn, undefined, []);
|
|
93
|
+
} catch {
|
|
94
|
+
/* One waiter cannot prevent retirement. */
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function close() {
|
|
98
|
+
if (state === "closed") return;
|
|
99
|
+
state = "closed";
|
|
100
|
+
detach();
|
|
101
|
+
const active = [...waiters];
|
|
102
|
+
waiters.clear();
|
|
103
|
+
for (const record of active)
|
|
104
|
+
if (record.active) {
|
|
105
|
+
record.active = false;
|
|
106
|
+
notify(record.fn);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function initialize(): Context {
|
|
110
|
+
if (state === "closed") stop();
|
|
111
|
+
if (state === "loading") {
|
|
112
|
+
close();
|
|
113
|
+
stop();
|
|
114
|
+
}
|
|
115
|
+
if (state === "live") {
|
|
116
|
+
need(context);
|
|
117
|
+
return context;
|
|
118
|
+
}
|
|
119
|
+
state = "loading";
|
|
120
|
+
try {
|
|
121
|
+
const loaded = loadOwnedOriginalSource();
|
|
122
|
+
need(state === "loading");
|
|
123
|
+
const source = own(loaded, [
|
|
124
|
+
"clock",
|
|
125
|
+
"originalPolicy",
|
|
126
|
+
"originAssociation",
|
|
127
|
+
]);
|
|
128
|
+
const clock = own(source.clock, ["basis", "sample", "subscribeRetirement"]);
|
|
129
|
+
const basis = own(clock.basis, [
|
|
130
|
+
"startWallMs",
|
|
131
|
+
"startNs",
|
|
132
|
+
"workEndWallMs",
|
|
133
|
+
"workEndNs",
|
|
134
|
+
"endWallMs",
|
|
135
|
+
"endNs",
|
|
136
|
+
]);
|
|
137
|
+
need(
|
|
138
|
+
ms(basis.startWallMs) &&
|
|
139
|
+
ms(basis.workEndWallMs) &&
|
|
140
|
+
ms(basis.endWallMs) &&
|
|
141
|
+
ns(basis.startNs) &&
|
|
142
|
+
ns(basis.workEndNs) &&
|
|
143
|
+
ns(basis.endNs) &&
|
|
144
|
+
basis.startWallMs < basis.workEndWallMs &&
|
|
145
|
+
basis.workEndWallMs <= basis.endWallMs &&
|
|
146
|
+
basis.startNs < basis.workEndNs &&
|
|
147
|
+
basis.workEndNs <= basis.endNs,
|
|
148
|
+
);
|
|
149
|
+
const policy = own(source.originalPolicy, [
|
|
150
|
+
"firstDispatchedAt",
|
|
151
|
+
"periodStart",
|
|
152
|
+
"periodEnd",
|
|
153
|
+
]);
|
|
154
|
+
need(
|
|
155
|
+
ms(policy.firstDispatchedAt) &&
|
|
156
|
+
ms(policy.periodStart) &&
|
|
157
|
+
ms(policy.periodEnd) &&
|
|
158
|
+
policy.periodStart < policy.periodEnd,
|
|
159
|
+
);
|
|
160
|
+
need(
|
|
161
|
+
source.originAssociation !== null &&
|
|
162
|
+
typeof source.originAssociation === "object" &&
|
|
163
|
+
!isProxy(source.originAssociation),
|
|
164
|
+
);
|
|
165
|
+
need(
|
|
166
|
+
typeof clock.sample === "function" &&
|
|
167
|
+
!isProxy(clock.sample) &&
|
|
168
|
+
typeof clock.subscribeRetirement === "function" &&
|
|
169
|
+
!isProxy(clock.subscribeRetirement),
|
|
170
|
+
);
|
|
171
|
+
// These assertions follow complete own-data validation above. Nothing is taken from a caller API.
|
|
172
|
+
context = Object.freeze({
|
|
173
|
+
basis: Object.freeze({ ...basis }) as unknown as OriginalBasis,
|
|
174
|
+
policy: Object.freeze({ ...policy }) as unknown as Context["policy"],
|
|
175
|
+
sample: clock.sample as Context["sample"],
|
|
176
|
+
clock: source.clock as object,
|
|
177
|
+
originAssociation: source.originAssociation,
|
|
178
|
+
});
|
|
179
|
+
const unsubscribe: unknown = Reflect.apply(
|
|
180
|
+
clock.subscribeRetirement,
|
|
181
|
+
source.clock,
|
|
182
|
+
[close],
|
|
183
|
+
);
|
|
184
|
+
need(typeof unsubscribe === "function" && !isProxy(unsubscribe));
|
|
185
|
+
detachClock = unsubscribe as () => void;
|
|
186
|
+
if (isClosed()) detach();
|
|
187
|
+
need(state === "loading");
|
|
188
|
+
state = "live";
|
|
189
|
+
return context;
|
|
190
|
+
} catch {
|
|
191
|
+
close();
|
|
192
|
+
stop();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function check(): Readonly<OriginalSample> {
|
|
196
|
+
if (state === "closed" || checking) {
|
|
197
|
+
close();
|
|
198
|
+
stop();
|
|
199
|
+
}
|
|
200
|
+
checking = true;
|
|
201
|
+
try {
|
|
202
|
+
const current = initialize();
|
|
203
|
+
need(state === "live");
|
|
204
|
+
const raw: unknown = Reflect.apply(current.sample, current.clock, []);
|
|
205
|
+
need(state === "live");
|
|
206
|
+
const value = own(raw, ["wallMs", "ns"]),
|
|
207
|
+
basis = current.basis,
|
|
208
|
+
policy = current.policy;
|
|
209
|
+
need(
|
|
210
|
+
ms(value.wallMs) &&
|
|
211
|
+
ns(value.ns) &&
|
|
212
|
+
value.wallMs >= basis.startWallMs &&
|
|
213
|
+
value.ns >= basis.startNs &&
|
|
214
|
+
value.wallMs < basis.workEndWallMs &&
|
|
215
|
+
value.ns < basis.workEndNs &&
|
|
216
|
+
value.wallMs < basis.endWallMs &&
|
|
217
|
+
value.ns < basis.endNs &&
|
|
218
|
+
(!last || (value.wallMs >= last.wallMs && value.ns >= last.ns)),
|
|
219
|
+
);
|
|
220
|
+
const nowSeconds = Math.floor(value.wallMs / 1000),
|
|
221
|
+
lastSeconds = last ? last.nowSeconds : policy.firstDispatchedAt;
|
|
222
|
+
need(
|
|
223
|
+
Number.isSafeInteger(nowSeconds) &&
|
|
224
|
+
nowSeconds >= lastSeconds &&
|
|
225
|
+
nowSeconds >= policy.firstDispatchedAt &&
|
|
226
|
+
nowSeconds - policy.firstDispatchedAt < 86400 &&
|
|
227
|
+
nowSeconds >= policy.periodStart &&
|
|
228
|
+
nowSeconds < policy.periodEnd,
|
|
229
|
+
);
|
|
230
|
+
need(state === "live");
|
|
231
|
+
const accepted = Object.freeze({
|
|
232
|
+
wallMs: value.wallMs,
|
|
233
|
+
ns: value.ns,
|
|
234
|
+
nowSeconds,
|
|
235
|
+
retired: false as const,
|
|
236
|
+
});
|
|
237
|
+
last = accepted;
|
|
238
|
+
return accepted;
|
|
239
|
+
} catch {
|
|
240
|
+
close();
|
|
241
|
+
stop();
|
|
242
|
+
} finally {
|
|
243
|
+
checking = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function subscribe(fn: () => void): () => void {
|
|
247
|
+
if (typeof fn !== "function" || isProxy(fn)) {
|
|
248
|
+
close();
|
|
249
|
+
stop();
|
|
250
|
+
}
|
|
251
|
+
if (state !== "closed") {
|
|
252
|
+
try {
|
|
253
|
+
initialize();
|
|
254
|
+
} catch {
|
|
255
|
+
/* Closed listeners are notified below. */
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (state === "closed") {
|
|
259
|
+
notify(fn);
|
|
260
|
+
return () => {};
|
|
261
|
+
}
|
|
262
|
+
const record = { fn, active: true };
|
|
263
|
+
waiters.add(record);
|
|
264
|
+
return () => {
|
|
265
|
+
if (record.active) {
|
|
266
|
+
record.active = false;
|
|
267
|
+
waiters.delete(record);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/** Stable internal identity. Importing it neither loads a Source nor samples a clock. */
|
|
272
|
+
export const OWNER = Object.freeze({
|
|
273
|
+
get basis() {
|
|
274
|
+
return initialize().basis;
|
|
275
|
+
},
|
|
276
|
+
check,
|
|
277
|
+
sample: check,
|
|
278
|
+
subscribe,
|
|
279
|
+
retire: close,
|
|
280
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
COMPILED_ENTRIES,
|
|
4
|
+
loadOwnedOriginalSource,
|
|
5
|
+
loadFixedStockClient,
|
|
6
|
+
} from "./sourceBindings.js";
|
|
7
|
+
|
|
8
|
+
it("keeps the registry empty and both Source capabilities unbound", () => {
|
|
9
|
+
expect(COMPILED_ENTRIES).toEqual([]);
|
|
10
|
+
expect(Object.isFrozen(COMPILED_ENTRIES)).toBe(true);
|
|
11
|
+
expect(() => loadOwnedOriginalSource()).toThrow("ORIGINAL_OWNER_UNBOUND");
|
|
12
|
+
expect(() => loadFixedStockClient()).toThrow("STOCK_CLIENT_UNBOUND");
|
|
13
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Internal adoption defaults. No production Source capability is bound. */
|
|
2
|
+
export const COMPILED_ENTRIES: readonly never[] = Object.freeze([]);
|
|
3
|
+
export function loadOwnedOriginalSource(): unknown {
|
|
4
|
+
throw new Error("ORIGINAL_OWNER_UNBOUND");
|
|
5
|
+
}
|
|
6
|
+
export function loadFixedStockClient(): unknown {
|
|
7
|
+
throw new Error("STOCK_CLIENT_UNBOUND");
|
|
8
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
3
|
+
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
vi.restoreAllMocks();
|
|
6
|
+
vi.doUnmock("./sourceBindings.js");
|
|
7
|
+
vi.doUnmock("./originalOwner.js");
|
|
8
|
+
vi.resetModules();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
async function delegate(client: unknown, check = () => {}) {
|
|
12
|
+
vi.resetModules();
|
|
13
|
+
vi.doMock("./originalOwner.js", () => ({ OWNER: { check } }));
|
|
14
|
+
vi.doMock("./sourceBindings.js", () => ({
|
|
15
|
+
loadFixedStockClient: () => client,
|
|
16
|
+
}));
|
|
17
|
+
return (await import("./stockRawDelegate.js")).createSourceStockRawDelegate;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
it("captures the inherited stock method and preserves receiver, args and exact promise", async () => {
|
|
21
|
+
const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY");
|
|
22
|
+
const promise = Promise.resolve({ ordinary: true });
|
|
23
|
+
let receiver: unknown;
|
|
24
|
+
let args: unknown[] = [];
|
|
25
|
+
vi.spyOn(Stripe.prototype, "rawRequest").mockImplementation(function (
|
|
26
|
+
this: Stripe,
|
|
27
|
+
...values
|
|
28
|
+
) {
|
|
29
|
+
receiver = this;
|
|
30
|
+
args = values;
|
|
31
|
+
return promise as ReturnType<Stripe["rawRequest"]>;
|
|
32
|
+
});
|
|
33
|
+
const create = await delegate(client);
|
|
34
|
+
let raw: ReturnType<typeof create> | undefined;
|
|
35
|
+
try {
|
|
36
|
+
raw = create();
|
|
37
|
+
} catch {
|
|
38
|
+
/* Fixed STOP scaffold must fail the live assertion. */
|
|
39
|
+
}
|
|
40
|
+
expect(raw).toBeDefined();
|
|
41
|
+
if (!raw) throw new Error("missing live delegate");
|
|
42
|
+
expect(Object.isFrozen(raw)).toBe(true);
|
|
43
|
+
expect(Object.prototype.hasOwnProperty.call(raw, "rawRequest")).toBe(true);
|
|
44
|
+
const options = { maxNetworkRetries: 0, idempotencyKey: "retained" };
|
|
45
|
+
expect(
|
|
46
|
+
raw.rawRequest(
|
|
47
|
+
"POST",
|
|
48
|
+
"/v1/subscription_schedules",
|
|
49
|
+
{ from_subscription: "sub_original" },
|
|
50
|
+
options,
|
|
51
|
+
),
|
|
52
|
+
).toBe(promise);
|
|
53
|
+
expect(receiver).toBe(client);
|
|
54
|
+
expect(args[3]).toBe(options);
|
|
55
|
+
expect(args).toEqual([
|
|
56
|
+
"POST",
|
|
57
|
+
"/v1/subscription_schedules",
|
|
58
|
+
{ from_subscription: "sub_original" },
|
|
59
|
+
options,
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("forwards the identical synchronous stock throw without retry or wrapping", async () => {
|
|
64
|
+
const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY"),
|
|
65
|
+
error = Error("SYNTHETIC");
|
|
66
|
+
let calls = 0;
|
|
67
|
+
vi.spyOn(Stripe.prototype, "rawRequest").mockImplementation(() => {
|
|
68
|
+
calls++;
|
|
69
|
+
throw error;
|
|
70
|
+
});
|
|
71
|
+
const create = await delegate(client);
|
|
72
|
+
const raw = create();
|
|
73
|
+
try {
|
|
74
|
+
void raw.rawRequest("GET", "/v1/customers/cus_original");
|
|
75
|
+
expect.fail("must throw");
|
|
76
|
+
} catch (caught) {
|
|
77
|
+
expect(caught).toBe(error);
|
|
78
|
+
}
|
|
79
|
+
expect(calls).toBe(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("forwards a real stock SDK request to a closed synthetic HTTP delegate once", async () => {
|
|
83
|
+
const requests: unknown[][] = [];
|
|
84
|
+
const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY", {
|
|
85
|
+
maxNetworkRetries: 0,
|
|
86
|
+
httpClient: Stripe.createFetchHttpClient(async (url, init) => {
|
|
87
|
+
requests.push([url, init?.method, init?.body]);
|
|
88
|
+
return new Response(
|
|
89
|
+
JSON.stringify({
|
|
90
|
+
id: "sub_sched_original",
|
|
91
|
+
object: "subscription_schedule",
|
|
92
|
+
}),
|
|
93
|
+
{ status: 200, headers: { "content-type": "application/json" } },
|
|
94
|
+
);
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
const create = await delegate(client),
|
|
98
|
+
raw = create();
|
|
99
|
+
const result = await raw.rawRequest(
|
|
100
|
+
"POST",
|
|
101
|
+
"/v1/subscription_schedules",
|
|
102
|
+
{ from_subscription: "sub_original" },
|
|
103
|
+
{ maxNetworkRetries: 0, idempotencyKey: "same-retained-key" },
|
|
104
|
+
);
|
|
105
|
+
expect(result.id).toBe("sub_sched_original");
|
|
106
|
+
expect(requests).toHaveLength(1);
|
|
107
|
+
expect(requests[0][1]).toBe("POST");
|
|
108
|
+
expect(requests[0][2]).toBe("from_subscription=sub_original");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it.each(["foreign", "own-method", "proxy"])(
|
|
112
|
+
"refuses %s clients before a raw request",
|
|
113
|
+
async (kind) => {
|
|
114
|
+
const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY");
|
|
115
|
+
let traps = 0;
|
|
116
|
+
const supplied =
|
|
117
|
+
kind === "foreign"
|
|
118
|
+
? {}
|
|
119
|
+
: kind === "proxy"
|
|
120
|
+
? new Proxy(client, {
|
|
121
|
+
getPrototypeOf() {
|
|
122
|
+
traps++;
|
|
123
|
+
return Stripe.prototype;
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
: Object.defineProperty(client, "rawRequest", {
|
|
127
|
+
value() {
|
|
128
|
+
throw Error("MUST_NOT_CALL");
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const create = await delegate(supplied);
|
|
132
|
+
expect(() => create()).toThrow("STOCK_SOURCE_DELEGATE_UNBOUND");
|
|
133
|
+
expect(traps).toBe(0);
|
|
134
|
+
},
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
it("checks the owner before loading or inspecting a client and rejects parameters", async () => {
|
|
138
|
+
let gets = 0;
|
|
139
|
+
const client = new Proxy(
|
|
140
|
+
{},
|
|
141
|
+
{
|
|
142
|
+
getPrototypeOf() {
|
|
143
|
+
gets++;
|
|
144
|
+
return null;
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
const create = await delegate(client, () => {
|
|
149
|
+
throw Error("RETIRED");
|
|
150
|
+
});
|
|
151
|
+
expect(() => create()).toThrow("RETIRED");
|
|
152
|
+
expect(gets).toBe(0);
|
|
153
|
+
expect(() => Reflect.apply(create, undefined, [client])).toThrow(
|
|
154
|
+
"STOCK_SOURCE_DELEGATE_UNBOUND",
|
|
155
|
+
);
|
|
156
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import Stripe from "stripe";
|
|
2
|
+
import { types } from "node:util";
|
|
3
|
+
import { OWNER } from "./originalOwner.js";
|
|
4
|
+
import { loadFixedStockClient } from "./sourceBindings.js";
|
|
5
|
+
|
|
6
|
+
function stop(): never {
|
|
7
|
+
throw new Error("STOCK_SOURCE_DELEGATE_UNBOUND");
|
|
8
|
+
}
|
|
9
|
+
const apply = Reflect.apply;
|
|
10
|
+
/** Internal fixed Source facade, not a public client or qualification selector. */
|
|
11
|
+
export function createSourceStockRawDelegate(): Readonly<{
|
|
12
|
+
rawRequest: Stripe["rawRequest"];
|
|
13
|
+
}> {
|
|
14
|
+
if (arguments.length !== 0) stop();
|
|
15
|
+
OWNER.check();
|
|
16
|
+
const client = loadFixedStockClient();
|
|
17
|
+
OWNER.check();
|
|
18
|
+
if (
|
|
19
|
+
!client ||
|
|
20
|
+
typeof client !== "object" ||
|
|
21
|
+
types.isProxy(client) ||
|
|
22
|
+
Object.getPrototypeOf(client) !== Stripe.prototype ||
|
|
23
|
+
Object.prototype.hasOwnProperty.call(client, "rawRequest")
|
|
24
|
+
)
|
|
25
|
+
stop();
|
|
26
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
27
|
+
Stripe.prototype,
|
|
28
|
+
"rawRequest",
|
|
29
|
+
);
|
|
30
|
+
if (
|
|
31
|
+
!descriptor ||
|
|
32
|
+
!Object.prototype.hasOwnProperty.call(descriptor, "value") ||
|
|
33
|
+
typeof descriptor.value !== "function"
|
|
34
|
+
)
|
|
35
|
+
stop();
|
|
36
|
+
const method = descriptor.value as Stripe["rawRequest"];
|
|
37
|
+
OWNER.check();
|
|
38
|
+
return Object.freeze({
|
|
39
|
+
rawRequest(...args: Parameters<Stripe["rawRequest"]>) {
|
|
40
|
+
return apply(method, client, args) as ReturnType<Stripe["rawRequest"]>;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -100,9 +100,13 @@ export function prepareRenewalPriceReplacement(
|
|
|
100
100
|
if (subscription[field] != null)
|
|
101
101
|
throw new Error(`Renewal replacement does not support ${field}.`);
|
|
102
102
|
const providerSnapshot = renewalSnapshot({
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
// Validation supplies semantic types; retain the complete detached bodies.
|
|
104
|
+
// Passthrough parsers can omit safe own JSON keys such as __proto__.
|
|
105
|
+
subscription: source.subscription as typeof subscription,
|
|
106
|
+
targetPrice: source.targetPrice as typeof targetPrice,
|
|
107
|
+
...(billingCustomer === undefined
|
|
108
|
+
? {}
|
|
109
|
+
: { billingCustomer: args.billingCustomer as typeof billingCustomer }),
|
|
106
110
|
});
|
|
107
111
|
const bound = {
|
|
108
112
|
version: 1 as const,
|