[ServerWebSocket] binaryType now defaults to "nodebuffer"

Previously, this defaulted to "uint8array", so this shouldn't be a breaking change unless you make use of `.slice()` in which case it will now be a reference to the same ArrayBuffer rather than a clone.

The rationale for this change is most usages of Uint8Array on the server need a little more than just the bytes. Many npm packages expect Buffer rather than Uint8Array. Directly returning it for binary websocket messages is faster than creating another one.
This commit is contained in:
Jarred Sumner
2023-05-22 11:46:28 -07:00
parent 3de350b24d
commit a5acf7bfa0
3 changed files with 18 additions and 17 deletions

View File

@@ -1334,7 +1334,9 @@ declare module "bun" {
/**
* Configure the {@link WebSocketHandler.message} callback to return a {@link ArrayBuffer} or {@link Buffer} instead of a {@link Uint8Array}
*
* @default "uint8array"
* @default "nodebuffer"
*
* In Bun v0.6.2 and earlier, this defaulted to "uint8array"
*/
binaryType?: "arraybuffer" | "uint8array" | "nodebuffer";
}

View File

@@ -3256,7 +3256,7 @@ pub const ServerWebSocket = struct {
this_value: JSValue = .zero,
websocket: uws.AnyWebSocket = undefined,
closed: bool = false,
binary_type: JSC.BinaryType = .Uint8Array,
binary_type: JSC.BinaryType = .Buffer,
opened: bool = false,
pub usingnamespace JSC.Codegen.JSServerWebSocket;
@@ -3355,18 +3355,18 @@ pub const ServerWebSocket = struct {
str.markUTF8();
break :brk str.toValueGC(globalObject);
},
.binary => if (this.binary_type == .Uint8Array)
JSC.ArrayBuffer.create(
globalObject,
message,
.Uint8Array,
)
else if (this.binary_type == .Buffer)
.binary => if (this.binary_type == .Buffer)
JSC.ArrayBuffer.create(
globalObject,
message,
.Buffer,
)
else if (this.binary_type == .Uint8Array)
JSC.ArrayBuffer.create(
globalObject,
message,
.Uint8Array,
)
else
JSC.ArrayBuffer.create(
globalObject,

View File

@@ -171,7 +171,6 @@ describe("websocket server", () => {
it("close inside open", async () => {
var resolve: () => void;
console.trace("here");
var server = serve({
port: 0,
websocket: {
@@ -441,7 +440,7 @@ describe("websocket server", () => {
// Then after nodebuffer, we switch it to "arraybuffer"
// and then we're done
switch (ws.binaryType) {
case "uint8array": {
case "nodebuffer": {
for (let badType of [
123,
NaN,
@@ -457,20 +456,20 @@ describe("websocket server", () => {
ws.binaryType = badType;
}).toThrow();
}
expect(ws.binaryType).toBe("uint8array");
ws.binaryType = "nodebuffer";
expect(ws.binaryType).toBe("nodebuffer");
ws.binaryType = "uint8array";
expect(ws.binaryType).toBe("uint8array");
expect(msg instanceof Uint8Array).toBe(true);
expect(Buffer.isBuffer(msg)).toBe(false);
expect(Buffer.isBuffer(msg)).toBe(true);
break;
}
case "nodebuffer": {
expect(ws.binaryType).toBe("nodebuffer");
case "uint8array": {
expect(ws.binaryType).toBe("uint8array");
ws.binaryType = "arraybuffer";
expect(ws.binaryType).toBe("arraybuffer");
expect(msg instanceof Uint8Array).toBe(true);
expect(Buffer.isBuffer(msg)).toBe(true);
expect(Buffer.isBuffer(msg)).toBe(false);
break;
}