UNPKG

@phnq/message

Version:

Asynchronous, incremental messaging client and server

351 lines (350 loc) 18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const http_1 = __importDefault(require("http")); const MessageConnection_1 = require("../MessageConnection"); const WebSocketMessageClient_1 = require("../WebSocketMessageClient"); const WebSocketMessageServer_1 = require("../WebSocketMessageServer"); // const wait = (millis: number = 0): Promise<void> => // new Promise(resolve => { // setTimeout(resolve, millis); // }); describe('MessageConnection', () => { describe('with WebSocketTransport', () => { it('should handle multiple responses with an iterator', () => __awaiter(void 0, void 0, void 0, function* () { var _a, e_1, _b, _c, _d, e_2, _e, _f; const httpServer = http_1.default.createServer(); yield new Promise((resolve) => { httpServer.listen({ port: 55556 }, resolve); }); const wsms = new WebSocketMessageServer_1.WebSocketMessageServer({ httpServer }); wsms.onReceive = (connection, message) => __awaiter(void 0, void 0, void 0, function* () { return (function () { return __asyncGenerator(this, arguments, function* () { expect(message).toBe('knock knock'); expect(connection).toBeInstanceOf(MessageConnection_1.MessageConnection); yield yield __await("who's"); yield yield __await('there'); yield yield __await('?'); }); })(); }); const clientConnection = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556'); const resps1 = []; try { for (var _g = true, _h = __asyncValues(yield clientConnection.requestMulti('knock knock')), _j; _j = yield _h.next(), _a = _j.done, !_a;) { _c = _j.value; _g = false; try { const resp = _c; resps1.push(resp); } finally { _g = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_g && !_a && (_b = _h.return)) yield _b.call(_h); } finally { if (e_1) throw e_1.error; } } expect(resps1).toEqual(["who's", 'there', '?']); const resps2 = []; try { for (var _k = true, _l = __asyncValues(yield clientConnection.requestMulti('knock knock')), _m; _m = yield _l.next(), _d = _m.done, !_d;) { _f = _m.value; _k = false; try { const resp = _f; resps2.push(resp); } finally { _k = true; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_k && !_d && (_e = _l.return)) yield _e.call(_l); } finally { if (e_2) throw e_2.error; } } expect(resps2).toEqual(["who's", 'there', '?']); yield clientConnection.close(); yield wsms.close(); yield new Promise((resolve) => { httpServer.close(resolve); }); })); it('should handle multiple simultaneous lazy connection initiations', () => __awaiter(void 0, void 0, void 0, function* () { const httpServer = http_1.default.createServer(); yield new Promise((resolve) => { httpServer.listen({ port: 55556 }, resolve); }); const wsms = new WebSocketMessageServer_1.WebSocketMessageServer({ httpServer, path: '/simultaneous-path' }); wsms.onReceive = (_, message) => __awaiter(void 0, void 0, void 0, function* () { return `you said ${message}`; }); const clientConnection = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556/simultaneous-path'); const [r1, r2, r3] = yield Promise.all([ clientConnection.request('one'), clientConnection.request('two'), clientConnection.request('three'), ]); expect(r1).toBe('you said one'); expect(r2).toBe('you said two'); expect(r3).toBe('you said three'); yield clientConnection.close(); yield wsms.close(); yield new Promise((resolve) => { httpServer.close(resolve); }); })); it('should handle push messages from the server', () => __awaiter(void 0, void 0, void 0, function* () { const httpServer = http_1.default.createServer(); yield new Promise((resolve) => { httpServer.listen({ port: 55556 }, resolve); }); const wsms = new WebSocketMessageServer_1.WebSocketMessageServer({ httpServer, path: '/simultaneous-path' }); wsms.onReceive = (_, message) => __awaiter(void 0, void 0, void 0, function* () { if (message === 'startPushing') { yield wsms.connections[0].send('one'); yield wsms.connections[0].send('two'); yield wsms.connections[0].send('three'); return 'pushed'; } return ''; }); const clientConnection = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556/simultaneous-path'); const receivedMessages1 = []; const receivedMessages2 = []; clientConnection.addReceiveHandler((message) => __awaiter(void 0, void 0, void 0, function* () { receivedMessages1.push(message); })); clientConnection.addReceiveHandler((message) => __awaiter(void 0, void 0, void 0, function* () { receivedMessages2.push(message); })); yield clientConnection.request('startPushing'); expect(receivedMessages1).toEqual(['one', 'two', 'three']); expect(receivedMessages2).toEqual(['one', 'two', 'three']); yield clientConnection.close(); yield wsms.close(); yield new Promise((resolve) => { httpServer.close(resolve); }); })); it('should close the socket if the wrong path is specified', () => __awaiter(void 0, void 0, void 0, function* () { const httpServer = http_1.default.createServer(); yield new Promise((resolve) => { httpServer.listen({ port: 55556 }, resolve); }); const wsms = new WebSocketMessageServer_1.WebSocketMessageServer({ httpServer, path: '/the-path' }); wsms.onReceive = (_, message) => __awaiter(void 0, void 0, void 0, function* () { return `you said ${message}`; }); const clientConnection = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556/the-wrong-path'); try { yield clientConnection.request('hello'); fail('Should have thrown'); } catch (err) { expect(err.message).toBe('Socket closed by server (unsupported path: /the-wrong-path)'); } expect(clientConnection.isOpen()).toBe(false); yield clientConnection.close(); yield wsms.close(); yield new Promise((resolve) => { httpServer.close(resolve); }); })); it('should throw an error if the client cannot connect to the server', () => __awaiter(void 0, void 0, void 0, function* () { try { const clientConnection = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:59999/some-path'); yield clientConnection.request('hello'); fail('Should have thrown'); } catch (err) { expect(err.message).toContain('Socket error (ws://localhost:59999/some-path)'); } })); it('should re-open the socket on the next request after being closed', () => __awaiter(void 0, void 0, void 0, function* () { var _o, e_3, _p, _q, _r, e_4, _s, _t; const httpServer = http_1.default.createServer(); yield new Promise((resolve) => { httpServer.listen({ port: 55556 }, resolve); }); const wsms = new WebSocketMessageServer_1.WebSocketMessageServer({ httpServer }); wsms.onReceive = (connection, message) => __awaiter(void 0, void 0, void 0, function* () { return (function () { return __asyncGenerator(this, arguments, function* () { expect(message).toBe('knock knock'); expect(connection).toBeInstanceOf(MessageConnection_1.MessageConnection); yield yield __await("who's"); yield yield __await('there'); yield yield __await('?'); }); })(); }); const clientConnection = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556'); const resps1 = []; try { for (var _u = true, _v = __asyncValues(yield clientConnection.requestMulti('knock knock')), _w; _w = yield _v.next(), _o = _w.done, !_o;) { _q = _w.value; _u = false; try { const resp = _q; resps1.push(resp); } finally { _u = true; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_u && !_o && (_p = _v.return)) yield _p.call(_v); } finally { if (e_3) throw e_3.error; } } expect(resps1).toEqual(["who's", 'there', '?']); yield clientConnection.close(); const resps2 = []; try { for (var _x = true, _y = __asyncValues(yield clientConnection.requestMulti('knock knock')), _z; _z = yield _y.next(), _r = _z.done, !_r;) { _t = _z.value; _x = false; try { const resp = _t; resps2.push(resp); } finally { _x = true; } } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (!_x && !_r && (_s = _y.return)) yield _s.call(_y); } finally { if (e_4) throw e_4.error; } } expect(resps2).toEqual(["who's", 'there', '?']); yield clientConnection.close(); yield wsms.close(); yield new Promise((resolve) => { httpServer.close(resolve); }); })); it('should share client connections for the same url', () => __awaiter(void 0, void 0, void 0, function* () { const httpServer = http_1.default.createServer(); yield new Promise((resolve) => { httpServer.listen({ port: 55556 }, resolve); }); const wsms = new WebSocketMessageServer_1.WebSocketMessageServer({ httpServer }); wsms.onReceive = (connection, message) => __awaiter(void 0, void 0, void 0, function* () { return (function () { return __asyncGenerator(this, arguments, function* () { expect(message).toBe('knock knock'); expect(connection).toBeInstanceOf(MessageConnection_1.MessageConnection); yield yield __await("who's"); yield yield __await('there'); yield yield __await('?'); }); })(); }); const clientConnection1 = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556'); const clientConnection2 = WebSocketMessageClient_1.WebSocketMessageClient.create('ws://localhost:55556'); expect(clientConnection1 === clientConnection2).toBe(true); yield Promise.all([ (() => __awaiter(void 0, void 0, void 0, function* () { var _0, e_5, _1, _2; const resps1 = []; try { for (var _3 = true, _4 = __asyncValues(yield clientConnection1.requestMulti('knock knock')), _5; _5 = yield _4.next(), _0 = _5.done, !_0;) { _2 = _5.value; _3 = false; try { const resp = _2; resps1.push(resp); } finally { _3 = true; } } } catch (e_5_1) { e_5 = { error: e_5_1 }; } finally { try { if (!_3 && !_0 && (_1 = _4.return)) yield _1.call(_4); } finally { if (e_5) throw e_5.error; } } expect(resps1).toEqual(["who's", 'there', '?']); }))(), (() => __awaiter(void 0, void 0, void 0, function* () { var _6, e_6, _7, _8; const resps2 = []; try { for (var _9 = true, _10 = __asyncValues(yield clientConnection2.requestMulti('knock knock')), _11; _11 = yield _10.next(), _6 = _11.done, !_6;) { _8 = _11.value; _9 = false; try { const resp = _8; resps2.push(resp); } finally { _9 = true; } } } catch (e_6_1) { e_6 = { error: e_6_1 }; } finally { try { if (!_9 && !_6 && (_7 = _10.return)) yield _7.call(_10); } finally { if (e_6) throw e_6.error; } } expect(resps2).toEqual(["who's", 'there', '?']); }))(), ]); yield clientConnection1.close(); yield clientConnection2.close(); yield wsms.close(); yield new Promise((resolve) => { httpServer.close(resolve); }); })); }); });