Skip to content

Commit

Permalink
chore: bump deps, xo linting
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Aug 15, 2023
1 parent 2fd4292 commit 8b5400b
Show file tree
Hide file tree
Showing 54 changed files with 304 additions and 250 deletions.
4 changes: 2 additions & 2 deletions ci/remove-deps-4-old-node.js
@@ -1,5 +1,5 @@
const fs = require('fs');
const path = require('path');
const fs = require('node:fs');
const path = require('node:path');
const package = require('../package.json');

const UNSUPPORT_DEPS_4_OLD = new Set([
Expand Down
24 changes: 12 additions & 12 deletions package.json
Expand Up @@ -27,27 +27,27 @@
"formidable": "^2.1.2",
"methods": "^1.1.2",
"mime": "2.6.0",
"qs": "^6.11.0",
"semver": "^7.3.8"
"qs": "^6.11.2",
"semver": "^7.5.4"
},
"devDependencies": {
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/plugin-transform-runtime": "^7.19.6",
"@babel/preset-env": "^7.20.2",
"@babel/runtime": "^7.20.13",
"@babel/cli": "^7.22.10",
"@babel/core": "^7.22.10",
"@babel/plugin-transform-runtime": "^7.22.10",
"@babel/preset-env": "^7.22.10",
"@babel/runtime": "^7.22.10",
"@commitlint/cli": "17",
"@commitlint/config-conventional": "17",
"Base64": "^1.1.0",
"Base64": "^1.2.0",
"babelify": "^10.0.0",
"basic-auth-connect": "^1.0.0",
"body-parser": "^1.20.1",
"body-parser": "^1.20.2",
"browserify": "^17.0.0",
"cookie-parser": "^1.4.6",
"cross-env": "^7.0.3",
"eslint": "^8.32.0",
"eslint": "^8.47.0",
"eslint-config-xo-lass": "2",
"eslint-plugin-compat": "4.0.2",
"eslint-plugin-compat": "4.1.4",
"eslint-plugin-node": "^11.1.0",
"express": "^4.18.2",
"express-session": "^1.17.3",
Expand All @@ -65,7 +65,7 @@
"should": "^13.2.3",
"should-http": "^0.1.1",
"tinyify": "3.0.0",
"xo": "^0.53.1",
"xo": "^0.56.0",
"zuul": "^3.12.0"
},
"engines": {
Expand Down
10 changes: 6 additions & 4 deletions src/client.js
Expand Up @@ -75,7 +75,9 @@ request.getXHR = () => {
* @api private
*/

const trim = ''.trim ? (s) => s.trim() : (s) => s.replace(/(^\s*|\s*$)/g, '');
const trim = ''.trim
? (s) => s.trim()
: (s) => s.replaceAll(/(^\s*|\s*$)/g, '');

/**
* Serialize the given `obj`.
Expand Down Expand Up @@ -310,7 +312,7 @@ function Response(request_) {
this.text =
(this.req.method !== 'HEAD' &&
(this.xhr.responseType === '' || this.xhr.responseType === 'text')) ||
typeof this.xhr.responseType === 'undefined'
this.xhr.responseType === undefined
? this.xhr.responseText
: null;
this.statusText = this.req.xhr.statusText;
Expand Down Expand Up @@ -423,7 +425,7 @@ function Request(method, url) {
if (self.xhr) {
// ie9 doesn't have 'response' property
error.rawResponse =
typeof self.xhr.responseType === 'undefined'
self.xhr.responseType === undefined
? self.xhr.responseText
: self.xhr.response;
// issue #876: return the http status code if the response parsing fails
Expand Down Expand Up @@ -867,7 +869,7 @@ Request.prototype._end = function () {

// IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
// We need null here if data is undefined
xhr.send(typeof data === 'undefined' ? null : data);
xhr.send(data === undefined ? null : data);
};

request.agent = () => new Agent();
Expand Down
4 changes: 2 additions & 2 deletions src/node/agent.js
Expand Up @@ -3,7 +3,7 @@
*/

// eslint-disable-next-line node/no-deprecated-api
const { parse } = require('url');
const { parse } = require('node:url');
const { CookieJar } = require('cookiejar');
const { CookieAccessInfo } = require('cookiejar');
const methods = require('methods');
Expand Down Expand Up @@ -66,7 +66,7 @@ Agent.prototype = Object.create(AgentBase.prototype);
Agent.prototype._saveCookies = function (res) {
const cookies = res.headers['set-cookie'];
if (cookies) {
const url = parse(res.request?.url || '')
const url = parse(res.request?.url || '');
this.jar.setCookies(cookies, url.hostname, url.pathname);
}
};
Expand Down
37 changes: 24 additions & 13 deletions src/node/http2wrapper.js
@@ -1,14 +1,14 @@
const Stream = require('stream');
const net = require('net');
const tls = require('tls');
const Stream = require('node:stream');
const net = require('node:net');
const tls = require('node:tls');
// eslint-disable-next-line node/no-deprecated-api
const { parse } = require('url');
const process = require('process');
const { parse } = require('node:url');
const process = require('node:process');
const semverGte = require('semver/functions/gte');

let http2;

if (semverGte(process.version, 'v10.10.0')) http2 = require('http2');
if (semverGte(process.version, 'v10.10.0')) http2 = require('node:http2');
else
throw new Error('superagent: this version of Node.js does not support http2');

Expand Down Expand Up @@ -70,15 +70,20 @@ class Request extends Stream {

createUnixConnection(authority, options) {
switch (this.protocol) {
case 'http:':
case 'http:': {
return net.connect(options.socketPath);
case 'https:':
}

case 'https:': {
options.ALPNProtocols = ['h2'];
options.servername = this.host;
options.allowHalfOpen = true;
return tls.connect(options.socketPath, options);
default:
}

default: {
throw new Error('Unsupported protocol', this.protocol);
}
}
}

Expand Down Expand Up @@ -129,11 +134,14 @@ class Request extends Stream {
let value = headers[key];
key = key.toLowerCase();
switch (key) {
case HTTP2_HEADER_SET_COOKIE:
case HTTP2_HEADER_SET_COOKIE: {
value = Array.isArray(value) ? value : [value];
break;
default:
}

default: {
break;
}
}

http2Headers[key] = value;
Expand All @@ -149,14 +157,17 @@ class Request extends Stream {
let value = headers[key];
key = key.toLowerCase();
switch (key) {
case HTTP2_HEADER_HOST:
case HTTP2_HEADER_HOST: {
key = HTTP2_HEADER_AUTHORITY;
value = /^http:\/\/|^https:\/\//.test(value)
? parse(value).host
: value;
break;
default:
}

default: {
break;
}
}

http2Headers[key] = value;
Expand Down
20 changes: 9 additions & 11 deletions src/node/index.js
Expand Up @@ -3,13 +3,13 @@
*/

// eslint-disable-next-line node/no-deprecated-api
const { parse, format, resolve } = require('url');
const Stream = require('stream');
const https = require('https');
const http = require('http');
const fs = require('fs');
const zlib = require('zlib');
const util = require('util');
const { parse, format, resolve } = require('node:url');
const Stream = require('node:stream');
const https = require('node:https');
const http = require('node:http');
const fs = require('node:fs');
const zlib = require('node:zlib');
const util = require('node:util');
const qs = require('qs');
const mime = require('mime');
let methods = require('methods');
Expand All @@ -19,7 +19,6 @@ const debug = require('debug')('superagent');
const CookieJar = require('cookiejar');
const semverGte = require('semver/functions/gte');
const safeStringify = require('fast-safe-stringify');

const utils = require('../utils');
const RequestBase = require('../request-base');
const { unzip } = require('./unzip');
Expand Down Expand Up @@ -469,7 +468,6 @@ Request.prototype._pipeContinue = function (stream, options) {
res.pipe(stream, options);
res.once('end', () => this.emit('end'));
}

});
return stream;
};
Expand Down Expand Up @@ -719,7 +717,7 @@ Request.prototype.request = function () {
// See https://github.com/ladjs/superagent/issues/1367
if (queryStringBackticks) {
let i = 0;
url.query = url.query.replace(/%60/g, () => queryStringBackticks[i++]);
url.query = url.query.replaceAll('%60', () => queryStringBackticks[i++]);
url.search = `?${url.query}`;
url.path = url.pathname + url.search;
}
Expand All @@ -731,7 +729,7 @@ Request.prototype.request = function () {

// get the socket, path
const unixParts = url.path.match(/^([^/]+)(.+)$/);
options.socketPath = unixParts[1].replace(/%2F/g, '/');
options.socketPath = unixParts[1].replaceAll('%2F', '/');
url.path = unixParts[2];
}

Expand Down
14 changes: 7 additions & 7 deletions src/node/response.js
Expand Up @@ -2,8 +2,8 @@
* Module dependencies.
*/

const util = require('util');
const Stream = require('stream');
const util = require('node:util');
const Stream = require('node:stream');
const ResponseBase = require('../response-base');
const { mixin } = require('../utils');

Expand Down Expand Up @@ -51,11 +51,11 @@ function Response(request) {
// https://github.com/nodejs/node/pull/39520#issuecomment-889697136
Object.defineProperty(Response.prototype, 'body', {
get() {
return this._body !== undefined
? this._body
: this.res.body !== undefined
? this.res.body
: {};
return this._body === undefined
? this.res.body === undefined
? {}
: this.res.body
: this._body;
},
set(value) {
this._body = value;
Expand Down
6 changes: 3 additions & 3 deletions src/node/unzip.js
Expand Up @@ -2,9 +2,9 @@
* Module dependencies.
*/

const { StringDecoder } = require('string_decoder');
const Stream = require('stream');
const zlib = require('zlib');
const { StringDecoder } = require('node:string_decoder');
const Stream = require('node:stream');
const zlib = require('node:zlib');

/**
* Buffers response data events and re-emits when they're unzipped.
Expand Down
38 changes: 26 additions & 12 deletions src/request-base.js
Expand Up @@ -112,17 +112,24 @@ RequestBase.prototype.timeout = function (options) {
for (const option in options) {
if (hasOwn(options, option)) {
switch (option) {
case 'deadline':
case 'deadline': {
this._timeout = options.deadline;
break;
case 'response':
}

case 'response': {
this._responseTimeout = options.response;
break;
case 'upload':
}

case 'upload': {
this._uploadTimeout = options.upload;
break;
default:
}

default: {
console.warn('Unknown timeout option', option);
}
}
}
}
Expand Down Expand Up @@ -513,20 +520,26 @@ RequestBase.prototype.abort = function () {

RequestBase.prototype._auth = function (user, pass, options, base64Encoder) {
switch (options.type) {
case 'basic':
case 'basic': {
this.set('Authorization', `Basic ${base64Encoder(`${user}:${pass}`)}`);
break;
}

case 'auto':
case 'auto': {
this.username = user;
this.password = pass;
break;
}

case 'bearer': // usage would be .auth(accessToken, { type: 'bearer' })
case 'bearer': {
// usage would be .auth(accessToken, { type: 'bearer' })
this.set('Authorization', `Bearer ${user}`);
break;
default:
}

default: {
break;
}
}

return this;
Expand Down Expand Up @@ -662,11 +675,12 @@ RequestBase.prototype.send = function (data) {
// merge
if (isObject_ && isObject(this._data)) {
for (const key in data) {
if (typeof data[key] == "bigint") throw new Error("Cannot serialize BigInt value to json");
if (typeof data[key] === 'bigint')
throw new Error('Cannot serialize BigInt value to json');
if (hasOwn(data, key)) this._data[key] = data[key];
}
}
else if (typeof data === 'bigint') throw new Error("Cannot send value of type BigInt");
} else if (typeof data === 'bigint')
throw new Error('Cannot send value of type BigInt');
else if (typeof data === 'string') {
// default to x-www-form-urlencoded
if (!type) this.type('form');
Expand Down Expand Up @@ -720,7 +734,7 @@ RequestBase.prototype.send = function (data) {

RequestBase.prototype.sortQuery = function (sort) {
// _sort default to true but otherwise can be a function or boolean
this._sort = typeof sort === 'undefined' ? true : sort;
this._sort = sort === undefined ? true : sort;
return this;
};

Expand Down
3 changes: 1 addition & 2 deletions test/agent-base.js
@@ -1,6 +1,5 @@
const assert = require('assert');
const assert = require('node:assert');
const getSetup = require('./support/setup');

const request = require('./support/client');

describe('Agent', () => {
Expand Down
3 changes: 1 addition & 2 deletions test/basic.js
@@ -1,6 +1,5 @@
const assert = require('assert');
const assert = require('node:assert');
const getSetup = require('./support/setup');

const request = require('./support/client');

describe('request', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/client/request.js
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('node:assert');
const request = require('../support/client');

describe('request', function () {
Expand Down

0 comments on commit 8b5400b

Please sign in to comment.