Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-files-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix partial file-backed HTTP bodies to report the selected byte range as their content length.
23 changes: 19 additions & 4 deletions packages/effect/src/unstable/http/HttpBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,12 +484,26 @@ export const stream = (
contentLength?: number
): Stream => new Stream(body, contentType ?? "application/octet-stream", contentLength)

const fileContentLength = (
size: FileSystem.SizeInput,
options?: {
readonly bytesToRead?: FileSystem.SizeInput | undefined
readonly offset?: FileSystem.SizeInput | undefined
}
): number => {
const available = Math.max(0, Number(size) - Number(options?.offset ?? 0))
return options?.bytesToRead === undefined
? available
: Math.min(available, Math.max(0, Number(options.bytesToRead)))
}

/**
* Creates a streaming HTTP body for a file path.
*
* **Details**
*
* The effect requires `FileSystem`, stats the file to set the content length, and can fail with `PlatformError`.
* The effect requires `FileSystem`, stats the file to set the selected content length, and can fail with
* `PlatformError`.
*
* @category constructors
* @since 4.0.0
Expand All @@ -510,7 +524,7 @@ export const file = (
stream(
fs.stream(path, options),
options?.contentType,
Number(info.size)
fileContentLength(info.size, options)
))
)

Expand All @@ -519,7 +533,8 @@ export const file = (
*
* **Details**
*
* The effect requires `FileSystem`, uses the provided file size as the content length, and can fail with `PlatformError`.
* The effect requires `FileSystem`, uses the provided file size to determine the selected content length, and can
* fail with `PlatformError`.
*
* @category constructors
* @since 4.0.0
Expand All @@ -540,6 +555,6 @@ export const fileFromInfo = (
stream(
fs.stream(path, options),
options?.contentType,
Number(info.size)
fileContentLength(info.size, options)
)
)
14 changes: 14 additions & 0 deletions packages/effect/test/unstable/http/HttpBody.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { assert, it } from "@effect/vitest"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove audit from the file name.

import { Effect, FileSystem, Stream } from "effect"
import { HttpBody } from "effect/unstable/http"

it.effect("uses the selected byte count as partial file content length", () =>
Effect.gen(function*() {
const body = yield* HttpBody.fileFromInfo("x", { size: 6n } as any, { offset: 2, bytesToRead: 2 }).pipe(
Effect.provideService(FileSystem.FileSystem, {
stream: () => Stream.succeed(new Uint8Array([3, 4]))
} as any)
)
const bytes = yield* Stream.mkUint8Array(body.stream)
assert.strictEqual(body.contentLength, bytes.length)
}))
Loading