This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
/
index.ts
70 lines (57 loc) · 1.62 KB
/
index.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
69
70
import {
Record
} from './record.js'
import * as utils from './utils.js'
import type { Uint8ArrayList } from 'uint8arraylist'
export class Libp2pRecord {
public key: Uint8Array
public value: Uint8Array
public timeReceived: Date
constructor (key: Uint8Array, value: Uint8Array, timeReceived: Date) {
if (!(key instanceof Uint8Array)) {
throw new Error('key must be a Uint8Array')
}
if (!(value instanceof Uint8Array)) {
throw new Error('value must be a Uint8Array')
}
this.key = key
this.value = value
this.timeReceived = timeReceived
}
serialize (): Uint8Array {
return Record.encode(this.prepareSerialize())
}
/**
* Return the object format ready to be given to the protobuf library.
*/
prepareSerialize (): Record {
return {
key: this.key,
value: this.value,
timeReceived: utils.toRFC3339(this.timeReceived)
}
}
/**
* Decode a protobuf encoded record
*/
static deserialize (raw: Uint8Array | Uint8ArrayList): Libp2pRecord {
const rec = Record.decode(raw)
return new Libp2pRecord(rec.key, rec.value, new Date(rec.timeReceived))
}
/**
* Create a record from the raw object returned from the protobuf library
*/
static fromDeserialized (obj: Record): Libp2pRecord {
const recvtime = utils.parseRFC3339(obj.timeReceived)
if (obj.key == null) {
throw new Error('key missing from deserialized object')
}
if (obj.value == null) {
throw new Error('value missing from deserialized object')
}
const rec = new Libp2pRecord(
obj.key, obj.value, recvtime
)
return rec
}
}