libassimp
0.4.1

Use in the browser

Load one artifact, resolve remote sidecars asynchronously, and move CPU work to a worker.

Open Markdown

Convert picked bytes

import { convert } from 'libassimp';const toGlb = async (file: File): Promise<Uint8Array> => {  const bytes = new Uint8Array(await file.arrayBuffer());  const { files } = await convert({ name: file.name, bytes }, { to: 'glb' });  return files[0].bytes;};

Input

objcube.obj136 B

Output

Preparing artifact…

Convert
—
Output
—
Files
—
WASM load
—

One artifact, automatic async route

Each package entry ships one Wasm artifact. During instantiation, libassimp checks for both WebAssembly.Suspending and WebAssembly.promising. A capable host uses JSPI to suspend Assimp parsing around an async resolver; every other supported host uses promise-cache replay. There is no user-agent test, alternate URL, or public mode option.

import { createAssimp } from 'libassimp';

using assimp = await createAssimp({
  wasmUrl: new URL('/assets/libassimp.wasm', location.href),
});

const bytes = new Uint8Array();
const { files } = await assimp.convert(
  { name: 'scene.gltf', bytes },
  {
    to: 'glb',
    resolve: async (name) => {
      const response = await fetch(`/models/${name}`);
      return response.ok ? new Uint8Array(await response.arrayBuffer()) : undefined;
    },
  },
);

console.log(files[0].name);

Serve Wasm with application/wasm, content-hashed immutable caching, and compression. Streaming compilation falls back to buffered compilation when MIME or host behavior requires it.

Use a worker

A conversion is CPU-heavy even when resolution suspends, and on the main thread a running Wasm import cannot be interrupted. Run libassimp in a module worker and transfer the buffers both ways instead of copying them. The artifact has no pthreads, so no cross-origin isolation headers are required.

convert-worker.ts creates one instance and answers each request by id:

import { createAssimp, type AssimpFile, type ExportFormat } from 'libassimp';

type Job = { id: number; name: string; bytes: Uint8Array<ArrayBuffer>; to: ExportFormat };

// Created on first use, so a message that arrives during startup is not lost.
let instance: ReturnType<typeof createAssimp> | undefined;

self.onmessage = async ({ data }: MessageEvent<Job>) => {
  try {
    instance ??= createAssimp();
    const { files } = await (await instance).convert({ name: data.name, bytes: data.bytes }, { to: data.to });
    // Each output owns its buffer; transfer moves it without a copy.
    const transfer = [...new Set(files.map(({ bytes }) => bytes.buffer))];
    self.postMessage({ id: data.id, files } satisfies { id: number; files: readonly AssimpFile[] }, {
      transfer,
    });
  } catch (error) {
    const { code, message } = error as { code?: string; message?: string };
    self.postMessage({ id: data.id, error: { code, message: message ?? String(error) } });
  }
};

The page wraps the worker in a Promise API and transfers the input:

import type { AssimpFile, ExportFormat } from 'libassimp';

type Reply = { id: number; files?: readonly AssimpFile[]; error?: { code?: string; message: string } };

const worker = new Worker(new URL('./convert-worker.ts', import.meta.url), { type: 'module' });
const pending = new Map<
  number,
  { resolve: (files: readonly AssimpFile[]) => void; reject: (error: Error) => void }
>();
let nextId = 0;

worker.onmessage = ({ data }: MessageEvent<Reply>) => {
  const job = pending.get(data.id);
  pending.delete(data.id);
  if (data.files) job?.resolve(data.files);
  else job?.reject(Object.assign(new Error(data.error?.message), { code: data.error?.code }));
};

export const convertInWorker = async (file: File, to: ExportFormat): Promise<readonly AssimpFile[]> => {
  const buffer = await file.arrayBuffer();
  const id = nextId++;
  return new Promise((resolve, reject) => {
    pending.set(id, { resolve, reject });
    worker.postMessage({ id, name: file.name, bytes: new Uint8Array(buffer), to }, [buffer]);
  });
};

To cancel a stuck conversion, worker.terminate() and start a new worker. For files from untrusted sources, see Untrusted input.

Download output

const download = (name: string, bytes: Uint8Array<ArrayBuffer>): void => {
  const url = URL.createObjectURL(new Blob([bytes]));
  const anchor = Object.assign(document.createElement('a'), { download: name, href: url });
  anchor.click();
  // The browser reads the URL after click() returns; revoking now can cancel
  // the download. FileSaver.js waits 40 s for the slowest (Safari) case.
  setTimeout(() => URL.revokeObjectURL(url), 40_000);
};

The canonical compatibility matrix records browser floors and which current hosts accelerate through JSPI.

On this page