@lp-labs/document-conversion-jobs
v0.1.0
Published
Bounded single-executor document conversion jobs using injected byte decoders and renderers.
Readme
@lp-labs/document-conversion-jobs
Bounded single-executor document conversion jobs using injected byte decoders and renderers.
Original, independent MIT implementation. Zero runtime, peer or sibling dependencies. Requires Node.js 18+; strict TypeScript declarations support both ESM and CommonJS.
Install
npm install @lp-labs/document-conversion-jobsQuickstart
import { createConversionExecutor, ConversionError } from '@lp-labs/document-conversion-jobs';
const executor = createConversionExecutor(
{
async decode(bytes, context) {
context.progress(1);
return new TextDecoder().decode(bytes);
},
async render(text, context) {
context.progress(1);
return new TextEncoder().encode(text.toUpperCase());
},
},
{ maxJobs: 2 },
);
const job = executor.submit('example', new TextEncoder().encode('notes'));
if (new TextDecoder().decode(await job.result) !== 'NOTES') throw new Error('Conversion mismatch');
executor.forget('example');
if (!(new ConversionError('invalid', 'Example') instanceof Error))
throw new Error('Error contract');API and types
createConversionExecutor<Document>(adapter: ConversionAdapter<Document>, options?): ConversionExecutor uses injected asynchronous decode and render stages, each with a ConversionContext AbortSignal and progress(fraction). submit(id, bytes): ConversionJob copies input, starts FIFO execution, and returns result Promise, status() and cancel(). Status is JobStatus (id, JobState, progress, error). decode progress maps to 0..0.5, render to 0.5..1 monotonically. statuses() returns copies; forget(id) removes only terminal jobs. ConversionError.code is capacity, duplicate, cancelled or invalid; adapter failures are preserved as rejected results.
Customize
Supply your own trusted decoders/renderers and honor AbortSignal. Set maxJobs/maxBytes for your workload. Release terminal history with forget; consume result rejections. An internal catch prevents abandoned result promises from generating unhandled rejection warnings.
Errors and bounds
Default maxJobs=32 (1..1024), maxBytes=16,777,216 (1..67,108,864) for each input/output. Counts include queued/running/terminal jobs until forgotten. One adapter chain runs at once. Cancellation rejects result immediately and signals the adapter; another job starts only when that chain settles, so uncooperative adapters can stall execution. Memory is O(maxJobs*maxBytes) plus adapter-owned state. No fake conversions, format detection, codec, PDF/Word engine or worker pool is bundled.
Invalid runtime inputs throw TypeError/RangeError or the documented typed error. Adapter errors propagate unchanged unless specifically noted; asynchronous methods reject. Inputs must be ordinary typed data, not hostile proxies or accessors. No I/O happens at import time. All runtime exports are documented above and the example executes without optional packages.
Development
npm ci --offline --ignore-scripts --no-audit --no-fund
npm run build
npm run typecheck
npm test
npm run format:checkBuild emits dist/esm and dist/cjs with matching declarations. Tests use Node's test runner; consumer.mts and consumer.cts validate meaningful bare-package imports and expected type errors. Only TypeScript ^5.9.3 and Prettier ^3.6.2 are development dependencies.
Support
If this library is useful to you, consider supporting its maintenance with a donation via PayPal.
Author
Created by Libin Prasanth. Connect on LinkedIn for updates and discussion.
