index.js
2.02 KB
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
/*!
* on-headers
* Copyright(c) 2014 Douglas Christopher Wilson
* MIT Licensed
*/
'use strict'
/**
* Reference to Array slice.
*/
var slice = Array.prototype.slice
/**
* Execute a listener when a response is about to write headers.
*
* @param {Object} res
* @return {Function} listener
* @api public
*/
module.exports = function onHeaders(res, listener) {
if (!res) {
throw new TypeError('argument res is required')
}
if (typeof listener !== 'function') {
throw new TypeError('argument listener must be a function')
}
res.writeHead = createWriteHead(res.writeHead, listener)
}
function createWriteHead(prevWriteHead, listener) {
var fired = false;
// return function with core name and argument list
return function writeHead(statusCode) {
// set headers from arguments
var args = setWriteHeadHeaders.apply(this, arguments);
// fire listener
if (!fired) {
fired = true
listener.call(this)
// pass-along an updated status code
if (typeof args[0] === 'number' && this.statusCode !== args[0]) {
args[0] = this.statusCode
args.length = 1
}
}
prevWriteHead.apply(this, args);
}
}
function setWriteHeadHeaders(statusCode) {
var length = arguments.length
var headerIndex = length > 1 && typeof arguments[1] === 'string'
? 2
: 1
var headers = length >= headerIndex + 1
? arguments[headerIndex]
: undefined
this.statusCode = statusCode
// the following block is from node.js core
if (Array.isArray(headers)) {
// handle array case
for (var i = 0, len = headers.length; i < len; ++i) {
this.setHeader(headers[i][0], headers[i][1])
}
} else if (headers) {
// handle object case
var keys = Object.keys(headers)
for (var i = 0; i < keys.length; i++) {
var k = keys[i]
if (k) this.setHeader(k, headers[k])
}
}
// copy leading arguments
var args = new Array(Math.min(length, headerIndex))
for (var i = 0; i < args.length; i++) {
args[i] = arguments[i]
}
return args
}