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 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-fatfs

v0.2.1

Published

JavaScript/TypeScript bindings for the FatFs library.

Downloads

39

Readme

js-fatfs

This library provides JavaScript/TypeScript bindings for ChaN's FatFs library.

Installation

npm install js-fatfs

Usage

There is no API reference yet. Please refer to the TypeScript definition file, tests and FatFs manual for usage.

Example

The program reads a raw disk image (e.g. floppy disk image) and displays the names and sizes of the files in the root directory.

import * as FatFs from 'js-fatfs';
import * as fs from 'fs';

// The FatFs.DiskIO interface is a set of callbacks provided to FatFs. This
// class implements storage controls for a raw disk image.
class RawDiskImage implements FatFs.DiskIO {
  private sectorSize: number;

  constructor(private image: Buffer) {
    this.sectorSize = image.readUInt16LE(11);  // BPB_BytsPerSec
  }
  // http://elm-chan.org/fsw/ff/doc/dinit.html
  initialize(ff: FatFs.FatFs, pdrv: number) {
    return 0;
  }
  // http://elm-chan.org/fsw/ff/doc/dstat.html
  status(ff: FatFs.FatFs, pdrv: number) {
    return 0;
  }
  // http://elm-chan.org/fsw/ff/doc/dread.html
  read(ff: FatFs.FatFs, pdrv: number, buff: number, sector: number, count: number) {
    const data = this.image.subarray(sector * this.sectorSize, (sector + count) * this.sectorSize);
    // Write the read data to the area starting from `buff` in the FatFs memory.
    ff.HEAPU8.set(data, buff);
    return FatFs.RES_OK;
  }
  // http://elm-chan.org/fsw/ff/doc/dwrite.html
  write(ff: FatFs.FatFs, pdrv: number, buff: number, sector: number, count: number) {
    // The data to be written is in the area starting from `buff` in the FatFs memory.
    const data = ff.HEAPU8.subarray(buff, buff + count * this.sectorSize);
    this.image.set(data, sector * this.sectorSize);
    return FatFs.RES_OK;
  }
  // http://elm-chan.org/fsw/ff/doc/dioctl.html
  ioctl(ff: FatFs.FatFs, pdrv: number, cmd: number, buff: number) {
    switch (cmd) {
      case FatFs.CTRL_SYNC:
        return FatFs.RES_OK;
      case FatFs.GET_SECTOR_COUNT:
        // Use `ff.setValue` to write an integer to the FatFs memory.
        ff.setValue(buff, this.image.byteLength / this.sectorSize, 'i32');
        return FatFs.RES_OK;
      case FatFs.GET_SECTOR_SIZE:
        ff.setValue(buff, this.sectorSize, 'i16');
        return FatFs.RES_OK;
      case FatFs.GET_BLOCK_SIZE:
        ff.setValue(buff, 1, 'i32');
        return FatFs.RES_OK;
      default:
        console.warn(`ioctl(${cmd}): not implemented`);
        return FatFs.RES_ERROR;
    }
  }
}

function check_result(r: number) {
  if (r !== FatFs.FR_OK) throw new Error(`FatFs error: ${r}`);
}

// Create a FatFs instance, by providing a DiskIO implementation.
// Note the `await` because `FatFs.create()` returns a promise.
const ff = await FatFs.create({
  diskio: new RawDiskImage(fs.readFileSync(process.argv[2]))
});

// Mount the filesystem.
// Since this library is a thin wrapper around FatFs, memory management is explicit.
const fatfs = ff.malloc(FatFs.sizeof_FATFS);
check_result(ff.f_mount(fatfs, '', 1));

// Open the root directory.
const dir = ff.malloc(FatFs.sizeof_DIR);
const filinfo = ff.malloc(FatFs.sizeof_FILINFO);
check_result(ff.f_opendir(dir, '/'));

// Iterate through the directory and display the names and sizes of entries.
while (true) {
  check_result(ff.f_readdir(dir, filinfo));
  const fname = ff.FILINFO_fname(filinfo);
  if (fname === '') break;
  console.log([fname, ff.FILINFO_fsize(filinfo)]);
}

// Clean up.
ff.free(filinfo);
check_result(ff.f_closedir(dir));
ff.free(dir);
check_result(ff.f_unmount(''));
ff.free(fatfs);

License

This JavaScript binding is licensed under the BSD 1-Clause License.

FatFs is licensed under the FatFs License, which is very similar to the BSD 1-Clause License.