const { join } = require ( "path" ) ; import { Router } from "express" ; import { Server } from "http" ; const Primus = require ( "primus" ) ; import { init_websocket_api } from "./api" ; import { getLogger } from "@cocalc/project/logger" ; export default function init ( server , basePath ) { const winston = getLogger ( "websocket-server" ) ; const opts = { pathname : join ( basePath , ".smc" , "ws" ) , transformer : "websockets" , } ; winston . info ( `Initalizing primus websocket server at "${ opts . pathname }"...` ) ; const primus = new Primus ( server , opts ) ; primus . plugin ( "multiplex" , require ( "primus-multiplex" ) ) ; init_websocket_api ( primus ) ; const router = Router ( ) ; const library = primus . library ( ) ; router . get ( "/.smc/primus.js" , ( _ , res ) => { winston . debug ( "serving up primus.js to a specific client" ) ; res . send ( library ) ; } ) ; winston . info ( `waiting for clients to request primus.js (length=${ library . length })...` ) ; return router ; }