forked from ChaosGroup/json-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (106 loc) · 3.3 KB
/
index.js
File metadata and controls
121 lines (106 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var Trace = require("./lib/trace.js");
var debugLogger;
module.exports = function(logger) {
debugLogger = logger;
return module.exports;
}
module.exports.api = require("./lib/api.js");
module.exports.client = require("./lib/client.js").RpcClient;
module.exports.transport = {
WebSocket: function(httpServer) {
var transport = require("./lib/ws-transport");
transport = new transport(httpServer);
transport.trace = new Trace(debugLogger);
return transport;
},
HTTP: function(httpServer, expressApp) {
if (!httpServer || !expressApp) {
throw new Error('HTTP transport requires an HTTP server and an Express application instance.');
}
var transport = require("./lib/rest-transport");
transport = new transport(httpServer, expressApp);
transport.trace = new Trace(debugLogger);
return transport;
}
};
module.exports.transport.WebSocket.type = 'WebSocket';
module.exports.transport.HTTP.type = 'HTTP';
/**
* Fetches proxy code from a URL
* Attempts to run the script in the VM and return to result to the caller
* @param {String} proxyUrl
* @param {Object} sslSettings
* @param {Function} callback
*/
module.exports.proxy = function(proxyUrl, sslSettings, callback) {
if (!callback) {
callback = sslSettings;
sslSettings = {};
}
var request = require('request');
request(proxyUrl, {
agentOptions: sslSettings
}, function(err, response, body) {
if (err) { callback(err); return; }
if (response.statusCode !== 200) {
callback(new Error('Proxy not available at ' + proxyUrl));
return;
}
var fileName = proxyUrl.substring(0, proxyUrl.indexOf('?'));
var proxyExports = {};
var vm = require('vm');
try {
vm.runInNewContext(body, {
module: {exports: proxyExports},
require: function(moduleName) {
if ('json-ws' === moduleName) {
return module.exports;
} else if ('json-ws/client' === moduleName) {
return module.exports.client;
} else {
return require(moduleName);
}
}
}, fileName);
} catch (vmError) {
callback(new TypeError('Error loading proxy: ' + vmError.message));
return;
}
callback(null, proxyExports);
});
};
module.exports.getClientProxy = function(apiRoot, apiType, version, sslSettings, callback) {
if (!callback) {
callback = sslSettings;
sslSettings = {};
}
var serviceUrl = apiRoot + '/' + apiType + '/' + version;
var proxyClassName = apiType.split(/\W+/).map(function(string) {
return string[0].toUpperCase() + string.slice(1).toLowerCase();
}).join('') + 'Proxy';
var proxyUrl = serviceUrl + '?proxy=JavaScript&localName=' + proxyClassName;
module.exports.proxy(proxyUrl, sslSettings, function(err, proxy) {
if (err) {
callback(err, null);
} else {
var proxyClass = proxy[proxyClassName];
if (proxyClass) {
callback(null, new proxyClass(serviceUrl, sslSettings));
} else {
callback(new Error('Proxy not available'));
}
}
});
}
/**
* API Registry middleware for Express/Connect
* Responds to OPTIONS request, renders a page listing all registered services
* and serves the NodeJS/browser client library.
* @param {String} rootPath Mount point of the service registry.
*/
module.exports.registry = function(rootPath) {
var registry = require('./lib/registry');
return registry(rootPath);
};
module.exports.getLanguageProxy = require('./lib/get-language-proxy');