# string-to-file-stream

> Create file stream from string.

Latest version **2.0.0** (published 2023-09-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install string-to-file-stream
pnpm add string-to-file-stream
yarn add string-to-file-stream
bun add string-to-file-stream
```

## 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 | 2023-09-16 |
| First published | 2019-08-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | ayqy |
| Maintainers | ayqy |
| Keywords | nodejs, string2file, string2filestream, covert, string, to, file, stream |

## Links

- npm: https://www.npmjs.com/package/string-to-file-stream
- Repository: https://github.com/ayqy/string-to-file-stream
- Homepage: https://github.com/ayqy/string-to-file-stream#readme
- Issues: https://github.com/ayqy/string-to-file-stream/issues
- npm.io page: https://npm.io/package/string-to-file-stream

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2023-09-16
- 1.3.0 — 2020-12-01
- 1.2.0 — 2020-06-15
- 1.1.0 — 2019-08-18

## README

## Target

Create a file stream from a string.

That is:

```js
string2fileStream('string-content') === fs.createReadStream(/* path to a text file with content 'string-content' */)
```

## Implementation

Rewrite [<fs.ReadStream>](https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_class_fs_readstream), and **replace all file operations with equivalent string operations.**

## Installation & Usage

```bash
npm install string-to-file-stream --save
```

Then, follow your intuitive feelings:

```js
const string2fileStream = require('string-to-file-stream');
const assert = require('assert');

const input = 'Oh, my great data!';
const s = string2fileStream(input);
s.on('data', (chunk) => {
  assert.equal(chunk.toString(), input);
});
```

Or the more useful example, upload a fake file:

```js
const string2fileStream = require('string-to-file-stream');
const FormData = require('form-data');

const formData = new FormData();
formData.append('filetoupload', string2fileStream('my-string-data', { path: 'no-this-file.txt' }));
form.submit('http://127.0.0.1:8123/fileupload', function(err, res) {
  console.log(res.statusCode);
});
```

See [Test Cases](https://github.com/ayqy/string-to-file-stream/blob/master/test/test.js) for more details.

## API Details

```js
/**
 * @typedef {Object} FileStreamOptions
 * @property {string} [flags = 'r']
 * @property {string} [encoding = 'utf8'] String encoding, 'utf8' by default.
 * @property {number} [fd = null]
 * @property {number} [mode = 0o666]
 * @property {number} [autoClose = true]
 * @property {number} [start = 0] Read bytes from specified position, start counting at 0.
 * @property {number} [end] Byte length of the input string.
 * @property {number} [highWaterMark = 64 * 1024]
 * @property {string} [path = 'no-this-file.txt'] Fake file path, which can be relative or absolute path, null by default.
 */

/**
 * Create file stream from a string.
 * @param {*} str The input string.
 * @param {FileStreamOptions} options Other options, including 'encoding', 'path' etc.
 * @return {fs.ReadStream} https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_class_fs_readstream
 */
function string2fileStream(str, options) {
  return new ReadStream(str, options);
}
```

P.S.Above option fileds without description are the same as `options` for [fs.createReadStream(path[, options])](https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_createreadstream_path_options).

## Contribution

```bash
git clone https://github.com/ayqy/string-to-file-stream.git
cd string-to-file-stream
npm install
npm test
```

## Changelog

### v1.3.0

- feat: Node.js < 9.3.0 supports ([#4](https://github.com/ayqy/string-to-file-stream/pull/4))

### v1.2.0

- feat: attach fake file path by default

### v1.1.0

Initial release.

## License

MIT

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