镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Feature: Immutable Buffer buffer.readonly() #27080

Description

@mikeal

Is your feature request related to a problem? Please describe.

I have an object with a buffer attached and I need to be able to pass around that Buffer but ensure that it won’t mutate.

Describe the solution you'd like

A method on Buffer instances to put it into a “read only mode” would be ideal. I’d prefer it not be in the constructor so that I don’t have to perform a memcopy in order to get it.

Describe alternatives you've considered

There isn’t much you can do except force a full copy every time you pass it around, which is pretty bad. Object.freeze() won’t work because all the mutations happen through methods that are effectively invisible to Object.freeze().

However, Object.freeze() has some negative performance implications while implementing this in the Buffer object itself would not have the same problems. This would be a nice features of Node.js Core.

Activity

  1. changed the title [-]Immutable Buffer `buffer.readonly()`[/-] [+]Feature: Immutable Buffer `buffer.readonly()`[/+] on Apr 3, 2019
  2. addaleax commented on Apr 3, 2019

    @addaleax
    Member

    I think this would have to be a language feature, because the only difference between Buffer and Uint8Array is that the former has a few more prototype methods.

  3. added
    bufferIssues and PRs related to the buffer subsystem.
    feature requestIssues requesting new Node.js features.
    on Apr 3, 2019
  4. addaleax commented on Apr 3, 2019

    @addaleax
    Member

    /cc @nodejs/open-standards (I guess?)

  5. jasnell commented on Apr 4, 2019

    @jasnell
    Member

    This would definitely be interesting but I agree we might want to push to tc39 first

  6. benjamingr commented on Apr 4, 2019

    @benjamingr
    Member

    You can also define a proxy over the buffer though that would probably be expensive since it'd be hit on every access. I wonder if a proxy with only a set trap (and not a get trap) would be slow or not.

  7. devsnek commented on Apr 4, 2019

    @devsnek
    Member

    you'd also want to intercept .buffer I assume

  8. addaleax commented on Apr 4, 2019

    @addaleax
    Member

    Yeah, that’s a good point to figure out when bringing this to TC39 – would we want read-only ArrayBufferViews, or read-only ArrayBuffers, or both?

  9. tniessen commented on Apr 4, 2019

    @tniessen
    Member

    I think read-only ArrayBuffers would be a useful feature to represent memory that should not or cannot be written to. However, if we still need to write to the memory through a different object, we might need two ArrayBuffers, which would technically act like a View I guess?

  10. ljharb commented on Apr 4, 2019

    @ljharb
    SponsorMember

    Borrowing ArrayBuffer.prototype methods and .calling them on a Proxy would throw, though, wouldn’t it? (since internal slots don’t tunnel)

  11. littledan commented on Apr 6, 2019

    @littledan

    This is an interesting problem, and I agree that it would probably be best solved at the engine level and across Javascript as a whole.

    Is the idea for the API that you would start with a mutable buffer and at some point freeze it?

    Do you want a read-only view on something which may still change out from under you? If so, a Proxy-based solution may work, if the performance can be good enough. Otherwise, we need to freeze the underlying ArrayBuffer somehow.

    For context, would it be reasonable to copy the Buffer into an immutable structure, or would that be too slow for your needs?

  12. jasnell commented on Apr 6, 2019

    @jasnell
    Member

    Copying would certainly be too slow, at least for the cases where I think this would be useful. A read only view on an otherwise mutable structure would work for most cases I could imagine but @mikeal may have other requirements. The most common case I would imagine for this is some bit of code that creates/mutates the buffer then passes it off to some other context, after which it would no longer be modified.

  13. tniessen commented on Apr 6, 2019

    @tniessen
    Member

    I agree with @jasnell. Would it be possible to allow freezing both buffers and views? For example,

    • ArrayBufferView.freeze() marks the view as "frozen", preventing all future write accesses through the view.
    • ArrayBuffer.freeze() marks the buffer as "frozen", preventing all future write accesses to the buffer, including write accesses through views.

    Would this work and cover all use cases? It would certainly cause some overhead, but still much less than copying the data into a new buffer.

  14. mikeal commented on Apr 6, 2019

    @mikeal
    ContributorAuthor
  15. 30 remaining items

  16. moved this to Pending Triage in Node.js feature requestson Mar 19, 2022
  17. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Mar 22, 2022
  18. github-actions commented on Sep 19, 2022

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  19. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Sep 19, 2022
  20. cirospaciari commented on Oct 6, 2022

    @cirospaciari

    I certainly will use this feature a lot, when reading from cached buffers, I likely use subarray for getting an view and will be very useful if this subarray are only read only to not modify the cached buffer

  21. added
    never-staleIssues and PRs exempt from automated stale handling.
    and removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Oct 6, 2022
  22. moved this from Stale to Todo in Node.js feature requestson Oct 6, 2022
  23. github-actions commented on Apr 5, 2023

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  24. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 5, 2023
  25. bnoordhuis commented on Apr 5, 2023

    @bnoordhuis
    Member

    To recap: this needs someone to champion a proposal at TC39.

    I'm going to close the issue because, while the discussion is interesting, it's inactionable for Node.js.

  26. phoddie commented on Apr 5, 2023

    @phoddie

    Just FYI – there is a proposal at TC39 for read-only collections which provides for immutable ArrayBuffers. The champions are @erights and me. It hasn't been very active, so it may still be appropriate to close this here.

  27. moved this from Todo to Not planned in Node.js feature requestson Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.feature requestIssues requesting new Node.js features.never-staleIssues and PRs exempt from automated stale handling.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions