Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement URL.createObjectURL and URL.revokeObjectURL methods #187

Merged
merged 6 commits into from
Jan 16, 2025

Conversation

andreiltd
Copy link
Contributor

@andreiltd andreiltd commented Dec 10, 2024

This adds URL.createObjectURL and URL.revokeObjectURL static methods to URL as well as support for blob-scheme fetch:

Some noteworthy changes include:

  • move uuid generation function to a separate file so that it can be used outside crypto component,
  • split main-fetch function into two fetch-https and fetch-blob functions called based on url scheme,
  • implement Range content fetch for blobs,
  • add integration tests for content range fetch.

Closes: #182

@andreiltd andreiltd marked this pull request as draft December 10, 2024 17:45
@andreiltd
Copy link
Contributor Author

andreiltd commented Dec 10, 2024

Hey @guybedford I was hoping I could get some early feedback about the initial commit for this work: 8d7d5c6. Specifically:

  • Is it OK to get UUID implementation from Rust crate, similar to what we do with Url crate?
  • the spec mentions getting origin for URL from settings object, is this something we support? Here.
  • would you know where in the request-response the support for blob: schemes should be connected?

@guybedford
Copy link
Contributor

Will ask @tschneidereit if he has any ideas about the UUID implementation - is there a small implementation we could inline into the codebase that avoids unnecessary code and might save some bytes? In general we want to watch binary size very carefully.

As for the request implementation it would be supporting the Blob URL everywhere we normally support a URL - so specifically fetch(blobUrl).

The worker location is the base origin to use.

@andreiltd
Copy link
Contributor Author

andreiltd commented Dec 10, 2024

Just for context, the size difference between the binary from main and this branch is 26KB. I'm not sure how much of this is due to the uuid crate.

@guybedford
Copy link
Contributor

Actually I think you could just use our existing implementation of Crypto::random_uuid here and then there's no need for a third-party crate.

@andreiltd
Copy link
Contributor Author

Actually I think you could just use our existing implementation of Crypto::random_uuid here and then there's no need for a third-party crate.

Nice, I totally missed that :)

@andreiltd andreiltd force-pushed the create-object-url branch 6 times, most recently from 88d10f2 to e43472b Compare December 19, 2024 12:12
"status": "PASS"
},
"Fetching URL.createObjectURL(invalid_type_blob) is OK": {
"status": "FAIL"
Copy link
Contributor Author

@andreiltd andreiltd Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test checks that blob created with type: "invalid" should result in empty Content-Type header. It assumes that the blob type should be mime parsable although this assumption is not consistent with other blob tests and the spec.

As stated here: https://w3c.github.io/FileAPI/#constructorBlob

If the type member of the options argument is not the empty string, run the following sub-steps:

  • Let t be the type dictionary member. If t contains any characters outside the range U+0020 to U+007E, then set t to the empty string and return from these substeps.
  • Convert every character in t to ASCII lowercase.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested this in Chrome and what you're saying seems to be correct in that:

const b = new Blob([new Uint8Array([1,2,3])], { type: 'blah' });
const res = await fetch(URL.createObjectURL(b));
console.log(res.headers.get('content-type'));

Gives a result of blah exactly.

If that aligns with your read on this, then agreed on the implementation.

@andreiltd
Copy link
Contributor Author

I believe this is done, although it's still depends on #181 being merged first.

@andreiltd andreiltd marked this pull request as ready for review January 10, 2025 17:53
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of points. Having some more refactoring of shared functions into blob could be useful without introducing unnecessary dependence. Otherwise would be great to land soon.

cmake/compile-flags.cmake Show resolved Hide resolved
"status": "PASS"
},
"Fetching URL.createObjectURL(invalid_type_blob) is OK": {
"status": "FAIL"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested this in Chrome and what you're saying seems to be correct in that:

const b = new Blob([new Uint8Array([1,2,3])], { type: 'blah' });
const res = await fetch(URL.createObjectURL(b));
console.log(res.headers.get('content-type'));

Gives a result of blah exactly.

If that aligns with your read on this, then agreed on the implementation.


// 2. If request's method is not `GET` or blobURLEntry is null, then return a network error.
if (std::memcmp(method.ptr.get(), "GET", method.len) != 0) {
return api::throw_error(cx, FetchErrors::FetchNetworkError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is an existing issue, but I believe fetch network errors are supposed to be DOMException::raise.

Copy link
Contributor Author

@andreiltd andreiltd Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried changing this calls to return DOMException::raise(cx, "The owls are not what they seem", "TypeError"); but then I'm getting bunch of test failures like this:

1:   NAME:    Fetching [PUT] URL.createObjectURL(blob) is KO
1:   MESSAGE: promise_rejects_js: function "function() { throw e }" threw object "TypeError: Invalid request method for scheme fetch" ("TypeError") expected instance of function "function TypeError() {

I'm not sure if it's possible to use DOMException the way the tests expect?

Copy link
Contributor

@guybedford guybedford Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be a deeper issue with our implementation of DOMException then. See here how throw a TypeError in the spec actually means throw a TypeError which is a DOM error - https://webidl.spec.whatwg.org/#exceptiondef-typeerror.

Sorry I misread the above, throw a TypeError definitely means throw a JS typeerror - it's only when we explicitly see DOMException in the spec that we need the dom exception.

It's just I recall that network errors are DOM Exceptions, but can't find this in the spec right now.

That all said, returning a network error looks like it should return a response? https://fetch.spec.whatwg.org/#concept-network-error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That all said, returning a network error looks like it should return a response? https://fetch.spec.whatwg.org/#concept-network-error

Indeed! It looks like there is a discrepancy between the tests and the spec. E.g. see this function: https://github.com/web-platform-tests/wpt/blob/master/fetch/api/basic/scheme-blob.sub.any.js#L22

It only expects scheme-fetch to throw a TypeError on failure. I've added a network_error function that sets the response to the object according to the spec, but throws a TypeError anyway to be compliant with the tests.

builtins/web/fetch/fetch-api.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to get this landed asap. Ping @tschneidereit for final review.

@guybedford
Copy link
Contributor

Will aim to land this one later today unless there is further feedback.

@guybedford guybedford merged commit ea54ba9 into bytecodealliance:main Jan 16, 2025
5 checks passed
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.

URL.createObjectURL support
2 participants