aboutsummaryrefslogtreecommitdiff
path: root/common/js/src/main/scala/http/XhrBackend.scala
diff options
context:
space:
mode:
Diffstat (limited to 'common/js/src/main/scala/http/XhrBackend.scala')
-rw-r--r--common/js/src/main/scala/http/XhrBackend.scala51
1 files changed, 0 insertions, 51 deletions
diff --git a/common/js/src/main/scala/http/XhrBackend.scala b/common/js/src/main/scala/http/XhrBackend.scala
deleted file mode 100644
index 3a791c1..0000000
--- a/common/js/src/main/scala/http/XhrBackend.scala
+++ /dev/null
@@ -1,51 +0,0 @@
-package triad
-package http
-
-import org.scalajs.dom.{ErrorEvent, Event, XMLHttpRequest}
-
-import scala.concurrent.{Future, Promise, TimeoutException}
-import scala.scalajs.js
-import scala.scalajs.js.typedarray.{ArrayBuffer, Int8Array}
-
-trait XhrBackend extends Backend {
-
- def send(request: Request): Future[Response] = {
- val promise = Promise[Response]
- val xhr = new XMLHttpRequest()
-
- xhr.open(request.method, request.url)
- xhr.responseType = "arraybuffer"
- for ((name, value) <- request.headers) {
- xhr.setRequestHeader(name, value)
- }
-
- xhr.send(js.Array(request.body: _*))
-
- xhr.onload = (e: Event) => {
- val body: Array[Byte] = if (!js.isUndefined(xhr.response)) {
- val buffer = new Int8Array(xhr.response.asInstanceOf[ArrayBuffer])
- buffer.toArray
- } else {
- Array.empty[Byte]
- }
-
- val response = Response(
- xhr.status,
- Map.empty,
- body
- )
- promise.success(response)
- }
-
- xhr.onerror = (e: ErrorEvent) => {
- promise.failure(new RuntimeException(s"XHR error: ${e.message}"))
- }
- xhr.ontimeout = (e: Event) => {
- promise.failure(
- new TimeoutException(s"Request timed out: ${xhr.statusText}"))
- }
-
- promise.future
- }
-
-}