Skip to content

Commit

Permalink
Added test-rpc.js
Browse files Browse the repository at this point in the history
Some fixes were made such that bonescript.js can run via node.js. I haven't yet verified that it doesn't break the browser.

An internal startClient function was created.

Added a devDependency on socket.io-client.
  • Loading branch information
Jason Kridner committed Jun 13, 2018
1 parent a9ca418 commit 47454f7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"coveralls": "~3.0.1",
"js-beautify": "^1.7.5",
"jscoverage": "~0.6.0",
"nodeunit": "^0.11.2"
"nodeunit": "^0.11.2",
"socket.io-client": "1.4.5"
},
"scripts": {
"start": "node server.js",
Expand Down
28 changes: 23 additions & 5 deletions src/bonescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ _bonescript.on.reconnecting = function () {};
_bonescript.on.initialized = function () {};

(function () {
if (typeof document == 'undefined') {
var io = require('socket.io-client');
module.exports.startClient = function (host, port, callback) {
_bonescript.on.initialized = callback;
var socket = _onSocketIOLoaded(host, port, io);
}
return;
}
require = myrequire;
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
Expand All @@ -23,11 +32,12 @@ _bonescript.on.initialized = function () {};
scriptObj.onload = _onSocketIOLoaded;
}());

function _onSocketIOLoaded() {
function _onSocketIOLoaded(host, port, socketio) {
//console.log("socket.io loaded");
var socket = io.connect('___INSERT_HOST___', {
port: 80
});
if (typeof host == 'undefined') host = '___INSERT_HOST___';
if (typeof port == 'undefined') port = 80;
if (typeof socketio == 'undefined' && typeof io != 'undefined') socketio = io;
var socket = socketio.connect('http://' + host + ':' + port);
socket.on('require', getRequireData);
socket.on('bonescript', _seqcall);
socket.on('connect', _bonescript.on.connect);
Expand Down Expand Up @@ -85,6 +95,8 @@ function _onSocketIOLoaded() {
_bonescript.modules[m.module].socket = socket;
_bonescript.on.initialized();
}

return (socket);
}

function _seqcall(data) {
Expand All @@ -97,8 +109,14 @@ function _seqcall(data) {
// Require must be synchronous to be able to return data structures and
// functions and therefore cannot call socket.io. All exported modules must
// be exported ahead of time.
function require(module) {
function myrequire(module) {
if (typeof _bonescript == 'undefined')
throw 'No BoneScript modules are not currently available';
if (typeof _bonescript.modules[module] == 'undefined')
throw 'Module "' + module + '" is not currently available';
return (_bonescript.modules[module]);
}

if (typeof module != 'undefined') {
module.exports.require = myrequire;
}
4 changes: 4 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ var serverStart = function (port, directory, callback) {
}
}

if (callback) {
callback(serverEmitter);
}

return (serverEmitter);
};

Expand Down
25 changes: 25 additions & 0 deletions test/test-rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var server = require('../src/server');
var bonescript = require('../src/bonescript');
var serverEmitter = null;

exports.setUp = function (callback) {
server.serverStart(8000, process.cwd(), mycb);

function mycb(emitter) {
serverEmitter = emitter;
bonescript.startClient('127.0.0.1', 8000, callback);
}
};

exports.testRPC1 = function (test) {
test.expect(1);
test.doesNotThrow(function () {
console.log(bonescript);
var b = bonescript.require('bonescript');
b.getPlatform(function (platform) {
console.log('Name: ' + platform.name);
console.log('Version: ' + platform.bonescript);
});
});
test.done();
};

0 comments on commit 47454f7

Please sign in to comment.