Skip to content

Commit 32c06e4

Browse files
t3dotggclaude
andcommitted
refactor(desktop): certificate approval file helpers get named spans
The four approval persistence helpers are now named Effect.fn operations, per the Effect service conventions. Drops a DesktopBrowserHost test that only checked callback wiring. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent b416159 commit 32c06e4

2 files changed

Lines changed: 40 additions & 66 deletions

File tree

‎apps/desktop/src/preview/BrowserSession.ts‎

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,17 @@ export const make = Effect.gen(function* BrowserSessionMake() {
333333

334334
const certificateTrust = new WeakMap<Session, CertificateTrust>();
335335
const writeCertificateApprovals = yield* Semaphore.make(1);
336-
const loadCertificateTrust = (browserSession: Session) => {
336+
const loadCertificateTrust = Effect.fn("BrowserSession.loadCertificateTrust")(function* (
337+
browserSession: Session,
338+
) {
337339
const file =
338340
browserSession.storagePath == null
339341
? null
340342
: path.join(browserSession.storagePath, CERTIFICATE_APPROVALS_FILE);
341-
return (
343+
const approvals: ReadonlyArray<CertificateApproval> =
342344
file === null
343-
? Effect.succeed([])
344-
: fileSystem.readFileString(file).pipe(
345+
? []
346+
: yield* fileSystem.readFileString(file).pipe(
345347
Effect.flatMap(decodeCertificateApprovals),
346348
Effect.catchTags({
347349
PlatformError: (error) =>
@@ -352,60 +354,54 @@ export const make = Effect.gen(function* BrowserSessionMake() {
352354
Effect.as([]),
353355
),
354356
),
355-
)
356-
).pipe(
357-
Effect.map((approvals): CertificateTrust => ({
358-
file,
359-
approvals: new Map(
360-
approvals.map((approval) => [certificateApprovalKey(approval), approval]),
361-
),
362-
revocations: 0,
363-
})),
364-
);
365-
};
366-
const writeApprovalsFile = (file: string, approvals: ReadonlyMap<string, CertificateApproval>) =>
367-
approvals.size === 0
368-
? fileSystem.remove(file, { force: true })
369-
: Effect.gen(function* () {
370-
yield* fileSystem.makeDirectory(path.dirname(file), { recursive: true });
371-
yield* fileSystem.writeFileString(
372-
file,
373-
yield* encodeCertificateApprovals([...approvals.values()]),
374357
);
375-
});
358+
const trust: CertificateTrust = {
359+
file,
360+
approvals: new Map(approvals.map((approval) => [certificateApprovalKey(approval), approval])),
361+
revocations: 0,
362+
};
363+
return trust;
364+
});
365+
const writeApprovalsFile = Effect.fn("BrowserSession.writeApprovalsFile")(function* (
366+
file: string,
367+
approvals: ReadonlyMap<string, CertificateApproval>,
368+
) {
369+
if (approvals.size === 0) return yield* fileSystem.remove(file, { force: true });
370+
yield* fileSystem.makeDirectory(path.dirname(file), { recursive: true });
371+
yield* fileSystem.writeFileString(
372+
file,
373+
yield* encodeCertificateApprovals([...approvals.values()]),
374+
);
375+
});
376376
/**
377377
* Saved before it is trusted, so a failed write cannot leave an approval that
378378
* silently ends at restart. Writes share one lock, so a slower write cannot
379379
* restore stale data.
380380
*/
381-
const rememberCertificate = (
381+
const rememberCertificate = Effect.fn("BrowserSession.rememberCertificate")(function* (
382382
trust: CertificateTrust,
383383
key: string,
384384
approval: CertificateApproval,
385385
revocations: number,
386-
) =>
387-
writeCertificateApprovals.withPermits(1)(
388-
Effect.gen(function* () {
389-
if (trust.revocations !== revocations) return false;
390-
if (trust.file !== null) {
391-
yield* writeApprovalsFile(trust.file, new Map(trust.approvals).set(key, approval));
392-
// A revocation during the write queues its own rewrite behind this one.
393-
if (trust.revocations !== revocations) return false;
394-
}
395-
trust.approvals.set(key, approval);
396-
return true;
397-
}),
398-
);
386+
) {
387+
if (trust.revocations !== revocations) return false;
388+
if (trust.file !== null) {
389+
yield* writeApprovalsFile(trust.file, new Map(trust.approvals).set(key, approval));
390+
// A revocation during the write queues its own rewrite behind this one.
391+
if (trust.revocations !== revocations) return false;
392+
}
393+
trust.approvals.set(key, approval);
394+
return true;
395+
}, writeCertificateApprovals.withPermits(1));
399396
/**
400397
* Rewrites the file from memory. Clearing cookies runs this even when memory
401398
* is already empty, so retrying after a failed removal still deletes the file.
402399
*/
403-
const saveCertificateApprovals = (trust: CertificateTrust) =>
404-
writeCertificateApprovals.withPermits(1)(
405-
Effect.suspend(() =>
406-
trust.file === null ? Effect.void : writeApprovalsFile(trust.file, trust.approvals),
407-
),
408-
);
400+
const saveCertificateApprovals = Effect.fn("BrowserSession.saveCertificateApprovals")(function* (
401+
trust: CertificateTrust,
402+
) {
403+
if (trust.file !== null) yield* writeApprovalsFile(trust.file, trust.approvals);
404+
}, writeCertificateApprovals.withPermits(1));
409405

410406
// One prompt at a time; tabs that hit the same certificate wait on it.
411407
let certificatePrompt: {

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,28 +286,6 @@ describe("DesktopBrowserHost", () => {
286286
}).pipe(Effect.scoped),
287287
);
288288

289-
it.effect("reports cookies the server cleared before it replies", () =>
290-
Effect.gen(function* () {
291-
const host = yield* DesktopBrowserHost.make;
292-
const debuggee = makeDebuggee();
293-
const cleared: Array<number> = [];
294-
host.attach(key, {
295-
...debuggee.tab,
296-
cookiesCleared: async () => {
297-
cleared.push(debuggee.sent.length);
298-
},
299-
});
300-
const replies = yield* watchReplies(host);
301-
302-
yield* command(host, 1, "Network.clearBrowserCache");
303-
yield* command(host, 2, "Network.clearBrowserCookies");
304-
yield* settle;
305-
debuggee.release();
306-
expect([yield* Queue.take(replies), yield* Queue.take(replies)]).toEqual([1, 2]);
307-
expect(cleared).toEqual([2]);
308-
}).pipe(Effect.scoped),
309-
);
310-
311289
it.effect("sends a page one screenshot at a time", () =>
312290
Effect.gen(function* () {
313291
const host = yield* DesktopBrowserHost.make;

0 commit comments

Comments
 (0)