initial commit
This commit is contained in:
15
node_modules/atomically/dist/constants.d.ts
generated
vendored
Normal file
15
node_modules/atomically/dist/constants.d.ts
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
declare const DEFAULT_ENCODING = "utf8";
|
||||
declare const DEFAULT_FILE_MODE = 438;
|
||||
declare const DEFAULT_FOLDER_MODE = 511;
|
||||
declare const DEFAULT_READ_OPTIONS: {};
|
||||
declare const DEFAULT_WRITE_OPTIONS: {};
|
||||
declare const DEFAULT_USER_UID: number;
|
||||
declare const DEFAULT_USER_GID: number;
|
||||
declare const DEFAULT_TIMEOUT_ASYNC = 7500;
|
||||
declare const DEFAULT_TIMEOUT_SYNC = 1000;
|
||||
declare const IS_POSIX: boolean;
|
||||
declare const IS_USER_ROOT: boolean;
|
||||
declare const LIMIT_BASENAME_LENGTH = 128;
|
||||
declare const LIMIT_FILES_DESCRIPTORS = 10000;
|
||||
declare const NOOP: () => void;
|
||||
export { DEFAULT_ENCODING, DEFAULT_FILE_MODE, DEFAULT_FOLDER_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_USER_UID, DEFAULT_USER_GID, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX, IS_USER_ROOT, LIMIT_BASENAME_LENGTH, LIMIT_FILES_DESCRIPTORS, NOOP };
|
||||
20
node_modules/atomically/dist/constants.js
generated
vendored
Normal file
20
node_modules/atomically/dist/constants.js
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
/* IMPORT */
|
||||
import os from 'node:os';
|
||||
import process from 'node:process';
|
||||
/* MAIN */
|
||||
const DEFAULT_ENCODING = 'utf8';
|
||||
const DEFAULT_FILE_MODE = 0o666;
|
||||
const DEFAULT_FOLDER_MODE = 0o777;
|
||||
const DEFAULT_READ_OPTIONS = {};
|
||||
const DEFAULT_WRITE_OPTIONS = {};
|
||||
const DEFAULT_USER_UID = os.userInfo().uid;
|
||||
const DEFAULT_USER_GID = os.userInfo().gid;
|
||||
const DEFAULT_TIMEOUT_ASYNC = 7500;
|
||||
const DEFAULT_TIMEOUT_SYNC = 1000;
|
||||
const IS_POSIX = !!process.getuid;
|
||||
const IS_USER_ROOT = process.getuid ? !process.getuid() : false;
|
||||
const LIMIT_BASENAME_LENGTH = 128; //TODO: Fetch the real limit from the filesystem //TODO: Fetch the whole-path length limit too
|
||||
const LIMIT_FILES_DESCRIPTORS = 10000; //TODO: Fetch the real limit from the filesystem
|
||||
const NOOP = () => { };
|
||||
/* EXPORT */
|
||||
export { DEFAULT_ENCODING, DEFAULT_FILE_MODE, DEFAULT_FOLDER_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_USER_UID, DEFAULT_USER_GID, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX, IS_USER_ROOT, LIMIT_BASENAME_LENGTH, LIMIT_FILES_DESCRIPTORS, NOOP };
|
||||
15
node_modules/atomically/dist/index.d.ts
generated
vendored
Normal file
15
node_modules/atomically/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
/// <reference types="node" />
|
||||
import type { Callback, Data, Encoding, Path, ReadOptions, WriteOptions } from './types';
|
||||
declare function readFile(filePath: Path, options: Encoding | ReadOptions & {
|
||||
encoding: string;
|
||||
}): Promise<string>;
|
||||
declare function readFile(filePath: Path, options?: ReadOptions): Promise<Buffer>;
|
||||
declare function readFileSync(filePath: Path, options: Encoding | ReadOptions & {
|
||||
encoding: string;
|
||||
}): string;
|
||||
declare function readFileSync(filePath: Path, options?: ReadOptions): Buffer;
|
||||
declare function writeFile(filePath: Path, data: Data, callback?: Callback): Promise<void>;
|
||||
declare function writeFile(filePath: Path, data: Data, options?: Encoding | WriteOptions, callback?: Callback): Promise<void>;
|
||||
declare function writeFileSync(filePath: Path, data: Data, options?: Encoding | WriteOptions): void;
|
||||
export { readFile, readFileSync, writeFile, writeFileSync };
|
||||
export type { Encoding, ReadOptions, WriteOptions };
|
||||
196
node_modules/atomically/dist/index.js
generated
vendored
Normal file
196
node_modules/atomically/dist/index.js
generated
vendored
Normal file
@@ -0,0 +1,196 @@
|
||||
/* IMPORT */
|
||||
import path from 'node:path';
|
||||
import fs from 'stubborn-fs';
|
||||
import { DEFAULT_ENCODING, DEFAULT_FILE_MODE, DEFAULT_FOLDER_MODE, DEFAULT_READ_OPTIONS, DEFAULT_WRITE_OPTIONS, DEFAULT_USER_UID, DEFAULT_USER_GID, DEFAULT_TIMEOUT_ASYNC, DEFAULT_TIMEOUT_SYNC, IS_POSIX } from './constants.js';
|
||||
import { isException, isFunction, isString, isUndefined } from './utils/lang.js';
|
||||
import Scheduler from './utils/scheduler.js';
|
||||
import Temp from './utils/temp.js';
|
||||
function readFile(filePath, options = DEFAULT_READ_OPTIONS) {
|
||||
if (isString(options))
|
||||
return readFile(filePath, { encoding: options });
|
||||
const timeout = Date.now() + ((options.timeout ?? DEFAULT_TIMEOUT_ASYNC) || -1);
|
||||
return fs.retry.readFile(timeout)(filePath, options);
|
||||
}
|
||||
function readFileSync(filePath, options = DEFAULT_READ_OPTIONS) {
|
||||
if (isString(options))
|
||||
return readFileSync(filePath, { encoding: options });
|
||||
const timeout = Date.now() + ((options.timeout ?? DEFAULT_TIMEOUT_SYNC) || -1);
|
||||
return fs.retry.readFileSync(timeout)(filePath, options);
|
||||
}
|
||||
function writeFile(filePath, data, options, callback) {
|
||||
if (isFunction(options))
|
||||
return writeFile(filePath, data, DEFAULT_WRITE_OPTIONS, options);
|
||||
const promise = writeFileAsync(filePath, data, options);
|
||||
if (callback)
|
||||
promise.then(callback, callback);
|
||||
return promise;
|
||||
}
|
||||
async function writeFileAsync(filePath, data, options = DEFAULT_WRITE_OPTIONS) {
|
||||
if (isString(options))
|
||||
return writeFileAsync(filePath, data, { encoding: options });
|
||||
const timeout = Date.now() + ((options.timeout ?? DEFAULT_TIMEOUT_ASYNC) || -1);
|
||||
let schedulerCustomDisposer = null;
|
||||
let schedulerDisposer = null;
|
||||
let tempDisposer = null;
|
||||
let tempPath = null;
|
||||
let fd = null;
|
||||
try {
|
||||
if (options.schedule)
|
||||
schedulerCustomDisposer = await options.schedule(filePath);
|
||||
schedulerDisposer = await Scheduler.schedule(filePath);
|
||||
const filePathReal = await fs.attempt.realpath(filePath);
|
||||
const filePathExists = !!filePathReal;
|
||||
filePath = filePathReal || filePath;
|
||||
[tempPath, tempDisposer] = Temp.get(filePath, options.tmpCreate || Temp.create, !(options.tmpPurge === false));
|
||||
const useStatChown = IS_POSIX && isUndefined(options.chown);
|
||||
const useStatMode = isUndefined(options.mode);
|
||||
if (filePathExists && (useStatChown || useStatMode)) {
|
||||
const stats = await fs.attempt.stat(filePath);
|
||||
if (stats) {
|
||||
options = { ...options };
|
||||
if (useStatChown) {
|
||||
options.chown = { uid: stats.uid, gid: stats.gid };
|
||||
}
|
||||
if (useStatMode) {
|
||||
options.mode = stats.mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!filePathExists) {
|
||||
const parentPath = path.dirname(filePath);
|
||||
await fs.attempt.mkdir(parentPath, {
|
||||
mode: DEFAULT_FOLDER_MODE,
|
||||
recursive: true
|
||||
});
|
||||
}
|
||||
fd = await fs.retry.open(timeout)(tempPath, 'w', options.mode || DEFAULT_FILE_MODE);
|
||||
if (options.tmpCreated) {
|
||||
options.tmpCreated(tempPath);
|
||||
}
|
||||
if (isString(data)) {
|
||||
await fs.retry.write(timeout)(fd, data, 0, options.encoding || DEFAULT_ENCODING);
|
||||
}
|
||||
else if (!isUndefined(data)) {
|
||||
await fs.retry.write(timeout)(fd, data, 0, data.length, 0);
|
||||
}
|
||||
if (options.fsync !== false) {
|
||||
if (options.fsyncWait !== false) {
|
||||
await fs.retry.fsync(timeout)(fd);
|
||||
}
|
||||
else {
|
||||
fs.attempt.fsync(fd);
|
||||
}
|
||||
}
|
||||
await fs.retry.close(timeout)(fd);
|
||||
fd = null;
|
||||
if (options.chown && (options.chown.uid !== DEFAULT_USER_UID || options.chown.gid !== DEFAULT_USER_GID)) {
|
||||
await fs.attempt.chown(tempPath, options.chown.uid, options.chown.gid);
|
||||
}
|
||||
if (options.mode && options.mode !== DEFAULT_FILE_MODE) {
|
||||
await fs.attempt.chmod(tempPath, options.mode);
|
||||
}
|
||||
try {
|
||||
await fs.retry.rename(timeout)(tempPath, filePath);
|
||||
}
|
||||
catch (error) {
|
||||
if (!isException(error))
|
||||
throw error;
|
||||
if (error.code !== 'ENAMETOOLONG')
|
||||
throw error;
|
||||
await fs.retry.rename(timeout)(tempPath, Temp.truncate(filePath));
|
||||
}
|
||||
tempDisposer();
|
||||
tempPath = null;
|
||||
}
|
||||
finally {
|
||||
if (fd)
|
||||
await fs.attempt.close(fd);
|
||||
if (tempPath)
|
||||
Temp.purge(tempPath);
|
||||
if (schedulerCustomDisposer)
|
||||
schedulerCustomDisposer();
|
||||
if (schedulerDisposer)
|
||||
schedulerDisposer();
|
||||
}
|
||||
}
|
||||
function writeFileSync(filePath, data, options = DEFAULT_WRITE_OPTIONS) {
|
||||
if (isString(options))
|
||||
return writeFileSync(filePath, data, { encoding: options });
|
||||
const timeout = Date.now() + ((options.timeout ?? DEFAULT_TIMEOUT_SYNC) || -1);
|
||||
let tempDisposer = null;
|
||||
let tempPath = null;
|
||||
let fd = null;
|
||||
try {
|
||||
const filePathReal = fs.attempt.realpathSync(filePath);
|
||||
const filePathExists = !!filePathReal;
|
||||
filePath = filePathReal || filePath;
|
||||
[tempPath, tempDisposer] = Temp.get(filePath, options.tmpCreate || Temp.create, !(options.tmpPurge === false));
|
||||
const useStatChown = IS_POSIX && isUndefined(options.chown);
|
||||
const useStatMode = isUndefined(options.mode);
|
||||
if (filePathExists && (useStatChown || useStatMode)) {
|
||||
const stats = fs.attempt.statSync(filePath);
|
||||
if (stats) {
|
||||
options = { ...options };
|
||||
if (useStatChown) {
|
||||
options.chown = { uid: stats.uid, gid: stats.gid };
|
||||
}
|
||||
if (useStatMode) {
|
||||
options.mode = stats.mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!filePathExists) {
|
||||
const parentPath = path.dirname(filePath);
|
||||
fs.attempt.mkdirSync(parentPath, {
|
||||
mode: DEFAULT_FOLDER_MODE,
|
||||
recursive: true
|
||||
});
|
||||
}
|
||||
fd = fs.retry.openSync(timeout)(tempPath, 'w', options.mode || DEFAULT_FILE_MODE);
|
||||
if (options.tmpCreated) {
|
||||
options.tmpCreated(tempPath);
|
||||
}
|
||||
if (isString(data)) {
|
||||
fs.retry.writeSync(timeout)(fd, data, 0, options.encoding || DEFAULT_ENCODING);
|
||||
}
|
||||
else if (!isUndefined(data)) {
|
||||
fs.retry.writeSync(timeout)(fd, data, 0, data.length, 0);
|
||||
}
|
||||
if (options.fsync !== false) {
|
||||
if (options.fsyncWait !== false) {
|
||||
fs.retry.fsyncSync(timeout)(fd);
|
||||
}
|
||||
else {
|
||||
fs.attempt.fsync(fd);
|
||||
}
|
||||
}
|
||||
fs.retry.closeSync(timeout)(fd);
|
||||
fd = null;
|
||||
if (options.chown && (options.chown.uid !== DEFAULT_USER_UID || options.chown.gid !== DEFAULT_USER_GID)) {
|
||||
fs.attempt.chownSync(tempPath, options.chown.uid, options.chown.gid);
|
||||
}
|
||||
if (options.mode && options.mode !== DEFAULT_FILE_MODE) {
|
||||
fs.attempt.chmodSync(tempPath, options.mode);
|
||||
}
|
||||
try {
|
||||
fs.retry.renameSync(timeout)(tempPath, filePath);
|
||||
}
|
||||
catch (error) {
|
||||
if (!isException(error))
|
||||
throw error;
|
||||
if (error.code !== 'ENAMETOOLONG')
|
||||
throw error;
|
||||
fs.retry.renameSync(timeout)(tempPath, Temp.truncate(filePath));
|
||||
}
|
||||
tempDisposer();
|
||||
tempPath = null;
|
||||
}
|
||||
finally {
|
||||
if (fd)
|
||||
fs.attempt.closeSync(fd);
|
||||
if (tempPath)
|
||||
Temp.purge(tempPath);
|
||||
}
|
||||
}
|
||||
/* EXPORT */
|
||||
export { readFile, readFileSync, writeFile, writeFileSync };
|
||||
28
node_modules/atomically/dist/types.d.ts
generated
vendored
Normal file
28
node_modules/atomically/dist/types.d.ts
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
/// <reference types="node" />
|
||||
type Callback = (error: Exception | void) => void;
|
||||
type Data = Uint8Array | string | undefined;
|
||||
type Disposer = () => void;
|
||||
type Encoding = 'ascii' | 'base64' | 'binary' | 'hex' | 'latin1' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2';
|
||||
type Exception = NodeJS.ErrnoException;
|
||||
type Path = string;
|
||||
type ReadOptions = {
|
||||
encoding?: Encoding | null;
|
||||
mode?: string | number | false;
|
||||
timeout?: number;
|
||||
};
|
||||
type WriteOptions = {
|
||||
chown?: {
|
||||
gid: number;
|
||||
uid: number;
|
||||
} | false;
|
||||
encoding?: Encoding | null;
|
||||
fsync?: boolean;
|
||||
fsyncWait?: boolean;
|
||||
mode?: string | number | false;
|
||||
schedule?: (filePath: string) => Promise<Disposer>;
|
||||
timeout?: number;
|
||||
tmpCreate?: (filePath: string) => string;
|
||||
tmpCreated?: (filePath: string) => void;
|
||||
tmpPurge?: boolean;
|
||||
};
|
||||
export type { Callback, Data, Disposer, Encoding, Exception, Path, ReadOptions, WriteOptions };
|
||||
2
node_modules/atomically/dist/types.js
generated
vendored
Normal file
2
node_modules/atomically/dist/types.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/* MAIN */
|
||||
export {};
|
||||
6
node_modules/atomically/dist/utils/lang.d.ts
generated
vendored
Normal file
6
node_modules/atomically/dist/utils/lang.d.ts
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
/// <reference types="node" />
|
||||
declare const isException: (value: unknown) => value is NodeJS.ErrnoException;
|
||||
declare const isFunction: (value: unknown) => value is Function;
|
||||
declare const isString: (value: unknown) => value is string;
|
||||
declare const isUndefined: (value: unknown) => value is undefined;
|
||||
export { isException, isFunction, isString, isUndefined };
|
||||
16
node_modules/atomically/dist/utils/lang.js
generated
vendored
Normal file
16
node_modules/atomically/dist/utils/lang.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
/* IMPORT */
|
||||
/* MAIN */
|
||||
const isException = (value) => {
|
||||
return (value instanceof Error) && ('code' in value);
|
||||
};
|
||||
const isFunction = (value) => {
|
||||
return (typeof value === 'function');
|
||||
};
|
||||
const isString = (value) => {
|
||||
return (typeof value === 'string');
|
||||
};
|
||||
const isUndefined = (value) => {
|
||||
return (value === undefined);
|
||||
};
|
||||
/* EXPORT */
|
||||
export { isException, isFunction, isString, isUndefined };
|
||||
6
node_modules/atomically/dist/utils/scheduler.d.ts
generated
vendored
Normal file
6
node_modules/atomically/dist/utils/scheduler.d.ts
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { Disposer } from '../types';
|
||||
declare const Scheduler: {
|
||||
next: (id: string) => void;
|
||||
schedule: (id: string) => Promise<Disposer>;
|
||||
};
|
||||
export default Scheduler;
|
||||
34
node_modules/atomically/dist/utils/scheduler.js
generated
vendored
Normal file
34
node_modules/atomically/dist/utils/scheduler.js
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
/* IMPORT */
|
||||
/* HELPERS */
|
||||
const Queues = {};
|
||||
/* MAIN */
|
||||
//TODO: Maybe publish this as a standalone package
|
||||
const Scheduler = {
|
||||
/* API */
|
||||
next: (id) => {
|
||||
const queue = Queues[id];
|
||||
if (!queue)
|
||||
return;
|
||||
queue.shift();
|
||||
const job = queue[0];
|
||||
if (job) {
|
||||
job(() => Scheduler.next(id));
|
||||
}
|
||||
else {
|
||||
delete Queues[id];
|
||||
}
|
||||
},
|
||||
schedule: (id) => {
|
||||
return new Promise(resolve => {
|
||||
let queue = Queues[id];
|
||||
if (!queue)
|
||||
queue = Queues[id] = [];
|
||||
queue.push(resolve);
|
||||
if (queue.length > 1)
|
||||
return;
|
||||
resolve(() => Scheduler.next(id));
|
||||
});
|
||||
}
|
||||
};
|
||||
/* EXPORT */
|
||||
export default Scheduler;
|
||||
11
node_modules/atomically/dist/utils/temp.d.ts
generated
vendored
Normal file
11
node_modules/atomically/dist/utils/temp.d.ts
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import type { Disposer } from '../types';
|
||||
declare const Temp: {
|
||||
store: Record<string, boolean>;
|
||||
create: (filePath: string) => string;
|
||||
get: (filePath: string, creator: (filePath: string) => string, purge?: boolean) => [string, Disposer];
|
||||
purge: (filePath: string) => void;
|
||||
purgeSync: (filePath: string) => void;
|
||||
purgeSyncAll: () => void;
|
||||
truncate: (filePath: string) => string;
|
||||
};
|
||||
export default Temp;
|
||||
59
node_modules/atomically/dist/utils/temp.js
generated
vendored
Normal file
59
node_modules/atomically/dist/utils/temp.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
/* IMPORT */
|
||||
import path from 'node:path';
|
||||
import fs from 'stubborn-fs';
|
||||
import whenExit from 'when-exit';
|
||||
import { LIMIT_BASENAME_LENGTH } from '../constants.js';
|
||||
/* MAIN */
|
||||
//TODO: Maybe publish this as a standalone package
|
||||
const Temp = {
|
||||
/* VARIABLES */
|
||||
store: {},
|
||||
/* API */
|
||||
create: (filePath) => {
|
||||
const randomness = `000000${Math.floor(Math.random() * 16777215).toString(16)}`.slice(-6); // 6 random-enough hex characters
|
||||
const timestamp = Date.now().toString().slice(-10); // 10 precise timestamp digits
|
||||
const prefix = 'tmp-';
|
||||
const suffix = `.${prefix}${timestamp}${randomness}`;
|
||||
const tempPath = `${filePath}${suffix}`;
|
||||
return tempPath;
|
||||
},
|
||||
get: (filePath, creator, purge = true) => {
|
||||
const tempPath = Temp.truncate(creator(filePath));
|
||||
if (tempPath in Temp.store)
|
||||
return Temp.get(filePath, creator, purge); // Collision found, try again
|
||||
Temp.store[tempPath] = purge;
|
||||
const disposer = () => delete Temp.store[tempPath];
|
||||
return [tempPath, disposer];
|
||||
},
|
||||
purge: (filePath) => {
|
||||
if (!Temp.store[filePath])
|
||||
return;
|
||||
delete Temp.store[filePath];
|
||||
fs.attempt.unlink(filePath);
|
||||
},
|
||||
purgeSync: (filePath) => {
|
||||
if (!Temp.store[filePath])
|
||||
return;
|
||||
delete Temp.store[filePath];
|
||||
fs.attempt.unlinkSync(filePath);
|
||||
},
|
||||
purgeSyncAll: () => {
|
||||
for (const filePath in Temp.store) {
|
||||
Temp.purgeSync(filePath);
|
||||
}
|
||||
},
|
||||
truncate: (filePath) => {
|
||||
const basename = path.basename(filePath);
|
||||
if (basename.length <= LIMIT_BASENAME_LENGTH)
|
||||
return filePath; //FIXME: Rough and quick attempt at detecting ok lengths
|
||||
const truncable = /^(\.?)(.*?)((?:\.[^.]+)?(?:\.tmp-\d{10}[a-f0-9]{6})?)$/.exec(basename);
|
||||
if (!truncable)
|
||||
return filePath; //FIXME: No truncable part detected, can't really do much without also changing the parent path, which is unsafe, hoping for the best here
|
||||
const truncationLength = basename.length - LIMIT_BASENAME_LENGTH;
|
||||
return `${filePath.slice(0, -basename.length)}${truncable[1]}${truncable[2].slice(0, -truncationLength)}${truncable[3]}`; //FIXME: The truncable part might be shorter than needed here
|
||||
}
|
||||
};
|
||||
/* INIT */
|
||||
whenExit(Temp.purgeSyncAll); // Ensuring purgeable temp files are purged on exit
|
||||
/* EXPORT */
|
||||
export default Temp;
|
||||
Reference in New Issue
Block a user