Skip to content

optiq-image / node

node ​

optiq-image/node — Node.js support (tests, benchmarks, server-side use). jSquash fetches its .wasm via fetch(import.meta.url), which does not work for file: URLs in Node, so compile the modules from disk and hand them over explicitly.

Interfaces ​

CodecBackend ​

Properties ​

PropertyModifierType
namereadonlystring

Methods ​

codecName() ​
ts
codecName(format, settings): string;

Human-readable codec label for reports, e.g. "mozjpeg".

Parameters ​
ParameterType
formatOutputFormat
settingsEncodeSettings
Returns ​

string

decode() ​
ts
decode(format, bytes): Promise<RawImage>;

Decode to 8-bit RGBA without applying EXIF orientation.

Parameters ​
ParameterType
format"jpeg" | "png" | "webp" | "avif"
bytesUint8Array
Returns ​

Promise<RawImage>

encode() ​
ts
encode(
   format, 
   img, 
   settings
): Promise<Uint8Array<ArrayBufferLike>>;
Parameters ​
ParameterType
formatOutputFormat
imgRawImage
settingsEncodeSettings
Returns ​

Promise<Uint8Array<ArrayBufferLike>>

optimisePng()? ​
ts
optimisePng?(bytes, settings): Promise<Uint8Array<ArrayBufferLike>>;

Lossless PNG re-compression of existing PNG bytes (keeps ancillary chunks).

Parameters ​
ParameterType
bytesUint8Array
settingsEncodeSettings
Returns ​

Promise<Uint8Array<ArrayBufferLike>>

resize()? ​
ts
resize?(
   img, 
   width, 
   height
): Promise<RawImage>;
Parameters ​
ParameterType
imgRawImage
widthnumber
heightnumber
Returns ​

Promise<RawImage>


DiskFileResult ​

Properties ​

PropertyTypeDescription
after?number-
before?number-
error?stringCODE: message when this file failed; other files are unaffected.
filestringAbsolute input path.
from?string-
quality?number | "exact"SSIM of the result, or "exact" when pixels are verified identical (or the file is unchanged).
saved?number1 − after / before.
to?DetectedFormat-
usedOriginal?booleanThe original was kept (already optimal, already optimised by Optiq, or the saving was below the threshold).
warnings?string[]-
written?stringAbsolute path written, if any.

DiskOptions ​

Properties ​

PropertyTypeDescription
concurrency?numberWorker threads. Default: available cores − 1.
inPlace?booleanOverwrite the inputs (write to a temp file, then rename). Use with allowFormatConversion: false.
onResult?(file, done, total) => void-
out?stringWrite results to this folder, mirroring each input folder's structure.
signal?AbortSignal-

DiskResult ​

Properties ​

PropertyType
filesDiskFileResult[]
totalsobject
totals.afternumber
totals.beforenumber
totals.errorsnumber
totals.filesnumber
totals.savednumber
totals.secondsnumber

EngineContext ​

Properties ​

PropertyTypeDescription
backendCodecBackend & object-
now?() => number-
onProgress?(e) => void-
onTiming?(phase, ms) => voidProfiling hook: wall time per pipeline phase (used by the benchmark audit; no effect when absent).
signal?AbortSignal-

EngineOutput ​

Properties ​

PropertyTypeDescription
bytes?Uint8Array<ArrayBufferLike>Output bytes; undefined when the result is the unmodified input.
reportEngineReport-

RawImage ​

8-bit RGBA, non-premultiplied, row-major. Structurally compatible with ImageData.

Properties ​

PropertyType
dataUint8ClampedArray
heightnumber
widthnumber

Type Aliases ​

EncodeSettings ​

ts
type EncodeSettings = Record<string, number | boolean | string>;

Codec-specific settings; also reported verbatim in CandidateReport.settings.


EngineReport ​

ts
type EngineReport = Omit<CompressionResult, "blob" | "fileName">;

TimingPhase ​

ts
type TimingPhase = 
  | "probe"
  | "decode"
  | "orient"
  | "resize"
  | "classify"
  | "encode"
  | "verify-decode"
  | "ssim"
  | "exact-compare"
  | "container";

Variables ​

IMAGE_EXTENSIONS ​

ts
const IMAGE_EXTENSIONS: readonly string[];

Functions ​

collectImages() ​

ts
function collectImages(paths): Promise<object[]>;

Expand files and folders (recursively, skipping dot-entries) into image files, each with the root it came from.

Parameters ​

ParameterType
pathsstring[]

Returns ​

Promise<object[]>


createWasmBackend() ​

ts
function createWasmBackend(): CodecBackend;

WASM codecs from jSquash (Squoosh builds): MozJPEG, libwebp, libavif (C/C++ via Emscripten), and the Rust crates png, oxipng and resize (via wasm-bindgen).

Returns ​

CodecBackend


initNodeCodecs() ​

ts
function initNodeCodecs(): Promise<void>;

Returns ​

Promise<void>


optimizeFilesOnDisk() ​

ts
function optimizeFilesOnDisk(
   paths, 
   options, 
   disk?
): Promise<DiskResult>;

Optimise files on disk. Without out or inPlace nothing is written (dry run). Per-file failures are reported in the result, not thrown; invalid options and missing paths throw before any work starts.

Parameters ​

ParameterType
pathsstring[]
optionsOptiqOptions
disk?DiskOptions

Returns ​

Promise<DiskResult>


optimizeInNode() ​

ts
function optimizeInNode(
   input, 
   options?, 
   ctx?
): Promise<EngineOutput>;

Optimise bytes in Node with the WASM codecs (single-threaded; no Worker).

Parameters ​

ParameterType
inputUint8Array
options?OptiqOptions
ctx?Partial<EngineContext>

Returns ​

Promise<EngineOutput>