elm / http

Make HTTP requests in Elm
https://package.elm-lang.org/packages/elm/http/latest
BSD 3-Clause "New" or "Revised" License
155 stars 46 forks source link

SSCCE - Arraybuffer / Blob / XHR in IE #58

Closed panthershark closed 5 years ago

panthershark commented 5 years ago

Related to #56 . Here is a small repro for generating the InvalidStateError in IE (I'm testing against IE11 latest update).

This code downloads bytes from an xhr using expectBytes response. Under the covers, the xhr generates an array buffer that is passed to new Blob in Kernel code at File.Download.bytes.

Here is the code with the error:

import Browser
import Bytes exposing (Bytes)
import Bytes.Decode as Decode
import File.Download as Download
import Html exposing (..)
import Html.Events exposing (onClick)
import Http exposing (Error(..), Response(..))

type Msg
    = Download
    | DownloadComplete (Result Http.Error Bytes)

type alias Model =
    Maybe Bytes

resolve : Response Bytes -> Result Error Bytes
resolve response =
    case response of
        BadUrl_ url ->
            Err (BadUrl url)

        Timeout_ ->
            Err Timeout

        NetworkError_ ->
            Err NetworkError

        BadStatus_ metadata _ ->
            Err (BadStatus metadata.statusCode)

        GoodStatus_ _ body ->
            Ok body

downloadBytes : Cmd Msg
downloadBytes =
    Http.get
        { url = "https://media.giphy.com/media/dUQakUPAZw1EY/200w_d.gif"
        , expect = Http.expectBytesResponse DownloadComplete resolve
        }

init : {} -> ( Model, Cmd Msg )
init _ =
    ( Nothing, downloadBytes )

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Download ->
            Maybe.map (\bytes -> ( model, Download.bytes "gracias.gif" "image/gif" bytes )) model
                |> Maybe.withDefault ( model, Cmd.none )

        DownloadComplete result ->
            case result of
                Ok bytes ->
                    ( Just bytes, Cmd.none )

                Err e ->
                    ( model, Cmd.none )

view : Model -> Html Msg
view model =
    Maybe.map (always (button [ onClick Download ] [ text "download bytes" ])) model
        |> Maybe.withDefault (text "Downloading file.")

main : Program {} Model Msg
main =
    Browser.element
        { init = init
        , update = update
        , view = view
        , subscriptions = always Sub.none
        }

Error screenshot.

screen shot 2019-02-12 at 9 30 17 am

evancz commented 5 years ago

@boianr mentioned https://github.com/bpampuch/pdfmake/issues/294 which appears to be the same problem. They solved it like this: https://github.com/bpampuch/pdfmake/pull/297/files

If you can try adding something like that on your side, let me know how it goes for you!

evancz commented 5 years ago

Got it down to something smaller in https://github.com/elm/file/issues/10