forked from seveibar/postgrest-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (77 loc) · 2.27 KB
/
Copy pathindex.js
File metadata and controls
87 lines (77 loc) · 2.27 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
const child_process = require("child_process")
const fs = require("fs")
const tmp = require("tmp")
const decamelize = require("decamelize")
const path = require("path")
const bent = require("bent")
const debug = require("debug")("postgrest")
const bunyan = require("bunyan")
const getJSON = bent("json")
const log = bunyan.createLogger({
name: "postgrest",
level: debug.enabled ? "trace" : "info",
})
module.exports.startServer = async (config, processOptions = {}) => {
let configPath
let serverPort
if (typeof config === "string") {
configPath = config
const configContent = fs.readFileSync(configPath).toString()
serverPort = parseInt(/^server-port=(.*)$/.match(configContent))
} else {
serverPort = config.serverPort
const postgrestConfigContent = `${Object.entries(config)
.map(([k, v]) => `${decamelize(k, "-")}="${v}"`)
.join("\n")}`
configPath = tmp.tmpNameSync() + ".conf"
fs.writeFileSync(configPath, postgrestConfigContent)
}
const proc = child_process.spawn(
path.resolve(__dirname, "postgrest"),
[configPath],
{
shell: true,
}
)
proc.stdout.on("data", (data) => {
log.trace({ postgrestStdout: data.toString() })
})
proc.stderr.on("data", (data) => {
log.trace({ postgrestStderr: data.toString() })
})
let isClosed = false
proc.on("close", (code) => {
log.trace({ postgrestShutdown: true })
isClosed = true
})
await new Promise((resolve, reject) => {
const processCloseTimeout = setTimeout(() => {
if (isClosed) {
reject("Postgrest didn't start properly")
} else {
reject(`Postgrest didn't respond`)
proc.kill("SIGINT")
}
}, processOptions.healthWaitTime || parseInt(process.env.POSTGRES_HEALTH_WAIT_TIME) || 5000) // 5s to wait for start
let backoff = 50
async function checkIfPostgrestRunning() {
const result = await getJSON(`http://127.0.0.1:${serverPort}`).catch(
() => null
)
if (result) {
clearTimeout(processCloseTimeout)
resolve()
} else {
setTimeout(checkIfPostgrestRunning, backoff)
backoff = Math.min(backoff + 50, 250)
}
}
checkIfPostgrestRunning()
})
return {
proc,
stop: async () => {
proc.kill("SIGINT")
},
}
}