@cocalc/project
Version:
CoCalc: project daemon
35 lines • 1.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const serve_index_1 = __importDefault(require("serve-index"));
const logger_1 = require("@cocalc/project/logger");
function init(app, base) {
const winston = (0, logger_1.getLogger)("serve-static-files-to-browser");
winston.info(`initialize with base="${base}"`);
// Setup the static raw HTTP server. This must happen after anything above,
// since it serves all URL's (so it has to be the fallback).
app.use(base, (req, res, next) => {
// this middleware function has to come before the express.static server!
// it sets the content type to octet-stream (aka "download me") if URL query ?download exists
if (req.query.download != null) {
res.setHeader("Content-Type", "application/octet-stream");
}
// Note: we do not set no-cache since that causes major issues on Safari:
// https://github.com/sagemathinc/cocalc/issues/5120
// By now our applications should use query params to break the cache.
res.setHeader("Cache-Control", "private, must-revalidate");
next();
});
const { HOME } = process.env;
if (HOME == null) {
throw Error("HOME env variable must be defined");
}
winston.info(`serving up HOME="${HOME}"`);
app.use(base, (0, serve_index_1.default)(HOME, { hidden: true, icons: true }));
app.use(base, (0, express_1.static)(HOME, { dotfiles: "allow" }));
}
exports.default = init;
//# sourceMappingURL=static.js.map