@rpcbase/client 0.146.0 → 0.147.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 +7 -8
- package/package.json +1 -1
package/get_object_id.js
CHANGED
|
@@ -12,23 +12,22 @@ const get_random_bytes = (n) => {
|
|
|
12
12
|
return array
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
// Validation
|
|
16
|
-
// is defined
|
|
15
|
+
// Validation: is defined
|
|
17
16
|
assert(TENANT_PREFIX, "expected TENANT_PREFIX to be defined")
|
|
18
17
|
// is hexadecimal
|
|
19
18
|
assert(isHexadecimal(TENANT_PREFIX), "expected TENANT_PREFIX to be a hexadecimal")
|
|
20
19
|
// is 4 bytes
|
|
21
20
|
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
21
|
|
|
27
22
|
// generates a 12 bytes mongodb object id using the org id prefix or custom customer id
|
|
28
23
|
const get_object_id = () => {
|
|
29
|
-
const
|
|
24
|
+
const ts_bytes = Math.floor(Date.now() / 1000).toString(16)
|
|
25
|
+
assert(ts_bytes.length === 8, "expected ts_bytes.length to be 4")
|
|
26
|
+
|
|
27
|
+
const random_bytes = get_random_bytes(4)
|
|
30
28
|
const random_hex = Buffer.from(random_bytes).toString("hex")
|
|
31
|
-
|
|
29
|
+
|
|
30
|
+
const obj_id = `${ts_bytes}${TENANT_PREFIX}${random_hex}`
|
|
32
31
|
return obj_id
|
|
33
32
|
}
|
|
34
33
|
|