UNPKG

telefunc

Version:

Remote functions. Instead of API.

37 lines (36 loc) 1.07 kB
export { getEtag }; import { assert } from '../utils.js'; async function getEtag(runContext) { if (runContext.disableEtag) { return null; } let createHash; try { createHash = (await import('node:crypto')).createHash; } catch (err) { /* assertWarning( false, 'The HTTP response ETag header missing because the Node.js module `crypto` could not be loaded. Set `config.disableEtag = true` to remove this warning.', { onlyOnce: true } ) */ return null; } const etag = computeEtag(runContext.httpResponseBody, createHash); return etag; } function computeEtag(body, createHash) { const etagValue = computeEtagValue(body, createHash); assert(!etagValue.includes('"')); const etag = `"${etagValue}"`; return etag; } function computeEtagValue(body, createHash) { if (body.length === 0) { // fast-path empty body return '1B2M2Y8AsgTpgAmY7PhCfg=='; } return createHash('md5').update(body, 'utf8').digest('base64'); }