Commit 86d2f05c authored by Tristan Cavelier's avatar Tristan Cavelier

jio.js updated

parent 28785568
...@@ -3,9 +3,6 @@ ...@@ -3,9 +3,6 @@
if (typeof define === 'function' && define.amd) { if (typeof define === 'function' && define.amd) {
return define(dependencies, module); return define(dependencies, module);
} }
if (typeof exports === 'object') {
return module(exports, require('rsvp'), require('sha256'));
}
window.jIO = {}; window.jIO = {};
module(window.jIO, RSVP, {hex_sha256: hex_sha256}); module(window.jIO, RSVP, {hex_sha256: hex_sha256});
}(['exports', 'rsvp', 'sha256'], function (exports, RSVP, sha256) { }(['exports', 'rsvp', 'sha256'], function (exports, RSVP, sha256) {
...@@ -81,6 +78,7 @@ constants.http_status_text = { ...@@ -81,6 +78,7 @@ constants.http_status_text = {
"507": "Insufficient Storage", "507": "Insufficient Storage",
"Ok": "Ok", "Ok": "Ok",
"OK": "Ok",
"Created": "Created", "Created": "Created",
"No Content": "No Content", "No Content": "No Content",
"Reset Content": "Reset Content", "Reset Content": "Reset Content",
...@@ -189,6 +187,7 @@ constants.http_status = { ...@@ -189,6 +187,7 @@ constants.http_status = {
"507": 507, "507": 507,
"Ok": 200, "Ok": 200,
"OK": 200,
"Created": 201, "Created": 201,
"No Content": 204, "No Content": 204,
"Reset Content": 205, "Reset Content": 205,
...@@ -297,6 +296,7 @@ constants.http_action = { ...@@ -297,6 +296,7 @@ constants.http_action = {
"507": "error", "507": "error",
"Ok": "success", "Ok": "success",
"OK": "success",
"Created": "success", "Created": "success",
"No Content": "success", "No Content": "success",
"Reset Content": "success", "Reset Content": "success",
...@@ -2161,71 +2161,103 @@ function restCommandRejecter(param, args) { ...@@ -2161,71 +2161,103 @@ function restCommandRejecter(param, args) {
// reject(status, reason, message, {"custom": "value"}); // reject(status, reason, message, {"custom": "value"});
// reject(status, reason, {..}); // reject(status, reason, {..});
// reject(status, {..}); // reject(status, {..});
var a = args[0], b = args[1], c = args[2], d = args[3], weak, strong; var arg, current_priority, priority = [
weak = {"result": "error"}; // 0 - custom parameter values
strong = {}; {},
weak.status = constants.http_status.unknown; // 1 - default values
weak.statusText = constants.http_status_text.unknown; {
weak.message = 'Command failed'; "status": constants.http_status.unknown,
weak.reason = 'fail'; "statusText": constants.http_status_text.unknown,
weak.method = param.method; "message": "Command failed",
"reason": "unknown"
},
// 2 - status, reason, message parameters
{},
// 3 - never change
{"result": "error", "method": param.method}
];
args = Array.prototype.slice.call(args);
arg = args.shift();
// priority 3 - never change
current_priority = priority[3];
if (param.kwargs._id) { if (param.kwargs._id) {
weak.id = param.kwargs._id; current_priority.id = param.kwargs._id;
} }
if (/Attachment$/.test(param.method)) { if (/Attachment$/.test(param.method)) {
weak.attachment = param.kwargs._attachment; current_priority.attachment = param.kwargs._attachment;
} }
if (typeof a !== 'object' || Array.isArray(a)) { // priority 2 - status, reason, message parameters
strong.status = constants.http_status[a]; current_priority = priority[2];
strong.statusText = constants.http_status_text[a]; // parsing first parameter if is not an object
if (strong.status === undefined || if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
strong.statusText === undefined) { // first parameter is mandatory
return restCommandRejecter(param, [ current_priority.status = arg;
// can create infernal loop if 'internal_storage_error' is not defined arg = args.shift();
// in the constants
'internal_storage_error',
'invalid response',
'Unknown status "' + a + '"'
]);
} }
a = b; // parsing second parameter if is not an object
b = c; if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
c = d; if (arg !== undefined) {
current_priority.reason = arg;
} }
arg = args.shift();
if (typeof a !== 'object' || Array.isArray(a)) {
strong.reason = a;
a = b;
b = c;
} }
// parsing third parameter if is not an object
if (typeof a !== 'object' || Array.isArray(a)) { if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
strong.message = a; if (arg !== undefined) {
a = b; current_priority.message = arg;
}
arg = args.shift();
} }
if (typeof a === 'object' && !Array.isArray(a)) { // priority 0 - custom values
dictUpdate(weak, a); current_priority = priority[0];
if (a instanceof Error) { // parsing fourth parameter if is an object
weak.reason = a.message; if (typeof arg === 'object' && arg !== null && !Array.isArray(arg)) {
weak.error = a.name; dictUpdate(current_priority, arg);
if (arg.hasOwnProperty('reason')) {
current_priority.reason = arg.reason;
}
if (arg.hasOwnProperty('message')) {
current_priority.message = arg.message;
}
if ((arg.statusText || arg.status >= 0)) {
current_priority.status = arg.statusText || arg.status;
}
if (arg instanceof Error) {
current_priority.reason = arg.message || "";
current_priority.error = arg.name;
} }
} }
dictUpdate(weak, strong); // merge priority dicts
strong = undefined; for (current_priority = priority.length - 1;
if (weak.error === undefined) { current_priority > 0;
weak.error = weak.statusText.toLowerCase().replace(/ /g, '_'). current_priority -= 1) {
replace(/[^_a-z]/g, ''); dictUpdate(priority[current_priority - 1], priority[current_priority]);
} }
if (typeof weak.message !== 'string') { priority = priority[0];
weak.message = "";
// check status
priority.statusText = constants.http_status_text[priority.status];
if (priority.statusText === undefined) {
return restCommandRejecter(param, [
// can create infernal loop if 'internal_storage_error' is not defined in
// the constants
'internal_storage_error',
'invalid response',
'Unknown status "' + priority.status + '"'
]);
} }
if (typeof weak.reason !== 'string') { priority.status = constants.http_status[priority.statusText];
weak.reason = "unknown";
// set default priority error if not already set
if (priority.error === undefined) {
priority.error = priority.statusText.toLowerCase().replace(/ /g, '_').
replace(/[^_a-z]/g, '');
} }
return param.solver.reject(deepClone(weak)); return param.solver.reject(deepClone(priority));
} }
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
...@@ -2236,77 +2268,118 @@ function restCommandResolver(param, args) { ...@@ -2236,77 +2268,118 @@ function restCommandResolver(param, args) {
// resolve('ok', {"custom": "value"}); // resolve('ok', {"custom": "value"});
// resolve(200, {...}); // resolve(200, {...});
// resolve({...}); // resolve({...});
var a = args[0], b = args[1], weak = {"result": "success"}, strong = {}; var arg, current_priority, priority = [
// 0 - custom parameter values
{},
// 1 - default values
{},
// 2 - status parameter
{},
// 3 - never change
{"result": "success", "method": param.method}
];
args = Array.prototype.slice.call(args);
arg = args.shift();
// priority 3 - never change
current_priority = priority[3];
if (param.kwargs._id) {
current_priority.id = param.kwargs._id;
}
if (/Attachment$/.test(param.method)) {
current_priority.attachment = param.kwargs._attachment;
}
// priority 1 - default values
current_priority = priority[1];
if (param.method === 'post') { if (param.method === 'post') {
weak.status = constants.http_status.created; current_priority.status = constants.http_status.created;
weak.statusText = constants.http_status_text.created; current_priority.statusText = constants.http_status_text.created;
} else if (methodType(param.method) === "writer" || } else if (methodType(param.method) === "writer" ||
param.method === "check") { param.method === "check") {
weak.status = constants.http_status.no_content; current_priority.status = constants.http_status.no_content;
weak.statusText = constants.http_status_text.no_content; current_priority.statusText = constants.http_status_text.no_content;
} else { } else {
weak.status = constants.http_status.ok; current_priority.status = constants.http_status.ok;
weak.statusText = constants.http_status_text.ok; current_priority.statusText = constants.http_status_text.ok;
} }
if (param.kwargs._id) {
weak.id = param.kwargs._id; // priority 2 - status parameter
current_priority = priority[2];
// parsing first parameter if is not an object
if (typeof arg !== 'object' || arg === null || Array.isArray(arg)) {
if (arg !== undefined) {
current_priority.status = arg;
} }
if (/Attachment$/.test(param.method)) { arg = args.shift();
weak.attachment = param.kwargs._attachment;
} }
weak.method = param.method;
if (typeof a === 'string' || (typeof a === 'number' && isFinite(a))) { // priority 0 - custom values
strong.status = constants.http_status[a]; current_priority = priority[0];
strong.statusText = constants.http_status_text[a]; // parsing second parameter if is an object
if (strong.status === undefined || if (typeof arg === 'object' && arg !== null && !Array.isArray(arg)) {
strong.statusText === undefined) { dictUpdate(current_priority, arg);
}
// merge priority dicts
for (current_priority = priority.length - 1;
current_priority > 0;
current_priority -= 1) {
dictUpdate(priority[current_priority - 1], priority[current_priority]);
}
priority = priority[0];
// check document id if post method
if (param.method === 'post' &&
(typeof priority.id !== 'string' || !priority.id)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
'Unknown status "' + a + '"' 'New document id have to be specified'
]); ]);
} }
a = b;
} // check status
if (typeof a === 'object' && !Array.isArray(a)) { priority.statusText = constants.http_status_text[priority.status];
dictUpdate(weak, a); if (priority.statusText === undefined) {
}
dictUpdate(weak, strong);
strong = undefined; // free memory
if (param.method === 'post' && (typeof weak.id !== 'string' || !weak.id)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
'New document id have to be specified' 'Unknown status "' + priority.status + '"'
]); ]);
} }
priority.status = constants.http_status[priority.statusText];
// check data for get Attachment
if (param.method === 'getAttachment') { if (param.method === 'getAttachment') {
if (typeof weak.data === 'string') { if (typeof priority.data === 'string') {
weak.data = new Blob([weak.data], { priority.data = new Blob([priority.data], {
"type": weak.content_type || weak.mimetype || "" "type": priority.content_type || priority.mimetype || ""
}); });
delete weak.content_type; delete priority.content_type;
delete weak.mimetype; delete priority.mimetype;
} }
if (!(weak.data instanceof Blob)) { if (!(priority.data instanceof Blob)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
'getAttachment method needs a Blob as returned "data".' 'getAttachment method needs a Blob as returned "data".'
]); ]);
} }
// check data for readers (except check method)
} else if (methodType(param.method) === 'reader' && } else if (methodType(param.method) === 'reader' &&
param.method !== 'check' && param.method !== 'check' &&
(typeof weak.data !== 'object' || (typeof priority.data !== 'object' ||
Object.getPrototypeOf(weak.data) !== Object.prototype)) { priority.data === null ||
Object.getPrototypeOf(priority.data) !== Object.prototype)) {
return restCommandRejecter(param, [ return restCommandRejecter(param, [
'internal_storage_error', 'internal_storage_error',
'invalid response', 'invalid response',
param.method + ' method needs a dict as returned "data".' param.method + ' method needs a dict as returned "data".'
]); ]);
} }
return param.solver.resolve(deepClone(weak));
return param.solver.resolve(deepClone(priority));
} }
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, unparam: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, unparam: true */
......
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