Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove calls to deprecated url.parse #1360

Merged
merged 4 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmarks/FB/hello.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ function sequelizeQuery(callback) {
}

function handlePrepared(req, res) {
const values = url.parse(req.url, true);
const queries = values.query.queries || 1;
const values = new url.URL(req.url);
const queries = values.searchParams.get('queries') || 1;
const results = [];
for (let i = 0; i < queries; ++i) {
mysql2conn.execute(
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/http-select-and-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const port = process.env.PORT;

http
.createServer((req, res) => {
const q = url.parse(req.url, true);
const q = new url.URL(req.url);
if (q.pathname === '/render') {
const sql = q.query.q;
const n = q.query.n;
const sql = q.searchParams.get('q');
const n = q.searchParams.get('n');
let rowsTotal = [];
const doQueries = function(number) {
if (number === 0) {
Expand Down
36 changes: 15 additions & 21 deletions lib/connection_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const urlParse = require('url').parse;
const { URL } = require('url');
const ClientConstants = require('./constants/client');
const Charsets = require('./constants/charsets');
let SSLProfiles = null;
Expand Down Expand Up @@ -232,29 +232,23 @@ class ConnectionConfig {
}

static parseUrl(url) {
url = urlParse(url, true);
const parsedUrl = new URL(url);
const options = {
host: url.hostname,
port: url.port,
database: url.pathname.substr(1)
host: parsedUrl.hostname,
port: parsedUrl.port,
database: parsedUrl.pathname.substr(1),
user: parsedUrl.username,
password: parsedUrl.password
};
if (url.auth) {
const auth = url.auth.split(':');
options.user = auth[0];
options.password = auth[1];
}
if (url.query) {
for (const key in url.query) {
const value = url.query[key];
try {
// Try to parse this as a JSON expression first
options[key] = JSON.parse(value);
} catch (err) {
// Otherwise assume it is a plain string
options[key] = value;
}
parsedUrl.searchParams.forEach((value, key) => {
try {
// Try to parse this as a JSON expression first
options[key] = JSON.parse(value);
} catch (err) {
// Otherwise assume it is a plain string
options[key] = value;
}
}
});
return options;
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/unit/connection/test-connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ assert.doesNotThrow(() => {
flags: ['-FOUND_ROWS']
});
}, 'Error, the constructor threw an exception due to a flags array');

assert.strictEqual(
ConnectionConfig.parseUrl(
String.raw`fml://test:pass!@$%^&*()\word:@www.example.com/database`
).password,
'pass!%40$%%5E&*()%5Cword%3A'
);