Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions components/cipher/CipherExecutionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { useCipherWorker } from "../../hooks/useCipherWorker";
import { clampStepIndex } from "../../lib/utils/visualizerPermalink";
import { resolveProvenance } from "../../lib/provenance/resolve";
import type { DataProvenanceMetadata } from "../../lib/provenance";
import { saveConversionHistory, type ConversionHistoryEntry } from "../../lib/utils/conversionHistory";

import { saveConversionHistory, type ConversionHistoryEntry } from "@/lib/utils/conversionHistory";
import { createVirtualizedCipherResult } from "@/lib/cipher/stepVirtualization";
interface Params {
cipher: CipherDefinition;
input: string;
Expand All @@ -33,10 +33,12 @@ export function buildCipherWorkerOptions(
demoMode: boolean,
): CipherOptions {
const workerOptions: CipherOptions = {
instrument: true,
signal: undefined,
...options,
};
instrument: true,
signal: undefined,
traceBufferSize: 32,
traceBatchSize: 32,
...options,
};
if (["des", "3des", "aes", "camellia"].includes(cipherId)) {
workerOptions.hexInput = typeof options.hexInput === "boolean" ? options.hexInput : true;
}
Expand All @@ -60,10 +62,10 @@ export function useCipherExecutionController({ cipher, input, key, action, autoC
const abortRef = useRef<AbortController | null>(null);

const run = useCallback(async () => {
abortRef.current?.abort();
const controller = new AbortController();
abortRef.current = controller;
onError(null);
abortRef.current?.abort();

const controller = new AbortController();
abortRef.current = controller; onError(null);

try {
const workerOptions = buildCipherWorkerOptions(cipher.id, options, demoMode);
Expand All @@ -76,10 +78,20 @@ export function useCipherExecutionController({ cipher, input, key, action, autoC
const provenance = isSimulated(cipher.id, demoMode)
? resolveProvenance({ provenance: "simulated", source: "CryptoViz educational simulation" } as DataProvenanceMetadata)
: resolveProvenance(result.metadata?.provenance);
const nextResult: CipherResult = { ...result, metadata: { ...result.metadata, provenance } };
onResult(nextResult);
onStepRestore(clampStepIndex(0, nextResult.steps?.length ?? 0));
const nextResult: CipherResult = {
...result,
metadata: {
...result.metadata,
provenance,
},
};

const visualizedResult = createVirtualizedCipherResult(nextResult);

onResult(visualizedResult);
onStepRestore(
clampStepIndex(0, visualizedResult.steps?.length ?? 0),
);
if (nextResult.output !== undefined) {
const entry: ConversionHistoryEntry = {
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
Expand Down
16 changes: 7 additions & 9 deletions components/cipher/CipherLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useRouter } from "next/navigation";
import type { CipherDefinition } from "@/lib/cipher/registry";
import type { CipherResult, CipherOptions } from "@/lib/cipher/types";
import type { AnimationSpeed } from "./StepAnimator";
import { createVirtualizedCipherResult } from "@/lib/cipher/stepVirtualization";
import WorkspacePresetManager from "./WorkspacePresetManager";
import ConversionHistory from "./ConversionHistory";
import WhereIsThisUsed from "./WhereIsThisUsed";
Expand Down Expand Up @@ -208,11 +207,7 @@ export default function CipherLayout({ cipher }: CipherLayoutProps) {
};

const direction = cipher.id === "dh" ? "encrypt" : action;
const virtualizedResult = useMemo(
() => (result ? createVirtualizedCipherResult(result) : null),
[result],
);
const activeStep = result?.steps?.[currentStep];
const virtualizedResult = result; const activeStep = result?.steps?.[currentStep];
const annotationScope = { cipherId: cipher.id, direction: direction as "encrypt" | "decrypt" };
const scopeAnnotations = getScopeAnnotations(annotationStore, annotationScope);
const activeStepId = activeStep ? createStableStepId(activeStep.label, currentStep) : null;
Expand Down Expand Up @@ -442,9 +437,12 @@ export default function CipherLayout({ cipher }: CipherLayoutProps) {
</div>
</div>
<StepAnimator
steps={virtualizedResult?.steps ?? result.steps}
stepMetadata={virtualizedResult?.stepMetadata}
currentStep={currentStep}
steps={result.steps}
stepMetadata={
"stepMetadata" in result
? result.stepMetadata
: undefined
} currentStep={currentStep}
onStepChange={handleStepChange}
speed={animationSpeed}
onSpeedChange={setAnimationSpeed}
Expand Down
56 changes: 35 additions & 21 deletions hooks/useCipherWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import { useCallback, useEffect, useRef, useState } from 'react'
import type { CipherResult } from '@/lib/cipher/types'
import type { WorkerRequest, WorkerResponse } from '@/types/worker'
import type { WorkerPriority } from '@/lib/workers/pool'
import type {
WorkerRequest,
WorkerResponse,
WorkerTraceBatchMessage,
WorkerTraceStartMessage,
WorkerTraceCompleteMessage,
} from '@/types/worker'import type { WorkerPriority } from '@/lib/workers/pool'

Check failure on line 11 in hooks/useCipherWorker.ts

View workflow job for this annotation

GitHub Actions / Static checks

';' expected.
import type { WorkerProgressMessage } from '@/lib/workers/cipher-worker-protocol'
import { CipherError } from '@/lib/utils/errors'
import { decodeCipherSteps } from '@/lib/workers/stepTransfer'
import { CipherError, type CipherErrorCode } from '@/lib/utils/errors'import { decodeCipherSteps } from '@/lib/workers/stepTransfer'

Check failure on line 13 in hooks/useCipherWorker.ts

View workflow job for this annotation

GitHub Actions / Static checks

';' expected.

const MAX_CACHE_SIZE = 200
const WORKER_TIMEOUT_MS = 10000
Expand Down Expand Up @@ -34,8 +38,9 @@
timeoutId: ReturnType<typeof setTimeout>
cacheKey: string | null
onProgress?: (percent: number, message: string) => void
traceSteps: import('@/lib/cipher/types').CipherStep[]
traceTotal: number
}

function sortObjectKeys(obj: unknown): unknown {
if (obj === null || typeof obj !== 'object') return obj
if (Array.isArray(obj)) return obj.map(sortObjectKeys)
Expand Down Expand Up @@ -130,12 +135,20 @@
reject(error)
}
} else {
const errorMsg = payload?.error ?? 'Operation failed in worker'
const code = payload?.errorCode
const cipherErr = code && code !== 'INVALID_WORKER_MESSAGE'
? new CipherError(code, errorMsg)
: new Error(errorMsg)
setError(errorMsg)
const errorMsg =
payload?.error ??
payload?.errorMessage ??
'Operation failed in worker'

const code = payload?.errorCode

const cipherErr =
code && code !== 'INVALID_WORKER_MESSAGE'
? new CipherError(code as CipherErrorCode, errorMsg, {
details: payload?.errorDetails,
remediation: payload?.remediation,
})
: new Error(errorMsg) setError(errorMsg)

Check failure on line 151 in hooks/useCipherWorker.ts

View workflow job for this annotation

GitHub Actions / Static checks

',' expected.

Check failure on line 151 in hooks/useCipherWorker.ts

View workflow job for this annotation

GitHub Actions / Static checks

',' expected.
reject(cipherErr)
}
}
Expand Down Expand Up @@ -254,16 +267,17 @@
reject(new Error('WORKER_TIMEOUT'))
}, WORKER_TIMEOUT_MS)

activeRequestsRef.current.set(id, {
resolve,
reject,
signal,
onAbort,
timeoutId,
cacheKey,
onProgress: options?.onProgress,
})
setLoading(true)
activeRequestsRef.current.set(id, {
resolve,
reject,
signal,
onAbort,
timeoutId,
cacheKey,
onProgress: options?.onProgress,
traceSteps: [],
traceTotal: 0,
}) setLoading(true)

Check failure on line 280 in hooks/useCipherWorker.ts

View workflow job for this annotation

GitHub Actions / Static checks

';' expected.
setError(null)
setProgress({ percent: 0, currentMilestone: 'Queued', jobId: id })

Expand Down
Loading
Loading