Repository navigation
fix(r2): read Object::range() as a plain JS object - #1097
Open
PedroRosalba wants to merge 1 commit into
Open
PedroRosalba wants to merge 1 commit into
PedroRosalba wants to merge 1 commit into
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
R2Object.rangeis a plain{ offset, length }object (orundefinedwhen 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 everyObject::range()call panics withexpected instance of R2Range, in both--devand--releasebuilds.Fix
Declare the getter as
JsValue, readoffset/length/suffixwithReflect::getinto the existingR2Range->Rangeconversion, 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(seeas_organization,asn). I keptResult<Range>to avoid a signature change in a bug fix; happy to switch toResult<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.