@rpcbase/server 0.47.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 +5 -0
- package/cli/build_server.js +29 -0
- package/cli/run_docker.js +3 -1
- package/cli/run_native.js +0 -1
- package/cli/start_server.js +0 -1
- package/database.js +32 -19
- package/express/get_session_middleware.js +75 -0
- package/express/index.js +3 -3
- package/package.json +7 -7
- package/express/session_middleware.js +0 -57
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)
|
|
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
package/cli/start_server.js
CHANGED
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
|
|
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,33 +12,46 @@ if (typeof DATABASE_NAME !== "string") {
|
|
|
12
12
|
throw new Error("expected DATABASE_NAME to be a string")
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const
|
|
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
|
-
console.log("mongo_url", mongo_url)
|
|
24
18
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
keepAliveInitialDelay: 5000,
|
|
29
|
-
minPoolSize: 3,
|
|
30
|
-
serverSelectionTimeoutMS: 5000,
|
|
31
|
-
family: 4, // force ipv4
|
|
19
|
+
// set listeners before attempting to connect
|
|
20
|
+
mongoose.connection.on("connected", () => {
|
|
21
|
+
console.log("mongoose connected")
|
|
32
22
|
})
|
|
33
23
|
|
|
34
24
|
mongoose.connection.on("disconnected", (arg) => {
|
|
35
25
|
console.log("Mongoose disconnected", arg)
|
|
36
26
|
})
|
|
37
27
|
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
// TODO: should we be retrying in dev only
|
|
29
|
+
mongoose.connection.on("error", (err) => {
|
|
30
|
+
// replicaSet has no primary, retry connecting in a moment
|
|
31
|
+
console.log("mongoose connection error", err)
|
|
40
32
|
})
|
|
41
33
|
|
|
42
|
-
|
|
34
|
+
|
|
35
|
+
console.log("connect mongo_url", mongo_url)
|
|
36
|
+
|
|
37
|
+
const connect_opts = {
|
|
38
|
+
keepAlive: true,
|
|
39
|
+
keepAliveInitialDelay: 5000,
|
|
40
|
+
minPoolSize: 5,
|
|
41
|
+
family: 4, // force ipv4
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// INFO: on reconnects
|
|
45
|
+
// https://jira.mongodb.org/browse/NODE-834?focusedCommentId=1412582&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-1412582
|
|
46
|
+
// The driver will fail on first connect if it cannot connect to the host. This is by design to ensure quick failure on unreachable hosts. Reconnect behavior only kicks in once the driver has performed the initial connect.
|
|
47
|
+
try {
|
|
48
|
+
await mongoose.connect(mongo_url, connect_opts)
|
|
49
|
+
} catch (err) {
|
|
50
|
+
// TODO: handle error ?
|
|
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)
|
|
55
|
+
}
|
|
43
56
|
|
|
44
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
|
|
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,7 +50,7 @@ module.exports = () => {
|
|
|
50
50
|
credentials: true // enable set-cookie
|
|
51
51
|
}))
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
const session_middleware = await get_session_middleware()
|
|
54
54
|
app.use(session_middleware)
|
|
55
55
|
|
|
56
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.
|
|
3
|
+
"version": "0.50.0",
|
|
4
4
|
"license": "SSPL-1.0",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"bin": {
|
|
@@ -10,18 +10,18 @@
|
|
|
10
10
|
"test": "echo \"Error: no test specified\" && exit 0"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"body-parser": "1.
|
|
13
|
+
"body-parser": "1.20.0",
|
|
14
14
|
"connect-redis": "6.1.3",
|
|
15
15
|
"cors": "2.8.5",
|
|
16
16
|
"debug": "4.3.4",
|
|
17
17
|
"dotenv": "16.0.0",
|
|
18
|
-
"express": "4.
|
|
18
|
+
"express": "4.18.1",
|
|
19
19
|
"express-session": "1.17.2",
|
|
20
|
-
"glob": "
|
|
21
|
-
"mongoose": "6.2
|
|
20
|
+
"glob": "8.0.1",
|
|
21
|
+
"mongoose": "6.3.2",
|
|
22
22
|
"postmark": "3.0.1",
|
|
23
|
-
"redis": "4.0
|
|
23
|
+
"redis": "4.1.0",
|
|
24
24
|
"validator": "13.7.0",
|
|
25
|
-
"yargs": "17.4.
|
|
25
|
+
"yargs": "17.4.1"
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -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
|