OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // A simple, incomplete implementation of the CacheStorage API, intended to faci
litate |
| 6 // end to end serviceworker testing. |
| 7 |
| 8 // See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.htm
l#cache-storage |
| 9 |
| 10 (function(global) { |
| 11 var CacheStorage = function() { |
| 12 this.cachesByName = {}; |
| 13 }; |
| 14 |
| 15 CacheStorage.prototype.get = function(key) { |
| 16 if (this.cachesByName.hasOwnProperty(key)) { |
| 17 return Promise.resolve(this.cachesByName[key]); |
| 18 } |
| 19 return Promise.reject('not found'); |
| 20 }; |
| 21 |
| 22 CacheStorage.prototype.has = function(key) { |
| 23 return Promise.resolve(this.cachesByName.hasOwnProperty(key)); |
| 24 }; |
| 25 |
| 26 // FIXME: Engage standardization on removing this method from the spec. |
| 27 CacheStorage.prototype.set = Promise.reject.bind(Promise, 'CacheStorage.prot
otype.set() not implemented.'); |
| 28 |
| 29 // FIXME: Engage standardization on adding this method to the spec. |
| 30 CacheStorage.prototype.create = function(key) { |
| 31 this.cachesByName[key] = new Cache(); |
| 32 |
| 33 return Promise.resolve(); |
| 34 }; |
| 35 |
| 36 // FIXME: Engage standarization on adding this method to the spec. |
| 37 CacheStorage.prototype.rename = function(fromKey, toKey) { |
| 38 if (!this.cachesByName.hasOwnProperty(fromKey)) { |
| 39 return Promise.reject('not found'); |
| 40 } |
| 41 this.cachesByName[toKey] = this.cachesByName[fromKey]; |
| 42 delete this.cachesByName[fromKey]; |
| 43 |
| 44 return Promise.resolve(); |
| 45 }; |
| 46 |
| 47 CacheStorage.prototype.clear = function() { |
| 48 this.cachesByName = {}; |
| 49 |
| 50 return Promise.resolve(); |
| 51 }; |
| 52 |
| 53 CacheStorage.prototype.delete = function(key) { |
| 54 delete this.cachesByName[key]; |
| 55 |
| 56 return Promise.resolve(); |
| 57 }; |
| 58 |
| 59 CacheStorage.prototype.forEach = function(callback, thisArg) { |
| 60 Object.keys(this.cachesByName).map(function(key) { |
| 61 thisArg.callback(this.cachesByName[key], key, this); |
| 62 }); |
| 63 return Promise.resolve(); |
| 64 }; |
| 65 |
| 66 // FIXME: Implement this. |
| 67 CacheStorage.prototype.entries = Promise.reject.bind(Promise, 'CacheStorage.
prototype.entries() not implemented.'); |
| 68 |
| 69 CacheStorage.prototype.keys = function() { |
| 70 return Promise.resolve(Object.keys(this.cachesByName)); |
| 71 }; |
| 72 |
| 73 CacheStorage.prototype.values = function() { |
| 74 return Promise.resolve(Object.keys(this.cachesByName).map(function(key)
{ |
| 75 return this.cachesByName[key]; |
| 76 })); |
| 77 }; |
| 78 |
| 79 CacheStorage.prototype.size = function() { |
| 80 return Promise.resolve(Object.keys(this.cachesByName).length); |
| 81 }; |
| 82 |
| 83 // FIXME: Figure out what should be done with undefined or poorly defined |c
acheName| values. |
| 84 CacheStorage.prototype.match = function(url, cacheName) { |
| 85 return this.get(cacheName).then(function(cache) { |
| 86 return cache.match(url); |
| 87 }); |
| 88 }; |
| 89 |
| 90 global.caches = global.caches || new CacheStorage(); |
| 91 }(self)); // window or worker global scope. |
OLD | NEW |