UNPKG

@cocalc/project

Version:
95 lines 5.25 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2022 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* This is an express http server that is meant to receive connections only from web browser clients that signed in as collaborators on this projects. It serves both HTTP and websocket connections, which should be proxied through some hub. */ const express_1 = __importDefault(require("express")); const http_1 = require("http"); const awaiting_1 = require("awaiting"); const compression_1 = __importDefault(require("compression")); const body_parser_1 = __importDefault(require("body-parser")); const path_1 = require("path"); const fs_1 = require("fs"); const async_utils_1 = require("@cocalc/util/async-utils"); const init_program_1 = require("@cocalc/project/init-program"); const base_path_1 = __importDefault(require("@cocalc/backend/base-path")); const logger_1 = require("@cocalc/project/logger"); const data_1 = require("@cocalc/project/data"); const directory_listing_1 = __importDefault(require("@cocalc/project/directory-listing")); const http_server_1 = __importDefault(require("@cocalc/project/jupyter/http-server")); const server_1 = __importDefault(require("@cocalc/project/browser-websocket/server")); const upload_1 = __importDefault(require("@cocalc/project/upload")); const static_1 = __importDefault(require("./static")); const root_symlink_1 = __importDefault(require("./root-symlink")); const kucalc = require("@cocalc/project/kucalc"); const winston = (0, logger_1.getLogger)("browser-http-server"); async function init() { winston.info("starting server..."); const app = (0, express_1.default)(); app.disable("x-powered-by"); // https://github.com/sagemathinc/cocalc/issues/6101 const server = (0, http_1.createServer)(app); // suggested by http://expressjs.com/en/advanced/best-practice-performance.html#use-gzip-compression app.use((0, compression_1.default)()); // Needed for POST file to custom path, which is used for uploading files to projects. // parse application/x-www-form-urlencoded app.use(body_parser_1.default.urlencoded({ extended: true })); // parse application/json app.use(body_parser_1.default.json()); winston.info("creating root symbolic link"); await (0, root_symlink_1.default)(); const base = (0, path_1.join)(base_path_1.default, data_1.project_id, "raw") + "/"; if (kucalc.IN_KUCALC) { // Add a /health handler, which is used as a health check for Kubernetes. winston.info("initializing KuCalc only health metrics server"); kucalc.init_health_metrics(app, data_1.project_id); } // Setup the directory_listing/... server, which is used to provide directory listings // to the hub (at least in KuCalc). It is still used by HUB! But why? Maybe it is only // for the deprecated public access to a project? If so, we can get rid of all of that. winston.info("initializing directory listings server (DEPRECATED)"); app.use(base, (0, directory_listing_1.default)()); // Setup the jupyter/... server, which is used by our jupyter server for blobs, etc. winston.info("initializing Jupyter support HTTP server"); (async () => { // if the BlobStore isn't available immediately, this will take a while to initialize, and // we don't want to block the remainder of this setup... app.use(base, await (0, http_server_1.default)()); })(); // Setup the ws websocket server, which is used by clients // for direct websocket connections to the project, and also // serves primus.js, which is the relevant client library. winston.info("initializing websocket server"); // We have to explicitly also include the base as a parameter // to initWebsocket, since of course it makes deeper user of server. app.use(base, (0, server_1.default)(server, base)); // Setup the upload POST endpoint winston.info("initializing file upload server"); app.use(base, (0, upload_1.default)()); winston.info("initializing static server"); (0, static_1.default)(app, base); server.listen(init_program_1.options.browserPort, init_program_1.options.hostname); await (0, async_utils_1.once)(server, "listening"); const address = server.address(); if (address == null || typeof address == "string") { // null = failed; string doesn't happen since that's for unix domain // sockets, which we aren't using. // This is probably impossible, but it makes typescript happier. throw Error("failed to assign a port"); } const assignedPort = address.port; // may be a server assigned random port. winston.info(`Started -- port=${assignedPort}, host='${init_program_1.options.hostname}', base='${base}'`); winston.info(`Writing port to ${data_1.browserPortFile}`); await (0, awaiting_1.callback)(fs_1.writeFile, data_1.browserPortFile, `${assignedPort}`); } exports.default = init; //# sourceMappingURL=http-server.js.map