@vbarbarosh/node-helpers 3.42.0 → 3.44.0
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/package.json +1 -1
- package/src/format_hrtime.js +10 -0
- package/src/fs_fopen_update.js +9 -9
- package/src/fs_fread.js +9 -8
- package/src/fs_fwrite.js +8 -7
- package/src/fs_read_buffer.js +3 -0
- package/src/fs_read_utf8.js +3 -0
- package/src/fs_rmrf.js +3 -0
- package/src/fs_size_enoent.js +2 -4
- package/src/perf_end_human.js +8 -0
- package/src/perf_end_ms.js +7 -0
- package/src/perf_measure_human.js +11 -0
- package/src/perf_start.js +6 -0
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return a human-readable representation of hrtime (the return value from `process.hrtime(hrtime0)`).
|
|
3
|
+
*/
|
|
4
|
+
function format_hrtime(hrtime, digits = 6)
|
|
5
|
+
{
|
|
6
|
+
const [u, v] = hrtime; // process.hrtime(hrtime0)
|
|
7
|
+
return (u + v/1E9).toFixed(digits) + 's';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
module.exports = format_hrtime;
|
package/src/fs_fopen_update.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
const fs_fopen = require('./fs_fopen');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The main use case is uploading files in chunks.
|
|
5
|
+
* Each time a chunk is uploaded, update the target file.
|
|
6
|
+
* By the end, you will have a ready-to-use file.
|
|
7
|
+
*/
|
|
7
8
|
async function fs_fopen_update(pathname)
|
|
8
9
|
{
|
|
9
10
|
try {
|
|
@@ -11,23 +12,22 @@ async function fs_fopen_update(pathname)
|
|
|
11
12
|
return await fs_fopen(pathname, 'r+');
|
|
12
13
|
}
|
|
13
14
|
catch (error) {
|
|
14
|
-
if (error.code
|
|
15
|
+
if (error.code !== 'ENOENT') {
|
|
15
16
|
throw error;
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
try {
|
|
20
|
-
// There is no such
|
|
21
|
+
// There is no such file; try to create it...
|
|
21
22
|
return await fs_fopen(pathname, 'wx');
|
|
22
23
|
}
|
|
23
24
|
catch (error) {
|
|
24
|
-
if (error.code
|
|
25
|
+
if (error.code !== 'EEXIST') {
|
|
25
26
|
throw error;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
// Another process might have already created it
|
|
30
|
-
// open for updating...
|
|
30
|
+
// Another process might have already created it; try opening it for updating
|
|
31
31
|
return fs_fopen(pathname, 'r+');
|
|
32
32
|
}
|
|
33
33
|
|
package/src/fs_fread.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
-
const util = require('util');
|
|
3
|
-
|
|
4
|
-
// https://nodejs.org/api/util.html#util_util_promisify_original
|
|
5
|
-
const read = util.promisify(fs.read);
|
|
6
|
-
|
|
7
|
-
// Main use case is for reading chunks of data from file.
|
|
8
2
|
|
|
3
|
+
/**
|
|
4
|
+
* The main use case is reading chunks of data from a file.
|
|
5
|
+
*/
|
|
9
6
|
async function fs_fread(fp, buffer, offset = null)
|
|
10
7
|
{
|
|
11
|
-
const {bytesRead} = await
|
|
12
|
-
|
|
8
|
+
const {bytesRead} = await new Promise(function (resolve, reject) {
|
|
9
|
+
fs.read(fp, buffer, 0, buffer.length, offset, function (error, out) {
|
|
10
|
+
error ? reject(error) : resolve(out);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
return (bytesRead === buffer.length) ? buffer : buffer.slice(0, bytesRead);
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
module.exports = fs_fread;
|
package/src/fs_fwrite.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
-
const util = require('util');
|
|
3
|
-
|
|
4
|
-
// https://nodejs.org/api/util.html#util_util_promisify_original
|
|
5
|
-
const write = util.promisify(fs.write);
|
|
6
|
-
|
|
7
|
-
// Main use case is for writing chunks of data to file.
|
|
8
2
|
|
|
3
|
+
/**
|
|
4
|
+
* The main use case is writing chunks of data to a file.
|
|
5
|
+
*/
|
|
9
6
|
async function fs_fwrite(fp, buffer, offset = null)
|
|
10
7
|
{
|
|
11
|
-
const {bytesWritten} = await
|
|
8
|
+
const {bytesWritten} = await new Promise(function (resolve, reject) {
|
|
9
|
+
fs.write(fp, buffer, 0, buffer.length, offset, function (error, out) {
|
|
10
|
+
error ? reject(error) : resolve(out);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
12
13
|
return bytesWritten;
|
|
13
14
|
}
|
|
14
15
|
|
package/src/fs_read_buffer.js
CHANGED
package/src/fs_read_utf8.js
CHANGED
package/src/fs_rmrf.js
CHANGED
|
@@ -5,6 +5,9 @@ const fs_readdir = require('./fs_readdir');
|
|
|
5
5
|
const fs_rm = require('./fs_rm');
|
|
6
6
|
const fs_rmdir = require('./fs_rmdir');
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Remove a file or directory, along with all nested files and directories, recursively.
|
|
10
|
+
*/
|
|
8
11
|
async function fs_rmrf(pathname)
|
|
9
12
|
{
|
|
10
13
|
const fi = await fs_fi(pathname).catch(() => null);
|
package/src/fs_size_enoent.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Returns size of a file, or 0 if file is not present.
|
|
5
|
-
* when you need to resume download.
|
|
6
|
-
*
|
|
7
|
-
* @returns {Promise<int>}
|
|
4
|
+
* Returns the size of a file, or 0 if the file is not present.
|
|
5
|
+
* It was created for cases when you need to resume a download.
|
|
8
6
|
*/
|
|
9
7
|
async function fs_size_enoent(path)
|
|
10
8
|
{
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const perf_end_human = require('./perf_end_human');
|
|
2
|
+
const perf_start = require('./perf_start');
|
|
3
|
+
|
|
4
|
+
async function perf_measure_human(fn, digits = 4)
|
|
5
|
+
{
|
|
6
|
+
const time0 = perf_start();
|
|
7
|
+
await fn();
|
|
8
|
+
return perf_end_human(time0, digits);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
module.exports = perf_measure_human;
|