From 4b53ec0d66c5d0317af450f758efdbab2ae63e18 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Sun, 19 Jan 2014 12:01:46 +0000 Subject: [PATCH] Cleanup. --- server.js | 20 +++++--------------- spec/Cache.js | 14 +++++++++++--- spec/CacheList.js | 20 ++++++++++++-------- spec/ServiceWorker.js | 4 ++-- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/server.js b/server.js index 2ade56f..0151c5f 100644 --- a/server.js +++ b/server.js @@ -38,18 +38,11 @@ function startServer(port) { */ function handleRequest(_request, _response, proxy) { - // Ignore requests without the X-For-Service-Worker header - // if (typeof _request.headers['x-for-service-worker'] === 'undefined') { - // return passThroughRequest(_request, _response, proxy); - // } - - // This may go to the network, so delete the ServiceWorker header - // delete _request.headers['x-for-service-worker']; - _response.setHeader('x-meddled-with', true); + _response.setHeader('X-Proxy', 'ServiceWorker-Polyfill'); var request = new Request(_request); var pageUrl; - var isNavigate = request.headers['x-service-worker-request-type'] === 'navigate'; + var isNavigate = (request.headers['x-service-worker-request-type'] === 'navigate'); if (!isNavigate) { // No referer header, not much we can do @@ -74,7 +67,7 @@ function handleRequest(_request, _response, proxy) { // Nothing matched against this URL, so pass-through if (!registration || !registration.activeWorker) { - _response.setHeader('x-worker', 'none'); + _response.setHeader('X-Worker', 'none'); return passThroughRequest(_request, _response, proxy); } @@ -99,11 +92,8 @@ function passThroughRequest(_request, _response, proxy) { } function handleWebSocket(socket) { - // Listen up! socket.on('message', function (message) { - // TODO guard this var data; - try { data = JSON.parse(message); } catch (e) { @@ -111,11 +101,11 @@ function handleWebSocket(socket) { return; } - Promise.resolve().then(function() { + Promise.resolve(data).then(function(data) { if (data.type === 'register') { return workerRegistry.register.apply(workerRegistry, data.data.args); } - + if (data.type === 'postMessage') { return workerRegistry.postMessageWorker.apply(workerRegistry, data.data.args); } diff --git a/spec/Cache.js b/spec/Cache.js index 82498cd..71e3390 100644 --- a/spec/Cache.js +++ b/spec/Cache.js @@ -8,6 +8,10 @@ var URL = require('dom-urls'); module.exports = Cache; +/** + * Static + */ + Cache.promiseFromValue = function (value) { return fetch(value); }; @@ -17,7 +21,7 @@ Cache.valueFromKey = function (key) { }); }; Cache.transformValue = function (response) { - response.headers['X-Service-Worker-Cache-Hit'] = true; + response.headers['X-Service-Worker-Cache'] = 'HIT'; return response; }; @@ -26,10 +30,14 @@ Cache.persistValuePromise = function (key, valuePromise) { return valuePromise; }; -Cache.persistvalue = function (key, value) { +Cache.persistValue = function (key, value) { return value; }; +/** + * Cache + */ + function Cache() { this.items = new AsyncMap(); var args = [].slice.call(arguments); @@ -57,7 +65,7 @@ Cache.prototype.add = function (key, valuePromise) { var cacheableValuePromise = Cache.persistValuePromise(key, valuePromise) .then(Cache.transformValue) - .then(Cache.persistvalue.bind(this, key)); + .then(Cache.persistValue.bind(this, key)); return this.items.set(key, cacheableValuePromise); }; \ No newline at end of file diff --git a/spec/CacheList.js b/spec/CacheList.js index d5326fb..4e1f2bb 100644 --- a/spec/CacheList.js +++ b/spec/CacheList.js @@ -1,13 +1,13 @@ var util = require('util'); var Cache = require('../spec/Cache'); -var Map = require('../spec/Map'); +var AsyncMap = require('../spec/AsyncMap'); var Promise = require('rsvp').Promise; -util.inherits(CacheList, Map); +util.inherits(CacheList, AsyncMap); module.exports = CacheList; function CacheList() { - Map.apply(this, arguments); + AsyncMap.apply(this, arguments); this.origin = 'none://none'; } @@ -15,18 +15,22 @@ CacheList.prototype.set = function (key, cache) { if (!(cache instanceof Cache)) { throw Error('CacheList only accepts Caches.'); } - return Map.prototype.set.call(this, key, cache); + return AsyncMap.prototype.set.call(this, key, cache); }; CacheList.prototype.ready = function () { - return Promise.all(this.items().map(function (cache, cacheName) { - return cache.ready(); + return Promise.all(this.items().map(function (cachePromise, cacheName) { + return cachePromise.then(function (cache) { + return cache.ready(); + }); })); }; CacheList.prototype.match = function (url) { - return Promise.all(this.items().map(function (cache) { - return cache.match(url); + return Promise.all(this.items().map(function (cachePromise) { + return cachePromise.then(function (cache) { + return cache.match(url); + }); })).then(function (matches) { return matches[0]; }); diff --git a/spec/ServiceWorker.js b/spec/ServiceWorker.js index 4afd50c..37468b2 100644 --- a/spec/ServiceWorker.js +++ b/spec/ServiceWorker.js @@ -16,7 +16,7 @@ function ServiceWorker(workerUrl) { // importScripts requires execution context info, so it's handled in _Worker.js // this.importScripts = ... - + // this means we can get the version number via worker.scope.version // I'm not entirely sure why this works or why it doesn't work as // a normal property. vm weirdness. Maybe there's a better way. @@ -38,7 +38,7 @@ ServiceWorker.prototype.addEventListener = function (type, listener) { ServiceWorker.prototype.removeEventListener = function (type, listener) { this._eventListeners[type] || (this._eventListeners[type] = []); - // FIME does this need to be repeated? while (index = blah > -1) + // FIXME: does this need to be repeated? while (index = blah > -1) var index = this._eventListeners[type].indexOf(listener); if (index < 0) return; this._eventListeners[type].splice(index, 1);