Visitar URL original
Code optimisations to headers calls by simonbuerger · Pull Request #92 · developit/unfetch · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace headers code to only get all header keys
Directly use case insensitive request.getResponseHeader for the header values checks
  • Loading branch information
simonbuerger committed Sep 20, 2018
commit ec14930d6a101371c8b098220924a48dac531409
21 changes: 7 additions & 14 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ export default function(url, options) {
request.send(options.body || null);

function response() {
let keys = [],
all = [],
headers = {},
header;

request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => {
keys.push(key = key.toLowerCase());
all.push([key, value]);
header = headers[key];
headers[key] = header ? `${header},${value}` : value;
let temp = {}, keys = [];
request.getAllResponseHeaders().toLowerCase().replace(/^(.+?):/gm, (m, key) => {
Comment thread
simonbuerger marked this conversation as resolved.
Resolved
!temp[key] && keys.push(temp[key] = key);
});

return {
ok: (request.status/100|0) == 2, // 200-299
ok: request.status/100|0 == 2, // 200-299
Comment thread
simonbuerger marked this conversation as resolved.
Resolved
status: request.status,
statusText: request.statusText,
url: request.responseURL,
Expand All @@ -43,9 +36,9 @@ export default function(url, options) {
blob: () => Promise.resolve(new Blob([request.response])),
headers: {
keys: () => keys,
entries: () => all,
get: n => headers[n.toLowerCase()],
has: n => n.toLowerCase() in headers
entries: () => keys.map(n => [n, request.getResponseHeader(n)]),
get: n => request.getResponseHeader(n),
has: n => request.getResponseHeader(n) != null
}
};
}
Expand Down
10 changes: 6 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('unfetch', () => {
beforeEach(() => {
xhr = {
setRequestHeader: jest.fn(),
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'),
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\r\nX-Foo: baz\r\nX-Bar: bar, baz\r\nx-test: \r\nX-Baz: bar, baz\r\ndate: 18:23:22'),
getResponseHeader: jest.fn().mockReturnValue('bar, baz'),
open: jest.fn(),
send: jest.fn(),
status: 200,
Expand Down Expand Up @@ -46,7 +47,8 @@ describe('unfetch', () => {
expect(r.clone()).not.toBe(r);
expect(r.clone().url).toEqual('/foo?redirect');
expect(r.headers.get).toEqual(expect.any(Function));
expect(r.headers.get('x-foo')).toEqual('bar,baz');
expect(r.headers.get('x-foo')).toEqual('bar, baz');
expect(r.headers.keys()).toEqual(['x-foo', 'x-bar', 'x-test', 'x-baz', 'date']);
return r.json();
})
.then( data => {
Expand All @@ -69,11 +71,11 @@ describe('unfetch', () => {
});

it('handles empty header values', () => {
xhr.getAllResponseHeaders = jest.fn().mockReturnValue('Server: \nX-Foo:baz');
// If we're using getResponseHeader directly this test might be pointless
xhr.getResponseHeader = jest.fn().mockReturnValue('');
let p = fetch('/foo')
.then(r => {
expect(r.headers.get('server')).toEqual('');
expect(r.headers.get('X-foo')).toEqual('baz');
});

xhr.onload();
Expand Down