rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
15 lines (14 loc) • 540 B
JavaScript
import { createHash } from "crypto";
/**
* Generate a Hash from a Body, Headers & Status Code to be used for caching using SHA1
* @since 6.0.0
*/ export default function toETag(data, headers, cookies, status) {
if (status < 200 || status >= 400)
return null;
const hashed = createHash('sha1')
.update(JSON.stringify(headers))
.update(JSON.stringify(Object.values(cookies).map((v) => v.value)))
.update(Buffer.from(data))
.digest('hex');
return `W/"${status.toString(16)}-${hashed}"`;
}