@mixd-id/web-scaffold 0.1.230406238 → 0.1.230406239

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mixd-id/web-scaffold",
3
3
  "private": false,
4
- "version": "0.1.230406238",
4
+ "version": "0.1.230406239",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
package/src/utils/wss.js CHANGED
@@ -136,7 +136,10 @@ class WSS extends EventEmitter2{
136
136
  socket.leave = (channel) => {
137
137
  if((socket.channels ?? {})[channel]){
138
138
  delete socket.channels[channel]
139
- //console.log('leave', channel)
139
+
140
+ if(this._opt.debug){
141
+ console.log('leave', channel)
142
+ }
140
143
  }
141
144
  }
142
145
 
@@ -146,7 +149,10 @@ class WSS extends EventEmitter2{
146
149
  }
147
150
 
148
151
  socket.channels[channel] = 1
149
- //console.log('join', channel)
152
+
153
+ if(this._opt.debug){
154
+ console.log('join', channel)
155
+ }
150
156
  }
151
157
 
152
158
  socket.to = (channel) => {
@@ -222,7 +228,10 @@ class WSS extends EventEmitter2{
222
228
  }
223
229
 
224
230
  async broadcast(channel, { model, event, items }){
225
- //console.log('broadcast', channel, JSON.stringify({ model, event, items }).substring(0, 70))
231
+
232
+ if(this._opt.debug){
233
+ console.log('broadcast', channel, JSON.stringify({ model, event, items }).substring(0, 70))
234
+ }
226
235
 
227
236
  for(let socket of this._instance.clients){
228
237
  if(socket.readyState === WebSocket.OPEN && (socket.channels ?? {})[channel]){
package/src/utils/wss.mjs CHANGED
@@ -1,6 +1,15 @@
1
1
  import CryptoJS from "crypto-js";
2
2
  import EventEmitter2 from "eventemitter2";
3
3
 
4
+ const fileToDataURL = (file) => {
5
+ return new Promise((resolve, reject) => {
6
+ const reader = new FileReader();
7
+ reader.onload = (event) => resolve(event.target.result);
8
+ reader.onerror = (error) => reject(error);
9
+ reader.readAsDataURL(file);
10
+ });
11
+ };
12
+
4
13
  class WSS extends EventEmitter2{
5
14
 
6
15
  _instance
@@ -12,93 +21,30 @@ class WSS extends EventEmitter2{
12
21
 
13
22
  static async convertDataUrlObj(obj){
14
23
 
15
- const fileToDataURL = (file) => {
16
- return new Promise((resolve, reject) => {
17
- const reader = new FileReader();
18
- reader.onload = (event) => resolve(event.target.result);
19
- reader.onerror = (error) => reject(error);
20
- reader.readAsDataURL(file);
21
- });
22
- };
23
-
24
- const processObjectProperties = async (inputObj) => {
25
- let outputObj;
26
-
27
- if(Array.isArray(inputObj)){
28
- outputObj = []
29
- for(let i = 0; i < inputObj.length; i++){
30
- const value = inputObj[i];
31
-
32
- if (value instanceof File) {
33
- outputObj[i] = await fileToDataURL(value);
34
- } else if (typeof value === 'object' && value !== null) {
35
- outputObj[i] = await processObjectProperties(value);
36
- } else {
37
- outputObj[i] = value;
38
- }
39
- }
40
- }
41
- else{
42
- outputObj = {}
43
-
44
- for (const key in inputObj) {
45
- if (inputObj.hasOwnProperty(key)) {
46
- const value = inputObj[key];
47
-
48
- if (value instanceof File) {
49
- outputObj[key] = await fileToDataURL(value);
50
- } else if (typeof value === 'object' && value !== null) {
51
- outputObj[key] = await processObjectProperties(value);
52
- } else {
53
- outputObj[key] = value;
54
- }
55
- }
56
- }
57
- }
58
-
59
-
60
- return outputObj;
61
- };
62
-
63
- return processObjectProperties(obj);
64
- }
65
-
66
- static async revertDataUrlObj(obj){
67
-
68
- const processObjectProperties = async (inputObj) => {
69
- let outputObj;
24
+ let output
70
25
 
71
- if(Array.isArray(inputObj)){
72
- outputObj = []
73
- for (let i = 0; i < inputObj.length; i++) {
74
- let value = inputObj[i];
75
-
76
- if (typeof value === 'object' && value !== null) {
77
- outputObj[i] = await processObjectProperties(value);
78
- } else {
79
- outputObj[i] = value;
80
- }
81
- }
26
+ if(Array.isArray(obj)){
27
+ output = []
28
+ for(let i = 0; i < obj.length; i++){
29
+ output.push(await WSS.convertDataUrlObj(obj[i]));
82
30
  }
83
- else if(typeof inputObj === 'object' && inputObj !== null){
84
- outputObj = {}
85
- for (const key in inputObj) {
86
- if (inputObj.hasOwnProperty(key)) {
87
- let value = inputObj[key];
88
-
89
- if (typeof value === 'object' && value !== null) {
90
- outputObj[key] = await processObjectProperties(value);
91
- } else {
92
- outputObj[key] = value;
93
- }
94
- }
31
+ }
32
+ else if(obj instanceof File || obj instanceof Blob) {
33
+ output = await fileToDataURL(obj);
34
+ }
35
+ else if(typeof obj === 'object' && obj !== null){
36
+ output = {}
37
+ for (const key in obj) {
38
+ if (obj.hasOwnProperty(key)) {
39
+ output[key] = await WSS.convertDataUrlObj(obj[key])
95
40
  }
96
41
  }
42
+ }
43
+ else{
44
+ output = obj;
45
+ }
97
46
 
98
- return outputObj;
99
- };
100
-
101
- return processObjectProperties(obj);
47
+ return output
102
48
  }
103
49
 
104
50
  constructor(opt) {
@@ -130,7 +76,7 @@ class WSS extends EventEmitter2{
130
76
  }
131
77
 
132
78
  _onFocus = () => {
133
- this.emit('focus')
79
+ this.emit('window', 'focus', [])
134
80
 
135
81
  if(this._lastBlurAt){
136
82
  this.send('ping', {}, { timeout:1000 })
@@ -142,10 +88,10 @@ class WSS extends EventEmitter2{
142
88
 
143
89
  _onOnlineChanged = (e) => {
144
90
  if(!navigator.onLine){
145
- this.emit('disconnect', e)
91
+ this.emit('disconnect', e, [])
146
92
  }
147
93
  else{
148
- this.emit('connect')
94
+ this.emit('connect', null, [])
149
95
  }
150
96
  }
151
97
 
@@ -153,8 +99,7 @@ class WSS extends EventEmitter2{
153
99
  const secretKey = this._opt.key;
154
100
  const encryptedString = new TextDecoder().decode(binaryData)
155
101
  const decryptedData = CryptoJS.AES.decrypt(encryptedString, secretKey).toString(CryptoJS.enc.Utf8);
156
- const receivedObject = JSON.parse(decryptedData);
157
- return await WSS.revertDataUrlObj(receivedObject);
102
+ return JSON.parse(decryptedData)
158
103
  }
159
104
 
160
105
  async toBinaryData(obj){
@@ -172,7 +117,7 @@ class WSS extends EventEmitter2{
172
117
  this._instance.onopen = () => {
173
118
  this.send('_auth', this._opt.auth)
174
119
  .then(() => {
175
- reconnect ? this.emit('reconnect') : this.emit('connect')
120
+ reconnect ? this.emit('reconnect', null, []) : this.emit('connect', null, [])
176
121
 
177
122
  for(let sendParams of this._pendingSend){
178
123
  this.sendSync(sendParams.path, sendParams.params, sendParams.cb, sendParams.err)
@@ -209,7 +154,7 @@ class WSS extends EventEmitter2{
209
154
  };
210
155
 
211
156
  this._instance.onerror = (e) => {
212
- this.emit('error', e)
157
+ this.emit('error', e, [])
213
158
  }
214
159
 
215
160
  this._instance.onclose = (e) => {
@@ -217,11 +162,11 @@ class WSS extends EventEmitter2{
217
162
  switch(e.code){
218
163
 
219
164
  case 1002:
220
- this.emit('connect_error', e)
165
+ this.emit('connect_error', e, [])
221
166
  break
222
167
 
223
168
  default:
224
- this.emit('disconnect')
169
+ this.emit('disconnect', null, [])
225
170
  break
226
171
  }
227
172
  };