This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-03-16
Channels
- # announcements (11)
- # atom-editor (4)
- # babashka (26)
- # beginners (126)
- # calva (35)
- # chlorine-clover (14)
- # clj-kondo (50)
- # cljfx (1)
- # cljs-dev (1)
- # cljsrn (3)
- # clojure (31)
- # clojure-europe (144)
- # clojure-germany (2)
- # clojure-nl (3)
- # clojure-serbia (17)
- # clojure-spain (11)
- # clojure-uk (38)
- # clojurescript (87)
- # community-development (1)
- # conjure (1)
- # datalog (1)
- # datascript (160)
- # datomic (28)
- # duct (2)
- # emacs (4)
- # events (1)
- # figwheel-main (1)
- # fulcro (15)
- # graalvm (4)
- # honeysql (53)
- # jobs (2)
- # jobs-discuss (14)
- # juxt (6)
- # lsp (59)
- # malli (13)
- # music (1)
- # off-topic (8)
- # pathom (22)
- # portal (7)
- # re-frame (2)
- # reagent (3)
- # releases (1)
- # remote-jobs (1)
- # rewrite-clj (1)
- # shadow-cljs (25)
- # sql (3)
- # tools-deps (38)
- # xtdb (17)
Anyone familiar with the cljs-ajax library? Is there a way to set the request content-type to text/csv
I went down a rabbit hole and to change the request
Header’s content type, it’s under format
I found a way of doing it, not sure if its good, but it works. pass in a valid text-request-format to :format
:format {:write #(str %)
:content-type "text/csv; charset=utf-8")}
This will pass in "text/csv; charset=utf-8
in the request header’s content-typeIs there a good online resource detailing the evolution of ClojureScript since it was created? I can’t find a changelog on the ClojureScript github repo.
Thanks @U2FRKM4TW - I must have missed that.
I have the following code:
(let [r (t/reader :json)
res->js (clj->js res)]
(js/console.log "res->js is " (clj->js res))
(js/console.log "response is " (t/read r res->js))
)
basically the res variable is a json response from a server. I convert it into js using (clj->js) and this is the output that I get:
{"predictions": [{"description": "Sydney NSW, Australia", "matched_substrings": [Array], "place_id": "ChIJP3Sa8ziYEmsRUKgyFmh9AQM", "reference": "ChIJP3Sa8ziYEmsRUKgyFmh9AQM", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "San Francisco, CA, USA", "matched_substrings": [Array], "place_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "reference": "ChIJIQBpAG2ahYAR_6128GcTUEo", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "Seattle, WA, USA", "matched_substrings": [Array], "place_id": "ChIJVTPokywQkFQRmtVEaUZlJRA", "reference": "ChIJVTPokywQkFQRmtVEaUZlJRA", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "Surat, Gujarat, India", "matched_substrings": [Array], "place_id": "ChIJYxUdQVlO4DsRQrA4CSlYRf4", "reference": "ChIJYxUdQVlO4DsRQrA4CSlYRf4", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "San Antonio, TX, USA", "matched_substrings": [Array], "place_id": "ChIJrw7QBK9YXIYRvBagEDvhVgg", "reference": "ChIJrw7QBK9YXIYRvBagEDvhVgg", "structured_formatting": [Object], "terms": [Array], "types": [Array]}], "status": "OK"}
So I’m using the cognitect transit library to parse this json into clojurescript. And I do it using
(t/read r res->js)
However, I’m getting this error on this line:
SyntaxError: JSON Parse error: Unexpected identifier "object"
How to fix this?that is not how transit works. t/read
can only be used to read the output produced by t/write. clj->js
already gave you the CLJ datastructures
@thheller this is what I get when I use clj->js:
{"predictions": [{"description": "San Francisco, CA, USA", "matched_substrings": [Array], "place_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "reference": "ChIJIQBpAG2ahYAR_6128GcTUEo", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "San Antonio, TX, USA", "matched_substrings": [Array], "place_id": "ChIJrw7QBK9YXIYRvBagEDvhVgg", "reference": "ChIJrw7QBK9YXIYRvBagEDvhVgg", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "San Diego, CA, USA", "matched_substrings": [Array], "place_id": "ChIJSx6SrQ9T2YARed8V_f0hOg0", "reference": "ChIJSx6SrQ9T2YARed8V_f0hOg0", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "São Paulo, State of São Paulo, Brazil", "matched_substrings": [Array], "place_id": "ChIJ0WGkg4FEzpQRrlsz_whLqZs", "reference": "ChIJ0WGkg4FEzpQRrlsz_whLqZs", "structured_formatting": [Object], "terms": [Array], "types": [Array]}, {"description": "Salt Lake City, UT, USA", "matched_substrings": [Array], "place_id": "ChIJ7THRiJQ9UocRyjFNSKC3U1s", "reference": "ChIJ7THRiJQ9UocRyjFNSKC3U1s", "structured_formatting": [Object], "terms": [Array], "types": [Array]}], "status": "OK"}
This is what I get when I use (js->clj):
{"__hash": null, "arr": ["predictions", {"__hash": null, "cljs$lang$protocol_mask$partition0$": 167666463, "cljs$lang$protocol_mask$partition1$": 139268, "cnt": 5, "meta": null, "root": [Object], "shift": 5, "tail": [Array]}, "status", "OK"], "cljs$lang$protocol_mask$partition0$": 16647951, "cljs$lang$protocol_mask$partition1$": 139268, "cnt": 2, "meta": null}
I don’t know what to do with the latterthe js->clj
is the proper CLJS datastructure. it just looks that way when printed in the console
perfect thanks @thheller
in fact just (pr-str res) works
i don’t know how it was converted automatically. I’m making an http-xhrio request with the response format set to json-response-format. Not sure why it’s automatically converted
@witek that seems pretty strange that =
would return anything other than true
or false
- I agree some minimal example would be useful here to uncover a bug if there is one
=
internally uses identical?
so it should have worked. also note that ClojureScript itself uses =
in many situations and we don't have this problem you've observed.
This is my code:
(fn [^js user]
(js/console.log "debug-1" user)
(js/console.log "debug-2" @AUTH_USER)
(js/console.log "debug-3" (= user @AUTH_USER))
(js/console.log "debug-4" (identical? user @AUTH_USER))
This is the output:
debug-1 hh {J: Array(0), l: "AIzaSyA0OD1u2f_weTWD7NXLacez7YdbOCjlbpo", m: "[DEFAULT]", s: "", a: Fa, …}
debug-2 null
debug-3 de {a: 0, i: undefined, c: de, b: null, f: null, …}
debug-4 false
And this is the JavaScript:
function(f){
console.log("debug-1",f);
console.log("debug-2",u(PR));
console.log("debug-3",A.c(f,u(PR)));
console.log("debug-4",f===u(PR));
I think thats just one thing with hh
or de
being the class/type name which the console prints first
@dnolen This is copy-paste from Chrome Console. Chrome prints hh
and then the "content" of it, which I can expand and so on...
you might be hitting the same kind of issue that clj->js
can sometimes hit where it can report false positives for certain "fast-path" protocols
ie. the :advanced
renamed the protocol mask property to J
and your JS object having a .J
property as well
Yes, I am using other minified .js
files. user
for example comes from Google Firebase Authentication library which is included via <script src=
.
You can verify that its name collisions with the suggestions > :pseudo-names true or :simple? and if so, you can set a prefix for the renaming https://clojurescript.org/reference/compiler-options#rename-prefix
typically externs take care of this but that doesn't really apply to this issue. kinda tricky
@witek try adding a log for (js/console.log "debug-5" (-equiv user "oops"))
. just to confirm it is actually hitting that protocol issue
(js/console.log "debug-5" (-equiv user "oops"))
prints debug-5 de {a: 0, i: undefined, c: de, b: null, f: null, …}
ok yeah that sort of confirms it. only workaround I can think of is adding cljs$core$IEquiv$_equiv$arity$2
to your externs. eg. externs/<build-name>.txt
in your project with just one line cljs$core$IEquiv$_equiv$arity$2
Oh maybe there's a possibility to include Google Firebase Authentication library as an NPM dependency and not via <script>
. This should help as well, right?
the issue likely is that closure renames cljs$core$IEquiv$_equiv$arity$2
to something short like J
so it just blindly calls that function which happens to not be the expected protocol impl but some other function that just happens to return a true-ish value
adding the externs should confirm this is actually what is happening. kinda hard to reproduce since it must hit exactly that renamed property name 😛
Is this somehow specific to -equiv
? Or could other functions also run into the same problem?
hmm doh. technically this can hit other protocols as well but requires very specific circumstances
But it seems that if both compilers/minimizers start their identifiers from a to Z to A to Z then there will be quite a number of name clashes and such issues.
clojure -M:shadow-cljs release spa --config-merge '{:compiler-options {:rename-prefix "happygast"}}'
did not solve the problem. Did I something wrong?
Hypothetically speaking - is it possible to completely isolate any interaction with such scripts so they cannot at all clash with anything? I assume using them from web workers would be one way.
not really no. you are interacting with a foreign object that would normally require externs
so you could also create externs that "ban" the shortened names that a likely to clash. a-zA-Z
or so might be enough
Oh, but what if you create a non-minifed small JS wrapper to be used along with the offending script in a web worker?
Would be nice to have a tool in the build pipeline which takes all minified .js
files and errors when there is a clash. Would this be somehow possible?
this is an extremely uncommon issue and very hard to hit. pretty much only happens because you basically have two :advanced
compiled scripts on the page
I imagine another potential way to prevent such issues would be to never call any CLJS code on the objects returned by the code from minimized third-party scripts.
So the example with user
above would first use regular JS code (or careful CLJS/JS interop) to create a plain JS object with all the necessary fields, and only then use that object in the CLJS code.
directly including separately minified JS into the page via script tags would lead to this problem
So converting objects from minified libraries like user
to a clojure map before using it would be a safe approach?
I would not do anything defensive - some foreign libraries like this simply need externs