Skip to content

Commit

Permalink
process errors correctly and call callback asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
W-A-James committed Oct 1, 2024
1 parent e5ffffb commit 4ac8ff9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/gridfs/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,14 @@ function init(stream: GridFSBucketReadStream): void {
}
}

const remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow(
`Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms`
);
let remainingTimeMS: number | undefined;
try {
remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow(
`Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms`
);
} catch (error) {
return stream.destroy(error);
}

stream.s.cursor = stream.s.chunks
.find(filter, {
Expand All @@ -390,9 +395,17 @@ function init(stream: GridFSBucketReadStream): void {
return;
};

const remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow(
`Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms`
);
let remainingTimeMS: number | undefined;
try {
remainingTimeMS = stream.s.timeoutContext?.getRemainingTimeMSOrThrow(
`Download timed out after ${stream.s.timeoutContext?.timeoutMS}ms`
);
} catch (error) {
if (!stream.destroyed)
stream.destroy(error);
return;
}

findOneOptions.timeoutMS = remainingTimeMS;

stream.s.files.findOne(stream.s.filter, findOneOptions).then(handleReadResult, error => {
Expand Down
2 changes: 1 addition & 1 deletion src/gridfs/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class GridFSBucketWriteStream extends Writable {
}
);
} else {
callback();
return process.nextTick(callback);
}
}

Expand Down

0 comments on commit 4ac8ff9

Please sign in to comment.