{"id":"d784bd48b0d739bf","repo":"mongodb/node-mongodb-native","slug":"stream-start-options-start-must-not-be-greate","errorCode":null,"errorMessage":"Stream start (${options.start}) must not be greater than stream end (${options.end})","messagePattern":"Stream start \\((.+?)\\) must not be greater than stream end \\((.+?)\\)","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/gridfs/download.ts","lineNumber":445,"sourceCode":"  });\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');\n}\n\nfunction handleEndOption(\n  stream: GridFSBucketReadStream,\n  doc: Document,\n  cursor: FindCursor<GridFSChunk>,\n  options: GridFSBucketReadStreamOptions\n) {","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/gridfs/download.ts#L427-L463","documentation":"handleStartOption enforces that start is not greater than end. A reversed range would produce a nonsensical byte window.","triggerScenarios":"bucket.openDownloadStream(id, { start: 200, end: 100 }).","commonSituations":"Swapped arguments; bad slice math; user input ordered high-to-low.","solutions":["Order the arguments so start <= end before opening the stream","Normalize with { start: Math.min(a,b), end: Math.max(a,b) }","Validate user-supplied ranges"],"exampleFix":"// before\n{ start: 200, end: 100 }\n// after\n{ start: Math.min(a, b), end: Math.max(a, b) }","handlingStrategy":"validation","validationCode":"if (start > end) [start, end] = [end, start];","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize ranges so start <= end before opening the stream","Validate user-supplied slice bounds"],"tags":["gridfs","stream","validation"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}