抽取 randomUUID, crypto-browserify

This commit is contained in:
CrescentLeaf
2025-12-06 11:08:24 +08:00
parent d76abcf512
commit d433ceb4a9
7 changed files with 68 additions and 17 deletions

View File

@@ -3,7 +3,7 @@ import { io, ManagerOptions, Socket, SocketOptions } from 'socket.io-client'
import crypto from 'node:crypto'
import { CallMethod, ClientEvent, ClientEventCallback } from './ApiDeclare.ts'
import ApiCallbackMessage from './ApiCallbackMessage.ts'
import { CallableMethodBeforeAuth } from "lingchair-internal-shared"
import { CallableMethodBeforeAuth, randomUUID } from "lingchair-internal-shared"
import CallbackError from "./CallbackError.ts"
import Message from "./Message.ts"
@@ -36,7 +36,7 @@ export default class LingChairClient {
auth: {
...args.io?.auth,
device_id: this.device_id,
session_id: crypto.randomUUID(),
session_id: randomUUID(),
},
})
this.client.on("The_White_Silk", (name: ClientEvent, data: any, _callback: (ret: unknown) => void) => {

View File

@@ -5,6 +5,7 @@
"dependencies": {
"lingchair-internal-shared": "*",
"marked": "16.3.0",
"socket.io-client": "4.8.1"
"socket.io-client": "4.8.1",
"crypto-browserify": "3.12.1"
}
}

View File

@@ -1,7 +1,7 @@
import { LingChairClient } from 'lingchair-client-protocol'
import data from "./Data.ts"
import { UAParser } from 'ua-parser-js'
import randomUUID from "./utils/randomUUID.ts"
import { randomUUID } from 'lingchair-internal-shared'
if (!data.device_id) {
const ua = new UAParser(navigator.userAgent)

View File

@@ -6,24 +6,26 @@
"build-watch": "npx vite --watch build"
},
"dependencies": {
"crypto-browserify": "3.12.1",
"crypto-js": "4.2.0",
"dompurify": "3.2.7",
"lingchair-internal-shared": "*",
"marked": "16.3.0",
"mdui": "2.1.4",
"pinch-zoom-element": "1.1.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"mdui": "2.1.4",
"split.js": "1.3.2",
"crypto-js": "4.2.0",
"socket.io-client": "4.8.1",
"marked": "16.3.0",
"dompurify": "3.2.7",
"pinch-zoom-element": "1.1.1",
"ua-parser-js": "2.0.6",
"lingchair-internal-shared": "*"
"split.js": "1.3.2",
"ua-parser-js": "2.0.6"
},
"devDependencies": {
"@rollup/wasm-node": "4.48.0",
"@types/react": "18.3.1",
"@types/react-dom": "18.3.1",
"@vitejs/plugin-react": "4.7.0",
"chalk": "5.4.1",
"vite": "7.0.6",
"@rollup/wasm-node": "4.48.0",
"chalk": "5.4.1"
"vite-plugin-node-polyfills": "^0.24.0"
}
}

View File

@@ -1,12 +1,23 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import config from '../server/config.ts'
import { nodePolyfills } from 'vite-plugin-node-polyfills'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
plugins: [
react(),
nodePolyfills({
include: ['crypto', 'stream', 'vm'],
globals: {
Buffer: true,
global: true,
process: true,
},
})
],
build: {
sourcemap: true,
outDir: "." + config.data_path + '/page_compiled',
}
},
})

View File

@@ -2,3 +2,6 @@ export * from './ApiDeclare.ts'
import ApiCallbackMessage from './ApiCallbackMessage.ts'
export type { ApiCallbackMessage }
import randomUUID from './randomUUID.ts'
export { randomUUID }

View 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') {
// crypto-browserify 没有这个方法
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)
}
}
// 随机数
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)
})
}