-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathreply.js
397 lines (336 loc) · 9.78 KB
/
reply.js
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
/* eslint-disable no-useless-return */
'use strict'
const eos = require('end-of-stream')
const validation = require('./validation')
const serialize = validation.serialize
const statusCodes = require('http').STATUS_CODES
const flatstr = require('flatstr')
const FJS = require('fast-json-stringify')
const runHooks = require('./hookRunner').onSendHookRunner
const wrapThenable = require('./wrapThenable')
const serializeError = FJS({
type: 'object',
properties: {
statusCode: { type: 'number' },
error: { type: 'string' },
message: { type: 'string' }
}
})
const CONTENT_TYPE = {
JSON: 'application/json; charset=utf-8',
PLAIN: 'text/plain; charset=utf-8',
OCTET: 'application/octet-stream'
}
var getHeader
function Reply (res, context, request) {
this.res = res
this.context = context
this.sent = false
this._serializer = null
this._customError = false
this._isError = false
this.request = request
this._headers = {}
this._hasStatusCode = false
}
Reply.prototype.send = function (payload) {
if (this.sent) {
this.res.log.warn({ err: new Error('Reply already sent') }, 'Reply already sent')
return
}
this.sent = true
if (payload instanceof Error || this._isError === true) {
handleError(this, payload, onSendHook)
return
}
if (payload === undefined) {
onSendHook(this, payload)
return
}
var contentType = getHeader(this, 'content-type')
var hasContentType = contentType !== undefined
if (payload !== null) {
if (Buffer.isBuffer(payload) || typeof payload.pipe === 'function') {
if (hasContentType === false) {
this._headers['content-type'] = CONTENT_TYPE.OCTET
}
onSendHook(this, payload)
return
}
if (hasContentType === false && typeof payload === 'string') {
this._headers['content-type'] = CONTENT_TYPE.PLAIN
onSendHook(this, payload)
return
}
}
if (this._serializer) {
payload = this._serializer(payload)
} else if (hasContentType === false || contentType.indexOf('application/json') > -1) {
if (hasContentType === false || contentType.indexOf('charset') === -1) {
this._headers['content-type'] = CONTENT_TYPE.JSON
}
payload = serialize(this.context, payload, this.res.statusCode)
flatstr(payload)
}
onSendHook(this, payload)
}
Reply.prototype.getHeader = function (key) {
return getHeader(this, key)
}
Reply.prototype.hasHeader = function (key) {
return this._headers[key.toLowerCase()] !== undefined
}
Reply.prototype.removeHeader = function (key) {
// Node.js does not like headers with keys set to undefined,
// so we have to delete the key.
delete this._headers[key.toLowerCase()]
return this
}
Reply.prototype.header = function (key, value) {
var _key = key.toLowerCase()
// default the value to ''
value = value === undefined ? '' : value
if (this._headers[_key] && _key === 'set-cookie') {
// https://tools.ietf.org/html/rfc7230#section-3.2.2
this._headers[_key] = [this._headers[_key]].concat(value)
} else {
this._headers[_key] = value
}
return this
}
Reply.prototype.headers = function (headers) {
var keys = Object.keys(headers)
for (var i = 0; i < keys.length; i++) {
this.header(keys[i], headers[keys[i]])
}
return this
}
Reply.prototype.code = function (code) {
this.res.statusCode = code
this._hasStatusCode = true
return this
}
Reply.prototype.status = Reply.prototype.code
Reply.prototype.serialize = function (payload) {
return serialize(this.context, payload, this.res.statusCode)
}
Reply.prototype.serializer = function (fn) {
this._serializer = fn
return this
}
Reply.prototype.type = function (type) {
this._headers['content-type'] = type
return this
}
Reply.prototype.redirect = function (code, url) {
if (typeof code === 'string') {
url = code
code = this._hasStatusCode ? this.res.statusCode : 302
}
this.header('location', url).code(code).send()
}
function onSendHook (reply, payload) {
if (reply.context.onSend !== null) {
runHooks(
reply.context.onSend,
reply,
payload,
wrapOnSendEnd
)
} else {
onSendEnd(reply, payload)
}
}
function wrapOnSendEnd (err, reply, payload) {
if (err) {
handleError(reply, err)
} else {
onSendEnd(reply, payload)
}
}
function onSendEnd (reply, payload) {
var res = reply.res
var statusCode = res.statusCode
if (payload === undefined || payload === null) {
reply.sent = true
// according to https://tools.ietf.org/html/rfc7230#section-3.3.2
// we cannot send a content-length for 304 and 204, and all status code
// < 200.
if (statusCode >= 200 && statusCode !== 204 && statusCode !== 304) {
reply._headers['content-length'] = '0'
}
res.writeHead(statusCode, reply._headers)
// avoid ArgumentsAdaptorTrampoline from V8
res.end(null, null, null)
return
}
if (typeof payload.pipe === 'function') {
sendStream(payload, res, reply)
return
}
if (typeof payload !== 'string' && !Buffer.isBuffer(payload)) {
throw new TypeError(`Attempted to send payload of invalid type '${typeof payload}'. Expected a string or Buffer.`)
}
if (!reply._headers['content-length']) {
reply._headers['content-length'] = '' + Buffer.byteLength(payload)
}
reply.sent = true
res.writeHead(statusCode, reply._headers)
// avoid ArgumentsAdaptorTrampoline from V8
res.end(payload, null, null)
}
function sendStream (payload, res, reply) {
var sourceOpen = true
eos(payload, { readable: true, writable: false }, function (err) {
sourceOpen = false
if (err) {
if (res.headersSent) {
res.log.error({ err }, 'response terminated with an error with headers already sent')
res.destroy()
} else {
handleError(reply, err)
}
}
// there is nothing to do if there is not an error
})
eos(res, function (err) {
if (err) {
if (res.headersSent) {
res.log.error({ err }, 'response terminated with an error with headers already sent')
}
if (sourceOpen) {
if (payload.destroy) {
payload.destroy()
} else if (typeof payload.close === 'function') {
payload.close(noop)
} else if (typeof payload.abort === 'function') {
payload.abort()
}
}
}
})
// streams will error asynchronously, and we want to handle that error
// appropriately, e.g. a 404 for a missing file. So we cannot use
// writeHead, and we need to resort to setHeader, which will trigger
// a writeHead when there is data to send.
if (!res.headersSent) {
for (var key in reply._headers) {
res.setHeader(key, reply._headers[key])
}
} else {
res.log.warn('response will send, but you shouldn\'t use res.writeHead in stream mode')
}
payload.pipe(res)
}
function handleError (reply, error, cb) {
var res = reply.res
var statusCode = res.statusCode
statusCode = (statusCode >= 400) ? statusCode : 500
// treat undefined and null as same
if (error != null) {
if (error.headers !== undefined) {
reply.headers(error.headers)
}
if (error.status >= 400) {
if (error.status === 404) {
notFound(reply)
return
}
statusCode = error.status
} else if (error.statusCode >= 400) {
if (error.statusCode === 404) {
notFound(reply)
return
}
statusCode = error.statusCode
}
}
res.statusCode = statusCode
if (statusCode >= 500) {
res.log.error({ req: reply.request.raw, res: res, err: error }, error && error.message)
} else if (statusCode >= 400) {
res.log.info({ res: res, err: error }, error && error.message)
}
var customErrorHandler = reply.context.errorHandler
if (customErrorHandler && reply._customError === false) {
reply.sent = false
reply._isError = false
reply._customError = true
var result = customErrorHandler(error, reply.request, reply)
if (result && typeof result.then === 'function') {
wrapThenable(result, reply)
}
return
}
var payload = serializeError({
error: statusCodes[statusCode + ''],
message: error ? error.message : '',
statusCode: statusCode
})
flatstr(payload)
reply._headers['content-type'] = CONTENT_TYPE.JSON
if (cb) {
cb(reply, payload)
return
}
reply._headers['content-length'] = '' + Buffer.byteLength(payload)
reply.sent = true
res.writeHead(res.statusCode, reply._headers)
res.end(payload)
}
function buildReply (R) {
function _Reply (res, context, request) {
this.res = res
this.context = context
this._isError = false
this._customError = false
this.sent = false
this._serializer = null
this.request = request
this._headers = {}
}
_Reply.prototype = new R()
return _Reply
}
function notFound (reply) {
reply.sent = false
reply._isError = false
if (reply.context._404Context === null) {
reply.res.log.warn('Trying to send a NotFound error inside a 404 handler. Sending basic 404 response.')
reply.code(404).send('404 Not Found')
return
}
reply.context = reply.context._404Context
reply.context.handler(reply.request, reply)
}
function noop () {}
function getHeaderProper (reply, key) {
key = key.toLowerCase()
var res = reply.res
var value = reply._headers[key]
if (value === undefined && res.hasHeader(key)) {
value = res.getHeader(key)
}
return value
}
function getHeaderFallback (reply, key) {
key = key.toLowerCase()
var res = reply.res
var value = reply._headers[key]
if (value === undefined) {
value = res.getHeader(key)
}
return value
}
// ponyfill for hasHeader. It has been intoroduced into Node 7.7,
// so it's ok to use it in 8+
{
const v = process.version.match(/v(\d+)/)[1]
if (Number(v) > 7) {
getHeader = getHeaderProper
} else {
getHeader = getHeaderFallback
}
}
module.exports = Reply
module.exports.buildReply = buildReply