printer-notifications/node_modules/@polka/url/build.js

48 lines
1.0 KiB
JavaScript
Raw Normal View History

2023-11-17 13:22:28 +00:00
function parse(str) {
let out={}, arr=str.split('&');
for (let i=0, k, v; i < arr.length; i++) {
[k, v=''] = arr[i].split('=');
out[k] = out[k] !== void 0 ? [].concat(out[k], v) : v;
}
return out;
}
2023-11-13 21:10:04 +00:00
2023-11-17 13:22:28 +00:00
module.exports = function (req, toDecode) {
let url = req.url;
if (url == null) return;
let obj = req._parsedUrl;
if (obj && obj._raw === url) return obj;
obj = {
path: url,
pathname: url,
search: null,
query: null,
href: url,
_raw: url
};
if (url.length > 1) {
if (toDecode && !req._decoded && !!~url.indexOf('%', 1)) {
let nxt = url;
try { nxt = decodeURIComponent(url) } catch (e) {/* bad */}
url = req.url = obj.href = obj.path = obj.pathname = obj._raw = nxt;
req._decoded = true;
}
2023-11-13 21:10:04 +00:00
2023-11-17 13:22:28 +00:00
let idx = url.indexOf('?', 1);
2023-11-13 21:10:04 +00:00
if (idx !== -1) {
2023-11-17 13:22:28 +00:00
obj.search = url.substring(idx);
obj.query = obj.search.substring(1);
obj.pathname = url.substring(0, idx);
if (toDecode && obj.query.length > 0) {
obj.query = parse(obj.query);
2023-11-13 21:10:04 +00:00
}
}
}
2023-11-17 13:22:28 +00:00
return (req._parsedUrl = obj);
2023-11-13 21:10:04 +00:00
}