|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeNet from "node:net"; |
| 3 | +import * as Exit from "effect/Exit"; |
| 4 | +import * as Schema from "effect/Schema"; |
| 5 | +import { SearchRequest, SearchResponse } from "../src/workspace/workspaceSearchProtocol.ts"; |
| 6 | +import { |
| 7 | + WorkspaceSearchIndexRefreshFailed, |
| 8 | + WorkspaceSearchIndexScanTimedOut, |
| 9 | +} from "../src/workspace/WorkspaceSearchIndexService.ts"; |
| 10 | + |
| 11 | +const decodeSearchRequest = Schema.decodeUnknownSync(SearchRequest); |
| 12 | +const encodeSearchResponse = Schema.encodeSync(SearchResponse); |
| 13 | + |
| 14 | +const directories = new Map<number, string>(); |
| 15 | +const block = () => { |
| 16 | + const socket = NodeNet.connect( |
| 17 | + Number(process.env.T3_SEARCH_TEST_RECEIPT_PORT), |
| 18 | + "127.0.0.1", |
| 19 | + () => { |
| 20 | + socket.write("blocked"); |
| 21 | + // Model a synchronous FFI call which cannot service IPC or JS timers. |
| 22 | + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0); |
| 23 | + }, |
| 24 | + ); |
| 25 | +}; |
| 26 | +process.on("disconnect", () => process.exit(0)); |
| 27 | +process.on("message", (message) => { |
| 28 | + const { id, operation: input } = decodeSearchRequest(message); |
| 29 | + if (input.method === "initialize") directories.set(id, input.cwd); |
| 30 | + const cwd = directories.get(id) ?? ""; |
| 31 | + if (input.method === "dispose") { |
| 32 | + if (cwd === "block-dispose") { |
| 33 | + block(); |
| 34 | + return; |
| 35 | + } |
| 36 | + directories.delete(id); |
| 37 | + process.send?.(encodeSearchResponse(Exit.succeed(null))); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (cwd === "block-initialize" || (input.method === "search" && input.query === "block")) { |
| 41 | + block(); |
| 42 | + return; |
| 43 | + } |
| 44 | + if (input.method === "search" && input.query === "crash") process.exit(1); |
| 45 | + if (input.method === "refresh") { |
| 46 | + process.send?.( |
| 47 | + encodeSearchResponse( |
| 48 | + Exit.fail(new WorkspaceSearchIndexRefreshFailed({ cwd, reason: "scan failed" })), |
| 49 | + ), |
| 50 | + ); |
| 51 | + return; |
| 52 | + } |
| 53 | + if (cwd === "scan-timeout") { |
| 54 | + process.send?.( |
| 55 | + encodeSearchResponse( |
| 56 | + Exit.fail(new WorkspaceSearchIndexScanTimedOut({ cwd, timeout: "15 seconds" })), |
| 57 | + ), |
| 58 | + ); |
| 59 | + return; |
| 60 | + } |
| 61 | + process.send?.( |
| 62 | + encodeSearchResponse( |
| 63 | + Exit.succeed( |
| 64 | + input.method === "initialize" |
| 65 | + ? null |
| 66 | + : { |
| 67 | + entries: [{ path: String(process.pid), kind: "file" }], |
| 68 | + truncated: false, |
| 69 | + }, |
| 70 | + ), |
| 71 | + ), |
| 72 | + ); |
| 73 | +}); |
0 commit comments