mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
common/ had become the backend's library: 30 of its 41 modules were imported by nothing in the frontend, so the name no longer said what belonged there. It now holds only what the frontend or the build imports too; backend-only code moved to server/, flat except for datasets/ (the dataset engine and its readers). The *-db/ dataset directories stay in common/: self-hosting docs and Docker volumes mount those paths. frontend/utils/ keeps its app-wide modules at the root and gains ip/ (IP / ASN shaping and validation), report/ (the report pipeline) and features/ (the pure layer of a single feature). Paths only — no logic changes. The Dockerfile copies server/, and the AGENTS.md files say where a new module goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
3.8 KiB
JavaScript
121 lines
3.8 KiB
JavaScript
// Run a fixed preview before an independent, user-configured Cloudflare test.
|
|
|
|
export const getSpeedTestSampleCounts = (raw) => ({
|
|
latency: raw.latency?.results?.timings?.length || 0,
|
|
download: Object.values(raw.download?.results || {}).reduce((sum, bucket) => sum + bucket.timings.length, 0),
|
|
upload: Object.values(raw.upload?.results || {}).reduce((sum, bucket) => sum + bucket.timings.length, 0),
|
|
});
|
|
|
|
// Missing formal samples leave the corresponding preview reading untouched.
|
|
export const getSpeedTestLiveValues = (results) => {
|
|
const counts = getSpeedTestSampleCounts(results.raw);
|
|
const metrics = [
|
|
['downloadSpeed', 'getDownloadBandwidth', counts.download, 1e6],
|
|
['uploadSpeed', 'getUploadBandwidth', counts.upload, 1e6],
|
|
['latency', 'getUnloadedLatency', counts.latency, 1],
|
|
['jitter', 'getUnloadedJitter', counts.latency >= 2, 1],
|
|
];
|
|
return Object.fromEntries(metrics.flatMap(([key, getter, hasSamples, divisor]) => {
|
|
if (!hasSamples) return [];
|
|
const value = results[getter]();
|
|
return Number.isFinite(value) ? [[key, Number((value / divisor).toFixed(2))]] : [];
|
|
}));
|
|
};
|
|
|
|
export const createSpeedTestSession = (Engine, packages) => {
|
|
// Snapshot options so the formal test uses the selection made at startup.
|
|
const measurements = [
|
|
{ type: 'latency', numPackets: packages.latency.count },
|
|
{ type: 'download', bytes: packages.download.bytes, count: packages.download.count },
|
|
{ type: 'upload', bytes: packages.upload.bytes, count: packages.upload.count },
|
|
];
|
|
let engine;
|
|
let isProbe = true;
|
|
let running = false;
|
|
let disposed = false;
|
|
let failed = false;
|
|
|
|
const detach = () => {
|
|
engine.onRunningChange = () => {};
|
|
engine.onPhaseChange = () => {};
|
|
engine.onResultsChange = () => {};
|
|
engine.onFinish = () => {};
|
|
engine.onError = () => {};
|
|
};
|
|
|
|
const session = {
|
|
get results() { return engine.results; },
|
|
get isProbe() { return isProbe; },
|
|
onRunningChange: () => {},
|
|
onPhaseChange: () => {},
|
|
onResultsChange: () => {},
|
|
onFinish: () => {},
|
|
onError: () => {},
|
|
play() {
|
|
if (disposed || running) return;
|
|
running = true;
|
|
session.onRunningChange(true);
|
|
engine.play();
|
|
},
|
|
pause() {
|
|
if (disposed) return;
|
|
running = false;
|
|
engine.pause();
|
|
session.onRunningChange(false);
|
|
},
|
|
destroy() {
|
|
disposed = true;
|
|
running = false;
|
|
detach();
|
|
engine.pause();
|
|
},
|
|
};
|
|
|
|
const prepare = () => {
|
|
const current = new Engine({
|
|
autoStart: false,
|
|
...(isProbe ? {
|
|
measurements: [
|
|
{ type: 'latency', numPackets: 2 },
|
|
{ type: 'download', bytes: 1e5, count: 1, bypassMinDuration: true },
|
|
],
|
|
// Even a fast preview must produce a reading; only the formal run is logged.
|
|
bandwidthMinRequestDuration: 0,
|
|
measureDownloadLoadedLatency: false,
|
|
measureUploadLoadedLatency: false,
|
|
logAimApiUrl: null,
|
|
} : { measurements }),
|
|
});
|
|
engine = current;
|
|
const isCurrent = () => !disposed && engine === current;
|
|
current.onPhaseChange = ({ measurement }) => {
|
|
if (isCurrent()) session.onPhaseChange(isProbe ? 'probe' : measurement.type);
|
|
};
|
|
current.onResultsChange = () => {
|
|
if (isCurrent()) session.onResultsChange();
|
|
};
|
|
current.onError = (error) => {
|
|
if (!isCurrent()) return;
|
|
failed = true;
|
|
session.onError(error);
|
|
};
|
|
current.onFinish = (results) => {
|
|
if (!isCurrent()) return;
|
|
// Flush queued final samples before replacing the engine's result object.
|
|
session.onResultsChange();
|
|
if (isProbe && !failed) {
|
|
detach();
|
|
isProbe = false;
|
|
prepare();
|
|
if (running) engine.play();
|
|
} else {
|
|
running = false;
|
|
session.onFinish(results);
|
|
}
|
|
};
|
|
};
|
|
|
|
prepare();
|
|
return session;
|
|
};
|