-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (47 loc) · 1.17 KB
/
index.js
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
const fs = require("fs");
const factory = require("./file_block.js");
const BLOCK_SIZE = 2097152;
let wasmFns = null;
const getWasmFns = async () => {
if (!wasmFns) {
wasmFns = await factory();
}
return wasmFns;
};
const transformASMFileResult = (parts) => {
const array = [];
for (let i = 0; i < parts.size(); i++) {
const part = parts.get(i);
const obj = {
partNum: part.part_num,
endOffset: part.end_offset,
cumulateSha1: part.cumulate_sha1,
};
array.push(obj);
}
return array;
};
const generateFileBlockDigest = async (file) => {
if (typeof file === "string") {
file = fs.readFileSync(file);
}
const wasmFns = await getWasmFns();
const fileParts = transformASMFileResult(
wasmFns.generateFileBlockDigest(file).parts
);
const fileSize = file.length;
const blockSha = [];
const blockBase64 = [];
fileParts.forEach((part) => {
blockSha.push(part.cumulateSha1);
blockBase64.push(
file.slice(part.endOffset - BLOCK_SIZE, part.endOffset).toString("base64")
);
});
return {
fileSize,
blockSha,
blockBase64,
};
};
exports.generateFileBlockDigest = generateFileBlockDigest;