Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1705)

Unified Diff: Source/modules/serviceworkers/polyfills/fetchPolyfill.js

Issue 311343003: ServiceWorker polyfill Fetch implementation. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: fix memory error Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/serviceworkers/polyfills/cachePolyfill.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
+ 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({
+ 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
« no previous file with comments | « Source/modules/serviceworkers/polyfills/cachePolyfill.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698