-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
768 additions
and
1,027 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module.exports = { | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"mocha": true, | ||
|
||
}, | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
"tab" | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"no-console":[ | ||
"off" | ||
] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- 4.6 | ||
- 6.7 | ||
- 6.9 | ||
- 6 | ||
- 8 | ||
- 10 | ||
os: | ||
- linux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Upgrade guide | ||
## api changes from version below 1.0 to 1.x | ||
* `exists` are changed to node-style arguments with callback. In previous version it uses memory as default repo to store keys, you can get result by return value but you can't in new version. seenreq uses `process.nextTick` to produce asynchronous callback. So be careful to change your code to get result in callback even if you use defualt memory repo. | ||
* `normalize` return value is an object now, it looks like: `{sign:"GET http://www.google.com\r\n",options:{key1:"val"}}`, the sign is same as the returned string by normalize before. | ||
* `options.update` is changed to `options.rupdate` to avoid duplicate, so you can place `rupdate` in `request`, e.g. `{uri:"http://www.google.com", rupdate:false}`. It also takes effect to place it in `options`. | ||
* Use `initialize` before use `exists` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(`Cannot load module ${moduleName}, please run 'npm install ${moduleName}' and retry`); | ||
return; | ||
} | ||
} | ||
|
||
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); | ||
}; | ||
|
||
seenreq.prototype.dispose = function() { | ||
return this.repo.dispose(); | ||
}; | ||
|
||
module.exports = seenreq; |
Oops, something went wrong.