# random-access-indexed-file

> A variation of [random-access-file (RAF)](https://github.com/random-access-storage/random-access-file) which provides "continuous reading or writing to a file using random offsets and lengths".

Latest version **2.0.0** (published 2018-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install random-access-indexed-file
pnpm add random-access-indexed-file
yarn add random-access-indexed-file
bun add random-access-indexed-file
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2018-09-11 |
| First published | 2018-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 22 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Paul Frazee |
| Maintainers | pfrazee |
| Keywords | random-access-storage |

## Links

- npm: https://www.npmjs.com/package/random-access-indexed-file
- Repository: https://github.com/pfrazee/random-access-indexed-file
- Homepage: https://github.com/pfrazee/random-access-indexed-file#readme
- Issues: https://github.com/pfrazee/random-access-indexed-file/issues
- npm.io page: https://npm.io/package/random-access-indexed-file

## Dependencies (4)

- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [uint48be](https://npm.io/package/uint48be.md) ^2.0.1
- [await-lock](https://npm.io/package/await-lock.md) ^1.1.3
- [random-access-storage](https://npm.io/package/random-access-storage.md) ^1.3.0

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2018-09-11
- 1.0.0 — 2018-09-08

## README

# Random Access Indexed File

A variation of [random-access-file (RAF)](https://github.com/random-access-storage/random-access-file) which provides "continuous reading or writing to a file using random offsets and lengths".

RAF depends on sparse files for implementation simplicity. Some operating systems (eg MacOS before APFS) do not support sparse files. This can be detected with [supports-sparse-files](https://github.com/mafintosh/supports-sparse-files).

This module provides an alternative to RAF which uses an index file to map chunks to a non-continuous content file.

This module is compatible with files created by RAF. If no index file is found, it will default to RAF's behaviors.

## `.index` file schema

The .index file is a list of 6-byte slots which map blocks to offsets in the content file. To lookup some bytes in the file, you use the following formula:

```js
function lookupContent (targetOffset) {
  // which block do we want
  var block = Math.floor(targetOffset / blocksize)
  // where does that block start in the content file
  var blockOffset = readIndexSlot(block + 2) // +2 to skip header
  // add the remainder offset
  var contentOffset = blockOffset + (targetOffset % blocksize)
  // read from content
  return readContent(contentOffset)
}
```

The .index has a header of 12 bytes which indicates the block size in bytes (6 bytes) and the next available offset (6 bytes).

This is an example .index map with 6 blocks.

|index slot|value|
|-|-|
|0|1024 (block size)|
|1|6144 (next available offset)
|2|0 (offset to block 0)|
|3|5120 (offset to block 1)|
|4|2048 (offset to block 2)|
|5|3072 (offset to block 3)|
|6|4096 (offset to block 4)|
|7|1024 (offset to block 5)|

---
_Source: https://npm.io/package/random-access-indexed-file · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
