61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
import type { ArchiveImport } from '@faunosteo/core-wasm/types'
|
|
import { coreRuntime } from '@/core/runtime'
|
|
import { ImportError, pdfImportOf } from '@/core/worker/outcome'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { bundledSheet } from './sheet'
|
|
|
|
export { ImportError }
|
|
|
|
/**
|
|
* The application's PDF import, in-process for the tests (R4.8, the TypeScript scanner
|
|
* gone): the module's reader (`SheetHandle.import_pdf`, R4.2) on a handle of the bundled
|
|
* sheet, its envelope mapped as HomeRoute maps the worker's settlement (`pdfImportOf`). The
|
|
* page never runs the reader on its main instance (OD-1: the import worker does, on a
|
|
* handle of its own); these helpers give the tests the same reading, same kinds, without a
|
|
* worker thread. The route suites cover the worker itself.
|
|
*/
|
|
export async function importReportFromPdf(
|
|
bytes: Uint8Array,
|
|
): Promise<ArchiveImport> {
|
|
const bundle = JSON.stringify(await bundledSheet())
|
|
const runtime = coreRuntime()
|
|
if (!(await runtime.ready())) throw new ImportError('core_unavailable')
|
|
const out = runtime.invoke<string>((glue) => {
|
|
const handle = new glue.SheetHandle(bundle)
|
|
try {
|
|
return { ok: true, value: handle.import_pdf(bytes) }
|
|
} finally {
|
|
handle.free()
|
|
}
|
|
})
|
|
if (!out.ok) throw new ImportError('core_unavailable', null, out.kind)
|
|
return pdfImportOf({
|
|
kind: 'result',
|
|
out: out.value,
|
|
diag: { ms: 0, linearBytes: null, chunks: 0 },
|
|
})
|
|
}
|
|
|
|
/**
|
|
* The core's layered reading of an attachment already parsed (`archive.import` on the
|
|
* loaded sheet): the v3 report as is, a v2 migrated through the sheet's map, the envelope
|
|
* kept or its snapshot dropped with a reason, then the rebase onto that sheet. The kinds
|
|
* are the worker's.
|
|
*/
|
|
export function importAttachment(
|
|
attachment: unknown,
|
|
index: SheetIndex,
|
|
): ArchiveImport {
|
|
const outcome = index.ops.importArchive(attachment)
|
|
if (outcome.ok) return outcome.value
|
|
switch (outcome.kind) {
|
|
case 'incompatible_version':
|
|
throw new ImportError('incompatible_version', outcome.found ?? null)
|
|
case 'no_attachment':
|
|
case 'lone_surrogate':
|
|
throw new ImportError(outcome.kind)
|
|
default:
|
|
throw new ImportError('core_unavailable', null, outcome.kind)
|
|
}
|
|
}
|