Search code examples
javascriptnode.jsazurestream

How do I find out a size of a stream in NodeJS?


I am trying to upload a zip file to Azure file shares. The zip file is being generated using the archiver library and I tried uploading it using piping. I am always getting the error StorageError: The range specified is invalid for the current size of the resource.. How do I find out the size of my archive? I tried 'collecting' the size of the zip like this:

const uploadStream = new stream.PassThrough();
    zip.pipe(uploadStream);
    zip.finalize();

zip.on("data", (chunk) => {
      this.zipSize += chunk.length;
    });

zip.on("finish", () => {
      console.log(this.zipSize);
      this._callFileService(uploadStream);
    });
async _callFileService(data: stream.PassThrough) {
    fileService.createShareIfNotExists(
      this.name,
      (error, result, response) => {
        if (!error) {

          fileService.createFileFromStream(
            this.name,
            "",
            "test.zip",
            data,
            this.zipSize,
            {},
            function (error, result, response) {
              if (error) {
                console.log(error);
              } else {
                console.log("Done upload");
                console.log(result, response);
              }
            },
          );
        } else {
          console.log("there was an error: ", error);
        }
      },
    );
  }

I also tried just fetching the size of the archive, and I tried using stream.writableLength but everything throws the same error. Help?


Solution

  • Did you try logging the data you send to this function ? fileService.createFileFromStream

    edit: GJ solving this :)

    • according to the documentation https://www.npmjs.com/package/archiver zip.pointer() is the way to get the total size of the archive. no need to calculate "zipSize".

    • zip.finalize() should be called last to prevent race conditions. at least after zip.on("finish").