@rpcbase/server 0.326.0 → 0.328.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/database.js +1 -2
- package/express/index.js +4 -0
- package/express/setup_handlers.js +46 -0
- package/package.json +1 -1
- package/src/api/stored-values/index.js +2 -4
- package/src/auth/index.js +7 -10
- package/src/client/client_router.js +2 -4
- package/src/rpc/rpc_router.js +4 -6
- package/src/helpers/async_wrapper.js +0 -8
- package/src/helpers/handler_wrapper.js +0 -11
package/database.js
CHANGED
|
@@ -47,8 +47,7 @@ const get_mongo_url = () => {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
module.exports = async(...database_names) => {
|
|
50
|
+
module.exports = async() => {
|
|
52
51
|
// set listeners before attempting to connect
|
|
53
52
|
mongoose.connection.on("connected", () => {
|
|
54
53
|
console.log("mongoose connected")
|
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,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.
|
|
9
|
-
app.
|
|
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.
|
|
18
|
-
app.
|
|
14
|
+
app.handler("/api/v1/auth/sign_in", sign_in)
|
|
15
|
+
app.handler("/api/v1/auth/sign_out", sign_out)
|
|
19
16
|
|
|
20
|
-
app.
|
|
21
|
-
app.
|
|
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.
|
|
20
|
+
app.handler("/api/v1/auth/check_session", check_session)
|
|
24
21
|
|
|
25
|
-
app.
|
|
26
|
-
app.
|
|
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("*",
|
|
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
|
|
package/src/rpc/rpc_router.js
CHANGED
|
@@ -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}`,
|
|
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/*",
|
|
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
|
|