forked from fastify/light-my-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
213 lines (187 loc) · 4.85 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
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
'use strict'
const assert = require('assert')
const http = require('http')
const Ajv = require('ajv')
const Request = require('./lib/request')
const Response = require('./lib/response')
const errorMessage = 'The dispatch function has already been invoked'
const urlSchema = {
oneOf: [
{ type: 'string' },
{
type: 'object',
properties: {
protocol: { type: 'string' },
hostname: { type: 'string' },
pathname: { type: 'string' }
// port type => any
// query type => any
},
additionalProperties: true,
required: ['pathname']
}
]
}
const ajv = new Ajv()
const schema = {
type: 'object',
properties: {
url: urlSchema,
path: urlSchema,
cookies: {
type: 'object',
additionalProperties: true
},
headers: {
type: 'object',
additionalProperties: true
},
query: {
type: 'object',
additionalProperties: true
},
simulate: {
type: 'object',
properties: {
end: { type: 'boolean' },
split: { type: 'boolean' },
error: { type: 'boolean' },
close: { type: 'boolean' }
}
},
authority: { type: 'string' },
remoteAddress: { type: 'string' },
method: { type: 'string', enum: http.METHODS.concat(http.METHODS.map(toLowerCase)) },
validate: { type: 'boolean' }
// payload type => any
},
additionalProperties: true,
oneOf: [
{ required: ['url'] },
{ required: ['path'] }
]
}
const optsValidator = ajv.compile(schema)
function inject (dispatchFunc, options, callback) {
if (typeof callback === 'undefined') {
return new Chain(dispatchFunc, options)
} else {
return doInject(dispatchFunc, options, callback)
}
}
function makeRequest (dispatchFunc, server, req, res) {
req.once('error', function (err) {
if (this.destroyed) res.destroy(err)
})
req.once('close', function () {
if (this.destroyed && !this._error) res.destroy()
})
return req.prepare(() => dispatchFunc.call(server, req, res))
}
function doInject (dispatchFunc, options, callback) {
options = (typeof options === 'string' ? { url: options } : options)
if (options.validate !== false) {
assert(typeof dispatchFunc === 'function', 'dispatchFunc should be a function')
const isOptionValid = optsValidator(options)
if (!isOptionValid) {
throw new Error(optsValidator.errors.map(e => e.message))
}
}
const server = options.server || {}
if (typeof callback === 'function') {
const req = new Request(options)
const res = new Response(req, callback)
return makeRequest(dispatchFunc, server, req, res)
} else {
return new Promise((resolve, reject) => {
const req = new Request(options)
const res = new Response(req, resolve, reject)
makeRequest(dispatchFunc, server, req, res)
})
}
}
function Chain (dispatch, option) {
if (typeof option === 'string') {
this.option = { url: option }
} else {
this.option = Object.assign({}, option)
}
this.dispatch = dispatch
this._hasInvoked = false
this._promise = null
if (this.option.autoStart !== false) {
process.nextTick(() => {
if (!this._hasInvoked) {
this.end()
}
})
}
}
const httpMethods = [
'delete',
'get',
'head',
'options',
'patch',
'post',
'put',
'trace'
]
httpMethods.forEach(method => {
Chain.prototype[method] = function (url) {
if (this._hasInvoked === true || this._promise) {
throw new Error(errorMessage)
}
this.option.url = url
this.option.method = method.toUpperCase()
return this
}
})
const chainMethods = [
'body',
'cookies',
'headers',
'payload',
'query'
]
chainMethods.forEach(method => {
Chain.prototype[method] = function (value) {
if (this._hasInvoked === true || this._promise) {
throw new Error(errorMessage)
}
this.option[method] = value
return this
}
})
Chain.prototype.end = function (callback) {
if (this._hasInvoked === true || this._promise) {
throw new Error(errorMessage)
}
this._hasInvoked = true
if (typeof callback === 'function') {
doInject(this.dispatch, this.option, callback)
} else {
this._promise = doInject(this.dispatch, this.option)
return this._promise
}
}
Object.getOwnPropertyNames(Promise.prototype).forEach(method => {
if (method === 'constructor') return
Chain.prototype[method] = function (...args) {
if (!this._promise) {
if (this._hasInvoked === true) {
throw new Error(errorMessage)
}
this._hasInvoked = true
this._promise = doInject(this.dispatch, this.option)
}
return this._promise[method](...args)
}
})
function isInjection (obj) {
return (obj instanceof Request || obj instanceof Response)
}
function toLowerCase (m) { return m.toLowerCase() }
module.exports = inject
module.exports.inject = inject
module.exports.isInjection = isInjection