diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 60fc3f8..7d0826b 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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) diff --git a/src/index.ts b/src/index.ts index 3dc0c60..2bb5b82 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -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() } }