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