Skip to content

Commit

Permalink
Merge pull request #18 from armanbilge/0.1.0
Browse files Browse the repository at this point in the history
Prep 0.1.0
  • Loading branch information
armanbilge authored Oct 6, 2021
2 parents 8e2cff3 + 181560d commit 94cec51
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 102 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ name: Continuous Integration

on:
pull_request:
branches: ['**']
branches: [series/0.1]
push:
branches: ['**']
branches: [series/0.1]
tags: [v*]

env:
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
- run: sbt ++${{ matrix.scala }} ci

- name: Compress target directories
run: tar cf targets.tar target tests/target fetch-client/target service-worker/target core/target project/target
run: tar cf targets.tar target dom/target tests/target project/target

- name: Upload target directories
uses: actions/upload-artifact@v2
Expand All @@ -69,7 +69,7 @@ jobs:
publish:
name: Publish Artifacts
needs: [build]
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v'))
strategy:
matrix:
os: [ubuntu-latest]
Expand Down
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# http4s-dom

Use http4s in your browser with Scala.js!
Features:

### Modules
* A `Client` implementation backed by [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
* A [`Service Worker`](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) integration to install your `HttpRoutes` as a [`FetchEvent` handler](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/onfetch)
* Encoders for [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File), [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)

* `core`: encoders for [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) and other [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)s
* `fetch-client`: a `Client` implementation backed by [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
* `service-worker`: install your `HttpRoutes` as a [`FetchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent) listener

### Usage

```sbt
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-dom-core" % "1.0.0-M27",
"org.http4s" %%% "http4s-dom-fetch-client" % "1.0.0-M27",
"org.http4s" %%% "http4s-dom-service-worker" % "1.0.0-M27",
)
libraryDependencies += "org.http4s" %%% "http4s-dom" % "0.1.0"
```
52 changes: 12 additions & 40 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,23 @@ import JSEnv._

name := "http4s-dom"

ThisBuild / baseVersion := "1.0"
ThisBuild / baseVersion := "0.1"

ThisBuild / organization := "org.http4s"
ThisBuild / organizationName := "http4s.org"
ThisBuild / publishGithubUser := "armanbilge"
ThisBuild / publishFullName := "Arman Bilge"

enablePlugins(SonatypeCiReleasePlugin)
ThisBuild / spiewakCiReleaseSnapshots := true
ThisBuild / spiewakMainBranches := Seq("main")
ThisBuild / githubWorkflowTargetBranches := Seq("series/0.1")

ThisBuild / homepage := Some(url("https://github.com/http4s/http4s-dom"))
ThisBuild / scmInfo := Some(
ScmInfo(
url("https://github.com/http4s/http4s-dom"),
"https://github.com/http4s/http4s-dom.git"))

ThisBuild / crossScalaVersions := Seq( /*"3.0.2",*/ "2.12.15", "2.13.6")
ThisBuild / crossScalaVersions := Seq("2.12.15", "2.13.6")

replaceCommandAlias("ci", CI.AllCIs.map(_.toString).mkString)
addCommandAlias("ciFirefox", CI.Firefox.toString)
Expand Down Expand Up @@ -108,55 +107,28 @@ ThisBuild / Test / jsEnv := {

val catsEffectVersion = "3.2.9"
val fs2Version = "3.1.4"
val http4sVersion = "1.0.0-M27"
val http4sVersion = "0.23.5"
val scalaJSDomVersion = "1.2.0"
val munitVersion = "0.7.29"
val munitCEVersion = "1.0.6"

lazy val root =
project
.in(file("."))
.aggregate(core, fetchClient, serviceWorker, tests)
.enablePlugins(NoPublishPlugin)
project.in(file(".")).aggregate(dom, tests).enablePlugins(NoPublishPlugin)

lazy val core = project
.in(file("core"))
lazy val dom = project
.in(file("dom"))
.settings(
name := "http4s-dom-core",
description := "Base library for dom http4s client and apps",
name := "http4s-dom",
description := "http4s browser integrations",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect-kernel" % catsEffectVersion,
"org.typelevel" %%% "cats-effect" % catsEffectVersion,
"co.fs2" %%% "fs2-core" % fs2Version,
"org.http4s" %%% "http4s-core" % http4sVersion,
"org.http4s" %%% "http4s-client" % http4sVersion,
"org.scala-js" %%% "scalajs-dom" % scalaJSDomVersion
)
)
.enablePlugins(ScalaJSPlugin)

lazy val fetchClient = project
.in(file("fetch-client"))
.settings(
name := "http4s-dom-fetch-client",
description := "browser fetch implementation for http4s clients",
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-client" % http4sVersion
)
)
.dependsOn(core)
.enablePlugins(ScalaJSPlugin)

lazy val serviceWorker = project
.in(file("service-worker"))
.settings(
name := "http4s-dom-service-worker",
description := "browser service worker implementation for http4s apps",
libraryDependencies ++= Seq(
"org.typelevel" %%% "cats-effect" % catsEffectVersion
)
)
.dependsOn(core)
.enablePlugins(ScalaJSPlugin)

lazy val tests = project
.in(file("tests"))
.settings(
Expand All @@ -169,5 +141,5 @@ lazy val tests = project
"org.typelevel" %%% "munit-cats-effect-3" % munitCEVersion % Test
)
)
.dependsOn(serviceWorker, fetchClient % Test)
.dependsOn(dom)
.enablePlugins(ScalaJSPlugin, BuildInfoPlugin, NoPublishPlugin)
43 changes: 0 additions & 43 deletions core/src/main/scala/org/http4s/dom/DomEntityEncoder.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private[dom] object FetchClient {
case (r, exitCase) =>
closeReadableStream(r.body, exitCase)
}
.evalMap(fromResponse[F])
.evalMap(fromDomResponse[F])

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object FetchEventContext {
event.clientId,
event.resultingClientId,
OptionT(F.fromPromise(F.pure(event.preloadResponse)).map(_.toOption))
.semiflatMap(fromResponse[F])
.semiflatMap(fromDomResponse[F])
.value,
supervisor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,36 @@ import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import cats.syntax.all._
import fs2.Stream
import org.scalajs.dom.Blob
import org.scalajs.dom.File
import org.scalajs.dom.experimental.ReadableStream
import org.scalajs.dom.experimental.{Headers => DomHeaders}
import org.scalajs.dom.experimental.{Response => DomResponse}

import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.typedarray.Uint8Array

package object dom {

private[dom] def fromResponse[F[_]](response: DomResponse)(
implicit def fileEncoder[F[_]](implicit F: Async[F]): EntityEncoder[F, File] =
blobEncoder.narrow

implicit def blobEncoder[F[_]](implicit F: Async[F]): EntityEncoder[F, Blob] =
EntityEncoder.entityBodyEncoder.contramap { blob =>
Stream
.bracketCase {
// lol, the facade is still broken. next time
F.delay(blob.stream().asInstanceOf[ReadableStream[Uint8Array]])
} { case (rs, exitCase) => closeReadableStream(rs, exitCase) }
.flatMap(fromReadableStream[F])
}

implicit def readableStreamEncoder[F[_]: Async]
: EntityEncoder[F, ReadableStream[Uint8Array]] =
EntityEncoder.entityBodyEncoder.contramap { rs => fromReadableStream(rs) }

private[dom] def fromDomResponse[F[_]](response: DomResponse)(
implicit F: Async[F]): F[Response[F]] =
F.fromEither(Status.fromInt(response.status)).map { status =>
Response[F](
Expand Down Expand Up @@ -77,13 +97,13 @@ package object dom {
// This checks if the stream is locked before canceling it to avoid an error
if (!rs.locked) exitCase match {
case Resource.ExitCase.Succeeded =>
rs.cancel(scalajs.js.undefined)
rs.cancel(js.undefined)
case Resource.ExitCase.Errored(ex) =>
rs.cancel(ex.getLocalizedMessage())
case Resource.ExitCase.Canceled =>
rs.cancel(scalajs.js.undefined)
rs.cancel(js.undefined)
}
else scalajs.js.Promise.resolve[Unit](())
else js.Promise.resolve[Unit](())
}
}.void

Expand Down

0 comments on commit 94cec51

Please sign in to comment.