larasopp 1.2.5 → 1.2.6
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/lib/Core.d.ts +6 -1
- package/lib/Core.js +26 -13
- package/package.json +1 -1
- package/src/Core.ts +33 -8
package/lib/Core.d.ts
CHANGED
|
@@ -20,10 +20,15 @@ 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
29
|
tls?: boolean;
|
|
30
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
31
|
+
dataReviver?: TConfigDataReviver;
|
|
27
32
|
}
|
|
28
33
|
export type TChannels = {
|
|
29
34
|
[channel: string]: Listener[];
|
|
@@ -31,7 +36,6 @@ export type TChannels = {
|
|
|
31
36
|
declare abstract class Core {
|
|
32
37
|
readonly events: Events;
|
|
33
38
|
private ws?;
|
|
34
|
-
protected _status: boolean;
|
|
35
39
|
private _socketId?;
|
|
36
40
|
private config;
|
|
37
41
|
constructor(config: IConfig);
|
|
@@ -51,6 +55,7 @@ declare abstract class Core {
|
|
|
51
55
|
private handleOpen;
|
|
52
56
|
private handleClose;
|
|
53
57
|
private handleError;
|
|
58
|
+
private jsonParse;
|
|
54
59
|
private handleMessage;
|
|
55
60
|
private emitListener;
|
|
56
61
|
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,
|
|
@@ -41,7 +35,6 @@ class Core {
|
|
|
41
35
|
});
|
|
42
36
|
this.events = new easy_event_emitter_1.default;
|
|
43
37
|
this.config = Object.assign({ tls: false }, config);
|
|
44
|
-
this._status = false;
|
|
45
38
|
this.handleOpen = this.handleOpen.bind(this);
|
|
46
39
|
this.handleClose = this.handleClose.bind(this);
|
|
47
40
|
this.handleError = this.handleError.bind(this);
|
|
@@ -87,7 +80,6 @@ class Core {
|
|
|
87
80
|
var _a;
|
|
88
81
|
if (this.status) {
|
|
89
82
|
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
|
|
90
|
-
this._status = false;
|
|
91
83
|
}
|
|
92
84
|
this.ws = undefined;
|
|
93
85
|
}
|
|
@@ -101,19 +93,37 @@ class Core {
|
|
|
101
93
|
return true;
|
|
102
94
|
}
|
|
103
95
|
handleOpen(e) {
|
|
104
|
-
this._status = true;
|
|
105
96
|
this.events.emit("open", e);
|
|
106
97
|
}
|
|
107
98
|
handleClose(e) {
|
|
108
|
-
this._status = false;
|
|
109
99
|
this.events.emit("close", e);
|
|
110
100
|
}
|
|
111
101
|
handleError(e) {
|
|
112
102
|
this.events.emit("error", e);
|
|
113
103
|
}
|
|
104
|
+
jsonParse(str) {
|
|
105
|
+
if (this.config) {
|
|
106
|
+
if (typeof this.config.reviver === 'function') {
|
|
107
|
+
return JSON.parse(str, this.config.reviver);
|
|
108
|
+
}
|
|
109
|
+
if (this.config.dataReviver) {
|
|
110
|
+
const replace = this.config.dataReviver;
|
|
111
|
+
return JSON.parse(str, (key, value) => {
|
|
112
|
+
if (key === "createdAt" || key === "updatedAt") {
|
|
113
|
+
return new Date(value);
|
|
114
|
+
}
|
|
115
|
+
if (key in replace && typeof replace[key] === 'function') {
|
|
116
|
+
return replace[key](value);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return JSON.parse(str);
|
|
123
|
+
}
|
|
114
124
|
handleMessage(e) {
|
|
115
125
|
if (this.isJson(e.data)) {
|
|
116
|
-
const json =
|
|
126
|
+
const json = this.jsonParse(e.data);
|
|
117
127
|
if (json.socket_id) {
|
|
118
128
|
this._socketId = json.socket_id;
|
|
119
129
|
return;
|
|
@@ -133,10 +143,13 @@ class Core {
|
|
|
133
143
|
return this._socketId;
|
|
134
144
|
}
|
|
135
145
|
get status() {
|
|
136
|
-
|
|
146
|
+
var _a, _b;
|
|
147
|
+
if (!this.ws)
|
|
148
|
+
return false;
|
|
149
|
+
return ((_a = this.ws) === null || _a === void 0 ? void 0 : _a.readyState) === ((_b = this.ws) === null || _b === void 0 ? void 0 : _b.OPEN);
|
|
137
150
|
}
|
|
138
151
|
_send(message) {
|
|
139
|
-
if (!this.ws)
|
|
152
|
+
if (!this.ws || !this.status)
|
|
140
153
|
return;
|
|
141
154
|
this.ws.send(JSON.stringify(message));
|
|
142
155
|
}
|
package/package.json
CHANGED
package/src/Core.ts
CHANGED
|
@@ -27,10 +27,16 @@ 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
37
|
tls?: boolean;
|
|
38
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
39
|
+
dataReviver?: TConfigDataReviver;
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
export type TChannels = {
|
|
@@ -40,7 +46,6 @@ export type TChannels = {
|
|
|
40
46
|
abstract class Core {
|
|
41
47
|
public readonly events: Events;
|
|
42
48
|
private ws?: WebSocket;
|
|
43
|
-
protected _status: boolean;
|
|
44
49
|
private _socketId?: string;
|
|
45
50
|
private config: IConfig;
|
|
46
51
|
|
|
@@ -51,7 +56,6 @@ abstract class Core {
|
|
|
51
56
|
tls: false,
|
|
52
57
|
...config,
|
|
53
58
|
};
|
|
54
|
-
this._status = false;
|
|
55
59
|
|
|
56
60
|
this.handleOpen = this.handleOpen.bind(this);
|
|
57
61
|
this.handleClose = this.handleClose.bind(this);
|
|
@@ -107,7 +111,6 @@ abstract class Core {
|
|
|
107
111
|
public disconnect(): void {
|
|
108
112
|
if (this.status) {
|
|
109
113
|
this.ws?.close();
|
|
110
|
-
this._status = false;
|
|
111
114
|
}
|
|
112
115
|
this.ws = undefined;
|
|
113
116
|
}
|
|
@@ -122,12 +125,10 @@ abstract class Core {
|
|
|
122
125
|
}
|
|
123
126
|
|
|
124
127
|
private handleOpen(e: Event): void {
|
|
125
|
-
this._status = true;
|
|
126
128
|
this.events.emit("open", e);
|
|
127
129
|
}
|
|
128
130
|
|
|
129
131
|
private handleClose(e: CloseEvent): void {
|
|
130
|
-
this._status = false;
|
|
131
132
|
this.events.emit("close", e);
|
|
132
133
|
}
|
|
133
134
|
|
|
@@ -135,9 +136,32 @@ abstract class Core {
|
|
|
135
136
|
this.events.emit("error", e);
|
|
136
137
|
}
|
|
137
138
|
|
|
139
|
+
private jsonParse(str: string) {
|
|
140
|
+
if (this.config) {
|
|
141
|
+
if (typeof this.config.reviver === 'function') {
|
|
142
|
+
return JSON.parse(str, this.config.reviver);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (this.config.dataReviver) {
|
|
146
|
+
const replace = this.config.dataReviver;
|
|
147
|
+
return JSON.parse(str, (key, value) => {
|
|
148
|
+
if (key === "createdAt" || key === "updatedAt") {
|
|
149
|
+
return new Date(value);
|
|
150
|
+
}
|
|
151
|
+
if (key in replace && typeof replace[key] === 'function') {
|
|
152
|
+
return replace[key](value);
|
|
153
|
+
}
|
|
154
|
+
return value;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return JSON.parse(str);
|
|
160
|
+
}
|
|
161
|
+
|
|
138
162
|
private handleMessage(e: MessageEvent): void {
|
|
139
163
|
if (this.isJson(e.data)) {
|
|
140
|
-
const json =
|
|
164
|
+
const json = this.jsonParse(e.data);
|
|
141
165
|
if (json.socket_id) {
|
|
142
166
|
this._socketId = json.socket_id;
|
|
143
167
|
return;
|
|
@@ -160,11 +184,12 @@ abstract class Core {
|
|
|
160
184
|
}
|
|
161
185
|
|
|
162
186
|
public get status(): boolean {
|
|
163
|
-
|
|
187
|
+
if (!this.ws) return false;
|
|
188
|
+
return this.ws?.readyState === this.ws?.OPEN;
|
|
164
189
|
}
|
|
165
190
|
|
|
166
191
|
private _send<T>(message: TMessage<T>) {
|
|
167
|
-
if (!this.ws) return;
|
|
192
|
+
if (!this.ws || !this.status) return;
|
|
168
193
|
this.ws.send(JSON.stringify(message));
|
|
169
194
|
}
|
|
170
195
|
|