Some code base improvements

This commit is contained in:
Wessel Tip
2019-06-07 20:22:59 +02:00
parent f8fa21e384
commit faa6deb8a9
7 changed files with 145 additions and 91 deletions

64
.eslintrc Normal file
View File

@@ -0,0 +1,64 @@
{
"env": {
"es6": true,
"amd": true,
"node": true,
"mongo": true,
"jquery": true,
"browser": true,
"commonjs": true
},
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"forOf": true,
"spread": true,
"modules": true,
"classes": true,
"generators": true,
"restParams": true,
"regexUFlag": true,
"regexYFlag": true,
"globalReturn": true,
"destructuring": true,
"impliedStrict": true,
"blockBindings": true,
"defaultParams": true,
"octalLiterals": true,
"arrowFunctions": true,
"binaryLiterals": true,
"templateStrings": true,
"superInFunctions": true,
"unicodeCodePointEscapes": true,
"objectLiteralShorthandMethods": true,
"objectLiteralComputedProperties": true,
"objectLiteralDuplicateProperties": true,
"objectLiteralShorthandProperties": true
}
},
"plugins": [],
"rules": {
"semi": "warn",
"indent": [ 0, 2 ],
"strict": "off",
"eqeqeq": "error",
"no-var": "warn",
"no-undef": "warn",
"valid-jsdoc": "warn",
"comma-dangle": "warn",
"no-dupe-args": "warn",
"no-dupe-keys": "warn",
"require-await": "warn",
"spaced-comment": "error",
"space-in-parens": "error",
"no-global-assign": "warn",
"no-duplicate-imports": "error",
"no-dupe-class-members": "error"
},
"globals": {
"_config": false,
"console": false
}
}

View File

@@ -1,14 +1,16 @@
const util = require('util'); const util = require('util');
const wump = require('../lib'); const wump = require('../lib');
console.log(`Using wumpfetch v${wump.version} [${wump.userAgent}]\n\n`); console.log(`Using wumpfetch v${wump.version} [${wump.userAgent}]`);
;(async() => { ;(async() => {
const requests = [ const requests = [
await wump({ url: 'https://jsonplaceholder.typicode.com/todos/1', parse: 'json' }).send(), await wump({ url: 'https://jsonplaceholder.typicode.com/todos/1', parse: 'json' }).send(),
await wump('https://jsonplaceholder.typicode.com/todos/1', { chaining: false }) await wump('https://jsonplaceholder.typicode.com/todos/1', { chain: false }),
await wump('https://jsonplaceholder.typicode.com/posts', { chain: false, method: 'POST', body: { title: 'yeet', body: 'us', userId: 1 } })
]; ];
console.log(`Test 1: \n${util.inspect(requests[0].body)}\n\n`); console.log(`Test 1: \n${util.inspect(requests[0].body)}\n`);
console.log(`Test 2: \n${util.inspect(requests[1].json())}\n\n`); console.log(`Test 2: \n${util.inspect(requests[1].json())}\n`);
console.log(`Test 3: \n${util.inspect(requests[2].parse())}\n`);
})(); })();

View File

@@ -142,7 +142,7 @@ const WumpRequest = class WumpRequest {
if (this.o.compressed) { if (this.o.compressed) {
if (res.headers['content-encoding'] === 'gzip') stream = res.pipe(createGunzip()); if (res.headers['content-encoding'] === 'gzip') stream = res.pipe(createGunzip());
else if (res.headers[ 'content-encoding'] === 'deflate') stream = res.pipe(createInflate()); else if (res.headers['content-encoding'] === 'deflate') stream = res.pipe(createInflate());
} }
let wumpRes; let wumpRes;

View File

@@ -1,18 +1,15 @@
const req = require('./model/WumpRequest'); const req = require('./model/WumpRequest');
const pkg = require('../package.json');
const common = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ]; const common = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
const metaData = require('../package.json');
module.exports = (url, method) => { module.exports = (url, method) => { return new req(url, method); };
return new req(url, method);
};
common.forEach((v) => { for (const method of common) {
module.exports[v.toLowerCase()] = (url, method) => { module.exports[method.toLowerCase()] = (url, method) => {
if (typeof url === 'string') return new req(url, { method: v, ...method }); if (typeof url === 'string') return new req(url, { method: method, ...method });
else return new req({ method: v, ...url }, method); else return new req({ method: method, ...url }, method);
} };
}); }
module.exports.version = pkg.version; module.exports.version = metaData.version;
module.exports.userAgent = `${pkg.name}/${pkg.version} (${pkg.repository.url})`; module.exports.userAgent = `${metaData.name}/${metaData.version} (${metaData.repository.url})`;

View File

@@ -1,75 +1,73 @@
const u = require('url'); const zlib = require('zlib');
const h = require('http'); const http = require('http');
const hs = require('https'); const https = require('https');
const pa = require('path'); const { URL } = require('url');
const qs = require('querystring'); const { stringify } = require('querystring');
const zl = require('zlib'); const { join: pJoin } = require('path');
const pkg = require('../../package.json');
const WumpRes = require('./WumpResponse');
const w = require('../index'); const w = require('../index');
const c = [ 'gzip', 'deflate' ]; const WumpRes = require('./WumpResponse');
const metaData = require('../../package.json');
const compressions = [ 'gzip', 'deflate' ];
module.exports = class WumpRequest { module.exports = class WumpRequest {
constructor (url = {}, method = {}) { constructor (url = {}, method = {}) {
const o = (typeof url === 'string' ? method : url); const o = (typeof url === 'string' ? method : url);
const obj = (typeof o === 'object');
if (typeof url !== 'string' && !o.hasOwnProperty('url')) throw new Error('Missing URL parameter'); if (typeof url !== 'string' && !o.hasOwnProperty('url')) throw new Error('Missing URL parameter');
this.o = { this.o = {
'm': typeof o === 'string' ? o : obj && o.method ? o.method : 'GET', 'm': typeof o === 'string' ? o : (o.method || 'GET'),
'url': typeof url === 'string' ? new u.URL(url) : obj && typeof o.url === 'string' ? new u.URL(o.url) : url, 'url': typeof url === 'string' ? new URL(url) : typeof o.url === 'string' ? new URL(o.url) : url,
'SDA': obj && typeof o.sendDataAs === 'string' ? o.sendDataAs : obj && o.data && typeof o.data === 'object' ? 'json' : undefined, 'SDA': typeof o.sendDataAs === 'string' ? o.sendDataAs : o.data && typeof o.data === 'object' ? 'json' : undefined,
'data': obj && o.body ? o.body : obj && o.data ? o.data : obj && o.json ? o.json : obj && o.form ? qs.stringify(o.form) : undefined, 'data': o.body || o.data || o.json || (o.form ? stringify(o.form) : undefined),
'parse': obj && o.parse ? o.parse : undefined, 'parse': o.parse,
'chain': obj && typeof o.chaining === 'boolean' ? o.chaining : true, 'chain': typeof o.chain === 'boolean' ? o.chain : true,
'follow': !!(obj && o.followRedirects), 'follow': !!(o.followRedirects),
'rHeaders': obj && typeof o.headers === 'object' ? o.headers : {}, 'rHeaders': typeof o.headers === 'object' ? o.headers : {},
'streamed': !!(obj && o.streamed), 'streamed': !!(o.streamed),
'compressed': !!(obj && o.compressed), 'compressed': !!(o.compressed),
'timeoutTime': obj && typeof o.timeout === 'number' ? o.timeout : null, 'timeoutTime': typeof o.timeout === 'number' ? o.timeout : null,
'coreOptions': obj && typeof o.coreOptions === 'object' ? o.coreOptions : {} 'coreOptions': typeof o.coreOptions === 'object' ? o.coreOptions : {}
}; };
if (typeof o.core === 'object') Object.keys(o.core).forEach((v) => this.option(v, o.core[v])); if (typeof o.core === 'object') for (const key of Object.keys(o.core)) this.option(key, o.core[key]);
if (!this.o.chain) return this.send(); if (!this.o.chain) return this.send();
return this; return this;
} }
query (a, b) { query (name, value) {
if (typeof a === 'object') Object.keys(a).forEach((v) => this.o.url.searchParams.append(v, a[v])); if (typeof a === 'object') for (const key of Object.keys(name)) this.o.url.searchParams.append(key, name[key]);
else this.o.url.searchParams.append(a, b); else this.o.url.searchParams.append(name, value);
return this; return this;
} }
body (data, SA) { body (data, sendAs) {
this.o.SDA = typeof data === 'object' && !SA && !Buffer.isBuffer(data) ? 'json' : (SA ? SA.toLowerCase() : 'buffer'); this.o.SDA = typeof data === 'object' && !sendAs && !Buffer.isBuffer(data) ? 'json' : sendAs ? sendAs.toLowerCase() : 'buffer';
this.o.data = this.SDA === 'form' ? qs.stringify(data) : (this.SDA === 'json' ? JSON.stringify(data) : data); this.o.data = this.sendAs === 'form' ? stringify(data) : this.SDA === 'json' ? JSON.stringify(data) : data;
return this; return this;
} }
header (a, b) { header (name, value) {
if (typeof a === 'object') Object.keys(a).forEach((v) => this.o.rHeaders[v.toLowerCase()] = a[v]); if (typeof name === 'object') for (const key of Object.keys(name)) this.o.rHeaders[key.toLowerCase()] = name[key];
else this.o.rHeaders[a.toLowerCase()] = b; else this.o.rHeaders[name.toLowerCase()] = value;
return this; return this;
} }
compress () { compress () {
this.compressed = true; this.compressed = true;
if (!this.o.rHeaders['accept-encoding']) this.o.rHeaders['accept-encoding'] = c.join(', '); if (!this.o.rHeaders['accept-encoding']) this.o.rHeaders['accept-encoding'] = compressions.join(', ');
return this; return this;
} }
path (p) { path (p) {
this.o.url.pathname = pa.join(this.o.url.pathname, p); this.o.url.pathname = pJoin(this.o.url.pathname, p);
return this; return this;
} }
@@ -80,12 +78,12 @@ module.exports = class WumpRequest {
return this; return this;
} }
option (n, v) { option (name, value) {
this.o.coreOptions[n] = v; this.o.coreOptions[name] = value;
return this; return this;
} }
timeout (timeout) { timeout (timeout = 0) {
this.o.timeoutTime = timeout; this.o.timeoutTime = timeout;
return this; return this;
@@ -94,7 +92,7 @@ module.exports = class WumpRequest {
send () { send () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.o.data) { if (this.o.data) {
if (!this.o.rHeaders.hasOwnProperty('user-agent')) this.o.rHeaders['User-Agent'] = w.userAgent || `${pkg.name}/${pkg.version} (${pkg.repository.urll})`; if (!this.o.rHeaders.hasOwnProperty('user-agent')) this.o.rHeaders['User-Agent'] = w.userAgent || `${metaData.name}/${metaData.version} (${metaData.repository.url})`;
if (this.o.SDA === 'json' && !this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/json'; if (this.o.SDA === 'json' && !this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/json';
if (this.o.SDA === 'form') { if (this.o.SDA === 'form') {
@@ -107,19 +105,21 @@ module.exports = class WumpRequest {
const options = { const options = {
'host': this.o.url.hostname, 'host': this.o.url.hostname,
'port': this.o.url.port, 'port': this.o.url.port,
'path': this.o.url.pathname + this.o.url.search, 'path': `${this.o.url.pathname}${this.o.url.search}`,
'method': this.o.m, 'method': this.o.m,
'headers': this.o.rHeaders, 'headers': this.o.rHeaders,
'protocol': this.o.url.protocol, 'protocol': this.o.url.protocol,
...this.o.coreOptions ...this.o.coreOptions
} };
const handler = async (res) => { const handler = async (res) => {
let stream = res; let stream = res;
if (this.o.compressed) { if (this.o.compressed) {
if (res.headers['content-encoding'] === 'gzip') stream = res.pipe(zl.createGunzip()); switch (res.headers['content-encoding']) {
else if (res.headers[ 'content-encoding'] === 'deflate') stream = res.pipe(zl.createInflate()); case 'gzip': stream = res.pipe(zlib.createGunzip()); break;
case 'defalte': stream = res.pipe(zlib.createInflate()); break;
}
} }
let wumpRes; let wumpRes;
@@ -129,7 +129,7 @@ module.exports = class WumpRequest {
wumpRes = new WumpRes(res); wumpRes = new WumpRes(res);
if (res.headers.hasOwnProperty('location') && this.o.follow) { if (res.headers.hasOwnProperty('location') && this.o.follow) {
this.o.url = (new u.URL(res.headers['location'], this.o.url)).toString(); this.o.url = (new URL(res.headers['location'], this.o.url)).toString();
return await w(this.o); return await w(this.o);
} }
@@ -137,9 +137,11 @@ module.exports = class WumpRequest {
stream.on('data', (c) => wumpRes._addChunk(c)); stream.on('data', (c) => wumpRes._addChunk(c));
stream.on('end', () => { stream.on('end', () => {
if (this.o.parse) { if (this.o.parse) {
if (this.o.parse === 'json') wumpRes.body = JSON.parse(wumpRes.body); switch (this.o.parse) {
else if (this.o.parse === 'text') wumpRes.body = wumpRes.body.toString(); case 'json': wumpRes.body = JSON.parse(wumpRes.body); break;
else wumpRes.body = wumpRes.body; case 'text': wumpRes.body = wumpRes.body.toString(); break;
default: wumpRes.body = wumpRes.body;
}
} }
resolve(wumpRes); resolve(wumpRes);
@@ -147,9 +149,11 @@ module.exports = class WumpRequest {
} }
}; };
if (this.o.url.protocol === 'http:') req = h.request(options, handler); switch (this.o.url.protocol) {
else if (this.o.url.protocol === 'https:') req = hs.request(options, handler); case 'http:': req = http.request(options, handler); break;
else throw new Error(`Bad URL protocol: ${this.o.url.protocol}`); case 'https:': req = https.request(options, handler); break;
default: throw new Error(`Bad URL protocol: ${this.o.url.protocol}`);
}
if (this.o.timeoutTime) { if (this.o.timeoutTime) {
req.setTimeout(this.o.timeoutTime, () => { req.setTimeout(this.o.timeoutTime, () => {
@@ -162,12 +166,10 @@ module.exports = class WumpRequest {
if (this.o.data) { if (this.o.data) {
if (this.o.SDA === 'json') req.write(JSON.stringify(this.o.data)); if (this.o.SDA === 'json') req.write(JSON.stringify(this.o.data));
else { else if (this.o.data instanceof Object) req.write(JSON.stringify(this.o.data));
if (typeof this.o.data === 'object') req.write(JSON.stringify(this.o.data)); else req.write(this.o.data);
else req.write(this.o.data);
}
} }
req.end(); req.end();
}); });
} }

View File

@@ -2,7 +2,6 @@ module.exports = class WumpResponse {
constructor (res) { constructor (res) {
this.body = Buffer.alloc(0); this.body = Buffer.alloc(0);
this.coreRes = res; this.coreRes = res;
this.headers = res.headers; this.headers = res.headers;
this.statusCode = res.statusCode; this.statusCode = res.statusCode;
} }
@@ -12,19 +11,11 @@ module.exports = class WumpResponse {
} }
parse () { parse () {
if (this.headers['content-type'] === 'application/json') return JSON.parse(this.body); if (this.headers['content-type'].includes('application/json')) return JSON.parse(this.body);
else return this.body.toString(); else return this.body.toString();
} }
buffer () {
return this.body;
}
text () { text () { return this.body.toString(); }
return this.body.toString(); json () { return JSON.parse(this.body); }
} buffer () { return this.body; }
json () {
return JSON.parse(this.body);
}
}; };

View File

@@ -1,6 +1,6 @@
{ {
"name": "wumpfetch", "name": "wumpfetch",
"version": "0.2.12", "version": "0.2.13",
"description": "🚀 A lightweight and fast Node.JS HTTP Client", "description": "🚀 A lightweight and fast Node.JS HTTP Client",
"author": "Wessel \"wesselgame\" T <discord@go2it.eu>", "author": "Wessel \"wesselgame\" T <discord@go2it.eu>",
"license": "MIT", "license": "MIT",
@@ -33,8 +33,6 @@
"lightweight" "lightweight"
], ],
"devDependencies": { "devDependencies": {
"gulp": "^4.0.0", "eslint": "^5.16.0"
"gulp-concat": "^2.6.1",
"gulp-minify": "^3.1.0"
} }
} }