Skip to content

Commit

Permalink
fix: socket path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Dec 11, 2019
1 parent 8a7269f commit 9004bb3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 105 deletions.
3 changes: 2 additions & 1 deletion cli/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"rimraf": "^3.0.0",
"tar": "^5.0.5",
"temp-write": "^4.0.0",
"tempy": "^0.3.0"
"tempy": "^0.3.0",
"url-parse": "^1.4.7"
},
"husky": {
"hooks": {
Expand Down

This file was deleted.

7 changes: 3 additions & 4 deletions cli/sdk/src/__tests__/convertCredentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ const uris = [
'mysql://user@localhost:3333/dbname?sslmode=prefer',
'mongodb://mongodb0.example.com:27017/admin',
'mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017/admin',
'mysql://root@/db?socket=/private/tmp/mysql.sock',
'postgresql://root:prisma@/prisma?host=/var/run/postgresql/',
]

for (const uri of uris) {
test(`Convert ${uri}`, () => {
expect({
before: uri,
after: credentialsToUri(uriToCredentials(uri)),
}).toMatchSnapshot()
expect(credentialsToUri(uriToCredentials(uri))).toBe(uri)
})
}
45 changes: 37 additions & 8 deletions cli/sdk/src/convertCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DatabaseCredentials } from './types'
import { URL } from 'url'
import URL from 'url-parse'
import { ConnectorType } from '@prisma/generator-helper'

export function credentialsToUri(credentials: DatabaseCredentials): string {
const type = databaseTypeToProtocol(credentials.type)
if (credentials.type === 'mongo') {
return credentials.uri!
}
const url = new URL(type + '//')
const url = new URL(type + '//', true)

if (credentials.host) {
url.hostname = credentials.host
Expand All @@ -19,14 +19,21 @@ export function credentialsToUri(credentials: DatabaseCredentials): string {
}

if (credentials.schema) {
url.searchParams.set('schema', credentials.schema)
url.query.schema = credentials.schema
}

if (credentials.socket) {
url.query.host = credentials.socket
}
} else if (credentials.type === 'mysql') {
url.pathname = '/' + (credentials.database || credentials.schema || '')
if (credentials.socket) {
url.query.socket = credentials.socket
}
}

if (credentials.ssl) {
url.searchParams.set('sslmode', 'prefer')
url.query.sslmode = 'prefer'
}

if (credentials.user) {
Expand All @@ -41,13 +48,31 @@ export function credentialsToUri(credentials: DatabaseCredentials): string {
url.port = String(credentials.port)
}

return url.toString()
url.host = `${url.hostname}${url.port ? `:${url.port}` : ''}`

if (credentials.extraFields) {
for (const [key, value] of Object.entries(credentials.extraFields)) {
url.query[key] = value
}
}

// trim away empty pathnames
if (url.pathname === '/') {
url.pathname = ''
}

// use a custom toString method, as we don't want escaping of query params
return url.toString(q =>
Object.entries(q)
.map(([key, value]) => `${key}=${value}`)
.join('&'),
)
}

export function uriToCredentials(
connectionString: string,
): DatabaseCredentials {
const uri = new URL(connectionString)
const uri = new URL(connectionString, true)
const type = protocolToDatabaseType(uri.protocol)

// needed, as the URL implementation adds empty strings
Expand All @@ -60,6 +85,8 @@ export function uriToCredentials(
}
}

const { schema, socket, host, ...extraFields } = uri.query

return {
type,
host: exists(uri.hostname) ? uri.hostname : undefined,
Expand All @@ -70,9 +97,11 @@ export function uriToCredentials(
uri.pathname && uri.pathname.length > 1
? uri.pathname.slice(1)
: undefined,
schema: uri.searchParams.get('schema') || undefined,
schema: uri.query.schema || undefined,
uri: connectionString,
ssl: uri.searchParams.has('sslmode'),
ssl: Boolean(uri.query.sslmode),
socket: uri.query.socket || uri.query.host,
extraFields,
}
}

Expand Down
2 changes: 2 additions & 0 deletions cli/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export interface DatabaseCredentials {
ssl?: boolean
uri?: string
executeRaw?: boolean
socket?: string
extraFields?: { [key: string]: string }
}

0 comments on commit 9004bb3

Please sign in to comment.