@rpcbase/server 0.331.0 → 0.333.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/get_object_id.js CHANGED
@@ -5,28 +5,28 @@ const crypto = require("crypto")
5
5
  const isHexadecimal = require("validator/lib/isHexadecimal")
6
6
 
7
7
 
8
- const {TENANT_PREFIX} = process.env
8
+ const {RB_TENANT_ID} = process.env
9
9
 
10
10
 
11
11
  // TODO: WARNING: DANGER: this code is duplicated in the client
12
12
  // TODO: move it to iso
13
13
 
14
14
  // Validation: is defined
15
- assert(TENANT_PREFIX, "expected TENANT_PREFIX to be defined")
15
+ assert(RB_TENANT_ID, "expected RB_TENANT_ID to be defined")
16
16
  // is hexadecimal
17
- assert(isHexadecimal(TENANT_PREFIX), "expected TENANT_PREFIX to be a hexadecimal")
17
+ assert(isHexadecimal(RB_TENANT_ID), "expected RB_TENANT_ID to be a hexadecimal")
18
18
  // is 4 bytes
19
- assert(TENANT_PREFIX.length === 8, "TENANT_PREFIX must be exactly 4 bytes (8 hex chars)")
19
+ assert(RB_TENANT_ID.length === 8, "RB_TENANT_ID must be exactly 4 bytes (8 hex chars)")
20
20
 
21
21
 
22
22
  // generates a 12 bytes mongodb object id using the org id prefix or custom customer id
23
23
  const get_object_id = () => {
24
24
  const ts_bytes = Math.floor(Date.now() / 1000).toString(16)
25
- assert(ts_bytes.length === 8, "expected ts_bytes.length to be 4")
25
+ expect(ts_bytes.length).toBe(8)
26
26
 
27
27
  const random_bytes = crypto.randomBytes(4).toString("hex")
28
28
 
29
- const obj_id = `${ts_bytes}${TENANT_PREFIX}${random_bytes}`
29
+ const obj_id = `${ts_bytes}${RB_TENANT_ID}${random_bytes}`
30
30
  return obj_id
31
31
  }
32
32
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/server",
3
- "version": "0.331.0",
3
+ "version": "0.333.0",
4
4
  "license": "SSPL-1.0",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -11,7 +11,7 @@ const dispatch_queue = require("./dispatch_queue")
11
11
 
12
12
  const log = debug("rb:queue:listener")
13
13
 
14
- const {RB_APP_NAME} = process.env
14
+ const {RB_APP_NAME, RB_TENANT_ID} = process.env
15
15
 
16
16
  const RETRY_MAXIMUM_DELAY = 2000
17
17
  const RETRY_MINIMUM_DELAY = 10
@@ -52,12 +52,31 @@ const mongoose_delete_plugin = (schema) => {
52
52
  }
53
53
 
54
54
 
55
+ const assert_doc_id = (change) => {
56
+ if (process.env.NODE_ENV !== "development") return
57
+
58
+ const doc_id = change.documentKey?._id?.toString()
59
+
60
+ if (!doc_id) return
61
+
62
+ const sub = doc_id.substring(8, 16)
63
+ try {
64
+ expect(sub).toBe(RB_TENANT_ID)
65
+ } catch (err) {
66
+ console.log("in document:", change.ns, change.documentKey)
67
+ console.log("_id must be constructed with get_object_id()")
68
+ console.log(err.message)
69
+ }
70
+ }
71
+
55
72
  const dispatch_change_handler = (change) => {
56
73
  // skip if change is on another db
57
74
  if (change?.ns?.db !== RB_APP_NAME) {
58
75
  return
59
76
  }
60
77
 
78
+ assert_doc_id(change)
79
+
61
80
  const op = change.operationType
62
81
  if (["insert", "update"].includes(op)) {
63
82
  const coll_name = change.ns.coll