This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+45
View File
@@ -0,0 +1,45 @@
# Changelog
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
### [1.0.1](https://github.com/nuxt-contrib/fs-memo/compare/v1.0.0...v1.0.1) (2020-06-16)
## [1.0.0](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.6...v1.0.0) (2020-06-16)
### [0.0.6](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.5...v0.0.6) (2020-06-01)
### Bug Fixes
* fix options type and destructure ([af16d46](https://github.com/nuxt-contrib/fs-memo/commit/af16d460a15b24027e9ab07a4fe14da2dfca74ef))
* **pkg:** add missing files field ([c27fb23](https://github.com/nuxt-contrib/fs-memo/commit/c27fb2398f0b086b047e09211abf9a970e058312))
### [0.0.5](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.4...v0.0.5) (2020-05-31)
### Bug Fixes
* **types:** set memo type to any ([7397658](https://github.com/nuxt-contrib/fs-memo/commit/7397658e1a9a24c0c58e26b766e02af1062f792f))
### [0.0.4](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.3...v0.0.4) (2020-05-31)
### Features
* always validate memo file ([224ba35](https://github.com/nuxt-contrib/fs-memo/commit/224ba3559f60ae8d7ed1d249d7c7a6326519b663))
### [0.0.3](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.2...v0.0.3) (2020-05-31)
### Bug Fixes
* pid check ([f5f6706](https://github.com/nuxt-contrib/fs-memo/commit/f5f67068c8dc731c2c39b42bb6b8cb0dc18cf286))
### [0.0.2](https://github.com/nuxt-contrib/fs-memo/compare/v0.0.1...v0.0.2) (2020-05-31)
### Bug Fixes
* default name ([5835f70](https://github.com/nuxt-contrib/fs-memo/commit/5835f70e6ad018f2d856725cad1bb149dc74a502))
### 0.0.1 (2020-05-31)
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+66
View File
@@ -0,0 +1,66 @@
# fs-memo
> Easy persisted memo object for Node.js
[![npm](https://img.shields.io/npm/dt/fs-memo.svg?style=flat-square)](https://npmjs.com/package/fs-memo)
[![npm (scoped with tag)](https://img.shields.io/npm/v/fs-memo/latest.svg?style=flat-square)](https://npmjs.com/package/fs-memo)
## Usage
Install package:
```bash
yarn add fs-memo
# or
or npm install fs-memo
```
```js
const { getMemo, setMemo } = require('fs-memo')
// or
import { getMemo, setMemo } from 'fs-memo'
```
### `getMemo(options)`
```ts
getMemo(options: MemoOptions): Promise<any>
```
Load latest memo from file-system and combine with local state from CJS cache.
FS loading silently bails if:
- The process that made memo is still alive with different pid
- Any fs error happens (like permission denied)
### `setMemo(eoptions)`
```ts
setMemo(memo: object, options: MemoOptions): Promise<void>
```
Update local state from CJS cache and persist memo object to file-system.
FS persistence silently bails if any error happens.
## Options
```ts
interface MemoOptions {
dir?: string
name?: string
}
```
### `dir`
Specify directory where memo file should be stored. Default dir is `__dirname` (`node_modules/fs-memo/dist`)
### `name`
Name of memo file. Default name is `.fs-memo`.
## License
MIT
+7
View File
@@ -0,0 +1,7 @@
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 {};
+128
View File
@@ -0,0 +1,128 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');
var 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 = {
_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;
}
function isAlive(pid) {
try {
process.kill(pid, 0);
return true;
} catch (e) {
return e.code === 'EPERM';
}
}
exports.getMemo = getMemo;
exports.setMemo = setMemo;
+61
View File
@@ -0,0 +1,61 @@
{
"_args": [
[
"fs-memo@1.0.1",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
]
],
"_from": "fs-memo@1.0.1",
"_id": "fs-memo@1.0.1",
"_inBundle": false,
"_integrity": "sha512-lV4dUPsyJHVgD3g0loRj+ClZQnb8eUQN0olNWgmYbq+ch4ponSMBI+l4l72l9krHakhq79QLCzPJMko+qpvmTQ==",
"_location": "/fs-memo",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "fs-memo@1.0.1",
"name": "fs-memo",
"escapedName": "fs-memo",
"rawSpec": "1.0.1",
"saveSpec": null,
"fetchSpec": "1.0.1"
},
"_requiredBy": [
"/get-port-please"
],
"_resolved": "https://registry.npmjs.org/fs-memo/-/fs-memo-1.0.1.tgz",
"_spec": "1.0.1",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"bugs": {
"url": "https://github.com/nuxt-contrib/fs-memo/issues"
},
"description": "easy persisted memo object",
"devDependencies": {
"@nuxtjs/eslint-config-typescript": "^2.0.0",
"@types/node": "^14.0.6",
"bili": "^4.10.0",
"eslint": "^7.1.0",
"rollup-plugin-typescript2": "^0.27.1",
"standard-version": "^8.0.0",
"typescript": "^3.9.3"
},
"files": [
"dist"
],
"homepage": "https://github.com/nuxt-contrib/fs-memo#readme",
"license": "MIT",
"main": "dist/index.js",
"name": "fs-memo",
"repository": {
"type": "git",
"url": "git+https://github.com/nuxt-contrib/fs-memo.git"
},
"scripts": {
"build": "bili src/index.ts --minimal",
"lint": "eslint --ext ts .",
"release": "yarn build && standard-version && npm publish && git push --follow-tags"
},
"types": "dist/index.d.ts",
"version": "1.0.1"
}