bajo-extra 1.1.1 → 1.1.3

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.
@@ -13,5 +13,8 @@
13
13
  "allDone": "All done!",
14
14
  "dlDirNotExists%s": "Download dir '%s' doesn't exist",
15
15
  "gettingStatus%s": "Getting %s status",
16
- "downloading": "Downloading..."
16
+ "downloading": "Downloading...",
17
+ "encryption type": "encryption type",
18
+ "decryption type": "decryption type",
19
+ "fetching%s": "Fetching %s"
17
20
  }
package/bajo/intl/id.json CHANGED
@@ -13,5 +13,8 @@
13
13
  "allDone": "Selesai semua!",
14
14
  "dlDirNotExists%s": "Dir unduh '%s' belum ada",
15
15
  "gettingStatus%s": "Mendapatkan status %s",
16
- "downloading": "Pengunduhan..."
16
+ "downloading": "Pengunduhan...",
17
+ "encryption type": "Tipe enkripsi",
18
+ "decryption type": "Tipe dekripsi",
19
+ "fetching%s": "Fetching %s"
17
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,6 +32,7 @@
32
32
  "fast-xml-parser": "^4.5.1",
33
33
  "numbro": "^2.5.0",
34
34
  "performant-array-to-tree": "^1.11.0",
35
+ "short-crypt": "^4.0.0",
35
36
  "undici": "^7.2.1"
36
37
  }
37
38
  }
package/plugin/factory.js CHANGED
@@ -2,9 +2,10 @@ import bcrypt from 'bcrypt'
2
2
  import crypto from 'crypto'
3
3
  import { createGzip, createGunzip } from 'zlib'
4
4
  import path from 'path'
5
- import { fetch, Agent } from 'undici'
5
+ import { fetch } from 'undici'
6
6
  import { Readable } from 'stream'
7
7
  import numbro from 'numbro'
8
+ import { ShortCrypt } from 'short-crypt'
8
9
 
9
10
  async function fetching ({ url, opts, bulk, spin }) {
10
11
  const { setImmediate, print } = this.app.bajo
@@ -115,6 +116,7 @@ async function factory (pkgName) {
115
116
  super(pkgName, me.app)
116
117
  this.alias = 'extra'
117
118
  this.config = {
119
+ secret: 'hxKY8Eh63Op9js6ovU25qmq2DmCE9dIB',
118
120
  fetch: {
119
121
  agent: {}
120
122
  }
@@ -251,7 +253,7 @@ async function factory (pkgName) {
251
253
  fetchUrl = async (url, opts = {}, extra = {}) => {
252
254
  const { isSet } = this.app.bajo
253
255
  const { fs } = this.app.bajo.lib
254
- const { has, isArray, isPlainObject, isString, cloneDeep, merge } = this.app.bajo.lib._
256
+ const { isEmpty, has, isArray, isPlainObject, isString, cloneDeep, merge } = this.app.bajo.lib._
255
257
  if (isPlainObject(url)) {
256
258
  extra = cloneDeep(opts)
257
259
  opts = cloneDeep(url)
@@ -263,12 +265,13 @@ async function factory (pkgName) {
263
265
  opts.headers.Authorization = `Basic ${Buffer.from(`${opts.auth.username}:${opts.auth.password}`).toString('base64')}`
264
266
  delete opts.auth
265
267
  }
266
- opts.query = merge({}, opts.query, opts.params ?? {})
268
+ const query = merge({}, opts.query, opts.params ?? {})
269
+ if (!isEmpty(query)) opts.query = query
267
270
  delete opts.params
268
271
  if (!has(extra, 'cacheBuster')) extra.cacheBuster = true
269
272
  if (extra.cacheBuster) opts.query[extra.cacheBusterKey ?? '_'] = Date.now()
270
273
  if (this.config.fetch.agent || extra.agent) {
271
- opts.dispatcher = new Agent(extra.agent ?? this.config.fetch.agent)
274
+ // opts.dispatcher = new Agent(extra.agent ?? this.config.fetch.agent)
272
275
  }
273
276
  if (opts.body && extra.formData) {
274
277
  const formData = new FormData()
@@ -290,6 +293,8 @@ async function factory (pkgName) {
290
293
  const query = new URLSearchParams(opts.query)
291
294
  url += '?' + query
292
295
  }
296
+ opts.headers = opts.headers ?? {}
297
+ if (this.config.fetch.userAgent) opts.headers['User-Agent'] = this.config.fetch.userAgent
293
298
  const resp = await fetch(url, opts)
294
299
  if (extra.rawResponse) return resp
295
300
  return await resp.json()
@@ -328,7 +333,7 @@ async function factory (pkgName) {
328
333
  return crypto.createHash(type, options).update(text).digest(options.digest)
329
334
  }
330
335
 
331
- isBcryptString = (text) => {
336
+ isBcrypt = (text) => {
332
337
  // return /^\$2[ayb]\$.{56}$/.test(text)
333
338
  return /^\$2[aby]?\$\d{1,2}\$[./A-Za-z0-9]{53}$/.test(text)
334
339
  }
@@ -336,6 +341,36 @@ async function factory (pkgName) {
336
341
  isMd5 = (text) => {
337
342
  return /^[a-f0-9]{32}$/i.test(text)
338
343
  }
344
+
345
+ encrypt = async (text, { type = 'short', subType = 'qr' } = {}) => {
346
+ const short = (item) => {
347
+ const sc = new ShortCrypt(this.config.secret)
348
+ const method = subType === 'qr' ? 'encryptToQRCodeAlphanumeric' : 'encryptToURLComponent'
349
+ return sc[method](item)
350
+ }
351
+ switch (type) {
352
+ case 'short': return short(text)
353
+ }
354
+ throw this.error('invalid%s%s', this.print.write('encryption type'), type)
355
+ }
356
+
357
+ decrypt = async (cipher, { type = 'short', subType = 'qr' } = {}) => {
358
+ const short = (item) => {
359
+ const sc = new ShortCrypt(this.config.secret)
360
+ const method = subType === 'qr' ? 'decryptToQRCodeAlphanumeric' : 'decryptToURLComponent'
361
+ return sc[method](item)
362
+ }
363
+ switch (type) {
364
+ case 'short': return short(cipher)
365
+ }
366
+ throw this.error('invalid%s%s', this.print.write('decryption type'), type)
367
+ }
368
+
369
+ randomRange = (min, max, alpha) => {
370
+ const num = Math.floor(Math.random() * (max - min + 1) + min)
371
+ if (!alpha) return num
372
+ return String.fromCharCode(96 + num)
373
+ }
339
374
  }
340
375
  }
341
376