import type { ImportJob, IssuePhase, StoredIssue } from '../job/types.js'; import type { Sha256 } from '../ports/hasher.js'; import type { JobAdmin, UploadedFile } from '../ports/jobAdmin.js'; import type { JobRequests } from '../ports/jobRequests.js'; import type { RenderMessage } from '../report/errorsCell.js'; import type { ImportEntity } from '../types.js'; /** The size of a stored chunk (§ 8.4): the table refuses more. */ export declare const CHUNK_BYTES = 1048576; export interface ImportServiceDeps { jobs: JobAdmin; sha256: Sha256; /** The most bytes an upload may have (`maxFileSizeMb`, § 8.7). */ maxFileBytes: number; /** The sentence for a problem in the admin's language; the stored English one without it. */ render?: RenderMessage; /** For a test that wants small chunks. */ chunkBytes?: number; /** * Asked for a worker whenever a job starts waiting for one (`uploaded` or * `queued`). Without it nothing starts a worker: the dispatch cron does. */ requests?: Pick; } export interface UploadArgs { entity: unknown; mode?: unknown; checkImages?: unknown; fileName: unknown; createdBy: number | null; /** The file as it arrives. Nothing is parsed. */ file: AsyncIterable; } /** An upload whose file has arrived whole but whose swap has not been made (`receive`). */ export interface ReceivedUpload { /** The new job, still `uploading`. */ job: ImportJob; entity: ImportEntity; file: UploadedFile; } export interface IssuesArgs { phase?: unknown; severity?: unknown; code?: unknown; column?: unknown; page?: unknown; limit?: unknown; } export interface IssuesView { job: ImportJob; phase: IssuePhase; page: number; limit: number; /** All the problems that match the filters. */ total: number; items: StoredIssue[]; /** The totals per code of the phase: complete also above the cap on stored problems. */ byCode: Record; /** More problems exist than are listed. */ truncated: boolean; } /** The name the admin's browser sent, as a name that is safe to show and to keep. */ export declare function cleanFileName(value: unknown): string; /** * The actions on import jobs (§ 8.3), over ports only: the upload and its swap, * Start, Cancel, Resume, Check again, Delete, and the problems of a job. It * checks the request and says no with an `ImportApiError`; the REST handlers * (not built yet) only parse the request, call it and send the answer. */ export declare class ImportService { private readonly deps; private readonly chunkBytes; constructor(deps: ImportServiceDeps); /** * Streams the file into the job's chunks, then swaps (D-11): the entity's * unstarted jobs are deleted only now, when the new file has arrived whole, so * an upload that fails or is dropped costs the admin nothing. Does no parsing. */ upload(args: UploadArgs): Promise<{ job: ImportJob; replaced: boolean; }>; /** * The first half of an upload: the file goes into the chunks of a new job that * is still `uploading`. Nothing of the entity is changed yet, so a caller that * has more to check in the request (the multipart parser) can still refuse it * and `discard` the job at no cost. `finalize` does the swap. */ receive(args: UploadArgs): Promise; /** * The second half: the swap (the entity's unstarted jobs go, a stopped one that * wrote something is closed), the job becomes `uploaded`, and the worker is * asked to check it. */ finalize(received: ReceivedUpload): Promise<{ job: ImportJob; replaced: boolean; }>; /** Throws away a received upload that was not finalized. A job that is not `uploading` is left alone. */ discard(received: ReceivedUpload): Promise; get(jobId: number): Promise; /** The job by the uuid the API gives it; a text that is not a uuid is a job that is not there. */ byUuid(uuid: unknown): Promise; issues(jobId: number, args?: IssuesArgs): Promise; start(jobId: number): Promise; cancel(jobId: number): Promise<{ job: ImportJob; how: 'canceled' | 'requested'; }>; resume(jobId: number): Promise; recheck(jobId: number): Promise; /** Deletes the job and gives back what it was. */ remove(jobId: number): Promise; listOpen(): Promise; /** * The job waits for a worker now. The action has been done, so a request that * cannot be made is not the admin's problem: the dispatch cron asks again for * a job that keeps waiting. */ private requestWorker; private parsePhase; private busy; private alreadyRunning; private refusal; }