@rpcbase/server 0.337.0 → 0.339.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/boot/server.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // MUST COME FIRST!
3
3
  require("./shared")
4
4
  //
5
- const rb_server_plugin = require("rb-plugin-server")
5
+ const rb_server_plugin = require("/rb-plugin-server")
6
6
 
7
7
  const http = require("http")
8
8
 
package/boot/worker.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* @flow */
2
2
  require("./shared")
3
3
 
4
- require("rb-plugin-worker")
4
+ require("/rb-plugin-worker")
5
5
 
6
6
  const register_queue_listener = require("../queue/register_queue_listener")
7
7
 
package/package.json CHANGED
@@ -1,11 +1,8 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.337.0",
3
+ "version": "0.339.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
- "bin": {
7
- "rb-server": "./bin.js"
8
- },
9
6
  "scripts": {
10
7
  "test": "../../node_modules/.bin/wireit"
11
8
  },
@@ -81,14 +78,12 @@
81
78
  "lodash": "4.17.21",
82
79
  "mkdirp": "2.1.3",
83
80
  "mongoose": "8.1.0",
84
- "nodemon": "3.0.3",
85
81
  "picocolors": "1.0.0",
86
82
  "postmark": "4.0.2",
87
83
  "redis": "4.6.12",
88
84
  "request-ip": "3.3.0",
89
85
  "sift": "17.0.1",
90
86
  "socket.io": "4.7.4",
91
- "validator": "13.11.0",
92
- "yargs": "17.7.2"
87
+ "validator": "13.11.0"
93
88
  }
94
89
  }
package/bin.js DELETED
@@ -1,80 +0,0 @@
1
- #!/usr/bin/env node
2
- /* @flow */
3
- process.title = "rb-cli-server"
4
- const path = require("path")
5
- require("dotenv").config({path: path.join(__dirname, "./.env")})
6
-
7
- const yargs = require("yargs/yargs")
8
- const {hideBin} = require("yargs/helpers")
9
-
10
- const pack = require("./package.json")
11
- const start_server_infrastructure = require("./cli/start_server_infrastructure")
12
- const run_dev_server = require("./cli/run_dev_server")
13
- const run_dev_worker = require("./cli/run_dev_worker")
14
- const build_server = require("./cli/build_server")
15
- const check_args = require("./cli/check_args")
16
- const run_agent = require("./cli/run_agent")
17
-
18
- let is_command = false
19
-
20
-
21
- const args = yargs(hideBin(process.argv))
22
- .option("verbose", {
23
- type: "boolean",
24
- default: false,
25
- description: "Run with verbose logging"
26
- })
27
- .option("sessions", {
28
- type: "boolean",
29
- default: false,
30
- description: "Run with the sessions store enabled"
31
- })
32
- .option("session", {
33
- hidden: true,
34
- })
35
- .command("start", "runs server/infrastructure", (yargs) => {
36
- yargs
37
- .option("docker", {
38
- type: "boolean",
39
- default: false,
40
- description: "Run in docker"
41
- })
42
- }, (args) => {
43
- is_command = true
44
- check_args(args)
45
- start_server_infrastructure(args)
46
- })
47
- .command("dev-server", "runs the web server in dev mode", () => {}, (args) => {
48
- is_command = true
49
- run_dev_server(args)
50
- })
51
- .command("dev-worker", "runs the worker in dev mode", () => {}, (args) => {
52
- is_command = true
53
- run_dev_worker(args)
54
- })
55
- .command("agent", "run the agent", () => {}, (args) => {
56
- is_command = true
57
- run_agent()
58
- })
59
- .command("build", "Build the server package", () => {}, (args) => {
60
- is_command = true
61
- build_server()
62
- })
63
- .parse()
64
-
65
-
66
- if (!is_command) {
67
- console.log("server default command exiting.")
68
- process.exit(0)
69
- }
70
-
71
-
72
- // TODO: why was this needed
73
- // -> was this for when running in docker ? I think yes
74
-
75
- // // add helpers to ensure proper process termination
76
- // const exit_clean = () => {
77
- // process.exit()
78
- // }
79
- // process.on("SIGINT", exit_clean) // ctrl-c
80
- // process.on("SIGTERM", exit_clean) // kill
@@ -1,27 +0,0 @@
1
- /* @flow */
2
- const path = require("path")
3
- const fs = require("fs")
4
-
5
- const run_docker = require("./run_docker")
6
- const run_native = require("./run_native")
7
-
8
-
9
- const build_server = async() => {
10
- // const build_schemas =
11
- // const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
12
- //
13
- // prefix
14
- // const proj_parent_dir = path.join(infrastructure_dir, "../../")
15
- // const proj_prefix = path.basename(proj_parent_dir)
16
- //
17
- //
18
- // if (runtime === "native") {
19
- // run_native(infrastructure_dir, proj_prefix)
20
- // } else {
21
- // run_docker(infrastructure_dir, proj_prefix)
22
- // }
23
- console.log("WOW run build server")
24
-
25
- }
26
-
27
- module.exports = build_server
package/cli/check_args.js DELETED
@@ -1,29 +0,0 @@
1
- /* @flow */
2
-
3
- const check_args = (args) => {
4
- if (typeof args.session !== "undefined") {
5
- throw new Error("Did you mean --sessions?")
6
- }
7
-
8
- try {
9
- const {npm_config_argv} = process.env
10
-
11
- // if not started from npm (ie: docker) this var is not set, we assume args are correct
12
- if (!npm_config_argv) {
13
- console.warn("no npm_config_argv skipping arg check")
14
- return
15
- }
16
-
17
- const raw_args = JSON.parse(npm_config_argv).original
18
- if (raw_args.indexOf("--no-session") > -1) {
19
- throw new Error("Did you mean --no-sessions?")
20
- }
21
- } catch (err) {
22
- console.log("ERROR CHECKING ARGS", raw_args)
23
- }
24
-
25
- return true
26
- }
27
-
28
-
29
- module.exports = check_args
@@ -1,51 +0,0 @@
1
- /* @flow */
2
- const picocolors = require("picocolors")
3
- const nodemon = require("nodemon")
4
-
5
-
6
- const MAX_RECENT_CRASHES = 16
7
- const CRASH_COOLDOWN_INTERVAL = 10000
8
- const CRASH_RESTART_DELAY = 1000
9
-
10
-
11
- const get_dev_watcher = ({script, nodeArgs = [], ...opts}) => {
12
-
13
- let crash_count = 0
14
-
15
- const instance = nodemon({
16
- script,
17
- nodeArgs: [...nodeArgs, "--stack-trace-limit=64"],
18
- ...opts,
19
- })
20
-
21
- instance.on("start", () => {
22
- console.log(picocolors.green(`${script} has started`))
23
- }).on("quit", () => {
24
- console.log(picocolors.green(`${script} has quit`))
25
- process.exit()
26
- }).on("restart", (files) => {
27
- console.log(picocolors.green(`${script} restarted due to: ${files?.length} changes`))
28
- }).on("crash", () => {
29
- crash_count++
30
-
31
- if (crash_count > MAX_RECENT_CRASHES) {
32
- return
33
- }
34
-
35
- console.log(picocolors.red(`${script} crashed, will restart`))
36
-
37
- setTimeout(() => {
38
- instance.emit("restart")
39
- }, CRASH_RESTART_DELAY)
40
- })
41
-
42
- setInterval(() => {
43
- if (crash_count > 0) {
44
- crash_count--
45
- }
46
- }, CRASH_COOLDOWN_INTERVAL)
47
-
48
- return instance
49
- }
50
-
51
- module.exports = get_dev_watcher
package/cli/run_agent.js DELETED
@@ -1,17 +0,0 @@
1
- /* @flow */
2
- const path = require("path")
3
- const fs = require("fs")
4
-
5
- const {run} = require("@rpcbase/agent")
6
-
7
- const run_agent = async() => {
8
- process.title = "rb-agent"
9
-
10
- const cwd = process.cwd()
11
- const proj_parent_dir = path.join(cwd, "../../")
12
- const proj_prefix = path.basename(proj_parent_dir)
13
-
14
- run(proj_prefix)
15
- }
16
-
17
- module.exports = run_agent
@@ -1,17 +0,0 @@
1
- /* @flow */
2
- const get_dev_watcher = require("./get_dev_watcher")
3
-
4
- const run_dev_server = (args) => {
5
-
6
- const watcher = get_dev_watcher({
7
- script: "./build/server.js",
8
- ignore: [],
9
- watch: [
10
- "./build/server.js",
11
- ],
12
- delay: "500ms"
13
- })
14
-
15
- }
16
-
17
- module.exports = run_dev_server
@@ -1,17 +0,0 @@
1
- /* @flow */
2
- const get_dev_watcher = require("./get_dev_watcher")
3
-
4
- const run_dev_worker = (args) => {
5
-
6
- const watcher = get_dev_watcher({
7
- script: "./build/worker.js",
8
- ignore: [],
9
- watch: [
10
- "./build/worker.js",
11
- ],
12
- delay: "500ms"
13
- })
14
-
15
- }
16
-
17
- module.exports = run_dev_worker
package/cli/run_docker.js DELETED
@@ -1,73 +0,0 @@
1
- /* @flow */
2
- const {execSync} = require("child_process")
3
- const path = require("path")
4
- const fs = require("fs")
5
- const debug = require("debug")
6
-
7
-
8
- const log = debug("docker")
9
-
10
- const is_production = process.env.NODE_ENV === "production"
11
- const is_ci = process.env.CI === "true"
12
-
13
- const run_docker = async(infrastructure_dir, proj_prefix, args) => {
14
- // env
15
- const env_path = path.relative(infrastructure_dir, path.join(process.cwd(), "./.env"))
16
-
17
- const exists = (f) => {
18
- const target_path = path.join(infrastructure_dir, f)
19
- return fs.existsSync(target_path)
20
- }
21
-
22
- // compose files
23
- const compose_files = ["-f", "common.yml"]
24
- // TODO: which files are loaded and when should be documented
25
- // when running with native mode, do not load native infrastructure
26
- if (is_production && exists("prod.yml")) {
27
- compose_files.push("-f", "prod.yml")
28
- }
29
- if (!is_production && exists("dev.yml")) {
30
- compose_files.push("-f", "dev.yml")
31
- }
32
-
33
- if (is_ci) {
34
- console.log("CI==true, will skip docker build")
35
- }
36
-
37
- const opts = [
38
- is_ci ? "--no-build" : "--build",
39
- is_ci ? "--no-recreate" : "--force-recreate",
40
- /*"--quiet-pull",*/
41
- "--remove-orphans",
42
- !is_production && "--abort-on-container-exit",
43
- ].filter(Boolean)
44
-
45
-
46
- const cmd = "docker compose"
47
- const cmd_args = ["--env-file", env_path, "-p", proj_prefix, ...compose_files, "up", ...opts]
48
-
49
- console.log("CMD", cmd_args.join(" "))
50
-
51
- const ps_env = {
52
- ...process.env,
53
- }
54
-
55
- if (args.sessions) {
56
- ps_env.RB_SESSION_STORE = "yes"
57
- }
58
-
59
- const exec_cmd = `${cmd} ${cmd_args.join(" ")}`
60
-
61
- log("CMD", exec_cmd)
62
-
63
- // WARNING: blocks the process
64
- // this is the only way to have docker-compose shut down gracefully
65
- execSync(exec_cmd, {
66
- cwd: infrastructure_dir,
67
- stdio: "inherit",
68
- env: ps_env
69
- })
70
-
71
- }
72
-
73
- module.exports = run_docker
package/cli/run_native.js DELETED
@@ -1,217 +0,0 @@
1
- /* @flow */
2
- const assert = require("assert")
3
- const {spawn} = require("child_process")
4
- const path = require("path")
5
- const fs = require("fs")
6
-
7
- const colors = require("picocolors")
8
- const validator = require("validator")
9
- const dotenv = require("dotenv")
10
- const mkdirp = require("mkdirp")
11
-
12
- // TODO: verify mongod and redis-sever and elasticsearch executables are present
13
- // and have the right version
14
-
15
- const is_production = process.env.NODE_ENV === "production"
16
-
17
-
18
- const NO_PRINT_LIST = [
19
- // "database",
20
- ]
21
-
22
- // load env
23
- const get_env = () => {
24
- const env_path = path.join(process.cwd(), "./.env")
25
- const env_buf = fs.readFileSync(env_path)
26
- const env = dotenv.parse(env_buf)
27
- return env
28
- }
29
-
30
- const get_processes = (args) => {
31
- const env = get_env()
32
-
33
- assert(env.RB_APP_NAME, "missing RB_APP_NAME in env")
34
-
35
- const infrastructure_conf_dir = path.join(__dirname, "../infrastructure")
36
-
37
- // db storage
38
- const db_path = path.join(process.cwd(), "../infrastructure/data/database/db/")
39
-
40
- // db log file
41
- const db_logs_path = path.join(process.cwd(), "../infrastructure/data/database/log/logs.txt")
42
-
43
- // session-store + worker-queue
44
- const session_store_dir = path.join(process.cwd(), "../infrastructure/data/session-store/data")
45
- const session_store_logs_path = path.join(process.cwd(), "../infrastructure/data/session-store/log/logs.txt")
46
- const worker_queue_dir = path.join(process.cwd(), "../infrastructure/data/worker-queue/data")
47
- const worker_queue_logs_path = path.join(process.cwd(), "../infrastructure/data/worker-queue/log/logs.txt")
48
-
49
- const has_uds = true
50
- const uds_root_path = `/tmp/sockets-${env.RB_APP_NAME}/`
51
- if (has_uds && !fs.existsSync(uds_root_path)) {
52
- mkdirp.sync(uds_root_path)
53
- }
54
-
55
- const processes = [
56
- // web server
57
- {
58
- name: "server",
59
- dirs: [],
60
- // yarn dev-server
61
- cmd: ["yarn", ["dev-server"]]
62
- },
63
- {
64
- name: "worker",
65
- dirs: [],
66
- cmd: ["yarn", ["dev-worker"]]
67
- },
68
- // mongodb database
69
- {
70
- name: "database",
71
- dirs: [
72
- db_path,
73
- path.dirname(db_logs_path),
74
- ],
75
- cmd: ["mongod", [
76
- "--quiet",
77
- "--dbpath", db_path,
78
- "--bind_ip", "127.0.0.1", // TODO: bind to 0.0.0.0 here when going multinodes
79
- "--port", env.DATABASE_PORT,
80
- ...(has_uds ? ["--unixSocketPrefix", uds_root_path, "--filePermissions", "0700"] : []),
81
- // if in verbose mode, print to stdout, else write to file
82
- ...(args.verbose ? [] : ["--logpath", db_logs_path]),
83
- "--replSet", "rs0"
84
- ]]
85
- },
86
- // session-store
87
- {
88
- name: "session-store",
89
- dirs: [
90
- session_store_dir,
91
- path.dirname(session_store_logs_path),
92
- ],
93
- cmd: ["redis-server", [
94
- path.join(infrastructure_conf_dir, "./redis/redis.conf"),
95
- "--dir", session_store_dir,
96
- "--port", env.SESSION_STORE_PORT,
97
- ...(args.verbose ? [] : ["--logfile", session_store_logs_path]),
98
- ]]
99
- },
100
- // server-store
101
- {
102
- name: "server-store",
103
- dirs: [
104
- session_store_dir,
105
- path.dirname(session_store_logs_path),
106
- ],
107
- cmd: ["redis-server", [
108
- path.join(infrastructure_conf_dir, "./redis/redis.conf"),
109
- "--dir", session_store_dir,
110
- "--port", env.SERVER_STORE_PORT,
111
- ...(args.verbose ? [] : ["--logfile", session_store_logs_path]),
112
- ]]
113
- },
114
- // worker-queue
115
- {
116
- name: "worker-queue",
117
- dirs: [
118
- worker_queue_dir,
119
- path.dirname(worker_queue_logs_path),
120
- ],
121
- cmd: ["redis-server", [
122
- path.join(infrastructure_conf_dir, "./redis/redis.conf"),
123
- "--dir", worker_queue_dir,
124
- "--port", env.WORKER_QUEUE_PORT,
125
- ...(args.verbose ? [] : ["--logfile", worker_queue_logs_path]),
126
- ]]
127
- },
128
- ]
129
-
130
- // add search-index elasticsearch if port is defined
131
- if (env.SEARCH_INDEX_PORT && validator.isPort(env.SEARCH_INDEX_PORT)) {
132
- const data_dir = path.join(process.cwd(), "../infrastructure/data/search-index/data")
133
- const logs_dir = path.join(process.cwd(), "../infrastructure/data/search-index/logs")
134
- processes.push({
135
- name: "search-index",
136
- dirs: [
137
- data_dir,
138
- logs_dir,
139
- ],
140
- cmd: ["elasticsearch", [
141
- "-s",
142
- "-E", `path.logs=${logs_dir}`,
143
- "-E", `http.port=${env.SEARCH_INDEX_PORT}`,
144
- ...(is_production ? [] : [
145
- "-E", `http.cors.enabled=true`,
146
- "-E", `http.cors.allow-origin=http://localhost:8080`,
147
- ]),
148
- "-E", `path.data=${data_dir}`,
149
- "-E", "discovery.type=single-node",
150
- "-E", "cluster.initial_master_nodes=''",
151
- "-E", "xpack.security.enabled=false",
152
- "-E", "xpack.ml.enabled=false",
153
- "-E", "ingest.geoip.downloader.enabled=false",
154
- ]]
155
- })
156
- }
157
-
158
- // create missing directories
159
- processes.forEach((item) => {
160
- item.dirs.forEach((dir) => {
161
- if (!fs.existsSync(dir)) {
162
- mkdirp.sync(dir)
163
- }
164
- })
165
- })
166
-
167
- return processes
168
- }
169
-
170
- // TODO: run each native process from list
171
- // TODO: handle process close
172
- const run_native = (infrastructure_dir, proj_prefix, args) => {
173
- console.log(`running in ${colors.bold(colors.magenta("NATIVE"))} mode`)
174
-
175
- const processes = get_processes(args)
176
-
177
-
178
- const run_process = (item) => {
179
- const ps_env = {
180
- ...process.env
181
- }
182
-
183
- if (args.sessions) {
184
- ps_env.RB_SESSION_STORE = "yes"
185
- }
186
-
187
-
188
- const ps = spawn(item.cmd[0], item.cmd[1], {env: ps_env})
189
-
190
- // TODO: add flag to force print
191
- if (!NO_PRINT_LIST.includes(item.name)) {
192
-
193
- let prefix = ""
194
- // avoid printing [server] server:
195
- if (!args.verbose && item.name !== "server") {
196
- prefix = `${colors.green(item.name)}:`
197
- }
198
-
199
- ps.stdout.on("data", (data) => {
200
- process.stdout.write(`${prefix}${data.toString()}`)
201
- })
202
-
203
- ps.stderr.on("data", (data) => {
204
- process.stderr.write(`${prefix}${data.toString()}`)
205
- })
206
- }
207
- }
208
-
209
- const spawned = processes.map(run_process)
210
-
211
- process.on("exit", () => {
212
- console.log("EXIT from native1")
213
- // ps.kill()
214
- })
215
- }
216
-
217
- module.exports = run_native
@@ -1,27 +0,0 @@
1
- /* @flow */
2
- const path = require("path")
3
- const fs = require("fs")
4
-
5
- const run_docker = require("./run_docker")
6
- const run_native = require("./run_native")
7
-
8
- // TODO: auto create network if necessary
9
- // docker network create ${RB_APP_NAME}-network
10
-
11
- const start_server = async(args) => {
12
- const infrastructure_dir = path.join(process.cwd(), "../infrastructure")
13
-
14
- const runtime = args.docker ? "docker" : "native"
15
-
16
- // prefix
17
- const proj_parent_dir = path.join(infrastructure_dir, "../../")
18
- const proj_prefix = path.basename(proj_parent_dir)
19
-
20
- if (runtime === "docker") {
21
- run_docker(infrastructure_dir, proj_prefix, args)
22
- } else if (runtime === "native") {
23
- run_native(infrastructure_dir, proj_prefix, args)
24
- }
25
- }
26
-
27
- module.exports = start_server
@@ -1,3 +0,0 @@
1
- FROM docker.elastic.co/elasticsearch/elasticsearch:8.6.1
2
-
3
- RUN bin/elasticsearch-plugin remove --purge repository-gcs