Chromium Code Reviews| Index: Source/modules/serviceworkers/polyfills/fetchPolyfill.js |
| diff --git a/Source/modules/serviceworkers/polyfills/fetchPolyfill.js b/Source/modules/serviceworkers/polyfills/fetchPolyfill.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..22b3bd41025822a8bd68e43403211df39c9e765b |
| --- /dev/null |
| +++ b/Source/modules/serviceworkers/polyfills/fetchPolyfill.js |
| @@ -0,0 +1,51 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// A simple, incomplete implementation of Fetch, intended to facilitate end |
| +// to end serviceworker testing. |
| + |
| +// See http://fetch.spec.whatwg.org/#fetch-method |
| + |
| +(function (global) { |
| + var _castToRequest = function (item) { |
|
falken
2014/06/06 17:29:03
No space after "function"
|
| + if (typeof item === 'string') { |
| + item = new Request({ |
| + url: item |
| + }); |
| + } |
| + return item; |
| + }; |
| + |
| + // FIXME: Support init argument to fetch. |
| + var fetch = function(request) { |
| + request = _castToRequest(request); |
| + |
| + return new Promise(function(resolve, reject) { |
| + // FIXME: Use extra headers from |request|. |
| + var xhr = new XMLHttpRequest(); |
| + xhr.open(request.method, request.url, true /* async */); |
| + xhr.send(null); |
| + xhr.onreadystatechange = function() { |
| + if (xhr.readyState !== 4) return; |
| + |
| + var response = new Response({ |
|
falken
2014/06/06 17:29:03
The Response ctor changed in https://codereview.ch
horo
2014/06/09 05:25:12
I noticed that passing a blob created by XMLHttpRe
|
| + status: xhr.status, |
| + statusText: xhr.statusText, |
| + // FIXME: Set response.headers. |
| + // FIXME: Set response.method when available. |
| + // FIXME: Set response.url when available. |
| + // FIXME: Set response.body when available. |
| + }); |
| + |
| + if (xhr.status === 200) { |
| + resolve(response); |
| + } else { |
| + reject(response); |
| + } |
| + }; |
| + }); |
| + }; |
| + |
| + global.fetch = global.fetch || fetch; |
| +}(self)); // window or worker global scope |