UNPKG

@jsenv/util

Version:

Set of functions often needed when using Node.js.

23 lines (16 loc) 583 B
import { createHash } from "crypto" const ETAG_FOR_EMPTY_CONTENT = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"' export const bufferToEtag = (buffer) => { if (!Buffer.isBuffer(buffer)) { throw new TypeError(`buffer expected, got ${buffer}`) } if (buffer.length === 0) { return ETAG_FOR_EMPTY_CONTENT } const hash = createHash("sha1") hash.update(buffer, "utf8") const hashBase64String = hash.digest("base64") const hashBase64StringSubset = hashBase64String.slice(0, 27) const length = buffer.length return `"${length.toString(16)}-${hashBase64StringSubset}"` }