
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
string-to-file-stream
Advanced tools
Create a file stream from a string.
That is:
string2fileStream('string-content') === fs.createReadStream(/* path to a text file with content 'string-content' */)
Rewrite <fs.ReadStream>, and replace all file operations with equivalent string operations.
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.
/**
* @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]).
git clone https://github.com/ayqy/string-to-file-stream.git
cd string-to-file-stream
npm install
npm test
Initial release.
MIT
FAQs
Create file stream from string.
We found that string-to-file-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.