Skip to content

Commit

Permalink
refactor(middleware): fix incompatibilities across Response
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Jan 5, 2025
1 parent ea1e6b8 commit 24f687a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/middleware/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-env browser */
/* global FixedLengthStream */
// @ts-expect-error no types
import httpRangeParse from 'http-range-parse'

Expand All @@ -23,11 +21,13 @@ export function parseRange (value) {
* @param {import("@cloudflare/workers-types").R2ObjectBody} obj
* @param {import("@cloudflare/workers-types").R2Range | undefined} range
* @param {Headers} [headers]
* @param { (arg0: ReadableStream, arg1: number) => ReadableStream} [transform]
* @returns
*/
export const toResponse = (obj, range, headers) => {
export const toResponse = (obj, range, headers, transform) => {
const status = range ? 206 : 200
headers = headers || new Headers({})
transform = transform || ((rs, _) => rs)
let contentLength = obj.size
if (range) {
let first, last
Expand All @@ -44,5 +44,5 @@ export const toResponse = (obj, range, headers) => {
headers.set('Content-Length', contentLength.toString())

// @ts-expect-error ReadableStream types incompatible
return new Response(obj.body.pipeThrough(new FixedLengthStream(contentLength)), { status, headers })
return new Response(transform(obj.body, contentLength), { status, headers })
}
5 changes: 4 additions & 1 deletion src/middleware/withCarBlockHandler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-env browser */
/* global FixedLengthStream */

import { CAR_CODE } from '../constants.js'
import { HttpError } from '@web3-storage/gateway-lib/util'
import { base58btc } from 'multiformats/bases/base58'
Expand Down Expand Up @@ -108,7 +111,7 @@ export async function handleCarBlock (request, env, ctx) {
'Cache-Control': 'public, max-age=29030400, immutable',
'Content-Disposition': `attachment; filename="${dataCid}.car"`,
Etag: etag
}))
}), (body, length) => body.pipeThrough(new FixedLengthStream(length)))
}

/** @param {import('multiformats').UnknownLink} cid */
Expand Down
4 changes: 1 addition & 3 deletions src/middleware/withCarParkFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ export function withCarParkFetch (handler) {
const rangeHeader = (new Headers(headers)).get('Range')

// extract range if present from range header
/** @type {import('@cloudflare/workers-types').R2GetOptions} */

/** @type {import('@cloudflare/workers-types').R2Range|undefined} */
let range
if (rangeHeader) {
try {
range = parseRange(request.headers.get('range') ?? '')
range = parseRange(rangeHeader)
} catch (err) {
return globalThis.fetch(input, init)
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"skipLibCheck": true,
"resolveJsonModule": true,
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"types": ["@cloudflare/workers-types"],
"target": "ES2022",
"sourceMap": true
}
Expand Down

0 comments on commit 24f687a

Please sign in to comment.