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

Commit

Permalink
fix: allow overwriting tags (#68)
Browse files Browse the repository at this point in the history
Allows setting the same tag twice, last update wins. Otherwise trying
to ensure atomic writes becomes very hard.
  • Loading branch information
achingbrain authored Mar 2, 2023
1 parent 0b5e25f commit 4182211
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,8 @@ export class PersistentPeerStore extends EventEmitter<PeerStoreEvents> implement
tags = Tags.decode(buf).tags
}

for (const t of tags) {
if (t.name === tag) {
throw new CodeError('Peer already tagged', 'ERR_DUPLICATE_TAG')
}
}
// do not allow duplicate tags
tags = tags.filter(t => t.name !== tag)

tags.push({
name: tag,
Expand Down
14 changes: 11 additions & 3 deletions test/peer-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,18 @@ describe('peer-store', () => {

it('does not tag a peer twice', async () => {
const name = 'a-tag'
await peerStore.tagPeer(peerIds[0], name)
await peerStore.tagPeer(peerIds[0], name, {
value: 1
})
await peerStore.tagPeer(peerIds[0], name, {
value: 10
})

const allTags = await peerStore.getTags(peerIds[0])
const tags = allTags.filter(t => t.name === name)

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

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

0 comments on commit 4182211

Please sign in to comment.