-
Notifications
You must be signed in to change notification settings - Fork 3
/
ksuid.js
52 lines (47 loc) · 1.41 KB
/
ksuid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const MAX_ENCODED_STRING_LENGTH = 27
const PAYLOAD_MAX_LENGTH = 16
const ksuid = require('ksuid')
const common = require('./common')
const hkdf = require('./hkdf')
module.exports = {
/**
* Create a new KSUID.
*
* @param {number} unixTimestamp - The UNIX timestamp to use.
* @returns {Promise} The KSUID value.
*/
create: (unixTimestamp = null) => {
return new Promise(async (resolve) => {
let utc
if (unixTimestamp === null) {
utc = (await common.utcTimestamp())
} else {
utc = unixTimestamp
}
const ikm = await common.random(32)
const payload = await hkdf.derive(ikm, PAYLOAD_MAX_LENGTH)
const identifier = ksuid.fromParts(utc * 1000, payload)
return resolve(identifier.string)
})
},
/**
* Parse a KSUID value and return its component parts.
*
* @param {string} ksuidValue - The KSUID value to parse.
* @returns {Promise} The component parts of the KSUID.
*/
parse: (ksuidValue) => {
return new Promise((resolve, reject) => {
if (ksuidValue.length !== MAX_ENCODED_STRING_LENGTH) {
reject(new Error('ksuidValue does not appear to be a KSUID.'))
}
const parsedIdentifier = ksuid.parse(ksuidValue)
return resolve({
ksuid: ksuidValue,
time: parsedIdentifier.date,
timestamp: parsedIdentifier.timestamp,
payload: parsedIdentifier.payload
})
})
}
}