Node.js streams not flushing between uses. How to flush?










1















I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream that I am piping to a custom write stream. The highWaterMark for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.



On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:



The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).



Here is the code I've got:



return new Promise(async (resolve, reject) => 
const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).

const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );

let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
readFileStream.pipe(writeStream);

writeStream.on("finish", () =>
resolve(writeStream.urn);
);
writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
);


I've tried calling .destroy() on both the read and write stream. I've tried calling .end(), .unpipe(). None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?










share|improve this question


























    1















    I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream that I am piping to a custom write stream. The highWaterMark for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.



    On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:



    The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).



    Here is the code I've got:



    return new Promise(async (resolve, reject) => 
    const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).

    const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
    let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );

    let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
    readFileStream.pipe(writeStream);

    writeStream.on("finish", () =>
    resolve(writeStream.urn);
    );
    writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
    );


    I've tried calling .destroy() on both the read and write stream. I've tried calling .end(), .unpipe(). None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?










    share|improve this question
























      1












      1








      1








      I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream that I am piping to a custom write stream. The highWaterMark for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.



      On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:



      The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).



      Here is the code I've got:



      return new Promise(async (resolve, reject) => 
      const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).

      const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
      let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );

      let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
      readFileStream.pipe(writeStream);

      writeStream.on("finish", () =>
      resolve(writeStream.urn);
      );
      writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
      );


      I've tried calling .destroy() on both the read and write stream. I've tried calling .end(), .unpipe(). None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?










      share|improve this question














      I'm having an issue with Node.js Streams/Buffers where they aren't being closed/flushed following first use. I have a read stream created from fs.createReadStream that I am piping to a custom write stream. The highWaterMark for each chunk is ~2MB (this is important). When I initially stream through a ~3MB file, it is handled in 2 chunks by my stream, first a ~2MB chunk, then a ~1MB chunk. This is expected.



      On the second file that I pass through, the first chunk is only ~1MB. This is an issue. When I add up the bytes piped, I can see that clearly after the first file, the streams/buffers associated were not cleaned up properly. This can be shown with the following math:



      The final chunk that is transferred of the first file is 875,837 bytes. The first chunk of the next file that is transferred is 1,221,316 bytes (expected: 2,097,152 bytes). When you add 875,837 to 1,221,316, you get 2,097,153, which is the high water mark I mentioned earlier (with an off by one error).



      Here is the code I've got:



      return new Promise(async (resolve, reject) => 
      const maximalChunkedTransferSize = 2*1024*1024; // 2MB (Minimum amount, Autodesk recommends 5MB).

      const pathToFile = await bucketManagement.locationOfBucketZip(bucketEntity.name);
      let readFileStream = fs.createReadStream(pathToFile, highWaterMark: maximalChunkedTransferSize );

      let writeStream = new HttpAutodeskPutBucketObjectWriteStream(accessToken, bucketEntity);
      readFileStream.pipe(writeStream);

      writeStream.on("finish", () =>
      resolve(writeStream.urn);
      );
      writeStream.on("error", err => reject("Putting the file into the bucket failed. " + err.message));
      );


      I've tried calling .destroy() on both the read and write stream. I've tried calling .end(), .unpipe(). None of these have worked. How can I destroy/flush the stream and/or underlying buffer so that the first chunk of the next file is the expected 2MB?







      node.js stream buffer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 20:45









      ReactingToAngularVuesReactingToAngularVues

      2,2941356104




      2,2941356104






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289234%2fnode-js-streams-not-flushing-between-uses-how-to-flush%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289234%2fnode-js-streams-not-flushing-between-uses-how-to-flush%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo