| author | |
| committer | |
| log | 5c1ac922574340be4560eacf928237929497d11d |
| tree | abd561916532dc5a36af7fd548811c2e98868d66 |
| parent | 8bfdacf206608c0e14b1c5471dfa1f482a452acc |
| signature | Signed by PGP key B5690EEEBB952194 |
Co-authored-by: codex <codex@users.noreply.github.com>8 files changed, 297 insertions(+), 272 deletions(-)
apps/server/src/environment/Layers/ServerEnvironmentLabel.test.ts+5-5| ... | ... | @@ -5,15 +5,15 @@ import * as Layer from "effect/Layer"; |
| 5 | 5 | import { HostProcessHostname, HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 6 | 6 | import { vi } from "vite-plus/test"; |
| 7 | 7 | |
| 8 | import { ProcessRunner, ProcessSpawnError, type ProcessRunnerShape } from "../../processRunner.ts"; | |
| 8 | import * as ProcessRunner from "../../processRunner.ts"; | |
| 9 | 9 | import { resolveServerEnvironmentLabel } from "./ServerEnvironmentLabel.ts"; |
| 10 | 10 | import { ChildProcessSpawner } from "effect/unstable/process"; |
| 11 | 11 | |
| 12 | const runMock = vi.fn<ProcessRunnerShape["run"]>(); | |
| 12 | const runMock = vi.fn<ProcessRunner.ProcessRunner["Service"]["run"]>(); | |
| 13 | 13 | |
| 14 | 14 | const ProcessRunnerTest = Layer.succeed( |
| 15 | ProcessRunner, | |
| 16 | ProcessRunner.of({ | |
| 15 | ProcessRunner.ProcessRunner, | |
| 16 | ProcessRunner.ProcessRunner.of({ | |
| 17 | 17 | run: (input) => runMock(input), |
| 18 | 18 | }), |
| 19 | 19 | ); |
| ... | ... | @@ -136,7 +136,7 @@ describe("resolveServerEnvironmentLabel", () => { |
| 136 | 136 | Effect.gen(function* () { |
| 137 | 137 | runMock.mockReturnValueOnce( |
| 138 | 138 | Effect.fail( |
| 139 | new ProcessSpawnError({ | |
| 139 | new ProcessRunner.ProcessSpawnError({ | |
| 140 | 140 | command: "scutil", |
| 141 | 141 | args: ["--get", "ComputerName"], |
| 142 | 142 | cause: new Error("spawn scutil ENOENT"), |
apps/server/src/mcp/PreviewAutomationBroker.test.ts+8-8| ... | ... | @@ -37,7 +37,7 @@ const makeOwner = (overrides: Partial<PreviewAutomationOwner> = {}): PreviewAuto |
| 37 | 37 | it.effect("atomically registers a connected owner and correlates its response", () => |
| 38 | 38 | Effect.scoped( |
| 39 | 39 | Effect.gen(function* () { |
| 40 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 40 | const broker = yield* PreviewAutomationBroker.make; | |
| 41 | 41 | const requests = yield* broker.connect(makeOwner()); |
| 42 | 42 | yield* Stream.runForEach(requests, (request) => |
| 43 | 43 | broker.respond({ |
| ... | ... | @@ -61,7 +61,7 @@ it.effect("atomically registers a connected owner and correlates its response", |
| 61 | 61 | |
| 62 | 62 | it.effect("rejects calls when no focused owner exists", () => |
| 63 | 63 | Effect.gen(function* () { |
| 64 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 64 | const broker = yield* PreviewAutomationBroker.make; | |
| 65 | 65 | const error = yield* broker |
| 66 | 66 | .invoke<void>({ scope, operation: "status", input: {} }) |
| 67 | 67 | .pipe(Effect.flip); |
| ... | ... | @@ -72,7 +72,7 @@ it.effect("rejects calls when no focused owner exists", () => |
| 72 | 72 | it.effect("routes interactive commands to a hidden durable browser host", () => |
| 73 | 73 | Effect.scoped( |
| 74 | 74 | Effect.gen(function* () { |
| 75 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 75 | const broker = yield* PreviewAutomationBroker.make; | |
| 76 | 76 | const requests = yield* broker.connect( |
| 77 | 77 | makeOwner({ clientId: "client-hidden", tabId: "tab-hidden" }), |
| 78 | 78 | ); |
| ... | ... | @@ -89,7 +89,7 @@ it.effect("routes interactive commands to a hidden durable browser host", () => |
| 89 | 89 | it.effect("lets the browser host resolve an active tab that has not been reported yet", () => |
| 90 | 90 | Effect.scoped( |
| 91 | 91 | Effect.gen(function* () { |
| 92 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 92 | const broker = yield* PreviewAutomationBroker.make; | |
| 93 | 93 | const requests = yield* broker.connect(makeOwner({ tabId: null })); |
| 94 | 94 | let routedTabId: string | undefined; |
| 95 | 95 | yield* Stream.runForEach(requests, (request) => { |
| ... | ... | @@ -108,7 +108,7 @@ it.effect("lets the browser host resolve an active tab that has not been reporte |
| 108 | 108 | it.effect("preserves current owner metadata when its request stream reconnects", () => |
| 109 | 109 | Effect.scoped( |
| 110 | 110 | Effect.gen(function* () { |
| 111 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 111 | const broker = yield* PreviewAutomationBroker.make; | |
| 112 | 112 | const firstRequests = yield* broker.connect(makeOwner()); |
| 113 | 113 | yield* Stream.runDrain(firstRequests).pipe(Effect.forkScoped); |
| 114 | 114 | yield* broker.reportOwner(makeOwner({ tabId: "tab-current", visible: true })); |
| ... | ... | @@ -131,7 +131,7 @@ it.effect("preserves current owner metadata when its request stream reconnects", |
| 131 | 131 | it.effect("ignores stale owner cleanup after the client moves to another thread", () => |
| 132 | 132 | Effect.scoped( |
| 133 | 133 | Effect.gen(function* () { |
| 134 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 134 | const broker = yield* PreviewAutomationBroker.make; | |
| 135 | 135 | const requests = yield* broker.connect(makeOwner()); |
| 136 | 136 | yield* Stream.runForEach(requests, (request) => |
| 137 | 137 | broker.respond({ requestId: request.requestId, ok: true }), |
| ... | ... | @@ -152,7 +152,7 @@ it.effect("ignores stale owner cleanup after the client moves to another thread" |
| 152 | 152 | it.effect("fails requests assigned to a browser stream when that stream reconnects", () => |
| 153 | 153 | Effect.scoped( |
| 154 | 154 | Effect.gen(function* () { |
| 155 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 155 | const broker = yield* PreviewAutomationBroker.make; | |
| 156 | 156 | const _requests = yield* broker.connect(makeOwner()); |
| 157 | 157 | const pending = yield* broker |
| 158 | 158 | .invoke<void>({ scope, operation: "status", input: {} }) |
| ... | ... | @@ -170,7 +170,7 @@ it.effect("fails requests assigned to a browser stream when that stream reconnec |
| 170 | 170 | it.effect("falls back to an older connected owner when a newer report is not connected", () => |
| 171 | 171 | Effect.scoped( |
| 172 | 172 | Effect.gen(function* () { |
| 173 | const broker = yield* PreviewAutomationBroker.__testing.make; | |
| 173 | const broker = yield* PreviewAutomationBroker.make; | |
| 174 | 174 | const requests = yield* broker.connect(makeOwner({ clientId: "client-connected" })); |
| 175 | 175 | yield* Stream.runForEach(requests, (request) => |
| 176 | 176 | broker.respond({ requestId: request.requestId, ok: true, result: "connected" }), |
apps/server/src/mcp/PreviewAutomationBroker.ts+22-33| ... | ... | @@ -35,34 +35,28 @@ export interface PreviewAutomationInvokeInput { |
| 35 | 35 | readonly timeoutMs?: number; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | export interface PreviewAutomationBrokerShape { | |
| 39 | readonly connect: ( | |
| 40 | owner: PreviewAutomationOwner, | |
| 41 | ) => Effect.Effect<Stream.Stream<PreviewAutomationRequest>>; | |
| 42 | readonly reportOwner: ( | |
| 43 | owner: PreviewAutomationOwner, | |
| 44 | ) => Effect.Effect<void, PreviewAutomationError>; | |
| 45 | readonly clearOwner: (owner: PreviewAutomationOwnerIdentity) => Effect.Effect<void>; | |
| 46 | readonly respond: ( | |
| 47 | response: PreviewAutomationResponse, | |
| 48 | ) => Effect.Effect<void, PreviewAutomationError>; | |
| 49 | readonly invoke: <A = unknown>( | |
| 50 | request: PreviewAutomationInvokeInput, | |
| 51 | ) => Effect.Effect<A, PreviewAutomationError>; | |
| 52 | } | |
| 53 | ||
| 54 | 38 | export class PreviewAutomationBroker extends Context.Service< |
| 55 | 39 | PreviewAutomationBroker, |
| 56 | PreviewAutomationBrokerShape | |
| 40 | { | |
| 41 | readonly connect: ( | |
| 42 | owner: PreviewAutomationOwner, | |
| 43 | ) => Effect.Effect<Stream.Stream<PreviewAutomationRequest>>; | |
| 44 | readonly reportOwner: ( | |
| 45 | owner: PreviewAutomationOwner, | |
| 46 | ) => Effect.Effect<void, PreviewAutomationError>; | |
| 47 | readonly clearOwner: (owner: PreviewAutomationOwnerIdentity) => Effect.Effect<void>; | |
| 48 | readonly respond: ( | |
| 49 | response: PreviewAutomationResponse, | |
| 50 | ) => Effect.Effect<void, PreviewAutomationError>; | |
| 51 | readonly invoke: <A = unknown>( | |
| 52 | request: PreviewAutomationInvokeInput, | |
| 53 | ) => Effect.Effect<A, PreviewAutomationError>; | |
| 54 | } | |
| 57 | 55 | >()("t3/mcp/PreviewAutomationBroker") {} |
| 58 | 56 | |
| 59 | 57 | interface ClientConnection { |
| 60 | 58 | readonly clientId: string; |
| 61 | readonly queue: Queue.Queue< | |
| 62 | Parameters<PreviewAutomationBrokerShape["respond"]>[0] extends never | |
| 63 | ? never | |
| 64 | : import("@t3tools/contracts").PreviewAutomationRequest | |
| 65 | >; | |
| 59 | readonly queue: Queue.Queue<PreviewAutomationRequest>; | |
| 66 | 60 | } |
| 67 | 61 | |
| 68 | 62 | interface PendingRequest { |
| ... | ... | @@ -123,7 +117,7 @@ const makeResponseError = ( |
| 123 | 117 | } |
| 124 | 118 | }; |
| 125 | 119 | |
| 126 | const make = Effect.gen(function* PreviewAutomationBrokerMake() { | |
| 120 | export const make = Effect.gen(function* PreviewAutomationBrokerMake() { | |
| 127 | 121 | const state = yield* SynchronizedRef.make<BrokerState>({ |
| 128 | 122 | clients: new Map(), |
| 129 | 123 | owners: new Map(), |
| ... | ... | @@ -166,7 +160,7 @@ const make = Effect.gen(function* PreviewAutomationBrokerMake() { |
| 166 | 160 | yield* Queue.shutdown(queue); |
| 167 | 161 | }); |
| 168 | 162 | |
| 169 | const connect: PreviewAutomationBrokerShape["connect"] = Effect.fn( | |
| 163 | const connect: PreviewAutomationBroker["Service"]["connect"] = Effect.fn( | |
| 170 | 164 | "PreviewAutomationBroker.connect", |
| 171 | 165 | )(function* (owner) { |
| 172 | 166 | const clientId = owner.clientId; |
| ... | ... | @@ -189,7 +183,7 @@ const make = Effect.gen(function* PreviewAutomationBrokerMake() { |
| 189 | 183 | return Stream.fromQueue(queue).pipe(Stream.ensuring(disconnect(clientId, queue))); |
| 190 | 184 | }); |
| 191 | 185 | |
| 192 | const reportOwner: PreviewAutomationBrokerShape["reportOwner"] = Effect.fn( | |
| 186 | const reportOwner: PreviewAutomationBroker["Service"]["reportOwner"] = Effect.fn( | |
| 193 | 187 | "PreviewAutomationBroker.reportOwner", |
| 194 | 188 | )(function* (owner) { |
| 195 | 189 | yield* SynchronizedRef.update(state, (current) => { |
| ... | ... | @@ -199,7 +193,7 @@ const make = Effect.gen(function* PreviewAutomationBrokerMake() { |
| 199 | 193 | }); |
| 200 | 194 | }); |
| 201 | 195 | |
| 202 | const clearOwner: PreviewAutomationBrokerShape["clearOwner"] = Effect.fn( | |
| 196 | const clearOwner: PreviewAutomationBroker["Service"]["clearOwner"] = Effect.fn( | |
| 203 | 197 | "PreviewAutomationBroker.clearOwner", |
| 204 | 198 | )(function* (owner) { |
| 205 | 199 | yield* SynchronizedRef.update(state, (current) => { |
| ... | ... | @@ -217,7 +211,7 @@ const make = Effect.gen(function* PreviewAutomationBrokerMake() { |
| 217 | 211 | }); |
| 218 | 212 | }); |
| 219 | 213 | |
| 220 | const respond: PreviewAutomationBrokerShape["respond"] = Effect.fn( | |
| 214 | const respond: PreviewAutomationBroker["Service"]["respond"] = Effect.fn( | |
| 221 | 215 | "PreviewAutomationBroker.respond", |
| 222 | 216 | )(function* (response) { |
| 223 | 217 | const pending = yield* SynchronizedRef.modify(state, (current) => { |
| ... | ... | @@ -243,7 +237,7 @@ const make = Effect.gen(function* PreviewAutomationBrokerMake() { |
| 243 | 237 | }); |
| 244 | 238 | |
| 245 | 239 | const invoke = Effect.fn("PreviewAutomationBroker.invoke")(function* <A = unknown>( |
| 246 | input: Parameters<PreviewAutomationBrokerShape["invoke"]>[0], | |
| 240 | input: Parameters<PreviewAutomationBroker["Service"]["invoke"]>[0], | |
| 247 | 241 | ): Effect.fn.Return<A, PreviewAutomationError> { |
| 248 | 242 | const current = yield* SynchronizedRef.get(state); |
| 249 | 243 | const candidates = Array.from(current.owners.values()) |
| ... | ... | @@ -317,8 +311,3 @@ const make = Effect.gen(function* PreviewAutomationBrokerMake() { |
| 317 | 311 | }).pipe(Effect.withSpan("PreviewAutomationBroker.make")); |
| 318 | 312 | |
| 319 | 313 | export const layer = Layer.effect(PreviewAutomationBroker, make); |
| 320 | ||
| 321 | /** Exposed for tests. */ | |
| 322 | export const __testing = { | |
| 323 | make, | |
| 324 | }; |
apps/server/src/preview/Manager.ts+98-95| ... | ... | @@ -28,33 +28,30 @@ import { |
| 28 | 28 | normalizePreviewUrl, |
| 29 | 29 | PreviewUrlNormalizationError, |
| 30 | 30 | } from "@t3tools/shared/preview"; |
| 31 | import { | |
| 32 | Context, | |
| 33 | DateTime, | |
| 34 | Effect, | |
| 35 | Layer, | |
| 36 | PubSub, | |
| 37 | type Scope, | |
| 38 | Stream, | |
| 39 | SynchronizedRef, | |
| 40 | } from "effect"; | |
| 41 | ||
| 42 | export interface PreviewManagerShape { | |
| 43 | readonly open: (input: PreviewOpenInput) => Effect.Effect<PreviewSessionSnapshot, PreviewError>; | |
| 44 | readonly navigate: ( | |
| 45 | input: PreviewNavigateInput, | |
| 46 | ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>; | |
| 47 | readonly reportStatus: (input: PreviewReportStatusInput) => Effect.Effect<void, PreviewError>; | |
| 48 | readonly refresh: (input: PreviewRefreshInput) => Effect.Effect<void, PreviewError>; | |
| 49 | readonly close: (input: PreviewCloseInput) => Effect.Effect<void, PreviewError>; | |
| 50 | readonly list: (input: PreviewListInput) => Effect.Effect<PreviewListResult>; | |
| 51 | readonly events: Stream.Stream<PreviewEvent>; | |
| 52 | readonly subscribeEvents: Effect.Effect<PubSub.Subscription<PreviewEvent>, never, Scope.Scope>; | |
| 53 | } | |
| 31 | import * as Context from "effect/Context"; | |
| 32 | import * as DateTime from "effect/DateTime"; | |
| 33 | import * as Effect from "effect/Effect"; | |
| 34 | import * as Layer from "effect/Layer"; | |
| 35 | import * as PubSub from "effect/PubSub"; | |
| 36 | import * as Scope from "effect/Scope"; | |
| 37 | import * as Stream from "effect/Stream"; | |
| 38 | import * as SynchronizedRef from "effect/SynchronizedRef"; | |
| 54 | 39 | |
| 55 | export class PreviewManager extends Context.Service<PreviewManager, PreviewManagerShape>()( | |
| 56 | "t3/preview/Manager/PreviewManager", | |
| 57 | ) {} | |
| 40 | export class PreviewManager extends Context.Service< | |
| 41 | PreviewManager, | |
| 42 | { | |
| 43 | readonly open: (input: PreviewOpenInput) => Effect.Effect<PreviewSessionSnapshot, PreviewError>; | |
| 44 | readonly navigate: ( | |
| 45 | input: PreviewNavigateInput, | |
| 46 | ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>; | |
| 47 | readonly reportStatus: (input: PreviewReportStatusInput) => Effect.Effect<void, PreviewError>; | |
| 48 | readonly refresh: (input: PreviewRefreshInput) => Effect.Effect<void, PreviewError>; | |
| 49 | readonly close: (input: PreviewCloseInput) => Effect.Effect<void, PreviewError>; | |
| 50 | readonly list: (input: PreviewListInput) => Effect.Effect<PreviewListResult>; | |
| 51 | readonly events: Stream.Stream<PreviewEvent>; | |
| 52 | readonly subscribeEvents: Effect.Effect<PubSub.Subscription<PreviewEvent>, never, Scope.Scope>; | |
| 53 | } | |
| 54 | >()("t3/preview/Manager/PreviewManager") {} | |
| 58 | 55 | |
| 59 | 56 | interface PreviewSessionState { |
| 60 | 57 | readonly threadId: string; |
| ... | ... | @@ -127,7 +124,7 @@ const buildIdleSnapshot = (input: { |
| 127 | 124 | updatedAt: input.updatedAt, |
| 128 | 125 | }); |
| 129 | 126 | |
| 130 | const make = Effect.gen(function* PreviewManagerMake() { | |
| 127 | export const make = Effect.gen(function* PreviewManagerMake() { | |
| 131 | 128 | const stateRef = yield* SynchronizedRef.make<ManagerState>(initialState); |
| 132 | 129 | // Unbounded PubSub is fine here — events are tiny and we don't want to |
| 133 | 130 | // block publishers if a subscriber is slow. WS clients backpressure on |
| ... | ... | @@ -184,38 +181,40 @@ const make = Effect.gen(function* PreviewManagerMake() { |
| 184 | 181 | ); |
| 185 | 182 | }; |
| 186 | 183 | |
| 187 | const open: PreviewManagerShape["open"] = Effect.fn("PreviewManager.open")(function* (input) { | |
| 188 | const tabId = newPreviewTabId(); | |
| 189 | const updatedAt = yield* currentIsoTimestamp; | |
| 190 | const snapshot = input.url | |
| 191 | ? buildLoadingSnapshot({ | |
| 184 | const open: PreviewManager["Service"]["open"] = Effect.fn("PreviewManager.open")( | |
| 185 | function* (input) { | |
| 186 | const tabId = newPreviewTabId(); | |
| 187 | const updatedAt = yield* currentIsoTimestamp; | |
| 188 | const snapshot = input.url | |
| 189 | ? buildLoadingSnapshot({ | |
| 190 | threadId: input.threadId, | |
| 191 | tabId, | |
| 192 | url: yield* normalizeUrl(input.url), | |
| 193 | title: "", | |
| 194 | updatedAt, | |
| 195 | }) | |
| 196 | : buildIdleSnapshot({ threadId: input.threadId, tabId, updatedAt }); | |
| 197 | yield* SynchronizedRef.update(stateRef, (state) => { | |
| 198 | const sessions = new Map(state.sessions); | |
| 199 | sessions.set(compositeKey(input.threadId, tabId), { | |
| 192 | 200 | threadId: input.threadId, |
| 193 | 201 | tabId, |
| 194 | url: yield* normalizeUrl(input.url), | |
| 195 | title: "", | |
| 196 | updatedAt, | |
| 197 | }) | |
| 198 | : buildIdleSnapshot({ threadId: input.threadId, tabId, updatedAt }); | |
| 199 | yield* SynchronizedRef.update(stateRef, (state) => { | |
| 200 | const sessions = new Map(state.sessions); | |
| 201 | sessions.set(compositeKey(input.threadId, tabId), { | |
| 202 | snapshot, | |
| 203 | }); | |
| 204 | return { sessions }; | |
| 205 | }); | |
| 206 | yield* PubSub.publish(eventsPubSub, { | |
| 207 | type: "opened", | |
| 202 | 208 | threadId: input.threadId, |
| 203 | 209 | tabId, |
| 210 | createdAt: snapshot.updatedAt, | |
| 204 | 211 | snapshot, |
| 205 | 212 | }); |
| 206 | return { sessions }; | |
| 207 | }); | |
| 208 | yield* PubSub.publish(eventsPubSub, { | |
| 209 | type: "opened", | |
| 210 | threadId: input.threadId, | |
| 211 | tabId, | |
| 212 | createdAt: snapshot.updatedAt, | |
| 213 | snapshot, | |
| 214 | }); | |
| 215 | return snapshot; | |
| 216 | }); | |
| 213 | return snapshot; | |
| 214 | }, | |
| 215 | ); | |
| 217 | 216 | |
| 218 | const navigate: PreviewManagerShape["navigate"] = Effect.fn("PreviewManager.navigate")( | |
| 217 | const navigate: PreviewManager["Service"]["navigate"] = Effect.fn("PreviewManager.navigate")( | |
| 219 | 218 | function* (input) { |
| 220 | 219 | const url = yield* normalizeUrl(input.url); |
| 221 | 220 | return yield* mutateExistingSession( |
| ... | ... | @@ -250,7 +249,7 @@ const make = Effect.gen(function* PreviewManagerMake() { |
| 250 | 249 | }, |
| 251 | 250 | ); |
| 252 | 251 | |
| 253 | const reportStatus: PreviewManagerShape["reportStatus"] = Effect.fn( | |
| 252 | const reportStatus: PreviewManager["Service"]["reportStatus"] = Effect.fn( | |
| 254 | 253 | "PreviewManager.reportStatus", |
| 255 | 254 | )(function* (input) { |
| 256 | 255 | yield* mutateExistingSession( |
| ... | ... | @@ -294,7 +293,7 @@ const make = Effect.gen(function* PreviewManagerMake() { |
| 294 | 293 | ); |
| 295 | 294 | }); |
| 296 | 295 | |
| 297 | const refresh: PreviewManagerShape["refresh"] = Effect.fn("PreviewManager.refresh")( | |
| 296 | const refresh: PreviewManager["Service"]["refresh"] = Effect.fn("PreviewManager.refresh")( | |
| 298 | 297 | function* (input) { |
| 299 | 298 | // Verify the session exists; the desktop bridge handles the actual reload |
| 300 | 299 | // and will report progress back via `reportStatus`. No event emitted. |
| ... | ... | @@ -304,50 +303,54 @@ const make = Effect.gen(function* PreviewManagerMake() { |
| 304 | 303 | }, |
| 305 | 304 | ); |
| 306 | 305 | |
| 307 | const close: PreviewManagerShape["close"] = Effect.fn("PreviewManager.close")(function* (input) { | |
| 308 | const createdAt = yield* currentIsoTimestamp; | |
| 309 | const events = yield* SynchronizedRef.modify(stateRef, (state) => { | |
| 310 | const eventsToEmit: PreviewEvent[] = []; | |
| 311 | const sessions = new Map(state.sessions); | |
| 312 | const targets = input.tabId | |
| 313 | ? [state.sessions.get(compositeKey(input.threadId, input.tabId))].filter( | |
| 314 | (entry): entry is PreviewSessionState => entry !== undefined, | |
| 315 | ) | |
| 316 | : sessionsForThread(state, input.threadId); | |
| 317 | for (const target of targets) { | |
| 318 | sessions.delete(compositeKey(target.threadId, target.tabId)); | |
| 319 | eventsToEmit.push({ | |
| 320 | type: "closed", | |
| 321 | threadId: target.threadId, | |
| 322 | tabId: target.tabId, | |
| 323 | createdAt, | |
| 306 | const close: PreviewManager["Service"]["close"] = Effect.fn("PreviewManager.close")( | |
| 307 | function* (input) { | |
| 308 | const createdAt = yield* currentIsoTimestamp; | |
| 309 | const events = yield* SynchronizedRef.modify(stateRef, (state) => { | |
| 310 | const eventsToEmit: PreviewEvent[] = []; | |
| 311 | const sessions = new Map(state.sessions); | |
| 312 | const targets = input.tabId | |
| 313 | ? [state.sessions.get(compositeKey(input.threadId, input.tabId))].filter( | |
| 314 | (entry): entry is PreviewSessionState => entry !== undefined, | |
| 315 | ) | |
| 316 | : sessionsForThread(state, input.threadId); | |
| 317 | for (const target of targets) { | |
| 318 | sessions.delete(compositeKey(target.threadId, target.tabId)); | |
| 319 | eventsToEmit.push({ | |
| 320 | type: "closed", | |
| 321 | threadId: target.threadId, | |
| 322 | tabId: target.tabId, | |
| 323 | createdAt, | |
| 324 | }); | |
| 325 | } | |
| 326 | if (eventsToEmit.length === 0) { | |
| 327 | return [eventsToEmit, state] as const; | |
| 328 | } | |
| 329 | return [eventsToEmit, { sessions }] as const; | |
| 330 | }); | |
| 331 | if (events.length > 0) { | |
| 332 | yield* Effect.forEach(events, (event) => PubSub.publish(eventsPubSub, event), { | |
| 333 | discard: true, | |
| 324 | 334 | }); |
| 325 | 335 | } |
| 326 | if (eventsToEmit.length === 0) { | |
| 327 | return [eventsToEmit, state] as const; | |
| 328 | } | |
| 329 | return [eventsToEmit, { sessions }] as const; | |
| 330 | }); | |
| 331 | if (events.length > 0) { | |
| 332 | yield* Effect.forEach(events, (event) => PubSub.publish(eventsPubSub, event), { | |
| 333 | discard: true, | |
| 334 | }); | |
| 335 | } | |
| 336 | }); | |
| 336 | }, | |
| 337 | ); | |
| 337 | 338 | |
| 338 | const list: PreviewManagerShape["list"] = Effect.fn("PreviewManager.list")(function* (input) { | |
| 339 | return yield* SynchronizedRef.get(stateRef).pipe( | |
| 340 | Effect.map( | |
| 341 | (state): PreviewListResult => ({ | |
| 342 | sessions: sessionsForThread(state, input.threadId) | |
| 343 | .map((s) => s.snapshot) | |
| 344 | .toSorted((a, b) => a.updatedAt.localeCompare(b.updatedAt)), | |
| 345 | }), | |
| 346 | ), | |
| 347 | ); | |
| 348 | }); | |
| 339 | const list: PreviewManager["Service"]["list"] = Effect.fn("PreviewManager.list")( | |
| 340 | function* (input) { | |
| 341 | return yield* SynchronizedRef.get(stateRef).pipe( | |
| 342 | Effect.map( | |
| 343 | (state): PreviewListResult => ({ | |
| 344 | sessions: sessionsForThread(state, input.threadId) | |
| 345 | .map((s) => s.snapshot) | |
| 346 | .toSorted((a, b) => a.updatedAt.localeCompare(b.updatedAt)), | |
| 347 | }), | |
| 348 | ), | |
| 349 | ); | |
| 350 | }, | |
| 351 | ); | |
| 349 | 352 | |
| 350 | return { | |
| 353 | return PreviewManager.of({ | |
| 351 | 354 | open, |
| 352 | 355 | navigate, |
| 353 | 356 | reportStatus, |
| ... | ... | @@ -356,7 +359,7 @@ const make = Effect.gen(function* PreviewManagerMake() { |
| 356 | 359 | list, |
| 357 | 360 | events, |
| 358 | 361 | subscribeEvents: PubSub.subscribe(eventsPubSub), |
| 359 | } satisfies PreviewManagerShape; | |
| 362 | }); | |
| 360 | 363 | }).pipe(Effect.withSpan("PreviewManager.make")); |
| 361 | 364 | |
| 362 | 365 | export const layer = Layer.effect(PreviewManager, make); |
apps/server/src/preview/PortScanner.ts+54-49| ... | ... | @@ -15,30 +15,36 @@ import { ThreadId, type DiscoveredLocalServer } from "@t3tools/contracts"; |
| 15 | 15 | import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 16 | 16 | import * as Net from "@t3tools/shared/Net"; |
| 17 | 17 | import { LSOF_LOCAL_HOST_TOKENS } from "@t3tools/shared/preview"; |
| 18 | import { Cause, Context, Duration, Effect, Layer, Ref, Schedule, Scope } from "effect"; | |
| 18 | import * as Cause from "effect/Cause"; | |
| 19 | import * as Context from "effect/Context"; | |
| 20 | import * as Duration from "effect/Duration"; | |
| 21 | import * as Effect from "effect/Effect"; | |
| 22 | import * as Layer from "effect/Layer"; | |
| 23 | import * as Ref from "effect/Ref"; | |
| 24 | import * as Schedule from "effect/Schedule"; | |
| 25 | import * as Scope from "effect/Scope"; | |
| 19 | 26 | |
| 20 | import { ProcessRunner } from "../processRunner.ts"; | |
| 27 | import * as ProcessRunner from "../processRunner.ts"; | |
| 21 | 28 | |
| 22 | export interface PortDiscoveryShape { | |
| 23 | readonly scan: () => Effect.Effect<ReadonlyArray<DiscoveredLocalServer>>; | |
| 24 | readonly subscribe: ( | |
| 25 | listener: (servers: ReadonlyArray<DiscoveredLocalServer>) => Effect.Effect<void>, | |
| 26 | ) => Effect.Effect<void, never, Scope.Scope>; | |
| 27 | readonly retain: Effect.Effect<void, never, Scope.Scope>; | |
| 28 | readonly registerTerminalProcesses: (input: { | |
| 29 | readonly threadId: string; | |
| 30 | readonly terminalId: string; | |
| 31 | readonly processIds: ReadonlyArray<number>; | |
| 32 | }) => Effect.Effect<void>; | |
| 33 | readonly unregisterTerminal: (input: { | |
| 34 | readonly threadId: string; | |
| 35 | readonly terminalId: string; | |
| 36 | }) => Effect.Effect<void>; | |
| 37 | } | |
| 38 | ||
| 39 | export class PortDiscovery extends Context.Service<PortDiscovery, PortDiscoveryShape>()( | |
| 40 | "t3/preview/PortScanner/PortDiscovery", | |
| 41 | ) {} | |
| 29 | export class PortDiscovery extends Context.Service< | |
| 30 | PortDiscovery, | |
| 31 | { | |
| 32 | readonly scan: () => Effect.Effect<ReadonlyArray<DiscoveredLocalServer>>; | |
| 33 | readonly subscribe: ( | |
| 34 | listener: (servers: ReadonlyArray<DiscoveredLocalServer>) => Effect.Effect<void>, | |
| 35 | ) => Effect.Effect<void, never, Scope.Scope>; | |
| 36 | readonly retain: Effect.Effect<void, never, Scope.Scope>; | |
| 37 | readonly registerTerminalProcesses: (input: { | |
| 38 | readonly threadId: string; | |
| 39 | readonly terminalId: string; | |
| 40 | readonly processIds: ReadonlyArray<number>; | |
| 41 | }) => Effect.Effect<void>; | |
| 42 | readonly unregisterTerminal: (input: { | |
| 43 | readonly threadId: string; | |
| 44 | readonly terminalId: string; | |
| 45 | }) => Effect.Effect<void>; | |
| 46 | } | |
| 47 | >()("t3/preview/PortScanner/PortDiscovery") {} | |
| 42 | 48 | |
| 43 | 49 | export const COMMON_DEV_PORTS: ReadonlyArray<number> = Object.freeze([ |
| 44 | 50 | 3000, 3001, 3333, 4173, 4200, 4321, 5000, 5173, 5174, 5175, 5500, 8000, 8080, 8081, 8888, 9000, |
| ... | ... | @@ -180,9 +186,9 @@ const serversEqual = ( |
| 180 | 186 | return true; |
| 181 | 187 | }; |
| 182 | 188 | |
| 183 | const make = Effect.gen(function* PortDiscoveryMake() { | |
| 189 | export const make = Effect.gen(function* PortDiscoveryMake() { | |
| 184 | 190 | const net = yield* Net.NetService; |
| 185 | const processRunner = yield* ProcessRunner; | |
| 191 | const processRunner = yield* ProcessRunner.ProcessRunner; | |
| 186 | 192 | const hostPlatform = yield* HostProcessPlatform; |
| 187 | 193 | const stateRef = yield* Ref.make<ScannerState>({ |
| 188 | 194 | lastSnapshot: [], |
| ... | ... | @@ -296,14 +302,14 @@ const make = Effect.gen(function* PortDiscoveryMake() { |
| 296 | 302 | } |
| 297 | 303 | }); |
| 298 | 304 | |
| 299 | const retain: PortDiscoveryShape["retain"] = Effect.acquireRelease(acquireRetention(), () => | |
| 305 | const retain: PortDiscovery["Service"]["retain"] = Effect.acquireRelease(acquireRetention(), () => | |
| 300 | 306 | Ref.update(stateRef, (state) => ({ |
| 301 | 307 | ...state, |
| 302 | 308 | retainCount: Math.max(0, state.retainCount - 1), |
| 303 | 309 | })), |
| 304 | 310 | ); |
| 305 | 311 | |
| 306 | const subscribe: PortDiscoveryShape["subscribe"] = Effect.fn("PortDiscovery.subscribe")( | |
| 312 | const subscribe: PortDiscovery["Service"]["subscribe"] = Effect.fn("PortDiscovery.subscribe")( | |
| 307 | 313 | (listener) => |
| 308 | 314 | Effect.acquireRelease( |
| 309 | 315 | Ref.update(stateRef, (state) => ({ |
| ... | ... | @@ -319,29 +325,28 @@ const make = Effect.gen(function* PortDiscoveryMake() { |
| 319 | 325 | ), |
| 320 | 326 | ); |
| 321 | 327 | |
| 322 | const registerTerminalProcesses: PortDiscoveryShape["registerTerminalProcesses"] = Effect.fn( | |
| 323 | "PortDiscovery.registerTerminalProcesses", | |
| 324 | )(function* (input) { | |
| 325 | const owner = { | |
| 326 | threadId: ThreadId.make(input.threadId), | |
| 327 | terminalId: input.terminalId, | |
| 328 | }; | |
| 329 | const processIds = new Set( | |
| 330 | input.processIds.filter((processId) => Number.isInteger(processId) && processId > 0), | |
| 331 | ); | |
| 332 | yield* Ref.update(stateRef, (state) => { | |
| 333 | const terminalProcesses = new Map(state.terminalProcesses); | |
| 334 | const key = terminalOwnerKey(owner); | |
| 335 | if (processIds.size === 0) { | |
| 336 | terminalProcesses.delete(key); | |
| 337 | } else { | |
| 338 | terminalProcesses.set(key, { owner, processIds }); | |
| 339 | } | |
| 340 | return { ...state, terminalProcesses }; | |
| 328 | const registerTerminalProcesses: PortDiscovery["Service"]["registerTerminalProcesses"] = | |
| 329 | Effect.fn("PortDiscovery.registerTerminalProcesses")(function* (input) { | |
| 330 | const owner = { | |
| 331 | threadId: ThreadId.make(input.threadId), | |
| 332 | terminalId: input.terminalId, | |
| 333 | }; | |
| 334 | const processIds = new Set( | |
| 335 | input.processIds.filter((processId) => Number.isInteger(processId) && processId > 0), | |
| 336 | ); | |
| 337 | yield* Ref.update(stateRef, (state) => { | |
| 338 | const terminalProcesses = new Map(state.terminalProcesses); | |
| 339 | const key = terminalOwnerKey(owner); | |
| 340 | if (processIds.size === 0) { | |
| 341 | terminalProcesses.delete(key); | |
| 342 | } else { | |
| 343 | terminalProcesses.set(key, { owner, processIds }); | |
| 344 | } | |
| 345 | return { ...state, terminalProcesses }; | |
| 346 | }); | |
| 341 | 347 | }); |
| 342 | }); | |
| 343 | 348 | |
| 344 | const unregisterTerminal: PortDiscoveryShape["unregisterTerminal"] = Effect.fn( | |
| 349 | const unregisterTerminal: PortDiscovery["Service"]["unregisterTerminal"] = Effect.fn( | |
| 345 | 350 | "PortDiscovery.unregisterTerminal", |
| 346 | 351 | )(function* (input) { |
| 347 | 352 | yield* Ref.update(stateRef, (state) => { |
| ... | ... | @@ -351,13 +356,13 @@ const make = Effect.gen(function* PortDiscoveryMake() { |
| 351 | 356 | }); |
| 352 | 357 | }); |
| 353 | 358 | |
| 354 | return { | |
| 359 | return PortDiscovery.of({ | |
| 355 | 360 | scan: scanOnce, |
| 356 | 361 | subscribe, |
| 357 | 362 | retain, |
| 358 | 363 | registerTerminalProcesses, |
| 359 | 364 | unregisterTerminal, |
| 360 | } satisfies PortDiscoveryShape; | |
| 365 | }); | |
| 361 | 366 | }).pipe(Effect.withSpan("PortDiscovery.make")); |
| 362 | 367 | |
| 363 | 368 | export const layer = Layer.effect(PortDiscovery, make); |
apps/server/src/process/externalLauncher.ts+19-25| ... | ... | @@ -22,7 +22,8 @@ import * as FileSystem from "effect/FileSystem"; |
| 22 | 22 | import * as Layer from "effect/Layer"; |
| 23 | 23 | import * as Option from "effect/Option"; |
| 24 | 24 | import * as Path from "effect/Path"; |
| 25 | import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; | |
| 25 | import * as ChildProcess from "effect/unstable/process/ChildProcess"; | |
| 26 | import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; | |
| 26 | 27 | |
| 27 | 28 | // ============================== |
| 28 | 29 | // Definitions |
| ... | ... | @@ -282,30 +283,23 @@ const resolveAvailableEditors = Effect.fn("externalLauncher.resolveAvailableEdit |
| 282 | 283 | return yield* buildAvailableEditors(platform, env); |
| 283 | 284 | }); |
| 284 | 285 | |
| 285 | /** | |
| 286 | * ExternalLauncherShape - Service API for browser and editor launch actions. | |
| 287 | */ | |
| 288 | export interface ExternalLauncherShape { | |
| 289 | readonly resolveAvailableEditors: () => Effect.Effect<ReadonlyArray<EditorId>>; | |
| 290 | /** | |
| 291 | * Launch a URL target in the default browser. | |
| 292 | */ | |
| 293 | readonly launchBrowser: (target: string) => Effect.Effect<void, ExternalLauncherError>; | |
| 294 | ||
| 295 | /** | |
| 296 | * Launch a workspace path in a selected editor integration. | |
| 297 | * | |
| 298 | * Launches the editor as a detached process so server startup is not blocked. | |
| 299 | */ | |
| 300 | readonly launchEditor: (input: LaunchEditorInput) => Effect.Effect<void, ExternalLauncherError>; | |
| 301 | } | |
| 302 | ||
| 303 | 286 | /** |
| 304 | 287 | * ExternalLauncher - Service tag for browser/editor launch operations. |
| 305 | 288 | */ |
| 306 | export class ExternalLauncher extends Context.Service<ExternalLauncher, ExternalLauncherShape>()( | |
| 307 | "t3/process/externalLauncher", | |
| 308 | ) {} | |
| 289 | export class ExternalLauncher extends Context.Service< | |
| 290 | ExternalLauncher, | |
| 291 | { | |
| 292 | readonly resolveAvailableEditors: () => Effect.Effect<ReadonlyArray<EditorId>>; | |
| 293 | /** Launch a URL target in the default browser. */ | |
| 294 | readonly launchBrowser: (target: string) => Effect.Effect<void, ExternalLauncherError>; | |
| 295 | /** | |
| 296 | * Launch a workspace path in a selected editor integration. | |
| 297 | * | |
| 298 | * Launches the editor as a detached process so server startup is not blocked. | |
| 299 | */ | |
| 300 | readonly launchEditor: (input: LaunchEditorInput) => Effect.Effect<void, ExternalLauncherError>; | |
| 301 | } | |
| 302 | >()("t3/process/externalLauncher") {} | |
| 309 | 303 | |
| 310 | 304 | // ============================== |
| 311 | 305 | // Implementations |
| ... | ... | @@ -397,7 +391,7 @@ const launchEditorProcess = Effect.fn("externalLauncher.launchEditorProcess")(fu |
| 397 | 391 | ); |
| 398 | 392 | }); |
| 399 | 393 | |
| 400 | const make = Effect.gen(function* () { | |
| 394 | export const make = Effect.gen(function* () { | |
| 401 | 395 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 402 | 396 | const fileSystem = yield* FileSystem.FileSystem; |
| 403 | 397 | const path = yield* Path.Path; |
| ... | ... | @@ -410,7 +404,7 @@ const make = Effect.gen(function* () { |
| 410 | 404 | Effect.provideService(Path.Path, path), |
| 411 | 405 | ); |
| 412 | 406 | |
| 413 | return { | |
| 407 | return ExternalLauncher.of({ | |
| 414 | 408 | resolveAvailableEditors: () => provideCommandResolutionServices(resolveAvailableEditors()), |
| 415 | 409 | launchBrowser: (target) => |
| 416 | 410 | launchBrowser(target).pipe( |
| ... | ... | @@ -424,7 +418,7 @@ const make = Effect.gen(function* () { |
| 424 | 418 | ), |
| 425 | 419 | ), |
| 426 | 420 | ), |
| 427 | } satisfies ExternalLauncherShape; | |
| 421 | }); | |
| 428 | 422 | }); |
| 429 | 423 | |
| 430 | 424 | export const layer = Layer.effect(ExternalLauncher, make); |
apps/server/src/processRunner.test.ts+11-17| ... | ... | @@ -11,14 +11,7 @@ import { ChildProcessSpawner } from "effect/unstable/process"; |
| 11 | 11 | import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 12 | 12 | import { SpawnExecutableResolution } from "@t3tools/shared/shell"; |
| 13 | 13 | |
| 14 | import { | |
| 15 | isWindowsCommandNotFound, | |
| 16 | ProcessOutputLimitError, | |
| 17 | ProcessRunner, | |
| 18 | ProcessTimeoutError, | |
| 19 | layer as ProcessRunnerLive, | |
| 20 | type ProcessRunInput, | |
| 21 | } from "./processRunner.ts"; | |
| 14 | import * as ProcessRunner from "./processRunner.ts"; | |
| 22 | 15 | |
| 23 | 16 | type ChildProcessCommand = { |
| 24 | 17 | readonly command: string; |
| ... | ... | @@ -68,15 +61,16 @@ function makeSpawner( |
| 68 | 61 | } |
| 69 | 62 | |
| 70 | 63 | const runWith = |
| 71 | (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) => (input: ProcessRunInput) => | |
| 72 | Effect.service(ProcessRunner).pipe( | |
| 64 | (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) => | |
| 65 | (input: ProcessRunner.ProcessRunInput) => | |
| 66 | Effect.service(ProcessRunner.ProcessRunner).pipe( | |
| 73 | 67 | Effect.flatMap((runner) => |
| 74 | 68 | runner.run({ |
| 75 | 69 | ...input, |
| 76 | 70 | }), |
| 77 | 71 | ), |
| 78 | 72 | Effect.provide( |
| 79 | ProcessRunnerLive.pipe( | |
| 73 | ProcessRunner.layer.pipe( | |
| 80 | 74 | Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)), |
| 81 | 75 | ), |
| 82 | 76 | ), |
| ... | ... | @@ -112,12 +106,12 @@ describe("runProcess", () => { |
| 112 | 106 | return makeHandle({ stdout: "service ok" }); |
| 113 | 107 | }), |
| 114 | 108 | ); |
| 115 | const layer = ProcessRunnerLive.pipe( | |
| 109 | const layer = ProcessRunner.layer.pipe( | |
| 116 | 110 | Layer.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)), |
| 117 | 111 | ); |
| 118 | 112 | |
| 119 | 113 | return Effect.gen(function* () { |
| 120 | const runner = yield* ProcessRunner; | |
| 114 | const runner = yield* ProcessRunner.ProcessRunner; | |
| 121 | 115 | const result = yield* runner.run({ |
| 122 | 116 | command: "fake", |
| 123 | 117 | args: ["--service"], |
| ... | ... | @@ -175,7 +169,7 @@ describe("runProcess", () => { |
| 175 | 169 | maxOutputBytes: 128, |
| 176 | 170 | }).pipe(Effect.flip); |
| 177 | 171 | |
| 178 | expect(error).toBeInstanceOf(ProcessOutputLimitError); | |
| 172 | expect(error).toBeInstanceOf(ProcessRunner.ProcessOutputLimitError); | |
| 179 | 173 | }), |
| 180 | 174 | ); |
| 181 | 175 | |
| ... | ... | @@ -200,7 +194,7 @@ describe("runProcess", () => { |
| 200 | 194 | timeout: "2 seconds", |
| 201 | 195 | }).pipe(Effect.flip); |
| 202 | 196 | |
| 203 | expect(error).toBeInstanceOf(ProcessOutputLimitError); | |
| 197 | expect(error).toBeInstanceOf(ProcessRunner.ProcessOutputLimitError); | |
| 204 | 198 | }), |
| 205 | 199 | ); |
| 206 | 200 | |
| ... | ... | @@ -285,7 +279,7 @@ describe("runProcess", () => { |
| 285 | 279 | yield* TestClock.adjust(Duration.millis(50)); |
| 286 | 280 | const error = yield* Fiber.join(errorFiber); |
| 287 | 281 | |
| 288 | expect(error).toBeInstanceOf(ProcessTimeoutError); | |
| 282 | expect(error).toBeInstanceOf(ProcessRunner.ProcessTimeoutError); | |
| 289 | 283 | }), |
| 290 | 284 | ); |
| 291 | 285 | |
| ... | ... | @@ -324,7 +318,7 @@ describe("runProcess", () => { |
| 324 | 318 | describe("isWindowsCommandNotFound", () => { |
| 325 | 319 | it.effect("matches the localized German cmd.exe error text", () => |
| 326 | 320 | Effect.gen(function* () { |
| 327 | const isCommandNotFound = yield* isWindowsCommandNotFound( | |
| 321 | const isCommandNotFound = yield* ProcessRunner.isWindowsCommandNotFound( | |
| 328 | 322 | 1, |
| 329 | 323 | "wird nicht als interner oder externer Befehl, betriebsfahiges Programm oder Batch-Datei erkannt", |
| 330 | 324 | ).pipe(Effect.provideService(HostProcessPlatform, "win32")); |
apps/server/src/processRunner.ts+80-40| ... | ... | @@ -1,13 +1,14 @@ |
| 1 | import * as Data from "effect/Data"; | |
| 2 | 1 | import * as Context from "effect/Context"; |
| 3 | 2 | import * as Duration from "effect/Duration"; |
| 4 | 3 | import * as Effect from "effect/Effect"; |
| 5 | 4 | import * as Layer from "effect/Layer"; |
| 6 | 5 | import * as Option from "effect/Option"; |
| 7 | 6 | import * as PlatformError from "effect/PlatformError"; |
| 7 | import * as Schema from "effect/Schema"; | |
| 8 | 8 | import * as Scope from "effect/Scope"; |
| 9 | 9 | import * as Stream from "effect/Stream"; |
| 10 | import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; | |
| 10 | import * as ChildProcess from "effect/unstable/process/ChildProcess"; | |
| 11 | import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; | |
| 11 | 12 | import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; |
| 12 | 13 | import { resolveSpawnCommand } from "@t3tools/shared/shell"; |
| 13 | 14 | import { |
| ... | ... | @@ -42,42 +43,82 @@ export interface ProcessRunOutput { |
| 42 | 43 | readonly stderrTruncated: boolean; |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | export class ProcessSpawnError extends Data.TaggedError("ProcessSpawnError")<{ | |
| 46 | readonly command: string; | |
| 47 | readonly args: ReadonlyArray<string>; | |
| 48 | readonly cwd?: string | undefined; | |
| 49 | readonly cause: unknown; | |
| 50 | }> {} | |
| 46 | const ProcessInvocationFields = { | |
| 47 | command: Schema.String, | |
| 48 | args: Schema.Array(Schema.String), | |
| 49 | cwd: Schema.optional(Schema.String), | |
| 50 | }; | |
| 51 | 51 | |
| 52 | export class ProcessStdinError extends Data.TaggedError("ProcessStdinError")<{ | |
| 52 | const formatProcessInvocation = (input: { | |
| 53 | 53 | readonly command: string; |
| 54 | 54 | readonly args: ReadonlyArray<string>; |
| 55 | 55 | readonly cwd?: string | undefined; |
| 56 | readonly cause: unknown; | |
| 57 | }> {} | |
| 56 | }): string => { | |
| 57 | const command = [input.command, ...input.args].join(" "); | |
| 58 | return input.cwd === undefined ? `'${command}'` : `'${command}' in '${input.cwd}'`; | |
| 59 | }; | |
| 58 | 60 | |
| 59 | export class ProcessOutputLimitError extends Data.TaggedError("ProcessOutputLimitError")<{ | |
| 60 | readonly command: string; | |
| 61 | readonly args: ReadonlyArray<string>; | |
| 62 | readonly cwd?: string | undefined; | |
| 63 | readonly stream: "stdout" | "stderr"; | |
| 64 | readonly maxBytes: number; | |
| 65 | }> {} | |
| 61 | export class ProcessSpawnError extends Schema.TaggedErrorClass<ProcessSpawnError>()( | |
| 62 | "ProcessSpawnError", | |
| 63 | { | |
| 64 | ...ProcessInvocationFields, | |
| 65 | cause: Schema.Defect(), | |
| 66 | }, | |
| 67 | ) { | |
| 68 | override get message(): string { | |
| 69 | return `Failed to spawn process ${formatProcessInvocation(this)}`; | |
| 70 | } | |
| 71 | } | |
| 66 | 72 | |
| 67 | export class ProcessReadError extends Data.TaggedError("ProcessReadError")<{ | |
| 68 | readonly command: string; | |
| 69 | readonly args: ReadonlyArray<string>; | |
| 70 | readonly cwd?: string | undefined; | |
| 71 | readonly stream: "stdout" | "stderr" | "exitCode"; | |
| 72 | readonly cause: unknown; | |
| 73 | }> {} | |
| 73 | export class ProcessStdinError extends Schema.TaggedErrorClass<ProcessStdinError>()( | |
| 74 | "ProcessStdinError", | |
| 75 | { | |
| 76 | ...ProcessInvocationFields, | |
| 77 | cause: Schema.Defect(), | |
| 78 | }, | |
| 79 | ) { | |
| 80 | override get message(): string { | |
| 81 | return `Failed to write stdin for process ${formatProcessInvocation(this)}`; | |
| 82 | } | |
| 83 | } | |
| 74 | 84 | |
| 75 | export class ProcessTimeoutError extends Data.TaggedError("ProcessTimeoutError")<{ | |
| 76 | readonly command: string; | |
| 77 | readonly args: ReadonlyArray<string>; | |
| 78 | readonly cwd?: string | undefined; | |
| 79 | readonly timeoutMs: number; | |
| 80 | }> {} | |
| 85 | export class ProcessOutputLimitError extends Schema.TaggedErrorClass<ProcessOutputLimitError>()( | |
| 86 | "ProcessOutputLimitError", | |
| 87 | { | |
| 88 | ...ProcessInvocationFields, | |
| 89 | stream: Schema.Literals(["stdout", "stderr"]), | |
| 90 | maxBytes: Schema.Number, | |
| 91 | }, | |
| 92 | ) { | |
| 93 | override get message(): string { | |
| 94 | return `Process ${formatProcessInvocation(this)} ${this.stream} exceeded ${this.maxBytes} bytes`; | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | export class ProcessReadError extends Schema.TaggedErrorClass<ProcessReadError>()( | |
| 99 | "ProcessReadError", | |
| 100 | { | |
| 101 | ...ProcessInvocationFields, | |
| 102 | stream: Schema.Literals(["stdout", "stderr", "exitCode"]), | |
| 103 | cause: Schema.Defect(), | |
| 104 | }, | |
| 105 | ) { | |
| 106 | override get message(): string { | |
| 107 | return `Failed to read ${this.stream} for process ${formatProcessInvocation(this)}`; | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | export class ProcessTimeoutError extends Schema.TaggedErrorClass<ProcessTimeoutError>()( | |
| 112 | "ProcessTimeoutError", | |
| 113 | { | |
| 114 | ...ProcessInvocationFields, | |
| 115 | timeoutMs: Schema.Number, | |
| 116 | }, | |
| 117 | ) { | |
| 118 | override get message(): string { | |
| 119 | return `Process ${formatProcessInvocation(this)} timed out after ${this.timeoutMs}ms`; | |
| 120 | } | |
| 121 | } | |
| 81 | 122 | |
| 82 | 123 | export type ProcessRunError = |
| 83 | 124 | | ProcessSpawnError |
| ... | ... | @@ -86,13 +127,12 @@ export type ProcessRunError = |
| 86 | 127 | | ProcessReadError |
| 87 | 128 | | ProcessTimeoutError; |
| 88 | 129 | |
| 89 | export interface ProcessRunnerShape { | |
| 90 | readonly run: (input: ProcessRunInput) => Effect.Effect<ProcessRunOutput, ProcessRunError>; | |
| 91 | } | |
| 92 | ||
| 93 | export class ProcessRunner extends Context.Service<ProcessRunner, ProcessRunnerShape>()( | |
| 94 | "t3/processRunner", | |
| 95 | ) {} | |
| 130 | export class ProcessRunner extends Context.Service< | |
| 131 | ProcessRunner, | |
| 132 | { | |
| 133 | readonly run: (input: ProcessRunInput) => Effect.Effect<ProcessRunOutput, ProcessRunError>; | |
| 134 | } | |
| 135 | >()("t3/processRunner") {} | |
| 96 | 136 | |
| 97 | 137 | const DEFAULT_TIMEOUT = "60 seconds"; |
| 98 | 138 | const DEFAULT_MAX_OUTPUT_BYTES = 8 * 1024 * 1024; |
| ... | ... | @@ -332,10 +372,10 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* ( |
| 332 | 372 | } satisfies ProcessRunOutput; |
| 333 | 373 | }); |
| 334 | 374 | |
| 335 | export const make = Effect.fn("makeProcessRunner")(function* () { | |
| 375 | export const make = Effect.fn("ProcessRunner.make")(function* () { | |
| 336 | 376 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 337 | 377 | |
| 338 | const run: ProcessRunnerShape["run"] = (input) => | |
| 378 | const run: ProcessRunner["Service"]["run"] = (input) => | |
| 339 | 379 | finalizeRunProcess(runProcessCore(spawner, input), input); |
| 340 | 380 | |
| 341 | 381 | return ProcessRunner.of({ |