helene
Version: 
Real-time Web Apps for Node.js
97 lines • 3.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.rpcLogout = exports.rpcOn = exports.rpcOff = exports.rpcInit = void 0;
const method_1 = require("./method");
const pick_1 = __importDefault(require("lodash/pick"));
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
const utils_1 = require("../utils");
const rpcInit = (server, method) => new method_1.Method(server, method, async function ({ meta, ...context }) {
    this.meta = meta;
    this.context = context;
    if (server.auth instanceof Function) {
        const caller = server.auth.call(this, context);
        const result = caller instanceof Promise ? await caller : caller;
        this.authenticated = Boolean(result);
        this.setContext(result);
        if (!this.authenticated)
            return false;
        server.emit(utils_1.ServerEvents.AUTHENTICATION, this);
        return (0, pick_1.default)(result, server.allowedContextKeys);
    }
    return false;
}, { protected: false });
exports.rpcInit = rpcInit;
const rpcOff = (server, method) => new method_1.Method(server, method, function ({ events, channel = utils_1.NO_CHANNEL }) {
    const node = this.socket ? this : null;
    return events.reduce((acc, eventName) => {
        const ch = server.channel(channel);
        const event = ch.server.events.get(eventName);
        if (!event) {
            console.log('[Helene] Event Not Found:', eventName);
            return {
                ...acc,
                [eventName]: false,
            };
        }
        const isSubscribed = ch.isSubscribed(node, event);
        if (!isSubscribed) {
            console.log('[Helene] Event Not Subscribed:', eventName);
        }
        ch.clients.get(event.name)?.delete(node);
        return {
            ...acc,
            [eventName]: true,
        };
    }, {});
}, { protected: false });
exports.rpcOff = rpcOff;
const rpcOn = (server, method) => new method_1.Method(server, method, async function ({ events, channel = utils_1.NO_CHANNEL }) {
    if ((0, isEmpty_1.default)(events))
        return {};
    const channelAllowed = await server.shouldAllowChannelSubscribe(this, channel);
    const acc = {};
    for (const eventName of events) {
        if (!channelAllowed) {
            acc[eventName] = false;
            continue;
        }
        const event = server.events.get(eventName);
        if (!event) {
            console.log('[Helene] Event Not Found:', eventName);
            acc[eventName] = false;
            continue;
        }
        const eventAllowed = !event.isProtected || this.authenticated;
        if (!eventAllowed) {
            acc[eventName] = false;
            continue;
        }
        const subscribeAllowed = await event.shouldSubscribe(this, eventName, channel);
        if (!subscribeAllowed) {
            acc[eventName] = false;
            continue;
        }
        const node = this.socket ? this : null;
        if (!node) {
            acc[eventName] = false;
            continue;
        }
        const serverChannel = server.channel(channel);
        serverChannel.addChannelClient(event.name, node);
        acc[eventName] = true;
    }
    return acc;
}, { protected: false });
exports.rpcOn = rpcOn;
const rpcLogout = (server, method) => new method_1.Method(server, method, async function () {
    this.context = null;
    this.authenticated = false;
    this.userId = null;
    server.emit(utils_1.ServerEvents.LOGOUT, this);
    return true;
}, { protected: true });
exports.rpcLogout = rpcLogout;
//# sourceMappingURL=methods.js.map