If your end goal is to turn a string into a readable stream, simply use the module into-stream.
var intoStream = require('into-stream')
intoStream('my-str').pipe(process.stdout)
If on the other hand, you want to know a way to actually do this yourself, the source code for that module is a little bit obtuse and so I'll create an example:
(You don't actually need a transform stream as in your code, just a writable stream)
var chars = 'my-str'.split('')
, Stream = require('stream').Readable
new Stream({ read: read }).pipe(process.stdout)
function read(n) {
this.push(chars.shift())
}
Note. This will only work with Node version >= 4. Previous versions didn't have the convenience methods in the Stream
constructor. For older Nodes (0.10.x, 0.12.x etc.) the following slightly longer example will work…
var chars = 'my-str'.split('')
, Stream = require('stream').Readable
, s = new Stream()
s._read = function (n) {
this.push(chars.shift())
}
s.pipe(process.stdout)