@rpcbase/server 0.49.0 → 0.52.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
  }
@@ -14,44 +14,60 @@ if (typeof SESSION_STORE_PORT === "string" && !validator.isPort(SESSION_STORE_PO
14
14
  throw new Error("expected SESSION_STORE_PORT to be a valid port number")
15
15
  }
16
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")
17
+ let session_middleware = null
18
+
19
+ // extreme warning: when there is blocking io redis client won't connect
20
+ process.nextTick(async() => {
21
+
22
+ if (!SESSION_STORE_PORT) {
23
+ return
24
24
  }
25
- }
26
25
 
26
+ const reconnectStrategy = (retries) => {
27
+ console.log("redis_client::reconnectStrategy::retrying with arg", retries)
28
+ if (retries < 5) {
29
+ console.log("retry count:", retries, "retrying in 1s")
30
+ return 4000
31
+ } else {
32
+ return new Error("max retries expiered")
33
+ }
34
+ }
27
35
 
28
- const redis_client = createClient({
29
- socket: {
30
- host: "session-store",
31
- port: SESSION_STORE_PORT,
32
- reconnectStrategy
33
- },
34
- legacyMode: true,
35
- })
36
+ const redis_client = createClient({
37
+ socket: {
38
+ host: "127.0.0.1",
39
+ port: SESSION_STORE_PORT,
40
+ reconnectStrategy,
41
+ connectTimeout: 10000,
42
+ keepAlive: 0,
43
+ },
44
+ legacyMode: true,
45
+ })
36
46
 
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
+ console.log("redis session willconnect")
48
+ await redis_client.connect()
49
+ console.log("redis session connected")
50
+
51
+ session_middleware = session({
52
+ store: new redis_store({client: redis_client}),
53
+ proxy: true,
54
+ saveUninitialized: false,
55
+ // TODO: use session secret from env
56
+ secret: "session secret wowow",
57
+ resave: false,
58
+ })
47
59
 
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
60
  })
56
61
 
57
- module.exports = session_middleware
62
+
63
+ module.exports = (req, res, next) => {
64
+ if (!SESSION_STORE_PORT) {
65
+ return next()
66
+ }
67
+
68
+ if (session_middleware) {
69
+ session_middleware(req, res, next)
70
+ } else {
71
+ console.warn("session middleware not set yet")
72
+ }
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.49.0",
3
+ "version": "0.52.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
  }