1 | /*!
|
---|
2 | * on-headers
|
---|
3 | * Copyright(c) 2014 Douglas Christopher Wilson
|
---|
4 | * MIT Licensed
|
---|
5 | */
|
---|
6 |
|
---|
7 | 'use strict'
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Module exports.
|
---|
11 | * @public
|
---|
12 | */
|
---|
13 |
|
---|
14 | module.exports = onHeaders
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Create a replacement writeHead method.
|
---|
18 | *
|
---|
19 | * @param {function} prevWriteHead
|
---|
20 | * @param {function} listener
|
---|
21 | * @private
|
---|
22 | */
|
---|
23 |
|
---|
24 | function createWriteHead (prevWriteHead, listener) {
|
---|
25 | var fired = false
|
---|
26 |
|
---|
27 | // return function with core name and argument list
|
---|
28 | return function writeHead (statusCode) {
|
---|
29 | // set headers from arguments
|
---|
30 | var args = setWriteHeadHeaders.apply(this, arguments)
|
---|
31 |
|
---|
32 | // fire listener
|
---|
33 | if (!fired) {
|
---|
34 | fired = true
|
---|
35 | listener.call(this)
|
---|
36 |
|
---|
37 | // pass-along an updated status code
|
---|
38 | if (typeof args[0] === 'number' && this.statusCode !== args[0]) {
|
---|
39 | args[0] = this.statusCode
|
---|
40 | args.length = 1
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | return prevWriteHead.apply(this, args)
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Execute a listener when a response is about to write headers.
|
---|
50 | *
|
---|
51 | * @param {object} res
|
---|
52 | * @return {function} listener
|
---|
53 | * @public
|
---|
54 | */
|
---|
55 |
|
---|
56 | function onHeaders (res, listener) {
|
---|
57 | if (!res) {
|
---|
58 | throw new TypeError('argument res is required')
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (typeof listener !== 'function') {
|
---|
62 | throw new TypeError('argument listener must be a function')
|
---|
63 | }
|
---|
64 |
|
---|
65 | res.writeHead = createWriteHead(res.writeHead, listener)
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Set headers contained in array on the response object.
|
---|
70 | *
|
---|
71 | * @param {object} res
|
---|
72 | * @param {array} headers
|
---|
73 | * @private
|
---|
74 | */
|
---|
75 |
|
---|
76 | function setHeadersFromArray (res, headers) {
|
---|
77 | for (var i = 0; i < headers.length; i++) {
|
---|
78 | res.setHeader(headers[i][0], headers[i][1])
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Set headers contained in object on the response object.
|
---|
84 | *
|
---|
85 | * @param {object} res
|
---|
86 | * @param {object} headers
|
---|
87 | * @private
|
---|
88 | */
|
---|
89 |
|
---|
90 | function setHeadersFromObject (res, headers) {
|
---|
91 | var keys = Object.keys(headers)
|
---|
92 | for (var i = 0; i < keys.length; i++) {
|
---|
93 | var k = keys[i]
|
---|
94 | if (k) res.setHeader(k, headers[k])
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Set headers and other properties on the response object.
|
---|
100 | *
|
---|
101 | * @param {number} statusCode
|
---|
102 | * @private
|
---|
103 | */
|
---|
104 |
|
---|
105 | function setWriteHeadHeaders (statusCode) {
|
---|
106 | var length = arguments.length
|
---|
107 | var headerIndex = length > 1 && typeof arguments[1] === 'string'
|
---|
108 | ? 2
|
---|
109 | : 1
|
---|
110 |
|
---|
111 | var headers = length >= headerIndex + 1
|
---|
112 | ? arguments[headerIndex]
|
---|
113 | : undefined
|
---|
114 |
|
---|
115 | this.statusCode = statusCode
|
---|
116 |
|
---|
117 | if (Array.isArray(headers)) {
|
---|
118 | // handle array case
|
---|
119 | setHeadersFromArray(this, headers)
|
---|
120 | } else if (headers) {
|
---|
121 | // handle object case
|
---|
122 | setHeadersFromObject(this, headers)
|
---|
123 | }
|
---|
124 |
|
---|
125 | // copy leading arguments
|
---|
126 | var args = new Array(Math.min(length, headerIndex))
|
---|
127 | for (var i = 0; i < args.length; i++) {
|
---|
128 | args[i] = arguments[i]
|
---|
129 | }
|
---|
130 |
|
---|
131 | return args
|
---|
132 | }
|
---|