我正在使用一个库,ya-csv,期望文件或流作为输入,但我有一个字符串。
如何将该字符串转换为节点中的流?
我正在使用一个库,ya-csv,期望文件或流作为输入,但我有一个字符串。
如何将该字符串转换为节点中的流?
当前回答
在coffee-script:
class StringStream extends Readable
constructor: (@str) ->
super()
_read: (size) ->
@push @str
@push null
使用它:
new StringStream('text here').pipe(stream1).pipe(stream2)
其他回答
编辑:Garth的答案可能更好。
我以前的答案文本保存在下面。
要将字符串转换为流,可以使用pause through流:
through().pause().queue('your string').end()
例子:
var through = require('through')
// Create a paused stream and buffer some data into it:
var stream = through().pause().queue('your string').end()
// Pass stream around:
callback(null, stream)
// Now that a consumer has attached, remember to resume the stream:
stream.resume()
正如@substack在#node中纠正了我,node v10中的新streams API使这更容易:
const Readable = require('stream').Readable;
const s = new Readable();
s._read = () => {}; // redundant? see update below
s.push('your text here');
s.push(null);
之后,你可以自由地将它输送给你的目标客户。
它不像resumer一行程序那样简洁,但它确实避免了额外的依赖关系。
(更新:在v0.10.26到v9.2.1到目前为止,如果你没有设置_read,直接从REPL提示符调用push将会崩溃,并出现一个未实现的异常。它不会在函数或脚本中崩溃。如果前后矛盾让你紧张,那就加上noop。)
我厌倦了每六个月重新学习一次,所以我发布了一个npm模块来抽象实现细节:
https://www.npmjs.com/package/streamify-string
这是该模块的核心:
const Readable = require('stream').Readable;
const util = require('util');
function Streamify(str, options) {
if (! (this instanceof Streamify)) {
return new Streamify(str, options);
}
Readable.call(this, options);
this.str = str;
}
util.inherits(Streamify, Readable);
Streamify.prototype._read = function (size) {
var chunk = this.str.slice(0, size);
if (chunk) {
this.str = this.str.slice(size);
this.push(chunk);
}
else {
this.push(null);
}
};
module.exports = Streamify;
STR是在调用时必须传递给构造函数的字符串,并将由流作为数据输出。根据文档,选项是可以传递给流的典型选项。
根据Travis CI,它应该与大多数版本的节点兼容。
在NodeJS中,你可以通过以下几种方式创建一个可读流:
解决方案1
你可以用fs模块来做。函数fs.createReadStream()允许你打开一个可读的流,你所要做的就是传递文件的路径来开始流。
const fs = require('fs');
const readable_stream = fs.createReadStream('file_path');
解决方案2
If you don't want to create file, you can create an in-memory stream and do something with it (for example, upload it somewhere). You can do this with stream module. You can import Readable from stream module and you can create a readable stream. When creating an object, you can also implement read() method which is used to read the data out of the internal buffer. If no data available to be read, null is returned. The optional size argument specifies a specific number of bytes to read. If the size argument is not specified, all of the data contained in the internal buffer will be returned.
const Readable = require('stream').Readable;
const readable_stream = new Readable({
read(size) {
// ...
}
});
解决方案3
当你通过网络获取一些东西时,它可以像流一样被获取(例如,你从一些API获取一个PDF文档)。
const axios = require('axios');
const readable_stream = await axios({
method: 'get',
url: "pdf_resource_url",
responseType: 'stream'
}).data;
解决方案4
第三方包可以支持创建流作为一个特性。这是aws-sdk包的一种方法,通常用于将文件上传到S3。
const file = await s3.getObject(params).createReadStream();
下面是TypeScript中的一个简洁的解决方案:
import { Readable } from 'stream'
class ReadableString extends Readable {
private sent = false
constructor(
private str: string
) {
super();
}
_read() {
if (!this.sent) {
this.push(Buffer.from(this.str));
this.sent = true
}
else {
this.push(null)
}
}
}
const stringStream = new ReadableString('string to be streamed...')