-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathindex.js
178 lines (160 loc) · 4.43 KB
/
index.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// @ts-check
/* global process */
import url from 'url';
import popen from 'child_process';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { E } from '@endo/eventual-send';
import { makePromiseKit } from '@endo/promise-kit';
import {
whereEndoState,
whereEndoEphemeralState,
whereEndoSock,
whereEndoCache,
} from '@endo/where';
import { makeEndoClient } from './src/client.js';
// Reexports:
export { makeEndoClient } from './src/client.js';
export { makeRefReader, makeRefIterator } from './src/ref-reader.js';
export { makeReaderRef, makeIteratorRef } from './src/reader-ref.js';
const removePath = async removalPath => {
return fs.promises
.rm(removalPath, { recursive: true, force: true })
.catch(cause => {
/** @type {object} */
const error = new Error(cause.message, { cause });
error.code = cause.code;
throw error;
});
};
const { username, homedir } = os.userInfo();
const temp = os.tmpdir();
const info = {
user: username,
home: homedir,
temp,
};
const defaultConfig = {
statePath: whereEndoState(process.platform, process.env, info),
ephemeralStatePath: whereEndoEphemeralState(
process.platform,
process.env,
info,
),
sockPath: whereEndoSock(process.platform, process.env, info),
cachePath: whereEndoCache(process.platform, process.env, info),
};
const endoDaemonPath = url.fileURLToPath(
new URL('src/daemon-node.js', import.meta.url),
);
export const terminate = async (config = defaultConfig) => {
const { resolve: cancel, promise: cancelled } = makePromiseKit();
const { getBootstrap, closed } = await makeEndoClient(
'harbinger',
config.sockPath,
cancelled,
);
const bootstrap = getBootstrap();
await E(bootstrap)
.terminate()
.catch(() => {});
// @ts-expect-error zero-argument promise resolve
cancel();
await closed.catch(() => {});
};
export const start = async (config = defaultConfig) => {
await fs.promises.mkdir(config.statePath, {
recursive: true,
});
const logPath = path.join(config.statePath, 'endo.log');
const output = fs.openSync(logPath, 'a');
const env = { ...process.env };
const child = popen.fork(
endoDaemonPath,
[
config.sockPath,
config.statePath,
config.ephemeralStatePath,
config.cachePath,
],
{
detached: true,
env,
stdio: ['ignore', output, output, 'ipc'],
},
);
return new Promise((resolve, reject) => {
child.on('error', (/** @type {Error} */ cause) => {
reject(
Error(
`Daemon exited prematurely with error ${cause.message}, see (${logPath})`,
),
);
});
child.on('exit', (/** @type {number?} */ code) => {
reject(
Error(
`Daemon exited prematurely with code (${code}), see (${logPath})`,
),
);
});
child.on('message', message => {
child.disconnect();
child.unref();
if (
typeof message === 'object' &&
message !== null &&
'type' in message
) {
if (message.type === 'ready') {
// This message corresponds to process.send({ type: 'ready' }) in
// src/daemon-node-powers.js and indicates the daemon is ready to receive
// clients.
resolve(undefined);
} else if (
message.type === 'error' &&
'message' in message &&
typeof message.message === 'string'
) {
reject(new Error(message.message));
}
}
});
});
};
const enoentOk = error => {
if (error.code === 'ENOENT') {
return;
}
throw error;
};
export const clean = async (config = defaultConfig) => {
await null;
if (process.platform !== 'win32') {
await removePath(config.sockPath).catch(enoentOk);
}
};
export const stop = async (config = defaultConfig) => {
await terminate(config).catch(() => {});
await clean(config);
};
export const restart = async (config = defaultConfig) => {
await stop(config);
return start(config);
};
export const purge = async (config = defaultConfig) => {
await terminate(config).catch(() => {});
const cleanedUp = clean(config);
const removedState = removePath(config.statePath).catch(enoentOk);
const removedEphemeralState = removePath(config.ephemeralStatePath).catch(
enoentOk,
);
const removedCache = removePath(config.cachePath).catch(enoentOk);
await Promise.all([
cleanedUp,
removedState,
removedEphemeralState,
removedCache,
]);
};