Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/packages / libraries / react-server / caching / deps / fromRequestToHash.js
Size: Mime:
import { EMPTY_OBJ, isSafe } from 'exotic';
import { logger } from '../../log';
const version = process.env.VERSION;
/**
 * @example GET/url/@@/{params, body}
 */
function getComputedValue(req, namespace) {
    // GET/url
    const { originalUrl, method } = req;
    let computed = '';
    if (req.url) {
        computed += req.url._raw || req.url;
    }
    if (!computed) {
        computed += isSafe(req.originalUrl) && originalUrl;
    }
    if (!computed) {
        console.error('[getComputedValue] should be never called, remove if never called');
        computed += isSafe(req._parsedOriginalUrl) && req._parsedOriginalUrl.href;
    }
    if (!computed) {
        console.error('[getComputedValue] should be never called, remove if never called');
        computed += isSafe(req._parsedUrl) && req._parsedUrl.url;
    }
    if (!computed) {
        console.error('[getComputedValue] should be never called, remove if never called');
        computed += isSafe(req.referer) && req.referer;
    }
    computed = '' + version + namespace + method + computed + '/@@/';
    return computed;
}
function fromRequestToHash(request, namespace = '') {
    const req = request.req || request;
    const { hash } = req;
    const query = req.query || req.params || req.body || EMPTY_OBJ;
    if (hash) {
        return hash;
    }
    // add C to hash if it's compat, D if default
    // short due to length of file restriction
    const compatKey = request.IS_COMPAT ? 'C' : 'D';
    let computed = compatKey + getComputedValue(req, namespace);
    Object.keys(query).forEach(key => (computed += key + JSON.stringify(query[key])));
    logger.info('[ssr][cache] hash: ' + computed);
    // maybe we want to like, hash the hash to keep it always same length
    if (computed.length > 100) {
        computed = computed.slice(0, 100);
    }
    return computed;
}
export { fromRequestToHash };
export default fromRequestToHash;
//# sourceMappingURL=fromRequestToHash.js.map