diff --git a/package-lock.json b/package-lock.json index ad7e6ad..10a5d64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.878", + "version": "1.0.880", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.878", + "version": "1.0.880", "license": "MIT", "dependencies": { "buffer": "^6.0.3", diff --git a/package.json b/package.json index abc1a2d..6f0f105 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.878", + "version": "1.0.880", "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/drivers/connection-ws.ts b/src/drivers/connection-ws.ts index c467313..917118d 100644 --- a/src/drivers/connection-ws.ts +++ b/src/drivers/connection-ws.ts @@ -98,14 +98,14 @@ export class SQLiteCloudWebsocketConnection extends SQLiteCloudConnection { // hostname (eg crvheg7dhk.g4 from crvheg7dhk.g4.sqlite.cloud) to form the gateway host // (→ crvheg7dhk.g4.gateway.sqlite.cloud). For local development, pass a `gatewayurl` // containing `localhost` — the driver routes TCP to it and injects the tenant Host header - // separately so the gateway still tenant-routes correctly. + // (with the gateway marker label) so the gateway still tenant-routes correctly. const authToken = this.config.apikey || this.config.token const ioOpts: Record = { auth: { token: authToken }, parser: createSocketIOParser(websocketMaxAttachments) } let gatewayUrl: string if (this.config.gatewayurl?.includes('localhost')) { const raw = this.config.gatewayurl gatewayUrl = raw.startsWith('ws://') || raw.startsWith('wss://') ? raw : `ws://${raw}` - ioOpts.extraHeaders = { Host: this.config.host } + ioOpts.extraHeaders = { Host: buildGatewayHost(this.config.host as string) } ioOpts.transports = ['websocket'] } else { const gatewayHost = buildGatewayHost(this.config.host as string, this.config.gatewayurl) diff --git a/test/connection-ws-unit.test.ts b/test/connection-ws-unit.test.ts index 968810f..ae3de89 100644 --- a/test/connection-ws-unit.test.ts +++ b/test/connection-ws-unit.test.ts @@ -44,6 +44,36 @@ describe('websocket transport helpers', () => { expect(decoder.opts?.maxAttachments).toBe(321) }) + it('should connect to localhost gateway with the marker-augmented Host header', () => { + const socket = { + connected: false, + on: jest.fn(), + removeAllListeners: jest.fn(), + close: jest.fn() + } + const mockedIo = io as jest.MockedFunction + mockedIo.mockReturnValue(socket as any) + + const connection = Object.create(SQLiteCloudWebsocketConnection.prototype) as any + connection.connectTransport( + { + connectionstring: 'sqlitecloud://host.sqlite.cloud/database?apikey=secret', + host: 'host.sqlite.cloud', + apikey: 'secret', + gatewayurl: 'ws://localhost:8090' + }, + jest.fn() + ) + + expect(mockedIo).toHaveBeenCalledWith( + 'ws://localhost:8090', + expect.objectContaining({ + extraHeaders: { Host: 'host.gateway.sqlite.cloud' }, + transports: ['websocket'] + }) + ) + }) + it('should encode bigint values before sending JSON payloads', () => { expect( encodeBigIntMarkers({