fix: missing navigator object in server-side

This commit is contained in:
alikia2x (寒寒) 2025-04-28 06:15:06 +08:00
parent 5a112aeaee
commit a31f702499
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6

View File

@ -1,11 +1,10 @@
<script lang="ts"> <script lang="ts">
import { N_ARRAY } from "src/const"; import {N_ARRAY} from "src/const";
import { fade } from "svelte/transition"; import {fade} from "svelte/transition";
import { UAParser } from 'ua-parser-js'; import {UAParser} from 'ua-parser-js';
import {onMount} from "svelte";
const ua = navigator ? navigator.userAgent : ""; let browserInfo: null | string = null;
const { browser } = UAParser(ua);
let bigintSupported = typeof BigInt !== 'undefined'; let bigintSupported = typeof BigInt !== 'undefined';
@ -95,7 +94,7 @@
// 创建需要测试的 N 和难度的组合 // 创建需要测试的 N 和难度的组合
N_ARRAY.forEach((n) => { N_ARRAY.forEach((n) => {
difficulties.forEach((difficulty) => { difficulties.forEach((difficulty) => {
testCombinations.push({ N: n, difficulty }); testCombinations.push({N: n, difficulty});
}); });
}); });
@ -111,14 +110,14 @@
benchmarkResults = []; benchmarkResults = [];
currentTestIndex = 0; currentTestIndex = 0;
const { N, difficulty } = testCombinations[currentTestIndex]; const {N, difficulty} = testCombinations[currentTestIndex];
const g = generateValidG(N); const g = generateValidG(N);
let blob = new Blob([workerContent], { type: "text/javascript" }); let blob = new Blob([workerContent], {type: "text/javascript"});
worker = new Worker(window.URL.createObjectURL(blob)); worker = new Worker(window.URL.createObjectURL(blob));
worker.onmessage = (event) => { worker.onmessage = (event) => {
const { type, N: resultNStr, difficulty: resultDifficultyStr, time, progress } = event.data; const {type, N: resultNStr, difficulty: resultDifficultyStr, time, progress} = event.data;
const resultN = BigInt(resultNStr); const resultN = BigInt(resultNStr);
const resultDifficulty = BigInt(resultDifficultyStr); const resultDifficulty = BigInt(resultDifficultyStr);
@ -128,7 +127,7 @@
currentN = resultN; currentN = resultN;
currentDifficulty = resultDifficulty; currentDifficulty = resultDifficulty;
} else if (type === "result") { } else if (type === "result") {
benchmarkResults = [...benchmarkResults, { N: resultN, difficulty: resultDifficulty, time }]; benchmarkResults = [...benchmarkResults, {N: resultN, difficulty: resultDifficulty, time}];
currentProgress = 0; currentProgress = 0;
currentTestIndex++; currentTestIndex++;
@ -136,7 +135,7 @@
// 继续下一个测试组合 // 继续下一个测试组合
const nextTest = testCombinations[currentTestIndex]; const nextTest = testCombinations[currentTestIndex];
const nextG = generateValidG(nextTest.N); const nextG = generateValidG(nextTest.N);
worker?.postMessage({ g: nextG, N: nextTest.N, difficulty: nextTest.difficulty }); worker?.postMessage({g: nextG, N: nextTest.N, difficulty: nextTest.difficulty});
} else { } else {
// 所有测试完毕 // 所有测试完毕
isBenchmarking = false; isBenchmarking = false;
@ -149,7 +148,7 @@
}; };
// 开始第一个测试 // 开始第一个测试
worker.postMessage({ g, N, difficulty }); worker.postMessage({g, N, difficulty});
} }
function getAccumulatedTime() { function getAccumulatedTime() {
@ -163,6 +162,12 @@
} }
return Number(speedSample.difficulty) / speedSample.time * 1000; return Number(speedSample.difficulty) / speedSample.time * 1000;
} }
onMount(() => {
const ua = navigator ? navigator.userAgent : "";
const {browser} = UAParser(ua);
browserInfo = browser.name + " " + browser.version;
})
</script> </script>
<div <div
@ -216,7 +221,9 @@
速度是在 N = {speedSample.N.toString(2).length} bits, T = {speedSample.difficulty} 的测试中测量的. 速度是在 N = {speedSample.N.toString(2).length} bits, T = {speedSample.difficulty} 的测试中测量的.
</span> </span>
<br/> <br/>
浏览器版本:{browser} {#if browserInfo}
浏览器版本:{browserInfo}
{/if}
</p> </p>
<table class="w-full text-sm text-left rtl:text-right mt-4"> <table class="w-full text-sm text-left rtl:text-right mt-4">
<thead class="text-sm uppercase font-medium border-b border-outline dark:border-dark-outline"> <thead class="text-sm uppercase font-medium border-b border-outline dark:border-dark-outline">
@ -254,21 +261,21 @@
<h2 class="text-lg font-medium">关于本页</h2> <h2 class="text-lg font-medium">关于本页</h2>
<div class="text-sm text-on-surface-variant dark:text-dark-on-surface-variant"> <div class="text-sm text-on-surface-variant dark:text-dark-on-surface-variant">
<p> <p>
这是一个性能测试页面,<br /> 这是一个性能测试页面,<br/>
旨在测试我们的一个 VDF (Verifiable Delayed Function, 可验证延迟函数) 实现的性能。<br /> 旨在测试我们的一个 VDF (Verifiable Delayed Function, 可验证延迟函数) 实现的性能。<br/>
这是一个数学函数它驱动了整个网站的验证码CAPTCHA<br /> 这是一个数学函数它驱动了整个网站的验证码CAPTCHA<br/>
通过使用该函数,我们可以让您无需通过点选图片或滑动滑块既可完成验证, 同时防御我们的网站,使其免受自动程序的攻击。 通过使用该函数,我们可以让您无需通过点选图片或滑动滑块既可完成验证, 同时防御我们的网站,使其免受自动程序的攻击。
<br /> <br/>
</p> </p>
<p> <p>
点击 <i>Start Benchmark</i> 按钮,会自动测试并展示结果。<br /> 点击 <i>Start Benchmark</i> 按钮,会自动测试并展示结果。<br/>
</p> </p>
<p> <p>
你可以将结果发送至邮箱: <a href="mailto:contact@alikia2x.com">contact@alikia2x.com</a> 你可以将结果发送至邮箱: <a href="mailto:contact@alikia2x.com">contact@alikia2x.com</a>
或 QQ<a href="https://qm.qq.com/q/WS8zyhlcEU">1559913735</a>,并附上自己的设备信息 或 QQ<a href="https://qm.qq.com/q/WS8zyhlcEU">1559913735</a>,并附上自己的设备信息
(例如,手机型号、电脑的 CPU 型号等)。<br /> (例如,手机型号、电脑的 CPU 型号等)。<br/>
我们会根据测试结果,优化我们的实现,使性能更优。<br /> 我们会根据测试结果,优化我们的实现,使性能更优。<br/>
感谢你的支持!<br /> 感谢你的支持!<br/>
</p> </p>
</div> </div>
</div> </div>