@rpcbase/server 0.92.0 → 0.95.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.
@@ -1,12 +1,18 @@
1
1
  /* @flow */
2
- // TODO: replace console debug
2
+ const debug = require("debug")
3
+
4
+ const log = debug("rb:coverage")
3
5
 
4
6
 
5
7
  const is_production = process.env.NODE_ENV === "production"
6
8
 
7
9
  module.exports = (app) => {
8
10
  app.post("/api/__dev_save_coverage", (req, res) => {
9
- console.log("sending coverage for", req.body.path_key)
11
+ if (is_production) {
12
+ log("refusing to save coverage in production")
13
+ return res.status(500).end()
14
+ }
15
+ // log("sending coverage for", req.body.path_key)
10
16
  res.json(global.__coverage__)
11
17
  })
12
18
  }
@@ -1,9 +1,12 @@
1
1
  /* @flow */
2
+ const debug = require("debug")
2
3
  const session = require("express-session")
3
4
  const validator = require("validator")
4
5
  const {createClient} = require("redis")
5
6
  const redis_store = require("connect-redis")(session)
6
7
 
8
+ const log = debug("rb:session")
9
+
7
10
  // WARNING:
8
11
  // https://stackoverflow.com/questions/70867229/error-connection-timeout-when-connecting-to-redis-docker-instance
9
12
  // https://github.com/redis/node-redis/issues/1656/
@@ -27,9 +30,9 @@ setTimeout(async() => {
27
30
  }
28
31
 
29
32
  const reconnectStrategy = (retries) => {
30
- console.log("redis_client::reconnectStrategy::retrying with arg", retries)
33
+ log("redis_client::reconnectStrategy::retrying with arg", retries)
31
34
  if (retries < 5) {
32
- console.log("retry count:", retries, "retrying in 1s")
35
+ log("retry count:", retries, "retrying in 1s")
33
36
  return 4000
34
37
  } else {
35
38
  return new Error("max retries expiered")
@@ -47,9 +50,8 @@ setTimeout(async() => {
47
50
  legacyMode: true,
48
51
  })
49
52
 
50
-
51
53
  redis_client.on("ready", () => {
52
- console.log("session-storage::redis_client connected")
54
+ log("session-storage::redis_client connected")
53
55
  })
54
56
 
55
57
  const res = await redis_client.connect()
@@ -69,7 +71,7 @@ setTimeout(async() => {
69
71
  })
70
72
 
71
73
  // TODO: is this still necessary
72
- }, 300)
74
+ }, 200)
73
75
 
74
76
 
75
77
  module.exports = (req, res, next) => {
@@ -80,6 +82,6 @@ module.exports = (req, res, next) => {
80
82
  if (session_middleware) {
81
83
  session_middleware(req, res, next)
82
84
  } else {
83
- console.warn("session middleware not set yet")
85
+ warn("session middleware not set yet")
84
86
  }
85
87
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.92.0",
3
+ "version": "0.95.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
package/queue/index.js CHANGED
@@ -11,8 +11,9 @@ let worker_queue
11
11
 
12
12
  const worker_add = async(task_name, payload, options) => {
13
13
  const res = await worker_queue.add({task_name, payload}, options)
14
- // myQueue.add(data, { repeat: { cron: '15 3 * * *' } });
15
14
  console.log("created task:", task_name, res.id)
15
+ console.log(payload)
16
+ console.log("\n")
16
17
  return res
17
18
  }
18
19
 
@@ -47,9 +48,11 @@ const start_worker = async() => {
47
48
  worker_queue.process(async(job) => {
48
49
  try {
49
50
  const {task_name, payload} = job.data
51
+
50
52
  console.log(`start job ${job.id} ${task_name}`)
51
- // await Pomise.delay(2000)
53
+
52
54
  const task_fn = tasks_list[task_name]
55
+
53
56
  const res = await task_fn(payload)
54
57
  return res
55
58
  } catch (err) {
@@ -1,9 +1,12 @@
1
1
  /* @flow */
2
+ const debug = require("debug")
3
+
2
4
  const mongoose = require("../mongoose")
3
5
  const queue = require("./index")
4
6
 
5
7
  const dispatch_queue = require("./dispatch_queue")
6
8
 
9
+ const log = debug("rb:queue:listener")
7
10
 
8
11
  // Listens for mongodb change events,
9
12
  // when a document is created, updated or deleted, dispatch job message
@@ -18,8 +21,8 @@ const mongoose_delete_plugin = (schema) => {
18
21
  // TODO: add other delete operations
19
22
  // https://mongoosejs.com/docs/queries.html
20
23
  schema.post("findOneAndDelete", function(doc) {
21
- console.log("queue::findOneAndDelete", "dispatch_doc_change NYI")
22
- console.log("DEL PLUGIN", doc)
24
+ log("queue::findOneAndDelete", "dispatch_doc_change NYI")
25
+ log("DEL PLUGIN", doc)
23
26
  const model_name = this.model.modelName
24
27
  dispatch_queue(queue, model_name, "delete", doc)
25
28
  })
@@ -35,30 +38,35 @@ const get_dispatch_change_handler = (model_name) => (change) => {
35
38
  }
36
39
 
37
40
 
38
- const register_queue_listener = async() => {
39
- // register the mongoose delete plugin
40
- mongoose.plugin(mongoose_delete_plugin)
41
+ const register_model_emitter = (model_name) => {
42
+ const model = mongoose.model(model_name)
41
43
 
42
- const models = mongoose.modelNames()
44
+ // TODO: implement delete operation fullDocument retrieve,
45
+ // when this is released https://jira.mongodb.org/browse/SERVER-36941
46
+ // this is done via a plugin right now
47
+ const emitter = model.watch({fullDocument: "updateLookup"})
48
+
49
+ emitter.on("change", get_dispatch_change_handler(model_name))
50
+
51
+ emitter.on("error", (err) => {
52
+ log("server::queue::register_queue_listener:: change listener emitter got error", err)
53
+ })
43
54
 
44
- models.forEach((model_name) => {
45
- const model = mongoose.model(model_name)
55
+ emitter.on("close", (arg, arg2) => {
56
+ log("queue_listener: emitter closed, model:", model_name, "retrying")
57
+ register_model_emitter(model_name)
58
+ })
59
+ }
46
60
 
47
- // TODO: implement delete operation fullDocument retrieve,
48
- // when this is released https://jira.mongodb.org/browse/SERVER-36941
49
- // this is done via a plugin right now
50
- const emitter = model.watch({fullDocument: "updateLookup"})
51
61
 
52
- emitter.on("change", get_dispatch_change_handler(model_name))
53
62
 
54
- emitter.on("error", (err) => {
55
- console.log("server::queue::register_queue_listener:: change listener emitter got error", err)
56
- })
63
+ const register_queue_listener = async() => {
64
+ // register the mongoose delete plugin
65
+ mongoose.plugin(mongoose_delete_plugin)
66
+
67
+ const models = mongoose.modelNames()
57
68
 
58
- emitter.on("close", () => {
59
- console.log("queue_listener: emitter closed, model:", model_name)
60
- })
61
- })
69
+ models.forEach(register_model_emitter)
62
70
 
63
71
  }
64
72
 
@@ -1,10 +1,19 @@
1
1
  /* @flow */
2
+ const debug = require("debug")
3
+
4
+
5
+ const log = debug("rb:auth:check_session")
6
+
2
7
 
3
8
  const check_session = async(payload, ctx) => {
4
9
  const {req} = ctx
5
- // console.log("session check", req.session)
10
+
11
+ log("session:", req.session)
6
12
 
7
13
  const is_signed_in = !!req.session.user_id
14
+
15
+ log("is_signed_in:", is_signed_in)
16
+
8
17
  return {
9
18
  status: "ok",
10
19
  is_signed_in,
@@ -1,8 +1,12 @@
1
1
  /* @flow */
2
2
  const {compare_hash} = require("@rpcbase/std/crypto/hash")
3
+ const debug = require("debug")
3
4
 
4
5
  const mongoose = require("../../mongoose")
5
6
 
7
+
8
+ const log = debug("rb:auth:signin")
9
+
6
10
  const fail = () => ({
7
11
  errors: {form: "Invalid email or password"}
8
12
  })
@@ -13,8 +17,6 @@ const sign_in = async({email, password}, ctx) => {
13
17
  const {req} = ctx
14
18
 
15
19
  // find the matching user
16
- // TODO: document ctx param
17
- // const user = await User.findOne({email}, null, {ctx})
18
20
  const user = await User.findOne({email}, null)
19
21
 
20
22
  if (!user) {
@@ -26,13 +28,20 @@ const sign_in = async({email, password}, ctx) => {
26
28
  const is_match = await compare_hash(password, hashed_pass)
27
29
 
28
30
  if (is_match) {
29
- console.log("is match, user signed in wow")
31
+ log("compare_hash is match")
32
+
33
+ req.session.user_id = user._id.toString()
30
34
  await req.session.save()
31
- console.log("rb:auth:user_signed_in")
35
+
36
+ log("session saved, user is signed in")
37
+
32
38
  return {
33
39
  status: "ok"
34
40
  }
41
+
35
42
  } else {
43
+ log("no match, returning error")
44
+
36
45
  return fail()
37
46
  }
38
47
  }