-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfastboot.ts
79 lines (75 loc) · 2.36 KB
/
fastboot.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
//@ts-expect-error no types for fastboot
import FastBoot from 'fastboot';
import { type FastBootInstance } from './fastboot-from-deployed';
import { instantiateFastBoot } from './fastboot-from-deployed';
import {
type IndexRunner,
type RunnerOpts,
} from '@cardstack/runtime-common/search-index';
import { JSDOM } from 'jsdom';
import { type ErrorReporter } from '@cardstack/runtime-common/realm';
const appName = '@cardstack/host';
export async function makeFastBootIndexRunner(
dist: URL | string,
getRunnerOpts: (optsId: number) => RunnerOpts,
): Promise<{ getRunner: IndexRunner; distPath: string }> {
let fastboot: FastBootInstance;
let distPath: string;
let globalWithErrorReporter = global as typeof globalThis & {
__boxelErrorReporter: ErrorReporter;
};
if (typeof dist === 'string') {
distPath = dist;
fastboot = new FastBoot({
distPath,
resilient: false,
buildSandboxGlobals(defaultGlobals: any) {
return Object.assign({}, defaultGlobals, {
__boxelErrorReporter: globalWithErrorReporter.__boxelErrorReporter,
URL: globalThis.URL,
Request: globalThis.Request,
Response: globalThis.Response,
btoa,
getRunnerOpts,
_logDefinitions: (globalThis as any)._logDefinitions,
jsdom: new JSDOM(''),
});
},
}) as FastBootInstance;
} else {
({ fastboot, distPath } = await instantiateFastBoot(
appName,
dist,
(defaultGlobals: any) => {
return Object.assign({}, defaultGlobals, {
__boxelErrorReporter: globalWithErrorReporter.__boxelErrorReporter,
URL: globalThis.URL,
Request: globalThis.Request,
Response: globalThis.Response,
btoa,
getRunnerOpts,
_logDefinitions: (globalThis as any)._logDefinitions,
jsdom: new JSDOM(''),
});
},
));
}
return {
getRunner: async (optsId: number) => {
await fastboot.visit(`/indexer/${optsId}`, {
// TODO we'll need to configure this host origin as part of the hosted realm work
request: { headers: { host: 'localhost:4200' } },
});
},
distPath,
};
}
function btoa(str: string | Buffer) {
let buffer;
if (str instanceof Buffer) {
buffer = str;
} else {
buffer = Buffer.from(str.toString(), 'binary');
}
return buffer.toString('base64');
}