From d433ceb4a9f32f8a314907e5fdbe3297caba4ee2 Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Sat, 6 Dec 2025 11:08:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=8F=96=20randomUUID,=20crypto-brows?= =?UTF-8?q?erify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client-protocol/LingChairClient.ts | 4 ++-- client-protocol/package.json | 3 ++- client/getClient.ts | 2 +- client/package.json | 24 +++++++++++---------- client/vite.config.ts | 15 +++++++++++-- internal-shared/main.ts | 3 +++ internal-shared/randomUUID.ts | 34 ++++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 internal-shared/randomUUID.ts diff --git a/client-protocol/LingChairClient.ts b/client-protocol/LingChairClient.ts index ab143e3..5471f1e 100644 --- a/client-protocol/LingChairClient.ts +++ b/client-protocol/LingChairClient.ts @@ -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) => { diff --git a/client-protocol/package.json b/client-protocol/package.json index af5aedd..36eacb9 100644 --- a/client-protocol/package.json +++ b/client-protocol/package.json @@ -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" } } diff --git a/client/getClient.ts b/client/getClient.ts index 50ea688..75cd98a 100644 --- a/client/getClient.ts +++ b/client/getClient.ts @@ -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) diff --git a/client/package.json b/client/package.json index 0566c74..19bd22a 100644 --- a/client/package.json +++ b/client/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/client/vite.config.ts b/client/vite.config.ts index d481303..8147abf 100644 --- a/client/vite.config.ts +++ b/client/vite.config.ts @@ -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', - } + }, }) diff --git a/internal-shared/main.ts b/internal-shared/main.ts index da8f128..4f78f1c 100644 --- a/internal-shared/main.ts +++ b/internal-shared/main.ts @@ -2,3 +2,6 @@ export * from './ApiDeclare.ts' import ApiCallbackMessage from './ApiCallbackMessage.ts' export type { ApiCallbackMessage } + +import randomUUID from './randomUUID.ts' +export { randomUUID } diff --git a/internal-shared/randomUUID.ts b/internal-shared/randomUUID.ts new file mode 100644 index 0000000..a323a82 --- /dev/null +++ b/internal-shared/randomUUID.ts @@ -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) + }) +} \ No newline at end of file