This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbase.js
292 lines (251 loc) · 6 KB
/
base.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
const MwPool = require('midware-pool')
const assign = require('lodash').assign
const middleware = require('./middleware')
const Emitter = require('events').EventEmitter
module.exports = Base
/**
* Base generic interface implementing HTTP shared logic and features
* inherited across different HTTP abstract entities.
*
* @param {Object} opts
* @class Base
* @extend EventEmitter
* @constructor
*/
function Base (opts) {
Emitter.call(this)
this.replays = []
this.opts = opts || {}
this.mw = new MwPool()
}
Base.prototype = Object.create(Emitter.prototype)
/**
* Target URI to foward the incoming traffic.
*
* @param {String} url
* @param {Object} opts
* @return {Base}
* @method target
* @alias forward
* @alias forwardTo
*/
Base.prototype.target =
Base.prototype.forward =
Base.prototype.forwardTo = function (url, opts) {
if (Array.isArray(url)) {
return this.balance(url)
}
this.opts.target = url
assign(this.opts, opts)
return this
}
/**
* Defines multiple URLs to balance the icnoming traffic.
*
* @param {Array} urls
* @return {Base}
* @method balance
*/
Base.prototype.balance = function (urls) {
if (Array.isArray(urls)) {
this.opts.balance = urls
}
return this
}
/**
* Defines a target replay server URL.
* You can optionally pass a replay only custom options.
*
* Call this method multiple times to replay
* traffic to multiple targets.
*
* @param {String} url
* @param {Object} opts
* @return {Base}
* @method replay
* @alias replayTo
*/
Base.prototype.replay =
Base.prototype.replayTo = function (url, opts) {
url = typeof url === 'string'
? { target: url }
: url
const replay = assign({}, url, opts)
this.replays.push(replay)
return this
}
/**
* Defines custom options, optionally overriden the existent ones.
*
* @param {Object} opts
* @return {Base}
* @method options
*/
Base.prototype.options = function (opts) {
assign(this.opts, opts)
return this
}
/**
* Enable first forward, and then if all success, replay operation mode, optionally passing
* a middleware-like function to determine if the forward mode
* should be applied or not to the current HTTP request.
*
* @param {Function} filter
* @return {Base}
* @method sequential
* @alias replayAfterForward
*/
Base.prototype.sequential =
Base.prototype.replayAfterForward = function (filter) {
this.bufferBody(filter)
this.opts.replayAfterForward = true
return this
}
/**
* Enable sequential replay mode. In case that one replay server fails
* the subsequent pending replay request will be canceled.
*
* @param {Function} filter
* @return {Base}
* @method replaySequentially
*/
Base.prototype.replaySequentially = function (filter) {
this.bufferBody(filter)
this.opts.replaySequentially = true
return this
}
/**
* Enable retry/backoff logic for HTTP traffic.
* The resilient algorithm will retry with a simple exponential backoff
* logic multiple times (configurable), until the server
* replies with a proper status.
*
* Enabling this implies that all the request payload data will be buffered in heap memory.
* Don't use it when dealing with large payloads.
*
* @param {Object} opts
* @param {Function} filter
* @return {Base}
* @method retry
*/
Base.prototype.retry = function (opts, filter) {
this.bufferBody(filter)
this.opts.retry = opts
return this
}
/**
* Shortcut method to stop replaying HTTP traffic.
*
* @return {Base}
* @method stopReplay
*/
Base.prototype.stopReplay = function () {
this.replays.splice(0)
return this
}
/**
* Adds custom HTTP headers.
*
* @param {Object} headers
* @return {Base}
* @method headers
*/
Base.prototype.headers = function (headers) {
this.use(middleware.headers(headers))
return this
}
/**
* Adds query params.
*
* @param {Object} query
* @return {Base}
* @method query
*/
Base.prototype.query = function (query) {
this.use(middleware.query(query))
return this
}
/**
* Specifies a custom request timeout in miliseconds. Defaults to `30000`.
*
* @param {Number} ms
* @return {Base}
* @method timeout
*/
Base.prototype.timeout = function (ms) {
this.opts.timeout = ms
return this
}
/**
* Waits and buffer all the request payload data buffer in heap before forwaring/replaing it.
* Useful for intercepting and modifying payloads.
*
* @param {Function} filter
* @return {Base}
* @method bufferBody
* @alias inteceptBody
*/
Base.prototype.bufferBody =
Base.prototype.interceptBody = function (filter) {
function pass (req, res, next) { next() }
this.use(middleware.requestBody(pass, filter))
return this
}
/**
* Attaches a custom middleware function to the given phase.
*
* @param {String} phase
* @param {Function} middleware
* @return {Base}
* @method useFor
*/
Base.prototype.useFor = function () {
this.mw.use.apply(this.mw, arguments)
return this
}
/**
* Attaches a custom middleware function to the forward incoming phase.
*
* @param {Function} middleware
* @return {Base}
* @method useForward
*/
Base.prototype.useForward = function () {
this.useFor('forward', arguments)
return this
}
/**
* Attaches a custom middleware function to the replay incoming phase.
*
* @param {Function} middleware
* @return {Base}
* @method useReplay
*/
Base.prototype.useReplay = function () {
this.useFor('replay', arguments)
return this
}
/**
* Attaches a custom middleware function to the outgoing phase.
*
* @param {Function} middleware
* @return {Base}
* @method useResponse
* @alias useOutgoing
*/
Base.prototype.useOutgoing =
Base.prototype.useResponse = function () {
const stack = this.mw.stack('response')
if (!stack || stack.length < 2) {
this.use(middleware.responseBody(function dispatch (req, res, next) {
// If body was already intercepted, just continue with it
if (res._alreadyIntercepted) return next()
// Flag the response as intercepted
res._alreadyIntercepted = true
// Run the response middleware
this.mw.run('response', req, res, next)
}.bind(this)))
}
this.useFor('response', arguments)
return this
}