Compare commits

...
Author SHA1 Message Date
Cursor AgentandJulius Marminge 3c531b0870 Use schema and options in Effect paths
Co-authored-by: Julius Marminge <[email protected]>
2026-07-11 16:05:12 +00:00
3 changed files with 32 additions and 29 deletions

No files matched your search

+8 -10
View File
@@ -279,28 +279,26 @@ export const staticAndDevRouteLayer = HttpRouter.add(
} }
} }
const fileInfo = yield* fileSystem.stat(filePath).pipe(Effect.orElseSucceed(() => null)); const fileInfo = yield* Effect.option(fileSystem.stat(filePath));
if (!fileInfo || fileInfo.type !== "File") { if (Option.isNone(fileInfo) || fileInfo.value.type !== "File") {
const indexPath = path.resolve(staticRoot, "index.html"); const indexPath = path.resolve(staticRoot, "index.html");
const indexData = yield* fileSystem const indexData = yield* Effect.option(fileSystem.readFile(indexPath));
.readFile(indexPath) if (Option.isNone(indexData)) {
.pipe(Effect.orElseSucceed(() => null));
if (!indexData) {
return HttpServerResponse.text("Not Found", { status: 404 }); return HttpServerResponse.text("Not Found", { status: 404 });
} }
return HttpServerResponse.uint8Array(indexData, { return HttpServerResponse.uint8Array(indexData.value, {
status: 200, status: 200,
contentType: "text/html; charset=utf-8", contentType: "text/html; charset=utf-8",
}); });
} }
const contentType = Mime.getType(filePath) ?? "application/octet-stream"; const contentType = Mime.getType(filePath) ?? "application/octet-stream";
const data = yield* fileSystem.readFile(filePath).pipe(Effect.orElseSucceed(() => null)); const data = yield* Effect.option(fileSystem.readFile(filePath));
if (!data) { if (Option.isNone(data)) {
return HttpServerResponse.text("Internal Server Error", { status: 500 }); return HttpServerResponse.text("Internal Server Error", { status: 500 });
} }
return HttpServerResponse.uint8Array(data, { return HttpServerResponse.uint8Array(data.value, {
status: 200, status: 200,
contentType, contentType,
}); });
@@ -3,7 +3,7 @@ import {
type GitActionProgressEvent, type GitActionProgressEvent,
type GitRunStackedActionResult, type GitRunStackedActionResult,
} from "@t3tools/contracts"; } from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest"; import { assert, describe, expect, it } from "@effect/vitest";
import * as Cause from "effect/Cause"; import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect"; import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer"; import * as Layer from "effect/Layer";
@@ -64,7 +64,7 @@ function progress<T extends GitActionProgressEvent>(event: T): T {
} }
describe("vcsActionState", () => { describe("vcsActionState", () => {
it("preserves malformed target key diagnostics and the native cause without copying the key", () => { it("preserves malformed target key diagnostics without copying the key", () => {
const key = "not-json-with-credential=do-not-log"; const key = "not-json-with-credential=do-not-log";
let error: unknown; let error: unknown;
@@ -74,16 +74,17 @@ describe("vcsActionState", () => {
error = cause; error = cause;
} }
expect(error).toBeInstanceOf(VcsActionTargetKeyParseError); assert.instanceOf(error, VcsActionTargetKeyParseError);
expect(error).toMatchObject({ keyLength: key.length, cause: expect.any(SyntaxError) }); assert.equal(error.keyLength, key.length);
expect(error).not.toHaveProperty("key"); assert.isDefined(error.cause);
expect((error as Error).message).not.toContain(key); assert.isFalse("key" in error);
assert.notInclude(error.message, key);
}); });
it("rejects invalid target key shapes", () => { it("rejects invalid target key shapes", () => {
const key = JSON.stringify([environmentId]); const key = JSON.stringify([environmentId]);
expect(() => parseVcsActionTargetKey(key)).toThrowError(VcsActionTargetKeyParseError); assert.throws(() => parseVcsActionTargetKey(key), VcsActionTargetKeyParseError);
}); });
it("projects phase and hook progress without owning the async operation", () => { it("projects phase and hook progress without owning the async operation", () => {
@@ -245,12 +246,15 @@ describe("vcsActionState", () => {
}); });
it("keys presentation state only when the environment and repository are known", () => { it("keys presentation state only when the environment and repository are known", () => {
expect( const key = getVcsActionTargetKey({
getVcsActionTargetKey({ environmentId,
environmentId, cwd,
cwd, });
}), if (key === null) {
).toBe(JSON.stringify([environmentId, cwd])); assert.fail("Expected a resolved target key.");
}
assert.equal(key, JSON.stringify([environmentId, cwd]));
assert.deepEqual(parseVcsActionTargetKey(key), { environmentId, cwd });
expect(getVcsActionTargetKey({ environmentId: null, cwd })).toBeNull(); expect(getVcsActionTargetKey({ environmentId: null, cwd })).toBeNull();
expect( expect(
getVcsActionTargetKey({ getVcsActionTargetKey({
@@ -157,9 +157,10 @@ export const EMPTY_VCS_ACTION_STATE = Object.freeze<VcsActionState>({
const nowMs = (): number => DateTime.toEpochMillis(DateTime.nowUnsafe()); const nowMs = (): number => DateTime.toEpochMillis(DateTime.nowUnsafe());
let nextLocalActionId = 0; let nextLocalActionId = 0;
const decodeVcsActionTargetKey = Schema.decodeUnknownSync( const VcsActionTargetKey = Schema.Tuple([EnvironmentId, Schema.String]);
Schema.Tuple([EnvironmentId, Schema.String]), const VcsActionTargetKeyJson = Schema.fromJsonString(VcsActionTargetKey);
); const decodeVcsActionTargetKey = Schema.decodeUnknownSync(VcsActionTargetKeyJson);
const encodeVcsActionTargetKey = Schema.encodeSync(VcsActionTargetKeyJson);
export const vcsActionStateAtom = Atom.family((key: string) => { export const vcsActionStateAtom = Atom.family((key: string) => {
return Atom.make(EMPTY_VCS_ACTION_STATE).pipe( return Atom.make(EMPTY_VCS_ACTION_STATE).pipe(
@@ -177,12 +178,12 @@ export function getVcsActionTargetKey(target: VcsActionTarget): string | null {
if (target.environmentId === null || target.cwd === null) { if (target.environmentId === null || target.cwd === null) {
return null; return null;
} }
return JSON.stringify([target.environmentId, target.cwd]); return encodeVcsActionTargetKey([target.environmentId, target.cwd]);
} }
export function parseVcsActionTargetKey(key: string): ResolvedVcsActionTarget { export function parseVcsActionTargetKey(key: string): ResolvedVcsActionTarget {
try { try {
const [environmentId, cwd] = decodeVcsActionTargetKey(JSON.parse(key)); const [environmentId, cwd] = decodeVcsActionTargetKey(key);
return { environmentId, cwd }; return { environmentId, cwd };
} catch (cause) { } catch (cause) {
throw new VcsActionTargetKeyParseError({ keyLength: key.length, cause }); throw new VcsActionTargetKeyParseError({ keyLength: key.length, cause });
@@ -232,7 +233,7 @@ export function createVcsActionTransportId(
target: ResolvedVcsActionTarget, target: ResolvedVcsActionTarget,
actionId: string, actionId: string,
): string { ): string {
const targetKey = JSON.stringify([target.environmentId, target.cwd]); const targetKey = encodeVcsActionTargetKey([target.environmentId, target.cwd]);
return `${targetKey.length}:${targetKey}${actionId}`; return `${targetKey.length}:${targetKey}${actionId}`;
} }