-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
79 lines (65 loc) · 1.82 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as fs from 'fs';
import * as ipfs from 'ipfs-http-client';
import * as crypto from 'crypto';
const client = ipfs.create({
url: 'http://127.0.0.1:5001/'
});
async function* readFileChunks(filePath: string): AsyncIterable<Uint8Array> {
const fileDescriptor = await fs.promises.open(filePath, 'r');
const fileSize = (await fileDescriptor.stat()).size;
let position = 0;
const chunk = Buffer.alloc(256 * 1024); // 256KB
while (position < fileSize) {
const { bytesRead, buffer } = await fileDescriptor.read(
chunk,
0,
chunk.byteLength,
);
yield buffer.slice(0, bytesRead);
if (bytesRead <= 0) {
break;
}
position += bytesRead;
}
await fileDescriptor.close();
}
(async () => {
console.log(await client.version());
if (1) {
const hash2 = crypto.createHash('sha256');
for await (const chunk of readFileChunks('LICENSES.chromium.html')) {
hash2.update(chunk);
}
console.log('expected hash: ', hash2.digest().toString('hex'));
}
const fileIterator = {
async *[Symbol.asyncIterator]() {
for (const entry of [1, 2]) {
const item = {
path: `${entry}.txt`,
content: readFileChunks('LICENSES.chromium.html'),
};
yield item;
}
},
};
const resp = await client.addAll(fileIterator, {
wrapWithDirectory: true
});
let rootCid: string = '';
for await (const item of resp) {
console.log(item);
if (item.path === '') {
rootCid = item.cid.toString();
}
}
console.log('rootCid: ', rootCid);
if (1) {
const downloaded = await client.get(`${rootCid}/1.txt`);
const hash2 = crypto.createHash('sha256');
for await (const chunk of downloaded) {
hash2.update(chunk);
}
console.log('downloaded hash: ', hash2.digest().toString('hex'));
}
})();