# sleepfile

> Sleep files for Node.js

Latest version **2.4.2** (published 2022-03-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install sleepfile
pnpm add sleepfile
yarn add sleepfile
bun add sleepfile
```

## 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.4.2 |
| Published | 2022-03-21 |
| First published | 2019-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 14 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/sleepfile
- Repository: https://github.com/mafintosh/sleepfile
- Issues: https://github.com/mafintosh/sleepfile/issues
- npm.io page: https://npm.io/package/sleepfile

## Dependencies (1)

- [nanoresource](https://npm.io/package/nanoresource.md) ^1.0.0

## Recent versions

- 2.4.2 (latest) — 2022-03-21
- 2.4.1 — 2022-03-21
- 2.4.0 — 2022-03-21
- 2.3.0 — 2020-01-22
- 2.2.0 — 2019-11-12
- 2.1.2 — 2019-04-24
- 2.1.1 — 2019-04-05
- 2.1.0 — 2019-02-28
- 2.0.1 — 2019-02-27
- 2.0.0 — 2019-02-27
- 1.0.1 — 2019-01-22
- 1.0.0 — 2019-01-22

## README

# sleepfile

[Sleep files](https://github.com/datprotocol/DEPs/blob/master/proposals/0009-sleep-headers.md) for Node.js

```
npm install sleepfile
```

## Usage

``` js
const Sleepfile = require('sleepfile')
const ram = require('random-access-memory')

const file = new Sleepfile(ram())

file.put(42, Buffer.alloc(42), function (err) {
  console.log('Inserted an entry at index 42')
  file.get(42, function (err, buf) {
    console.log('Read it out again')
  })
})
```

## API

#### `file = new Sleepfile(storage, [options])`

Create a new sleep file. Storage should be a [random-access-storage](https://github.com/random-access-storage/random-access-storage) instance.

Options include

```js
{
  magicBytes: 0x00000000, // set a magic bytes header of the file
  valueSize: 42, // explicitly set the valueSize (infered on first put otherwise)
  valueEncoding: encoding, // optionally set an abstract-encoding for the values
  name: 'algo', // optionally set the algorithm name in the header
  overwrite: false // reset the file content
}
```

#### `file.put(index, value, [callback])`

Insert a value at an index.

#### `file.putBatch(offset, values, [callback])`

Insert an array of values starting at offset.

#### `file.get(index, callback)`

Get the value at index.

#### `file.getBatch(offset, length, [callback])`

Get an array of values starting at offset.

#### `file.stat(callback)`

Get metadata about the sleepfile itself. Returned object looks like this:

```js
{
  magicBytes: 0x00000000, // the magic bytes stored
  version: 0, // sleep version
  valueSize: 42,
  name: 'algo name',
  length: 42, // how many values are there maximum
  density: 0.60 // how much of the underlying storage are actually in use
}
```

#### `file.head(callback)`

Get the value out with the highest index, i.e. `stat.length - 1`.
If the file is empty `null` is returned.

## License

MIT

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