-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver.ts
227 lines (206 loc) · 6.67 KB
/
server.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Koa from 'koa';
import cors from '@koa/cors';
import Router from '@koa/router';
import { Memoize } from 'typescript-memoize';
import {
Realm,
baseRealm,
assetsDir,
logger,
SupportedMimeType,
} from '@cardstack/runtime-common';
import { webStreamToText } from '@cardstack/runtime-common/stream';
import { setupCloseHandler } from './node-realm';
import {
livenessCheck,
healthCheck,
httpLogging,
httpBasicAuth,
ecsMetadata,
assetRedirect,
rootRealmRedirect,
fullRequestURL,
} from './middleware';
import convertAcceptHeaderQueryParam from './middleware/convert-accept-header-qp';
import { monacoMiddleware } from './middleware/monaco';
import './lib/externals';
import { nodeStreamToText } from './stream';
import mime from 'mime-types';
import { extractSupportedMimeType } from '@cardstack/runtime-common/router';
import * as Sentry from '@sentry/node';
interface Options {
assetsURL?: URL;
}
export class RealmServer {
private assetsURL: URL;
private log = logger('realm:requests');
constructor(
private realms: Realm[],
opts?: Options,
) {
detectRealmCollision(realms);
this.realms = realms;
// defaults to using the base realm to host assets (this is the dev env default)
// All realms should have URL mapping for the base realm
this.assetsURL =
opts?.assetsURL ??
realms[0].loader.resolve(`${baseRealm.url}${assetsDir}`);
}
@Memoize()
get app() {
let router = new Router();
router.head('/', livenessCheck);
router.get(
'/',
healthCheck,
this.serveIndex(),
rootRealmRedirect(this.realms),
this.serveFromRealm,
);
let app = new Koa<Koa.DefaultState, Koa.Context>()
.use(httpLogging)
.use(ecsMetadata)
.use(
cors({
origin: '*',
allowHeaders:
'Authorization, Content-Type, If-Match, X-Requested-With, X-Boxel-Client-Request-Id',
}),
)
.use(async (ctx, next) => {
// Disable browser cache for all data requests to the realm server. The condition captures our supported mime types but not others,
// such as assets, which we probably want to cache.
let mimeType = extractSupportedMimeType(
ctx.header.accept as unknown as null | string | [string],
);
if (
Object.values(SupportedMimeType).includes(
mimeType as SupportedMimeType,
)
) {
ctx.set('Cache-Control', 'no-store, no-cache, must-revalidate');
}
await next();
})
.use(monacoMiddleware(this.assetsURL))
.use(assetRedirect(this.assetsURL))
.use(convertAcceptHeaderQueryParam)
.use(httpBasicAuth)
.use(rootRealmRedirect(this.realms))
.use(router.routes())
.use(this.serveFromRealm);
app.on('error', (err, ctx) => {
Sentry.withScope((scope) => {
scope.setSDKProcessingMetadata({ request: ctx.request });
Sentry.captureException(err);
});
});
return app;
}
listen(port: number) {
let instance = this.app.listen(port);
this.log.info(`Realm server listening on port %s\n`, port);
return instance;
}
private serveIndex(): (ctxt: Koa.Context, next: Koa.Next) => Promise<void> {
return async (ctxt: Koa.Context, next: Koa.Next) => {
if (ctxt.header.accept?.includes('text/html') && this.realms.length > 0) {
ctxt.type = 'html';
ctxt.body = await this.realms[0].getIndexHTML({
realmsServed: this.realms.map((r) => r.url),
});
return;
}
return next();
};
}
private serveFromRealm = async (ctxt: Koa.Context, _next: Koa.Next) => {
if (ctxt.request.path === '/_boom') {
throw new Error('boom');
}
let realm = this.realms.find((r) => {
let reversedResolution = r.loader.reverseResolution(
fullRequestURL(ctxt).href,
);
this.log.debug(
`Looking for realm to handle request with full URL: ${
fullRequestURL(ctxt).href
} (reversed: ${reversedResolution.href})`,
);
let inRealm = r.paths.inRealm(reversedResolution);
this.log.debug(
`${reversedResolution} in realm ${JSON.stringify({
url: r.url,
paths: r.paths,
})}: ${inRealm}`,
);
return inRealm;
});
if (!realm) {
ctxt.status = 404;
return;
}
let reqBody: string | undefined;
if (['POST', 'PATCH'].includes(ctxt.method)) {
reqBody = await nodeStreamToText(ctxt.req);
}
let reversedResolution = realm.loader.reverseResolution(
fullRequestURL(ctxt).href,
);
let request = new Request(reversedResolution.href, {
method: ctxt.method,
headers: ctxt.req.headers as { [name: string]: string },
...(reqBody ? { body: reqBody } : {}),
});
setupCloseHandler(ctxt.res, request);
let realmResponse = await realm.handle(request);
let { status, statusText, headers, body, nodeStream } = realmResponse;
ctxt.status = status;
ctxt.message = statusText;
for (let [header, value] of headers.entries()) {
ctxt.set(header, value);
}
if (!headers.get('content-type')) {
let fileName = reversedResolution.href.split('/').pop()!;
ctxt.type = mime.lookup(fileName) || 'application/octet-stream';
}
if (nodeStream) {
ctxt.body = nodeStream;
} else if (body instanceof ReadableStream) {
// A quirk with native fetch Response in node is that it will be clever
// and convert strings or buffers in the response.body into web-streams
// automatically. This is not to be confused with actual file streams
// that the Realm is creating. The node HTTP server does not play nice
// with web-streams, so we will read these streams back into strings and
// then include in our node ServerResponse. Actual node file streams
// (i.e streams that we are intentionally creating in the Realm) will
// not be handled here--those will be taken care of above.
ctxt.body = await webStreamToText(body);
} else if (body != null) {
ctxt.body = body;
}
};
}
function detectRealmCollision(realms: Realm[]): void {
let collisions: string[] = [];
let realmsURLs = realms.map(({ url }) => ({
url,
path: new URL(url).pathname,
}));
for (let realmA of realmsURLs) {
for (let realmB of realmsURLs) {
if (realmA.path.length > realmB.path.length) {
if (realmA.path.startsWith(realmB.path)) {
collisions.push(`${realmA.url} collides with ${realmB.url}`);
}
}
}
}
if (collisions.length > 0) {
throw new Error(
`Cannot start realm server--realm route collisions detected: ${JSON.stringify(
collisions,
)}`,
);
}
}