Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/hub/projects.coffee
Views: 687
#########################################################################1# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2# License: MS-RSL – see LICENSE.md for details3#########################################################################45###6Projects7###89winston = require('./logger').getLogger('projects')1011postgres = require('@cocalc/database')12local_hub_connection = require('./local_hub_connection')13message = require('@cocalc/util/message')14{callback2} = require('@cocalc/util/async-utils')15misc = require('@cocalc/util/misc')16misc_node = require('@cocalc/backend/misc_node')17{defaults, required} = misc1819# Create a project object that is connected to a local hub (using20# appropriate port and secret token), login, and enhance socket21# with our message protocol.2223_project_cache = {}24exports.new_project = (project_id, database, projectControl) ->25P = _project_cache[project_id]26if not P?27P = new Project(project_id, database, projectControl)28_project_cache[project_id] = P29return P3031class Project32constructor: (@project_id, @database, @projectControl) ->33@dbg("instantiating Project class")34@local_hub = local_hub_connection.new_local_hub(@project_id, @database, @projectControl)35# we always look this up and cache it36@get_info()3738dbg: (m) =>39winston.debug("project(#{@project_id}): #{m}")4041_fixpath: (obj) =>42if obj? and @local_hub?43if obj.path?44if obj.path[0] != '/'45obj.path = @local_hub.path+ '/' + obj.path46else47obj.path = @local_hub.path4849owner: (cb) =>50if not @database?51cb('need database in order to determine owner')52return53@database.get_project54project_id : @project_id55columns : ['account_id']56cb : (err, result) =>57if err58cb(err)59else60cb(err, result[0])6162# get latest info about project from database63get_info: (cb) =>64if not @database?65cb('need database in order to determine owner')66return67@database.get_project68project_id : @project_id69columns : postgres.PROJECT_COLUMNS70cb : (err, result) =>71if err72cb?(err)73else74@cached_info = result75cb?(undefined, result)7677call: (opts) =>78opts = defaults opts,79mesg : required80multi_response : false81timeout : 1582cb : undefined83#@dbg("call")84@_fixpath(opts.mesg)85opts.mesg.project_id = @project_id86@local_hub.call(opts)8788# async function89named_server_port: (name) =>90@dbg("named_server_port(name=#{name})")91resp = await callback2(@call, {mesg : message.named_server_port(name:name), timeout : 30})92@dbg("named_server_port #{resp.port}")93return resp.port9495read_file: (opts) =>96@dbg("read_file")97@_fixpath(opts)98opts.project_id = @project_id99@local_hub.read_file(opts)100101write_file: (opts) =>102@dbg("write_file")103@_fixpath(opts)104opts.project_id = @project_id105@local_hub.write_file(opts)106107108109