UNPKG

nxkit

Version:

This is a collection of tools, independent of any other libraries

129 lines (128 loc) 4.89 kB
"use strict"; /* ***** BEGIN LICENSE BLOCK ***** * Distributed under the BSD license: * * Copyright (c) 2015, xuewen.chu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of xuewen.chu nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL xuewen.chu BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ***** END LICENSE BLOCK ***** */ Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../../util"); const buffer_1 = require("../../buffer"); const errno_1 = require("../../errno"); const data_1 = require("../data"); const conv_1 = require("./conv"); const WebSocket = globalThis.WebSocket; // Web implementation class WebConversation extends conv_1.WSConversation { setGzip(value) { // web disable gzip } /** * @ovrewrite */ async initialize() { util_1.default.assert(!this.m_req, 'No need to repeat open'); var self = this; var url = this.m_url; var bind_services = Object.keys(this.m_handles).join(','); var headers = Object.create(this.getRequestHeaders()); if (this.m_signer) { Object.assign(headers, await this.m_signer.sign(url.path)); } url.setParam('_headers', JSON.stringify(headers)); url.setParam('bind_services', bind_services); var req = this.m_req = new WebSocket(url.href); req.onopen = function (e) { console.log('CLI WebConversation Upgrade', self.m_url.href); if (!self.m_connecting) { self.close(); return; } // self.m_token = res.headers['session-token'] || ''; req.onmessage = function (e) { var data = e.data; if (data instanceof ArrayBuffer) { self.handlePacket(buffer_1.default.from(data), false); } else if (data instanceof Blob && data.arrayBuffer) { // Compatible with old browser data.arrayBuffer().then((e) => self.handlePacket(buffer_1.default.from(e), false)); } else { // string self.handlePacket(data, true); } }; req.onclose = function (e) { self.close(); }; self._open(); }; req.binaryType = 'arraybuffer'; req.onerror = function (e) { console.log('CLI WebConversation error', self.m_url.href); self._error(Error.new(e)); self.close(); }; console.log('CLI WebConversation init', self.m_url.href, self.m_connecting); } /** * @ovrewrite */ close() { var req = this.m_req; if (req) { this.m_req = null; try { req.close(); } catch (err) { console.error(err); } } super.close(); } /** * @ovrewrite */ async send(data) { util_1.default.assert(this.isOpen, errno_1.default.ERR_CONNECTION_CLOSE_STATUS); this.m_req.send(data.buffer); } /** * @ovrewrite */ async ping() { util_1.default.assert(this.isOpen, errno_1.default.ERR_CONNECTION_CLOSE_STATUS); this.m_req.send(data_1.PING_BUFFER); } /** * @ovrewrite */ async pong() { util_1.default.assert(this.isOpen, errno_1.default.ERR_CONNECTION_CLOSE_STATUS); this.m_req.send(data_1.PONG_BUFFER); } } exports.default = WebConversation;