【问题标题】:Readable.from is not a function node 8.12.0 error in productionReadable.from is not a function node 8.12.0 生产中的错误
【发布时间】: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;
});
}
});
}

【问题讨论】:

    标签: node.js stream azure-blob-storage readable


    【解决方案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)
    

    参考资料:

       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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-04
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      相关资源
      最近更新 更多