Comparing version 0.1.4 to 0.1.5
@@ -5,2 +5,9 @@ # Changelog | ||
### [0.1.5](https://github.com/unjsio/unstorage/compare/v0.1.4...v0.1.5) (2021-04-16) | ||
### Bug Fixes | ||
* **fs:** race condition for ensuredir ([437cc76](https://github.com/unjsio/unstorage/commit/437cc76824fea7121107aaa45a17bd47155a6201)) | ||
### [0.1.4](https://github.com/unjsio/unstorage/compare/v0.1.3...v0.1.4) (2021-04-14) | ||
@@ -7,0 +14,0 @@ |
@@ -295,5 +295,8 @@ #!/usr/bin/env node | ||
function isNotFound(err) { | ||
return err.code === "ENOENT" || err.code === "EISDIR"; | ||
function ignoreNotfound(err) { | ||
return err.code === "ENOENT" || err.code === "EISDIR" ? null : err; | ||
} | ||
function ignoreExists(err) { | ||
return err.code === "EEXIST" ? null : err; | ||
} | ||
async function writeFile(path$1, data) { | ||
@@ -304,20 +307,16 @@ await ensuredir(path.dirname(path$1)); | ||
function readFile(path) { | ||
return fs.promises.readFile(path, "utf8").catch((err) => isNotFound(err) ? null : Promise.reject(err)); | ||
return fs.promises.readFile(path, "utf8").catch(ignoreNotfound); | ||
} | ||
function stat(path) { | ||
return fs.promises.stat(path).catch((err) => isNotFound(err) ? null : Promise.reject(err)); | ||
} | ||
function unlink(path) { | ||
return fs.promises.unlink(path).catch((err) => isNotFound(err) ? void 0 : Promise.reject(err)); | ||
return fs.promises.unlink(path).catch(ignoreNotfound); | ||
} | ||
function readdir(dir) { | ||
return fs.promises.readdir(dir, {withFileTypes: true}).catch((err) => isNotFound(err) ? [] : Promise.reject(err)); | ||
return fs.promises.readdir(dir, {withFileTypes: true}).catch(ignoreNotfound); | ||
} | ||
async function ensuredir(dir) { | ||
const _stat = await stat(dir); | ||
if (_stat && _stat.isDirectory()) { | ||
if (fs.existsSync(dir)) { | ||
return; | ||
} | ||
await ensuredir(path.dirname(dir)); | ||
await fs.promises.mkdir(dir); | ||
await ensuredir(path.dirname(dir)).catch(ignoreExists); | ||
await fs.promises.mkdir(dir).catch(ignoreExists); | ||
} | ||
@@ -324,0 +323,0 @@ async function readdirRecursive(dir, ignore) { |
/// <reference types="node" /> | ||
import { Dirent } from 'fs'; | ||
export declare function writeFile(path: string, data: string): Promise<void>; | ||
export declare function readFile(path: string): Promise<string>; | ||
export declare function stat(path: string): Promise<import("fs").Stats>; | ||
export declare function unlink(path: string): Promise<void>; | ||
export declare function readFile(path: string): Promise<any>; | ||
export declare function stat(path: string): Promise<any>; | ||
export declare function unlink(path: string): Promise<any>; | ||
export declare function readdir(dir: string): Promise<Dirent[]>; | ||
@@ -8,0 +8,0 @@ export declare function ensuredir(dir: string): Promise<void>; |
@@ -19,6 +19,10 @@ "use strict"; | ||
function isNotFound(err) { | ||
return err.code === "ENOENT" || err.code === "EISDIR"; | ||
function ignoreNotfound(err) { | ||
return err.code === "ENOENT" || err.code === "EISDIR" ? null : err; | ||
} | ||
function ignoreExists(err) { | ||
return err.code === "EEXIST" ? null : err; | ||
} | ||
async function writeFile(path, data) { | ||
@@ -30,11 +34,11 @@ await ensuredir((0, _path.dirname)(path)); | ||
function readFile(path) { | ||
return _fs.promises.readFile(path, "utf8").catch(err => isNotFound(err) ? null : Promise.reject(err)); | ||
return _fs.promises.readFile(path, "utf8").catch(ignoreNotfound); | ||
} | ||
function stat(path) { | ||
return _fs.promises.stat(path).catch(err => isNotFound(err) ? null : Promise.reject(err)); | ||
return _fs.promises.stat(path).catch(ignoreNotfound); | ||
} | ||
function unlink(path) { | ||
return _fs.promises.unlink(path).catch(err => isNotFound(err) ? void 0 : Promise.reject(err)); | ||
return _fs.promises.unlink(path).catch(ignoreNotfound); | ||
} | ||
@@ -45,14 +49,12 @@ | ||
withFileTypes: true | ||
}).catch(err => isNotFound(err) ? [] : Promise.reject(err)); | ||
}).catch(ignoreNotfound); | ||
} | ||
async function ensuredir(dir) { | ||
const _stat = await stat(dir); | ||
if (_stat && _stat.isDirectory()) { | ||
if ((0, _fs.existsSync)(dir)) { | ||
return; | ||
} | ||
await ensuredir((0, _path.dirname)(dir)); | ||
await _fs.promises.mkdir(dir); | ||
await ensuredir((0, _path.dirname)(dir)).catch(ignoreExists); | ||
await _fs.promises.mkdir(dir).catch(ignoreExists); | ||
} | ||
@@ -59,0 +61,0 @@ |
{ | ||
"name": "unstorage", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "repository": "unjsio/unstorage", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60669
1445