This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
pubsub.ts
196 lines (160 loc) · 5.93 KB
/
pubsub.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { CodeError } from '@libp2p/interface'
import { logger } from '@libp2p/logger'
import { peerIdToRoutingKey } from 'ipns'
import { ipnsSelector } from 'ipns/selector'
import { ipnsValidator } from 'ipns/validator'
import { CustomProgressEvent, type ProgressEvent } from 'progress-events'
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { localStore, type LocalStore } from './local-store.js'
import type { GetOptions, IPNSRouting, PutOptions } from './index.js'
import type { PeerId, Message, PublishResult, PubSub } from '@libp2p/interface'
import type { Datastore } from 'interface-datastore'
const log = logger('helia:ipns:routing:pubsub')
export interface PubsubRoutingComponents {
datastore: Datastore
libp2p: {
peerId: PeerId
services: {
pubsub: PubSub
}
}
}
export type PubSubProgressEvents =
ProgressEvent<'ipns:pubsub:publish', { topic: string, result: PublishResult }> |
ProgressEvent<'ipns:pubsub:subscribe', { topic: string }> |
ProgressEvent<'ipns:pubsub:error', Error>
class PubSubRouting implements IPNSRouting {
private subscriptions: string[]
private readonly localStore: LocalStore
private readonly peerId: PeerId
private readonly pubsub: PubSub
constructor (components: PubsubRoutingComponents) {
this.subscriptions = []
this.localStore = localStore(components.datastore)
this.peerId = components.libp2p.peerId
this.pubsub = components.libp2p.services.pubsub
this.pubsub.addEventListener('message', (evt) => {
const message = evt.detail
if (!this.subscriptions.includes(message.topic)) {
return
}
this.#processPubSubMessage(message).catch(err => {
log.error('Error processing message', err)
})
})
}
async #processPubSubMessage (message: Message): Promise<void> {
log('message received for topic', message.topic)
if (message.type !== 'signed') {
log.error('unsigned message received, this module can only work with signed messages')
return
}
if (message.from.equals(this.peerId)) {
log('not storing record from self')
return
}
const routingKey = topicToKey(message.topic)
await ipnsValidator(routingKey, message.data)
if (await this.localStore.has(routingKey)) {
const currentRecord = await this.localStore.get(routingKey)
if (uint8ArrayEquals(currentRecord, message.data)) {
log('not storing record as we already have it')
return
}
const records = [currentRecord, message.data]
const index = ipnsSelector(routingKey, records)
if (index === 0) {
log('not storing record as the one we have is better')
return
}
}
await this.localStore.put(routingKey, message.data)
}
/**
* Put a value to the pubsub datastore indexed by the received key properly encoded
*/
async put (routingKey: Uint8Array, marshaledRecord: Uint8Array, options: PutOptions = {}): Promise<void> {
try {
const topic = keyToTopic(routingKey)
log('publish value for topic %s', topic)
const result = await this.pubsub.publish(topic, marshaledRecord)
log('published record on topic %s to %d recipients', topic, result.recipients)
options.onProgress?.(new CustomProgressEvent('ipns:pubsub:publish', { topic, result }))
} catch (err: any) {
options.onProgress?.(new CustomProgressEvent<Error>('ipns:pubsub:error', err))
throw err
}
}
/**
* Get a value from the pubsub datastore indexed by the received key properly encoded.
* Also, the identifier topic is subscribed to and the pubsub datastore records will be
* updated once new publishes occur
*/
async get (routingKey: Uint8Array, options: GetOptions = {}): Promise<Uint8Array> {
try {
const topic = keyToTopic(routingKey)
// ensure we are subscribed to topic
if (!this.pubsub.getTopics().includes(topic)) {
log('add subscription for topic', topic)
this.pubsub.subscribe(topic)
this.subscriptions.push(topic)
options.onProgress?.(new CustomProgressEvent('ipns:pubsub:subscribe', { topic }))
}
// chain through to local store
return await this.localStore.get(routingKey, options)
} catch (err: any) {
options.onProgress?.(new CustomProgressEvent<Error>('ipns:pubsub:error', err))
throw err
}
}
/**
* Get pubsub subscriptions related to ipns
*/
getSubscriptions (): string[] {
return this.subscriptions
}
/**
* Cancel pubsub subscriptions related to ipns
*/
cancel (key: PeerId): void {
const routingKey = peerIdToRoutingKey(key)
const topic = keyToTopic(routingKey)
// Not found topic
if (!this.subscriptions.includes(topic)) {
return
}
this.pubsub.unsubscribe(topic)
this.subscriptions = this.subscriptions.filter(t => t !== topic)
}
}
const PUBSUB_NAMESPACE = '/record/'
/**
* converts a binary record key to a pubsub topic key
*/
function keyToTopic (key: Uint8Array): string {
const b64url = uint8ArrayToString(key, 'base64url')
return `${PUBSUB_NAMESPACE}${b64url}`
}
/**
* converts a pubsub topic key to a binary record key
*/
function topicToKey (topic: string): Uint8Array {
if (topic.substring(0, PUBSUB_NAMESPACE.length) !== PUBSUB_NAMESPACE) {
throw new CodeError('topic received is not from a record', 'ERR_TOPIC_IS_NOT_FROM_RECORD_NAMESPACE')
}
const key = topic.substring(PUBSUB_NAMESPACE.length)
return uint8ArrayFromString(key, 'base64url')
}
/**
* This IPNS routing receives IPNS record updates via dedicated
* pubsub topic.
*
* Note we must first be subscribed to the topic in order to receive
* updated records, so the first call to `.get` should be expected
* to fail!
*/
export function pubsub (components: PubsubRoutingComponents): IPNSRouting {
return new PubSubRouting(components)
}