alagarr
Version:
Alagarr is a request-response helper library that removes the boilerplate from your Node.js serverless functions and helps make your code portable.
30 lines (29 loc) • 1.09 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const crypto = __importStar(require("crypto"));
exports.EMPTY_ENTITY_TAG = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
function entitytag(entity) {
const hash = crypto
.createHash('sha1')
.update(entity, 'utf8')
.digest('base64')
.substring(0, 27);
const length = typeof entity === 'string'
? Buffer.byteLength(entity, 'utf8')
: entity.length;
return `"${length.toString(16)}-${hash}"`;
}
function etag(entity) {
return entity.length === 0 ? exports.EMPTY_ENTITY_TAG : entitytag(entity);
}
function etagHeader(response) {
return Object.assign({}, response, { headers: Object.assign({}, response.headers, { etag: etag(response.body) }) });
}
exports.default = etagHeader;