Skip to content

Commit d6c4ebb

Browse files
committed
.
1 parent aad6903 commit d6c4ebb

7 files changed

Lines changed: 57 additions & 23 deletions

File tree

plugin/src/InMemoryFastLinkHashScalaJSModule.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ trait InMemoryFastLinkHashScalaJSModule extends FileBasedContentHashScalaJSModul
159159
.fileNames()
160160
.foreach {
161161
name =>
162+
Task.log.debug(s"Adding in-memory file to hashedOutputFiles: $name")
162163
val buf = inMemoryOutputDirectory.content(name).get
163164
val bytes = new Array[Byte](buf.remaining())
164165
buf.get(bytes)

routes/src/appRoute.scala

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import org.http4s.server.staticcontent.fileService
1313
import fs2.io.file.Files
1414

1515
import cats.effect.kernel.Async
16+
import cats.syntax.all.*
17+
18+
import scribe.Scribe
1619

1720
def appRoute[F[_]: Files](stringPath: String)(using f: Async[F]): HttpRoutes[F] = HttpRoutes.of[F] {
1821

@@ -33,25 +36,29 @@ def appRoute[F[_]: Files](stringPath: String)(using f: Async[F]): HttpRoutes[F]
3336

3437
}
3538

36-
def appRouteInMemory[F[_]](lookup: String => Option[Array[Byte]])(using f: Async[F]): HttpRoutes[F] =
39+
def appRouteInMemory[F[_]](lookup: String => Option[Array[Byte]])(using
40+
f: Async[F],
41+
logger: Scribe[F]
42+
): HttpRoutes[F] =
3743
def contentTypeFor(ext: String): `Content-Type` =
3844
MediaType.forExtension(ext).fold(`Content-Type`(MediaType.application.`octet-stream`))(m => `Content-Type`(m))
3945

46+
def serve(req: org.http4s.Request[F], ext: String): F[Response[F]] =
47+
val key = req.uri.path.renderString.stripPrefix("/")
48+
lookup(key) match
49+
case Some(bytes) =>
50+
logger.debug(
51+
s"[appRouteInMemory] HIT ext=$ext key='$key' size=${bytes.length} bytes"
52+
) >> f.pure(Response[F](Status.Ok).withEntity(bytes).withContentType(contentTypeFor(ext)))
53+
case None =>
54+
logger.debug(
55+
s"[appRouteInMemory] MISS ext=$ext key='$key'"
56+
) >> f.pure(Response[F](Status.NotFound))
57+
4058
HttpRoutes.of[F] {
41-
case req @ GET -> Root / fName ~ "js" =>
42-
lookup(req.uri.path.renderString.stripPrefix("/")) match
43-
case Some(bytes) => f.pure(Response[F](Status.Ok).withEntity(bytes).withContentType(contentTypeFor("js")))
44-
case None => f.pure(Response[F](Status.NotFound))
45-
46-
case req @ GET -> Root / fName ~ "wasm" =>
47-
lookup(req.uri.path.renderString.stripPrefix("/")) match
48-
case Some(bytes) => f.pure(Response[F](Status.Ok).withEntity(bytes).withContentType(contentTypeFor("wasm")))
49-
case None => f.pure(Response[F](Status.NotFound))
50-
51-
case req @ GET -> Root / fName ~ "map" =>
52-
lookup(req.uri.path.renderString.stripPrefix("/")) match
53-
case Some(bytes) => f.pure(Response[F](Status.Ok).withEntity(bytes).withContentType(contentTypeFor("map")))
54-
case None => f.pure(Response[F](Status.NotFound))
59+
case req @ GET -> Root / fName ~ "js" => serve(req, "js")
60+
case req @ GET -> Root / fName ~ "wasm" => serve(req, "wasm")
61+
case req @ GET -> Root / fName ~ "map" => serve(req, "map")
5562
}
5663
end appRouteInMemory
5764

routes/test/src/build.routes.test.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import munit.CatsEffectSuite
1010

1111
class BuildRoutesSuite extends CatsEffectSuite:
1212

13+
given scribe.Scribe[IO] = scribe.cats[IO]
14+
1315
def makeReq(s: String) = org.http4s.Request[IO](uri = org.http4s.Uri.unsafeFromString(s))
1416

1517
// def simpleResponse(body: String) = HttpRoutes.of[IO] {

sjsls/src/liveServer.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,14 @@ object LiveServer extends IOApp:
179179
)(logger)
180180

181181
_ <- lsc.inMemoryFiles match
182-
case Some(files) => updateMapRefFromMemory(files, fileToHashRef)(logger).toResource
183-
case None => updateMapRef(outDirPath, fileToHashRef)(logger).toResource
182+
case Some(files) =>
183+
logger.debug(
184+
s"[liveServer] Seeding hash ref from IN-MEMORY files. count=${files.size()} keys=${scala.jdk.CollectionConverters.SetHasAsScala(files.keySet()).asScala.mkString(", ")}"
185+
).toResource >>
186+
updateMapRefFromMemory(files, fileToHashRef)(logger).toResource
187+
case None =>
188+
logger.debug(s"[liveServer] Seeding hash ref from DISK. outDirPath=$outDirPath").toResource >>
189+
updateMapRef(outDirPath, fileToHashRef)(logger).toResource
184190
// _ <- stylesDir.fold(Resource.unit)(sd => seedMapOnStart(sd, mr))
185191
_ <- fileWatcher(outDirPath, fileToHashRef, linkingTopic, refreshTopic)(logger)
186192
// Only watch the indexHtmlTemplate dir for changes when the caller has NOT supplied a

sjsls/src/refreshRoute.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ def refreshRoutes(
4545
_ =>
4646
// A different tool is responsible for linking, so we hash the files "on the fly" when an update is requested
4747
logger.debug("Updating Map Ref") >>
48-
updateMapRef(stringPath, mr)(logger)
49-
// (inMemoryFiles match
50-
// case Some(files) => updateMapRefFromMemory(files, mr)(logger)
51-
// case None => updateMapRef(stringPath, mr)(logger))
48+
(inMemoryFiles match
49+
case Some(files) =>
50+
logger.debug(
51+
s"[refreshRoute] Updating from IN-MEMORY files. count=${files.size()} keys=${scala.jdk.CollectionConverters.SetHasAsScala(files.keySet()).asScala.mkString(", ")}"
52+
) >> updateMapRefFromMemory(files, mr)(logger)
53+
case None => updateMapRef(stringPath, mr)(logger)
54+
)
5255
)
5356
.as(PageRefresh())
5457
)

sjsls/src/routes.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def routes[F[_]: Files: MonadThrow](
5454
val linkedAppWithCaching: HttpRoutes[IO] = inMemoryFiles match
5555
case Some(files) =>
5656
val lookup: String => Option[Array[Byte]] = name => Option(files.get(name))
57-
ETagMiddleware(appRouteInMemory[IO](lookup), ref)(logger)
57+
ETagMiddleware(appRouteInMemory[IO](lookup)(using Async[IO], logger), ref)(logger)
5858
case None =>
5959
ETagMiddleware(appRoute[IO](stringPath), ref)(logger)
6060
val spaRoutes = clientRoutingPrefix.map(s => (s, buildSpaRoute(indexOpts, ref, zdt, injectPreloads)(logger)))
@@ -75,6 +75,16 @@ def routes[F[_]: Files: MonadThrow](
7575
)
7676
logger.info("Routes created at : ").toResource >>
7777
logger.info("Path: " + stringPath).toResource >>
78+
(inMemoryFiles match
79+
case Some(files) =>
80+
logger
81+
.debug(
82+
s"[routes] Using IN-MEMORY appRoute. inMemoryFiles.size=${files.size()} keys=${scala.jdk.CollectionConverters.SetHasAsScala(files.keySet()).asScala.mkString(", ")}"
83+
)
84+
.toResource
85+
case None =>
86+
logger.debug(s"[routes] Using DISK appRoute. path=$stringPath").toResource
87+
) >>
7888
IO(refreshableApp).toResource
7989

8090
end routes

sjsls/src/staticWatcher.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,9 @@ def updateMapRefFromMemory(
146146
name -> hash
147147
}
148148
.toMap
149-
}.flatMap(newMap => logger.trace(s"Updated in-memory hashes $newMap") *> mr.set(newMap))
149+
}.flatMap(newMap =>
150+
logger.debug(
151+
s"[updateMapRefFromMemory] keys=${newMap.keys.mkString(", ")} hashes=${newMap.mkString(", ")}"
152+
) *>
153+
logger.debug(s"Updated in-memory hashes $newMap") *> mr.set(newMap)
154+
)

0 commit comments

Comments
 (0)