-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
606 lines (587 loc) · 18.3 KB
/
index.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
const { URLSearchParams } = require('node:url');
const entryModule = require('./extension.js');
const { origins } = entryModule;
const { request: httpsRequest } = require('node:https');
const { join } = require('node:path');
const { CustomCacheKey } = require('./CustomCacheKey'); // to re-export
const send = require('send');
const { readFileSync, existsSync } = require('node:fs');
const { Readable } = require('stream');
const manifest = new Map();
// Handle route manifest first since it contains the dynamic routes in Next.js v9
const ROUTE_MANIFEST = '.next/routes-manifest.json';
try {
if (existsSync(ROUTE_MANIFEST)) {
const routesManifest = JSON.parse(readFileSync(ROUTE_MANIFEST, 'utf8'));
for (const route of routesManifest.dynamicRoutes ?? []) {
manifest.set(route.page, new RegExp(route.regex));
}
// Later Next.js versions also have static routes in the route manifest
for (const route of routesManifest.staticRoutes ?? []) {
manifest.set(route.page, new RegExp(route.regex));
}
}
} catch (error) {
console.error('Could not read routes manifest', error);
}
// Anything not in the route manifest will be in the pages manifest
const PATHS_TO_PATHS_FILES = ['.next/serverless/pages-manifest.json', '.next/server/app-paths-routes-manifest.json'];
for (let path of PATHS_TO_PATHS_FILES) {
try {
if (!existsSync(path)) continue;
const pagesManifest = JSON.parse(readFileSync(path, 'utf8'));
for (let key in pagesManifest) {
if (!manifest.has(key)) {
manifest.set(key, new RegExp(`^${key}$`));
}
}
} catch (error) {
console.error(`Could not read ${path} manifest`, error);
}
}
/**
* The main router class for defining a set of routes and their handlers.
*/
class Router {
rules = [];
get(path, options) {
if (path.path) path = path.path;
this.rules.push(new Rule({ path, method: 'GET' }, options));
return this;
}
post(path, options) {
if (path.path) path = path.path;
this.rules.push(new Rule({ path, method: 'POST' }, options));
return this;
}
use() {
this.rules.push(new Rule(null, { useNext: true }));
return this;
}
match(match, options) {
let router = this.currentRouter || this;
router.rules.push(new Rule(match, options));
return this;
}
matchAny(matches, options) {
for (let match of matches) {
this.rules.push(new Rule(match, options));
}
return this;
}
fallback(options) {
return this.match(null, options);
}
always(options) {
return this.match(null, options);
}
destination(originName, router) {
const originConfig = getOriginConfig(originName);
return this.match(
originConfig.hostname
? {
headers: {
Host: originConfig.hostname,
},
}
: null,
router
);
}
catch(statusCode, handler) {
// TODO: Define status code handlers
return this;
}
if(condition, handler) {
let conditionalRouter = new Router();
this.rules.push(new Rule(condition, conditionalRouter));
this.currentRouter = conditionalRouter;
return this;
}
/**
* For each incoming request, perform routing based on the defined rules, returning a function that can be called
* to process the request. This will determine if and what the cache key is, so that caching can be attempted
* before routing to the main handler function.
* @param request
*/
async onRequest(request, nextHandler) {
let foundRule = false;
const nodeResponse = request._nodeResponse;
let responseHeaders = {};
for (let rule of this.rules) {
if (rule.match(request)) {
if (rule.isFallback && foundRule) continue;
foundRule = true;
if (rule.router) {
return rule.router.onRequest(request, nextHandler);
}
let actions;
if (rule.handler) {
actions = new RequestActions(request);
// I believe the handler is supposed to be executed on each request, but not sure
let result = rule.handler(actions);
if (result) {
if (result.caching) actions.setCaching(result.caching);
if (result.origin) actions.setProxying(result.origin);
}
} else {
actions = rule.actions;
}
const headers = actions.headers;
if (headers) {
if (headers.set_response_headers) {
for (let key in headers.set_response_headers) {
let value = headers.set_response_headers[key];
responseHeaders[key] = value;
}
}
if (headers.add_response_headers) {
for (let key in headers.add_response_headers) {
let value = headers.add_response_headers[key];
responseHeaders[key] = value;
}
}
if (headers.remove_response_headers) {
for (let key in headers.remove_response_headers) {
delete responseHeaders[key];
}
}
if (headers.set_client_ip_custom_header) {
responseHeaders[headers.set_client_ip_custom_header] = request.ip;
}
}
if (actions.redirecting) {
return () => ({
status: actions.redirecting.status,
headers: { ...responseHeaders, Location: actions.redirecting.location },
});
}
if (actions.caching) {
const caching = actions.caching;
let cacheControlDirectives = [];
if (caching.maxAgeSeconds || caching.staleWhileRevalidateSeconds) {
// enable caching, set a cache key
let keyParts = [request.pathname];
if (caching.cache_key?.include_query_params) {
const queryStart = request.url.indexOf('?');
if (queryStart !== -1) {
const query = request.url.slice(queryStart + 1);
new URLSearchParams(query).forEach((value, key) => {
if (caching.cache_key.include_query_params.includes(key)) {
keyParts.push(`${key}=${value}`);
}
});
}
} else {
// this is a bit of a mystery to me, do we include the query params in the cache key or not if
// they're not specified? (using request.url instead of request.pathname will include the query params)
keyParts = [request.url];
}
if (caching.cache_key?.include_headers) {
keyParts = keyParts ?? [];
for (let header of caching.cache_key?.include_headers) {
keyParts.push(`${header}=${request.headers.get(header)}`);
}
}
if (caching.cache_key?.include_cookies) {
keyParts = keyParts ?? [];
const cookie = request.headers.get('cookie');
const cookies = cookie?.split(/;\s+/) || [];
for (const cookie of cookies || []) {
const [name, value] = cookie.split('=');
if (caching.cache_key.include_cookies.includes(name)) {
keyParts.push(`${name}=${value}`);
}
}
}
request.maxAgeSeconds = caching.maxAgeSeconds;
// disable SWR for now
// request.staleWhileRevalidateSeconds = caching.staleWhileRevalidateSeconds;
if (caching.maxAgeSeconds) cacheControlDirectives.push(`s-maxage=${caching.maxAgeSeconds}`);
request.cacheKey = keyParts.join('&');
// let the caching layer handle the headers
}
if (caching.forcePrivateCaching) cacheControlDirectives.push('private');
if (caching.clientMaxAgeSeconds !== undefined) {
if (!caching.clientMaxAgeSeconds && !caching.maxAgeSeconds)
cacheControlDirectives.push('no-store', 'no-cache', 'must-revalidate');
else cacheControlDirectives.push(`max-age=${caching.clientMaxAgeSeconds}`);
}
responseHeaders['Cache-Control'] = cacheControlDirectives.join(', ');
} else if (actions.caching === false) {
request.cacheKey = undefined;
}
const proxying = actions.proxying;
if (proxying) {
// proxy the request, first get the origin hostname
const originName = typeof proxying === 'string' ? proxying : (proxying.set_origin ?? proxying.origin);
const originConfig = getOriginConfig(originName);
const originHostname = originConfig.hostname;
if (!originHostname) throw new Error('No hostname found for origin');
let url = request.url;
if (actions.url?.url_rewrite) {
for (let rewrite of actions.url.url_rewrite) {
if (rewrite.syntax === 'regexp') {
url = url.replace(new RegExp(rewrite.source), rewrite.destination);
}
}
} else if (proxying.path) {
const param = rule.condition.path.exec(request.pathname)[1];
url = proxying.path.replace(/:[\w\*\+]+/, param) + request.url.slice(request.pathname.length);
}
const headers = request.headers.asObject;
delete headers.host;
delete headers.Host;
if (originConfig.hostHeader) headers.Host = originConfig.hostHeader;
const requestOptions = {
timeout: 60000,
hostname: originHostname,
path: url,
method: request.method,
headers,
};
requestOptions.rejectUnauthorized = originConfig.rejectUnauthorized;
if (originConfig.servername) requestOptions.servername = originConfig.servername;
return () => {
return new Promise((resolve, reject) => {
try {
let proxiedRequest = httpsRequest(requestOptions, (response) => {
logger.info(
'Received proxied response for',
request.url,
response.statusCode,
JSON.stringify(response.headers)
);
if (actions.update_response_headers) {
// this is a series of regular expression replacements, applied successively
for (let { name, match, replacement } of actions.update_response_headers || []) {
if (!(match instanceof RegExp)) match = new RegExp(match);
let previousValue = response.headers[name];
if (previousValue) {
response.headers[name] = previousValue.replace(match, replacement);
}
}
}
const headers = {
...responseHeaders,
...response.headers,
};
delete headers.connection;
logger.info(
`Returning proxied response for ${request.url} from ${originHostname}`,
JSON.stringify(headers)
);
nodeResponse.writeHead(response.statusCode, response.statusMessage, headers);
response
.pipe(nodeResponse)
.on('finish', () => {
resolve();
logger.info(`Finished proxied response for ${request.url}`);
})
.on('error', (error) => {
logger.warn(`Error in sending proxied body ${request.url}`, error);
reject(error);
});
}).on('error', (error) => {
logger.warn(`Error in proxying request ${request.url}`, error);
reject(error);
});
logger.info(`Sending proxied request for ${request.url} to ${originHostname}`);
if (request.method !== 'GET' && request.method !== 'HEAD') {
streamToBuffer(request._nodeRequest).then((buffer) => {
proxiedRequest.end(buffer);
});
} else proxiedRequest.end();
} catch (error) {
logger.warn(`Error preparing proxying request ${request.url}`, error);
reject(error);
}
});
};
}
if (actions.servingStaticPath) {
return () =>
new Promise((resolve, reject) => {
const param = rule.condition.path.exec(request.pathname)[1];
let path = decodeURIComponent(actions.servingStaticPath.replace(/:[\w\*\+]+/, param));
for (let key in responseHeaders) {
nodeResponse.setHeader(key, responseHeaders[key]);
}
send(request, path, {
dotfiles: 'allow',
root: entryModule.baseDir,
})
.pipe(nodeResponse)
.on('finish', () => resolve())
.on('error', reject);
});
}
if (actions.useNext) {
if (Array.from(manifest.values()).some((path) => path.test(request.pathname))) {
for (let key in responseHeaders) {
nodeResponse.setHeader(key, responseHeaders[key]);
}
return nextHandler;
}
}
}
}
return nextHandler;
}
}
exports.Router = Router;
exports.CustomCacheKey = CustomCacheKey;
class Rule {
condition = {};
actions = new RequestActions();
constructor(condition, options) {
if (condition == null) this.condition = null;
else if (typeof condition === 'string' || condition instanceof RegExp || condition?.not) {
this.condition.path = stringToRegex(condition);
} else {
let path = condition.path;
if (path) {
this.condition.path = stringToRegex(path);
}
if (condition.query) {
for (let name in condition.query) {
condition.query[name] = stringToRegex(condition.query[name]);
}
this.condition.query = condition.query;
}
if (condition.headers) {
for (let name in condition.headers) {
condition.headers[name] = stringToRegex(condition.headers[name]);
}
this.condition.headers = condition.headers;
}
}
if (options instanceof Router) {
this.router = options;
return;
}
if (options.caching) {
this.actions.setCaching(options.caching);
}
if (options.origin) {
this.actions.setProxying(options.origin);
}
if (options.headers) this.actions.headers = options.headers;
if (typeof options === 'function') {
this.handler = options;
} else {
Object.assign(this.actions, options);
}
}
/**
* Determine if the rule matches the request
* @param request
* @return {boolean}
*/
match(request) {
if (this.condition == null) return true;
if (this.condition.path) {
if (!this.condition.path.test(request.pathname)) {
return false;
}
}
const query = this.condition.query;
if (query) {
let requestQuery = new URLSearchParams(request.url);
for (let key in query) {
if (!query[key].test(requestQuery.get(key))) {
return false;
}
}
}
const headers = this.condition.headers;
if (headers) {
for (let key in headers) {
if (!headers[key].test(request.headers.get(key))) {
return false;
}
}
}
return true;
}
}
class RequestActions {
constructor(request) {
this.request = request;
}
// we do theses as a getters, because the function is accessed through destructuring and called without its
// context/this
get setResponseHeader() {
// This should also work with middleware that returns a response object, but that's not how the
// next.js middleware works
let actions = this;
return (key, value) => {
const headers = actions.headers || (actions.headers = {});
if (headers) {
if (!headers.set_response_headers) headers.set_response_headers = {};
headers.set_response_headers[key] = value;
}
};
}
get cache() {
let actions = this;
return (options) => {
let caching = actions.caching ?? (actions.caching = {});
if (options.edge) {
caching.maxAgeSeconds = options.edge.maxAgeSeconds;
caching.staleWhileRevalidateSeconds = options.edge.staleWhileRevalidateSeconds;
caching.forcePrivateCaching = options.edge.forcePrivateCaching;
} else if (options.edge === false) actions.caching = false;
if (options.browser) {
if (options.browser.maxAgeSeconds != null) {
caching.clientMaxAgeSeconds = options.browser.maxAgeSeconds;
}
} else if (options.browser === false) {
caching.clientMaxAgeSeconds = 0;
}
if (options.key) {
if (!actions.caching) actions.caching = {};
actions.caching.cache_key = options.key;
}
};
}
get serveStatic() {
let actions = this;
return (path) => {
actions.servingStaticPath = path;
};
}
setCaching(caching) {
if (caching.max_age)
caching.maxAgeSeconds = convertToMS(
typeof caching.max_age === 'object' ? caching.max_age['200'] : caching.max_age
);
if (caching.client_max_age) caching.clientMaxAgeSeconds = convertToMS(caching.client_max_age);
if (caching.stale_while_revalidate)
caching.staleWhileRevalidateSeconds = convertToMS(caching.stale_while_revalidate);
this.caching = caching;
}
setProxying(origin) {
this.proxying = origin;
}
get proxy() {
let actions = this;
return (path, options) => {
actions.proxying = {
origin: path,
...options,
};
};
}
get redirect() {
let actions = this;
return (location, status) => {
actions.redirecting = { location, status };
};
}
get updateResponseHeader() {
let actions = this;
return (name, match, replacement) => {
actions.update_response_headers = actions.update_response_headers || [];
actions.update_response_headers.push({ name, match, replacement });
};
}
async run(handler) {
if (this.redirect) {
return {
status: this.redirect.status,
headers: { Location: this.redirect.location },
};
}
let response = await handler(this.request);
if (response) {
if (this.responseHeaders) {
for (let [key, value] of this.responseHeaders) {
response.headers.set(key, value);
}
}
if (this.maxAgeSeconds) {
response.headers.set('Cache-Control', `max-age=${this.maxAgeSeconds}`);
}
}
return response;
}
}
exports.or = function (...conditions) {
return new OrRule(conditions);
};
exports.nextRoutes = {}; // I think this is for the next.js routes
class OrRule {
constructor(conditions) {
this.conditions = conditions;
}
match(request) {
for (let condition of this.conditions) {
if (condition.match(request)) {
return true;
}
}
}
}
function getOriginConfig(origin_name) {
if (!origin_name) throw new Error('No origin name provided');
const origin_config = origins.get(origin_name);
if (!origin_config && origin_name === 'pwa') return {}; // special catchall to go the local origin, I guess?
if (!origin_config) throw new Error(`Origin "${origin_name}" not found`);
return origin_config;
}
function stringToRegex(str) {
if (str instanceof RegExp) return str;
if (typeof str === 'string') {
return new RegExp(
'^' +
str.replace(/:[^/]+\*?/g, (match) => {
if (match.endsWith('*')) {
return '(.*)';
} else {
return '([^/]+)';
}
}) +
'$'
);
} else if (str.not) {
const regex = stringToRegex(str.not);
return {
test(value) {
return !stringToRegex(regex).test(value);
},
};
} else throw new TypeError('Unknown type of matching requests ' + str);
}
function convertToMS(interval) {
let seconds = 0;
if (typeof interval === 'number') seconds = interval;
if (typeof interval === 'string') {
seconds = parseFloat(interval);
switch (interval.slice(-1)) {
case 'M':
seconds *= 86400 * 30;
break;
case 'D':
case 'd':
seconds *= 86400;
break;
case 'H':
case 'h':
seconds *= 3600;
break;
case 'm':
seconds *= 60;
break;
}
}
return seconds * 1000;
}
function streamToBuffer(stream) {
return new Promise((resolve, reject) => {
const buffers = [];
stream.on('data', (data) => buffers.push(data));
stream.on('end', () => resolve(Buffer.concat(buffers)));
stream.on('error', reject);
});
}