chore(xo-server): move Xo#createUserConnection() to Api#createApiConnection()

This commit is contained in:
Julien Fontanet
2022-03-15 15:23:01 +01:00
parent 3acbc08ec5
commit 244b150385
3 changed files with 28 additions and 27 deletions

View File

@@ -558,7 +558,7 @@ const setUpApi = (webServer, xo, config) => {
const { remoteAddress } = upgradeReq.socket
// Create the abstract XO object for this connection.
const connection = xo.createUserConnection(remoteAddress)
const connection = xo.createApiConnection(remoteAddress)
connection.once('close', () => {
socket.close()
})

View File

@@ -10,6 +10,7 @@ import { format, JsonRpcError, MethodNotFound } from 'json-rpc-peer'
import * as methods from '../api/index.mjs'
import * as sensitiveValues from '../sensitive-values.mjs'
import Connection from '../connection.mjs'
import { noop, serializeError } from '../utils.mjs'
import * as errors from 'xo-common/api-errors.js'
@@ -146,6 +147,12 @@ async function resolveParams(method, params) {
// -------------------------------------------------------------------
export default class Api {
#connections = new Set()
get apiConnections() {
return this.#connections
}
constructor(app) {
this._logger = null
this._methods = { __proto__: null }
@@ -380,6 +387,24 @@ export default class Api {
}
}
createApiConnection(remoteAddress) {
const connections = this.#connections
const connection = new Connection()
connection.set('user_ip', remoteAddress)
connections.add(connection)
connection.on('close', () => {
connections.delete(connection)
log.info(`- WebSocket connection (${remoteAddress}) (${connections.size} connected)`)
})
log.info(`+ WebSocket connection (${remoteAddress}) (${connections.size} connected)`)
return connection
}
registerApiHttpRequest(method, session, fn, data, { exposeAllErrors = false, ...opts } = {}) {
const app = this._app
const logger = this._logger

View File

@@ -17,7 +17,6 @@ import { parseDuration } from '@vates/parse-duration'
import { UniqueIndex as XoUniqueIndex } from 'xo-collection/unique-index.js'
import mixins from './xo-mixins/index.mjs'
import Connection from './connection.mjs'
import { generateToken, noop } from './utils.mjs'
// ===================================================================
@@ -39,9 +38,6 @@ export default class Xo extends EventEmitter {
this._objects = new XoCollection()
this._objects.createIndex('byRef', new XoUniqueIndex('_xapiRef'))
// Connections to users.
this._connections = new Set()
this._httpRequestWatchers = { __proto__: null }
// Connects to Redis.
@@ -124,26 +120,6 @@ export default class Xo extends EventEmitter {
// -----------------------------------------------------------------
createUserConnection(remoteAddress) {
const { _connections: connections } = this
const connection = new Connection()
connection.set('user_ip', remoteAddress)
connections.add(connection)
connection.on('close', () => {
connections.delete(connection)
log.info(`- WebSocket connection (${remoteAddress}) (${connections.size} connected)`)
})
log.info(`+ WebSocket connection (${remoteAddress}) (${connections.size} connected)`)
return connection
}
// -----------------------------------------------------------------
_handleHttpRequest(req, res, next) {
const { url } = req
@@ -269,7 +245,7 @@ export default class Xo extends EventEmitter {
// Some should be forwarded to connected clients.
// Some should be persistently saved.
_watchObjects() {
const { _connections: connections, _objects: objects } = this
const { _objects: objects } = this
let entered, exited
function reset() {
@@ -308,7 +284,7 @@ export default class Xo extends EventEmitter {
return
}
for (const connection of connections) {
for (const connection of this.apiConnections) {
// Notifies only authenticated clients.
if (connection.has('user_id') && connection.notify) {
if (enteredMessage) {