-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgadget_jio_serviceworker_storage.js
247 lines (224 loc) · 6.82 KB
/
gadget_jio_serviceworker_storage.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* JIO Service Worker Storage Type = "serviceworker".
* Servieworker "filesystem" storage.
*/
/*global Blob, jIO, RSVP*/
/*jslint nomen: true*/
(function (jIO, RSVP, Blob) {
"use strict";
// no need to validate attachment name, because serviceworker.js will throw
function restrictDocumentId(id) {
if (id.indexOf("/") > -1) {
throw new jIO.util.jIOError("id should be a project name, not a path)",
400);
}
return id;
}
function validateConnection() {
if ('serviceWorker' in navigator) {
if (navigator.serviceWorker.controller === null) {
return new RSVP.Promise(function(resolve, reject) {
navigator.serviceWorker.register('serviceworker.js', {scope: './'})
.then(function () {
if (navigator.serviceWorker.controller) {
resolve();
} else {
reject(new Error("Please refresh to initialize serviceworker"));
}
}).catch(function (err) {
reject(err);
});
});
}
} else {
throw new jIO.util.jIOError("Serviceworker not available in browser",
503);
}
}
// This wraps the message posting/response in a promise, which will resolve if
// the response doesn't contain an error, and reject with the error if it does.
// Alternatively, onmessage handle and controller.postMessage() could be used
function sendMessage(message) {
return new RSVP.Promise(function (resolve, reject, notify) {
var messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function (event) {
if (event.data.error) {
reject(event.data.error);
} else {
resolve(event.data.data);
}
};
// This sends the message data as well as transferring
// messageChannel.port2 to the service worker. The service worker can then
// use the transferred port to reply via postMessage(), which will in turn
// trigger the onmessage handler on messageChannel.port1.
// See https://html.spec.whatwg.org/multipage/workers.html
return navigator.serviceWorker.controller
.postMessage(message, [messageChannel.port2]);
});
}
/**
* The JIO Serviceworker Storage extension
*
* @class ServiceWorkerStorage
* @constructor
*/
function ServiceWorkerStorage () {}
ServiceWorkerStorage.prototype.post = function () {
throw new jIO.util.jIOError("Storage requires 'put' to create new cache",
400);
};
ServiceWorkerStorage.prototype.get = function () {
throw new jIO.util.jIOError("Storage does not support 'get' method",
400);
};
ServiceWorkerStorage.prototype.put = function (id) {
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'get',
id: restrictDocumentId(id)
});
})
.push(undefined, function (error) {
if (error.status === 404) {
return new RSVP.Queue()
.push(function () {
return sendMessage({
command: 'put',
id: id
});
});
}
throw error;
});
};
ServiceWorkerStorage.prototype.remove = function (id) {
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'allAttachments',
id: restrictDocumentId(id)
});
})
.push(function (attachment_dict) {
var url_list = [],
url;
for (url in attachment_dict) {
if (attachment_dict.hasOwnProperty(url)) {
url_list.append(sendMessage({
command: 'removeAttachment',
id: url
}));
}
}
return RSVP.all(url_list);
})
.push(function () {
return sendMessage({
command: 'remove',
id: restrictDocumentId(id)
});
});
};
ServiceWorkerStorage.prototype.removeAttachment = function (id, url) {
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'removeAttachment',
id: restrictDocumentId(id),
name: url
});
});
};
ServiceWorkerStorage.prototype.getAttachment = function (id, url) {
// NOTE: alternatively get could also be run "official" way via
// an ajax request, which the serviceworker would catch via fetch listener!
// for a filesystem equivalent however, we don't assume fetching resources
// from the network, so all methods will go through sendMessage
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'getAttachment',
id: restrictDocumentId(id),
name: url
});
})
.push(function (my_blob_response) {
return my_blob_response;
});
};
ServiceWorkerStorage.prototype.putAttachment = function (id, name, param) {
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'putAttachment',
id: id,
name: name,
content: param
});
});
};
ServiceWorkerStorage.prototype.allAttachments = function (id) {
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'allAttachments',
id: restrictDocumentId(id)
});
});
};
ServiceWorkerStorage.prototype.hasCapacity = function (name) {
return (name === "list");
};
// returns a list of all caches ~ folders
ServiceWorkerStorage.prototype.allDocs = function (options) {
var context = this;
if (options === undefined) {
options = {};
}
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
if (context.hasCapacity("list")) {
return context.buildQuery(options);
}
})
.push(function (result) {
return result;
});
};
ServiceWorkerStorage.prototype.buildQuery = function (options) {
return new RSVP.Queue()
.push(function () {
return validateConnection();
})
.push(function () {
return sendMessage({
command: 'allDocs',
options: options
});
});
};
jIO.addStorage('serviceworker', ServiceWorkerStorage);
}(jIO, RSVP, Blob));