Skip to content

Commit

Permalink
refactor(server): [wip] move to es2022 + improve typescript support
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed May 24, 2024
1 parent 310ab1b commit 1d1db84
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 333 deletions.
16 changes: 5 additions & 11 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
AUDIT_STATE_NAME,
} from '../common/constants.js';
import logger from '../common/logger.js';
import version from '../common/version.js';
import VERSION from '../common/version.js';

// for testing purposes
export const kClientVersionTest = Symbol('soundworks:client-version-test');
Expand Down Expand Up @@ -61,9 +61,9 @@ export const kClientVersionTest = Symbol('soundworks:client-version-test');
* ```
*/
class Client {
#config = null;
#version = null;
#role = null;
#config = null;
#id = null;
#uuid = null;
#target = null;
Expand Down Expand Up @@ -121,21 +121,15 @@ class Client {
this.#config.env = {};
}

// minimal configuration for websockets
this.#config.env.websockets = Object.assign({
path: 'socket',
pingInterval: 5000,
}, config.env.websockets);

this.#version = version;
this.#version = VERSION;
// allow override though config for testing
if (config[kClientVersionTest]) {
this.#version = config[kClientVersionTest];
}

this.#role = config.role;
this.#target = isBrowser() ? 'browser' : 'node';
this.#socket = new ClientSocket();
this.#socket = new ClientSocket(this.#role, this.#config, { path: 'socket' });
this.#contextManager = new ClientContextManager();
this.#pluginManager = new ClientPluginManager(this);
this.#stateManager = new ClientStateManager();
Expand Down Expand Up @@ -305,7 +299,7 @@ class Client {
*/
async init() {
// init socket communications
await this.#socket.init(this.#role, this.#config);
await this.#socket.init();

// we need the try/catch block to change the promise rejection into proper error
try {
Expand Down
33 changes: 20 additions & 13 deletions src/client/ClientSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ export const kSocketTerminate = Symbol('soundworks:socket-terminate');
* @hideconstructor
*/
class ClientSocket {
#role = null;
#config = null;
#socketOptions = null;
#socket = null;
#listeners = new Map();

constructor() {}
constructor(role, config, socketOptions) {
this.#role = role;
this.#config = config;
this.#socketOptions = socketOptions;
}

/**
* Initialize a websocket connection with the server. Automatically called
Expand All @@ -47,39 +54,39 @@ class ClientSocket {
* @param {object} config - Configuration of the sockets
* @private
*/
async init(role, config) {
let { path } = config.env.websockets;
async init() {
let { path } = this.#socketOptions;
// cf. https://github.com/collective-soundworks/soundworks/issues/35
if (config.env.subpath) {
path = `${config.env.subpath}/${path}`;
if (this.#config.env.subpath) {
path = `${this.#config.env.subpath}/${path}`;
}

const protocol = config.env.useHttps ? 'wss:' : 'ws:';
const port = config.env.port;
const protocol = this.#config.env.useHttps ? 'wss:' : 'ws:';
const port = this.#config.env.port;
let serverAddress;
let webSocketOptions;

if (isBrowser()) {
// if a server address is given in config, use it, else fallback to URL hostname
if (config.env.serverAddress !== '') {
serverAddress = config.env.serverAddress;
if (this.#config.env.serverAddress !== '') {
serverAddress = this.#config.env.serverAddress;
} else {
serverAddress = window.location.hostname;
}

webSocketOptions = [];
} else {
serverAddress = config.env.serverAddress;
serverAddress = this.#config.env.serverAddress;

webSocketOptions = {
rejectUnauthorized: false,
};
}

let queryParams = `role=${role}`;
let queryParams = `role=${this.#role}`;

if (config.token) {
queryParams += `&token=${config.token}`;
if (this.#config.token) {
queryParams += `&token=${this.#config.token}`;
}

const url = `${protocol}//${serverAddress}:${port}/${path}?${queryParams}`;
Expand Down
Loading

0 comments on commit 1d1db84

Please sign in to comment.