Skip to content

Commit d5430dc

Browse files
committed
fix(plugin-sdk): preserve HTTP body export boundaries
1 parent 3823042 commit d5430dc

6 files changed

Lines changed: 37 additions & 219 deletions

File tree

‎packages/media-core/src/read-response-with-limit.ts‎

Lines changed: 0 additions & 188 deletions
This file was deleted.

‎scripts/plugin-sdk-surface-report.mjs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ let publicDeprecatedExportsByEntrypointBudget;
202202
try {
203203
budgets = {
204204
publicEntrypoints: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_ENTRYPOINTS", 323),
205-
publicExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS", 10422),
206-
publicFunctionExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS", 5233),
205+
publicExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_EXPORTS", 10425),
206+
publicFunctionExports: readBudgetEnv("OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_FUNCTION_EXPORTS", 5237),
207207
publicDeprecatedExports: readBudgetEnv(
208208
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_DEPRECATED_EXPORTS",
209209
3261,
210210
),
211211
publicWildcardReexports: readBudgetEnv(
212212
"OPENCLAW_PLUGIN_SDK_MAX_PUBLIC_WILDCARD_REEXPORTS",
213-
214,
213+
212,
214214
),
215215
};
216216
publicDeprecatedExportsByEntrypointBudget = readEntrypointBudgetEnv(

‎src/infra/http-body.ts‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Reads HTTP request and response bodies with timeout and byte limits.
22
import type { IncomingMessage, ServerResponse } from "node:http";
33
import { clearTimeout as clearNodeTimeout, setTimeout as setNodeTimeout } from "node:timers";
4+
import { toErrorObject } from "@openclaw/normalization-core/error-coercion";
45
import { resolveTimerTimeoutMs } from "@openclaw/normalization-core/number-coercion";
56
import { formatErrorMessage } from "./errors.js";
67
import { parseStrictNonNegativeInteger } from "./parse-finite-number.js";
@@ -126,7 +127,8 @@ function advanceRequestBodyChunk(
126127
};
127128
}
128129

129-
async function readResponseChunkWithIdleTimeout(
130+
/** Reads one chunk, rejecting and cancelling the reader after an idle timeout. */
131+
export async function readChunkWithIdleTimeout(
130132
reader: ReadableStreamDefaultReader<Uint8Array>,
131133
chunkTimeoutMs: number,
132134
onIdleTimeout?: (params: { chunkTimeoutMs: number }) => Error,
@@ -165,7 +167,7 @@ async function readResponseChunkWithIdleTimeout(
165167
(error: unknown) => {
166168
clear();
167169
if (!timedOut) {
168-
reject(toHttpBodyError(error, "Non-Error rejection"));
170+
reject(toErrorObject(error, "Non-Error rejection"));
169171
}
170172
},
171173
);
@@ -207,7 +209,7 @@ async function readResponsePrefix(
207209
try {
208210
while (true) {
209211
const { done, value } = options?.chunkTimeoutMs
210-
? await readResponseChunkWithIdleTimeout(
212+
? await readChunkWithIdleTimeout(
211213
reader,
212214
options.chunkTimeoutMs,
213215
options.onIdleTimeout,
@@ -313,20 +315,6 @@ export async function readResponseTextSnippet(
313315
return prefix.truncated ? `${collapsed}…` : collapsed;
314316
}
315317

316-
function toHttpBodyError(value: unknown, fallbackMessage: string): Error {
317-
if (value instanceof Error) {
318-
return value;
319-
}
320-
if (typeof value === "string") {
321-
return new Error(value);
322-
}
323-
const error = new Error(fallbackMessage, { cause: value });
324-
if ((typeof value === "object" && value !== null) || typeof value === "function") {
325-
Object.assign(error, value);
326-
}
327-
return error;
328-
}
329-
330318
export async function readRequestBodyWithLimit(
331319
req: IncomingMessage,
332320
options: ReadRequestBodyOptions,

‎src/media/fetch.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ import { MAX_DOCUMENT_BYTES } from "@openclaw/media-core/constants";
33
import { parseMediaContentLength } from "@openclaw/media-core/content-length";
44
import { basenameFromAnyPath, extnameFromAnyPath } from "@openclaw/media-core/file-name";
55
import { detectMime, extensionForMime } from "@openclaw/media-core/mime";
6-
<<<<<<< HEAD
7-
import { readChunkWithIdleTimeout } from "@openclaw/media-core/read-response-with-limit";
6+
import { isAbortError } from "../infra/abort-signal.js";
7+
import { formatErrorMessage } from "../infra/errors.js";
88
import {
9+
readChunkWithIdleTimeout,
910
readResponseTextSnippet,
1011
readResponseWithLimit,
11-
} from "@openclaw/media-core/read-response-with-limit";
12-
import { isAbortError } from "../infra/abort-signal.js";
13-
import { formatErrorMessage } from "../infra/errors.js";
14-
=======
15-
import { formatErrorMessage, toErrorObject } from "../infra/errors.js";
16-
import { readResponseTextSnippet, readResponseWithLimit } from "../infra/http-body.js";
17-
>>>>>>> 7b9c4eb3647 (refactor(infra): consolidate bounded HTTP body reads)
12+
} from "../infra/http-body.js";
1813
import {
1914
fetchWithSsrFGuard,
2015
withStrictGuardedFetchMode,

‎src/plugin-sdk/infra-runtime.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,26 @@ export * from "../infra/heartbeat-events.ts";
3636
export * from "../infra/heartbeat-summary.ts";
3737
export * from "../infra/heartbeat-visibility.ts";
3838
export * from "../infra/home-dir.js";
39-
export * from "../infra/http-body.js";
39+
// Keep this deprecated barrel pinned to its shipped request-body surface; new
40+
// response readers belong only to the focused response-limit/media entrypoints.
41+
export {
42+
__test__,
43+
DEFAULT_WEBHOOK_BODY_TIMEOUT_MS,
44+
DEFAULT_WEBHOOK_MAX_BODY_BYTES,
45+
installRequestBodyLimitGuard,
46+
isRequestBodyLimitError,
47+
readJsonBodyWithLimit,
48+
readRequestBodyWithLimit,
49+
requestBodyErrorToText,
50+
RequestBodyLimitError,
51+
testApi,
52+
type ReadJsonBodyOptions,
53+
type ReadJsonBodyResult,
54+
type ReadRequestBodyOptions,
55+
type RequestBodyLimitErrorCode,
56+
type RequestBodyLimitGuard,
57+
type RequestBodyLimitGuardOptions,
58+
} from "../infra/http-body.js";
4059
export * from "../infra/json-files.js";
4160
export * from "../infra/local-file-access.js";
4261
export * from "../infra/map-size.js";

‎src/plugin-sdk/media-runtime.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ export * from "../media/png-encode.ts";
5151
export * from "../media/qr-image.ts";
5252
export * from "../media/qr-terminal.ts";
5353
export * from "@openclaw/media-core/read-byte-stream-with-limit";
54-
export { readResponseTextSnippet, readResponseWithLimit } from "../infra/http-body.js";
54+
export {
55+
readChunkWithIdleTimeout,
56+
readResponseTextSnippet,
57+
readResponseWithLimit,
58+
} from "../infra/http-body.js";
5559
export * from "../media/store.js";
5660
export * from "../media/temp-files.js";
5761
export { resolveChannelMediaMaxBytes } from "../channels/plugins/media-limits.js";

0 commit comments

Comments
 (0)