|
206 | 206 | ;; * Client<->server payloads are arbitrary Clojure vals (cb replies or events). |
207 | 207 | ;; * Payloads are packed for client<->server transit. |
208 | 208 |
|
209 | | -(defn- parse-packed |
210 | | - "Returns [<packed> <?format>]. Used to support some minimal backwards |
211 | | - compatibility between v2 `pack` and v1 `unpack`." |
212 | | - ;; TODO Remove this in a future ~breaking release |
213 | | - [packed] |
214 | | - (if (string? packed) |
215 | | - (cond |
216 | | - (enc/str-starts-with? packed "+") [(subs packed 1) :v1/wrapped] |
217 | | - (enc/str-starts-with? packed "-") [(subs packed 1) :v1/unwrapped] |
218 | | - :else [ packed :v2/unwrapped]) |
219 | | - [packed :v2/unwrapped])) |
220 | | - |
221 | | -(comment (parse-packed "+[[\"foo\"] \"uuid\"]")) |
222 | | - |
223 | 209 | (defn- unpack "packed->[clj ?cb-uuid]" |
224 | 210 | [packer packed] |
225 | | - (let [[packed ?format] (parse-packed packed) |
226 | | - unpacked ; [clj ?cb-uuid] |
| 211 | + (let [[clj ?cb-uuid] |
227 | 212 | (try |
228 | 213 | (interfaces/unpack packer packed) |
229 | 214 | (catch #?(:clj Throwable :cljs :default) t |
230 | 215 | (timbre/errorf t "Failed to unpack: %s" packed) |
231 | 216 | [[:chsk/bad-package packed] nil])) |
232 | 217 |
|
233 | | - [clj ?cb-uuid] |
234 | | - (case ?format |
235 | | - :v1/wrapped unpacked |
236 | | - :v1/unwrapped [unpacked nil] |
237 | | - :v2/unwrapped unpacked) |
238 | | - |
239 | 218 | ?cb-uuid (if (= 0 ?cb-uuid) :ajax-cb ?cb-uuid)] |
240 | 219 |
|
241 | 220 | [clj ?cb-uuid])) |
242 | 221 |
|
243 | | -(def ^:dynamic *write-legacy-pack-format?* |
244 | | - "Advanced option, most users can ignore this var. Only necessary |
245 | | - for those that want to use Sente < v1.18 with a non-standard |
246 | | - IPacker that deals with non-string payloads. |
247 | | -
|
248 | | - Details: |
249 | | - Sente uses a private message format as an implementation detail |
250 | | - for client<->server comms. |
251 | | -
|
252 | | - As part of [#398], this format is being updated to support |
253 | | - non-string (e.g. binary) payloads. |
254 | | -
|
255 | | - Unfortunately updating the format is non-trivial because: |
256 | | - 1. Both the client & server need to support the same format. |
257 | | - 2. Clients are often served as cached cl/js. |
258 | | -
|
259 | | - To help ease migration, the new pack format is being rolled out |
260 | | - in stages: |
261 | | -
|
262 | | - Sente <= v1.16: reads v1 format only |
263 | | - writes v1 format only |
264 | | -
|
265 | | - Sente v1.17: reads v1 and v2 formats |
266 | | - writes v1 and v2 formats (v1 default) |
267 | | -
|
268 | | - Sente v1.18: reads v1 and v2 formats |
269 | | - writes v1 and v2 formats (v2 default) <- Currently here |
270 | | -
|
271 | | - Sente >= v1.19: reads v2 format only |
272 | | - writes v2 format only |
273 | | -
|
274 | | - This var controls which format to use for writing. |
275 | | - Override default with `alter-var-root` or `binding`." |
276 | | - |
277 | | - false) |
278 | | - |
279 | 222 | (defn- pack "[clj ?cb-uuid]->packed" |
280 | 223 | ([packer clj ] (pack packer clj nil)) |
281 | 224 | ([packer clj ?cb-uuid] |
282 | | - (let [?cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid) |
283 | | - packed |
284 | | - (interfaces/pack packer |
285 | | - (if-some [cb-uuid ?cb-uuid] |
286 | | - [clj cb-uuid] |
287 | | - [clj ]))] |
288 | | - |
289 | | - (if *write-legacy-pack-format?* |
290 | | - (str "+" (have string? packed)) |
291 | | - (do packed))))) |
| 225 | + (let [?cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)] |
| 226 | + (interfaces/pack packer |
| 227 | + (if-some [cb-uuid ?cb-uuid] |
| 228 | + [clj cb-uuid] |
| 229 | + [clj ]))))) |
292 | 230 |
|
293 | 231 | (comment |
294 | 232 | (unpack default-edn-packer |
295 | | - (binding [*write-legacy-pack-format?* true] |
296 | | - (pack default-edn-packer [:foo])))) |
| 233 | + (pack default-edn-packer [:foo]))) |
297 | 234 |
|
298 | 235 | (deftype EdnPacker [] |
299 | 236 | interfaces/IPacker |
|
0 commit comments