Skip to content
This repository has been archived by the owner on Sep 6, 2018. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed May 9, 2018
1 parent f22d100 commit 8fb6c5c
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 61 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ module.exports = {
],
rules: {
'strict': ['error', 'global'],
'curly': ['error', 'multi-or-nest', 'consistent'],
'no-bitwise': 'off',
'no-iterator': 'off',
'global-require': 'off',
'quote-props': ['error', 'consistent-as-needed'],
'brace-style': ['error', '1tbs', { allowSingleLine: false }],
'curly': ['error', 'all'],
'no-param-reassign': 'off',
'arrow-parens': ['error', 'always'],
'no-multi-assign': 'off',
'no-underscore-dangle': 'off',
'no-restricted-syntax': 'off',
'object-curly-newline': 'off',
'prefer-const': ['error', { destructuring: 'all' }],
'class-methods-use-this': 'off',
'import/no-dynamic-require': 'off',
'import/no-extraneous-dependencies': ['error', {
devDependencies: true,
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "node ./node_modules/.bin/jest",
"coverage": "cat ./coverage/lcov.info | coveralls",
"docs": "node docs.js",
"build:browser": "webpack",
"build:browser": "webpack-cli",
"prepublishOnly": "npm run lint && npm run test && npm run build:browser"
},
"repository": {
Expand All @@ -28,22 +28,22 @@
"dependencies": {},
"devDependencies": {
"@snek/syncify": "0.0.6",
"babel-eslint": "^8.2.2",
"coveralls": "^3.0.0",
"docma": "^1.5.1",
"babel-eslint": "^8.2.3",
"coveralls": "^3.0.1",
"docma": "^2.1.0",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.10.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"form-data": "^2.3.2",
"install-peerdeps": "^1.6.0",
"jest": "^21.2.1",
"jest": "^22.4.3",
"jsdoc-dynamic": "^1.0.4",
"json-filter-loader": "^1.0.0",
"node-fetch": "^2.1.2",
"uglifyjs-webpack-plugin": "^1.2.4",
"webpack": "^3.8.1"
"webpack": "^4.8.1",
"webpack-cli": "^2.1.3"
},
"description": "Just do http requests without all that weird nastiness from other libs",
"browser": {
Expand Down
6 changes: 4 additions & 2 deletions src/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ function request(snek) {
return window.fetch(snek.options.url, snek.options)
.then((r) => r[type]().then((raw) => {
const headers = {};
for (const [k, v] of r.headers.entries())
for (const [k, v] of r.headers.entries()) {
headers[k.toLowerCase()] = v;
}
return {
raw,
headers,
Expand All @@ -28,8 +29,9 @@ module.exports = {
querystring: {
parse: (str) => {
const parsed = {};
for (const [k, v] of new window.URLSearchParams(str).entries())
for (const [k, v] of new window.URLSearchParams(str).entries()) {
parsed[k] = v;
}
return parsed;
},
stringify: (obj) => new window.URLSearchParams(obj).toString(),
Expand Down
46 changes: 30 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ class Snekfetch extends transport.Parent {
query: undefined,
data: undefined,
});
if (opts.headers)
if (opts.headers) {
this.set(opts.headers);
if (opts.query)
}
if (opts.query) {
this.query(opts.query);
if (opts.data)
}
if (opts.data) {
this.send(opts.data);
}
}

/**
Expand All @@ -56,12 +59,14 @@ class Snekfetch extends transport.Parent {
* @returns {Snekfetch} This request
*/
query(name, value) {
if (this.options.query === undefined)
if (this.options.query === undefined) {
this.options.query = {};
if (typeof name === 'object')
}
if (typeof name === 'object') {
Object.assign(this.options.query, name);
else
} else {
this.options.query[name] = value;
}

return this;
}
Expand All @@ -74,8 +79,9 @@ class Snekfetch extends transport.Parent {
*/
set(name, value) {
if (typeof name === 'object') {
for (const [k, v] of Object.entries(name))
for (const [k, v] of Object.entries(name)) {
this.options.headers[k.toLowerCase()] = v;
}
} else {
this.options.headers[name.toLowerCase()] = value;
}
Expand All @@ -94,8 +100,9 @@ class Snekfetch extends transport.Parent {
const form = this.options.data instanceof transport.FormData ?
this.options.data : this.options.data = new transport.FormData();
if (typeof args[0] === 'object') {
for (const [k, v] of Object.entries(args[0]))
for (const [k, v] of Object.entries(args[0])) {
this.attach(k, v);
}
} else {
form.append(...args);
}
Expand All @@ -115,10 +122,11 @@ class Snekfetch extends transport.Parent {
const header = this.options.headers['content-type'];
let serialize;
if (header) {
if (header.includes('application/json'))
if (header.includes('application/json')) {
serialize = JSON.stringify;
else if (header.includes('urlencoded'))
} else if (header.includes('urlencoded')) {
serialize = this.options.qs.stringify;
}
} else {
this.set('Content-Type', 'application/json');
serialize = JSON.stringify;
Expand All @@ -131,8 +139,9 @@ class Snekfetch extends transport.Parent {
}

then(resolver, rejector) {
if (this._response)
if (this._response) {
return this._response.then(resolver, rejector);
}
this._finalizeRequest();
// eslint-disable-next-line no-return-assign
return this._response = transport.request(this)
Expand All @@ -155,8 +164,9 @@ class Snekfetch extends transport.Parent {
get body() {
delete res.body;
const type = res.headers['content-type'];
if (raw instanceof ArrayBuffer)
if (raw instanceof ArrayBuffer) {
raw = new window.TextDecoder('utf8').decode(raw); // eslint-disable-line no-undef
}
if (/application\/json/.test(type)) {
try {
res.body = JSON.parse(raw);
Expand All @@ -177,8 +187,9 @@ class Snekfetch extends transport.Parent {
statusText,
};

if (res.ok)
if (res.ok) {
return res;
}
const err = new Error(`${statusCode} ${statusText}`.trim());
Object.assign(err, res);
return Promise.reject(err);
Expand All @@ -203,10 +214,12 @@ class Snekfetch extends transport.Parent {
}

_finalizeRequest() {
if (this.options.method !== 'HEAD')
if (this.options.method !== 'HEAD') {
this.set('Accept-Encoding', 'gzip, deflate');
if (this.options.data && this.options.data.getBoundary)
}
if (this.options.data && this.options.data.getBoundary) {
this.set('Content-Type', `multipart/form-data; boundary=${this.options.data.getBoundary()}`);
}

if (this.options.query) {
const [url, query] = this.options.url.split('?');
Expand All @@ -216,8 +229,9 @@ class Snekfetch extends transport.Parent {

_read() {
this.resume();
if (this._response)
if (this._response) {
return;
}
this.catch((err) => this.emit('error', err));
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/node/FormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ class FormData {
}

append(name, data, filename) {
if (typeof data === 'undefined')
if (typeof data === 'undefined') {
return;
}
let str = `\r\n--${this.boundary}\r\nContent-Disposition: form-data; name="${name}"`;
let mimetype = null;
if (filename) {
str += `; filename="${filename}"`;
mimetype = 'application/octet-stream';
const extname = path.extname(filename).slice(1);
if (extname)
if (extname) {
mimetype = mime.lookup(extname);
}
}

if (data instanceof Buffer) {
Expand All @@ -31,8 +33,9 @@ class FormData {
data = Buffer.from(String(data));
}

if (mimetype)
if (mimetype) {
str += `\r\nContent-Type: ${mimetype}`;
}
this.buffers.push(Buffer.from(`${str}\r\n\r\n`));
this.buffers.push(data);
}
Expand Down
36 changes: 23 additions & 13 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ const FormData = require('./FormData');

function shouldUnzip(statusCode, headers) {
/* istanbul ignore next */
if (statusCode === 204 || statusCode === 304)
if (statusCode === 204 || statusCode === 304) {
return false;
if (+headers['content-length'] === 0)
}
if (+headers['content-length'] === 0) {
return false;
}
return /^\s*(?:deflate|gzip)\s*$/.test(headers['content-encoding']);
}

function request(snek, options = snek.options) {
return new Promise(async (resolve, reject) => {
Object.assign(options, UrlParse(options.url));

if (!options.headers['user-agent'])
if (!options.headers['user-agent']) {
options.headers['user-agent'] = `snekfetch/${Package.version} (${Package.homepage})`;
}

let { data } = options;
if (data && data.end)
if (data && data.end) {
data = data.end();
}

if (!options.headers['content-length']) {
let length = 0;
Expand All @@ -49,10 +53,12 @@ function request(snek, options = snek.options) {
} else {
const O = await socket(options);
({ req } = O);
if (O.http2)
if (O.http2) {
http2 = true;
if (O.connection)
}
if (O.connection) {
options.connection = O.connection;
}
}
} catch (err) {
reject(err);
Expand All @@ -71,10 +77,11 @@ function request(snek, options = snek.options) {
resolve(request(snek, Object.assign({}, options, {
url: UrlResolve(options.url, headers.location),
})));
if (req.abort)
if (req.abort) {
req.abort();
else if (req.close)
} else if (req.close) {
req.close();
}
return;
}

Expand All @@ -87,8 +94,9 @@ function request(snek, options = snek.options) {

stream.on('data', (chunk) => {
/* istanbul ignore next */
if (!snek.push(chunk))
if (!snek.push(chunk)) {
snek.pause();
}
body.push(chunk);
});

Expand All @@ -97,8 +105,9 @@ function request(snek, options = snek.options) {
stream.once('end', () => {
snek.push(null);
const raw = Buffer.concat(body);
if (options.connection && options.connection.close)
if (options.connection && options.connection.close) {
options.connection.close();
}
resolve({
raw, headers, statusCode, statusText,
});
Expand All @@ -119,12 +128,13 @@ function request(snek, options = snek.options) {
});

/* istanbul ignore next */
if (data instanceof Stream)
if (data instanceof Stream) {
data.pipe(req);
else if (data instanceof Buffer)
} else if (data instanceof Buffer) {
req.write(data);
else if (data)
} else if (data) {
req.write(data);
}
req.end();
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/node/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ function connectHttps(opt) {
break;
}
case 'h2': {
if (http2 === undefined)
if (http2 === undefined) {
http2 = require('http2');
}

const connection = http2.connect({
host: opt.host,
Expand Down

0 comments on commit 8fb6c5c

Please sign in to comment.