抽取 randomUUID, crypto-browserify
This commit is contained in:
@@ -3,7 +3,7 @@ import { io, ManagerOptions, Socket, SocketOptions } from 'socket.io-client'
|
|||||||
import crypto from 'node:crypto'
|
import crypto from 'node:crypto'
|
||||||
import { CallMethod, ClientEvent, ClientEventCallback } from './ApiDeclare.ts'
|
import { CallMethod, ClientEvent, ClientEventCallback } from './ApiDeclare.ts'
|
||||||
import ApiCallbackMessage from './ApiCallbackMessage.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 CallbackError from "./CallbackError.ts"
|
||||||
|
|
||||||
import Message from "./Message.ts"
|
import Message from "./Message.ts"
|
||||||
@@ -36,7 +36,7 @@ export default class LingChairClient {
|
|||||||
auth: {
|
auth: {
|
||||||
...args.io?.auth,
|
...args.io?.auth,
|
||||||
device_id: this.device_id,
|
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) => {
|
this.client.on("The_White_Silk", (name: ClientEvent, data: any, _callback: (ret: unknown) => void) => {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lingchair-internal-shared": "*",
|
"lingchair-internal-shared": "*",
|
||||||
"marked": "16.3.0",
|
"marked": "16.3.0",
|
||||||
"socket.io-client": "4.8.1"
|
"socket.io-client": "4.8.1",
|
||||||
|
"crypto-browserify": "3.12.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { LingChairClient } from 'lingchair-client-protocol'
|
import { LingChairClient } from 'lingchair-client-protocol'
|
||||||
import data from "./Data.ts"
|
import data from "./Data.ts"
|
||||||
import { UAParser } from 'ua-parser-js'
|
import { UAParser } from 'ua-parser-js'
|
||||||
import randomUUID from "./utils/randomUUID.ts"
|
import { randomUUID } from 'lingchair-internal-shared'
|
||||||
|
|
||||||
if (!data.device_id) {
|
if (!data.device_id) {
|
||||||
const ua = new UAParser(navigator.userAgent)
|
const ua = new UAParser(navigator.userAgent)
|
||||||
|
|||||||
@@ -6,24 +6,26 @@
|
|||||||
"build-watch": "npx vite --watch build"
|
"build-watch": "npx vite --watch build"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"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": "18.3.1",
|
||||||
"react-dom": "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",
|
"socket.io-client": "4.8.1",
|
||||||
"marked": "16.3.0",
|
"split.js": "1.3.2",
|
||||||
"dompurify": "3.2.7",
|
"ua-parser-js": "2.0.6"
|
||||||
"pinch-zoom-element": "1.1.1",
|
|
||||||
"ua-parser-js": "2.0.6",
|
|
||||||
"lingchair-internal-shared": "*"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@rollup/wasm-node": "4.48.0",
|
||||||
"@types/react": "18.3.1",
|
"@types/react": "18.3.1",
|
||||||
"@types/react-dom": "18.3.1",
|
"@types/react-dom": "18.3.1",
|
||||||
"@vitejs/plugin-react": "4.7.0",
|
"@vitejs/plugin-react": "4.7.0",
|
||||||
|
"chalk": "5.4.1",
|
||||||
"vite": "7.0.6",
|
"vite": "7.0.6",
|
||||||
"@rollup/wasm-node": "4.48.0",
|
"vite-plugin-node-polyfills": "^0.24.0"
|
||||||
"chalk": "5.4.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,23 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import react from '@vitejs/plugin-react'
|
import react from '@vitejs/plugin-react'
|
||||||
import config from '../server/config.ts'
|
import config from '../server/config.ts'
|
||||||
|
import { nodePolyfills } from 'vite-plugin-node-polyfills'
|
||||||
|
|
||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react()],
|
plugins: [
|
||||||
|
react(),
|
||||||
|
nodePolyfills({
|
||||||
|
include: ['crypto', 'stream', 'vm'],
|
||||||
|
globals: {
|
||||||
|
Buffer: true,
|
||||||
|
global: true,
|
||||||
|
process: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
],
|
||||||
build: {
|
build: {
|
||||||
sourcemap: true,
|
sourcemap: true,
|
||||||
outDir: "." + config.data_path + '/page_compiled',
|
outDir: "." + config.data_path + '/page_compiled',
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,3 +2,6 @@ export * from './ApiDeclare.ts'
|
|||||||
|
|
||||||
import ApiCallbackMessage from './ApiCallbackMessage.ts'
|
import ApiCallbackMessage from './ApiCallbackMessage.ts'
|
||||||
export type { ApiCallbackMessage }
|
export type { ApiCallbackMessage }
|
||||||
|
|
||||||
|
import randomUUID from './randomUUID.ts'
|
||||||
|
export { randomUUID }
|
||||||
|
|||||||
34
internal-shared/randomUUID.ts
Normal file
34
internal-shared/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') {
|
||||||
|
// 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user