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,390 +0,0 @@
1
- import { KeyValueModule } from "../keyvalue-module";
2
- import { DCContext } from "../../interfaces/DCContext";
3
- import { KeyValueDB, KeyValueStoreType } from "../../implements/keyvalue/manager";
4
- import { Direction } from "../../common/define";
5
-
6
- export interface NewsData {
7
- id: string;
8
- title: string;
9
- content: string;
10
- images?: string[];
11
- author: string;
12
- timestamp: number;
13
- category: string;
14
- tags: string[];
15
- status: 'published' | 'deleted';
16
- extra?: any;
17
- }
18
-
19
- export interface CommentData {
20
- id: string;
21
- newsId: string;
22
- content: string;
23
- author: string;
24
- timestamp: number;
25
- }
26
-
27
- export interface LikeData {
28
- newsId: string;
29
- author: string;
30
- timestamp: number;
31
- }
32
-
33
- export class NewsComponent {
34
- private kvModule: KeyValueModule;
35
- private context: DCContext;
36
- private kvdb: KeyValueDB | null = null;
37
- private appId: string;
38
- private theme: string;
39
- private themeAuthor: string;
40
-
41
- constructor(
42
- kvModule: KeyValueModule,
43
- context: DCContext,
44
- appId: string = "DCAPP",
45
- theme: string = "news_pub",
46
- themeAuthor: string
47
- ) {
48
- this.kvModule = kvModule;
49
- this.context = context;
50
- this.appId = appId;
51
- this.theme = theme;
52
- this.themeAuthor = themeAuthor;
53
- }
54
-
55
- /**
56
- * 初始化组件,连接数据库
57
- */
58
- async initialize(): Promise<boolean> {
59
- // 尝试获取存储
60
- let [db, err] = await this.kvModule.getStore(this.appId, this.theme, this.themeAuthor);
61
-
62
- if (!db) {
63
- // 如果获取失败且当前用户是主题作者,则尝试创建
64
- if (this.context.publicKey?.string() === this.themeAuthor) {
65
- [db, err] = await this.kvModule.createStore(
66
- this.appId,
67
- this.theme,
68
- 100 * 1024 * 1024, // 100MB
69
- KeyValueStoreType.Public
70
- );
71
- }
72
- }
73
-
74
- if (err || !db) {
75
- console.error("NewsComponent initialize failed:", err);
76
- return false;
77
- }
78
-
79
- this.kvdb = db;
80
- return true;
81
- }
82
-
83
- private checkDB(): void {
84
- if (!this.kvdb) {
85
- throw new Error("NewsComponent not initialized");
86
- }
87
- }
88
-
89
- /**
90
- * 发布新闻
91
- */
92
- async publishNews(news: Omit<NewsData, 'id' | 'author' | 'timestamp' | 'status'>): Promise<string | null> {
93
- this.checkDB();
94
- if (!this.context.publicKey) throw new Error("User not logged in");
95
-
96
- const timestamp = Date.now();
97
- const id = `news_${timestamp}_${Math.random().toString(36).substr(2, 9)}`;
98
- const author = this.context.publicKey.string();
99
-
100
- const newsData: NewsData = {
101
- ...news,
102
- id,
103
- author,
104
- timestamp,
105
- status: 'published'
106
- };
107
-
108
- const indexes = [
109
- { key: "type", value: "news", type: "string" },
110
- { key: "category", value: news.category, type: "string" },
111
- { key: "timestamp", value: timestamp, type: "number" },
112
- { key: "author", value: author, type: "string" }
113
- ];
114
-
115
- const [success, , err] = await this.kvModule.set(
116
- this.kvdb!,
117
- id,
118
- JSON.stringify(newsData),
119
- JSON.stringify(indexes)
120
- );
121
-
122
- if (err || !success) {
123
- console.error("Publish news failed:", err);
124
- return null;
125
- }
126
-
127
- return id;
128
- }
129
-
130
- /**
131
- * 修改新闻
132
- */
133
- async updateNews(id: string, updates: Partial<Omit<NewsData, 'id' | 'author' | 'timestamp'>>): Promise<boolean> {
134
- this.checkDB();
135
-
136
- // 先获取原有新闻
137
- const [currentDataStr, err] = await this.kvModule.get(this.kvdb!, id);
138
- if (err || !currentDataStr) return false;
139
-
140
- // 解析原有数据 (格式: value$$$dckv_extra$$$...)
141
- const valuePart = currentDataStr.split("$$$")[0];
142
- const currentData: NewsData = JSON.parse(valuePart);
143
-
144
- // 检查权限 (仅作者可修改)
145
- if (currentData.author !== this.context.publicKey?.string()) {
146
- throw new Error("Permission denied");
147
- }
148
-
149
- const newData: NewsData = {
150
- ...currentData,
151
- ...updates,
152
- timestamp: Date.now() // 更新时间戳? 或者保留原发布时间? 通常保留发布时间,增加updateTime。这里简单处理。
153
- };
154
-
155
- // 重建索引 (保持原有索引键,更新值)
156
- const indexes = [
157
- { key: "type", value: "news", type: "string" },
158
- { key: "category", value: newData.category, type: "string" },
159
- { key: "timestamp", value: newData.timestamp, type: "number" }, // 如果按更新时间排
160
- { key: "author", value: newData.author, type: "string" }
161
- ];
162
-
163
- const [success, , updateErr] = await this.kvModule.set(
164
- this.kvdb!,
165
- id,
166
- JSON.stringify(newData),
167
- JSON.stringify(indexes)
168
- );
169
-
170
- return !!success && !updateErr;
171
- }
172
-
173
- /**
174
- * 删除新闻 (软删除)
175
- */
176
- async deleteNews(id: string): Promise<boolean> {
177
- return this.updateNews(id, { status: 'deleted' });
178
- }
179
-
180
- /**
181
- * 获取新闻列表
182
- */
183
- async getNewsList(
184
- options: {
185
- category?: string;
186
- limit?: number;
187
- offset?: number;
188
- seekKey?: string;
189
- } = {}
190
- ): Promise<NewsData[]> {
191
- this.checkDB();
192
-
193
- const limit = options.limit || 10;
194
- const offset = options.offset || 0;
195
-
196
- let indexKey = "type";
197
- let indexValue = "news";
198
-
199
- // 如果指定了分类,使用分类索引
200
- if (options.category) {
201
- indexKey = "category";
202
- indexValue = options.category;
203
- }
204
-
205
- // 默认按时间倒序 (Direction.Reverse)
206
- const [resultStr, err] = await this.kvModule.getWithIndex(
207
- this.kvdb!,
208
- indexKey,
209
- indexValue,
210
- {
211
- limit,
212
- offset,
213
- seekKey: options.seekKey,
214
- direction: Direction.Reverse
215
- }
216
- );
217
-
218
- if (err || !resultStr) return [];
219
-
220
- // 解析结果
221
- // 结果格式: JSON array of "key:value$$$dckv_extra$$$..."
222
- try {
223
- const items: any[] = JSON.parse(resultStr);
224
- const newsList: NewsData[] = [];
225
-
226
- for (const item of items) {
227
- // item 是一个对象 { key: "value$$$..." } 或者直接是 value 字符串?
228
- // 实际返回格式可能是 [{ "key": "value$$$..." }, ...]
229
-
230
- const key = Object.keys(item)[0];
231
- const valStr = item[key];
232
- const valuePart = valStr.split("$$$")[0];
233
- const newsData: NewsData = JSON.parse(valuePart);
234
-
235
- if (newsData.status !== 'deleted') {
236
- newsList.push(newsData);
237
- }
238
- }
239
- return newsList;
240
- } catch (e) {
241
- console.error("Parse news list failed", e);
242
- return [];
243
- }
244
- }
245
-
246
- /**
247
- * 获取新闻详情
248
- */
249
- async getNewsDetail(id: string): Promise<NewsData | null> {
250
- this.checkDB();
251
- const [dataStr, err] = await this.kvModule.get(this.kvdb!, id);
252
- if (err || !dataStr) return null;
253
-
254
- const valuePart = dataStr.split("$$$")[0];
255
- try {
256
- const data: NewsData = JSON.parse(valuePart);
257
- if (data.status === 'deleted') return null;
258
- return data;
259
- } catch (e) {
260
- return null;
261
- }
262
- }
263
-
264
- /**
265
- * 点赞新闻
266
- */
267
- async likeNews(newsId: string): Promise<boolean> {
268
- this.checkDB();
269
- if (!this.context.publicKey) throw new Error("User not logged in");
270
-
271
- const author = this.context.publicKey.string();
272
- const id = `like_${newsId}_${author}`;
273
- const timestamp = Date.now();
274
-
275
- const likeData: LikeData = { newsId, author, timestamp };
276
-
277
- const indexes = [
278
- { key: "like_target", value: newsId, type: "string" },
279
- { key: "type", value: "like", type: "string" }
280
- ];
281
-
282
- const [success, , err] = await this.kvModule.set(
283
- this.kvdb!,
284
- id,
285
- JSON.stringify(likeData),
286
- JSON.stringify(indexes)
287
- );
288
-
289
- return !!success && !err;
290
- }
291
-
292
- /**
293
- * 评价新闻
294
- */
295
- async commentNews(newsId: string, content: string): Promise<boolean> {
296
- this.checkDB();
297
- if (!this.context.publicKey) throw new Error("User not logged in");
298
-
299
- const author = this.context.publicKey.string();
300
- const timestamp = Date.now();
301
- const id = `comment_${newsId}_${timestamp}_${Math.random().toString(36).substr(2, 5)}`;
302
-
303
- const commentData: CommentData = {
304
- id,
305
- newsId,
306
- content,
307
- author,
308
- timestamp
309
- };
310
-
311
- const indexes = [
312
- { key: "comment_target", value: newsId, type: "string" },
313
- { key: "type", value: "comment", type: "string" },
314
- { key: "timestamp", value: timestamp, type: "number" }
315
- ];
316
-
317
- const [success, , err] = await this.kvModule.set(
318
- this.kvdb!,
319
- id,
320
- JSON.stringify(commentData),
321
- JSON.stringify(indexes)
322
- );
323
-
324
- return !!success && !err;
325
- }
326
-
327
- /**
328
- * 获取新闻评论列表
329
- */
330
- async getNewsComments(newsId: string, limit: number = 20, offset: number = 0): Promise<CommentData[]> {
331
- this.checkDB();
332
-
333
- const [resultStr, err] = await this.kvModule.getWithIndex(
334
- this.kvdb!,
335
- "comment_target",
336
- newsId,
337
- {
338
- limit,
339
- offset,
340
- direction: Direction.Reverse // 最新评论在前
341
- }
342
- );
343
-
344
- if (err || !resultStr) return [];
345
-
346
- try {
347
- const items: any[] = JSON.parse(resultStr);
348
- const comments: CommentData[] = [];
349
- for (const item of items) {
350
- const key = Object.keys(item)[0];
351
- const valStr = item[key];
352
- const valuePart = valStr.split("$$$")[0];
353
- comments.push(JSON.parse(valuePart));
354
- }
355
- return comments;
356
- } catch (e) {
357
- return [];
358
- }
359
- }
360
-
361
- /**
362
- * 获取新闻点赞数 (估算,因为是分页获取)
363
- * 实际场景可能需要专门的计数器,这里仅演示获取列表
364
- */
365
- async getNewsLikes(newsId: string, limit: number = 100): Promise<LikeData[]> {
366
- this.checkDB();
367
- const [resultStr, err] = await this.kvModule.getWithIndex(
368
- this.kvdb!,
369
- "like_target",
370
- newsId,
371
- { limit }
372
- );
373
-
374
- if (err || !resultStr) return [];
375
-
376
- try {
377
- const items: any[] = JSON.parse(resultStr);
378
- const likes: LikeData[] = [];
379
- for (const item of items) {
380
- const key = Object.keys(item)[0];
381
- const valStr = item[key];
382
- const valuePart = valStr.split("$$$")[0];
383
- likes.push(JSON.parse(valuePart));
384
- }
385
- return likes;
386
- } catch(e) {
387
- return [];
388
- }
389
- }
390
- }