From 2839c7a55222d5b74feb2ce423b3cc2be61ee4e8 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Fri, 11 Nov 2022 00:30:48 +0100 Subject: fix: remove dead code --- lib/file.js | 57 +-------------------------------------------------------- package.json | 1 - 2 files changed, 1 insertion(+), 57 deletions(-) diff --git a/lib/file.js b/lib/file.js index e972113..1199fb8 100644 --- a/lib/file.js +++ b/lib/file.js @@ -3,9 +3,7 @@ const { PassThrough } = require('streamx') const { BlobWriteStream } = require('fast-blob-stream') const streamToBuffer = require('stream-with-known-length-to-buffer') const queueMicrotask = require('queue-microtask') -const rangeParser = require('range-parser') const mime = require('mime') -const eos = require('end-of-stream') const FileStream = require('./file-stream.js') class File extends EventEmitter { @@ -122,7 +120,7 @@ class File extends EventEmitter { if (typeof window === 'undefined') throw new Error('browser-only method') const writeStream = new BlobWriteStream(blob => { cb(null, blob) - }, { mimeType: this._getMimeType() }) + }, { mimeType: mime.getType(this.name) || 'application/octet-stream' }) this.createReadStream().pipe(writeStream) } @@ -132,59 +130,6 @@ class File extends EventEmitter { }) } - _serve (req) { - const res = { - status: 200, - headers: { - // Support range-requests - 'Accept-Ranges': 'bytes', - 'Content-Type': mime.getType(this.name), - 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0', - Expires: '0' - }, - body: req.method === 'HEAD' ? '' : 'STREAM' - } - // force the browser to download the file if if it's opened in a new tab - if (req.destination === 'document') { - res.headers['Content-Type'] = 'application/octet-stream' - res.headers['Content-Disposition'] = 'attachment' - res.body = 'DOWNLOAD' - } - - // `rangeParser` returns an array of ranges, or an error code (number) if - // there was an error parsing the range. - let range = rangeParser(this.length, req.headers.range || '') - - if (range.constructor === Array) { - res.status = 206 // indicates that range-request was understood - - // no support for multi-range request, just use the first range - range = range[0] - - res.headers['Content-Range'] = `bytes ${range.start}-${range.end}/${this.length}` - res.headers['Content-Length'] = `${range.end - range.start + 1}` - } else { - res.headers['Content-Length'] = this.length - } - - const stream = req.method === 'GET' && this.createReadStream(range) - - let pipe = null - if (stream) { - this.emit('stream', { stream, req, file: this }, piped => { - pipe = piped - - // piped stream might not close the original filestream on close/error, this is agressive but necessary - eos(piped, () => { - if (piped) piped.destroy() - stream.destroy() - }) - }) - } - - return [res, pipe || stream, pipe && stream] - } - getStreamURL () { if (!this._server) throw new Error('No server created') const url = `${this._server.pathname}/${this._torrent.infoHash}/${encodeURI(this.path)}` diff --git a/package.json b/package.json index f90297d..19aebb4 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "cpus": "^1.0.3", "create-torrent": "^5.0.6", "debug": "^4.3.4", - "end-of-stream": "^1.4.4", "escape-html": "^1.0.3", "fast-blob-stream": "^1.1.1", "fs-chunk-store": "^3.0.1", -- cgit v1.2.3