You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: lib/once.ex
+40-5
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,24 @@ defmodule Once do
13
13
- `:nonce_type` how the nonce is generated, one of `t:nonce_type/0` (default `:counter`)
14
14
- `:get_key` a zero-arity getter for the 192-bits encryption key, required if encryption is enabled
15
15
- `:encrypt?` **deprecated**, use `type: :encrypted` (default `false`).
16
+
17
+
## Integer format caveats
18
+
19
+
> #### Don't use raw integers with JS clients {: .warning}
20
+
>
21
+
> Stringify int-format Once's.
22
+
23
+
While JSON does not impose a precision limit on numbers, JavaScript can't deal with >= 2^53 numbers. That means the first 11 nonce bits can't be used, so the first 11 timestamp bits can't be used, which leaves 33 timestamp bits, which will run out after exactly 24 days, so let's say immediately. If you want to use integers, convert them to strings.
> If you use an integer format as `:ex_format`, casting and dumping hex-encoded, url64-encoded and raw formats will be disabled.
28
+
29
+
That's because we can't disambiguate some binaries that are valid hex, url64 and raw binaries and also valid stringified integers. An example is "12345678901", which is either int 12_345_678_901 or url64-encoded `<<215, 109, 248, 231, 174, 252, 247, 77>>` (a.k.a. quite a different number).
30
+
31
+
By treating all incoming binaries as either a valid stringified int or invalid when using an integer Elixir format, this ambiguity is resolved at the cost of some flexibility. Note that `to_format/2` does *not* support stringified integers, but that does mean it converts reliably between formats once values have been cast/dumped/loaded.
32
+
33
+
Conversely, when using one of the binary formats, no binaries will be parsed as stringified ints.
16
34
"""
17
35
18
36
@moduledoc"""
@@ -66,7 +84,7 @@ defmodule Once do
66
84
67
85
The negative integers will not cause problems with Postgres and MySQL, they both happily swallow them. Also, negative integers will only start to appear after ~70 years of usage.
68
86
69
-
If you don't like the formats, it's really easy to change them! The Elixir format especially, which can be changed at any time. Be mindful of JSON limitations if you use integers.
87
+
If you don't like the formats, it's really easy to change them! The Elixir format especially, which can be changed at any time.
0 commit comments