Readable.from is not a function node 8.12.0 生产中的错误

Posted

技术标签:

【中文标题】Readable.from is not a function node 8.12.0 生产中的错误【英文标题】:Readable.from is not a function node 8.12.0 error in production 【发布时间】:2022-01-16 22:03:48 【问题描述】:

从很多天以来,我都无法检测到我的 api 在生产中失败的根本原因。今天,当我检查 kubernetes 日志时,我发现一个错误,readable.from 不起作用。还有其他方法可以将缓冲区内容转换为流吗?

在本地我们使用的是节点14,我不想建议升级节点,所以我想从我的api来做。

这是我的 api:

function uploadFilesToDE(req, res) 
const blockId = req.headers["x-block-id"];
const chunkSize = Number(req.headers["x-content-length"]);
const userrole = req.headers["x-userrole"];
const pathname = req.headers["x-pathname"];

var form = new multiparty.Form();
form.parse(req, function (err, fields, files) 
if (files && files["payload"] && files["payload"].length > 0) 
var fileContent = fs.readFileSync(files["payload"][0].path);
var size = fileContent.length;
if (size !== chunkSize) 
sendBadRequest(res, "Chunk uploading was not completed");
return;


genericHandler.getUserSubscMapping().then(function (results) 
if (results != undefined && results != null) 
var sasurl = results[0].mapping.find(item => item.name == userrole).sasurl;
if (sasurl == null) 
response.status(500).send("Subscription mapping not configured");
return;

var host = sasurl.substring(0, sasurl.lastIndexOf("/"));
var containerName = sasurl.substring(sasurl.lastIndexOf("/"), sasurl.indexOf("?")).split("/")[1];
var saskey = sasurl.substring(sasurl.indexOf("?"), sasurl.length);
var blobService = storage.createBlobServiceWithSas(host, saskey);
//converting chunk[buffers] to readable stream
try 
var stream = Readable.from(fileContent);  // here is the error I see in logs

 catch (error) 
console.log('hhhhhhh',error);------Readable.from is not a function


var options = 
contentSettings: 
contentType: fields['Content-Type']



blobService.createBlockFromStream(blockId, containerName, pathname, stream, size, options, (error, data) => 
if (error) 
console.log("unable to upload", blockId, error);
res.status(500).send("Blob upload failed");
return;
 else 
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.setHeader("Content-Type", "application/json");
res.write(JSON.stringify(
status: 200,
size:size
));
res.end();

//    return res.status(200).json(
//         status: 200,
//         size
//     );

);

).catch((error) => 
console.log(error)
reject(false);
res.status(500).send("ssssssss");
return;
);

);

【问题讨论】:

【参考方案1】:

请检查这些变通方法是否可以缩小问题范围:

1.

const  Readable  = require('stream');

const fileStream =Readable.from(fileContent);   

//(OR) const stream = Readable.from(myBuffer.toString());
import  Readable  from 'stream'    // or import stream,  Readable  from 'stream';

//const buffer = new Buffer(img_string, 'base64')

const readable = new Readable()
readable._read = () =>  // _read is required but you can noop it
readable.push(buffer)
readable.push(null)

参考资料:

array values as a readable stream in node.js? - Stack Overflow nodejs - Stack Overflow
   const Duplex = require('stream').Duplex;  // core NodeJS API
    
   function bufferToStream(buffer)   
      let stream = new Duplex();
      stream.push(buffer);
      stream.push(null);
      return stream;
    

参考:buffer-to-stream-in-node

注意:但如果需要,在 nodeversion 中添加 readable.from v10.17。或 12.3 版本见 _stream_readable_from_iterable_options

【讨论】:

以上是关于Readable.from is not a function node 8.12.0 生产中的错误的主要内容,如果未能解决你的问题,请参考以下文章

What is the reason that a likelihood function is not a pdf?

python代码`if not x:` 和`if x is not None:`和`if not x is None:`使用

$(...).wordExport is not a function

小程序iOS Promise不支持finally语法 TypeError: undefined is not a function .finally is not a function

django is not a registered tag library.

怎样使用 svelte/store 中的 readable