From d8bd113946aa74f4ef553960346494fc1477cccb Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Thu, 19 Nov 2026 12:18:05 -0500 Subject: [PATCH 1/2] Keep the URI path when the TypeScript SDK connects Token and subscribe URLs now resolve under the connection URI's path, so ws://host/stdb reaches /stdb/v1/..., as the Rust SDK does. --- crates/bindings-typescript/src/sdk/ws.ts | 8 ++- .../bindings-typescript/tests/ws_url.test.ts | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 crates/bindings-typescript/tests/ws_url.test.ts diff --git a/crates/bindings-typescript/src/sdk/ws.ts b/crates/bindings-typescript/src/sdk/ws.ts index 5fef5173369..1359aa97e86 100644 --- a/crates/bindings-typescript/src/sdk/ws.ts +++ b/crates/bindings-typescript/src/sdk/ws.ts @@ -95,12 +95,16 @@ export async function openWebSocket({ const WS = await resolveWS(); + // Endpoints resolve under the URI's path, so `ws://host/stdb` reaches `/stdb/v1/...`. + const base = new URL(url.toString()); + if (!base.pathname.endsWith('/')) base.pathname += '/'; + // We swap our original token to a shorter-lived token // to avoid sending the original via query params. let temporaryAuthToken: string | undefined; if (authToken) { headers.set('Authorization', `Bearer ${authToken}`); - const tokenUrl = new URL('v1/identity/websocket-token', url); + const tokenUrl = new URL('v1/identity/websocket-token', base); tokenUrl.protocol = url.protocol === 'wss:' ? 'https:' : 'http:'; const response = await fetch(tokenUrl, { method: 'POST', headers }); @@ -112,7 +116,7 @@ export async function openWebSocket({ } } - const databaseUrl = new URL(`v1/database/${nameOrAddress}/subscribe`, url); + const databaseUrl = new URL(`v1/database/${nameOrAddress}/subscribe`, base); if (temporaryAuthToken) { databaseUrl.searchParams.set('token', temporaryAuthToken); } diff --git a/crates/bindings-typescript/tests/ws_url.test.ts b/crates/bindings-typescript/tests/ws_url.test.ts new file mode 100644 index 00000000000..cdb29265fd6 --- /dev/null +++ b/crates/bindings-typescript/tests/ws_url.test.ts @@ -0,0 +1,59 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { openWebSocket } from '../src/sdk/ws'; + +describe('openWebSocket', () => { + afterEach(() => vi.unstubAllGlobals()); + + async function urlsFor(uri: string, authToken: string | null = 'stored') { + const fetched: string[] = []; + const opened: string[] = []; + vi.stubGlobal('fetch', async (url: URL) => { + fetched.push(url.href); + return new Response(JSON.stringify({ token: 'short' }), { status: 200 }); + }); + vi.stubGlobal( + 'WebSocket', + class { + binaryType = ''; + constructor(url: string) { + opened.push(url); + } + } + ); + await openWebSocket({ + url: new URL(uri), + nameOrAddress: 'app', + wsProtocol: [], + authToken: authToken ?? undefined, + compression: 'none', + lightMode: false, + connectionId: 'abc', + }); + return { fetched, opened }; + } + + it('keeps a path prefix with or without a trailing slash', async () => { + for (const uri of ['ws://host/stdb', 'ws://host/stdb/']) { + const { fetched, opened } = await urlsFor(uri); + expect(fetched).toEqual(['http://host/stdb/v1/identity/websocket-token']); + expect(opened[0]).toMatch( + /^ws:\/\/host\/stdb\/v1\/database\/app\/subscribe\?/ + ); + expect(new URL(opened[0]).searchParams.get('connection_id')).toBe('abc'); + } + }); + + it('keeps a path prefix without an auth token', async () => { + const { fetched, opened } = await urlsFor('ws://host/stdb', null); + expect(fetched).toEqual([]); + expect(opened[0]).toMatch( + /^ws:\/\/host\/stdb\/v1\/database\/app\/subscribe\?/ + ); + }); + + it('is unchanged for an address without a path', async () => { + const { fetched, opened } = await urlsFor('wss://host'); + expect(fetched).toEqual(['https://host/v1/identity/websocket-token']); + expect(opened[0]).toMatch(/^wss:\/\/host\/v1\/database\/app\/subscribe\?/); + }); +}); From e34cefdc15a63331f22fe5ca9d7ea20a8306cda0 Mon Sep 17 00:00:00 2001 From: bradleyshep Date: Fri, 20 Nov 2026 10:14:05 -0500 Subject: [PATCH 2/2] Trim the URL path test to one table --- crates/bindings-typescript/src/sdk/ws.ts | 1 - .../bindings-typescript/tests/ws_url.test.ts | 81 +++++++------------ 2 files changed, 29 insertions(+), 53 deletions(-) diff --git a/crates/bindings-typescript/src/sdk/ws.ts b/crates/bindings-typescript/src/sdk/ws.ts index 1359aa97e86..283a160882b 100644 --- a/crates/bindings-typescript/src/sdk/ws.ts +++ b/crates/bindings-typescript/src/sdk/ws.ts @@ -95,7 +95,6 @@ export async function openWebSocket({ const WS = await resolveWS(); - // Endpoints resolve under the URI's path, so `ws://host/stdb` reaches `/stdb/v1/...`. const base = new URL(url.toString()); if (!base.pathname.endsWith('/')) base.pathname += '/'; diff --git a/crates/bindings-typescript/tests/ws_url.test.ts b/crates/bindings-typescript/tests/ws_url.test.ts index cdb29265fd6..00facc014cd 100644 --- a/crates/bindings-typescript/tests/ws_url.test.ts +++ b/crates/bindings-typescript/tests/ws_url.test.ts @@ -1,59 +1,36 @@ -import { afterEach, describe, expect, it, vi } from 'vitest'; +import { afterEach, expect, it, vi } from 'vitest'; import { openWebSocket } from '../src/sdk/ws'; -describe('openWebSocket', () => { - afterEach(() => vi.unstubAllGlobals()); +afterEach(() => vi.unstubAllGlobals()); - async function urlsFor(uri: string, authToken: string | null = 'stored') { - const fetched: string[] = []; - const opened: string[] = []; - vi.stubGlobal('fetch', async (url: URL) => { - fetched.push(url.href); - return new Response(JSON.stringify({ token: 'short' }), { status: 200 }); - }); - vi.stubGlobal( - 'WebSocket', - class { - binaryType = ''; - constructor(url: string) { - opened.push(url); - } +it.each([ + ['ws://host/stdb', 'http://host/stdb/', 'ws://host/stdb/'], + ['ws://host/stdb/', 'http://host/stdb/', 'ws://host/stdb/'], + ['wss://host', 'https://host/', 'wss://host/'], +])('connects under the path of %s', async (uri, http, ws) => { + const fetched: string[] = []; + const opened: string[] = []; + vi.stubGlobal('fetch', async (url: URL) => { + fetched.push(url.href); + return new Response(JSON.stringify({ token: 'short' }), { status: 200 }); + }); + vi.stubGlobal( + 'WebSocket', + class { + binaryType = ''; + constructor(url: string) { + opened.push(url); } - ); - await openWebSocket({ - url: new URL(uri), - nameOrAddress: 'app', - wsProtocol: [], - authToken: authToken ?? undefined, - compression: 'none', - lightMode: false, - connectionId: 'abc', - }); - return { fetched, opened }; - } - - it('keeps a path prefix with or without a trailing slash', async () => { - for (const uri of ['ws://host/stdb', 'ws://host/stdb/']) { - const { fetched, opened } = await urlsFor(uri); - expect(fetched).toEqual(['http://host/stdb/v1/identity/websocket-token']); - expect(opened[0]).toMatch( - /^ws:\/\/host\/stdb\/v1\/database\/app\/subscribe\?/ - ); - expect(new URL(opened[0]).searchParams.get('connection_id')).toBe('abc'); } + ); + await openWebSocket({ + url: new URL(uri), + nameOrAddress: 'app', + wsProtocol: [], + authToken: 'stored', + compression: 'none', + lightMode: false, }); - - it('keeps a path prefix without an auth token', async () => { - const { fetched, opened } = await urlsFor('ws://host/stdb', null); - expect(fetched).toEqual([]); - expect(opened[0]).toMatch( - /^ws:\/\/host\/stdb\/v1\/database\/app\/subscribe\?/ - ); - }); - - it('is unchanged for an address without a path', async () => { - const { fetched, opened } = await urlsFor('wss://host'); - expect(fetched).toEqual(['https://host/v1/identity/websocket-token']); - expect(opened[0]).toMatch(/^wss:\/\/host\/v1\/database\/app\/subscribe\?/); - }); + expect(fetched).toEqual([`${http}v1/identity/websocket-token`]); + expect(opened[0].startsWith(`${ws}v1/database/app/subscribe?`)).toBe(true); });