/
requestIterator.js
132 lines (116 loc) · 4 KB
/
requestIterator.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
'use strict'
const hyperid = require('hyperid')
const inherits = require('util').inherits
const requestBuilder = require('./httpRequestBuilder')
const clone = require('lodash.clonedeep')
const chunk = require('lodash.chunk')
const flatten = require('lodash.flatten')
const toHeaderKeyValue = (rawHeaders) => {
const tupleHeaders = chunk(rawHeaders, 2)
const headers = {}
tupleHeaders.forEach((val) => {
const currentValue = headers[val[0]]
if (!currentValue) {
headers[val[0]] = val[1]
} else {
headers[val[0]] = flatten([currentValue, val[1]])
}
})
return headers
}
function RequestIterator (opts) {
if (!(this instanceof RequestIterator)) {
return new RequestIterator(opts)
}
this.hyperid = hyperid({ urlSafe: true })
this.resetted = false
this.headers = {}
this.initialContext = opts.initialContext || {}
this.resetContext()
this.reqDefaults = opts
this.requestBuilder = requestBuilder(opts)
this.setRequests(opts.requests)
}
inherits(RequestIterator, Object)
RequestIterator.prototype.resetContext = function () {
this.context = clone(this.initialContext)
}
RequestIterator.prototype.nextRequest = function () {
this.resetted = false
++this.currentRequestIndex
// when looping over available request, clear context for a fresh start
if (this.currentRequestIndex === this.requests.length) {
this.resetContext()
this.currentRequestIndex = 0
}
this.currentRequest = this.requests[this.currentRequestIndex]
// only builds if it has dynamic setup
if (this.reqDefaults.idReplacement || typeof this.currentRequest.setupRequest === 'function') {
this.rebuildRequest()
}
return this.currentRequest
}
RequestIterator.prototype.nextRequestBuffer = function () {
// get next request
this.nextRequest()
return this.currentRequest.requestBuffer
}
RequestIterator.prototype.setRequests = function (newRequests) {
this.resetted = false
this.requests = newRequests || [{}]
this.currentRequestIndex = 0
// build all request which don't have dynamic setup, except if it's the first one
this.requests.forEach((request, i) => {
this.currentRequest = request
if (i === 0 || typeof request.setupRequest !== 'function') {
this.rebuildRequest()
}
})
this.currentRequest = this.requests[0]
}
RequestIterator.prototype.setHeaders = function (newHeaders) {
this.headers = newHeaders || {}
this.currentRequest.headers = this.headers
this.rebuildRequest()
}
RequestIterator.prototype.setBody = function (newBody) {
this.currentRequest.body = newBody || Buffer.alloc(0)
this.rebuildRequest()
}
RequestIterator.prototype.setHeadersAndBody = function (newHeaders, newBody) {
this.headers = newHeaders || {}
this.currentRequest.headers = this.headers
this.currentRequest.body = newBody || Buffer.alloc(0)
this.rebuildRequest()
}
RequestIterator.prototype.setRequest = function (newRequest) {
this.currentRequest = newRequest || {}
this.rebuildRequest()
}
RequestIterator.prototype.rebuildRequest = function () {
let data
this.resetted = false
if (this.currentRequest) {
this.currentRequest.headers = this.currentRequest.headers || this.headers
data = this.requestBuilder(this.currentRequest, this.context)
if (data) {
this.currentRequest.requestBuffer = this.reqDefaults.idReplacement
? Buffer.from(data.toString().replace(/\[<id>\]/g, this.hyperid()))
: data
} else if (this.currentRequestIndex === 0) {
// when first request fails to build, we can not reset pipeline, or it'll never end
throw new Error('First setupRequest() failed did not returned valid request. Stopping')
} else {
this.currentRequestIndex = this.requests.length - 1
this.nextRequest()
this.resetted = true
}
}
return data
}
RequestIterator.prototype.recordBody = function (request, status, body, headers) {
if (request && typeof request.onResponse === 'function') {
request.onResponse(status, body, this.context, toHeaderKeyValue(headers || []))
}
}
module.exports = RequestIterator