@kevisual/api 0.0.52 → 0.0.54
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/query-ai.js +16 -3
- package/dist/query-app.js +16 -3
- package/dist/query-config.js +14 -1
- package/dist/query-login-node.d.ts +340 -0
- package/dist/query-login-node.js +1374 -0
- package/dist/query-login.js +17 -1
- package/dist/query-mark.js +14 -1
- package/dist/query-proxy.js +134 -76
- package/dist/query-resources.js +1 -1
- package/dist/query-secret.js +14 -1
- package/dist/query-shop.js +16 -3
- package/dist/store-mark.js +13 -0
- package/package.json +7 -6
- package/query/query-login/login-node-cache.ts +50 -25
- package/query/query-login/query-login-node.ts +8 -3
- package/query/query-login/query-login.ts +3 -0
|
@@ -2,9 +2,8 @@ import { Cache } from './login-cache.ts';
|
|
|
2
2
|
import { homedir } from 'node:os';
|
|
3
3
|
import { join, dirname } from 'node:path';
|
|
4
4
|
import fs from 'node:fs';
|
|
5
|
-
import { readFileSync, writeFileSync, accessSync } from 'node:fs';
|
|
6
|
-
|
|
7
|
-
export const fileExists = async (
|
|
5
|
+
import { readFileSync, writeFileSync, accessSync, unlinkSync, mkdirSync } from 'node:fs';
|
|
6
|
+
export const fileExists = (
|
|
8
7
|
filePath: string,
|
|
9
8
|
{ createIfNotExists = true, isFile = true, isDir = false }: { createIfNotExists?: boolean; isFile?: boolean; isDir?: boolean } = {},
|
|
10
9
|
) => {
|
|
@@ -13,10 +12,10 @@ export const fileExists = async (
|
|
|
13
12
|
return true;
|
|
14
13
|
} catch (error) {
|
|
15
14
|
if (createIfNotExists && isDir) {
|
|
16
|
-
|
|
15
|
+
mkdirSync(filePath, { recursive: true });
|
|
17
16
|
return true;
|
|
18
17
|
} else if (createIfNotExists && isFile) {
|
|
19
|
-
|
|
18
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
20
19
|
return false;
|
|
21
20
|
}
|
|
22
21
|
return false;
|
|
@@ -34,32 +33,53 @@ export const readConfigFile = (filePath: string) => {
|
|
|
34
33
|
export const writeConfigFile = (filePath: string, data: any) => {
|
|
35
34
|
writeFileSync(filePath, JSON.stringify(data, null, 2));
|
|
36
35
|
};
|
|
37
|
-
|
|
36
|
+
/**
|
|
37
|
+
* 获取主机名,
|
|
38
|
+
* 例如:https://kevisual.cn 返回 kevisual.cn
|
|
39
|
+
* 这里是读取默认的配置文件 ~/.config/envision/config.json 中的 baseURL 字段来获取主机名的,如果没有配置 baseURL 则默认使用 https://kevisual.cn
|
|
40
|
+
* @returns 主机名
|
|
41
|
+
*/
|
|
42
|
+
export const getHostName = (baseURL?: string) => {
|
|
38
43
|
const configDir = join(homedir(), '.config', 'envision');
|
|
39
44
|
const configFile = join(configDir, 'config.json');
|
|
40
45
|
const config = readConfigFile(configFile);
|
|
41
|
-
const
|
|
42
|
-
const hostname = new URL(
|
|
46
|
+
const _baseURL = baseURL || config.baseURL || 'https://kevisual.cn';
|
|
47
|
+
const hostname = new URL(_baseURL).hostname;
|
|
43
48
|
return hostname;
|
|
44
49
|
};
|
|
45
50
|
export class StorageNode implements Storage {
|
|
46
51
|
cacheData: any;
|
|
47
|
-
filePath: string;
|
|
48
|
-
|
|
52
|
+
filePath: string = '';
|
|
53
|
+
hostname: string = '';
|
|
54
|
+
isLoaded: boolean = false;
|
|
55
|
+
constructor(opts?: { baseURL?: string, load?: boolean }) {
|
|
49
56
|
this.cacheData = {};
|
|
57
|
+
const hostname = getHostName(opts?.baseURL);
|
|
58
|
+
this.setHostName(hostname, { load: opts?.load });
|
|
59
|
+
}
|
|
60
|
+
setHostName(hostname: string, opts?: { load?: boolean }) {
|
|
61
|
+
const load = opts?.load ?? false;
|
|
62
|
+
if (hostname.startsWith('http')) {
|
|
63
|
+
hostname = new URL(hostname).hostname;
|
|
64
|
+
}
|
|
50
65
|
const configDir = join(homedir(), '.config', 'envision');
|
|
51
|
-
const hostname = getHostName();
|
|
52
66
|
this.filePath = join(configDir, 'config', `${hostname}-storage.json`);
|
|
67
|
+
this.hostname = hostname;
|
|
53
68
|
fileExists(this.filePath, { isFile: true });
|
|
69
|
+
if (load) {
|
|
70
|
+
this.loadCache();
|
|
71
|
+
}
|
|
54
72
|
}
|
|
55
|
-
|
|
73
|
+
loadCache(force?: boolean) {
|
|
74
|
+
if (this.isLoaded && !force) return;
|
|
56
75
|
const filePath = this.filePath;
|
|
57
76
|
try {
|
|
58
|
-
const data =
|
|
77
|
+
const data = readConfigFile(filePath);
|
|
59
78
|
this.cacheData = data;
|
|
79
|
+
this.isLoaded = true;
|
|
60
80
|
} catch (error) {
|
|
61
81
|
this.cacheData = {};
|
|
62
|
-
|
|
82
|
+
writeFileSync(filePath, JSON.stringify(this.cacheData, null, 2));
|
|
63
83
|
}
|
|
64
84
|
}
|
|
65
85
|
get length() {
|
|
@@ -70,15 +90,15 @@ export class StorageNode implements Storage {
|
|
|
70
90
|
}
|
|
71
91
|
setItem(key: string, value: any) {
|
|
72
92
|
this.cacheData[key] = value;
|
|
73
|
-
|
|
93
|
+
writeFileSync(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
|
74
94
|
}
|
|
75
95
|
removeItem(key: string) {
|
|
76
96
|
delete this.cacheData[key];
|
|
77
|
-
|
|
97
|
+
writeFileSync(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
|
78
98
|
}
|
|
79
99
|
clear() {
|
|
80
100
|
this.cacheData = {};
|
|
81
|
-
|
|
101
|
+
writeFileSync(this.filePath, JSON.stringify(this.cacheData, null, 2));
|
|
82
102
|
}
|
|
83
103
|
key(index: number) {
|
|
84
104
|
return Object.keys(this.cacheData)[index];
|
|
@@ -86,10 +106,13 @@ export class StorageNode implements Storage {
|
|
|
86
106
|
}
|
|
87
107
|
export class LoginNodeCache implements Cache {
|
|
88
108
|
filepath: string;
|
|
89
|
-
|
|
90
|
-
constructor(
|
|
91
|
-
this.filepath =
|
|
109
|
+
isLoaded: boolean = false;
|
|
110
|
+
constructor(opts?: { baseURL?: string, load?: boolean }) {
|
|
111
|
+
this.filepath = join(homedir(), '.config', 'envision', 'config', `${getHostName(opts?.baseURL)}-login.json`);
|
|
92
112
|
fileExists(this.filepath, { isFile: true });
|
|
113
|
+
if (opts?.load) {
|
|
114
|
+
this.loadCache(this.filepath);
|
|
115
|
+
}
|
|
93
116
|
}
|
|
94
117
|
async get(_key: string) {
|
|
95
118
|
try {
|
|
@@ -111,12 +134,14 @@ export class LoginNodeCache implements Cache {
|
|
|
111
134
|
}
|
|
112
135
|
}
|
|
113
136
|
async del() {
|
|
114
|
-
|
|
137
|
+
unlinkSync(this.filepath);
|
|
115
138
|
}
|
|
116
|
-
|
|
139
|
+
loadCache(filePath: string, force?: boolean) {
|
|
140
|
+
if (this.isLoaded && !force) return;
|
|
117
141
|
try {
|
|
118
|
-
const data =
|
|
142
|
+
const data = readFileSync(filePath, 'utf-8');
|
|
119
143
|
const jsonData = JSON.parse(data);
|
|
144
|
+
this.isLoaded = true;
|
|
120
145
|
return jsonData;
|
|
121
146
|
} catch (error) {
|
|
122
147
|
// console.log('loadCache error', error);
|
|
@@ -126,7 +151,7 @@ export class LoginNodeCache implements Cache {
|
|
|
126
151
|
return defaultData;
|
|
127
152
|
}
|
|
128
153
|
}
|
|
129
|
-
|
|
130
|
-
return
|
|
154
|
+
init() {
|
|
155
|
+
return this.loadCache(this.filepath);
|
|
131
156
|
}
|
|
132
157
|
}
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { QueryLogin, QueryLoginOpts } from './query-login.ts';
|
|
2
2
|
import { LoginNodeCache, StorageNode } from './login-node-cache.ts';
|
|
3
3
|
type QueryLoginNodeOptsWithoutCache = Omit<QueryLoginOpts, 'cache'>;
|
|
4
|
-
export
|
|
5
|
-
|
|
4
|
+
export { StorageNode }
|
|
5
|
+
export const cache = new LoginNodeCache();
|
|
6
6
|
export class QueryLoginNode extends QueryLogin {
|
|
7
|
+
declare storage: StorageNode;
|
|
7
8
|
constructor(opts: QueryLoginNodeOptsWithoutCache) {
|
|
9
|
+
const baseURL = opts?.query?.baseURL;
|
|
10
|
+
const storage = new StorageNode({ baseURL, load: true });
|
|
11
|
+
const cache = new LoginNodeCache({ baseURL, load: true });
|
|
8
12
|
super({
|
|
9
13
|
...opts,
|
|
14
|
+
isBrowser: false,
|
|
10
15
|
storage,
|
|
11
|
-
cache
|
|
16
|
+
cache,
|
|
12
17
|
});
|
|
13
18
|
}
|
|
14
19
|
}
|
|
@@ -39,6 +39,9 @@ export class QueryLogin extends BaseQuery {
|
|
|
39
39
|
this.init();
|
|
40
40
|
this.onLoad = opts?.onLoad;
|
|
41
41
|
this.storage = opts?.storage || globalThis?.localStorage;
|
|
42
|
+
if (!this.storage) {
|
|
43
|
+
throw new Error('storage is required');
|
|
44
|
+
}
|
|
42
45
|
}
|
|
43
46
|
setQuery(query: Query) {
|
|
44
47
|
this.query = query;
|