update
This commit is contained in:
+9
-7
@@ -1,7 +1,9 @@
|
||||
interface MemoOptions {
|
||||
dir?: string;
|
||||
name?: string;
|
||||
}
|
||||
export declare function getMemo(options: MemoOptions): Promise<any>;
|
||||
export declare function setMemo(memo: object, options: MemoOptions): Promise<void>;
|
||||
export {};
|
||||
interface MemoOptions {
|
||||
dir: string;
|
||||
name: string;
|
||||
file: string;
|
||||
}
|
||||
declare function getMemo(config: Partial<MemoOptions>): Promise<any>;
|
||||
declare function setMemo(memo: object, config: Partial<MemoOptions>): Promise<void>;
|
||||
|
||||
export { getMemo, setMemo };
|
||||
|
||||
+38
-110
@@ -2,125 +2,53 @@
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
function _await(value, then, direct) {
|
||||
if (direct) {
|
||||
return then ? then(value) : value;
|
||||
}
|
||||
|
||||
if (!value || !value.then) {
|
||||
value = Promise.resolve(value);
|
||||
}
|
||||
|
||||
return then ? value.then(then) : value;
|
||||
}
|
||||
|
||||
function _empty() {}
|
||||
|
||||
function _catch(body, recover) {
|
||||
try {
|
||||
var result = body();
|
||||
} catch (e) {
|
||||
return recover(e);
|
||||
}
|
||||
|
||||
if (result && result.then) {
|
||||
return result.then(void 0, recover);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function _continue(value, then) {
|
||||
return value && value.then ? value.then(then) : then(value);
|
||||
}
|
||||
|
||||
function _async(f) {
|
||||
return function () {
|
||||
var arguments$1 = arguments;
|
||||
|
||||
for (var args = [], i = 0; i < arguments.length; i++) {
|
||||
args[i] = arguments$1[i];
|
||||
}
|
||||
|
||||
try {
|
||||
return Promise.resolve(f.apply(this, args));
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function _awaitIgnored(value, direct) {
|
||||
if (!direct) {
|
||||
return value && value.then ? value.then(_empty) : Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
function _continueIgnored(value) {
|
||||
if (value && value.then) {
|
||||
return value.then(_empty);
|
||||
}
|
||||
}
|
||||
|
||||
var setMemo = _async(function (memo, options) {
|
||||
var ref = getOptions(options);
|
||||
var file = ref.file; // Set local memo
|
||||
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid; // Try to persist
|
||||
|
||||
return _continueIgnored(_catch(function () {
|
||||
return _awaitIgnored(fs.promises.writeFile(file, JSON.stringify(_memo), 'utf-8'));
|
||||
}, _empty));
|
||||
});
|
||||
var getMemo = _async(function (options) {
|
||||
var ref = getOptions(options);
|
||||
var file = ref.file; // Try to load latest memo
|
||||
|
||||
return _continue(_catch(function () {
|
||||
return _await(fs.promises.readFile(file, 'utf-8'), function (_fs$readFile) {
|
||||
var memo = JSON.parse(_fs$readFile) || {};
|
||||
|
||||
if (!memo._pid) {
|
||||
throw new Error('InvalidMemo');
|
||||
}
|
||||
|
||||
if (memo._pid === _memo._pid || // fs is more reliable than require cache
|
||||
!isAlive(memo.pid) // RIP
|
||||
) {
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid;
|
||||
}
|
||||
});
|
||||
}, _empty), function (_result) {
|
||||
return _memo;
|
||||
});
|
||||
});
|
||||
var _memo = {
|
||||
const _memo = {
|
||||
_pid: process.pid
|
||||
};
|
||||
var defaults = {
|
||||
dir: __dirname,
|
||||
name: '.fs-memo'
|
||||
};
|
||||
|
||||
function getOptions(options) {
|
||||
var opts = Object.assign({}, defaults, options, {
|
||||
file: ''
|
||||
});
|
||||
opts.file = path.resolve(opts.dir, opts.name);
|
||||
return opts;
|
||||
async function getMemo(config) {
|
||||
const options = getOptions(config);
|
||||
try {
|
||||
const memo = JSON.parse(await fs.promises.readFile(options.file, "utf-8")) || {};
|
||||
if (!memo._pid) {
|
||||
throw new Error("Memo lacks _pid");
|
||||
}
|
||||
if (memo._pid === _memo._pid || !isAlive(memo.pid)) {
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid;
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
return _memo;
|
||||
}
|
||||
async function setMemo(memo, config) {
|
||||
const options = getOptions(config);
|
||||
Object.assign(_memo, memo);
|
||||
_memo._pid = process.pid;
|
||||
try {
|
||||
await fs.promises.mkdir(options.dir);
|
||||
} catch (e) {
|
||||
}
|
||||
try {
|
||||
await fs.promises.writeFile(options.file, JSON.stringify(_memo), "utf-8");
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
function getOptions(config) {
|
||||
const options = {...config};
|
||||
options.name = options.name || "default";
|
||||
options.dir = options.dir || path.resolve(process.cwd(), "node_modules/.cache/fs-memo");
|
||||
options.file = options.file || path.resolve(options.dir, options.name + ".json");
|
||||
return options;
|
||||
}
|
||||
|
||||
function isAlive(pid) {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return e.code === 'EPERM';
|
||||
return e.code === "EPERM";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user