laravel-echo-api-gateway 0.5.2 → 0.5.4

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/js-src/Channel.ts CHANGED
@@ -3,6 +3,8 @@ import { Channel as BaseChannel } from 'laravel-echo/src/channel/channel';
3
3
  import { PresenceChannel } from "laravel-echo/src/channel";
4
4
  import { Websocket } from "./Websocket";
5
5
 
6
+ const LOG_PREFIX = '[LE-AG-Channel]';
7
+
6
8
  /**
7
9
  * This class represents a Pusher channel.
8
10
  */
@@ -45,6 +47,8 @@ export class Channel extends BaseChannel implements PresenceChannel {
45
47
  * Subscribe to a Pusher channel.
46
48
  */
47
49
  subscribe(): any {
50
+ this.options["debug"] && console.log(`${LOG_PREFIX} subscribe for channel ${this.name} ...`);
51
+
48
52
  this.socket.subscribe(this)
49
53
  }
50
54
 
@@ -52,13 +56,17 @@ export class Channel extends BaseChannel implements PresenceChannel {
52
56
  * Unsubscribe from a Pusher channel.
53
57
  */
54
58
  unsubscribe(): void {
59
+ this.options["debug"] && console.log(`${LOG_PREFIX} unsubscribe for channel ${this.name} ...`);
60
+
55
61
  this.socket.unsubscribe(this);
56
62
  }
57
63
 
58
64
  /**
59
65
  * Listen for an event on the channel instance.
60
66
  */
61
- listen(event: string, callback: Function): Channel {
67
+ listen(event: string, callback: Function): this {
68
+ this.options["debug"] && console.log(`${LOG_PREFIX} listen to ${event} for channel ${this.name} ...`);
69
+
62
70
  this.on(this.eventFormatter.format(event), callback);
63
71
 
64
72
  return this;
@@ -67,7 +75,9 @@ export class Channel extends BaseChannel implements PresenceChannel {
67
75
  /**
68
76
  * Stop listening for an event on the channel instance.
69
77
  */
70
- stopListening(event: string, callback?: Function): Channel {
78
+ stopListening(event: string, callback?: Function): this {
79
+ this.options["debug"] && console.log(`${LOG_PREFIX} stop listening to ${event} for channel ${this.name} ...`);
80
+
71
81
  this.socket.unbindEvent(this, event, callback)
72
82
 
73
83
  return this;
@@ -76,7 +86,9 @@ export class Channel extends BaseChannel implements PresenceChannel {
76
86
  /**
77
87
  * Register a callback to be called anytime a subscription succeeds.
78
88
  */
79
- subscribed(callback: Function): Channel {
89
+ subscribed(callback: Function): this {
90
+ this.options["debug"] && console.log(`${LOG_PREFIX} subscribed for channel ${this.name} ...`);
91
+
80
92
  this.on('subscription_succeeded', () => {
81
93
  callback();
82
94
  });
@@ -87,7 +99,9 @@ export class Channel extends BaseChannel implements PresenceChannel {
87
99
  /**
88
100
  * Register a callback to be called anytime a subscription error occurs.
89
101
  */
90
- error(callback: Function): Channel {
102
+ error(callback: Function): this {
103
+ this.options["debug"] && console.log(`${LOG_PREFIX} error for channel ${this.name} ...`);
104
+
91
105
  this.on('error', (status) => {
92
106
  callback(status);
93
107
  });
@@ -99,12 +113,14 @@ export class Channel extends BaseChannel implements PresenceChannel {
99
113
  * Bind a channel to an event.
100
114
  */
101
115
  on(event: string, callback: Function): Channel {
116
+ this.options["debug"] && console.log(`${LOG_PREFIX} on ${event} for channel ${this.name} ...`);
117
+
102
118
  this.socket.bind(this, event, callback)
103
119
 
104
120
  return this;
105
121
  }
106
122
 
107
- whisper(event: string, data: object): Channel {
123
+ whisper(event: string, data: object): this {
108
124
  let channel = this.name;
109
125
  let formattedEvent = "client-" + event;
110
126
  this.socket.send({
@@ -116,7 +132,7 @@ export class Channel extends BaseChannel implements PresenceChannel {
116
132
  return this;
117
133
  }
118
134
 
119
- here(callback: Function): Channel {
135
+ here(callback: Function): this {
120
136
  // TODO: implement
121
137
 
122
138
  return this
@@ -125,7 +141,7 @@ export class Channel extends BaseChannel implements PresenceChannel {
125
141
  /**
126
142
  * Listen for someone joining the channel.
127
143
  */
128
- joining(callback: Function): Channel {
144
+ joining(callback: Function): this {
129
145
  // TODO: implement
130
146
 
131
147
  return this
@@ -134,9 +150,11 @@ export class Channel extends BaseChannel implements PresenceChannel {
134
150
  /**
135
151
  * Listen for someone leaving the channel.
136
152
  */
137
- leaving(callback: Function): Channel {
153
+ leaving(callback: Function): this {
138
154
  // TODO: implement
139
155
 
140
156
  return this
141
157
  }
142
158
  }
159
+
160
+ export { PresenceChannel };
@@ -4,10 +4,10 @@ import {Channel} from "./Channel";
4
4
 
5
5
  export const broadcaster = (options: object): Connector => new Connector(options);
6
6
 
7
+ const LOG_PREFIX = '[LE-AG-Connector]';
8
+
7
9
  export class Connector extends BaseConnector {
8
- /**
9
- * The Socket.io connection instance.
10
- */
10
+
11
11
  socket: Websocket;
12
12
 
13
13
  /**
@@ -15,10 +15,19 @@ export class Connector extends BaseConnector {
15
15
  */
16
16
  channels: { [name: string]: Channel } = {};
17
17
 
18
+ /**
19
+ * Create a new class instance.
20
+ */
21
+ constructor(options: any) {
22
+ super(options);
23
+ }
24
+
18
25
  /**
19
26
  * Create a fresh Socket.io connection.
20
27
  */
21
28
  connect(): void {
29
+ this.options.debug && console.log(LOG_PREFIX + 'Connect ...' );
30
+
22
31
  this.socket = new Websocket(this.options);
23
32
 
24
33
  return;
@@ -103,6 +112,8 @@ export class Connector extends BaseConnector {
103
112
  * Disconnect socket connection.
104
113
  */
105
114
  disconnect(): void {
115
+ this.options.debug && console.log(LOG_PREFIX + 'Disconnect ...' );
116
+
106
117
  this.socket.close();
107
118
  }
108
119
  }
@@ -1,10 +1,13 @@
1
1
  import { AxiosResponse } from "axios";
2
2
  import { Channel } from "./Channel";
3
+ import axios from 'axios';
3
4
 
4
5
  export type Options = { authEndpoint: string, host: string, bearerToken: string, auth: any, debug: boolean };
5
6
 
6
7
  export type MessageBody = { event: string, channel?: string, data: object };
7
8
 
9
+ const LOG_PREFIX = '[LE-AG-Websocket]';
10
+
8
11
  export class Websocket {
9
12
  buffer: Array<object> = [];
10
13
 
@@ -12,7 +15,7 @@ export class Websocket {
12
15
 
13
16
  websocket: WebSocket;
14
17
 
15
- private listeners: { [channelName: string]: { [eventName: string]: Function } } = {}
18
+ private listeners: { [channelName: string]: { [eventName: string]: Function } } = {};
16
19
 
17
20
  private internalListeners: { [eventName: string]: Function } = {};
18
21
 
@@ -25,57 +28,75 @@ export class Websocket {
25
28
 
26
29
  private pingInterval: NodeJS.Timeout;
27
30
 
31
+ constructor(options: Options) {
32
+ this.options = options;
33
+
34
+ this.connect(this.options.host);
35
+
36
+ return this;
37
+ }
38
+
28
39
  private connect(host: string): void {
29
- this.options.debug && console.log('Trying to connect...');
30
40
 
31
- this.websocket = new WebSocket(host)
41
+ if (!host) {
42
+ this.options.debug && console.error(LOG_PREFIX + `Cannont connect without host !`);
43
+
44
+ return;
45
+ }
46
+
47
+ this.options.debug && console.log(LOG_PREFIX + `Trying to connect to ${host}...` );
48
+
49
+ this.websocket = new WebSocket(host);
32
50
 
33
51
  this.websocket.onerror = () => {
52
+
34
53
  if (!this.hasConnected) {
54
+
35
55
  setTimeout(() => {
36
- this.socketId = undefined
37
- this.connect(host)
56
+ this.socketId = undefined;
57
+ this.connect(host);
38
58
  }, 3000);
39
59
  }
40
- }
60
+ };
41
61
 
42
62
  this.websocket.onopen = () => {
43
- this.options.debug && console.log('Connected !');
63
+ this.options.debug && console.log(LOG_PREFIX + ' Connected !');
44
64
  this.hasConnected = true;
65
+
45
66
  this.send({
46
67
  event: 'whoami',
47
- })
68
+ });
48
69
 
49
70
  while (this.buffer.length) {
50
- const message = this.buffer[0]
71
+ const message = this.buffer[0];
51
72
 
52
- this.send(message)
73
+ this.send(message);
53
74
 
54
- this.buffer.splice(0, 1)
75
+ this.buffer.splice(0, 1);
55
76
  }
56
77
 
57
78
  // Register events only once connected, or they won't be registered if connection failed/lost
58
79
 
59
80
  this.websocket.onmessage = (messageEvent: MessageEvent) => {
60
- const message = this.parseMessage(messageEvent.data)
61
- this.options.debug && console.log('onmessage', messageEvent.data)
81
+ const message = this.parseMessage(messageEvent.data);
82
+ this.options.debug && console.log(LOG_PREFIX + ' onmessage', messageEvent.data);
62
83
 
63
84
  if (!message) {
64
- return
85
+ return;
65
86
  }
66
87
 
67
88
  if (message.channel) {
68
- this.options.debug && console.log(`Received event ${message.event} on channel ${message.channel}`)
89
+ this.options.debug && console.log(`${LOG_PREFIX} Received event ${message.event} on channel ${message.channel}`);
69
90
 
70
91
  if (this.listeners[message.channel] && this.listeners[message.channel][message.event]) {
71
- this.listeners[message.channel][message.event](message.data)
92
+ this.listeners[message.channel][message.event](message.data);
72
93
  }
73
94
 
74
- return
95
+ return;
75
96
  }
76
97
 
77
98
  if (this.internalListeners[message.event]) {
78
- this.internalListeners[message.event](message.data)
99
+ this.internalListeners[message.event](message.data);
79
100
  }
80
101
  }
81
102
 
@@ -83,100 +104,96 @@ export class Websocket {
83
104
  // send ping every 60 seconds to keep connection alive
84
105
  this.pingInterval = setInterval(() => {
85
106
  if (this.websocket.readyState === this.websocket.OPEN) {
86
- this.options.debug && console.log('Sending ping')
107
+ this.options.debug && console.log(LOG_PREFIX + ' Sending ping');
108
+
87
109
  this.send({
88
110
  event: 'ping',
89
- })
111
+ });
90
112
  }
91
- }, 60 * 1000)
113
+ }, 60 * 1000);
92
114
  }
93
115
 
94
116
 
95
117
  this.websocket.onclose = () => {
96
118
  this.options.debug && console.info('Connection closed.');
119
+
97
120
  if (this.closing){
98
121
  return;
99
122
  }
100
- this.hasConnected = false
123
+
124
+ this.hasConnected = false;
101
125
  this.options.debug && console.info('Connection lost, reconnecting...');
126
+
102
127
  setTimeout(() => {
103
- this.socketId = undefined
104
- this.connect(host)
128
+ this.socketId = undefined;
129
+ this.connect(host);
105
130
  }, 1000);
106
131
  };
107
132
 
108
133
  this.on('whoami', ({ socket_id: socketId }) => {
109
- this.socketId = socketId
134
+ this.socketId = socketId;
110
135
 
111
- this.options.debug && console.log(`just set socketId to ${socketId}`)
136
+ this.options.debug && console.log(`${LOG_PREFIX} Just set socketId to ${socketId}`);
112
137
 
113
138
  // Handle the backlog and don't empty it, we'll need it if we lose connection
114
139
  let channel: Channel;
140
+
115
141
  for(channel of this.channelBacklog){
116
- this.actuallySubscribe(channel)
142
+ this.actuallySubscribe(channel);
117
143
  }
118
-
119
- })
120
-
121
-
122
- }
123
-
124
- constructor(options: Options) {
125
- this.options = options;
126
-
127
- this.connect(this.options.host);
128
-
129
- return this
144
+ });
130
145
  }
131
146
 
132
147
  protected parseMessage(body: string): MessageBody {
133
148
  try {
134
- return JSON.parse(body)
149
+ return JSON.parse(body);
135
150
  } catch (error) {
136
- this.options.debug && console.error(error)
151
+ this.options.debug && console.error(error);
137
152
 
138
- return undefined
153
+ return undefined;
139
154
  }
140
155
  }
141
156
 
142
157
  getSocketId(): string {
143
- return this.socketId
158
+ return this.socketId;
144
159
  }
145
160
 
146
161
  private socketIsReady(): boolean {
147
- return this.websocket.readyState === this.websocket.OPEN
162
+ return this.websocket.readyState === this.websocket.OPEN;
148
163
  }
149
164
 
150
165
  send(message: object): void {
151
166
  if (this.socketIsReady()) {
152
- this.websocket.send(JSON.stringify(message))
153
- return
167
+ this.websocket.send(JSON.stringify(message));
168
+ return;
154
169
  }
155
170
 
156
- this.buffer.push(message)
171
+ this.buffer.push(message);
157
172
  }
158
173
 
159
174
  close(): void {
160
- this.closing = true
161
- this.internalListeners = {}
175
+ this.closing = true;
176
+ this.internalListeners = {};
162
177
 
163
- clearInterval(this.pingInterval)
164
- this.pingInterval = undefined
178
+ clearInterval(this.pingInterval);
179
+ this.pingInterval = undefined;
165
180
 
166
- this.websocket.close()
181
+ this.websocket.close();
167
182
  }
168
183
 
169
184
  subscribe(channel: Channel): void {
170
185
  if (this.getSocketId()) {
171
- this.actuallySubscribe(channel)
186
+ this.actuallySubscribe(channel);
172
187
  } else {
173
- this.channelBacklog.push(channel)
188
+ this.options.debug && console.log(`${LOG_PREFIX} subscribe - push channel backlog for channel ${channel.name}`);
189
+
190
+ this.channelBacklog.push(channel);
174
191
  }
175
192
  }
176
193
 
177
194
  private actuallySubscribe(channel: Channel): void {
178
195
  if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
179
- this.options.debug && console.log(`Sending auth request for channel ${channel.name}`)
196
+ this.options.debug && console.log(`${LOG_PREFIX} Sending auth request for channel ${channel.name}`);
180
197
 
181
198
  if (this.options.bearerToken) {
182
199
  this.options.auth.headers['Authorization'] = 'Bearer ' + this.options.bearerToken;
@@ -188,7 +205,7 @@ export class Websocket {
188
205
  }, {
189
206
  headers: this.options.auth.headers || {}
190
207
  }).then((response: AxiosResponse) => {
191
- this.options.debug && console.log(`Subscribing to channels ${channel.name}`)
208
+ this.options.debug && console.log(`${LOG_PREFIX} Subscribing to private channel ${channel.name}`);
192
209
 
193
210
  this.send({
194
211
  event: 'subscribe',
@@ -196,51 +213,59 @@ export class Websocket {
196
213
  channel: channel.name,
197
214
  ...response.data
198
215
  },
199
- })
216
+ });
200
217
  }).catch((error) => {
201
- this.options.debug && console.log(`Auth request for channel ${channel.name} failed`)
202
- this.options.debug && console.error(error)
218
+ this.options.debug && console.log(`${LOG_PREFIX} Auth request for channel ${channel.name} failed`);
219
+ this.options.debug && console.error(error);
203
220
  })
204
221
  } else {
205
- this.options.debug && console.log(`Subscribing to channels ${channel.name}`)
222
+ this.options.debug && console.log(`${LOG_PREFIX} Subscribing to channel ${channel.name}`);
206
223
 
207
224
  this.send({
208
225
  event: 'subscribe',
209
226
  data: {
210
227
  channel: channel.name,
211
228
  },
212
- })
229
+ });
213
230
  }
214
231
  }
215
232
 
216
233
  unsubscribe(channel: Channel): void {
234
+ this.options.debug && console.log(`${LOG_PREFIX} unsubscribe for channel ${channel.name}`);
235
+
217
236
  this.send({
218
237
  event: 'unsubscribe',
219
238
  data: {
220
239
  channel: channel.name,
221
240
  },
222
- })
241
+ });
223
242
 
224
243
  if (this.listeners[channel.name]) {
225
- delete this.listeners[channel.name]
244
+ delete this.listeners[channel.name];
226
245
  }
227
246
  }
228
247
 
229
248
  on(event: string, callback: Function = null): void {
230
- this.internalListeners[event] = callback
249
+ this.options.debug && console.log(`${LOG_PREFIX} on event ${event} ...`);
250
+
251
+ this.internalListeners[event] = callback;
231
252
  }
232
253
 
233
254
  bind(channel: Channel, event: string, callback: Function): void {
255
+ this.options.debug && console.log(`${LOG_PREFIX} bind event ${event} for channel ${channel.name} ...`);
256
+
234
257
  if (!this.listeners[channel.name]) {
235
- this.listeners[channel.name] = {}
258
+ this.listeners[channel.name] = {};
236
259
  }
237
260
 
238
- this.listeners[channel.name][event] = callback
261
+ this.listeners[channel.name][event] = callback;
239
262
  }
240
263
 
241
264
  unbindEvent(channel: Channel, event: string, callback: Function = null): void {
265
+ this.options.debug && console.log(`${LOG_PREFIX} unbind event ${event} for channel ${channel.name} ...`);
266
+
242
267
  if (this.internalListeners[event] && (callback === null || this.internalListeners[event] === callback)) {
243
- delete this.internalListeners[event]
268
+ delete this.internalListeners[event];
244
269
  }
245
270
  }
246
271
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "laravel-echo-api-gateway",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Use Laravel Echo with API Gateway Websockets",
5
5
  "keywords": [
6
6
  "laravel",
@@ -40,6 +40,7 @@
40
40
  "@babel/plugin-transform-object-assign": "^7.8.3",
41
41
  "@babel/preset-env": "^7.9.6",
42
42
  "@rollup/plugin-babel": "^5.0.0",
43
+ "@types/babel__traverse": "^7.20.6",
43
44
  "@types/jest": "^24.0.18",
44
45
  "@types/node": "^12.7.5",
45
46
  "@typescript-eslint/eslint-plugin": "^3.7.0",
@@ -48,14 +49,17 @@
48
49
  "eslint": "^7.5.0",
49
50
  "jest": "^24.9.0",
50
51
  "jest-websocket-mock": "^2.2.0",
51
- "laravel-echo": "^1.10.0",
52
+ "laravel-echo": "1.10.0",
52
53
  "mock-socket": "^9.0.3",
53
54
  "rollup": "^2.10.2",
55
+ "rollup-plugin-commonjs": "^10.1.0",
56
+ "rollup-plugin-json": "^4.0.0",
57
+ "rollup-plugin-node-resolve": "^5.2.0",
54
58
  "rollup-plugin-typescript2": "^0.27.1",
55
59
  "standard-version": "^8.0.1",
56
60
  "ts-jest": "^24.1.0",
57
61
  "tslib": "^1.10.0",
58
- "typescript": "^3.6.3"
62
+ "typescript": "^5.7.2"
59
63
  },
60
64
  "engines": {
61
65
  "node": ">=10"
package/rollup.config.js CHANGED
@@ -9,7 +9,10 @@ export default {
9
9
  { file: './dist/laravel-echo-api-gateway.iife.js', format: 'iife', name: 'LaravelEchoApiGateway' },
10
10
  ],
11
11
  plugins: [
12
- typescript(),
12
+ typescript({
13
+ tsconfig: './tsconfig.json', // Chemin explicite vers votre fichier TypeScript
14
+ useTsconfigDeclarationDir: true, // Respecte les options `outDir` et `declarationDir`
15
+ }),
13
16
  babel({
14
17
  babelHelpers: 'bundled',
15
18
  exclude: 'node_modules/**',