-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvirtual-network.ts
235 lines (204 loc) · 7.64 KB
/
virtual-network.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
228
229
230
231
232
233
234
235
import { RealmPaths } from './paths';
import {
PackageShimHandler,
PACKAGES_FAKE_ORIGIN,
} from './package-shim-handler';
import type { Readable } from 'stream';
import { simulateNetworkBehaviors } from './fetcher';
export interface ResponseWithNodeStream extends Response {
nodeStream?: Readable;
}
import { time } from './helpers/time';
export type Handler = (req: Request) => Promise<ResponseWithNodeStream | null>;
export class VirtualNetwork {
private handlers: Handler[] = [];
private urlMappings: [string, string][] = [];
constructor(nativeFetch = globalThis.fetch.bind(globalThis)) {
this.nativeFetch = nativeFetch;
this.mount(this.packageShimHandler.handle);
}
resolveImport = (moduleIdentifier: string) => {
if (!isUrlLike(moduleIdentifier)) {
moduleIdentifier = new URL(moduleIdentifier, PACKAGES_FAKE_ORIGIN).href;
}
return moduleIdentifier;
};
private packageShimHandler = new PackageShimHandler(this.resolveImport);
shimModule(moduleIdentifier: string, module: Record<string, any>) {
this.packageShimHandler.shimModule(moduleIdentifier, module);
}
addURLMapping(from: URL, to: URL) {
this.urlMappings.push([from.href, to.href]);
}
private nativeFetch: typeof globalThis.fetch;
private resolveURLMapping(
url: string,
direction: 'virtual-to-real' | 'real-to-virtual',
): string | undefined {
let absoluteURL = new URL(url);
for (let [virtual, real] of this.urlMappings) {
let sourcePath = new RealmPaths(
new URL(direction === 'virtual-to-real' ? virtual : real),
);
if (sourcePath.inRealm(absoluteURL)) {
let toPath = new RealmPaths(
new URL(direction === 'virtual-to-real' ? real : virtual),
);
if (absoluteURL.href.endsWith('/')) {
return toPath.directoryURL(sourcePath.local(absoluteURL)).href;
} else {
let local = sourcePath.local(absoluteURL, {
preserveQuerystring: true,
});
let resolved = toPath.fileURL(local).href;
// A special case for root realm urls with missing trailing slash, for
// example http://localhost:4201/base – we want the mapped url also not to have a trailing slash
// (so that the realm handler knows it needs to redirect to the correct url with a trailing slash)
if (local === '' && !absoluteURL.pathname.endsWith('/')) {
resolved = resolved.replace(/\/$/, '');
}
return resolved;
}
}
}
return undefined;
}
mount(handler: Handler, opts?: { prepend: boolean }) {
if (opts?.prepend) {
this.handlers.unshift(handler);
} else {
this.handlers.push(handler);
}
}
unmount(handler: Handler) {
this.handlers = this.handlers.filter((h) => h !== handler);
}
fetch: typeof fetch = async (
urlOrRequest: string | URL | Request,
init?: RequestInit,
) => {
let request =
urlOrRequest instanceof Request
? urlOrRequest
: new Request(urlOrRequest, init);
let response = await this.runFetch(request, init);
if (response.url !== request.url) {
Object.defineProperty(response, 'url', {
value:
this.resolveURLMapping(response.url, 'real-to-virtual') ??
response.url,
});
}
return response;
};
// This method is used to handle the boundary between the real and virtual network,
// when a request is made to the realm from the realm server - it maps requests
// by changing their URL from real to virtual, as defined in the url mapping config
// (e.g http://localhost:4201/base to https://cardstack.com/base) so that the realms
// that have a virtual URL know that they are being requested
async handle(
request: Request,
onMappedRequest?: (request: Request) => void,
): Promise<ResponseWithNodeStream> {
let internalRequest = await this.mapRequest(request, 'real-to-virtual');
if (onMappedRequest) {
onMappedRequest(internalRequest);
}
for (let handler of this.handlers) {
let response = await handler(internalRequest);
if (response) {
this.mapRedirectionURL(response);
return response;
}
}
return new Response(undefined, { status: 404 });
}
private async mapRequest(
request: Request,
direction: 'virtual-to-real' | 'real-to-virtual',
) {
let remappedUrl = await time('mapRequest:resolveURLMapping', () =>
this.resolveURLMapping(request.url, direction),
);
if (remappedUrl) {
return await time('mapRequest:buildRequest', () =>
buildRequest(remappedUrl, request),
);
} else {
return request;
}
}
private mapRedirectionURL(response: Response): void {
if (response.status > 300 && response.status < 400) {
let redirectionURL = response.headers.get('Location')!;
let isRelativeRedirectionURL = !/^[a-z][a-z0-9+.-]*:|\/\//i.test(
redirectionURL,
); // doesn't start with a protocol scheme and "//" (e.g., "http://", "https://", "//")
let finalRedirectionURL;
if (isRelativeRedirectionURL) {
finalRedirectionURL = redirectionURL;
} else {
let remappedRedirectionURL = this.resolveURLMapping(
redirectionURL,
'virtual-to-real',
);
finalRedirectionURL = remappedRedirectionURL || redirectionURL;
}
response.headers.set('Location', finalRedirectionURL);
}
}
private async runFetch(request: Request, init?: RequestInit) {
for (let handler of this.handlers) {
let response = await handler(request);
if (response) {
return await simulateNetworkBehaviors(request, response, this.fetch);
}
}
let internalRequest = await this.mapRequest(request, 'virtual-to-real');
return await this.nativeFetch(internalRequest, init);
}
createEventSource(url: string) {
let mappedUrl = this.resolveURLMapping(url, 'virtual-to-real');
return new EventSource(mappedUrl || url);
}
}
function isUrlLike(moduleIdentifier: string): boolean {
return (
moduleIdentifier.startsWith('.') ||
moduleIdentifier.startsWith('/') ||
moduleIdentifier.startsWith('http://') ||
moduleIdentifier.startsWith('https://')
);
}
async function buildRequest(url: string, originalRequest: Request) {
if (url === originalRequest.url) {
return originalRequest;
}
// To reach the goal of creating a new Request but with a different url it is
// usually enough to create a new Request object with the new url and the same
// properties as the original request, but there are issues when the body is
// a ReadableStream - Chrome browser, for example, reports the following error:
// "TypeError: Failed to construct 'Request': The `duplex` member must be
// specified for a request with a streaming body." Even adding the `duplex`
// property will not fix the issue - the browser request being made to
// our local server then expects HTTP/2 connection which is currently not
// supported in our local server. To avoid all these issues, we resort to
// reading the body of the original request and creating a new Request with
// the new url and the body as a Uint8Array.
let body = null;
if (['POST', 'PUT', 'PATCH'].includes(originalRequest.method)) {
body = await originalRequest.clone().text();
}
return new Request(url, {
method: originalRequest.method,
headers: originalRequest.headers,
body,
referrer: originalRequest.referrer,
referrerPolicy: originalRequest.referrerPolicy,
mode: originalRequest.mode,
credentials: originalRequest.credentials,
cache: originalRequest.cache,
redirect: originalRequest.redirect,
integrity: originalRequest.integrity,
});
}