refactor: randomUUID with fallback
This commit is contained in:
@@ -4,6 +4,7 @@ import ApiCallbackMessage from './ApiCallbackMessage.ts'
|
|||||||
import User from "./client_data/User.ts"
|
import User from "./client_data/User.ts"
|
||||||
import data from "../Data.ts"
|
import data from "../Data.ts"
|
||||||
import { checkApiSuccessOrSncakbar } from "../ui/snackbar.ts"
|
import { checkApiSuccessOrSncakbar } from "../ui/snackbar.ts"
|
||||||
|
import randomUUID from "../randomUUID.ts"
|
||||||
|
|
||||||
type UnknownObject = { [key: string]: unknown }
|
type UnknownObject = { [key: string]: unknown }
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ class Client {
|
|||||||
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
|
static events: { [key: string]: (data: UnknownObject) => UnknownObject | void } = {}
|
||||||
static connect() {
|
static connect() {
|
||||||
if (data.device_id == null)
|
if (data.device_id == null)
|
||||||
data.device_id = crypto.randomUUID()
|
data.device_id = randomUUID()
|
||||||
this.socket?.disconnect()
|
this.socket?.disconnect()
|
||||||
this.socket && delete this.socket
|
this.socket && delete this.socket
|
||||||
this.socket = io({
|
this.socket = io({
|
||||||
|
|||||||
34
client/randomUUID.ts
Normal file
34
client/randomUUID.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// https://www.xiabingbao.com/post/crypto/js-crypto-randomuuid-qxcuqj.html
|
||||||
|
// 在此表示感謝
|
||||||
|
|
||||||
|
export default function randomUUID() {
|
||||||
|
// crypto - 只支持在安全的上下文使用
|
||||||
|
if (typeof crypto === 'object') {
|
||||||
|
if (typeof crypto.randomUUID === 'function') {
|
||||||
|
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID
|
||||||
|
return crypto.randomUUID()
|
||||||
|
}
|
||||||
|
if (typeof crypto.getRandomValues === 'function' && typeof Uint8Array === 'function') {
|
||||||
|
// https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid
|
||||||
|
const callback = (c: string) => {
|
||||||
|
const num = Number(c)
|
||||||
|
return (num ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (num / 4)))).toString(16)
|
||||||
|
};
|
||||||
|
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 隨機數 - fallback
|
||||||
|
let timestamp = new Date().getTime()
|
||||||
|
let perforNow = (typeof performance !== 'undefined' && performance.now && performance.now() * 1000) || 0
|
||||||
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||||
|
let random = Math.random() * 16
|
||||||
|
if (timestamp > 0) {
|
||||||
|
random = (timestamp + random) % 16 | 0
|
||||||
|
timestamp = Math.floor(timestamp / 16)
|
||||||
|
} else {
|
||||||
|
random = (perforNow + random) % 16 | 0
|
||||||
|
perforNow = Math.floor(perforNow / 16)
|
||||||
|
}
|
||||||
|
return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user