Git Product home page Git Product logo

Comments (9)

mkruisselbrink avatar mkruisselbrink commented on August 23, 2024 1

It's (unfortunately?) not true that nobody implements close(). Chrome has had an implementation behind a flag for quite a while, and according to @dmurph we regularly get people asking us when we're finally going to ship it, and IE/Edge have had an implementation under the name msClose() since IE10. Not sure how close either of those implementations are currently following the spec though.

So I don't think just getting rid of close() is the right thing to do. Instead I'll try to audit all the callsites and edge cases to figure out what sensible behavior would be (and write tests to go with that, and see where the current Chrome and Edge implementations stand).

from fileapi.

inexorabletash avatar inexorabletash commented on August 23, 2024

See also #17 and #18

from fileapi.

annevk avatar annevk commented on August 23, 2024

So as far as I can tell nobody implements close(). Should we just remove it?

from fileapi.

annevk avatar annevk commented on August 23, 2024

Note that if we decide to keep and implement it, we need to carefully audit all callsites that take a blob and decide how they should handle a closed blob.

from fileapi.

annevk avatar annevk commented on August 23, 2024

That sounds reasonable. If you can put the processing model here for review before proceeding with enabling it by default that would be good. I'd like Firefox engineering to have a chance to review it first.

from fileapi.

mkruisselbrink avatar mkruisselbrink commented on August 23, 2024

So I played around with this a bit more myself, trying to figure out what sensible behavior would be for a bunch of situations. And I wrote some tests to see what Chrome and Edge currently do. Modulo throwing different types of exceptions Edge at least passes (almost all) those tests. Chrome unfortunately fairs a bit worse, mostly due to bugs in our implementation.

But writing those tests also made me realize that no matter how we treat closed Blobs in the various APIs, it's probably never going to be entirely nice and consistent.

Naively I would expect things to behave such that any kind of cloning of a blob/passing a blob to some kind of API behaves as if a completely new blob, with a completely new copy of the data was created. As such calling close() on one blob should not effect previously clones of that blob in any way (where clone means structured clone, new Blob([oldBlob]), oldBlob.slice(), or just passing the blob to any API that accepts a blob). Similarly closing one blob should revoke all URLs made for that specific blob instance, and leave alone all blob URLs that were made for clones of that blob instance. Chrome's current implementation fails pretty badly in this regard in a number of cases, edge seems to mostly get it right.

A more tricky question is what various methods should do when a closed Blob is passed to them. For consistency it could make sense to act similarly to how neutered array buffers are treated (for cases where both blobs and array buffers are valid options). That would mean that trying to postMessage a closed Blob would fail with a DataCloneException, but for example trying to pass a closed Blob as body to an XHR.send, or as body for a Response or Request will result in a valid zero-byte body instead (why is that the case for neutered array buffers anyway?)

Or maybe a more consistent model would be that any method that takes a Blob should throw if passed a closed blob, as it almost certainly is an error to pass a closed Blob to XHR.send, new Response, etc.

Finally there is the question what to do with blobs returned by various methods. XHR.response is documented to return the same Blob every time it is called, so closing that blob would really close the blob. On the other hand what should FormData.get do? Currently that method is seemingly specified to return the same File instance every time it is called (as it just returns the internal state directly), but what should happen if you close that returned instance?

So yeah, actually figuring this all out is far from trivial. And I'm sure I missed cases in my tests. Coming back to my current attempt at tests, Edge passes all my tests except that trying to XHR.send a closed blob throws an exception (yet posting a closed blob using fetch(new Request(...)) "works" and just posts zero bytes). Chrome is kind of all over the place...

from fileapi.

mkruisselbrink avatar mkruisselbrink commented on August 23, 2024

And I wonder if Microsoft has any usage stats for their Blob.msClose method...

from fileapi.

annevk avatar annevk commented on August 23, 2024

The behavior for a detached ArrayBuffer has historical baggage and still the specification and implementations are not aligned.

If we could just throw here that would be better I think, but you'll keep issues such as adding a Blob to a FormData instance and then closing it…

from fileapi.

mkruisselbrink avatar mkruisselbrink commented on August 23, 2024

I think I've actually changed my mind about Blob.close(). The main use-case I've heard for it is to allow websites to make sure memory is freed up when the website no longer needs the blob, to then for example allow it to create more blobs. But that particular use-case wouldn't actually be supported very well by Blob.close() as specified. One issue being that at least in the chrome implementation chrome might not actually be able to free up memory until some time later. And in that case it's not much better than just relying on garbage collection. Possibly a close method that returned a promise (that would resolve when the memory has been freed) would be more useful for this, but would also get even more confusing to explain to web developers. Also the fact that various ongoing usages of the blob might or might not keep the memory alive anyway seems like it would make it very difficult to explain what closing a blob would actually do, other than "the memory might get freed at some point in the future", which garbage collecting the Blob instance itself already does...

from fileapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.