This repository has been archived by the owner on Apr 13, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
136 lines (112 loc) · 3.48 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
module.exports = parseTorrent
module.exports.toBuffer = toBuffer
var bencode = require('bencode')
var path = require('path')
var Rusha = require('rusha-browserify') // Fast SHA1 (works in browser)
/**
* Parse a torrent. Throws an exception if the torrent is missing required fields.
* @param {Buffer|Object} torrent
* @return {Object} parsed torrent
*/
function parseTorrent (torrent) {
if (Buffer.isBuffer(torrent)) {
torrent = bencode.decode(torrent)
}
// sanity check
ensure(torrent.info, 'info')
ensure(torrent.info.name, 'info.name')
ensure(torrent.info['piece length'], 'info[\'piece length\']')
ensure(torrent.info.pieces, 'info.pieces')
if (torrent.info.files) {
torrent.info.files.forEach(function (file) {
ensure(typeof file.length === 'number', 'info.files[0].length')
ensure(file.path, 'info.files[0].path')
})
} else {
ensure(torrent.info.length, 'info.length')
}
var result = {}
result.info = torrent.info
result.infoBuffer = bencode.encode(torrent.info)
result.infoHash = sha1(result.infoBuffer)
result.name = torrent.info.name.toString()
result.private = !!torrent.info.private
if (torrent['creation date'])
result.created = new Date(torrent['creation date'] * 1000)
// announce/announce-list may be missing if metadata fetched via ut_metadata extension
var announce = torrent['announce-list']
if (!announce) {
if (torrent.announce) {
announce = [[torrent.announce]]
} else {
announce = []
}
}
result.announceList = announce.map(function (urls) {
return urls.map(function (url) {
return url.toString()
})
})
result.announce = [].concat.apply([], result.announceList)
var files = torrent.info.files || [torrent.info]
result.files = files.map(function (file, i) {
var parts = [].concat(file.name || result.name, file.path || []).map(function (p) {
return p.toString()
})
return {
path: path.join.apply(null, [path.sep].concat(parts)).slice(1),
name: parts[parts.length - 1],
length: file.length,
offset: files.slice(0, i).reduce(sumLength, 0)
}
})
result.length = files.reduce(sumLength, 0)
var lastFile = result.files[result.files.length - 1]
result.pieceLength = torrent.info['piece length']
result.lastPieceLength = ((lastFile.offset + lastFile.length) % result.pieceLength) || result.pieceLength
result.pieces = splitPieces(torrent.info.pieces)
return result
}
/**
* Convert a parsed torrent object back into a .torrent file buffer.
* @param {Object} parsed parsed torrent
* @return {Buffer}
*/
function toBuffer (parsed) {
var torrent = {
info: parsed.info
}
if (parsed.announceList) {
torrent['announce-list'] = parsed.announceList.map(function (urls) {
return urls.map(function (url) {
url = new Buffer(url, 'utf8')
if (!torrent.announce) {
torrent.announce = url
}
return url
})
})
}
if (parsed.created) {
torrent['creation date'] = (parsed.created.getTime() / 1000) | 0
}
return bencode.encode(torrent)
}
function sumLength (sum, file) {
return sum + file.length
}
function splitPieces (buf) {
var pieces = []
for (var i = 0; i < buf.length; i += 20) {
pieces.push(buf.slice(i, i + 20).toString('hex'))
}
return pieces
}
function sha1 (buf) {
return (new Rusha()).digestFromBuffer(buf)
}
function ensure (bool, fieldName) {
if (!bool) {
throw new Error('Torrent is missing required field: ' + fieldName)
}
}