Fork me on GitHub
#babashka
<
2023-02-15
>
mike_ananev08:02:07

@borkdude is it possible to add to bb these classes?

(javax.crypto 
      Cipher 
      SecretKeyFactory) 
(javax.crypto.spec
      GCMParameterSpec
      PBEKeySpec
      SecretKeySpec)
I want to perform standard JVM encryption and other crypto operations. I know about buddy but JVM has well known API and functionality in this field.

borkdude09:02:59

Let me check how much binary size this will add

borkdude10:02:53

Added on master

🎉 2
mike_ananev11:02:52

What can be a problem with public method getEncoded?

clojure.lang.ExceptionInfo: Method getEncoded on class com.sun.crypto.provider.PBKDF2KeyImpl not allowed!
I have the following code
(defn gen-secret-key
  "Generate secret key based on a given token string.
  Returns bytes array 256-bit length."
  [^String secret-token]
  (let [salt    (.getBytes "abcde")
        factory (SecretKeyFactory/getInstance "PBKDF2WithHmacSHA256")
        spec    (PBEKeySpec. (.toCharArray secret-token) salt 10000 256)]
    (.getEncoded (.generateSecret factory spec))))
.generateSecret performs well. but .getEncoded is not allowed

borkdude12:02:58

I'll fix it

borkdude12:02:04

it has to do with native image + reflection

🎉 2
borkdude12:02:23

Fixed on master

mike_ananev12:02:52

Got it. Thank you.

borkdude12:02:47

sorry, there was still a problem, fixing it now

borkdude12:02:46

fixed

👍 2
🚀 2
mike_ananev12:02:41

Now, everything works fine! Great job!

mike_ananev10:02:30

Why symbols set-agent-send-executor! and set-agent-send-off-executor! cannot be resolved in babashka ? This fns are useful if I want to use (Executors/newVirtualThreadPerTaskExecutor)` for agents.

borkdude10:02:41

Probably because nobody has ever asked for it. I have come across those when playing around with loom

👍 2
borkdude10:02:15

Issue / PR welcome

borkdude10:02:19

done

🎉 2
babashka 2
🚀 4
mike_ananev10:02:42

During call script/compile I got an error

Error: Virtual threads are used in code, but are not currently available or active. Use JDK 19 with preview features enabled (--enable-preview).
So I added "--enable-preview" in args list and then got build successfully.

borkdude10:02:58

Yes, this is in the jdk19 branch

teodorlu13:02:42

I want to be able to write babashka script that can control the current directory -- and otherwise have access to my terminal environment. So here's a tiny example of taking the output from a bb script and evaluating it in a bash context:

#!/usr/bin/env bash

eval_lines() {
    while read -r line; do
        eval "$line"
    done
}

bb_script=$(cat <<EOF
(doseq [n (range 5)]
  (println "echo" n))
EOF
)

bb -e "$bb_script" | eval_lines
$ ./script.sh
0
1
2
3
4
I'm curious about whether others have tried to do similar things. I'm also not sure whether using the babashka script's stdout is the way to go -- ideally, I'd like a "side channel" so that I can avoid "hijacking stdout".

borkdude13:02:59

Yes, I've done this before: https://stackoverflow.com/a/64804398/6264 Another way is to start a new shell in another directory

💯 2
borkdude13:02:26

e.g.

bb -e '(babashka.process/shell {:dir "/tmp"} (System/getenv "SHELL"))'

👍 2
teodorlu13:02:39

bb -o '(->> (System/getenv)
            keys
            (filter #(str/ends-with? % "_PROXY"))
            (map #(str "unset " %)))' | 
  source /dev/stdin
Everything is a file! 💯

teodorlu13:02:06

Though if I keep creating subshells, I get a stack of bash -> bb -> bash -> bb -> ...

borkdude13:02:24

You can use process/exec to prevent that, but it doesn't support starting in a different directory. So you'll have to give your shell an init command of going to the other directory

👍 2
borkdude13:02:02

Bash supports --init-file but for zsh I can't find such an option

👍 2
lukasz15:02:14

Are there any guides for customizing babashka build? My use case is "simple": AWS' Java SDK are apparently graalvm/native-image ready and I'd like to bundle include them in the bb binary. (Feels simpler than figuring out how to build my own CLI 😉 )

lispyclouds15:02:22

so the usecase is to be able to use the java aws sdk from bb?

lispyclouds15:02:29

if thats the case, does using https://github.com/grzm/awyeah-api sufffice?

lukasz15:02:06

Right, I forgot about awyeah-api - only thing is I already have all of the code working using "official" AWS SDK, what I really need is a single binary that doesn't depend on the JVM. Might be that I'll just borrow bb's setup for producing binaries

lispyclouds15:02:02

yeah probably thats easier that making the classes available on a custom bb.

lispyclouds15:02:38

you need to add the SDK as a deps.edn/lein dep and add the classes youre using to https://github.com/babashka/babashka/blob/master/src/babashka/impl/classes.clj to get it running on your bb.

lispyclouds15:02:14

id simply try to native compile the clojure code you already have. much less involved process and usecase specific i think

borkdude16:02:37

yes, it depends if this is a one-off script that you would like to execute natively or if it should be scriptable at runtime

lukasz16:02:06

Gotcha, looks like I tried to cut the wrong corners

borkdude16:02:08

There's also snapshot support for JVMs in AWS now which you could use for better startup time

lukasz16:02:41

that's for Lambda - right?

lukasz16:02:07

I'm building a CLI for local use - basically a secrets manager/loader, to replace http://envkey.com - it uses AWS' Secrets Manager as the store

borkdude16:02:30

you could also shell out to aws from bb - or indeed, use awyeah-api :)

lukasz16:02:19

that was my first approach, but I need to fetch multiple resources from AWS, and the CLI is too slow for that when I have to fetch 10+ secrets

lukasz16:02:45

otherwise I'd totally go with bb + shelling out, like in our other scripts/tools

borkdude16:02:18

ok - if you go with plain native image, there's #CAJN79WNT if you run into problems

lukasz16:02:36

Will do! Thanks

chromalchemy16:02:42

What is a good way to attain a simple but reliable web/html live-reload ? Currently I am using bb-http-server (via bbin) and live.js. My page usually reloads, but sometimes fails to repaint CSS changes. It seems like there are issues if the build is too slow, and live.js is reloading too eagerly. I have used Etaoin in the past to execute the reload from a fn. But I prefer not to have an extra anonymous browser profile window to juggle (and have not been able to use Etaoin sucessfully with my personal chrome profile… somehow the profile got corrupted when I tried it, and google thwart’s webdriver for logins, etc). I feel like I’m missing something. Would I need to set up a watch and a websocket or something? Or maybe just force live.js to wait untill build is complete? (not sure how atm)

2
👀 2
lread17:02:04

Heya @U09D96P9B, I don't have an answer for you, but I am a maintainer on Etaoin. If you have a reproducible way to corrupt your chrome profile with Etaoin, and have some time to report it, an issue would be appreciated.

respatialized18:02:21

I’ve been working on a solution to this for my own static site generator (not yet implemented) and I landed on websockets as the solution because they can trigger a refresh and support more dynamic behavior. I think Oz is useful prior art to look at if you want to go that route: https://github.com/metasoarous/oz

borkdude19:02:37

@UFTRLDZEW in quickblog I'm using live.js for this purpose

borkdude19:02:27

I've also used etaoin for auto-refreshing asciidoctor output in the browser in the bb book

keychera03:02:04

wow, I am actually planning to share my solution regarding this here this week, I am currently making a web app with only babashka + htmx and I kinda succeeded in making a (kinda) reliable live reload even with css changes, and the reload relies on websocket, htmx’s swapping mechanism, and a file watcher but I am not sure if this solution works without htmx though…

keychera05:02:15

I played around and I think this can also be used for static html (but the reloader still injects htmx at first) you can see the code here: https://github.com/keychera/panas.reload also I use this code in my other playground, though undocumented, if you want to see it in action in a slightly bigger code (and with tailwind css which surprisingly still works really well) https://github.com/keychera/bantu

🎉 2
chromalchemy05:02:09

Thanks all! @UFTRLDZEW I will check out oz’s solution. @U042NRFKHQE Nice!! I will definitely try this out. I was thinking a htmx version might be close to as simple inclusion as live.js…, but theres some serious code there clojure-spin At least I wasn’t missing something too obvious.. Thanks for sharing, will try this out tomorrow.

2
keychera06:02:53

@U09D96P9B yeah for now, the only way to get the code is to copy them in your src and use it as a middleware, I need to figure out how to distribute this, maybe I need to make it a library or a pod, I guess I’m gonna ask a question in the channel later

keychera10:02:22

@U09D96P9B just wanted to let you know that I just made it possible to use https://github.com/keychera/panas.reload via git/sha dependency, there should be minimal configurations needed to run it, but there might be some quirks since this is still really new. More details in the project readme, let me know if you need any help

chromalchemy21:02:19

Awesome! thanks for sharing, will try out.

2