Skip to content

Commit cd78d06

Browse files
authored
fix: stringify Uint8Arrays like Buffers (#1383)
1 parent 1764a8f commit cd78d06

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/lib/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { inclusionUserCallbacks } from "./inclusion_user_callbacks.js";
4747
import { MessageHandler } from "./message_handler.js";
4848
import { ConfigManagerMessageHandler } from "./config_manager/message_handler.js";
4949
import { ZnifferMessageHandler } from "./zniffer/message_handler.js";
50+
import { stringifyReplacer } from "../util/stringify.js";
5051

5152
function getVersionData(driver: Driver): {
5253
homeId: number | undefined;
@@ -318,7 +319,7 @@ export class Client {
318319
}
319320

320321
sendData(data: OutgoingMessages.OutgoingMessage, compress = false) {
321-
this.socket.send(JSON.stringify(data), { compress });
322+
this.socket.send(JSON.stringify(data, stringifyReplacer), { compress });
322323
}
323324

324325
checkAlive() {

src/util/stringify.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { isUint8Array } from "node:util/types";
2+
3+
export function stringifyReplacer(key: string, value: any): any {
4+
// Ensure that Uint8Arrays are serialized as if they were Buffers
5+
// to keep the API backwards compatible
6+
if (isUint8Array(value)) {
7+
return Buffer.from(value).toJSON();
8+
}
9+
return value;
10+
}

0 commit comments

Comments
 (0)