-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
87 lines (75 loc) · 1.92 KB
/
index.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
'use strict'
/**
* To do: move most of the body parsing stuff to a separate library.
*/
const get = require('raw-body')
const qs = require('querystring')
module.exports = (app) => {
Object.keys(request).forEach((key) => {
app.request[key] = request[key]
})
Object.keys(response).forEach((key) => {
app.response[key] = response[key]
})
return app
}
const request = {}
const response = {}
request.json = function (limit) {
if (!this.length) return Promise.resolve()
return this.text(limit).then((text) => this._parse_json(text))
}
request._parse_json = function (text) {
if (this.app.jsonStrict !== false) {
text = text.trim()
const first = text[0]
if (first !== '{' && first !== '[') {
this.ctx.throw(400, 'only json objects or arrays allowed')
}
}
try {
return JSON.parse(text)
} catch (err) {
this.ctx.throw(400, 'invalid json received')
}
}
request.urlencoded = function (limit) {
if (!this.length) return Promise.resolve()
return this.text(limit).then((text) => this._parse_urlencoded(text))
}
request._parse_urlencoded = function (text) {
const parse = (this.app.querystring || qs).parse
try {
return parse(text)
} catch (err) {
this.ctx.throw(400, 'invalid urlencoded received')
}
}
request.body = function (limit) {
switch (this.is('urlencoded', 'json')) {
case 'json': return this.json(limit)
case 'urlencoded': return this.urlencoded(limit)
}
}
request.text = function (limit) {
this.response.writeContinue()
return get(this.req, {
limit: limit || '100kb',
length: this.length,
encoding: 'utf8'
})
}
request.buffer = function (limit) {
this.response.writeContinue()
return get(this.req, {
limit: limit || '1mb',
length: this.length
})
}
response.writeContinue = function () {
if (!this._checkedContinue && this.req.checkContinue) {
this.res.writeContinue()
this._checkedContinue = true
}
return this
}