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 Cache API, intended to facilitate |
| 6 // end to end serviceworker testing. |
| 7 |
| 8 // See https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.htm
l#cache |
| 9 |
| 10 // FIXME: Support AbstractResponse/OpaqueResponse correctly. |
| 11 // FIXME: Serialize the cache. |
| 12 // FIXME: Implement CacheStorage API. |
| 13 // FIXME: Bind all function references. |
| 14 (function(global) { |
| 15 var _castToRequest = function(item) { |
| 16 if (typeof item === 'string') { |
| 17 item = new Request({ |
| 18 url: item, |
| 19 }); |
| 20 } |
| 21 return item; |
| 22 }; |
| 23 |
| 24 var Cache = function() { |
| 25 // An object containing a property for each HTTP fetch method. Those |
| 26 // referenced objects contain a property for each URL, which is the |
| 27 // Response. |
| 28 this.entriesByMethod = {}; |
| 29 }; |
| 30 |
| 31 // FIXME: Should this be in the spec? |
| 32 Cache.prototype.keys = function() { |
| 33 var that = this; |
| 34 |
| 35 var flatten = Array.prototype.concat.apply.bind(Array.prototype.concat,
[]); |
| 36 |
| 37 return Promise.resolve(flatten( |
| 38 Object.keys(this.entriesByMethod).map(function(method) { |
| 39 return Object.keys(that.entriesByMethod[method]).map(function(ur
l) { |
| 40 return new Request({method: method, url: url}); |
| 41 }); |
| 42 }))); |
| 43 }; |
| 44 |
| 45 // FIXME: Implement this. |
| 46 // FIXME: Should spec rename each --> forEach? |
| 47 Cache.prototype.forEach = Promise.reject.bind(Promise, 'Cache.prototype.forE
ach() not implemented.'); |
| 48 Cache.prototype.each = Promise.reject.bind(Promise, 'Cache.prototype.each()
not implemented.'); |
| 49 |
| 50 Cache.prototype.put = function(request, response) { |
| 51 var that = this; |
| 52 |
| 53 return new Promise(function(resolve, reject) { |
| 54 request = _castToRequest(request); |
| 55 |
| 56 if (!that.entriesByMethod.hasOwnProperty(request.method)) { |
| 57 that.entriesByMethod[request.method] = {}; |
| 58 } |
| 59 |
| 60 var entriesByUrl = that.entriesByMethod[request.method]; |
| 61 entriesByUrl[request.url] = response; |
| 62 |
| 63 resolve(); |
| 64 }); |
| 65 }; |
| 66 |
| 67 // FIXME: Implement this. |
| 68 Cache.prototype.add = Promise.reject.bind(Promise, 'Cache.prototype.add() no
t implemented.'); |
| 69 |
| 70 // FIXME: Add QueryParams argument. |
| 71 Cache.prototype.delete = function(request) { |
| 72 request = _castToRequest(request); |
| 73 |
| 74 var that = this; |
| 75 return new Promise(function(resolve, reject) { |
| 76 if (that.entriesByMethod.hasOwnProperty(request.method)) { |
| 77 var entriesByUrl = that.entriesByMethod[request.method]; |
| 78 delete entriesByUrl[request.url]; |
| 79 } |
| 80 resolve(); |
| 81 }); |
| 82 }; |
| 83 |
| 84 // FIXME: Add QueryParams argument. |
| 85 Cache.prototype.match = function(request) { |
| 86 var that = this; |
| 87 |
| 88 return new Promise(function(resolve, reject) { |
| 89 request = _castToRequest(request); |
| 90 |
| 91 if (!that.entriesByMethod.hasOwnProperty(request.method)) { |
| 92 reject('not found'); |
| 93 return; |
| 94 } |
| 95 |
| 96 var entriesByUrl = that.entriesByMethod[request.method]; |
| 97 |
| 98 if (!entriesByUrl.hasOwnProperty(request.url)) { |
| 99 reject('not found'); |
| 100 return; |
| 101 } |
| 102 |
| 103 var entry = entriesByUrl[request.url]; |
| 104 resolve(entry); |
| 105 }); |
| 106 }; |
| 107 |
| 108 // FIXME: Implement this. |
| 109 Cache.prototype.matchAll = Promise.reject.bind(Promise, 'Cache.prototype.mat
chAll not implemented.'); |
| 110 |
| 111 global.Cache = global.Cache || Cache; |
| 112 }(self)); // window or worker global scope. |
OLD | NEW |