Skip to content

Commit 614e739

Browse files
authored
fix: Host header with localhost connections (#274)
1 parent 796dfe5 commit 614e739

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.878",
3+
"version": "1.0.880",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/connection-ws.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection {
9898
// hostname (eg crvheg7dhk.g4 from crvheg7dhk.g4.sqlite.cloud) to form the gateway host
9999
// (→ crvheg7dhk.g4.gateway.sqlite.cloud). For local development, pass a `gatewayurl`
100100
// containing `localhost` — the driver routes TCP to it and injects the tenant Host header
101-
// separately so the gateway still tenant-routes correctly.
101+
// (with the gateway marker label) so the gateway still tenant-routes correctly.
102102
const authToken = this.config.apikey || this.config.token
103103
const ioOpts: Record<string, unknown> = { auth: { token: authToken }, parser: createSocketIOParser(websocketMaxAttachments) }
104104
let gatewayUrl: string
105105
if (this.config.gatewayurl?.includes('localhost')) {
106106
const raw = this.config.gatewayurl
107107
gatewayUrl = raw.startsWith('ws://') || raw.startsWith('wss://') ? raw : `ws://${raw}`
108-
ioOpts.extraHeaders = { Host: this.config.host }
108+
ioOpts.extraHeaders = { Host: buildGatewayHost(this.config.host as string) }
109109
ioOpts.transports = ['websocket']
110110
} else {
111111
const gatewayHost = buildGatewayHost(this.config.host as string, this.config.gatewayurl)

test/connection-ws-unit.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@ describe('websocket transport helpers', () => {
4444
expect(decoder.opts?.maxAttachments).toBe(321)
4545
})
4646

47+
it('should connect to localhost gateway with the marker-augmented Host header', () => {
48+
const socket = {
49+
connected: false,
50+
on: jest.fn(),
51+
removeAllListeners: jest.fn(),
52+
close: jest.fn()
53+
}
54+
const mockedIo = io as jest.MockedFunction<typeof io>
55+
mockedIo.mockReturnValue(socket as any)
56+
57+
const connection = Object.create(SQLiteCloudWebsocketConnection.prototype) as any
58+
connection.connectTransport(
59+
{
60+
connectionstring: 'sqlitecloud://host.sqlite.cloud/database?apikey=secret',
61+
host: 'host.sqlite.cloud',
62+
apikey: 'secret',
63+
gatewayurl: 'ws://localhost:8090'
64+
},
65+
jest.fn()
66+
)
67+
68+
expect(mockedIo).toHaveBeenCalledWith(
69+
'ws://localhost:8090',
70+
expect.objectContaining({
71+
extraHeaders: { Host: 'host.gateway.sqlite.cloud' },
72+
transports: ['websocket']
73+
})
74+
)
75+
})
76+
4777
it('should encode bigint values before sending JSON payloads', () => {
4878
expect(
4979
encodeBigIntMarkers({

0 commit comments

Comments
 (0)