Skip to content

feat(convert): @p10node/convert, server-side conversion (CLI + HTTP service with Gotenberg's routes) (#43) - #56

Merged
pierreneter merged 3 commits into
p10node:mainfrom
mkroemer:feat/convert-server
Nov 27, 2026
Merged

pierreneter merged 3 commits into
p10node:mainfrom
mkroemer:feat/convert-server

Conversation

@mkroemer

Copy link
Copy Markdown
Contributor

Closes #43 (except PDF, which is #44).

What it does

A new package, @p10node/convert, runs office-engine on a server with no browser and no LibreOffice. It works on Node ≥ 22 and Bun ≥ 1.3.

  • convert(input, { to }) converts in-process. It hides docx.start / docx:done / xlsx:progress and returns { data, name, type, ext, kind, from?, warnings }.
    • A document can be written as docx, txt, md or html.
    • A workbook can be written as xlsx, csv or tsv (sheet: an index or a name).
    • A presentation can be written as pptx or txt.
    • Any format the viewer opens (.doc, .odt, .pages, .csv, .md…) is accepted as input.
  • Converter is a pool on node:worker_threads, which works on both Node and Bun.
    • A file that runs past its timeout, or whose caller gives up, ends its thread.
    • The pool has a queue limit, a byte budget for input that is running or waiting, and optional per-thread heap limits.
    • Each job gets its own copy of the input, so a Node Buffer's shared pool is never transferred.
  • office-convert CLI:
    • office-convert a.doc b.xlsx, office-convert in.doc out.md, -o dir, and - for stdin and stdout;
    • converts files in parallel, with --max-size and --timeout;
    • never writes over an input, including through symlinks or redirected stdin; the check runs on the opened file, so there is no gap between check and write.
  • office-convert serve is a Web-standard fetch handler that runs on Bun.serve, or on node:http through an adapter.
    • POST /convert takes multipart or a raw body. Several files come back as a ZIP.
    • It has /health and /version in Gotenberg's shapes, and honours the Gotenberg-Output-Filename and Gotenberg-Trace headers.
    • Gotenberg's /forms/libreoffice/convert answers 501 until PDF output exists (PDF output without a browser #44). It refuses before reading the body.
    • Limits: maxBytes per request, maxFiles, and a shared maxPending budget for bodies being read. A request over the budget gets 503 and Retry-After.
    • Statuses: 400, 413, 422, 499, 500, 501 and 503, listed in the README.
  • Docker: a oven/bun build stage and a node:26-slim runtime. It runs as a non-root user and has a HEALTHCHECK. The image is about 383 MB, and it builds, starts and reports healthy.

Core changes

  • Workbook.text() awaits settled(), so CSV exported right after load no longer has empty formula values. It also takes a limit and throws a RangeError while writing once the limit is passed.
  • xlsx.text in the protocol takes an optional limit.
  • OpenParams.maxUnpacked limits gzip-compressed inputs (.csv.gz, .svgz); ungzipped() counts the bytes as they are unpacked.

Review

Codex reviewed the branch in two rounds before anything was pushed.

  • Round 1 (fixed in ef9b056):
    • memory budgets;
    • a Node Buffer DataCloneError that wedged the pool;
    • CLI stdout cut short on Node;
    • --force overwriting an input through a symlink;
    • ZIP names that differ only in case;
    • rejected batches still holding slots;
    • leaked abort listeners;
    • a crash on sheet 0.5;
    • CLI reads happening before the size check;
    • .d.ts files under NodeNext;
    • the ambiguous in out form;
    • the Gotenberg route answering 503 under load.
  • Round 2 (fixed in 748ced4):
    • multipart bodies buffered before admission (now covered by the maxPending budget);
    • the CSV pre-check refusing small valid exports (now checked while the CSV is written);
    • redirected stdin being overwritten under --force -o;
    • a time-of-check/time-of-use gap on the output;
    • named FIFOs getting past --max-size;
    • a failed spawn leaving its bytes charged to the budget;
    • the CLI's byte budget dropping valid files;
    • two existing inputs being rejected.

Known limitation: a multipart request is parsed (buffered, up to maxBytes) before maxFiles is checked.

Tests

  • test/convert-service.test.ts adds 20 tests covering conversions, limits, the pool, HTTP and the CLI.
  • Full suite: 1659 pass. The 7 failures also fail on main (encodings on Bun 1.3.6).
  • bun run typecheck is clean and bun run build:packages builds.
  • Checked by hand:
    • all 24 sample files convert on Node from dist and on Bun from source;
    • curl against every route;
    • a timeout gives 503 and the pool recovers;
    • a client hang-up frees its thread;
    • SIGTERM stops the server cleanly;
    • npm pack, then install and npx office-convert.

Before release

🤖 Generated with Claude Code

mkroemer and others added 3 commits November 25, 2026 15:41
…CLI, HTTP service with Gotenberg's routes)

First part of p10node#43. The engine runs in Node or Bun on worker threads and converts
files without LibreOffice:

- convert(bytes, { to }): opens any format the viewer opens, waits until it is
  read whole (docx.start / sheet loading hidden), writes DOCX / XLSX / PPTX, or
  the document as text, Markdown, HTML, CSV / TSV
- Converter: a pool of worker threads; a file past its time limit or whose
  caller gave up ends with its thread, a full queue is refused
- office-convert: files on the command line, several at once, size and time
  limits; `office-convert serve` for the HTTP service
- createHandler: POST /convert (multipart or the file as the body), Gotenberg's
  /forms/libreoffice/convert (501 until PDF, p10node#44), /health, /version,
  Gotenberg-Output-Filename and Gotenberg-Trace, a ZIP for several files
- Dockerfile: built with Bun, runs on node:26-slim with the bundle only

Core: XlsxWorkbook.text() waits for the values computed after reading, so a CSV
written right after opening has the formulas the file gave no value for (it
had them empty before, also in the browser's Download as CSV).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nputs never overwritten

- pool: the bytes are copied before they move to a thread (a Node Buffer can share
  its memory: posting it failed and left the thread busy); a failed dispatch
  rejects its conversion and frees the thread; input bytes running and waiting
  have a budget (`bytes`, 1 GB); a queued conversion's abort listener is removed
  once it leaves the queue
- convert: `maxOutput` (1 GB; a sparse sheet is refused before its CSV is
  written) and `maxUnpacked` (gzip inputs, 1 GB, through a new `maxUnpacked`
  open parameter of the engine); a sheet index must be a whole number
- HTTP: Gotenberg's route answers 501 before the body is read or the pool asked;
  a file of a request failing gives up the others; ZIP names unique without
  their case; `too-large` is 413
- CLI: stdout is written whole before the process ends (exitCode, not exit);
  an output that is one of the inputs under any name (a link) is refused, and
  without --force a file is only created (wx); stdin is refused once past
  --max-size; files are read and converted as threads become free; `in out` with
  an existing output is refused instead of read as two inputs
- types: relative imports of the .d.ts files with .js, for NodeNext

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ds, held bodies

- CSV / TSV: the size limit is applied by the exporter (xlsx.text takes a
  limit): the cells in use are counted, not the sheet's extent, so a blank
  formatted cell far away no longer refuses a small export, and the text is
  counted while it is written
- pool: a thread that cannot start fails its conversion and gives its bytes back
- HTTP: request bodies held at once have a budget (`maxPending`, 1 GB): past it
  503 with Retry-After, before the pool is asked
- CLI: named files are read up to --max-size too (a pipe or a device has no
  size); an output is checked on the file opened, before it is truncated, against
  the inputs and stdin when it is a file; no byte budget (files go one per
  thread); `a.doc b.md` with both existing converts both again (only --force
  with an existing second name is refused)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server-side conversion: @p10node/convert, a CLI and an HTTP service

2 participants