nbb

martinklepsch 2022-08-16T17:14:32.874819Z

I’m confused I’m having such a hard time finding this info but how do I get stdin in an nbb script?

souenzzo 2022-08-17T12:31:11.508409Z

i tried to use stdin in nodejs once. it is a mess.

valtteri 2022-08-16T17:32:04.727289Z

(js/process.stdin.on "data" (fn [data] (println "From stdin: " (str data))))

valtteri 2022-08-16T17:32:48.595029Z

You can put that in a .cljs file and call echo ‘something’ | nbb myfile.cljs

valtteri 2022-08-16T17:35:15.550639Z

Basically, process.stdin gives you a Stream object and you can attach callbacks for certain events on that.

valtteri 2022-08-16T17:35:55.691239Z

https://nodejs.org/api/process.html#processstdin

martinklepsch 2022-08-16T18:39:31.955789Z

Thank you! That API feels weird but I guess we’ll go with it 😁

valtteri 2022-08-16T19:39:44.388659Z

Yep, that’s typical node weirdness. 🙂 No blocking is good so it’s callbacks or sugar around it like promises

valtteri 2022-08-16T19:40:14.594659Z

I think there’s a promises based version of the stream api

valtteri 2022-08-16T19:42:45.068249Z

Uhhhmm kinda yes. At least there’s pipeline function that can construct streaming pipelines. Here’s a stupid example that just pipes stuff from stdin to stdout. 😄

(ns demo
  (:require ["stream/promises" :as sp]))

(sp/pipeline js/process.stdin js/process.stdout)

valtteri 2022-08-16T19:43:00.468539Z

But you could add there transform steps and whatnot

valtteri 2022-08-16T19:43:09.867089Z

Kinda weird, kinda cool.

valtteri 2022-08-16T19:52:46.907229Z

Heh, yeah you can do this kind of things

(ns demo
  (:require ["stream/promises" :as sp]
            ["stream" :as s]))

(def upcase
  (s/Transform. #js{:transform (fn [chunk enc cb]
                                 (cb nil (clojure.string/upper-case (str chunk))))}))

(sp/pipeline js/process.stdin upcase js/process.stdout)

valtteri 2022-08-16T19:54:20.424659Z

And now I can upcase enormous amounts of text with very low memory footprint.

2022-08-16T20:45:08.705829Z

Would it be possible to use zprint within nbb?

borkdude 2022-08-16T20:49:12.030689Z

zprint does currently work with babashka, bit I expect there to be some issues with nbb compatibility since nbb doesn't have rewrite-clj built-in

2022-08-16T20:53:25.920659Z

Thanks! Use case is a build script that fetches data, formats it as a clojure file str, and was hoping to use zprint api to format it as a file and write it to the fs. Worst case can use it from an npm wrapper after the build script https://www.npmjs.com/package/zprint-wrapper

2022-08-16T20:54:33.670899Z

Didn't realize you were on vacation, sorry for the interruption!

borkdude 2022-08-16T20:55:26.695479Z

Oh not at all, it would be ridiculous if people couldn't ask questions here if I were on vacation. It's up to me to answer or not ;)

👍 1
❤️ 1
lread 2022-08-16T20:50:00.900769Z

hey, aren’t you on vacation?

lread 2022-08-16T20:52:35.475979Z

fwiw, zprint does carry out some tests for self-hosted clojurescript under planck.