UNPKG

@simplito/privmx-webendpoint

Version:

PrivMX Web Endpoint library

95 lines (92 loc) 4.06 kB
"use strict"; /*! PrivMX Web Endpoint. Copyright © 2024 Simplito sp. z o.o. This file is part of the PrivMX Platform (https://privmx.dev). This software is Licensed under the PrivMX Free License. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ConnectionNative = void 0; const BaseNative_1 = require("./BaseNative"); class ConnectionNative extends BaseNative_1.BaseNative { lastConnectionId = -1; userVerifierPtr = -1; static verifierBindingId = -1; static getVerifierBindingId() { return ++this.verifierBindingId; } async newApi(_connectionPtr) { throw new Error("Use the newConnection() - specialized version of method instead."); } async deleteApi(ptr) { await this.runAsync((taskId) => this.api.lib.Connection_deleteConnection(taskId, ptr)); this.deleteApiRef(); } async newConnection() { return this.runAsync((taskId) => this.api.lib.Connection_newConnection(taskId)); } async deleteConnection(ptr) { await this.runAsync((taskId) => this.api.lib.Connection_deleteConnection(taskId, ptr)); this.deleteApiRef(); } async connect(ptr, args) { await this.runAsync((taskId) => this.api.lib.Connection_connect(taskId, ptr, args)); await this.getConnectionId(ptr, []); } async connectPublic(ptr, args) { return this.runAsync((taskId) => this.api.lib.Connection_connectPublic(taskId, ptr, args)); } async getConnectionId(ptr, args) { if (this.lastConnectionId < 0) { this.lastConnectionId = await this.runAsync((taskId) => this.api.lib.Connection_getConnectionId(taskId, ptr, args)); } return this.lastConnectionId; } async listContexts(ptr, args) { return this.runAsync((taskId) => this.api.lib.Connection_listContexts(taskId, ptr, args)); } async listContextUsers(ptr, args) { return this.runAsync((taskId) => this.api.lib.Connection_listContextUsers(taskId, ptr, args)); } async subscribeFor(ptr, args) { return this.runAsync((taskId) => this.api.lib.Connection_subscribeFor(taskId, ptr, args)); } async unsubscribeFrom(ptr, args) { return this.runAsync((taskId) => this.api.lib.Connection_unsubscribeFrom(taskId, ptr, args)); } async buildSubscriptionQuery(ptr, args) { return this.runAsync((taskId) => this.api.lib.Connection_buildSubscriptionQuery(taskId, ptr, args)); } async disconnect(ptr, args) { await this.runAsync((taskId) => this.api.lib.Connection_disconnect(taskId, ptr, args)); } async setUserVerifier(_ptr, args) { if (this.userVerifierPtr > -1) { await this.deleteUserVerifierInterface(this.userVerifierPtr); this.userVerifierPtr = -1; } const [connectionPtr, verifierInterface] = args; const bindingId = ConnectionNative.getVerifierBindingId(); if (!window.userVerifierBinder) { window.userVerifierBinder = {}; } window.userVerifierBinder[bindingId] = { userVierifier_verify: async (request) => { if (verifierInterface && typeof verifierInterface.verify === "function") { return verifierInterface.verify(request); } throw new Error("Call on UserVerifierInterface with missing implementation"); }, }; this.userVerifierPtr = await this.runAsync((taskId) => this.api.lib.Connection_newUserVerifierInterface(taskId, connectionPtr, bindingId)); } async newUserVerifierInterface(connectionPtr) { return this.runAsync((taskId) => this.api.lib.Connection_newUserVerifierInterface(taskId, connectionPtr)); } async deleteUserVerifierInterface(ptr) { await this.runAsync((taskId) => this.api.lib.Connection_deleteUserVerifierInterface(taskId, ptr)); } } exports.ConnectionNative = ConnectionNative;