Skip to content

@bendyline/molen-pack ​

Molen content packs: build, open and read zip packs of models and documents from any source.

ts
import { /* … */ } from '@bendyline/molen-pack';

Classes ​

PackChangedError ​

The server replaced the pack while it was being read (its ETag changed).

Extends ​

  • Error

Constructors ​

Constructor ​
ts
new PackChangedError(url): PackChangedError;
Parameters ​
url ​

string

Returns ​

PackChangedError

Overrides ​
ts
Error.constructor

PackEntryNotFoundError ​

Extends ​

  • Error

Constructors ​

Constructor ​
ts
new PackEntryNotFoundError(pack, path): PackEntryNotFoundError;
Parameters ​
pack ​

string

path ​

string

Returns ​

PackEntryNotFoundError

Overrides ​
ts
Error.constructor

Properties ​

pack ​
ts
readonly pack: string;
path ​
ts
readonly path: string;

PackIntegrityError ​

Extends ​

  • Error

Constructors ​

Constructor ​
ts
new PackIntegrityError(
   pack, 
   path, 
   detail
): PackIntegrityError;
Parameters ​
pack ​

string

path ​

string

detail ​

string

Returns ​

PackIntegrityError

Overrides ​
ts
Error.constructor

Interfaces ​

AssetProviderLike ​

The shape of the client's AssetProvider, so this package needs no client dependency.

Methods ​

load() ​
ts
load(ref): Promise<ArrayBuffer>;
Parameters ​
ref ​

string

Returns ​

Promise&lt;ArrayBuffer&gt;

loadText() ​
ts
loadText(ref): Promise<string>;
Parameters ​
ref ​

string

Returns ​

Promise&lt;string&gt;


BuiltPack ​

Properties ​

bytes ​
ts
bytes: Uint8Array;
manifest ​
ts
manifest: PackManifest;

OpenPackOptions ​

Properties ​

concurrency? ​
ts
optional concurrency?: number;

Reads in flight at once (default 6).

expect? ​
ts
optional expect?: object;

Fail to open unless the manifest's contentHash matches.

contentHash ​
ts
contentHash: string;
fetch? ​
ts
optional fetch?: (input, init?) => Promise<Response>;

MDN Reference

Parameters ​
input ​

RequestInfo | URL

init? ​

RequestInit

Returns ​

Promise&lt;Response&gt;

integrity? ​
ts
optional integrity?: "crc" | "sha256";

'crc' (default) checks each zip member; 'sha256' also checks every file's hash.

label? ​
ts
optional label?: string;

Name used in errors and progress (default: the URL, or "bytes"/"blob"/"reader").

maxCacheBytes? ​
ts
optional maxCacheBytes?: number;

Decoded bytes kept for reuse (default 16 MiB).

mode? ​
ts
optional mode?: "auto" | "whole" | "range";

'auto' (default) picks whole or range reads; 'whole' downloads the file in one request.

retry? ​
ts
optional retry?: PackRetryOptions;
signal? ​
ts
optional signal?: AbortSignal;

Aborts opening and every later read.

sizeHint? ​
ts
optional sizeHint?: number;

Expected size in bytes; with auto, packs up to wholeThreshold are fetched whole.

wholeThreshold? ​
ts
optional wholeThreshold?: number;

Default 4 MiB.

Methods ​

onProgress()? ​
ts
optional onProgress(progress): void;
Parameters ​
progress ​

PackProgress

Returns ​

void


Pack ​

Properties ​

label ​
ts
readonly label: string;
manifest ​
ts
readonly manifest: PackManifest;

Methods ​

close() ​
ts
close(): void;

Abort outstanding reads and drop cached bytes.

Returns ​

void

has() ​
ts
has(path): boolean;
Parameters ​
path ​

string

Returns ​

boolean

paths() ​
ts
paths(): string[];

Every file path, sorted.

Returns ​

string[]

prefetch() ​
ts
prefetch(paths, options?): Promise<void>;

Start fetching files now so later reads are served from the cache.

Parameters ​
paths ​

readonly string[]

options? ​

ReadOptions

Returns ​

Promise&lt;void&gt;

readBytes() ​
ts
readBytes(path, options?): Promise<ArrayBuffer>;
Parameters ​
path ​

string

options? ​

ReadOptions

Returns ​

Promise&lt;ArrayBuffer&gt;

readJson() ​
ts
readJson<T>(path, options?): Promise<T>;
Type Parameters ​
T ​

T = unknown

Parameters ​
path ​

string

options? ​

ReadOptions

Returns ​

Promise&lt;T&gt;

readText() ​
ts
readText(path, options?): Promise<string>;
Parameters ​
path ​

string

options? ​

ReadOptions

Returns ​

Promise&lt;string&gt;


PackAssetProviderOptions ​

Properties ​

fallback? ​
ts
optional fallback?: AssetProviderLike;

Refs no pack provides go here; without it they are an error.

variant? ​
ts
optional variant?: string;

Prefer this variant (e.g. "ktx2") when the manifest lists one for the asset.


PackFile ​

Properties ​

bytes ​
ts
bytes: Uint8Array;
path ​
ts
path: string;

Pack-relative POSIX path.


PackIndex ​

A directory listing of built packs, for hosts that publish several.

Properties ​

format ​
ts
format: "molen/pack-index@1";
packs ​
ts
packs: Record<string, PackIndexEntry>;

PackIndexEntry ​

Properties ​

contentHash ​
ts
contentHash: string;
file ​
ts
file: string;

Pack file, relative to the index.

size ​
ts
size: number;

File size in bytes.

version ​
ts
version: string;

PackManifest ​

The manifest inside a built pack.

Properties ​

blocks ​
ts
blocks: Record<string, PackBlock>;
contentHash ​
ts
contentHash: string;

sha256 over every logical file (path and content), independent of the zip layout.

entries ​
ts
entries: Record<string, PackEntry>;
format ​
ts
format: "molen/pack@1";
id ​
ts
id: string;
ids ​
ts
ids: Record<string, string>;

Asset id to the entry path of its runtime file.

license? ​
ts
optional license?: string;

SPDX expression for the pack's content.

notice? ​
ts
optional notice?: string;

Path of the entry that carries the pack's attribution notice.

provides ​
ts
provides: Record<string, string[]>;

Role (e.g. types, stylepack, stars) to the entry paths that provide it.

title? ​
ts
optional title?: string;
version ​
ts
version: string;

PackOptions ​

Properties ​

compression? ​
ts
optional compression?: "auto" | "store";

auto (default) deflates a member when that saves at least 10%. store writes every member uncompressed: the manifest and contents are identical, only the archive is larger, so a pack built for local reading (a test run) skips the deflate that dominates build time.

id ​
ts
id: string;
ids? ​
ts
optional ids?: Record<string, string>;

Asset id to file path. Ids declared by molen/asset@1 sidecars are added automatically.

license? ​
ts
optional license?: string;
notice? ​
ts
optional notice?: string;

Path of the file carrying the attribution notice.

provides? ​
ts
optional provides?: Record<string, string | readonly string[]>;

Role (e.g. types, stylepack) to the file or files that provide it.

solid? ​
ts
optional solid?: boolean;

Group small text files into compressed solid blocks (default true).

title? ​
ts
optional title?: string;
version ​
ts
version: string;

PackProgress ​

Properties ​

bytes ​
ts
bytes: number;

Bytes read from the source so far.

label ​
ts
label: string;

The pack's label (URL, file name, ...).

requests ​
ts
requests: number;

Reads (network requests, for a URL) so far.


PackRetryOptions ​

Properties ​

attempts? ​
ts
optional attempts?: number;

Attempts per request, including the first (default 3).

baseDelayMs? ​
ts
optional baseDelayMs?: number;

Delay before the first retry; doubles each time (default 250 ms).

maxDelayMs? ​
ts
optional maxDelayMs?: number;
timeoutMs? ​
ts
optional timeoutMs?: number;

Per-request deadline (default 30 s).


PackSet ​

Properties ​

packs ​
ts
readonly packs: readonly Pack[];

Methods ​

add() ​
ts
add(pack): void;
Parameters ​
pack ​

Pack

Returns ​

void

assetProvider() ​
ts
assetProvider(options?): AssetProviderLike;

An asset provider for the client (assets: { provider }).

Parameters ​
options? ​

PackAssetProviderOptions

Returns ​

AssetProviderLike

close() ​
ts
close(): void;
Returns ​

void

provided() ​
ts
provided(role): object[];

The paths every pack lists for a role (e.g. types), earliest pack first.

Parameters ​
role ​

string

Returns ​

object[]

readBytes() ​
ts
readBytes(ref, options?): Promise<ArrayBuffer>;
Parameters ​
ref ​

string

options? ​

ReadOptions

Returns ​

Promise&lt;ArrayBuffer&gt;

readJson() ​
ts
readJson<T>(ref, options?): Promise<T>;
Type Parameters ​
T ​

T = unknown

Parameters ​
ref ​

string

options? ​

ReadOptions

Returns ​

Promise&lt;T&gt;

readText() ​
ts
readText(ref, options?): Promise<string>;
Parameters ​
ref ​

string

options? ​

ReadOptions

Returns ​

Promise&lt;string&gt;

resolve() ​
ts
resolve(ref): 
  | {
  pack: Pack;
  path: string;
}
  | undefined;

Find the pack and path for a ref: pack:<id>/<path>, an asset id from a manifest's ids, or a bare path. Undefined when no pack has it.

Parameters ​
ref ​

string

Returns ​

| { pack: Pack; path: string; } | undefined


RangeReader ​

Random access to a pack's bytes. Supply one for storage Molen does not know about.

Properties ​

size ​
ts
readonly size: number;

Total size in bytes.

Methods ​

read() ​
ts
read(
   offset, 
   length, 
   signal?
): Promise<Uint8Array<ArrayBufferLike>>;
Parameters ​
offset ​

number

length ​

number

signal? ​

AbortSignal

Returns ​

Promise&lt;Uint8Array&lt;ArrayBufferLike&gt;&gt;


ReadOptions ​

Properties ​

priority? ​
ts
optional priority?: "high" | "low";

Low-priority reads wait behind high-priority ones (default 'high').

signal? ​
ts
optional signal?: AbortSignal;

Type Aliases ​

PackInput ​

ts
type PackInput = 
  | string
  | URL
  | Blob
  | ArrayBuffer
  | Uint8Array
  | RangeReader;

Where a pack's bytes come from: a URL, a file the user picked, bytes, or your own reader.

Functions ​

createPack() ​

ts
function createPack(files, options): Promise<BuiltPack>;

Build a pack: a deterministic zip whose last member is the manifest.

Parameters ​

files ​

readonly PackFile[]

options ​

PackOptions

Returns ​

Promise&lt;BuiltPack&gt;


createPackSet() ​

ts
function createPackSet(packs?): PackSet;

Parameters ​

packs? ​

readonly Pack[]

Returns ​

PackSet


describePack() ​

ts
function describePack(files, options): Promise<PackManifest>;

Describe files as a pack manifest with no blocks: entries, ids, roles and the content hash. The content hash covers paths and contents only, so a built pack and its unbuilt source directory report the same one.

Parameters ​

files ​

readonly PackFile[]

options ​

PackOptions

Returns ​

Promise&lt;PackManifest&gt;


isTextPath() ​

ts
function isTextPath(path): boolean;

Parameters ​

path ​

string

Returns ​

boolean


mediaTypeOf() ​

ts
function mediaTypeOf(path): string;

Parameters ​

path ​

string

Returns ​

string


openPack() ​

ts
function openPack(input, options?): Promise<Pack>;

Open a pack. Reads only the manifest and zip directory; files are fetched when read.

Parameters ​

input ​

PackInput

options? ​

OpenPackOptions

Returns ​

Promise&lt;Pack&gt;


packFromFiles() ​

ts
function packFromFiles(
   files, 
   options, 
   label?
): Promise<Pack>;

A pack over loose files, with the same manifest a built pack of those files would have. For tests, development, and content generated at runtime.

Parameters ​

files ​

readonly PackFile[]

options ​

PackOptions

label? ​

string

Returns ​

Promise&lt;Pack&gt;


sha256() ​

ts
function sha256(bytes): Promise<string>;

Parameters ​

bytes ​

Uint8Array

Returns ​

Promise&lt;string&gt;

37 guides · 36 schema formats · 41 API entry points · 10 samples. API, CLI and schema pages are generated from the shipped build.