UNPKG

intern

Version:

Intern. A next-generation code testing stack for JavaScript.

88 lines 3.97 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "tslib", "@theintern/common", "./Base", "../browser/util", "../common/util"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var common_1 = require("@theintern/common"); var Base_1 = tslib_1.__importDefault(require("./Base")); var util_1 = require("../browser/util"); var util_2 = require("../common/util"); var WebSocketChannel = (function (_super) { tslib_1.__extends(WebSocketChannel, _super); function WebSocketChannel(options) { var _this = _super.call(this, options) || this; _this.timeout = 10000; if (options.timeout !== undefined) { _this.timeout = options.timeout; } if (!options.port) { throw new Error('A port is required for a WebSocket channel'); } var url = util_1.parseUrl(options.url); var host = url.hostname; var protocol = url.protocol === 'https' ? 'wss' : 'ws'; _this._socket = new common_1.global.WebSocket(protocol + "://" + host + ":" + options.port); _this._ready = new common_1.Task(function (resolve, reject) { _this._socket.addEventListener('open', resolve); _this._socket.addEventListener('error', reject); }); _this._socket.addEventListener('message', function (event) { _this._handleMessage(JSON.parse(event.data)); }); _this._socket.addEventListener('error', function (_event) { _this._handleError(new Error('WebSocket error')); }); _this._sendQueue = Object.create(null); _this._sequence = 1; return _this; } WebSocketChannel.prototype._sendData = function (name, data) { var _this = this; return this._ready.then(function () { return new common_1.Task(function (resolve, reject) { var id = String(_this._sequence++); var sessionId = _this.sessionId; var message = { id: id, sessionId: sessionId, name: name, data: data }; _this._socket.send(util_2.stringify(message)); var timer = setTimeout(function () { reject(new Error('Send timed out')); }, _this.timeout); _this._sendQueue[id] = { resolve: function (data) { clearTimeout(timer); resolve(data); }, reject: function (error) { reject(error); }, }; }); }); }; WebSocketChannel.prototype._handleMessage = function (message) { var id = message.id; this._sendQueue[id].resolve(message); this._sendQueue[id] = undefined; }; WebSocketChannel.prototype._handleError = function (error) { var _this = this; this._ready = common_1.Task.reject(error); Object.keys(this._sendQueue) .filter(function (id) { return _this._sendQueue[id] != null; }) .forEach(function (id) { _this._sendQueue[id].reject(error); _this._sendQueue[id] = undefined; }); }; return WebSocketChannel; }(Base_1.default)); exports.default = WebSocketChannel; }); //# sourceMappingURL=WebSocket.js.map