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