-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathfile.mjs
585 lines (497 loc) · 16.8 KB
/
file.mjs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import { e64, d64, AES, formatKey, getCipher, megaDecrypt } from './crypto/index.mjs'
import API from './api.mjs'
import { EventEmitter } from 'events'
import { streamToCb, createPromise } from './util.mjs'
import { PassThrough } from 'stream'
import StreamSkip from 'stream-skip'
import AbortController from './abort-controller-polyfill.mjs'
class File extends EventEmitter {
constructor (opt) {
super()
this.checkConstructorArgument(opt.downloadId)
this.checkConstructorArgument(opt.key)
this.checkConstructorArgument(opt.loadedFile)
this.downloadId = opt.downloadId
this.key = opt.key ? formatKey(opt.key) : null
this.type = opt.directory ? 1 : 0
this.directory = !!opt.directory
if (this.directory && !this.children) this.children = []
// Accept an API object
this.api = opt.api || API.getGlobalApi()
if (!(this.api instanceof API)) {
throw Error('api must be a instance of API')
}
this.loadedFile = opt.loadedFile
}
get createdAt () {
if (typeof this.timestamp !== 'undefined') {
return this.timestamp * 1000
}
}
checkConstructorArgument (value) {
// If a string was passed then check if it's not empty and
// contains only base64 valid characters
if (typeof value === 'string' && !/^[\w-]+$/.test(value)) {
throw Error(`Invalid argument: "${value}"`)
}
}
loadMetadata (aes, opt) {
this.size = opt.s || 0
this.timestamp = opt.ts || 0
this.type = opt.t
this.directory = !!opt.t
this.owner = opt.u
this.name = null
if (this.directory && !this.children) this.children = []
if (!aes || !opt.k) return
const parts = opt.k.split(':')
this.key = formatKey(parts[parts.length - 1])
if (this.key.length <= 32) {
// Regular AES-ECB encrypted key
aes.decryptECB(this.key)
} else if (this.storage) {
// RSA encrypted key
this.key = this.storage.decryptRsaKey(this.key).slice(0, this.directory ? 16 : 32)
} else {
// Can't decrypt a RSA key without a storage
this.key = null
}
if (opt.a) {
this.decryptAttributes(opt.a)
}
}
decryptAttributes (at) {
if (!this.key) return this
at = d64(at)
getCipher(this.key).decryptCBC(at)
const unpackedAttribtes = File.unpackAttributes(at)
if (unpackedAttribtes) {
this.parseAttributes(unpackedAttribtes)
}
}
parseAttributes (at) {
this.attributes = at
this.name = at.n
this.label = LABEL_NAMES[at.lbl || 0]
this.favorited = !!at.fav
}
loadAttributes (originalCb) {
const [cb, promise] = createPromise(originalCb)
// todo: nodeId version ('n')
const req = this.directory
? {
a: 'f',
c: 1,
ca: 1,
r: 1,
_querystring: {
n: this.downloadId
}
}
: {
a: 'g',
p: this.downloadId
}
this.api.request(req, (err, response) => {
if (err) return cb(err)
if (this.directory) {
const filesMap = Object.create(null)
const nodes = response.f
const folder = nodes.find(node => node.k &&
// the root folder is the one which "n" equals the first part of "k"
node.h === node.k.split(':')[0]
)
const aes = this.key ? new AES(this.key) : null
this.nodeId = folder.h
this.timestamp = folder.ts
filesMap[folder.h] = this
for (const file of nodes) {
if (file === folder) continue
const fileObj = new File(file, this.storage)
fileObj.loadMetadata(aes, file)
// is it the best way to handle this?
fileObj.downloadId = [this.downloadId, file.h]
filesMap[file.h] = fileObj
}
for (const file of nodes) {
const parent = filesMap[file.p]
if (parent) {
const fileObj = filesMap[file.h]
parent.children.push(fileObj)
fileObj.parent = parent
}
}
this.loadMetadata(aes, folder)
if (this.key && !this.attributes) {
return cb(Error('Attributes could not be decrypted with provided key.'))
}
if (this.loadedFile) {
const loadedNode = filesMap[this.loadedFile]
if (typeof loadedNode === 'undefined') {
cb(Error('Node (file or folder) not found in folder'))
} else {
cb(null, loadedNode)
}
} else {
cb(null, this)
}
} else {
this.size = response.s
this.decryptAttributes(response.at)
if (this.key && !this.attributes) {
return cb(Error('Attributes could not be decrypted with provided key.'))
}
cb(null, this)
}
})
return promise
}
download (options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}
if (!options) options = {}
const start = options.start || 0
const apiStart = options.returnCiphertext ? start : start - start % 16
let end = options.end || null
const maxConnections = options.maxConnections || 4
const initialChunkSize = options.initialChunkSize || 128 * 1024
const chunkSizeIncrement = options.chunkSizeIncrement || 128 * 1024
const maxChunkSize = options.maxChunkSize || 1024 * 1024
const ssl = API.handleForceHttps(options.forceHttps) ? 2 : 0
const req = {
a: 'g',
g: 1,
ssl
}
if (this.nodeId) {
req.n = this.nodeId
} else if (Array.isArray(this.downloadId)) {
req._querystring = {
n: this.downloadId[0]
}
req.n = this.downloadId[1]
} else {
req.p = this.downloadId
}
if (this.directory) throw Error("Can't download: folder download isn't supported")
// If options.returnCiphertext is true then the ciphertext is returned.
// The result can be decrypted later using mega.decrypt() stream
if (!this.key && !options.returnCiphertext) throw Error("Can't download: key isn't defined")
const decryptStream = this.key && !options.returnCiphertext
? megaDecrypt(this.key, {
start: apiStart,
disableVerification: apiStart !== 0 || end !== null
})
: new PassThrough()
const stream = apiStart === start
? decryptStream
: decryptStream.pipe(new StreamSkip({
skip: start - apiStart
}))
const handleRetries = options.handleRetries || File.defaultHandleRetries
this.api.request(req, (err, response) => {
if (err) return stream.emit('error', err)
if (typeof response.g !== 'string' || response.g.substr(0, 4) !== 'http') {
return stream.emit('error', Error('MEGA servers returned an invalid response, maybe caused by rate limit'))
}
// Special case for empty files
if (response.s === 0) return stream.end()
if (!end) end = response.s - 1
if (start > end) return stream.emit('error', Error("You can't download past the end of the file."))
function handleMegaErrors (resp) {
if (resp.status === 200) return
if (resp.status === 509) {
const timeLimit = resp.headers.get('x-mega-time-left')
const error = Error('Bandwidth limit reached: ' + timeLimit + ' seconds until it resets')
// Export error as a property of the error
error.timeLimit = timeLimit
stream.emit('error', error)
return
}
stream.emit('error', Error('MEGA returned a ' + resp.status + ' status code'))
}
function handleError (err) {
stream.emit('error', err)
}
// Handle progress events
let i = 0
stream.on('data', d => {
i += d.length
stream.emit('progress', {
bytesLoaded: i,
bytesTotal: response.s
})
})
// If maxConnections is 1 then disable chunking
if (maxConnections === 1) {
const controller = new AbortController()
// Abort stream if required
stream.on('close', () => {
controller.abort()
})
this.api.fetch(response.g + '/' + apiStart + '-' + end, {
signal: controller.signal
}).then(response => {
handleMegaErrors(response)
const body = response.body
if (!body) {
throw Error('Missing response body')
} else if (body.pipe) {
response.body.pipe(decryptStream)
} else if (body.getReader) {
const reader = body.getReader()
const read = ({ done, value }) => {
if (done) {
decryptStream.end()
} else {
decryptStream.write(value)
return reader.read().then(read)
}
}
reader.read().then(read)
} else {
throw Error('Single connection streaming not supported by fetch')
}
}).catch(handleError)
return
}
const chunkBuffer = {}
let lastStartedChunk = 0
let nextChunk = 0
let stopped = false
let currentOffset = apiStart
let chunkSize = initialChunkSize
// Stop the stream on errors and if required
stream.on('error', () => {
stopped = true
})
stream.on('close', () => {
stopped = true
})
const getChunk = () => {
if (currentOffset > end) {
stopped = true
if (lastStartedChunk === nextChunk) {
decryptStream.end()
}
return
}
const chunkOffset = currentOffset
const chunkMax = Math.min(end, chunkOffset + chunkSize - 1)
const chunkNumber = lastStartedChunk++
let tries = 0
const tryFetchChunk = () => {
tries++
this.api.fetch(response.g + '/' + chunkOffset + '-' + chunkMax)
.then(response => {
handleMegaErrors(response)
return response.arrayBuffer()
}).then(data => {
const dataBuffer = Buffer.from(data)
chunkBuffer[chunkNumber] = dataBuffer
if (nextChunk === chunkNumber) {
handleStreamWrite()
}
}, error => {
handleRetries(tries, error, error => {
if (error) {
handleError(error)
} else {
tryFetchChunk()
}
})
})
}
tryFetchChunk()
currentOffset = chunkMax + 1
if (chunkSize < maxChunkSize) {
chunkSize = chunkSize + chunkSizeIncrement
}
}
const handleStreamWrite = () => {
let shouldWaitDrain
// Check if the next chunk is in the buffer
while (true) {
const bufferChunk = chunkBuffer[nextChunk]
if (!bufferChunk) break
shouldWaitDrain = !decryptStream.write(bufferChunk)
delete chunkBuffer[nextChunk]
nextChunk++
if (shouldWaitDrain) break
}
// Check if the stream stopped and if it's the last chunk then end the stream
if (stopped && lastStartedChunk === nextChunk) {
decryptStream.end()
}
if (shouldWaitDrain) {
decryptStream.once('drain', handleStreamWrite)
} else {
getChunk()
}
}
for (let i = 0; i < maxConnections; i++) {
getChunk()
}
})
if (cb) streamToCb(stream, cb)
return stream
}
// Just wraps the download function as it can't adapted to
// use promises without causing performance issues.
downloadBuffer (options, originalCb) {
const [cb, promise] = createPromise(originalCb)
this.download(options, cb)
return promise
}
link (options, originalCb) {
if (arguments.length === 1 && typeof options === 'function') {
originalCb = options
options = {
noKey: false
}
}
const [cb, promise] = createPromise(originalCb)
if (typeof options === 'boolean') {
options = {
noKey: options
}
}
let url = `https://mega.nz/${this.directory ? 'folder' : 'file'}/${this.downloadId}`
if (!options.noKey && this.key) url += `#${e64(this.key)}`
if (!options.noKey && this.loadedFile) {
// TODO: check if the loaded file is, in fact, a folder
url += `/file/${this.loadedFile}`
}
// Is a synchronous function, but returns a promise to keep the same signature as MutableFile
cb(null, url)
return promise
}
find (query, deep) {
if (!this.children) throw Error('You can only call .find on directories')
if (typeof query === 'string') {
const queryString = query
query = file => file.name === queryString
} else if (Array.isArray(query)) {
const queryArray = query
query = file => queryArray.includes(file.name)
}
if (typeof query !== 'function') {
throw Error('Query must be a file matching function, an array of valid file names or a string with a file name')
}
return this.children.reduce((result, entry) => {
if (result) return result
if (query(entry)) return entry
if (entry.children && deep) {
return entry.find(query, deep)
}
return null
}, null)
}
filter (query, deep) {
if (!this.children) throw Error('You can only call .filter on directories')
if (typeof query === 'string') {
const queryString = query
query = file => file.name === queryString
} else if (Array.isArray(query)) {
const queryArray = query
query = file => queryArray.includes(file.name)
}
if (typeof query !== 'function') {
throw Error('Query must be a file matching function, an array of valid file names or a string with a file name')
}
return this.children.reduce((results, entry) => {
if (query(entry)) results.push(entry)
if (entry.children && deep) {
return results.concat(entry.filter(query, deep))
}
return results
}, [])
}
navigate (query) {
if (!this.children) throw Error('You can only call .navigate on directories')
if (typeof query === 'string') {
query = query.split('/')
} else if (!Array.isArray(query)) {
throw Error('Query must be an array or a string')
}
return query.reduce((node, name) => {
return node && node.children && node.children.find(e => e.name === name)
}, this)
}
static fromURL (opt, extraOpt = {}) {
if (typeof opt === 'object') {
// todo: warn to use File directly
return new File(opt)
}
// Supported formats:
// Old format:
// https://mega.nz/#!file_handler
// https://mega.nz/#!file_handler!file_key
// https://mega.nz/#F!folder_handler
// https://mega.nz/#F!folder_handler!folder_key
// https://mega.nz/#F!folder_handler!folder_key!file_handler
// New format (2020):
// https://mega.nz/file/file_handler
// https://mega.nz/file/file_handler#file_key
// https://mega.nz/folder/folder_handler
// https://mega.nz/folder/folder_handler#folder_key
// https://mega.nz/folder/folder_handler#folder_key/file/file_handler
const url = new URL(opt)
if (url.hostname !== 'mega.nz' && url.hostname !== 'mega.co.nz') throw Error('Invalid URL: wrong hostname')
if (!url.hash) throw Error('Invalid URL: no hash')
if (url.pathname.match(/\/(file|folder)\//) !== null) {
// new format
const split = url.hash.substr(1).split('/file/')
const fileHandler = url.pathname.substring(
url.pathname.lastIndexOf('/') + 1,
url.pathname.length + 1
)
const fileKey = split[0]
if ((fileHandler && !fileKey) || (!fileHandler && fileKey)) throw Error('Invalid URL: too few arguments')
return new File({
downloadId: fileHandler,
key: fileKey,
directory: url.pathname.indexOf('/folder/') >= 0,
loadedFile: split[1],
...extraOpt
})
} else {
// old format
const split = url.hash.split('!')
if (split[0] !== '#' && split[0] !== '#F') throw Error('Invalid URL: format not recognized')
if (split.length <= 1) throw Error('Invalid URL: too few arguments')
if (split.length >= (split[0] === '#' ? 4 : 5)) throw Error('Invalid URL: too many arguments')
return new File({
downloadId: split[1],
key: split[2],
directory: split[0] === '#F',
loadedFile: split[3],
...extraOpt
})
}
}
static unpackAttributes (at) {
// read until the first null byte
let end = 0
while (end < at.length && at.readUInt8(end)) end++
at = at.slice(0, end).toString()
if (at.substr(0, 6) !== 'MEGA{"') return
try {
return JSON.parse(at.substr(4))
} catch (e) {}
}
static defaultHandleRetries (tries, error, cb) {
if (tries > 8) {
cb(error)
} else {
setTimeout(cb, 1000 * Math.pow(2, tries))
}
}
}
const LABEL_NAMES = ['', 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'grey']
export default File
export {
LABEL_NAMES
}