@fantastic.dev/better-stripe 0.88.1 → 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.
Files changed (28) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts +27 -0
  4. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAttach/internal/originalOwner.js +236 -0
  6. package/dist/client/billing/renewalAttach/internal/originalOwner.js.map +1 -0
  7. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts +5 -0
  8. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/sourceBindings.js +9 -0
  10. package/dist/client/billing/renewalAttach/internal/sourceBindings.js.map +1 -0
  11. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts +6 -0
  12. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js +35 -0
  14. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js.map +1 -0
  15. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  16. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
  17. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  18. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  19. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  20. package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
  21. package/docs/renewal-context-reads.md +15 -0
  22. package/package.json +1 -1
  23. package/src/client/billing/renewalAttach/internal/originalOwner.test.ts +258 -0
  24. package/src/client/billing/renewalAttach/internal/originalOwner.ts +280 -0
  25. package/src/client/billing/renewalAttach/internal/sourceBindings.test.ts +13 -0
  26. package/src/client/billing/renewalAttach/internal/sourceBindings.ts +8 -0
  27. package/src/client/billing/renewalAttach/internal/stockRawDelegate.test.ts +156 -0
  28. package/src/client/billing/renewalAttach/internal/stockRawDelegate.ts +43 -0
@@ -2351,14 +2351,6 @@ export declare function setupAllowance(): Promise<{
2351
2351
  preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
2352
2352
  requestKey: string;
2353
2353
  result?: {
2354
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2355
- retryable: false;
2356
- status: "conflict";
2357
- } | {
2358
- reason: "policy_invalid";
2359
- retryable: false;
2360
- status: "rejected";
2361
- } | {
2362
2354
  operation: {
2363
2355
  accountId: string;
2364
2356
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2369,6 +2361,14 @@ export declare function setupAllowance(): Promise<{
2369
2361
  } | {
2370
2362
  enabled: boolean;
2371
2363
  status: "unchanged";
2364
+ } | {
2365
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2366
+ retryable: false;
2367
+ status: "conflict";
2368
+ } | {
2369
+ reason: "policy_invalid";
2370
+ retryable: false;
2371
+ status: "rejected";
2372
2372
  } | {
2373
2373
  reason: "provider_pending";
2374
2374
  retryable: true;
@@ -2379,14 +2379,6 @@ export declare function setupAllowance(): Promise<{
2379
2379
  payload: import("convex/values").VString<string, "required">;
2380
2380
  preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
2381
2381
  result: import("convex/values").VUnion<{
2382
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2383
- retryable: false;
2384
- status: "conflict";
2385
- } | {
2386
- reason: "policy_invalid";
2387
- retryable: false;
2388
- status: "rejected";
2389
- } | {
2390
2382
  operation: {
2391
2383
  accountId: string;
2392
2384
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2397,6 +2389,14 @@ export declare function setupAllowance(): Promise<{
2397
2389
  } | {
2398
2390
  enabled: boolean;
2399
2391
  status: "unchanged";
2392
+ } | {
2393
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2394
+ retryable: false;
2395
+ status: "conflict";
2396
+ } | {
2397
+ reason: "policy_invalid";
2398
+ retryable: false;
2399
+ status: "rejected";
2400
2400
  } | {
2401
2401
  reason: "provider_pending";
2402
2402
  retryable: true;
@@ -2351,6 +2351,14 @@ export declare function setupCredits(): Promise<{
2351
2351
  preparationId: import("convex/values").GenericId<"balanceFundingPreparations">;
2352
2352
  requestKey: string;
2353
2353
  result?: {
2354
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2355
+ retryable: false;
2356
+ status: "conflict";
2357
+ } | {
2358
+ reason: "policy_invalid";
2359
+ retryable: false;
2360
+ status: "rejected";
2361
+ } | {
2354
2362
  operation: {
2355
2363
  accountId: string;
2356
2364
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2361,14 +2369,6 @@ export declare function setupCredits(): Promise<{
2361
2369
  } | {
2362
2370
  enabled: boolean;
2363
2371
  status: "unchanged";
2364
- } | {
2365
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2366
- retryable: false;
2367
- status: "conflict";
2368
- } | {
2369
- reason: "policy_invalid";
2370
- retryable: false;
2371
- status: "rejected";
2372
2372
  } | {
2373
2373
  reason: "provider_pending";
2374
2374
  retryable: true;
@@ -2379,6 +2379,14 @@ export declare function setupCredits(): Promise<{
2379
2379
  payload: import("convex/values").VString<string, "required">;
2380
2380
  preparationId: import("convex/values").VId<import("convex/values").GenericId<"balanceFundingPreparations">, "required">;
2381
2381
  result: import("convex/values").VUnion<{
2382
+ reason: "identity_changed" | "revision_changed" | "scope_busy";
2383
+ retryable: false;
2384
+ status: "conflict";
2385
+ } | {
2386
+ reason: "policy_invalid";
2387
+ retryable: false;
2388
+ status: "rejected";
2389
+ } | {
2382
2390
  operation: {
2383
2391
  accountId: string;
2384
2392
  operationId: import("convex/values").GenericId<"balanceFundingOperations">;
@@ -2389,14 +2397,6 @@ export declare function setupCredits(): Promise<{
2389
2397
  } | {
2390
2398
  enabled: boolean;
2391
2399
  status: "unchanged";
2392
- } | {
2393
- reason: "identity_changed" | "revision_changed" | "scope_busy";
2394
- retryable: false;
2395
- status: "conflict";
2396
- } | {
2397
- reason: "policy_invalid";
2398
- retryable: false;
2399
- status: "rejected";
2400
2400
  } | {
2401
2401
  reason: "provider_pending";
2402
2402
  retryable: true;
@@ -41,6 +41,21 @@ Older snapshots that already omitted a field cannot recover it or establish
41
41
  retroactive full-record provenance. Reconstructing a changed full record does
42
42
  not migrate its old binding or authorize a retry.
43
43
 
44
+ ## Internal ownership and transport groundwork
45
+
46
+ The internal `renewalAttach/internal` modules keep one original owner identity
47
+ and adapt the stock Stripe client's inherited `rawRequest` method without
48
+ changing its receiver, arguments, result or thrown value. The owner checks the
49
+ same original wall and monotonic time bounds and closes permanently on expiry,
50
+ regression or retirement. These modules do not read a clock at import time.
51
+
52
+ The compiled entry registry remains empty, and the original Source and stock
53
+ client bindings remain unbound. There is no package export or operational
54
+ attachment actor in this increment. It does not authorize reads, dispatch a
55
+ request, enable recovery or establish provider custody or native qualification.
56
+ The internal Node-only mechanics are not a caller-supplied clock, client or
57
+ qualification API. The public preparation and receipt constructors are unchanged.
58
+
44
59
  ## Fixed acquisition profile
45
60
 
46
61
  The initial profile supports platform requests using the configured
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fantastic.dev/better-stripe",
3
- "version": "0.88.1",
3
+ "version": "0.89.0",
4
4
  "description": "Convex component for Stripe Accounts v2, subscriptions, metered billing and Connect marketplaces, with React workflows.",
5
5
  "keywords": [
6
6
  "stripe",
@@ -0,0 +1,258 @@
1
+ import { afterEach, expect, it, vi } from "vitest";
2
+
3
+ afterEach(() => {
4
+ vi.doUnmock("./sourceBindings.js");
5
+ vi.resetModules();
6
+ });
7
+
8
+ function fixture() {
9
+ const basis = {
10
+ startWallMs: 1_000_000,
11
+ startNs: 1_000_000_000n,
12
+ workEndWallMs: 1_180_000,
13
+ workEndNs: 1_180_000_000n,
14
+ endWallMs: 1_210_000,
15
+ endNs: 1_210_000_000n,
16
+ };
17
+ const policy = { firstDispatchedAt: 1000, periodStart: 900, periodEnd: 2000 };
18
+ const sample = { wallMs: basis.startWallMs, ns: basis.startNs };
19
+ let samples = 0;
20
+ let loads = 0;
21
+ let detached = 0;
22
+ let listener = () => {};
23
+ const source = {
24
+ clock: {
25
+ basis,
26
+ sample() {
27
+ samples++;
28
+ return { ...sample };
29
+ },
30
+ subscribeRetirement(fn: () => void) {
31
+ listener = fn;
32
+ return () => {
33
+ detached++;
34
+ };
35
+ },
36
+ },
37
+ originalPolicy: policy,
38
+ originAssociation: Object.freeze({}),
39
+ };
40
+ return {
41
+ basis,
42
+ policy,
43
+ sample,
44
+ source,
45
+ load() {
46
+ loads++;
47
+ return source;
48
+ },
49
+ retire() {
50
+ listener();
51
+ },
52
+ counts() {
53
+ return { samples, loads, detached };
54
+ },
55
+ };
56
+ }
57
+
58
+ async function owner(f = fixture()) {
59
+ vi.resetModules();
60
+ vi.doMock("./sourceBindings.js", () => ({
61
+ loadOwnedOriginalSource: () => f.load(),
62
+ }));
63
+ return { f, OWNER: (await import("./originalOwner.js")).OWNER };
64
+ }
65
+
66
+ it("valid original owner is live with exactly one accepted sample", async () => {
67
+ const { OWNER, f } = await owner();
68
+ let actual: unknown;
69
+ try {
70
+ actual = OWNER.check();
71
+ } catch {
72
+ actual = "stopped";
73
+ }
74
+ expect(actual).toEqual({
75
+ wallMs: 1_000_000,
76
+ ns: 1_000_000_000n,
77
+ nowSeconds: 1000,
78
+ retired: false,
79
+ });
80
+ expect(f.counts()).toEqual({ loads: 1, samples: 1, detached: 0 });
81
+ expect(OWNER.sample).toBe(OWNER.check);
82
+ expect(Object.isFrozen(OWNER)).toBe(true);
83
+ });
84
+
85
+ it("loads lazily once, copies the original basis, and does not sample on basis access", async () => {
86
+ const { OWNER, f } = await owner();
87
+ expect(f.counts().loads).toBe(0);
88
+ const captured = OWNER.basis;
89
+ f.basis.workEndWallMs = 9_000_000;
90
+ expect(OWNER.basis).toBe(captured);
91
+ expect(captured.workEndWallMs).toBe(1_180_000);
92
+ expect(Object.isFrozen(captured)).toBe(true);
93
+ expect(f.counts()).toEqual({ loads: 1, samples: 0, detached: 0 });
94
+ });
95
+
96
+ it.each(["wall", "ns"] as const)(
97
+ "original %s work end is exclusive and irreversibly closes",
98
+ async (clock) => {
99
+ const { OWNER, f } = await owner();
100
+ OWNER.check();
101
+ if (clock === "wall") f.sample.wallMs = f.basis.workEndWallMs;
102
+ else f.sample.ns = f.basis.workEndNs;
103
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
104
+ f.sample.wallMs = f.basis.startWallMs;
105
+ f.sample.ns = f.basis.startNs;
106
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
107
+ expect(f.counts().samples).toBe(2);
108
+ },
109
+ );
110
+
111
+ it.each(["wall", "ns"] as const)(
112
+ "%s regression cannot renew the owner",
113
+ async (clock) => {
114
+ const { OWNER, f } = await owner();
115
+ f.sample.wallMs++;
116
+ f.sample.ns++;
117
+ OWNER.check();
118
+ if (clock === "wall") f.sample.wallMs--;
119
+ else f.sample.ns--;
120
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
121
+ },
122
+ );
123
+
124
+ it("derives floor seconds from the same sample and enforces future first dispatch", async () => {
125
+ const f = fixture();
126
+ f.policy.firstDispatchedAt = 1001;
127
+ const { OWNER } = await owner(f);
128
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
129
+ expect(f.counts().samples).toBe(1);
130
+ });
131
+
132
+ it("age 86400 is refused independently under a declared long synthetic basis", async () => {
133
+ const f = fixture();
134
+ f.basis.workEndWallMs = 100_000_000;
135
+ f.basis.endWallMs = 100_001_000;
136
+ f.basis.workEndNs = 100_000_000_000n;
137
+ f.basis.endNs = 100_001_000_000n;
138
+ f.policy.periodEnd = 200_000;
139
+ const { OWNER } = await owner(f);
140
+ f.sample.wallMs = (1000 + 86399) * 1000;
141
+ expect(OWNER.check().nowSeconds).toBe(87399);
142
+ f.sample.wallMs += 1000;
143
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
144
+ });
145
+
146
+ it("paid period start is inclusive and end exclusive", async () => {
147
+ const f = fixture();
148
+ f.policy.periodStart = 1000;
149
+ f.policy.periodEnd = 1001;
150
+ const { OWNER } = await owner(f);
151
+ expect(OWNER.check().nowSeconds).toBe(1000);
152
+ f.sample.wallMs = 1_001_000;
153
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
154
+ });
155
+
156
+ it("rejects a Source Proxy before any reflective traps or samples", async () => {
157
+ const f = fixture();
158
+ let traps = 0;
159
+ const value = new Proxy(f.source, {
160
+ getPrototypeOf() {
161
+ traps++;
162
+ return Object.prototype;
163
+ },
164
+ ownKeys() {
165
+ traps++;
166
+ return [];
167
+ },
168
+ get() {
169
+ traps++;
170
+ return null;
171
+ },
172
+ });
173
+ f.load = () => value;
174
+ const { OWNER } = await owner(f);
175
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
176
+ expect(traps).toBe(0);
177
+ expect(f.counts().samples).toBe(0);
178
+ });
179
+
180
+ it("rejects an own accessor without invoking it", async () => {
181
+ const f = fixture();
182
+ let gets = 0;
183
+ Object.defineProperty(f.source, "clock", {
184
+ get() {
185
+ gets++;
186
+ return null;
187
+ },
188
+ enumerable: true,
189
+ });
190
+ const { OWNER } = await owner(f);
191
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
192
+ expect(gets).toBe(0);
193
+ });
194
+
195
+ it("retirement notifies active waiters once and never samples again", async () => {
196
+ const { OWNER, f } = await owner();
197
+ let active = 0;
198
+ let removed = 0;
199
+ OWNER.check();
200
+ OWNER.subscribe(() => {
201
+ active++;
202
+ });
203
+ const unsubscribe = OWNER.subscribe(() => {
204
+ removed++;
205
+ });
206
+ unsubscribe();
207
+ unsubscribe();
208
+ f.retire();
209
+ OWNER.retire();
210
+ expect(active).toBe(1);
211
+ expect(removed).toBe(0);
212
+ expect(f.counts().detached).toBe(1);
213
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
214
+ expect(f.counts().samples).toBe(1);
215
+ });
216
+
217
+ it("cannot revive after a Source loader catches a reentrant check", async () => {
218
+ const f = fixture();
219
+ let nested = () => {};
220
+ const ordinaryLoad = f.load;
221
+ f.load = () => {
222
+ try {
223
+ nested();
224
+ } catch {
225
+ /* The outer load must stay closed. */
226
+ }
227
+ return ordinaryLoad();
228
+ };
229
+ const { OWNER } = await owner(f);
230
+ nested = () => {
231
+ OWNER.check();
232
+ };
233
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
234
+ expect(f.counts().samples).toBe(0);
235
+ });
236
+
237
+ it("cannot revive during listener registration and detaches its returned listener", async () => {
238
+ const f = fixture();
239
+ let nested = () => {};
240
+ let detached = 0;
241
+ f.source.clock.subscribeRetirement = () => {
242
+ try {
243
+ nested();
244
+ } catch {
245
+ /* The registration cannot publish live. */
246
+ }
247
+ return () => {
248
+ detached++;
249
+ };
250
+ };
251
+ const { OWNER } = await owner(f);
252
+ nested = () => {
253
+ OWNER.check();
254
+ };
255
+ expect(() => OWNER.check()).toThrow("ORIGINAL_OWNER_STOP");
256
+ expect(detached).toBe(1);
257
+ expect(f.counts().samples).toBe(0);
258
+ });
@@ -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
+ });