etag
Version:
Create simple HTTP ETags
38 lines • 1.46 kB
JavaScript
/*!
* etag
* Copyright(c) 2014-2016 Douglas Christopher Wilson
* MIT Licensed
*/
import { Buffer } from "node:buffer";
import { createHash, hash } from "node:crypto";
// Above this size, one-shot hashing no longer improves performance.
const ONESHOT_MAX_LENGTH = 100 * 1024;
/** Create an ETag for a string, ArrayBuffer view, or file stats object. */
export function etag(entity, options) {
const isBody = typeof entity === "string" || ArrayBuffer.isView(entity);
const weak = typeof options?.weak === "boolean" ? options.weak : !isBody;
const tag = isBody ? entitytag(entity) : stattag(entity);
return weak ? `W/${tag}` : tag;
}
/** Generate a tag from the entity's byte length and hash. */
function entitytag(entity) {
const length = Buffer.byteLength(entity);
if (length === 0) {
return '"0~47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU"';
}
const digest = length <= ONESHOT_MAX_LENGTH
? hash("sha256", entity, "base64url")
: createHash("sha256").update(entity).digest("base64url");
const size = length.toString(32);
return `"${size}~${digest}"`;
}
/** Generate a tag from the file size and modification time. */
function stattag(stat) {
const ms = stat.mtime.getTime();
const mtime = Number.isSafeInteger(ms)
? BigInt(ms).toString(32)
: ms.toString(32);
const size = stat.size.toString(32);
return `"${size}~${mtime}"`;
}
//# sourceMappingURL=index.js.map