Skip to content

Commit

Permalink
Fix aborting when onDownloadProgress throws an exception (#301)
Browse files Browse the repository at this point in the history
* Add failing test aborting w/ onDownloadProgress

Note: this hangs the test runner after indicating failure

* Fix rejected promise not being passed up

* Make test pass by working around a Chromium bug

Seems like https://bugs.chromium.org/p/chromium/issues/detail?id=817687
was not completely fixed?
  • Loading branch information
zqianem committed Dec 19, 2020
1 parent 16b5012 commit 87c94fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class Ky {

return new globals.Response(
new globals.ReadableStream({
start(controller) {
async start(controller) {
const reader = response.body.getReader();

if (onDownloadProgress) {
Expand All @@ -489,10 +489,10 @@ class Ky {
}

controller.enqueue(value);
read();
await read();
}

read();
await read();
}
})
);
Expand Down
33 changes: 33 additions & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ test('aborting a request', withPage, async (t, page) => {
await server.close();
});

test('aborting a request with onDonwloadProgress', withPage, async (t, page) => {
const server = await createTestServer();

server.get('/', (request, response) => {
response.end('meow');
});

server.get('/test', (request, response) => {
response.writeHead(200, {
'content-length': '4'
});

response.write('me');
setTimeout(() => {
response.end('ow');
}, 1000);
});

await page.goto(server.url);
await page.addScriptTag({path: './umd.js'});

const error = await page.evaluate(url => {
const controller = new AbortController();
const request = window.ky(`${url}/test`, {signal: controller.signal, onDownloadProgress: () => {}}).text();
setTimeout(() => controller.abort(), 500);
return request.catch(error_ => error_.toString());
}, server.url);
// This should be an AbortError like in the 'aborting a request' test, but there is a bug in Chromium
t.is(error, 'TypeError: Failed to fetch');

await server.close();
});

test('throws TimeoutError even though it does not support AbortController', withPage, async (t, page) => {
const server = await createTestServer();

Expand Down

0 comments on commit 87c94fd

Please sign in to comment.