Skip to content

Commit

Permalink
fix: fix write after close error
Browse files Browse the repository at this point in the history
fixes #695
  • Loading branch information
fent committed Nov 14, 2020
1 parent fc84ca8 commit 64d643c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
18 changes: 8 additions & 10 deletions lib/index.js
Expand Up @@ -43,15 +43,15 @@ const createStream = options => {
const stream = new PassThrough({
highWaterMark: (options && options.highWaterMark) || 1024 * 512,
});
stream.destroy = () => { stream._isDestroyed = true; };
stream._destroy = () => { stream.destroyed = true; };
return stream;
};


const pipeAndSetEvents = (req, stream, end) => {
// Forward events from the request to the stream.
[
'abort', 'request', 'response', 'error', 'redirect', 'retry', 'reconnect',
'abort', 'request', 'response', 'error', 'close', 'redirect', 'retry', 'reconnect',
].forEach(event => {
req.prependListener(event, stream.emit.bind(stream, event));
});
Expand Down Expand Up @@ -88,7 +88,7 @@ const downloadFromInfoCallback = (stream, info, options) => {
return;
}
stream.emit('info', info, format);
if (stream._isDestroyed) { return; }
if (stream.destroyed) { return; }

let contentLength, downloaded = 0;
const ondata = chunk => {
Expand Down Expand Up @@ -146,7 +146,7 @@ const downloadFromInfoCallback = (stream, info, options) => {
req = miniget(format.url, requestOptions);
req.on('data', ondata);
req.on('end', () => {
if (stream._isDestroyed) { return; }
if (stream.destroyed) { return; }
if (end && end !== rangeEnd) {
start = end + 1;
end += dlChunkSize;
Expand All @@ -168,20 +168,18 @@ const downloadFromInfoCallback = (stream, info, options) => {
}
req = miniget(format.url, requestOptions);
req.on('response', res => {
if (stream._isDestroyed) { return; }
if (stream.destroyed) { return; }
contentLength = contentLength || parseInt(res.headers['content-length']);
});
req.on('data', ondata);
pipeAndSetEvents(req, stream, shouldEnd);
}
}

stream.destroy = () => {
stream._isDestroyed = true;
if (req.abort) req.abort();
stream._destroy = () => {
stream.destroyed = true;
req.destroy();
req.end();
req.removeListener('data', ondata);
req.unpipe();
};
};

Expand Down
2 changes: 1 addition & 1 deletion lib/info.js
Expand Up @@ -31,7 +31,7 @@ exports.watchPageCache = new Cache();
* @returns {Promise<Object>}
*/
exports.getBasicInfo = async(id, options) => {
const retryOptions = Object.assign({}, miniget.Defaults, options.requestOptions);
const retryOptions = Object.assign({}, miniget.defaultOptions, options.requestOptions);
let info = await retryFn(getJSONWatchPage, [id, options], retryOptions);
let player_response =
(info.player && info.player.args && info.player.args.player_response) ||
Expand Down
28 changes: 17 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -36,8 +36,8 @@
},
"dependencies": {
"html-entities": "^1.3.1",
"m3u8stream": "^0.8.0",
"miniget": "^2.1.0",
"m3u8stream": "^0.8.1",
"miniget": "^3.0.0",
"sax": "^1.1.3"
},
"devDependencies": {
Expand All @@ -47,7 +47,7 @@
"eslint": "^6.8.0",
"mocha": "^7.0.0",
"muk-require": "^1.2.0",
"nock": "^12.0.0",
"nock": "^13.0.4",
"nyc": "^15.0.0",
"sinon": "^9.0.0",
"stream-equal": "~1.1.0",
Expand Down
14 changes: 6 additions & 8 deletions test/download-test.js
Expand Up @@ -91,7 +91,7 @@ describe('Download video', () => {
const abort = sinon.spy();
stream.on('abort', abort);
stream.on('error', err => {
assert.ok(abort.called);
assert.ok(!abort.called);
assert.strictEqual(err.message, 'socket hang up');
scope.done();
done();
Expand Down Expand Up @@ -119,9 +119,8 @@ describe('Download video', () => {

const abort = sinon.spy();
stream.on('abort', abort);
stream.on('error', err => {
assert.ok(abort.called);
assert.strictEqual(err.message, 'socket hang up');
stream.on('close', () => {
assert.ok(!abort.called);
scope.done();
done();
});
Expand Down Expand Up @@ -173,7 +172,7 @@ describe('Download video', () => {
const abort = sinon.spy();
stream.on('abort', abort);
stream.on('error', err => {
assert.ok(abort.called);
assert.ok(!abort.called);
assert.strictEqual(err.message, 'socket hang up');
scope.done();
done();
Expand Down Expand Up @@ -201,9 +200,8 @@ describe('Download video', () => {

const abort = sinon.spy();
stream.on('abort', abort);
stream.on('error', err => {
assert.ok(abort.called);
assert.strictEqual(err.message, 'socket hang up');
stream.on('close', () => {
assert.ok(!abort.called);
scope.done();
done();
});
Expand Down
6 changes: 3 additions & 3 deletions test/info-test.js
Expand Up @@ -8,9 +8,9 @@ const miniget = require('miniget');
describe('ytdl.getInfo()', () => {
let expectedInfo;
before(() => expectedInfo = require('./files/videos/regular/expected-info.json'));
let minigetDefaults = miniget.Defaults;
before(() => miniget.Defaults = Object.assign({}, minigetDefaults, { maxRetries: 0 }));
after(() => miniget.Defaults = minigetDefaults);
let minigetDefaults = miniget.defaultOptions;
before(() => miniget.defaultOptions = Object.assign({}, minigetDefaults, { maxRetries: 0 }));
after(() => miniget.defaultOptions = minigetDefaults);

describe('From a regular video', () => {
it('Retrieves correct metainfo', async() => {
Expand Down

0 comments on commit 64d643c

Please sign in to comment.