New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

string-to-file-stream

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-to-file-stream

Create file stream from string.

latest
Source
npmnpm
Version
2.0.0
Version published
Maintainers
1
Created
Source

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

Keywords

nodejs

FAQs

Package last updated on 16 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts