Skip to content

Commit

Permalink
Honor the size option after following a redirect and revert data ur…
Browse files Browse the repository at this point in the history
…i support

Co-authored-by: Richie Bendall <richiebendall@gmail.com>
  • Loading branch information
xxczaki and Richienb committed Sep 5, 2020
1 parent 8c197f8 commit 2358a6c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,12 @@ Changelog

# 2.x release

## v2.6.1

**This is an important security release. It is strongly recommended to update as soon as possible.**

- Fix: honor the `size` option after following a redirect.

## v2.6.0

- Enhance: `options.agent`, it now accepts a function that returns custom http(s).Agent instance based on current URL, see readme for more information.
Expand Down
14 changes: 2 additions & 12 deletions src/index.js
Expand Up @@ -38,17 +38,6 @@ export default function fetch(url, opts) {
throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
}

if (/^data:/.test(url)) {
const request = new Request(url, opts);
try {
const data = Buffer.from(url.split(',')[1], 'base64')
const res = new Response(data.body, { headers: { 'Content-Type': data.mimeType || url.match(/^data:(.+);base64,.*$/)[1] } });
return fetch.Promise.resolve(res);
} catch (err) {
return fetch.Promise.reject(new FetchError(`[${request.method}] ${request.url} invalid URL, ${err.message}`, 'system', err));
}
}

Body.Promise = fetch.Promise;

// wrap http.request into fetch
Expand Down Expand Up @@ -164,7 +153,8 @@ export default function fetch(url, opts) {
method: request.method,
body: request.body,
signal: request.signal,
timeout: request.timeout
timeout: request.timeout,
size: request.size
};

// HTTP-redirect fetch step 9
Expand Down
25 changes: 0 additions & 25 deletions test/test.js
Expand Up @@ -2844,29 +2844,4 @@ describe('external encoding', () => {
});
});
});

describe('data uri', function() {
const dataUrl = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';

const invalidDataUrl = 'data:@@@@';

it('should accept data uri', function() {
return fetch(dataUrl).then(r => {
console.assert(r.status == 200);
console.assert(r.headers.get('Content-Type') == 'image/gif');

return r.buffer().then(b => {
console.assert(b instanceof Buffer);
});
});
});

it('should reject invalid data uri', function() {
return fetch(invalidDataUrl)
.catch(e => {
console.assert(e);
console.assert(e.message.includes('invalid URL'));
});
});
});
});

0 comments on commit 2358a6c

Please sign in to comment.