mirror of https://github.com/kern/filepizza
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
819 B
JavaScript
46 lines
819 B
JavaScript
import bases from 'bases'
|
|
import crypto from 'crypto'
|
|
|
|
var tokenLength = 8
|
|
var tokens = {}
|
|
|
|
exports.create = function (socket) {
|
|
|
|
var maxNum = Math.pow(62, tokenLength)
|
|
var numBytes = Math.ceil(Math.log(maxNum) / Math.log(256))
|
|
|
|
var token = ''
|
|
do {
|
|
do {
|
|
var bytes = crypto.randomBytes(numBytes)
|
|
var num = 0
|
|
for (var i = 0; i < bytes.length; i++) {
|
|
num += Math.pow(256, i) * bytes[i]
|
|
}
|
|
} while (num >= maxNum)
|
|
|
|
token = bases.toBase62(num)
|
|
} while (token in tokens)
|
|
|
|
const result = {
|
|
token: token,
|
|
socket: socket
|
|
}
|
|
|
|
tokens[token] = result
|
|
return result
|
|
|
|
}
|
|
|
|
exports.exists = function (token) {
|
|
return token in tokens
|
|
}
|
|
|
|
exports.find = function (token) {
|
|
return tokens[token]
|
|
}
|
|
|
|
exports.remove = function (client) {
|
|
delete tokens[client.token]
|
|
}
|