mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
feat(server): stream fs.read responses with HTTP Range support (#53088)
This commit is contained in:
committed by
GitHub
Unverified
parent
369aad308b
commit
74678c4677
@@ -2,7 +2,7 @@ export * as FileSystem from "./filesystem.js"
|
||||
|
||||
import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import path from "path"
|
||||
import { Context, Effect, Layer, Schema } from "effect"
|
||||
import { Context, Effect, Layer, type Option, type PlatformError, Schema, type Stream } from "effect"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { Location } from "./location.js"
|
||||
import { AbsolutePath, PositiveInt, RelativePath } from "./schema.js"
|
||||
@@ -87,10 +87,18 @@ export class GrepInput extends Schema.Class<GrepInput>("FileSystem.GrepInput")({
|
||||
|
||||
export const Event = FileSystem.Event
|
||||
|
||||
export interface File {
|
||||
readonly mime: string
|
||||
readonly size: number
|
||||
readonly mtime: Option.Option<Date>
|
||||
readonly stream: (options?: {
|
||||
readonly offset?: number
|
||||
readonly bytesToRead?: number
|
||||
}) => Stream.Stream<Uint8Array, PlatformError.PlatformError>
|
||||
}
|
||||
|
||||
export interface Interface {
|
||||
readonly read: (
|
||||
input: ReadInput,
|
||||
) => Effect.Effect<{ readonly content: Uint8Array; readonly mime: string }, NotFoundError>
|
||||
readonly read: (input: ReadInput) => Effect.Effect<File, NotFoundError>
|
||||
readonly list: (input?: ListInput) => Effect.Effect<Entry[]>
|
||||
readonly find: (input: FindInput) => Effect.Effect<Entry[]>
|
||||
/** Writes a file at an absolute path or one relative to the location; not confined to it. */
|
||||
@@ -129,25 +137,25 @@ const baseLayer = Layer.effect(
|
||||
return Effect.die(cause)
|
||||
}),
|
||||
)
|
||||
const resolve = Effect.fnUntraced(function* (input?: RelativePath) {
|
||||
const absolute = path.resolve(location.directory, input ?? ".")
|
||||
if (!FSUtil.contains(location.directory, absolute))
|
||||
return yield* Effect.die(new Error("Path escapes the location"))
|
||||
const real = yield* fs.realPath(absolute)
|
||||
if (!FSUtil.contains(root, real)) return yield* Effect.die(new Error("Path escapes the location"))
|
||||
const resolve = Effect.fnUntraced(function* (input: RelativePath) {
|
||||
if (input.includes("\u0000")) return yield* Effect.fail(new NotFoundError({ path: input }))
|
||||
const absolute = path.resolve(location.directory, input)
|
||||
if (!FSUtil.contains(location.directory, absolute)) return yield* Effect.fail(new NotFoundError({ path: input }))
|
||||
const real = yield* fs.realPath(absolute).pipe(
|
||||
Effect.catchReason(
|
||||
"PlatformError",
|
||||
"NotFound",
|
||||
() => Effect.fail(new NotFoundError({ path: input })),
|
||||
(_, error) => Effect.die(error),
|
||||
),
|
||||
)
|
||||
if (!FSUtil.contains(root, real)) return yield* Effect.fail(new NotFoundError({ path: input }))
|
||||
return { absolute, real, directory: location.directory }
|
||||
})
|
||||
return Service.of({
|
||||
find: search.find,
|
||||
read: Effect.fn("FileSystem.read")(function* (input) {
|
||||
const target = yield* resolve(input.path).pipe(
|
||||
Effect.catchReason(
|
||||
"PlatformError",
|
||||
"NotFound",
|
||||
() => Effect.fail(new NotFoundError({ path: input.path })),
|
||||
(_, error) => Effect.die(error),
|
||||
),
|
||||
)
|
||||
const target = yield* resolve(input.path)
|
||||
const info = yield* fs.stat(target.real).pipe(
|
||||
Effect.catchReason(
|
||||
"PlatformError",
|
||||
@@ -156,17 +164,12 @@ const baseLayer = Layer.effect(
|
||||
(_, error) => Effect.die(error),
|
||||
),
|
||||
)
|
||||
if (info.type !== "File") return yield* Effect.die(new Error("Path is not a file"))
|
||||
if (info.type !== "File") return yield* Effect.fail(new NotFoundError({ path: input.path }))
|
||||
return {
|
||||
content: yield* fs.readFile(target.real).pipe(
|
||||
Effect.catchReason(
|
||||
"PlatformError",
|
||||
"NotFound",
|
||||
() => Effect.fail(new NotFoundError({ path: input.path })),
|
||||
(_, error) => Effect.die(error),
|
||||
),
|
||||
),
|
||||
mime: FSUtil.mimeType(target.real),
|
||||
size: Number(info.size),
|
||||
mtime: info.mtime,
|
||||
stream: (options) => fs.stream(target.real, options),
|
||||
}
|
||||
}),
|
||||
list: Effect.fn("FileSystem.list")(function* (input = {}) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import fs from "fs/promises"
|
||||
import path from "path"
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Cause, Effect, Exit, Layer, PlatformError } from "effect"
|
||||
import { Cause, Effect, Exit, Layer, Option, PlatformError, Stream } from "effect"
|
||||
import { FSUtil } from "@opencode/util/fs-util"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { FileSystem } from "@opencode/core/filesystem"
|
||||
@@ -37,13 +37,18 @@ describe("FileSystem", () => {
|
||||
withTmp((directory) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.promise(() => fs.writeFile(path.join(directory, "text.txt"), "hello"))
|
||||
yield* Effect.promise(() => fs.writeFile(path.join(directory, "data.bin"), Buffer.from([0, 1, 2])))
|
||||
yield* Effect.promise(() => fs.writeFile(path.join(directory, "data.bin"), Buffer.from([0, 1, 2, 3, 4])))
|
||||
const service = yield* FileSystem.Service
|
||||
const text = yield* service.read({ path: RelativePath.make("text.txt") })
|
||||
const binary = yield* service.read({ path: RelativePath.make("data.bin") })
|
||||
expect(new TextDecoder().decode(text.content)).toBe("hello")
|
||||
expect(new TextDecoder().decode(yield* Stream.mkUint8Array(text.stream()))).toBe("hello")
|
||||
expect(text.mime).toBe("text/plain")
|
||||
expect(binary.content).toEqual(new Uint8Array([0, 1, 2]))
|
||||
expect(text.size).toBe(5)
|
||||
expect(Option.isSome(text.mtime)).toBe(true)
|
||||
expect(yield* Stream.mkUint8Array(binary.stream())).toEqual(new Uint8Array([0, 1, 2, 3, 4]))
|
||||
expect(yield* Stream.mkUint8Array(binary.stream({ offset: 1, bytesToRead: 3 }))).toEqual(
|
||||
new Uint8Array([1, 2, 3]),
|
||||
)
|
||||
}).pipe(provide(directory)),
|
||||
),
|
||||
)
|
||||
@@ -190,9 +195,10 @@ describe("FileSystem", () => {
|
||||
// the location root to the real directory.
|
||||
const read = yield* FileSystem.Service.pipe(
|
||||
Effect.flatMap((service) => service.read({ path: RelativePath.make("file.txt") })),
|
||||
Effect.flatMap((file) => Stream.mkUint8Array(file.stream())),
|
||||
provide(link),
|
||||
)
|
||||
expect(new TextDecoder().decode(read.content)).toBe("linked")
|
||||
expect(new TextDecoder().decode(read)).toBe("linked")
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -8481,7 +8481,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Serve one file relative to the requested location.",
|
||||
"description": "Stream one file relative to the requested location with ETag, Last-Modified, and single-range HTTP Range support (206 Partial Content, 416 Range Not Satisfiable, and 304 Not Modified; multi-range and malformed Range headers fall back to 200).",
|
||||
"summary": "Read file"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -39,7 +39,8 @@ export const FileSystemGroup = HttpApiGroup.make("server.fs")
|
||||
OpenApi.annotations({
|
||||
identifier: "fs.read",
|
||||
summary: "Read file",
|
||||
description: "Serve one file relative to the requested location.",
|
||||
description:
|
||||
"Stream one file relative to the requested location with ETag, Last-Modified, and single-range HTTP Range support (206 Partial Content, 416 Range Not Satisfiable, and 304 Not Modified; multi-range and malformed Range headers fall back to 200).",
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { FileSystem } from "@opencode/core/filesystem"
|
||||
import { RelativePath } from "@opencode/core/schema"
|
||||
import { FileNotFoundError } from "@opencode/protocol/errors"
|
||||
import { Effect } from "effect"
|
||||
import { HttpServerResponse } from "effect/unstable/http"
|
||||
import { Effect, Option, Stream } from "effect"
|
||||
import { type HttpServerRequest, HttpServerResponse } from "effect/unstable/http"
|
||||
import { HttpApiBuilder } from "effect/unstable/httpapi"
|
||||
import { Api } from "../api"
|
||||
import { response } from "../location"
|
||||
@@ -12,19 +12,16 @@ export const FileSystemHandler = HttpApiBuilder.group(Api, "server.fs", (handler
|
||||
return handlers
|
||||
.handleRaw("fs.read", (ctx) =>
|
||||
Effect.gen(function* () {
|
||||
const path = yield* decodeRequestPath(ctx.request.url)
|
||||
const fs = yield* FileSystem.Service
|
||||
const file = yield* fs
|
||||
.read({
|
||||
path: RelativePath.make(
|
||||
decodeURIComponent(new URL(ctx.request.url, "http://localhost").pathname.slice(13)),
|
||||
),
|
||||
})
|
||||
.read({ path })
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(error) => new FileNotFoundError({ path: error.path, message: `File not found: ${error.path}` }),
|
||||
),
|
||||
)
|
||||
return HttpServerResponse.uint8Array(file.content, { contentType: file.mime })
|
||||
return serveFile(ctx.request, file)
|
||||
}),
|
||||
)
|
||||
.handle("fs.list", (ctx) =>
|
||||
@@ -53,3 +50,86 @@ export const FileSystemHandler = HttpApiBuilder.group(Api, "server.fs", (handler
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
function decodeRequestPath(url: string) {
|
||||
const raw = new URL(url, "http://localhost").pathname.slice(13)
|
||||
return Effect.try({
|
||||
try: () => RelativePath.make(decodeURIComponent(raw)),
|
||||
catch: () => new FileNotFoundError({ path: raw, message: `File not found: ${raw}` }),
|
||||
})
|
||||
}
|
||||
|
||||
function serveFile(request: HttpServerRequest.HttpServerRequest, file: FileSystem.File) {
|
||||
const etag = `W/"${file.size.toString(16)}-${Option.match(file.mtime, {
|
||||
onNone: () => "0",
|
||||
onSome: (mtime) => mtime.getTime().toString(16),
|
||||
})}"`
|
||||
const lastModified = Option.getOrUndefined(Option.map(file.mtime, (mtime) => mtime.toUTCString()))
|
||||
// Clients must revalidate after agent edits; no-transform keeps compression from changing Content-Length.
|
||||
const validators = {
|
||||
"cache-control": "no-cache, no-transform",
|
||||
etag,
|
||||
...(lastModified === undefined ? {} : { "last-modified": lastModified }),
|
||||
}
|
||||
if (isNotModified(request, etag, lastModified)) return HttpServerResponse.empty({ status: 304, headers: validators })
|
||||
|
||||
const rangeHeader = request.method === "GET" ? request.headers["range"] : undefined
|
||||
const range =
|
||||
rangeHeader !== undefined && matchesIfRange(request.headers["if-range"], etag, lastModified)
|
||||
? parseRange(rangeHeader, file.size)
|
||||
: undefined
|
||||
if (range === "unsatisfiable")
|
||||
return HttpServerResponse.empty({
|
||||
status: 416,
|
||||
headers: { "accept-ranges": "bytes", "content-range": `bytes */${file.size}` },
|
||||
})
|
||||
|
||||
const offset = range?.start ?? 0
|
||||
const length = range === undefined ? file.size : range.end - range.start + 1
|
||||
return HttpServerResponse.stream(length === 0 ? Stream.empty : file.stream({ offset, bytesToRead: length }), {
|
||||
status: range === undefined ? 200 : 206,
|
||||
contentLength: length,
|
||||
headers: {
|
||||
...validators,
|
||||
"accept-ranges": "bytes",
|
||||
"content-type": file.mime,
|
||||
...(range === undefined ? {} : { "content-range": `bytes ${range.start}-${range.end}/${file.size}` }),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function isNotModified(request: HttpServerRequest.HttpServerRequest, etag: string, lastModified: string | undefined) {
|
||||
const ifNoneMatch = request.headers["if-none-match"]
|
||||
if (ifNoneMatch !== undefined)
|
||||
return ifNoneMatch.split(",").some((tag) => tag.trim() === "*" || stripWeak(tag) === stripWeak(etag))
|
||||
const ifModifiedSince = request.headers["if-modified-since"]
|
||||
return (
|
||||
ifModifiedSince !== undefined &&
|
||||
lastModified !== undefined &&
|
||||
Date.parse(lastModified) <= Date.parse(ifModifiedSince)
|
||||
)
|
||||
}
|
||||
|
||||
function matchesIfRange(ifRange: string | undefined, etag: string, lastModified: string | undefined) {
|
||||
if (ifRange === undefined) return true
|
||||
if (/^\s*(w\/)?"/i.test(ifRange)) return stripWeak(ifRange) === stripWeak(etag)
|
||||
return lastModified !== undefined && Date.parse(ifRange) === Date.parse(lastModified)
|
||||
}
|
||||
|
||||
function stripWeak(etag: string) {
|
||||
return etag.trim().replace(/^w\//i, "")
|
||||
}
|
||||
|
||||
function parseRange(header: string, size: number) {
|
||||
const match = /^bytes=\s*(\d*)\s*-\s*(\d*)\s*$/i.exec(header.trim())
|
||||
if (match === null || (match[1] === "" && match[2] === "")) return undefined
|
||||
if (match[1] === "") {
|
||||
const suffix = Number(match[2])
|
||||
if (suffix === 0 || size === 0) return "unsatisfiable"
|
||||
return { start: Math.max(size - suffix, 0), end: size - 1 }
|
||||
}
|
||||
const start = Number(match[1])
|
||||
const end = match[2] === "" ? size - 1 : Number(match[2])
|
||||
if (start > end || start >= size) return "unsatisfiable"
|
||||
return { start, end: Math.min(end, size - 1) }
|
||||
}
|
||||
|
||||
@@ -267,6 +267,7 @@ it.live("returns 404 when a previously readable file is deleted", () =>
|
||||
|
||||
const readable = yield* Effect.promise(() => handler(new Request(url)))
|
||||
expect(readable.status).toBe(200)
|
||||
expect(yield* Effect.promise(() => readable.text())).toBe("content")
|
||||
|
||||
yield* Effect.promise(() => fs.unlink(file))
|
||||
const missing = yield* Effect.promise(() => handler(new Request(url)))
|
||||
|
||||
@@ -77,3 +77,261 @@ it.live(
|
||||
}),
|
||||
15_000,
|
||||
)
|
||||
|
||||
it.live(
|
||||
"streams files with Range, conditional requests, confinement, and multi-megabyte byte integrity",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const tmp = yield* tmpdirScoped()
|
||||
const current = path.join(tmp.path, "project")
|
||||
const outside = path.join(tmp.path, "outside")
|
||||
const largeBytes = Uint8Array.from({ length: 4 * 1024 * 1024 + 321 }, (_, i) => (i * 31 + (i >> 8)) & 0xff)
|
||||
|
||||
yield* Effect.promise(async () => {
|
||||
await fs.mkdir(path.join(current, "nested"), { recursive: true })
|
||||
await fs.mkdir(outside, { recursive: true })
|
||||
await fs.writeFile(path.join(current, "clip.mp4"), "0123456789")
|
||||
await fs.writeFile(path.join(current, "empty.bin"), "")
|
||||
await fs.writeFile(path.join(current, "large.mp4"), largeBytes)
|
||||
await fs.writeFile(path.join(current, "notes.txt"), "compressible ".repeat(1024))
|
||||
await fs.writeFile(path.join(outside, "secret.txt"), "secret")
|
||||
await fs.symlink(outside, path.join(current, "escape-link"), "junction")
|
||||
})
|
||||
|
||||
const server = yield* startServer(path.join(tmp.path, "config"))
|
||||
const readUrl = (relativePath: string) => {
|
||||
const url = new URL(`/api/fs/read/${relativePath}`, server.base)
|
||||
url.searchParams.set("location[directory]", current)
|
||||
return url
|
||||
}
|
||||
const request = (relativePath: string, init?: BunFetchRequestInit) =>
|
||||
Effect.promise(() =>
|
||||
fetch(readUrl(relativePath), {
|
||||
...init,
|
||||
headers: {
|
||||
...server.headers,
|
||||
...init?.headers,
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
// 1. Full 200 read headers and body
|
||||
const full = yield* request("clip.mp4")
|
||||
expect(full.status).toBe(200)
|
||||
expect(full.headers.get("accept-ranges")).toBe("bytes")
|
||||
expect(full.headers.get("content-length")).toBe("10")
|
||||
expect(full.headers.get("content-type")).toBe("video/mp4")
|
||||
expect(full.headers.get("content-encoding")).toBeNull()
|
||||
expect(full.headers.get("cache-control")).toBe("no-cache, no-transform")
|
||||
const etag = full.headers.get("etag")
|
||||
const lastModified = full.headers.get("last-modified")
|
||||
expect(etag).toMatch(/^W\/"[0-9a-f]+-[0-9a-f]+"$/)
|
||||
expect(lastModified).toBeTruthy()
|
||||
expect(yield* Effect.promise(() => full.text())).toBe("0123456789")
|
||||
|
||||
// 2. HEAD probe (and HEAD with Range ignores Range)
|
||||
const head = yield* request("clip.mp4", { method: "HEAD", headers: { Range: "bytes=0-3" } })
|
||||
expect(head.status).toBe(200)
|
||||
expect(head.headers.get("accept-ranges")).toBe("bytes")
|
||||
expect(head.headers.get("content-length")).toBe("10")
|
||||
expect(head.headers.get("content-type")).toBe("video/mp4")
|
||||
expect(head.headers.get("content-range")).toBeNull()
|
||||
expect(head.headers.get("etag")).toBe(etag)
|
||||
expect(head.headers.get("last-modified")).toBe(lastModified)
|
||||
expect(yield* Effect.promise(() => head.text())).toBe("")
|
||||
|
||||
// 3. Explicit, open-ended, clamped, and suffix Range forms -> 206 Partial Content
|
||||
yield* Effect.forEach(
|
||||
[
|
||||
["bytes=0-0", "0", "bytes 0-0/10"],
|
||||
["bytes=2-5", "2345", "bytes 2-5/10"],
|
||||
["bytes=7-99", "789", "bytes 7-9/10"],
|
||||
["bytes=6-", "6789", "bytes 6-9/10"],
|
||||
["bytes=-3", "789", "bytes 7-9/10"],
|
||||
["bytes=-99", "0123456789", "bytes 0-9/10"],
|
||||
] as const,
|
||||
([range, expectedBody, expectedContentRange]) =>
|
||||
Effect.gen(function* () {
|
||||
const partial = yield* request("clip.mp4", { headers: { Range: range } })
|
||||
expect({ range, status: partial.status }).toEqual({ range, status: 206 })
|
||||
expect(partial.headers.get("accept-ranges")).toBe("bytes")
|
||||
expect(partial.headers.get("content-range")).toBe(expectedContentRange)
|
||||
expect(partial.headers.get("content-length")).toBe(String(expectedBody.length))
|
||||
expect(partial.headers.get("content-type")).toBe("video/mp4")
|
||||
expect(partial.headers.get("cache-control")).toBe("no-cache, no-transform")
|
||||
expect(partial.headers.get("etag")).toBe(etag)
|
||||
expect(partial.headers.get("last-modified")).toBe(lastModified)
|
||||
expect(yield* Effect.promise(() => partial.text())).toBe(expectedBody)
|
||||
}),
|
||||
)
|
||||
|
||||
// 4. Unsatisfiable Range -> 416 with Content-Range: bytes */size
|
||||
yield* Effect.forEach(
|
||||
[
|
||||
["clip.mp4", "bytes=10-12", 10],
|
||||
["clip.mp4", "bytes=10-", 10],
|
||||
["clip.mp4", "bytes=5-2", 10],
|
||||
["clip.mp4", "bytes=-0", 10],
|
||||
["empty.bin", "bytes=0-0", 0],
|
||||
] as const,
|
||||
([file, range, total]) =>
|
||||
Effect.gen(function* () {
|
||||
const unsatisfiable = yield* request(file, { headers: { Range: range } })
|
||||
expect({ file, range, status: unsatisfiable.status }).toEqual({ file, range, status: 416 })
|
||||
expect(unsatisfiable.headers.get("content-range")).toBe(`bytes */${total}`)
|
||||
expect(yield* Effect.promise(() => unsatisfiable.text())).toBe("")
|
||||
}),
|
||||
)
|
||||
|
||||
// 5. Multi-range and malformed Range headers fall back to full 200
|
||||
yield* Effect.forEach(
|
||||
["bytes=0-1, 4-5", "items=0-3", "bytes=", "bytes=-", "bytes=abc-def", "bytes=0-1-2"] as const,
|
||||
(range) =>
|
||||
Effect.gen(function* () {
|
||||
const fallback = yield* request("clip.mp4", { headers: { Range: range } })
|
||||
expect({ range, status: fallback.status }).toEqual({ range, status: 200 })
|
||||
expect(fallback.headers.get("content-range")).toBeNull()
|
||||
expect(fallback.headers.get("content-length")).toBe("10")
|
||||
expect(yield* Effect.promise(() => fallback.text())).toBe("0123456789")
|
||||
}),
|
||||
)
|
||||
|
||||
// 6. Conditional requests: If-None-Match & If-Modified-Since -> 304 Not Modified
|
||||
if (!etag || !lastModified) return yield* Effect.die(new Error("Missing ETag or Last-Modified"))
|
||||
const strongEtag = etag.slice(2)
|
||||
yield* Effect.forEach(
|
||||
[
|
||||
["If-None-Match", etag],
|
||||
["If-None-Match", strongEtag],
|
||||
["If-None-Match", `"other", ${etag}`],
|
||||
["If-None-Match", "*"],
|
||||
["If-Modified-Since", lastModified],
|
||||
] as const,
|
||||
([header, value]) =>
|
||||
Effect.gen(function* () {
|
||||
const notModified = yield* request("clip.mp4", { headers: { [header]: value } })
|
||||
expect(notModified.status).toBe(304)
|
||||
expect(notModified.headers.get("etag")).toBe(etag)
|
||||
expect(notModified.headers.get("last-modified")).toBe(lastModified)
|
||||
expect(notModified.headers.get("cache-control")).toBe("no-cache, no-transform")
|
||||
expect(yield* Effect.promise(() => notModified.text())).toBe("")
|
||||
}),
|
||||
)
|
||||
|
||||
const modifiedSincePast = yield* request("clip.mp4", {
|
||||
headers: { "If-Modified-Since": "Wed, 01 Jan 2020 00:00:00 GMT" },
|
||||
})
|
||||
expect(modifiedSincePast.status).toBe(200)
|
||||
expect(yield* Effect.promise(() => modifiedSincePast.text())).toBe("0123456789")
|
||||
|
||||
// Mismatched If-None-Match takes precedence over matching If-Modified-Since
|
||||
const mismatchIfNoneMatch = yield* request("clip.mp4", {
|
||||
headers: { "If-None-Match": 'W/"mismatch"', "If-Modified-Since": lastModified },
|
||||
})
|
||||
expect(mismatchIfNoneMatch.status).toBe(200)
|
||||
expect(yield* Effect.promise(() => mismatchIfNoneMatch.text())).toBe("0123456789")
|
||||
|
||||
// 7. If-Range matching -> 206; If-Range mismatch -> full 200
|
||||
yield* Effect.forEach([etag, strongEtag, lastModified], (ifRange) =>
|
||||
Effect.gen(function* () {
|
||||
const matchedRange = yield* request("clip.mp4", {
|
||||
headers: { Range: "bytes=1-4", "If-Range": ifRange },
|
||||
})
|
||||
expect(matchedRange.status).toBe(206)
|
||||
expect(matchedRange.headers.get("content-range")).toBe("bytes 1-4/10")
|
||||
expect(yield* Effect.promise(() => matchedRange.text())).toBe("1234")
|
||||
}),
|
||||
)
|
||||
|
||||
yield* Effect.forEach(['W/"stale"', '"stale"', "Wed, 01 Jan 2020 00:00:00 GMT", "not-a-date"], (ifRange) =>
|
||||
Effect.gen(function* () {
|
||||
const mismatchedRange = yield* request("clip.mp4", {
|
||||
headers: { Range: "bytes=1-4", "If-Range": ifRange },
|
||||
})
|
||||
expect(mismatchedRange.status).toBe(200)
|
||||
expect(mismatchedRange.headers.get("content-range")).toBeNull()
|
||||
expect(mismatchedRange.headers.get("content-length")).toBe("10")
|
||||
expect(yield* Effect.promise(() => mismatchedRange.text())).toBe("0123456789")
|
||||
}),
|
||||
)
|
||||
|
||||
// 8. Confinement enforced (404 FileNotFoundError instead of 500)
|
||||
yield* Effect.forEach(["..%2Foutside%2Fsecret.txt", "escape-link/secret.txt", "nested"], (forbiddenPath) =>
|
||||
Effect.gen(function* () {
|
||||
const escaped = yield* request(forbiddenPath)
|
||||
expect({ forbiddenPath, status: escaped.status }).toEqual({ forbiddenPath, status: 404 })
|
||||
expect(yield* Effect.promise(() => escaped.json())).toMatchObject({
|
||||
_tag: "FileNotFoundError",
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
// 9. Compressible files keep their exact Content-Length instead of being gzipped
|
||||
const text = yield* request("notes.txt", { headers: { "Accept-Encoding": "gzip" }, decompress: false })
|
||||
expect(text.status).toBe(200)
|
||||
expect(text.headers.get("content-encoding")).toBeNull()
|
||||
expect(text.headers.get("content-length")).toBe(String("compressible ".length * 1024))
|
||||
expect(yield* Effect.promise(() => text.text())).toBe("compressible ".repeat(1024))
|
||||
|
||||
// 10. Multi-megabyte file streams byte-for-byte on full read and range slice
|
||||
const fullLarge = yield* request("large.mp4")
|
||||
expect(fullLarge.status).toBe(200)
|
||||
expect(fullLarge.headers.get("content-length")).toBe(String(largeBytes.length))
|
||||
const fullLargeReceived = new Uint8Array(yield* Effect.promise(() => fullLarge.arrayBuffer()))
|
||||
expect(fullLargeReceived.length).toBe(largeBytes.length)
|
||||
expect(Buffer.compare(fullLargeReceived, largeBytes)).toBe(0)
|
||||
|
||||
const sliceStart = 1024 * 1024 + 17
|
||||
const sliceEnd = 2 * 1024 * 1024 + 519
|
||||
const partialLarge = yield* request("large.mp4", {
|
||||
headers: { Range: `bytes=${sliceStart}-${sliceEnd}` },
|
||||
})
|
||||
expect(partialLarge.status).toBe(206)
|
||||
expect(partialLarge.headers.get("content-range")).toBe(`bytes ${sliceStart}-${sliceEnd}/${largeBytes.length}`)
|
||||
expect(partialLarge.headers.get("content-length")).toBe(String(sliceEnd - sliceStart + 1))
|
||||
const partialLargeReceived = new Uint8Array(yield* Effect.promise(() => partialLarge.arrayBuffer()))
|
||||
expect(Buffer.compare(partialLargeReceived, largeBytes.subarray(sliceStart, sliceEnd + 1))).toBe(0)
|
||||
}),
|
||||
15_000,
|
||||
)
|
||||
|
||||
it.live("opens no file for HEAD and closes it when a download is aborted", () =>
|
||||
Effect.gen(function* () {
|
||||
if (process.platform !== "linux" && process.platform !== "darwin") return
|
||||
const tmp = yield* tmpdirScoped()
|
||||
const file = path.join(tmp.path, "large.bin")
|
||||
yield* Effect.promise(() => fs.writeFile(file, new Uint8Array(32 * 1024 * 1024)))
|
||||
const real = yield* Effect.promise(() => fs.realpath(file))
|
||||
const server = yield* startServer(path.join(tmp.path, "config"))
|
||||
const url = new URL("/api/fs/read/large.bin", server.base)
|
||||
url.searchParams.set("location[directory]", tmp.path)
|
||||
const openHandles = Effect.promise(async () => {
|
||||
if (process.platform === "darwin")
|
||||
return Bun.spawnSync(["lsof", "-p", String(process.pid), "-Fn"])
|
||||
.stdout.toString()
|
||||
.split("\n")
|
||||
.filter((line) => line === `n${real}`).length
|
||||
const fds = await fs.readdir("/proc/self/fd")
|
||||
const targets = await Promise.all(fds.map((fd) => fs.readlink(`/proc/self/fd/${fd}`).catch(() => "")))
|
||||
return targets.filter((target) => target === real).length
|
||||
})
|
||||
|
||||
const head = yield* Effect.promise(() => fetch(url, { method: "HEAD", headers: server.headers }))
|
||||
expect(head.status).toBe(200)
|
||||
expect(head.headers.get("content-length")).toBe(String(32 * 1024 * 1024))
|
||||
expect(yield* openHandles).toBe(0)
|
||||
|
||||
const controller = new AbortController()
|
||||
const download = yield* Effect.promise(() => fetch(url, { headers: server.headers, signal: controller.signal }))
|
||||
const reader = download.body?.getReader()
|
||||
yield* Effect.promise(async () => reader?.read())
|
||||
expect(yield* openHandles).toBe(1)
|
||||
controller.abort()
|
||||
expect(
|
||||
yield* openHandles.pipe(
|
||||
Effect.repeat({ until: (count) => count === 0, schedule: Schedule.spaced("25 millis") }),
|
||||
Effect.timeout("5 seconds"),
|
||||
),
|
||||
).toBe(0)
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -8481,7 +8481,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Serve one file relative to the requested location.",
|
||||
"description": "Stream one file relative to the requested location with ETag, Last-Modified, and single-range HTTP Range support (206 Partial Content, 416 Range Not Satisfiable, and 304 Not Modified; multi-range and malformed Range headers fall back to 200).",
|
||||
"summary": "Read file"
|
||||
}
|
||||
},
|
||||
|
||||
@@ -8481,7 +8481,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Serve one file relative to the requested location.",
|
||||
"description": "Stream one file relative to the requested location with ETag, Last-Modified, and single-range HTTP Range support (206 Partial Content, 416 Range Not Satisfiable, and 304 Not Modified; multi-range and malformed Range headers fall back to 200).",
|
||||
"summary": "Read file"
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user