Honor the `size` option after following a redirect and revert data uri support
Co-authored-by: Richie Bendall <richiebendall@gmail.com>
This commit is contained in:
parent
8c197f8982
commit
2358a6c256
|
@ -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.
|
||||
|
|
14
src/index.js
14
src/index.js
|
@ -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
|
||||
|
@ -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
|
||||
|
|
25
test/test.js
25
test/test.js
|
@ -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'));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue