# file-contents

> Set the `contents` property on a file object. Abstraction from vinyl-fs to support stream or non-stream usage.

Latest version **1.0.1** (published 2017-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install file-contents
pnpm add file-contents
yarn add file-contents
bun add file-contents
```

## 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.0.1 |
| Published | 2017-02-03 |
| First published | 2015-07-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 8 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Jon Schlinkert |
| Maintainers | jonschlinkert |
| Keywords | buffer, content, contents, directory, file, filepath, fs, is, is-directory, is-file, path, plugin, read, read-file, readdir, stat, stats, stream, through, vinyl |

## Links

- npm: https://www.npmjs.com/package/file-contents
- Repository: https://github.com/jonschlinkert/file-contents
- Issues: https://github.com/jonschlinkert/file-contents/issues
- npm.io page: https://npm.io/package/file-contents

## Dependencies (8)

- [kind-of](https://npm.io/package/kind-of.md) ^3.1.0
- [through2](https://npm.io/package/through2.md) ^2.0.3
- [is-buffer](https://npm.io/package/is-buffer.md) ^1.1.4
- [lazy-cache](https://npm.io/package/lazy-cache.md) ^2.0.2
- [extend-shallow](https://npm.io/package/extend-shallow.md) ^2.0.1
- [define-property](https://npm.io/package/define-property.md) ^0.2.5
- [strip-bom-buffer](https://npm.io/package/strip-bom-buffer.md) ^0.1.1
- [strip-bom-string](https://npm.io/package/strip-bom-string.md) ^0.1.2

## 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.0.1 (latest) — 2017-02-03
- 1.0.0 — 2017-02-03
- 0.3.2 — 2016-07-21
- 0.3.1 — 2016-06-19
- 0.3.0 — 2016-06-11
- 0.2.4 — 2015-10-09
- 0.2.3 — 2015-10-09
- 0.2.2 — 2015-10-06
- 0.2.1 — 2015-10-06
- 0.2.0 — 2015-07-17
- 0.1.1 — 2015-07-15
- 0.1.0 — 2015-07-15

## README

# file-contents [![NPM version](https://img.shields.io/npm/v/file-contents.svg?style=flat)](https://www.npmjs.com/package/file-contents) [![NPM monthly downloads](https://img.shields.io/npm/dm/file-contents.svg?style=flat)](https://npmjs.org/package/file-contents)  [![NPM total downloads](https://img.shields.io/npm/dt/file-contents.svg?style=flat)](https://npmjs.org/package/file-contents) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/file-contents.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/file-contents)

> Set the `contents` property on a file object. Abstraction from vinyl-fs to support stream or non-stream usage.

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm install --save file-contents
```

This is inspired by the code in [vinyl-fs](http://github.com/wearefractal/vinyl-fs) `src`. I needed a function that essentially did the same thing but could be used with stream or non-stream code.

## Usage

```js
var through = require('through2');
var contents = require('file-contents');

function toStream(fp) {
  var stream = through.obj();
  stream.write({path: fp});
  stream.end();
  return stream;
}

toStream('README.md')
  .pipe(contents())
  .on('data', function(file) {
    console.log(file.contents.toString());
  });

```

**async**

An `.async()` method is exposed for non-stream, async usage.

```js
contents.async({path: 'README.md'}, function (err, file) {
  // results in something like:
  // 
  //   { path: 'README.md',
  //   stat:
  //    { dev: 16777218,
  //      mode: 33188,
  //      nlink: 1,
  //      uid: 501,
  //      gid: 20,
  //      rdev: 0,
  //      blksize: 4096,
  //      ino: 26436116,
  //      size: 2443,
  //      blocks: 8,
  //      atime: Fri Jul 17 2015 03:01:54 GMT-0400 (EDT),
  //      mtime: Wed Jul 15 2015 02:46:55 GMT-0400 (EDT),
  //      ctime: Wed Jul 15 2015 02:46:55 GMT-0400 (EDT),
  //      birthtime: Tue Jul 14 2015 23:03:58 GMT-0400 (EDT) },
  // contents: <Buffer 23 20 66 69 6c 65 2d 63 6f 6e 74 65 6e 74 73 20 5b 21 5b 4e 50 4d 20 76 65 72 73 69 6f 6e 5d 28 68 74 74 70 73 3a 2f 2f 62 61 64 67 65 2e 66 75 72 79 ... > }
});
```

**sync**

A `.sync()` method is exposed for non-stream, sync usage.

```js
var file = {path: 'README.md'};
contents.sync(file);
// results in something like:
// 
//   { path: 'README.md',
//   stat:
//    { dev: 16777218,
//      mode: 33188,
//      nlink: 1,
//      uid: 501,
//      gid: 20,
//      rdev: 0,
//      blksize: 4096,
//      ino: 26436116,
//      size: 2443,
//      blocks: 8,
//      atime: Fri Jul 17 2015 03:01:54 GMT-0400 (EDT),
//      mtime: Wed Jul 15 2015 02:46:55 GMT-0400 (EDT),
//      ctime: Wed Jul 15 2015 02:46:55 GMT-0400 (EDT),
//      birthtime: Tue Jul 14 2015 23:03:58 GMT-0400 (EDT) },
// contents: <Buffer 23 20 66 69 6c 65 2d 63 6f 6e 74 65 6e 74 73 20 5b 21 5b 4e 50 4d 20 76 65 72 73 69 6f 6e 5d 28 68 74 74 70 73 3a 2f 2f 62 61 64 67 65 2e 66 75 72 79 ... > }
```

## History

**v0.2.0**

* renames `.getContents` method to `.async`
* adds `.sync` method

## About

### Related projects

* [file-stat](https://www.npmjs.com/package/file-stat): Set the `stat` property on a file object. Abstraction from vinyl-fs to support stream or… [more](https://github.com/jonschlinkert/file-stat) | [homepage](https://github.com/jonschlinkert/file-stat "Set the `stat` property on a file object. Abstraction from vinyl-fs to support stream or non-stream usage.")
* [file-symlinks](https://www.npmjs.com/package/file-symlinks): Resolve symlinks and expose the `stat` property on a file object. | [homepage](https://github.com/jonschlinkert/file-symlinks "Resolve symlinks and expose the `stat` property on a file object.")
* [stream-loader](https://www.npmjs.com/package/stream-loader): create a read stream from a glob of files. can be used as a loader-cache… [more](https://github.com/jonschlinkert/stream-loader) | [homepage](https://github.com/jonschlinkert/stream-loader "create a read stream from a glob of files. can be used as a loader-cache loader, or by itself as an a-la-carte replacement or addition to vinyl src.")
* [vinyl-fs](https://www.npmjs.com/package/vinyl-fs): Vinyl adapter for the file system | [homepage](http://github.com/wearefractal/vinyl-fs "Vinyl adapter for the file system")
* [vinyl](https://www.npmjs.com/package/vinyl): Virtual file format. | [homepage](https://github.com/gulpjs/vinyl#readme "Virtual file format.")

### Contributing

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).

### Building docs

_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_

To generate the readme, run the following command:

```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```

### Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

```sh
$ npm install && npm test
```

### Author

**Jon Schlinkert**

* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)

### License

Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
MIT

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 03, 2017._

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