Google-api-nodejs-client: Drive API: example mistake

Created on 8 Aug 2019  路  2Comments  路  Source: googleapis/google-api-nodejs-client

On /samples/drive/export.js there is a mistake
I tried the function a few times and it downloaded 20kb less each time
So I discover that the function finish to run by await to res.data.on('end')
but the stream didn't finish to write the data to the fs yet
The correct function is:

async function runSample() {
  // [START main_body]
  const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
  const destPath = path.join(os.tmpdir(), 'important.pdf');
  const dest = fs.createWriteStream(destPath);
  const res = await drive.files.export(
    {fileId, mimeType: 'application/pdf'},
    {responseType: 'stream'}
  );
  res.data.pipe(dest)
  await new Promise((resolve, reject) => {
    dest
      .on('finish', () => {
        console.log(`Done downloading document: ${destPath}.`);
        resolve();
      })
      .on('error', err => {
        console.error('Error downloading document.');
        reject(err);
      })
  });
  // [END main_body]
}
p2 bug docs

Most helpful comment

After I learn how to use stream:
The problem with the example is that the function wait for the end event of the read stream instead of waiting for the finish event of the file write stream to finish
Better code with error handling for the download and the file stream:

async function runSample() {
  // [START main_body]
  const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
  const destPath = path.join(os.tmpdir(), 'important.pdf');
  const dest = fs.createWriteStream(destPath);
  const res = await drive.files.export(
    {fileId, mimeType: 'application/pdf'},
    {responseType: 'stream'}
  );
await Promise.all([
    new Promise((resolve, reject) => {
      res.data
        .on('end', () => {
          console.log(`Done downloading document: ${destPath}.`);
          resolve();
        })
        .on('error', err => {
          console.error('Error downloading document.');
          reject(err);
        })
        .pipe(dest);
    }),
    new Promise((resolve, reject) => {
      dest
        .on('finish', () => {
          console.log(`Done saving document: ${destPath}.`);
          resolve();
        })
        .on('error', err => {
          console.error('Error saving document.');
          reject(err);
        })
    })
  ])
  // [END main_body]
}

All 2 comments

After I learn how to use stream:
The problem with the example is that the function wait for the end event of the read stream instead of waiting for the finish event of the file write stream to finish
Better code with error handling for the download and the file stream:

async function runSample() {
  // [START main_body]
  const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
  const destPath = path.join(os.tmpdir(), 'important.pdf');
  const dest = fs.createWriteStream(destPath);
  const res = await drive.files.export(
    {fileId, mimeType: 'application/pdf'},
    {responseType: 'stream'}
  );
await Promise.all([
    new Promise((resolve, reject) => {
      res.data
        .on('end', () => {
          console.log(`Done downloading document: ${destPath}.`);
          resolve();
        })
        .on('error', err => {
          console.error('Error downloading document.');
          reject(err);
        })
        .pipe(dest);
    }),
    new Promise((resolve, reject) => {
      dest
        .on('finish', () => {
          console.log(`Done saving document: ${destPath}.`);
          resolve();
        })
        .on('error', err => {
          console.error('Error saving document.');
          reject(err);
        })
    })
  ])
  // [END main_body]
}

@ttv20 good catch :+1: would happily land an update to the sample with end changed to finish. I'll leave this open, in case you don't have time to contribute the patch, and we'll update the sample soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JustinBeckwith picture JustinBeckwith  路  3Comments

streamnsight picture streamnsight  路  4Comments

joseparoli picture joseparoli  路  3Comments

rainabba picture rainabba  路  4Comments

Chethandsagar picture Chethandsagar  路  4Comments