bajo-extra 0.2.15 → 0.2.17
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/bajo/config.json +3 -0
- package/bajo/helper/count-file-lines.js +23 -0
- package/bajo/helper/download.js +45 -0
- package/bajo/helper/fetch.js +9 -2
- package/bajo/helper/format/byte.js +11 -0
- package/bajo/helper/format/float.js +9 -0
- package/bajo/helper/format/integer.js +9 -0
- package/bajo/helper/format/percentage.js +10 -0
- package/bajo/helper/gzip.js +3 -3
- package/bajo/helper/hash.js +9 -3
- package/bajo/helper/import-from.js +9 -6
- package/bajoCli/tool/download.js +17 -0
- package/package.json +2 -2
package/bajo/config.json
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// taken from: https://stackoverflow.com/a/41439945
|
|
2
|
+
import fs from 'fs'
|
|
3
|
+
|
|
4
|
+
function countFileLines (file) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
let lineCount = 0
|
|
7
|
+
fs.createReadStream(file)
|
|
8
|
+
.on('data', (buffer) => {
|
|
9
|
+
let idx = -1
|
|
10
|
+
lineCount--
|
|
11
|
+
do {
|
|
12
|
+
idx = buffer.indexOf(10, idx + 1)
|
|
13
|
+
lineCount++
|
|
14
|
+
} while (idx !== -1)
|
|
15
|
+
})
|
|
16
|
+
.on('end', () => {
|
|
17
|
+
resolve(lineCount)
|
|
18
|
+
})
|
|
19
|
+
.on('error', reject)
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default countFileLines
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
|
|
3
|
+
async function download (url, opts = {}) {
|
|
4
|
+
const { getPluginDataDir, importPkg, error, generateId } = this.bajo.helper
|
|
5
|
+
const { fetch, formatByte, formatPercentage } = this.bajoExtra.helper
|
|
6
|
+
const { isFunction } = await importPkg('lodash-es')
|
|
7
|
+
if (typeof opts === 'string') opts = { dir: opts }
|
|
8
|
+
const [fs, increment] = await importPkg('fs-extra', 'add-filename-increment')
|
|
9
|
+
if (!opts.dir) {
|
|
10
|
+
opts.dir = `${getPluginDataDir('bajoExtra')}/download`
|
|
11
|
+
fs.ensureDirSync(opts.dir)
|
|
12
|
+
}
|
|
13
|
+
const fetchOpts = opts.fetchOpts ?? {}
|
|
14
|
+
if (!fs.existsSync(opts.dir)) throw error('Download dir \'%s\' doesn\'t exists', opts.dir)
|
|
15
|
+
if (opts.randomFileName) {
|
|
16
|
+
const ext = path.extname(url)
|
|
17
|
+
opts.fileName = `${generateId()}${ext}`
|
|
18
|
+
}
|
|
19
|
+
if (!opts.fileName) opts.fileName = path.basename(url)
|
|
20
|
+
const file = path.resolve(increment(`${opts.dir}/${opts.fileName}`, { fs: true }))
|
|
21
|
+
const writer = fs.createWriteStream(file)
|
|
22
|
+
fetchOpts.responseType = 'stream'
|
|
23
|
+
const { headers, data } = await fetch(url, fetchOpts, { rawResponse: true })
|
|
24
|
+
const total = headers['content-length'] ?? 0
|
|
25
|
+
let length = 0
|
|
26
|
+
data.on('data', chunk => {
|
|
27
|
+
length += chunk.length
|
|
28
|
+
if (isFunction(opts.progressFn)) opts.progressFn.call(this, length, total)
|
|
29
|
+
else if (opts.spin) {
|
|
30
|
+
opts.spinText = opts.spinText ?? 'Downloading...'
|
|
31
|
+
if (total === 0) opts.spin.setText(`${opts.spinText} %s`, formatByte(length))
|
|
32
|
+
else opts.spin.setText(`${opts.spinText} %s of %s (%s)`, formatByte(length), formatByte(total), formatPercentage(length / total))
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
data.pipe(writer)
|
|
36
|
+
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
writer.on('error', reject)
|
|
39
|
+
writer.on('finish', () => {
|
|
40
|
+
resolve(file)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default download
|
package/bajo/helper/fetch.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
|
+
import http from 'http'
|
|
3
|
+
import https from 'https'
|
|
2
4
|
|
|
3
5
|
async function fetch (url, opts = {}, ext = {}) {
|
|
4
|
-
const { importPkg } = this.bajo.helper
|
|
5
|
-
const { has, isPlainObject, cloneDeep } = await importPkg('lodash-es')
|
|
6
|
+
const { importPkg, getConfig } = this.bajo.helper
|
|
7
|
+
const { has, isPlainObject, cloneDeep, isEmpty } = await importPkg('lodash-es')
|
|
8
|
+
const cfg = getConfig('bajoExtra')
|
|
6
9
|
if (isPlainObject(url)) {
|
|
7
10
|
ext = cloneDeep(opts)
|
|
8
11
|
opts = cloneDeep(url)
|
|
@@ -10,6 +13,10 @@ async function fetch (url, opts = {}, ext = {}) {
|
|
|
10
13
|
opts.params = opts.params ?? {}
|
|
11
14
|
if (!has(ext, 'cacheBuster')) ext.cacheBuster = true
|
|
12
15
|
if (ext.cacheBuster) opts.params[ext.cacheBusterKey ?? '_'] = Date.now()
|
|
16
|
+
if (!isEmpty(cfg.fetch.agent)) {
|
|
17
|
+
opts.httpAgent = opts.httpAgent ?? new http.Agent(cfg.fetch.agent)
|
|
18
|
+
opts.httpsAgent = opts.httpsAgent ?? new https.Agent(cfg.fetch.agent)
|
|
19
|
+
}
|
|
13
20
|
const resp = await axios(opts)
|
|
14
21
|
if (ext.rawResponse) return resp
|
|
15
22
|
return resp.data
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import numbro from 'numbro'
|
|
2
|
+
|
|
3
|
+
function byte (value, opts = {}) {
|
|
4
|
+
opts.output = 'byte'
|
|
5
|
+
opts.base = 'binary'
|
|
6
|
+
opts.mantissa = opts.mantissa ?? opts.scale ?? 2
|
|
7
|
+
opts.spaceSeparated = opts.spaceSeparated ?? true
|
|
8
|
+
return numbro(value).format(opts)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default byte
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import numbro from 'numbro'
|
|
2
|
+
|
|
3
|
+
function percentage (value, opts = {}) {
|
|
4
|
+
opts.output = 'percent'
|
|
5
|
+
opts.mantissa = opts.mantissa ?? opts.scale ?? 2
|
|
6
|
+
opts.spaceSeparated = opts.spaceSeparated ?? true
|
|
7
|
+
return numbro(value).format(opts)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default percentage
|
package/bajo/helper/gzip.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { createGzip, createGunzip } from 'zlib'
|
|
2
2
|
|
|
3
|
-
function gzip (file, deleteOld,
|
|
3
|
+
function gzip (file, deleteOld, expand) {
|
|
4
4
|
return new Promise((resolve, reject) => {
|
|
5
5
|
const { importPkg } = this.bajo.helper
|
|
6
6
|
importPkg('fs-extra')
|
|
7
7
|
.then(fs => {
|
|
8
|
-
const newFile =
|
|
8
|
+
const newFile = expand ? file.slice(0, file.length - 3) : (file + '.gz')
|
|
9
9
|
const reader = fs.createReadStream(file)
|
|
10
10
|
const writer = fs.createWriteStream(newFile)
|
|
11
|
-
const method =
|
|
11
|
+
const method = expand ? createGunzip() : createGzip()
|
|
12
12
|
reader.pipe(method).pipe(writer)
|
|
13
13
|
writer.on('error', reject)
|
|
14
14
|
writer.on('finish', err => {
|
package/bajo/helper/hash.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import bcrypt from 'bcrypt'
|
|
2
2
|
import crypto from 'crypto'
|
|
3
3
|
|
|
4
|
-
async function hash (text, type = 'md5',
|
|
4
|
+
async function hash (text, type = 'md5', options = {}) {
|
|
5
|
+
options.digest = options.digest ?? 'hex'
|
|
6
|
+
options.salt = options.hash ?? 10
|
|
5
7
|
if (typeof text !== 'string') text = JSON.stringify(text)
|
|
6
|
-
if (type === 'bcrypt') return await bcrypt.hash(text, salt)
|
|
7
|
-
|
|
8
|
+
if (type === 'bcrypt') return await bcrypt.hash(text, options.salt)
|
|
9
|
+
if (type === 'short') {
|
|
10
|
+
type = 'shake256'
|
|
11
|
+
options.outputLength = 6
|
|
12
|
+
}
|
|
13
|
+
return crypto.createHash(type, options).update(text).digest(options.digest)
|
|
8
14
|
}
|
|
9
15
|
|
|
10
16
|
export default hash
|
|
@@ -9,9 +9,10 @@ const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx', '.tsv']
|
|
|
9
9
|
|
|
10
10
|
async function importFrom (source, dest, { trashOld = true, batch = 1, progressFn, converterFn, useHeader = true, fileType, createOpts = {} } = {}, opts = {}) {
|
|
11
11
|
const { error, importPkg, getConfig, getPluginDataDir } = this.bajo.helper
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
if (dest !== false) {
|
|
13
|
+
if (!this.bajoDb) throw error('Bajo DB isn\'t loaded')
|
|
14
|
+
await this.bajoDb.helper.getInfo(dest)
|
|
15
|
+
}
|
|
15
16
|
const { merge } = await importPkg('lodash-es')
|
|
16
17
|
const fs = await importPkg('fs-extra')
|
|
17
18
|
const cfg = getConfig('bajoExtra')
|
|
@@ -30,7 +31,7 @@ async function importFrom (source, dest, { trashOld = true, batch = 1, progressF
|
|
|
30
31
|
decompress = true
|
|
31
32
|
}
|
|
32
33
|
if (!supportedExt.includes(ext)) throw error('Unsupported format \'%s\'', ext.slice(1))
|
|
33
|
-
if (trashOld) await recordClear(dest)
|
|
34
|
+
if (trashOld && dest !== false) await this.bajoDb.helper.recordClear(dest)
|
|
34
35
|
const reader = fs.createReadStream(file)
|
|
35
36
|
batch = parseInt(batch) || 100
|
|
36
37
|
if (batch > cfg.stream.import.maxBatch) batch = cfg.stream.import.maxBatch
|
|
@@ -46,6 +47,7 @@ async function importFrom (source, dest, { trashOld = true, batch = 1, progressF
|
|
|
46
47
|
|
|
47
48
|
const stream = DataStream.pipeline(...pipes)
|
|
48
49
|
let batchNo = 1
|
|
50
|
+
const data = []
|
|
49
51
|
await stream
|
|
50
52
|
.batch(batch)
|
|
51
53
|
.map(async items => {
|
|
@@ -54,14 +56,15 @@ async function importFrom (source, dest, { trashOld = true, batch = 1, progressF
|
|
|
54
56
|
for (let item of items) {
|
|
55
57
|
count++
|
|
56
58
|
item = converterFn ? await converterFn.call(this, item) : item
|
|
57
|
-
await recordCreate(dest, item, createOpts)
|
|
59
|
+
if (dest !== false) await this.bajoDb.helper.recordCreate(dest, item, createOpts)
|
|
60
|
+
else data.push(item)
|
|
58
61
|
}
|
|
59
62
|
if (progressFn) await progressFn.call(this, { batchNo, data: items, batchStart, batchEnd: new Date() })
|
|
60
63
|
batchNo++
|
|
61
64
|
})
|
|
62
65
|
.run()
|
|
63
66
|
|
|
64
|
-
return { file, count }
|
|
67
|
+
return dest === false ? data : { file, count }
|
|
65
68
|
}
|
|
66
69
|
|
|
67
70
|
export default importFrom
|
|
@@ -0,0 +1,17 @@
|
|
|
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, { 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bajo-extra",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.17",
|
|
4
4
|
"description": "Extra package for Bajo Framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"fast-csv": "^5.0.1",
|
|
35
35
|
"fast-jwt": "^3.2.0",
|
|
36
36
|
"fast-xml-parser": "^4.3.2",
|
|
37
|
-
"littlehash": "^1.0.1",
|
|
38
37
|
"ndjson": "^2.0.0",
|
|
38
|
+
"numbro": "^2.5.0",
|
|
39
39
|
"performant-array-to-tree": "^1.11.0",
|
|
40
40
|
"query-string": "^8.1.0",
|
|
41
41
|
"scramjet": "^4.36.9",
|