@strapi/utils 4.6.0-beta.2 → 4.6.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/lib/async.d.ts +12 -0
- package/lib/async.js +34 -0
- package/lib/file.js +60 -0
- package/lib/index.js +5 -1
- package/lib/sanitize/index.js +1 -1
- package/lib/sanitize/sanitizers.js +1 -1
- package/package.json +3 -2
- package/lib/pipe-async.js +0 -13
package/lib/async.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type MapAsync<T = any, R = any> = lodash.CurriedFunction3<
|
|
2
|
+
T[],
|
|
3
|
+
(element: T, index: number) => R | Promise<R>,
|
|
4
|
+
{ concurrency?: number },
|
|
5
|
+
Promise<R[]>
|
|
6
|
+
>;
|
|
7
|
+
|
|
8
|
+
export type ForEachAsync<T = any, R = any> = (
|
|
9
|
+
array: T[],
|
|
10
|
+
func: (element: T, index: number) => R | Promise<R>,
|
|
11
|
+
options?: { concurrency?: number }
|
|
12
|
+
) => Promise<R[]>;
|
package/lib/async.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const pMap = require('p-map');
|
|
4
|
+
const { curry } = require('lodash/fp');
|
|
5
|
+
|
|
6
|
+
function pipeAsync(...methods) {
|
|
7
|
+
return async (data) => {
|
|
8
|
+
let res = data;
|
|
9
|
+
|
|
10
|
+
for (const method of methods) {
|
|
11
|
+
res = await method(res);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return res;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @type { import('./async').MapAsync }
|
|
20
|
+
*/
|
|
21
|
+
const mapAsync = curry(pMap);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @type { import('./async').ForEachAsync }
|
|
25
|
+
*/
|
|
26
|
+
const forEachAsync = curry(async (array, func, options) => {
|
|
27
|
+
await mapAsync(array, func, options);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
mapAsync,
|
|
32
|
+
forEachAsync,
|
|
33
|
+
pipeAsync,
|
|
34
|
+
};
|
package/lib/file.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utils file containing file treatment utils
|
|
5
|
+
*/
|
|
6
|
+
const { Writable } = require('stream');
|
|
7
|
+
|
|
8
|
+
const kbytesToBytes = (kbytes) => kbytes * 1000;
|
|
9
|
+
const bytesToKbytes = (bytes) => Math.round((bytes / 1000) * 100) / 100;
|
|
10
|
+
const bytesToHumanReadable = (bytes) => {
|
|
11
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
|
12
|
+
if (bytes === 0) return '0 Bytes';
|
|
13
|
+
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)), 10);
|
|
14
|
+
return `${Math.round(bytes / 1000 ** i, 2)} ${sizes[i]}`;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const streamToBuffer = (stream) =>
|
|
18
|
+
new Promise((resolve, reject) => {
|
|
19
|
+
const chunks = [];
|
|
20
|
+
stream.on('data', (chunk) => {
|
|
21
|
+
chunks.push(chunk);
|
|
22
|
+
});
|
|
23
|
+
stream.on('end', () => {
|
|
24
|
+
resolve(Buffer.concat(chunks));
|
|
25
|
+
});
|
|
26
|
+
stream.on('error', reject);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const getStreamSize = (stream) =>
|
|
30
|
+
new Promise((resolve, reject) => {
|
|
31
|
+
let size = 0;
|
|
32
|
+
stream.on('data', (chunk) => {
|
|
33
|
+
size += Buffer.byteLength(chunk);
|
|
34
|
+
});
|
|
35
|
+
stream.on('close', () => resolve(size));
|
|
36
|
+
stream.on('error', reject);
|
|
37
|
+
stream.resume();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Create a writeable Node.js stream that discards received data.
|
|
42
|
+
* Useful for testing, draining a stream of data, etc.
|
|
43
|
+
*/
|
|
44
|
+
function writableDiscardStream(options) {
|
|
45
|
+
return new Writable({
|
|
46
|
+
...options,
|
|
47
|
+
write(chunk, encding, callback) {
|
|
48
|
+
setImmediate(callback);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = {
|
|
54
|
+
streamToBuffer,
|
|
55
|
+
bytesToHumanReadable,
|
|
56
|
+
bytesToKbytes,
|
|
57
|
+
kbytesToBytes,
|
|
58
|
+
getStreamSize,
|
|
59
|
+
writableDiscardStream,
|
|
60
|
+
};
|
package/lib/index.js
CHANGED
|
@@ -37,10 +37,11 @@ const providerFactory = require('./provider-factory');
|
|
|
37
37
|
const pagination = require('./pagination');
|
|
38
38
|
const sanitize = require('./sanitize');
|
|
39
39
|
const traverseEntity = require('./traverse-entity');
|
|
40
|
-
const pipeAsync = require('./
|
|
40
|
+
const { pipeAsync, mapAsync, forEachAsync } = require('./async');
|
|
41
41
|
const convertQueryParams = require('./convert-query-params');
|
|
42
42
|
const importDefault = require('./import-default');
|
|
43
43
|
const template = require('./template');
|
|
44
|
+
const file = require('./file');
|
|
44
45
|
|
|
45
46
|
module.exports = {
|
|
46
47
|
yup,
|
|
@@ -81,9 +82,12 @@ module.exports = {
|
|
|
81
82
|
providerFactory,
|
|
82
83
|
pagination,
|
|
83
84
|
pipeAsync,
|
|
85
|
+
mapAsync,
|
|
86
|
+
forEachAsync,
|
|
84
87
|
errors,
|
|
85
88
|
validateYupSchema,
|
|
86
89
|
validateYupSchemaSync,
|
|
87
90
|
convertQueryParams,
|
|
88
91
|
importDefault,
|
|
92
|
+
file,
|
|
89
93
|
};
|
package/lib/sanitize/index.js
CHANGED
|
@@ -4,7 +4,7 @@ const { isArray } = require('lodash/fp');
|
|
|
4
4
|
|
|
5
5
|
const traverseEntity = require('../traverse-entity');
|
|
6
6
|
const { getNonWritableAttributes } = require('../content-types');
|
|
7
|
-
const pipeAsync = require('../
|
|
7
|
+
const { pipeAsync } = require('../async');
|
|
8
8
|
|
|
9
9
|
const visitors = require('./visitors');
|
|
10
10
|
const sanitizers = require('./sanitizers');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/utils",
|
|
3
|
-
"version": "4.6.
|
|
3
|
+
"version": "4.6.1",
|
|
4
4
|
"description": "Shared utilities for the Strapi packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -39,11 +39,12 @@
|
|
|
39
39
|
"date-fns": "2.29.3",
|
|
40
40
|
"http-errors": "1.8.1",
|
|
41
41
|
"lodash": "4.17.21",
|
|
42
|
+
"p-map": "4.0.0",
|
|
42
43
|
"yup": "0.32.9"
|
|
43
44
|
},
|
|
44
45
|
"engines": {
|
|
45
46
|
"node": ">=14.19.1 <=18.x.x",
|
|
46
47
|
"npm": ">=6.0.0"
|
|
47
48
|
},
|
|
48
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "17a7845e3d453ea2e7911bda6ec25ed196dd5f16"
|
|
49
50
|
}
|