web-dc-api 0.0.87 → 0.0.89
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/cjs/index.js +1 -0
- package/dist/cjs/protobuf-BVBdi7Hh.js +1 -0
- package/dist/dc.min.js +1 -19
- package/dist/esm/chunks/protobuf-CbxDm-Gy.js +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/index.d.ts +947 -886
- package/package.json +14 -17
- package/dist/index.cjs.js +0 -19
- package/dist/index.esm.js +0 -19
- package/lib/common/blowfish/block.ts +0 -259
- package/lib/common/blowfish/cipher.ts +0 -144
- package/lib/common/blowfish/const.ts +0 -195
- package/lib/common/chain.ts +0 -469
- package/lib/common/commonclient.ts +0 -202
- package/lib/common/constants.ts +0 -55
- package/lib/common/dc-key/ed25519.ts +0 -343
- package/lib/common/dc-key/keyManager.ts +0 -424
- package/lib/common/dcapi.ts +0 -98
- package/lib/common/dcutil.ts +0 -595
- package/lib/common/define.ts +0 -66
- package/lib/common/error.ts +0 -67
- package/lib/common/grpc-dc.ts +0 -104
- package/lib/common/module-system.ts +0 -184
- package/lib/common/service-worker.ts +0 -234
- package/lib/common/types/types.ts +0 -344
- package/lib/dc.ts +0 -694
- package/lib/implements/account/client.ts +0 -185
- package/lib/implements/account/manager.ts +0 -683
- package/lib/implements/aiproxy/client.ts +0 -357
- package/lib/implements/aiproxy/manager.ts +0 -670
- package/lib/implements/cache/client.ts +0 -105
- package/lib/implements/cache/manager.ts +0 -127
- package/lib/implements/comment/client.ts +0 -982
- package/lib/implements/comment/manager.ts +0 -1151
- package/lib/implements/dc/client.ts +0 -51
- package/lib/implements/dc/manager.ts +0 -33
- package/lib/implements/file/client.ts +0 -253
- package/lib/implements/file/file-cache-manager.ts +0 -142
- package/lib/implements/file/manager.ts +0 -1240
- package/lib/implements/file/seekableFileStream.ts +0 -344
- package/lib/implements/file/streamwriter.ts +0 -322
- package/lib/implements/keyvalue/client.ts +0 -376
- package/lib/implements/keyvalue/manager.ts +0 -759
- package/lib/implements/message/client.ts +0 -250
- package/lib/implements/message/manager.ts +0 -215
- package/lib/implements/threaddb/cbor/coding.ts +0 -62
- package/lib/implements/threaddb/cbor/event.ts +0 -336
- package/lib/implements/threaddb/cbor/node.ts +0 -542
- package/lib/implements/threaddb/cbor/record.ts +0 -398
- package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
- package/lib/implements/threaddb/common/addrinfo.ts +0 -135
- package/lib/implements/threaddb/common/dispatcher.ts +0 -81
- package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
- package/lib/implements/threaddb/common/json-patcher.ts +0 -204
- package/lib/implements/threaddb/common/key.ts +0 -290
- package/lib/implements/threaddb/common/level-adapter.ts +0 -235
- package/lib/implements/threaddb/common/lineReader.ts +0 -79
- package/lib/implements/threaddb/common/logstore.ts +0 -215
- package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
- package/lib/implements/threaddb/core/app.ts +0 -202
- package/lib/implements/threaddb/core/core.ts +0 -230
- package/lib/implements/threaddb/core/db.ts +0 -249
- package/lib/implements/threaddb/core/event.ts +0 -54
- package/lib/implements/threaddb/core/head.ts +0 -89
- package/lib/implements/threaddb/core/identity.ts +0 -171
- package/lib/implements/threaddb/core/logstore.ts +0 -137
- package/lib/implements/threaddb/core/options.ts +0 -14
- package/lib/implements/threaddb/core/record.ts +0 -54
- package/lib/implements/threaddb/db/collection.ts +0 -1910
- package/lib/implements/threaddb/db/db.ts +0 -698
- package/lib/implements/threaddb/db/json2Query.ts +0 -192
- package/lib/implements/threaddb/db/query.ts +0 -524
- package/lib/implements/threaddb/dbclient.ts +0 -443
- package/lib/implements/threaddb/dbmanager.ts +0 -1901
- package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -452
- package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
- package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
- package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
- package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -280
- package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
- package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
- package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
- package/lib/implements/threaddb/net/define.ts +0 -138
- package/lib/implements/threaddb/net/grpcClient.ts +0 -582
- package/lib/implements/threaddb/net/grpcserver.ts +0 -146
- package/lib/implements/threaddb/net/net.ts +0 -2006
- package/lib/implements/threaddb/pb/lstore.proto +0 -38
- package/lib/implements/threaddb/pb/lstore.ts +0 -393
- package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
- package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
- package/lib/implements/threaddb/pb/net.proto +0 -194
- package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
- package/lib/implements/threaddb/pb/net_pb.js +0 -5525
- package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
- package/lib/implements/util/client.ts +0 -72
- package/lib/implements/util/manager.ts +0 -146
- package/lib/implements/wallet/manager.ts +0 -664
- package/lib/index.ts +0 -57
- package/lib/interfaces/DCContext.ts +0 -51
- package/lib/interfaces/aiproxy-interface.ts +0 -145
- package/lib/interfaces/auth-interface.ts +0 -118
- package/lib/interfaces/cache-interface.ts +0 -22
- package/lib/interfaces/client-interface.ts +0 -11
- package/lib/interfaces/comment-interface.ts +0 -167
- package/lib/interfaces/database-interface.ts +0 -169
- package/lib/interfaces/file-interface.ts +0 -120
- package/lib/interfaces/index.ts +0 -10
- package/lib/interfaces/keyvalue-interface.ts +0 -156
- package/lib/interfaces/message-interface.ts +0 -22
- package/lib/interfaces/util-interface.ts +0 -31
- package/lib/modules/aiproxy-module.ts +0 -246
- package/lib/modules/auth-module.ts +0 -753
- package/lib/modules/cache-module.ts +0 -99
- package/lib/modules/client-module.ts +0 -71
- package/lib/modules/comment-module.ts +0 -429
- package/lib/modules/database-module.ts +0 -598
- package/lib/modules/file-module.ts +0 -291
- package/lib/modules/index.ts +0 -13
- package/lib/modules/keyvalue-module.ts +0 -379
- package/lib/modules/message-module.ts +0 -107
- package/lib/modules/util-module.ts +0 -148
- package/lib/polyfills/process-env-browser.ts +0 -1
- package/lib/proto/datasource.ts +0 -93
- package/lib/proto/dcnet.proto +0 -1601
- package/lib/proto/dcnet_proto.d.ts +0 -22857
- package/lib/proto/dcnet_proto.js +0 -55204
- package/lib/proto/dcnet_proto_sparse.js +0 -55166
- package/lib/proto/oidfetch.proto +0 -25
- package/lib/proto/oidfetch_proto.d.ts +0 -585
- package/lib/proto/oidfetch_proto.js +0 -1247
- package/lib/serverless/babel-browser.ts +0 -39
- package/lib/serverless/base_entity.ts +0 -78
- package/lib/serverless/base_repository.ts +0 -414
- package/lib/serverless/browser_schema_extractor.ts +0 -283
- package/lib/serverless/decorator_factory.ts +0 -322
- package/lib/util/BrowserLineReader.ts +0 -73
- package/lib/util/base64.ts +0 -105
- package/lib/util/bcrypt.ts +0 -206
- package/lib/util/curve25519Encryption.ts +0 -418
- package/lib/util/dccrypt.ts +0 -73
- package/lib/util/logger.ts +0 -104
- package/lib/util/utils.ts +0 -289
package/lib/common/dcutil.ts
DELETED
|
@@ -1,595 +0,0 @@
|
|
|
1
|
-
import { ChainUtil } from "./chain";
|
|
2
|
-
import { isName, multiaddr } from "@multiformats/multiaddr";
|
|
3
|
-
import { IDBDatastore } from "datastore-idb";
|
|
4
|
-
import { IDBBlockstore } from "blockstore-idb";
|
|
5
|
-
import { keys } from "@libp2p/crypto";
|
|
6
|
-
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2";
|
|
7
|
-
import { webRTC, webRTCDirect } from "@libp2p/webrtc";
|
|
8
|
-
import { createHelia, HeliaLibp2p } from "helia";
|
|
9
|
-
import { createLibp2p, Libp2p } from "libp2p";
|
|
10
|
-
import { identify, identifyPush } from "@libp2p/identify";
|
|
11
|
-
|
|
12
|
-
import { yamux } from "@chainsafe/libp2p-yamux";
|
|
13
|
-
import { noise } from "@chainsafe/libp2p-noise";
|
|
14
|
-
import type { Multiaddr } from "@multiformats/multiaddr";
|
|
15
|
-
import { kadDHT } from "@libp2p/kad-dht";
|
|
16
|
-
import { loadKeyPair, saveKeyPair } from "../util/utils";
|
|
17
|
-
import { Ed25519PrivateKey } from "@libp2p/interface";
|
|
18
|
-
import { ping } from "@libp2p/ping";
|
|
19
|
-
// import {mdns} from '@libp2p/mdns'
|
|
20
|
-
import {StreamWriter } from '../implements/file/streamwriter'
|
|
21
|
-
import { Stream } from '@libp2p/interface'
|
|
22
|
-
import { Uint8ArrayList } from 'uint8arraylist';
|
|
23
|
-
import { oidfetch } from "../proto/oidfetch_proto";
|
|
24
|
-
import { Blocks } from "@helia/interface";
|
|
25
|
-
import {CID} from 'multiformats/cid'
|
|
26
|
-
import {
|
|
27
|
-
concatenateUint8Arrays,
|
|
28
|
-
} from '../util/utils'
|
|
29
|
-
|
|
30
|
-
// http2 type
|
|
31
|
-
export class Http2_Type {
|
|
32
|
-
static Handshake = 0x00;
|
|
33
|
-
static Data = 0x01;
|
|
34
|
-
static ACK = 0x02;
|
|
35
|
-
static Close = 0x03;
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export class BrowserType {
|
|
40
|
-
static File = 1;
|
|
41
|
-
static ThreadDB = 2;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
interface CustomMessage {
|
|
48
|
-
type: number; // uint8 (1字节)
|
|
49
|
-
version: number; // uint16 (2字节, 大端序)
|
|
50
|
-
payload: Uint8Array; // 二进制数据
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
import { autoNAT } from "@libp2p/autonat";
|
|
55
|
-
import { dcutr } from "@libp2p/dcutr";
|
|
56
|
-
import { bitswap } from "@helia/block-brokers";
|
|
57
|
-
import { Client } from "./dcapi";
|
|
58
|
-
import { dc_protocol, dial_timeout } from "./define";
|
|
59
|
-
|
|
60
|
-
const controller = new AbortController();
|
|
61
|
-
const { signal } = controller;
|
|
62
|
-
// import {uPnPNAT} from '@libp2p/upnp-nat'
|
|
63
|
-
export class DcUtil {
|
|
64
|
-
dcChain: ChainUtil;
|
|
65
|
-
connectLength: number;
|
|
66
|
-
dcNodeClient: HeliaLibp2p<Libp2p> | undefined; // 什么类型?dc node 对象,主要用于建立连接
|
|
67
|
-
defaultPeerId: string | undefined; // 默认 peerId
|
|
68
|
-
|
|
69
|
-
constructor(dcChain: ChainUtil) {
|
|
70
|
-
this.dcChain = dcChain;
|
|
71
|
-
this.connectLength = 5;
|
|
72
|
-
}
|
|
73
|
-
// 连接到所有文件存储节点
|
|
74
|
-
_connectToObjNodes = async (cid: string): Promise<[Multiaddr | null,string[] | null]> => {
|
|
75
|
-
const peers = await this.dcChain.getObjNodes(cid);
|
|
76
|
-
if (!peers) {
|
|
77
|
-
console.error("peers is null");
|
|
78
|
-
return [null,null];
|
|
79
|
-
}
|
|
80
|
-
const res = await this._connectPeers(peers);
|
|
81
|
-
return [res, peers];
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
connectToPeer = async (peerAddr: string): Promise<Multiaddr> => {
|
|
86
|
-
return await this._connectPeers([peerAddr]);
|
|
87
|
-
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
_connectPeers = (peerListJson: string[]): Promise<Multiaddr> => {
|
|
91
|
-
return new Promise((reslove, reject) => {
|
|
92
|
-
const _this = this;
|
|
93
|
-
const len = peerListJson.length;
|
|
94
|
-
|
|
95
|
-
let num = 0;
|
|
96
|
-
|
|
97
|
-
async function dialNodeAddr(i: number) {
|
|
98
|
-
if (!peerListJson[i]) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const [nodeAddr,_] = await _this.dcChain.getDcNodeWebrtcDirectAddr(
|
|
102
|
-
peerListJson[i]
|
|
103
|
-
);
|
|
104
|
-
if (!nodeAddr) {
|
|
105
|
-
console.error("no nodeAddr return");
|
|
106
|
-
num++;
|
|
107
|
-
if (num >= len) {
|
|
108
|
-
reject("no nodeAddr return");
|
|
109
|
-
}
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
if (_this.dcNodeClient?.libp2p) {
|
|
115
|
-
const resCon = await _this.dcNodeClient?.libp2p.dial(nodeAddr, {
|
|
116
|
-
signal: AbortSignal.timeout(dial_timeout)
|
|
117
|
-
});
|
|
118
|
-
if (resCon) {
|
|
119
|
-
reslove(nodeAddr);
|
|
120
|
-
} else {
|
|
121
|
-
num++;
|
|
122
|
-
if (num >= len) {
|
|
123
|
-
reject("dial nodeAddr failed");
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
} catch (error) {
|
|
128
|
-
if (error && typeof error === "object" && "message" in error) {
|
|
129
|
-
num++;
|
|
130
|
-
if (num >= len) {
|
|
131
|
-
|
|
132
|
-
console.error("dial nodeAddr error,error:%s", (error as any).message);
|
|
133
|
-
reject((error as any).message);
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
num++;
|
|
137
|
-
if (num >= len) {
|
|
138
|
-
console.error("dial nodeAddr error,error:", error);
|
|
139
|
-
reject(error);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// 遍历传进来的promise数组
|
|
146
|
-
for (let i = 0; i < len; i++) {
|
|
147
|
-
dialNodeAddr(i);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
connectToUserDcPeer = async (
|
|
152
|
-
account: Uint8Array,
|
|
153
|
-
) : Promise<Client | null> => {
|
|
154
|
-
const peerAddrs = await this.dcChain.getAccountPeers(account);
|
|
155
|
-
if (!peerAddrs || peerAddrs.length == 0) {
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
// 连接节点
|
|
159
|
-
if (!this.dcNodeClient || !this.dcNodeClient?.libp2p) {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
const nodeAddr = await this._connectPeers(peerAddrs);
|
|
163
|
-
console.log("_connectNodeAddrs nodeAddr:", nodeAddr);
|
|
164
|
-
if (!nodeAddr) {
|
|
165
|
-
return null;
|
|
166
|
-
}
|
|
167
|
-
const client = new Client(this.dcNodeClient?.libp2p,this.dcNodeClient.blockstore, nodeAddr, dc_protocol);
|
|
168
|
-
return client;
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
// 连接节点列表
|
|
172
|
-
connectToUserAllDcPeers = async (
|
|
173
|
-
account: Uint8Array,
|
|
174
|
-
) : Promise<Client[] | null> => {
|
|
175
|
-
const peerAddrs = await this.dcChain.getAccountPeers(account);
|
|
176
|
-
if (!peerAddrs || peerAddrs.length == 0) {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
let clients: Client[] = [];
|
|
181
|
-
// 连接节点
|
|
182
|
-
for (let i = 0; i < peerAddrs.length; i++) {
|
|
183
|
-
const item = peerAddrs[i];
|
|
184
|
-
if(item){
|
|
185
|
-
try {
|
|
186
|
-
if (!this.dcNodeClient || !this.dcNodeClient?.libp2p) {
|
|
187
|
-
return null;
|
|
188
|
-
}
|
|
189
|
-
const nodeAddr = await this._connectPeers([item]);
|
|
190
|
-
console.log("_connectNodeAddrs nodeAddr:", nodeAddr);
|
|
191
|
-
if (!nodeAddr) {
|
|
192
|
-
return null;
|
|
193
|
-
}
|
|
194
|
-
const client = new Client(this.dcNodeClient?.libp2p, this.dcNodeClient.blockstore, nodeAddr, dc_protocol);
|
|
195
|
-
clients.push(client)
|
|
196
|
-
} catch (error) {
|
|
197
|
-
console.error("connectToUserAllDcPeers error", error);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return clients;
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
// 连接节点列表
|
|
205
|
-
_connectNodeAddrs = (peers: string[]): Promise<Multiaddr | null> => {
|
|
206
|
-
return new Promise((reslove, reject) => {
|
|
207
|
-
const _this = this;
|
|
208
|
-
const len = peers.length;
|
|
209
|
-
|
|
210
|
-
let num = 0;
|
|
211
|
-
|
|
212
|
-
async function dialNodeAddr(i: number) {
|
|
213
|
-
if (!peers[i]) {
|
|
214
|
-
console.log("nodeAddr return");
|
|
215
|
-
num++;
|
|
216
|
-
if (num >= len) {
|
|
217
|
-
reslove(null);
|
|
218
|
-
}
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
const addrParts = peers[i].split(",");
|
|
222
|
-
const nodeAddr = multiaddr(addrParts[1]);
|
|
223
|
-
|
|
224
|
-
try {
|
|
225
|
-
if (_this.dcNodeClient?.libp2p) {
|
|
226
|
-
const res = await _this.dcNodeClient.libp2p.dial(nodeAddr, {
|
|
227
|
-
signal: AbortSignal.timeout(dial_timeout)
|
|
228
|
-
});
|
|
229
|
-
console.log("nodeAddr try return");
|
|
230
|
-
console.log(res);
|
|
231
|
-
if (res) {
|
|
232
|
-
reslove(nodeAddr);
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
num++;
|
|
237
|
-
if (num >= len) {
|
|
238
|
-
reslove(null);
|
|
239
|
-
}
|
|
240
|
-
} catch (error) {
|
|
241
|
-
console.error("nodeAddr catch return", error);
|
|
242
|
-
num++;
|
|
243
|
-
if (num >= len) {
|
|
244
|
-
reslove(null);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// 遍历传进来的promise数组
|
|
250
|
-
for (let i = 0; i < len; i++) {
|
|
251
|
-
dialNodeAddr(i);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
_createHeliaNode = async (): Promise<HeliaLibp2p<Libp2p>> => {
|
|
259
|
-
console.log("_createHeliaNode=======");
|
|
260
|
-
const datastore = new IDBDatastore("helia-meta");
|
|
261
|
-
await datastore.open();
|
|
262
|
-
const blockstore = new IDBBlockstore("helia-blocks");
|
|
263
|
-
await blockstore.open();
|
|
264
|
-
|
|
265
|
-
// const memoryDatastore = new MemoryDatastore();
|
|
266
|
-
// 创建或导入私钥
|
|
267
|
-
let keyPair = (await loadKeyPair("ed25519_privateKey")) as Ed25519PrivateKey;
|
|
268
|
-
if (!keyPair) {
|
|
269
|
-
keyPair = await keys.generateKeyPair("Ed25519");
|
|
270
|
-
await saveKeyPair("ed25519_privateKey", keyPair);
|
|
271
|
-
}
|
|
272
|
-
// libp2p is the networking layer that underpins Helia
|
|
273
|
-
const libp2p = await createLibp2p({
|
|
274
|
-
privateKey: keyPair,
|
|
275
|
-
datastore: datastore,
|
|
276
|
-
transports: [webRTCDirect(), circuitRelayTransport(), webRTC()], //
|
|
277
|
-
connectionEncrypters: [noise()],
|
|
278
|
-
connectionGater: {
|
|
279
|
-
denyDialMultiaddr: () => false, // this is necessary to dial local addresses at all
|
|
280
|
-
},
|
|
281
|
-
|
|
282
|
-
connectionManager: {
|
|
283
|
-
maxParallelDials: 30,
|
|
284
|
-
maxConnections: 30,
|
|
285
|
-
inboundConnectionThreshold: 30,
|
|
286
|
-
},
|
|
287
|
-
|
|
288
|
-
streamMuxers: [
|
|
289
|
-
yamux({
|
|
290
|
-
maxStreamWindowSize: 256 * 1024, // 流窗口大小
|
|
291
|
-
maxMessageSize: 16 * 1024, // 消息分片阈值
|
|
292
|
-
keepAliveInterval: 15_000, // 保活检测间隔 (ms)
|
|
293
|
-
maxInboundStreams: 30,
|
|
294
|
-
maxOutboundStreams: 50,
|
|
295
|
-
initialStreamWindowSize: 256 * 1024,
|
|
296
|
-
enableKeepAlive: true,
|
|
297
|
-
}),
|
|
298
|
-
],
|
|
299
|
-
services: {
|
|
300
|
-
// dht: kadDHT({
|
|
301
|
-
// // 启用 DHT 加强节点发现
|
|
302
|
-
// clientMode: true,
|
|
303
|
-
// }),
|
|
304
|
-
autoNAT: autoNAT(),
|
|
305
|
-
dcutr: dcutr(),
|
|
306
|
-
identify: identify(),
|
|
307
|
-
identifyPush: identifyPush(),
|
|
308
|
-
ping: ping(),
|
|
309
|
-
autoRelay: (components) => ({
|
|
310
|
-
// 使用函数式配置
|
|
311
|
-
enabled: true, // 通过闭包传递参数
|
|
312
|
-
maxListeners: 2,
|
|
313
|
-
peerSource: components.peerStore, // 注入依赖组件
|
|
314
|
-
}),
|
|
315
|
-
},
|
|
316
|
-
addresses: {
|
|
317
|
-
listen: ["/webrtc-direct", "/p2p-circuit", "/webrtc"],
|
|
318
|
-
},
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
const dcNodeClient: HeliaLibp2p<Libp2p> = await createHelia({
|
|
322
|
-
blockBrokers: [
|
|
323
|
-
bitswap({
|
|
324
|
-
maxInboundStreams: 64,
|
|
325
|
-
maxOutboundStreams: 128,
|
|
326
|
-
}),
|
|
327
|
-
],
|
|
328
|
-
datastore,
|
|
329
|
-
blockstore,
|
|
330
|
-
libp2p,
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
this.dcNodeClient = dcNodeClient;
|
|
334
|
-
|
|
335
|
-
return dcNodeClient;
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
// 获取链接过的peerid
|
|
339
|
-
_getConnectedPeerId = async (): Promise<string> => {
|
|
340
|
-
if (this.defaultPeerId) {
|
|
341
|
-
return this.defaultPeerId;
|
|
342
|
-
} else {
|
|
343
|
-
const defaultPeerId = localStorage.getItem("defaultPeerId");
|
|
344
|
-
if (defaultPeerId) {
|
|
345
|
-
this.defaultPeerId = defaultPeerId;
|
|
346
|
-
return defaultPeerId;
|
|
347
|
-
}
|
|
348
|
-
return "";
|
|
349
|
-
}
|
|
350
|
-
};
|
|
351
|
-
_getNodeAddr = async (peerId: string): Promise<Multiaddr | undefined> => {
|
|
352
|
-
let [nodeAddr, _] = await this.dcChain.getDcNodeWebrtcDirectAddr(peerId);
|
|
353
|
-
if (!nodeAddr) {
|
|
354
|
-
console.error("no node address found for peer: ", peerId);
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
if (isName(nodeAddr)) {
|
|
358
|
-
const addrs = await nodeAddr.resolve();
|
|
359
|
-
nodeAddr = addrs[0] ? addrs[0] : null;
|
|
360
|
-
}
|
|
361
|
-
return nodeAddr ? nodeAddr : undefined;
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
getDefaultDcNodeAddr = async (): Promise<Multiaddr | undefined> => {
|
|
365
|
-
const peerId = await this._getConnectedPeerId();
|
|
366
|
-
if (peerId) {
|
|
367
|
-
let nodeAddr = await this._getNodeAddr(peerId);
|
|
368
|
-
if (nodeAddr) {
|
|
369
|
-
try {
|
|
370
|
-
const connection = await this.dcNodeClient?.libp2p.dial(nodeAddr, {
|
|
371
|
-
signal: AbortSignal.timeout(dial_timeout)
|
|
372
|
-
});
|
|
373
|
-
if (connection) {
|
|
374
|
-
return nodeAddr;
|
|
375
|
-
}
|
|
376
|
-
} catch (error) {
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
localStorage.removeItem("defaultPeerId");
|
|
380
|
-
}
|
|
381
|
-
// 获取节点上的默认节点列表,随机获取几个,批量连接节点,得到最快的节点
|
|
382
|
-
const allNodeList = await this.dcChain.getDcNodeList();
|
|
383
|
-
if (!allNodeList) {
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
// 连接节点,得到最快的节点(随机取几个连接取最快,如果都没有连接上继续随机取)
|
|
387
|
-
const nodeAddr = await this._getConnectDcNodeList(allNodeList);
|
|
388
|
-
if (!nodeAddr) {
|
|
389
|
-
console.error("no node connected");
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// 保存默认节点
|
|
394
|
-
const defaultPeerId = (nodeAddr as Multiaddr).getPeerId();
|
|
395
|
-
if (defaultPeerId) {
|
|
396
|
-
localStorage.setItem("defaultPeerId", defaultPeerId.toString());
|
|
397
|
-
}
|
|
398
|
-
return nodeAddr as Multiaddr;
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
_getConnectDcNodeList = async (
|
|
402
|
-
nodeList: string[]
|
|
403
|
-
): Promise<Multiaddr | undefined> => {
|
|
404
|
-
if (nodeList.length > this.connectLength) {
|
|
405
|
-
let dcNodeList = this._getRamdomNodeList(nodeList, this.connectLength);
|
|
406
|
-
const nodeAddr = await this._connectNodeAddrs(dcNodeList);
|
|
407
|
-
if (!nodeAddr) {
|
|
408
|
-
// allNodeList 过滤掉dcNodeList
|
|
409
|
-
const leftNodeList = nodeList.filter(
|
|
410
|
-
(node) => dcNodeList.indexOf(node) === -1
|
|
411
|
-
);
|
|
412
|
-
return this._getConnectDcNodeList(leftNodeList);
|
|
413
|
-
}
|
|
414
|
-
return nodeAddr as Multiaddr;
|
|
415
|
-
} else {
|
|
416
|
-
let nodeAddr = await this._connectNodeAddrs(nodeList);
|
|
417
|
-
if (!nodeAddr) {
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
return nodeAddr as Multiaddr;
|
|
421
|
-
}
|
|
422
|
-
};
|
|
423
|
-
_getRamdomNodeList = (nodeList: string[], num: number): string[] => {
|
|
424
|
-
const len = nodeList.length;
|
|
425
|
-
const res: string[] = [];
|
|
426
|
-
for (let i = 0; i < num; i++) {
|
|
427
|
-
const randomIndex = Math.floor(Math.random() * len);
|
|
428
|
-
res.push(nodeList[randomIndex]!);
|
|
429
|
-
}
|
|
430
|
-
return res;
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
//创建主动上报流处理,type:1-文件或文件夹假Cid,2-threaddb threadid,3-threaddb recordid
|
|
438
|
-
async createTransferStream(libp2p: Libp2p,blockstore: Blocks,nodeAddr: Multiaddr, type: number,oid: string) {
|
|
439
|
-
const nodeConn = await libp2p.dial(nodeAddr, {
|
|
440
|
-
signal: AbortSignal.timeout(dial_timeout)
|
|
441
|
-
});
|
|
442
|
-
//判断是否有现成的stream,如果已经存在就直接使用
|
|
443
|
-
const stream = await nodeConn.newStream("/dc/transfer/1.0.0")
|
|
444
|
-
try{
|
|
445
|
-
const writer = new StreamWriter(stream.sink)
|
|
446
|
-
const mParts: Uint8Array[] = [];
|
|
447
|
-
let parsedMessage: { type: number; version: number; payload: Uint8Array } | null = null;
|
|
448
|
-
let data: Uint8Array;
|
|
449
|
-
let handshakeFlag = false
|
|
450
|
-
|
|
451
|
-
for await (const chunk of this.chunkGenerator(stream)) {
|
|
452
|
-
if (chunk instanceof Uint8ArrayList) {
|
|
453
|
-
data = chunk.subarray();
|
|
454
|
-
} else {
|
|
455
|
-
data = chunk;
|
|
456
|
-
}
|
|
457
|
-
mParts.push(data);
|
|
458
|
-
// 合并所有数据块为完整 Uint8Array
|
|
459
|
-
const fullMessage = concatenateUint8Arrays(...mParts);
|
|
460
|
-
parsedMessage = null
|
|
461
|
-
parsedMessage = this.parseMessage(fullMessage);
|
|
462
|
-
if (parsedMessage) {
|
|
463
|
-
if (parsedMessage.type === 3) {//close
|
|
464
|
-
break
|
|
465
|
-
}
|
|
466
|
-
if (!handshakeFlag){
|
|
467
|
-
// 解析消息
|
|
468
|
-
const initRequest = oidfetch.pb.InitRequset.decode(parsedMessage.payload)
|
|
469
|
-
if (!initRequest) {
|
|
470
|
-
continue
|
|
471
|
-
}
|
|
472
|
-
//mParts 清空
|
|
473
|
-
mParts.length = 0
|
|
474
|
-
//发送数据到服务器
|
|
475
|
-
const message = new TextEncoder().encode(oid)
|
|
476
|
-
const initReply = new oidfetch.pb.InitReply({type: type, oid: message})
|
|
477
|
-
//组装数据
|
|
478
|
-
const initReplyBytes = oidfetch.pb.InitReply.encode(initReply).finish()
|
|
479
|
-
const messageData = this.assembleCustomMessage({
|
|
480
|
-
type: Http2_Type.ACK,
|
|
481
|
-
version: 1,
|
|
482
|
-
payload: initReplyBytes,
|
|
483
|
-
}) as any
|
|
484
|
-
await writer.write(messageData)
|
|
485
|
-
handshakeFlag = true
|
|
486
|
-
}else{
|
|
487
|
-
// 解析消息
|
|
488
|
-
const fetchRequest = oidfetch.pb.FetchRequest.decode(parsedMessage.payload)
|
|
489
|
-
|
|
490
|
-
const resCid = new TextDecoder().decode(fetchRequest.cid)
|
|
491
|
-
//获取resCid对应的block
|
|
492
|
-
const cid = CID.parse(resCid);
|
|
493
|
-
|
|
494
|
-
// 通过 blockstore 获取该 CID 对应的区块
|
|
495
|
-
try {
|
|
496
|
-
const block = await blockstore.get(cid);
|
|
497
|
-
const fetchReply = new oidfetch.pb.FetchReply({data: block})
|
|
498
|
-
const fetchReplyBytes = oidfetch.pb.FetchReply.encode(fetchReply).finish()
|
|
499
|
-
const messageData = this.assembleCustomMessage({
|
|
500
|
-
type: Http2_Type.ACK,
|
|
501
|
-
version: 1,
|
|
502
|
-
payload: fetchReplyBytes,
|
|
503
|
-
}) as any
|
|
504
|
-
await writer.write(messageData)
|
|
505
|
-
mParts.length = 0
|
|
506
|
-
} catch (error) {
|
|
507
|
-
console.error('Error retrieving block:', error);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
}catch(err){
|
|
513
|
-
console.error("createTransferStream error:", err);
|
|
514
|
-
throw err
|
|
515
|
-
}finally{
|
|
516
|
-
stream.close()
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
private async *chunkGenerator(stream: Stream): AsyncGenerator<Uint8Array> {
|
|
523
|
-
const iterator = stream.source[Symbol.asyncIterator]();
|
|
524
|
-
while (true) {
|
|
525
|
-
try {
|
|
526
|
-
const { done, value } = await iterator.next();
|
|
527
|
-
if (done)
|
|
528
|
-
break;
|
|
529
|
-
const res = value instanceof Uint8ArrayList ? value.subarray() : value;
|
|
530
|
-
yield res;
|
|
531
|
-
} catch (err) {
|
|
532
|
-
console.log('chunkGenerator error:', err);
|
|
533
|
-
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
/**
|
|
541
|
-
* 组装 CustomMessage 数据到 Uint8Array
|
|
542
|
-
* @param message - CustomMessage 包含消息的基本结构
|
|
543
|
-
* @returns Uint8Array - 序列化后的数据
|
|
544
|
-
*/
|
|
545
|
-
assembleCustomMessage(message: CustomMessage): Uint8Array {
|
|
546
|
-
// Step 1: header部分(1字节类型 + 2字节版本号 + 4字节payload长度)
|
|
547
|
-
const headerLength = 7; // Header固定长度:1字节Type + 2字节Version + 4字节Payload长度
|
|
548
|
-
const payloadLength = message.payload.byteLength;
|
|
549
|
-
|
|
550
|
-
const buffer = new Uint8Array(headerLength + payloadLength);
|
|
551
|
-
|
|
552
|
-
buffer[0] = message.type;
|
|
553
|
-
buffer[1] = (message.version >> 8) & 0xff;
|
|
554
|
-
buffer[2] = message.version & 0xff;
|
|
555
|
-
buffer[3] = (payloadLength >> 24) & 0xff;
|
|
556
|
-
buffer[4] = (payloadLength >> 16) & 0xff;
|
|
557
|
-
buffer[5] = (payloadLength >> 8) & 0xff;
|
|
558
|
-
buffer[6] = payloadLength & 0xff;
|
|
559
|
-
|
|
560
|
-
// Step 5: 设置 Payload 数据
|
|
561
|
-
buffer.set(message.payload, headerLength);
|
|
562
|
-
|
|
563
|
-
return buffer;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
parseMessage(data: Uint8Array): { type: number; version: number; payload: Uint8Array } | null {
|
|
567
|
-
if (data.length < 7) {
|
|
568
|
-
return null;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
// 第 1 字节: 消息类型
|
|
572
|
-
const type = data[0]!;
|
|
573
|
-
|
|
574
|
-
// 第 2 和 3 字节: 版本号(大端序)
|
|
575
|
-
const version = (data[1]! << 8) | data[2]!; // 手动处理大端序
|
|
576
|
-
|
|
577
|
-
// 第 4 至 7 字节: payload 长度(大端序)
|
|
578
|
-
const payloadLength = (data[3]! << 24) | (data[4]! << 16) | (data[5]! << 8) | data[6]!;
|
|
579
|
-
|
|
580
|
-
// 验证数据完整性
|
|
581
|
-
if (data.length < 7 + payloadLength) {
|
|
582
|
-
return null;
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// 提取 payload
|
|
586
|
-
const payload = data.slice(7, 7 + payloadLength); // 提取负载数据
|
|
587
|
-
|
|
588
|
-
return {
|
|
589
|
-
type,
|
|
590
|
-
version,
|
|
591
|
-
payload,
|
|
592
|
-
};
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
}
|
package/lib/common/define.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
// 只考虑浏览器环境
|
|
2
|
-
declare const __IS_PROD__: boolean | undefined;
|
|
3
|
-
|
|
4
|
-
let isProd = false;
|
|
5
|
-
// 打包后用 __IS_PROD__,源码直用时用 window.IS_PROD
|
|
6
|
-
if (typeof __IS_PROD__ !== "undefined") {
|
|
7
|
-
isProd = __IS_PROD__;
|
|
8
|
-
} else if (
|
|
9
|
-
typeof window !== "undefined" &&
|
|
10
|
-
typeof (window as any).IS_PROD !== "undefined"
|
|
11
|
-
) {
|
|
12
|
-
isProd = (window as any).IS_PROD;
|
|
13
|
-
}
|
|
14
|
-
const walletOpenType =
|
|
15
|
-
typeof globalThis !== "undefined" ? (globalThis as any).walletOpenType : ""; // 用于判断是否是直接打开;
|
|
16
|
-
const walletOpenOrgin =
|
|
17
|
-
typeof globalThis !== "undefined" ? (globalThis as any).walletOpenOrgin : ""; // 用户传入的打开钱包的源;
|
|
18
|
-
const walletOpenVersion =
|
|
19
|
-
typeof globalThis !== "undefined" ? (globalThis as any).walletOpenVersion : ""; // 钱包版本号;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
let _baseUrl = "";
|
|
23
|
-
let _walletOrigin = "";
|
|
24
|
-
if (true) {
|
|
25
|
-
_baseUrl = "/v0_0_16";
|
|
26
|
-
_walletOrigin = "https://wallet.dcnetio.com";
|
|
27
|
-
|
|
28
|
-
if(walletOpenOrgin) {
|
|
29
|
-
_walletOrigin = walletOpenOrgin || "https://wallet.dcnetio.com";
|
|
30
|
-
}
|
|
31
|
-
if(walletOpenVersion) {
|
|
32
|
-
_baseUrl = walletOpenVersion && walletOpenVersion.startsWith("/") ? walletOpenVersion : "/" + walletOpenVersion;
|
|
33
|
-
}
|
|
34
|
-
} else {
|
|
35
|
-
_baseUrl = "";
|
|
36
|
-
_walletOrigin = "http://localhost:3000";
|
|
37
|
-
}
|
|
38
|
-
export const walletOrigin = _walletOrigin;
|
|
39
|
-
export const walletUrl = _walletOrigin + _baseUrl; // 钱包地址后面统一改成origin+version
|
|
40
|
-
export const walletWindowName = "walletWindow"; // 窗口名称
|
|
41
|
-
export const dc_protocol = "/dc/thread/0.0.1";
|
|
42
|
-
export const dial_timeout = 1000;
|
|
43
|
-
export const keyExpire = 60 * 60 * 24; // setcachekey 过期时间默认一天
|
|
44
|
-
export const OffChainOpTimes = 20000; // 链下操作次数
|
|
45
|
-
export const OffChainSpaceLimit = 1024 * 1024 * 10; // 评论空间下限10m
|
|
46
|
-
export const OffChainOpTimesLimit = 1000; // 链下操作次数下限
|
|
47
|
-
export enum Direction {
|
|
48
|
-
Forward = 0,
|
|
49
|
-
Reverse = 1,
|
|
50
|
-
}
|
|
51
|
-
export enum CommentType {
|
|
52
|
-
/** 普通评论 */
|
|
53
|
-
Comment = 0,
|
|
54
|
-
|
|
55
|
-
/** 赞 */
|
|
56
|
-
Up = 1,
|
|
57
|
-
|
|
58
|
-
/** 踩 */
|
|
59
|
-
Down = 2,
|
|
60
|
-
|
|
61
|
-
/** 推荐或转发 */
|
|
62
|
-
Transfer = 3,
|
|
63
|
-
|
|
64
|
-
/** keyvalue形式的数据 */
|
|
65
|
-
KeyValue = 4,
|
|
66
|
-
}
|