web-dc-api 0.1.5 → 0.1.7

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.
Files changed (148) hide show
  1. package/dist/cjs/index.js +1 -1
  2. package/dist/dc.min.js +1 -1
  3. package/dist/esm/index.js +1 -1
  4. package/dist/index.d.ts +934 -878
  5. package/package.json +4 -8
  6. package/dist/cjs/helia-core-B1Xqha7a.js +0 -1
  7. package/dist/cjs/helia-core-D8Uv1KjQ.js +0 -1
  8. package/dist/cjs/polkadot-api-7PhQf3ws.js +0 -1
  9. package/dist/cjs/polkadot-api-CtrJVWuZ.js +0 -1
  10. package/dist/esm/chunks/helia-core-BxMqyK2Y.js +0 -1
  11. package/dist/esm/chunks/helia-core-DMXRpcO-.js +0 -1
  12. package/dist/esm/chunks/polkadot-api-5Y9Bw8VT.js +0 -1
  13. package/dist/esm/chunks/polkadot-api-D69Ioun_.js +0 -1
  14. package/lib/common/blowfish/block.ts +0 -259
  15. package/lib/common/blowfish/cipher.ts +0 -144
  16. package/lib/common/blowfish/const.ts +0 -195
  17. package/lib/common/chain.ts +0 -469
  18. package/lib/common/commonclient.ts +0 -202
  19. package/lib/common/constants.ts +0 -55
  20. package/lib/common/dc-key/ed25519.ts +0 -343
  21. package/lib/common/dc-key/keyManager.ts +0 -424
  22. package/lib/common/dcapi.ts +0 -98
  23. package/lib/common/dcutil.ts +0 -627
  24. package/lib/common/define.ts +0 -70
  25. package/lib/common/error.ts +0 -67
  26. package/lib/common/grpc-dc.ts +0 -104
  27. package/lib/common/module-system.ts +0 -184
  28. package/lib/common/service-worker.ts +0 -234
  29. package/lib/common/types/types.ts +0 -344
  30. package/lib/dc.ts +0 -701
  31. package/lib/implements/account/client.ts +0 -185
  32. package/lib/implements/account/manager.ts +0 -683
  33. package/lib/implements/aiproxy/client.ts +0 -357
  34. package/lib/implements/aiproxy/manager.ts +0 -670
  35. package/lib/implements/cache/client.ts +0 -105
  36. package/lib/implements/cache/manager.ts +0 -127
  37. package/lib/implements/comment/client.ts +0 -982
  38. package/lib/implements/comment/manager.ts +0 -1151
  39. package/lib/implements/dc/client.ts +0 -51
  40. package/lib/implements/dc/manager.ts +0 -33
  41. package/lib/implements/file/client.ts +0 -253
  42. package/lib/implements/file/file-cache-manager.ts +0 -142
  43. package/lib/implements/file/manager.ts +0 -1240
  44. package/lib/implements/file/seekableFileStream.ts +0 -344
  45. package/lib/implements/file/streamwriter.ts +0 -322
  46. package/lib/implements/keyvalue/client.ts +0 -376
  47. package/lib/implements/keyvalue/manager.ts +0 -759
  48. package/lib/implements/message/client.ts +0 -250
  49. package/lib/implements/message/manager.ts +0 -215
  50. package/lib/implements/threaddb/cbor/coding.ts +0 -62
  51. package/lib/implements/threaddb/cbor/event.ts +0 -336
  52. package/lib/implements/threaddb/cbor/node.ts +0 -542
  53. package/lib/implements/threaddb/cbor/record.ts +0 -398
  54. package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
  55. package/lib/implements/threaddb/common/addrinfo.ts +0 -135
  56. package/lib/implements/threaddb/common/dispatcher.ts +0 -81
  57. package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
  58. package/lib/implements/threaddb/common/json-patcher.ts +0 -204
  59. package/lib/implements/threaddb/common/key.ts +0 -290
  60. package/lib/implements/threaddb/common/level-adapter.ts +0 -235
  61. package/lib/implements/threaddb/common/lineReader.ts +0 -79
  62. package/lib/implements/threaddb/common/logstore.ts +0 -215
  63. package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
  64. package/lib/implements/threaddb/core/app.ts +0 -206
  65. package/lib/implements/threaddb/core/core.ts +0 -230
  66. package/lib/implements/threaddb/core/db.ts +0 -249
  67. package/lib/implements/threaddb/core/event.ts +0 -54
  68. package/lib/implements/threaddb/core/head.ts +0 -89
  69. package/lib/implements/threaddb/core/identity.ts +0 -171
  70. package/lib/implements/threaddb/core/logstore.ts +0 -137
  71. package/lib/implements/threaddb/core/options.ts +0 -14
  72. package/lib/implements/threaddb/core/record.ts +0 -54
  73. package/lib/implements/threaddb/db/collection.ts +0 -1910
  74. package/lib/implements/threaddb/db/db.ts +0 -698
  75. package/lib/implements/threaddb/db/json2Query.ts +0 -192
  76. package/lib/implements/threaddb/db/query.ts +0 -524
  77. package/lib/implements/threaddb/dbclient.ts +0 -543
  78. package/lib/implements/threaddb/dbmanager.ts +0 -1906
  79. package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -549
  80. package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
  81. package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
  82. package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
  83. package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -373
  84. package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
  85. package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
  86. package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
  87. package/lib/implements/threaddb/net/define.ts +0 -149
  88. package/lib/implements/threaddb/net/grpcClient.ts +0 -589
  89. package/lib/implements/threaddb/net/grpcserver.ts +0 -146
  90. package/lib/implements/threaddb/net/net.ts +0 -2047
  91. package/lib/implements/threaddb/pb/lstore.proto +0 -38
  92. package/lib/implements/threaddb/pb/lstore.ts +0 -393
  93. package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
  94. package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
  95. package/lib/implements/threaddb/pb/net.proto +0 -194
  96. package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
  97. package/lib/implements/threaddb/pb/net_pb.js +0 -5525
  98. package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
  99. package/lib/implements/util/client.ts +0 -72
  100. package/lib/implements/util/manager.ts +0 -146
  101. package/lib/implements/wallet/manager.ts +0 -671
  102. package/lib/index.ts +0 -57
  103. package/lib/interfaces/DCContext.ts +0 -51
  104. package/lib/interfaces/aiproxy-interface.ts +0 -145
  105. package/lib/interfaces/auth-interface.ts +0 -118
  106. package/lib/interfaces/cache-interface.ts +0 -22
  107. package/lib/interfaces/client-interface.ts +0 -11
  108. package/lib/interfaces/comment-interface.ts +0 -167
  109. package/lib/interfaces/components/news-component.ts +0 -0
  110. package/lib/interfaces/database-interface.ts +0 -169
  111. package/lib/interfaces/file-interface.ts +0 -120
  112. package/lib/interfaces/index.ts +0 -10
  113. package/lib/interfaces/keyvalue-interface.ts +0 -156
  114. package/lib/interfaces/message-interface.ts +0 -22
  115. package/lib/interfaces/util-interface.ts +0 -31
  116. package/lib/modules/aiproxy-module.ts +0 -246
  117. package/lib/modules/auth-module.ts +0 -753
  118. package/lib/modules/cache-module.ts +0 -99
  119. package/lib/modules/client-module.ts +0 -71
  120. package/lib/modules/comment-module.ts +0 -429
  121. package/lib/modules/components/news-components.ts +0 -390
  122. package/lib/modules/database-module.ts +0 -598
  123. package/lib/modules/file-module.ts +0 -291
  124. package/lib/modules/index.ts +0 -13
  125. package/lib/modules/keyvalue-module.ts +0 -379
  126. package/lib/modules/message-module.ts +0 -107
  127. package/lib/modules/util-module.ts +0 -148
  128. package/lib/polyfills/process-env-browser.ts +0 -1
  129. package/lib/proto/datasource.ts +0 -93
  130. package/lib/proto/dcnet.proto +0 -1601
  131. package/lib/proto/dcnet_proto.d.ts +0 -22857
  132. package/lib/proto/dcnet_proto.js +0 -55204
  133. package/lib/proto/dcnet_proto_sparse.js +0 -55166
  134. package/lib/proto/oidfetch.proto +0 -25
  135. package/lib/proto/oidfetch_proto.d.ts +0 -585
  136. package/lib/proto/oidfetch_proto.js +0 -1247
  137. package/lib/serverless/babel-browser.ts +0 -39
  138. package/lib/serverless/base_entity.ts +0 -78
  139. package/lib/serverless/base_repository.ts +0 -414
  140. package/lib/serverless/browser_schema_extractor.ts +0 -283
  141. package/lib/serverless/decorator_factory.ts +0 -322
  142. package/lib/util/BrowserLineReader.ts +0 -73
  143. package/lib/util/base64.ts +0 -105
  144. package/lib/util/bcrypt.ts +0 -206
  145. package/lib/util/curve25519Encryption.ts +0 -418
  146. package/lib/util/dccrypt.ts +0 -73
  147. package/lib/util/logger.ts +0 -104
  148. package/lib/util/utils.ts +0 -289
@@ -1,51 +0,0 @@
1
- import type { Multiaddr } from "@multiformats/multiaddr";
2
- import type { Client } from "../../common/dcapi";
3
- import { Libp2pGrpcClient } from "grpc-libp2p-client";
4
- import { dcnet } from "../../proto/dcnet_proto";
5
- import { base58btc } from "multiformats/bases/base58";
6
- import { toString as uint8ArrayToString } from "uint8arrays/to-string";
7
-
8
- export class DCClient {
9
- client: Client;
10
-
11
- constructor(dcClient: Client, peerAddr?: Multiaddr) {
12
- this.client = dcClient;
13
- }
14
-
15
- async getHostID(
16
- peerAddr?: Multiaddr
17
- ): Promise<{ peerID: string; reqAddr: string }> {
18
- try {
19
- if (this.client.p2pNode == null) {
20
- throw new Error("p2pNode is null");
21
- }
22
- if (!peerAddr) {
23
- peerAddr = this.client.peerAddr;
24
- }
25
- const message = new dcnet.pb.GetHostIDRequest({});
26
- const messageBytes = dcnet.pb.GetHostIDRequest.encode(message).finish();
27
- const grpcClient = new Libp2pGrpcClient(
28
- this.client.p2pNode,
29
- peerAddr || this.client.peerAddr,
30
- this.client.token,
31
- this.client.protocol
32
- );
33
- const responseData = await grpcClient.unaryCall(
34
- "/dcnet.pb.Service/GetHostID",
35
- messageBytes,
36
- 30000
37
- );
38
- const decoded = dcnet.pb.GetHostIDReply.decode(responseData);
39
- const encodedPeerid = base58btc.encode(decoded.peerID);
40
- const encodedReqAddr = uint8ArrayToString(decoded.reqAddr);
41
- const reply = {
42
- peerID: encodedPeerid,
43
- reqAddr: encodedReqAddr,
44
- };
45
- return reply;
46
- } catch (err) {
47
- console.error("getHostID error:", err);
48
- throw err;
49
- }
50
- }
51
- }
@@ -1,33 +0,0 @@
1
- import type { Multiaddr } from "@multiformats/multiaddr";
2
- import { DCClient } from "./client";
3
- import { DCConnectInfo } from "../../common/types/types";
4
- // 错误定义
5
- export class DCError extends Error {
6
- constructor(message: string) {
7
- super(message);
8
- this.name = "DCError";
9
- }
10
- }
11
- export const Errors = {
12
- ErrNoDcPeerConnected: new DCError("no dc peer connected"),
13
- };
14
-
15
- export class DCManager {
16
- public connectedDc: DCConnectInfo = {};
17
- constructor(connectedDc: DCConnectInfo) {
18
- this.connectedDc = connectedDc;
19
- }
20
-
21
- async getHostID(
22
- peerAddr?: Multiaddr
23
- ): Promise<[{ peerID: string; reqAddr: string } | null, Error | null]> {
24
- if (!this.connectedDc?.client) {
25
- return [null, Errors.ErrNoDcPeerConnected];
26
- }
27
- const dcClient = new DCClient(
28
- this.connectedDc.client,
29
- );
30
- const reply = await dcClient.getHostID(peerAddr);
31
- return [reply, null];
32
- }
33
- }
@@ -1,253 +0,0 @@
1
- import { Libp2pGrpcClient } from "grpc-libp2p-client";
2
- import type { Client } from "../../common/dcapi";
3
- import { dcnet } from "../../proto/dcnet_proto";
4
- import { HeliaLibp2p } from "helia";
5
- import { Libp2p } from "libp2p";
6
- import { DCContext } from "../../../lib/interfaces/DCContext";
7
- import { uint32ToLittleEndianBytes, uint64ToLittleEndianBytes } from "../../util/utils";
8
- import CID from "cids";
9
- import { UploadStatus } from "../../common/types/types";
10
-
11
- const uploadRespondStatus = {
12
- FilePulling: 3, //文件拉取中
13
- PullFail: 4, //拉取失败
14
- PullSuccess: 5, //拉取成功
15
- BlockFinality: 6, //区块已经最终
16
- FinalityTimeout: 7, //最终化超时
17
- FaultSize: 8, //文件大小错误
18
- FaultCount: 9, //文件总数错误
19
- NoUserSpace: 10, //用户存储空间不足
20
- NoPeerSpace: 11, //节点空间不足
21
- SpaceExpire: 12, //用户空间过期
22
- Error: 13, //内部错误
23
- TooManyBackups: 14, //备份过多
24
- VAccountParseError: 15, //Virth Account解析错误
25
- };
26
-
27
- export class FileClient {
28
- client: Client;
29
- dcNodeClient: HeliaLibp2p<Libp2p>;
30
- context: DCContext;
31
-
32
- constructor(
33
- dcClient: Client,
34
- dcNodeClient: HeliaLibp2p<Libp2p>,
35
- context: DCContext
36
- ) {
37
- this.client = dcClient;
38
- this.dcNodeClient = dcNodeClient;
39
- this.context = context;
40
- }
41
-
42
- async storeFile(
43
- fileSize: number,
44
- blockHeight: number,
45
- cid: string,
46
- peerId: string,
47
- onUpdateTransmitSize: (status: number, size: number) => void,
48
- onErrorCallback: (error: Error) => void
49
- ): Promise<void> {
50
-
51
- const sizeValue = uint64ToLittleEndianBytes(fileSize);
52
- const bhValue = uint32ToLittleEndianBytes(blockHeight ? blockHeight : 0);
53
- const typeValue = uint32ToLittleEndianBytes(1);
54
- // 将字符串 (dc.ConnectedDc.peerid) 转换为字节数组
55
- const peerIdValue = new TextEncoder().encode(peerId);
56
- const cidIdValue = new TextEncoder().encode(cid);
57
- // 组合所有部分
58
- const messageParts = new Uint8Array([
59
- ...cidIdValue,
60
- ...sizeValue,
61
- ...bhValue,
62
- ...typeValue,
63
- ...peerIdValue,
64
- ]);
65
- const signature = await this.context.sign(messageParts);
66
- try {
67
- if (this.client.p2pNode == null) {
68
- throw new Error("p2pNode is null");
69
- }
70
- const message = new dcnet.pb.StroeFileRequest({});
71
- message.cid = new TextEncoder().encode(cid);
72
- message.filesize = fileSize;
73
- message.blockheight = blockHeight;
74
- message.signature = signature;
75
- const messageBytes = dcnet.pb.StroeFileRequest.encode(message).finish();
76
-
77
- // const signatureDataSource = new DataSource();
78
- const onDataCallback = async (payload: Uint8Array) => {
79
- const decodedPayload = dcnet.pb.StroeFileReply.decode(payload);
80
- let resStatus = UploadStatus.UPLOADING;
81
- switch (decodedPayload.status) {
82
- case uploadRespondStatus.FilePulling:
83
- resStatus = UploadStatus.UPLOADING;
84
- break;
85
- case uploadRespondStatus.PullFail:
86
- resStatus = UploadStatus.PULLERROR;
87
- break;
88
- case uploadRespondStatus.PullSuccess:
89
- resStatus = UploadStatus.OK;
90
- break;
91
- case uploadRespondStatus.BlockFinality:
92
- resStatus = UploadStatus.OK;
93
- break;
94
- case uploadRespondStatus.FinalityTimeout:
95
- resStatus = UploadStatus.ABNORMAL;
96
- break;
97
- case uploadRespondStatus.FaultSize:
98
- resStatus = UploadStatus.FILESIZEERROR;
99
- break;
100
- case uploadRespondStatus.FaultCount:
101
- resStatus = UploadStatus.FILECOUNTERROR;
102
- break;
103
- case uploadRespondStatus.NoUserSpace:
104
- resStatus = UploadStatus.NOSPACE;
105
- break;
106
- }
107
- if(onUpdateTransmitSize ) {
108
- onUpdateTransmitSize(resStatus, Number(decodedPayload.receivesize));
109
- }
110
- };
111
-
112
- // const dataSourceCallback = (): AsyncIterable<Uint8Array> => {
113
- // console.log("dataSourceCallback");
114
- // return signatureDataSource.getDataSource();
115
- // };
116
-
117
-
118
- // 使用方法
119
- const grpcClient = new Libp2pGrpcClient(
120
- this.client.p2pNode,
121
- this.client.peerAddr,
122
- this.client.token,
123
- this.client.protocol
124
- );
125
-
126
- await grpcClient.Call(
127
- "/dcnet.pb.Service/StoreFile",
128
- messageBytes,
129
- 100000,
130
- "server-streaming",
131
- onDataCallback,
132
- );
133
- return;
134
- // const decoded = dcnet.pb.StroeFileReply.decode(responseData);
135
- // return [decoded.cid, decoded.decryptKey, null];
136
- } catch (err) {
137
- console.error("storeFile error:", err);
138
- throw err;
139
- }
140
- }
141
-
142
-
143
- /**
144
- * Stores a folder on the DC network
145
- * @param id - CID of the folder to store
146
- * @param options - File options including signature, size, etc.
147
- * @returns AsyncIterable that yields storage status updates
148
- */
149
- async storeFolder(
150
- cid: string,
151
- options: {
152
- signature: Uint8Array;
153
- blockHeight: number;
154
- fileSize: number;
155
- fileCount: number;
156
- pubkey: Uint8Array;
157
- vaccount?: any;
158
- },
159
- updateTransmitCount: (status: UploadStatus, total: number, progress: number) => void,
160
- onErrorCallback?: (error: Error) => void
161
- ): Promise<void> {
162
- try {
163
- // Check client
164
- if (this.client.p2pNode == null) {
165
- throw new Error("p2pNode is null");
166
- }
167
-
168
- // Create the request message
169
- const message = new dcnet.pb.StoreFolderRequest({
170
- cid: new TextEncoder().encode(cid),
171
- filecount: options.fileCount,
172
- foldersize: options.fileSize,
173
- blockheight: options.blockHeight,
174
- signature: options.signature
175
- });
176
-
177
- // Add virtual account if provided
178
- if (options.vaccount) {
179
- try {
180
- message.vaccount = options.vaccount.pubKeyRaw;
181
- } catch (error: any) {
182
- throw new Error("Failed to parse virtual account: " + error.message);
183
- }
184
- }
185
-
186
- // Encode the message
187
- const messageBytes = dcnet.pb.StoreFolderRequest.encode(message).finish();
188
- const onDataCallback = async (payload: Uint8Array) => {
189
- try {
190
- const decodedPayload = dcnet.pb.StoreFolderReply.decode(payload);
191
- let resStatus = UploadStatus.UPLOADING;
192
- switch (decodedPayload.status) {
193
- case uploadRespondStatus.FilePulling:
194
- resStatus = UploadStatus.UPLOADING;
195
- break;
196
- case uploadRespondStatus.PullFail:
197
- resStatus = UploadStatus.PULLERROR;
198
- break;
199
- case uploadRespondStatus.PullSuccess:
200
- resStatus = UploadStatus.OK;
201
- break;
202
- case uploadRespondStatus.BlockFinality:
203
- resStatus = UploadStatus.OK;
204
- break;
205
- case uploadRespondStatus.FinalityTimeout:
206
- resStatus = UploadStatus.ABNORMAL;
207
- break;
208
- case uploadRespondStatus.FaultSize:
209
- resStatus = UploadStatus.FILESIZEERROR;
210
- break;
211
- case uploadRespondStatus.FaultCount:
212
- resStatus = UploadStatus.FILECOUNTERROR;
213
- break;
214
- case uploadRespondStatus.NoUserSpace:
215
- resStatus = UploadStatus.NOSPACE;
216
- break;
217
- }
218
- if (updateTransmitCount !== null) {
219
- updateTransmitCount(resStatus, options.fileCount, decodedPayload.receivecount);
220
- }
221
-
222
- } catch (error: any) {
223
- console.error("Error decoding StoreFolderReply:", error);
224
- if (onErrorCallback) {
225
- onErrorCallback(new Error("Failed to decode StoreFolderReply: " + error.message));
226
- }
227
- }
228
- };
229
-
230
- // 使用方法
231
- const grpcClient = new Libp2pGrpcClient(
232
- this.client.p2pNode,
233
- this.client.peerAddr,
234
- this.client.token,
235
- this.client.protocol
236
- );
237
-
238
- await grpcClient.Call(
239
- "/dcnet.pb.Service/StoreFolder",
240
- messageBytes,
241
- 100000,
242
- "server-streaming",
243
- onDataCallback,
244
- );
245
- }catch (err) {
246
- console.error("storeFolder error:", err);
247
- if (onErrorCallback) {
248
- onErrorCallback(new Error("StoreFolder failed: " + (err instanceof Error ? err.message : String(err))));
249
- }
250
- }
251
- }
252
-
253
- }
@@ -1,142 +0,0 @@
1
- // file-cache-manager.ts
2
- // 文件缓存管理工具
3
-
4
- import { createLogger } from "../../util/logger";
5
- import { SeekableFileStream } from "./seekableFileStream";
6
-
7
- const logger = createLogger('FileCacheManager');
8
-
9
- /**
10
- * 文件缓存管理器
11
- * 负责管理可寻址文件流的缓存,提高性能并减少网络负担
12
- */
13
- export class FileCacheManager {
14
- // 文件缓存相关属性
15
- private seekableFileStreamCache: Map<string, {
16
- stream: SeekableFileStream,
17
- lastAccessTime: number
18
- }> = new Map();
19
-
20
- private readonly CACHE_TIMEOUT: number;
21
- private cacheCleanupInterval: NodeJS.Timeout | null = null;
22
-
23
- /**
24
- * 创建文件缓存管理器
25
- * @param timeout 缓存超时时间(毫秒),默认100秒
26
- */
27
- constructor(timeout: number = 100000) {
28
- this.CACHE_TIMEOUT = timeout;
29
- this.startCacheCleanupTask();
30
- }
31
-
32
- /**
33
- * 获取缓存的文件流,如果不存在则返回null
34
- * @param ipfsPath IPFS路径
35
- * @param decryptKey 解密密钥
36
- * @returns 缓存的文件流或null
37
- */
38
- getCachedFileStream(ipfsPath: string, decryptKey: string): SeekableFileStream | null {
39
- const cacheKey = `${ipfsPath}_${decryptKey}`;
40
- const cachedItem = this.seekableFileStreamCache.get(cacheKey);
41
-
42
- if (cachedItem) {
43
- logger.info(`使用缓存的SeekableFileStream: ${cacheKey}`);
44
- // 更新最后访问时间
45
- cachedItem.lastAccessTime = Date.now();
46
- return cachedItem.stream;
47
- }
48
-
49
- return null;
50
- }
51
-
52
- /**
53
- * 将文件流添加到缓存
54
- * @param ipfsPath IPFS路径
55
- * @param decryptKey 解密密钥
56
- * @param fileStream 文件流对象
57
- */
58
- cacheFileStream(ipfsPath: string, decryptKey: string, fileStream: SeekableFileStream): void {
59
- const cacheKey = `${ipfsPath}_${decryptKey}`;
60
- logger.info(`创建新的SeekableFileStream缓存: ${cacheKey}`);
61
-
62
- // 将新创建的流保存到缓存,并记录访问时间
63
- this.seekableFileStreamCache.set(cacheKey, {
64
- stream: fileStream,
65
- lastAccessTime: Date.now()
66
- });
67
- }
68
-
69
- /**
70
- * 清理文件缓存
71
- * @param pathname 可选,指定要清理的路径
72
- */
73
- clearFileCache(pathname?: string): void {
74
- if (pathname) {
75
- // 清理特定路径的缓存
76
- const pathParts = pathname.split('/');
77
- let ipfsPath = pathParts[3];
78
- const keyParts = ipfsPath.split('_');
79
- if (keyParts.length > 1) {
80
- ipfsPath = keyParts[0];
81
- const decryptKey = keyParts[1];
82
- const cacheKey = `${ipfsPath}_${decryptKey}`;
83
- this.seekableFileStreamCache.delete(cacheKey);
84
- logger.info(`清理特定缓存: ${cacheKey}`);
85
- }
86
- } else {
87
- // 清理所有缓存
88
- this.seekableFileStreamCache.clear();
89
- logger.info('清理所有文件缓存');
90
- }
91
- }
92
-
93
- /**
94
- * 启动定时缓存清理任务
95
- * 超过设定时间未访问的流将被释放
96
- */
97
- startCacheCleanupTask(): void {
98
- if (this.cacheCleanupInterval) {
99
- clearInterval(this.cacheCleanupInterval);
100
- }
101
-
102
- this.cacheCleanupInterval = setInterval(() => {
103
- const now = Date.now();
104
- let removedCount = 0;
105
-
106
- // 检查所有缓存项是否超时
107
- for (const [key, value] of this.seekableFileStreamCache.entries()) {
108
- if (now - value.lastAccessTime > this.CACHE_TIMEOUT) {
109
- // 超过设定时间没有访问,释放资源
110
- logger.info(`缓存超时,释放资源: ${key}`);
111
- this.seekableFileStreamCache.delete(key);
112
- removedCount++;
113
- }
114
- }
115
-
116
- if (removedCount > 0 || this.seekableFileStreamCache.size > 0) {
117
- logger.info(`文件缓存清理: 移除 ${removedCount} 项,剩余 ${this.seekableFileStreamCache.size} 项`);
118
- }
119
- }, 5000); // 每5秒检查一次超时缓存
120
- }
121
-
122
- /**
123
- * 停止缓存清理任务
124
- */
125
- stopCacheCleanupTask(): void {
126
- if (this.cacheCleanupInterval) {
127
- clearInterval(this.cacheCleanupInterval);
128
- this.cacheCleanupInterval = null;
129
- }
130
- }
131
-
132
- /**
133
- * 获取缓存状态信息
134
- * @returns 缓存状态信息对象
135
- */
136
- getCacheStats(): { total: number, keys: string[] } {
137
- return {
138
- total: this.seekableFileStreamCache.size,
139
- keys: Array.from(this.seekableFileStreamCache.keys())
140
- };
141
- }
142
- }