-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.ts
68 lines (52 loc) · 2.03 KB
/
event.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as capnp from '@jkomyno/capnp-ts'
// @deno-types='./capnp/event.capnp.d.ts'
import { Event as EventRoot } from './capnp/event.capnp.js'
// @deno-types='./wasm/event_capnp.d.ts'
import { /* createEvent, */ modifyEvent, readEvent, type Event } from './wasm/event_capnp.js'
import { xxd } from './utils/xxd.ts'
import { computeSHA } from './utils/sha.ts'
function createEvent(event: Event): Uint8Array {
const message = new capnp.Message()
const root = message.initRoot(EventRoot)
root.setName(event.name)
root.setYear(event.year)
const arrayBuffer = message.toArrayBuffer()
return new Uint8Array(arrayBuffer)
}
async function main() {
const eventJS = {
name: 'trivago',
year: 2023,
}
console.log('eventJS', eventJS, '\n')
const eventAsBinary = createEvent(eventJS)
console.log('eventAsBinary')
console.log(xxd(eventAsBinary), '\n')
const eventAsPlainJS = readEvent(eventAsBinary)
console.log('eventAsPlainJS', eventAsPlainJS, '\n')
const modifiedEventAsBinary = eventAsBinary.slice()
modifyEvent(modifiedEventAsBinary)
console.log('modifiedEventAsBinary')
console.log(xxd(modifiedEventAsBinary), '\n')
const modifiedEventAsPlainJS = readEvent(modifiedEventAsBinary)
console.log('modifiedEventAsPlainJS', modifiedEventAsPlainJS, '\n')
// {
// const array = new Uint8Array([0xe8, 0x07])
// const data = new DataView(array.buffer)
// console.log(data.getUint16(0, true))
// }
const eventBinaryPath = './bin/event.bin'
try {
await Deno.stat(eventBinaryPath)
console.log(`Found previous binary in ${eventBinaryPath}`)
const oldEventAsBinary = await Deno.readFile(eventBinaryPath)
const shaOldEventAsBinary = await computeSHA(oldEventAsBinary)
const shaNewEventAsBinary = await computeSHA(eventAsBinary)
console.log('SHA-256 match?', shaOldEventAsBinary === shaNewEventAsBinary)
} catch (_) {
// File does not exist, so we create it
console.log(`Writing binary to ${eventBinaryPath}`)
await Deno.writeFile(eventBinaryPath, eventAsBinary)
}
}
main()