UNPKG

@cocalc/hub

Version:
162 lines (139 loc) 4.64 kB
// Generated by CoffeeScript 2.5.1 (function() { //######################################################################## // This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. // License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details //######################################################################## /* Projects */ var Project, _project_cache, callback2, defaults, local_hub_connection, message, misc, misc_node, postgres, required, winston; winston = require('./logger').getLogger('projects'); postgres = require('@cocalc/database'); local_hub_connection = require('./local_hub_connection'); message = require('@cocalc/util/message'); ({callback2} = require('@cocalc/util/async-utils')); misc = require('@cocalc/util/misc'); misc_node = require('@cocalc/backend/misc_node'); ({defaults, required} = misc); // Create a project object that is connected to a local hub (using // appropriate port and secret token), login, and enhance socket // with our message protocol. _project_cache = {}; exports.new_project = function(project_id, database, compute_server) { var P; P = _project_cache[project_id]; if (P == null) { P = new Project(project_id, database, compute_server); _project_cache[project_id] = P; } return P; }; Project = class Project { constructor(project_id1, database1, compute_server1) { this.dbg = this.dbg.bind(this); this._fixpath = this._fixpath.bind(this); this.owner = this.owner.bind(this); // get latest info about project from database this.get_info = this.get_info.bind(this); this.call = this.call.bind(this); // async function this.named_server_port = this.named_server_port.bind(this); this.read_file = this.read_file.bind(this); this.write_file = this.write_file.bind(this); this.project_id = project_id1; this.database = database1; this.compute_server = compute_server1; this.dbg("instantiating Project class"); this.local_hub = local_hub_connection.new_local_hub(this.project_id, this.database, this.compute_server); // we always look this up and cache it this.get_info(); } dbg(m) { return winston.debug(`project(${this.project_id}): ${m}`); } _fixpath(obj) { if ((obj != null) && (this.local_hub != null)) { if (obj.path != null) { if (obj.path[0] !== '/') { return obj.path = this.local_hub.path + '/' + obj.path; } } else { return obj.path = this.local_hub.path; } } } owner(cb) { if (this.database == null) { cb('need database in order to determine owner'); return; } return this.database.get_project({ project_id: this.project_id, columns: ['account_id'], cb: (err, result) => { if (err) { return cb(err); } else { return cb(err, result[0]); } } }); } get_info(cb) { if (this.database == null) { cb('need database in order to determine owner'); return; } return this.database.get_project({ project_id: this.project_id, columns: postgres.PROJECT_COLUMNS, cb: (err, result) => { if (err) { return typeof cb === "function" ? cb(err) : void 0; } else { this.cached_info = result; return typeof cb === "function" ? cb(void 0, result) : void 0; } } }); } call(opts) { opts = defaults(opts, { mesg: required, multi_response: false, timeout: 15, cb: void 0 }); //@dbg("call") this._fixpath(opts.mesg); opts.mesg.project_id = this.project_id; return this.local_hub.call(opts); } async named_server_port(name) { var resp; this.dbg("named_server_port(name=${name})"); resp = (await callback2(this.call, { mesg: message.named_server_port({ name: name }), timeout: 30 })); this.dbg(`named_server_port ${resp.port}`); return resp.port; } read_file(opts) { this.dbg("read_file"); this._fixpath(opts); opts.project_id = this.project_id; return this.local_hub.read_file(opts); } write_file(opts) { this.dbg("write_file"); this._fixpath(opts); opts.project_id = this.project_id; return this.local_hub.write_file(opts); } }; }).call(this); //# sourceMappingURL=projects.js.map