npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@statewalker/webrun-files-browser

v0.5.0

Published

Browser File System Access API implementation for webrun-files

Readme

@statewalker/webrun-files-browser

Browser File System Access API implementation for the @statewalker/webrun-files package.

Overview

This package provides an IFilesApi implementation that works in modern browsers using the File System Access API. It allows web applications to read and write files to:

  • User-selected directories via showDirectoryPicker()
  • Origin Private File System (OPFS) - a sandboxed filesystem that persists between sessions

Installation

npm install @statewalker/webrun-files-browser @statewalker/webrun-files

Usage

With User-Selected Directory

import { openBrowserFilesApi } from "@statewalker/webrun-files-browser";
import { FilesApi } from "@statewalker/webrun-files";

// Opens a directory picker dialog (requires user gesture)
const browserFs = await openBrowserFilesApi();
const api = new FilesApi(browserFs);

// Write a file
await api.write("/notes/hello.txt", [
  new TextEncoder().encode("Hello, World!")
]);

// Read a file
const content = await api.readFile("/notes/hello.txt");
console.log(new TextDecoder().decode(content)); // "Hello, World!"

// List directory contents
for await (const entry of api.list("/notes")) {
  console.log(entry.name, entry.kind);
}

With Origin Private File System (OPFS)

OPFS provides a sandboxed filesystem that doesn't require user interaction:

import { getOPFSFilesApi } from "@statewalker/webrun-files-browser";
import { FilesApi } from "@statewalker/webrun-files";

// No user gesture required
const opfsFs = await getOPFSFilesApi();
const api = new FilesApi(opfsFs);

// Use the same API as above
await api.write("/data/config.json", [
  new TextEncoder().encode('{"theme": "dark"}')
]);

With Custom Directory Handle

You can also create a BrowserFilesApi instance with any FileSystemDirectoryHandle:

import { BrowserFilesApi } from "@statewalker/webrun-files-browser";
import { FilesApi } from "@statewalker/webrun-files";

// Get a directory handle from drag & drop, IndexedDB, etc.
const directoryHandle = await getDirectoryHandleSomehow();

const browserFs = new BrowserFilesApi({ rootHandle: directoryHandle });
const api = new FilesApi(browserFs);

API Reference

BrowserFilesApi

Main class implementing IFilesApi for browser environments.

interface BrowserFilesApiOptions {
  rootHandle: FileSystemDirectoryHandle;
}

class BrowserFilesApi implements IFilesApi {
  constructor(options: BrowserFilesApiOptions);

  // Core IFilesApi methods
  list(file: FileRef, options?: ListOptions): AsyncGenerator<FileInfo>;
  stats(file: FileRef): Promise<FileInfo | undefined>;
  remove(file: FileRef): Promise<boolean>;
  open(file: FileRef): Promise<FileHandle>;

  // Optional methods (implemented)
  mkdir(file: FileRef): Promise<void>;
  move(source: FileRef, target: FileRef): Promise<boolean>;
  copy(source: FileRef, target: FileRef, options?: CopyOptions): Promise<boolean>;
}

BrowserFileHandle

File handle class for random access read/write operations.

class BrowserFileHandle implements FileHandle {
  readonly size: number;

  close(): Promise<void>;
  appendFile(data: BinaryStream, options?: AppendOptions): Promise<number>;
  createReadStream(options?: ReadStreamOptions): AsyncGenerator<Uint8Array>;
  createWriteStream(data: BinaryStream, options?: WriteStreamOptions): Promise<number>;
}

Helper Functions

// Opens a directory picker and returns a BrowserFilesApi instance
function openBrowserFilesApi(): Promise<BrowserFilesApi>;

// Returns a BrowserFilesApi backed by the Origin Private File System
function getOPFSFilesApi(): Promise<BrowserFilesApi>;

How It Works

Directory Navigation

The implementation navigates the filesystem by traversing FileSystemDirectoryHandle entries. Paths like /docs/notes/file.txt are resolved by getting directory handles step by step:

root → "docs" → "notes" → "file.txt"

Reading Files

Files are read using File.slice() for efficient partial reads. This enables random access without loading the entire file into memory:

const handle = await api.open("/large-file.bin");
// Read only bytes 1000-2000
for await (const chunk of handle.createReadStream({ start: 1000, end: 2000 })) {
  console.log(chunk);
}
await handle.close();

Writing Files

Writes use FileSystemWritableFileStream:

  • Full writes create a new writable stream and write all data
  • Partial writes (with start option) preserve content before the start position
  • Appends seek to the end of the file before writing

Directory Creation

Directories are created automatically when writing files. You can also create empty directories:

await api.mkdir("/path/to/new/directory");

Browser Support

The File System Access API is supported in:

  • Chrome/Edge 86+
  • Opera 72+
  • Chrome for Android 86+

For other browsers, you may need a polyfill like native-file-system-adapter.

Security Considerations

  • User gesture required: showDirectoryPicker() must be called from a user gesture (click, key press)
  • Secure context: The API only works in secure contexts (HTTPS or localhost)
  • Permission prompts: Users must grant permission to access directories
  • OPFS isolation: Origin Private File System is isolated per-origin and not accessible to the user

Testing

For testing in Node.js environments, we use native-file-system-adapter which provides a memory backend:

import { getOriginPrivateDirectory } from "native-file-system-adapter";
import { BrowserFilesApi } from "@statewalker/webrun-files-browser";

const rootHandle = await getOriginPrivateDirectory(
  import("native-file-system-adapter/src/adapters/memory.js")
);

const api = new BrowserFilesApi({ rootHandle });

License

MIT