From e9b24b25896c26fd44a247163e1d88c67a5d6bc8 Mon Sep 17 00:00:00 2001 From: robinzhxie Date: Thu, 26 Oct 2023 19:44:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(incoming):=20=E5=88=B0=E8=BE=BE=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E9=99=90=E5=88=B6=E5=90=8E=EF=BC=8C=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=8C=85=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/runtime/capture/incoming.ts | 11 ++++++++++- lib/core/runtime/capture/outgoing.ts | 5 +++++ lib/core/runtime/create-server.hack.ts | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) 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";