bajo-extra 1.1.2 → 1.1.4

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.2",
3
+ "version": "1.1.4",
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,13 +2,14 @@ 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
11
- const { isEmpty, isFunction, has } = this.app.bajo.lib._
12
+ const { isEmpty, isFunction, has } = this.lib._
12
13
  const { validationErrorMessage } = this.app.bajoDb
13
14
  const resp = await this.fetch(url, opts ?? {})
14
15
  if (isEmpty(resp)) {
@@ -56,7 +57,7 @@ async function fetching ({ url, opts, bulk, spin }) {
56
57
  }
57
58
 
58
59
  async function handler (rec, bulk) {
59
- const { isFunction, set } = this.app.bajo.lib._
60
+ const { isFunction, set } = this.lib._
60
61
  const { recordCreate, recordFind, recordUpdate } = this.app.bajoDb
61
62
  const save = bulk.save ?? {}
62
63
  const current = save.current ?? {}
@@ -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
  }
@@ -150,7 +152,7 @@ async function factory (pkgName) {
150
152
 
151
153
  // taken from: https://stackoverflow.com/a/41439945
152
154
  countFileLines = async (file) => {
153
- const { fs } = this.app.bajo.lib
155
+ const { fs } = this.lib
154
156
  return new Promise((resolve, reject) => {
155
157
  let lineCount = 0
156
158
  fs.createReadStream(file)
@@ -171,10 +173,10 @@ async function factory (pkgName) {
171
173
 
172
174
  download = async (url, opts = {}, extra = {}) => {
173
175
  const { getPluginDataDir, importPkg, generateId } = this.app.bajo
174
- const { fs } = this.app.bajo.lib
175
- const { isFunction, merge } = this.app.bajo.lib._
176
+ const { fs } = this.lib
177
+ const { isFunction, merge } = this.lib._
176
178
  if (typeof opts === 'string') extra = { dir: opts }
177
- const increment = await importPkg('add-filename-increment')
179
+ const increment = await importPkg('bajo:add-filename-increment')
178
180
  if (!extra.dir) {
179
181
  extra.dir = `${getPluginDataDir('bajoExtra')}/download`
180
182
  fs.ensureDirSync(extra.dir)
@@ -250,8 +252,8 @@ async function factory (pkgName) {
250
252
 
251
253
  fetchUrl = async (url, opts = {}, extra = {}) => {
252
254
  const { isSet } = this.app.bajo
253
- const { fs } = this.app.bajo.lib
254
- const { has, isArray, isPlainObject, isString, cloneDeep, merge } = this.app.bajo.lib._
255
+ const { fs } = this.lib
256
+ const { isEmpty, has, isArray, isPlainObject, isString, cloneDeep, merge } = this.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()
@@ -300,7 +305,7 @@ async function factory (pkgName) {
300
305
  }
301
306
 
302
307
  gzip = async (file, deleteOld, expand) => {
303
- const { fs } = this.app.bajo.lib
308
+ const { fs } = this.lib
304
309
  return new Promise((resolve, reject) => {
305
310
  const newFile = expand ? file.slice(0, file.length - 3) : (file + '.gz')
306
311
  const reader = fs.createReadStream(file)
@@ -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