Skip to content

Commit

Permalink
Adding Brotli Support (#598)
Browse files Browse the repository at this point in the history
* adding brotli support
* support old node versions
* better test
  • Loading branch information
Muhammet Öztürk authored and bitinn committed Apr 26, 2019
1 parent 05f5ac1 commit 2a2d438
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -37,3 +37,6 @@ lib
# Ignore package manager lock files
package-lock.json
yarn.lock

# Ignore IDE
.idea
8 changes: 8 additions & 0 deletions src/index.js
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions test/server.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Expand Up @@ -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'
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 2a2d438

Please sign in to comment.