@mixd-id/web-scaffold 0.1.230406375 → 0.1.230406377

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.230406375",
4
+ "version": "0.1.230406377",
5
5
  "scripts": {
6
6
  "dev": "vite serve",
7
7
  "build": "vite build",
@@ -20,18 +20,29 @@
20
20
  "require": "./src/utils/helpers.js",
21
21
  "import": "./src/utils/helpers.mjs"
22
22
  },
23
+ "./helpers.js": "./src/utils/helpers.js",
24
+ "./helpers.mjs": "./src/utils/helpers.mjs",
25
+ "./queue": "./src/utils/queue.js",
26
+
23
27
  "./wss": {
24
28
  "require": "./src/utils/wss.js",
25
29
  "import": "./src/utils/wss.mjs"
26
30
  },
31
+ "./wss.js": "./src/utils/wss.js",
32
+ "./wss.mjs": "./src/utils/wss.mjs",
33
+
27
34
  "./importer": "./src/utils/importer.js",
28
35
  "./listpage1": "./src/utils/listpage1.js",
29
36
  "./dashboard": "./src/utils/dashboard.js",
30
37
  "./listview": "./src/utils/listview.js",
38
+
31
39
  "./preset-selector": {
32
40
  "require": "./src/utils/preset-selector.js",
33
41
  "import": "./src/utils/preset-selector.mjs"
34
42
  },
43
+ "./preset-selector.js": "./src/utils/preset-selector.js",
44
+ "./preset-selector.mjs": "./src/utils/preset-selector.mjs",
45
+
35
46
  "./web": "./src/utils/web.js"
36
47
  },
37
48
  "dependencies": {
@@ -40,6 +51,8 @@
40
51
  "@tailwindcss/line-clamp": "^0.4.0",
41
52
  "@vueuse/core": "^9.0.2",
42
53
  "adm-zip": "^0.5.10",
54
+ "axios": "^1.7.9",
55
+ "bcrypt": "^5.1.1",
43
56
  "chart.js": "^4.2.1",
44
57
  "compression": "^1.7.4",
45
58
  "cookie-parser": "^1.4.6",
@@ -58,6 +71,7 @@
58
71
  "pinia": "^2.3.0",
59
72
  "prismjs": "^1.28.0",
60
73
  "redis": "^4.6.13",
74
+ "sequelize": "^6.37.5",
61
75
  "serve-static": "^1.15.0",
62
76
  "tailwindcss": "^3.2.4",
63
77
  "vue": "^3.2.25",
@@ -316,7 +316,9 @@ export default{
316
316
  toolbar: {
317
317
  type: [ String, Boolean ],
318
318
  default: true
319
- }
319
+ },
320
+
321
+ sorts: Array
320
322
  },
321
323
 
322
324
  methods: {
@@ -395,23 +397,15 @@ export default{
395
397
  ((this.data ?? {}).hasNext !== false) &&
396
398
  !(this.preset.pivot ?? {}).enabled) {
397
399
 
398
- const afterItem = this.data.items[this.data.items.length - 1]
399
- console.log('#next', afterItem.id, afterItem.lastMessageAt)
400
+ const afterItem = this.dataItems[this.dataItems.length - 1]
400
401
 
401
402
  this.readyState = 4
402
403
  this.socket.send(this.src, {
403
404
  ...this.preset,
404
405
  itemsPerPage: this.data.itemsPerPage,
405
- afterItem: this.data.items[this.data.items.length - 1],
406
+ afterItem,
406
407
  })
407
408
  .then(data => {
408
- const firstItem = data.items[0]
409
- const lastItem = data.items[data.items.length - 1]
410
- console.log('#loaded',
411
- firstItem?.id, firstItem?.lastMessageAt,
412
- lastItem?.id, lastItem?.lastMessageAt,
413
- data.items.length)
414
-
415
409
  this.data.items.push(...data.items)
416
410
  this.loadEnums(data.items)
417
411
  })
@@ -919,7 +913,13 @@ export default{
919
913
  },
920
914
 
921
915
  dataItems(){
922
- if((this.preset.sorts ?? []).length > 0){
916
+
917
+ if((this.sorts ?? []).length > 0){
918
+ return (this.data.items ?? []).sort((a, b) => {
919
+ return sortsFn(a, b, this.sorts, 0)
920
+ })
921
+ }
922
+ else if((this.preset.sorts ?? []).length > 0){
923
923
  return (this.data.items ?? []).sort((a, b) => {
924
924
  return sortsFn(a, b, this.preset.sorts, 0)
925
925
  })
package/src/index.js CHANGED
@@ -393,6 +393,7 @@ export default{
393
393
 
394
394
  const privateVars = {}
395
395
 
396
+ app.config.globalProperties.$t = t => t
396
397
  app.config.globalProperties.uniqid = uniqid
397
398
  app.config.globalProperties.formatDate = formatDate
398
399
  app.config.globalProperties.$download = download
@@ -1,10 +1,3 @@
1
- const md5 = require("md5");
2
- const fs = require("fs");
3
- const { Op } = require('sequelize')
4
- const axios = require('axios')
5
- const dayjs = require('dayjs')
6
-
7
-
8
1
  const ceil = (num, precision = 0) => {
9
2
  var p = Math.pow(10, precision)
10
3
  var n = (num * p) * (1 + Number.EPSILON)
@@ -32,6 +25,7 @@ const ftWildcard = (key) => {
32
25
  }
33
26
 
34
27
  const moveFile = function(oldPath, newPath, callback) {
28
+ const fs = require("fs");
35
29
 
36
30
  fs.rename(oldPath, newPath, function (err) {
37
31
  if (err) {
@@ -46,6 +40,8 @@ const moveFile = function(oldPath, newPath, callback) {
46
40
  });
47
41
 
48
42
  function copy() {
43
+ const fs = require("fs");
44
+
49
45
  var readStream = fs.createReadStream(oldPath);
50
46
  var writeStream = fs.createWriteStream(newPath);
51
47
 
@@ -96,6 +92,9 @@ const saveBase64 = async(data, dir = '/storage/files/images') => {
96
92
 
97
93
  const saveBuffer = async(buffer, dir = '/storage/files/images') => {
98
94
 
95
+ const fs = require("fs");
96
+ const md5 = require('md5')
97
+
99
98
  const { fileTypeFromBuffer } = await import('file-type')
100
99
 
101
100
  const type = await fileTypeFromBuffer(buffer)
@@ -111,6 +110,7 @@ const saveBuffer = async(buffer, dir = '/storage/files/images') => {
111
110
  }
112
111
 
113
112
  const saveFromUrl = async(url, dir = '/storage/files/images') => {
113
+ const axios = require('axios')
114
114
 
115
115
  const res = await axios({
116
116
  method: 'GET',
@@ -153,6 +153,8 @@ const strVars = (text, vars, opt = { emptyUndefined:true }) => {
153
153
 
154
154
  const writeStorage = (path, content, append = false) => {
155
155
 
156
+ const fs = require("fs");
157
+
156
158
  if(path.startsWith('/')) path = path.substring(1)
157
159
 
158
160
  if(append){
@@ -195,6 +197,8 @@ const sequelizeChunk = async (model, opt, callback, chunkSize = 5000) => {
195
197
 
196
198
  const getPresetSortWhereParams = (order, afterItem) => {
197
199
 
200
+ const { Op } = require('sequelize')
201
+
198
202
  if(order.filter((_) => _[0] === 'id').length <= 0){
199
203
  order.push([ 'id', 'desc' ])
200
204
  }
@@ -505,6 +509,7 @@ const datasourceLoad = async ({ datasource, Models }) => {
505
509
  }
506
510
 
507
511
  const dayTimeRange = (params, value) => {
512
+ const dayjs = require('dayjs')
508
513
 
509
514
  /*params = {
510
515
  '*': [ [ '08:00', '12:00' ], [ '13:00', '18:00' ] ],
@@ -543,6 +548,18 @@ const removeUnderscoredKey = (obj) => {
543
548
  }
544
549
  }
545
550
 
551
+ const hashMake = async (text) => {
552
+ const bcrypt = require('bcrypt')
553
+
554
+ return bcrypt.hash(text + (process.env.APP_KEY ?? ''), 10)
555
+ }
556
+
557
+ const hashCheck = (text, hash) => {
558
+ const bcrypt = require('bcrypt')
559
+
560
+ return bcrypt.compare(text + (process.env.APP_KEY ?? ''), hash)
561
+ }
562
+
546
563
  module.exports = {
547
564
  capitalize,
548
565
  ceil,
@@ -572,5 +589,7 @@ module.exports = {
572
589
  datasourceGet,
573
590
  datasourceLoad,
574
591
  dayTimeRange,
575
- removeUnderscoredKey
592
+ removeUnderscoredKey,
593
+ hashMake,
594
+ hashCheck
576
595
  }
@@ -0,0 +1,63 @@
1
+ const redis = require('redis');
2
+ const client = redis.createClient();
3
+ client.connect()
4
+ .then(() => console.log('Connected to Redis'))
5
+ .catch((err) => console.error('Redis connection error:', err));
6
+
7
+ const queues = {}
8
+
9
+ const sleep = ms => new Promise(r => setTimeout(r, ms))
10
+
11
+ const processQueue = async(key) => {
12
+ const queue = queues[key]
13
+ if(queue.processing) return
14
+ queue.processing = true
15
+
16
+ const result = await client.hGetAll(key);
17
+ const keys = Object.keys(result ?? {})
18
+ if(keys.length < 1){
19
+ queue.processing = false
20
+ return
21
+ }
22
+
23
+ const itemKeys = keys.splice(0, queue.concurrency)
24
+ if(itemKeys.length > 0){
25
+ await Promise.all(itemKeys.map(async itemKey => {
26
+ try{
27
+ const obj = JSON.parse(await client.hGet(key, itemKey))
28
+ await queue.pop(obj)
29
+ }
30
+ finally{
31
+ await client.hDel(key, itemKey)
32
+ }
33
+ }))
34
+
35
+ await sleep(queue.interval ?? 0)
36
+ }
37
+ queue.processing = false
38
+
39
+ await processQueue(key)
40
+ }
41
+
42
+ const queueItem = async(obj, opt) => {
43
+ if(!queues[opt.key]){
44
+ queues[opt.key] = {
45
+ interval: 16,
46
+ concurrency: 1,
47
+
48
+ ...opt
49
+ }
50
+
51
+ await client.del(opt.key)
52
+ }
53
+
54
+ const { key } = opt
55
+ const subKey = `${key}-${new Date().getTime() + Math.random()}`
56
+ await client.hSet(key, subKey, JSON.stringify(obj));
57
+
58
+ processQueue(key).then()
59
+ }
60
+
61
+ module.exports = {
62
+ queueItem
63
+ }
package/src/utils/wss.js CHANGED
@@ -222,7 +222,14 @@ class WSS extends EventEmitter2{
222
222
 
223
223
  const token = req.headers['sec-websocket-protocol'];
224
224
  try{
225
- await this._opt.auth(token, socket)
225
+ if(this._opt.auth){
226
+ await this._opt.auth(token, socket)
227
+ }
228
+
229
+ for(let fn of this._authFn) {
230
+ await fn(token, socket)
231
+ }
232
+
226
233
  socket.isAuth = true
227
234
  socket.send(await this.toBinaryData({ auth:true }))
228
235
 
@@ -230,7 +237,7 @@ class WSS extends EventEmitter2{
230
237
  await subscriber.connect()
231
238
  }
232
239
  catch(e){
233
- return socket.close(1002, e.message);
240
+ return socket.close(1002, (e.message ?? '').substring(0, 123));
234
241
  }
235
242
 
236
243
  if(this._opt.ping !== false){
@@ -271,6 +278,10 @@ class WSS extends EventEmitter2{
271
278
  this._reqFn.push(fn)
272
279
  }
273
280
 
281
+ use(fn){
282
+ this.req.apply(this, arguments)
283
+ }
284
+
274
285
  async broadcast(channels, { model, event, items }){
275
286
 
276
287
  if(!Array.isArray(channels))
package/src/utils/wss.mjs CHANGED
@@ -69,7 +69,6 @@ class WSS extends EventEmitter2{
69
69
  this.emit('visibilitychange', this._instance.readyState, [])
70
70
 
71
71
  this.ping().catch(e => {
72
- console.log('ping error', e)
73
72
  this.reconnect().then()
74
73
  })
75
74
  }
@@ -128,8 +127,8 @@ class WSS extends EventEmitter2{
128
127
 
129
128
  if(this._opt.debug){
130
129
  status === 200 ?
131
- console.log(`${new Date().getTime() - t1}ms`, `l:${JSON.stringify(data ?? '').length}`, path, params, data) :
132
- console.error(`${new Date().getTime() - t1}ms`, `l:${JSON.stringify(data ?? '').length}`, path, params, data)
130
+ console.log(`${path} (${new Date().getTime() - t1}ms)`, params, data, `l:${JSON.stringify(data ?? '').length}`) :
131
+ console.error(`${path} (${new Date().getTime() - t1}ms)`, params, data, `l:${JSON.stringify(data ?? '').length}`)
133
132
  }
134
133
  }
135
134
  }
@@ -185,6 +184,7 @@ class WSS extends EventEmitter2{
185
184
 
186
185
  sendSync(path, params, cb, err, override){
187
186
  if(!this._instance || this._instance.readyState > 1){
187
+ this._pendingSend.push({ path, params, cb, err })
188
188
  return
189
189
  }
190
190
  else if(!this._instance.isAuth){