-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathutils.js
52 lines (44 loc) · 1.01 KB
/
utils.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 crypto = require("crypto")
const querystring = require("querystring")
function randomString() {
const randomBytes = crypto.randomBytes(20)
return randomBytes.toString("base64")
}
function containsAll(arr1, arr2) {
const arr1Set = new Set()
for (let i = 0; i < arr1.length; i++) {
arr1Set.add(arr1[i])
}
for (let i = 0; i < arr2.length; i++) {
if (!arr1Set.has(arr2[i])) {
return false
}
}
return true
}
function decodeAuthCredentials(auth) {
var clientCredentials = Buffer.from(auth.slice("basic ".length), "base64")
.toString()
.split(":")
var clientId = querystring.unescape(clientCredentials[0])
var clientSecret = querystring.unescape(clientCredentials[1])
return { clientId, clientSecret }
}
function deleteAllKeys(obj) {
Object.keys(obj).forEach((k) => {
delete obj[k]
})
}
function timeout(req, res, next) {
res.setTimeout(400, function () {
res.status(408).end()
})
next()
}
module.exports = {
randomString,
containsAll,
decodeAuthCredentials,
deleteAllKeys,
timeout,
}