Skip to content

Commit

Permalink
fix: get stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jun 22, 2024
1 parent 5d6277f commit e0f3a3e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/s3",
"version": "0.2.5",
"version": "0.2.6",
"description": "Filen S3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -50,7 +50,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@filen/sdk": "^0.1.120",
"@filen/sdk": "^0.1.122",
"aws-sdk": "^2.1636.0",
"body-parser": "^1.20.2",
"dayjs": "^1.11.11",
Expand Down
37 changes: 35 additions & 2 deletions src/handlers/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class GetObject {
res.set("Content-Type", mimeType)
res.set("Accept-Ranges", "bytes")

const stream = await this.server.sdk.cloud().downloadFileToReadableStream({
const stream = this.server.sdk.cloud().downloadFileToReadableStream({
uuid: object.stats.uuid,
bucket: object.stats.bucket,
region: object.stats.region,
Expand All @@ -78,7 +78,40 @@ export class GetObject {

const nodeStream = Readable.fromWeb(stream as unknown as ReadableStreamWebType<Buffer>)

nodeStream.once("error", next)
const cleanup = () => {
try {
if (!nodeStream.closed || !nodeStream.destroyed) {
nodeStream.destroy()
}
} catch {
// Noop
}
}

res.once("close", () => {
cleanup()
})

res.once("error", () => {
cleanup()
})

res.once("finish", () => {
cleanup()
})

req.once("close", () => {
cleanup()
})

req.once("error", () => {
cleanup()
})

nodeStream.once("error", err => {
cleanup()
next(err)
})

nodeStream.pipe(res)
}
Expand Down
4 changes: 4 additions & 0 deletions src/semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const Semaphore = function (this: ISemaphore, max: number) {
}

this.release = function (): void {
if (counter <= 0) {
return
}

counter--

take()
Expand Down

0 comments on commit e0f3a3e

Please sign in to comment.