FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: Host header with localhost connections by Gioee · Pull Request #274 · sqlitecloud/sqlitecloud-js · GitHub

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

Filter by extension

Filter by extension .json  (2) .ts  (2) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/connection-ws.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = { 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)
Expand Down
30 changes: 30 additions & 0 deletions test/connection-ws-unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof io>
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({
Expand Down
Loading

Back | FazBrowse Home | New Git URL