web-dc-api 0.0.7 → 0.0.8
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/lib/common/service-worker.ts +2 -2
- package/lib/dc.ts +4 -1
- package/lib/interfaces/auth-interface.ts +0 -2
- package/lib/interfaces/keyvalue-interface.ts +21 -0
- package/lib/modules/auth-module.ts +11 -9
- package/lib/modules/file-module.ts +6 -1
- package/lib/modules/keyvalue-module.ts +33 -4
- package/package.json +1 -1
|
@@ -11,7 +11,7 @@ const logger = createLogger('ServiceWorker');
|
|
|
11
11
|
* @param fileOps 文件操作对象,用于处理IPFS请求
|
|
12
12
|
* @returns Promise<ServiceWorkerRegistration | null>
|
|
13
13
|
*/
|
|
14
|
-
export async function registerServiceWorker(fileOps?: IFileOperations): Promise<ServiceWorkerRegistration | null> {
|
|
14
|
+
export async function registerServiceWorker(fileOps?: IFileOperations, swUrl: string = ''): Promise<ServiceWorkerRegistration | null> {
|
|
15
15
|
if ('serviceWorker' in navigator) {
|
|
16
16
|
try {
|
|
17
17
|
// const registration = await navigator.serviceWorker.register(
|
|
@@ -20,7 +20,7 @@ export async function registerServiceWorker(fileOps?: IFileOperations): Promise<
|
|
|
20
20
|
|
|
21
21
|
// 路径在函数内部解析,不在模块顶层
|
|
22
22
|
const swPath = typeof window !== 'undefined'
|
|
23
|
-
? (new URL('/sw.js', window.location.origin).href)
|
|
23
|
+
? (new URL(swUrl || '/sw.js', window.location.origin).href)
|
|
24
24
|
: '/sw.js';
|
|
25
25
|
|
|
26
26
|
const registration = await navigator.serviceWorker.register(swPath);
|
package/lib/dc.ts
CHANGED
|
@@ -50,6 +50,7 @@ export class DC implements DCContext {
|
|
|
50
50
|
public grpcServer: DCGrpcServer;
|
|
51
51
|
public appInfo: APPInfo;
|
|
52
52
|
public dbManager: any;
|
|
53
|
+
public swUrl: string = "";
|
|
53
54
|
|
|
54
55
|
// 模块系统
|
|
55
56
|
private moduleSystem: ModuleSystem;
|
|
@@ -65,11 +66,13 @@ export class DC implements DCContext {
|
|
|
65
66
|
wssUrl: string;
|
|
66
67
|
backWssUrl: string;
|
|
67
68
|
appInfo?: APPInfo ;
|
|
69
|
+
swUrl?: string;
|
|
68
70
|
logLevel?: LogLevel;
|
|
69
71
|
names?: string[];
|
|
70
72
|
}) {
|
|
71
73
|
this.blockChainAddr = options.wssUrl;
|
|
72
74
|
this.backChainAddr = options.backWssUrl;
|
|
75
|
+
this.swUrl = options.swUrl || "";
|
|
73
76
|
this.dcChain = new ChainUtil();
|
|
74
77
|
this.dcutil = new DcUtil(this.dcChain);
|
|
75
78
|
// //todo remove
|
|
@@ -105,7 +108,7 @@ export class DC implements DCContext {
|
|
|
105
108
|
}
|
|
106
109
|
}else {
|
|
107
110
|
// 注册核心功能模块
|
|
108
|
-
this.moduleSystem.registerModule(new FileModule());
|
|
111
|
+
this.moduleSystem.registerModule(new FileModule(this.swUrl || ""));
|
|
109
112
|
this.moduleSystem.registerModule(new AuthModule());
|
|
110
113
|
this.moduleSystem.registerModule(new CommentModule());
|
|
111
114
|
this.moduleSystem.registerModule(new DatabaseModule());
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Ed25519PrivKey } from "../common/dc-key/ed25519";
|
|
2
1
|
import { DCConnectInfo, NFTBindStatus, User,AccountInfo } from "../common/types/types";
|
|
3
2
|
|
|
4
3
|
|
|
@@ -29,7 +28,6 @@ export interface IAuthOperations {
|
|
|
29
28
|
*/
|
|
30
29
|
accountLogin(nftAccount: string, password: string, safecode: string): Promise<{
|
|
31
30
|
mnemonic: string;
|
|
32
|
-
privKey: Ed25519PrivKey;
|
|
33
31
|
}>;
|
|
34
32
|
|
|
35
33
|
/**
|
|
@@ -87,6 +87,27 @@ export interface IKeyValueOperations {
|
|
|
87
87
|
vaccount?: string
|
|
88
88
|
): Promise<[string, Error | null]>;
|
|
89
89
|
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 获取指定键的值列表,包括所有用户写入的值,可以用在类似排名这些需要多人数据汇总的场景,key为场景名称,各个用户写入的值为各自在该场景下的内容
|
|
93
|
+
* @param kvdb: KeyValueDB,
|
|
94
|
+
* @param key 键名
|
|
95
|
+
* @param limit 返回结果数量限制
|
|
96
|
+
* @param seekKey 查询起始键,用于分页查询
|
|
97
|
+
* @param offset 结果偏移量
|
|
98
|
+
* @param vaccount 可选的虚拟账户
|
|
99
|
+
* @returns [值列表生成的json字符串, 错误信息]
|
|
100
|
+
*/
|
|
101
|
+
getValues(
|
|
102
|
+
kvdb: KeyValueDB,
|
|
103
|
+
key: string,
|
|
104
|
+
limit: number,
|
|
105
|
+
seekKey:string,
|
|
106
|
+
offset: number,
|
|
107
|
+
vaccount?: string
|
|
108
|
+
): Promise<[string, Error | null]>;
|
|
109
|
+
|
|
110
|
+
|
|
90
111
|
/**
|
|
91
112
|
* 批量获取多个键的值
|
|
92
113
|
* @param kvdb: KeyValueDB,
|
|
@@ -150,7 +150,6 @@ export class AuthModule implements DCModule, IAuthOperations {
|
|
|
150
150
|
safecode: string
|
|
151
151
|
): Promise<{
|
|
152
152
|
mnemonic: string;
|
|
153
|
-
privKey: Ed25519PrivKey;
|
|
154
153
|
}> {
|
|
155
154
|
this.assertInitialized();
|
|
156
155
|
|
|
@@ -163,19 +162,22 @@ export class AuthModule implements DCModule, IAuthOperations {
|
|
|
163
162
|
password,
|
|
164
163
|
safecode
|
|
165
164
|
);
|
|
165
|
+
console.log("=================accountLogin success");
|
|
166
166
|
|
|
167
167
|
if (mnemonic) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
168
|
+
if(this.context.appInfo?.appId){
|
|
169
|
+
const accountManager = new AccountManager(this.context);
|
|
170
|
+
const res = await accountManager.generateAppAccount(this.context.appInfo?.appId, mnemonic);
|
|
171
|
+
console.log("=================generateAppAccount success");
|
|
172
|
+
if (res[0] === null) {
|
|
173
|
+
throw res[1] || new Error("generateAppAccount error");
|
|
174
|
+
}
|
|
175
|
+
// 获取私钥
|
|
176
|
+
const privKey = Ed25519PrivKey.unmarshalString(res[0]);
|
|
177
|
+
console.log("=================获取私钥 success");
|
|
172
178
|
}
|
|
173
|
-
// 获取私钥
|
|
174
|
-
const privKey = Ed25519PrivKey.unmarshalString(res[0]);
|
|
175
|
-
|
|
176
179
|
return {
|
|
177
180
|
mnemonic,
|
|
178
|
-
privKey,
|
|
179
181
|
};
|
|
180
182
|
}
|
|
181
183
|
return;
|
|
@@ -22,7 +22,12 @@ export class FileModule implements DCModule, IFileOperations {
|
|
|
22
22
|
private fileCacheManager: FileCacheManager;
|
|
23
23
|
private context: DCContext;
|
|
24
24
|
private initialized: boolean = false;
|
|
25
|
+
private swUrl: string;
|
|
25
26
|
|
|
27
|
+
constructor(url: string) {
|
|
28
|
+
this.swUrl = url;
|
|
29
|
+
}
|
|
30
|
+
|
|
26
31
|
/**
|
|
27
32
|
* 初始化文件模块
|
|
28
33
|
* @param context DC上下文
|
|
@@ -43,7 +48,7 @@ export class FileModule implements DCModule, IFileOperations {
|
|
|
43
48
|
|
|
44
49
|
// 注册 Service Worker
|
|
45
50
|
try {
|
|
46
|
-
await registerServiceWorker(this);
|
|
51
|
+
await registerServiceWorker(this, this.swUrl || '');
|
|
47
52
|
logger.info('Service Worker 已注册');
|
|
48
53
|
} catch (err) {
|
|
49
54
|
logger.warn('Service Worker 注册失败:', err);
|
|
@@ -9,7 +9,7 @@ import { ThemeManager } from "../implements/cache/manager";
|
|
|
9
9
|
import { createLogger } from "../util/logger";
|
|
10
10
|
import { ThemeAuthInfo, ThemeComment } from "../common/types/types";
|
|
11
11
|
const logger = createLogger('KeyValueModule');
|
|
12
|
-
|
|
12
|
+
const indexkey_dckv = "indexkey_dckv"; //索引键名,keyvalue设置过程中key本身的索引键
|
|
13
13
|
/**
|
|
14
14
|
* 键值存储模块
|
|
15
15
|
* 提供键值存储功能
|
|
@@ -165,7 +165,7 @@ export class KeyValueModule implements DCModule, IKeyValueOperations {
|
|
|
165
165
|
const res = await kvdb.set(key, value, indexs, vaccount);
|
|
166
166
|
return res;
|
|
167
167
|
} catch (error) {
|
|
168
|
-
logger.error(`设置
|
|
168
|
+
logger.error(`设置set-value失败:`, error);
|
|
169
169
|
throw error;
|
|
170
170
|
}
|
|
171
171
|
}
|
|
@@ -188,6 +188,35 @@ export class KeyValueModule implements DCModule, IKeyValueOperations {
|
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 获取指定键的值列表,包括所有用户写入的值,可以用在类似排名这些需要多人数据汇总的场景,key为场景名称,各个用户写入的值为各自在该场景下的内容
|
|
194
|
+
* @param kvdb: KeyValueDB,
|
|
195
|
+
* @param key 键名
|
|
196
|
+
* @param limit 返回结果数量限制
|
|
197
|
+
* @param seekKey 查询起始键,用于分页查询
|
|
198
|
+
* @param offset 结果偏移量
|
|
199
|
+
* @param vaccount 可选的虚拟账户
|
|
200
|
+
* @returns [值列表生成的json字符串, 错误信息]
|
|
201
|
+
*/
|
|
202
|
+
async getValues(
|
|
203
|
+
kvdb: KeyValueDB,
|
|
204
|
+
key: string,
|
|
205
|
+
limit: number,
|
|
206
|
+
seekKey:string,
|
|
207
|
+
offset: number,
|
|
208
|
+
vaccount?: string
|
|
209
|
+
): Promise<[string, Error | null]> {
|
|
210
|
+
this.assertInitialized();
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
const res = await kvdb.getWithIndex(indexkey_dckv, key, limit,seekKey, offset, vaccount);
|
|
214
|
+
return res;
|
|
215
|
+
} catch (error) {
|
|
216
|
+
logger.error(`getValues失败:`, error);
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
191
220
|
|
|
192
221
|
async getBatch(
|
|
193
222
|
kvdb: KeyValueDB,
|
|
@@ -201,7 +230,7 @@ export class KeyValueModule implements DCModule, IKeyValueOperations {
|
|
|
201
230
|
const res = await kvdb.getBatch(writerPubkey, keys, vaccount);
|
|
202
231
|
return res;
|
|
203
232
|
} catch (error) {
|
|
204
|
-
logger.error(
|
|
233
|
+
logger.error(`getBatch失败:`, error);
|
|
205
234
|
throw error;
|
|
206
235
|
}
|
|
207
236
|
}
|
|
@@ -221,7 +250,7 @@ export class KeyValueModule implements DCModule, IKeyValueOperations {
|
|
|
221
250
|
const res = await kvdb.getWithIndex(indexKey, indexValue, limit,seekKey, offset, vaccount);
|
|
222
251
|
return res;
|
|
223
252
|
} catch (error) {
|
|
224
|
-
logger.error(
|
|
253
|
+
logger.error(`getWithIndex失败:`, error);
|
|
225
254
|
throw error;
|
|
226
255
|
}
|
|
227
256
|
}
|