2014-07-26 10:15:29 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-05 11:53:27 +01:00
|
|
|
var Bluebird = require('bluebird');
|
2015-02-05 17:05:49 +01:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
var inherits = require('util').inherits;
|
|
|
|
|
var jsonRpc = require('json-rpc');
|
2015-02-10 11:40:11 +01:00
|
|
|
var makeError = require('make-error');
|
2015-02-05 17:05:49 +01:00
|
|
|
var MethodNotFound = require('json-rpc/errors').MethodNotFound;
|
2015-02-05 11:53:27 +01:00
|
|
|
var WebSocket = require('ws');
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-10 14:42:56 +01:00
|
|
|
var createCollection = require('./collection');
|
|
|
|
|
|
2014-07-26 10:15:29 +02:00
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-10 17:53:33 +01:00
|
|
|
// Expose Bluebird for now to ease integration (e.g. with Angular.js).
|
|
|
|
|
exports.setScheduler = Bluebird.setScheduler;
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-05 13:23:26 +01:00
|
|
|
function makeDeferred() {
|
|
|
|
|
var resolve, reject;
|
|
|
|
|
var promise = new Bluebird(function (resolve_, reject_) {
|
|
|
|
|
resolve = resolve_;
|
|
|
|
|
reject = reject_;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
promise: promise,
|
|
|
|
|
reject: reject,
|
|
|
|
|
resolve: resolve,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 13:19:47 +01:00
|
|
|
function startsWith(string, target) {
|
|
|
|
|
return (string.lastIndexOf(target, 0) === 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-10 11:40:11 +01:00
|
|
|
function returnThis() {
|
|
|
|
|
/* jshint validthis: true */
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns an iterator to the Fibonacci sequence.
|
|
|
|
|
function fibonacci(start) {
|
|
|
|
|
var prev = 0;
|
|
|
|
|
var curr = start || 1;
|
|
|
|
|
|
|
|
|
|
var iterator = {
|
|
|
|
|
next: function () {
|
|
|
|
|
var tmp = curr;
|
|
|
|
|
curr += prev;
|
|
|
|
|
prev = tmp;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
done: false,
|
|
|
|
|
value: prev,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Make the iterator a true iterable (ES6).
|
|
|
|
|
if (typeof Symbol !== 'undefined') {
|
|
|
|
|
iterator[Symbol.iterator] = returnThis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return iterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
|
|
2014-07-28 13:21:19 +02:00
|
|
|
// Fix URL if necessary.
|
2015-02-12 12:22:49 +01:00
|
|
|
var URL_RE = /^(?:(?:http|ws)(s)?:\/\/)?(.*?)\/*(?:\/api\/)?(\?.*?)?(?:#.*)?$/;
|
2015-02-05 11:57:05 +01:00
|
|
|
function fixUrl(url) {
|
2015-02-05 12:41:25 +01:00
|
|
|
var matches = URL_RE.exec(url);
|
|
|
|
|
var isSecure = !!matches[1];
|
2015-02-12 12:22:49 +01:00
|
|
|
var hostAndPath = matches[2];
|
|
|
|
|
var search = matches[3];
|
2015-02-05 12:41:25 +01:00
|
|
|
|
2014-07-28 13:21:19 +02:00
|
|
|
return [
|
2015-02-05 12:41:25 +01:00
|
|
|
isSecure ? 'wss' : 'ws',
|
|
|
|
|
'://',
|
2015-02-12 12:22:49 +01:00
|
|
|
hostAndPath,
|
2015-02-05 12:41:25 +01:00
|
|
|
'/api/',
|
2015-02-12 12:22:49 +01:00
|
|
|
search,
|
2014-07-28 13:21:19 +02:00
|
|
|
].join('');
|
2015-02-05 11:57:05 +01:00
|
|
|
}
|
2015-02-10 11:40:11 +01:00
|
|
|
exports.fixUrl = fixUrl;
|
2014-07-28 13:21:19 +02:00
|
|
|
|
2014-07-26 10:15:29 +02:00
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-10 18:21:07 +01:00
|
|
|
function getCurrentUrl() {
|
|
|
|
|
/* global window: false */
|
|
|
|
|
|
|
|
|
|
if (typeof window === undefined) {
|
|
|
|
|
throw new Error('cannot get current URL');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return window.location.host + window.location.pathname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-10 11:40:11 +01:00
|
|
|
var ConnectionLost = makeError('ConnectionLost');
|
|
|
|
|
|
|
|
|
|
// Low level interface to XO.
|
|
|
|
|
function Api(url) {
|
2015-02-05 17:05:49 +01:00
|
|
|
// Super constructor.
|
|
|
|
|
EventEmitter.call(this);
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-05 17:05:49 +01:00
|
|
|
// Fix the URL (ensure correct protocol and /api/ path).
|
2015-02-10 18:21:07 +01:00
|
|
|
this._url = fixUrl(url || getCurrentUrl());
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-05 17:05:49 +01:00
|
|
|
// Will contains the WebSocket.
|
|
|
|
|
this._socket = null;
|
|
|
|
|
|
|
|
|
|
// The JSON-RPC server.
|
|
|
|
|
var this_ = this;
|
|
|
|
|
this._jsonRpc = jsonRpc.createServer(function (message) {
|
|
|
|
|
if (message.type === 'notification') {
|
|
|
|
|
this_.emit('notification', message);
|
|
|
|
|
} else {
|
|
|
|
|
// This object does not support requests.
|
|
|
|
|
throw new MethodNotFound(message.method);
|
|
|
|
|
}
|
|
|
|
|
}).on('data', function (message) {
|
|
|
|
|
this_._socket.send(JSON.stringify(message));
|
|
|
|
|
});
|
2015-02-05 11:57:05 +01:00
|
|
|
}
|
2015-02-10 11:40:11 +01:00
|
|
|
inherits(Api, EventEmitter);
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
Api.prototype.close = function () {
|
|
|
|
|
if (this._socket) {
|
|
|
|
|
this._socket.close();
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
Api.prototype.connect = Bluebird.method(function () {
|
|
|
|
|
if (this._socket) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
var deferred = makeDeferred();
|
2015-02-05 13:23:26 +01:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
var opts = {};
|
|
|
|
|
if (startsWith(this._url, 'wss')) {
|
|
|
|
|
// Due to imperfect TLS implementation in XO-Server.
|
|
|
|
|
opts.rejectUnauthorized = false;
|
|
|
|
|
}
|
|
|
|
|
var socket = this._socket = new WebSocket(this._url, '', opts);
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
// Used to avoid binding listeners to this object.
|
|
|
|
|
var this_ = this;
|
2015-02-05 17:05:49 +01:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
// When the socket opens, send any queued requests.
|
|
|
|
|
socket.addEventListener('open', function () {
|
|
|
|
|
// Resolves the promise.
|
|
|
|
|
deferred.resolve();
|
2015-02-10 11:40:11 +01:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
this_.emit('connected');
|
|
|
|
|
});
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
socket.addEventListener('message', function (message) {
|
|
|
|
|
this_._jsonRpc.write(message.data);
|
|
|
|
|
});
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
socket.addEventListener('close', function () {
|
|
|
|
|
this_._socket = null;
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
this_._jsonRpc.failPendingRequests(new ConnectionLost());
|
2015-02-10 11:40:11 +01:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
// Only emit this event if connected before.
|
|
|
|
|
if (deferred.promise.isFulfilled()) {
|
|
|
|
|
this_.emit('disconnected');
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
socket.addEventListener('error', function (error) {
|
|
|
|
|
// Fails the connect promise if possible.
|
|
|
|
|
deferred.reject(error);
|
|
|
|
|
});
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
return deferred.promise;
|
|
|
|
|
});
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
Api.prototype.call = function (method, params) {
|
|
|
|
|
var jsonRpc = this._jsonRpc;
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-11 14:11:38 +01:00
|
|
|
return this.connect().then(function () {
|
|
|
|
|
return jsonRpc.request(method, params);
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-07-26 10:15:29 +02:00
|
|
|
|
2015-02-10 11:40:11 +01:00
|
|
|
exports.Api = Api;
|
|
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
|
|
2015-02-10 14:42:56 +01:00
|
|
|
var objectsOptions = {
|
|
|
|
|
indexes: [
|
|
|
|
|
'ref',
|
|
|
|
|
'type',
|
|
|
|
|
'UUID',
|
|
|
|
|
],
|
|
|
|
|
key: function (item) {
|
|
|
|
|
return item.UUID || item.ref;
|
|
|
|
|
},
|
|
|
|
|
};
|
2015-02-10 11:40:11 +01:00
|
|
|
|
2015-02-10 17:12:10 +01:00
|
|
|
function tryConnect() {
|
|
|
|
|
/* jshint validthis: true */
|
2015-02-10 11:40:11 +01:00
|
|
|
|
2015-02-11 15:11:03 +01:00
|
|
|
this.status = 'connecting';
|
2015-02-10 17:12:10 +01:00
|
|
|
return this._api.connect().bind(this).catch(function () {
|
2015-02-11 15:11:03 +01:00
|
|
|
this.status = 'disconnected';
|
2015-02-10 18:21:53 +01:00
|
|
|
var delay = this._backOff.next().value;
|
|
|
|
|
return Bluebird.delay(delay).bind(this).then(tryConnect);
|
2015-02-10 17:12:10 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onSuccessfulConnection() {
|
|
|
|
|
/* jshint validthis: true */
|
|
|
|
|
|
2015-02-11 15:17:42 +01:00
|
|
|
// Reset back off.
|
|
|
|
|
this._backOff = fibonacci(1e3);
|
|
|
|
|
|
2015-02-10 17:12:10 +01:00
|
|
|
// FIXME: session.signIn() should work with both token and password.
|
|
|
|
|
return this._api.call(
|
|
|
|
|
this._auth.token ?
|
|
|
|
|
'session.signInWithToken' :
|
|
|
|
|
'session.signInWithPassword',
|
|
|
|
|
this._auth
|
|
|
|
|
).bind(this).then(function (user) {
|
|
|
|
|
this.user = user;
|
|
|
|
|
this.status = 'connected';
|
|
|
|
|
|
|
|
|
|
this._api.call('xo.getAllObjects').bind(this).then(function (objects) {
|
2015-02-10 20:12:48 +01:00
|
|
|
this.objects.clear();
|
2015-02-10 17:12:10 +01:00
|
|
|
this.objects.setMultiple(objects);
|
2015-02-10 11:40:11 +01:00
|
|
|
});
|
2015-02-10 17:12:10 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onFailedConnection() {
|
|
|
|
|
/* jshint validthis: true */
|
|
|
|
|
|
|
|
|
|
this.status = 'disconnected';
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-11 14:13:01 +01:00
|
|
|
function connect() {
|
|
|
|
|
/* jshint validthis: true */
|
|
|
|
|
|
2015-02-10 17:12:10 +01:00
|
|
|
if (this._connection) {
|
|
|
|
|
return this._connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._connection = tryConnect.call(this).then(
|
|
|
|
|
onSuccessfulConnection, onFailedConnection
|
|
|
|
|
);
|
|
|
|
|
return this._connection;
|
2015-02-11 14:13:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// High level interface to Xo.
|
|
|
|
|
//
|
|
|
|
|
// Handle auto-reconnect, sign in & objects cache.
|
|
|
|
|
function Xo(opts) {
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
|
|
this._api = new Api(opts.url);
|
|
|
|
|
this._auth = opts.auth;
|
|
|
|
|
this._backOff = fibonacci(1e3);
|
|
|
|
|
this.objects = createCollection(objectsOptions);
|
|
|
|
|
this.status = 'disconnected';
|
|
|
|
|
this.user = null;
|
|
|
|
|
|
|
|
|
|
// Promise representing the connection status.
|
|
|
|
|
this._connection = null;
|
|
|
|
|
|
|
|
|
|
self._api.on('disconnected', function () {
|
|
|
|
|
// Automatically reconnect.
|
|
|
|
|
self._connection = null;
|
2015-02-11 15:11:23 +01:00
|
|
|
connect.call(self);
|
2015-02-11 14:13:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self._api.on('notification', function (notification) {
|
|
|
|
|
if (notification.method !== 'all') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var method = (
|
|
|
|
|
notification.params.type === 'exit' ?
|
|
|
|
|
'unset' :
|
|
|
|
|
'set'
|
|
|
|
|
) + 'Multiple';
|
|
|
|
|
|
|
|
|
|
self.objects[method](notification.params.items);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Bootstrap the connection.
|
|
|
|
|
connect.call(this);
|
|
|
|
|
}
|
2015-02-10 11:40:11 +01:00
|
|
|
|
2015-02-10 17:12:10 +01:00
|
|
|
Xo.prototype.call = function (method, params) {
|
2015-02-11 14:16:38 +01:00
|
|
|
// Prevent session.*() from being because it may interfere
|
2015-02-10 17:12:10 +01:00
|
|
|
// with this class session management.
|
2015-02-11 14:16:38 +01:00
|
|
|
if (startsWith(method, 'session.')) {
|
|
|
|
|
throw new Error('session.*() methods are disabled from this interface');
|
|
|
|
|
}
|
2015-02-10 17:12:10 +01:00
|
|
|
|
2015-02-11 14:13:01 +01:00
|
|
|
return connect.call(this).then(function () {
|
2015-02-10 17:12:10 +01:00
|
|
|
var self = this;
|
|
|
|
|
return this._api.call(method, params).catch(ConnectionLost, function () {
|
|
|
|
|
// Retry automatically.
|
|
|
|
|
return self.call(method, params);
|
2015-02-10 11:40:11 +01:00
|
|
|
});
|
2015-02-10 17:12:10 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-02-10 11:40:11 +01:00
|
|
|
exports.Xo = Xo;
|