@rpcbase/server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ /* @flow */
2
+ const rpc_router = require("./rpc/rpc_router")
3
+
4
+ module.exports = {
5
+ rpc_router
6
+ }
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@rpcbase/server",
3
+ "version": "0.1.0",
4
+ "license": "SSPL-1.0",
5
+ "main": "./index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 0"
8
+ },
9
+ "dependencies": {
10
+ "glob": "7.2.0"
11
+ }
12
+ }
@@ -0,0 +1,41 @@
1
+ /* @flow */
2
+ const fs = require("fs")
3
+ const path = require("path")
4
+ const glob = require("glob")
5
+
6
+ const src_path = path.join(process.cwd(), "./src/")
7
+ const build_dir = path.join(process.cwd(), "build/")
8
+
9
+
10
+ const async_wrapper = fn => (req, res, next) => {
11
+ Promise.resolve(fn(req, res, next))
12
+ .catch(next)
13
+ }
14
+
15
+
16
+ const rpc_router = (app) => {
17
+ const rpc_routes = glob.sync(path.join(build_dir, "./rpc/*"))
18
+
19
+ rpc_routes.forEach((file_path) => {
20
+ const basename = path.basename(file_path)
21
+ const route_path = basename.replace(/__slash__/g, "/")
22
+ const module_path = path.join(src_path, `${route_path}.js`)
23
+
24
+ // if module not found, remove created route file
25
+ if (!fs.existsSync(module_path)) {
26
+ fs.unlinkSync(file_path)
27
+ console.log("removed inexistant rpc route", basename, "with module path", module_path)
28
+ }
29
+
30
+ const rpc_fn = require(module_path)
31
+ app.post(`/rpc/${route_path}`, async_wrapper(async(req, res) => {
32
+ // console.log("GOT REQ", req.body)
33
+ const result = await rpc_fn(req.body, req)
34
+ // console.log("rpc", route_path, result)
35
+ res.json(result)
36
+ }))
37
+ })
38
+
39
+ }
40
+
41
+ module.exports = rpc_router