mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-19 21:32:20 +03:00
Merged via /merge-batch (v3.8.51 provenance sweep). Boarded and validated together with the batch's other provenance/dependency PRs in a combined worktree — full gate suite green. Thank you.
167 lines
5.4 KiB
TypeScript
167 lines
5.4 KiB
TypeScript
import { existsSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { Worker } from "node:worker_threads";
|
|
|
|
import { findDeepSeekPowNonce, MAX_DEEPSEEK_POW_DIFFICULTY } from "./deepseek-pow-hash.js";
|
|
|
|
const DEEPSEEK_POW_ALGORITHM = "DeepSeekHashV1";
|
|
const SHA3_256_HEX_PATTERN = /^[a-f0-9]{64}$/i;
|
|
const MAX_DIFFICULTY = MAX_DEEPSEEK_POW_DIFFICULTY;
|
|
const MAX_SALT_LENGTH = 1_024;
|
|
const MAX_CONCURRENT_WORKERS = 2;
|
|
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
const MAX_TIMEOUT_MS = 30_000;
|
|
|
|
let activeWorkerCount = 0;
|
|
|
|
export interface SolveDeepSeekPowOptions {
|
|
signal?: AbortSignal | null;
|
|
timeoutMs?: number;
|
|
}
|
|
|
|
interface ValidatedChallenge {
|
|
challenge: string;
|
|
prefix: string;
|
|
difficulty: number;
|
|
}
|
|
|
|
function createAbortError(): Error {
|
|
const error = new Error("DeepSeek PoW computation aborted");
|
|
error.name = "AbortError";
|
|
return error;
|
|
}
|
|
|
|
function validateChallenge(
|
|
algorithm: string,
|
|
challenge: string,
|
|
salt: string,
|
|
difficulty: number,
|
|
expireAt: number
|
|
): ValidatedChallenge {
|
|
if (algorithm !== DEEPSEEK_POW_ALGORITHM) {
|
|
throw new Error(`Unsupported DeepSeek PoW algorithm: ${algorithm}`);
|
|
}
|
|
if (!SHA3_256_HEX_PATTERN.test(challenge)) {
|
|
throw new Error("DeepSeek PoW challenge must be a 64-character SHA3-256 hex digest");
|
|
}
|
|
if (typeof salt !== "string" || salt.length === 0 || salt.length > MAX_SALT_LENGTH) {
|
|
throw new Error(`DeepSeek PoW salt must contain 1-${MAX_SALT_LENGTH} characters`);
|
|
}
|
|
if (!Number.isSafeInteger(difficulty) || difficulty < 1 || difficulty > MAX_DIFFICULTY) {
|
|
throw new Error(`DeepSeek PoW difficulty must be an integer from 1 to ${MAX_DIFFICULTY}`);
|
|
}
|
|
if (!Number.isSafeInteger(expireAt) || expireAt < 0) {
|
|
throw new Error("DeepSeek PoW expiry must be a non-negative safe integer");
|
|
}
|
|
|
|
return {
|
|
challenge: challenge.toLowerCase(),
|
|
prefix: `${salt}_${expireAt}_`,
|
|
difficulty,
|
|
};
|
|
}
|
|
|
|
function resolveTimeoutMs(timeoutMs: number | undefined): number {
|
|
if (timeoutMs === undefined) return DEFAULT_TIMEOUT_MS;
|
|
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > MAX_TIMEOUT_MS) {
|
|
throw new Error(`DeepSeek PoW timeout must be an integer from 1 to ${MAX_TIMEOUT_MS}ms`);
|
|
}
|
|
return timeoutMs;
|
|
}
|
|
|
|
function solveSynchronously({ challenge, prefix, difficulty }: ValidatedChallenge): number {
|
|
return findDeepSeekPowNonce(prefix, challenge, difficulty);
|
|
}
|
|
|
|
function resolveWorkerPath(): string {
|
|
const tracedPath = path.join(process.cwd(), "open-sse/lib/deepseek-pow-worker.mjs");
|
|
if (existsSync(tracedPath)) return tracedPath;
|
|
return fileURLToPath(new URL("./deepseek-pow-worker.mjs", import.meta.url));
|
|
}
|
|
|
|
function solveInWorker(
|
|
validated: ValidatedChallenge,
|
|
options: SolveDeepSeekPowOptions
|
|
): Promise<number> {
|
|
const { signal } = options;
|
|
if (signal?.aborted) return Promise.reject(createAbortError());
|
|
if (activeWorkerCount >= MAX_CONCURRENT_WORKERS) {
|
|
return Promise.reject(
|
|
new Error(`DeepSeek PoW worker capacity reached (${MAX_CONCURRENT_WORKERS})`)
|
|
);
|
|
}
|
|
|
|
const timeoutMs = resolveTimeoutMs(options.timeoutMs);
|
|
activeWorkerCount += 1;
|
|
|
|
return new Promise<number>((resolve, reject) => {
|
|
const worker = new Worker(resolveWorkerPath(), { workerData: validated });
|
|
let settled = false;
|
|
|
|
const cleanup = () => {
|
|
clearTimeout(timeout);
|
|
signal?.removeEventListener("abort", onAbort);
|
|
activeWorkerCount = Math.max(0, activeWorkerCount - 1);
|
|
};
|
|
const finish = (callback: () => void, terminate: boolean) => {
|
|
if (settled) return;
|
|
settled = true;
|
|
cleanup();
|
|
if (terminate) void worker.terminate();
|
|
callback();
|
|
};
|
|
const onAbort = () => {
|
|
finish(() => reject(createAbortError()), true);
|
|
};
|
|
const timeout = setTimeout(() => {
|
|
finish(() => reject(new Error(`DeepSeek PoW computation exceeded ${timeoutMs}ms`)), true);
|
|
}, timeoutMs);
|
|
|
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
worker.once("message", (answer: unknown) => {
|
|
if (!Number.isSafeInteger(answer) || (answer as number) < -1) {
|
|
finish(() => reject(new Error("DeepSeek PoW worker returned an invalid answer")), true);
|
|
return;
|
|
}
|
|
finish(() => resolve(answer as number), false);
|
|
});
|
|
worker.once("error", (error) => {
|
|
finish(() => reject(error), true);
|
|
});
|
|
worker.once("exit", (code) => {
|
|
if (code !== 0) {
|
|
finish(() => reject(new Error(`DeepSeek PoW worker exited with code ${code}`)), false);
|
|
} else {
|
|
finish(() => reject(new Error("DeepSeek PoW worker exited without an answer")), false);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function solveDeepSeekPowAsync(
|
|
algorithm: string,
|
|
challenge: string,
|
|
salt: string,
|
|
difficulty: number,
|
|
expireAt: number,
|
|
options: SolveDeepSeekPowOptions = {}
|
|
): Promise<number> {
|
|
const validated = validateChallenge(algorithm, challenge, salt, difficulty, expireAt);
|
|
return solveInWorker(validated, options);
|
|
}
|
|
|
|
export function solveDeepSeekPow(
|
|
algorithm: string,
|
|
challenge: string,
|
|
salt: string,
|
|
difficulty: number,
|
|
expireAt: number
|
|
): number {
|
|
// Compatibility-only synchronous API. The validated 250k ceiling bounds CPU
|
|
// use; request handling must use solveDeepSeekPowAsync() so hashing stays off
|
|
// the event loop and remains abortable.
|
|
const validated = validateChallenge(algorithm, challenge, salt, difficulty, expireAt);
|
|
return solveSynchronously(validated);
|
|
}
|