Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Ashworth committed Jan 19, 2014
1 parent 1f8746f commit 4b53ec0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
20 changes: 5 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -99,23 +92,20 @@ 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) {
console.error(chalk.red(why.stack));
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);
}
Expand Down
14 changes: 11 additions & 3 deletions spec/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ var URL = require('dom-urls');

module.exports = Cache;

/**
* Static
*/

Cache.promiseFromValue = function (value) {
return fetch(value);
};
Expand All @@ -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;
};

Expand All @@ -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);
Expand Down Expand Up @@ -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);
};
20 changes: 12 additions & 8 deletions spec/CacheList.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
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';
}

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];
});
Expand Down
4 changes: 2 additions & 2 deletions spec/ServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down

0 comments on commit 4b53ec0

Please sign in to comment.