-
Notifications
You must be signed in to change notification settings - Fork 131
/
lake.ts
115 lines (99 loc) · 2.63 KB
/
lake.ts
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
import { ChildProcess, spawn, SpawnOptions } from 'child_process';
import { mkdirpSync } from 'fs-extra';
import { join } from 'path';
import fetch from 'node-fetch';
import { getZedPath } from './binpath';
type ConstructorOpts = {
root: string;
logs: string;
port?: number;
bin?: string;
corsOrigins?: string[];
};
export class Lake {
lake?: ChildProcess;
root: string;
port: number;
logs: string;
bin: string;
cors: string[];
constructor(opts: ConstructorOpts) {
this.root = opts.root;
this.logs = opts.logs;
this.port = opts.port || 9867;
this.bin = opts.bin || getZedPath();
this.cors = opts.corsOrigins || [];
}
addr(): string {
return `localhost:${this.port}`;
}
start() {
mkdirpSync(this.root, { mode: 0o755 });
mkdirpSync(this.logs, { mode: 0o755 });
const args = [
'serve',
'-l',
this.addr(),
'-lake',
this.root,
'-log.level=info',
'-log.filemode=rotate',
'-log.path',
join(this.logs, 'zlake.log'),
];
for (const origin of this.cors) {
args.push(`--cors.origin=${origin}`);
}
const opts = {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
};
// For unix systems, pass posix pipe read file descriptor into lake process.
// In the event of Zui getting shutdown via `SIGKILL`, this will let lake
// know that it has been orphaned and to shutdown.
if (process.platform !== 'win32') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { readfd } = require('node-pipe').pipeSync();
opts.stdio.push(readfd);
args.push(`-brimfd=${opts.stdio.length - 1}`);
}
this.lake = spawn(this.bin, args, opts as SpawnOptions);
this.lake.on('error', (err) => {
console.error('lake spawn error', err);
});
return waitFor(async () => this.isUp());
}
async stop(): Promise<boolean> {
if (this.lake) {
this.lake.kill('SIGTERM');
return waitFor(() => this.isDown());
} else {
return true;
}
}
async isUp() {
try {
const response = await fetch(`http://${this.addr()}/status`);
const text = await response.text();
return text === 'ok';
} catch (e) {
return false;
}
}
async isDown() {
return !(await this.isUp());
}
}
async function waitFor(condition: () => Promise<boolean>) {
let giveUp = false;
const id = setTimeout(() => {
giveUp = true;
}, 5000);
while (!giveUp) {
if (await condition()) break;
await sleep(50);
}
clearTimeout(id);
return !giveUp;
}
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));