npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

string-to-file-stream

v2.0.0

Published

Create file stream from string.

Downloads

239,919

Readme

Target

Create a file stream from a string.

That is:

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

Implementation

Rewrite <fs.ReadStream>, and replace all file operations with equivalent string operations.

Installation & Usage

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

Then, follow your intuitive feelings:

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:

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 for more details.

API Details

/**
 * @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]).

Contribution

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)

v1.2.0

  • feat: attach fake file path by default

v1.1.0

Initial release.

License

MIT