Skip to content

fix(r2): read Object::range() as a plain JS object - #1097

Open
PedroRosalba wants to merge 1 commit into
cloudflare:mainfrom
PedroRosalba:fix/r2-object-range
Open

PedroRosalba wants to merge 1 commit into
cloudflare:mainfrom
PedroRosalba:fix/r2-object-range

Conversation

@PedroRosalba

Copy link
Copy Markdown

Problem

R2Object.range is a plain { offset, length } object (or undefined when the read had no range), but worker-sys declares the getter as returning the Rust #[wasm_bindgen] struct R2Range. wasm-bindgen then emits _assertClass(ret, R2Range) for the import, which only accepts an instance Rust itself created, so every Object::range() call panics with expected instance of R2Range, in both --dev and --release builds.

Fix

Declare the getter as JsValue, read offset / length / suffix with Reflect::get into the existing R2Range -> Range conversion, and return an error instead of panicking when the object has no range. Public signature is unchanged (Result<Range>).

Design note: crate precedent for absent optional fields leans towards Option (see as_organization, asn). I kept Result<Range> to avoid a signature change in a bug fix; happy to switch to Result<Option<Range>> if that is preferred.

Tests

Three r2 tests: offset+length range, suffix range (echoed by the runtime as offset+length), and a read without a range. All panic on main, pass with this change; suite 148 passed / 4 skipped.

Tested on macOS (Apple Silicon) only.

`R2Object.range` is a plain `{ offset, length }` object (or `undefined`
when the read had no range), but worker-sys declared the getter as
returning the Rust `#[wasm_bindgen] struct R2Range`. wasm-bindgen then
emits `_assertClass(ret, R2Range)` for the import, which can only accept
an instance Rust itself created, so every `Object::range()` call panicked
("expected instance of R2Range") in both `--dev` and `--release` builds.

Declare the getter as `JsValue`, read `offset`/`length`/`suffix` with
`Reflect::get` into the existing `R2Range` -> `Range` conversion, and
return an error instead of panicking when the object has no range.

Adds three r2 tests: offset+length range, suffix range (echoed by the
runtime as offset+length), and a read without a range.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant