72 lines
3.1 KiB
JavaScript
72 lines
3.1 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.acceptCrossOrigin = exports.fetchBody = exports.respondStatic = exports.respondWithJSON = exports.respond500 = exports.respond404 = exports.respond204 = exports.addPreventCachingHeaders = exports.PREVENT_CACHING_HEADERS = void 0;
|
|
const lodash_1 = require("lodash");
|
|
const promisify_stream_1 = __importDefault(require("../utils/promisify-stream"));
|
|
const builtin_header_names_1 = __importDefault(require("../request-pipeline/builtin-header-names"));
|
|
const json_1 = require("./json");
|
|
const STATIC_RESOURCES_DEFAULT_CACHING_OPTIONS = {
|
|
maxAge: 30,
|
|
mustRevalidate: true,
|
|
};
|
|
exports.PREVENT_CACHING_HEADERS = {
|
|
[builtin_header_names_1.default.cacheControl]: 'no-cache, no-store, must-revalidate',
|
|
[builtin_header_names_1.default.pragma]: 'no-cache',
|
|
};
|
|
function addPreventCachingHeaders(res) {
|
|
res.setHeader(builtin_header_names_1.default.cacheControl, exports.PREVENT_CACHING_HEADERS[builtin_header_names_1.default.cacheControl]);
|
|
res.setHeader(builtin_header_names_1.default.pragma, exports.PREVENT_CACHING_HEADERS[builtin_header_names_1.default.pragma]);
|
|
}
|
|
exports.addPreventCachingHeaders = addPreventCachingHeaders;
|
|
function respond204(res) {
|
|
res.statusCode = 204;
|
|
res.end();
|
|
}
|
|
exports.respond204 = respond204;
|
|
function respond404(res) {
|
|
res.statusCode = 404;
|
|
res.end();
|
|
}
|
|
exports.respond404 = respond404;
|
|
function respond500(res, err = '') {
|
|
res.statusCode = 500;
|
|
res.end(err);
|
|
}
|
|
exports.respond500 = respond500;
|
|
function respondWithJSON(res, data, skipContentType, shouldAcceptCrossOrigin) {
|
|
if (!skipContentType)
|
|
res.setHeader(builtin_header_names_1.default.contentType, 'application/json');
|
|
// NOTE: GH-105
|
|
addPreventCachingHeaders(res);
|
|
if (shouldAcceptCrossOrigin)
|
|
acceptCrossOrigin(res);
|
|
res.end(data ? (0, json_1.stringify)(data) : '');
|
|
}
|
|
exports.respondWithJSON = respondWithJSON;
|
|
function respondStatic(req, res, resource, cachingOptions = {}) {
|
|
cachingOptions = (0, lodash_1.defaultsDeep)(cachingOptions, STATIC_RESOURCES_DEFAULT_CACHING_OPTIONS);
|
|
if (resource.etag === req.headers[builtin_header_names_1.default.ifNoneMatch]) {
|
|
res.statusCode = 304;
|
|
res.end();
|
|
}
|
|
else {
|
|
const { maxAge, mustRevalidate } = cachingOptions;
|
|
res.setHeader(builtin_header_names_1.default.cacheControl, `max-age=${maxAge}${mustRevalidate ? ', must-revalidate' : ''}`);
|
|
res.setHeader(builtin_header_names_1.default.eTag, resource.etag);
|
|
res.setHeader(builtin_header_names_1.default.contentType, resource.contentType);
|
|
res.end(resource.content);
|
|
}
|
|
}
|
|
exports.respondStatic = respondStatic;
|
|
function fetchBody(r, contentLength) {
|
|
return (0, promisify_stream_1.default)(r, contentLength);
|
|
}
|
|
exports.fetchBody = fetchBody;
|
|
function acceptCrossOrigin(res) {
|
|
res.setHeader('access-control-allow-origin', '*');
|
|
}
|
|
exports.acceptCrossOrigin = acceptCrossOrigin;
|