@eggjs/koa 2.17.0 → 2.18.1

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/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { Application } from './application.js';
2
+
3
+ export default Application;
4
+
5
+ export * from './application.js';
6
+ export * from './context.js';
7
+ export * from './request.js';
8
+ export * from './response.js';
9
+ export * from './types.js';
package/src/request.ts ADDED
@@ -0,0 +1,580 @@
1
+ import net from 'node:net';
2
+ import type { Socket } from 'node:net';
3
+ import { format as stringify } from 'node:url';
4
+ import qs from 'node:querystring';
5
+ import util from 'node:util';
6
+ import type { ParsedUrlQuery } from 'node:querystring';
7
+ import type { IncomingMessage, ServerResponse } from 'node:http';
8
+ import accepts from 'accepts';
9
+ import contentType from 'content-type';
10
+ import parse from 'parseurl';
11
+ import typeis from 'type-is';
12
+ import fresh from 'fresh';
13
+ import type Application from './application.js';
14
+ import type Context from './context.js';
15
+ import type Response from './response.js';
16
+
17
+ export default class Request {
18
+ app: Application;
19
+ req: IncomingMessage;
20
+ res: ServerResponse;
21
+ ctx: Context;
22
+ response: Response;
23
+ originalUrl: string;
24
+
25
+ constructor(app: Application, ctx: Context, req: IncomingMessage, res: ServerResponse) {
26
+ this.app = app;
27
+ this.req = req;
28
+ this.res = res;
29
+ this.ctx = ctx;
30
+ this.originalUrl = req.url!;
31
+ }
32
+
33
+ /**
34
+ * Return request header.
35
+ */
36
+
37
+ get header() {
38
+ return this.req.headers;
39
+ }
40
+
41
+ /**
42
+ * Set request header.
43
+ */
44
+
45
+ set header(val) {
46
+ this.req.headers = val;
47
+ }
48
+
49
+ /**
50
+ * Return request header, alias as request.header
51
+ */
52
+
53
+ get headers() {
54
+ return this.req.headers;
55
+ }
56
+
57
+ /**
58
+ * Set request header, alias as request.header
59
+ */
60
+
61
+ set headers(val) {
62
+ this.req.headers = val;
63
+ }
64
+
65
+ /**
66
+ * Get request URL.
67
+ */
68
+
69
+ get url() {
70
+ return this.req.url!;
71
+ }
72
+
73
+ /**
74
+ * Set request URL.
75
+ */
76
+
77
+ set url(val) {
78
+ this.req.url = val;
79
+ }
80
+
81
+ /**
82
+ * Get origin of URL.
83
+ */
84
+
85
+ get origin() {
86
+ return `${this.protocol}://${this.host}`;
87
+ }
88
+
89
+ /**
90
+ * Get full request URL.
91
+ */
92
+
93
+ get href() {
94
+ // support: `GET http://example.com/foo`
95
+ if (/^https?:\/\//i.test(this.originalUrl)) return this.originalUrl;
96
+ return this.origin + this.originalUrl;
97
+ }
98
+
99
+ /**
100
+ * Get request method.
101
+ */
102
+
103
+ get method() {
104
+ return this.req.method!;
105
+ }
106
+
107
+ /**
108
+ * Set request method.
109
+ */
110
+
111
+ set method(val) {
112
+ this.req.method = val;
113
+ }
114
+
115
+ /**
116
+ * Get request pathname.
117
+ */
118
+
119
+ get path() {
120
+ return parse(this.req)!.pathname as string;
121
+ }
122
+
123
+ /**
124
+ * Set pathname, retaining the query string when present.
125
+ */
126
+
127
+ set path(path) {
128
+ const url = parse(this.req)!;
129
+ if (url.pathname === path) return;
130
+
131
+ url.pathname = path;
132
+ url.path = null;
133
+
134
+ this.url = stringify(url);
135
+ }
136
+
137
+ #parsedUrlQueryCache: Record<string, ParsedUrlQuery>;
138
+
139
+ /**
140
+ * Get parsed query string.
141
+ */
142
+ get query() {
143
+ const str = this.querystring;
144
+ if (!this.#parsedUrlQueryCache) {
145
+ this.#parsedUrlQueryCache = {};
146
+ }
147
+ let parsedUrlQuery = this.#parsedUrlQueryCache[str];
148
+ if (!parsedUrlQuery) {
149
+ parsedUrlQuery = this.#parsedUrlQueryCache[str] = qs.parse(str);
150
+ }
151
+ return parsedUrlQuery;
152
+ }
153
+
154
+ /**
155
+ * Set query string as an object.
156
+ */
157
+
158
+ set query(obj) {
159
+ this.querystring = qs.stringify(obj);
160
+ }
161
+
162
+ /**
163
+ * Get query string.
164
+ */
165
+
166
+ get querystring() {
167
+ if (!this.req) return '';
168
+ return parse(this.req)!.query as string || '';
169
+ }
170
+
171
+ /**
172
+ * Set query string.
173
+ */
174
+
175
+ set querystring(str) {
176
+ const url = parse(this.req)!;
177
+ if (url.search === `?${str}`) return;
178
+
179
+ url.search = str;
180
+ url.path = null;
181
+ this.url = stringify(url);
182
+ }
183
+
184
+ /**
185
+ * Get the search string. Same as the query string
186
+ * except it includes the leading ?.
187
+ */
188
+
189
+ get search() {
190
+ if (!this.querystring) return '';
191
+ return `?${this.querystring}`;
192
+ }
193
+
194
+ /**
195
+ * Set the search string. Same as
196
+ * request.querystring= but included for ubiquity.
197
+ */
198
+
199
+ set search(str) {
200
+ this.querystring = str;
201
+ }
202
+
203
+ /**
204
+ * Parse the "Host" header field host
205
+ * and support X-Forwarded-Host when a
206
+ * proxy is enabled.
207
+ * return `hostname:port` format
208
+ */
209
+ get host() {
210
+ const proxy = this.app.proxy;
211
+ let host = proxy ? this.get<string>('X-Forwarded-Host') : '';
212
+ if (!host) {
213
+ if (this.req.httpVersionMajor >= 2) host = this.get(':authority');
214
+ if (!host) host = this.get('Host');
215
+ }
216
+ if (!host) return '';
217
+ return host.split(/\s*,\s*/, 1)[0];
218
+ }
219
+
220
+ /**
221
+ * Parse the "Host" header field hostname
222
+ * and support X-Forwarded-Host when a
223
+ * proxy is enabled.
224
+ */
225
+ get hostname() {
226
+ const host = this.host;
227
+ if (!host) return '';
228
+ if (host[0] === '[') return this.URL.hostname || ''; // IPv6
229
+ return host.split(':', 1)[0];
230
+ }
231
+
232
+ #memoizedURL: URL;
233
+
234
+ /**
235
+ * Get WHATWG parsed URL.
236
+ * Lazily memoized.
237
+ */
238
+ get URL() {
239
+ if (!this.#memoizedURL) {
240
+ const originalUrl = this.originalUrl || ''; // avoid undefined in template string
241
+ try {
242
+ this.#memoizedURL = new URL(`${this.origin}${originalUrl}`);
243
+ } catch {
244
+ this.#memoizedURL = Object.create(null);
245
+ }
246
+ }
247
+ return this.#memoizedURL;
248
+ }
249
+
250
+ /**
251
+ * Check if the request is fresh, aka
252
+ * Last-Modified and/or the ETag
253
+ * still match.
254
+ */
255
+ get fresh() {
256
+ const method = this.method;
257
+ const status = this.response.status;
258
+
259
+ // GET or HEAD for weak freshness validation only
260
+ if (method !== 'GET' && method !== 'HEAD') return false;
261
+
262
+ // 2xx or 304 as per rfc2616 14.26
263
+ if ((status >= 200 && status < 300) || status === 304) {
264
+ return fresh(this.header, this.response.header);
265
+ }
266
+
267
+ return false;
268
+ }
269
+
270
+ /**
271
+ * Check if the request is stale, aka
272
+ * "Last-Modified" and / or the "ETag" for the
273
+ * resource has changed.
274
+ */
275
+ get stale() {
276
+ return !this.fresh;
277
+ }
278
+
279
+ /**
280
+ * Check if the request is idempotent.
281
+ */
282
+ get idempotent() {
283
+ const methods = [ 'GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE' ];
284
+ return methods.includes(this.method);
285
+ }
286
+
287
+ /**
288
+ * Return the request socket.
289
+ */
290
+ get socket() {
291
+ return this.req.socket as (Socket & { encrypted: boolean; });
292
+ }
293
+
294
+ /**
295
+ * Get the charset when present or undefined.
296
+ */
297
+ get charset() {
298
+ try {
299
+ const { parameters } = contentType.parse(this.req);
300
+ return parameters.charset || '';
301
+ } catch {
302
+ return '';
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Return parsed Content-Length when present.
308
+ */
309
+ get length() {
310
+ const len = this.get<string>('Content-Length');
311
+ if (len === '') return;
312
+ return parseInt(len);
313
+ }
314
+
315
+ /**
316
+ * Return the protocol string "http" or "https"
317
+ * when requested with TLS. When the proxy setting
318
+ * is enabled the "X-Forwarded-Proto" header
319
+ * field will be trusted. If you're running behind
320
+ * a reverse proxy that supplies https for you this
321
+ * may be enabled.
322
+ */
323
+ get protocol() {
324
+ if (this.socket.encrypted) return 'https';
325
+ if (!this.app.proxy) return 'http';
326
+ const proto = this.get<string>('X-Forwarded-Proto');
327
+ return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http';
328
+ }
329
+
330
+ /**
331
+ * Shorthand for:
332
+ *
333
+ * this.protocol == 'https'
334
+ */
335
+ get secure() {
336
+ return this.protocol === 'https';
337
+ }
338
+
339
+ /**
340
+ * When `app.proxy` is `true`, parse
341
+ * the "X-Forwarded-For" ip address list.
342
+ *
343
+ * For example if the value was "client, proxy1, proxy2"
344
+ * you would receive the array `["client", "proxy1", "proxy2"]`
345
+ * where "proxy2" is the furthest down-stream.
346
+ */
347
+ get ips() {
348
+ const proxy = this.app.proxy;
349
+ const val = this.get<string>(this.app.proxyIpHeader);
350
+ let ips = proxy && val
351
+ ? val.split(/\s*,\s*/)
352
+ : [];
353
+ if (this.app.maxIpsCount > 0) {
354
+ ips = ips.slice(-this.app.maxIpsCount);
355
+ }
356
+ return ips;
357
+ }
358
+
359
+ #ip: string;
360
+ /**
361
+ * Return request's remote address
362
+ * When `app.proxy` is `true`, parse
363
+ * the "X-Forwarded-For" ip address list and return the first one
364
+ */
365
+ get ip() {
366
+ if (!this.#ip) {
367
+ this.#ip = this.ips[0] || this.socket.remoteAddress || '';
368
+ }
369
+ return this.#ip;
370
+ }
371
+
372
+ set ip(ip: string) {
373
+ this.#ip = ip;
374
+ }
375
+
376
+ /**
377
+ * Return subdomains as an array.
378
+ *
379
+ * Subdomains are the dot-separated parts of the host before the main domain
380
+ * of the app. By default, the domain of the app is assumed to be the last two
381
+ * parts of the host. This can be changed by setting `app.subdomainOffset`.
382
+ *
383
+ * For example, if the domain is "tobi.ferrets.example.com":
384
+ * If `app.subdomainOffset` is not set, this.subdomains is
385
+ * `["ferrets", "tobi"]`.
386
+ * If `app.subdomainOffset` is 3, this.subdomains is `["tobi"]`.
387
+ */
388
+ get subdomains() {
389
+ const offset = this.app.subdomainOffset;
390
+ const hostname = this.hostname;
391
+ if (net.isIP(hostname)) return [];
392
+ return hostname
393
+ .split('.')
394
+ .reverse()
395
+ .slice(offset);
396
+ }
397
+
398
+ #accept: any;
399
+ /**
400
+ * Get accept object.
401
+ * Lazily memoized.
402
+ */
403
+ get accept() {
404
+ return this.#accept || (this.#accept = accepts(this.req));
405
+ }
406
+
407
+ /**
408
+ * Set accept object.
409
+ */
410
+ set accept(obj) {
411
+ this.#accept = obj;
412
+ }
413
+
414
+ /**
415
+ * Check if the given `type(s)` is acceptable, returning
416
+ * the best match when true, otherwise `false`, in which
417
+ * case you should respond with 406 "Not Acceptable".
418
+ *
419
+ * The `type` value may be a single mime type string
420
+ * such as "application/json", the extension name
421
+ * such as "json" or an array `["json", "html", "text/plain"]`. When a list
422
+ * or array is given the _best_ match, if any is returned.
423
+ *
424
+ * Examples:
425
+ *
426
+ * // Accept: text/html
427
+ * this.accepts('html');
428
+ * // => "html"
429
+ *
430
+ * // Accept: text/*, application/json
431
+ * this.accepts('html');
432
+ * // => "html"
433
+ * this.accepts('text/html');
434
+ * // => "text/html"
435
+ * this.accepts('json', 'text');
436
+ * // => "json"
437
+ * this.accepts('application/json');
438
+ * // => "application/json"
439
+ *
440
+ * // Accept: text/*, application/json
441
+ * this.accepts('image/png');
442
+ * this.accepts('png');
443
+ * // => false
444
+ *
445
+ * // Accept: text/*;q=.5, application/json
446
+ * this.accepts(['html', 'json']);
447
+ * this.accepts('html', 'json');
448
+ * // => "json"
449
+ */
450
+ accepts(...args: any[]): string | string[] | false {
451
+ return this.accept.types(...args);
452
+ }
453
+
454
+ /**
455
+ * Return accepted encodings or best fit based on `encodings`.
456
+ *
457
+ * Given `Accept-Encoding: gzip, deflate`
458
+ * an array sorted by quality is returned:
459
+ *
460
+ * ['gzip', 'deflate']
461
+ */
462
+ acceptsEncodings(...args: any[]): string | string[] {
463
+ return this.accept.encodings(...args);
464
+ }
465
+
466
+ /**
467
+ * Return accepted charsets or best fit based on `charsets`.
468
+ *
469
+ * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
470
+ * an array sorted by quality is returned:
471
+ *
472
+ * ['utf-8', 'utf-7', 'iso-8859-1']
473
+ */
474
+ acceptsCharsets(...args: any[]): string | string[] {
475
+ return this.accept.charsets(...args);
476
+ }
477
+
478
+ /**
479
+ * Return accepted languages or best fit based on `langs`.
480
+ *
481
+ * Given `Accept-Language: en;q=0.8, es, pt`
482
+ * an array sorted by quality is returned:
483
+ *
484
+ * ['es', 'pt', 'en']
485
+ */
486
+ acceptsLanguages(...args: any[]): string | string[] {
487
+ return this.accept.languages(...args);
488
+ }
489
+
490
+ /**
491
+ * Check if the incoming request contains the "Content-Type"
492
+ * header field and if it contains any of the given mime `type`s.
493
+ * If there is no request body, `null` is returned.
494
+ * If there is no content type, `false` is returned.
495
+ * Otherwise, it returns the first `type` that matches.
496
+ *
497
+ * Examples:
498
+ *
499
+ * // With Content-Type: text/html; charset=utf-8
500
+ * this.is('html'); // => 'html'
501
+ * this.is('text/html'); // => 'text/html'
502
+ * this.is('text/*', 'application/json'); // => 'text/html'
503
+ *
504
+ * // When Content-Type is application/json
505
+ * this.is('json', 'urlencoded'); // => 'json'
506
+ * this.is('application/json'); // => 'application/json'
507
+ * this.is('html', 'application/*'); // => 'application/json'
508
+ *
509
+ * this.is('html'); // => false
510
+ */
511
+ is(type?: string | string[], ...types: string[]): string | false | null {
512
+ const testTypes: string[] = Array.isArray(type) ? type :
513
+ (type ? [ type ] : []);
514
+ return typeis(this.req, [ ...testTypes, ...types ]);
515
+ }
516
+
517
+ /**
518
+ * Return the request mime type void of
519
+ * parameters such as "charset".
520
+ */
521
+ get type() {
522
+ const type = this.get<string>('Content-Type');
523
+ if (!type) return '';
524
+ return type.split(';')[0];
525
+ }
526
+
527
+ /**
528
+ * Return request header.
529
+ *
530
+ * The `Referrer` header field is special-cased,
531
+ * both `Referrer` and `Referer` are interchangeable.
532
+ *
533
+ * Examples:
534
+ *
535
+ * this.get('Content-Type');
536
+ * // => "text/plain"
537
+ *
538
+ * this.get('content-type');
539
+ * // => "text/plain"
540
+ *
541
+ * this.get('Something');
542
+ * // => ''
543
+ */
544
+ get<T = string | string []>(field: string): T {
545
+ const req = this.req;
546
+ switch (field = field.toLowerCase()) {
547
+ case 'referer':
548
+ case 'referrer':
549
+ return (req.headers.referrer || req.headers.referer || '') as T;
550
+ default:
551
+ return (req.headers[field] || '') as T;
552
+ }
553
+ }
554
+
555
+ /**
556
+ * Inspect implementation.
557
+ */
558
+ inspect() {
559
+ if (!this.req) return;
560
+ return this.toJSON();
561
+ }
562
+
563
+ /**
564
+ * Custom inspection implementation for newer Node.js versions.
565
+ */
566
+ [util.inspect.custom]() {
567
+ return this.inspect();
568
+ }
569
+
570
+ /**
571
+ * Return JSON representation.
572
+ */
573
+ toJSON() {
574
+ return {
575
+ method: this.method,
576
+ url: this.url,
577
+ header: this.header,
578
+ };
579
+ }
580
+ }