@eggjs/koa 3.0.1 → 3.1.0-beta.3

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/context.ts DELETED
@@ -1,534 +0,0 @@
1
- import util from 'node:util';
2
- import type { IncomingMessage, ServerResponse } from 'node:http';
3
- import type { ParsedUrlQuery } from 'node:querystring';
4
-
5
- import createError from 'http-errors';
6
- import statuses from 'statuses';
7
- import Cookies from 'cookies';
8
- import type { Accepts } from 'accepts';
9
-
10
- import type { Application } from './application.ts';
11
- import type { Request } from './request.ts';
12
- import type { Response } from './response.ts';
13
- import type { CustomError, AnyProto } from './types.ts';
14
-
15
- export class Context {
16
- [key: symbol | string]: unknown;
17
- app: Application;
18
- req: IncomingMessage;
19
- res: ServerResponse;
20
- request: Request & AnyProto;
21
- response: Response & AnyProto;
22
- originalUrl: string;
23
- respond?: boolean;
24
- // oxlint-disable-next-line typescript/no-explicit-any
25
- #state: Record<string, any> = {};
26
-
27
- constructor(app: Application, req: IncomingMessage, res: ServerResponse) {
28
- this.app = app;
29
- this.req = req;
30
- this.res = res;
31
- this.request = new app.RequestClass(app, this, req, res);
32
- this.response = new app.ResponseClass(app, this, req, res);
33
- this.request.response = this.response;
34
- this.response.request = this.request;
35
- this.originalUrl = req.url ?? '/';
36
- }
37
-
38
- /**
39
- * util.inspect() implementation, which
40
- * just returns the JSON output.
41
- */
42
- inspect() {
43
- return this.toJSON();
44
- }
45
-
46
- /**
47
- * Custom inspection implementation for newer Node.js versions.
48
- */
49
- [util.inspect.custom]() {
50
- return this.inspect();
51
- }
52
-
53
- /**
54
- * Return JSON representation.
55
- *
56
- * Here we explicitly invoke .toJSON() on each
57
- * object, as iteration will otherwise fail due
58
- * to the getters and cause utilities such as
59
- * clone() to fail.
60
- */
61
-
62
- toJSON() {
63
- return {
64
- request: this.request.toJSON(),
65
- response: this.response.toJSON(),
66
- app: this.app.toJSON(),
67
- originalUrl: this.originalUrl,
68
- req: '<original node req>',
69
- res: '<original node res>',
70
- socket: '<original node socket>',
71
- };
72
- }
73
-
74
- /**
75
- * Similar to .throw(), adds assertion.
76
- *
77
- * ```ts
78
- * this.assert(this.user, 401, 'Please login!');
79
- * ```
80
- */
81
- assert(
82
- value: unknown,
83
- status?: number,
84
- errorProps?: Record<string, unknown>
85
- ): void;
86
- assert(
87
- value: unknown,
88
- status?: number,
89
- errorMessage?: string,
90
- errorProps?: Record<string, unknown>
91
- ): void;
92
- assert(
93
- value: unknown,
94
- status?: number,
95
- errorMessageOrProps?: string | Record<string, unknown>,
96
- errorProps?: Record<string, unknown>
97
- ) {
98
- if (value) {
99
- return;
100
- }
101
- status = status ?? 500;
102
- if (typeof errorMessageOrProps === 'string') {
103
- // assert(value, status, errorMessage, errorProps?)
104
- throw createError(status, errorMessageOrProps, errorProps ?? {});
105
- }
106
- // assert(value, status, errorProps?)
107
- throw createError(status, errorMessageOrProps ?? {});
108
- }
109
-
110
- /**
111
- * Throw an error with `status` (default 500) and
112
- * `msg`. Note that these are user-level
113
- * errors, and the message may be exposed to the client.
114
- *
115
- * this.throw(403)
116
- * this.throw(400, 'name required')
117
- * this.throw('something exploded')
118
- * this.throw(new Error('invalid'))
119
- * this.throw(400, new Error('invalid'))
120
- * this.throw(400, new Error('invalid'), { foo: 'bar' })
121
- * this.throw(new Error('invalid'), { foo: 'bar' })
122
- *
123
- * See: https://github.com/jshttp/http-errors
124
- *
125
- * Note: `status` should only be passed as the first parameter.
126
- *
127
- * @param {String|Number|Error} status error, msg or status
128
- * @param {String|Number|Error|Object} [error] error, msg, status or errorProps
129
- * @param {Object} [errorProps] error object properties
130
- */
131
-
132
- throw(status: number): void;
133
- throw(status: number, errorProps: object): void;
134
- throw(status: number, errorMessage: string): void;
135
- throw(status: number, errorMessage: string, errorProps: object): void;
136
- throw(status: number, error: Error): void;
137
- throw(status: number, error: Error, errorProps: object): void;
138
- throw(errorMessage: string): void;
139
- throw(errorMessage: string, errorProps: object): void;
140
- throw(errorMessage: string, status: number): void;
141
- throw(errorMessage: string, status: number, errorProps: object): void;
142
- throw(error: Error): void;
143
- throw(error: Error, errorProps: object): void;
144
- throw(error: Error, status: number): void;
145
- throw(error: Error, status: number, errorProps: object): void;
146
- throw(
147
- arg1: number | string | Error,
148
- arg2?: number | string | Error | object,
149
- errorProps?: object
150
- ) {
151
- // oxlint-disable-next-line typescript/no-explicit-any
152
- const args: any[] = [];
153
- if (typeof arg2 === 'number') {
154
- // throw(error, status)
155
- args.push(arg2);
156
- args.push(arg1);
157
- } else {
158
- // throw(status, error?)
159
- args.push(arg1);
160
- if (arg2) {
161
- args.push(arg2);
162
- }
163
- }
164
- if (errorProps) {
165
- args.push(errorProps);
166
- }
167
- throw createError(...args);
168
- }
169
-
170
- /**
171
- * Default error handling.
172
- * @private
173
- */
174
- onerror(err: CustomError) {
175
- // don't do anything if there is no error.
176
- // this allows you to pass `this.onerror`
177
- // to node-style callbacks.
178
- if (err === null || err === undefined) return;
179
-
180
- // When dealing with cross-globals a normal `instanceof` check doesn't work properly.
181
- // See https://github.com/koajs/koa/issues/1466
182
- // We can probably remove it once jest fixes https://github.com/facebook/jest/issues/2549.
183
- const isNativeError =
184
- err instanceof Error ||
185
- Object.prototype.toString.call(err) === '[object Error]';
186
- if (!isNativeError) {
187
- err = new Error(util.format('non-error thrown: %j', err));
188
- }
189
-
190
- let headerSent = false;
191
- if (this.response.headerSent || !this.response.writable) {
192
- headerSent = true;
193
- err.headerSent = true;
194
- }
195
-
196
- // delegate
197
- this.app.emit('error', err, this);
198
-
199
- // nothing we can do here other
200
- // than delegate to the app-level
201
- // handler and log.
202
- if (headerSent) {
203
- return;
204
- }
205
-
206
- const res = this.res;
207
-
208
- // first unset all headers
209
- for (const name of res.getHeaderNames()) {
210
- res.removeHeader(name);
211
- }
212
-
213
- // then set those specified
214
- if (err.headers) {
215
- this.response.set(err.headers);
216
- }
217
-
218
- // force text/plain
219
- this.response.type = 'text';
220
-
221
- let statusCode = err.status || err.statusCode;
222
-
223
- // ENOENT support
224
- if (err.code === 'ENOENT') {
225
- statusCode = 404;
226
- }
227
-
228
- // default to 500
229
- if (typeof statusCode !== 'number' || !statuses.message[statusCode]) {
230
- statusCode = 500;
231
- }
232
-
233
- // respond
234
- const statusMessage = statuses.message[statusCode] as string;
235
- const msg = err.expose ? err.message : statusMessage;
236
- err.status = statusCode;
237
- this.response.status = statusCode;
238
- this.response.length = Buffer.byteLength(msg);
239
- res.end(msg);
240
- }
241
-
242
- protected _cookies: Cookies | undefined;
243
-
244
- get cookies() {
245
- if (!this._cookies) {
246
- this._cookies = new Cookies(this.req, this.res, {
247
- keys: this.app.keys,
248
- secure: this.request.secure,
249
- });
250
- }
251
- return this._cookies;
252
- }
253
-
254
- set cookies(cookies: Cookies) {
255
- this._cookies = cookies;
256
- }
257
-
258
- get state() {
259
- return this.#state;
260
- }
261
-
262
- /**
263
- * Request delegation.
264
- */
265
-
266
- acceptsLanguages(): string[];
267
- acceptsLanguages(languages: string[]): string | false;
268
- acceptsLanguages(...languages: string[]): string | false;
269
- acceptsLanguages(
270
- languages?: string | string[],
271
- ...others: string[]
272
- ): string | string[] | false {
273
- return this.request.acceptsLanguages(languages as string, ...others);
274
- }
275
-
276
- acceptsEncodings(): string[];
277
- acceptsEncodings(encodings: string[]): string | false;
278
- acceptsEncodings(...encodings: string[]): string | false;
279
- acceptsEncodings(
280
- encodings?: string | string[],
281
- ...others: string[]
282
- ): string[] | string | false {
283
- return this.request.acceptsEncodings(encodings as string, ...others);
284
- }
285
-
286
- acceptsCharsets(): string[];
287
- acceptsCharsets(charsets: string[]): string | false;
288
- acceptsCharsets(...charsets: string[]): string | false;
289
- acceptsCharsets(
290
- charsets?: string | string[],
291
- ...others: string[]
292
- ): string[] | string | false {
293
- return this.request.acceptsCharsets(charsets as string, ...others);
294
- }
295
-
296
- accepts(args: string[]): string | string[] | false;
297
- accepts(...args: string[]): string | string[] | false;
298
- accepts(
299
- args?: string | string[],
300
- ...others: string[]
301
- ): string | string[] | false {
302
- return this.request.accepts(args as string, ...others);
303
- }
304
-
305
- get<T = string | string[]>(field: string): T {
306
- return this.request.get(field);
307
- }
308
-
309
- is(type?: string | string[], ...types: string[]): string | false | null {
310
- return this.request.is(type, ...types);
311
- }
312
-
313
- get querystring(): string {
314
- return this.request.querystring;
315
- }
316
-
317
- set querystring(str: string) {
318
- this.request.querystring = str;
319
- }
320
-
321
- get idempotent(): boolean {
322
- return this.request.idempotent;
323
- }
324
-
325
- get socket() {
326
- return this.request.socket;
327
- }
328
-
329
- get search(): string {
330
- return this.request.search;
331
- }
332
-
333
- set search(str: string) {
334
- this.request.search = str;
335
- }
336
-
337
- get method(): string {
338
- return this.request.method;
339
- }
340
-
341
- set method(method: string) {
342
- this.request.method = method;
343
- }
344
-
345
- get query(): ParsedUrlQuery {
346
- return this.request.query;
347
- }
348
-
349
- set query(obj: ParsedUrlQuery) {
350
- this.request.query = obj;
351
- }
352
-
353
- get path(): string {
354
- return this.request.path;
355
- }
356
-
357
- set path(path: string) {
358
- this.request.path = path;
359
- }
360
-
361
- get url(): string {
362
- return this.request.url;
363
- }
364
-
365
- set url(url: string) {
366
- this.request.url = url;
367
- }
368
-
369
- get accept(): Accepts {
370
- return this.request.accept;
371
- }
372
-
373
- set accept(accept: Accepts) {
374
- this.request.accept = accept;
375
- }
376
-
377
- get origin(): string {
378
- return this.request.origin;
379
- }
380
-
381
- get href(): string {
382
- return this.request.href;
383
- }
384
-
385
- get subdomains(): string[] {
386
- return this.request.subdomains;
387
- }
388
-
389
- get protocol(): string {
390
- return this.request.protocol;
391
- }
392
-
393
- get host(): string {
394
- return this.request.host;
395
- }
396
-
397
- get hostname(): string {
398
- return this.request.hostname;
399
- }
400
-
401
- get URL(): URL {
402
- return this.request.URL;
403
- }
404
-
405
- get header() {
406
- return this.request.header;
407
- }
408
-
409
- get headers() {
410
- return this.request.headers;
411
- }
412
-
413
- get secure(): boolean {
414
- return this.request.secure;
415
- }
416
-
417
- get stale(): boolean {
418
- return this.request.stale;
419
- }
420
-
421
- get fresh(): boolean {
422
- return this.request.fresh;
423
- }
424
-
425
- get ips(): string[] {
426
- return this.request.ips;
427
- }
428
-
429
- get ip(): string {
430
- return this.request.ip;
431
- }
432
-
433
- /**
434
- * Response delegation.
435
- */
436
-
437
- attachment(...args: Parameters<Response['attachment']>) {
438
- return this.response.attachment(...args);
439
- }
440
-
441
- redirect(...args: Parameters<Response['redirect']>) {
442
- return this.response.redirect(...args);
443
- }
444
-
445
- remove(...args: Parameters<Response['remove']>) {
446
- return this.response.remove(...args);
447
- }
448
-
449
- vary(...args: Parameters<Response['vary']>) {
450
- return this.response.vary(...args);
451
- }
452
-
453
- has(...args: Parameters<Response['has']>) {
454
- return this.response.has(...args);
455
- }
456
-
457
- set(...args: Parameters<Response['set']>) {
458
- return this.response.set(...args);
459
- }
460
-
461
- append(...args: Parameters<Response['append']>) {
462
- return this.response.append(...args);
463
- }
464
-
465
- flushHeaders(...args: Parameters<Response['flushHeaders']>) {
466
- return this.response.flushHeaders(...args);
467
- }
468
-
469
- get status() {
470
- return this.response.status;
471
- }
472
-
473
- set status(status: number) {
474
- this.response.status = status;
475
- }
476
-
477
- get message() {
478
- return this.response.message;
479
- }
480
-
481
- set message(msg: string) {
482
- this.response.message = msg;
483
- }
484
-
485
- // oxlint-disable-next-line typescript/no-explicit-any
486
- get body(): any {
487
- return this.response.body;
488
- }
489
-
490
- // oxlint-disable-next-line typescript/no-explicit-any
491
- set body(val: any) {
492
- this.response.body = val;
493
- }
494
-
495
- get length(): number | undefined {
496
- return this.response.length;
497
- }
498
-
499
- set length(n: number | string | undefined) {
500
- this.response.length = n;
501
- }
502
-
503
- get type(): string {
504
- return this.response.type;
505
- }
506
-
507
- set type(type: string | null | undefined) {
508
- this.response.type = type;
509
- }
510
-
511
- get lastModified() {
512
- return this.response.lastModified;
513
- }
514
-
515
- set lastModified(val: string | Date | undefined) {
516
- this.response.lastModified = val;
517
- }
518
-
519
- get etag() {
520
- return this.response.etag;
521
- }
522
-
523
- set etag(val: string) {
524
- this.response.etag = val;
525
- }
526
-
527
- get headerSent() {
528
- return this.response.headerSent;
529
- }
530
-
531
- get writable() {
532
- return this.response.writable;
533
- }
534
- }
package/src/index.ts DELETED
@@ -1,9 +0,0 @@
1
- import { Application } from './application.ts';
2
-
3
- export default Application;
4
-
5
- export * from './application.ts';
6
- export * from './context.ts';
7
- export * from './request.ts';
8
- export * from './response.ts';
9
- export type { CustomError, AnyProto } from './types.ts';