Skip to content

Commit 53b7d8f

Browse files
committed
Replaced Url.parse() with new URL().
1 parent ebe70db commit 53b7d8f

5 files changed

Lines changed: 38 additions & 35 deletions

File tree

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- always set `cwd` directory for the current executed script
2727
- added `timeout {Number}` property into the `NEWACTION` method
2828
- extended `NEWAPI(type, [config], config)` by adding the `config` argument
29+
- replaced `Url.parse()` with `new URL()`
2930

3031
========================
3132
0.0.15

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,7 @@ process.on('message', function(msg, h) {
29422942
F.Tls = F.require('node:tls');
29432943
F.Stream = F.require('node:stream');
29442944
F.Cluster = require('node:cluster');
2945+
F.Util = F.require('node:util');
29452946

29462947
// Total.js modules
29472948
F.TUtils = require('./utils');

routing.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ function Proxy(url, target) {
804804
if (typeof(target) === 'function')
805805
t.stream = target;
806806
else if ((/^(https|http):\/\//).test(target))
807-
t.target = F.Url.parse(target);
807+
t.target = new URL(target);
808808
else
809809
t.target = { socketPath: target };
810810

@@ -887,20 +887,20 @@ exports.proxy = function(url, target) {
887887
url = F.virtualpath(url);
888888

889889
if (!target) {
890-
let index = F.routes.proxies.TfindIndex('url', url.toLowerCase());
890+
const index = F.routes.proxies.TfindIndex('url', url.toLowerCase());
891891
if (index !== -1)
892892
F.routes.proxies.splice(index, 1);
893893
return;
894894
}
895895

896-
let proxy = new Proxy(url, target);
896+
const proxy = new Proxy(url, target);
897897
F.routes.proxies.push(proxy);
898898
return proxy;
899899
};
900900

901901
exports.lookupproxy = function(ctrl) {
902-
for (var proxy of F.routes.proxies) {
903-
var u = ctrl.uri.key.substring(0, proxy.url.length);
902+
for (let proxy of F.routes.proxies) {
903+
let u = ctrl.uri.key.substring(0, proxy.url.length);
904904
if (u[u.length - 1] !== '/')
905905
u += '/';
906906
if (u === proxy.url && (!proxy.$check || proxy.$check(ctrl))) {
@@ -913,10 +913,10 @@ exports.lookupproxy = function(ctrl) {
913913

914914
function proxyheadersws(header, headers) {
915915

916-
var output = [];
916+
const output = [];
917917

918918
for (let key in headers) {
919-
var value = headers[key];
919+
const value = headers[key];
920920
if (value instanceof Array) {
921921
for (let item of value)
922922
output.push(key + ': ' + item);

utils.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -491,16 +491,16 @@ exports.toURLEncode = function(value) {
491491

492492
exports.resolve = function(url, callback, param) {
493493

494-
var uri;
494+
let uri;
495495

496496
try {
497-
uri = F.Url.parse(url);
497+
uri = new URL(url);
498498
} catch (e) {
499499
callback(e);
500500
return;
501501
}
502502

503-
var cache = F.temporary.dnscache[uri.host];
503+
const cache = F.temporary.dnscache[uri.host];
504504

505505
if (!callback)
506506
return cache;
@@ -538,18 +538,18 @@ function keywordscleaner(c) {
538538

539539
function parseProxy(p) {
540540

541-
var key = 'proxy_' + p;
541+
const key = 'proxy_' + p;
542542

543543
if (F.temporary.utils[key])
544544
return F.temporary.utils[key];
545545

546546
if (p.indexOf('://') === -1)
547547
p = 'http://' + p;
548548

549-
var obj = F.Url.parse(p);
549+
const obj = new URL(p);
550550

551-
if (obj.auth)
552-
obj._auth = 'Basic ' + Buffer.from(obj.auth).toString('base64');
551+
if (obj.username || obj.password)
552+
obj._auth = 'Basic ' + Buffer.from(obj.username + ':' + obj.password).toString('base64');
553553

554554
obj.port = +obj.port;
555555

@@ -563,7 +563,7 @@ function parseProxy(p) {
563563

564564
function _request(opt, callback) {
565565

566-
var options = { length: 0, timeout: opt.timeout == false || opt.timeout == 0 ? 0 : (opt.timeout || 8000), encoding: opt.encoding || 'utf8', callback: callback || opt.callback || NOOP, post: true, redirect: 0 };
566+
const options = { length: 0, timeout: opt.timeout == false || opt.timeout == 0 ? 0 : (opt.timeout || 8000), encoding: opt.encoding || 'utf8', callback: callback || opt.callback || NOOP, post: true, redirect: 0 };
567567
var proxy;
568568

569569
F.stats.performance.external++;
@@ -691,7 +691,7 @@ function _request(opt, callback) {
691691
}
692692
}
693693

694-
var uri = opt.unixsocket ? { socketPath: opt.unixsocket.socket, path: opt.unixsocket.path } : F.Url.parse(opt.url);
694+
const uri = opt.unixsocket ? { socketPath: opt.unixsocket.socket, path: opt.unixsocket.path } : new URL(opt.url);
695695

696696
if ((opt.unixsocket && !uri.socketPath) || (!opt.unixsocket && (!uri.hostname || !uri.host))) {
697697
options.response.canceled = true;
@@ -794,9 +794,9 @@ PAP.createConnection = function(pending) {
794794

795795
PAP.createSocket = function(options, callback) {
796796

797-
var self = this;
798-
var proxy = self.options.proxy;
799-
var uri = self.options.uri;
797+
const self = this;
798+
const proxy = self.options.proxy;
799+
const uri = self.options.uri;
800800

801801
PROXYOPTIONS.host = proxy.hostname;
802802
PROXYOPTIONS.port = proxy.port;
@@ -805,7 +805,7 @@ PAP.createSocket = function(options, callback) {
805805
if (proxy._auth)
806806
PROXYOPTIONS.headers['Proxy-Authorization'] = proxy._auth;
807807

808-
var req = self.request(PROXYOPTIONS);
808+
const req = self.request(PROXYOPTIONS);
809809
req.setTimeout(10000);
810810
req.on('response', proxyagent_response);
811811
req.on('connect', function(res, socket) {
@@ -825,7 +825,7 @@ PAP.createSocket = function(options, callback) {
825825
});
826826

827827
req.on('error', function(err) {
828-
var e = new Error('Request Proxy "proxy {0} --> target {1}": {2}'.format(PROXYOPTIONS.host + ':' + proxy.port, PROXYOPTIONS.path, err.toString()));
828+
const e = new Error('Request Proxy "proxy {0} --> target {1}": {2}'.format(PROXYOPTIONS.host + ':' + proxy.port, PROXYOPTIONS.path, err.toString()));
829829
e.code = err.code;
830830
req.destroy && req.destroy();
831831
req = null;
@@ -1030,8 +1030,8 @@ function request_writefile(req, options, file, next) {
10301030

10311031
function request_response(res) {
10321032

1033-
var options = this.$options;
1034-
var uri = this.$uri;
1033+
const options = this.$options;
1034+
const uri = this.$uri;
10351035

10361036
res._buffer = null;
10371037
res._bufferlength = 0;
@@ -1093,18 +1093,18 @@ function request_response(res) {
10931093

10941094
options.redirect++;
10951095

1096-
var loc = res.headers.location;
1097-
var proto = loc.substring(0, 6);
1096+
let loc = res.headers.location;
1097+
const proto = loc.substring(0, 6);
10981098

10991099
if (proto !== 'http:/' && proto !== 'https:')
11001100
loc = uri.protocol + '//' + uri.hostname + (uri.port && !SKI_PPORTS[uri.port] ? (':' + uri.port) : '') + loc;
11011101

1102-
var tmp = F.Url.parse(loc);
1102+
var tmp = new URL(loc);
11031103
tmp.headers = uri.headers;
11041104

11051105
// Transfers cookies
11061106
if (!options.nocookies) {
1107-
var cookies = res.headers['set-cookie'];
1107+
const cookies = res.headers['set-cookie'];
11081108
if (cookies) {
11091109

11101110
if (options.$totalinit.cook && !options.$totalinit.cookies)
@@ -1113,9 +1113,9 @@ function request_response(res) {
11131113
if (!options.cookies)
11141114
options.cookies = {};
11151115

1116-
for (var i = 0; i < cookies.length; i++) {
1117-
var cookie = cookies[i];
1118-
var index = cookie.indexOf(';');
1116+
for (let i = 0; i < cookies.length; i++) {
1117+
let cookie = cookies[i];
1118+
let index = cookie.indexOf(';');
11191119
if (index !== -1){
11201120
cookie = cookie.substring(0, index);
11211121
index = cookie.indexOf('=');

websocket.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ WebSocketClient.prototype.connect = function(url, protocol, origin) {
12251225

12261226
WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
12271227

1228-
var options = {};
1228+
const options = {};
12291229

12301230
self.url = url;
12311231
self.origin = origin;
@@ -1238,10 +1238,10 @@ WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
12381238
var secured = false;
12391239

12401240
if (typeof(url) === 'string') {
1241-
url = F.Url.parse(url);
1241+
url = new URL(url);
12421242
options.host = url.hostname;
1243-
options.path = url.path;
1244-
options.query = url.query;
1243+
options.path = url.pathname + url.search;
1244+
options.query = url.search.substring(1);
12451245
secured = url.protocol === 'wss:';
12461246
options.port = url.port || (secured ? 443 : 80);
12471247
} else {
@@ -1284,7 +1284,8 @@ WebSocketClient.prototype.connectforce = function(self, url, protocol, origin) {
12841284
for (let key in self.headers)
12851285
options.headers[key] = self.headers[key];
12861286

1287-
var tmp = [];
1287+
const tmp = [];
1288+
12881289
for (let key in self.cookies)
12891290
tmp.push(key + '=' + self.cookies[key]);
12901291

0 commit comments

Comments
 (0)