Skip to content

Commit

Permalink
Merge pull request #922 from routineco/master
Browse files Browse the repository at this point in the history
Do not systematically create a XMLHttpRequest object.
  • Loading branch information
mseri authored Aug 4, 2022
2 parents 7d8a1b4 + 8267de2 commit 03cf408
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
+ cleanup: remove cohttp-{curl,server,proxy}-lwt (rgrinberg #904)
+ fix: all parsers now follow the spec and require `\r\n` endings.
Previously, the `\r` was optional. (rgrinberg, #921)
- `cohttp-lwt-jsoo`: do not instantiate `XMLHttpRequest` object on boot (mefyl #922)

## v5.0.0 (2021-12-15)

Expand Down
15 changes: 9 additions & 6 deletions cohttp-lwt-jsoo/src/cohttp_lwt_jsoo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ end

let xhr_response_supported =
(* from http://stackoverflow.com/questions/8926505/how-to-feature-detect-if-xmlhttprequest-supports-responsetype-arraybuffer *)
let xhr = XmlHttpRequest.create () in
let rt = xhr##.responseType in
Js.to_string (Js.typeof rt) = "string"
lazy
(let xhr = XmlHttpRequest.create () in
let rt = xhr##.responseType in
Js.to_string (Js.typeof rt) = "string")

let binary_string str =
let len = String.length str in
Expand Down Expand Up @@ -111,7 +112,7 @@ module Body_builder (P : Params) = struct
(fun () -> `String (Js.string ""))
(fun s -> `String s)
in
match xhr_response_supported with
match Lazy.force xhr_response_supported with
| true when Js.Opt.return xml##.response == Js.null ->
Log.warn (fun m -> m "XHR Response is null; using empty string");
`String (Js.string "")
Expand Down Expand Up @@ -195,7 +196,8 @@ module Make_client_async (P : Params) = Make_api (struct
let call ?headers ?body meth uri =
let xml = XmlHttpRequest.create () in
xml##.withCredentials := Js.bool P.with_credentials;
if xhr_response_supported then xml##.responseType := Js.string "arraybuffer";
if Lazy.force xhr_response_supported then
xml##.responseType := Js.string "arraybuffer";
let (res : (Http.Response.t Lwt.t * CLB.t) Lwt.t), wake = Lwt.task () in
let () =
xml
Expand Down Expand Up @@ -283,7 +285,8 @@ module Make_client_sync (P : Params) = Make_api (struct
let call ?headers ?body meth uri =
let xml = XmlHttpRequest.create () in
xml##.withCredentials := Js.bool P.with_credentials;
if xhr_response_supported then xml##.responseType := Js.string "arraybuffer";
if Lazy.force xhr_response_supported then
xml##.responseType := Js.string "arraybuffer";
let () =
xml
## (_open
Expand Down

0 comments on commit 03cf408

Please sign in to comment.