Skip to content

Commit

Permalink
Merge 6ff2677 into c00fce3
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 20, 2020
2 parents c00fce3 + 6ff2677 commit cb85e38
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 42 deletions.
26 changes: 13 additions & 13 deletions package.json
Expand Up @@ -57,19 +57,19 @@
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@sindresorhus/tsconfig": "^0.7.0",
"@sindresorhus/tsconfig": "^0.8.0",
"@sinonjs/fake-timers": "^6.0.1",
"@types/benchmark": "^1.0.33",
"@types/express": "^4.17.7",
"@types/node": "^14.14.0",
"@types/benchmark": "^2.1.0",
"@types/express": "^4.17.9",
"@types/node": "^14.14.9",
"@types/node-fetch": "^2.5.7",
"@types/pem": "^1.9.5",
"@types/pify": "^3.0.2",
"@types/request": "^2.48.5",
"@types/sinon": "^9.0.5",
"@types/sinon": "^9.0.8",
"@types/tough-cookie": "^4.0.0",
"ava": "^3.11.1",
"axios": "^0.20.0",
"ava": "^3.13.0",
"axios": "^0.21.0",
"benchmark": "^2.1.4",
"coveralls": "^3.1.0",
"create-test-server": "^3.0.1",
Expand All @@ -78,20 +78,20 @@
"express": "^4.17.1",
"form-data": "^3.0.0",
"get-stream": "^6.0.0",
"nock": "^13.0.4",
"node-fetch": "^2.6.0",
"np": "^6.4.0",
"nock": "^13.0.5",
"node-fetch": "^2.6.1",
"np": "^7.0.0",
"nyc": "^15.1.0",
"p-event": "^4.2.0",
"pem": "^1.14.4",
"pify": "^5.0.0",
"sinon": "^9.0.3",
"sinon": "^9.2.1",
"slow-stream": "0.0.4",
"tempy": "^1.0.0",
"to-readable-stream": "^2.1.0",
"tough-cookie": "^4.0.0",
"typescript": "4.0.3",
"xo": "^0.34.1"
"typescript": "4.1.2",
"xo": "^0.35.0"
},
"types": "dist/source",
"sideEffects": false,
Expand Down
12 changes: 7 additions & 5 deletions source/core/utils/proxy-events.ts
Expand Up @@ -4,19 +4,21 @@ type Fn = (...args: unknown[]) => void;
type Fns = Record<string, Fn>;

export default function (from: EventEmitter, to: EventEmitter, events: string[]): () => void {
const fns: Fns = {};
const eventFunctions: Fns = {};

for (const event of events) {
fns[event] = (...args: unknown[]) => {
const eventFunction = (...args: unknown[]) => {
to.emit(event, ...args);
};

from.on(event, fns[event]);
eventFunctions[event] = eventFunction;

from.on(event, eventFunction);
}

return () => {
for (const event of events) {
from.off(event, fns[event]);
for (const [event, eventFunction] of Object.entries(eventFunctions)) {
from.off(event, eventFunction);
}
};
}
3 changes: 2 additions & 1 deletion source/create.ts
Expand Up @@ -128,7 +128,8 @@ const create = (defaults: InstanceDefaults): Got => {
const got: Got = ((url: string | URL, options: Options = {}, _defaults?: Defaults): GotReturn => {
let iteration = 0;
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => {
return defaults.handlers[iteration++](
// TODO: Remove the `!`. This could probably be simplified to not use index access.
return defaults.handlers[iteration++]!(
newOptions,
iteration === defaults.handlers.length ? getPromiseOrStream : iterateHandlers
) as GotReturn;
Expand Down
9 changes: 6 additions & 3 deletions source/index.ts
Expand Up @@ -86,11 +86,14 @@ const defaults: InstanceDefaults = {

let next: string | undefined;
for (const item of items) {
// TODO: Use destructuring here.
const parsed = item.split(';');

if (parsed[1].includes('next')) {
next = parsed[0].trimStart().trim();
next = next.slice(1, -1);
if (parsed[1]?.includes('next')) {
next = parsed[0]!
.trim()
.slice(1, -1);

break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/cancel.ts
Expand Up @@ -15,7 +15,7 @@ import withServer, {withServerAndFakeTimers} from './helpers/with-server';
const prepareServer = (server: ExtendedHttpTestServer, clock: GlobalClock): {emitter: EventEmitter; promise: Promise<unknown>} => {
const emitter = new EventEmitter();

const promise = new Promise((resolve, reject) => {
const promise = new Promise<void>((resolve, reject) => {
server.all('/abort', async (request, response) => {
emitter.emit('connection');

Expand Down Expand Up @@ -149,7 +149,7 @@ test.serial('cancels in-progress request with timeout', withServerAndFakeTimers,
});

test.serial('cancel immediately', withServerAndFakeTimers, async (t, server, got, clock) => {
const promise = new Promise((resolve, reject) => {
const promise = new Promise<void>((resolve, reject) => {
// We won't get an abort or even a connection
// We assume no request within 1000ms equals a (client side) aborted request
server.get('/abort', (_request, response) => {
Expand Down
18 changes: 7 additions & 11 deletions test/cookies.ts
Expand Up @@ -15,7 +15,7 @@ test('reads a cookie', withServer, async (t, server, got) => {

await got({cookieJar});

const cookie = cookieJar.getCookiesSync(server.url)[0];
const cookie = cookieJar.getCookiesSync(server.url)[0]!;
t.is(cookie.key, 'hello');
t.is(cookie.value, 'world');
});
Expand All @@ -30,14 +30,11 @@ test('reads multiple cookies', withServer, async (t, server, got) => {

await got({cookieJar});

const cookies = cookieJar.getCookiesSync(server.url);
const cookieA = cookies[0];
t.is(cookieA.key, 'hello');
t.is(cookieA.value, 'world');

const cookieB = cookies[1];
t.is(cookieB.key, 'foo');
t.is(cookieB.value, 'bar');
const [cookieA, cookieB] = cookieJar.getCookiesSync(server.url);
t.is(cookieA!.key, 'hello');
t.is(cookieA!.value, 'world');
t.is(cookieB!.key, 'foo');
t.is(cookieB!.value, 'bar');
});

test('cookies doesn\'t break on redirects', withServer, async (t, server, got) => {
Expand Down Expand Up @@ -159,8 +156,7 @@ test('accepts custom `cookieJar` object', withServer, async (t, server, got) =>
const cookieJar = {
async getCookieString(url: string) {
t.is(typeof url, 'string');

return cookies[url] || '';
return cookies[url] ?? '';
},

async setCookie(rawCookie: string, url: string) {
Expand Down
1 change: 0 additions & 1 deletion test/error.ts
Expand Up @@ -225,7 +225,6 @@ test('no uncaught parse errors', async t => {
const listen = promisify(server.listen.bind(server));
const close = promisify(server.close.bind(server));

// @ts-expect-error TS is sooo dumb. It doesn't need an argument at all.
await listen();

server.on('connection', socket => {
Expand Down
1 change: 1 addition & 0 deletions test/helpers/with-server.ts
Expand Up @@ -112,6 +112,7 @@ export const withSocketServer: test.Macro<[RunTestWithSocket]> = async (t, run)

server.socketPath = socketPath;

// @ts-expect-error - TS 4.1 bug.
await promisify(server.listen.bind(server))(socketPath);

try {
Expand Down
2 changes: 1 addition & 1 deletion test/http.ts
Expand Up @@ -333,7 +333,7 @@ test.serial('deprecated `family` option', withServer, async (t, server, got) =>
response.end('ok');
});

await new Promise(resolve => {
await new Promise<void>(resolve => {
let request: CancelableRequest;
(async () => {
const warning = await pEvent(process, 'warning');
Expand Down
2 changes: 1 addition & 1 deletion test/https.ts
Expand Up @@ -155,7 +155,7 @@ test.serial('deprecated `rejectUnauthorized` option', withHttpsServer(), async (
response.end('ok');
});

await new Promise(resolve => {
await new Promise<void>(resolve => {
let request: CancelableRequest;
(async () => {
const warning = await pEvent(process, 'warning');
Expand Down
4 changes: 2 additions & 2 deletions test/progress.ts
Expand Up @@ -89,7 +89,7 @@ test('download progress - missing total size', withServer, async (t, server, got

await got('').on('downloadProgress', (event: Progress) => events.push(event));

t.is(events[0].total, undefined);
t.is(events[0]?.total, undefined);
checkEvents(t, events);
});

Expand Down Expand Up @@ -188,7 +188,7 @@ test('upload progress - stream with unknown body size', withServer, async (t, se
stream.pipeline(toReadableStream(file), request, () => {})
);

t.is(events[0].total, undefined);
t.is(events[0]?.total, undefined);
checkEvents(t, events);
});

Expand Down
2 changes: 1 addition & 1 deletion test/stream.ts
Expand Up @@ -419,7 +419,7 @@ test('async iterator works', withServer, async (t, server, got) => {
t.is(Buffer.concat(chunks).toString(), payload);
});

if (process.versions.node.split('.')[0] <= '12') {
if (Number.parseInt(process.versions.node.split('.')[0]!, 10) <= 12) {
test('does not emit end event on error', withServer, async (t, server, got) => {
server.get('/', infiniteHandler);

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -6,7 +6,8 @@
"lib": [
"es2018",
"es2019.string"
]
],
"useDefineForClassFields": false
},
"include": [
"source",
Expand Down

0 comments on commit cb85e38

Please sign in to comment.