0

很多天以来,我都无法检测到我的 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;
});
}
});
}
4

1 回答 1

0

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

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 v10.17 中添加了 readable.from。或 12.3 版本参见 _stream_readable_from_iterable_options

于 2021-12-14T09:52:43.847 回答