index.js
3.02 KB
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
'use strict';
const URL = require('node-url-utils');
/*
*
*
*/
function seenreq(options) {
let Repo = null;
const Normalizers = [];
options = options || {};
if(!options.repo || options.repo==='default' || options.repo==='memory'){
Repo = require('./lib/repo/default.js');
}else{
const moduleName = `seenreq-repo-${options.repo}`;
try{
Repo = require(moduleName);
}catch(e){
console.error(`\nCannot load module ${moduleName}, please run 'npm install ${moduleName}' and retry\n`);
throw e;
}
}
this.repo = new Repo(options);
if(!options.normalizer){
Normalizers.push(require('./lib/normalizer/default.js'));
}else{
let moduleNames = null;
if(typeof options.normalizer === 'string'){
moduleNames = [options.normalizer];
}else{
moduleNames = options.normalizer;
}
moduleNames.map(moduleName=>{
moduleName = `seenreq-nmlz-${moduleName}`;
try{
Normalizers.push(require(moduleName));
}catch(e){
console.error(`Cannot load module ${moduleName}, please run 'npm install ${moduleName}' and retry`);
}
});
}
this.normalizers = Normalizers.map(ctor => new ctor(options));
this.globalOptions = options;
}
/* Initialize repo
* - callback
* @return Promise if there is no callback
*/
seenreq.prototype.initialize = function(){
return this.repo.initialize();
};
/* Generate method + full uri + body string.
* - req, String|Object
* - [options], Object
* @return, Object. e.g {sign, options}
*/
seenreq.prototype.normalize = function(req, options) {
if(!req){
throw new Error('Argument req is required.');
}
const opt = {
method: 'GET',
body: null
};
options = Object.assign({},this.globalOptions,options);
if (typeof req === 'string') {
opt.uri = req;
}else if(typeof req === 'object'){
Object.assign(opt, req);
opt.uri = opt.uri || opt.url;
}
/* A normalizedRequest is an object of request with some modified keys and values */
const normalizedRequest = this.normalizers.reduce((r, cur) => cur.normalize(r,options), opt);
const sign = [
[normalizedRequest.method, URL.normalize(normalizedRequest.uri, options)].join(' '), normalizedRequest.body
].join('\r\n');
const requestArgsSet = new Set(['uri','url','qs','method','headers','body','form','json','multipart','followRedirect','followAllRedirects', 'maxRedirects','encoding','pool','timeout','proxy','auth','oauth','strictSSL','jar','aws','gzip','time','tunnel','proxyHeaderWhiteList','proxyHeaderExclusiveList','localAddress','forever']);
Object.keys(normalizedRequest).filter(key => !requestArgsSet.has(key) ).forEach(key=>options[key]=normalizedRequest[key]);
return {sign,options};
};
seenreq.prototype.exists = function(req, options) {
if(!req){
throw new Error('Argument req is required.');
}
if (!(req instanceof Array)) {
req = [req];
}
const rs = req.map(r=>this.normalize(r,options));
return this.repo.exists(rs, options).then( rst => rst.length == 1 ? rst[0] : rst);
};
seenreq.prototype.dispose = function() {
return this.repo.dispose();
};
module.exports = seenreq;