Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit 4182211

Browse files
authored
fix: allow overwriting tags (#68)
Allows setting the same tag twice, last update wins. Otherwise trying to ensure atomic writes becomes very hard.
1 parent 0b5e25f commit 4182211

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,8 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> implement
135135
tags = Tags.decode(buf).tags
136136
}
137137

138-
for (const t of tags) {
139-
if (t.name === tag) {
140-
throw new CodeError('Peer already tagged', 'ERR_DUPLICATE_TAG')
141-
}
142-
}
138+
// do not allow duplicate tags
139+
tags = tags.filter(t => t.name !== tag)
143140

144141
tags.push({
145142
name: tag,

test/peer-store.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,18 @@ describe('peer-store', () => {
288288

289289
it('does not tag a peer twice', async () => {
290290
const name = 'a-tag'
291-
await peerStore.tagPeer(peerIds[0], name)
291+
await peerStore.tagPeer(peerIds[0], name, {
292+
value: 1
293+
})
294+
await peerStore.tagPeer(peerIds[0], name, {
295+
value: 10
296+
})
297+
298+
const allTags = await peerStore.getTags(peerIds[0])
299+
const tags = allTags.filter(t => t.name === name)
292300

293-
await expect(peerStore.tagPeer(peerIds[0], name), 'PeerStore allowed duplicate tags')
294-
.to.eventually.be.rejected().with.property('code', 'ERR_DUPLICATE_TAG')
301+
expect(tags).to.have.lengthOf(1)
302+
expect(tags).to.have.nested.property('[0].value', 10)
295303
})
296304

297305
it('untags a peer', async () => {

0 commit comments

Comments
 (0)