Commit a1e93301 authored by Sven Franck's avatar Sven Franck

libs: update JIO and localStorage to latest to fix bugs

parent ac0a7350
This diff is collapsed.
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, regexp: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, regexp: true */
/*global jIO, localStorage, setTimeout, complex_queries, window, define, /*global jIO, localStorage, setTimeout, window, define, Blob, Uint8Array,
exports, require */ exports, require */
/** /**
...@@ -54,15 +54,14 @@ ...@@ -54,15 +54,14 @@
return define(dependencies, module); return define(dependencies, module);
} }
if (typeof exports === 'object') { if (typeof exports === 'object') {
return module(exports, require('jio'), require('complex_queries')); return module(exports, require('jio'));
} }
window.local_storage = {}; window.local_storage = {};
module(window.local_storage, jIO, complex_queries); module(window.local_storage, jIO);
}([ }([
'exports', 'exports',
'jio', 'jio'
'complex_queries' ], function (exports, jIO) {
], function (exports, jIO, complex_queries) {
"use strict"; "use strict";
/** /**
...@@ -124,9 +123,8 @@ ...@@ -124,9 +123,8 @@
* @constructor * @constructor
*/ */
function LocalStorage(spec) { function LocalStorage(spec) {
if (typeof spec.username !== 'string' && !spec.username) { if (typeof spec.username !== 'string' || spec.username === '') {
throw new TypeError("LocalStorage 'username' must be a string " + throw new TypeError("LocalStorage 'username' must be a non-empty string");
"which contains more than one character.");
} }
this._localpath = 'jio/localstorage/' + spec.username + '/' + ( this._localpath = 'jio/localstorage/' + spec.username + '/' + (
spec.application_name === null || spec.application_name === spec.application_name === null || spec.application_name ===
...@@ -142,6 +140,7 @@ ...@@ -142,6 +140,7 @@
this._database = localStorage; this._database = localStorage;
this._storage = localstorage; this._storage = localstorage;
this._mode = "localStorage"; this._mode = "localStorage";
this._key_schema = spec.key_schema;
break; break;
} }
} }
...@@ -215,7 +214,7 @@ ...@@ -215,7 +214,7 @@
* @param {Object} options The command options * @param {Object} options The command options
*/ */
LocalStorage.prototype.putAttachment = function (command, param) { LocalStorage.prototype.putAttachment = function (command, param) {
var that = this, doc, status = "ok"; var that = this, doc, status = "created";
doc = this._storage.getItem(this._localpath + "/" + param._id); doc = this._storage.getItem(this._localpath + "/" + param._id);
if (doc === null) { if (doc === null) {
// the document does not exist // the document does not exist
...@@ -231,7 +230,7 @@ ...@@ -231,7 +230,7 @@
jIO.util.readBlobAsBinaryString(param._blob).then(function (e) { jIO.util.readBlobAsBinaryString(param._blob).then(function (e) {
doc._attachments = doc._attachments || {}; doc._attachments = doc._attachments || {};
if (doc._attachments[param._attachment]) { if (doc._attachments[param._attachment]) {
status = "created"; status = "no_content";
} }
doc._attachments[param._attachment] = { doc._attachments[param._attachment] = {
"content_type": param._blob.type, "content_type": param._blob.type,
...@@ -287,7 +286,7 @@ ...@@ -287,7 +286,7 @@
* @param {Object} options The command options * @param {Object} options The command options
*/ */
LocalStorage.prototype.getAttachment = function (command, param) { LocalStorage.prototype.getAttachment = function (command, param) {
var doc; var doc, i, uint8array, binarystring;
doc = this._storage.getItem(this._localpath + "/" + param._id); doc = this._storage.getItem(this._localpath + "/" + param._id);
if (doc === null) { if (doc === null) {
return command.error( return command.error(
...@@ -306,12 +305,22 @@ ...@@ -306,12 +305,22 @@
); );
} }
// Storing data twice in binarystring and in uint8array (in memory)
// is not a problem here because localStorage <= 5MB
binarystring = this._storage.getItem(
this._localpath + "/" + param._id + "/" + param._attachment
) || "";
uint8array = new Uint8Array(binarystring.length);
for (i = 0; i < binarystring.length; i += 1) {
uint8array[i] = binarystring.charCodeAt(i); // mask `& 0xFF` not necessary
}
uint8array = new Blob([uint8array], {
"type": doc._attachments[param._attachment].content_type || ""
});
command.success({ command.success({
"data": this._storage.getItem( "data": uint8array,
this._localpath + "/" + param._id + "digest": doc._attachments[param._attachment].digest
"/" + param._attachment
) || "",
"content_type": doc._attachments[param._attachment].content_type || ""
}); });
}; };
...@@ -362,7 +371,7 @@ ...@@ -362,7 +371,7 @@
*/ */
LocalStorage.prototype.removeAttachment = function (command, param) { LocalStorage.prototype.removeAttachment = function (command, param) {
var doc = this._storage.getItem(this._localpath + "/" + param._id); var doc = this._storage.getItem(this._localpath + "/" + param._id);
if (typeof doc !== 'object') { if (typeof doc !== 'object' || doc === null) {
return command.error( return command.error(
"not_found", "not_found",
"missing document", "missing document",
...@@ -402,7 +411,7 @@ ...@@ -402,7 +411,7 @@
rows = []; rows = [];
document_list = []; document_list = [];
path_re = new RegExp( path_re = new RegExp(
"^" + complex_queries.stringEscapeRegexpCharacters(this._localpath) + "^" + jIO.Query.stringEscapeRegexpCharacters(this._localpath) +
"/[^/]+$" "/[^/]+$"
); );
if (options.query === undefined && options.sort_on === undefined && if (options.query === undefined && options.sort_on === undefined &&
...@@ -425,7 +434,7 @@ ...@@ -425,7 +434,7 @@
} }
command.success({"data": {"rows": rows, "total_rows": rows.length}}); command.success({"data": {"rows": rows, "total_rows": rows.length}});
} else { } else {
// create complex query object from returned results // create jio query object from returned results
for (i in this._database) { for (i in this._database) {
if (this._database.hasOwnProperty(i)) { if (this._database.hasOwnProperty(i)) {
if (path_re.test(i)) { if (path_re.test(i)) {
...@@ -444,27 +453,29 @@ ...@@ -444,27 +453,29 @@
document_object[meta._id] = meta; document_object[meta._id] = meta;
}); });
} }
complex_queries.QueryFactory.create(options.query || ""). jIO.QueryFactory.create(options.query || "",
exec(document_list, options); this._key_schema).
document_list = document_list.map(function (value) { exec(document_list, options).then(function () {
var o = { document_list = document_list.map(function (value) {
"id": value._id, var o = {
"key": value._id "id": value._id,
}; "key": value._id
if (options.include_docs === true) { };
o.doc = document_object[value._id]; if (options.include_docs === true) {
delete document_object[value._id]; o.doc = document_object[value._id];
} delete document_object[value._id];
if (delete_id) { }
delete value._id; if (delete_id) {
} delete value._id;
o.value = value; }
return o; o.value = value;
}); return o;
command.success({"data": { });
"total_rows": document_list.length, command.success({"data": {
"rows": document_list "total_rows": document_list.length,
}}); "rows": document_list
}});
});
} }
}; };
...@@ -502,13 +513,13 @@ ...@@ -502,13 +513,13 @@
*/ */
LocalStorage.prototype.genericRepair = function (command, param, repair) { LocalStorage.prototype.genericRepair = function (command, param, repair) {
var that = this, result; var that = this, final_result;
function referenceAttachment(param, attachment) { function referenceAttachment(param, attachment) {
if (jIO.util.indexOf(param.referenced_attachments, attachment) !== -1) { if (param.referenced_attachments.indexOf(attachment) !== -1) {
return; return;
} }
var i = jIO.util.indexOf(param.unreferenced_attachments, attachment); var i = param.unreferenced_attachments.indexOf(attachment);
if (i !== -1) { if (i !== -1) {
param.unreferenced_attachments.splice(i, 1); param.unreferenced_attachments.splice(i, 1);
} }
...@@ -517,10 +528,10 @@ ...@@ -517,10 +528,10 @@
} }
function attachmentFound(param, attachment) { function attachmentFound(param, attachment) {
if (jIO.util.indexOf(param.referenced_attachments, attachment) !== -1) { if (param.referenced_attachments.indexOf(attachment) !== -1) {
return; return;
} }
if (jIO.util.indexOf(param.unreferenced_attachments, attachment) !== -1) { if (param.unreferenced_attachments.indexOf(attachment) !== -1) {
return; return;
} }
param.unreferenced_attachments[param.unreferenced_attachments.length] = param.unreferenced_attachments[param.unreferenced_attachments.length] =
...@@ -535,7 +546,7 @@ ...@@ -535,7 +546,7 @@
} }
// check document type // check document type
if (typeof doc !== 'object') { if (typeof doc !== 'object' || doc === null) {
// wrong document // wrong document
if (!repair) { if (!repair) {
return {"error": true, "answers": [ return {"error": true, "answers": [
...@@ -656,14 +667,14 @@ ...@@ -656,14 +667,14 @@
param.referenced_attachments = []; param.referenced_attachments = [];
param.unreferenced_attachments = []; param.unreferenced_attachments = [];
if (typeof param._id === 'string') { if (typeof param._id === 'string') {
result = repairOne(param, repair) || {}; final_result = repairOne(param, repair) || {};
} else { } else {
result = repairAll(param, repair) || {}; final_result = repairAll(param, repair) || {};
} }
if (result.error) { if (final_result.error) {
return command.error.apply(command, result.answers || []); return command.error.apply(command, final_result.answers || []);
} }
command.success.apply(command, result.answers || []); command.success.apply(command, final_result.answers || []);
}; };
jIO.addStorage('local', LocalStorage); jIO.addStorage('local', LocalStorage);
......
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