Unified · Split
| @@ -30,7 +30,7 @@ import { · 29 unmodified lines | ||
| 30 | 30 | requiredTemplateVariables, |
| 31 | 31 | type OpenApiIntegrationConfig, |
| 32 | 32 | } from "./config"; |
| 33 | −import { OpenApiExtractionError, OpenApiParseError } from "./errors"; | |
| 33 | +import { OpenApiExtractionError, OpenApiInvocationError, OpenApiParseError } from "./errors"; | |
| 34 | 34 | import { |
| 35 | 35 | buildInputSchema, |
| 36 | 36 | extract, |
| @@ -38,7 +38,13 @@ import { · 1 unmodified line | ||
| 38 | 38 | streamOperationBindingsFromStructure, |
| 39 | 39 | } from "./extract"; |
| 40 | 40 | import { compileToolDefinitions, type ToolDefinition } from "./definitions"; |
| 41 | −import { annotationsForOperation, buildRequest, invokeWithLayer, REQUIRE_APPROVAL } from "./invoke"; | |
| 41 | +import { | |
| 42 | + annotationsForOperation, | |
| 43 | + buildRequest, | |
| 44 | + invokeWithLayer, | |
| 45 | + REQUIRE_APPROVAL, | |
| 46 | + type InvokeOptions, | |
| 47 | +} from "./invoke"; | |
| 42 | 48 | import { parse, type ParsedDocument } from "./parse"; |
| 43 | 49 | import { parseEntry, structuralSplit, type KeepPathItem, type SpecStructure } from "./split"; |
| 44 | 50 | import { type OpenapiStore, type StoredOperation } from "./store"; |
| @@ -609,6 +615,7 @@ export const invokeOpenApiBackedTool = (input: { · 564 unmodified lines | ||
| 609 | 615 | readonly credential: ToolInvocationCredential; |
| 610 | 616 | readonly args: unknown; |
| 611 | 617 | readonly httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>; |
| 618 | + readonly invokeOptions?: InvokeOptions; | |
| 612 | 619 | }) => |
| 613 | 620 | Effect.gen(function* () { |
| 614 | 621 | const integration = input.toolRow.integration; |
| @@ -671,15 +678,33 @@ export const invokeOpenApiBackedTool = (input: { · 56 unmodified lines | ||
| 671 | 678 | Object.assign(queryParams, rendered.queryParams); |
| 672 | 679 | } |
| 673 | 680 | |
| 674 | − const result = yield* invokeWithLayer( | |
| 681 | + const invocation = yield* invokeWithLayer( | |
| 675 | 682 | binding, |
| 676 | 683 | (input.args ?? {}) as Record<string, unknown>, |
| 677 | 684 | config?.baseUrl ?? "", |
| 678 | 685 | headers, |
| 679 | 686 | queryParams, |
| 680 | 687 | input.httpClientLayer, |
| 688 | + input.invokeOptions, | |
| 689 | + ).pipe( | |
| 690 | + Effect.map((result) => ({ ok: true as const, result })), | |
| 691 | + Effect.catchTag("OpenApiInvocationError", (error: OpenApiInvocationError) => | |
| 692 | + error.message.startsWith("Upstream returned no response headers within ") | |
| 693 | + ? Effect.succeed({ | |
| 694 | + ok: false as const, | |
| 695 | + failure: ToolResult.fail({ | |
| 696 | + code: "upstream_http_error", | |
| 697 | + message: error.message, | |
| 698 | + details: error.cause ?? error, | |
| 699 | + }), | |
| 700 | + }) | |
| 701 | + : Effect.fail(error), | |
| 702 | + ), | |
| 681 | 703 | ); |
| 682 | 704 | |
| 705 | + if (!invocation.ok) return invocation.failure; | |
| 706 | + | |
| 707 | + const result = invocation.result; | |
| 683 | 708 | const ok = result.status >= 200 && result.status < 300; |
| 684 | 709 | if (!ok) { |
| 685 | 710 | if (result.status === 401 || result.status === 403) { |
| @@ -12,7 +12,14 @@ export { · 11 unmodified lines | ||
| 12 | 12 | type ByteRange, |
| 13 | 13 | type KeepPathItem, |
| 14 | 14 | } from "./split"; |
| 15 | −export { invoke, invokeWithLayer, buildRequest, annotationsForOperation } from "./invoke"; | |
| 15 | +export { | |
| 16 | + invoke, | |
| 17 | + invokeWithLayer, | |
| 18 | + buildRequest, | |
| 19 | + annotationsForOperation, | |
| 20 | + RESPONSE_HEADERS_TIMEOUT_MS, | |
| 21 | + type InvokeOptions, | |
| 22 | +} from "./invoke"; | |
| 16 | 23 | export { |
| 17 | 24 | buildDefsJsonStreaming, |
| 18 | 25 | checkHealthOpenApi, |
| @@ -1,5 +1,5 @@ | ||
| 1 | −import { Effect, Exit, Layer, Option, Schema, Stream } from "effect"; | |
| 2 | −import { HttpClient, HttpClientRequest } from "effect/unstable/http"; | |
| 1 | +import { Effect, Exit, Fiber, Layer, Option, Schema, Stream } from "effect"; | |
| 2 | +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; | |
| 3 | 3 | import type { ToolFileValue } from "@executor-js/sdk/core"; |
| 4 | 4 | |
| 5 | 5 | import { OpenApiInvocationError } from "./errors"; |
| @@ -153,6 +153,7 @@ const normalizeContentType = (ct: string | null | undefined): string => · 147 unmodified lines | ||
| 153 | 153 | |
| 154 | 154 | export const STREAM_MAX_BYTES = 1_000_000; |
| 155 | 155 | export const STREAM_MAX_MS = 10_000; |
| 156 | +export const RESPONSE_HEADERS_TIMEOUT_MS = 60_000; | |
| 156 | 157 | |
| 157 | 158 | class StreamCapReached extends Error { |
| 158 | 159 | readonly _tag = "StreamCapReached"; |
| @@ -163,6 +164,16 @@ export interface StreamingResponseCaps { · 4 unmodified lines | ||
| 163 | 164 | readonly maxMs?: number; |
| 164 | 165 | } |
| 165 | 166 | |
| 167 | +export interface InvokeOptions { | |
| 168 | + readonly responseHeadersTimeoutMs?: number; | |
| 169 | +} | |
| 170 | + | |
| 171 | +const formatTimeout = (timeoutMs: number): string => | |
| 172 | + timeoutMs % 1_000 === 0 ? `${timeoutMs / 1_000}s` : `${timeoutMs}ms`; | |
| 173 | + | |
| 174 | +const responseHeadersTimeoutMessage = (timeoutMs: number): string => | |
| 175 | + `Upstream returned no response headers within ${formatTimeout(timeoutMs)}. The endpoint may be a live stream with no data to send (for example, runtime logs of an idle deployment); the request was aborted. Retry when the resource has activity, or use a non-streaming endpoint.`; | |
| 176 | + | |
| 166 | 177 | const STREAMING_RESPONSE_CONTENT_TYPES = new Set([ |
| 167 | 178 | "application/stream+json", |
| 168 | 179 | "application/x-ndjson", |
| @@ -260,39 +271,71 @@ export const collectStreamingBody = ( · 91 unmodified lines | ||
| 260 | 271 | let truncated = false; |
| 261 | 272 | const startedAt = Date.now(); |
| 262 | 273 | |
| 263 | − const completed = yield* stream.pipe( | |
| 264 | − Stream.timeout(maxMs), | |
| 265 | − Stream.runForEach((chunk) => | |
| 266 | − Effect.gen(function* () { | |
| 267 | − const remaining = maxBytes - bytes; | |
| 268 | − if (remaining <= 0) { | |
| 269 | − truncated = true; | |
| 270 | − return yield* Effect.fail(new StreamCapReached()); | |
| 271 | − } | |
| 272 | − | |
| 273 | − if (chunk.byteLength > remaining) { | |
| 274 | − chunks.push(chunk.subarray(0, remaining)); | |
| 275 | − bytes += remaining; | |
| 276 | − truncated = true; | |
| 277 | − return yield* Effect.fail(new StreamCapReached()); | |
| 278 | − } | |
| 279 | − | |
| 280 | − chunks.push(chunk); | |
| 281 | − bytes += chunk.byteLength; | |
| 282 | − if (bytes >= maxBytes) { | |
| 283 | − truncated = true; | |
| 284 | − return yield* Effect.fail(new StreamCapReached()); | |
| 285 | − } | |
| 286 | − }), | |
| 287 | − ), | |
| 288 | − Effect.timeoutOption(maxMs + 1_000), | |
| 289 | − Effect.catch((error: unknown) => | |
| 290 | − error instanceof StreamCapReached | |
| 291 | − ? Effect.succeed(Option.some(undefined)) | |
| 292 | − : Effect.fail(error), | |
| 293 | − ), | |
| 274 | + const runFork = Effect.runForkWith(yield* Effect.context<never>()); | |
| 275 | + const completedExitOption = yield* Effect.callback<Option.Option<Exit.Exit<void, unknown>>>( | |
| 276 | + (resume, signal) => { | |
| 277 | + let settled = false; | |
| 278 | + const collectEffect = stream.pipe( | |
| 279 | + Stream.timeout(maxMs), | |
| 280 | + Stream.runForEach((chunk) => | |
| 281 | + Effect.gen(function* () { | |
| 282 | + const remaining = maxBytes - bytes; | |
| 283 | + if (remaining <= 0) { | |
| 284 | + truncated = true; | |
| 285 | + return yield* Effect.fail(new StreamCapReached()); | |
| 286 | + } | |
| 287 | + | |
| 288 | + if (chunk.byteLength > remaining) { | |
| 289 | + chunks.push(chunk.subarray(0, remaining)); | |
| 290 | + bytes += remaining; | |
| 291 | + truncated = true; | |
| 292 | + return yield* Effect.fail(new StreamCapReached()); | |
| 293 | + } | |
| 294 | + | |
| 295 | + chunks.push(chunk); | |
| 296 | + bytes += chunk.byteLength; | |
| 297 | + if (bytes >= maxBytes) { | |
| 298 | + truncated = true; | |
| 299 | + return yield* Effect.fail(new StreamCapReached()); | |
| 300 | + } | |
| 301 | + }), | |
| 302 | + ), | |
| 303 | + Effect.catch((error: unknown) => | |
| 304 | + error instanceof StreamCapReached ? Effect.void : Effect.fail(error), | |
| 305 | + ), | |
| 306 | + Effect.exit, | |
| 307 | + Effect.tap((exit) => | |
| 308 | + Effect.sync(() => { | |
| 309 | + if (settled) return; | |
| 310 | + settled = true; | |
| 311 | + clearTimeout(timer); | |
| 312 | + resume(Effect.succeed(Option.some(exit))); | |
| 313 | + }), | |
| 314 | + ), | |
| 315 | + ); | |
| 316 | + const fiber = runFork(collectEffect); | |
| 317 | + const interrupt = () => { | |
| 318 | + runFork(Fiber.interrupt(fiber)); | |
| 319 | + }; | |
| 320 | + const timer = setTimeout(() => { | |
| 321 | + if (settled) return; | |
| 322 | + settled = true; | |
| 323 | + truncated = true; | |
| 324 | + interrupt(); | |
| 325 | + resume(Effect.succeed(Option.none())); | |
| 326 | + }, maxMs + 1_000); | |
| 327 | + signal.addEventListener("abort", interrupt, { once: true }); | |
| 328 | + return Effect.sync(() => { | |
| 329 | + clearTimeout(timer); | |
| 330 | + signal.removeEventListener("abort", interrupt); | |
| 331 | + if (!settled) interrupt(); | |
| 332 | + }); | |
| 333 | + }, | |
| 294 | 334 | ); |
| 295 | − if (Option.isNone(completed)) truncated = true; | |
| 335 | + if (Option.isSome(completedExitOption) && Exit.isFailure(completedExitOption.value)) { | |
| 336 | + return yield* Effect.failCause(completedExitOption.value.cause); | |
| 337 | + } | |
| 338 | + if (Option.isNone(completedExitOption)) truncated = true; | |
| 296 | 339 | |
| 297 | 340 | const durationMs = Date.now() - startedAt; |
| 298 | 341 | if (durationMs >= maxMs) truncated = true; |
| @@ -952,6 +995,7 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 653 unmodified lines | ||
| 952 | 995 | args: Record<string, unknown>, |
| 953 | 996 | resolvedHeaders: Record<string, string>, |
| 954 | 997 | sourceQueryParams: Record<string, string> = {}, |
| 998 | + options: InvokeOptions = {}, | |
| 955 | 999 | ) { |
| 956 | 1000 | const client = yield* HttpClient.HttpClient; |
| 957 | 1001 | |
| @@ -965,16 +1009,57 @@ export const invoke = Effect.fn("OpenApi.invoke")(function* ( · 7 unmodified lines | ||
| 965 | 1009 | |
| 966 | 1010 | const request = yield* buildRequest(operation, args, resolvedHeaders, sourceQueryParams); |
| 967 | 1011 | |
| 968 | − const response = yield* client.execute(request).pipe( | |
| 969 | − Effect.mapError( | |
| 970 | − (err) => | |
| 971 | − new OpenApiInvocationError({ | |
| 972 | − message: "HTTP request failed", | |
| 973 | − statusCode: Option.none(), | |
| 974 | − cause: err, | |
| 1012 | + const responseHeadersTimeoutMs = options.responseHeadersTimeoutMs ?? RESPONSE_HEADERS_TIMEOUT_MS; | |
| 1013 | + const runFork = Effect.runForkWith(yield* Effect.context<never>()); | |
| 1014 | + const responseExitOption = yield* Effect.callback< | |
| 1015 | + Option.Option<Exit.Exit<HttpClientResponse.HttpClientResponse, OpenApiInvocationError>> | |
| 1016 | + >((resume, signal) => { | |
| 1017 | + let settled = false; | |
| 1018 | + const responseEffect = client.execute(request).pipe( | |
| 1019 | + Effect.mapError( | |
| 1020 | + (err) => | |
| 1021 | + new OpenApiInvocationError({ | |
| 1022 | + message: "HTTP request failed", | |
| 1023 | + statusCode: Option.none(), | |
| 1024 | + cause: err, | |
| 1025 | + }), | |
| 1026 | + ), | |
| 1027 | + Effect.exit, | |
| 1028 | + Effect.tap((exit) => | |
| 1029 | + Effect.sync(() => { | |
| 1030 | + if (settled) return; | |
| 1031 | + settled = true; | |
| 1032 | + clearTimeout(timer); | |
| 1033 | + resume(Effect.succeed(Option.some(exit))); | |
| 975 | 1034 | }), |
| 976 | − ), | |
| 977 | − ); | |
| 1035 | + ), | |
| 1036 | + ); | |
| 1037 | + const fiber = runFork(responseEffect); | |
| 1038 | + const interrupt = () => { | |
| 1039 | + runFork(Fiber.interrupt(fiber)); | |
| 1040 | + }; | |
| 1041 | + const timer = setTimeout(() => { | |
| 1042 | + if (settled) return; | |
| 1043 | + settled = true; | |
| 1044 | + interrupt(); | |
| 1045 | + resume(Effect.succeed(Option.none())); | |
| 1046 | + }, responseHeadersTimeoutMs); | |
| 1047 | + signal.addEventListener("abort", interrupt, { once: true }); | |
| 1048 | + return Effect.sync(() => { | |
| 1049 | + clearTimeout(timer); | |
| 1050 | + signal.removeEventListener("abort", interrupt); | |
| 1051 | + if (!settled) interrupt(); | |
| 1052 | + }); | |
| 1053 | + }); | |
| 1054 | + if (Option.isNone(responseExitOption)) { | |
| 1055 | + return yield* new OpenApiInvocationError({ | |
| 1056 | + message: responseHeadersTimeoutMessage(responseHeadersTimeoutMs), | |
| 1057 | + statusCode: Option.none(), | |
| 1058 | + }); | |
| 1059 | + } | |
| 1060 | + const responseExit = responseExitOption.value; | |
| 1061 | + if (Exit.isFailure(responseExit)) return yield* Effect.failCause(responseExit.cause); | |
| 1062 | + const response = responseExit.value; | |
| 978 | 1063 | |
| 979 | 1064 | const status = response.status; |
| 980 | 1065 | yield* Effect.annotateCurrentSpan({ |
| @@ -1073,6 +1158,7 @@ export const invokeWithLayer = ( · 92 unmodified lines | ||
| 1073 | 1158 | resolvedHeaders: Record<string, string>, |
| 1074 | 1159 | sourceQueryParams: Record<string, string>, |
| 1075 | 1160 | httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>, |
| 1161 | + options: InvokeOptions = {}, | |
| 1076 | 1162 | ) => { |
| 1077 | 1163 | const effectiveBaseUrl = resolveRequestHost(operation.servers ?? [], args.server, baseUrl); |
| 1078 | 1164 | const clientWithBaseUrl = effectiveBaseUrl |
| @@ -1085,7 +1171,7 @@ export const invokeWithLayer = ( · 6 unmodified lines | ||
| 1085 | 1171 | ).pipe(Layer.provide(httpClientLayer)) |
| 1086 | 1172 | : httpClientLayer; |
| 1087 | 1173 | |
| 1088 | − return invoke(operation, args, resolvedHeaders, sourceQueryParams).pipe( | |
| 1174 | + return invoke(operation, args, resolvedHeaders, sourceQueryParams, options).pipe( | |
| 1089 | 1175 | Effect.provide(clientWithBaseUrl), |
| 1090 | 1176 | // `invoke` annotates http.status_code on ITS span (`OpenApi.invoke`, |
| 1091 | 1177 | // via Effect.fn) — annotateCurrentSpan inside it never reaches this |
| @@ -48,6 +48,7 @@ import { · 47 unmodified lines | ||
| 48 | 48 | resolveOpenApiBackedTools, |
| 49 | 49 | validateOpenApiBackedToolArgs, |
| 50 | 50 | } from "./backing"; |
| 51 | +import type { InvokeOptions } from "./invoke"; | |
| 51 | 52 | import { resolveServerUrl } from "./openapi-utils"; |
| 52 | 53 | |
| 53 | 54 | // --------------------------------------------------------------------------- |
| @@ -563,6 +564,7 @@ export const describeOpenApiIntegrationDisplay = ( · 509 unmodified lines | ||
| 563 | 564 | |
| 564 | 565 | export interface OpenApiPluginOptions { |
| 565 | 566 | readonly httpClientLayer?: Layer.Layer<HttpClient.HttpClient, never, never>; |
| 567 | + readonly invokeOptions?: InvokeOptions; | |
| 566 | 568 | } |
| 567 | 569 | |
| 568 | 570 | export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { |
| @@ -1044,6 +1046,7 @@ export const openApiPlugin = definePlugin((options?: OpenApiPluginOptions) => { · 475 unmodified lines | ||
| 1044 | 1046 | credential, |
| 1045 | 1047 | args, |
| 1046 | 1048 | httpClientLayer, |
| 1049 | + invokeOptions: options?.invokeOptions, | |
| 1047 | 1050 | }); |
| 1048 | 1051 | }, |
| 1049 | 1052 | |
| @@ -0,0 +1,207 @@ | ||
| 1 | +import { describe, expect, it } from "@effect/vitest"; | |
| 2 | +import { Deferred, Effect, Option, Schema } from "effect"; | |
| 3 | +import { FetchHttpClient } from "effect/unstable/http"; | |
| 4 | +import { HttpApi, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"; | |
| 5 | +import { createServer, type ServerResponse } from "node:http"; | |
| 6 | +import type { AddressInfo, Socket } from "node:net"; | |
| 7 | + | |
| 8 | +import { | |
| 9 | + createExecutor, | |
| 10 | + AuthTemplateSlug, | |
| 11 | + ConnectionName, | |
| 12 | + IntegrationSlug, | |
| 13 | + ToolAddress, | |
| 14 | +} from "@executor-js/sdk"; | |
| 15 | +import { makeTestConfig, memoryCredentialsPlugin } from "@executor-js/sdk/testing"; | |
| 16 | +import { makeOpenApiHttpApiTestSourceConfig, unwrapInvocation } from "../testing"; | |
| 17 | + | |
| 18 | +import { invokeWithLayer } from "./invoke"; | |
| 19 | +import { openApiPlugin } from "./plugin"; | |
| 20 | +import type { OperationBinding } from "./types"; | |
| 21 | + | |
| 22 | +const RESPONSE_HEADERS_TIMEOUT_MS = 100; | |
| 23 | +const STREAM_TOOL = "logs.getLogs"; | |
| 24 | +const encoder = new TextEncoder(); | |
| 25 | + | |
| 26 | +const LogsGroup = HttpApiGroup.make("logs").add( | |
| 27 | + HttpApiEndpoint.get("getLogs", "/logs", { | |
| 28 | + success: Schema.Unknown, | |
| 29 | + }), | |
| 30 | +); | |
| 31 | + | |
| 32 | +const TimeoutApi = HttpApi.make("responseHeadersTimeoutTest") | |
| 33 | + .add(LogsGroup) | |
| 34 | + .annotateMerge(OpenApi.annotations({ title: "ResponseHeadersTimeoutTest", version: "1.0.0" })); | |
| 35 | + | |
| 36 | +const testPlugins = () => | |
| 37 | + [ | |
| 38 | + openApiPlugin({ | |
| 39 | + httpClientLayer: FetchHttpClient.layer, | |
| 40 | + invokeOptions: { responseHeadersTimeoutMs: RESPONSE_HEADERS_TIMEOUT_MS }, | |
| 41 | + }), | |
| 42 | + memoryCredentialsPlugin(), | |
| 43 | + ] as const; | |
| 44 | + | |
| 45 | +const buildExecutor = (baseUrl: string) => | |
| 46 | + Effect.gen(function* () { | |
| 47 | + const executor = yield* createExecutor(makeTestConfig({ plugins: testPlugins() })); | |
| 48 | + yield* executor.openapi.addSpec( | |
| 49 | + makeOpenApiHttpApiTestSourceConfig(TimeoutApi, { slug: "headers_timeout", baseUrl }), | |
| 50 | + ); | |
| 51 | + yield* executor.connections.create({ | |
| 52 | + owner: "org", | |
| 53 | + name: ConnectionName.make("main"), | |
| 54 | + integration: IntegrationSlug.make("headers_timeout"), | |
| 55 | + template: AuthTemplateSlug.make("apiKey"), | |
| 56 | + value: "token", | |
| 57 | + }); | |
| 58 | + return { | |
| 59 | + executor, | |
| 60 | + address: ToolAddress.make(`tools.headers_timeout.org.main.${STREAM_TOOL}`), | |
| 61 | + }; | |
| 62 | + }); | |
| 63 | + | |
| 64 | +const startRawServer = (handler: (res: ServerResponse) => void) => | |
| 65 | + Effect.acquireRelease( | |
| 66 | + Effect.callback<{ baseUrl: string; close: () => void }>((resume) => { | |
| 67 | + const sockets = new Set<Socket>(); | |
| 68 | + const timers = new Set<ReturnType<typeof setTimeout>>(); | |
| 69 | + const server = createServer((req, res) => { | |
| 70 | + if (req.url?.split("?")[0] !== "/logs") { | |
| 71 | + res.writeHead(200, { "content-type": "application/json" }); | |
| 72 | + res.end(JSON.stringify({ ok: true })); | |
| 73 | + return; | |
| 74 | + } | |
| 75 | + handler(res); | |
| 76 | + }); | |
| 77 | + server.on("connection", (socket) => { | |
| 78 | + sockets.add(socket); | |
| 79 | + socket.on("close", () => sockets.delete(socket)); | |
| 80 | + }); | |
| 81 | + server.listen(0, "127.0.0.1", () => { | |
| 82 | + const port = (server.address() as AddressInfo).port; | |
| 83 | + resume( | |
| 84 | + Effect.succeed({ | |
| 85 | + baseUrl: `http://127.0.0.1:${port}`, | |
| 86 | + close: () => { | |
| 87 | + for (const timer of timers) clearTimeout(timer); | |
| 88 | + for (const socket of sockets) socket.destroy(); | |
| 89 | + server.close(); | |
| 90 | + }, | |
| 91 | + }), | |
| 92 | + ); | |
| 93 | + }); | |
| 94 | + }), | |
| 95 | + (server) => Effect.sync(() => server.close()), | |
| 96 | + ); | |
| 97 | + | |
| 98 | +const startHeaderlessServer = (closed: Deferred.Deferred<void>) => | |
| 99 | + Effect.acquireRelease( | |
| 100 | + Effect.callback<{ baseUrl: string; close: () => void }>((resume) => { | |
| 101 | + const sockets = new Set<Socket>(); | |
| 102 | + const server = createServer((req, res) => { | |
| 103 | + if (req.url?.split("?")[0] === "/logs") return; | |
| 104 | + res.writeHead(200, { "content-type": "application/json" }); | |
| 105 | + res.end(JSON.stringify({ ok: true })); | |
| 106 | + }); | |
| 107 | + server.on("connection", (socket) => { | |
| 108 | + sockets.add(socket); | |
| 109 | + socket.on("close", () => { | |
| 110 | + sockets.delete(socket); | |
| 111 | + Effect.runFork(Deferred.succeed(closed, undefined)); | |
| 112 | + }); | |
| 113 | + }); | |
| 114 | + server.listen(0, "127.0.0.1", () => { | |
| 115 | + const port = (server.address() as AddressInfo).port; | |
| 116 | + resume( | |
| 117 | + Effect.succeed({ | |
| 118 | + baseUrl: `http://127.0.0.1:${port}`, | |
| 119 | + close: () => { | |
| 120 | + for (const socket of sockets) socket.destroy(); | |
| 121 | + server.close(); | |
| 122 | + }, | |
| 123 | + }), | |
| 124 | + ); | |
| 125 | + }); | |
| 126 | + }), | |
| 127 | + (server) => Effect.sync(() => server.close()), | |
| 128 | + ); | |
| 129 | + | |
| 130 | +const operation: OperationBinding = { | |
| 131 | + method: "get", | |
| 132 | + pathTemplate: "/logs", | |
| 133 | + parameters: [], | |
| 134 | + requestBody: Option.none(), | |
| 135 | + responseBody: Option.none(), | |
| 136 | + servers: [], | |
| 137 | +}; | |
| 138 | + | |
| 139 | +describe("OpenAPI response headers timeout", () => { | |
| 140 | + it.effect("aborts a headerless upstream and returns an actionable tool failure", () => | |
| 141 | + Effect.gen(function* () { | |
| 142 | + const closed = yield* Deferred.make<void>(); | |
| 143 | + const server = yield* startHeaderlessServer(closed); | |
| 144 | + const { executor, address } = yield* buildExecutor(server.baseUrl); | |
| 145 | + const startedAt = Date.now(); | |
| 146 | + | |
| 147 | + const result = yield* executor.execute(address, {}); | |
| 148 | + const elapsedMs = Date.now() - startedAt; | |
| 149 | + const socketClosed = yield* Deferred.await(closed).pipe(Effect.timeoutOption(1_000)); | |
| 150 | + | |
| 151 | + expect(elapsedMs).toBeGreaterThanOrEqual(RESPONSE_HEADERS_TIMEOUT_MS - 25); | |
| 152 | + expect(elapsedMs).toBeLessThan(2_000); | |
| 153 | + expect(Option.isSome(socketClosed)).toBe(true); | |
| 154 | + expect(result).toMatchObject({ | |
| 155 | + ok: false, | |
| 156 | + error: { | |
| 157 | + code: "upstream_http_error", | |
| 158 | + message: expect.stringContaining("Upstream returned no response headers within 100ms"), | |
| 159 | + }, | |
| 160 | + }); | |
| 161 | + }), | |
| 162 | + ); | |
| 163 | + | |
| 164 | + it.effect("leaves normal fast JSON responses unaffected", () => | |
| 165 | + Effect.gen(function* () { | |
| 166 | + const server = yield* startRawServer((res) => { | |
| 167 | + res.writeHead(200, { "content-type": "application/json" }); | |
| 168 | + res.end(JSON.stringify({ ok: true })); | |
| 169 | + }); | |
| 170 | + const { executor, address } = yield* buildExecutor(server.baseUrl); | |
| 171 | + | |
| 172 | + const result = unwrapInvocation<Record<string, unknown>>( | |
| 173 | + yield* executor.execute(address, {}), | |
| 174 | + ); | |
| 175 | + | |
| 176 | + expect(result.status).toBe(200); | |
| 177 | + expect(result.data).toEqual({ ok: true }); | |
| 178 | + }), | |
| 179 | + ); | |
| 180 | + | |
| 181 | + it.effect("does not apply the headers timeout after streaming response headers arrive", () => | |
| 182 | + Effect.gen(function* () { | |
| 183 | + const server = yield* startRawServer((res) => { | |
| 184 | + res.writeHead(200, { "content-type": "application/jsonl" }); | |
| 185 | + res.write(encoder.encode(`${JSON.stringify({ id: 1 })}\n`)); | |
| 186 | + setTimeout(() => { | |
| 187 | + res.write(encoder.encode(`${JSON.stringify({ id: 2 })}\n`)); | |
| 188 | + res.end(); | |
| 189 | + }, RESPONSE_HEADERS_TIMEOUT_MS + 100); | |
| 190 | + }); | |
| 191 | + | |
| 192 | + const result = yield* invokeWithLayer( | |
| 193 | + operation, | |
| 194 | + {}, | |
| 195 | + server.baseUrl, | |
| 196 | + {}, | |
| 197 | + {}, | |
| 198 | + FetchHttpClient.layer, | |
| 199 | + { responseHeadersTimeoutMs: RESPONSE_HEADERS_TIMEOUT_MS }, | |
| 200 | + ); | |
| 201 | + | |
| 202 | + expect(result.status).toBe(200); | |
| 203 | + expect(result.data).toEqual([{ id: 1 }, { id: 2 }]); | |
| 204 | + expect(result.headers["x-executor-stream"]).toBe("complete"); | |
| 205 | + }), | |
| 206 | + ); | |
| 207 | +}); | |