Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

file-collection

cheminfo1.7kMIT5.2.1TypeScript support: included

Library to manage large amount of files coming from different sources

readme

file-collection

NPM version build status Test coverage npm download

Library to manage large amount of files coming from different sources

Introduction

This library allows to manipulate a large amount of files coming from local or remote sources.

This library allows to have the same code in the browser and in nodejs.

This package allows to create a file-collection than can further be saved as a ium file (zip file containing all the data and an index.json file).

The structure of the zip file is at follow:

  • index.json
  • data/
    • all the files

Installation

npm i file-collection

Basic usage

Append a browser filelist

import { FileCollection } from 'file-collection';

const fileList; // a fileList resulting from a drag / drop in the browser
const fileCollection = new FileCollection();
await fileCollection.appendFileList(fileList);

// get a zip file that can be reload later
const iumFile = fileCollection.toIum();

// list the content of the fileCollection
for (const file of fileCollection) {
  console.log(file.name);
  console.log(await file.text());
}

Reload a 'ium' file

import { fromIum } from 'file-collection';

const fileCollection = await fromIum(iumFile);

for (const file of fileCollection) {
  console.log(file.name);
  console.log(await file.text());
}

Using in a react component

A webSource is a URL to data like for example https://image-js.github.io/image-dataset-demo/index.json:

<MyComponent webSource onchange={(fileCollection) => {}}></MyComponent>

Inside the component we should:

const fileCollection = new FileCollection();
if (webSource) fileCollection.appendWebSource(webSource);

If we drag / drop some files we should:

fileCollection.appendFileList(fileList);

If we drag / drop a 'ium' file we should replace the full fileCollection

const fileCollection = FileCollection.fromIum(iumArrayBuffer);
const state = fileCollection.get('state');

If we want to save as a ium file:

fileCollection.set('state', myCurrentState);
const toSave = fileCollection.toIum();

When fileCollection changes we can always retrieve the various files using:

const files = [...fileCollection]; // fileCollection is iterable on its files property

Referencing files in the fileCollection

Internals - sources versus files

In an instance of FileCollection we have 2 properties:

  • sources: an array containing the different data sources
  • files: an array containing all the files contained in the different sources

Some source may contain many files. This is the case of the .iumfiles as well as for the .zip file.

License

MIT

changelog

Changelog

5.2.1 (2025-10-14)

Bug Fixes

  • appendPath with keepBasename had a leading '/' for first level files (76979c3)

5.2.0 (2025-09-29)

Features

  • add toZip helper and getExtraFiles option on toIum (#41) (3619a6b)

5.1.1 (2025-08-13)

Bug Fixes

  • exports and ArrayBuffer handling with TS 4.9 (#39) (d56ac7b)

5.1.0 (2025-07-24)

Features

  • add options support on SourceItem (#35) (e09fab1)

5.0.1 (2025-07-23)

Bug Fixes

  • complete buffer types supported by appendArrayBuffer (#32) (03c1ac5)
  • normalize relativePath (#34) (c8a4621)

5.0.0 (2025-07-21)

⚠ BREAKING CHANGES

  • Remove filter and cache props shortcut. Access filter and cache directly from options props.

Features

  • add .filter method to FileCollection prototype (8757cc0)
  • expose chainable API (#30) (8757cc0)

Bug Fixes

  • export missing Source and SourceItem interfaces (8757cc0)

Code Refactoring

  • remove filter and cache props shortcut (8757cc0)

4.1.1 (2025-07-10)

Bug Fixes

4.1.0 (2025-07-09)

Features

  • add FileCollection.fromPath static method (#25) (0b1c783)
  • add FileCollection.fromSource static method (#26) (588002f)
  • add FileCollection.fromZip static method (#23) (77498b9)

Theses are ports of methods from filelist-utils:

  • fileCollectionFromPath
  • fileCollectionFromWebSource
  • fileCollectionFromZip

4.0.0 (2025-06-27)

⚠ BREAKING CHANGES

  • remove fromIum input string and NodeJS.ReadableStream support. If you used string you have to decode it to Uint8Array. If you used Node.js Readable Streams, convert them to Web Stream with Readable.toWeb
  • migrate to ESM-only (#15)

Bug Fixes

  • .stream() method on sources added by FileCollection.appendSource (156cab8)
    It returned a promise rejecting an error because it used .stream() on a response (this method does not exist).
    Now it instantiates a stream, the body response is piped to the stream, and it returns the stream synchronously.
  • file paths from toIum zip should not include // anymore (156cab8)

Code Refactoring

3.0.0 (2025-03-07)

⚠ BREAKING CHANGES

  • ungzip create a relativePath that contains the .gz file and the name is appended

Features

  • ungzip create a relativePath that contains the .gz file and the name is appended (6819ac9)

2.0.0 (2025-03-07)

⚠ BREAKING CHANGES

  • remove support of node 18 to use crypto.randomUUID
  • remove support of node 18 to use crypto.randomUUID

Features

  • add parent FileItem if item is decompressed (3c60b18)
  • remove support of node 18 to use crypto.randomUUID (4006fe5)
  • remove support of node 18 to use crypto.randomUUID (4006fe5)

Bug Fixes

  • remove @lukeed/uuid dependency and use crypto uuid generator (d9baf96)

1.0.0 (2024-07-13)

⚠ BREAKING CHANGES

  • update depencies

Features

  • add keepBasename option in appendPath (83af64f)

Miscellaneous Chores

0.2.0 (2023-06-18)

Features

0.1.0 (2023-06-17)

Features

Bug Fixes

  • build for browser is working now correctly (ac7326f)
  • deal correctly with unzip and ungzip options (d851341)

Documentation

0.0.1 (2023-06-14)

release-as

Documentation