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.
Principle
Section titled “Principle”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/”| File | Contents | Node fallback |
|---|---|---|
runtime.ts | isBunRuntime, hashPassword/verifyPassword, sha256/digest, uuid (randomUUIDv7), randomBytes, readTextFile/writeFile, deepEquals, parseJson5, escapeHTML | node:crypto, node:fs/promises, @node-rs/argon2+bcryptjs, structural walk |
scheduler.ts | PortableScheduler — Bun.cron vs in-process setInterval | in-process setInterval |
markdown.ts | Bun.markdown.html vs minimal regex→HTML | regex + HTML-escape |
image.ts | Bun.Image pipeline (resize/rotate/flip/modulate/encode webp/jpeg/avif/heic) | sharp-like adapter via registerNodeImageAdapter |
browser.ts | connectToCdp — Bun.WebView CDP vs puppeteer-core | puppeteer-core |
html-pdf.ts | HTML/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.
Critical Bun.Image 1.4 facts (verified)
Section titled “Critical Bun.Image 1.4 facts (verified)”- Constructor:
new Bun.Image(bytes)— not static.from(). .width/.height=-1untilawait img.metadata()resolves.resizefit supports only'fill' | 'inside'— nocover/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/outsidethrow a clear error (no crop). True cover-crop requires a registered Node adapter (sharp). - Open Graph cards use
fit:'inside'(Bun-safe).
Image-processing module
Section titled “Image-processing module”Endpoints (permission-guarded, tenant-scoped):
POST /image-processing/transform — dynamic resize/rotate/flip/modulate + re-encode.
Returns binary via @Res with X-Image-Width / X-Image-Height headers.
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.
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 portableprocessImage/getImageInfo/buildOpenGraphMetaand theOPEN_GRAPH_PRESETSconstant.image-processing.controller.ts—FileInterceptor(memoryStorage), permissionImageProcessingCreate/ImageProcessingRead.image-processing.module.ts— registered inapp.module.ts.image-processing.service.spec.ts— colocated unit tests (mocks portable layer).
Permissions (synced 3 ways)
Section titled “Permissions (synced 3 ways)”ImageProcessingRead + ImageProcessingCreate in permissions.ts, seeded roles, and the
frontend permissions.ts mirror.
Path gotcha
Section titled “Path gotcha”Files in src/modules/image-processing/ reach src/common/ two levels up:
// correctimport { processImage } from '../../common/portable/image';// wrong — 'Failed to load url'// import { processImage } from '../../../common/portable/image';