Skip to content

Blob constructor error message when passing a string is misleading #38856

Description

@Ethan-Arrowood

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

Working with the new Blob api. Cannot instantiate using a string though I don't see why not.

new Blob('nodejs')

// -> The "sources" argument must be an instance of Iterable. Received type string ('nodejs')

Describe the solution you'd like

Maybe I'm misunderstanding, but I think strings to implement [Symbol.iterator]; does that not make it an instance of Iterable?

Describe alternatives you've considered

Plenty of work arounds - think this would make for a good addition.

Happy to contribute this feature too

Activity

  1. Ethan-Arrowood commented on May 30, 2021

    @Ethan-Arrowood
    ContributorAuthor

    Oh looks like this is intentionally not supported

    typeof sources === 'string') {

  2. Ethan-Arrowood commented on May 30, 2021

    @Ethan-Arrowood
    ContributorAuthor

    Can we add a reason why to the docs?

  3. bl-ue commented on May 30, 2021

    @bl-ue
    Contributor
  4. aduh95 commented on May 30, 2021

    @aduh95
    Contributor

    Here's my interpretation of why a string is not accepted: The specs dictates that the param must be a sequence<BlobPart>. It's not clear to me if the string is interpreted as a USVString (which doesn't seem to be a sequence in that specification), or a DOMString (which is defined as a sequence<unsigned short> and unsigned short is not a BlobPart), but either way new Blob('string') is meant to fail.

    Now I agree the error message is misleading, for reference here's the error messages thrown by some browsers:

    • Chromium: TypeError: Failed to construct 'Blob': The provided value cannot be converted to a sequence.
    • Firefox: TypeError: Blob constructor: Argument 1 can't be converted to a sequence.
    • Safari: TypeError: Value is not a sequence
  5. added
    bufferIssues and PRs related to the buffer subsystem.
    errorsIssues and PRs related to JavaScript errors originating in Node.js core.
    on May 30, 2021
  6. jasnell commented on May 31, 2021

    @jasnell
    Member

    This works just fine, however: new Blob(['hello'])

  7. jasnell commented on May 31, 2021

    @jasnell
    Member

    @Ethan-Arrowood ... just as a heads up since you're using Blob... sometime in the next two months as time allows, I will be working on Blob supporting async data sources. It won't impact the API usage at all, but figured it's worthwhile to give a heads up.

  8. Ethan-Arrowood commented on May 31, 2021

    @Ethan-Arrowood
    ContributorAuthor

    Yes I saw that, very excited for it. I'm using this in undici-fetch and it'll be a nice perf boost if blob can support the async iterator sources directly 😄

  9. changed the title [-]Support string as source for Blob[/-] [+]Blob constructor error message when passing a string is misleading[/+] on Jun 1, 2021
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.errorsIssues and PRs related to JavaScript errors originating in Node.js core.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions