2015-10-28 17:37:17 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-11-16 11:33:11 +00:00
|
|
|
const base64 = require('./base64');
|
2015-10-21 20:25:12 +00:00
|
|
|
const constants = require('./constants');
|
2015-10-08 08:52:37 +00:00
|
|
|
|
2015-10-21 20:25:12 +00:00
|
|
|
const DEVICE_HOST = 'localhost:8082';
|
|
|
|
|
|
|
|
const {keys, objectTypes, propTypes} = constants;
|
|
|
|
const typeConverters = {};
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-21 06:59:02 +00:00
|
|
|
let XMLHttpRequest = global.originalXMLHttpRequest || global.XMLHttpRequest;
|
2015-10-20 22:10:22 +00:00
|
|
|
let sessionId;
|
2015-10-06 20:36:42 +00:00
|
|
|
|
|
|
|
// Check if XMLHttpRequest has been overridden, and get the native one if that's the case.
|
2015-10-21 06:59:02 +00:00
|
|
|
if (XMLHttpRequest.__proto__ != global.XMLHttpRequestEventTarget) {
|
|
|
|
let fakeXMLHttpRequest = XMLHttpRequest;
|
|
|
|
delete global.XMLHttpRequest;
|
|
|
|
XMLHttpRequest = global.XMLHttpRequest;
|
|
|
|
global.XMLHttpRequest = fakeXMLHttpRequest;
|
2015-10-06 20:36:42 +00:00
|
|
|
}
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
module.exports = {
|
|
|
|
registerTypeConverter,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-20 22:10:22 +00:00
|
|
|
createSession,
|
2015-10-15 01:53:37 +00:00
|
|
|
createRealm,
|
2015-10-19 19:06:47 +00:00
|
|
|
callMethod,
|
2015-10-19 22:26:42 +00:00
|
|
|
getProperty,
|
|
|
|
setProperty,
|
2015-10-15 01:53:37 +00:00
|
|
|
beginTransaction,
|
|
|
|
cancelTransaction,
|
|
|
|
commitTransaction,
|
2015-10-02 05:56:47 +00:00
|
|
|
|
2015-10-15 01:53:37 +00:00
|
|
|
clearTestState,
|
|
|
|
};
|
2015-10-15 10:12:28 +00:00
|
|
|
|
2015-11-16 11:33:11 +00:00
|
|
|
registerTypeConverter(propTypes.DATA, (_, {value}) => base64.decode(value));
|
|
|
|
registerTypeConverter(propTypes.DATE, (_, {value}) => new Date(value));
|
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
function registerTypeConverter(type, handler) {
|
|
|
|
typeConverters[type] = handler;
|
|
|
|
}
|
|
|
|
|
2015-10-20 22:10:22 +00:00
|
|
|
function createSession() {
|
|
|
|
sessionId = sendRequest('create_session');
|
|
|
|
return sessionId;
|
2015-10-19 23:59:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 10:00:13 +00:00
|
|
|
function createRealm(args) {
|
|
|
|
if (args) {
|
|
|
|
args = args.map((arg) => serialize(null, arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendRequest('create_realm', {arguments: args});
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:52:32 +00:00
|
|
|
function callMethod(realmId, id, name, args) {
|
2015-10-08 16:43:38 +00:00
|
|
|
if (args) {
|
|
|
|
args = args.map((arg) => serialize(realmId, arg));
|
|
|
|
}
|
2015-10-08 08:53:22 +00:00
|
|
|
|
2015-10-19 22:52:32 +00:00
|
|
|
let result = sendRequest('call_method', {realmId, id, name, arguments: args});
|
2015-10-08 16:43:38 +00:00
|
|
|
return deserialize(realmId, result);
|
2015-10-08 08:53:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:26:42 +00:00
|
|
|
function getProperty(realmId, id, name) {
|
2015-10-19 19:06:47 +00:00
|
|
|
let result = sendRequest('get_property', {realmId, id, name});
|
2015-10-08 08:52:37 +00:00
|
|
|
return deserialize(realmId, result);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 22:26:42 +00:00
|
|
|
function setProperty(realmId, id, name, value) {
|
2015-10-08 09:00:10 +00:00
|
|
|
value = serialize(realmId, value);
|
2015-10-19 19:06:47 +00:00
|
|
|
sendRequest('set_property', {realmId, id, name, value});
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function beginTransaction(realmId) {
|
|
|
|
sendRequest('begin_transaction', {realmId});
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelTransaction(realmId) {
|
|
|
|
sendRequest('cancel_transaction', {realmId});
|
|
|
|
}
|
|
|
|
|
|
|
|
function commitTransaction(realmId) {
|
|
|
|
sendRequest('commit_transaction', {realmId});
|
|
|
|
}
|
|
|
|
|
2015-10-16 01:48:13 +00:00
|
|
|
function clearTestState() {
|
|
|
|
sendRequest('clear_test_state');
|
2015-10-15 10:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
function serialize(realmId, value) {
|
2015-10-15 01:00:21 +00:00
|
|
|
if (typeof value == 'function') {
|
2015-10-19 23:19:43 +00:00
|
|
|
return {type: objectTypes.FUNCTION};
|
2015-10-15 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 19:13:19 +00:00
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
return {type: 'undefined'};
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
if (!value || typeof value != 'object') {
|
|
|
|
return {value: value};
|
|
|
|
}
|
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
let id = value[keys.id];
|
2015-10-08 08:52:37 +00:00
|
|
|
if (id) {
|
2015-10-19 19:06:47 +00:00
|
|
|
if (value[keys.realm] != realmId) {
|
2015-10-08 08:52:37 +00:00
|
|
|
throw new Error('Unable to serialize value from another Realm');
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:05:49 +00:00
|
|
|
return {id};
|
2015-10-08 08:52:37 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 23:46:56 +00:00
|
|
|
if (value instanceof Date) {
|
|
|
|
return {type: propTypes.DATE, value: value.getTime()};
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
let array = value.map((item) => serialize(realmId, item));
|
|
|
|
return {value: array};
|
|
|
|
}
|
|
|
|
|
2015-11-16 11:33:11 +00:00
|
|
|
if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
|
|
|
|
return {type: propTypes.DATA, value: base64.encode(value)};
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:52:37 +00:00
|
|
|
let object = {};
|
|
|
|
for (let key in value) {
|
|
|
|
object[key] = serialize(realmId, value[key]);
|
|
|
|
}
|
|
|
|
return {value: object};
|
|
|
|
}
|
|
|
|
|
|
|
|
function deserialize(realmId, info) {
|
|
|
|
let type = info.type;
|
|
|
|
let handler = type && typeConverters[type];
|
|
|
|
if (handler) {
|
|
|
|
return handler(realmId, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = info.value;
|
|
|
|
if (value && Array.isArray(value)) {
|
|
|
|
return value.map((item) => deserialize(realmId, item));
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendRequest(command, data) {
|
2015-10-20 22:10:22 +00:00
|
|
|
data = Object.assign({}, data, sessionId ? {sessionId} : null);
|
|
|
|
|
|
|
|
let body = JSON.stringify(data);
|
2015-10-06 20:36:42 +00:00
|
|
|
let request = new XMLHttpRequest();
|
2015-10-02 05:56:47 +00:00
|
|
|
let url = 'http://' + DEVICE_HOST + '/' + command;
|
|
|
|
|
|
|
|
request.open('POST', url, false);
|
|
|
|
request.send(body);
|
|
|
|
|
|
|
|
if (request.status != 200) {
|
|
|
|
throw new Error(request.responseText);
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = JSON.parse(request.responseText);
|
2015-10-08 08:52:37 +00:00
|
|
|
|
2015-10-02 05:56:47 +00:00
|
|
|
if (!response || response.error) {
|
2015-12-01 22:05:33 +00:00
|
|
|
let error = response && response.error;
|
|
|
|
|
|
|
|
// Remove the type prefix from the error message (e.g. "Error: ").
|
|
|
|
if (error) {
|
|
|
|
error = error.replace(/^[a-z]+: /i, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(error || `Invalid response for "${command}"`);
|
2015-10-02 05:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.result;
|
|
|
|
}
|