| 68 | }) |
| 69 | }, |
| 70 | fileWebResponse(file, status, statusText, headers, options) { |
| 71 | const offset = Number(options?.offset ?? 0) |
| 72 | const available = Math.max(0, file.size - offset) |
| 73 | const contentLength = options?.bytesToRead === undefined |
| 74 | ? available |
| 75 | : Math.min(available, Math.max(0, Number(options.bytesToRead))) |
| 76 | let body: typeof file | ReadableStream<Uint8Array> = file |
| 77 | if (contentLength === 0) { |
| 78 | body = new ReadableStream<Uint8Array>({ |
| 79 | start(controller) { |
| 80 | controller.close() |
| 81 | } |
| 82 | }) |
| 83 | } else if (offset > 0 || options?.bytesToRead !== undefined) { |
| 84 | body = (file.stream() as ReadableStream<Uint8Array>).pipeThrough( |
| 85 | new ByteSliceStream(offset, offset + contentLength - 1) |
| 86 | ) |
| 87 | } |
| 88 | return Response.raw(body, { |
| 89 | headers: { |
| 90 | ...headers, |
| 91 | "content-type": file.type, |
| 92 | "content-length": contentLength.toString() |
| 93 | }, |
| 94 | status, |
| 95 | statusText |
| 96 | }) |
| 97 | } |
| 98 | }) |
| 99 | |
| 100 | /** |