-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
205 lines (165 loc) · 5.05 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
'use strict';
/* eslint-disable indent */
const concat = require('concat-stream'),
parse = JSON.parse;
/* eslint-enable indent */
/**
* @ngdoc function
* @name oboeStreamRequest
*
* @description
* Wrapper for using oboe.js & request.js together by providing
* a similar interface as the bundled HTTP client. This should
* permit more options to be set as part of the request eg: PROXY
*
* @param {function} oboe oboe.js module
* @param {function} request request.js module
*
* @example
*
* const request = require('request'),
* oboe = require('request'),
* fetch = require('oboe-stream-request')(oboe, request);
*
* fetch({
* url : 'http://myhost.com/json.json'
* .... other request.js options
* })
* .on('start', () => { ... })
* .on('fail', () => { ... })
* .on('aborted', () => { ... })
* .on('done', () => { ... })
*/
function oboeStreamRequest (oboe, request) {
if (arguments.length < 2) {
throw new Error('oboe-stream-request, expects 2 arguments');
}
const OBOE_ABORTED_EVENT = 7;
return (requestOptions) => {
let req = request(requestOptions),
oboeStream = oboe(),
handlers = oboeStreamRequest.setup(req, oboeStream);
req
.on('response', handlers.onResponse)
.on('error', handlers.onError);
// oboe will emit this event internally when an abort is received, but
// its not really publically documented, and has a non-intuitive name
oboeStream.on(OBOE_ABORTED_EVENT, handlers.onAborted);
return oboeStream;
};
}
/**
* @ngdoc function
* @name setup
*
* @description
* Handles the incoming response before the request body has
* been fully received and emits the correct oboe events for
* start, data & end.
*
* @param {object} res readable stream
* @param {object} oboeStream an oboe stream
*
*/
oboeStreamRequest.setup = (req, oboeStream) => {
let self = oboeStreamRequest,
aborted = false;
return {
/**
* @ngdoc function
* @name onResponse
*
* @description
* Handles the incoming response before the request body has
* been fully received and emits the correct oboe events for
* start, data & end.
*
* Based upon behavior seen here :
* https://github.com/jimhigson/oboe.js/blob/master/src/streamingHttp.node.js#L91
*
* @param {object} res readable stream
*
*/
onResponse : (res) => {
let statusCode = res.statusCode,
successful = String(statusCode)[0] === '2';
// a non-native oboe stream will not emit a start
// event - since this is still a HTTP request send
// one to maintain parity
oboeStream.emit('start', statusCode, res.headers);
if (!successful) {
return req.pipe(concat((errorBody) => {
oboeStream.emit(
'fail', self.errorReport(statusCode, errorBody.toString())
);
}));
}
req.on('data', (chunk) => {
if (!aborted) { oboeStream.emit('data', chunk.toString()); }
});
req.on('end', () => {
if (!aborted) { oboeStream.emit('end'); }
});
},
/**
* @ngdoc function
* @name onError
*
* @description
* Generic handler for emitting errors via oboe
*
* @param {object} error an error instance
*
*/
onError : (error) => {
oboeStream.emit(
'fail', self.errorReport(undefined, undefined, error)
);
},
/**
* @ngdoc function
* @name onAborted
*
* @description
* Upon receiving an abort event ensure that we end the request,
* and flag it has being aborted.
*
* As oboe does not send a public "aborted" event, emit one
*/
onAborted : () => {
aborted = true;
req.abort();
oboeStream.emit(
'aborted', self.errorReport(
undefined, undefined, new Error('HTTP request aborted')
)
);
}
};
};
/**
* @ngdoc function
* @name errorReport
*
* @description
* Lifted directly from oboe to maintain a consistent error
* response
*
* @param {number} statusCode incoming HTTP status code
* @param {string} body response body
* @param {object} error an error instance
*
*/
oboeStreamRequest.errorReport = (statusCode, body, error) => {
let jsonBody;
try {
jsonBody = parse(body);
} catch (e) {}
return {
statusCode : statusCode,
body : body,
jsonBody : jsonBody,
thrown : error
};
};
module.exports = oboeStreamRequest;