@rpcbase/server 0.49.0 → 0.50.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/bin.js CHANGED
@@ -7,6 +7,7 @@ const yargs = require("yargs/yargs")
7
7
  const {hideBin} = require("yargs/helpers")
8
8
 
9
9
  const start_server = require("./cli/start_server")
10
+ const build_server = require("./cli/build_server")
10
11
 
11
12
  let is_command = false
12
13
 
@@ -15,6 +16,10 @@ const args = yargs(hideBin(process.argv))
15
16
  is_command = true
16
17
  start_server()
17
18
  })
19
+ .command("build", "build server", () => {}, (argv) => {
20
+ is_command = true
21
+ build_server()
22
+ })
18
23
  .command("ping", "ping", () => {}, (argv) => {
19
24
  is_command = true
20
25
  console.log("PING")
@@ -0,0 +1,29 @@
1
+ /* @flow */
2
+ const path = require("path")
3
+ const fs = require("fs")
4
+
5
+ const get_runtime = require("./get_runtime")
6
+ const run_docker = require("./run_docker")
7
+ const run_native = require("./run_native")
8
+
9
+
10
+ const build_server = async() => {
11
+ // const build_schemas =
12
+ // const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
13
+ //
14
+ // // prefix
15
+ // const proj_parent_dir = path.join(infrastructure_dir, "../../")
16
+ // const proj_prefix = path.basename(proj_parent_dir)
17
+ //
18
+ // const runtime = get_runtime()
19
+ //
20
+ // if (runtime === "native") {
21
+ // run_native(infrastructure_dir, proj_prefix)
22
+ // } else {
23
+ // run_docker(infrastructure_dir, proj_prefix)
24
+ // }
25
+ console.log("WOW run build server")
26
+
27
+ }
28
+
29
+ module.exports = build_server
package/cli/run_docker.js CHANGED
@@ -10,7 +10,9 @@ const run_docker = async(infrastructure_dir, proj_prefix) => {
10
10
 
11
11
  // compose files
12
12
  const compose_files = ["-f common.yml"]
13
- if (is_production) compose_files.push("-f prod.yml")
13
+ if (is_production)) {
14
+ compose_files.push("-f prod.yml")
15
+ }
14
16
  else compose_files.push("-f dev.yml")
15
17
 
16
18
  const opts = ["--build", "--remove-orphans"]
package/cli/run_native.js CHANGED
@@ -26,7 +26,6 @@ const get_env = () => {
26
26
  const env_path = path.join(process.cwd(), ".env")
27
27
  const env_buf = fs.readFileSync(env_path)
28
28
  const env = dotenv.parse(env_buf)
29
-
30
29
  return env
31
30
  }
32
31
 
@@ -11,7 +11,6 @@ const run_native = require("./run_native")
11
11
  // docker network create local-network
12
12
 
13
13
  const start_server = async() => {
14
-
15
14
  const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
16
15
 
17
16
  // prefix
package/database.js CHANGED
@@ -2,9 +2,9 @@
2
2
  const mongoose = require("mongoose")
3
3
  const validator = require("validator")
4
4
 
5
- const {DATABASE_NAME} = process.env
5
+ const {DATABASE_NAME, DATABASE_PORT} = process.env
6
6
 
7
- if (typeof DATABASE_PORT === "string" && !validator.isPort(DATABASE_PORT)) {
7
+ if (typeof DATABASE_PORT !== "string" || !validator.isPort(DATABASE_PORT)) {
8
8
  throw new Error("expected DATABASE_PORT to be a valid port number")
9
9
  }
10
10
 
@@ -12,12 +12,7 @@ if (typeof DATABASE_NAME !== "string") {
12
12
  throw new Error("expected DATABASE_NAME to be a string")
13
13
  }
14
14
 
15
- const replicaset_str = [0, 1, 2].map((i) => `database${i}:${process.env["DATABASE_PORT_" + i]}`)
16
- .join(",")
17
-
18
- const mongo_url = `mongodb://${replicaset_str}/${DATABASE_NAME}`
19
- // &readPreference=secondary
20
-
15
+ const mongo_url = `mongodb://127.0.0.1:${DATABASE_PORT}/${DATABASE_NAME}`
21
16
 
22
17
  module.exports = async() => {
23
18
 
@@ -32,29 +27,17 @@ module.exports = async() => {
32
27
 
33
28
  // TODO: should we be retrying in dev only
34
29
  mongoose.connection.on("error", (err) => {
35
- console.log("Mongoose connection error", /*err*/)
36
30
  // replicaSet has no primary, retry connecting in a moment
37
- if (err.reason.type === "ReplicaSetNoPrimary") {
38
- console.log("no primary, will retry")
39
- setTimeout(async() => {
40
- console.log("retrying now")
41
- await mongoose.connect(mongo_url, connect_opts)
42
- }, 3000)
43
- } else {
44
- console.log("WOW ici mongoose connection error", err)
45
- }
31
+ console.log("mongoose connection error", err)
46
32
  })
47
33
 
48
34
 
49
- console.log("mongo_url", mongo_url)
35
+ console.log("connect mongo_url", mongo_url)
50
36
 
51
37
  const connect_opts = {
52
- replicaSet: "rs0",
53
38
  keepAlive: true,
54
39
  keepAliveInitialDelay: 5000,
55
- minPoolSize: 3,
56
- serverSelectionTimeoutMS: 5000,
57
- heartbeatFrequencyMS: 1000,
40
+ minPoolSize: 5,
58
41
  family: 4, // force ipv4
59
42
  }
60
43
 
@@ -65,18 +48,10 @@ module.exports = async() => {
65
48
  await mongoose.connect(mongo_url, connect_opts)
66
49
  } catch (err) {
67
50
  // TODO: handle error ?
68
- if (err.reason.type === "ReplicaSetNoPrimary") {
69
- // console.log("no primary, will retry")
70
- // setTimeout(async() => {
71
- // console.log("retrying now")
72
- // await mongoose.connect(mongo_url, connect_opts)
73
- // }, 3000)
74
- } else {
75
- console.log("ICI SERVER GOT ERROR")
76
- console.log(err)
77
- console.log("JSON:", JSON.stringify(err, null, 2))
78
- console.log("resaon", err.reason)
79
- }
51
+ console.log("mongoose connect SERVER GOT ERROR")
52
+ console.log(err)
53
+ console.log("JSON:", JSON.stringify(err, null, 2))
54
+ console.log("resaon", err.reason)
80
55
  }
81
56
 
82
57
  }
@@ -0,0 +1,75 @@
1
+ /* @flow */
2
+ const session = require("express-session")
3
+ const validator = require("validator")
4
+ const {createClient} = require("redis")
5
+ const redis_store = require("connect-redis")(session)
6
+
7
+ // WARNING:
8
+ // https://stackoverflow.com/questions/70867229/error-connection-timeout-when-connecting-to-redis-docker-instance
9
+ // https://github.com/redis/node-redis/issues/1656/
10
+
11
+ const {SESSION_STORE_PORT, SESSION_STORE_HOST} = process.env
12
+
13
+
14
+ // extreme warning: when there is blocking io redis client won't connect
15
+ // process.nextTick(() => {
16
+ // redis_client.connect()
17
+ // .catch((err) => {
18
+ // console.log("session store error ohh", err)
19
+ // })
20
+ // .then(() => {
21
+ // console.log("redis session-store connected port:", SESSION_STORE_PORT)
22
+ // })
23
+ // })
24
+
25
+
26
+ module.exports = () => new Promise((resolve, reject) => {
27
+
28
+ if (!SESSION_STORE_PORT) {
29
+ return resolve((req, res, next) => next())
30
+ }
31
+
32
+ if (typeof SESSION_STORE_PORT === "string" && !validator.isPort(SESSION_STORE_PORT)) {
33
+ throw new Error("expected SESSION_STORE_PORT to be a valid port number")
34
+ }
35
+
36
+ setTimeout(async() => {
37
+ const reconnectStrategy = (retries) => {
38
+ console.log("redis_client::reconnectStrategy::retrying with arg", retries)
39
+ if (retries < 5) {
40
+ console.log("retry count:", retries, "retrying in 1s")
41
+ return 4000
42
+ } else {
43
+ return new Error("max retries expiered")
44
+ }
45
+ }
46
+
47
+ const redis_client = createClient({
48
+ socket: {
49
+ host: SESSION_STORE_HOST,
50
+ port: SESSION_STORE_PORT,
51
+ reconnectStrategy,
52
+ connectTimeout: 10000,
53
+ keepAlive: 0,
54
+ },
55
+ legacyMode: true,
56
+ })
57
+
58
+ console.log("redis session willconnect")
59
+ await redis_client.connect()
60
+ console.log("redis session connected")
61
+
62
+ const session_middleware = session({
63
+ store: new redis_store({client: redis_client}),
64
+ proxy: true,
65
+ saveUninitialized: false,
66
+ // TODO: use session secret from env
67
+ secret: "session secret wowow",
68
+ resave: false,
69
+ })
70
+
71
+ resolve(session_middleware)
72
+
73
+ }, 100)
74
+
75
+ })
package/express/index.js CHANGED
@@ -3,13 +3,13 @@ const cors = require("cors")
3
3
  const express = require("express")
4
4
  const body_parser = require("body-parser")
5
5
 
6
- const session_middleware = require("./session_middleware")
6
+ const get_session_middleware = require("./get_session_middleware")
7
7
 
8
8
  const is_production = process.env.IS_PRODUCTION === "yes"
9
9
  const {APP_DOMAIN, CLIENT_PORT} = process.env
10
10
 
11
11
 
12
- module.exports = () => {
12
+ module.exports = async() => {
13
13
  const app = express()
14
14
  app.use(body_parser.json({limit: "2mb"}))
15
15
 
@@ -50,6 +50,7 @@ module.exports = () => {
50
50
  credentials: true // enable set-cookie
51
51
  }))
52
52
 
53
+ const session_middleware = await get_session_middleware()
53
54
  app.use(session_middleware)
54
55
 
55
56
  app.get("/api/ping", (req, res) => res.json({message: "pong"}))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.49.0",
3
+ "version": "0.50.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -15,12 +15,12 @@
15
15
  "cors": "2.8.5",
16
16
  "debug": "4.3.4",
17
17
  "dotenv": "16.0.0",
18
- "express": "4.17.3",
18
+ "express": "4.18.1",
19
19
  "express-session": "1.17.2",
20
20
  "glob": "8.0.1",
21
- "mongoose": "6.3.1",
21
+ "mongoose": "6.3.2",
22
22
  "postmark": "3.0.1",
23
- "redis": "4.0.6",
23
+ "redis": "4.1.0",
24
24
  "validator": "13.7.0",
25
25
  "yargs": "17.4.1"
26
26
  }
@@ -1,57 +0,0 @@
1
- /* @flow */
2
- const session = require("express-session")
3
- const validator = require("validator")
4
- const {createClient} = require("redis")
5
- const redis_store = require("connect-redis")(session)
6
-
7
- // WARNING:
8
- // https://stackoverflow.com/questions/70867229/error-connection-timeout-when-connecting-to-redis-docker-instance
9
- // https://github.com/redis/node-redis/issues/1656/
10
-
11
- const {SESSION_STORE_PORT} = process.env
12
-
13
- if (typeof SESSION_STORE_PORT === "string" && !validator.isPort(SESSION_STORE_PORT)) {
14
- throw new Error("expected SESSION_STORE_PORT to be a valid port number")
15
- }
16
-
17
- const reconnectStrategy = (retries) => {
18
- console.log("reconnectStrategy::retrying with arg", retries)
19
- if (retries < 5) {
20
- console.log("retry count:", retries, "retrying in 1s")
21
- return 1000
22
- } else {
23
- return new Error("max retries expiered")
24
- }
25
- }
26
-
27
-
28
- const redis_client = createClient({
29
- socket: {
30
- host: "session-store",
31
- port: SESSION_STORE_PORT,
32
- reconnectStrategy
33
- },
34
- legacyMode: true,
35
- })
36
-
37
- // extreme warning: when there is blocking io redis client won't connect
38
- process.nextTick(() => {
39
- redis_client.connect()
40
- .catch((err) => {
41
- console.log("session store error ohh", err)
42
- })
43
- .then(() => {
44
- console.log("redis session-store connected port:", SESSION_STORE_PORT)
45
- })
46
- })
47
-
48
- const session_middleware = session({
49
- store: new redis_store({client: redis_client}),
50
- proxy: true,
51
- saveUninitialized: false,
52
- // TODO: use session secret from env
53
- secret: "session secret wowow",
54
- resave: false,
55
- })
56
-
57
- module.exports = session_middleware