Skip to content
Open
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
10 changes: 10 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ describe('config', () => {
assert.notStrictEqual(got, undefined)
})

test('strips port 3306 from database URL', async () => {
const connection = connect({ fetch, url: 'mysql://someuser:password@example.com:3306/db' })
assert.strictEqual(connection.config.host, 'example.com')
})

test('strips port 3306 from config.host', async () => {
const connection = connect({ fetch, host: 'example.com:3306', username: 'someuser', password: 'password' })
assert.strictEqual(connection.config.host, 'example.com')
})

test('exposes config as a public field', async () => {
const config = { url: 'mysql://someuser:password@example.com/db' }
const connection = connect(config)
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ function protocol(protocol: string): string {

function buildURL(url: URL): string {
const scheme = `${protocol(url.protocol)}//`
const host = url.port === '3306' ? url.hostname : url.host

return new URL(url.pathname, `${scheme}${url.host}`).toString()
return new URL(url.pathname, `${scheme}${host}`).toString()
}

export class Connection {
Expand All @@ -226,6 +227,10 @@ export class Connection {
this.config.host = url.hostname
this.url = buildURL(url)
} else {
const url = new URL(`https://${this.config.host}`)
if (url.port === '3306') {
this.config.host = url.hostname
}
this.url = new URL(`https://${this.config.host}`).toString()
}
}
Expand Down