Commit f9435ae1 authored by Boris Kocherov's avatar Boris Kocherov

erp5_officejs: using MappingStorge thx @vincentB

parent c7647853
...@@ -20,11 +20,8 @@ ...@@ -20,11 +20,8 @@
return { return {
type: "query", type: "query",
sub_storage: { sub_storage: {
type: "uuid", type: "indexeddb",
sub_storage: { database: 'officejs_' + name + '_cache_erp5'
type: "indexeddb",
database: 'officejs_' + name + '_cache_erp5'
}
} }
}; };
} }
...@@ -146,7 +143,7 @@ ...@@ -146,7 +143,7 @@
return jIO.createJIO({ return jIO.createJIO({
type: "replicate", type: "replicate",
query: { query: {
query: '(portal_type: ("Web Style", "Web Page", "Web Script")) AND ' + query: 'reference: "%" AND (portal_type: ("Web Style", "Web Page", "Web Script")) AND ' +
erp5_query + modification_date, erp5_query + modification_date,
limit: [0, 1234567890] limit: [0, 1234567890]
}, },
...@@ -158,26 +155,22 @@ ...@@ -158,26 +155,22 @@
check_remote_modification: true, check_remote_modification: true,
check_remote_creation: true, check_remote_creation: true,
check_remote_deletion: true, check_remote_deletion: true,
local_sub_storage: { //use_bulk_get: true,
type: "attachasproperty", use_bulk: false,
map: { local_sub_storage: get_jio_cache_storage(name),
text_content: {
body_name: "text_content",
content_type_name: "content_type"
},
data: {
body_name: "data",
content_type_name: "content_type"
}
},
sub_storage: get_jio_cache_storage(name)
},
remote_sub_storage: { remote_sub_storage: {
type: "erp5", type: "mapping",
url: (new URI("hateoasnoauth")) sub_storage: {
.absoluteTo(location.href) type: "erp5",
.toString(), url: (new URI("hateoasnoauth"))
default_view_reference: "jio_view" .absoluteTo(location.href)
.toString(),
default_view_reference: "jio_view"
},
mapping_dict: {"id": {
"equal": "url_string",
"default_property": "reference"
}}
} }
}); });
} }
......
...@@ -238,7 +238,7 @@ ...@@ -238,7 +238,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>955.8487.48731.31863</string> </value> <value> <string>955.20973.48171.43827</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -256,7 +256,7 @@ ...@@ -256,7 +256,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1478563709.48</float> <float>1480578496.19</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*global window, rJS, jIO, URI, location, console, document, RSVP, loopEventListener, navigator, XMLHttpRequest, ProgressEvent */ /*global window, rJS, jIO, URI, location, console, document, RSVP, loopEventListener, navigator, XMLHttpRequest, ProgressEvent*/
/*jslint nomen: true, indent: 2, maxerr: 15*/ /*jslint nomen: true, indent: 2*/
(function (window, rJS) { (function (window, rJS) {
"use strict"; "use strict";
...@@ -13,11 +13,8 @@ ...@@ -13,11 +13,8 @@
return { return {
type: "query", type: "query",
sub_storage: { sub_storage: {
type: "uuid", type: "indexeddb",
sub_storage: { database: 'officejs_' + name + '_cache_erp5'
type: "indexeddb",
database: 'officejs_' + name + '_cache_erp5'
}
} }
}; };
} }
...@@ -139,7 +136,7 @@ ...@@ -139,7 +136,7 @@
return jIO.createJIO({ return jIO.createJIO({
type: "replicate", type: "replicate",
query: { query: {
query: '(portal_type: ("Web Style", "Web Page", "Web Script")) AND ' + query: 'reference: "%" AND (portal_type: ("Web Style", "Web Page", "Web Script")) AND ' +
erp5_query + modification_date, erp5_query + modification_date,
limit: [0, 1234567890] limit: [0, 1234567890]
}, },
...@@ -151,26 +148,22 @@ ...@@ -151,26 +148,22 @@
check_remote_modification: true, check_remote_modification: true,
check_remote_creation: true, check_remote_creation: true,
check_remote_deletion: true, check_remote_deletion: true,
local_sub_storage: { //use_bulk_get: true,
type: "attachasproperty", use_bulk: false,
map: { local_sub_storage: get_jio_cache_storage(name),
text_content: {
body_name: "text_content",
content_type_name: "content_type"
},
data: {
body_name: "data",
content_type_name: "content_type"
}
},
sub_storage: get_jio_cache_storage(name)
},
remote_sub_storage: { remote_sub_storage: {
type: "erp5", type: "mapping",
url: (new URI("hateoasnoauth")) sub_storage: {
.absoluteTo(location.href) type: "erp5",
.toString(), url: (new URI("hateoasnoauth"))
default_view_reference: "jio_view" .absoluteTo(location.href)
.toString(),
default_view_reference: "jio_view"
},
mapping_dict: {"id": {
"equal": "url_string",
"default_property": "reference"
}}
} }
}); });
} }
...@@ -416,4 +409,4 @@ ...@@ -416,4 +409,4 @@
}); });
}); });
}(window, rJS)); }(window, rJS));
\ No newline at end of file
...@@ -224,7 +224,7 @@ ...@@ -224,7 +224,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>955.22065.61366.57770</string> </value> <value> <string>955.40145.23133.18397</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -242,7 +242,7 @@ ...@@ -242,7 +242,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1479378360.51</float> <float>1480578663.21</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
/*jslint indent: 2*/ /*jslint indent: 2*/
/*global self, caches, importScripts, fetch, Promise, Request, Response, jIO, console, Headers, URI, location*/ /*global self, caches, importScripts, fetch,
Promise, Request, Response, jIO, console, Headers, URI, location, RSVP*/
var global = self, var global = self,
window = self; window = self;
(function (self, fetch) { (function (self, fetch) {
...@@ -52,25 +53,10 @@ var global = self, ...@@ -52,25 +53,10 @@ var global = self,
// 2. readonly cache for end user // 2. readonly cache for end user
self.jio_erp5_cache_storage = { self.jio_erp5_cache_storage = {
type: "memoryindex", type: "query",
select_list: ['reference', 'url_string'],
index: {
url_string: null,
reference: function (obj) {
if (!obj.url_string) {
return obj.reference;
}
}
},
sub_storage: { sub_storage: {
type: "query", type: "indexeddb",
sub_storage: { database: self.jio_cache.name + '_erp5'
type: "uuid",
sub_storage: {
type: "indexeddb",
database: self.jio_cache.name + '_erp5'
}
}
} }
}; };
// sync in service worker not work. // sync in service worker not work.
...@@ -80,8 +66,7 @@ var global = self, ...@@ -80,8 +66,7 @@ var global = self,
type: "replicate", type: "replicate",
// XXX This drop the signature lists... // XXX This drop the signature lists...
query: { query: {
query: '(portal_type: ("Web Style", "Web Page", "Web Script")) AND ' + query: self.jio_cache.erp5_query,
self.jio_cache.erp5_query,
limit: [0, 1234567890] limit: [0, 1234567890]
}, },
use_remote_post: true, use_remote_post: true,
...@@ -92,20 +77,25 @@ var global = self, ...@@ -92,20 +77,25 @@ var global = self,
check_remote_modification: true, check_remote_modification: true,
check_remote_creation: true, check_remote_creation: true,
check_remote_deletion: true, check_remote_deletion: true,
local_sub_storage: { use_bulk: false,
type: "attachasproperty", local_sub_storage: self.jio_erp5_cache_storage,
map: {
text_content: "text_content",
data: "data"
},
sub_storage: self.jio_erp5_cache_storage
},
remote_sub_storage: { remote_sub_storage: {
type: "erp5", type: "mapping",
url: (new URI("hateoas")) sub_storage: {
.absoluteTo(location.href) type: "erp5",
.toString(), url: (new URI("hateoasnoauth"))
default_view_reference: "jio_view" .absoluteTo(location.href)
.toString(),
default_view_reference: "jio_view"
},
mapping_dict: {"id": {
"equal": "url_string",
"default_property": "reference"
}},
query: {
"query":
'reference: "%" AND (portal_type: ("Web Style", "Web Page", "Web Script"))'
}
} }
}); });
} else { } else {
...@@ -155,7 +145,7 @@ var global = self, ...@@ -155,7 +145,7 @@ var global = self,
}, },
is_excluded_url = function (url) { is_excluded_url = function (url) {
var prefix, i; var prefix, i;
for (i = 0; i < exclude_urls.length; i++) { for (i = 0; i < exclude_urls.length; i += 1) {
prefix = exclude_urls[i]; prefix = exclude_urls[i];
if (url === prefix) { if (url === prefix) {
return true; return true;
...@@ -166,27 +156,12 @@ var global = self, ...@@ -166,27 +156,12 @@ var global = self,
} }
return false; return false;
}, },
get_mapped_url = function (url) {
var prefix,
prefix_id,
key;
for (key in map_url2url) {
if (map_url2url.hasOwnProperty(key)) {
if (url === key) {
return map_url2url[key];
}
if (url.startsWith(key)) {
return url.replace(key, map_url2url[key]);
}
}
}
},
get_relative_url = function (url) { get_relative_url = function (url) {
var prefix, var prefix,
relative_url, relative_url,
i, i,
prefix_id; prefix_id;
for (i = 0; i < websections_url.length; i++) { for (i = 0; i < websections_url.length; i += 1) {
prefix = websections_url[i]; prefix = websections_url[i];
if (url.startsWith(prefix)) { if (url.startsWith(prefix)) {
prefix_id = map_url2id_prefix[prefix]; prefix_id = map_url2id_prefix[prefix];
...@@ -196,20 +171,19 @@ var global = self, ...@@ -196,20 +171,19 @@ var global = self,
relative_url = url.replace(prefix, prefix_id); relative_url = url.replace(prefix, prefix_id);
if (relative_url) { if (relative_url) {
return relative_url; return relative_url;
} else {
return url;
} }
return url;
} }
} }
return url; return url;
}, },
get_from_cache_storage = function (url, storage) { get_from_cache_storage = function (url, storage) {
var jio_key = get_relative_url(url); var jio_key = url;
if (!storage) { if (!storage) {
storage = self.jio_cache_storage; storage = self.jio_cache_storage;
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
storage.getAttachment(jio_key, 'body') storage.getAttachment(jio_key, "body")
.push(function (body) { .push(function (body) {
resolve(new Response(body, { resolve(new Response(body, {
'headers': { 'headers': {
...@@ -222,69 +196,23 @@ var global = self, ...@@ -222,69 +196,23 @@ var global = self,
}); });
}); });
}, },
get_specific_url = function (url) {
var prefix,
prefix_id,
i;
for (i = 0; i < websections_url.length; i++) {
prefix = websections_url[i];
if (url.startsWith(prefix)) {
prefix_id = map_url2id_prefix[prefix];
if (prefix_id !== "") {
return url.replace(prefix, prefix_id + '/');
}
break;
}
}
},
find_and_get = function (query, storage) {
if (!storage) {
storage = self.jio_dev_storage;
}
query.limit = [0, 1];
query.select_list = ['portal_type'];
return storage.allDocs(query)
.push(function (result) {
if (result.data.total_rows == 1) {
return {
id: result.data.rows[0].id,
portal_type: result.data.rows[0].value.portal_type
};
} else {
throw {status_code: 404};
}
});
},
get_from_storage = function (url, storage) { get_from_storage = function (url, storage) {
var url_string = get_specific_url(url),
url_object = new URI(url),
key,
reference = url_object.filename();
if (!storage) { if (!storage) {
storage = self.jio_dev_storage; storage = self.jio_dev_storage;
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var find_queue; if (!url) {
if (url_string !== undefined) {
key = storage.__storage._find_key('url_string', url_string);
//if (!self.jio_cache.development_mode && !key) {
// key = storage.__storage._find_key('reference', reference);
//}
} else if (reference === get_relative_url(url)) {
key = storage.__storage._find_key('reference', reference);
}
if (!key) {
reject({status_code: 404}); reject({status_code: 404});
return; return;
} }
find_queue = storage.getAttachment(key, 'text_content') storage.get(url)
.push(function (body) { .push(function (body) {
resolve(new Response(body, { resolve(new Response(body.text_content, {
'headers': { 'headers': {
'content-type': body.type 'content-type': body.content_type
} }
})); }));
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
reject(error); reject(error);
}); });
...@@ -357,7 +285,8 @@ var global = self, ...@@ -357,7 +285,8 @@ var global = self,
metadata.portal_type = portal_type; metadata.portal_type = portal_type;
}*/ }*/
if (!portal_type) { if (!portal_type) {
console.log('content_type ' + content_type + ' not supported: ' + url); console.log('content_type ' + content_type +
' not supported: ' + url);
return resolve(); return resolve();
} }
self.jio_dev_storage.put(jio_key, metadata) self.jio_dev_storage.put(jio_key, metadata)
...@@ -378,9 +307,8 @@ var global = self, ...@@ -378,9 +307,8 @@ var global = self,
}); });
}); });
}); });
} else {
return Promise.resolve();
} }
return Promise.resolve();
}; };
(function () { (function () {
...@@ -403,8 +331,8 @@ var global = self, ...@@ -403,8 +331,8 @@ var global = self,
}()); }());
// TODO: generate from special websections current website // TODO: generate from special websections current website
map_url2id_prefix[site_url + 'rjsunsafe/ooffice_fonts/'] = 'ooffice_fonts'; //map_url2id_prefix[site_url + 'rjsunsafe/ooffice_fonts/'] = 'ooffice_fonts';
map_url2id_prefix[site_url + 'rjsunsafe/ooffice/'] = 'ooffice'; //map_url2id_prefix[site_url + 'rjsunsafe/ooffice/'] = 'ooffice';
map_url2id_prefix[site_url + 'rjsunsafe/'] = ''; map_url2id_prefix[site_url + 'rjsunsafe/'] = '';
map_url2id_prefix[site_url] = ''; map_url2id_prefix[site_url] = '';
map_url2id_prefix['https:'] = ''; map_url2id_prefix['https:'] = '';
...@@ -412,9 +340,12 @@ var global = self, ...@@ -412,9 +340,12 @@ var global = self,
exclude_urls.push(site_url + 'hateoas'); exclude_urls.push(site_url + 'hateoas');
exclude_urls.push(site_url + 'hateoasnoauth'); exclude_urls.push(site_url + 'hateoasnoauth');
map_url2url[site_url + 'rjsunsafe/ooffice/apps/'] = 'https://localhost/OfficeWebDeploy/apps/'; map_url2url[site_url + 'rjsunsafe/ooffice/apps/'] =
map_url2url[site_url + 'rjsunsafe/ooffice/sdkjs/'] = 'https://localhost/OfficeWebDeploy/sdkjs/'; 'https://localhost/OfficeWebDeploy/apps/';
map_url2url[site_url + 'rjsunsafe/ooffice/vendor/'] = 'https://localhost/OfficeWebDeploy/vendor/'; map_url2url[site_url + 'rjsunsafe/ooffice/sdkjs/'] =
'https://localhost/OfficeWebDeploy/sdkjs/';
map_url2url[site_url + 'rjsunsafe/ooffice/vendor/'] =
'https://localhost/OfficeWebDeploy/vendor/';
(function () { (function () {
var url; var url;
...@@ -471,15 +402,18 @@ var global = self, ...@@ -471,15 +402,18 @@ var global = self,
var modification_mark_exist = false; var modification_mark_exist = false;
if (metadata.headers.ETag !== undefined) { if (metadata.headers.ETag !== undefined) {
modification_mark_exist = true; modification_mark_exist = true;
request.headers.append('If-None-Match', metadata.headers.Etag); request.headers.append('If-None-Match',
metadata.headers.Etag);
} else if (metadata.headers['last-modified'] !== undefined) { } else if (metadata.headers['last-modified'] !== undefined) {
modification_mark_exist = true; modification_mark_exist = true;
request.headers.append('If-Modified-Since', metadata.headers['last-modified']); request.headers.append('If-Modified-Since',
metadata.headers['last-modified']);
} }
if (modification_mark_exist) { if (modification_mark_exist) {
return fetch(request) return fetch(request)
.then(undefined, function () { .then(undefined, function () {
// We say 'file not changed' if downloading is not possible with additional header // We say 'file not changed' if downloading is not
// possible with additional header
return new Response(null, {status: 304}); return new Response(null, {status: 304});
}); });
} }
...@@ -493,39 +427,38 @@ var global = self, ...@@ -493,39 +427,38 @@ var global = self,
result_jio_save = result_jio_save.then(resolve); result_jio_save = result_jio_save.then(resolve);
} }
return Promise.resolve(); return Promise.resolve();
} else { }
return response.metadata_w_blob() return response.metadata_w_blob()
.then(function (response) { .then(function (response) {
// files save one by one // files save one by one
result_jio_save = result_jio_save.then(function () { result_jio_save = result_jio_save.then(function () {
return new Promise(function (resolve) { return new Promise(function (resolve) {
self.jio_cache_storage.put(jio_key, response.metadata) self.jio_cache_storage.put(jio_key, response.metadata)
.push(function () { .push(function () {
return self.jio_cache_storage.putAttachment( return self.jio_cache_storage.putAttachment(
jio_key, jio_key,
"body", "body",
response.blob response.blob
) )
.push(function () { .push(function () {
console.log('jio_save: ' + jio_key); console.log('jio_save: ' + jio_key);
resolve(); resolve();
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
console.log(error); console.log(error);
reject(); reject();
}); });
});
}); });
});
if (i === requests_len) { if (i === requests_len) {
// latest file saved // latest file saved
result_jio_save = result_jio_save.then(resolve); result_jio_save = result_jio_save.then(resolve);
} }
return Promise.resolve(); return Promise.resolve();
}); });
}
}); });
}); });
}) })
...@@ -541,106 +474,66 @@ var global = self, ...@@ -541,106 +474,66 @@ var global = self,
self.jio_cache_fetch = function (event) { self.jio_cache_fetch = function (event) {
var url = event.request.url, var url = event.request.url,
mapped_url, url_string,
relative_url = get_relative_url(url), not_found_in_dev_storage = false;
specific_url = get_specific_url(url) || relative_url,
not_found_in_dev_storage = false,
queue;
if (is_excluded_url(url)) { if (is_excluded_url(url) || url === site_url) {
queue = fetch(event.request); event.respondWith(fetch(event.request));
} else { } else {
mapped_url = get_mapped_url(url); url_string = get_relative_url(url);
if (self.jio_cache.development_mode && mapped_url) { event.respondWith(new RSVP.Queue()
queue = fetch(mapped_url) .push(function () {
.then(undefined, function (error) { if (self.jio_cache.development_mode) {
if (error.status_code === 404) { // 1 level storage development
console.log(url + ',' + specific_url + ' not found by ' + mapped_url + ' storage'); return get_from_storage(url_string, self.jio_dev_storage);
return get_from_storage(url, self.jio_erp5_cache_storage); }
} else { throw {status_code: 404};
throw error; })
} .push(undefined, function (error) {
}) if (error.status_code === 404) {
.then(undefined, function (error) {
if (error.status_code === 404) {
console.log(url + ',' + specific_url + ' not found in erp5 cache storage');
return get_from_cache_storage(url);
} else {
throw error;
}
})
.then(function (response) {
if (response.ok) {
save_in_dev_storage(url, response.clone());
//console.log('returned: ' + url);
return response;
} else {
debugger;
}
})
.then(undefined, function (error) {
console.log(error);
});
} else {
queue = Promise.resolve()
.then(function () {
if (self.jio_cache.development_mode) { if (self.jio_cache.development_mode) {
// 1 level storage development console.log(url + ' not found in dev storage');
return get_from_storage(url, self.jio_dev_storage); not_found_in_dev_storage = true;
} else {
throw {status_code: 404};
} }
}) // 2 level storage from erp5
.then(undefined, function (error) { return get_from_storage(url_string, self.jio_erp5_cache_storage);
if (error.status_code === 404) { }
if (self.jio_cache.development_mode) { throw error;
console.log(url + ',' + specific_url + ' not found in dev storage'); })
not_found_in_dev_storage = true; .push(undefined, function (error) {
} if (error.status_code === 404) {
// 2 level storage from erp5 console.log(url + ' not found in erp5 cache storage');
return get_from_storage(url, self.jio_erp5_cache_storage); // 3 level cache urls one for all aplications
} else { return get_from_cache_storage(url_string);
throw error; }
} throw error;
}) })
.then(undefined, function (error) { .push(undefined, function (error) {
if (error.status_code === 404) { if (error.status_code === 404) {
console.log(url + ',' + specific_url + ' not found in erp5 cache storage'); console.log(url + ' not found in cache storage');
// 3 level cache urls one for all aplications // fetch
return get_from_cache_storage(url); return fetch(event.request);
} else { }
throw error; throw error;
} })
}) .push(function (response) {
.then(undefined, function (error) { if (response.ok) {
if (error.status_code === 404) { if (not_found_in_dev_storage) {
console.log(url + ',' + relative_url + ' not found in cache storage'); save_in_dev_storage(url_string, response.clone());
// fetch
return fetch(event.request);
} else {
throw error;
}
})
.then(function (response) {
if (response.ok) {
if (not_found_in_dev_storage) {
save_in_dev_storage(url, response.clone());
}
//console.log('returned: ' + url);
} }
return response; //console.log('returned: ' + url);
}) }
.then(undefined, function (error) { return response;
console.log(error); })
}); .push(undefined, function (error) {
} console.log(error);
}));
} }
event.respondWith(queue);
}; };
self.jio_cache_activate = function (event) { self.jio_cache_activate = function (event) {
/* Just like with the install event, event.waitUntil blocks activate on a promise. /* Just like with the install event, event.waitUntil blocks activate on a
Activation will fail unless the promise is fulfilled. promise. Activation will fail unless the promise is fulfilled.
*/ */
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim());
}; };
...@@ -649,4 +542,4 @@ var global = self, ...@@ -649,4 +542,4 @@ var global = self,
self.addEventListener('fetch', self.jio_cache_fetch); self.addEventListener('fetch', self.jio_cache_fetch);
self.addEventListener("activate", self.jio_cache_activate); self.addEventListener("activate", self.jio_cache_activate);
}(self, fetch)); }(self, fetch));
\ No newline at end of file
...@@ -243,7 +243,7 @@ ...@@ -243,7 +243,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>955.630.63038.16349</string> </value> <value> <string>955.40147.13377.15940</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -261,7 +261,7 @@ ...@@ -261,7 +261,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1478729480.3</float> <float>1480529399.17</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment