@rpcbase/client 0.47.0 → 0.48.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.
Files changed (2) hide show
  1. package/get_object_id.js +35 -0
  2. package/package.json +1 -1
@@ -0,0 +1,35 @@
1
+ /* @flow */
2
+ import assert from "assert"
3
+
4
+ import isHexadecimal from "validator/lib/isHexadecimal"
5
+
6
+ import {TENANT_PREFIX} from "env"
7
+
8
+
9
+ const get_random_bytes = (n) => {
10
+ let array = new Uint8Array(n)
11
+ window.crypto.getRandomValues(array)
12
+ return array
13
+ }
14
+
15
+ // Validation
16
+ // is defined
17
+ assert(TENANT_PREFIX, "expected TENANT_PREFIX to be defined")
18
+ // is hexadecimal
19
+ assert(isHexadecimal(TENANT_PREFIX), "expected TENANT_PREFIX to be a hexadecimal")
20
+ // is 4 bytes
21
+ assert(TENANT_PREFIX.length === 8, "TENANT_PREFIX must be exactly bytes long ie: 8 hex chars")
22
+ // is lower than
23
+ const max_val = parseInt("6387427d", 16)
24
+ const tenant_prefix_int = parseInt(TENANT_PREFIX, 16)
25
+ assert(max_val - tenant_prefix_int > 0, "TENANT_PREFIX must be lower than 6387427d")
26
+
27
+ // generates a 12 bytes mongodb object id using the org id prefix or custom customer id
28
+ const get_object_id = () => {
29
+ const random_bytes = get_random_bytes(8)
30
+ const random_hex = Buffer.from(random_bytes).toString("hex")
31
+ const obj_id = `${TENANT_PREFIX}${random_hex}`
32
+ return obj_id
33
+ }
34
+
35
+ export default get_object_id
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.47.0",
3
+ "version": "0.48.0",
4
4
  "scripts": {
5
5
  "test": "echo \"Error: no test specified\" && exit 0"
6
6
  },