-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustomScalarHandlers.ts
29 lines (26 loc) · 979 Bytes
/
customScalarHandlers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { ByteIterator } from '../iterator'
import { ScalarHandlers } from '../scalarHandlers'
const length = 4
const customScalarHandlers: ScalarHandlers = {
// Date with seconds precision spanning from Jan 01 1970 till ~Feb 07 2106
// that takes up a 4 byte integer unlike JSON representation which would
// take 10 bytes for seconds or 13 bytes for milliseconds timestamp.
// It is also nicely decoded directly to Date() object
UDateS: {
decode: (data: ByteIterator) => {
const subset = data.take(length)
const view = new DataView(subset.buffer)
return new Date(view.getInt32(0) * 1000)
},
encode: (data: Date) => {
const timestampSeconds = data.getTime() / 1000
return new Uint8Array([
(timestampSeconds & 0xff000000) >> 24,
(timestampSeconds & 0x00ff0000) >> 16,
(timestampSeconds & 0x0000ff00) >> 8,
timestampSeconds & 0x000000ff
])
}
}
}
export default customScalarHandlers