尝试下载大于约256MB的文件时,request.js出错

时间:2018-03-28 21:42:51

标签: node.js request buffer node-request

使用请求JS作为工件处理程序的一部分,并且由于工件已大幅增加,我在尝试下载时不断收到此错误:

End of download
buffer.js:556
if (encoding === undefined) return buf.utf8Slice(start, end);
                                     ^

Error: "toString()" failed
at stringSlice (buffer.js:556:42)
at Buffer.toString (buffer.js:633:10)
at Request.<anonymous> (/node_modules/request/request.js:1137:39)
at emitOne (events.js:121:20)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> 
(node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)

我在这里看到了一些最具体的线索:https://github.com/request/request/issues/2826

我粘贴了它遇到问题的代码块。任何人都会看到任何不正确的建议,欢迎

function downloadArtifact(options) {
  return new Promise((resolve, reject) => {
    log('\nDownloading artifact from bamboo');

    if (options.skip.downloadArtifact) {
      log("Skipping");
      return resolve(options);
    }

    const planKey = options.branch.planKey;
    const buildNumber = options.latestBuild.number;
    const artifactName = options.bamboo.artifactName;
    const destinationDir = options.artifact.directory;
    const downloadUrl = options.bamboo.downloadUrl;
    const username = options.bamboo.username;
    const password = options.bamboo.password;
    const tmpFilePath = options.tmpFilePath;

    const requestOptions = {
      gzip: true,
      timeout: 15000,
      time: true,
      followRedirect: true,
      uri: downloadUrl,
      qs: {
        os_authType: 'basic',
        planKey: planKey,
        buildNumber: buildNumber,
        artifactName: artifactName
      },
      headers: {
        'Accept': 'application/octet-stream'
      }
    };

    request
      .get(downloadUrl, requestOptions, downloadArtifactCallback)
      .auth(username, password)
      .on('data', (data) => {
        process.stdout.write('.');
      })
      .on('end', () => {
        log('\nEnd of download');
      })
      .on('error', function(err) {
        console.log(err)
      })
      .pipe(fs.createWriteStream(tmpFilePath));

    function downloadArtifactCallback(err, res, body) {
      log('\nDownload artifact callback');

      if (err) {
        const error = {
          error: err,
          headers: res && res.headers,
          message: 'Error on the download'
        };
        return reject(error);
      }

      log(`Headers: ${stringify(res.headers)}`);
      log(`Elapsed Time (ms): ${res['elapsedTime']}`);

      // get file name from header
      let fileName;
      try {
        const contentDispositionHeader = res.headers[constants.headers.CONTENT_DISPOSITION];
        const parsedContentDisposition = contentDisposition.parse(contentDispositionHeader);
        fileName = parsedContentDisposition['parameters'].filename;
      } catch (e) {
        const error = {
          error: e.message,
          message: 'Most likely tried to download a web page'
        };
        return reject(error);
      }

      // rename the file
      const destinationPath = `${destinationDir}/${fileName}`;
      options.artifact.fileName = fileName;
      options.artifact.path = destinationPath;
      try {
        shell.mv(tmpFilePath, destinationPath);
      } catch (err) {
        return reject(err);
      }

      log(green('\nLeaving download artifact callback'));
      return resolve(options)
    }
  });
}

是的,在有人说有类似的问题之前,我已经看过所有这些问题,8小时后我的头疼,但没有成功。 :)

0 个答案:

没有答案