From effb8f189b09ad55d65c6567cfd62b7a3b5fa05e Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 24 Jan 2024 19:40:59 -0800 Subject: [PATCH] windows: fix some websocket tests (#8433) * windows: fix some websocket tests * this file should work now, report any errors * make this change later * add back running this with node * add as const to these --------- Co-authored-by: Jarred Sumner --- scripts/codegen.ps1 | 2 +- src/bun.js/bindings/webcore/WebSocket.cpp | 1 + src/deps/uws.zig | 1 + src/http/websocket_http_client.zig | 21 ++++--------------- .../js/bun/websocket/websocket-server.test.ts | 14 ++++++------- test/js/first_party/ws/ws.test.ts | 6 +++--- 6 files changed, 17 insertions(+), 28 deletions(-) diff --git a/scripts/codegen.ps1 b/scripts/codegen.ps1 index 98027335b3..7c99d980de 100644 --- a/scripts/codegen.ps1 +++ b/scripts/codegen.ps1 @@ -14,4 +14,4 @@ Set-Content $Script2 -Force -NoNewline -Value $CreateHashTable Remove-Item -Path "build-release/codegen" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "build-release/js" -Recurse -Force -ErrorAction SilentlyContinue Copy-Item -Path "build/codegen" -Destination "build-release/codegen" -Recurse -Force -Copy-Item -Path "build/js" -Destination "build-release/js" -Recurse -Force \ No newline at end of file +Copy-Item -Path "build/js" -Destination "build-release/js" -Recurse -Force diff --git a/src/bun.js/bindings/webcore/WebSocket.cpp b/src/bun.js/bindings/webcore/WebSocket.cpp index ca5cf59e45..d571b2c508 100644 --- a/src/bun.js/bindings/webcore/WebSocket.cpp +++ b/src/bun.js/bindings/webcore/WebSocket.cpp @@ -503,6 +503,7 @@ ExceptionOr WebSocket::send(ArrayBuffer& binaryData) } char* data = static_cast(binaryData.data()); size_t length = binaryData.byteLength(); + this->sendWebSocketData(data, length, Opcode::Binary); return {}; diff --git a/src/deps/uws.zig b/src/deps/uws.zig index c8eb6aec5e..281ad7b948 100644 --- a/src/deps/uws.zig +++ b/src/deps/uws.zig @@ -2413,6 +2413,7 @@ pub const PING: i32 = 9; pub const PONG: i32 = 10; pub const Opcode = enum(i32) { + continuation = 0, text = 1, binary = 2, close = 8, diff --git a/src/http/websocket_http_client.zig b/src/http/websocket_http_client.zig index bcd57a34f2..06763e3388 100644 --- a/src/http/websocket_http_client.zig +++ b/src/http/websocket_http_client.zig @@ -214,18 +214,7 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { const HTTPClient = @This(); - pub fn register(global: *JSC.JSGlobalObject, loop_: *anyopaque, ctx_: *anyopaque) callconv(.C) void { - var vm = global.bunVM(); - const loop: *bun.Async.Loop = @alignCast(@ptrCast(loop_)); - const ctx: *uws.SocketContext = @as(*uws.SocketContext, @ptrCast(ctx_)); - - if (vm.event_loop_handle) |other| { - std.debug.assert(other == loop); - } - const is_new_loop = vm.event_loop_handle == null; - - vm.event_loop_handle = loop; - + pub fn register(_: *JSC.JSGlobalObject, _: *anyopaque, ctx: *uws.SocketContext) callconv(.C) void { Socket.configure( ctx, true, @@ -242,9 +231,6 @@ pub fn NewHTTPUpgradeClient(comptime ssl: bool) type { pub const onHandshake = handleHandshake; }, ); - if (is_new_loop) { - vm.prepareLoop(); - } } pub fn connect( @@ -1636,12 +1622,12 @@ pub fn NewWebSocketClient(comptime ssl: bool) type { len: usize, op: u8, ) callconv(.C) void { - if (this.tcp.isClosed() or this.tcp.isShutdown()) { + if (this.tcp.isClosed() or this.tcp.isShutdown() or op > 0xF) { this.dispatchAbruptClose(); return; } - const opcode = @as(Opcode, @enumFromInt(@as(u4, @truncate(op)))); + const opcode: Opcode = @enumFromInt(op); const slice = ptr[0..len]; const bytes = Copy{ .bytes = slice }; // fast path: small frame, no backpressure, attempt to send without allocating @@ -1655,6 +1641,7 @@ pub fn NewWebSocketClient(comptime ssl: bool) type { _ = this.sendData(bytes, !this.hasBackpressure(), opcode); } + pub fn writeString( this: *WebSocket, str_: *const JSC.ZigString, diff --git a/test/js/bun/websocket/websocket-server.test.ts b/test/js/bun/websocket/websocket-server.test.ts index 17bcc3815b..f8ebdd4d36 100644 --- a/test/js/bun/websocket/websocket-server.test.ts +++ b/test/js/bun/websocket/websocket-server.test.ts @@ -1,9 +1,10 @@ -// @known-failing-on-windows: 1 failing import { describe, it, expect, afterEach } from "bun:test"; import type { Server, Subprocess, WebSocketHandler } from "bun"; import { serve, spawn } from "bun"; import { bunEnv, bunExe, nodeExe } from "harness"; import { isIP } from "node:net"; +import path from "node:path"; + const strings = [ { label: "string (ascii)", @@ -20,7 +21,7 @@ const strings = [ message: "utf8-😶", bytes: [0x75, 0x74, 0x66, 0x38, 0x2d, 0xf0, 0x9f, 0x98, 0xb6], }, -]; +] as const; const buffers = [ { @@ -38,9 +39,9 @@ const buffers = [ message: Buffer.from("utf8-🤩"), bytes: [0x75, 0x74, 0x66, 0x38, 0x2d, 0xf0, 0x9f, 0xa4, 0xa9], }, -]; +] as const; -const messages = [...strings, ...buffers]; +const messages = [...strings, ...buffers] as const; const binaryTypes = [ { @@ -590,14 +591,13 @@ function test( async function connect(server: Server): Promise { const url = new URL(`ws://${server.hostname}:${server.port}/`); - const { pathname } = new URL("./websocket-client-echo.mjs", import.meta.url); + const pathname = path.resolve(import.meta.dir, "./websocket-client-echo.mjs"); // @ts-ignore const client = spawn({ cmd: [nodeExe() ?? bunExe(), pathname, url], cwd: import.meta.dir, env: bunEnv, - stderr: "ignore", - stdout: "pipe", + stdio: ["pipe", "pipe", "ignore"], }); clients.push(client); for await (const chunk of client.stdout) { diff --git a/test/js/first_party/ws/ws.test.ts b/test/js/first_party/ws/ws.test.ts index 683b9ab2e4..778c18f9e3 100644 --- a/test/js/first_party/ws/ws.test.ts +++ b/test/js/first_party/ws/ws.test.ts @@ -21,7 +21,7 @@ const strings = [ message: "utf8-😶", bytes: [0x75, 0x74, 0x66, 0x38, 0x2d, 0xf0, 0x9f, 0x98, 0xb6], }, -]; +] as const; const buffers = [ { @@ -39,9 +39,9 @@ const buffers = [ message: Buffer.from("utf8-🤩"), bytes: [0x75, 0x74, 0x66, 0x38, 0x2d, 0xf0, 0x9f, 0xa4, 0xa9], }, -]; +] as const; -const messages = [...strings, ...buffers]; +const messages = [...strings, ...buffers] as const; const binaryTypes = [ {