diff --git a/lib/core/runtime/capture/incoming.ts b/lib/core/runtime/capture/incoming.ts index f5542d99..fb153ea3 100644 --- a/lib/core/runtime/capture/incoming.ts +++ b/lib/core/runtime/capture/incoming.ts @@ -29,10 +29,19 @@ export const captureReadableStream = ( body: Buffer.alloc(0) }; + Object.defineProperty(info, "body", { + // 需要用的时候才拼接buffer + get: () => Buffer.concat(info.bodyChunks) + }); + const handler = (chunk: any): void => { info.bodyLength += Buffer.byteLength(chunk); + // 到达最大限制后,不再记录回包内容 + if (info.bodyTooLarge) { + return; + } + info.bodyChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); - info.body = Buffer.concat(info.bodyChunks); info.bodyTooLarge = info.bodyLength > maxBodySize; }; diff --git a/lib/core/runtime/capture/outgoing.ts b/lib/core/runtime/capture/outgoing.ts index dff1d7d9..96f23b23 100644 --- a/lib/core/runtime/capture/outgoing.ts +++ b/lib/core/runtime/capture/outgoing.ts @@ -30,6 +30,11 @@ export const captureOutgoing = (outgoing: http.OutgoingMessage): void => { callback = callbackOrUndefined; } + // 达到最大长度限制,不再收集包内容 + if (bodyLength > maxBodySize) { + return fn.apply(outgoing, [data, encoding, callback]); + } + const buffer = ((): Buffer => { if (Buffer.isBuffer(data)) { return data; diff --git a/lib/core/runtime/create-server.hack.ts b/lib/core/runtime/create-server.hack.ts index d496552e..7fd99402 100644 --- a/lib/core/runtime/create-server.hack.ts +++ b/lib/core/runtime/create-server.hack.ts @@ -9,7 +9,7 @@ import * as http from "http"; import * as https from "https"; import * as domain from "domain"; -import currentContext, { Context, RequestLog } from "../context"; +import { Context, RequestLog } from "../context"; import { address } from "ip"; import { AddressInfo, isIP } from "net"; import { captureOutgoing } from "./capture/outgoing";