Commit c22cfdd6 authored by Alain Takoudjou's avatar Alain Takoudjou

Add webhttp and replicatedopmltree storage

webhttp storage can get json documents by calling http GET method. for jio allDocs,
the storage will get a file called _document_list which should contain the list
of document id that can be downloaded.

replicatedopmltree storage can convert a list of opml storage (each opml can contain
one or more sub_storage) into an indexeddb. All document in opml tree will have an
entry into the indexeddb storage, the full document is added as attachment in the
indexeddb.
parent 81cb08dd
...@@ -184,6 +184,8 @@ module.exports = function (grunt) { ...@@ -184,6 +184,8 @@ module.exports = function (grunt) {
'src/jio.storage/cryptstorage.js', 'src/jio.storage/cryptstorage.js',
'src/jio.storage/websqlstorage.js', 'src/jio.storage/websqlstorage.js',
'src/jio.storage/fbstorage.js' 'src/jio.storage/fbstorage.js'
'src/jio.storage/replicatedopmltreestorage.js',
'src/jio.storage/webhttpstorage.js'
], ],
dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js' dest: 'dist/<%= pkg.name %>-<%= pkg.version %>.js'
// dest: 'jio.js' // dest: 'jio.js'
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Monitor Coverage Scenario</title>
<script src="../node_modules/rsvp/dist/rsvp-2.0.4.js"></script>
<script src="../dist/jio-latest.js"></script>
<link rel="stylesheet" href="../node_modules/grunt-contrib-qunit/test/libs/qunit.css" type="text/css" media="screen"/>
<script src="../node_modules/grunt-contrib-qunit/test/libs/qunit.js" type="text/javascript"></script>
<script src="scenario_monitor.js"></script>
</head>
<body>
<h1 id="qunit-header">Monitor Coverage Scenario</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
/*
* Copyright 2016, Nexedi SA
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true*/
/*global jIO, RSVP */
(function (jIO, RSVP) {
"use strict";
function ajax(storage, options) {
if (options === undefined) {
options = {};
}
if (storage._authorization !== undefined) {
if (options.headers === undefined) {
options.headers = {};
}
options.headers.Authorization = storage._authorization;
}
if (storage._with_credentials !== undefined) {
if (options.xhrFields === undefined) {
options.xhrFields = {};
}
options.xhrFields.withCredentials = storage._with_credentials;
}
return new RSVP.Queue()
.push(function () {
return jIO.util.ajax(options);
});
}
function restrictDocumentId(id) {
var slash_index = id.indexOf("/");
if (slash_index !== 0 && slash_index !== -1) {
throw new jIO.util.jIOError("id " + id + " is forbidden (no begin /)",
400);
}
if (id.lastIndexOf("/") !== (id.length - 1)) {
throw new jIO.util.jIOError("id " + id + " is forbidden (no end /)",
400);
}
return id;
}
function getJsonDocument(context, id) {
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "GET",
url: context._url + "/" + id + ".json",
dataType: "text"
});
})
.push(function (response) {
return {id: id, doc: JSON.parse(response.target.responseText)};
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document '" + id + "'", 404);
}
throw error;
});
}
/**
* The JIO WEB HTTP Storage extension
*
* @class WEBHTTPStorage
* @constructor
*/
function WEBHTTPStorage(spec) {
if (typeof spec.url !== 'string') {
throw new TypeError("WEBHTTPStorage 'url' is not of type string");
}
this._url = spec.url.replace(new RegExp("[/]+$"), "");
if (typeof spec.basic_login === 'string') {
this._authorization = "Basic " + spec.basic_login;
}
this._with_credentials = spec.with_credentials;
}
WEBHTTPStorage.prototype.get = function (id) {
var context = this,
element;
id = restrictDocumentId(id);
element = getJsonDocument(context, id);
if (element !== undefined) {
return element.doc;
}
};
WEBHTTPStorage.prototype.hasCapacity = function (capacity) {
return (capacity === "list") || (capacity === "include");
};
WEBHTTPStorage.prototype.buildQuery = function (options) {
var context = this,
item_list = [],
push_item;
if (options.include_docs === true) {
push_item = function (id, item) {
item_list.push({
"id": id,
"value": {},
"doc": item
});
};
} else {
push_item = function (id) {
item_list.push({
"id": id,
"value": {}
});
};
}
return new RSVP.Queue()
.push(function () {
return ajax(context, {
type: "GET",
url: context._url + "/_document_list",
dataType: "text"
});
})
.push(function (response) {
var document_list = [],
promise_list = [],
i;
document_list = response.target.responseText.split('\n');
for (i = 0; i < document_list.length; i += 1) {
if (document_list[i]) {
promise_list.push(getJsonDocument(context, document_list[i]));
}
}
return RSVP.all(promise_list);
}, function (error) {
if ((error.target !== undefined) &&
(error.target.status === 404)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
throw error;
})
.push(function (result_list) {
var i;
for (i = 0; i < result_list.length; i += 1) {
push_item(result_list[i].id, result_list[i].doc);
}
return item_list;
});
};
jIO.addStorage('webhttp', WEBHTTPStorage);
}(jIO, RSVP));
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