首页 > 解决方案 > @Google-cloud/storage 在工作了很长时间后突然抛出奇怪的异常

问题描述

语境:

我有一个 API 端点,它提供存储在公共 Google Cloud Storage 存储桶中的 .stl 文件。直到本周的某个时候,它工作正常。我可以使用调试器逐步完成我的代码。我的项目中甚至没有引用有问题的 NPM 模块。我尝试使用 Google 文档上的确切代码进行下载并得到相同的异常:https ://github.com/googleapis/nodejs-storage/blob/master/samples/downloadFile.js

我试过的

npm rebuild @google-cloud/storage以及使用相同 Google npm 包的不同方式。

问题:

1.)不应该捕获,捕获异常以防止崩溃吗?

2.) 有人对解决方法有任何想法吗?

文件: https ://storage.cloud.google.com/fancy_induction_ui/1inX1in.stl

代码:

  getFile: async (req, res) => {
    try {
      const fileName = req.param('file');
      res.setHeader('Content-Type', 'application/octet-stream');
      res.setHeader('Content-Disposition', 'attachment; filename=' + fileName + '');
      const storage = new Storage();
      let file = await storage.bucket('fancy_induction_ui').file(fileName).createReadStream();
      file.pipe(res);
    } catch (e) {
      res.status(500).json({message: 'Something is wrong!', err: e.message});
    }
  }

堆栈跟踪:

path/to/code/node_modules/readable-stream/lib/_stream_writable.js:317
  var isBuf = !state.objectMode && _isUint8Array(chunk);
                     ^

TypeError: Cannot read property 'objectMode' of undefined
    at DestroyableTransform.Writable.write (path/to/code/node_modules/readable-stream/lib/_stream_writable.js:317:22)
    at PassThrough.ondata (_stream_readable.js:714:22)
    at PassThrough.emit (events.js:321:20)
    at PassThrough.EventEmitter.emit (domain.js:482:12)
    at PassThrough.Readable.read (_stream_readable.js:512:10)
    at flow (_stream_readable.js:989:34)
    at resume_ (_stream_readable.js:970:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

标签: javascriptnode.jsgoogle-cloud-storage

解决方案


在我看来,深度依赖中不同版本的可读流中的错误。我已经添加了

readable-stream: "3.6.0"

到我的 package.lock ,这对我来说是固定的这个问题。


推荐阅读