Skip to content

Commit 2914285

Browse files
marten-de-vriesnolanlawson
authored andcommitted
(#3885) - Service worker & fetch() fixes
Changes I needed while writing https://gist.github.com/marten-de-vries/3bfaf635f9efdbcf5102
1 parent 5bad545 commit 2914285

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

lib/adapters/http/http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var CHANGES_BATCH_SIZE = 25;
44

55
// according to http://stackoverflow.com/a/417184/680742,
6-
// the de factor URL length limit is 2000 characters.
6+
// the de facto URL length limit is 2000 characters.
77
// but since most of our measurements don't take the full
88
// URL into account, we fudge it a bit.
99
// TODO: we could measure the full URL to enforce exactly 2000 chars
@@ -597,7 +597,7 @@ function HttpPouch(opts, callback) {
597597
}));
598598

599599
// Add the document given by doc (in JSON string format) to the database
600-
// given by host. This does not assume that doc is a new document
600+
// given by host. This does not assume that doc is a new document
601601
// (i.e. does not have a _id or a _rev field.)
602602
api.post = utils.adapterFun('post', function (doc, opts, callback) {
603603
// If no options were given, set the callback to be the second parameter
@@ -1030,7 +1030,7 @@ function HttpPouch(opts, callback) {
10301030
source.close();
10311031
utils.call(opts.complete, err);
10321032
}
1033-
1033+
10341034
};
10351035

10361036
api._useSSE = false;

lib/deps/request-browser.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,21 @@ function wrappedFetch() {
2020
args[i] = arguments[i];
2121
}
2222

23-
wrappedPromise.then = promise.then.bind(promise);
24-
wrappedPromise.catch = promise.catch.bind(promise);
2523
wrappedPromise.promise = promise;
2624

27-
fetch.apply(null, args).then(function(response) {
25+
utils.Promise.resolve().then(function () {
26+
return fetch.apply(null, args);
27+
}).then(function(response) {
2828
wrappedPromise.resolve(response);
29-
}, function(error) {
30-
wrappedPromise.reject(error);
3129
}).catch(function(error) {
32-
wrappedPromise.catch(error);
30+
wrappedPromise.reject(error);
3331
});
3432

3533
return wrappedPromise;
3634
}
3735

3836
function fetchRequest(options, callback) {
39-
var wrappedPromise, timer, fetchResponse;
37+
var wrappedPromise, timer, response;
4038

4139
var headers = new Headers();
4240

@@ -81,28 +79,28 @@ function fetchRequest(options, callback) {
8179
}, options.timeout);
8280
}
8381

84-
wrappedPromise.promise.then(function(response) {
85-
var result;
86-
87-
fetchResponse = response;
82+
wrappedPromise.promise.then(function(fetchResponse) {
83+
response = {
84+
statusCode: fetchResponse.status
85+
};
8886

8987
if (options.timeout > 0) {
9088
clearTimeout(timer);
9189
}
9290

93-
if (response.status >= 200 && response.status < 300) {
94-
return options.binary ? response.blob() : response.text();
91+
if (response.statusCode >= 200 && response.statusCode < 300) {
92+
return options.binary ? fetchResponse.blob() : fetchResponse.text();
9593
}
9694

97-
return result.json();
95+
return fetchResponse.json();
9896
}).then(function(result) {
99-
if (fetchResponse.status >= 200 && fetchResponse.status < 300) {
100-
callback(null, fetchResponse, result);
97+
if (response.statusCode >= 200 && response.statusCode < 300) {
98+
callback(null, response, result);
10199
} else {
102-
callback(result, fetchResponse);
100+
callback(result, response);
103101
}
104102
}).catch(function(error) {
105-
callback(error, fetchResponse);
103+
callback(error, response);
106104
});
107105

108106
return {abort: wrappedPromise.reject};
@@ -214,10 +212,21 @@ function xhRequest(options, callback) {
214212
return {abort: abortReq};
215213
}
216214

215+
function testXhr() {
216+
try {
217+
new XMLHttpRequest();
218+
return true;
219+
} catch (err) {
220+
return false;
221+
}
222+
}
223+
224+
var hasXhr = testXhr();
225+
217226
module.exports = function(options, callback) {
218-
if (typeof XMLHttpRequest === 'undefined' && !options.xhr) {
219-
return fetchRequest(options, callback);
220-
} else {
227+
if (hasXhr || options.xhr) {
221228
return xhRequest(options, callback);
229+
} else {
230+
return fetchRequest(options, callback);
222231
}
223232
};

0 commit comments

Comments
 (0)