bajo-extra 0.3.7 → 1.0.1
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/README.md +23 -1
- package/bajo/.alias +1 -0
- package/bajo/config.json +0 -1
- package/bajo/{helper → method}/count-file-lines.js +1 -2
- package/bajo/{helper → method}/download.js +7 -7
- package/bajo/{helper → method}/fetch-and-save.js +5 -6
- package/bajo/{helper → method}/fetch-bulk.js +11 -13
- package/bajo/{helper → method}/fetch.js +5 -5
- package/bajo/{helper → method}/gzip.js +1 -1
- package/bajoCli/applet/download.js +15 -0
- package/bajoCli/applet.js +1 -0
- package/bajoI18N/resource/id.json +5 -0
- package/package.json +5 -5
- package/bajoCli/tool/download.js +0 -17
- package/bajoCli/tool.js +0 -1
- /package/bajo/{helper → method}/format/byte.js +0 -0
- /package/bajo/{helper → method}/format/float.js +0 -0
- /package/bajo/{helper → method}/format/integer.js +0 -0
- /package/bajo/{helper → method}/format/percentage.js +0 -0
- /package/bajo/{helper → method}/gunzip.js +0 -0
- /package/bajo/{helper → method}/hash.js +0 -0
- /package/bajo/{helper → method}/is-bcrypt.js +0 -0
- /package/bajo/{helper → method}/is-md5.js +0 -0
package/README.md
CHANGED
|
@@ -1 +1,23 @@
|
|
|
1
|
-
# bajo-extra
|
|
1
|
+
# bajo-extra
|
|
2
|
+
|
|
3
|
+
Plugin name: **bajoExtra**, alias: **extra**
|
|
4
|
+
|
|
5
|
+
 
|
|
6
|
+
|
|
7
|
+
> <br />**Attention**: I do NOT accept any pull request at the moment, thanks!<br /><br />
|
|
8
|
+
|
|
9
|
+
Extra tools & support for [Bajo](https://github.com/ardhi/bajo)
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Goto your ```<bajo-base-dir>``` and type:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
$ npm install bajo-extra
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Now open your ```<bajo-data-dir>/config/.plugins``` and put ```bajo-extra``` in it
|
|
20
|
+
|
|
21
|
+
## License
|
|
22
|
+
|
|
23
|
+
[MIT](LICENSE)
|
package/bajo/.alias
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
extra
|
package/bajo/config.json
CHANGED
|
@@ -2,16 +2,16 @@ import path from 'path'
|
|
|
2
2
|
import { Readable } from 'node:stream'
|
|
3
3
|
|
|
4
4
|
async function download (url, opts = {}, extra = {}) {
|
|
5
|
-
const {
|
|
6
|
-
const {
|
|
7
|
-
const { isFunction, merge } = this.bajo.
|
|
5
|
+
const { getPluginDataDir, importPkg, generateId } = this.app.bajo
|
|
6
|
+
const { fs } = this.app.bajo.lib
|
|
7
|
+
const { isFunction, merge } = this.app.bajo.lib._
|
|
8
8
|
if (typeof opts === 'string') extra = { dir: opts }
|
|
9
9
|
const increment = await importPkg('add-filename-increment')
|
|
10
10
|
if (!extra.dir) {
|
|
11
11
|
extra.dir = `${getPluginDataDir('bajoExtra')}/download`
|
|
12
12
|
fs.ensureDirSync(extra.dir)
|
|
13
13
|
}
|
|
14
|
-
if (!fs.existsSync(extra.dir)) throw error('Download dir \'%s\' doesn\'t
|
|
14
|
+
if (!fs.existsSync(extra.dir)) throw this.error('Download dir \'%s\' doesn\'t exist', extra.dir)
|
|
15
15
|
if (extra.randomFileName) {
|
|
16
16
|
const ext = path.extname(url)
|
|
17
17
|
extra.fileName = `${generateId()}${ext}`
|
|
@@ -22,7 +22,7 @@ async function download (url, opts = {}, extra = {}) {
|
|
|
22
22
|
const { headers, body, ok, status } = await fetch(url, opts, merge({}, extra, { rawResponse: true }))
|
|
23
23
|
if (!ok) {
|
|
24
24
|
fs.removeSync(file)
|
|
25
|
-
throw error('Getting %s status', status)
|
|
25
|
+
throw this.error('Getting %s status', status)
|
|
26
26
|
}
|
|
27
27
|
const total = headers['content-length'] ?? 0
|
|
28
28
|
const data = Readable.fromWeb(body)
|
|
@@ -32,8 +32,8 @@ async function download (url, opts = {}, extra = {}) {
|
|
|
32
32
|
if (isFunction(extra.progressFn)) extra.progressFn.call(this, length, total)
|
|
33
33
|
else if (extra.spin) {
|
|
34
34
|
extra.spinText = extra.spinText ?? 'Downloading...'
|
|
35
|
-
if (total === 0) extra.spin.setText(`${extra.spinText} %s`, formatByte(length))
|
|
36
|
-
else extra.spin.setText(`${extra.spinText} %s of %s (%s)`, formatByte(length), formatByte(total), formatPercentage(length / total))
|
|
35
|
+
if (total === 0) extra.spin.setText(`${extra.spinText} %s`, this.formatByte(length))
|
|
36
|
+
else extra.spin.setText(`${extra.spinText} %s of %s (%s)`, this.formatByte(length), this.formatByte(total), this.formatPercentage(length / total))
|
|
37
37
|
}
|
|
38
38
|
})
|
|
39
39
|
data.pipe(writer)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
async function handler (rec, bulk) {
|
|
2
|
-
const { isFunction, set } = this.bajo.
|
|
3
|
-
const { recordCreate, recordFind, recordUpdate } = this.bajoDb
|
|
2
|
+
const { isFunction, set } = this.app.bajo.lib._
|
|
3
|
+
const { recordCreate, recordFind, recordUpdate } = this.app.bajoDb
|
|
4
4
|
const save = bulk.save ?? {}
|
|
5
5
|
const current = save.current ?? {}
|
|
6
6
|
let existing
|
|
@@ -51,13 +51,12 @@ async function handler (rec, bulk) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async function fetchAndSave ({ url, bulk, save = {}, opts = {} } = {}) {
|
|
54
|
-
const { startPlugin } = this.bajo
|
|
55
|
-
const {
|
|
56
|
-
const { merge } = this.bajo.helper._
|
|
54
|
+
const { startPlugin } = this.bajo
|
|
55
|
+
const { merge } = this.bajo.lib._
|
|
57
56
|
merge(bulk, { handler, save })
|
|
58
57
|
await startPlugin('bajoDb')
|
|
59
58
|
|
|
60
|
-
await fetchBulk(url, bulk, opts)
|
|
59
|
+
await this.fetchBulk(url, bulk, opts)
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
export default fetchAndSave
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
async function fetching ({ url, opts, bulk, spin }) {
|
|
2
|
-
const { setImmediate, print } = this.bajo
|
|
3
|
-
const { isEmpty, isFunction, has } = this.bajo.
|
|
4
|
-
const { validationErrorMessage } = this.bajoDb
|
|
5
|
-
const
|
|
6
|
-
const resp = await fetch(url, opts ?? {})
|
|
2
|
+
const { setImmediate, print } = this.app.bajo
|
|
3
|
+
const { isEmpty, isFunction, has } = this.app.bajo.lib._
|
|
4
|
+
const { validationErrorMessage } = this.app.bajoDb
|
|
5
|
+
const resp = await this.fetch(url, opts ?? {})
|
|
7
6
|
if (isEmpty(resp)) {
|
|
8
7
|
spin.fatal('No result from server, aborted!')
|
|
9
8
|
return -1
|
|
@@ -49,28 +48,27 @@ async function fetching ({ url, opts, bulk, spin }) {
|
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
async function fetchBulk (url, bulk = {}, opts = {}) {
|
|
52
|
-
const {
|
|
53
|
-
const { isFunction } = this.bajo.helper._
|
|
51
|
+
const { isFunction } = this.bajo.lib._
|
|
54
52
|
opts.params = opts.params ?? {}
|
|
55
53
|
bulk.maxStep = bulk.maxStep ?? 0
|
|
56
|
-
if (!isFunction(bulk.handler)) throw error('A function handler must be provided')
|
|
54
|
+
if (!isFunction(bulk.handler)) throw this.error('A function handler must be provided')
|
|
57
55
|
if (isFunction(bulk.paramsIncFn)) {
|
|
58
|
-
print.info('Bulk fetch starting')
|
|
59
|
-
const spin = spinner({ showCounter: true }).start('Fetching starts...')
|
|
56
|
+
this.print.info('Bulk fetch starting')
|
|
57
|
+
const spin = this.print.spinner({ showCounter: true }).start('Fetching starts...')
|
|
60
58
|
let step = 1
|
|
61
59
|
for (;;) {
|
|
62
|
-
print.info('[%s] Batch #%d', spin.getElapsed(), step)
|
|
60
|
+
this.print.info('[%s] Batch #%d', spin.getElapsed(), step)
|
|
63
61
|
const newOpts = await bulk.paramsIncFn.call(this, { url, bulk, opts })
|
|
64
62
|
if (newOpts) opts = newOpts
|
|
65
63
|
const length = await fetching.call(this, { url, bulk, opts, spin })
|
|
66
64
|
if (length === 0 || (bulk.maxStep > 0 && step >= bulk.maxStep)) {
|
|
67
|
-
print.info('All done!')
|
|
65
|
+
this.print.info('All done!')
|
|
68
66
|
break
|
|
69
67
|
}
|
|
70
68
|
step++
|
|
71
69
|
}
|
|
72
70
|
} else {
|
|
73
|
-
const spin = spinner({ showCounter: true }).start('Fetching starts...')
|
|
71
|
+
const spin = this.print.spinner({ showCounter: true }).start('Fetching starts...')
|
|
74
72
|
await fetching.call(this, { url, bulk, opts, spin })
|
|
75
73
|
}
|
|
76
74
|
}
|
|
@@ -2,9 +2,9 @@ import path from 'path'
|
|
|
2
2
|
import { fetch, Agent } from 'undici'
|
|
3
3
|
|
|
4
4
|
async function fetchUrl (url, opts = {}, extra = {}) {
|
|
5
|
-
const {
|
|
6
|
-
const {
|
|
7
|
-
const
|
|
5
|
+
const { isSet } = this.app.bajo
|
|
6
|
+
const { fs } = this.app.bajo.lib
|
|
7
|
+
const { has, isArray, isPlainObject, isString, cloneDeep, merge } = this.app.bajo.lib._
|
|
8
8
|
if (isPlainObject(url)) {
|
|
9
9
|
extra = cloneDeep(opts)
|
|
10
10
|
opts = cloneDeep(url)
|
|
@@ -20,8 +20,8 @@ async function fetchUrl (url, opts = {}, extra = {}) {
|
|
|
20
20
|
delete opts.params
|
|
21
21
|
if (!has(extra, 'cacheBuster')) extra.cacheBuster = true
|
|
22
22
|
if (extra.cacheBuster) opts.query[extra.cacheBusterKey ?? '_'] = Date.now()
|
|
23
|
-
if (
|
|
24
|
-
opts.dispatcher = new Agent(
|
|
23
|
+
if (this.config.fetch.agent || extra.agent) {
|
|
24
|
+
opts.dispatcher = new Agent(extra.agent ?? this.config.fetch.agent)
|
|
25
25
|
}
|
|
26
26
|
if (opts.body && extra.formData) {
|
|
27
27
|
const formData = new FormData()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createGzip, createGunzip } from 'zlib'
|
|
2
2
|
|
|
3
3
|
function gzip (file, deleteOld, expand) {
|
|
4
|
-
const { fs } = this.bajo.
|
|
4
|
+
const { fs } = this.app.bajo.lib
|
|
5
5
|
return new Promise((resolve, reject) => {
|
|
6
6
|
const newFile = expand ? file.slice(0, file.length - 3) : (file + '.gz')
|
|
7
7
|
const reader = fs.createReadStream(file)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
async function download (...args) {
|
|
2
|
+
const [url] = args
|
|
3
|
+
const spinText = 'Downloading file...'
|
|
4
|
+
const spin = this.print.spinner({ showCounter: true }).start(spinText)
|
|
5
|
+
|
|
6
|
+
let dest
|
|
7
|
+
try {
|
|
8
|
+
dest = await this.download(url, undefined, { spin, spinText })
|
|
9
|
+
} catch (err) {
|
|
10
|
+
spin.fatal('Error: %s', err.message)
|
|
11
|
+
}
|
|
12
|
+
spin.succeed('File saved as \'%s\'', dest)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default download
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default 'default'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bajo-extra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Extra package for Bajo Framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/ardhi/bajo-extra#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"async": "^3.2.
|
|
29
|
+
"async": "^3.2.6",
|
|
30
30
|
"bcrypt": "^5.1.1",
|
|
31
|
-
"fast-jwt": "^
|
|
32
|
-
"fast-xml-parser": "^4.
|
|
31
|
+
"fast-jwt": "^5.0.2",
|
|
32
|
+
"fast-xml-parser": "^4.5.1",
|
|
33
33
|
"numbro": "^2.5.0",
|
|
34
34
|
"performant-array-to-tree": "^1.11.0",
|
|
35
|
-
"undici": "^
|
|
35
|
+
"undici": "^7.2.1"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/bajoCli/tool/download.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
async function download ({ path, args }) {
|
|
2
|
-
const { spinner } = this.bajo.helper
|
|
3
|
-
const { download } = this.bajoExtra.helper
|
|
4
|
-
const url = args[0]
|
|
5
|
-
const spinText = 'Downloading file...'
|
|
6
|
-
const spin = spinner({ showCounter: true }).start(spinText)
|
|
7
|
-
|
|
8
|
-
let dest
|
|
9
|
-
try {
|
|
10
|
-
dest = await download(url, undefined, { spin, spinText })
|
|
11
|
-
} catch (err) {
|
|
12
|
-
spin.fatal('Error: %s', err.message)
|
|
13
|
-
}
|
|
14
|
-
spin.succeed('File saved as \'%s\'', dest)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default download
|
package/bajoCli/tool.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default 'defCliHandler'
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|