This commit is contained in:
Raven Scott
2026-05-20 22:28:59 -04:00
parent 341542f41b
commit e4400872c0
82 changed files with 2533 additions and 775 deletions
@@ -1,7 +1,8 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const b4a = require('b4a')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
const PROTOCOL = 'document-line-lock/v1'
@@ -10,50 +11,89 @@ class HyperP2PDocumentLineLock extends EventEmitter {
super()
this.topic = opts.topic || null
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._store = new Map()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.holderHex = b4a.toString(this.keyPair.publicKey, 'hex')
this._locks = new Map()
this._stats = { acquired: 0, released: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
this._peerMsgs = null
}
put (key, value) {
assertNonEmpty(key, 'key')
this._store.set(key, value)
this._stats.ops++
sendGossip(this, { type: 'document-line-lock-sync', key, value })
this.emit('update', { key, value })
_key (docId, line) {
return `${docId}:${line | 0}`
}
acquire (docId, line) {
assertNonEmpty(docId, 'docId')
const key = this._key(docId, line)
const existing = this._locks.get(key)
if (existing && existing.holder !== this.holderHex) {
return { ok: false, holder: existing.holder }
}
const lock = { docId, line: line | 0, holder: this.holderHex, at: Date.now() }
this._locks.set(key, lock)
this._stats.acquired++
if (this._peerMsgs) {
gossipSend(this, { type: 'line-lock', lock })
this._stats.gossipOut++
}
this.emit('acquire', lock)
return { ok: true, ...lock }
}
release (docId, line) {
const key = this._key(docId, line)
const lock = this._locks.get(key)
if (!lock || lock.holder !== this.holderHex) return false
this._locks.delete(key)
this._stats.released++
if (this._peerMsgs) {
gossipSend(this, { type: 'line-unlock', docId, line: line | 0, holder: this.holderHex })
this._stats.gossipOut++
}
this.emit('release', { docId, line })
return true
}
get (key) { return this._store.get(key) }
delete (key) {
const ok = this._store.delete(key)
if (ok) sendGossip(this, { type: 'document-line-lock-sync', key, value: null })
return ok
isLocked (docId, line) {
return this._locks.has(this._key(docId, line))
}
entries () { return [...this._store.entries()] }
getLock (docId, line) {
return this._locks.get(this._key(docId, line)) || null
}
_onGossip (d) {
if (!d || d.type !== 'document-line-lock-sync') return
_onGossip (data) {
if (!data) return
this._stats.gossipIn++
if (d.key !== undefined) {
if (d.value === null) this._store.delete(d.key)
else this._store.set(d.key, d.value)
if (data.type === 'line-lock' && data.lock) {
this._locks.set(this._key(data.lock.docId, data.lock.line), data.lock)
this.emit('remote-acquire', data.lock)
}
if (data.type === 'line-unlock') {
this._locks.delete(this._key(data.docId, data.line))
this.emit('remote-release', data)
}
}
getStats () { return { ...this._stats, size: this._store.size, protocol: PROTOCOL } }
getStats () {
return { ...this._stats, locks: this._locks.size, protocol: PROTOCOL }
}
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
await initModuleSwarm(this, {
keyPair: this.keyPair,
topic: this.topic,
protocol: PROTOCOL,
onmessage: (d) => this._onGossip(d)
})
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
this._locks.clear()
}
}
@@ -4,23 +4,34 @@ const { HyperP2PDocumentLineLock, PROTOCOL } = require('../index.js')
test('exports', (t) => {
t.ok(HyperP2PDocumentLineLock)
t.ok(PROTOCOL)
t.is(PROTOCOL, 'document-line-lock/v1')
})
test('basic operation', async (t) => {
test('acquire and release', async (t) => {
const m = new HyperP2PDocumentLineLock()
m.put('k', 1); t.is(m.get('k'), 1)
const r = m.acquire('doc', 5)
t.ok(r.ok)
t.ok(m.isLocked('doc', 5))
t.ok(m.release('doc', 5))
await m.close()
})
test('conflict', async (t) => {
const m = new HyperP2PDocumentLineLock()
m._onGossip({ type: 'line-lock', lock: { docId: 'd', line: 1, holder: 'other', at: 1 } })
const r = m.acquire('d', 1)
t.is(r.ok, false)
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PDocumentLineLock()
try { m.put(null, 1) } catch (e) { t.ok(e) }
try { m.acquire('', 0) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PDocumentLineLock()
t.ok(m.getStats().protocol)
t.is(m.getStats().protocol, 'document-line-lock/v1')
await m.close()
})