{"id":"7aa9d2c08f16c8ce","repo":"mongodb/node-mongodb-native","slug":"stream-start-options-start-must-not-be-more-t","errorCode":null,"errorMessage":"Stream start (${options.start}) must not be more than the length of the file (${doc.length})","messagePattern":"Stream start \\((.+?)\\) must not be more than the length of the file \\((.+?)\\)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/gridfs/download.ts","lineNumber":437,"sourceCode":"\n  if (!stream.s.init) {\n    init(stream);\n    stream.s.init = true;\n  }\n\n  stream.once('file', () => {\n    callback();\n  });\n}\n\nfunction handleStartOption(\n  stream: GridFSBucketReadStream,\n  doc: Document,\n  options: GridFSBucketReadStreamOptions\n): number {\n  if (options && options.start != null) {\n    if (options.start > doc.length) {\n      throw new MongoInvalidArgumentError(\n        `Stream start (${options.start}) must not be more than the length of the file (${doc.length})`\n      );\n    }\n    if (options.start < 0) {\n      throw new MongoInvalidArgumentError(`Stream start (${options.start}) must not be negative`);\n    }\n    if (options.end != null && options.end < options.start) {\n      throw new MongoInvalidArgumentError(\n        `Stream start (${options.start}) must not be greater than stream end (${options.end})`\n      );\n    }\n\n    stream.s.bytesRead = Math.floor(options.start / doc.chunkSize) * doc.chunkSize;\n    stream.s.expected = Math.floor(options.start / doc.chunkSize);\n\n    return options.start - stream.s.bytesRead;\n  }\n  throw new MongoInvalidArgumentError('Start option must be defined');","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/gridfs/download.ts#L419-L455","documentation":"handleStartOption validates that the requested start offset does not exceed the file's length (as recorded in the files collection document). A start past EOF would read no useful bytes, so it is rejected up front.","triggerScenarios":"bucket.openDownloadStream(id, { start: 1000 }) when the file's length is 500.","commonSituations":"Wrong file id; stale length assumption; user-supplied range from pagination; race where the file was truncated.","solutions":["Fetch the GridFSFile length from the files collection first and clamp start to it","Validate user-provided ranges against the file length before opening the stream","Verify the file id matches the intended file"],"exampleFix":"// before\nbucket.openDownloadStream(id, { start: file.length + 100 });\n// after\nbucket.openDownloadStream(id, { start: Math.min(userStart, file.length) });","handlingStrategy":"validation","validationCode":"async function clampStart(bucket, id, desired) {\n  const file = await bucket.find({ _id: id }).next();\n  return Math.min(desired, file?.length ?? 0);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Look up the file length before computing ranges","Validate user-supplied start against the actual file size"],"tags":["gridfs","stream","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}