Skip to content

Commit d4ac38e

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 d4ac38e

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

src/taoensso/sente.cljc

Lines changed: 32 additions & 39 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:
@@ -203,7 +202,8 @@
203202
;; * Client<->server payloads are arbitrary Clojure vals (cb replies or events).
204203
;; * Payloads are packed for client<->server transit.
205204

206-
(defn- unpack "packed->[clj ?cb-uuid]"
205+
(defn- unpack
206+
"packed->[clj ?cb-uuid]"
207207
[packer packed]
208208
(let [[clj ?cb-uuid]
209209
(try
@@ -216,14 +216,14 @@
216216

217217
[clj ?cb-uuid]))
218218

219-
(defn- pack "[clj ?cb-uuid]->packed"
219+
(defn- pack
220+
"[clj ?cb-uuid]->packed"
220221
([packer clj ] (pack packer clj nil))
221222
([packer clj ?cb-uuid]
222-
(let [?cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)]
223-
(interfaces/pack packer
224-
(if-some [cb-uuid ?cb-uuid]
225-
[clj cb-uuid]
226-
[clj ])))))
223+
(interfaces/pack packer
224+
(if-some [cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)]
225+
[clj cb-uuid]
226+
[clj ]))))
227227

228228
(comment
229229
(unpack default-edn-packer
@@ -517,9 +517,9 @@
517517
(have? vector? buffered-evs)
518518
(have? set? ev-uuids)
519519

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

524524
(if (= ev [:chsk/close]) ; Currently undocumented
525525
(do
@@ -643,9 +643,9 @@
643643
(fn [server-ch websocket?]
644644
(assert (not websocket?))
645645
(let [params (get ring-req :params)
646-
ppstr (get params :ppstr)
646+
packed (or (get params :packed) (get params :ppstr))
647647
client-id (get params :client-id)
648-
[clj has-cb?] (unpack packer ppstr)
648+
[clj has-cb?] (unpack packer packed)
649649
reply-fn
650650
(let [replied?_ (atom false)]
651651
(fn [resp-clj] ; Any clj form
@@ -727,13 +727,13 @@
727727
lid* error))
728728

729729
on-msg
730-
(fn [server-ch websocket? req-ppstr]
730+
(fn [server-ch websocket? packed]
731731
(assert websocket?)
732732
(swap-in! conns_ [:ws uid client-id]
733733
(fn [[?sch _udt conn-id]]
734734
(when conn-id [?sch (enc/now-udt) conn-id])))
735735

736-
(let [[clj ?cb-uuid] (unpack packer req-ppstr)]
736+
(let [[clj ?cb-uuid] (unpack packer packed)]
737737
;; clj should be ev
738738
(cond
739739
(= clj [:chsk/ws-pong]) (receive-event-msg! clj nil)
@@ -938,7 +938,7 @@
938938
(defn- send-buffered-server-evs>clients!
939939
"Actually pushes buffered events (as packed-str) to all uid's conns.
940940
Allows some time for possible reconnects."
941-
[conn-type conns_ uid buffered-evs-pstr n-buffered-evs]
941+
[conn-type conns_ uid packed-buffered-evs n-buffered-evs]
942942
(have? [:el #{:ajax :ws}] conn-type)
943943
(let [;; Mean max wait time: sum*1.5 = 2790*1.5 = 4.2s
944944
ms-backoffs [90 180 360 720 720 720] ; => max 1+6 attempts
@@ -955,7 +955,7 @@
955955
(when-let [[?sch _udt conn-id] (get-in @conns_ [conn-type uid client-id])]
956956
(when-let [sch ?sch]
957957
(when-not (simulated-bad-conn?)
958-
(when (interfaces/sch-send! sch websocket? buffered-evs-pstr)
958+
(when (interfaces/sch-send! sch websocket? packed-buffered-evs)
959959
conn-id))))]
960960

961961
(swap-in! conns_ [conn-type uid client-id]
@@ -1359,7 +1359,7 @@
13591359

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

13641364
(when-let [cb-uuid ?cb-uuid]
13651365
(reset-in! cbs-waiting_ [cb-uuid] (have ?cb-fn))
@@ -1372,8 +1372,8 @@
13721372
(or
13731373
(when-let [[s _sid] @socket_]
13741374
(try
1375-
#?(:cljs (.send s ppstr)
1376-
:clj (.send ^WebSocketClient s ^String ppstr))
1375+
#?(:cljs (.send s packed)
1376+
:clj (.send ^WebSocketClient s packed))
13771377

13781378
(reset! udt-last-comms_ (enc/now-udt))
13791379
:apparent-success
@@ -1435,15 +1435,10 @@
14351435
{:udt (enc/now-udt), :ex ex})))))
14361436

14371437
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)]
1438+
(fn #?(:cljs [ws-ev] :clj [packed])
1439+
(let [#?@(:cljs [packed (enc/oget ws-ev "data")])
1440+
;; `clj` may/not satisfy `event?` since we also receive cb replies here
1441+
[clj ?cb-uuid] (unpack packer packed)]
14471442

14481443
(reset! udt-last-comms_ (enc/now-udt))
14491444

@@ -1615,14 +1610,14 @@
16151610
{:method :post
16161611
:timeout-ms (or ?timeout-ms (:timeout-ms ajax-opts)
16171612
default-client-side-ajax-timeout-ms)
1618-
:resp-type :text ; We'll do our own pstr decoding
1613+
:resp-type :text ; TODO Support binary?
16191614
:headers
16201615
(merge
16211616
(:headers ajax-opts) ; 1st (don't clobber impl.)
16221617
{:X-CSRF-Token csrf-token-str})
16231618

16241619
:params
1625-
(let [ppstr (pack packer ev (when ?cb-fn :ajax-cb))]
1620+
(let [packed (pack packer ev (when ?cb-fn :ajax-cb))]
16261621
(merge params ; 1st (don't clobber impl.):
16271622
{:udt (enc/now-udt) ; Force uncached resp
16281623

@@ -1635,7 +1630,7 @@
16351630
;; implementation:
16361631
:client-id client-id
16371632

1638-
:ppstr ppstr}))})
1633+
:packed packed}))})
16391634

16401635
(fn ajax-cb [{:keys [?error ?content]}]
16411636
(if ?error
@@ -1645,9 +1640,8 @@
16451640
(swap-chsk-state! chsk #(chsk-state->closed % :unexpected))
16461641
(when ?cb-fn (?cb-fn :chsk/error))))
16471642

1648-
(let [content ?content
1649-
resp-ppstr content
1650-
[resp-clj _] (unpack packer resp-ppstr)]
1643+
(let [packed ?content
1644+
[resp-clj _] (unpack packer packed)]
16511645
(if ?cb-fn
16521646
(?cb-fn resp-clj)
16531647
(when (not= resp-clj :chsk/dummy-cb-200)
@@ -1678,7 +1672,7 @@
16781672
{:method :get ; :timeout-ms timeout-ms
16791673
:timeout-ms (or (:timeout-ms ajax-opts)
16801674
default-client-side-ajax-timeout-ms)
1681-
:resp-type :text ; Prefer to do our own pstr reading
1675+
:resp-type :text ; TODO Support binary?
16821676
:xhr-cb-fn (fn [xhr] (reset! curr-xhr_ xhr))
16831677
:params
16841678
(merge
@@ -1714,9 +1708,8 @@
17141708
(retry-fn)))
17151709

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

17221715
(when handshake?

0 commit comments

Comments
 (0)