mirror of https://codeberg.org/pzp/pzp-dict.git
change API to not require account ID
This commit is contained in:
parent
2d890eaba0
commit
cc7cf9858d
40
lib/index.js
40
lib/index.js
|
@ -258,7 +258,7 @@ function initRecord(peer, config) {
|
||||||
const mootID = MsgV3.getMootID(accountID, fromSubdomain(subdomain))
|
const mootID = MsgV3.getMootID(accountID, fromSubdomain(subdomain))
|
||||||
const tangle = peer.db.getTangle(mootID)
|
const tangle = peer.db.getTangle(mootID)
|
||||||
const maxDepth = tangle.maxDepth
|
const maxDepth = tangle.maxDepth
|
||||||
const fieldRoots = getFieldRoots(accountID, subdomain)
|
const fieldRoots = getFieldRoots(subdomain)
|
||||||
let minDepth = Infinity
|
let minDepth = Infinity
|
||||||
for (const field in fieldRoots) {
|
for (const field in fieldRoots) {
|
||||||
for (const msgID of fieldRoots[field]) {
|
for (const msgID of fieldRoots[field]) {
|
||||||
|
@ -326,12 +326,10 @@ function initRecord(peer, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} id
|
|
||||||
* @param {string} subdomain
|
* @param {string} subdomain
|
||||||
*/
|
*/
|
||||||
function getFieldRoots(id, subdomain) {
|
function getFieldRoots(subdomain) {
|
||||||
// prettier-ignore
|
if (!accountID) throw new Error('Cannot getFieldRoots() before loading')
|
||||||
if (id !== accountID) throw new Error(`Cannot getFieldRoots for another user's record. Given ID was "${id}"`)
|
|
||||||
return fieldRoots.getAll(subdomain)
|
return fieldRoots.getAll(subdomain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,13 +337,17 @@ function initRecord(peer, config) {
|
||||||
* @public
|
* @public
|
||||||
* @param {string} id
|
* @param {string} id
|
||||||
* @param {string} subdomain
|
* @param {string} subdomain
|
||||||
|
* @returns {Record<string, any> | null}
|
||||||
*/
|
*/
|
||||||
function get(id, subdomain) {
|
function read(id, subdomain) {
|
||||||
assertDBExists(peer)
|
assertDBExists(peer)
|
||||||
const domain = fromSubdomain(subdomain)
|
const domain = fromSubdomain(subdomain)
|
||||||
const mootID = MsgV3.getMootID(id, domain)
|
const mootID = MsgV3.getMootID(id, domain)
|
||||||
const tangle = peer.db.getTangle(mootID)
|
const tangle = peer.db.getTangle(mootID)
|
||||||
if (!tangle || tangle.size === 0) return {}
|
if (!tangle || tangle.size === 0) {
|
||||||
|
if (id === accountID) return {}
|
||||||
|
else return null
|
||||||
|
}
|
||||||
const msgIDs = tangle.topoSort()
|
const msgIDs = tangle.topoSort()
|
||||||
const record = /** @type {Record<string, any>}*/ ({})
|
const record = /** @type {Record<string, any>}*/ ({})
|
||||||
for (const msgID of msgIDs) {
|
for (const msgID of msgIDs) {
|
||||||
|
@ -360,17 +362,18 @@ function initRecord(peer, config) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
* @param {string} id
|
|
||||||
* @param {string} subdomain
|
* @param {string} subdomain
|
||||||
* @param {Record<string, any>} update
|
* @param {Record<string, any>} update
|
||||||
* @param {CB<boolean>} cb
|
* @param {CB<boolean>} cb
|
||||||
*/
|
*/
|
||||||
function update(id, subdomain, update, cb) {
|
function update(subdomain, update, cb) {
|
||||||
// prettier-ignore
|
if (!accountID) return cb(new Error('Cannot update before loading'))
|
||||||
if (id !== accountID) return cb(new Error(`Cannot update another user's record. Given ID was "${id}"`))
|
|
||||||
|
|
||||||
loaded(() => {
|
loaded(() => {
|
||||||
const record = get(id, subdomain)
|
if (!accountID) return cb(new Error('Expected account to be loaded'))
|
||||||
|
const record = read(accountID, subdomain)
|
||||||
|
// prettier-ignore
|
||||||
|
if (!record) return cb(new Error(`Cannot update non-existent record "${subdomain}`))
|
||||||
|
|
||||||
let hasChanges = false
|
let hasChanges = false
|
||||||
for (const [field, value] of Object.entries(update)) {
|
for (const [field, value] of Object.entries(update)) {
|
||||||
|
@ -385,18 +388,19 @@ function initRecord(peer, config) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} id
|
|
||||||
* @param {string} subdomain
|
* @param {string} subdomain
|
||||||
* @param {CB<boolean>} cb
|
* @param {CB<boolean>} cb
|
||||||
*/
|
*/
|
||||||
function squeeze(id, subdomain, cb) {
|
function squeeze(subdomain, cb) {
|
||||||
// prettier-ignore
|
if (!accountID) return cb(new Error('Cannot squeeze before loading'))
|
||||||
if (id !== accountID) return cb(new Error(`Cannot squeeze another user's record. Given ID was "${id}"`))
|
|
||||||
const potential = _squeezePotential(subdomain)
|
const potential = _squeezePotential(subdomain)
|
||||||
if (potential < 1) return cb(null, false)
|
if (potential < 1) return cb(null, false)
|
||||||
|
|
||||||
loaded(() => {
|
loaded(() => {
|
||||||
const record = get(id, subdomain)
|
if (!accountID) return cb(new Error('Expected account to be loaded'))
|
||||||
|
const record = read(accountID, subdomain)
|
||||||
|
// prettier-ignore
|
||||||
|
if (!record) return cb(new Error(`Cannot squeeze non-existent record "${subdomain}`))
|
||||||
forceUpdate(subdomain, record, (err, _forceUpdated) => {
|
forceUpdate(subdomain, record, (err, _forceUpdated) => {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (err) return cb(new Error('Failed to force update when squeezing Record', { cause: err }))
|
if (err) return cb(new Error('Failed to force update when squeezing Record', { cause: err }))
|
||||||
|
@ -410,7 +414,7 @@ function initRecord(peer, config) {
|
||||||
return {
|
return {
|
||||||
load,
|
load,
|
||||||
update,
|
update,
|
||||||
get,
|
read,
|
||||||
getFieldRoots,
|
getFieldRoots,
|
||||||
squeeze,
|
squeeze,
|
||||||
|
|
||||||
|
|
|
@ -29,22 +29,22 @@ test('setup', async (t) => {
|
||||||
|
|
||||||
test('Record update() and get()', async (t) => {
|
test('Record update() and get()', async (t) => {
|
||||||
assert(
|
assert(
|
||||||
await p(peer.record.update)(aliceID, 'profile', { name: 'alice' }),
|
await p(peer.record.update)('profile', { name: 'alice' }),
|
||||||
'update .name'
|
'update .name'
|
||||||
)
|
)
|
||||||
assert.deepEqual(peer.record.get(aliceID, 'profile'), { name: 'alice' }, 'get')
|
assert.deepEqual(peer.record.read(aliceID, 'profile'), { name: 'alice' }, 'get')
|
||||||
|
|
||||||
const fieldRoots1 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots1 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(fieldRoots1, { name: ['PbwnLbJS4oninQ1RPCdgRn'] }, 'fieldRoots')
|
assert.deepEqual(fieldRoots1, { name: ['PbwnLbJS4oninQ1RPCdgRn'] }, 'fieldRoots')
|
||||||
|
|
||||||
assert(await p(peer.record.update)(aliceID, 'profile', { age: 20 }), 'update .age')
|
assert(await p(peer.record.update)('profile', { age: 20 }), 'update .age')
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
peer.record.get(aliceID, 'profile'),
|
peer.record.read(aliceID, 'profile'),
|
||||||
{ name: 'alice', age: 20 },
|
{ name: 'alice', age: 20 },
|
||||||
'get'
|
'get'
|
||||||
)
|
)
|
||||||
|
|
||||||
const fieldRoots2 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots2 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fieldRoots2,
|
fieldRoots2,
|
||||||
{ name: ['PbwnLbJS4oninQ1RPCdgRn'], age: ['9iTTqNabtnXmw4AiZxNMRq'] },
|
{ name: ['PbwnLbJS4oninQ1RPCdgRn'], age: ['9iTTqNabtnXmw4AiZxNMRq'] },
|
||||||
|
@ -52,28 +52,28 @@ test('Record update() and get()', async (t) => {
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
await p(peer.record.update)(aliceID, 'profile', { name: 'alice' }),
|
await p(peer.record.update)('profile', { name: 'alice' }),
|
||||||
false,
|
false,
|
||||||
'redundant update .name'
|
'redundant update .name'
|
||||||
)
|
)
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
peer.record.get(aliceID, 'profile'),
|
peer.record.read(aliceID, 'profile'),
|
||||||
{ name: 'alice', age: 20 },
|
{ name: 'alice', age: 20 },
|
||||||
'get'
|
'get'
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.equal(
|
assert.equal(
|
||||||
await p(peer.record.update)(aliceID, 'profile', { name: 'Alice' }),
|
await p(peer.record.update)('profile', { name: 'Alice' }),
|
||||||
true,
|
true,
|
||||||
'update .name'
|
'update .name'
|
||||||
)
|
)
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
peer.record.get(aliceID, 'profile'),
|
peer.record.read(aliceID, 'profile'),
|
||||||
{ name: 'Alice', age: 20 },
|
{ name: 'Alice', age: 20 },
|
||||||
'get'
|
'get'
|
||||||
)
|
)
|
||||||
|
|
||||||
const fieldRoots3 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots3 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fieldRoots3,
|
fieldRoots3,
|
||||||
{ age: ['9iTTqNabtnXmw4AiZxNMRq'], name: ['M2JhM7TE2KX5T5rfnxBh6M'] },
|
{ age: ['9iTTqNabtnXmw4AiZxNMRq'], name: ['M2JhM7TE2KX5T5rfnxBh6M'] },
|
||||||
|
@ -82,11 +82,11 @@ test('Record update() and get()', async (t) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Record squeeze', async (t) => {
|
test('Record squeeze', async (t) => {
|
||||||
assert(await p(peer.record.update)(aliceID, 'profile', { age: 21 }), 'update .age')
|
assert(await p(peer.record.update)('profile', { age: 21 }), 'update .age')
|
||||||
assert(await p(peer.record.update)(aliceID, 'profile', { age: 22 }), 'update .age')
|
assert(await p(peer.record.update)('profile', { age: 22 }), 'update .age')
|
||||||
assert(await p(peer.record.update)(aliceID, 'profile', { age: 23 }), 'update .age')
|
assert(await p(peer.record.update)('profile', { age: 23 }), 'update .age')
|
||||||
|
|
||||||
const fieldRoots4 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots4 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fieldRoots4,
|
fieldRoots4,
|
||||||
{ name: ['M2JhM7TE2KX5T5rfnxBh6M'], age: ['S3xiydrT6Y34Bp1vg6wN7P'] },
|
{ name: ['M2JhM7TE2KX5T5rfnxBh6M'], age: ['S3xiydrT6Y34Bp1vg6wN7P'] },
|
||||||
|
@ -94,9 +94,9 @@ test('Record squeeze', async (t) => {
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.equal(peer.record._squeezePotential('profile'), 3, 'squeezePotential=3')
|
assert.equal(peer.record._squeezePotential('profile'), 3, 'squeezePotential=3')
|
||||||
assert.equal(await p(peer.record.squeeze)(aliceID, 'profile'), true, 'squeezed')
|
assert.equal(await p(peer.record.squeeze)('profile'), true, 'squeezed')
|
||||||
|
|
||||||
const fieldRoots5 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots5 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fieldRoots5,
|
fieldRoots5,
|
||||||
{ name: ['Y4JkpPCHN8Avtz4VALaAmK'], age: ['Y4JkpPCHN8Avtz4VALaAmK'] },
|
{ name: ['Y4JkpPCHN8Avtz4VALaAmK'], age: ['Y4JkpPCHN8Avtz4VALaAmK'] },
|
||||||
|
@ -104,9 +104,9 @@ test('Record squeeze', async (t) => {
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.equal(peer.record._squeezePotential('profile'), 0, 'squeezePotential=0')
|
assert.equal(peer.record._squeezePotential('profile'), 0, 'squeezePotential=0')
|
||||||
assert.equal(await p(peer.record.squeeze)(aliceID, 'profile'), false,'squeeze idempotent')
|
assert.equal(await p(peer.record.squeeze)('profile'), false,'squeeze idempotent')
|
||||||
|
|
||||||
const fieldRoots6 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots6 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(fieldRoots6, fieldRoots5, 'fieldRoots')
|
assert.deepEqual(fieldRoots6, fieldRoots5, 'fieldRoots')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ test('Record receives old branched update', async (t) => {
|
||||||
const rec = await p(peer.db.add)(msg, mootID)
|
const rec = await p(peer.db.add)(msg, mootID)
|
||||||
assert.equal(rec.id, 'XZWr3DZFG253awsWXgSkS2', 'msg ID')
|
assert.equal(rec.id, 'XZWr3DZFG253awsWXgSkS2', 'msg ID')
|
||||||
|
|
||||||
const fieldRoots7 = peer.record.getFieldRoots(aliceID, 'profile')
|
const fieldRoots7 = peer.record.getFieldRoots('profile')
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
fieldRoots7,
|
fieldRoots7,
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue