@rpcbase/server 0.74.0 → 0.75.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/admin/README.md CHANGED
@@ -1 +1 @@
1
- admin middleware that exposes models, schemas etc
1
+ admin middleware that exposes the local models, schemas etc
package/bin.js CHANGED
@@ -13,8 +13,6 @@ const run_agent = require("./cli/run_agent")
13
13
 
14
14
  let is_command = false
15
15
 
16
- console.log("PACKVER", pack.version)
17
-
18
16
  const args = yargs(hideBin(process.argv))
19
17
  .command("start", "runs server/infrastructure", () => {}, (args) => {
20
18
  is_command = true
@@ -29,7 +27,6 @@ const args = yargs(hideBin(process.argv))
29
27
  build_server()
30
28
  })
31
29
  .option("verbose", {
32
- alias: "v",
33
30
  type: "boolean",
34
31
  default: false,
35
32
  description: "Run with verbose logging"
@@ -40,6 +37,8 @@ if (!is_command) {
40
37
  console.log("server default com22mand")
41
38
  }
42
39
 
40
+
41
+ // TODO: why was this needed
43
42
  // // add helpers to ensure proper process termination
44
43
  // const exit_clean = () => {
45
44
  // process.exit()
@@ -1,10 +1,10 @@
1
1
  /* @flow */
2
+ // TODO: replace console debug
2
3
 
3
- const is_production = process.env.NODE_ENV === "production"
4
4
 
5
+ const is_production = process.env.NODE_ENV === "production"
5
6
 
6
7
  module.exports = (app) => {
7
-
8
8
  app.post("/api/__dev_save_coverage", (req, res) => {
9
9
  console.log("sending coverage for", req.body.path_key)
10
10
  res.json(global.__coverage__)
package/express/index.js CHANGED
@@ -3,9 +3,13 @@ const cors = require("cors")
3
3
  const express = require("express")
4
4
  const body_parser = require("body-parser")
5
5
 
6
+ // functionality middlewares
7
+ const auth = require("../src/auth")
8
+
6
9
  const dev_save_coverage = require("./dev_save_coverage")
7
10
  const session_middleware = require("./session_middleware")
8
11
 
12
+
9
13
  const is_production = process.env.IS_PRODUCTION === "yes"
10
14
  const {APP_DOMAIN, CLIENT_PORT} = process.env
11
15
 
@@ -57,6 +61,7 @@ module.exports = () => {
57
61
  app.get("/api/ping", (req, res) => res.json({message: "pong"}))
58
62
  app.post("/api/ping", (req, res) => res.json({message: "pong"}))
59
63
 
64
+ auth(app)
60
65
  dev_save_coverage(app)
61
66
 
62
67
  return app
@@ -58,9 +58,14 @@ setTimeout(async() => {
58
58
  store: new redis_store({client: redis_client}),
59
59
  proxy: true,
60
60
  saveUninitialized: false,
61
+ // WARNING
61
62
  // TODO: use session secret from env
62
63
  secret: "session secret wowow",
63
64
  resave: false,
65
+ cookie: {
66
+ // TODO: test this
67
+ maxAge: 1000 * 3600 * 24 * 100 // 100 days
68
+ }
64
69
  })
65
70
 
66
71
  // TODO: is this still necessary
package/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  /* @flow */
2
- const client_router = require("./client/client_router")
3
2
  const database = require("./database")
4
3
  const express = require("./express")
5
- const rpc_router = require("./rpc/rpc_router")
4
+ const client_router = require("./src/client/client_router")
5
+ const rpc_router = require("./src/rpc/rpc_router")
6
6
 
7
7
  module.exports = {
8
- client_router,
9
8
  database,
10
9
  express,
10
+ client_router,
11
11
  rpc_router,
12
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.74.0",
3
+ "version": "0.75.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@rpcbase/agent": "0.9.0",
14
- "@rpcbase/std": "0.2.0",
14
+ "@rpcbase/std": "0.3.0",
15
15
  "body-parser": "1.20.0",
16
16
  "connect-redis": "6.1.3",
17
17
  "cors": "2.8.5",
@@ -0,0 +1,15 @@
1
+ /* @flow */
2
+
3
+ const check_session = async(payload, ctx) => {
4
+ const {req} = ctx
5
+ // console.log("session check", req.session)
6
+
7
+ const is_signed_in = !!req.session.user_id
8
+ return {
9
+ status: "ok",
10
+ is_signed_in,
11
+ user_id: req.session.user_id,
12
+ }
13
+ }
14
+
15
+ module.exports = check_session
@@ -0,0 +1,35 @@
1
+ /* @flow */
2
+ const async_wrapper = require("../helpers/async_wrapper")
3
+
4
+ const sign_up = require("./sign_up")
5
+ const sign_in = require("./sign_in")
6
+ const sign_out = require("./sign_out")
7
+ const check_session = require("./check_session")
8
+
9
+ const sign_up_handler = async(req, res) => {
10
+ const result = await sign_up(req.body, {req})
11
+ res.json(result)
12
+ }
13
+
14
+ const sign_in_handler = async(req, res) => {
15
+ const result = await sign_in(req.body, {req})
16
+ res.json(result)
17
+ }
18
+
19
+ const sign_out_handler = async(req, res) => {
20
+ const result = await sign_out(req.body, {req})
21
+ res.json(result)
22
+ }
23
+
24
+ const check_session_handler = async(req, res) => {
25
+ const result = await check_session(req.body, {req})
26
+ res.json(result)
27
+ }
28
+
29
+ module.exports = (app) => {
30
+ app.post("/api/v1/auth/sign_up", async_wrapper(sign_up_handler))
31
+ app.post("/api/v1/auth/sign_in", async_wrapper(sign_in_handler))
32
+ app.post("/api/v1/auth/sign_out", async_wrapper(sign_out_handler))
33
+
34
+ app.post("/api/v1/auth/check_session", async_wrapper(check_session_handler))
35
+ }
@@ -0,0 +1,11 @@
1
+ /* @flow */
2
+
3
+ const sign_out = async(payload, ctx) => {
4
+ const {req} = ctx
5
+ await req.session.destroy()
6
+ return {
7
+ status: "ok"
8
+ }
9
+ }
10
+
11
+ module.exports = sign_out
@@ -0,0 +1,2 @@
1
+ client router serves the client from the server
2
+ in production the client could be served from the CDN, but the server is also suitable
@@ -0,0 +1,8 @@
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
package/crypto/index.js DELETED
@@ -1,47 +0,0 @@
1
- /* @flow */
2
- const crypto = require("crypto")
3
-
4
- const ALGORITHM = "aes-256-cbc"
5
- const {CRYPTO_SECRET} = process.env
6
-
7
- if (!CRYPTO_SECRET || CRYPTO_SECRET.trim() === "") {
8
- throw new Error("CRYPTO_SECRET not found in env")
9
- }
10
-
11
- // TODO: move to rb:std
12
-
13
- const key = crypto.createHash("sha256")
14
- .update(String(CRYPTO_SECRET))
15
- .digest()
16
-
17
-
18
- const encrypt = (text) => {
19
- const iv = crypto.randomBytes(16)
20
- const cipher = crypto.createCipheriv(ALGORITHM, key, iv)
21
-
22
- const encrypted = Buffer.concat([
23
- cipher.update(text),
24
- cipher.final()
25
- ])
26
-
27
- return {
28
- iv: iv.toString("hex"),
29
- content: encrypted.toString("hex")
30
- }
31
- }
32
-
33
- const decrypt = (hash) => {
34
- const decipher = crypto.createDecipheriv(
35
- ALGORITHM, key, Buffer.from(hash.iv, "hex")
36
- )
37
-
38
- const decrpyted = Buffer.concat([
39
- decipher.update(Buffer.from(hash.content, "hex")),
40
- decipher.final(),
41
- ])
42
-
43
- return decrpyted.toString()
44
- }
45
-
46
-
47
- module.exports = {encrypt, decrypt}