@rpcbase/server 0.325.0 → 0.327.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.
@@ -0,0 +1,23 @@
1
+ /* @flow */
2
+ const glob = require("glob")
3
+ const path = require("path")
4
+
5
+
6
+ const is_pascal_case = (str) => /^[A-Z][a-zA-Z0-9]*$/.test(str)
7
+
8
+
9
+ const load_models = () => {
10
+ const wd = process.cwd()
11
+
12
+ const models_dir = path.join(wd, "./src/models")
13
+
14
+ const files = glob.sync(path.join(models_dir, "./**/*.js"))
15
+
16
+ files
17
+ .filter((f) => !path.dirname(f).endsWith("/helpers"))
18
+ .filter((f) => is_pascal_case(path.basename(f, ".js")))
19
+ .forEach((f) => require(f))
20
+
21
+ }
22
+
23
+ load_models()
package/boot/shared.js CHANGED
@@ -6,6 +6,8 @@ require("../extend-expect")
6
6
  const mongoose = require("../mongoose")
7
7
  require("../src/access-control")(mongoose)
8
8
 
9
+ require("./load_models")
10
+
9
11
  const {database, firebase} = require("../")
10
12
 
11
13
  database()
package/express/index.js CHANGED
@@ -12,6 +12,7 @@ const sessions = require("../src/sessions")
12
12
 
13
13
  const dev_save_coverage = require("./dev_save_coverage")
14
14
  const custom_cors = require("./custom_cors")
15
+ const setup_handlers = require("./setup_handlers")
15
16
 
16
17
 
17
18
  const log = debug("rb:server")
@@ -33,6 +34,9 @@ module.exports = () => {
33
34
 
34
35
  const app = express()
35
36
 
37
+ setup_handlers(app)
38
+
39
+
36
40
  if (has_sentry && is_production) {
37
41
  Sentry.init({
38
42
  dsn: SENTRY_DSN,
@@ -0,0 +1,46 @@
1
+ /* @flow */
2
+
3
+ const setup_handlers = (app) => {
4
+ const original_get = app.get.bind(app)
5
+ const original_post = app.post.bind(app)
6
+
7
+ // function to sequentially execute handlers
8
+ function apply_handlers(handlers, req, res, next) {
9
+ const handler = handlers.shift()
10
+ if (handler) {
11
+ Promise.resolve(handler(req, res, function(err) {
12
+ if (err) {
13
+ return next(err)
14
+ }
15
+ apply_handlers(handlers, req, res, next)
16
+ })).catch(next)
17
+ } else {
18
+ next()
19
+ }
20
+ }
21
+
22
+ // app.get
23
+ app.get = function (path, ...handlers) {
24
+ original_get(path, (req, res, next) => apply_handlers([...handlers], req, res, next))
25
+ }
26
+
27
+ // app.post
28
+ app.post = function (path, ...handlers) {
29
+ original_post(path, (req, res, next) => apply_handlers([...handlers], req, res, next))
30
+ }
31
+
32
+ // app.handler
33
+ app.handler = (path, fn) => {
34
+ app.post(path, async (req, res, next) => {
35
+ try {
36
+ const result = await fn(req.body, {req, res})
37
+ res.json(result)
38
+ } catch (err) {
39
+ next(err)
40
+ }
41
+ })
42
+ }
43
+
44
+ }
45
+
46
+ module.exports = setup_handlers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.325.0",
3
+ "version": "0.327.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -1,10 +1,8 @@
1
1
  /* @flow */
2
- const handler_wrapper = require("../../helpers/handler_wrapper")
3
-
4
2
  const get_stored_values = require("./get_stored_values")
5
3
  const set_stored_values = require("./set_stored_values")
6
4
 
7
5
  module.exports = (app) => {
8
- app.post("/api/v1/stored-values/get", handler_wrapper(get_stored_values))
9
- app.post("/api/v1/stored-values/set", handler_wrapper(set_stored_values))
6
+ app.handler("/api/v1/stored-values/get", get_stored_values)
7
+ app.handler("/api/v1/stored-values/set", set_stored_values)
10
8
  }
package/src/auth/index.js CHANGED
@@ -1,7 +1,4 @@
1
1
  /* @flow */
2
- const async_wrapper = require("../helpers/async_wrapper")
3
- const handler_wrapper = require("../helpers/handler_wrapper")
4
-
5
2
  const sign_up = require("./sign_up")
6
3
  const sign_in = require("./sign_in")
7
4
  const sign_out = require("./sign_out")
@@ -14,14 +11,14 @@ const get_account = require("./get_account")
14
11
 
15
12
 
16
13
  module.exports = (app) => {
17
- app.post("/api/v1/auth/sign_in", handler_wrapper(sign_in))
18
- app.post("/api/v1/auth/sign_out", handler_wrapper(sign_out))
14
+ app.handler("/api/v1/auth/sign_in", sign_in)
15
+ app.handler("/api/v1/auth/sign_out", sign_out)
19
16
 
20
- app.post("/api/v1/auth/reset_password", handler_wrapper(reset_password))
21
- app.post("/api/v1/auth/set_new_password", handler_wrapper(set_new_password))
17
+ app.handler("/api/v1/auth/reset_password", reset_password)
18
+ app.handler("/api/v1/auth/set_new_password", set_new_password)
22
19
 
23
- app.post("/api/v1/auth/check_session", handler_wrapper(check_session))
20
+ app.handler("/api/v1/auth/check_session", check_session)
24
21
 
25
- app.post("/api/v1/auth/get_account", handler_wrapper(get_account))
26
- app.post("/api/v1/auth/get_accounts", handler_wrapper(get_accounts))
22
+ app.handler("/api/v1/auth/get_account", get_account)
23
+ app.handler("/api/v1/auth/get_accounts", get_accounts)
27
24
  }
@@ -5,8 +5,6 @@ const path = require("path")
5
5
  const glob = require("glob")
6
6
  const Sentry = require("@sentry/node")
7
7
 
8
- const async_wrapper = require("../helpers/async_wrapper")
9
-
10
8
 
11
9
  const MIME_TYPES = {
12
10
  ".css": "text/css",
@@ -64,7 +62,7 @@ const client_router = (app) => {
64
62
  if (fs.existsSync(index_file_path)) {
65
63
  const index_file_buffer = fs.readFileSync(index_file_path)
66
64
 
67
- app.get("*", async_wrapper(async(req, res, next) => {
65
+ app.get("*", async(req, res, next) => {
68
66
  const full_path = req.baseUrl + req.path
69
67
 
70
68
  // TODO: this shouldn't be here,
@@ -83,7 +81,7 @@ const client_router = (app) => {
83
81
  })
84
82
  res.end(index_file_buffer)
85
83
  }
86
- }))
84
+ })
87
85
  }
88
86
  // register final error handlers
89
87
 
@@ -4,8 +4,6 @@ const path = require("path")
4
4
  const glob = require("glob")
5
5
  const debug = require("debug")
6
6
 
7
- const async_wrapper = require("../helpers/async_wrapper")
8
-
9
7
 
10
8
  const log = debug("rb:rpc")
11
9
 
@@ -32,14 +30,14 @@ const setup_prod_rpc_routes = (app) => {
32
30
  }
33
31
 
34
32
  const rpc_fn = require(module_path)
35
- app.post(`/rpc/${route_path}`, async_wrapper(async(req, res) => {
33
+ app.post(`/rpc/${route_path}`, async(req, res) => {
36
34
 
37
35
  log(`POST /rpc/${route_path}`)
38
36
 
39
37
  // TODO: add jaegerclient / opentelemetry
40
38
  const result = await rpc_fn(req.body, {req, res})
41
39
  res.json(result)
42
- }))
40
+ })
43
41
  })
44
42
  }
45
43
 
@@ -47,7 +45,7 @@ const setup_prod_rpc_routes = (app) => {
47
45
  const setup_dev_rpc_routes = (app) => {
48
46
  const rpc_routes = glob.sync(path.join(client_dir, "./build/rpc/*"))
49
47
 
50
- app.post("/rpc/*", async_wrapper(async(req, res) => {
48
+ app.post("/rpc/*", async(req, res) => {
51
49
  const route_path = req.params[0]
52
50
 
53
51
  const module_path = path.join(server_src_dir, `${route_path}.js`)
@@ -58,7 +56,7 @@ const setup_dev_rpc_routes = (app) => {
58
56
  // TODO: add jaegerclient / opentelemetry
59
57
  const result = await rpc_fn(req.body, {req, res})
60
58
  res.json(result)
61
- }))
59
+ })
62
60
  }
63
61
 
64
62
 
@@ -1,8 +0,0 @@
1
- /* @flow */
2
-
3
- const async_wrapper = fn => (req, res, next) => {
4
- Promise.resolve(fn(req, res, next))
5
- .catch(next)
6
- }
7
-
8
- module.exports = async_wrapper
@@ -1,11 +0,0 @@
1
- /* @flow */
2
- const async_wrapper = require("./async_wrapper")
3
-
4
-
5
- const handler_wrapper = (fn) =>
6
- async_wrapper(async(req, res) => {
7
- const result = await fn(req.body, {req, res})
8
- res.json(result)
9
- })
10
-
11
- module.exports = handler_wrapper