atomically
Advanced tools
Comparing version 1.2.0 to 1.3.0
declare const DEFAULT_ENCODING = "utf8"; | ||
declare const DEFAULT_MODE = 438; | ||
declare const DEFAULT_FILE_MODE = 438; | ||
declare const DEFAULT_FOLDER_MODE = 511; | ||
declare const DEFAULT_READ_OPTIONS: {}; | ||
@@ -11,2 +12,2 @@ declare const DEFAULT_WRITE_OPTIONS: {}; | ||
declare const NOOP: () => void; | ||
export { DEFAULT_ENCODING, DEFAULT_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX, IS_USER_ROOT, LIMIT_BASENAME_LENGTH, NOOP }; | ||
export { DEFAULT_ENCODING, DEFAULT_FILE_MODE, DEFAULT_FOLDER_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX, IS_USER_ROOT, LIMIT_BASENAME_LENGTH, NOOP }; |
"use strict"; | ||
/* CONSTS */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NOOP = exports.LIMIT_BASENAME_LENGTH = exports.IS_USER_ROOT = exports.IS_POSIX = exports.DEFAULT_TIMEOUT_SYNC = exports.DEFAULT_TIMEOUT_ASYNC = exports.DEFAULT_WRITE_OPTIONS = exports.DEFAULT_READ_OPTIONS = exports.DEFAULT_MODE = exports.DEFAULT_ENCODING = void 0; | ||
exports.NOOP = exports.LIMIT_BASENAME_LENGTH = exports.IS_USER_ROOT = exports.IS_POSIX = exports.DEFAULT_TIMEOUT_SYNC = exports.DEFAULT_TIMEOUT_ASYNC = exports.DEFAULT_WRITE_OPTIONS = exports.DEFAULT_READ_OPTIONS = exports.DEFAULT_FOLDER_MODE = exports.DEFAULT_FILE_MODE = exports.DEFAULT_ENCODING = void 0; | ||
const DEFAULT_ENCODING = 'utf8'; | ||
exports.DEFAULT_ENCODING = DEFAULT_ENCODING; | ||
const DEFAULT_MODE = 0o666; | ||
exports.DEFAULT_MODE = DEFAULT_MODE; | ||
const DEFAULT_FILE_MODE = 0o666; | ||
exports.DEFAULT_FILE_MODE = DEFAULT_FILE_MODE; | ||
const DEFAULT_FOLDER_MODE = 0o777; | ||
exports.DEFAULT_FOLDER_MODE = DEFAULT_FOLDER_MODE; | ||
const DEFAULT_READ_OPTIONS = {}; | ||
@@ -10,0 +12,0 @@ exports.DEFAULT_READ_OPTIONS = DEFAULT_READ_OPTIONS; |
@@ -5,2 +5,3 @@ "use strict"; | ||
exports.writeFileSync = exports.writeFile = exports.readFileSync = exports.readFile = void 0; | ||
const path = require("path"); | ||
const consts_1 = require("./consts"); | ||
@@ -61,3 +62,8 @@ const fs_1 = require("./utils/fs"); | ||
} | ||
fd = await fs_1.default.openRetry(timeout)(tempPath, 'w', options.mode || consts_1.DEFAULT_MODE); | ||
const parentPath = path.dirname(filePath); | ||
await fs_1.default.mkdirAttempt(parentPath, { | ||
mode: consts_1.DEFAULT_FOLDER_MODE, | ||
recursive: true | ||
}); | ||
fd = await fs_1.default.openRetry(timeout)(tempPath, 'w', options.mode || consts_1.DEFAULT_FILE_MODE); | ||
if (options.tmpCreated) | ||
@@ -127,3 +133,8 @@ options.tmpCreated(tempPath); | ||
} | ||
fd = fs_1.default.openSyncRetry(timeout)(tempPath, 'w', options.mode || consts_1.DEFAULT_MODE); | ||
const parentPath = path.dirname(filePath); | ||
fs_1.default.mkdirSyncAttempt(parentPath, { | ||
mode: consts_1.DEFAULT_FOLDER_MODE, | ||
recursive: true | ||
}); | ||
fd = fs_1.default.openSyncRetry(timeout)(tempPath, 'w', options.mode || consts_1.DEFAULT_FILE_MODE); | ||
if (options.tmpCreated) | ||
@@ -130,0 +141,0 @@ options.tmpCreated(tempPath); |
@@ -8,2 +8,3 @@ /// <reference types="node" /> | ||
fsyncAttempt: typeof fs.fsync.__promisify__; | ||
mkdirAttempt: typeof fs.mkdir.__promisify__; | ||
realpathAttempt: typeof fs.realpath.__promisify__; | ||
@@ -21,2 +22,3 @@ statAttempt: typeof fs.stat.__promisify__; | ||
closeSyncAttempt: typeof fs.closeSync; | ||
mkdirSyncAttempt: typeof fs.mkdirSync; | ||
realpathSyncAttempt: typeof fs.realpathSync; | ||
@@ -23,0 +25,0 @@ statSyncAttempt: typeof fs.statSync; |
@@ -15,2 +15,3 @@ "use strict"; | ||
fsyncAttempt: attemptify_1.attemptifyAsync(util_1.promisify(fs.fsync)), | ||
mkdirAttempt: attemptify_1.attemptifyAsync(util_1.promisify(fs.mkdir)), | ||
realpathAttempt: attemptify_1.attemptifyAsync(util_1.promisify(fs.realpath)), | ||
@@ -28,2 +29,3 @@ statAttempt: attemptify_1.attemptifyAsync(util_1.promisify(fs.stat)), | ||
closeSyncAttempt: attemptify_1.attemptifySync(fs.closeSync), | ||
mkdirSyncAttempt: attemptify_1.attemptifySync(fs.mkdirSync), | ||
realpathSyncAttempt: attemptify_1.attemptifySync(fs.realpathSync), | ||
@@ -30,0 +32,0 @@ statSyncAttempt: attemptify_1.attemptifySync(fs.statSync), |
{ | ||
"name": "atomically", | ||
"description": "Read and write files atomically and reliably.", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "types": "dist/index.d.ts", |
@@ -18,2 +18,3 @@ # Atomically | ||
- Temporary files can be configured to not be purged from disk if the write operation fails, which is useful for when keeping the temporary file is better than just losing data. | ||
- Any needed missing parent folder will be created automatically. | ||
- Symlinks are resolved automatically. | ||
@@ -20,0 +21,0 @@ - `ENOSYS` errors on `chmod`/`chown` operations are ignored. |
@@ -6,4 +6,6 @@ | ||
const DEFAULT_MODE = 0o666; | ||
const DEFAULT_FILE_MODE = 0o666; | ||
const DEFAULT_FOLDER_MODE = 0o777; | ||
const DEFAULT_READ_OPTIONS = {}; | ||
@@ -27,2 +29,2 @@ | ||
export {DEFAULT_ENCODING, DEFAULT_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX, IS_USER_ROOT, LIMIT_BASENAME_LENGTH, NOOP}; | ||
export {DEFAULT_ENCODING, DEFAULT_FILE_MODE, DEFAULT_FOLDER_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX, IS_USER_ROOT, LIMIT_BASENAME_LENGTH, NOOP}; |
/* IMPORT */ | ||
import {DEFAULT_ENCODING, DEFAULT_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX} from './consts'; | ||
import * as path from 'path'; | ||
import {DEFAULT_ENCODING, DEFAULT_FILE_MODE, DEFAULT_FOLDER_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX} from './consts'; | ||
import FS from './utils/fs'; | ||
@@ -90,4 +91,11 @@ import Lang from './utils/lang'; | ||
fd = await FS.openRetry ( timeout )( tempPath, 'w', options.mode || DEFAULT_MODE ); | ||
const parentPath = path.dirname ( filePath ); | ||
await FS.mkdirAttempt ( parentPath, { | ||
mode: DEFAULT_FOLDER_MODE, | ||
recursive: true | ||
}); | ||
fd = await FS.openRetry ( timeout )( tempPath, 'w', options.mode || DEFAULT_FILE_MODE ); | ||
if ( options.tmpCreated ) options.tmpCreated ( tempPath ); | ||
@@ -192,4 +200,11 @@ | ||
fd = FS.openSyncRetry ( timeout )( tempPath, 'w', options.mode || DEFAULT_MODE ); | ||
const parentPath = path.dirname ( filePath ); | ||
FS.mkdirSyncAttempt ( parentPath, { | ||
mode: DEFAULT_FOLDER_MODE, | ||
recursive: true | ||
}); | ||
fd = FS.openSyncRetry ( timeout )( tempPath, 'w', options.mode || DEFAULT_FILE_MODE ); | ||
if ( options.tmpCreated ) options.tmpCreated ( tempPath ); | ||
@@ -196,0 +211,0 @@ |
@@ -18,2 +18,3 @@ | ||
fsyncAttempt: attemptifyAsync ( promisify ( fs.fsync ) ), | ||
mkdirAttempt: attemptifyAsync ( promisify ( fs.mkdir ) ), | ||
realpathAttempt: attemptifyAsync ( promisify ( fs.realpath ) ), | ||
@@ -33,2 +34,3 @@ statAttempt: attemptifyAsync ( promisify ( fs.stat ) ), | ||
closeSyncAttempt: attemptifySync ( fs.closeSync ), | ||
mkdirSyncAttempt: attemptifySync ( fs.mkdirSync ), | ||
realpathSyncAttempt: attemptifySync ( fs.realpathSync ), | ||
@@ -35,0 +37,0 @@ statSyncAttempt: attemptifySync ( fs.statSync ), |
@@ -22,2 +22,5 @@ 'use strict' | ||
/* ASYNC */ | ||
mkdir (filename, opts, cb) { | ||
return cb(null); | ||
}, | ||
realpath (filename, cb) { | ||
@@ -72,2 +75,3 @@ return cb(null, filename) | ||
/* SYNC */ | ||
mkdirSync (filename) {}, | ||
realpathSync (filename, cb) { | ||
@@ -162,3 +166,3 @@ return filename | ||
t.test('non-root tests', t => { | ||
t.plan(27) | ||
t.plan(28) | ||
@@ -248,2 +252,6 @@ writeFileAtomic('good', 'test', { mode: '0777' }, err => { | ||
}) | ||
const pathMissingFolders = path.join(os.tmpdir(),String(Math.random()),String(Math.random()),String(Math.random()),'foo.txt'); | ||
writeFileAtomicNative(pathMissingFolders,'test', err => { | ||
t.notOk(err) | ||
}) | ||
}) | ||
@@ -302,3 +310,3 @@ | ||
t.test('non-root', t => { | ||
t.plan(37) | ||
t.plan(38) | ||
noexception(t, 'No errors occur when passing in options', () => { | ||
@@ -421,2 +429,7 @@ writeFileAtomicSync('good', 'test', { mode: '0777' }) | ||
}) | ||
const pathMissingFolders = path.join(os.tmpdir(),String(Math.random()),String(Math.random()),String(Math.random()),'foo.txt'); | ||
noexception(t, 'parent folders are created', () => { | ||
const {writeFileSync: writeFileAtomicSync} = requireInject('../dist', { fs }); | ||
writeFileAtomicSync(pathMissingFolders, 'test') | ||
}) | ||
}) | ||
@@ -423,0 +436,0 @@ |
@@ -12,2 +12,5 @@ 'use strict' | ||
/* ASYNC */ | ||
mkdir (filename, opts, cb) { | ||
return cb(null); | ||
}, | ||
realpath (filename, cb) { | ||
@@ -53,2 +56,3 @@ return cb(null, filename) | ||
/* SYNC */ | ||
mkdirSync (filename) {}, | ||
realpathSync (filename, cb) { | ||
@@ -55,0 +59,0 @@ return filename |
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
87658
1935
148