Giter Club home page Giter Club logo

string-to-file-stream's Introduction

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 = null] 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

License

MIT

string-to-file-stream's People

Contributors

ayqy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.