Fork me on GitHub
#clojurescript
<
2021-12-03
>
Chris McCormick08:12:15

Hey all, in the Stripe API it has a section on pagination and it says this: > To use the auto-pagination feature in Node 10+, simply iterate over a "list" call with the parameters you need in a for await loop. With this code example:

// In Node 10+:
for await (const customer of stripe.customers.list({limit: 3})) {
  // Do something with customer
}
I'm bamboozled as to how to accomplish this with cljs. Any pointers?

thheller08:12:05

@chris358 try cljs.core/es6-iterator-seq. in clojure terms its just a lazy seq of promises

Chris McCormick08:12:30

Thanks, will give it a try. 🙏

Adam Helins11:12:42

In Node, I was hoping to read EDN from STDIN using tools.reader 1.3.6. Am I missing some steps?

(edn/read (reader-types/node-readable-push-back-reader js/process.stdin))
ERROR [salus.client.cli:35] - #error {:message "Argument to char must be a character or number", :data {:type :reader-exception}, :cause #object[Error Error: Argument to char must be a character or number]}
Error: Argument to char must be a character or number
    at new cljs$core$ExceptionInfo (/home/adam/projects/bevuta/cljs-client/.shadow-cljs/builds/cli/dev/out/cljs-runtime/cljs/core.cljs:11472:11)
    at Function.cljs$core$IFn$_invoke$arity$3 (/home/adam/projects/bevuta/cljs-client/.shadow-cljs/builds/cli/dev/out/cljs-runtime/cljs/core.cljs:11499:1)
    at Function.cljs$core$IFn$_invoke$arity$4 (/home/adam/projects/bevuta/cljs-client/.shadow-cljs/builds/cli/dev/out/cljs-runtime/cljs/tools/reader/edn.cljs:427:19)
    at Function.cljs$core$IFn$_invoke$arity$2 (/home/adam/projects/bevuta/cljs-client/.shadow-cljs/builds/cli/dev/out/cljs-runtime/cljs/tools/reader/edn.cljs:398:9)
    at Function.cljs$core$IFn$_invoke$arity$1 (/home/adam/projects/bevuta/cljs-client/.shadow-cljs/builds/cli/dev/out/cljs-runtime/cljs/tools/reader/edn.cljs:395:14)

thheller15:12:17

JS/node doesn't have blocking IO so process.stdin is not a regular inputstream that tools.reader can use

👍 1
Ahmad Rasyid Salim12:12:10

Hello, I follow the guide from https://clojurescript.org/guides/quick-start, using the latest release ClojureScript JAR (1.10.891) and I got an error, am I missing something?

PS D:\Rasyid\Coding\clojurescript\quick-start> java -cp ".\cljs.jar;src" cljs.main -co .\build.edn -r
Exception in thread "main" clojure.lang.ExceptionInfo: Unexpected error during REPL initialization {:cljs.repl/error :init-failed}
        at cljs.repl$repl_STAR_.invokeStatic(repl.cljc:1220)
        at cljs.repl$repl_STAR_.invoke(repl.cljc:1033)
        at cljs.cli$repl_opt.invokeStatic(cli.clj:325)
        at cljs.cli$repl_opt.invoke(cli.clj:312)
        at cljs.cli$main.invokeStatic(cli.clj:703)
        at cljs.cli$main.doInvoke(cli.clj:692)
        at clojure.lang.RestFn.applyTo(RestFn.java:139)
        at clojure.core$apply.invokeStatic(core.clj:667)
        at clojure.core$apply.invoke(core.clj:660)
        at cljs.main$_main.invokeStatic(main.clj:65)
        at cljs.main$_main.doInvoke(main.clj:56)
        at clojure.lang.RestFn.applyTo(RestFn.java:137)
        at cljs.main.main(Unknown Source)
Caused by: clojure.lang.ExceptionInfo: null {:clojure.error/source nil, :clojure.error/line nil, :clojure.error/column nil, :clojure.error/phase :compilation}
        at cljs.analyzer$analyze.invokeStatic(analyzer.cljc:4269)
        at cljs.analyzer$analyze.invoke(analyzer.cljc:4255)
        at cljs.analyzer$analyze_file$fn__3374.invoke(analyzer.cljc:4773)
        at cljs.analyzer$analyze_file.invokeStatic(analyzer.cljc:4768)
        at cljs.analyzer$analyze_file.invoke(analyzer.cljc:4726)
        at cljs.analyzer$analyze_file.invokeStatic(analyzer.cljc:4790)
        at cljs.analyzer$analyze_file.invoke(analyzer.cljc:4726)
        at cljs.analyzer$analyze_file.invokeStatic(analyzer.cljc:4740)
        at cljs.analyzer$analyze_file.invoke(analyzer.cljc:4726)
        at cljs.compiler$with_core_cljs.invokeStatic(compiler.cljc:1473)
        at cljs.compiler$with_core_cljs.invoke(compiler.cljc:1463)
        at cljs.repl$repl_STAR_.invokeStatic(repl.cljc:1121)
        ... 12 more
Caused by: clojure.lang.ExceptionInfo: Illegal char <:> at index 4: file:\D:\Rasyid\Coding\clojurescript\quick-start\cljs.jar!\goog\math\long.js in file file:/D:/Rasyid/Coding/clojurescript/quick-start/cljs.jar!/cljs/core.cljs {:tag :cljs/analysis-error}
        at cljs.analyzer$error.invokeStatic(analyzer.cljc:719)
        at cljs.analyzer$error.invoke(analyzer.cljc:715)
        ... 24 more
Caused by: java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\D:\Rasyid\Coding\clojurescript\quick-start\cljs.jar!\goog\math\long.js
        at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
        at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229)
        at java.base/java.io.File.toPath(File.java:2329)
        at cljs.externs$resource__GT_source_file.invokeStatic(externs.clj:293)
        at cljs.externs$resource__GT_source_file.invoke(externs.clj:290)
        at cljs.externs$analyze_goog_file.invokeStatic(externs.clj:307)
        at cljs.externs$analyze_goog_file.invoke(externs.clj:297)
        at cljs.analyzer$analyze_deps.invokeStatic(analyzer.cljc:2640)
        at cljs.analyzer$analyze_deps.invoke(analyzer.cljc:2612)
        at cljs.analyzer$ns_side_effects.invokeStatic(analyzer.cljc:4131)
        at cljs.analyzer$ns_side_effects.invoke(analyzer.cljc:4126)
        at cljs.analyzer$analyze_STAR_$fn__3248.invoke(analyzer.cljc:4253)
        at clojure.lang.PersistentVector.reduce(PersistentVector.java:343)
        at clojure.core$reduce.invokeStatic(core.clj:6827)
        at clojure.core$reduce.invoke(core.clj:6810)
        at cljs.analyzer$analyze_STAR_.invokeStatic(analyzer.cljc:4253)
        at cljs.analyzer$analyze_STAR_.invoke(analyzer.cljc:4244)
        at cljs.analyzer$analyze.invokeStatic(analyzer.cljc:4272)
        ... 23 more

p-himik13:12:53

build.edn is supposed to be a namespace, not a file. So, it shouldn't start with .\ and instead there should be a .\src\build\edn.cljs file with a top (ns build.edn) form.

p-himik13:12:23

Notice how in the guide they specify --compile hello-world.core and have the file at src/hello_world/core.cljs.

p-himik13:12:57

Also, I think you don't have to use a JAR file even on Windows - clj CLI is available there as well, although it seems to be in its alpha state: https://github.com/clojure/tools.deps.alpha/wiki/clj-on-Windows

dnolen14:12:52

@ahmadrasyidsalim bug that's since been fixed

dnolen14:12:23

try 1.10.896 instead

1
manutter5119:12:25

This one has me stumped. From the code for a browser-based PDF viewer that gets the PDF file data as a Base64 string from a remote server:

(let [image-string (:imageString image-data)
        image-blob (when-not (empty? image-string) 
                     (js/Blob. #js [(js/atob image-string)] #js {:type "application/pdf"}))]
    (cljs.pprint/pprint {:string-size (when image-string (count image-string))
                         :bytes--size (when image-blob (.-size image-blob))}))
==> {:string-size 5509500, :bytes--size 6223041} 
How is the Base64-encoded string SMALLER than the binary it decodes to??

Léo J19:12:36

aren’t string char sometimes multi-bytes? :thinking_face:

Léo J19:12:13

like äëãñ… ?

manutter5119:12:48

It’s Base64 though--all plain vanilla ASCII.

Léo J19:12:22

true … I’ll go hide myself :x

😄 1
andy.fingerhut20:12:18

I've never actually looked at the details of Base64 before -- perhaps it has some features for run-length encoding of a bunch of 0s in the binary data?

andy.fingerhut20:12:04

5509500 * 3/4 = 4132125 should be close to the expected decoded binary length. It seems suspicious that 6223041 is almost exactly 1.5 times larger than that.

manutter5120:12:15

Here’s what I get with a different PDF file: {:string-size 9833864, :bytes--size 11096841}

manutter5120:12:40

And another: {:string-size 8068844, :bytes--size 9116349}

manutter5120:12:40

Ok, that last one I tracked down the original file, and the actual file size is 6,051,632 bytes. Definitely something funk-ish happening here.

manutter5121:12:42

Aha, solved the problem: you have to copy the individual bytes of the atob result into a Uint8Array, and use the Uint8Array in the constructor for the Blob.

manutter5121:12:14

The decoded Base64 value was indeed smaller than the encoded value, as it should be. The problem was that it was being inflated by the Blob constructor somehow, probably related to character encoding. Putting a Uint8Array in the middle kept it the right size.

👍 1
roklenarcic21:12:16

I am trying to call this function: openImageDialog. I have an instance of Reader react component but (.openImageDialog reader) doesn’t work. I also tried to call it on the class itself and it says it’s not a function. https://github.com/JodusNodus/react-qr-reader/blob/master/src/index.js#L61

p-himik21:12:28

What do you mean by "doesn't work"? And what do you mean by "call it on the class itself"?

roklenarcic21:12:41

I have imported this as QrReader symbol in my ns

roklenarcic21:12:06

I instantiate this react component into reader variable

roklenarcic21:12:13

Then I tried: (.openImageDialog reader) which results in camera.cljs:68 Uncaught TypeError: reader.openImageDialog is not a function

roklenarcic21:12:54

I tried (.openImageDialog QrReader) and I get `camera.cljs:68 Uncaught TypeError: module$node_modules$react_qr_reader_rokl$lib$index.openImageDialog is not a function`

p-himik21:12:27

reader.openImageDialog is not a function most likely means that reader is not what you think it is. Try logging the value and comparing the result to your expectations.

p-himik21:12:54

> I instantiate this react component into `reader` variable How are you doing this?

roklenarcic21:12:09

core.cljs:200 #js {$$typeof #object[Symbol(react.element)], :type #object[Reader], :key nil, :ref nil, :props #js {:delay 300, :style #js {:width 100%}, :resolution 1000, :onScan #object[Function], :onError #object[Function], :characterSet ISO-8859-2, :facingMode user, :showViewFinder true, :constraints nil}, :_owner #object[FiberNode [object Object]], :_store #js {}}

roklenarcic21:12:17

that’s what println for reader prints

roklenarcic21:12:26

seems like an instance of react element

roklenarcic21:12:56

Instantiated by js/React.createElement

p-himik21:12:27

An element is not an instance of the component's class. You have to use a ref to get the underlying instance.

roklenarcic21:12:56

hm seems like ref is nil

p-himik21:12:57

Also, you probably don't need to use js/React.createElement directly - but that depends on the UI library/framework that you're using on top of React, if any.

p-himik21:12:29

If the ref is nil then you're either not setting it right, or you're looking at it at the wrong time, or you're setting it for some wrapper that doesn't support refs.