mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
[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:
4
packages/bun-types/bun.d.ts
vendored
4
packages/bun-types/bun.d.ts
vendored
@@ -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";
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user