Skip to content
Bun 1.4 Portable Layer & Image Processing | Omni Manual Book

Bun 1.4 Portable Layer & Image Processing

Omni backend runs on Bun 1.4 with a portable abstraction layer that stays Node/Deno-compatible — plus the /image-processing module.

This page is the reference for Omni’s Bun 1.4 portable runtime layer and the image-processing module built on top of it. The backend is optimized for Bun 1.4 but must keep working on non-Bun runtimes (Node/Deno) with comparable performance.

Every portable function detects the runtime via globalThis.Bun:

  • Bun present → use Bun.* (native fast path).
  • Bun absent → fall back to node:* / addon.

Portable files are framework-agnostic: no @nestjs/* or TypeORM imports. Features that are inherently non-portable (Bun.cron, Bun.WebView) are kept as opt-in seams with Node fallbacks.

Portable modules — backend/src/common/portable/

Section titled “Portable modules — backend/src/common/portable/”
FileContentsNode fallback
runtime.tsisBunRuntime, hashPassword/verifyPassword, sha256/digest, uuid (randomUUIDv7), randomBytes, readTextFile/writeFile, deepEquals, parseJson5, escapeHTMLnode:crypto, node:fs/promises, @node-rs/argon2+bcryptjs, structural walk
scheduler.tsPortableScheduler — Bun.cron vs in-process setIntervalin-process setInterval
markdown.tsBun.markdown.html vs minimal regex→HTMLregex + HTML-escape
image.tsBun.Image pipeline (resize/rotate/flip/modulate/encode webp/jpeg/avif/heic)sharp-like adapter via registerNodeImageAdapter
browser.tsconnectToCdp — Bun.WebView CDP vs puppeteer-corepuppeteer-core
html-pdf.tsHTML/CSS → PDF via takumi-pdf (WebAssembly, no browser)same entry point — wasm instantiated through initSync

Wired into production: PasswordHashService + audit/diff.util.ts → runtime.ts; obscura-cdp.client.ts → browser.ts; modules/rendering/takumi.renderer.ts → html-pdf.ts.

  • Constructor: new Bun.Image(bytes) — not static .from().
  • .width/.height = -1 until await img.metadata() resolves.
  • resize fit supports only 'fill' | 'inside' — no cover/contain/outside/crop.
  • Encode methods (jpeg/png/webp/avif/heic) return a chainable Image; terminal is .bytes().
  • metadata() returns source dimensions even after resize.
  • Design: contain→inside (native), fill→fill; cover/outside throw a clear error (no crop). True cover-crop requires a registered Node adapter (sharp).
  • Open Graph cards use fit:'inside' (Bun-safe).

Endpoints (permission-guarded, tenant-scoped):

Transform

POST /image-processing/transform — dynamic resize/rotate/flip/modulate + re-encode. Returns binary via @Res with X-Image-Width / X-Image-Height headers.

Open Graph

POST /image-processing/open-graph — builds og:image meta tags + card binary (base64). Presets: og 1200×630, twitter-large 1200×675, twitter-summary 1200×630, square 1200×1200.

Inspect

POST /image-processing/inspect — reads dimensions + format without re-encoding.

  • dto/image-processing.dto.ts — ImageTransformDto, BuildOpenGraphDto, MAX_IMAGE_BYTES (10MB).
  • image-processing.service.ts — transformImage/openGraph/inspect; uses the portable processImage/getImageInfo/buildOpenGraphMeta and the OPEN_GRAPH_PRESETS constant.
  • image-processing.controller.ts — FileInterceptor (memoryStorage), permission ImageProcessingCreate/ImageProcessingRead.
  • image-processing.module.ts — registered in app.module.ts.
  • image-processing.service.spec.ts — colocated unit tests (mocks portable layer).

ImageProcessingRead + ImageProcessingCreate in permissions.ts, seeded roles, and the frontend permissions.ts mirror.

Files in src/modules/image-processing/ reach src/common/ two levels up:

// correct
import { processImage } from '../../common/portable/image';
// wrong — 'Failed to load url'
// import { processImage } from '../../../common/portable/image';