@warp-drive/holodeck 0.1.0-alpha.14 → 0.1.0-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server/index.js CHANGED
@@ -1,463 +1,7 @@
1
1
  /* global Bun */
2
- import chalk from 'chalk';
3
- import { Hono } from 'hono';
4
- import { serve } from '@hono/node-server';
5
- import { createSecureServer } from 'node:http2';
6
- import { logger } from 'hono/logger';
7
- import { HTTPException } from 'hono/http-exception';
8
- import { cors } from 'hono/cors';
9
- import crypto from 'node:crypto';
10
- import fs from 'node:fs';
11
- import zlib from 'node:zlib';
12
- import { homedir } from 'os';
13
2
  import path from 'path';
14
- import { threadId, parentPort } from 'node:worker_threads';
15
-
16
3
  const isBun = typeof Bun !== 'undefined';
17
- const DEBUG =
18
- process.env.DEBUG?.includes('wd:holodeck') || process.env.DEBUG === '*' || process.env.DEBUG?.includes('wd:*');
19
-
20
- async function getCertInfo() {
21
- let CERT_PATH = process.env.HOLODECK_SSL_CERT_PATH;
22
- let KEY_PATH = process.env.HOLODECK_SSL_KEY_PATH;
23
-
24
- if (!CERT_PATH) {
25
- CERT_PATH = path.join(homedir(), 'holodeck-localhost.pem');
26
- process.env.HOLODECK_SSL_CERT_PATH = CERT_PATH;
27
-
28
- console.log(
29
- `HOLODECK_SSL_CERT_PATH was not found in the current environment. Setting it to default value of ${CERT_PATH}`
30
- );
31
- }
32
-
33
- if (!KEY_PATH) {
34
- KEY_PATH = path.join(homedir(), 'holodeck-localhost-key.pem');
35
- process.env.HOLODECK_SSL_KEY_PATH = KEY_PATH;
36
-
37
- console.log(
38
- `HOLODECK_SSL_KEY_PATH was not found in the current environment. Setting it to default value of ${KEY_PATH}`
39
- );
40
- }
41
-
42
- if (isBun) {
43
- const CERT = Bun.file(CERT_PATH);
44
- const KEY = Bun.file(KEY_PATH);
45
-
46
- if (!(await CERT.exists()) || !(await KEY.exists())) {
47
- throw new Error(
48
- 'SSL certificate or key not found, you may need to run `pnpm dlx @warp-drive/holodeck ensure-cert`'
49
- );
50
- }
51
-
52
- return {
53
- CERT_PATH,
54
- KEY_PATH,
55
- CERT: await CERT.text(),
56
- KEY: await KEY.text(),
57
- };
58
- } else {
59
- if (!fs.existsSync(CERT_PATH) || !fs.existsSync(KEY_PATH)) {
60
- throw new Error(
61
- 'SSL certificate or key not found, you may need to run `pnpm dlx @warp-drive/holodeck ensure-cert`'
62
- );
63
- }
64
-
65
- return {
66
- CERT_PATH,
67
- KEY_PATH,
68
- CERT: fs.readFileSync(CERT_PATH, 'utf8'),
69
- KEY: fs.readFileSync(KEY_PATH, 'utf8'),
70
- };
71
- }
72
- }
73
-
74
- const DEFAULT_PORT = 1135;
75
- const BROTLI_OPTIONS = {
76
- params: {
77
- [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
78
- // brotli currently defaults to 11 but lets be explicit
79
- [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY,
80
- },
81
- };
82
- function compress(code) {
83
- return zlib.brotliCompressSync(code, BROTLI_OPTIONS);
84
- }
85
-
86
- /**
87
- * removes the protocol, host, and port from a url
88
- */
89
- function getNiceUrl(url) {
90
- const urlObj = new URL(url);
91
- urlObj.searchParams.delete('__xTestId');
92
- urlObj.searchParams.delete('__xTestRequestNumber');
93
- return (urlObj.pathname + urlObj.searchParams.toString()).slice(1);
94
- }
95
-
96
- /*
97
- {
98
- projectRoot: string;
99
- testId: string;
100
- url: string;
101
- method: string;
102
- body: string;
103
- testRequestNumber: number
104
- }
105
- */
106
- function generateFilepath(options) {
107
- const { body } = options;
108
- const bodyHash = body ? crypto.createHash('md5').update(JSON.stringify(body)).digest('hex') : null;
109
- const cacheDir = generateFileDir(options);
110
- return `${cacheDir}/${bodyHash ? bodyHash : 'res'}`;
111
- }
112
-
113
- /*
114
- Generate a human scannable file name for the test assets to be stored in,
115
- the `.mock-cache` directory should be checked-in to the codebase.
116
- */
117
- function generateFileDir(options) {
118
- const { projectRoot, testId, url, method, testRequestNumber } = options;
119
- const normalizedUrl = url.startsWith('/') ? url.slice(1) : url;
120
- // make path look nice but not be a sub-directory
121
- // using alternative `/`-like characters would be nice but results in odd encoding
122
- // on disk path
123
- const pathUrl = normalizedUrl.replaceAll('/', '_');
124
- return `${projectRoot}/.mock-cache/${testId}/${method}::${pathUrl}::${testRequestNumber}`;
125
- }
126
-
127
- async function replayRequest(context, cacheKey) {
128
- let metaJson;
129
- try {
130
- if (isBun) {
131
- metaJson = await Bun.file(`${cacheKey}.meta.json`).json();
132
- } else {
133
- metaJson = JSON.parse(fs.readFileSync(`${cacheKey}.meta.json`, 'utf8'));
134
- }
135
- } catch (e) {
136
- context.header('Content-Type', 'application/vnd.api+json');
137
- context.status(400);
138
- return context.body(
139
- JSON.stringify({
140
- errors: [
141
- {
142
- status: '400',
143
- code: 'MOCK_NOT_FOUND',
144
- title: 'Mock not found',
145
- detail: `No meta was found for ${context.req.method} ${context.req.url}. The expected cacheKey was ${cacheKey}. You may need to record a mock for this request.`,
146
- },
147
- ],
148
- })
149
- );
150
- }
151
-
152
- try {
153
- const bodyPath = `${cacheKey}.body.br`;
154
- const bodyInit =
155
- metaJson.status !== 204 && metaJson.status < 500
156
- ? isBun
157
- ? Bun.file(bodyPath)
158
- : fs.createReadStream(bodyPath)
159
- : '';
160
-
161
- const headers = new Headers(metaJson.headers || {});
162
- // @ts-expect-error - createReadStream is supported in node
163
- const response = new Response(bodyInit, {
164
- status: metaJson.status,
165
- statusText: metaJson.statusText,
166
- headers,
167
- });
168
-
169
- if (metaJson.status > 400) {
170
- throw new HTTPException(metaJson.status, { res: response, message: metaJson.statusText });
171
- }
172
-
173
- return response;
174
- } catch (e) {
175
- if (e instanceof HTTPException) {
176
- throw e;
177
- }
178
- context.header('Content-Type', 'application/vnd.api+json');
179
- context.status(500);
180
- return context.body(
181
- JSON.stringify({
182
- errors: [
183
- {
184
- status: '500',
185
- code: 'MOCK_SERVER_ERROR',
186
- title: 'Mock Replay Failed',
187
- detail: `Failed to create the response for ${context.req.method} ${context.req.url}.\n\n\n${e.message}\n${e.stack}`,
188
- },
189
- ],
190
- })
191
- );
192
- }
193
- }
194
-
195
- function createTestHandler(projectRoot) {
196
- const TestHandler = async (context) => {
197
- try {
198
- const { req } = context;
199
-
200
- const testId = req.query('__xTestId');
201
- const testRequestNumber = req.query('__xTestRequestNumber');
202
- const niceUrl = getNiceUrl(req.url);
203
-
204
- if (!testId) {
205
- context.header('Content-Type', 'application/vnd.api+json');
206
- context.status(400);
207
- return context.body(
208
- JSON.stringify({
209
- errors: [
210
- {
211
- status: '400',
212
- code: 'MISSING_X_TEST_ID_HEADER',
213
- title: 'Request to the http mock server is missing the `X-Test-Id` header',
214
- detail:
215
- "The `X-Test-Id` header is used to identify the test that is making the request to the mock server. This is used to ensure that the mock server is only used for the test that is currently running. If using @ember-data/request add import { MockServerHandler } from '@warp-drive/holodeck'; to your request handlers.",
216
- source: { header: 'X-Test-Id' },
217
- },
218
- ],
219
- })
220
- );
221
- }
222
-
223
- if (!testRequestNumber) {
224
- context.header('Content-Type', 'application/vnd.api+json');
225
- context.status(400);
226
- return context.body(
227
- JSON.stringify({
228
- errors: [
229
- {
230
- status: '400',
231
- code: 'MISSING_X_TEST_REQUEST_NUMBER_HEADER',
232
- title: 'Request to the http mock server is missing the `X-Test-Request-Number` header',
233
- detail:
234
- "The `X-Test-Request-Number` header is used to identify the request number for the current test. This is used to ensure that the mock server response is deterministic for the test that is currently running. If using @ember-data/request add import { MockServerHandler } from '@warp-drive/holodeck'; to your request handlers.",
235
- source: { header: 'X-Test-Request-Number' },
236
- },
237
- ],
238
- })
239
- );
240
- }
241
-
242
- if (req.method === 'POST' && niceUrl === '__record') {
243
- const payload = await req.json();
244
- const { url, headers, method, status, statusText, body, response } = payload;
245
- const cacheKey = generateFilepath({
246
- projectRoot,
247
- testId,
248
- url,
249
- method,
250
- body,
251
- testRequestNumber,
252
- });
253
- const compressedResponse = compress(JSON.stringify(response));
254
- // allow Content-Type to be overridden
255
- headers['Content-Type'] = headers['Content-Type'] || 'application/vnd.api+json';
256
- // We always compress and chunk the response
257
- headers['Content-Encoding'] = 'br';
258
- // we don't cache since tests will often reuse similar urls for different payload
259
- headers['Cache-Control'] = 'no-store';
260
- // streaming requires Content-Length
261
- headers['Content-Length'] = compressedResponse.length;
262
-
263
- const cacheDir = generateFileDir({
264
- projectRoot,
265
- testId,
266
- url,
267
- method,
268
- testRequestNumber,
269
- });
270
-
271
- fs.mkdirSync(cacheDir, { recursive: true });
272
-
273
- if (isBun) {
274
- const newMetaFile = Bun.file(`${cacheKey}.meta.json`);
275
- await newMetaFile.write(JSON.stringify({ url, status, statusText, headers, method, requestBody: body }));
276
- const newBodyFile = Bun.file(`${cacheKey}.body.br`);
277
- await newBodyFile.write(compressedResponse);
278
- } else {
279
- fs.writeFileSync(
280
- `${cacheKey}.meta.json`,
281
- JSON.stringify({ url, status, statusText, headers, method, requestBody: body })
282
- );
283
- fs.writeFileSync(`${cacheKey}.body.br`, compressedResponse);
284
- }
285
-
286
- context.status(201);
287
- return context.body(
288
- JSON.stringify({
289
- message: `Recorded ${method} ${url} for test ${testId} request #${testRequestNumber}`,
290
- cacheKey,
291
- cacheDir,
292
- })
293
- );
294
- } else {
295
- const body = req.raw.body ? await req.text() : null;
296
- const cacheKey = generateFilepath({
297
- projectRoot,
298
- testId,
299
- url: niceUrl,
300
- method: req.method,
301
- body: body ? body : null,
302
- testRequestNumber,
303
- });
304
- return replayRequest(context, cacheKey);
305
- }
306
- } catch (e) {
307
- if (e instanceof HTTPException) {
308
- console.log(`HTTPException Encountered`);
309
- console.error(e);
310
- throw e;
311
- }
312
- console.log(`500 MOCK_SERVER_ERROR Encountered`);
313
- console.error(e);
314
- context.header('Content-Type', 'application/vnd.api+json');
315
- context.status(500);
316
- return context.body(
317
- JSON.stringify({
318
- errors: [
319
- {
320
- status: '500',
321
- code: 'MOCK_SERVER_ERROR',
322
- title: 'Mock Server Error during Request',
323
- detail: e.message,
324
- },
325
- ],
326
- })
327
- );
328
- }
329
- };
330
-
331
- return TestHandler;
332
- }
333
-
334
- export function startNodeServer() {
335
- const args = process.argv.slice();
336
-
337
- if (!isBun && args.length) {
338
- const options = JSON.parse(args[2]);
339
- _createServer(options);
340
- }
341
- }
342
-
343
- export function startWorker() {
344
- // listen for launch message
345
- globalThis.onmessage = async (event) => {
346
- console.log('starting holodeck worker');
347
- const { options } = event.data;
348
-
349
- const { server } = await _createServer(options);
350
-
351
- // listen for messages
352
- globalThis.onmessage = (event) => {
353
- const message = event.data;
354
- if (message === 'end') {
355
- server.close();
356
- globalThis.close();
357
- }
358
- };
359
- };
360
- }
361
-
362
- async function waitForLog(server, logMessage) {
363
- for await (const chunk of server.stdout) {
364
- process.stdout.write(chunk);
365
- const txt = new TextDecoder().decode(chunk);
366
- if (txt.includes(logMessage)) {
367
- return;
368
- }
369
- }
370
- }
371
-
372
- /*
373
- { port?: number, projectRoot: string }
374
- */
375
- export async function createServer(options, useBun = false) {
376
- if (!useBun) {
377
- const CURRENT_FILE = new URL(import.meta.url).pathname;
378
- const START_FILE = path.join(CURRENT_FILE, '../start-node.js');
379
- const server = Bun.spawn(['node', START_FILE, JSON.stringify(options)], {
380
- env: Object.assign({}, process.env, { FORCE_COLOR: 1 }),
381
- cwd: process.cwd(),
382
- stdin: 'inherit',
383
- stdout: 'pipe',
384
- stderr: 'inherit',
385
- });
386
-
387
- await waitForLog(server, 'Serving Holodeck HTTP Mocks');
388
-
389
- return {
390
- terminate() {
391
- server.kill();
392
- // server.unref();
393
- },
394
- };
395
- }
396
-
397
- console.log('starting holodeck worker');
398
- const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
399
-
400
- worker.postMessage({
401
- type: 'launch',
402
- options,
403
- });
404
-
405
- return new Promise((resolve) => {
406
- // @ts-expect-error
407
- worker.onmessage((v) => {
408
- console.log('worker message received', v);
409
- if (v.data === 'launched') {
410
- resolve(worker);
411
- }
412
- });
413
- });
414
- }
415
-
416
- async function _createServer(options) {
417
- const { CERT, KEY } = await getCertInfo();
418
- const app = new Hono();
419
- if (DEBUG) {
420
- app.use('*', logger());
421
- }
422
- app.use(
423
- '*',
424
- cors({
425
- origin: (origin) =>
426
- origin.startsWith('http://localhost:') || origin.startsWith('https://localhost:') ? origin : '*',
427
- allowHeaders: ['Accept', 'Content-Type'],
428
- allowMethods: ['GET', 'HEAD', 'OPTIONS', 'PUT', 'POST', 'DELETE', 'PATCH'],
429
- exposeHeaders: ['Content-Length', 'Content-Type'],
430
- maxAge: 60_000,
431
- credentials: false,
432
- })
433
- );
434
- app.all('*', createTestHandler(options.projectRoot));
435
-
436
- const server = serve({
437
- overrideGlobalObjects: !isBun,
438
- fetch: app.fetch,
439
- serverOptions: {
440
- key: KEY,
441
- cert: CERT,
442
- },
443
- createServer: createSecureServer,
444
- port: options.port ?? DEFAULT_PORT,
445
- hostname: options.hostname ?? 'localhost',
446
- });
447
-
448
- console.log(
449
- `\tServing Holodeck HTTP Mocks from ${chalk.yellow('https://') + chalk.magenta((options.hostname ?? 'localhost') + ':') + chalk.yellow(options.port ?? DEFAULT_PORT)}\n`
450
- );
451
-
452
- if (typeof threadId === 'number' && threadId !== 0) {
453
- parentPort.postMessage('launched');
454
- }
455
-
456
- return { app, server };
457
- }
458
-
459
- /** @type {Map<string, Awaited<ReturnType<typeof createServer>>>} */
460
- const servers = new Map();
4
+ let closeHandler = () => {};
461
5
 
462
6
  export default {
463
7
  async launchProgram(config = {}) {
@@ -466,41 +10,33 @@ export default {
466
10
  (pkg) => pkg.name
467
11
  );
468
12
  const options = { name, projectRoot, ...config };
469
- console.log(
470
- chalk.grey(
471
- `\n\t@${chalk.greenBright('warp-drive')}/${chalk.magentaBright(
472
- 'holodeck'
473
- )} 🌅\n\t=================================\n`
474
- ) +
475
- chalk.grey(
476
- `\n\tHolodeck Access Granted\n\t\tprogram: ${chalk.magenta(name)}\n\t\tsettings: ${chalk.green(JSON.stringify(config).split('\n').join(' '))}\n\t\tdirectory: ${chalk.cyan(projectRoot)}\n\t\tengine: ${chalk.cyan(
477
- isBun ? 'bun@' + Bun.version : 'node'
478
- )}`
479
- )
480
- );
481
- console.log(chalk.grey(`\n\tStarting Holodeck Subroutines (mode:${chalk.cyan(isBun ? 'bun' : 'node')})`));
482
13
 
483
- if (servers.has(projectRoot)) {
484
- throw new Error(`Holodeck is already running for project '${name}' at '${projectRoot}'`);
14
+ if (!isBun) {
15
+ // @ts-expect-error
16
+ options.useWorker = config.useWorker ?? true;
17
+ const nodeImpl = await import('./node.js');
18
+ const program = await nodeImpl.launchProgram(options);
19
+ closeHandler = program.endProgram;
20
+ return program.config;
485
21
  }
486
22
 
487
- // toggle to true if Bun fixes CORS support for HTTP/2
488
- const project = await createServer(options, false);
489
- servers.set(projectRoot, project);
490
- },
491
- async endProgram() {
492
- console.log(chalk.grey(`\n\tEnding Holodeck Subroutines (mode:${chalk.cyan(isBun ? 'bun' : 'node')})`));
493
- const projectRoot = process.cwd();
494
-
495
- if (!servers.has(projectRoot)) {
496
- const name = require(path.join(projectRoot, 'package.json')).name;
497
- console.log(chalk.red(`\n\nHolodeck was not running for project '${name}' at '${projectRoot}'\n\n`));
498
- return;
23
+ // if we are bun but should use node
24
+ if (!config.useBun) {
25
+ const compatImpl = await import('./compat-shim.js');
26
+ const program = await compatImpl.launchProgram(options);
27
+ closeHandler = program.endProgram;
28
+ return program.config;
499
29
  }
500
30
 
501
- const project = servers.get(projectRoot);
502
- servers.delete(projectRoot);
503
- project.terminate();
504
- console.log(chalk.grey(`\n\tHolodeck program ended`));
31
+ // use bun
32
+ // @ts-expect-error
33
+ options.useWorker = config.useWorker ?? true;
34
+ const nodeImpl = await import('./bun.js');
35
+ const program = await nodeImpl.launchProgram(options);
36
+ closeHandler = program.endProgram;
37
+ return program.config;
38
+ },
39
+ async endProgram() {
40
+ closeHandler();
505
41
  },
506
42
  };
@@ -0,0 +1,12 @@
1
+ import { launchProgram } from './node.js';
2
+
3
+ function startNodeServer() {
4
+ const args = process.argv.slice();
5
+
6
+ if (args.length) {
7
+ const options = JSON.parse(args[2]);
8
+ launchProgram(options);
9
+ }
10
+ }
11
+
12
+ startNodeServer();
@@ -0,0 +1,3 @@
1
+ import { startWorker } from './node.js';
2
+
3
+ startWorker();