-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.31 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
const crypto = require('crypto');
const fetch = require('node-fetch');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const defaultOptions = {
algorithm: 'aes256',
mimeType: 'audio/ogg',
chunkSize: 1024 * 1024,
sleep: 0.1
};
module.exports.decrypt = (url, key, options) => {
options = { ...defaultOptions, ...options };
return fetch(url)
.then(response => response.arrayBuffer())
.then(blob => {
const keyBuf = Buffer.from(key);
const cipher = crypto.createDecipher(options.algorithm, keyBuf);
return new Promise(resolve => {
let buffer = Buffer.alloc(0);
cipher.on('readable', async () => {
while (null !== (chunk = cipher.read(options.chunkSize))) {
buffer = Buffer.concat([buffer, chunk]);
await sleep(options.sleep);
}
});
cipher.on('end', () => {
resolve(`data:${options.mimeType};base64,` + buffer.toString('base64'));
});
cipher.write(Buffer.from(blob).toString('base64'), 'base64');
cipher.end();
});
})
.catch((err) => {
console.log(err)
});
}