Skip to content

Commit

Permalink
chore: fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Oct 28, 2021
1 parent 56c5e0f commit c2c2039
Show file tree
Hide file tree
Showing 2 changed files with 380 additions and 380 deletions.
144 changes: 72 additions & 72 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
// we know that the behavior of URL will remove the default port for protocol
// https://url.spec.whatwg.org/#url-miscellaneous
const protocols = ["ftp", 21, "http", 80, "https", 443, "ws", 80, "wss", 443];
const slashProtocols = ["ftp:", "http:", "https:", "ws:", "wss:"];
const protocols = ['ftp', 21, 'http', 80, 'https', 443, 'ws', 80, 'wss', 443]
const slashProtocols = ['ftp:', 'http:', 'https:', 'ws:', 'wss:']

// check if we should keep port
function checkKeepPort(base) {
if (typeof base !== "string") return -1;
function checkKeepPort (base) {
if (typeof base !== 'string') return -1
for (let i = 0; i < protocols.length; i += 2) {
if (
base.startsWith(protocols[i] + "://") &&
base.includes(":" + protocols[i + 1])
base.startsWith(protocols[i] + '://') &&
base.includes(':' + protocols[i + 1])
) {
return i;
return i
}
}
return -1;
return -1
}

function sanitizePath(pathname) {
let i = 0;
function sanitizePath (pathname) {
let i = 0
// we detech how many slash before a valid path
for (i; i < pathname.length; i++) {
if (pathname[i] !== "/" && pathname[i] !== "\\") break;
if (pathname[i] !== '/' && pathname[i] !== '\\') break
}
// turns all leading / or \ into a single /
return i ? "/" + pathname.substr(i) : pathname;
return i ? '/' + pathname.substr(i) : pathname
}

function isOption(base) {
function isOption (base) {
return (
typeof base === "object" &&
typeof base === 'object' &&
base !== null &&
!(base instanceof URL) &&
!(base instanceof SecureURL)
);
)
}

/**
Expand All @@ -54,78 +54,78 @@ function isOption(base) {
* In "insecure" mode, the return is exactly the same when
* using `URL`
*/
function SecureURL(path, base, option) {
function SecureURL (path, base, option) {
if (!(this instanceof SecureURL)) {
return new SecureURL(path, base, option);
return new SecureURL(path, base, option)
}

option = Object.assign(
{
mode: "path",
keepPort: true,
mode: 'path',
keepPort: true
},
option
);
)
if (arguments.length === 2 && isOption(base)) {
option = Object.assign(option, base);
base = undefined;
option = Object.assign(option, base)
base = undefined
}
const { mode, keepPort } = option;
const { mode, keepPort } = option

if (
typeof mode !== "string" ||
(mode !== "path" && mode !== "relax" && mode !== "insecure")
typeof mode !== 'string' ||
(mode !== 'path' && mode !== 'relax' && mode !== 'insecure')
) {
throw new Error(
`"mode" is expected to be "path", "relax" or "insecure", but recieved "${mode}"`
);
)
}

if (typeof keepPort !== "boolean") {
if (typeof keepPort !== 'boolean') {
throw new Error(
`"keepPort" is expected to be boolean, but recieved "${keepPort}"`
);
)
}

// allow base to be either SecureURL or URL
if (base instanceof SecureURL || base instanceof URL) base = base.href;
if (base instanceof SecureURL || base instanceof URL) base = base.href
// allow path to be either SecureURL or URL
if (path instanceof SecureURL || path instanceof URL) path = path.href;
if (path instanceof SecureURL || path instanceof URL) path = path.href

// in mode "path" or "relax", we do not allow "//" host to be exist
if (mode === "path" || mode === "relax") {
path = sanitizePath(path);
if (mode === 'path' || mode === 'relax') {
path = sanitizePath(path)
}

// we default a base
const kBase = base || path;
const kBase = base || path
// use URL to filter out invalid base
const kHost = new URL(kBase);
const kHost = new URL(kBase)
// use URL to filter out invalid path
let kURL = new URL(path, kHost);
let kURL = new URL(path, kHost)

/**
* In mode `insecure`, we should behave exactly the same as `URL`.
*/
if (mode === "insecure") {
kURL = new URL(path, base);
if (mode === 'insecure') {
kURL = new URL(path, base)
}

const kBasePort = keepPort ? checkKeepPort(base) : -1;
const kPathPort = keepPort ? checkKeepPort(path) : -1;
const kBasePort = keepPort ? checkKeepPort(base) : -1
const kPathPort = keepPort ? checkKeepPort(path) : -1

// in mode "path", we do not allow to modify the host
const kUseHost = mode === "path" ? kHost : kURL;
const kUsePort = mode === "path" ? kBasePort : kPathPort;
const isSlashProtocol = slashProtocols.includes(kUseHost.protocol);
const kUseHost = mode === 'path' ? kHost : kURL
const kUsePort = mode === 'path' ? kBasePort : kPathPort
const isSlashProtocol = slashProtocols.includes(kUseHost.protocol)

// internal store of
const internal = {
// computed value - calculate all the value again on set
href: "",
href: '',
// computed value - no effect on set
origin: "",
host: "",
origin: '',
host: '',
protocol: kUseHost.protocol,
username: kUseHost.username,
password: kUseHost.password,
Expand All @@ -134,66 +134,66 @@ function SecureURL(path, base, option) {
pathname: kURL.pathname,
hash: kURL.hash,
search: kURL.search,
searchParams: kURL.searchParams,
};
searchParams: kURL.searchParams
}

let hasCredential = false;
let hasCredential = false

/* istanbul ignore next */
if (internal.protocol) {
internal.href += internal.protocol;
internal.origin += internal.protocol;
internal.href += internal.protocol
internal.origin += internal.protocol
}
if (isSlashProtocol) {
internal.href += "//";
internal.origin += "//";
internal.href += '//'
internal.origin += '//'
}
if (internal.username) {
internal.href += internal.username;
hasCredential = true;
internal.href += internal.username
hasCredential = true
}
// password must come with username
if (internal.username && internal.password) {
internal.href += ":" + internal.password;
hasCredential = true;
internal.href += ':' + internal.password
hasCredential = true
}
if (hasCredential) {
internal.href += "@";
internal.href += '@'
}
if (internal.hostname) {
internal.href += internal.hostname;
internal.origin += internal.hostname;
internal.host += internal.hostname;
internal.href += internal.hostname
internal.origin += internal.hostname
internal.host += internal.hostname
}
if (internal.port) {
internal.href += ":" + internal.port;
internal.origin += ":" + internal.port;
internal.host += ":" + internal.port;
internal.href += ':' + internal.port
internal.origin += ':' + internal.port
internal.host += ':' + internal.port
}
/* istanbul ignore next */
if (internal.pathname) {
internal.href += internal.pathname;
internal.href += internal.pathname
}
if (internal.search) {
internal.href += internal.search;
internal.href += internal.search
}
if (internal.hash) {
internal.href += internal.hash;
internal.href += internal.hash
}

// non-slash protocol to not have origin
if (!isSlashProtocol) {
internal.origin = "null";
internal.origin = 'null'
}

Object.assign(this, internal);
Object.assign(this, internal)
}

// allow const SecureURL = require('url-sanitizer')
module.exports = SecureURL;
module.exports = SecureURL
// allow import SecureURL from 'url-sanitizer'
module.exports.default = SecureURL;
module.exports.default = SecureURL
// allow import { SecureURL } from 'url-sanitizer'
module.exports.SecureURL = SecureURL;
module.exports.SecureURL = SecureURL
// allow import { URL } from 'url-sanitizer'
module.exports.URL = SecureURL;
module.exports.URL = SecureURL

0 comments on commit c2c2039

Please sign in to comment.