/
index.js
89 lines (78 loc) · 2.45 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const hyperdrive = require('hyperdrive')
const memdb = require('memdb')
const http = require('stream-http')
const crypto = require('crypto')
const swarm = require('hyperdrive-archive-swarm')
const signalhub = require('signalhub')
const normalize = require('normalize-url')
const toBlobURL = require('stream-to-blob-url')
const path = require('path')
var hub = signalhub(`peerdownload-${signature(window.location.href)}`, ['https://signalhub.mafintosh.com'])
var drive = hyperdrive(memdb())
var fileHashes = {}
var channel = signature(window.location.href)
var sub = hub.subscribe(channel)
sub.on('data', (msg) => {
if (msg.type === 'fetch') {
if (Object.keys(fileHashes).length > 0) {
hub.broadcast(channel, {type: 'hash', hashes: fileHashes})
}
} else if (msg.type === 'hash') {
Object.keys(msg.hashes).forEach((url) => {
fileHashes[url] = msg.hashes[url]
})
}
})
hub.broadcast(channel, {type: 'fetch'})
document.addEventListener('DOMContentLoaded', hook)
function hook () {
document.querySelectorAll('[peerdownload]').forEach((dom) => {
dom.addEventListener('click', (e) => {
e.preventDefault()
download(dom.href)
})
})
}
function download (fileURL) {
if (fileHashes[signature(fileURL)]) {
// if it's a known file in fileHashes, use peerdownload
var archive = drive.createArchive(fileHashes[signature(fileURL)])
swarm(archive)
downloadArchiveFile(archive, fileURL, () => { console.log('done') })
} else {
var archive = drive.createArchive()
var ws = archive.createFileWriteStream('data')
http.get(fileURL, (res) => {
res.pipe(ws).on('finish', () => {
archive.finalize(() => {
swarm(archive)
downloadArchiveFile(archive, fileURL, () => {
fileHashes[signature(fileURL)] = archive.key.toString('hex')
hub.broadcast(channel, {type: 'hash', hashes: fileHashes})
})
})
})
})
}
}
function signature (url) {
return crypto.createHash('sha256').update(normalize(url)).digest('hex')
}
function saveBlobURL (fileURL, blobURL) {
var a = document.createElement('a')
document.body.appendChild(a)
a.style = "display: none"
a.href = blobURL
a.download = path.basename(fileURL)
a.click()
}
function downloadArchiveFile(archive, fileURL, cb) {
var rs = archive.createFileReadStream('data')
toBlobURL(rs, (err, blobURL) => {
if (err) {
return cb(err)
}
saveBlobURL(fileURL, blobURL)
cb()
})
}