# diskette

> Stream buffers and strings efficiently in-memory

Latest version **1.2.2** (published 2017-06-07) · MIT license · 0 weekly downloads

## Install

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

## 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 | 1.2.2 |
| Published | 2017-06-07 |
| First published | 2016-05-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sam Vervaeck |
| Maintainers | samvv |
| Keywords | file, stream, buffer, string, io, memory |

## Links

- npm: https://www.npmjs.com/package/diskette
- Repository: https://github.com/samvv/diskette
- Homepage: https://github.com/samvv/diskette#readme
- Issues: https://github.com/samvv/diskette/issues
- npm.io page: https://npm.io/package/diskette

## Dependencies (1)

- [@types/node](https://npm.io/package/@types/node.md) ^7.0.28

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.2.2 (latest) — 2017-06-07
- 1.2.1 — 2016-06-06
- 1.2.0 — 2016-06-06
- 1.1.0 — 2016-06-06
- 1.0.6 — 2016-05-16
- 1.0.5 — 2016-05-16
- 1.0.4 — 2016-05-15
- 1.0.2 — 2016-05-15
- 1.0.1 — 2016-05-15
- 1.0.0 — 2016-05-14
- 0.4.0 — 2016-05-14
- 0.3.1 — 2016-05-14
- 0.3.0 — 2016-05-14

## README

Diskette
========

[![Build Status](https://travis-ci.org/samvv/diskette.svg?branch=master)](https://travis-ci.org/samvv/diskette) [![Dependency Status](https://david-dm.org/samvv/diskette.svg)](https://david-dm.org/samvv/diskette)

> Diskette is a virtual file format that allows for efficient in-memory reads and writes. Files can be constructed from any buffer or string and they are fully streamable. As a consequence, any buffer or string can be streamed as if it was a regular file.

**Projects making use of it:**

 - [Comet](http://github.com/comet-platform/comet)

[Your project here?](https://github.com/samvv/diskette/pulls)

```javascript
let diskette = require('diskette')

let file = new diskette.File({
  blockLength: 20
})

file.append('yo bruh\n')
file.append('i heard you like files\n')
file.write(4, 'dude')

let rest = new File('so i made files in memory that can be piped to just about anything\n')
let restStream = new diskette.ReadableFileStream(rest)

let out = new diskette.WritableFileStream(file)

restStream.pipe(out)

restStream.on('end', () => {
  file.append('hope you like it')
})

let s = new diskette.ReadableFileStream(file)
s.pipe(process.stdout)
```

Result:

```
yo dude 
i heard you like files
so i made files in memory that can be piped to just about anything
hope you like it
```

## API

### File

#### new File([content][, options])

Create a new file which has its contents stored in-memory, optionally filled with `content`, and with one of the following options:

 - **blockLength**: the size of one block

**Note:** `content` can be omitted.

#### file.size 

The total length of the file, in bytes.

#### file.write(position, bufferOrString[, bufferStart][, bufferEnd])

Writes the content of buffer between the given indices to the given position. If no indices are provided, the enitre content of the buffer is written to file.

#### file.append(bufferOrString[, bufferStart][, bufferEnd]) 

Just like `file.write`, except that it appends data to the end of the file.

#### file.read(position, buffer, bufferStart, bufferEnd)

Reads content of file starting at the given position to the given buffer, optionally limited by two indices denoting the start and the end position of the buffer.

#### file.buffer([freeSpacePreceding = 0][, freeSpaceFollowing = 0])

Get a buffer that holds the content of file. Optionally make the buffer contain `freeSpacePreceding` and `freeSpaceFollowing` of free space.

#### file.toString([enc]) 

Convert the file to a string representation, using the specified encoding. Defaults to `utf8`.

#### file.allocateBlock()

Allocates a new block for writing content. This method is automatically called by the class, and generally should not be used.

#### file.totalLength()

Returns the total space the file occupies in-memory, including unused free space.

#### file.clear()

Clears all content of file by destroying the blocks, making it seem like an empty file was just created.

### ReadableFileStream

#### new ReadableFileStream(file[, options])

Constructs a new [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) of which data of file can be read. The options are:

 - **chunkSize**: how much data to stream in one cycle. Use `-1` to make the chunk as large as possible (possibly the enitre file content). Defaults to `-1`.
 - **startPosition**: an offset in the file to start reading from. Defaults to `0`.

### WritableFileStream

#### new WritableFileStream(file[, options])

Constructs a new [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable) that writes data to file starting at the given position. 

## Similar projects

- [vinyl](https://github.com/gulpjs/vinyl): only stream or buffer (exclusively)
- [node-stream-buffer](https://github.com/samcday/node-stream-buffer): only delay in reads and writes

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