@fantastic.dev/better-stripe 0.107.0 → 0.108.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 +6 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/observation/reader.d.ts +64 -0
- package/dist/client/billing/renewalAttach/internal/observation/reader.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/reader.js +170 -0
- package/dist/client/billing/renewalAttach/internal/observation/reader.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts +44 -0
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js +145 -0
- package/dist/client/billing/renewalAttach/internal/observation/readerAuthorization.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.d.ts +2 -0
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.js +1 -0
- package/dist/client/billing/renewalAttach/internal/operations/sourceOperations.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +2 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +9 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
- package/dist/component/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +16 -16
- package/docs/renewal-context-reads.md +52 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/observation/reader.integration.test.ts +93 -0
- package/src/client/billing/renewalAttach/internal/observation/reader.ts +241 -0
- package/src/client/billing/renewalAttach/internal/observation/readerAuthorization.ts +179 -0
- package/src/client/billing/renewalAttach/internal/observation/readerControls.integration.test.ts +553 -0
- package/src/client/billing/renewalAttach/internal/operations/sourceOperations.ts +3 -0
- package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +12 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/observationReader.ts +100 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerConfigureAdmission.ts +9 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +5 -1
package/src/client/billing/renewalAttach/internal/observation/readerControls.integration.test.ts
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import { expect, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
configureAdmissionCase,
|
|
4
|
+
admissionControls,
|
|
5
|
+
} from "../test-fixtures/producerConfigureAdmission.js";
|
|
6
|
+
import {
|
|
7
|
+
prepareObservationReader,
|
|
8
|
+
observerIdentity,
|
|
9
|
+
authorizeObservation,
|
|
10
|
+
readerControls,
|
|
11
|
+
} from "../test-fixtures/observationReader.js";
|
|
12
|
+
import { captureAuthorizedObservationHistoryReader } from "./reader.js";
|
|
13
|
+
import { BetterStripe } from "../../../../index.js";
|
|
14
|
+
import { getFunctionAddress } from "convex/server";
|
|
15
|
+
function mutable(value: unknown): Record<string, unknown> {
|
|
16
|
+
if (!value || typeof value !== "object") throw Error("FIXTURE_DATA");
|
|
17
|
+
return value as Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
configureAdmissionCase(
|
|
20
|
+
"manifest mutation after an event reply cannot extend the fixed event-query bound",
|
|
21
|
+
prepareObservationReader,
|
|
22
|
+
async (h, state) => {
|
|
23
|
+
await h.inAnotherAction(async (ctx) => {
|
|
24
|
+
const query = ctx.runQuery;
|
|
25
|
+
let manifest: unknown[] | null = null,
|
|
26
|
+
eventQueries = 0;
|
|
27
|
+
ctx.runQuery = async (ref, ...args) => {
|
|
28
|
+
const result = await query(ref, ...args);
|
|
29
|
+
const address = JSON.stringify(getFunctionAddress(ref));
|
|
30
|
+
if (
|
|
31
|
+
address.includes("readAttempt") &&
|
|
32
|
+
mutable(args[0]).operationId === state.operationId
|
|
33
|
+
)
|
|
34
|
+
manifest = mutable(result).events as unknown[];
|
|
35
|
+
if (
|
|
36
|
+
address.includes("readStageEvent") &&
|
|
37
|
+
mutable(args[0]).operationId === state.operationId
|
|
38
|
+
) {
|
|
39
|
+
eventQueries++;
|
|
40
|
+
if (eventQueries === 1 && manifest)
|
|
41
|
+
manifest.push(structuredClone(manifest[2]));
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
46
|
+
actualActionCtx: ctx,
|
|
47
|
+
operationId: state.operationId,
|
|
48
|
+
parentOperationId: state.parentOperationId,
|
|
49
|
+
});
|
|
50
|
+
const result = await reader.read().catch(() => null);
|
|
51
|
+
expect(eventQueries).toBe(3);
|
|
52
|
+
expect(result?.history.configureHead).toBe(3);
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
56
|
+
);
|
|
57
|
+
configureAdmissionCase(
|
|
58
|
+
"Proxy event manifests reject before length or key traps and before event queries",
|
|
59
|
+
prepareObservationReader,
|
|
60
|
+
async (h, state) => {
|
|
61
|
+
await h.inAnotherAction(async (ctx) => {
|
|
62
|
+
const query = ctx.runQuery;
|
|
63
|
+
let traps = 0,
|
|
64
|
+
events = 0;
|
|
65
|
+
ctx.runQuery = async (ref, ...args) => {
|
|
66
|
+
const result = await query(ref, ...args);
|
|
67
|
+
const address = JSON.stringify(getFunctionAddress(ref));
|
|
68
|
+
if (
|
|
69
|
+
address.includes("readAttempt") &&
|
|
70
|
+
mutable(args[0]).operationId === state.parentOperationId
|
|
71
|
+
) {
|
|
72
|
+
const array = mutable(result).events;
|
|
73
|
+
mutable(result).events = new Proxy(array as unknown[], {
|
|
74
|
+
get() {
|
|
75
|
+
traps++;
|
|
76
|
+
throw Error("TRAP");
|
|
77
|
+
},
|
|
78
|
+
ownKeys() {
|
|
79
|
+
traps++;
|
|
80
|
+
throw Error("TRAP");
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (address.includes("readStageEvent")) events++;
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
87
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
88
|
+
actualActionCtx: ctx,
|
|
89
|
+
operationId: state.operationId,
|
|
90
|
+
parentOperationId: state.parentOperationId,
|
|
91
|
+
});
|
|
92
|
+
await expect(reader.read()).rejects.toThrow();
|
|
93
|
+
expect(traps).toBe(0);
|
|
94
|
+
expect(events).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
98
|
+
);
|
|
99
|
+
configureAdmissionCase(
|
|
100
|
+
"auth, RPC, root and host address drift during authorization closes before DATA reads",
|
|
101
|
+
prepareObservationReader,
|
|
102
|
+
async (h, state) => {
|
|
103
|
+
for (const mode of ["auth", "rpc", "root", "address"] as const)
|
|
104
|
+
await h.inAnotherAction(async (ctx) => {
|
|
105
|
+
const query = ctx.runQuery,
|
|
106
|
+
identity = ctx.auth.getUserIdentity,
|
|
107
|
+
proto = Object.getPrototypeOf(h.sdk);
|
|
108
|
+
let calls = 0;
|
|
109
|
+
ctx.runQuery = async (ref, ...args) => {
|
|
110
|
+
const value = await query(ref, ...args);
|
|
111
|
+
if (++calls === 1) {
|
|
112
|
+
if (mode === "auth") ctx.auth.getUserIdentity = async () => null;
|
|
113
|
+
if (mode === "rpc") ctx.runQuery = async () => null;
|
|
114
|
+
if (mode === "root") Object.setPrototypeOf(h.sdk, {});
|
|
115
|
+
if (mode === "address")
|
|
116
|
+
Reflect.set(
|
|
117
|
+
state.entry.authorizationQuery,
|
|
118
|
+
Symbol.for("functionName"),
|
|
119
|
+
"initialOwner:foreign",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
return value;
|
|
123
|
+
};
|
|
124
|
+
try {
|
|
125
|
+
const reader = await captureAuthorizedObservationHistoryReader(
|
|
126
|
+
h.sdk,
|
|
127
|
+
{
|
|
128
|
+
actualActionCtx: ctx,
|
|
129
|
+
operationId: state.operationId,
|
|
130
|
+
parentOperationId: state.parentOperationId,
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
await expect(reader.read()).rejects.toThrow();
|
|
134
|
+
expect(calls).toBe(1);
|
|
135
|
+
} finally {
|
|
136
|
+
ctx.runQuery = query;
|
|
137
|
+
ctx.auth.getUserIdentity = identity;
|
|
138
|
+
Object.setPrototypeOf(h.sdk, proto);
|
|
139
|
+
Reflect.set(
|
|
140
|
+
state.entry.authorizationQuery,
|
|
141
|
+
Symbol.for("functionName"),
|
|
142
|
+
"initialOwner:authorize",
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
148
|
+
);
|
|
149
|
+
configureAdmissionCase(
|
|
150
|
+
"all four component query address substitutions refuse before authorization",
|
|
151
|
+
prepareObservationReader,
|
|
152
|
+
async (h, state) => {
|
|
153
|
+
const actual = state.entry.component,
|
|
154
|
+
q = actual.renewalOperations.queries;
|
|
155
|
+
const queries = {
|
|
156
|
+
loadAuthorizedOperation: q.loadAuthorizedOperation,
|
|
157
|
+
readAttempt: q.readAttempt,
|
|
158
|
+
readStageEvent: q.readStageEvent,
|
|
159
|
+
readProviderAcknowledgement: q.readProviderAcknowledgement,
|
|
160
|
+
};
|
|
161
|
+
const operations = new Proxy(actual.renewalOperations, {
|
|
162
|
+
get(target, key) {
|
|
163
|
+
return key === "queries" ? queries : Reflect.get(target, key);
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
const component = new Proxy(actual, {
|
|
167
|
+
get(target, key) {
|
|
168
|
+
return key === "renewalOperations"
|
|
169
|
+
? operations
|
|
170
|
+
: Reflect.get(target, key);
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
const root = new BetterStripe(component, {
|
|
174
|
+
STRIPE_SECRET_KEY: "sk_test_invalid_read_only",
|
|
175
|
+
});
|
|
176
|
+
admissionControls.history.splice(
|
|
177
|
+
0,
|
|
178
|
+
1,
|
|
179
|
+
Object.freeze({ ...state.entry, component }),
|
|
180
|
+
);
|
|
181
|
+
for (const key of Object.keys(queries))
|
|
182
|
+
await h.inAnotherAction(async (ctx) => {
|
|
183
|
+
const reader = await captureAuthorizedObservationHistoryReader(root, {
|
|
184
|
+
actualActionCtx: ctx,
|
|
185
|
+
operationId: state.operationId,
|
|
186
|
+
parentOperationId: state.parentOperationId,
|
|
187
|
+
});
|
|
188
|
+
const original = Reflect.get(queries, key);
|
|
189
|
+
Reflect.set(
|
|
190
|
+
queries,
|
|
191
|
+
key,
|
|
192
|
+
key === "readAttempt" ? queries.readStageEvent : queries.readAttempt,
|
|
193
|
+
);
|
|
194
|
+
try {
|
|
195
|
+
await expect(reader.read()).rejects.toThrow();
|
|
196
|
+
} finally {
|
|
197
|
+
Reflect.set(queries, key, original);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
expect(readerControls.authorizations).toBe(0);
|
|
201
|
+
},
|
|
202
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
203
|
+
);
|
|
204
|
+
configureAdmissionCase(
|
|
205
|
+
"authorization expiry after the first component query stops all later reads",
|
|
206
|
+
prepareObservationReader,
|
|
207
|
+
async (h, state) => {
|
|
208
|
+
readerControls.grant.expiresAtMs = Date.now() + 1000;
|
|
209
|
+
await h.inAnotherAction(async (ctx) => {
|
|
210
|
+
const query = ctx.runQuery;
|
|
211
|
+
let queries = 0;
|
|
212
|
+
ctx.runQuery = async (ref, ...args) => {
|
|
213
|
+
const result = await query(ref, ...args);
|
|
214
|
+
if (++queries === 2)
|
|
215
|
+
vi.setSystemTime(Number(readerControls.grant.expiresAtMs));
|
|
216
|
+
return result;
|
|
217
|
+
};
|
|
218
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
219
|
+
actualActionCtx: ctx,
|
|
220
|
+
operationId: state.operationId,
|
|
221
|
+
parentOperationId: state.parentOperationId,
|
|
222
|
+
});
|
|
223
|
+
await expect(reader.read()).rejects.toThrow();
|
|
224
|
+
expect(queries).toBe(2);
|
|
225
|
+
});
|
|
226
|
+
},
|
|
227
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
228
|
+
);
|
|
229
|
+
const changes: [string, (r: Record<string, unknown>) => void][] = [
|
|
230
|
+
[
|
|
231
|
+
"subject",
|
|
232
|
+
(r) => {
|
|
233
|
+
r.tokenIdentifier = "foreign";
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
"component",
|
|
238
|
+
(r) => {
|
|
239
|
+
r.componentScope = "foreign";
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
[
|
|
243
|
+
"parent",
|
|
244
|
+
(r) => {
|
|
245
|
+
r.parentOperationId = "foreign";
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
[
|
|
249
|
+
"child",
|
|
250
|
+
(r) => {
|
|
251
|
+
r.operationId = "foreign";
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
[
|
|
255
|
+
"account",
|
|
256
|
+
(r) => {
|
|
257
|
+
mutable(r.providerScope).customerAccountId = "foreign";
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
"subscription",
|
|
262
|
+
(r) => {
|
|
263
|
+
mutable(r.providerScope).subscriptionId = "sub_foreign";
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
[
|
|
267
|
+
"schedule",
|
|
268
|
+
(r) => {
|
|
269
|
+
mutable(r.providerScope).scheduleId = "sub_sched_foreign";
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
[
|
|
273
|
+
"mode",
|
|
274
|
+
(r) => {
|
|
275
|
+
mutable(r.providerScope).mode = "connected";
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
[
|
|
279
|
+
"stripeAccount",
|
|
280
|
+
(r) => {
|
|
281
|
+
mutable(r.providerScope).stripeAccount = "acct_foreign";
|
|
282
|
+
},
|
|
283
|
+
],
|
|
284
|
+
[
|
|
285
|
+
"livemode",
|
|
286
|
+
(r) => {
|
|
287
|
+
mutable(r.providerScope).livemode = true;
|
|
288
|
+
},
|
|
289
|
+
],
|
|
290
|
+
[
|
|
291
|
+
"id",
|
|
292
|
+
(r) => {
|
|
293
|
+
r.authorizationId = "";
|
|
294
|
+
},
|
|
295
|
+
],
|
|
296
|
+
[
|
|
297
|
+
"revision",
|
|
298
|
+
(r) => {
|
|
299
|
+
r.authorizationRevision = 0;
|
|
300
|
+
},
|
|
301
|
+
],
|
|
302
|
+
[
|
|
303
|
+
"expired",
|
|
304
|
+
(r) => {
|
|
305
|
+
r.expiresAtMs = Date.now();
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
[
|
|
309
|
+
"extra",
|
|
310
|
+
(r) => {
|
|
311
|
+
r.allowed = true;
|
|
312
|
+
},
|
|
313
|
+
],
|
|
314
|
+
[
|
|
315
|
+
"stable revision changes",
|
|
316
|
+
(r) => {
|
|
317
|
+
r.authorizationRevision = 4;
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
[
|
|
321
|
+
"stable expiry changes",
|
|
322
|
+
(r) => {
|
|
323
|
+
r.expiresAtMs = Number(r.expiresAtMs) + 1;
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
[
|
|
327
|
+
"stable authorization id changes",
|
|
328
|
+
(r) => {
|
|
329
|
+
r.authorizationId = "other-valid-id";
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
];
|
|
333
|
+
for (const [name, change] of changes)
|
|
334
|
+
configureAdmissionCase(
|
|
335
|
+
"rejects first and final authorization: " + name,
|
|
336
|
+
prepareObservationReader,
|
|
337
|
+
async (h, state) => {
|
|
338
|
+
for (const final of [false, true]) {
|
|
339
|
+
readerControls.authorizations = 0;
|
|
340
|
+
readerControls.reply = (r, count) => {
|
|
341
|
+
if (!final || count === 2) change(r);
|
|
342
|
+
return r;
|
|
343
|
+
};
|
|
344
|
+
await h.inAnotherAction(async (ctx) => {
|
|
345
|
+
const reader = await captureAuthorizedObservationHistoryReader(
|
|
346
|
+
h.sdk,
|
|
347
|
+
{
|
|
348
|
+
actualActionCtx: ctx,
|
|
349
|
+
operationId: state.operationId,
|
|
350
|
+
parentOperationId: state.parentOperationId,
|
|
351
|
+
},
|
|
352
|
+
);
|
|
353
|
+
let refused = false;
|
|
354
|
+
try {
|
|
355
|
+
await reader.read();
|
|
356
|
+
} catch {
|
|
357
|
+
refused = true;
|
|
358
|
+
}
|
|
359
|
+
const stableChange = name.startsWith("stable ");
|
|
360
|
+
expect(refused, (final ? "final " : "first ") + name).toBe(
|
|
361
|
+
final || !stableChange,
|
|
362
|
+
);
|
|
363
|
+
await expect(reader.read()).rejects.toThrow();
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
},
|
|
367
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
368
|
+
);
|
|
369
|
+
configureAdmissionCase(
|
|
370
|
+
"idle deadline, clock rollback and exact wall/ns equality permanently close the reader",
|
|
371
|
+
prepareObservationReader,
|
|
372
|
+
async (h, state) => {
|
|
373
|
+
const wall = Date.now(),
|
|
374
|
+
ns = process.hrtime.bigint();
|
|
375
|
+
for (const mode of [
|
|
376
|
+
"idle",
|
|
377
|
+
"wall-end",
|
|
378
|
+
"ns-end",
|
|
379
|
+
"wall-back",
|
|
380
|
+
"ns-back",
|
|
381
|
+
] as const) {
|
|
382
|
+
vi.setSystemTime(wall);
|
|
383
|
+
vi.mocked(process.hrtime.bigint).mockReturnValue(ns);
|
|
384
|
+
await h.inAnotherAction(async (ctx) => {
|
|
385
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
386
|
+
actualActionCtx: ctx,
|
|
387
|
+
operationId: state.operationId,
|
|
388
|
+
parentOperationId: state.parentOperationId,
|
|
389
|
+
});
|
|
390
|
+
if (mode === "idle") await vi.advanceTimersByTimeAsync(30000);
|
|
391
|
+
if (mode === "wall-end") vi.setSystemTime(wall + 30000);
|
|
392
|
+
if (mode === "wall-back") vi.setSystemTime(wall - 1);
|
|
393
|
+
if (mode === "ns-end")
|
|
394
|
+
vi.mocked(process.hrtime.bigint).mockReturnValue(ns + 30000000000n);
|
|
395
|
+
if (mode === "ns-back")
|
|
396
|
+
vi.mocked(process.hrtime.bigint).mockReturnValue(ns - 1n);
|
|
397
|
+
await expect(reader.read()).rejects.toThrow(
|
|
398
|
+
"OBSERVATION_HISTORY_READER_EXPIRED",
|
|
399
|
+
);
|
|
400
|
+
await expect(reader.read()).rejects.toThrow();
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
vi.setSystemTime(wall);
|
|
404
|
+
vi.mocked(process.hrtime.bigint).mockReturnValue(ns);
|
|
405
|
+
},
|
|
406
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
407
|
+
);
|
|
408
|
+
configureAdmissionCase(
|
|
409
|
+
"fixed authorization address mutation is detected before any query",
|
|
410
|
+
prepareObservationReader,
|
|
411
|
+
async (h, state) => {
|
|
412
|
+
await h.inAnotherAction(async (ctx) => {
|
|
413
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
414
|
+
actualActionCtx: ctx,
|
|
415
|
+
operationId: state.operationId,
|
|
416
|
+
parentOperationId: state.parentOperationId,
|
|
417
|
+
});
|
|
418
|
+
const key = Symbol.for("functionName");
|
|
419
|
+
Reflect.set(state.entry.authorizationQuery, key, "initialOwner:foreign");
|
|
420
|
+
try {
|
|
421
|
+
await expect(reader.read()).rejects.toThrow();
|
|
422
|
+
} finally {
|
|
423
|
+
Reflect.set(
|
|
424
|
+
state.entry.authorizationQuery,
|
|
425
|
+
key,
|
|
426
|
+
"initialOwner:authorize",
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
expect(readerControls.authorizations).toBe(0);
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
433
|
+
);
|
|
434
|
+
configureAdmissionCase(
|
|
435
|
+
"null identity and host denial refuse before component reads",
|
|
436
|
+
prepareObservationReader,
|
|
437
|
+
async (h, state) => {
|
|
438
|
+
for (const denied of [false, true])
|
|
439
|
+
await h.inAnotherAction(async (ctx) => {
|
|
440
|
+
const originalIdentity = ctx.auth.getUserIdentity;
|
|
441
|
+
if (!denied) ctx.auth.getUserIdentity = async () => null;
|
|
442
|
+
readerControls.deny = denied;
|
|
443
|
+
const queries = vi.spyOn(ctx, "runQuery");
|
|
444
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
445
|
+
actualActionCtx: ctx,
|
|
446
|
+
operationId: state.operationId,
|
|
447
|
+
parentOperationId: state.parentOperationId,
|
|
448
|
+
});
|
|
449
|
+
await expect(reader.read()).rejects.toThrow();
|
|
450
|
+
expect(queries).toHaveBeenCalledTimes(denied ? 1 : 0);
|
|
451
|
+
ctx.auth.getUserIdentity = originalIdentity;
|
|
452
|
+
queries.mockRestore();
|
|
453
|
+
});
|
|
454
|
+
},
|
|
455
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
456
|
+
);
|
|
457
|
+
configureAdmissionCase(
|
|
458
|
+
"hung metadata and hung authorization expire once and ignore late replies",
|
|
459
|
+
prepareObservationReader,
|
|
460
|
+
async (h, state) => {
|
|
461
|
+
await h.inAnotherAction(async (ctx) => {
|
|
462
|
+
const metadata = ctx.meta.getFunctionMetadata;
|
|
463
|
+
let release!: () => void;
|
|
464
|
+
const held = new Promise<void>((resolve) => {
|
|
465
|
+
release = resolve;
|
|
466
|
+
});
|
|
467
|
+
ctx.meta.getFunctionMetadata = async () => {
|
|
468
|
+
await held;
|
|
469
|
+
return metadata();
|
|
470
|
+
};
|
|
471
|
+
const pending = captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
472
|
+
actualActionCtx: ctx,
|
|
473
|
+
operationId: state.operationId,
|
|
474
|
+
parentOperationId: state.parentOperationId,
|
|
475
|
+
});
|
|
476
|
+
const rejected = expect(pending).rejects.toThrow(
|
|
477
|
+
"OBSERVATION_HISTORY_READER_EXPIRED",
|
|
478
|
+
);
|
|
479
|
+
await vi.advanceTimersByTimeAsync(30000);
|
|
480
|
+
await rejected;
|
|
481
|
+
release();
|
|
482
|
+
await Promise.resolve();
|
|
483
|
+
});
|
|
484
|
+
await h.inAnotherAction(async (ctx) => {
|
|
485
|
+
let release!: () => void;
|
|
486
|
+
const held = new Promise<void>((resolve) => {
|
|
487
|
+
release = resolve;
|
|
488
|
+
});
|
|
489
|
+
ctx.runQuery = async () => {
|
|
490
|
+
await held;
|
|
491
|
+
return null;
|
|
492
|
+
};
|
|
493
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
494
|
+
actualActionCtx: ctx,
|
|
495
|
+
operationId: state.operationId,
|
|
496
|
+
parentOperationId: state.parentOperationId,
|
|
497
|
+
});
|
|
498
|
+
const pending = reader.read();
|
|
499
|
+
const rejected = expect(pending).rejects.toThrow(
|
|
500
|
+
"OBSERVATION_HISTORY_READER_EXPIRED",
|
|
501
|
+
);
|
|
502
|
+
await vi.advanceTimersByTimeAsync(30000);
|
|
503
|
+
await rejected;
|
|
504
|
+
release();
|
|
505
|
+
await expect(reader.read()).rejects.toThrow();
|
|
506
|
+
});
|
|
507
|
+
},
|
|
508
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
509
|
+
);
|
|
510
|
+
configureAdmissionCase(
|
|
511
|
+
"refuses ambiguous/wrong component entries and captured pointer drift",
|
|
512
|
+
prepareObservationReader,
|
|
513
|
+
async (h, state) => {
|
|
514
|
+
admissionControls.history.push(state.entry);
|
|
515
|
+
await expect(
|
|
516
|
+
captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
517
|
+
actualActionCtx: h.ctx,
|
|
518
|
+
operationId: state.operationId,
|
|
519
|
+
parentOperationId: state.parentOperationId,
|
|
520
|
+
}),
|
|
521
|
+
).rejects.toThrow();
|
|
522
|
+
admissionControls.history.splice(0, admissionControls.history.length, {
|
|
523
|
+
...state.entry,
|
|
524
|
+
component: {},
|
|
525
|
+
});
|
|
526
|
+
await expect(
|
|
527
|
+
captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
528
|
+
actualActionCtx: h.ctx,
|
|
529
|
+
operationId: state.operationId,
|
|
530
|
+
parentOperationId: state.parentOperationId,
|
|
531
|
+
}),
|
|
532
|
+
).rejects.toThrow();
|
|
533
|
+
admissionControls.history.splice(0, 1, state.entry);
|
|
534
|
+
for (const mode of ["auth", "query", "root"] as const)
|
|
535
|
+
await h.inAnotherAction(async (ctx) => {
|
|
536
|
+
const reader = await captureAuthorizedObservationHistoryReader(h.sdk, {
|
|
537
|
+
actualActionCtx: ctx,
|
|
538
|
+
operationId: state.operationId,
|
|
539
|
+
parentOperationId: state.parentOperationId,
|
|
540
|
+
});
|
|
541
|
+
if (mode === "auth") ctx.auth.getUserIdentity = async () => null;
|
|
542
|
+
if (mode === "query") ctx.runQuery = async () => null;
|
|
543
|
+
const proto = Object.getPrototypeOf(h.sdk);
|
|
544
|
+
if (mode === "root") Object.setPrototypeOf(h.sdk, {});
|
|
545
|
+
try {
|
|
546
|
+
await expect(reader.read()).rejects.toThrow();
|
|
547
|
+
} finally {
|
|
548
|
+
Object.setPrototypeOf(h.sdk, proto);
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
},
|
|
552
|
+
{ identity: observerIdentity, hostQuery: authorizeObservation },
|
|
553
|
+
);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ObservationHistorySourceEntry } from "../observation/readerAuthorization.js";
|
|
1
2
|
/** Fixed internal operation associations. No production producer or class is bound. */
|
|
2
3
|
export const COMPILED_INITIAL_ENTRIES: readonly unknown[] = Object.freeze([]);
|
|
3
4
|
export const COMPILED_RECOVERY_ENTRIES: readonly unknown[] = Object.freeze([]);
|
|
@@ -45,6 +46,8 @@ export function loadConfigureReplayTransportSource(): unknown {
|
|
|
45
46
|
export const COMPILED_OBSERVATION_ENTRIES: readonly unknown[] = Object.freeze(
|
|
46
47
|
[],
|
|
47
48
|
);
|
|
49
|
+
export const COMPILED_OBSERVATION_HISTORY_ENTRIES: readonly ObservationHistorySourceEntry[] =
|
|
50
|
+
Object.freeze([]);
|
|
48
51
|
export function loadFixedObservationOperation(): unknown {
|
|
49
52
|
throw Error("OBSERVATION_SOURCE_UNBOUND");
|
|
50
53
|
}
|
|
@@ -26,6 +26,18 @@ function rootUnbound(): never {
|
|
|
26
26
|
throw Error("CLASS_ACTION_ROOT_UNBOUND");
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
/** Neutral construction identity only; no Action, client, Owner or execution grant. */
|
|
30
|
+
export function readMaintainedRootComponent(actualRoot: object): Component {
|
|
31
|
+
const cell = roots.get(actualRoot);
|
|
32
|
+
if (
|
|
33
|
+
!cell ||
|
|
34
|
+
cell.phase !== "ready" ||
|
|
35
|
+
Object.getPrototypeOf(actualRoot) !== cell.bootstrap.prototype
|
|
36
|
+
)
|
|
37
|
+
rootUnbound();
|
|
38
|
+
return cell.component;
|
|
39
|
+
}
|
|
40
|
+
|
|
29
41
|
/** Internal neutral base; construction alone grants no operational authority. */
|
|
30
42
|
export abstract class ClassOwnedActionRoot extends RenewalContextClient {
|
|
31
43
|
protected constructor(
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { makeFunctionReference, queryGeneric } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { vi } from "vitest";
|
|
4
|
+
import { historySnapshot } from "./historySnapshot.js";
|
|
5
|
+
import { admissionControls } from "./producerConfigureAdmission.js";
|
|
6
|
+
import {
|
|
7
|
+
captureMaintainedJournalBinding,
|
|
8
|
+
readCheckedJournalActionRPC,
|
|
9
|
+
} from "../producer/classOwnedActionRoot.js";
|
|
10
|
+
export const observerIdentity = {
|
|
11
|
+
subject: "observer",
|
|
12
|
+
issuer: "https://host.test",
|
|
13
|
+
tokenIdentifier: "https://host.test|observer",
|
|
14
|
+
};
|
|
15
|
+
export const readerControls = {
|
|
16
|
+
grant: {} as Record<string, unknown>,
|
|
17
|
+
args: {} as Record<string, unknown>,
|
|
18
|
+
authorizations: 0,
|
|
19
|
+
deny: false,
|
|
20
|
+
reply: null as
|
|
21
|
+
null | ((grant: Record<string, unknown>, count: number) => unknown),
|
|
22
|
+
};
|
|
23
|
+
export const authorizeObservation = queryGeneric({
|
|
24
|
+
args: {
|
|
25
|
+
componentScope: v.string(),
|
|
26
|
+
parentOperationId: v.string(),
|
|
27
|
+
operationId: v.string(),
|
|
28
|
+
},
|
|
29
|
+
handler: async (ctx, args) => {
|
|
30
|
+
readerControls.authorizations++;
|
|
31
|
+
const who = await ctx.auth.getUserIdentity();
|
|
32
|
+
if (
|
|
33
|
+
!who ||
|
|
34
|
+
who.tokenIdentifier !== observerIdentity.tokenIdentifier ||
|
|
35
|
+
readerControls.deny ||
|
|
36
|
+
Object.entries(args).some(
|
|
37
|
+
([key, value]) => readerControls.args[key] !== value,
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
return null;
|
|
41
|
+
const snapshot = structuredClone(readerControls.grant);
|
|
42
|
+
return readerControls.reply
|
|
43
|
+
? readerControls.reply(snapshot, readerControls.authorizations)
|
|
44
|
+
: snapshot;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
export async function prepareObservationReader(
|
|
48
|
+
h: Parameters<typeof historySnapshot>[0],
|
|
49
|
+
) {
|
|
50
|
+
const full = await historySnapshot(h);
|
|
51
|
+
const operationId = full.configure.operation.operationId;
|
|
52
|
+
const parentOperationId = full.parent.operation.operationId;
|
|
53
|
+
const binding = await captureMaintainedJournalBinding(h.sdk, {
|
|
54
|
+
actualActionCtx: h.ctx,
|
|
55
|
+
operationId,
|
|
56
|
+
});
|
|
57
|
+
const component = readCheckedJournalActionRPC(binding).component;
|
|
58
|
+
const authorizationQuery = makeFunctionReference<
|
|
59
|
+
"query",
|
|
60
|
+
{ componentScope: string; parentOperationId: string; operationId: string },
|
|
61
|
+
unknown
|
|
62
|
+
>("initialOwner:authorize");
|
|
63
|
+
const entry = Object.freeze({
|
|
64
|
+
component,
|
|
65
|
+
componentScope: "trusted-installation-a",
|
|
66
|
+
authorizationQuery,
|
|
67
|
+
});
|
|
68
|
+
admissionControls.history.splice(0, admissionControls.history.length, entry);
|
|
69
|
+
h.core.owner.retire();
|
|
70
|
+
vi.setSystemTime(
|
|
71
|
+
(JSON.parse(full.configure.operation.recordJSON).configureRecord
|
|
72
|
+
.configuration.preparation.expected.periodEnd +
|
|
73
|
+
1) *
|
|
74
|
+
1000,
|
|
75
|
+
);
|
|
76
|
+
readerControls.args = {
|
|
77
|
+
componentScope: entry.componentScope,
|
|
78
|
+
parentOperationId,
|
|
79
|
+
operationId,
|
|
80
|
+
};
|
|
81
|
+
readerControls.grant = {
|
|
82
|
+
...readerControls.args,
|
|
83
|
+
tokenIdentifier: observerIdentity.tokenIdentifier,
|
|
84
|
+
providerScope: {
|
|
85
|
+
mode: "platform",
|
|
86
|
+
stripeAccount: null,
|
|
87
|
+
livemode: false,
|
|
88
|
+
customerAccountId: "acct_buyer",
|
|
89
|
+
subscriptionId: "sub_original",
|
|
90
|
+
scheduleId: "sub_sched_owned",
|
|
91
|
+
},
|
|
92
|
+
authorizationId: "fresh-observation",
|
|
93
|
+
authorizationRevision: 3,
|
|
94
|
+
expiresAtMs: Date.now() + 60000,
|
|
95
|
+
};
|
|
96
|
+
readerControls.authorizations = 0;
|
|
97
|
+
readerControls.deny = false;
|
|
98
|
+
readerControls.reply = null;
|
|
99
|
+
return { full, operationId, parentOperationId, entry };
|
|
100
|
+
}
|