-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
74 lines (70 loc) · 2.38 KB
/
Copy pathhelpers.js
File metadata and controls
74 lines (70 loc) · 2.38 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
// Shared test harness: boot a real JavaScript Solid Server from npm with a
// set of plugin entries, on a probed port, against a throwaway data root.
//
// createServer is the documented composition entry (docs/configuration.md);
// the no-internals rule is about *plugins*, not the host the tests boot.
import fs from 'node:fs';
import net from 'node:net';
import os from 'node:os';
import path from 'node:path';
import { createServer } from 'javascript-solid-server/src/server.js';
export function probePort() {
return new Promise((resolve, reject) => {
const probe = net.createServer();
probe.once('error', reject);
probe.listen(0, '127.0.0.1', () => {
const port = probe.address().port;
probe.close(() => resolve(port));
});
});
}
/**
* @param {object} opts extra createServer options (idp, …)
* @param {Array} opts.plugins plugin entries under test
* @returns {{ base, wsBase, root, fastify, close }}
*/
export async function startJss({ plugins, ...opts } = {}) {
const root = opts.root ?? fs.mkdtempSync(path.join(os.tmpdir(), 'jss-plugins-test-'));
delete opts.root;
// A fixed port lets configs reference the server's own origin (some
// plugins need it — see notifications; finding: api.serverInfo).
const port = opts.port ?? await probePort();
delete opts.port;
const fastify = createServer({
logger: false,
forceCloseConnections: true,
root,
...(opts.idp ? { idpIssuer: `http://127.0.0.1:${port}` } : {}),
plugins,
...opts,
});
await fastify.listen({ port, host: '127.0.0.1' });
return {
base: `http://127.0.0.1:${port}`,
wsBase: `ws://127.0.0.1:${port}`,
root,
fastify,
async close({ keepData = false } = {}) {
await fastify.close();
if (!keepData) fs.rmSync(root, { recursive: true, force: true });
},
};
}
/** Collect messages from a ws until predicate or timeout. */
export function wsCollect(socket, isDone, timeoutMs = 4000) {
return new Promise((resolve, reject) => {
const msgs = [];
const timer = setTimeout(() => reject(new Error(`ws timeout; got ${JSON.stringify(msgs)}`)), timeoutMs);
socket.on('message', (data) => {
msgs.push(JSON.parse(String(data)));
if (isDone(msgs)) {
clearTimeout(timer);
resolve(msgs);
}
});
socket.on('error', (err) => {
clearTimeout(timer);
reject(err);
});
});
}