UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

90 lines 3.75 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.render_static_path = void 0; /* Render a public path using the express static server. */ var fs = __importStar(require("fs")); var os_path = __importStar(require("path")); var url_1 = require("url"); var serve_static_1 = __importDefault(require("serve-static")); var serve_index_1 = __importDefault(require("serve-index")); var finalhandler_1 = __importDefault(require("finalhandler")); var STATIC_OPTIONS = { index: ["index.html", "index.htm"] }; var INDEX_OPTIONS = { icons: true }; // NOTE: we never clear these caches. However, there is at most one for // every public_path, so it probably wastes very little memory. Someday // should change to an LRU cache... var serve_static_cache = {}; function get_serve_static(dir) { return serve_static_cache[dir] != null ? serve_static_cache[dir] : (serve_static_cache[dir] = serve_static_1.default(dir, STATIC_OPTIONS)); } var serve_index_cache = {}; function get_serve_index(dir) { return serve_index_cache[dir] != null ? serve_index_cache[dir] : (serve_index_cache[dir] = serve_index_1.default(dir, INDEX_OPTIONS)); } // res = html response object function render_static_path(opts) { // We first test that we have access to the file (and it exists) before // messing with the express static server. I don't know why, but for some // reason it hangs forever when fed an unknown path, which obviously leads // to a very bad experience for users! var req = opts.req, res = opts.res, dir = opts.dir, path = opts.path; // see https://stackoverflow.com/questions/14166898/node-js-with-express-how-to-remove-the-query-string-from-the-url var pathname = url_1.parse(path).pathname; if (pathname == null) { // I think this shouldn't be possible, but typescript thinks it is. res.sendStatus(404); return; } var target = os_path.join(dir, decodeURI(pathname)); fs.access(target, fs.constants.R_OK, function (err) { if (err != null) { res.sendStatus(404); return; } var s_static = get_serve_static(dir); var s_index = get_serve_index(dir); req.url = path === "" ? "/" : path; s_static(req, res, function (err) { if (err) { finalhandler_1.default(err); } else { s_index(req, res, finalhandler_1.default); } }); }); } exports.render_static_path = render_static_path; //# sourceMappingURL=render-static-path.js.map