Skip to content

Commit 208a572

Browse files
committed
truncate unexpected response from server if it is too long
1 parent 1986be9 commit 208a572

4 files changed

Lines changed: 20 additions & 13 deletions

File tree

src/sender.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ class Sender {
174174
async close() {
175175
const pos = this.buffer.currentPosition();
176176
if (pos > 0) {
177-
this.log("warn", `Buffer contains data which has not been flushed, and it will be lost [position=${pos}]`);
177+
this.log(
178+
"warn",
179+
`Buffer contains data which has not been flushed, and it will be lost [position=${pos}]`,
180+
);
178181
}
179182
return this.transport.close();
180183
}

src/transport/http/stdlib.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ class HttpTransport extends HttpTransportBase {
9292
if (statusCode === HTTP_NO_CONTENT) {
9393
response.on("end", () => {
9494
if (body.length > 0) {
95-
this.log(
96-
"warn",
97-
`Unexpected message from server: ${Buffer.concat(body)}`,
98-
);
95+
const message = Buffer.concat(body).toString();
96+
const logMessage =
97+
message.length < 256
98+
? message
99+
: `${message.substring(0, 256)}... (truncated, full length=${message.length})`;
100+
this.log("warn", `Unexpected message from server: ${logMessage}`);
99101
}
100102
resolve(true);
101103
});

src/transport/http/undici.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ class UndiciTransport extends HttpTransportBase {
139139
const body = await responseData.body.arrayBuffer();
140140
if (statusCode === HTTP_NO_CONTENT) {
141141
if (body.byteLength > 0) {
142-
this.log(
143-
"warn",
144-
`Unexpected message from server: ${Buffer.from(body).toString()}`,
145-
);
142+
const message = Buffer.from(body).toString();
143+
const logMessage =
144+
message.length < 256
145+
? message
146+
: `${message.substring(0, 256)}... (truncated, full length=${message.length})`;
147+
this.log("warn", `Unexpected message from server: ${logMessage}`);
146148
}
147149
return true;
148150
} else {

test/sender.transport.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ describe("Sender TCP suite", function () {
598598

599599
// add another line to the buffer without calling flush()
600600
await sender
601-
.table("test")
602-
.symbol("location", "gb")
603-
.floatColumn("temperature", 16.4)
604-
.at(1658484775000000000n, "ns");
601+
.table("test")
602+
.symbol("location", "gb")
603+
.floatColumn("temperature", 16.4)
604+
.at(1658484775000000000n, "ns");
605605

606606
// assert that only the first line was sent
607607
await assertSentData(

0 commit comments

Comments
 (0)