larasopp 1.2.5 → 1.2.7

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/README.md CHANGED
@@ -16,11 +16,13 @@ composer require larasopp/larasopp
16
16
  import Larasopp from "Larasopp";
17
17
 
18
18
  const larasopp = new Larasopp({
19
- host: '127.0.0.1:9002',
19
+ host: 'ws://127.0.0.1:3001',
20
20
  token: 'token'
21
21
  });
22
22
 
23
23
  larasopp.connect();
24
+ //or
25
+ larasopp.connect('token');
24
26
 
25
27
  ```
26
28
 
package/lib/Core.d.ts CHANGED
@@ -20,10 +20,14 @@ export type TMessage<T> = {
20
20
  message?: T;
21
21
  type?: TPermissions;
22
22
  };
23
+ export type TConfigDataReviver = {
24
+ [index: string]: (value: any) => any;
25
+ };
23
26
  export interface IConfig {
24
27
  host: string;
25
28
  token?: string;
26
- tls?: boolean;
29
+ reviver?: (this: any, key: string, value: any) => any;
30
+ dataReviver?: TConfigDataReviver;
27
31
  }
28
32
  export type TChannels = {
29
33
  [channel: string]: Listener[];
@@ -31,7 +35,6 @@ export type TChannels = {
31
35
  declare abstract class Core {
32
36
  readonly events: Events;
33
37
  private ws?;
34
- protected _status: boolean;
35
38
  private _socketId?;
36
39
  private config;
37
40
  constructor(config: IConfig);
@@ -41,7 +44,7 @@ declare abstract class Core {
41
44
  * Connect to websocket
42
45
  * @returns {this}
43
46
  */
44
- connect(): this;
47
+ connect(token?: string): this;
45
48
  /**
46
49
  * Disconnect
47
50
  * @returns {void}
@@ -51,6 +54,7 @@ declare abstract class Core {
51
54
  private handleOpen;
52
55
  private handleClose;
53
56
  private handleError;
57
+ private jsonParse;
54
58
  private handleMessage;
55
59
  private emitListener;
56
60
  get socketId(): string | undefined;
package/lib/Core.js CHANGED
@@ -21,12 +21,6 @@ class Core {
21
21
  writable: true,
22
22
  value: void 0
23
23
  });
24
- Object.defineProperty(this, "_status", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: void 0
29
- });
30
24
  Object.defineProperty(this, "_socketId", {
31
25
  enumerable: true,
32
26
  configurable: true,
@@ -40,15 +34,14 @@ class Core {
40
34
  value: void 0
41
35
  });
42
36
  this.events = new easy_event_emitter_1.default;
43
- this.config = Object.assign({ tls: false }, config);
44
- this._status = false;
37
+ this.config = config;
45
38
  this.handleOpen = this.handleOpen.bind(this);
46
39
  this.handleClose = this.handleClose.bind(this);
47
40
  this.handleError = this.handleError.bind(this);
48
41
  this.handleMessage = this.handleMessage.bind(this);
49
42
  }
50
43
  setConfig(config) {
51
- this.config = Object.assign({ tls: false }, config);
44
+ this.config = config;
52
45
  }
53
46
  setToken(token) {
54
47
  this.config = Object.assign(Object.assign({}, this.config), { token });
@@ -60,12 +53,11 @@ class Core {
60
53
  * Connect to websocket
61
54
  * @returns {this}
62
55
  */
63
- connect() {
56
+ connect(token) {
64
57
  try {
65
- const host = [(this.config.tls ? 'wss' : 'ws') + '://'];
66
- host.push(this.config.host);
58
+ const host = [this.config.host];
67
59
  if (this.config.token)
68
- host.push('/token=' + this.config.token);
60
+ host.push('/token=' + (token !== null && token !== void 0 ? token : this.config.token));
69
61
  this.ws = new WebSocket(host.join(''));
70
62
  this.ws.onopen = this.handleOpen;
71
63
  this.ws.onclose = this.handleClose;
@@ -87,7 +79,6 @@ class Core {
87
79
  var _a;
88
80
  if (this.status) {
89
81
  (_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
90
- this._status = false;
91
82
  }
92
83
  this.ws = undefined;
93
84
  }
@@ -101,19 +92,37 @@ class Core {
101
92
  return true;
102
93
  }
103
94
  handleOpen(e) {
104
- this._status = true;
105
95
  this.events.emit("open", e);
106
96
  }
107
97
  handleClose(e) {
108
- this._status = false;
109
98
  this.events.emit("close", e);
110
99
  }
111
100
  handleError(e) {
112
101
  this.events.emit("error", e);
113
102
  }
103
+ jsonParse(str) {
104
+ if (this.config) {
105
+ if (typeof this.config.reviver === 'function') {
106
+ return JSON.parse(str, this.config.reviver);
107
+ }
108
+ if (this.config.dataReviver) {
109
+ const replace = this.config.dataReviver;
110
+ return JSON.parse(str, (key, value) => {
111
+ if (key === "createdAt" || key === "updatedAt") {
112
+ return new Date(value);
113
+ }
114
+ if (key in replace && typeof replace[key] === 'function') {
115
+ return replace[key](value);
116
+ }
117
+ return value;
118
+ });
119
+ }
120
+ }
121
+ return JSON.parse(str);
122
+ }
114
123
  handleMessage(e) {
115
124
  if (this.isJson(e.data)) {
116
- const json = JSON.parse(e.data);
125
+ const json = this.jsonParse(e.data);
117
126
  if (json.socket_id) {
118
127
  this._socketId = json.socket_id;
119
128
  return;
@@ -133,10 +142,13 @@ class Core {
133
142
  return this._socketId;
134
143
  }
135
144
  get status() {
136
- return this._status;
145
+ var _a, _b;
146
+ if (!this.ws)
147
+ return false;
148
+ return ((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ((_b = this.ws) === null || _b === void 0 ? void 0 : _b.OPEN);
137
149
  }
138
150
  _send(message) {
139
- if (!this.ws)
151
+ if (!this.ws || !this.status)
140
152
  return;
141
153
  this.ws.send(JSON.stringify(message));
142
154
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "larasopp",
3
- "version": "1.2.5",
3
+ "version": "1.2.7",
4
4
  "description": "Websocket client for laravel",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
package/src/Core.ts CHANGED
@@ -27,10 +27,15 @@ export type TMessage<T> = {
27
27
  type?: TPermissions;
28
28
  }
29
29
 
30
+ export type TConfigDataReviver = {
31
+ [index: string]: (value: any) => any;
32
+ };
33
+
30
34
  export interface IConfig {
31
35
  host: string;
32
36
  token?: string;
33
- tls?: boolean;
37
+ reviver?: (this: any, key: string, value: any) => any;
38
+ dataReviver?: TConfigDataReviver;
34
39
  }
35
40
 
36
41
  export type TChannels = {
@@ -40,18 +45,13 @@ export type TChannels = {
40
45
  abstract class Core {
41
46
  public readonly events: Events;
42
47
  private ws?: WebSocket;
43
- protected _status: boolean;
44
48
  private _socketId?: string;
45
49
  private config: IConfig;
46
50
 
47
51
  constructor(config: IConfig) {
48
52
  this.events = new EventEmitter;
49
53
 
50
- this.config = {
51
- tls: false,
52
- ...config,
53
- };
54
- this._status = false;
54
+ this.config = config;
55
55
 
56
56
  this.handleOpen = this.handleOpen.bind(this);
57
57
  this.handleClose = this.handleClose.bind(this);
@@ -60,10 +60,7 @@ abstract class Core {
60
60
  }
61
61
 
62
62
  public setConfig(config: IConfig): void {
63
- this.config = {
64
- tls: false,
65
- ...config,
66
- };
63
+ this.config = config;
67
64
  }
68
65
 
69
66
  public setToken(token: string): void {
@@ -80,11 +77,10 @@ abstract class Core {
80
77
  * Connect to websocket
81
78
  * @returns {this}
82
79
  */
83
- public connect(): this {
80
+ public connect(token?: string): this {
84
81
  try {
85
- const host = [(this.config.tls ? 'wss' : 'ws') + '://'];
86
- host.push(this.config.host);
87
- if (this.config.token) host.push('/token=' + this.config.token);
82
+ const host = [this.config.host];
83
+ if (this.config.token) host.push('/token=' + (token ?? this.config.token));
88
84
 
89
85
  this.ws = new WebSocket(host.join(''));
90
86
  this.ws.onopen = this.handleOpen;
@@ -107,7 +103,6 @@ abstract class Core {
107
103
  public disconnect(): void {
108
104
  if (this.status) {
109
105
  this.ws?.close();
110
- this._status = false;
111
106
  }
112
107
  this.ws = undefined;
113
108
  }
@@ -122,12 +117,10 @@ abstract class Core {
122
117
  }
123
118
 
124
119
  private handleOpen(e: Event): void {
125
- this._status = true;
126
120
  this.events.emit("open", e);
127
121
  }
128
122
 
129
123
  private handleClose(e: CloseEvent): void {
130
- this._status = false;
131
124
  this.events.emit("close", e);
132
125
  }
133
126
 
@@ -135,9 +128,32 @@ abstract class Core {
135
128
  this.events.emit("error", e);
136
129
  }
137
130
 
131
+ private jsonParse(str: string) {
132
+ if (this.config) {
133
+ if (typeof this.config.reviver === 'function') {
134
+ return JSON.parse(str, this.config.reviver);
135
+ }
136
+
137
+ if (this.config.dataReviver) {
138
+ const replace = this.config.dataReviver;
139
+ return JSON.parse(str, (key, value) => {
140
+ if (key === "createdAt" || key === "updatedAt") {
141
+ return new Date(value);
142
+ }
143
+ if (key in replace && typeof replace[key] === 'function') {
144
+ return replace[key](value);
145
+ }
146
+ return value;
147
+ });
148
+ }
149
+ }
150
+
151
+ return JSON.parse(str);
152
+ }
153
+
138
154
  private handleMessage(e: MessageEvent): void {
139
155
  if (this.isJson(e.data)) {
140
- const json = JSON.parse(e.data);
156
+ const json = this.jsonParse(e.data);
141
157
  if (json.socket_id) {
142
158
  this._socketId = json.socket_id;
143
159
  return;
@@ -160,11 +176,12 @@ abstract class Core {
160
176
  }
161
177
 
162
178
  public get status(): boolean {
163
- return this._status;
179
+ if (!this.ws) return false;
180
+ return this.ws?.readyState === this.ws?.OPEN;
164
181
  }
165
182
 
166
183
  private _send<T>(message: TMessage<T>) {
167
- if (!this.ws) return;
184
+ if (!this.ws || !this.status) return;
168
185
  this.ws.send(JSON.stringify(message));
169
186
  }
170
187