diff options
author | 2023-05-22 11:46:28 -0700 | |
---|---|---|
committer | 2023-05-22 11:46:39 -0700 | |
commit | a5acf7bfa0333fc5cb298df2b3ea09eaa56075ac (patch) | |
tree | 94277814b5229d03f10a1e2dd7d2541265482b58 | |
parent | 3de350b24dbae7512b5afd71c1e566137390bf08 (diff) | |
download | bun-a5acf7bfa0333fc5cb298df2b3ea09eaa56075ac.tar.gz bun-a5acf7bfa0333fc5cb298df2b3ea09eaa56075ac.tar.zst bun-a5acf7bfa0333fc5cb298df2b3ea09eaa56075ac.zip |
[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.
-rw-r--r-- | packages/bun-types/bun.d.ts | 4 | ||||
-rw-r--r-- | src/bun.js/api/server.zig | 10 | ||||
-rw-r--r-- | test/js/bun/websocket/websocket-server.test.ts | 15 |
3 files changed, 15 insertions, 14 deletions
diff --git a/packages/bun-types/bun.d.ts b/packages/bun-types/bun.d.ts index d06a7235e..5558e2db7 100644 --- a/packages/bun-types/bun.d.ts +++ b/packages/bun-types/bun.d.ts @@ -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"; } diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 59410d0cb..2ce7347c0 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -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,17 +3355,17 @@ pub const ServerWebSocket = struct { str.markUTF8(); break :brk str.toValueGC(globalObject); }, - .binary => if (this.binary_type == .Uint8Array) + .binary => if (this.binary_type == .Buffer) JSC.ArrayBuffer.create( globalObject, message, - .Uint8Array, + .Buffer, ) - else if (this.binary_type == .Buffer) + else if (this.binary_type == .Uint8Array) JSC.ArrayBuffer.create( globalObject, message, - .Buffer, + .Uint8Array, ) else JSC.ArrayBuffer.create( diff --git a/test/js/bun/websocket/websocket-server.test.ts b/test/js/bun/websocket/websocket-server.test.ts index 014178b84..3868d0267 100644 --- a/test/js/bun/websocket/websocket-server.test.ts +++ b/test/js/bun/websocket/websocket-server.test.ts @@ -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; } |