-
Notifications
You must be signed in to change notification settings - Fork 13
/
swgetheaders-page.js
144 lines (121 loc) · 4.41 KB
/
swgetheaders-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var Logger = function() {
var debug = false;
function log(message) {
if (debug) {
console.info(message);
}
}
function error(message) {
console.error(message);
}
function getDebug() {
return debug;
}
function setDebug(value) {
debug = value;
}
return {
log: log,
error: error,
getDebug: getDebug,
setDebug: setDebug
};
};
module.exports = new Logger();
},{}],"swgetheaders":[function(require,module,exports){
var logger = require('./logger');
(function() {
'use strict';
var onPluggedCallbacks = [];
var onRequestCallbacks = [];
var onResponseCallbacks = [];
var plugged = false;
var corsExceptions = [];
var sameOriginOnly = false;
function registerServiceWorker(swPath, options) {
if (options && options.debug !== undefined) {
logger.setDebug(options.debug);
}
if (options && options.corsExceptions !== undefined) {
corsExceptions = options.corsExceptions;
}
if (options && options.sameOriginOnly !== undefined) {
sameOriginOnly = options.sameOriginOnly;
}
if (!('serviceWorker' in navigator)) {
logger.log('[Page] This browser doesn\'t support service workers');
return;
}
if (navigator.serviceWorker.controller) {
if (navigator.serviceWorker.controller.scriptURL.indexOf(swPath) >= 0) {
logger.log('[Page] The service worker is already active');
openCommunicationWithWorker();
} else {
logger.error('[Page] The page already has another service worker: ' + navigator.serviceWorker.controller.scriptURL);
}
return;
}
logger.log('[Page] The service worker needs to be installed');
navigator.serviceWorker.register(swPath)
.then(navigator.serviceWorker.ready)
.then(function (serviceWorkerRegistration) {
logger.log('[Page] The service worker is registered. It will work after the page changes or is refreshed.');
});
}
function openCommunicationWithWorker() {
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
if (event.data.error) {
logger.error('[Page] Receveived an error message from SW:');
logger.error(event.data.error);
} else {
logger.log('[Page] Received a message from SW:');
logger.log(event.data);
onDataReceivedFromSW(event.data);
}
};
navigator.serviceWorker.controller.postMessage({
type: 'hello',
debug: logger.getDebug(),
corsExceptions: corsExceptions,
sameOriginOnly: sameOriginOnly
}, [messageChannel.port2]);
}
function isPlugged() {
return plugged;
}
function onDataReceivedFromSW(data) {
if (data.type === 'plugged') {
plugged = true;
broadcast(onPluggedCallbacks);
} else if (data.type === 'response') {
broadcast(onResponseCallbacks, data.request, data.response);
}
}
function broadcast(cbList, ...data) {
cbList.forEach(function(cb) {
cb(...data);
});
}
function on(event, callback) {
switch(event) {
case 'plugged':
onPluggedCallbacks.push(callback);
break;
case 'response':
onResponseCallbacks.push(callback);
break;
default:
logger.error('[Page] Unknown event:');
logger.error(event);
break;
}
}
module.exports = {
registerServiceWorker: registerServiceWorker,
on: on,
isPlugged: isPlugged
};
})();
},{"./logger":1}]},{},[]);