@zwa73/utils 1.0.280 → 1.0.282
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/UtilClass/DataStore.d.ts +71 -0
- package/dist/UtilClass/DataStore.js +105 -0
- package/dist/UtilClass/ObsidianDepot.d.ts +4 -3
- package/dist/UtilClass/ObsidianDepot.js +16 -5
- package/dist/UtilClass/index.d.ts +1 -0
- package/dist/UtilClass/index.js +1 -0
- package/dist/UtilHttp.js +8 -0
- package/dist/UtilInterfaces.d.ts +1 -0
- package/dist/UtilInterfaces.js +0 -1
- package/package.json +4 -3
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { JObject, MPromise } from '@zwa73/js-utils';
|
|
2
|
+
/**数据存储接口 */
|
|
3
|
+
export type DataStore<T> = {
|
|
4
|
+
/**尝试保存
|
|
5
|
+
* @returns 是否真实产生写入保存
|
|
6
|
+
*/
|
|
7
|
+
save: (data: T) => MPromise<boolean>;
|
|
8
|
+
getData: () => MPromise<T>;
|
|
9
|
+
getUid: () => string;
|
|
10
|
+
};
|
|
11
|
+
/**JSON文件存储实现 */
|
|
12
|
+
export declare class JsonStore<T extends JObject> implements DataStore<T> {
|
|
13
|
+
/**存储的数据 */
|
|
14
|
+
private _data;
|
|
15
|
+
/**上次保存时的数据快照,用于变更检测 */
|
|
16
|
+
private _lastSaved;
|
|
17
|
+
/**文件存储路径 */
|
|
18
|
+
private _filepath;
|
|
19
|
+
/**是否检查数据变更 */
|
|
20
|
+
private _checkChange;
|
|
21
|
+
/**
|
|
22
|
+
* 构造函数
|
|
23
|
+
* @param opt 配置选项
|
|
24
|
+
*/
|
|
25
|
+
constructor(opt: {
|
|
26
|
+
/**文件路径 */
|
|
27
|
+
filepath: string;
|
|
28
|
+
/**是否检查数据变更,默认true */
|
|
29
|
+
checkChange?: boolean;
|
|
30
|
+
});
|
|
31
|
+
/**保存数据到文件
|
|
32
|
+
* @param data 要保存的数据
|
|
33
|
+
* @returns 是否真实产生写入保存
|
|
34
|
+
*/
|
|
35
|
+
save(data: T): Promise<boolean>;
|
|
36
|
+
/**获取数据
|
|
37
|
+
* @returns 存储的数据
|
|
38
|
+
*/
|
|
39
|
+
getData(): Promise<T>;
|
|
40
|
+
/**获取唯一标识
|
|
41
|
+
* @returns 基于路径的唯一标识符
|
|
42
|
+
*/
|
|
43
|
+
getUid(): string;
|
|
44
|
+
}
|
|
45
|
+
/**内存对象存储实现 */
|
|
46
|
+
export declare class ObjectStore<T> implements DataStore<T> {
|
|
47
|
+
/**唯一标识符 */
|
|
48
|
+
private _uid;
|
|
49
|
+
/**存储的数据 */
|
|
50
|
+
private _data;
|
|
51
|
+
/**构造函数
|
|
52
|
+
* @param data 初始数据
|
|
53
|
+
*/
|
|
54
|
+
constructor(opt: {
|
|
55
|
+
data: T;
|
|
56
|
+
uid?: string;
|
|
57
|
+
});
|
|
58
|
+
/**保存数据
|
|
59
|
+
* @param data 要保存的数据
|
|
60
|
+
* @returns 始终返回false,因为内存存储不产生实际写入
|
|
61
|
+
*/
|
|
62
|
+
save(data: T): Promise<boolean>;
|
|
63
|
+
/**获取数据
|
|
64
|
+
* @returns 存储的数据
|
|
65
|
+
*/
|
|
66
|
+
getData(): Promise<T>;
|
|
67
|
+
/**获取唯一标识
|
|
68
|
+
* @returns 唯一标识符
|
|
69
|
+
*/
|
|
70
|
+
getUid(): string;
|
|
71
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ObjectStore = exports.JsonStore = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const UtilFileTools_1 = require("../UtilFileTools");
|
|
9
|
+
const UtilFunctions_1 = require("../UtilFunctions");
|
|
10
|
+
const UtilLogger_1 = require("../UtilLogger");
|
|
11
|
+
/**JSON文件存储实现 */
|
|
12
|
+
class JsonStore {
|
|
13
|
+
/**存储的数据 */
|
|
14
|
+
_data;
|
|
15
|
+
/**上次保存时的数据快照,用于变更检测 */
|
|
16
|
+
_lastSaved = '';
|
|
17
|
+
/**文件存储路径 */
|
|
18
|
+
_filepath;
|
|
19
|
+
/**是否检查数据变更 */
|
|
20
|
+
_checkChange;
|
|
21
|
+
/**
|
|
22
|
+
* 构造函数
|
|
23
|
+
* @param opt 配置选项
|
|
24
|
+
*/
|
|
25
|
+
constructor(opt) {
|
|
26
|
+
const { filepath, checkChange = true } = opt;
|
|
27
|
+
this._filepath = filepath;
|
|
28
|
+
this._checkChange = checkChange;
|
|
29
|
+
UtilFileTools_1.UtilFT.pathExists(this._filepath).then(exists => {
|
|
30
|
+
if (!exists)
|
|
31
|
+
UtilLogger_1.SLogger.warn(`JsonStore 配置文件不存在, 将会尝试用空对象创建 path:${this._filepath}`);
|
|
32
|
+
}).catch(err => {
|
|
33
|
+
UtilLogger_1.SLogger.error(`JsonStore 检查配置文件失败 path:${this._filepath}`, err);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**保存数据到文件
|
|
37
|
+
* @param data 要保存的数据
|
|
38
|
+
* @returns 是否真实产生写入保存
|
|
39
|
+
*/
|
|
40
|
+
async save(data) {
|
|
41
|
+
this._data = data;
|
|
42
|
+
const newSaved = this._checkChange ? JSON.stringify(this._data) : '';
|
|
43
|
+
if (this._checkChange == false || this._lastSaved != newSaved) {
|
|
44
|
+
await fs_1.default.promises.writeFile(this._filepath, UtilFunctions_1.UtilFunc.stringifyJToken(this._data));
|
|
45
|
+
this._lastSaved = newSaved;
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
/**获取数据
|
|
51
|
+
* @returns 存储的数据
|
|
52
|
+
*/
|
|
53
|
+
async getData() {
|
|
54
|
+
if (this._data == undefined) {
|
|
55
|
+
this._data = await UtilFileTools_1.UtilFT.loadJSONFile(this._filepath, { default: {} });
|
|
56
|
+
if (this._checkChange)
|
|
57
|
+
this._lastSaved = JSON.stringify(this._data);
|
|
58
|
+
}
|
|
59
|
+
return this._data;
|
|
60
|
+
}
|
|
61
|
+
/**获取唯一标识
|
|
62
|
+
* @returns 基于路径的唯一标识符
|
|
63
|
+
*/
|
|
64
|
+
getUid() {
|
|
65
|
+
return `JsonStore_${this._filepath}`;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.JsonStore = JsonStore;
|
|
69
|
+
/**内存对象存储实现 */
|
|
70
|
+
class ObjectStore {
|
|
71
|
+
/**唯一标识符 */
|
|
72
|
+
_uid;
|
|
73
|
+
/**存储的数据 */
|
|
74
|
+
_data;
|
|
75
|
+
/**构造函数
|
|
76
|
+
* @param data 初始数据
|
|
77
|
+
*/
|
|
78
|
+
constructor(opt) {
|
|
79
|
+
const { data, uid } = opt;
|
|
80
|
+
this._data = data;
|
|
81
|
+
this._uid = uid ?? `ObjectStore_${UtilFunctions_1.UtilFunc.genUUID()}`;
|
|
82
|
+
}
|
|
83
|
+
/**保存数据
|
|
84
|
+
* @param data 要保存的数据
|
|
85
|
+
* @returns 始终返回false,因为内存存储不产生实际写入
|
|
86
|
+
*/
|
|
87
|
+
async save(data) {
|
|
88
|
+
this._data = data;
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
/**获取数据
|
|
92
|
+
* @returns 存储的数据
|
|
93
|
+
*/
|
|
94
|
+
async getData() {
|
|
95
|
+
;
|
|
96
|
+
return this._data;
|
|
97
|
+
}
|
|
98
|
+
/**获取唯一标识
|
|
99
|
+
* @returns 唯一标识符
|
|
100
|
+
*/
|
|
101
|
+
getUid() {
|
|
102
|
+
return this._uid;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.ObjectStore = ObjectStore;
|
|
@@ -39,9 +39,10 @@ type ScanResult = {
|
|
|
39
39
|
codeobj: undefined | Record<string, any>;
|
|
40
40
|
};
|
|
41
41
|
export declare class ObsidianDepot {
|
|
42
|
-
/**将数组转为markdown
|
|
43
|
-
static toList(
|
|
44
|
-
|
|
42
|
+
/**将数组转为markdown列表 */
|
|
43
|
+
static toList(itemList?: string[]): string | undefined;
|
|
44
|
+
/**将数组转为markdown文件预览 */
|
|
45
|
+
static toPreview(filepath: string): string;
|
|
45
46
|
/**搜索info.md文件
|
|
46
47
|
* @param opt - 搜索选项
|
|
47
48
|
*/
|
|
@@ -6,17 +6,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.ObsidianDepot = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const js_utils_1 = require("@zwa73/js-utils");
|
|
9
|
-
const Constant_1 = require("../Constant");
|
|
10
9
|
const pathe_1 = __importDefault(require("pathe"));
|
|
10
|
+
const Constant_1 = require("../Constant");
|
|
11
11
|
const UtilFileTools_1 = require("../UtilFileTools");
|
|
12
12
|
const UtilFunctions_1 = require("../UtilFunctions");
|
|
13
13
|
const Markdown_1 = require("./Markdown");
|
|
14
14
|
class ObsidianDepot {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
//static create(opt:ObsidianDepotOpt){return new ObsidianDepot(opt);}
|
|
16
|
+
//private constructor(private opt:ObsidianDepotOpt){}
|
|
17
|
+
//async scanMarkdown(opt:Omit<ScanOpt,'rootPath'>){
|
|
18
|
+
// return ObsidianDepot.scanMarkdown({...opt,rootPath:this.opt.depotPath});
|
|
19
|
+
//}
|
|
20
|
+
//async updateMarkdown(opt:Omit<UpdateOpt,'rootPath'>){
|
|
21
|
+
// return ObsidianDepot.updateMarkdown({...opt,rootPath:this.opt.depotPath});
|
|
22
|
+
//}
|
|
23
|
+
/**将数组转为markdown列表 */
|
|
24
|
+
static toList(itemList) {
|
|
25
|
+
return itemList?.map(item => ` - ${item.trim()}`).join('\n');
|
|
26
|
+
}
|
|
27
|
+
/**将数组转为markdown文件预览 */
|
|
28
|
+
static toPreview(filepath) {
|
|
29
|
+
return `![[${filepath}]]`;
|
|
18
30
|
}
|
|
19
|
-
constructor() { }
|
|
20
31
|
/**搜索info.md文件
|
|
21
32
|
* @param opt - 搜索选项
|
|
22
33
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './Hbs';
|
|
2
2
|
export * from './Markdown';
|
|
3
3
|
export * from './ObsidianDepot';
|
|
4
|
+
export * from './DataStore';
|
|
4
5
|
export type { EventData, DListMiddleNode, DListHeadNode, DListTailNode, DListInvalidNode, DListMaybeNode, DListNode, BridgeInterface } from "@zwa73/js-utils";
|
|
5
6
|
export { Stream, Spool, SmartCache, SequenceQueue, PromiseQueue, Piper, EventSystem, DLinkedList, DelayQueue, Bridge } from "@zwa73/js-utils";
|
package/dist/UtilClass/index.js
CHANGED
|
@@ -18,6 +18,7 @@ exports.Bridge = exports.DelayQueue = exports.DLinkedList = exports.EventSystem
|
|
|
18
18
|
__exportStar(require("./Hbs"), exports);
|
|
19
19
|
__exportStar(require("./Markdown"), exports);
|
|
20
20
|
__exportStar(require("./ObsidianDepot"), exports);
|
|
21
|
+
__exportStar(require("./DataStore"), exports);
|
|
21
22
|
var js_utils_1 = require("@zwa73/js-utils");
|
|
22
23
|
Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
|
|
23
24
|
Object.defineProperty(exports, "Spool", { enumerable: true, get: function () { return js_utils_1.Spool; } });
|
package/dist/UtilHttp.js
CHANGED
|
@@ -493,6 +493,14 @@ class UtilHttp {
|
|
|
493
493
|
}
|
|
494
494
|
}
|
|
495
495
|
exports.UtilHttp = UtilHttp;
|
|
496
|
+
if (false)
|
|
497
|
+
void (async () => {
|
|
498
|
+
const tool2 = UtilHttp
|
|
499
|
+
.url('http://postman-echo.com/post')
|
|
500
|
+
.postJson()
|
|
501
|
+
.option({ timeout: 20000, port: 80 });
|
|
502
|
+
console.log(await tool2.once({ json: { j: 123 } }));
|
|
503
|
+
})();
|
|
496
504
|
if (false)
|
|
497
505
|
void ((async () => {
|
|
498
506
|
const tool = UtilHttp
|
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
1
|
export type { JToken, JValue, JArray, JObject, IJData, AnyFunc, Keyable, DeepReadonly, DeepWritable, PartialOption, ProperSubset, ProperSubsetCheck, IncludeCheck, UnionCheck, Literal, LiteralCheck, AllExtends, AssignObject, Writeable, Requireify, Inverted, FixedLengthTuple, Increment, RangeTuple, AnyString, UnionToIntersection, ExclusiveRecord, ExclusiveJObject, PromiseStatus, StatusVerifyFn, Await, FuncPropNames, ExtendThen, RequiredOnly, WithPrefix, Outcome, Either, Matchable, MatchableFlag, ExtractOutcome, ExtractMatchable, NeedInit, PRecord, MPromise, SPromise, SMPromise, NotPromise, AnyRecord, MReturnType, CmtTuple, Flasy, SrtSegment, ILogger, LogLevel, Asyncize, SharpSchema, ExpandSharpSchema, Preset, PresetOption, PresetFinal } from "@zwa73/js-utils";
|
|
2
|
+
export type { StringifyOpt, PromiseRetries, PromiseRetryResult } from "@zwa73/js-utils";
|
package/dist/UtilInterfaces.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.282",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,19 +19,19 @@
|
|
|
19
19
|
"@zwa73/js-utils": "*",
|
|
20
20
|
"@zwa73/modular-mixer": "^1.0.8",
|
|
21
21
|
"form-data": "^4.0.2",
|
|
22
|
+
"glob": "^13.0.0",
|
|
22
23
|
"html-entities": "^2.3.3",
|
|
23
24
|
"http-proxy-agent": "^7.0.2",
|
|
24
25
|
"https-proxy-agent": "^7.0.6",
|
|
26
|
+
"pathe": "^1.1.2",
|
|
25
27
|
"querystring": "^0.2.1",
|
|
26
28
|
"winston": "^3.10.0",
|
|
27
29
|
"winston-daily-rotate-file": "^4.7.1"
|
|
28
30
|
},
|
|
29
31
|
"peerDependencies": {
|
|
30
32
|
"@iarna/toml": "^2.2.5",
|
|
31
|
-
"glob": "^13.0.0",
|
|
32
33
|
"handlebars": "^4.7.8",
|
|
33
34
|
"json5": "^2.2.3",
|
|
34
|
-
"pathe": "^1.1.2",
|
|
35
35
|
"public-ip": "^6.0.2",
|
|
36
36
|
"tiktoken": "^1.0.7",
|
|
37
37
|
"yaml": "^2.8.1"
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@deepkit/type": "^1.0.1-alpha.153",
|
|
58
|
+
"@iarna/toml": "^2.2.5",
|
|
58
59
|
"@types/jest": "^29.5.12",
|
|
59
60
|
"@types/node": "^20.19.25",
|
|
60
61
|
"@zwa73/dev-utils": "*",
|