laravel-echo-api-gateway 0.1.2 → 0.5.2
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.
- package/.github/workflows/ci.yml +10 -7
- package/.github/workflows/release.yml +2 -2
- package/README.md +19 -1
- package/dist/js-src/Websocket.d.ts +6 -0
- package/dist/laravel-echo-api-gateway.common.js +230 -67
- package/dist/laravel-echo-api-gateway.iife.js +230 -67
- package/dist/laravel-echo-api-gateway.js +230 -67
- package/js-src/Channel.ts +8 -5
- package/js-src/Websocket.ts +92 -44
- package/js-tests/Connector.test.ts +57 -5
- package/package.json +1 -1
- package/phpstan.neon.dist +2 -0
|
@@ -20,20 +20,44 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
20
20
|
function _createClass(Constructor, protoProps, staticProps) {
|
|
21
21
|
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
22
22
|
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
23
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
24
|
+
writable: false
|
|
25
|
+
});
|
|
23
26
|
return Constructor;
|
|
24
27
|
}
|
|
25
28
|
|
|
29
|
+
function _extends() {
|
|
30
|
+
_extends = Object.assign || function (target) {
|
|
31
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
32
|
+
var source = arguments[i];
|
|
33
|
+
|
|
34
|
+
for (var key in source) {
|
|
35
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
36
|
+
target[key] = source[key];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return target;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return _extends.apply(this, arguments);
|
|
45
|
+
}
|
|
46
|
+
|
|
26
47
|
function _inherits(subClass, superClass) {
|
|
27
48
|
if (typeof superClass !== "function" && superClass !== null) {
|
|
28
49
|
throw new TypeError("Super expression must either be null or a function");
|
|
29
50
|
}
|
|
30
51
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
Object.defineProperty(subClass, "prototype", {
|
|
53
|
+
value: Object.create(superClass && superClass.prototype, {
|
|
54
|
+
constructor: {
|
|
55
|
+
value: subClass,
|
|
56
|
+
writable: true,
|
|
57
|
+
configurable: true
|
|
58
|
+
}
|
|
59
|
+
}),
|
|
60
|
+
writable: false
|
|
37
61
|
});
|
|
38
62
|
if (superClass) _setPrototypeOf(subClass, superClass);
|
|
39
63
|
}
|
|
@@ -60,7 +84,7 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
60
84
|
if (typeof Proxy === "function") return true;
|
|
61
85
|
|
|
62
86
|
try {
|
|
63
|
-
|
|
87
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
64
88
|
return true;
|
|
65
89
|
} catch (e) {
|
|
66
90
|
return false;
|
|
@@ -78,6 +102,8 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
78
102
|
function _possibleConstructorReturn(self, call) {
|
|
79
103
|
if (call && (typeof call === "object" || typeof call === "function")) {
|
|
80
104
|
return call;
|
|
105
|
+
} else if (call !== void 0) {
|
|
106
|
+
throw new TypeError("Derived constructors may only return object or undefined");
|
|
81
107
|
}
|
|
82
108
|
|
|
83
109
|
return _assertThisInitialized(self);
|
|
@@ -102,6 +128,80 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
102
128
|
};
|
|
103
129
|
}
|
|
104
130
|
|
|
131
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
132
|
+
if (!o) return;
|
|
133
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
134
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
135
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
136
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
137
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function _arrayLikeToArray(arr, len) {
|
|
141
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
142
|
+
|
|
143
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
144
|
+
|
|
145
|
+
return arr2;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function _createForOfIteratorHelper(o, allowArrayLike) {
|
|
149
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
150
|
+
|
|
151
|
+
if (!it) {
|
|
152
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
153
|
+
if (it) o = it;
|
|
154
|
+
var i = 0;
|
|
155
|
+
|
|
156
|
+
var F = function () {};
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
s: F,
|
|
160
|
+
n: function () {
|
|
161
|
+
if (i >= o.length) return {
|
|
162
|
+
done: true
|
|
163
|
+
};
|
|
164
|
+
return {
|
|
165
|
+
done: false,
|
|
166
|
+
value: o[i++]
|
|
167
|
+
};
|
|
168
|
+
},
|
|
169
|
+
e: function (e) {
|
|
170
|
+
throw e;
|
|
171
|
+
},
|
|
172
|
+
f: F
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
var normalCompletion = true,
|
|
180
|
+
didErr = false,
|
|
181
|
+
err;
|
|
182
|
+
return {
|
|
183
|
+
s: function () {
|
|
184
|
+
it = it.call(o);
|
|
185
|
+
},
|
|
186
|
+
n: function () {
|
|
187
|
+
var step = it.next();
|
|
188
|
+
normalCompletion = step.done;
|
|
189
|
+
return step;
|
|
190
|
+
},
|
|
191
|
+
e: function (e) {
|
|
192
|
+
didErr = true;
|
|
193
|
+
err = e;
|
|
194
|
+
},
|
|
195
|
+
f: function () {
|
|
196
|
+
try {
|
|
197
|
+
if (!normalCompletion && it.return != null) it.return();
|
|
198
|
+
} finally {
|
|
199
|
+
if (didErr) throw err;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
105
205
|
class Connector {
|
|
106
206
|
/**
|
|
107
207
|
* Create a new class instance.
|
|
@@ -156,83 +256,136 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
156
256
|
|
|
157
257
|
var Websocket = /*#__PURE__*/function () {
|
|
158
258
|
function Websocket(options) {
|
|
159
|
-
var _this = this;
|
|
160
|
-
|
|
161
259
|
_classCallCheck(this, Websocket);
|
|
162
260
|
|
|
163
261
|
this.buffer = [];
|
|
164
262
|
this.listeners = {};
|
|
165
263
|
this.internalListeners = {};
|
|
166
264
|
this.channelBacklog = [];
|
|
265
|
+
this.closing = false;
|
|
266
|
+
this.hasConnected = false;
|
|
167
267
|
this.options = options;
|
|
168
|
-
this.
|
|
268
|
+
this.connect(this.options.host);
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
169
271
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
272
|
+
_createClass(Websocket, [{
|
|
273
|
+
key: "connect",
|
|
274
|
+
value: function connect(host) {
|
|
275
|
+
var _this = this;
|
|
173
276
|
|
|
174
|
-
|
|
277
|
+
this.options.debug && console.log('Trying to connect...');
|
|
278
|
+
this.websocket = new WebSocket(host);
|
|
175
279
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
280
|
+
this.websocket.onerror = function () {
|
|
281
|
+
if (!_this.hasConnected) {
|
|
282
|
+
setTimeout(function () {
|
|
283
|
+
_this.socketId = undefined;
|
|
179
284
|
|
|
180
|
-
|
|
181
|
-
|
|
285
|
+
_this.connect(host);
|
|
286
|
+
}, 3000);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
182
289
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
290
|
+
this.websocket.onopen = function () {
|
|
291
|
+
_this.options.debug && console.log('Connected !');
|
|
292
|
+
_this.hasConnected = true;
|
|
186
293
|
|
|
187
|
-
|
|
188
|
-
|
|
294
|
+
_this.send({
|
|
295
|
+
event: 'whoami'
|
|
296
|
+
});
|
|
189
297
|
|
|
190
|
-
|
|
191
|
-
_this.
|
|
192
|
-
}
|
|
298
|
+
while (_this.buffer.length) {
|
|
299
|
+
var message = _this.buffer[0];
|
|
193
300
|
|
|
194
|
-
|
|
195
|
-
}
|
|
301
|
+
_this.send(message);
|
|
196
302
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
};
|
|
303
|
+
_this.buffer.splice(0, 1);
|
|
304
|
+
} // Register events only once connected, or they won't be registered if connection failed/lost
|
|
201
305
|
|
|
202
|
-
this.on('whoami', function (_ref) {
|
|
203
|
-
var socketId = _ref.socket_id;
|
|
204
|
-
_this.socketId = socketId;
|
|
205
|
-
console.log("just set socketId to ".concat(socketId));
|
|
206
306
|
|
|
207
|
-
|
|
208
|
-
|
|
307
|
+
_this.websocket.onmessage = function (messageEvent) {
|
|
308
|
+
var message = _this.parseMessage(messageEvent.data);
|
|
209
309
|
|
|
210
|
-
|
|
310
|
+
_this.options.debug && console.log('onmessage', messageEvent.data);
|
|
211
311
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
this.send({
|
|
216
|
-
event: 'whoami'
|
|
217
|
-
}); // send ping every 60 seconds to keep connection alive
|
|
312
|
+
if (!message) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
218
315
|
|
|
219
|
-
|
|
220
|
-
|
|
316
|
+
if (message.channel) {
|
|
317
|
+
_this.options.debug && console.log("Received event ".concat(message.event, " on channel ").concat(message.channel));
|
|
221
318
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}, 60 * 1000);
|
|
226
|
-
return this;
|
|
227
|
-
}
|
|
319
|
+
if (_this.listeners[message.channel] && _this.listeners[message.channel][message.event]) {
|
|
320
|
+
_this.listeners[message.channel][message.event](message.data);
|
|
321
|
+
}
|
|
228
322
|
|
|
229
|
-
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (_this.internalListeners[message.event]) {
|
|
327
|
+
_this.internalListeners[message.event](message.data);
|
|
328
|
+
}
|
|
329
|
+
}; // send ping every 60 seconds to keep connection alive
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
_this.pingInterval = setInterval(function () {
|
|
333
|
+
if (_this.websocket.readyState === _this.websocket.OPEN) {
|
|
334
|
+
_this.options.debug && console.log('Sending ping');
|
|
335
|
+
|
|
336
|
+
_this.send({
|
|
337
|
+
event: 'ping'
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
}, 60 * 1000);
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
this.websocket.onclose = function () {
|
|
344
|
+
_this.options.debug && console.info('Connection closed.');
|
|
345
|
+
|
|
346
|
+
if (_this.closing) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
_this.hasConnected = false;
|
|
351
|
+
_this.options.debug && console.info('Connection lost, reconnecting...');
|
|
352
|
+
setTimeout(function () {
|
|
353
|
+
_this.socketId = undefined;
|
|
354
|
+
|
|
355
|
+
_this.connect(host);
|
|
356
|
+
}, 1000);
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
this.on('whoami', function (_ref) {
|
|
360
|
+
var socketId = _ref.socket_id;
|
|
361
|
+
_this.socketId = socketId;
|
|
362
|
+
_this.options.debug && console.log("just set socketId to ".concat(socketId)); // Handle the backlog and don't empty it, we'll need it if we lose connection
|
|
363
|
+
|
|
364
|
+
var channel;
|
|
365
|
+
|
|
366
|
+
var _iterator = _createForOfIteratorHelper(_this.channelBacklog),
|
|
367
|
+
_step;
|
|
368
|
+
|
|
369
|
+
try {
|
|
370
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
371
|
+
channel = _step.value;
|
|
372
|
+
|
|
373
|
+
_this.actuallySubscribe(channel);
|
|
374
|
+
}
|
|
375
|
+
} catch (err) {
|
|
376
|
+
_iterator.e(err);
|
|
377
|
+
} finally {
|
|
378
|
+
_iterator.f();
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}, {
|
|
230
383
|
key: "parseMessage",
|
|
231
384
|
value: function parseMessage(body) {
|
|
232
385
|
try {
|
|
233
386
|
return JSON.parse(body);
|
|
234
387
|
} catch (error) {
|
|
235
|
-
console.error(error);
|
|
388
|
+
this.options.debug && console.error(error);
|
|
236
389
|
return undefined;
|
|
237
390
|
}
|
|
238
391
|
}
|
|
@@ -259,6 +412,7 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
259
412
|
}, {
|
|
260
413
|
key: "close",
|
|
261
414
|
value: function close() {
|
|
415
|
+
this.closing = true;
|
|
262
416
|
this.internalListeners = {};
|
|
263
417
|
clearInterval(this.pingInterval);
|
|
264
418
|
this.pingInterval = undefined;
|
|
@@ -279,26 +433,32 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
279
433
|
var _this2 = this;
|
|
280
434
|
|
|
281
435
|
if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
|
|
282
|
-
console.log("Sending auth request for channel ".concat(channel.name));
|
|
436
|
+
this.options.debug && console.log("Sending auth request for channel ".concat(channel.name));
|
|
437
|
+
|
|
438
|
+
if (this.options.bearerToken) {
|
|
439
|
+
this.options.auth.headers['Authorization'] = 'Bearer ' + this.options.bearerToken;
|
|
440
|
+
}
|
|
441
|
+
|
|
283
442
|
axios.post(this.options.authEndpoint, {
|
|
284
443
|
socket_id: this.getSocketId(),
|
|
285
444
|
channel_name: channel.name
|
|
445
|
+
}, {
|
|
446
|
+
headers: this.options.auth.headers || {}
|
|
286
447
|
}).then(function (response) {
|
|
287
|
-
console.log("Subscribing to channels ".concat(channel.name));
|
|
448
|
+
_this2.options.debug && console.log("Subscribing to channels ".concat(channel.name));
|
|
288
449
|
|
|
289
450
|
_this2.send({
|
|
290
451
|
event: 'subscribe',
|
|
291
|
-
data: {
|
|
292
|
-
channel: channel.name
|
|
293
|
-
|
|
294
|
-
}
|
|
452
|
+
data: _extends({
|
|
453
|
+
channel: channel.name
|
|
454
|
+
}, response.data)
|
|
295
455
|
});
|
|
296
456
|
})["catch"](function (error) {
|
|
297
|
-
console.log("Auth request for channel ".concat(channel.name, " failed"));
|
|
298
|
-
console.error(error);
|
|
457
|
+
_this2.options.debug && console.log("Auth request for channel ".concat(channel.name, " failed"));
|
|
458
|
+
_this2.options.debug && console.error(error);
|
|
299
459
|
});
|
|
300
460
|
} else {
|
|
301
|
-
console.log("Subscribing to channels ".concat(channel.name));
|
|
461
|
+
this.options.debug && console.log("Subscribing to channels ".concat(channel.name));
|
|
302
462
|
this.send({
|
|
303
463
|
event: 'subscribe',
|
|
304
464
|
data: {
|
|
@@ -507,9 +667,12 @@ var LaravelEchoApiGateway = (function (exports) {
|
|
|
507
667
|
}, {
|
|
508
668
|
key: "whisper",
|
|
509
669
|
value: function whisper(event, data) {
|
|
670
|
+
var channel = this.name;
|
|
671
|
+
var formattedEvent = "client-" + event;
|
|
510
672
|
this.socket.send({
|
|
511
|
-
event:
|
|
512
|
-
data: data
|
|
673
|
+
"event": formattedEvent,
|
|
674
|
+
data: data,
|
|
675
|
+
channel: channel
|
|
513
676
|
});
|
|
514
677
|
return this;
|
|
515
678
|
}
|