@mixd-id/web-scaffold 0.1.230406264 → 0.1.230406266

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.230406264",
4
+ "version": "0.1.230406266",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -114,7 +114,15 @@ export default{
114
114
  },
115
115
 
116
116
  close(){
117
- this._state = false
117
+ if(this.hash){
118
+ this.$router.replace({
119
+ ...this.$route,
120
+ hash: this.$route.hash.replace(`${this.hash}`, '')
121
+ })
122
+ }
123
+ else{
124
+ this._state = false
125
+ }
118
126
  },
119
127
 
120
128
  dismiss(){
@@ -255,9 +263,11 @@ export default{
255
263
  }
256
264
  },
257
265
 
258
- '$route.hash'(to){
259
- if(this.hash !== null){
260
- this._state = to === this.hash
266
+ '$route.hash': {
267
+ handler(to){
268
+ if(this.hash !== null){
269
+ this._state = (to ?? '').indexOf(this.hash) >= 0
270
+ }
261
271
  }
262
272
  }
263
273
 
@@ -275,7 +285,7 @@ export default{
275
285
  }
276
286
 
277
287
  if(this.hash !== null){
278
- this._state = this.$route.hash === this.hash
288
+ this._state = (this.$route.hash ?? '').indexOf(this.hash) >= 0
279
289
  }
280
290
  })
281
291
  },
package/src/utils/wss.js CHANGED
@@ -128,7 +128,6 @@ class WSS extends EventEmitter2{
128
128
  }
129
129
 
130
130
  socket.on('message', async (binaryData) => {
131
- console.log('message#1', socket.isAuth)
132
131
  if(!socket.isAuth) return
133
132
 
134
133
  let obj
@@ -139,45 +138,46 @@ class WSS extends EventEmitter2{
139
138
  return socket.close(1002, e.message);
140
139
  }
141
140
 
142
- const { _requestId, path, params } = obj
141
+ const { _requestId, path, params, pong } = obj
143
142
 
144
- let status = 200
145
- let data
146
- switch(path){
147
-
148
- case 'ping':
149
- data = { type:'pong' }
150
- break
143
+ if(pong){
144
+ delete socket.pinging
145
+ }
146
+ else{
147
+ let status = 200
148
+ let data
149
+ switch(path){
151
150
 
152
- default:
153
- try{
154
- const arr = (await Promise.all(this._reqFn.map(fn => fn({ path, params }, socket))))
155
- .filter(_ => _)
151
+ default:
152
+ try{
153
+ const arr = (await Promise.all(this._reqFn.map(fn => fn({ path, params }, socket))))
154
+ .filter(_ => _)
156
155
 
157
- data = arr.length > 0 ? JSON.parse(JSON.stringify(arr.pop())) : data
158
- }
159
- catch(e){
160
- if(this._opt.debug){
161
- console.error(e)
156
+ data = arr.length > 0 ? JSON.parse(JSON.stringify(arr.pop())) : data
162
157
  }
163
-
164
- status = 500
165
- data = {
166
- name: e.name,
167
- message: e.message,
168
- errors: e.errors,
169
- stack: process.env.APP_DEBUG === 'true' ?
170
- e.stack : undefined
158
+ catch(e){
159
+ if(this._opt.debug){
160
+ console.error(e)
161
+ }
162
+
163
+ status = 500
164
+ data = {
165
+ name: e.name,
166
+ message: e.message,
167
+ errors: e.errors,
168
+ stack: process.env.APP_DEBUG === 'true' ?
169
+ e.stack : undefined
170
+ }
171
171
  }
172
- }
173
- break
174
- }
172
+ break
173
+ }
175
174
 
176
- socket.send(await this.toBinaryData({
177
- _requestId,
178
- status,
179
- data
180
- }))
175
+ socket.send(await this.toBinaryData({
176
+ _requestId,
177
+ status,
178
+ data
179
+ }))
180
+ }
181
181
  });
182
182
 
183
183
  socket.on('close', (e) => {
@@ -193,6 +193,21 @@ class WSS extends EventEmitter2{
193
193
  catch(e){
194
194
  return socket.close(1002, e.message);
195
195
  }
196
+
197
+ const ping = async() => {
198
+ socket.send(await this.toBinaryData({ ping:1 }))
199
+ socket.pinging = true
200
+
201
+ setTimeout(() => {
202
+ if(socket.pinging){
203
+ socket.close(1002, 'ping timeout')
204
+ }
205
+ else{
206
+ setTimeout(ping, (Math.round(Math.random() * 10) % 5) * 5000)
207
+ }
208
+ }, 3000)
209
+ }
210
+ setTimeout(ping, (Math.round(Math.random() * 10) % 5) * 5000)
196
211
  })
197
212
  }
198
213
 
package/src/utils/wss.mjs CHANGED
@@ -45,7 +45,6 @@ class WSS extends EventEmitter2{
45
45
  _counter = 0
46
46
  _callbacks = {}
47
47
  _pendingSend = []
48
- _lastBlurAt
49
48
 
50
49
  constructor(opt) {
51
50
  super();
@@ -58,40 +57,9 @@ class WSS extends EventEmitter2{
58
57
  url: '',
59
58
  }, opt ?? {})
60
59
 
61
- /*if(typeof window !== 'undefined'){
62
- window.addEventListener('online', this._onOnlineChanged)
63
- window.addEventListener('offline', this._onOnlineChanged)
64
- window.addEventListener('blur', this._onBlur)
65
- window.addEventListener('focus', this._onFocus)
66
- }*/
67
-
68
60
  this.connect().then()
69
61
  }
70
62
 
71
- _onBlur = () => {
72
- this._lastBlurAt = new Date().getTime()
73
- }
74
-
75
- _onFocus = () => {
76
- this.emit('window', 'focus', [])
77
-
78
- if(this._lastBlurAt){
79
- this.send('ping', {}, { timeout:1000 })
80
- .catch(_ => {
81
- this.reconnect().then()
82
- })
83
- }
84
- }
85
-
86
- _onOnlineChanged = (e) => {
87
- if(!navigator.onLine){
88
- this.emit('disconnect', e, [])
89
- }
90
- else{
91
- this.emit('connect', null, [])
92
- }
93
- }
94
-
95
63
  async fromBinaryData(binaryData){
96
64
  const encryptedString = new TextDecoder().decode(binaryData)
97
65
  const decryptedData = this._opt.key ?
@@ -109,21 +77,24 @@ class WSS extends EventEmitter2{
109
77
 
110
78
  async connect(reconnect = false){
111
79
 
112
- this._instance = new WebSocket(this._opt.url, this._opt.token)
80
+ this._instance = new WebSocket(this._opt.url, [ this._opt.token ])
113
81
 
114
82
  this._instance.binaryType = 'arraybuffer';
115
83
 
116
84
  this._instance.onmessage = async (event) => {
85
+
117
86
  const obj = await this.fromBinaryData(event.data);
118
87
 
119
- const { _requestId, status, data, auth } = obj
88
+ const { _requestId, status, data, auth, ping } = obj
120
89
 
121
- if(auth){
90
+ if(ping){
91
+ this._instance.send(await this.toBinaryData({ pong:1 }))
92
+ }
93
+ else if(auth){
122
94
  this._instance.isAuth = auth
123
95
 
124
96
  reconnect ? this.emit('reconnect', null, []) : this.emit('connect', null, [])
125
97
 
126
- console.log('pending send', this._pendingSend)
127
98
  for(let sendParams of this._pendingSend){
128
99
  this.sendSync(sendParams.path, sendParams.params, sendParams.cb, sendParams.err)
129
100
  }
@@ -184,8 +155,6 @@ class WSS extends EventEmitter2{
184
155
  }
185
156
 
186
157
  sendSync(path, params, cb, err, override){
187
- console.log('sendSync', path, this._instance.readyState)
188
-
189
158
  if(this._instance.readyState > 1){
190
159
  console.log('unable to send, ready state not 1', this._instance.readyState)
191
160
  return
@@ -211,7 +180,6 @@ class WSS extends EventEmitter2{
211
180
  .then(async(dataUrlObj) => {
212
181
 
213
182
  const binaryData = await this.toBinaryData(dataUrlObj)
214
- console.log('sendSync', this._instance.readyState, dataUrlObj)
215
183
  this._instance.send(binaryData)
216
184
 
217
185
  this._callbacks[_requestId] = {
@@ -239,12 +207,6 @@ class WSS extends EventEmitter2{
239
207
  })
240
208
  }
241
209
 
242
- async resume(){
243
- if(this._instance.readyState > 1){
244
- return this.reconnect()
245
- }
246
- }
247
-
248
210
  }
249
211
 
250
212
  export {