-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
323 lines (277 loc) · 7.78 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
var extend = require("extend");
var debug = require('debug')("pigfarm-fetcher");
var hrtime = require("process.hrtime");
var get = require('lodash.get');
var getProtocol = {
parse: function (str) {
var protocol = str.indexOf('//') == -1 ? null : str.split('//')[0];
return {
protocol: protocol.slice(0, -1)
}
}
};
function buildRequestMethod(config) {
// 兼容直接传入url的情况
if (typeof config == 'string') {
config = { url: config };
}
// 从url中获取出协议
var protocol = getProtocol.parse(config.url).protocol;
var requestor;
// 检查是否有对应的requestor
if (!(requestors[protocol] || requestors['default'])) {
throw new Error('unsupport protocol: ' + protocol);
} else {
requestor = requestors[protocol] || requestors['default'];
}
// 找到对应的配置预处理器
var compiler = compilers[protocol];
var urlstring = config.url;
var urltemplate = function (data) {
return urlstring.replace(/\{([\w\.]*)\}/g, function (a, b) {
return get(data, b);
});
}
// 预处理传入的请求配置
compiler && compiler(config);
// 默认的请求逻辑处理hook
config.fixParam = config.fixParam || config.fixBefore || function (a) {
return a
};
config.fixResult = config.fixResult || config.fixAfter || function (a) {
return a;
};
config.onError = config.onError || function (a) {
return false;
};
/**
* 返回出去的函数
* @param data 请求数据
* @return promise
*/
return function (data) {
var timestat = {}; // 请求的耗时数据
var param; // 要传入请求器的数据
var result; // 返回出来的数据
var requestCfg = {};// 传给请求器的配置
var self = this;
return new Promise(function (resolve, reject) {
data = data || {};
// 调用fixBefore来调整处理请求数据
// fixBefore的this指针与调用该函数的this指针相同
var err;
debug('call fixParam');
timestat.fixParam = hrtime();
try {
param = config.fixParam.call(self, data);
} catch (e) {
err = e;
}
timestat.fixParam = hrtime(timestat.fixParam, 'us');
debug('called fixParam');
if (isInvalid(param) || err) {
debug('fixParam failed', param);
err = err || new Error('fixParam(fixBefore) returned ' + param);
param = data; // 把请求参数回复到调用fixBefore之前的状态
return reject(err);
}
debug('start fixUrl');
timestat.fixUrl = hrtime();
// 复制一份请求数据
var requestData;
if (param instanceof Array) {
requestData = param.slice(0);
} else {
requestData = extend({}, param, config.data);
}
// 如果url上存在{var}这样的配置,就替换掉
var url = urltemplate(param);
timestat.fixUrl = hrtime(timestat.fixUrl, 'us');
debug('end fixUrl');
// 拷贝一份请求配置
Object.keys(config).forEach(function (key) {
if (typeof config[key] != 'function') {
requestCfg[key] = config[key];
}
});
requestCfg.url = url;
debug('start globalBeforeHook');
globalBeforeHook.forEach(function (hook) {
var hookData = requestData;
try {
hookData = hook.call(self, hookData);
hookData != void 0 && (requestData = hookData);
} catch (e) {
}
});
requestCfg.data = requestData;
requestCfg.originalData = data;
debug('end globalBeforeHook');
timestat.request = hrtime();
// 调用请求器
debug('do request');
try {
requestor.call({
log: onlog.bind(self, requestCfg)
}, requestCfg, function (err, res, requestinfo) {
requestinfo = requestinfo || {};
err ? reject(extend(err, { // 出错时把请求信息挂在err上
requestinfo: requestinfo,
fetcherErrorType: 'requestor'
})) : resolve({
data: res,
requestinfo: requestinfo
});
});
} catch (e) {
e.fetcherErrorType = 'requestor'; // 确认是请求器的错误
reject(e);
}
}).then(function (res) {
timestat.request = hrtime(timestat.request, 'us');
debug('call fixResult');
timestat.fixResult = hrtime();
debug('start globalAfterHook');
globalAfterHook.forEach(function (hook) {
var hookRequestInfo = extend({}, {time: timestat.request}, res.requestinfo);
try {
hook.call(self, res.data, param, hookRequestInfo, requestCfg);
} catch (e) {
}
});
debug('end globalAfterHook');
try {
// check if the result is legal by invoking fixResult。
result = config.fixResult.call(self, res.data, param, extend({
time: timestat.request
}, res.requestinfo));
} catch (e) {
throw extend(e, {
requestinfo: res.requestinfo
});
}
// if fixResult returned false, treat as an error
if (isInvalid(result)) {
throw extend(new Error('fixResult(fixAfter) returned' + result), {
requestinfo: res.requestinfo
});
}
timestat.fixResult = hrtime(timestat.fixResult, 'us');
debug('called fixResult');
return result
}, function (e) {
timestat.request = hrtime(timestat.request, 'us');
throw e
}).catch(function (err) {
debug('start globalErrorHook');
globalErrorHook.forEach(function (hook) {
var hookResult = extend({}, result);
var hookParam = extend({}, param);
var hookRequestCfg = extend({}, requestCfg);
try {
hook.call(self, err, hookResult, hookParam, hookRequestCfg);
} catch (e) {
}
});
debug('end globalErrorHook');
// if there is an error, use the onError fixer
//
// and the user can ignore the error by returning false in onError fixer.
debug('call onError');
timestat.onError = hrtime();
try {
err = config.onError.call(self, err, result, param);
} catch (e) {
err = e;
}
// if onError returned something and it is not an Error
// treat it as a result
if ((err && !(err instanceof Error)) || err === '') {
var ret = err;
err = null;
}
timestat.onError = hrtime(timestat.onError, 'us');
debug('called onError', ret);
if (err) {
throw err
} else {
return ret;
}
}).then(function (ret) {
timestat.all = Object.keys(timestat)
.map(function (key) {
return timestat[key]
})
.reduce(function (prev, cur) {
return prev + cur
});
// otherwise, return result or null
return {
result: isInvalid(ret) ? null : ret,
timestat: timestat
}
}, function (err) {
err.timestat = timestat;
throw err;
});
}
}
var requestors = {};
var compilers = {};
/**
* entry
*
* @param configs: request configs.
* @returns the request methods.
*/
function factory(configs) {
var exportee = {};
for (var i in configs) {
exportee[i] = buildRequestMethod(configs[i]);
}
return exportee;
}
/**
* parse single config
* @param config
* @returns the request method.
*/
factory.build = buildRequestMethod;
/**
* @param protocol: which protocol will the requestor serve.
* @param fn: request behavior
*/
factory.registerRequestor = function (protocol, fn) {
requestors[protocol] = fn;
};
factory.registerCompiler = function (protocol, fn) {
compilers[protocol] = fn;
};
// 全局钩子的参数与局部钩子一样,但是多了一个请求配置的参数
var globalBeforeHook = [];
var globalAfterHook = []; // 不准用来做修改数据的操作,支持业务做一些数据上报、监控等额外操作
var globalErrorHook = []; // 不准用来做修改数据的操作
factory.registerHook = function (type, cb) {
if (type == 'before') {
globalBeforeHook.push(cb);
} else if (type == 'after') {
globalAfterHook.push(cb);
} else if (type == 'error') {
globalErrorHook.push(cb);
} else {
throw new Error('invalid hook');
}
};
function onlog(config, log) {
debug(log);
}
factory.on = function (event, hook) {
if (event == 'log' && typeof hook == 'function') {
onlog = hook;
}
};
module.exports = factory;
function isInvalid(e) {
return e === null || e === void 0 || e === false;
}