-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
response.ts
630 lines (554 loc) · 14.2 KB
/
response.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
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import { contentDisposition } from "./utils/contentDisposition.ts";
import { stringify } from "./utils/stringify.ts";
import { normalizeType, normalizeTypes } from "./utils/normalizeType.ts";
import {
setCookie,
Cookie,
delCookie,
Status,
STATUS_TEXT,
extname,
basename,
contentType,
vary,
encodeUrl,
fromFileUrl,
} from "../deps.ts";
import {
Response as DenoResponse,
ResponseBody,
Request,
Application,
DenoResponseBody,
NextFunction,
} from "../src/types.ts";
/**
* Response class.
*
* @public
*/
export class Response implements DenoResponse {
status: Status = 200;
headers: Headers = new Headers();
body!: DenoResponseBody;
app!: Application;
req!: Request;
locals!: any;
// TODO: Supporting arrays.
/**
* Append additional header `field` with value `val`.
*
* Example:
*
* res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
* res.append('Warning', '199 Miscellaneous warning');
*
* @param {string} field
* @param {string} value
* @return {Response} for chaining
* @public
*/
append(field: string, value: string): this {
this.headers.append(field, value);
return this;
}
/**
* Set _Content-Disposition_ header to _attachment_ with optional `filename`.
*
* @param {string} filename
* @return {Response} for chaining
* @public
*/
attachment(filename: string): this {
if (filename) {
this.type(extname(filename));
}
this.set("Content-Disposition", contentDisposition("attachment", filename));
return this;
}
// TODO: back-compat support for Express signature.
/**
* Set a cookie. Sets the cookie path to "/" if not defined.
*
* Examples:
*
* // "Remember Me" for 15 minutes
* res.cookie({ name: "rememberme", value: "1", expires: new Date(Date.now() + 900000), httpOnly: true });
*
* @param {Cookie} cookie
* @return {Response} for chaining
* @public
*/
cookie(cookie: Cookie): this {
if (cookie.path == null) {
cookie.path = "/";
}
setCookie(this, cookie);
return this;
}
// TODO: back-compat support for Express signature.
/**
* Clear a cookie.
*
* @param {string|Cookie} cookie
* @return {Response} for chaining
* @public
*/
clearCookie(cookie: string | Cookie): this {
const cookieName = typeof cookie === "string" ? cookie : cookie.name;
delCookie(this, cookieName);
return this;
}
// TODO: back-compat support for Express signature. i.e. support options.
/**
* Transfer the file at the given `path` as an attachment.
*
* Optionally providing an alternate attachment `filename`.
*
* This function will set the `Content-Disposition` header, overriding
* any existing `Content-Disposition` header in order to set the attachment
* and filename.
*
* This method uses `res.sendFile()`.
*
* @param {string} path
* @param {string} filename
* @return {Promise<Response>}
* @public
*/
async download(
path: string,
filename?: string,
): Promise<this | void> {
this.set(
"Content-Disposition",
contentDisposition("attachment", basename(filename || path)),
);
try {
await this.sendFile(path);
} catch (err) {
this.unset("Content-Disposition");
throw err;
}
}
/**
* Ends the response process.
*
* @param {DenoResponseBody} body
* @return {Promise<void>}
* @public
*/
async end(
body?: DenoResponseBody,
): Promise<void> {
if (body) {
this.body = body;
}
await this.req.respond(this);
}
/**
* Sets an ETag header.
*
* @param {string|Uint8Array|Deno.FileInfo} chunk
* @returns {Response} for chaining
* @public
*/
etag(chunk: string | Uint8Array | Deno.FileInfo): this {
const etagFn = this.app.get("etag fn");
if (typeof etagFn === "function" && typeof (chunk as any).length) {
const etag = etagFn(chunk);
if (etag) {
this.set("ETag", etag);
}
}
return this;
}
/**
* Respond to the Acceptable formats using an `obj`
* of mime-type callbacks.
*
* This method uses `req.accepted`, an array of
* acceptable types ordered by their quality values.
* When "Accept" is not present the _first_ callback
* is invoked, otherwise the first match is used. When
* no match is performed the server responds with
* 406 "Not Acceptable".
*
* Content-Type is set for you, however if you choose
* you may alter this within the callback using `res.type()`
* or `res.set('Content-Type', ...)`.
*
* res.format({
* 'text/plain': function(){
* res.send('hey');
* },
*
* 'text/html': function(){
* res.send('<p>hey</p>');
* },
*
* 'application/json': function(){
* res.send({ message: 'hey' });
* }
* });
*
* In addition to canonicalized MIME types you may
* also use extnames mapped to these types:
*
* res.format({
* text: function(){
* res.send('hey');
* },
*
* html: function(){
* res.send('<p>hey</p>');
* },
*
* json: function(){
* res.send({ message: 'hey' });
* }
* });
*
* By default Express passes an `Error`
* with a `.status` of 406 to `next(err)`
* if a match is not made. If you provide
* a `.default` callback it will be invoked
* instead.
*
* @param {Object} obj
* @return {Response} for chaining
* @public
*/
format(obj: any): this {
const req = this.req;
const next = req.next as NextFunction;
const { default: fn, ...rest } = obj;
const keys = Object.keys(rest);
const key = keys.length > 0 ? req.accepts(keys)[0] : false;
this.vary("Accept");
if (key) {
this.set("Content-Type", normalizeType(key).value);
obj[key](req, this, next);
} else if (fn) {
fn();
} else {
const err = new Error("Not Acceptable") as any;
err.status = err.statusCode = 406;
err.types = normalizeTypes(keys).map(function (o) {
return o.value;
});
next(err);
}
return this;
}
/**
* Get value for header `field`.
*
* @param {string} field
* @return {string} the header
* @public
*/
get(field: string): string {
return this.headers.get(field.toLowerCase()) || "";
}
/**
* Send JSON response.
*
* Examples:
*
* res.json(null);
* res.json({ user: 'tj' });
*
* @param {ResponseBody} body
* @return {Response} for chaining
* @public
*/
json(body: ResponseBody): this {
const app = this.app;
const replacer = app.get("json replacer");
const spaces = app.get("json spaces");
const escape = app.get("json escape");
body = stringify(body, replacer, spaces, escape);
if (!this.get("Content-Type")) {
this.type("application/json");
}
return this.send(body);
}
/**
* Send JSON response with JSONP callback support.
*
* Examples:
*
* res.jsonp(null);
* res.jsonp({ user: 'tj' });
*
* @param {ResponseBody} body
* @return {Response} for chaining
* @public
*/
jsonp(body: ResponseBody) {
const app = this.app;
const replacer = app.get("json replacer");
const spaces = app.get("json spaces");
const escape = app.get("json escape");
body = stringify(body, replacer, spaces, escape);
let callback = this.req.query[app.get("jsonp callback name")];
if (Array.isArray(callback)) {
callback = callback[0];
}
if (typeof callback === "string" && callback.length !== 0) {
this.set("X-Content-Type-Options", "nosniff");
this.type("text/javascript");
// restrict callback charset
callback = callback.replace(/[^\[\]\w$.]/g, "");
// replace chars not allowed in JavaScript that are in JSON
body = body
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029");
// the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"
// the typeof check is just to reduce client error noise
body = `/**/ typeof ${callback} === 'function' && ${callback}(${body});`;
} else if (!this.get("Content-Type")) {
this.set("X-Content-Type-Options", "nosniff");
this.set("Content-Type", "application/json");
}
return this.send(body);
}
/**
* Set Link header field with the given `links`.
*
* Examples:
*
* res.links({
* next: 'http://api.example.com/users?page=2',
* last: 'http://api.example.com/users?page=5'
* });
*
* @param {any} links
* @return {Response} for chaining
* @public
*/
links(links: any) {
let currentLink = this.get("Link");
if (currentLink) {
currentLink += ", ";
}
const link = currentLink +
Object.entries(links).map(([field, rel]) => `<${field}>; rel="${rel}"`)
.join(", ");
return this.set("Link", link);
}
/**
* Set the location header to `url`.
*
* The given `url` can also be "back", which redirects
* to the _Referrer_ or _Referer_ headers or "/".
*
* Examples:
*
* res.location('/foo/bar').;
* res.location('http://example.com');
* res.location('../login');
*
* @param {string} url
* @return {Response} for chaining
* @public
*/
location(url: string): this {
const loc = url === "back" ? (this.req.get("Referrer") || "/") : url;
// set location
return this.set("Location", encodeUrl(loc));
}
// TODO: redirect() {}
// TODO: render() {}
/**
* Send a response.
*
* Examples:
*
* res.send({ some: 'json' });
* res.send('<p>some html</p>');
*
* @param {ResponseBody} body
* @return {Response} for chaining
* @public
*/
send(body: ResponseBody = ""): this {
let chunk: DenoResponseBody;
switch (typeof body) {
case "string":
chunk = body;
break;
case "boolean":
case "number":
return this.json(body);
case "object":
default:
if (
body instanceof Uint8Array ||
typeof (body as Deno.Reader).read === "function"
) {
chunk = body as Uint8Array | Deno.Reader;
if (!this.get("Content-Type")) {
this.type("bin");
}
} else {
return this.json(body);
}
}
if (typeof chunk === "string" && !this.get("Content-Type")) {
this.type("html");
}
if (
!this.get("ETag") && (typeof chunk === "string" ||
chunk instanceof Uint8Array)
) {
this.etag(chunk);
}
if (this.req.fresh) {
this.status = 304;
}
if (this.status === 204 || this.status === 304) {
this.unset("Content-Type");
this.unset("Content-Length");
this.unset("Transfer-Encoding");
chunk = "";
}
if (this.req.method === "HEAD") {
this.end();
} else {
this.end(chunk);
}
return this;
}
// TODO: back-compat support for Express signature. Specifically options
// parameter, but likely not callback. Should support:
//
// - abort handling
// - directory handling
// - error handling - see https://github.com/pillarjs/send/blob/master/index.js#L267
// - `options` - see https://github.com/pillarjs/send#sendreq-path-options
// - other headers: 'Accept-Ranges', 'Cache-Control', 'Content-Range'
/**
* Transfer the file at the given `path`.
*
* Automatically sets the _Content-Type_ response header field.
*
* @param {string} path
* @return {Promise<Response>}
* @public
*/
async sendFile(path: string): Promise<this> {
path = path.startsWith("file:") ? fromFileUrl(path) : path;
const body = await Deno.readFile(path);
const stats: Deno.FileInfo = await Deno.stat(path);
if (stats.mtime) {
this.set("Last-Modified", stats.mtime.toUTCString());
}
if (!this.get("ETag")) {
this.etag(stats);
}
this.type(extname(path));
return this.send(body);
}
/**
* Send given HTTP status code.
*
* Sets the response status to `code` and the body of the
* response to the standard description from deno's http_status.STATUS_TEXT
* or the code number if no description.
*
* Examples:
*
* res.sendStatus(200);
*
* @param {Status} code
* @return {Response} for chaining
* @public
*/
sendStatus(code: Status): this {
const body: string = STATUS_TEXT.get(code) || String(code);
this.setStatus(code);
this.type("txt");
return this.send(body);
}
// TODO: back-compat support for Express signature.
// Namely objects and arrays.
/**
* Set header `field` to `value`, or pass
* an object of header fields.
*
* Examples:
*
* res.set('Accept', 'application/json');
*
* @param {string} field
* @param {string} value
* @return {Response} for chaining
* @public
*/
set(field: string, value: string): this {
const lowerCaseField = field.toLowerCase();
if (lowerCaseField === "content-type") {
return this.type(value);
}
this.headers.set(lowerCaseField, value);
return this;
}
/**
* Set status `code`.
*
* This method deviates from Express due to the naming clash
* with Deno.Response `status` property.
*
* @param {Status} code
* @return {Response} for chaining
* @public
*/
setStatus(code: Status): this {
this.status = code;
return this;
}
/**
* Set _Content-Type_ response header with `type`.
*
* Examples:
*
* res.type('.html');
* res.type('html');
* res.type('json');
* res.type('application/json');
* res.type('png');
*
* @param {string} type
* @return {Response} for chaining
* @public
*/
type(type: string): this {
this.headers.set("content-type", contentType(type) || "");
return this;
}
/**
* Deletes a header.
*
* @param {string} field
* @return {Response} for chaining
* @public
*/
unset(field: string): this {
this.headers.delete(field);
return this;
}
/**
* Add `field` to Vary. If already present in the Vary set, then
* this call is simply ignored.
*
* @param {Array|String} field
* @return {Response} for chaining
* @public
*/
vary(field: string | string[]): this {
vary(this.headers, field);
return this;
}
}