@alphapoint/apex-web 5.7.0 → 5.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/lib/apex.integration.test.js +435 -0
  2. package/lib/apex.js +37 -1
  3. package/lib/apex.test.js +138 -0
  4. package/lib/components/2FA/PasskeyOption.js +126 -0
  5. package/lib/components/2FA/TwoFactorAuthComponent.js +24 -5
  6. package/lib/components/2FA/TwoFactorAuthComponent.test.js +94 -0
  7. package/lib/components/2FA/TwoFactorAuthContainer.js +94 -16
  8. package/lib/components/2FA/TwoFactorAuthContainer.test.js +412 -0
  9. package/lib/components/ConfirmBlockTradeModal/ConfirmBlockTradeModalContainer.js +34 -6
  10. package/lib/components/ConfirmBlockTradeModal/ConfirmBlockTradeModalContainer.test.js +298 -0
  11. package/lib/components/ConfirmOrderModal/ConfirmOrderModalComponent.js +26 -4
  12. package/lib/components/ConfirmOrderModal/ConfirmOrderModalContainer.js +4 -1
  13. package/lib/components/EnableTwoFactorAuth/Disable2FAForm.js +22 -7
  14. package/lib/components/EnableTwoFactorAuth/Disable2FAModal.js +15 -5
  15. package/lib/components/EnableTwoFactorAuth/Disable2FAModal.test.js +460 -0
  16. package/lib/components/EnableTwoFactorAuth/Disable2FAModalContainer.js +108 -16
  17. package/lib/components/EnableTwoFactorAuth/EnableTwoFactorAuthContainerFactory.js +3 -2
  18. package/lib/components/FusionAuth/UnlinkOAuthIdentityModal.css +13 -0
  19. package/lib/components/FusionAuth/UnlinkOAuthIdentityModal.js +246 -0
  20. package/lib/components/FusionAuth/UnlinkOAuthIdentityModal.test.js +521 -0
  21. package/lib/components/Login/LoginFormComponent.js +1 -1
  22. package/lib/components/OrderEntryComponents/advancedForm/AdvancedOrderFormContainer.js +2 -0
  23. package/lib/components/OrderEntryComponents/common/FormAmountFields.js +5 -0
  24. package/lib/components/OrderEntryComponents/common/TwapScheduleFields.js +98 -0
  25. package/lib/components/OrderEntryComponents/defaultForm/OrderEntryAnchoring.js +86 -39
  26. package/lib/components/OrderEntryComponents/defaultForm/OrderEntryAnchoring.test.js +326 -0
  27. package/lib/components/OrderEntryComponents/defaultForm/OrderEntryContainer.js +10 -2
  28. package/lib/components/OrderHistoryComponents/DepositsStatus/DepositsStatusComponent.js +1 -1
  29. package/lib/components/OrderHistoryComponents/DepositsStatus/DepositsStatusComponent.test.js +99 -0
  30. package/lib/components/OrderHistoryComponents/InactiveOrders/InactiveOrdersComponent.js +1 -1
  31. package/lib/components/OrderHistoryComponents/OpenOrders/OpenOrdersComponent.js +1 -1
  32. package/lib/components/OrderHistoryComponents/WithdrawStatus/WithdrawStatusComponent.js +1 -1
  33. package/lib/components/OrderHistoryComponents/WithdrawStatus/WithdrawStatusComponent.test.js +113 -0
  34. package/lib/components/Settings/ProfileAndSecurity/PasskeysSettingsCard/PasskeysSettingsCard.css +108 -0
  35. package/lib/components/Settings/ProfileAndSecurity/PasskeysSettingsCard/PasskeysSettingsCard.test.js +983 -0
  36. package/lib/components/Settings/ProfileAndSecurity/PasskeysSettingsCard/PasskeysSettingsCardComponent.js +500 -0
  37. package/lib/components/Settings/ProfileAndSecurity/PasskeysSettingsCard/index.js +9 -0
  38. package/lib/components/Settings/ProfileAndSecurity/ProfileSettingsCard/ProfileForm/SsoLinkRowComponent.css +4 -0
  39. package/lib/components/Settings/ProfileAndSecurity/ProfileSettingsCard/ProfileForm/SsoLinkRowComponent.js +29 -17
  40. package/lib/components/Settings/ProfileAndSecurity/ProfileSettingsCard/ProfileForm/SsoLinkRowComponent.test.js +135 -0
  41. package/lib/components/Settings/ProfileAndSecurity/TwoFactorAuthSettingsCard/EnableTwoFactorAuth/EnableTwoFactorAuthComponent.test.js +23 -1
  42. package/lib/components/TwoFactorAuthForm/TwoFactorAuthForm.test.js +475 -0
  43. package/lib/components/TwoFactorAuthForm/TwoFactorAuthFormComponent.js +26 -5
  44. package/lib/components/TwoFactorAuthForm/TwoFactorAuthFormContainer.js +50 -32
  45. package/lib/components/common/APAutoTabInput/APAutoTabInput.js +5 -2
  46. package/lib/components/common/APSelect/APSelect.js +3 -1
  47. package/lib/config/CONFIG.md +4 -0
  48. package/lib/config/configDefault.json +2 -1
  49. package/lib/constants/sendOrder/orderEntryFormConstants.js +23 -4
  50. package/lib/constants/sendOrder/timeInForceConstants.js +3 -1
  51. package/lib/constants/sendOrder/twapRejectReasons.js +26 -0
  52. package/lib/helpers/orderHistoryHelper.js +13 -1
  53. package/lib/helpers/orderHistoryHelper.test.js +58 -13
  54. package/lib/helpers/passkeyApi.js +228 -0
  55. package/lib/helpers/passkeyApi.test.js +171 -0
  56. package/lib/helpers/placeOrderHelper.js +76 -61
  57. package/lib/helpers/placeOrderHelper.test.js +169 -10
  58. package/lib/helpers/recalculateOrderHelper.js +4 -1
  59. package/lib/helpers/recalculateOrderHelper.test.js +88 -2
  60. package/lib/helpers/twapOrderHelper.js +265 -0
  61. package/lib/helpers/twapOrderHelper.test.js +467 -0
  62. package/lib/helpers/webauthnHelper.js +300 -0
  63. package/lib/helpers/webauthnHelper.test.js +199 -0
  64. package/lib/hocs/withOneSubmission.js +168 -0
  65. package/lib/redux/actions/apiActions.js +52 -1
  66. package/lib/redux/actions/apiActions.test.js +135 -0
  67. package/lib/redux/actions/authActions.js +514 -198
  68. package/lib/redux/actions/authActions.test.js +627 -47
  69. package/lib/redux/actions/depositActions.js +1 -1
  70. package/lib/redux/actions/depositActions.test.js +100 -30
  71. package/lib/redux/actions/fusionAuthActions.test.js +265 -84
  72. package/lib/redux/actions/modalActions.js +1 -0
  73. package/lib/redux/actions/orderEntryActions.blockTrade.test.js +306 -0
  74. package/lib/redux/actions/orderEntryActions.js +74 -22
  75. package/lib/redux/actions/orderEntryActions.test.js +63 -11
  76. package/lib/redux/actions/orderHistoryActions.js +9 -4
  77. package/lib/redux/actions/orderHistoryActions.test.js +63 -0
  78. package/lib/redux/actions/userActions.js +314 -177
  79. package/lib/redux/actions/userActions.test.js +844 -268
  80. package/lib/redux/margin/actions/marginActions.js +6 -3
  81. package/lib/redux/margin/actions/marginActions.test.js +166 -0
  82. package/lib/redux/middlewares/authMiddleware.js +5 -0
  83. package/lib/redux/middlewares/authMiddleware.test.js +88 -29
  84. package/lib/redux/reducers/authReducer.js +1 -0
  85. package/lib/redux/reducers/authReducer.test.js +38 -0
  86. package/lib/redux/selectors/orderEntrySelector.js +19 -8
  87. package/lib/redux/selectors/orderEntrySelector.test.js +209 -0
  88. package/lib/redux/selectors/orderTotalSelector.test.js +40 -1
  89. package/lib/services/fms/fms.js +2 -0
  90. package/lib/services/fms/fms.test.js +35 -0
  91. package/package.json +4 -4
@@ -0,0 +1,435 @@
1
+ "use strict";
2
+
3
+ var _alphapointApexApi = require("alphapoint-apex-api");
4
+ var _universalWebsocketClient = _interopRequireDefault(require("universal-websocket-client"));
5
+ var _apex = require("./apex");
6
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
7
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8
+ function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
9
+ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
10
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
11
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
12
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
13
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
14
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
15
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
16
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /**
17
+ * Integration spec: the real alphapoint-apex-api library over a fake socket.
18
+ *
19
+ * Nothing from the library is mocked. Only the transport is replaced: the
20
+ * library builds its WebSocketSubject with `WebSocketCtor: require('universal-
21
+ * websocket-client')`, which under jest resolves to the Node entry (`ws`), so
22
+ * that module is swapped for an in-memory WebSocket class the test can drive.
23
+ * (A `global.WebSocket` stub is never consulted here; jest 27 ignores the
24
+ * package's `browser` field, so the library never reads the global.)
25
+ */
26
+ jest.mock('universal-websocket-client', function () {
27
+ var MockWebSocket = /*#__PURE__*/function () {
28
+ function MockWebSocket(url) {
29
+ _classCallCheck(this, MockWebSocket);
30
+ this.url = url;
31
+ this.readyState = 0; // CONNECTING
32
+ this.sent = [];
33
+ this.closed = false;
34
+ MockWebSocket.instances.push(this);
35
+ }
36
+ return _createClass(MockWebSocket, [{
37
+ key: "send",
38
+ value: function send(data) {
39
+ this.sent.push(data);
40
+ }
41
+ }, {
42
+ key: "close",
43
+ value: function close() {
44
+ this.closed = true;
45
+ this.readyState = 3; // CLOSED
46
+ }
47
+
48
+ // Test helpers: drive the socket like a server would.
49
+ }, {
50
+ key: "open",
51
+ value: function open() {
52
+ this.readyState = 1; // OPEN
53
+ if (this.onopen) this.onopen({
54
+ type: 'open'
55
+ });
56
+ }
57
+ }, {
58
+ key: "reply",
59
+ value: function reply(frame) {
60
+ this.onmessage({
61
+ data: JSON.stringify(frame)
62
+ });
63
+ }
64
+ }, {
65
+ key: "lastSentFrame",
66
+ value: function lastSentFrame() {
67
+ return JSON.parse(this.sent[this.sent.length - 1]);
68
+ }
69
+ }]);
70
+ }();
71
+ MockWebSocket.instances = [];
72
+ return MockWebSocket;
73
+ });
74
+ var GATEWAY = 'wss://fake.example/WSGateway';
75
+ var NOT_FOUND = 'Endpoint Not Found';
76
+ var delay = function delay(ms) {
77
+ return new Promise(function (resolve) {
78
+ return setTimeout(resolve, ms);
79
+ });
80
+ };
81
+
82
+ // 'pending' if the promise has not settled within `ms`, else 'settled'.
83
+ var settlesWithin = function settlesWithin(promise, ms) {
84
+ return Promise.race([promise.then(function () {
85
+ return 'settled';
86
+ }, function () {
87
+ return 'settled';
88
+ }), delay(ms).then(function () {
89
+ return 'pending';
90
+ })]);
91
+ };
92
+ var latestSocket = function latestSocket() {
93
+ return _universalWebsocketClient["default"].instances[_universalWebsocketClient["default"].instances.length - 1];
94
+ };
95
+ beforeEach(function () {
96
+ _universalWebsocketClient["default"].instances.length = 0;
97
+ localStorage.clear();
98
+ });
99
+ describe('alphapoint-apex-api 3.2.19 over a fake socket', function () {
100
+ it('opens the socket through the mocked transport and sends the RPC frame', function () {
101
+ var connection = new _alphapointApexApi.APEX(GATEWAY, {
102
+ onopen: function onopen() {},
103
+ onclose: function onclose() {}
104
+ });
105
+ var socket = latestSocket();
106
+ expect(_universalWebsocketClient["default"].instances).toHaveLength(1);
107
+ expect(socket.url).toBe(GATEWAY);
108
+ expect(connection.ws.socket).toBe(socket);
109
+ socket.open();
110
+ connection.RPCPromise('GetUserInfo', {
111
+ UserId: 1
112
+ });
113
+ expect(socket.sent).toHaveLength(1);
114
+ expect(socket.lastSentFrame()).toEqual({
115
+ m: 0,
116
+ i: 0,
117
+ n: 'GetUserInfo',
118
+ o: JSON.stringify({
119
+ UserId: 1
120
+ })
121
+ });
122
+ });
123
+ it('buffers RPC frames sent before the socket is open and flushes them on open', function () {
124
+ var connection = new _alphapointApexApi.APEX(GATEWAY, {
125
+ onopen: function onopen() {},
126
+ onclose: function onclose() {}
127
+ });
128
+ var socket = latestSocket();
129
+ connection.RPCPromise('GetUserInfo', {
130
+ UserId: 1
131
+ });
132
+ expect(socket.sent).toHaveLength(0);
133
+ socket.open();
134
+ expect(socket.sent).toHaveLength(1);
135
+ expect(socket.lastSentFrame().n).toBe('GetUserInfo');
136
+ });
137
+ });
138
+ describe('the bug: unpatched library drops m:5 error replies', function () {
139
+ it('leaves RPCPromise pending forever after an m:5 "Endpoint Not Found" reply', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
140
+ var connection, socket, promise, _socket$lastSentFrame, i;
141
+ return _regenerator().w(function (_context) {
142
+ while (1) switch (_context.n) {
143
+ case 0:
144
+ connection = new _alphapointApexApi.APEX(GATEWAY, {
145
+ onopen: function onopen() {},
146
+ onclose: function onclose() {}
147
+ });
148
+ socket = latestSocket();
149
+ socket.open();
150
+ promise = connection.RPCPromise('GetUserInfo', {
151
+ UserId: 1
152
+ });
153
+ _socket$lastSentFrame = socket.lastSentFrame(), i = _socket$lastSentFrame.i;
154
+ expect(connection.callbacks[i]).toEqual(expect.any(Function));
155
+ socket.reply({
156
+ m: 5,
157
+ i: i,
158
+ n: 'GetUserInfo',
159
+ o: NOT_FOUND
160
+ });
161
+ _context.n = 1;
162
+ return expect(settlesWithin(promise, 50)).resolves.toBe('pending');
163
+ case 1:
164
+ // The callback is still registered: the library never routed the frame.
165
+ expect(connection.callbacks[i]).toEqual(expect.any(Function));
166
+ case 2:
167
+ return _context.a(2);
168
+ }
169
+ }, _callee);
170
+ })));
171
+ it('leaves a generated endpoint method pending forever after an m:5 reply', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
172
+ var connection, socket, promise, _socket$lastSentFrame2, i;
173
+ return _regenerator().w(function (_context2) {
174
+ while (1) switch (_context2.n) {
175
+ case 0:
176
+ connection = new _alphapointApexApi.APEX(GATEWAY, {
177
+ onopen: function onopen() {},
178
+ onclose: function onclose() {}
179
+ });
180
+ socket = latestSocket();
181
+ socket.open();
182
+ expect(_typeof(connection.GetUserInfo)).toBe('function');
183
+ promise = connection.GetUserInfo({
184
+ UserId: 1
185
+ });
186
+ _socket$lastSentFrame2 = socket.lastSentFrame(), i = _socket$lastSentFrame2.i;
187
+ socket.reply({
188
+ m: 5,
189
+ i: i,
190
+ n: 'GetUserInfo',
191
+ o: NOT_FOUND
192
+ });
193
+ _context2.n = 1;
194
+ return expect(settlesWithin(promise, 50)).resolves.toBe('pending');
195
+ case 1:
196
+ return _context2.a(2);
197
+ }
198
+ }, _callee2);
199
+ })));
200
+ it('still resolves RPCPromise on an m:1 reply (the only path the library handles)', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
201
+ var connection, socket, promise, _socket$lastSentFrame3, i, reply;
202
+ return _regenerator().w(function (_context3) {
203
+ while (1) switch (_context3.n) {
204
+ case 0:
205
+ connection = new _alphapointApexApi.APEX(GATEWAY, {
206
+ onopen: function onopen() {},
207
+ onclose: function onclose() {}
208
+ });
209
+ socket = latestSocket();
210
+ socket.open();
211
+ promise = connection.RPCPromise('GetUserInfo', {
212
+ UserId: 1
213
+ });
214
+ _socket$lastSentFrame3 = socket.lastSentFrame(), i = _socket$lastSentFrame3.i;
215
+ reply = {
216
+ m: 1,
217
+ i: i,
218
+ n: 'GetUserInfo',
219
+ o: JSON.stringify({
220
+ UserId: 1
221
+ })
222
+ };
223
+ socket.reply(reply);
224
+ _context3.n = 1;
225
+ return expect(promise).resolves.toEqual(reply);
226
+ case 1:
227
+ expect(connection.callbacks[i]).toBeUndefined();
228
+ case 2:
229
+ return _context3.a(2);
230
+ }
231
+ }, _callee3);
232
+ })));
233
+ });
234
+ describe('the fix: routeErrorReplies on the real connection', function () {
235
+ var openPatchedConnection = function openPatchedConnection() {
236
+ (0, _apex.setGateway)(GATEWAY);
237
+ var _getApex = (0, _apex.getApex)(),
238
+ connection = _getApex.connection;
239
+ var socket = latestSocket();
240
+ expect(connection.ws.socket).toBe(socket);
241
+ socket.open();
242
+ return {
243
+ connection: connection,
244
+ socket: socket
245
+ };
246
+ };
247
+ it('rejects RPCPromise with the m:5 frame', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
248
+ var _openPatchedConnectio, connection, socket, promise, _socket$lastSentFrame4, i, errorFrame;
249
+ return _regenerator().w(function (_context4) {
250
+ while (1) switch (_context4.n) {
251
+ case 0:
252
+ _openPatchedConnectio = openPatchedConnection(), connection = _openPatchedConnectio.connection, socket = _openPatchedConnectio.socket;
253
+ promise = connection.RPCPromise('GetUserInfo', {
254
+ UserId: 1
255
+ });
256
+ _socket$lastSentFrame4 = socket.lastSentFrame(), i = _socket$lastSentFrame4.i;
257
+ errorFrame = {
258
+ m: 5,
259
+ i: i,
260
+ n: 'GetUserInfo',
261
+ o: NOT_FOUND
262
+ };
263
+ socket.reply(errorFrame);
264
+ _context4.n = 1;
265
+ return expect(promise).rejects.toEqual(errorFrame);
266
+ case 1:
267
+ expect(connection.callbacks[i]).toBeUndefined();
268
+ case 2:
269
+ return _context4.a(2);
270
+ }
271
+ }, _callee4);
272
+ })));
273
+ it('rejects a generated endpoint method with the m:5 frame', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
274
+ var _openPatchedConnectio2, connection, socket, promise, _socket$lastSentFrame5, i, errorFrame;
275
+ return _regenerator().w(function (_context5) {
276
+ while (1) switch (_context5.n) {
277
+ case 0:
278
+ _openPatchedConnectio2 = openPatchedConnection(), connection = _openPatchedConnectio2.connection, socket = _openPatchedConnectio2.socket;
279
+ promise = connection.GetUserInfo({
280
+ UserId: 1
281
+ });
282
+ _socket$lastSentFrame5 = socket.lastSentFrame(), i = _socket$lastSentFrame5.i;
283
+ errorFrame = {
284
+ m: 5,
285
+ i: i,
286
+ n: 'GetUserInfo',
287
+ o: NOT_FOUND
288
+ };
289
+ socket.reply(errorFrame);
290
+ _context5.n = 1;
291
+ return expect(promise).rejects.toEqual(errorFrame);
292
+ case 1:
293
+ expect(connection.callbacks[i]).toBeUndefined();
294
+ case 2:
295
+ return _context5.a(2);
296
+ }
297
+ }, _callee5);
298
+ })));
299
+ it('still resolves RPCPromise on an m:1 reply', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6() {
300
+ var _openPatchedConnectio3, connection, socket, promise, _socket$lastSentFrame6, i, reply;
301
+ return _regenerator().w(function (_context6) {
302
+ while (1) switch (_context6.n) {
303
+ case 0:
304
+ _openPatchedConnectio3 = openPatchedConnection(), connection = _openPatchedConnectio3.connection, socket = _openPatchedConnectio3.socket;
305
+ promise = connection.RPCPromise('GetUserInfo', {
306
+ UserId: 1
307
+ });
308
+ _socket$lastSentFrame6 = socket.lastSentFrame(), i = _socket$lastSentFrame6.i;
309
+ reply = {
310
+ m: 1,
311
+ i: i,
312
+ n: 'GetUserInfo',
313
+ o: JSON.stringify({
314
+ UserId: 1
315
+ })
316
+ };
317
+ socket.reply(reply);
318
+ _context6.n = 1;
319
+ return expect(promise).resolves.toEqual(reply);
320
+ case 1:
321
+ expect(connection.callbacks[i]).toBeUndefined();
322
+ case 2:
323
+ return _context6.a(2);
324
+ }
325
+ }, _callee6);
326
+ })));
327
+ it('still resolves a generated endpoint method with the parsed payload on m:1', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
328
+ var _openPatchedConnectio4, connection, socket, promise, _socket$lastSentFrame7, i;
329
+ return _regenerator().w(function (_context7) {
330
+ while (1) switch (_context7.n) {
331
+ case 0:
332
+ _openPatchedConnectio4 = openPatchedConnection(), connection = _openPatchedConnectio4.connection, socket = _openPatchedConnectio4.socket;
333
+ promise = connection.GetUserInfo({
334
+ UserId: 1
335
+ });
336
+ _socket$lastSentFrame7 = socket.lastSentFrame(), i = _socket$lastSentFrame7.i;
337
+ socket.reply({
338
+ m: 1,
339
+ i: i,
340
+ n: 'GetUserInfo',
341
+ o: JSON.stringify({
342
+ UserId: 1,
343
+ UserName: 'max'
344
+ })
345
+ });
346
+ _context7.n = 1;
347
+ return expect(promise).resolves.toEqual({
348
+ UserId: 1,
349
+ UserName: 'max'
350
+ });
351
+ case 1:
352
+ return _context7.a(2);
353
+ }
354
+ }, _callee7);
355
+ })));
356
+ it('settles each in-flight call independently and does not touch unrelated sequence numbers', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
357
+ var _openPatchedConnectio5, connection, socket, first, firstSeq, second, secondSeq;
358
+ return _regenerator().w(function (_context8) {
359
+ while (1) switch (_context8.n) {
360
+ case 0:
361
+ _openPatchedConnectio5 = openPatchedConnection(), connection = _openPatchedConnectio5.connection, socket = _openPatchedConnectio5.socket;
362
+ first = connection.RPCPromise('GetUserInfo', {
363
+ UserId: 1
364
+ });
365
+ firstSeq = socket.lastSentFrame().i;
366
+ second = connection.RPCPromise('GetOMSs', {
367
+ OperatorId: 1
368
+ });
369
+ secondSeq = socket.lastSentFrame().i;
370
+ expect(secondSeq).toBe(firstSeq + 2);
371
+ socket.reply({
372
+ m: 5,
373
+ i: secondSeq,
374
+ n: 'GetOMSs',
375
+ o: NOT_FOUND
376
+ });
377
+ _context8.n = 1;
378
+ return expect(second).rejects.toMatchObject({
379
+ m: 5,
380
+ o: NOT_FOUND
381
+ });
382
+ case 1:
383
+ _context8.n = 2;
384
+ return expect(settlesWithin(first, 50)).resolves.toBe('pending');
385
+ case 2:
386
+ socket.reply({
387
+ m: 1,
388
+ i: firstSeq,
389
+ n: 'GetUserInfo',
390
+ o: '{}'
391
+ });
392
+ _context8.n = 3;
393
+ return expect(first).resolves.toMatchObject({
394
+ m: 1,
395
+ i: firstSeq
396
+ });
397
+ case 3:
398
+ return _context8.a(2);
399
+ }
400
+ }, _callee8);
401
+ })));
402
+ it('applies the same patch to a bare APEX instance (the unit of the fix)', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
403
+ var connection, socket, promise, _socket$lastSentFrame8, i;
404
+ return _regenerator().w(function (_context9) {
405
+ while (1) switch (_context9.n) {
406
+ case 0:
407
+ connection = new _alphapointApexApi.APEX(GATEWAY, {
408
+ onopen: function onopen() {},
409
+ onclose: function onclose() {}
410
+ });
411
+ socket = latestSocket();
412
+ socket.open();
413
+ (0, _apex.routeErrorReplies)(connection);
414
+ promise = connection.RPCPromise('GetUserInfo', {
415
+ UserId: 1
416
+ });
417
+ _socket$lastSentFrame8 = socket.lastSentFrame(), i = _socket$lastSentFrame8.i;
418
+ socket.reply({
419
+ m: 5,
420
+ i: i,
421
+ n: 'GetUserInfo',
422
+ o: NOT_FOUND
423
+ });
424
+ _context9.n = 1;
425
+ return expect(promise).rejects.toMatchObject({
426
+ m: 5,
427
+ i: i,
428
+ o: NOT_FOUND
429
+ });
430
+ case 1:
431
+ return _context9.a(2);
432
+ }
433
+ }, _callee9);
434
+ })));
435
+ });
package/lib/apex.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.setGateway = exports.initApex = exports.getApex = exports["default"] = void 0;
6
+ exports.setGateway = exports.routeErrorReplies = exports.initApex = exports.getApex = exports["default"] = void 0;
7
7
  var _alphapointApexApi = require("alphapoint-apex-api");
8
8
  var apex = {
9
9
  connection: {},
@@ -13,6 +13,41 @@ var setGateway = exports.setGateway = function setGateway(gateway) {
13
13
  apex.gateway = gateway;
14
14
  apex.connection = {};
15
15
  };
16
+
17
+ /**
18
+ * Deliver error replies (m === 5) to the caller that is waiting for them.
19
+ *
20
+ * alphapoint-apex-api only hands a frame to the pending callback when m === 1,
21
+ * so a gateway that answers "Endpoint Not Found" (a user whose permissions
22
+ * were all stripped, a session the gateway no longer knows) leaves RPCPromise
23
+ * and every generated endpoint method pending forever. Both already reject on
24
+ * m === 5 inside the callback; that code is simply unreachable without this.
25
+ *
26
+ * The WebSocketSubject multicasts, so a second subscription shares the socket
27
+ * the library opened. The library's own subscriber still owns m === 1.
28
+ */
29
+ var routeErrorReplies = exports.routeErrorReplies = function routeErrorReplies(connection) {
30
+ if (!connection || !connection.ws || typeof connection.ws.subscribe !== 'function') {
31
+ return;
32
+ }
33
+ connection.ws.subscribe(function (frame) {
34
+ if (!frame || frame.m !== 5) {
35
+ return;
36
+ }
37
+ var callback = connection.callbacks && connection.callbacks[frame.i];
38
+ if (!callback) {
39
+ return;
40
+ }
41
+ delete connection.callbacks[frame.i];
42
+ // rxjs 5 unsubscribes a subscriber whose `next` throws, which would
43
+ // silently restore the hang for every later refused call.
44
+ try {
45
+ callback(frame);
46
+ } catch (error) {
47
+ console.error("Callback for ".concat(frame.n, " threw on an error reply"), error);
48
+ }
49
+ }, function () {}, function () {});
50
+ };
16
51
  var initApex = exports.initApex = function initApex(onopen, onclose) {
17
52
  var gateway = apex.gateway;
18
53
  apex.connection = new _alphapointApexApi.APEX(gateway, {
@@ -20,6 +55,7 @@ var initApex = exports.initApex = function initApex(onopen, onclose) {
20
55
  onclose: onclose
21
56
  });
22
57
  apex.connection.standardCallback = function () {};
58
+ routeErrorReplies(apex.connection);
23
59
  };
24
60
  var getApex = exports.getApex = function getApex() {
25
61
  var onopen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+
3
+ var _apex = require("./apex");
4
+ // A stand-in for the alphapoint-apex-api connection: `ws` is the multicast
5
+ // WebSocketSubject, `callbacks` the pending RPC callbacks keyed by sequence.
6
+ var makeConnection = function makeConnection() {
7
+ var subscribers = [];
8
+ var connection = {
9
+ callbacks: {},
10
+ ws: {
11
+ subscribe: jest.fn(function (next, error, complete) {
12
+ subscribers.push({
13
+ next: next,
14
+ error: error,
15
+ complete: complete
16
+ });
17
+ return {
18
+ unsubscribe: jest.fn()
19
+ };
20
+ })
21
+ }
22
+ };
23
+ var emit = function emit(frame) {
24
+ return subscribers.forEach(function (s) {
25
+ return s.next(frame);
26
+ });
27
+ };
28
+ return {
29
+ connection: connection,
30
+ emit: emit
31
+ };
32
+ };
33
+ describe('routeErrorReplies', function () {
34
+ it('delivers an m:5 reply to the pending callback and clears it', function () {
35
+ var _makeConnection = makeConnection(),
36
+ connection = _makeConnection.connection,
37
+ emit = _makeConnection.emit;
38
+ var callback = jest.fn();
39
+ connection.callbacks[20] = callback;
40
+ (0, _apex.routeErrorReplies)(connection);
41
+ var frame = {
42
+ m: 5,
43
+ i: 20,
44
+ n: 'GetUserInfo',
45
+ o: 'Endpoint Not Found'
46
+ };
47
+ emit(frame);
48
+ expect(callback).toHaveBeenCalledWith(frame);
49
+ expect(connection.callbacks[20]).toBeUndefined();
50
+ });
51
+ it('leaves m:1 replies to the library subscriber', function () {
52
+ var _makeConnection2 = makeConnection(),
53
+ connection = _makeConnection2.connection,
54
+ emit = _makeConnection2.emit;
55
+ var callback = jest.fn();
56
+ connection.callbacks[4] = callback;
57
+ (0, _apex.routeErrorReplies)(connection);
58
+ emit({
59
+ m: 1,
60
+ i: 4,
61
+ n: 'WebAuthenticateUser',
62
+ o: '{}'
63
+ });
64
+ expect(callback).not.toHaveBeenCalled();
65
+ expect(connection.callbacks[4]).toBe(callback);
66
+ });
67
+ it('ignores an m:5 reply nobody is waiting for', function () {
68
+ var _makeConnection3 = makeConnection(),
69
+ connection = _makeConnection3.connection,
70
+ emit = _makeConnection3.emit;
71
+ (0, _apex.routeErrorReplies)(connection);
72
+ expect(function () {
73
+ return emit({
74
+ m: 5,
75
+ i: 999,
76
+ n: 'Whatever',
77
+ o: 'Endpoint Not Found'
78
+ });
79
+ }).not.toThrow();
80
+ expect(function () {
81
+ return emit(null);
82
+ }).not.toThrow();
83
+ });
84
+ it('keeps routing after a callback throws on an error reply', function () {
85
+ var _makeConnection4 = makeConnection(),
86
+ connection = _makeConnection4.connection,
87
+ emit = _makeConnection4.emit;
88
+ var errorSpy = jest.spyOn(console, 'error').mockImplementation(function () {});
89
+ connection.callbacks[2] = function () {
90
+ throw new SyntaxError('Unexpected token E in JSON');
91
+ };
92
+ var later = jest.fn();
93
+ connection.callbacks[4] = later;
94
+ (0, _apex.routeErrorReplies)(connection);
95
+ expect(function () {
96
+ return emit({
97
+ m: 5,
98
+ i: 2,
99
+ n: 'GetLastPrices',
100
+ o: 'Endpoint Not Found'
101
+ });
102
+ }).not.toThrow();
103
+ emit({
104
+ m: 5,
105
+ i: 4,
106
+ n: 'GetUserInfo',
107
+ o: 'Endpoint Not Found'
108
+ });
109
+ expect(later).toHaveBeenCalledTimes(1);
110
+ expect(errorSpy).toHaveBeenCalled();
111
+ errorSpy.mockRestore();
112
+ });
113
+ it('does nothing for a connection without a socket', function () {
114
+ expect(function () {
115
+ return (0, _apex.routeErrorReplies)({});
116
+ }).not.toThrow();
117
+ expect(function () {
118
+ return (0, _apex.routeErrorReplies)(undefined);
119
+ }).not.toThrow();
120
+ });
121
+ it('settles an RPCPromise-style callback once per reply', function () {
122
+ var _makeConnection5 = makeConnection(),
123
+ connection = _makeConnection5.connection,
124
+ emit = _makeConnection5.emit;
125
+ var callback = jest.fn();
126
+ connection.callbacks[6] = callback;
127
+ (0, _apex.routeErrorReplies)(connection);
128
+ var frame = {
129
+ m: 5,
130
+ i: 6,
131
+ n: 'GetOMSs',
132
+ o: 'Endpoint Not Found'
133
+ };
134
+ emit(frame);
135
+ emit(frame);
136
+ expect(callback).toHaveBeenCalledTimes(1);
137
+ });
138
+ });