@vbarbarosh/node-helpers 2.0.3 → 2.2.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 CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
3
3
  "license": "MIT",
4
4
  "name": "@vbarbarosh/node-helpers",
5
- "version": "2.0.3",
5
+ "version": "2.2.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
package/src/fs_mkdirp.js CHANGED
@@ -19,8 +19,13 @@ async function fs_mkdirp(pathname)
19
19
  }
20
20
  catch (error) {
21
21
  // Error: EEXIST: file already exists, mkdir [...]
22
- if (error.code == 'EEXIST') {
23
- break;
22
+ if (error.code === 'EEXIST') {
23
+ // Exiting here is not correct. When two processes are running in parallel,
24
+ // one tries to create `a/b/c` and another `a`. When second process creates
25
+ // `a`, the first process will get `EEXIST` and stops (break) creating any
26
+ // nested directories, which is not correct.
27
+ // break;
28
+ continue;
24
29
  }
25
30
  throw error;
26
31
  }
@@ -12,4 +12,13 @@ describe('fs_mkdirp', function () {
12
12
  assert(await fs_fi(tmp).then(v => v.isDirectory()));
13
13
  });
14
14
  });
15
+ it('should create the same directory structure in parallel', async function () {
16
+ await fs_tempdir(async function (d) {
17
+ const tmp = fs_path_resolve(d, 'a/b/c/d/e/f/g/h');
18
+ await Promise.all(Array(10).fill(tmp).map(async function (v, i) {
19
+ await fs_mkdirp(tmp);
20
+ assert(await fs_fi(tmp).then(v => v.isDirectory()));
21
+ }));
22
+ });
23
+ });
15
24
  });
@@ -1,8 +1,8 @@
1
1
  const fs_read = require('./fs_read');
2
2
 
3
- async function fs_read_buffer(file)
3
+ function fs_read_buffer(file)
4
4
  {
5
- return await fs_read(file);
5
+ return fs_read(file);
6
6
  }
7
7
 
8
8
  module.exports = fs_read_buffer;
@@ -0,0 +1,10 @@
1
+ const fs_read = require('./fs_read');
2
+
3
+ // XXX Should support catchReturn, e.g.
4
+ // fs_read_lines(file).catchReturn(default_value)
5
+ function fs_read_lines(file)
6
+ {
7
+ return fs_read(file, {encoding: 'utf8'}).then(v => v.split('\n'));
8
+ }
9
+
10
+ module.exports = fs_read_lines;
@@ -1,8 +1,8 @@
1
1
  const fs_read = require('./fs_read');
2
2
 
3
- async function fs_read_utf8(file)
3
+ function fs_read_utf8(file)
4
4
  {
5
- return await fs_read(file, {encoding: 'utf8'});
5
+ return fs_read(file, {encoding: 'utf8'});
6
6
  }
7
7
 
8
8
  module.exports = fs_read_utf8;