Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-847

GridFSBucket can't write a static buffer in few chunks

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.10
    • Component/s: MongoDB 3.2
    • Labels:
    • Environment:
      Node.js 4

      Let's say I have a StaticBuffer which it's size is 2X bytes where the default chunkSizeBytes is X.

      this is my starting code:

      var bucket = new mongodb.GridFSBucket(db);
      var writer = bucket.openUploadStreamWithId(fileId, null, {contentType: ContentType});
      writer.on('finish', function () {
          return true;
      }).on('error', function (Error) {
          return Error;
      });
      
      writer.end(StaticBuffer);
      

      When doing this, the app will crash because it is trying to close the stream twice.
      But also when trying to write and then end like this:

      writer.write(StaticBuffer, null, function () {
          writer.end(null)
      });
      

      the problem will persist because the end will be called before all chunks are done flushing to the db.

      The only way to workaround this, is to define the chunkSizeBytes to be bigger than the StaticBuffer and using only the end function in the following way:

      var bucket = new mongodb.GridFSBucket(db);
      var writer = bucket.openUploadStreamWithId(fileId, null, {chunkSizeBytes: StaticBuffer.length + 1, contentType: ContentType});
      writer.on('finish', function () {
          return true;
      }).on('error', function (Error) {
          return Error;
      });
      
      writer.end(StaticBuffer);
      

      But I want my buffer to be saved in multiple chunks which doesn't seems possible.

            Assignee:
            Unassigned Unassigned
            Reporter:
            tomg1988@gmail.com Tom Grossman
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: