Skip to content

Commit

Permalink
lib/request: use whatwg url everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Dec 17, 2023
1 parent 2652b83 commit 5376bd7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 96 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ A minimal request module.
## Usage

``` js
const brq = require('brq');
const res = await brq('google.com');
console.log(res.text());
import request from 'brq';
const response = await request('https://icanhazip.com');
console.log(response.text());
```

## Contribution and License Agreement
Expand Down
43 changes: 26 additions & 17 deletions lib/request-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class RequestOptions {
}

fromOptions(options) {
if (options instanceof URL)
options = options.toString();

if (typeof options === 'string')
options = { url: options };

Expand Down Expand Up @@ -186,20 +189,27 @@ class RequestOptions {
}

navigate(url) {
assert(typeof url === 'string');
if (url instanceof URL)
url = url.toString();

if (url.indexOf('://') === -1)
url = 'http://' + url;
assert(typeof url === 'string');

this.url = new URL(url);

if (this.url.protocol !== 'http:' &&
this.url.protocol !== 'https:') {
throw new Error('Invalid URL protocol.');
}

if (this.url.port === '0')
throw new Error('Invalid URL port.');

this.username = this.url.username;
this.password = this.url.password;

this.url.username = '';
this.url.password = '';
this.url.hash = '';

return this;
}

isExpected(type) {
Expand Down Expand Up @@ -262,11 +272,9 @@ class RequestOptions {
for (const name of Object.keys(this.headers)) {
const value = String(this.headers[name]);

switch (name.toLowerCase()) {
case 'referer':
case 'referrer':
referrer = value;
continue;
if (name.toLowerCase() === 'referer') {
referrer = value;
continue;
}

headers.append(name, value);
Expand All @@ -280,6 +288,7 @@ class RequestOptions {
}

toHTTP() {
// https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters
// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#parameters
// https://developer.mozilla.org/en-US/docs/Web/API/Request/mode
// https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
Expand Down Expand Up @@ -534,19 +543,19 @@ function encodeSearch(data) {

function decodeSearch(text) {
const search = new URLSearchParams(text);
const body = Object.create(null);
const data = Object.create(null);

for (const [key, value] of search.entries()) {
if (Array.isArray(body[key])) {
body[key].push(value);
} else if (body[key] != null) {
body[key] = [body[key], value];
if (Array.isArray(data[key])) {
data[key].push(value);
} else if (data[key] != null) {
data[key] = [data[key], value];
} else {
body[key] = value;
data[key] = value;
}
}

return body;
return data;
}

function prepend(qs) {
Expand Down
127 changes: 51 additions & 76 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ class RequestOptions {
*/

constructor(options, buffer) {
ensureRequires();

this.method = 'GET';
this.ssl = false;
this.host = 'localhost';
this.port = 80;
this.path = '/';
this.query = '';
this.url = new URL('http://localhost');
this.strictSSL = true;
this.pool = false;
this.agent = null;
Expand All @@ -56,14 +54,14 @@ class RequestOptions {
this.headers = Object.create(null);
this.ca = null;

// Hack
ensureRequires();

if (options != null)
this.fromOptions(options);
}

fromOptions(options) {
if (options instanceof URL)
options = options.toString();

if (typeof options === 'string')
options = { url: options };

Expand All @@ -82,32 +80,37 @@ class RequestOptions {

if (options.ssl != null) {
assert(typeof options.ssl === 'boolean');
this.ssl = options.ssl;
this.port = 443;
if (options.ssl)
this.url.protocol = 'https:';
else
this.url.protocol = 'http:';
}

if (options.host != null) {
assert(typeof options.host === 'string');
this.host = options.host;
if (options.host.indexOf(':') !== -1)
this.url.hostname = `[${options.host}]`;
else
this.url.hostname = options.host;
}

if (options.port != null) {
assert((options.port & 0xffff) === options.port);
assert(options.port !== 0);
this.port = options.port;
this.url.port = String(options.port);
}

if (options.path != null) {
assert(typeof options.path === 'string');
this.path = options.path;
this.url.pathname = options.path;
}

if (options.query != null) {
if (typeof options.query === 'string') {
this.query = options.query;
this.url.search = prepend(options.query);
} else {
assert(typeof options.query === 'object');
this.query = qs.stringify(options.query);
this.url.search = prepend(qs.stringify(options.query));
}
}

Expand Down Expand Up @@ -201,41 +204,31 @@ class RequestOptions {
}

navigate(url) {
assert(typeof url === 'string');

if (url.indexOf('://') === -1)
url = 'http://' + url;

const data = URL.parse(url);
if (url instanceof URL)
url = url.toString();

if (data.protocol !== 'http:' &&
data.protocol !== 'https:') {
throw new Error('Malformed URL.');
}
assert(typeof url === 'string');

if (!data.hostname)
throw new Error('Malformed URL.');
this._navigate(new URL(url));
}

this.ssl = data.protocol === 'https:';
this.host = data.hostname;
this.port = this.ssl ? 443 : 80;
_navigate(url) {
this.url = url;

if (data.port != null) {
const port = parseInt(data.port, 10);
assert((port & 0xffff) === port);
this.port = port;
if (this.url.protocol !== 'http:' &&
this.url.protocol !== 'https:') {
throw new Error('Invalid URL protocol.');
}

this.path = data.pathname;
this.query = data.query;
if (this.url.port === '0')
throw new Error('Invalid URL port.');

if (data.auth) {
const parts = data.auth.split(':');
this.username = parts.shift();
this.password = parts.join(':');
}
this.username = this.url.username;
this.password = this.url.password;

return this;
this.url.username = '';
this.url.password = '';
this.url.hash = '';
}

isExpected(type) {
Expand Down Expand Up @@ -278,8 +271,9 @@ class RequestOptions {
}

getBackend() {
ensureRequires(this.ssl);
return this.ssl ? https : http;
const ssl = this.url.protocol === 'https:';
ensureRequires(ssl);
return ssl ? https : http;
}

getHeaders() {
Expand Down Expand Up @@ -312,47 +306,24 @@ class RequestOptions {
}

redirect(location) {
assert(typeof location === 'string');

let url = '';

if (this.ssl)
url += 'https://';
else
url += 'http://';

if (this.host.indexOf(':') !== -1)
url += `[${this.host}]`;
else
url += this.host;

url += ':' + this.port;
url += this.path;

if (this.query)
url += '?' + this.query;

this.navigate(URL.resolve(url, location));

return this;
this._navigate(new URL(location, this.url));
}

toHTTP() {
let query = '';

if (this.query)
query = '?' + this.query;
const defaultPort = this.url.protocol === 'https:' ? 443 : 80;
const hostname = this.url.hostname;
const isV6 = hostname[0] === '[';

return {
method: this.method,
host: this.host,
port: this.port,
path: this.path + query,
host: isV6 ? hostname.slice(1, -1) : hostname,
port: Number(this.url.port || defaultPort),
path: (this.url.pathname || '/') + this.url.search,
headers: this.getHeaders(),
agent: this.pool ? null : false,
lookup: this.lookup || undefined,
rejectUnauthorized: this.strictSSL,
ca: this.ca
ca: this.ca || undefined
};
}
}
Expand Down Expand Up @@ -698,7 +669,7 @@ request.stream = function stream(options) {

function ensureRequires(ssl) {
if (!URL)
URL = require('url');
URL = global.URL || require('url').URL;

if (!qs)
qs = require('querystring');
Expand All @@ -713,6 +684,10 @@ function ensureRequires(ssl) {
StringDecoder = require('string_decoder').StringDecoder;
}

function prepend(qs) {
return qs ? '?' + qs : '';
}

/*
* Expose
*/
Expand Down

0 comments on commit 5376bd7

Please sign in to comment.