-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathutils.ts
More file actions
60 lines (53 loc) · 1.67 KB
/
utils.ts
File metadata and controls
60 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pgcs from 'pg-connection-string'
import { FastifyRequest } from 'fastify'
import { DEFAULT_POOL_CONFIG } from './constants.js'
import { PoolConfig } from '../lib/types.js'
export const extractRequestForLogging = (request: FastifyRequest) => {
let pg: string = 'unknown'
try {
if (request.headers.pg) {
pg = pgcs.parse(request.headers.pg as string).host || pg
}
} catch (e: any) {
console.warn('failed to parse PG connstring for ' + request.url)
}
const additional = request.headers['x-supabase-info']?.toString() || ''
return {
method: request.method,
url: request.url,
pg,
opt: additional,
}
}
export function createConnectionConfig(
request: FastifyRequest,
queryTimeoutSecs?: number | string
): PoolConfig {
const connectionString = request.headers.pg as string
const timeout = queryTimeoutSecs !== undefined ? Number(queryTimeoutSecs) : undefined
const config = {
...DEFAULT_POOL_CONFIG,
connectionString,
...(timeout !== undefined && {
query_timeout: timeout === 0 ? undefined : timeout * 1000,
}),
}
// Override application_name if custom one provided in header
if (request.headers['x-pg-application-name']) {
config.application_name = request.headers['x-pg-application-name'] as string
}
return config
}
export function translateErrorToResponseCode(
error: { message: string },
defaultResponseCode = 400
): number {
if (error.message === 'Connection terminated due to connection timeout') {
return 504
} else if (error.message === 'sorry, too many clients already') {
return 503
} else if (error.message === 'Query read timeout') {
return 408
}
return defaultResponseCode
}