-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathssr-entry-hono.ts
106 lines (90 loc) · 3.57 KB
/
ssr-entry-hono.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
import assert from 'node:assert';
import { setTimeout } from 'node:timers/promises';
import { replaceInFile, writeMultipleFiles } from '../../utils/fs';
import { ng, silentNg, waitForAnyProcessOutputToMatch } from '../../utils/process';
import { installPackage, installWorkspacePackages, uninstallPackage } from '../../utils/packages';
import { ngServe, useSha } from '../../utils/project';
import { getGlobalVariable } from '../../utils/env';
export default async function () {
assert(
getGlobalVariable('argv')['esbuild'],
'This test should not be called in the Webpack suite.',
);
// Forcibly remove in case another test doesn't clean itself up.
await uninstallPackage('@angular/ssr');
await ng('add', '@angular/ssr', '--server-routing', '--skip-confirmation', '--skip-install');
await useSha();
await installWorkspacePackages();
await installPackage('hono@4');
await writeMultipleFiles({
// Replace the template of app.component.html as it makes it harder to debug
'src/app/app.component.html': '<router-outlet />',
'src/app/app.routes.ts': `
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
export const routes: Routes = [
{ path: 'home', component: HomeComponent }
];
`,
'src/app/app.routes.server.ts': `
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{ path: '**', renderMode: RenderMode.Server }
];
`,
'src/server.ts': `
import { AngularAppEngine, createRequestHandler } from '@angular/ssr';
import { Hono } from 'hono';
export function app() {
const server = new Hono();
const angularAppEngine = new AngularAppEngine();
server.get('/api/*', (c) => c.json({ hello: 'foo' }));
server.get('/*', async (c) => {
const res = await angularAppEngine.handle(c.req.raw);
return res || undefined
});
return server;
}
const server = app();
export const reqHandler = createRequestHandler(server.fetch);
`,
});
await silentNg('generate', 'component', 'home');
const port = await ngServe();
// Verify the server is running and the API response is correct.
await validateResponse('/main.js', /bootstrapApplication/);
await validateResponse('/api/test', /foo/);
await validateResponse('/home', /home works/);
// Modify the home component and validate the change.
await modifyFileAndWaitUntilUpdated(
'src/app/home/home.component.html',
'home works',
'yay home works!!!',
true,
);
await validateResponse('/api/test', /foo/);
await validateResponse('/home', /yay home works/);
// Modify the API response and validate the change.
await modifyFileAndWaitUntilUpdated('src/server.ts', `{ hello: 'foo' }`, `{ hello: 'bar' }`);
await validateResponse('/api/test', /bar/);
await validateResponse('/home', /yay home works/);
async function validateResponse(pathname: string, match: RegExp): Promise<void> {
const response = await fetch(new URL(pathname, `http://localhost:${port}`));
const text = await response.text();
assert.match(text, match);
assert.equal(response.status, 200);
}
}
async function modifyFileAndWaitUntilUpdated(
filePath: string,
searchValue: string,
replaceValue: string,
hmr = false,
): Promise<void> {
await Promise.all([
waitForAnyProcessOutputToMatch(
hmr ? /Component update sent to client/ : /Page reload sent to client/,
),
setTimeout(100).then(() => replaceInFile(filePath, searchValue, replaceValue)),
]);
}