UNPKG

@tonappchain/adnl

Version:

ADNL TypeScript implementation

65 lines 2.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ADNLClientWS = void 0; const isomorphic_ws_1 = __importDefault(require("isomorphic-ws")); const client_1 = require("./client"); const packet_1 = require("./packet"); class ADNLClientWS extends client_1.ADNLClient { constructor(url, peerPublicKey) { super(null, url, peerPublicKey); this.url = url; } async parse(message) { let data; switch (true) { case typeof message === 'string': data = Buffer.from(message); break; case message instanceof Buffer: data = message; break; case message instanceof ArrayBuffer: data = Buffer.from(message); break; default: const blob = message; data = Buffer.from(await blob.arrayBuffer()); break; } return data; } onHandshake() { this.socket.send(this.handshake); } async connect() { await this.onBeforeConnect(); this.socket = new isomorphic_ws_1.default(this.url); this.socket.onopen = () => { this.onConnect(); this.onHandshake(); }; this.socket.onmessage = async (event) => { const data = await this.parse(event.data); this.onData(data); }; this.socket.onclose = this.onClose.bind(this); this.socket.onerror = this.onError.bind(this); } end() { if (this.state === client_1.ADNLClientState.CLOSING || this.state === client_1.ADNLClientState.CLOSED) { return undefined; } this.socket.destroySoon(); } write(data) { const packet = new packet_1.ADNLPacket(Buffer.from(data)); const encrypted = this.encrypt(packet.data); this.socket.send(encrypted); } } exports.ADNLClientWS = ADNLClientWS; //# sourceMappingURL=client-ws.js.map