@rpcbase/server 0.239.0 → 0.241.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.239.0",
3
+ "version": "0.241.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -6,19 +6,24 @@ const debug = require("debug")
6
6
 
7
7
  const async_wrapper = require("../helpers/async_wrapper")
8
8
 
9
- const src_path = path.join(process.cwd(), "./src/")
9
+
10
+ const log = debug("rb:rpc")
11
+
12
+ const server_src_dir = path.join(process.cwd(), "./src/")
10
13
  const build_dir = path.join(process.cwd(), "build/")
14
+ const client_dir = path.join(process.cwd(), "../../client/")
11
15
 
16
+ const is_production = process.env.NODE_ENV === "production"
12
17
 
13
- const rpc_router = (app) => {
14
- const rpc_routes = glob.sync(path.join(build_dir, "./rpc/*"))
15
18
 
16
- // console.log("rpc_routes", rpc_routes)
19
+ const setup_prod_rpc_routes = (app) => {
20
+ // the difference with the dev router is that routes are loaded at startup time, whereas in dev mode routes are resolved on each request
21
+ const rpc_routes = glob.sync(path.join(build_dir, "./client/rpc/*"))
17
22
 
18
23
  rpc_routes.forEach((file_path) => {
19
24
  const basename = path.basename(file_path)
20
25
  const route_path = basename.replace(/__slash__/g, "/")
21
- const module_path = path.join(src_path, `${route_path}.js`)
26
+ const module_path = path.join(server_src_dir, `${route_path}.js`)
22
27
 
23
28
  // if module not found, remove created route file
24
29
  if (!fs.existsSync(module_path)) {
@@ -29,13 +34,41 @@ const rpc_router = (app) => {
29
34
  const rpc_fn = require(module_path)
30
35
  app.post(`/rpc/${route_path}`, async_wrapper(async(req, res) => {
31
36
 
32
- console.log(`POST /rpc/${route_path}`)
37
+ log(`POST /rpc/${route_path}`)
33
38
 
34
39
  // TODO: add jaegerclient / opentelemetry
35
40
  const result = await rpc_fn(req.body, {req, res})
36
41
  res.json(result)
37
42
  }))
38
43
  })
44
+ }
45
+
46
+
47
+ const setup_dev_rpc_routes = (app) => {
48
+ const rpc_routes = glob.sync(path.join(client_dir, "./build/rpc/*"))
49
+
50
+ app.post("/rpc/*", async_wrapper(async(req, res) => {
51
+ const route_path = req.params[0]
52
+
53
+ const module_path = path.join(server_src_dir, `${route_path}.js`)
54
+
55
+ const rpc_fn = require(module_path)
56
+ log(`POST /rpc/${route_path}`)
57
+
58
+ // TODO: add jaegerclient / opentelemetry
59
+ const result = await rpc_fn(req.body, {req, res})
60
+ res.json(result)
61
+ }))
62
+ }
63
+
64
+
65
+ const rpc_router = (app) => {
66
+
67
+ if (is_production) {
68
+ setup_prod_rpc_routes(app)
69
+ } else {
70
+ setup_dev_rpc_routes(app)
71
+ }
39
72
 
40
73
  }
41
74
 
package/admin/README.md DELETED
@@ -1 +0,0 @@
1
- admin middleware that exposes the local models, schemas etc
package/admin/index.js DELETED
@@ -1,43 +0,0 @@
1
- // /* @flow */
2
- // const fs = require("fs")
3
- // const path = require("path")
4
- // const glob = require("glob")
5
- // const debug = require("debug")
6
- //
7
- // const src_path = path.join(process.cwd(), "./src/")
8
- // const build_dir = path.join(process.cwd(), "build/")
9
- //
10
- //
11
- // const async_wrapper = fn => (req, res, next) => {
12
- // Promise.resolve(fn(req, res, next))
13
- // .catch(next)
14
- // }
15
- //
16
- //
17
- // const rpc_router = (app) => {
18
- // const rpc_routes = glob.sync(path.join(build_dir, "./rpc/*"))
19
- //
20
- // rpc_routes.forEach((file_path) => {
21
- // const basename = path.basename(file_path)
22
- // const route_path = basename.replace(/__slash__/g, "/")
23
- // const module_path = path.join(src_path, `${route_path}.js`)
24
- //
25
- // // if module not found, remove created route file
26
- // if (!fs.existsSync(module_path)) {
27
- // fs.unlinkSync(file_path)
28
- // console.log("removed inexistant rpc route", basename, "with module path", module_path)
29
- // }
30
- //
31
- // const rpc_fn = require(module_path)
32
- // app.post(`/rpc/${route_path}`, async_wrapper(async(req, res) => {
33
- //
34
- // console.log(`POST /rpc/${route_path}`)
35
- // // TODO: add jaegerclient / opentelemetry
36
- // const result = await rpc_fn(req.body, {req})
37
- // res.json(result)
38
- // }))
39
- // })
40
- //
41
- // }
42
- //
43
- // module.exports = rpc_router