UNPKG

@authup/core-realtime-kit

Version:

Package containing a toolkit for the core realtime service.

184 lines (175 loc) 5.94 kB
'use strict'; var socket_ioClient = require('socket.io-client'); var kit = require('@authup/kit'); /* * Copyright (c) 2025. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function cleanDoubleSlashes(input = '') { if (input.indexOf('://') !== -1) { return input.split('://').map((str)=>cleanDoubleSlashes(str)).join('://'); } return input.replace(/\/+/g, '/'); } function toClientManagerTokenAsyncFn(input) { if (typeof input === 'undefined') { return ()=>undefined; } if (typeof input === 'string') { return ()=>input; } return input; } function isDisconnectDescription(input) { return kit.isObject(input) && typeof input.description === 'string'; } class ClientManager { get options() { return this.manager.opts; } async connect(namespace = '/') { const socket = this.inject(namespace); if (socket.connected) { return socket; } return new Promise((resolve, reject)=>{ socket.once('connect', ()=>{ resolve(socket); }); socket.once('connect_error', (err)=>{ reject(err); }); socket.connect(); }); } async disconnect(namespace = '/') { const socket = this.inject(namespace); if (!socket.connected) { return; } await new Promise((resolve, reject)=>{ socket.once('disconnect', (reason, description)=>{ if (reason === 'io client disconnect') { resolve(); return; } if (isDisconnectDescription(description)) { reject(new Error(description.description)); return; } reject(description); }); socket.disconnect(); }); } async reconnect(namespace = '/') { await this.disconnect(namespace); return new Promise((resolve, reject)=>{ setTimeout(()=>{ this.connect(namespace).then((socket)=>resolve(socket)).catch((err)=>reject(err)); }); }); } async reconnectAll() { const keys = this.sockets.keys(); const promises = []; // eslint-disable-next-line no-constant-condition while(true){ const key = keys.next(); if (key.done) { break; } promises.push(this.reconnect(key.value)); } return Promise.all(promises); } inject(namespace = '/') { if (this.sockets.has(namespace)) { return this.sockets.get(namespace); } const socket = this.manager.socket(namespace, { auth: (cb)=>{ Promise.resolve().then(()=>this.tokenFn()).then((token)=>{ cb({ token }); }).catch(()=>cb()); } }); this.sockets.set(namespace, socket); return socket; } eject(namespace = '/') { if (this.sockets[namespace]) { this.sockets[namespace].disconnect(); delete this.sockets[namespace]; } } constructor(ctx){ this.sockets = new Map(); const url = new URL(ctx.url); const baseURL = `${url.protocol}//${url.host}`; const options = { ...ctx.options || {} }; if (options.path) { if (url.pathname.endsWith(options.path)) { options.path = url.pathname; } else if (url.pathname !== '/') { options.path = cleanDoubleSlashes(url.pathname + options.path); } } else if (url.pathname.endsWith('/socket.io')) { options.path = url.pathname; } else if (url.pathname !== '/') { options.path = cleanDoubleSlashes(`${url.pathname}/socket.io`); } this.manager = new socket_ioClient.Manager(baseURL, { autoConnect: false, ...options }); this.tokenFn = toClientManagerTokenAsyncFn(ctx.token); } } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ var EventNameSuffix = /*#__PURE__*/ function(EventNameSuffix) { EventNameSuffix["CREATED"] = "created"; EventNameSuffix["DELETED"] = "deleted"; EventNameSuffix["UPDATED"] = "updated"; EventNameSuffix["SUBSCRIBE"] = "subscribe"; EventNameSuffix["UNSUBSCRIBE"] = "unsubscribe"; return EventNameSuffix; }({}); /* * Copyright (c) 2023-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function buildEventFullName(entity, event) { const eventCapitalized = event.substring(0, 1).toUpperCase() + event.substring(1); return entity + eventCapitalized; } /* * Copyright (c) 2021-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function isEventTarget(input) { return typeof input === 'number' || typeof input === 'string' || typeof input === 'undefined'; } function isEventCallback(input, fnArgs) { if (typeof fnArgs === 'undefined') { return typeof input === 'function'; } return typeof input === 'function' && input.length >= fnArgs; } exports.ClientManager = ClientManager; exports.EventNameSuffix = EventNameSuffix; exports.buildEventFullName = buildEventFullName; exports.isEventCallback = isEventCallback; exports.isEventTarget = isEventTarget; //# sourceMappingURL=index.cjs.map