Skip to content

Commit 3a01a94

Browse files
committed
Optimize FileResponse body stream for large models
1 parent 79255f0 commit 3a01a94

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/utils/hub.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ class FileResponse {
8080

8181
this.updateContentType();
8282

83-
let self = this;
83+
const stream = fs.createReadStream(filePath);
8484
this.body = new ReadableStream({
8585
start(controller) {
86-
self.arrayBuffer().then(buffer => {
87-
controller.enqueue(new Uint8Array(buffer));
88-
controller.close();
89-
})
86+
stream.on('data', (chunk) => controller.enqueue(chunk));
87+
stream.on('end', () => controller.close());
88+
stream.on('error', (err) => controller.error(err));
89+
},
90+
cancel() {
91+
stream.destroy();
9092
}
9193
});
9294
} else {

0 commit comments

Comments
 (0)