mirror of
https://github.com/Wessel/wumpfetch.git
synced 2026-07-17 13:53:56 +02:00
Many fixes!
This commit is contained in:
125
dist/wumpfetch.concat.js
vendored
125
dist/wumpfetch.concat.js
vendored
@@ -1,17 +1,17 @@
|
||||
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const { URL } = require('url');
|
||||
const { join } = require('path');
|
||||
const { stringify } = require('querystring');
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const { URL } = require('url');
|
||||
const { join } = require('path');
|
||||
const { stringify } = require('querystring');
|
||||
const { createGunzip, createInflate } = require('zlib');
|
||||
|
||||
const c = [ 'gzip', 'deflate' ];
|
||||
const common = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
|
||||
const { version } = require('../package.json');
|
||||
|
||||
const WumpResponse = class WumpResponse {
|
||||
constructor (res) {
|
||||
this.body = Buffer.alloc(0);
|
||||
this.body = Buffer.alloc(0);
|
||||
this.coreRes = res;
|
||||
|
||||
this.headers = res.headers;
|
||||
@@ -30,28 +30,33 @@ const WumpResponse = class WumpResponse {
|
||||
return JSON.parse(this.body);
|
||||
}
|
||||
};
|
||||
const WumpRequest = class WumpRequest {
|
||||
constructor (url = {}, m = {}) {
|
||||
const o = typeof url === 'string' ? m : url;
|
||||
const obj = typeof o === 'object';
|
||||
|
||||
if (typeof url !== 'string' && !o.hasOwnProperty('url')) throw new Error('Missing url parameter');
|
||||
const WumpRequest = class WumpRequest {
|
||||
constructor (url = {}, method = {}) {
|
||||
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');
|
||||
}
|
||||
|
||||
this.o = {
|
||||
'm' : typeof o === 'string' ? o : obj && o.method ? o.method : 'GET',
|
||||
'url' : typeof url === 'string' ? new URL( url ) : obj && typeof o.url === 'string' ? new URL( o.url ) : url,
|
||||
'SDA' : obj && typeof o.sendDataAs === 'string' ? o.sendDataAs : undefined,
|
||||
'data' : obj && o.data ? o.data : obj && o.form ? stringify( o.form ) : undefined,
|
||||
'parse' : obj && o.parse ? o.parse : undefined,
|
||||
'follow' : !!( obj && o.followRedirects ),
|
||||
'rHeaders' : obj && typeof o.headers === 'object' ? o.headers : {},
|
||||
'streamed' : !!( obj && o.streamed ),
|
||||
'compressed' : !!( obj && o.compressed ),
|
||||
'm': typeof o === 'string' ? o : obj && o.method ? o.method : 'GET',
|
||||
'url': typeof url === 'string' ? new URL(url) : obj && 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,
|
||||
'data': obj && o.data ? o.data : obj && o.json ? o.json : obj && o.form ? stringify(o.form) : undefined,
|
||||
'parse': obj && o.parse ? o.parse : undefined,
|
||||
'follow': !!(obj && o.followRedirects),
|
||||
'rHeaders': obj && typeof o.headers === 'object' ? o.headers : {},
|
||||
'streamed': !!(obj && o.streamed),
|
||||
'compressed': !!(obj && o.compressed),
|
||||
'timeoutTime': obj && typeof o.timeout === 'number' ? o.timeout : null,
|
||||
'coreOptions': obj && 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') {
|
||||
Object.keys(o.core).forEach((v) => this.option(v, o.core[v]));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -64,13 +69,13 @@ const WumpRequest = class WumpRequest {
|
||||
}
|
||||
|
||||
body (data, SA) {
|
||||
this.o.SDA = typeof data === 'object' && !SA && !Buffer.isBuffer(data) ? 'json' : (SA ? SA.toLowerCase() : 'buffer');
|
||||
this.o.SDA = typeof data === 'object' && !SA && !Buffer.isBuffer(data) ? 'json' : (SA ? SA.toLowerCase() : 'buffer');
|
||||
this.o.data = this.SDA === 'form' ? stringify(data) : (this.SDA === 'json' ? JSON.stringify(data) : data);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
header ( a, b ) {
|
||||
header (a, b) {
|
||||
if (typeof a === 'object') Object.keys(a).forEach((v) => this.o.rHeaders[v.toLowerCase()] = a[v]);
|
||||
else this.o.rHeaders[a.toLowerCase()] = b;
|
||||
|
||||
@@ -79,34 +84,40 @@ const WumpRequest = class WumpRequest {
|
||||
|
||||
compress () {
|
||||
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'] = c.join(', ');
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
path (p) {
|
||||
this.o.url.pathname = join(this.o.url.pathname, p);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
stream () {
|
||||
this.o.streamed = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
option (n, v) {
|
||||
this.o.coreOptions[n] = v;
|
||||
|
||||
return this;
|
||||
}
|
||||
timeout (timeout) {
|
||||
this.o.timeoutTime = timeout;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
send () {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.o.data) {
|
||||
if (!this.o.rHeaders.hasOwnProperty('user-agent')) this.o.rHeaders['User-Agent'] = 'wumpfetch/0.0.1 (https://github.com/PassTheWessel/wumpfetch)';
|
||||
if (!this.o.rHeaders.hasOwnProperty('user-agent')) {
|
||||
this.o.rHeaders['User-Agent'] = w.userAgent;
|
||||
}
|
||||
|
||||
if (this.o.SDA === 'json' && !this.o.rHeaders.hasOwnProperty('content-type')) this.o.rHeaders['Content-Type'] = 'application/json';
|
||||
if (this.o.SDA === 'form') {
|
||||
@@ -116,32 +127,33 @@ const WumpRequest = class WumpRequest {
|
||||
}
|
||||
|
||||
let req;
|
||||
const options = Object.assign({
|
||||
'host' : this.o.url.hostname,
|
||||
'port' : this.o.url.port,
|
||||
'path' : this.o.url.pathname + this.o.url.search,
|
||||
'method' : this.o.m,
|
||||
'headers' : this.o.rHeaders,
|
||||
'protocol': this.o.url.protocol
|
||||
}, this.o.coreOptions);
|
||||
const options = {
|
||||
'host': this.o.url.hostname,
|
||||
'port': this.o.url.port,
|
||||
'path': this.o.url.pathname + this.o.url.search,
|
||||
'method': this.o.m,
|
||||
'headers': this.o.rHeaders,
|
||||
'protocol': this.o.url.protocol,
|
||||
...this.o.coreOptions
|
||||
}
|
||||
|
||||
const resHandler = async (res) => {
|
||||
const handler = async (res) => {
|
||||
let stream = res;
|
||||
|
||||
if (this.o.compressed) {
|
||||
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;
|
||||
|
||||
if ( this.o.streamed ) resolve(stream);
|
||||
if (this.o.streamed) resolve(stream);
|
||||
else {
|
||||
wumpRes = new WumpResponse(res);
|
||||
|
||||
if (res.headers.hasOwnProperty('location') && this.o.follow) {
|
||||
this.o.url = (new URL(res.headers['location'], this.o.url)).toString();
|
||||
return await w(this.o);
|
||||
return await module.exports(this.o);
|
||||
}
|
||||
|
||||
stream.on('error', (e) => reject(e));
|
||||
@@ -158,8 +170,8 @@ const WumpRequest = class WumpRequest {
|
||||
}
|
||||
};
|
||||
|
||||
if (this.o.url.protocol === 'http:') req = http.request(options, resHandler);
|
||||
else if (this.o.url.protocol === 'https:') req = https.request(options, resHandler);
|
||||
if (this.o.url.protocol === 'http:') req = http.request(options, handler);
|
||||
else if (this.o.url.protocol === 'https:') req = https.request(options, handler);
|
||||
else throw new Error(`Bad URL protocol: ${this.o.url.protocol}`);
|
||||
|
||||
|
||||
@@ -171,22 +183,37 @@ const WumpRequest = class WumpRequest {
|
||||
}
|
||||
|
||||
req.on('error', (e) => reject(e));
|
||||
if (this.o.data) req.write(this.o.SDA === 'json' ? JSON.stringify(this.o.data) : this.o.data);
|
||||
|
||||
if (this.o.data) {
|
||||
if (this.o.SDA === 'json') {
|
||||
req.write(JSON.stringify(this.o.data));
|
||||
} else {
|
||||
if (typeof this.o.data === 'object') {
|
||||
req.write(JSON.stringify(this.o.data));
|
||||
} else {
|
||||
req.write(this.o.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function w(url, method) {
|
||||
module.exports = (url, method) => {
|
||||
return new WumpRequest(url, method);
|
||||
}
|
||||
|
||||
module.exports = w;
|
||||
};
|
||||
|
||||
common.forEach((v) => {
|
||||
module.exports[v.toLowerCase()] = (url, method = v) => {
|
||||
return new request(url, Object.assign({ method: v }, method));
|
||||
module.exports[v.toLowerCase()] = (url, method) => {
|
||||
if (typeof url === 'string') {
|
||||
return new WumpRequest(url, { method: v, ...method });
|
||||
} else {
|
||||
return new WumpRequest({ method: v, ...url }, method);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
module.exports.version = version;
|
||||
module.exports.userAgent = `wumpfetch/${version} (https://github.com/PassTheWessel/wumpfetch)`;
|
||||
|
||||
Reference in New Issue
Block a user