-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathutils.js
More file actions
142 lines (132 loc) · 4.46 KB
/
utils.js
File metadata and controls
142 lines (132 loc) · 4.46 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Cluster } from '@nftstorage/ipfs-cluster'
import pg from 'pg'
import { DBClient } from '../../../api/src/utils/db-client.js'
/**
* Create a new IPFS Cluster instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
*/
export function getCluster1(env) {
const clusterApiUrl = env.CLUSTER1_API_URL
if (!clusterApiUrl) throw new Error('missing IPFS Cluster API URL')
const basicAuthToken = env.CLUSTER1_BASIC_AUTH_TOKEN
if (!basicAuthToken) throw new Error('missing IPFS Cluster credentials')
return new Cluster(clusterApiUrl, {
headers: { authorization: `Basic ${basicAuthToken}` },
})
}
/**
* Create a new IPFS Cluster instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
*/
export function getCluster2(env) {
const clusterApiUrl = env.CLUSTER2_API_URL
if (!clusterApiUrl) throw new Error('missing IPFS Cluster API URL')
const basicAuthToken = env.CLUSTER2_BASIC_AUTH_TOKEN
if (!basicAuthToken) throw new Error('missing IPFS Cluster credentials')
return new Cluster(clusterApiUrl, {
headers: { authorization: `Basic ${basicAuthToken}` },
})
}
/**
* Create a new IPFS Cluster instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
*/
export function getCluster3(env) {
const clusterApiUrl = env.CLUSTER3_API_URL
if (!clusterApiUrl) throw new Error('missing IPFS Cluster API URL')
const basicAuthToken = env.CLUSTER3_BASIC_AUTH_TOKEN
if (!basicAuthToken) throw new Error('missing IPFS Cluster credentials')
return new Cluster(clusterApiUrl, {
headers: { authorization: `Basic ${basicAuthToken}` },
})
}
/**
* Create a new IPFS Cluster instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
*/
export function getPickup(env) {
const pickupUrl = env.PICKUP_URL
if (!pickupUrl) throw new Error('PICKUP_URL must be set in env')
const basicAuthToken = env.PICKUP_BASIC_AUTH_TOKEN
if (!basicAuthToken) {
throw new Error('PICKUP_BASIC_AUTH_TOKEN must be set in env')
}
return new Cluster(pickupUrl, {
headers: { authorization: `Basic ${basicAuthToken}` },
})
}
/**
* Create a new DBClient instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
*/
export function getDBClient(env) {
let url, token
if (env.ENV === 'production') {
url = env.PROD_DATABASE_URL
token = env.PROD_DATABASE_TOKEN
} else if (env.ENV === 'staging') {
url = env.STAGING_DATABASE_URL
token = env.STAGING_DATABASE_TOKEN
} else {
url = env.DATABASE_URL
token = env.DATABASE_TOKEN
}
if (!url || !token) throw new Error('missing PostgREST credentials')
return new DBClient(url, token)
}
/**
* Create a new Postgres client instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
* @param {'ro'|'rw'} [mode]
*/
export function getPg(env, mode = 'rw') {
return new pg.Client({ connectionString: getPgConnString(env, mode) })
}
/**
* Create a new Postgres pool instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
* @param {'ro'|'rw'} [mode]
*/
export function getPgPool(env, mode = 'rw') {
return new pg.Pool({
connectionString: getPgConnString(env, mode),
max: MAX_CONCURRENT_QUERIES,
})
}
/**
* Get a postgres connection string from the passed environment variables.
* @param {Record<string, string|undefined>} env
* @param {'ro'|'rw'} [mode]
*/
export function getPgConnString(env, mode = 'rw') {
let connectionString
if (env.ENV === 'production') {
connectionString =
mode === 'rw'
? env.PROD_DATABASE_CONNECTION
: env.PROD_RO_DATABASE_CONNECTION
} else if (env.ENV === 'staging') {
connectionString =
mode === 'rw'
? env.STAGING_DATABASE_CONNECTION
: env.STAGING_RO_DATABASE_CONNECTION
} else {
connectionString =
mode === 'rw' ? env.DATABASE_CONNECTION : env.RO_DATABASE_CONNECTION
}
if (!connectionString) throw new Error('missing Postgres connection string')
return connectionString
}
export const MAX_CONCURRENT_QUERIES = 10
/**
* Object.hasOwnProperty as typescript type guard
* @template {unknown} X
* @template {PropertyKey} Y
* @param {X} obj
* @param {Y} prop
* @returns {obj is X & Record<Y, unknown>}
* https://fettblog.eu/typescript-hasownproperty/
*/
export function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop)
}