Skip to content

Commit

Permalink
server-node: Updated backward compability and packages
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Nov 19, 2016
1 parent 4f4daf6 commit 346b194
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
11 changes: 7 additions & 4 deletions src/packages/default/Broadway/api.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(function() {

exports.register = function(API, VFS, instance) {
API.broadway = function(args, callback, request, response) {
callback(false, false);
};
module.exports.api = {
broadway: function(instance, http, resolve, reject, args) {
resolve(false);
}
};

module.exports.register = function(instance, metadata, servers) {
};

})();
18 changes: 5 additions & 13 deletions src/packages/default/Settings/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,13 @@
(function() {
'use strict';

/*
* See http://os.js.org/doc/tutorials/application-with-server-api.html
*/

//
// Run `app._api('test', {}, fn)` in client to reach this
//
module.exports.test = function(args, callback, request, response) {
callback(false, 'test');
module.exports.api = {
test: function(instance, http, resolve, reject, args) {
resolve('test');
}
};

//
// This is called whenever the HTTP server starts up
//
module.exports._onServerStart = function(server, instance, metadata) {
module.exports.register = function(instance, metadata, servers) {
};

})();
Expand Down
30 changes: 14 additions & 16 deletions src/server/node/core/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,23 @@ function registerPackages(servers) {
};
}

function _registerApplication() {
if ( typeof module.register === 'function' ) {
module.register(instance, packages[path], {
http: servers.httpServer,
ws: servers.websocketServer,
proxy: servers.proxyServer
});

function _registerApplication(packages, module) {
if ( typeof module.api === 'object' ) {
if ( typeof module.register === 'function' ) {
module.register(instance, packages[path], {
http: servers.httpServer,
ws: servers.websocketServer,
proxy: servers.proxyServer
});
}
return false;
} else if ( typeof module._onServerStart === 'function' ) {
// Backward compatible with old API
module._onServerStart(server.httpServer, _createOldInstance(instance), packages[path]);
module._onServerStart(servers.httpServer, _createOldInstance(instance), packages[path]);
return true;
}

return true;
return typeof module.api === 'undefined';
}

function _registerExtension(module) {
Expand All @@ -303,7 +305,6 @@ function registerPackages(servers) {
};
});
}

return true;
}

Expand Down Expand Up @@ -339,17 +340,14 @@ function registerPackages(servers) {
const check = _path.join(instance.DIRS.packages, path, filename);
if ( metadata.enabled !== false && _fs.existsSync(check) ) {
var deprecated = false;
var loaded;
if ( metadata.type === 'extension' ) {
logger().lognt('INFO', 'Loading:', logger().colored('Application', 'bold'), check.replace(instance.DIRS.root, ''));
deprecated = _registerExtension(require(check));
_launchSpawners(path, module, metadata);
} else {
logger().lognt('INFO', 'Loading:', logger().colored('Extension', 'bold'), check.replace(instance.DIRS.root, ''));
deprecated = _registerApplication(require(check));
}

if ( typeof module.api === 'undefined' ) {
deprecated = true;
deprecated = _registerApplication(packages, require(check));
}

if ( deprecated ) {
Expand Down
7 changes: 5 additions & 2 deletions src/server/node/modules/api/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ module.exports.application = function(http, data) {
}
} else {
// Backward compatible with old API
if ( typeof module[ameth] === 'function' ) {
var imported = {};
module.register(imported, {}, {});

if ( typeof imported[ameth] === 'function' ) {
found = function backwardCompatibleApplicationApiCall() {
module[ameth](aargs, function(error, result) {
imported[ameth](aargs, function(error, result) {
if ( error ) {
reject(error);
} else {
Expand Down
15 changes: 13 additions & 2 deletions src/templates/package/service/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@
(function() {
'use strict';

// See http://os.js.org/doc/tutorials/application-with-server-api.html
module.exports = {
/**
* Registers your package when OS.js server starts.
*/
module.exports.register = function(instance, metadata, servers) {
};

/**
* Registers your Application API methods
*/
module.exports.api = {
test: function(instance, http, resolve, reject, args) {
resolve('This is a response from your application');
}
};

})();
Expand Down
4 changes: 0 additions & 4 deletions src/templates/package/simple-application/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
(function() {
'use strict';

/*
* See http://os.js.org/doc/tutorials/application-with-server-api.html
*/

/**
* Registers your package when OS.js server starts.
*/
Expand Down

0 comments on commit 346b194

Please sign in to comment.