"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.send = void 0;
const etag_1 = __importDefault(require("etag"));
const isDebug = process.env.DEBUG;
const alias = {
    js: 'application/javascript',
    css: 'text/css',
    html: 'text/html',
    json: 'application/json'
};
function send(req, res, content, type, etag = etag_1.default(content, { weak: true }), cacheControl = 'no-cache', map) {
    if (req.headers['if-none-match'] === etag) {
        res.statusCode = 304;
        return res.end();
    }
    res.setHeader('Content-Type', alias[type] || type);
    res.setHeader('Cache-Control', cacheControl);
    res.setHeader('Etag', etag);
    // inject source map reference
    if (map && map.mappings) {
        if (isDebug) {
            content += `\n/*${JSON.stringify(map, null, 2)}*/\n`;
        }
        content += genSourceMapString(map);
    }
    res.statusCode = 200;
    return res.end(content);
}
exports.send = send;
function genSourceMapString(map) {
    if (typeof map !== 'string') {
        map = JSON.stringify(map);
    }
    return `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(map).toString('base64')}`;
}
//# sourceMappingURL=send.js.map