Skip to content

Commit 5877ae0

Browse files
committed
[nop] Rename :pstr -> :packed
Packed values were historically always strings, but this changed some time ago and may now be binary data (at least for WebSockets).
1 parent bbd89d4 commit 5877ae0

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

src/taoensso/sente.cljc

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
* cb - Callback
2121
* tout - Timeout
2222
* ws - WebSocket/s
23-
* pstr - Packed string. Arbitrary Clojure data serialized as a
24-
string (e.g. edn) for client<->server comms
23+
* packed - Arbitrary Clojure value serialized for client<->server comms
2524
* udt - Unix timestamp (datetime long)
2625
2726
Special messages:
@@ -517,9 +516,9 @@
517516
(have? vector? buffered-evs)
518517
(have? set? ev-uuids)
519518

520-
(let [buffered-evs-ppstr (pack packer buffered-evs)]
519+
(let [packed-buffered-evs (pack packer buffered-evs)]
521520
(send-buffered-server-evs>clients! conn-type
522-
conns_ uid buffered-evs-ppstr (count buffered-evs))))))]
521+
conns_ uid packed-buffered-evs (count buffered-evs))))))]
523522

524523
(if (= ev [:chsk/close]) ; Currently undocumented
525524
(do
@@ -643,9 +642,9 @@
643642
(fn [server-ch websocket?]
644643
(assert (not websocket?))
645644
(let [params (get ring-req :params)
646-
ppstr (get params :ppstr)
645+
packed (or (get params :packed) (get params :ppstr))
647646
client-id (get params :client-id)
648-
[clj has-cb?] (unpack packer ppstr)
647+
[clj has-cb?] (unpack packer packed)
649648
reply-fn
650649
(let [replied?_ (atom false)]
651650
(fn [resp-clj] ; Any clj form
@@ -727,13 +726,13 @@
727726
lid* error))
728727

729728
on-msg
730-
(fn [server-ch websocket? req-ppstr]
729+
(fn [server-ch websocket? packed]
731730
(assert websocket?)
732731
(swap-in! conns_ [:ws uid client-id]
733732
(fn [[?sch _udt conn-id]]
734733
(when conn-id [?sch (enc/now-udt) conn-id])))
735734

736-
(let [[clj ?cb-uuid] (unpack packer req-ppstr)]
735+
(let [[clj ?cb-uuid] (unpack packer packed)]
737736
;; clj should be ev
738737
(cond
739738
(= clj [:chsk/ws-pong]) (receive-event-msg! clj nil)
@@ -938,7 +937,7 @@
938937
(defn- send-buffered-server-evs>clients!
939938
"Actually pushes buffered events (as packed-str) to all uid's conns.
940939
Allows some time for possible reconnects."
941-
[conn-type conns_ uid buffered-evs-pstr n-buffered-evs]
940+
[conn-type conns_ uid packed-buffered-evs n-buffered-evs]
942941
(have? [:el #{:ajax :ws}] conn-type)
943942
(let [;; Mean max wait time: sum*1.5 = 2790*1.5 = 4.2s
944943
ms-backoffs [90 180 360 720 720 720] ; => max 1+6 attempts
@@ -955,7 +954,7 @@
955954
(when-let [[?sch _udt conn-id] (get-in @conns_ [conn-type uid client-id])]
956955
(when-let [sch ?sch]
957956
(when-not (simulated-bad-conn?)
958-
(when (interfaces/sch-send! sch websocket? buffered-evs-pstr)
957+
(when (interfaces/sch-send! sch websocket? packed-buffered-evs)
959958
conn-id))))]
960959

961960
(swap-in! conns_ [conn-type uid client-id]
@@ -1359,7 +1358,7 @@
13591358

13601359
;; TODO Buffer before sending (but honor `:flush?`)
13611360
(let [?cb-uuid (when ?cb-fn (enc/uuid-str 6))
1362-
ppstr (pack packer ev ?cb-uuid)]
1361+
packed (pack packer ev ?cb-uuid)]
13631362

13641363
(when-let [cb-uuid ?cb-uuid]
13651364
(reset-in! cbs-waiting_ [cb-uuid] (have ?cb-fn))
@@ -1372,8 +1371,8 @@
13721371
(or
13731372
(when-let [[s _sid] @socket_]
13741373
(try
1375-
#?(:cljs (.send s ppstr)
1376-
:clj (.send ^WebSocketClient s ^String ppstr))
1374+
#?(:cljs (.send s packed)
1375+
:clj (.send ^WebSocketClient s packed))
13771376

13781377
(reset! udt-last-comms_ (enc/now-udt))
13791378
:apparent-success
@@ -1435,15 +1434,10 @@
14351434
{:udt (enc/now-udt), :ex ex})))))
14361435

14371436
on-message ; Nb receives both push & cb evs!
1438-
(fn #?(:cljs [ws-ev] :clj [ppstr])
1439-
(let [ppstr #?(:clj ppstr
1440-
:cljs (enc/oget ws-ev "data"))
1441-
1442-
;; `clj` may/not satisfy `event?` since
1443-
;; we also receive cb replies here. This
1444-
;; is why we prefix pstrs to indicate
1445-
;; whether they're wrapped or not
1446-
[clj ?cb-uuid] (unpack packer ppstr)]
1437+
(fn #?(:cljs [ws-ev] :clj [packed])
1438+
(let [#?@[:cljs [packed (enc/oget ws-ev "data")]]
1439+
;; `clj` may/not satisfy `event?` since we also receive cb replies here
1440+
[clj ?cb-uuid] (unpack packer packed)]
14471441

14481442
(reset! udt-last-comms_ (enc/now-udt))
14491443

@@ -1615,14 +1609,14 @@
16151609
{:method :post
16161610
:timeout-ms (or ?timeout-ms (:timeout-ms ajax-opts)
16171611
default-client-side-ajax-timeout-ms)
1618-
:resp-type :text ; We'll do our own pstr decoding
1612+
:resp-type :text ; TODO Support binary?
16191613
:headers
16201614
(merge
16211615
(:headers ajax-opts) ; 1st (don't clobber impl.)
16221616
{:X-CSRF-Token csrf-token-str})
16231617

16241618
:params
1625-
(let [ppstr (pack packer ev (when ?cb-fn :ajax-cb))]
1619+
(let [packed (pack packer ev (when ?cb-fn :ajax-cb))]
16261620
(merge params ; 1st (don't clobber impl.):
16271621
{:udt (enc/now-udt) ; Force uncached resp
16281622

@@ -1635,7 +1629,7 @@
16351629
;; implementation:
16361630
:client-id client-id
16371631

1638-
:ppstr ppstr}))})
1632+
:packed packed}))})
16391633

16401634
(fn ajax-cb [{:keys [?error ?content]}]
16411635
(if ?error
@@ -1645,9 +1639,8 @@
16451639
(swap-chsk-state! chsk #(chsk-state->closed % :unexpected))
16461640
(when ?cb-fn (?cb-fn :chsk/error))))
16471641

1648-
(let [content ?content
1649-
resp-ppstr content
1650-
[resp-clj _] (unpack packer resp-ppstr)]
1642+
(let [packed ?content
1643+
[resp-clj _] (unpack packer packed)]
16511644
(if ?cb-fn
16521645
(?cb-fn resp-clj)
16531646
(when (not= resp-clj :chsk/dummy-cb-200)
@@ -1678,7 +1671,7 @@
16781671
{:method :get ; :timeout-ms timeout-ms
16791672
:timeout-ms (or (:timeout-ms ajax-opts)
16801673
default-client-side-ajax-timeout-ms)
1681-
:resp-type :text ; Prefer to do our own pstr reading
1674+
:resp-type :text ; TODO Support binary?
16821675
:xhr-cb-fn (fn [xhr] (reset! curr-xhr_ xhr))
16831676
:params
16841677
(merge
@@ -1714,9 +1707,8 @@
17141707
(retry-fn)))
17151708

17161709
;; The Ajax long-poller is used only for events, never cbs:
1717-
(let [content ?content
1718-
ppstr content
1719-
[clj] (unpack packer ppstr)
1710+
(let [packed ?content
1711+
[clj] (unpack packer packed)
17201712
handshake? (handshake? clj)]
17211713

17221714
(when handshake?

0 commit comments

Comments
 (0)