Index: Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js |
diff --git a/Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js b/Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bf1c78c0ded203cd6343de03bd4ed8c3084666df |
--- /dev/null |
+++ b/Source/modules/serviceworkers/polyfills/cacheStoragePolyfill.js |
@@ -0,0 +1,94 @@ |
+// 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 the CacheStorage API, intended to facilitate |
+// end to end serviceworker testing. |
+ |
+// See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache-storage |
+ |
+(function(global) { |
+ var CacheStorage = function() { |
+ this.cachesByName = {}; |
+ }; |
+ |
+ CacheStorage.prototype.get = function(key) { |
+ if (this.cachesByName.hasOwnProperty(key)) { |
+ return Promise.resolve(this.cachesByName[key]); |
jsbell
2014/06/10 18:36:19
Overall: this polyfill is doing all of the changes
gavinp
2014/06/10 20:03:08
Aha, yes, good point. This way, when the user chai
|
+ } |
+ return Promise.reject('not found'); |
+ } |
+ |
+ CacheStorage.prototype.has = function(key) { |
+ if (this.cachesByName.hasOwnProperty(key)) { |
+ return Promise.resolve(true); |
+ } |
+ return Promise.reject(false); |
jsbell
2014/06/10 18:36:19
Should resolve(false);
reject() should only be us
gavinp
2014/06/10 20:03:08
Done.
|
+ } |
+ |
+ // FIXME: Engage standardisation on removing this method from the spec. |
+ CacheStorage.prototype.set = Promise.reject.bind(Promise, 'CacheStorage.prototype.set() not implemented.'); |
+ |
+ // FIXME: Engage standarisation on adding this method to the spec. |
jsbell
2014/06/10 18:36:19
typo: standar*d*isation
(Also, insert 's' vs. 'z'
gavinp
2014/06/10 20:03:08
Done. While I'm religious about US english in iden
|
+ CacheStorage.prototype.create = function(key) { |
+ this.cachesByName[key] = new Cache(); |
+ |
+ return Promise.resolve(); |
+ } |
+ |
+ // FIXME: Engage standarisation on adding this method to the spec. |
+ CacheStorage.prototype.rename = function(fromKey, toKey) { |
+ if (!this.cachesByName.hasOwnProperty(fromKey)) { |
+ return Promise.reject('not found'); |
+ } |
+ this.cachesByName[toKey] = this.cachesByName[fromKey]; |
jsbell
2014/06/10 18:36:19
I assume it's intentional that no checking is done
gavinp
2014/06/10 20:03:08
Yes. Akin to doing "set" followed by "delete."
|
+ delete this.cachesByName[fromKey]; |
+ |
+ return Promise.resolve(); |
+ } |
+ |
+ CacheStorage.prototype.clear = function() { |
+ this.cachesByName = {}; |
+ |
+ return Promise.resolve(); |
+ } |
+ |
+ CacheStorage.prototype.delete = function(key) { |
+ delete this.cachesByName[key]; |
+ |
+ return Promise.resolve(); |
+ } |
+ |
+ CacheStorage.prototype.forEach = function(callback, thisArg) { |
+ Object.keys(this.cachesByName).map(function(key) { |
+ thisArg.callback(this.cachesByName[key], key, this); |
+ }); |
+ return Promise.resolve(); |
+ } |
+ |
+ // FIXME: Implement this. |
+ CacheStorage.prototype.entries = Promise.reject.bind(Promise, 'CacheStorage.prototype.entries() not implemented.'); |
+ |
+ CacheStorage.prototype.keys = function() { |
+ return Promise.resolve(this.cachesByName.keys()); |
+ } |
+ |
+ CacheStorage.prototype.values = function() { |
+ return Promise.resolve(Object.keys(this.cachesByName).map(function (key) { |
falken
2014/06/10 18:36:12
function space here
gavinp
2014/06/10 20:03:09
Done.
|
+ return this.cachesByName[key]; |
+ })); |
+ } |
+ |
+ CacheStorage.prototype.size = function() { |
+ return Promise.resolve(Object.keys(this.cachesByName).length); |
+ } |
+ |
+ CacheStorage.prototype.match = function(url, cacheName) { |
+ this.get(cacheName).then(function (cache) { |
falken
2014/06/10 18:36:12
here too
gavinp
2014/06/10 20:03:08
Done.
|
+ return cache.match(url); |
+ }); |
+ } |
+ |
+ global.CacheStorage = global.CacheStorage || CacheStorage; |
jsbell
2014/06/10 18:36:19
Intentional to expose constructor for this, in the
gavinp
2014/06/10 20:03:08
Fair point, no real need. Done.
|
+ global.caches = global.caches || new CacheStorage(); |
+}(self)); // window or worker global scope. |