-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproximal.js
More file actions
172 lines (151 loc) · 4.39 KB
/
proximal.js
File metadata and controls
172 lines (151 loc) · 4.39 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* proximal - minimal JSON RPC over HTTP with Proxy/Promise interface
* https://github.com/gavinhungry/proximal
*/
((global, props, factory) => {
(typeof define === 'function' && define.amd) ? define(props.name, factory) :
(typeof module === 'object' && module.exports) ? module.exports = factory() :
global[props.export || props.name] = factory();
})(this, {
name: 'proximal'
}, () => {
'use strict';
/**
* Proximal client constructor
*
* @param {Object} opts
* @param {String} opts.url - URL of remote endpoint
* @param {Function} [opts.xhr] - alternate XMLHttpRequest constructor
*/
let Client = (() => {
let Client = function ProximalClient(opts = {}) {
if (!opts.url) {
throw new Error('url is required');
}
this.XHR = opts.xhr || XMLHttpRequest;
this.url = opts.url;
};
Client.prototype = {
/**
* Get a Proxy object representing a remote module
*
* @param {String} moduleName
* @return {Proxy}
*/
getModule: function(moduleName) {
if (!moduleName) {
throw new Error('moduleName is required');
}
return new Proxy({
url: this.url,
moduleName: moduleName
}, {
get: (obj, methodName) => {
return (...args) => {
return new Promise((res, rej) => {
let xhr = new this.XHR();
xhr.open('POST', this.url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onerror = e => rej(xhr.responseText);
xhr.onload = e => {
if (xhr.status !== 200) {
return xhr.onerror(e);
}
try {
res(JSON.parse(xhr.responseText));
} catch(err) {
res(null);
}
};
xhr.send(JSON.stringify({ moduleName, methodName, args }));
});
};
}
});
}
};
return Client;
})();
/**
* Proximal server constructor
*
* @param {Object} opts
* @param {Object} opts.modules - map of modules to be included
*/
let Server = (() => {
class ImplError extends Error {}
let Server = function ProximalServer(opts = {}) {
if (!opts.modules || !Object.keys(opts.modules).length) {
throw new Error('modules are required');
}
this.modules = opts.modules;
};
Server.prototype = {
/**
* Get the parsed JSON body from a request, even without body-parser
*
* @private
*
* @param {Request} req
* @return {Promise<Object>}
*/
_getReqCall: function(req) {
if (!req) {
throw new Error('no body');
}
if (req.body) {
return Promise.resolve(req.body);
}
return new Promise((resolve, reject) => {
let data = '';
req.on('data', chunk => data += chunk );
req.on('end', () => {
try {
resolve(JSON.parse(data));
} catch(err) {
reject(err);
}
});
});
},
/**
* Get the result of a module method
*
* @private
*
* @param {Object} call
* @param {String} call.moduleName
* @param {String} call.methodName
* @param {Array<Mixed>} call.args
* @return {Mixed} method result
*/
_getCallResult: function(call = {}) {
let module = this.modules[call.moduleName];
if (!module) {
throw new ImplError('module not found');
}
let method = module[call.methodName];
if (!method || typeof method !== 'function') {
throw new ImplError('method not found');
}
return method.apply(module, call.args);
},
/**
* Middleware function for handing calls
*
* @return {Function}
*/
middleware: function() {
return (req, res) => {
this._getReqCall(req).then(call => this._getCallResult(call), err => {
res.status(400).json('Error parsing RPC request body');
}).then(result => res.json(result), err => {
res.status(err instanceof ImplError ? 501 : 500).json(err.message);
});
};
}
};
return Server;
})();
return { Client, Server };
});