api-clawer/src/server/utilities.js
2026-03-13 20:52:09 +08:00

43 lines
883 B
JavaScript

/**
* Does the hostname of `URL` equal `host`?
*
* @param url {string}
* @param host {string}
* @return {boolean}
*/
const isHost = (url, host) => {
// FIXME: Due to #118, we can only check the url
// by .includes(). You are welcome to fix
// it (CWE-20).
return url.includes(host);
};
/**
* The wrapper of `isHost()` to simplify the code.
*
* @param url {string}
* @return {(host: string) => boolean}
* @see isHost
*/
const isHostWrapper = (url) => (host) => isHost(url, host);
const cookieToMap = (cookie) => {
return cookie
.split(';')
.map((cookie) => cookie.trim().split('='))
.reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});
};
const mapToCookie = (map) => {
return Object.entries(map)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
};
module.exports = {
isHost,
isHostWrapper,
cookieToMap,
mapToCookie,
};