Adding Brotli Support (#598)
* adding brotli support * support old node versions * better test
This commit is contained in:
parent
05f5ac12a2
commit
2a2d4384af
|
@ -37,3 +37,6 @@ lib
|
|||
# Ignore package manager lock files
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
|
||||
# Ignore IDE
|
||||
.idea
|
||||
|
|
|
@ -244,6 +244,14 @@ export default function fetch(url, opts) {
|
|||
return;
|
||||
}
|
||||
|
||||
// for br
|
||||
if (codings == 'br' && typeof zlib.createBrotliDecompress === 'function') {
|
||||
body = body.pipe(zlib.createBrotliDecompress());
|
||||
response = new Response(body, response_options);
|
||||
resolve(response);
|
||||
return;
|
||||
}
|
||||
|
||||
// otherwise, use response as-is
|
||||
response = new Response(body, response_options);
|
||||
resolve(response);
|
||||
|
|
|
@ -94,6 +94,18 @@ export default class TestServer {
|
|||
});
|
||||
}
|
||||
|
||||
if (p === '/brotli') {
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
if (typeof zlib.createBrotliDecompress === 'function') {
|
||||
res.setHeader('Content-Encoding', 'br');
|
||||
zlib.brotliCompress('hello world', function (err, buffer) {
|
||||
res.end(buffer);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (p === '/deflate-raw') {
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
|
@ -308,6 +320,12 @@ export default class TestServer {
|
|||
res.end();
|
||||
}
|
||||
|
||||
if (p === '/no-content/brotli') {
|
||||
res.statusCode = 204;
|
||||
res.setHeader('Content-Encoding', 'br');
|
||||
res.end();
|
||||
}
|
||||
|
||||
if (p === '/not-modified') {
|
||||
res.statusCode = 304;
|
||||
res.end();
|
||||
|
|
28
test/test.js
28
test/test.js
|
@ -50,6 +50,7 @@ import RequestOrig from '../src/request.js';
|
|||
import ResponseOrig from '../src/response.js';
|
||||
import Body from '../src/body.js';
|
||||
import Blob from '../src/blob.js';
|
||||
import zlib from "zlib";
|
||||
|
||||
const supportToString = ({
|
||||
[Symbol.toStringTag]: 'z'
|
||||
|
@ -664,6 +665,33 @@ describe('node-fetch', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('should decompress brotli response', function() {
|
||||
if(typeof zlib.createBrotliDecompress !== 'function') this.skip();
|
||||
const url = `${base}brotli`;
|
||||
return fetch(url).then(res => {
|
||||
expect(res.headers.get('content-type')).to.equal('text/plain');
|
||||
return res.text().then(result => {
|
||||
expect(result).to.be.a('string');
|
||||
expect(result).to.equal('hello world');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle no content response with brotli encoding', function() {
|
||||
if(typeof zlib.createBrotliDecompress !== 'function') this.skip();
|
||||
const url = `${base}no-content/brotli`;
|
||||
return fetch(url).then(res => {
|
||||
expect(res.status).to.equal(204);
|
||||
expect(res.statusText).to.equal('No Content');
|
||||
expect(res.headers.get('content-encoding')).to.equal('br');
|
||||
expect(res.ok).to.be.true;
|
||||
return res.text().then(result => {
|
||||
expect(result).to.be.a('string');
|
||||
expect(result).to.be.empty;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should skip decompression if unsupported', function() {
|
||||
const url = `${base}sdch`;
|
||||
return fetch(url).then(res => {
|
||||
|
|
Loading…
Reference in New Issue