feat(app): R4.3b/c import worker, client, controller; HomeRoute on the disposable worker
Protocol v1 (task → ready → result | failed with phase and kind), the worker entry with the glue bundled statically and the page's compiled Module posted, the file streamed into the module's memory; client with start and task timers (paused while hidden), settle-once, terminate always, error events never masked; module-level controller (one import at a time, cancel on leaving Home, worker chunk prefetched once the engine is loaded); the settlement mapped to the B.1.8 messages (OD-5: a trap, a timeout or an unreadable file is « Erreur de lecture du PDF. », never a corrupt PDF). HomeRoute keeps its routing and size pre-checks, loads its own index after acceptance, drops a result when the session changed. Tests: handler and client in-process, the corpus message test (O8 51, O7 stored inputs, O2/O3/Step 5), the six existing suites migrated to the real worker logic.
This commit is contained in:
1 parent
52471dbbc0
commit
6d1cce6e06
26 files changed
+2164
-247
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { useRef, useState } from 'react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import type {
|
||||
ArchiveReadOutcome,
|
||||
RebaseOutcome,
|
||||
@@ -10,21 +10,23 @@ import { CoreStoppedBanner } from '@/components/report/CoreStoppedNotice'
|
||||
import ReplaceSessionDialog from '@/components/report/ReplaceSessionDialog'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Card from '@/components/ui/Card'
|
||||
import {
|
||||
importAttachment,
|
||||
ImportError,
|
||||
MAX_ATTACHMENT_BYTES,
|
||||
MAX_PDF_BYTES,
|
||||
readAttachment,
|
||||
readRescueCopy,
|
||||
} from '@/pdf/import'
|
||||
import { coreRuntime, coreStopped } from '@/core/runtime'
|
||||
import type { ImportSettlement } from '@/core/worker/client'
|
||||
import { cancelImport, startImport } from '@/core/worker/controller'
|
||||
import { MAX_COPY_BYTES, MAX_PDF_BYTES } from '@/core/worker/handler'
|
||||
import { copyRestoreOf, ImportError, pdfImportOf } from '@/core/worker/outcome'
|
||||
import type { ImportTaskKind } from '@/core/worker/protocol'
|
||||
import {
|
||||
isSessionInProgress,
|
||||
useReportStore,
|
||||
type ImportedSession,
|
||||
} from '@/state/reportStore'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadSheetIndex, useSheetAvailability } from '@/sheets/useSheet'
|
||||
import {
|
||||
loadSheetBundleText,
|
||||
loadSheetIndex,
|
||||
useSheetAvailability,
|
||||
} from '@/sheets/useSheet'
|
||||
import { DEFAULT_SPECIES_ID } from './NewReportRoute'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { coreFailureMessage } from '@/core/failure'
|
||||
@@ -119,6 +121,8 @@ function importErrorMessage(err: unknown, t: Translate): string {
|
||||
return t('home.import.error_invalid_copy')
|
||||
case 'core_unavailable':
|
||||
return coreFailureMessage(t, 'home.import.error_core_unavailable')
|
||||
case 'generic':
|
||||
return t('home.import.error_generic')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,20 +147,39 @@ function firstContentByte(head: Uint8Array): number | undefined {
|
||||
* is JSON (an object) or is named as such; anything else goes the PDF path and its error.
|
||||
*/
|
||||
function isRescueCopy(file: File, head: Uint8Array): boolean {
|
||||
const start = new TextDecoder('latin1').decode(head.slice(0, PDF_MAGIC.length))
|
||||
const start = new TextDecoder('latin1').decode(
|
||||
head.slice(0, PDF_MAGIC.length),
|
||||
)
|
||||
if (start === PDF_MAGIC) return false
|
||||
if (firstContentByte(head) === 0x7b) return true
|
||||
return file.name.toLowerCase().endsWith('.json') || file.type === 'application/json'
|
||||
return (
|
||||
file.name.toLowerCase().endsWith('.json') ||
|
||||
file.type === 'application/json'
|
||||
)
|
||||
}
|
||||
|
||||
/** The copy's text, refused (never altered) when it is over the cap or not valid UTF-8. */
|
||||
async function readCopyText(file: File): Promise<string> {
|
||||
if (file.size > MAX_ATTACHMENT_BYTES) throw new ImportError('invalid_copy')
|
||||
try {
|
||||
return new TextDecoder('utf-8', { fatal: true }).decode(await file.arrayBuffer())
|
||||
} catch {
|
||||
throw new ImportError('invalid_copy')
|
||||
/**
|
||||
* One task in the disposable import worker (R4.3, plan §B.1.5): the page refuses up front
|
||||
* when its own engine stopped, hands the file (never read here), the sheet payload and
|
||||
* its compiled module to the worker, and reads the settlement. Diagnostics in DEV only.
|
||||
*/
|
||||
async function runImportTask(
|
||||
task: ImportTaskKind,
|
||||
file: File,
|
||||
): Promise<ImportSettlement> {
|
||||
if (coreStopped()) throw new ImportError('core_unavailable')
|
||||
const sheet = await loadSheetBundleText(DEFAULT_SPECIES_ID)
|
||||
if (!sheet) throw new ImportError('core_unavailable')
|
||||
const module = await (
|
||||
coreRuntime().compiledModule() ?? Promise.resolve(null)
|
||||
).catch(() => null)
|
||||
const settlement = await startImport({ task, file, sheet, module })
|
||||
// Diagnostics only (never the outcome: it holds the bilan's names), DEV console only.
|
||||
if (import.meta.env.DEV) {
|
||||
const diag = settlement.kind === 'result' ? settlement.diag : settlement
|
||||
console.debug('[import]', task, diag)
|
||||
}
|
||||
return settlement
|
||||
}
|
||||
|
||||
export default function HomeRoute() {
|
||||
@@ -169,48 +192,64 @@ export default function HomeRoute() {
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [confirmingReplace, setConfirmingReplace] = useState(false)
|
||||
const t = useT()
|
||||
// Leaving Home cancels a running import: its worker is terminated, its result dropped.
|
||||
useEffect(() => cancelImport, [])
|
||||
|
||||
/** A Faunosteo PDF: its attachment, read and rebased by the core on the loaded sheet. */
|
||||
async function readPdf(file: File): Promise<ImportRead> {
|
||||
// The engine refuses the same size; checking first keeps a huge file out of memory.
|
||||
if (file.size > MAX_PDF_BYTES) throw new ImportError('invalid_pdf')
|
||||
const bytes = new Uint8Array(await file.arrayBuffer())
|
||||
const attachment = await readAttachment(bytes)
|
||||
/** The page's own index, loaded after the worker accepted the file, for `hydrateImport`. */
|
||||
async function mainIndex(): Promise<SheetIndex> {
|
||||
const index = await loadSheetIndex(DEFAULT_SPECIES_ID)
|
||||
if (!index) throw new ImportError('core_unavailable')
|
||||
const { read: outcome, rebase } = importAttachment(attachment, index)
|
||||
return index
|
||||
}
|
||||
|
||||
/** A Faunosteo PDF: its attachment, read and rebased by the worker's core on the sheet. */
|
||||
async function readPdf(file: File): Promise<ImportRead> {
|
||||
// The worker and the core refuse the same size; checking first reads nothing.
|
||||
if (file.size > MAX_PDF_BYTES) throw new ImportError('invalid_pdf')
|
||||
const { read: outcome, rebase } = pdfImportOf(
|
||||
await runImportTask('pdf', file),
|
||||
)
|
||||
return {
|
||||
session: { report: rebase.report, archive: outcome.archive },
|
||||
index,
|
||||
index: await mainIndex(),
|
||||
lines: importMessages(outcome, rebase, t),
|
||||
}
|
||||
}
|
||||
|
||||
/** A rescue copy: read before any session change, so a refusal leaves the tab untouched. */
|
||||
async function readCopy(file: File): Promise<ImportRead> {
|
||||
const text = await readCopyText(file)
|
||||
const index = await loadSheetIndex(DEFAULT_SPECIES_ID)
|
||||
if (!index) throw new ImportError('core_unavailable')
|
||||
const rebase = readRescueCopy(text, index)
|
||||
if (file.size > MAX_COPY_BYTES) throw new ImportError('invalid_copy')
|
||||
const { rebase } = copyRestoreOf(await runImportTask('copy', file))
|
||||
const count = String(rebase.unresolvedZones + rebase.unresolvedFindings)
|
||||
return {
|
||||
session: { report: rebase.report, archive: null },
|
||||
index,
|
||||
lines: rebase.rebased ? [t('editor.draft_rebased').replace('{count}', count)] : [],
|
||||
index: await mainIndex(),
|
||||
lines: rebase.rebased
|
||||
? [t('editor.draft_rebased').replace('{count}', count)]
|
||||
: [],
|
||||
}
|
||||
}
|
||||
|
||||
/** Read, rebase onto the loaded sheet, open; the editor always works on that sheet. */
|
||||
async function importFile(file: File) {
|
||||
const head = new Uint8Array(await file.slice(0, SNIFF_BYTES).arrayBuffer())
|
||||
const read = isRescueCopy(file, head) ? await readCopy(file) : await readPdf(file)
|
||||
const reportAtPick = useReportStore.getState().report
|
||||
const read = isRescueCopy(file, head)
|
||||
? await readCopy(file)
|
||||
: await readPdf(file)
|
||||
// The session changed under the import (another tab action): its result is dropped.
|
||||
if (useReportStore.getState().report !== reportAtPick) return
|
||||
const opened = hydrateImport(read.session, read.index)
|
||||
// A tab text that could not be set aside would be overwritten: resolve it first.
|
||||
if (!opened.ok) {
|
||||
// A core failure reading the tab's draft is an engine error, not a held text.
|
||||
if (opened.reason !== 'held') throw new ImportError('core_unavailable')
|
||||
window.alert(
|
||||
t(opened.engine ? 'session.held_blocked_engine' : 'session.held_blocked'),
|
||||
t(
|
||||
opened.engine
|
||||
? 'session.held_blocked_engine'
|
||||
: 'session.held_blocked',
|
||||
),
|
||||
)
|
||||
navigate('/editor')
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user