UNPKG

@litert/televoke

Version:
98 lines 3.35 kB
"use strict"; /** * Copyright 2025 Angus.Fenying <fenying@litert.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.WebSocketTransporter = void 0; const LibWS = require("@litert/websocket"); const Shared = require("../../shared"); const node_events_1 = require("node:events"); const PROPERTY_NAMES = ['remoteAddress', 'remotePort', 'localAddress', 'localPort', 'peerCertificate']; class WebSocketTransporter extends node_events_1.EventEmitter { constructor(protocol, _conn) { super(); this.protocol = protocol; this._conn = _conn; this._conn .on('end', () => this.emit('end')) .on('finish', () => this.emit('finish')) .on('message', (frame) => { switch (frame.opcode) { case LibWS.EOpcode.BINARY: this.emit('frame', frame.data); break; case LibWS.EOpcode.CLOSE: this.end(); break; case LibWS.EOpcode.PING: this._conn.pong(Buffer.concat(frame.data)); break; default: this.emit('error', new Shared.errors.network_error({ reason: 'invalid_frame' })); this.destroy(); } }) .on('error', (e) => this.emit('error', e)) .on('close', () => this.emit('close')); } destroy() { this._conn.destroy(); } getPropertyNames() { return PROPERTY_NAMES; } getAllProperties() { return { 'remoteAddress': this._conn.remoteAddress, 'remotePort': this._conn.remotePort, 'localAddress': this._conn.localAddress, 'localPort': this._conn.localPort, 'peerCertificate': this._conn.peerCertificate, }; } getProperty(name) { switch (name) { case 'localPort': case 'localAddress': case 'remotePort': case 'remoteAddress': case 'peerCertificate': return this._conn[name]; default: return undefined; } } get writable() { return this._conn.connected; } write(frame) { if (!this._conn.writable) { throw new Shared.errors.network_error({ reason: 'conn_lost' }); } try { this._conn.writeBinary(frame); } catch (e) { throw new Shared.errors.network_error({ reason: 'conn_lost', cause: e }); } } end() { if (this._conn.writable) { this._conn.end(); } } } exports.WebSocketTransporter = WebSocketTransporter; //# sourceMappingURL=WebSocket.Transporter.js.map