Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/core/src/browser/cdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

import { fstatSync } from 'node:fs';
import { SOCKET_OPTIONS } from '../client.ts';

type Listener = (params: Record<string, unknown>, sessionId: string | undefined) => void;
interface Waiting { resolve(value: Record<string, unknown>): void; reject(error: Error): void; timer: ReturnType<typeof setTimeout>; method: string }
Expand Down Expand Up @@ -95,7 +94,7 @@ export class Cdp {
/** The Windows transport: the browser's WebSocket endpoint on 127.0.0.1. */
static connect(url: string, timeoutMs = 10_000): Promise<Cdp> {
return new Promise((resolve, reject) => {
const socket = new WebSocket(url, SOCKET_OPTIONS as unknown as string[]);
const socket = new WebSocket(url);
const timer = setTimeout(() => { socket.close(); reject(new CdpError(`the browser did not accept a DevTools connection within ${timeoutMs / 1000} seconds`)); }, timeoutMs);
socket.addEventListener('error', () => { clearTimeout(timer); reject(new CdpError(`could not connect to the browser's DevTools at ${url}`)); }, { once: true });
socket.addEventListener('open', () => {
Expand Down
15 changes: 3 additions & 12 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,12 @@ export class SocketClosedError extends Error {
}
}

/**
* What every WebSocket this code opens with Bun's client passes: no
* `permessage-deflate` offer. With the extension on, Bun 1.4.2 and 1.4.3
* sometimes close a sound stream with 1002 ("control frame is fragmented",
* "unsupported control frame", "unexpected opcode"): against the core's own
* server and against Chrome's DevTools, whose bytes a relay checked frame by
* frame and found well-formed. Uncompressed, a message is larger on the wire;
* compressed, the connection drops.
*/
export const SOCKET_OPTIONS = { perMessageDeflate: false } as const;

export async function connect(url: string, token: string, options: ConnectOptions = {}): Promise<CoreClient> {
const timeoutMs = options.timeoutMs ?? 5000;
const deadline = Date.now() + timeoutMs;
const socket = new WebSocket(socketUrl(url), { ...SOCKET_OPTIONS, ...(options.headers === undefined ? {} : { headers: options.headers }) } as unknown as string[]);
const socket = options.headers === undefined
? new WebSocket(socketUrl(url))
: new WebSocket(socketUrl(url), { headers: options.headers } as unknown as string[]);
const pending = new Map<number, Pending>();
const listeners = new Map<string, Set<(payload: unknown) => void>>();
let nextId = 1;
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/group/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import type { ServerWebSocket } from 'bun';
import { FILE_ROUTE, GROUP_RELAY_ROUTE, PROTOCOL_VERSION, RPC_PATH, RpcErrorCode, VIEW_ROUTE } from '@boite/contracts';
import { SOCKET_OPTIONS } from '../client.ts';
import type { Core } from '../core.ts';
import { messageOf } from '../errors.ts';
import { OUTBOUND_CONGESTION_TIMEOUT_MS, OUTBOUND_MAX_BUFFERED_BYTES } from '../server/connection.ts';
Expand Down Expand Up @@ -389,7 +388,7 @@ export class RelayPipe {
const opened = await new Promise<Upstream | null>((resolve) => {
let socket: Upstream;
try {
socket = new WebSocket(`${target.replace(/^http/, 'ws')}${RPC_PATH}`, SOCKET_OPTIONS as unknown as string[]);
socket = new WebSocket(`${target.replace(/^http/, 'ws')}${RPC_PATH}`);
} catch {
resolve(null);
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/browser/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import type { RemoteBrowserFrame, RemoteBrowserUnchanged } from '@boite/contracts';
import { connect, SOCKET_OPTIONS, type CoreClient } from '../../src/client.ts';
import { connect, type CoreClient } from '../../src/client.ts';
import { chromiumArgs, findChromium, waitForEndpoint } from '../../src/browser/chromium.ts';
import { echoThread, startTestCore, type TestCore } from '../harness.ts';

Expand Down Expand Up @@ -148,7 +148,7 @@ export function createBrowserFixture() {
try {
const args = chromiumArgs(dir, 'linux').filter(arg => arg !== '--remote-debugging-pipe');
browser = Bun.spawn([findChromium().path!, ...args, '--remote-debugging-port=0', '--remote-debugging-address=127.0.0.1', ...extraArgs], { stdout: 'ignore', stderr: 'ignore' });
const connectedSocket = socket = new WebSocket(await waitForEndpoint(dir, browser.exited), SOCKET_OPTIONS as unknown as string[]);
const connectedSocket = socket = new WebSocket(await waitForEndpoint(dir, browser.exited));
await new Promise<void>((resolve, reject) => {
const finish = (error?: Error) => {
clearTimeout(timer);
Expand Down
28 changes: 0 additions & 28 deletions packages/core/test/socket-options.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tests/e2e/group-devices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { startUi } from './lib/ui.ts';
import { startCore, type RunningCore } from './lib/core.ts';
import { mobileAction } from './lib/mobile.ts';
import { codeVideo } from './lib/qr-video.ts';
import { connect, SOCKET_OPTIONS, type CoreClient } from '../../packages/core/src/client.ts';
import { connect, type CoreClient } from '../../packages/core/src/client.ts';

/**
* A phone is a device of the group, not of one machine.
Expand Down Expand Up @@ -71,7 +71,7 @@ function proxy(core: RunningCore): ReturnType<typeof Bun.serve> {
websocket: {
open(socket: ServerWebSocket<Piped>) {
const headers: Record<string, string> = { host: socket.data.host, ...(socket.data.origin === null ? {} : { origin: socket.data.origin }) };
const upstream = new WebSocket(`${core.url.replace(/^http/, 'ws')}${socket.data.path}`, { ...SOCKET_OPTIONS, headers } as unknown as string[]);
const upstream = new WebSocket(`${core.url.replace(/^http/, 'ws')}${socket.data.path}`, { headers } as unknown as string[]);
upstream.binaryType = 'arraybuffer';
socket.data.upstream = upstream;
upstream.onopen = () => {
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/lib/cdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { ONBOARDING_STORAGE_KEY, ONBOARDING_VERSION } from '../../../packages/ui/src/lib/onboarding.ts';
import type { Subprocess } from 'bun';
import { SOCKET_OPTIONS } from '../../../packages/core/src/client.ts';
import { E2E_DIR_PREFIX, killProcessTreeAsync, removeDirectory, watchParentDeath, type ParentDeathWatch } from './cleanup.ts';

/**
Expand Down Expand Up @@ -711,7 +710,7 @@ function openSocket(target: TargetInfo): Promise<WebSocket> {
const url = target.webSocketDebuggerUrl;
if (url === undefined) throw new Error('the page target has no debugger url');
return new Promise<WebSocket>((resolve, reject) => {
const socket = new WebSocket(url, SOCKET_OPTIONS as unknown as string[]);
const socket = new WebSocket(url);
// A socket that never opened is closed with its refusal: nothing else
// holds it, and a late open would keep its connection for the whole run.
const fail = (message: string) => {
Expand Down
Loading