Skip to content

Commit

Permalink
feat: forward request and response events to miniget stream
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Oct 26, 2020
1 parent 099d1bb commit 82cff9b
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 35 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Emitted when a video request is made, including after any redirects, retries, an

Emitted when a video response has been found and has started downloading, including after any successful reconnects.

#### Forwarded events

Any events emitted from the [request](https://nodejs.org/api/http.html#http_class_http_clientrequest) or [response](https://nodejs.org/api/http.html#http_class_http_serverresponse) objects will be forwarded to the miniget stream.

# Install

Expand Down
147 changes: 123 additions & 24 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
},
"dependencies": {},
"devDependencies": {
"@types/lolex": "^5.1.0",
"@types/mocha": "^7.0.0",
"@types/node": "^13.1.0",
"lolex": "^6.0.0",
"@types/sinon": "^9.0.8",
"longjohn": "^0.2.12",
"mocha": "^7.0.1",
"nock": "^12.0.0",
"nyc": "^15.0.0",
"sinon": "^9.2.0",
"stream-equal": "^1.1.1",
"ts-node": "^8.10.1",
"typescript": "^3.9.3"
Expand Down
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestOptions, IncomingMessage, ClientRequest } from 'http';
import { EventEmitter } from 'events';
import http from 'http';
import https from 'https';
import { parse as urlParse } from 'url';
Expand All @@ -13,6 +14,10 @@ const httpLibs: {
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
const retryStatusCodes = new Set([429, 503]);

// `request`, `response`, `abort` left out, miniget will emit these.
const requestEvents = ['connect', 'continue', 'information', 'socket', 'timeout', 'upgrade'];
const responseEvents = ['aborted', 'close'];

namespace Miniget {
export interface Options extends RequestOptions {
maxRedirects?: number;
Expand Down Expand Up @@ -119,6 +124,12 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
}
};

const forwardEvents = (ee: EventEmitter, events: string[]) => {
for (let event of events) {
ee.on(event, stream.emit.bind(stream, event));
}
};

const doDownload = (): void => {
if (aborted) { return; }
let parsed: RequestOptions, httpLib;
Expand Down Expand Up @@ -185,6 +196,7 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
}
return;
}

let decodedStream = res as unknown as Transform;
const cleanup = (): void => {
res.removeListener('data', ondata);
Expand Down Expand Up @@ -224,8 +236,10 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
activeDecodedStream = decodedStream;
stream.emit('response', res);
res.on('error', onerror);
forwardEvents(res, responseEvents);
});
activeRequest.on('error', onRequestError);
forwardEvents(activeRequest, requestEvents);
stream.emit('request', activeRequest);
};

Expand Down
35 changes: 26 additions & 9 deletions test/request-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import fs from 'fs';
import path from 'path';
import zlib from 'zlib';
import assert from 'assert';
import nock from 'nock';
import lolex from 'lolex';
import streamEqual from 'stream-equal';
import miniget from '../dist';
import { Transform } from 'stream';
import { IncomingMessage, ClientRequest } from 'http';

import miniget from '../dist';

import nock from 'nock';
import sinon from 'sinon';
import streamEqual from 'stream-equal';
import 'longjohn';

nock.disableNetConnect();

describe('Make a request', () => {
afterEach(() => { nock.cleanAll(); });
let clock: lolex.InstalledClock;
beforeEach(() => clock = lolex.install());
let clock: sinon.SinonFakeTimers;
beforeEach(() => clock = sinon.useFakeTimers());
afterEach(() => clock.uninstall());

describe('with `.text()`', () => {
Expand Down Expand Up @@ -759,13 +761,28 @@ describe('Make a request', () => {
const scope = nock('http://hello.net')
.head('/world')
.reply(200, '', { 'content-length': '10' });
const req = miniget('http://hello.net/world', { method: 'HEAD' });
req.on('error', done);
req.on('response', res => {
const stream = miniget('http://hello.net/world', { method: 'HEAD' });
stream.on('error', done);
stream.on('response', res => {
scope.done();
assert.equal(res.headers['content-length'], '10');
done();
});
});
});

it('Events from request and response are forwarded to miniget stream', (done) => {
const scope = nock('https://randomhost.com')
.get('/randompath')
.reply(200, 'hi');
const stream = miniget('https://randomhost.com/randompath');
const socketSpy = sinon.spy();
stream.on('socket', socketSpy);
stream.on('end', () => {
scope.done();
assert.equal(socketSpy.callCount, 1);
done();
});
stream.resume();
});
});

0 comments on commit 82cff9b

Please sign in to comment.