lib/gridfs/chunk.js
function(file, mongoObject, writeConcern) {
  if (!(this instanceof Chunk)) return new Chunk(file, mongoObject);

  this.file = file;
  var mongoObjectFinal = mongoObject == null ? {} : mongoObject;
  this.writeConcern = writeConcern || { w: 1 };
  this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
  this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
  this.data = new Binary();

  if (typeof mongoObjectFinal.data === 'string') {
    var buffer = Buffer.alloc(mongoObjectFinal.data.length);
    buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
    this.data = new Binary(buffer);
  } else if (Array.isArray(mongoObjectFinal.data)) {
    buffer = Buffer.alloc(mongoObjectFinal.data.length);
    var data = mongoObjectFinal.data.join('');
    buffer.write(data, 0, data.length, 'binary');
    this.data = new Binary(buffer);
  } else if (mongoObjectFinal.data && mongoObjectFinal.data._bsontype === 'Binary') {
    this.data = mongoObjectFinal.data;
  } else if (!Buffer.isBuffer(mongoObjectFinal.data) && !(mongoObjectFinal.data == null)) {
    throw Error('Illegal chunk format');
  }

  // Update position
  this.internalPosition = 0;
}
function(file, mongoObject, writeConcern) {
  if (!(this instanceof Chunk)) return new Chunk(file, mongoObject);

  this.file = file;
  var mongoObjectFinal = mongoObject == null ? {} : mongoObject;
  this.writeConcern = writeConcern || { w: 1 };
  this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
  this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
  this.data = new Binary();

  if (typeof mongoObjectFinal.data === 'string') {
    var buffer = Buffer.alloc(mongoObjectFinal.data.length);
    buffer.write(mongoObjectFinal.data, 0, mongoObjectFinal.data.length, 'binary');
    this.data = new Binary(buffer);
  } else if (Array.isArray(mongoObjectFinal.data)) {
    buffer = Buffer.alloc(mongoObjectFinal.data.length);
    var data = mongoObjectFinal.data.join('');
    buffer.write(data, 0, data.length, 'binary');
    this.data = new Binary(buffer);
  } else if (mongoObjectFinal.data && mongoObjectFinal.data._bsontype === 'Binary') {
    this.data = mongoObjectFinal.data;
  } else if (!Buffer.isBuffer(mongoObjectFinal.data) && !(mongoObjectFinal.data == null)) {
    throw Error('Illegal chunk format');
  }

  // Update position
  this.internalPosition = 0;
}
function ( file , mongoObject , writeConcern ) { if ( ! ( this instanceof Chunk ) ) return new Chunk ( file , mongoObject ) ; this . file = file ; var mongoObjectFinal = mongoObject == null ? { } : mongoObject ; this . writeConcern = writeConcern || { w : 1 } ; this . objectId = mongoObjectFinal . _id == null ? new ObjectID ( ) : mongoObjectFinal . _id ; this . chunkNumber = mongoObjectFinal . n == null ? 0 : mongoObjectFinal . n ; this . data = new Binary ( ) ; if ( typeof mongoObjectFinal . data === 'string' ) { var buffer = Buffer . alloc ( mongoObjectFinal . data . length ) ; buffer . write ( mongoObjectFinal . data , 0 , mongoObjectFinal . data . length , 'binary' ) ; this . data = new Binary ( buffer ) ; } else if ( Array . isArray ( mongoObjectFinal . data ) ) { buffer = Buffer . alloc ( mongoObjectFinal . data . length ) ; var data = mongoObjectFinal . data . join ( '' ) ; buffer . write ( data , 0 , data . length , 'binary' ) ; this . data = new Binary ( buffer ) ; } else if ( mongoObjectFinal . data && mongoObjectFinal . data . _bsontype === 'Binary' ) { this . data = mongoObjectFinal . data ; } else if ( ! Buffer . isBuffer ( mongoObjectFinal . data ) && ! ( mongoObjectFinal . data == null ) ) { throw Error ( 'Illegal chunk format' ) ; } // Update position this . internalPosition = 0 ; }
Class for representing a single chunk in GridFS.

@class

@param file {GridStore} The {@link GridStore} object holding this chunk.
@param mongoObject {object} The mongo object representation of this chunk.

@throws Error when the type of data field for {@link mongoObject} is not
supported. Currently supported types for data field are instances of
{@link String}, {@link Array}, {@link Binary} and {@link Binary}
from the bson module

@see Chunk#buildMongoObject
Class for representing a single chunk in GridFS .