Skip to content

Commit c6dde30

Browse files
authored
Serve viewer.js from storage.clerk.garden (#415)
Serve viewer.js from clerk CAS on storage.clerk.garden instead of google bucket. Fixes #377: adds support for serving the source map for Clerk's cljs bundle Fixes #387: avoids a http request on boot which would lead to a failure when offline
1 parent cb6fe28 commit c6dde30

File tree

7 files changed

+29
-85
lines changed

7 files changed

+29
-85
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@ jobs:
3535
restore-keys: |
3636
${{ runner.os }}-maven-
3737
38-
- name: 🔐 Google Auth
39-
uses: google-github-actions/auth@v0
40-
with:
41-
credentials_json: ${{ secrets.GCLOUD_SERVICE_KEY }}
42-
43-
- name: 🔧 Setup Google Cloud SDK
44-
uses: google-github-actions/[email protected]
45-
4638
- name: Build and upload viewer resources
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.CAS_AUTH_TOKEN }}
4741
run: bb build+upload-viewer-resources
4842

4943
clj-kondo:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ clerk.iml
2020
.nrepl-port
2121
.clj-kondo/marick
2222
.clj-kondo/nextjournal/clerk
23+
.secrets.env

bb.edn

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{:min-bb-version "0.9.159"
22
:paths ["bb"]
3-
:deps {io.github.nextjournal/dejavu {:git/sha "4980e0cc18c9b09fb220874ace94ba6b57a749ca"}}
3+
:deps {io.github.nextjournal/dejavu {:git/sha "4980e0cc18c9b09fb220874ace94ba6b57a749ca"}
4+
io.github.nextjournal/cas-client {:git/sha "534d8c6679cc005fd9ad99cfe3d5aeaa6411aee8"}
5+
mvxcvi/multihash {:git/url "https://github.com/borkdude/clj-multihash"
6+
:git/sha "7cd4d1fc1301aeea36d5ba80e65ebe2fdbaaa392"}}
47
:tasks
58
{:requires
69
([tasks :as t]
@@ -27,10 +30,6 @@
2730
lint {:doc "Lints project using clj-kondo"
2831
:task (apply shell "clj-kondo --lint src test" *command-line-args*)}
2932

30-
release:js {:doc "Uploads the js release and updates the sha reference."
31-
:task (clojure "-T:build upload-to-cas :resource viewer.js")
32-
:depends [build:js]}
33-
3433
build:static-app {:doc "Builds a static app with default notebooks"
3534
:task (apply clojure "-X:demo:nextjournal/clerk" *command-line-args*)}
3635

build.clj

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
[babashka.process :as process]
44
[clojure.string :as str]
55
[clojure.tools.build.api :as b]
6-
[nextjournal.cas :as cas]
7-
[nextjournal.clerk.render.hashing]
6+
[nextjournal.clerk.config :as config]
87
[shared]))
98

109
(def lib 'io.github.nextjournal/clerk)
@@ -19,7 +18,7 @@
1918
(defn package-clerk-asset-map [{:as opts :keys [target-dir]}]
2019
(when-not target-dir
2120
(throw (ex-info "target dir must be set" {:opts opts})))
22-
(let [asset-map (slurp (nextjournal.clerk.render.hashing/get-lookup-url))]
21+
(let [asset-map @config/!asset-map]
2322
(spit (str target-dir java.io.File/separator "clerk-asset-map.edn") asset-map)))
2423

2524
(defn jar [_]
@@ -62,21 +61,3 @@
6261
(throw e)
6362
(println "This release was already deployed."))))
6463
opts)
65-
66-
(defn asserting [val msg] (assert (seq val) msg) val)
67-
68-
(defn upload! [opts file] (:url (cas/upload! opts file)))
69-
70-
(defn update-resource! [opts _resource _ file]
71-
(upload! opts file))
72-
73-
(defn get-gsutil [] (str/trim (:out (process/sh ["which" "gsutil"]))))
74-
75-
(def resource->path
76-
'{viewer.js "/js/viewer.js"})
77-
78-
(defn upload-to-cas [{:keys [resource]}]
79-
(if-let [target (resource->path resource)]
80-
(update-resource! {:exec-path (str/trim (asserting (get-gsutil) "Can't find gsutil executable."))
81-
:target-path "gs://nextjournal-cas-eu/data/"} target :uploading (str "build/" resource))
82-
(throw (ex-info (str "unsupported resource " resource) {:supported-resources (keys resource->path)}))))

deps.edn

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
binaryage/devtools {:mvn/version "1.0.3"}
4242
cider/cider-nrepl {:mvn/version "0.29.0"}
4343
com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.0.3"}
44+
io.github.nextjournal/cas-client {:git/sha "534d8c6679cc005fd9ad99cfe3d5aeaa6411aee8"}
4445
org.slf4j/slf4j-nop {:mvn/version "1.7.36"}
4546
org.babashka/cli {:mvn/version "0.5.40"}}
4647
:extra-paths ["dev" "notebooks"]
@@ -53,8 +54,7 @@
5354
:test {:extra-deps {nubank/matcher-combinators {:mvn/version "3.5.1"}
5455
org.clojure/test.check {:mvn/version "1.1.1"}
5556
lambdaisland/kaocha {:mvn/version "1.66.1034"}
56-
io.github.cognitect-labs/test-runner
57-
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}
57+
io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}
5858
:extra-paths ["test"]
5959
:jvm-opts ["-Dclerk.resource_manifest={\"/js/viewer.js\" \"/js/viewer.js\"}"]
6060
:exec-fn kaocha.runner/exec-fn
@@ -70,9 +70,9 @@
7070
io.github.nextjournal/clerk-slideshow {:git/sha "5fe197ca0c8f28128379849dc677cd718be71524"}}}
7171

7272
:build {:deps {io.github.nextjournal/clerk {:local/root "."}
73+
io.github.nextjournal/cas-client {:git/sha "534d8c6679cc005fd9ad99cfe3d5aeaa6411aee8"}
7374
io.github.clojure/tools.build {:git/tag "v0.6.1" :git/sha "515b334"}
7475
io.github.slipset/deps-deploy {:git/sha "b4359c5d67ca002d9ed0c4b41b710d7e5a82e3bf"}
75-
io.github.nextjournal/cas {:git/url "[email protected]:nextjournal/cas"
76-
:git/sha "5e8079b720e347b9466db9c2282ce79a125a011c"}}
76+
mvxcvi/multihash {:mvn/version "2.0.3"}}
7777
:extra-paths ["bb" "src" "resources"] ;; for loading lookup-url in build
7878
:ns-default build}}}

src/nextjournal/clerk/config.clj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
(when-not (str/blank? prop)
1717
(read-string prop))))
1818

19-
20-
2119
(def !asset-map
2220
;; In mvn releases, the asset map is available in the artifact
2321
(delay (or (some-> (io/resource "clerk-asset-map.edn") slurp edn/read-string)
24-
(try ((requiring-resolve 'nextjournal.clerk.render.hashing/read-dynamic-asset-map!))
22+
(try ((requiring-resolve 'nextjournal.clerk.render.hashing/dynamic-asset-map))
2523
(catch Exception e
2624
(throw (ex-info "Error reading dynamic asset map"
2725
(or (ex-data e)
@@ -39,7 +37,6 @@
3937
#_(reset! !resource->url default-resource-manifest)
4038
#_(reset! !resource->url (-> (slurp lookup-url) edn/read-string))
4139

42-
4340
(def ^:dynamic *in-clerk* false)
4441

4542
(def ^:dynamic *bounded-count-limit*

src/nextjournal/clerk/render/hashing.clj

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
"Computes a hash based for Clerk's render cljs bundle."
33
{:no-doc true}
44
(:require [babashka.fs :as fs]
5-
[clojure.edn :as edn]
65
[clojure.java.io :as io]
76
[clojure.string :as str]
87
[nextjournal.dejavu :as djv]))
98

109
(def output-dirs ["resources/public/ui"
1110
"resources/public/build"])
1211

13-
;; Example link in bucket:
14-
;; "https://storage.googleapis.com/nextjournal-cas-eu/data/8VwKauX6JACEP3K6ahNmP5p1w7rWdhKzeGXCDrHMnJiVrUxHVxcm3Xj84K2r3fcAKWxMQKzqoFe92osgFEHCuKCtZC"
15-
16-
(def gs-bucket "gs://nextjournal-cas-eu")
17-
(def storage-base-url "https://storage.googleapis.com/nextjournal-cas-eu")
18-
19-
2012
(defn file-set [base-dir]
2113
(reduce into
2214
[]
@@ -26,7 +18,6 @@
2618
"yarn.lock"])
2719
(djv/cljs-files (mapv #(fs/file base-dir %) ["src" "resources"]))]))
2820

29-
3021
#_(file-set (fs/file "."))
3122
#_(System/setProperty "nextjournal.dejavu.debug" "1")
3223

@@ -38,41 +29,22 @@
3829
(throw (ex-info "Clerk could note compute `font-end-hash` for cljs bundle." {:base-dir base-dir})))
3930
(str (djv/file-set-hash base-dir (file-set base-dir)))))
4031

41-
(defn bucket-lookup-url [lookup-hash]
42-
(str gs-bucket "/lookup/" lookup-hash))
43-
44-
(defn asset-name [hash suffix]
45-
(str "/assets/" hash
46-
(when suffix
47-
(str "-" suffix))))
48-
49-
(defn get-lookup-url []
50-
(str storage-base-url "/lookup/" (front-end-hash)))
51-
52-
#_(get-lookup-url)
53-
54-
(defn read-dynamic-asset-map!
55-
"Computes a hash for Clerk's cljs bundle and tries to load the asset manifest for it.
56-
57-
Used only when Clerk is used as a git dep, should never be called from the jar."
58-
[]
59-
(edn/read-string (try
60-
(slurp (get-lookup-url))
61-
(catch java.io.FileNotFoundException e
62-
(throw (ex-info (str "Clerk could not find dynamic asset map at " (get-lookup-url)) {:url (get-lookup-url)} e))))))
32+
(defn assets-tag []
33+
(str "clerk-assets@" (front-end-hash)))
6334

64-
#_(read-dynamic-asset-map!)
35+
(defn dynamic-asset-map []
36+
{"/js/viewer.js" (str "https://storage.clerk.garden/nextjournal/" (assets-tag) "/viewer.js?immutable=true")})
6537

6638
(defn build+upload-viewer-resources []
67-
(let [front-end-hash (front-end-hash)
68-
manifest (str (fs/create-temp-file))
69-
res (djv/gs-copy (str (bucket-lookup-url front-end-hash)) manifest false)]
70-
(when (= res ::djv/not-found)
39+
(let [tag (assets-tag)]
40+
(when-not ((requiring-resolve 'nextjournal.cas-client/tag-exists?) {:namespace "nextjournal" :tag tag})
41+
(println (format "Could not find entry at %s. Building..." tag))
7142
((requiring-resolve 'babashka.tasks/run) 'build:js)
72-
(let [content-hash (djv/sha512 (slurp "build/viewer.js"))
73-
viewer-js-http-link (str storage-base-url (asset-name content-hash "viewer.js"))]
74-
(spit manifest {"/js/viewer.js" viewer-js-http-link})
75-
(println "Manifest:" (slurp manifest))
76-
(println "Coping manifest to" (bucket-lookup-url front-end-hash))
77-
(djv/gs-copy manifest (bucket-lookup-url front-end-hash))
78-
(djv/gs-copy "build/viewer.js" (str gs-bucket (asset-name content-hash "viewer.js")))))))
43+
(println "Uploading...")
44+
(let [res ((requiring-resolve 'nextjournal.cas-client/cas-put) {:path "build"
45+
:auth-token (System/getenv "GITHUB_TOKEN")
46+
:namespace "nextjournal"
47+
:tag tag})]
48+
(doseq [[k v] res]
49+
(println (str k ": " v))))
50+
(println "Done"))))

0 commit comments

Comments
 (0)