Skip to content

Commit

Permalink
refactor: Rename pin/unpin to add/remove.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenyoung committed Nov 18, 2024
1 parent d6f305e commit 5c560f9
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ yargs(hideBin(process.argv))
'Add an authorized address',
yargs => {
yargs.positional('id', {
describe: 'The id of the user who is allowed to pin one or more databases (or denied depending on default access settings).',
describe: 'The id of the user who is allowed to add one or more databases (or denied depending on default access settings).',
type: 'string'
})
},
Expand All @@ -59,7 +59,7 @@ yargs(hideBin(process.argv))
'Remove an authorized address',
yargs => {
yargs.positional('id', {
describe: 'The id of the user who will no longer be allowed to pin one or more databases (or denied depending on default access settings).',
describe: 'The id of the user who will no longer be allowed to add one or more databases (or denied depending on default access settings).',
type: 'string'
})
},
Expand Down Expand Up @@ -105,7 +105,7 @@ yargs(hideBin(process.argv))
}).option('allow', {
alias: 'a',
type: 'boolean',
description: 'Allow anyone to pin a database. The default is false.'
description: 'Allow anyone to add a database. The default is false.'
})
.demandCommand(1, 'Error: specify a command.')
.help()
Expand Down
14 changes: 7 additions & 7 deletions src/lib/handle-request.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import handlePinRequest from './handlers/pin.js'
import handleUnpinRequest from './handlers/unpin.js'
import handleAddRequest from './handlers/add.js'
import handleRemoveRequest from './handlers/remove.js'
import { createResponseMessage, parseMessage, Requests, Responses } from './messages/index.js'

export const handleRequest = (orbiter) => source => {
Expand All @@ -20,7 +20,7 @@ export const handleRequest = (orbiter) => source => {

// check that the identity is authorized to store their dbs on this orbiter.
if (!await orbiter.auth.hasAccess(identity.id)) {
throw Object.assign(new Error('user is not authorized to pin'), { type: Responses.E_NOT_AUTHORIZED })
throw Object.assign(new Error('user is not authorized to add'), { type: Responses.E_NOT_AUTHORIZED })
}

// verify that the params have come from the user who owns the identity's pubkey.
Expand All @@ -31,12 +31,12 @@ export const handleRequest = (orbiter) => source => {
const { orbitdb, pins, dbs } = orbiter

switch (type) {
case Requests.PIN:
await handlePinRequest({ orbitdb, pins, dbs, id, addresses })
case Requests.PIN_ADD:
await handleAddRequest({ orbitdb, pins, dbs, id, addresses })
response = createResponseMessage(Responses.OK)
break
case Requests.UNPIN:
await handleUnpinRequest({ orbitdb, pins, dbs, id, addresses })
case Requests.PIN_REMOVE:
await handleRemoveRequest({ orbitdb, pins, dbs, id, addresses })
response = createResponseMessage(Responses.OK)
break
default:
Expand Down
6 changes: 3 additions & 3 deletions src/lib/handlers/pin.js → src/lib/handlers/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from '@libp2p/logger'

const log = logger('voyager:orbiter:pin')
const log = logger('voyager:orbiter:pin:add')

const waitForReplication = (db) => {
return new Promise((resolve, reject) => {
Expand All @@ -11,7 +11,7 @@ const waitForReplication = (db) => {

export default async ({ orbitdb, pins, dbs, id, addresses }) => {
for (const address of addresses) {
log('pin ', address)
log('add ', address)

let identities = await pins.get(address)
const hasDb = identities !== undefined
Expand All @@ -31,6 +31,6 @@ export default async ({ orbitdb, pins, dbs, id, addresses }) => {

await pins.set(address, identities)

log('pinned', address)
log('pin added', address)
}
}
6 changes: 3 additions & 3 deletions src/lib/handlers/unpin.js → src/lib/handlers/remove.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { logger } from '@libp2p/logger'

const log = logger('voyager:orbiter:unpin')
const log = logger('voyager:orbiter:pin:remove')

export default async ({ orbitdb, pins, dbs, id, addresses }) => {
for (const address of addresses) {
log('unpin ', address)
log('remove ', address)

const identities = await pins.get(address)

Expand All @@ -25,6 +25,6 @@ export default async ({ orbitdb, pins, dbs, id, addresses }) => {
delete dbs[address]
}

log('unpinned', address)
log('pin removed', address)
}
}
33 changes: 17 additions & 16 deletions src/lib/lander.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,61 @@ import { Requests, Responses, createRequestMessage, parseMessage } from './messa
import { voyagerProtocol } from './protocol.js'

export default async ({ orbitdb, orbiterAddressOrId }) => {
const pin = async (addresses) => {
let pinned = false
const pinDBs = source => {
const add = async (addresses) => {
let added = false

const addDBs = source => {
return (async function * () {
addresses = Array.isArray(addresses) ? addresses : [addresses]
const message = await createRequestMessage(Requests.PIN, addresses, orbitdb.identity)
const message = await createRequestMessage(Requests.PIN_ADD, addresses, orbitdb.identity)
yield message
})()
}

const stream = await orbitdb.ipfs.libp2p.dialProtocol(orbiterAddressOrId, voyagerProtocol, { runOnLimitedConnection: true })

await pipe(pinDBs, stream, async (source) => {
await pipe(addDBs, stream, async (source) => {
for await (const chunk of source) {
const message = parseMessage(chunk.subarray())

if (message.type === Responses.OK) {
pinned = true
added = true
}
}
})

return pinned
return added
}

const unpin = async (addresses) => {
let unpinned = false
const remove = async (addresses) => {
let removed = false

const unpinDBs = source => {
const removeDBs = source => {
return (async function * () {
addresses = Array.isArray(addresses) ? addresses : [addresses]
const message = await createRequestMessage(Requests.UNPIN, addresses, orbitdb.identity)
const message = await createRequestMessage(Requests.PIN_REMOVE, addresses, orbitdb.identity)
yield message
})()
}

const stream = await orbitdb.ipfs.libp2p.dialProtocol(orbiterAddressOrId, voyagerProtocol, { runOnLimitedConnection: true })

await pipe(unpinDBs, stream, async source => {
await pipe(removeDBs, stream, async source => {
for await (const chunk of source) {
const message = parseMessage(chunk.subarray())

if (message.type === Responses.OK) {
unpinned = true
removed = true
}
}
})

return unpinned
return removed
}

return {
orbitdb,
pin,
unpin
add,
remove
}
}
4 changes: 2 additions & 2 deletions src/lib/messages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'

export const Requests = Object.freeze({
PIN: 1,
UNPIN: 2
PIN_ADD: 1,
PIN_REMOVE: 2
})

export const Responses = Object.freeze({
Expand Down
6 changes: 3 additions & 3 deletions test/pin.test.js → test/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ describe('Pin', function () {
await rimraf('./lander')
})

it('pins a database', async function () {
it('adds a database', async function () {
const { pinned, addresses } = await createPins(1, lander)

strictEqual(pinned, true)
strictEqual(Object.values(orbiter.dbs).pop().address, addresses.pop())
})

it('pins multiple databases', async function () {
it('adds multiple databases', async function () {
const { pinned, addresses } = await createPins(2, lander)

strictEqual(pinned, true)
Expand All @@ -49,7 +49,7 @@ describe('Pin', function () {
it('tries to pin a database when not authorized', async function () {
await orbiter.auth.del(lander.orbitdb.identity.id)
const db = await lander.orbitdb.open('db')
const pinned = await lander.pin(db.address)
const pinned = await lander.add(db.address)

strictEqual(pinned, false)
})
Expand Down
18 changes: 9 additions & 9 deletions test/e2e-browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('End-to-End Browser Tests', function () {
await rimraf('./lander3')
})

it('pin and replicate a database - lander1->orbiter1->lander2', async function () {
it('add pins and replicate a database - lander1->orbiter1->lander2', async function () {
const entryAmount = 100
let replicated = false

Expand All @@ -58,12 +58,12 @@ describe('End-to-End Browser Tests', function () {
const expected = await db1.all()

console.time('pin')
await lander1.pin(db1.address)
await lander1.add(db1.address)
console.timeEnd('pin')
await lander1.shutdown()

console.time('pin2')
await lander2.pin(db1.address)
await lander2.add(db1.address)
console.timeEnd('pin2')

console.time('replicate')
Expand All @@ -85,7 +85,7 @@ describe('End-to-End Browser Tests', function () {
deepStrictEqual(expected, res)
})

it('pin and replicate a database - lander1->orbiter1->orbiter2->lander3', async function () {
it('add pins and replicate a database - lander1->orbiter1->orbiter2->lander3', async function () {
const entryAmount = 100
let replicated = false

Expand All @@ -98,12 +98,12 @@ describe('End-to-End Browser Tests', function () {
const expected = await db1.all()

console.time('pin')
await lander1.pin(db1.address)
await lander1.add(db1.address)
console.timeEnd('pin')
await lander1.shutdown()

console.time('pin2')
await lander3.pin(db1.address)
await lander3.add(db1.address)
console.timeEnd('pin2')

console.time('replicate')
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('End-to-End Browser Tests', function () {
await rimraf('./orbiter3')
})

it('pin and replicate a database - lander1->orbiter1(nodejs)->orbiter2(browser)->lander3', async function () {
it('add pins and replicate a database - lander1->orbiter1(nodejs)->orbiter2(browser)->lander3', async function () {
const entryAmount = 100
let replicated = false

Expand All @@ -172,7 +172,7 @@ describe('End-to-End Browser Tests', function () {
const expected = await db1.all()

console.time('pin')
await lander1.pin(db1.address)
await lander1.add(db1.address)
console.timeEnd('pin')

await lander1.shutdown()
Expand All @@ -182,7 +182,7 @@ describe('End-to-End Browser Tests', function () {
await orbiter.auth.add(lander2.orbitdb.identity.id)

console.time('pin2')
await lander2.pin(db1.address)
await lander2.add(db1.address)
console.timeEnd('pin2')

console.time('replicate')
Expand Down
22 changes: 11 additions & 11 deletions test/messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe('Messages', function () {
let lander
let db

const pinDBs = ({ type, signer } = {}) => source => {
const addDBs = ({ type, signer } = {}) => source => {
return (async function * () {
const addresses = [db.address]
const message = await createRequestMessage(type || Requests.PIN, addresses, lander.orbitdb.identity, signer)
const message = await createRequestMessage(type || Requests.PIN_ADD, addresses, lander.orbitdb.identity, signer)
yield message
})()
}
Expand All @@ -35,7 +35,7 @@ describe('Messages', function () {
await rimraf('./orbiter')
})

it('pins a database with OK response', async function () {
it('adds a database with OK response', async function () {
await orbiter.auth.add(lander.orbitdb.identity.id)

const sink = async source => {
Expand All @@ -45,21 +45,21 @@ describe('Messages', function () {
}
}

await pipe(pinDBs(), handleRequest(orbiter), sink)
await pipe(addDBs(), handleRequest(orbiter), sink)
})

it('pins a database with E_NOT_AUTHORIZED response', async function () {
it('adds a database with E_NOT_AUTHORIZED response', async function () {
const sink = async source => {
for await (const chunk of source) {
const response = parseMessage(chunk.subarray())
deepStrictEqual(response, { message: 'user is not authorized to pin', type: Responses.E_NOT_AUTHORIZED })
deepStrictEqual(response, { message: 'user is not authorized to add', type: Responses.E_NOT_AUTHORIZED })
}
}

await pipe(pinDBs(), handleRequest(orbiter), sink)
await pipe(addDBs(), handleRequest(orbiter), sink)
})

it('pins a database with E_INVALID_SIGNATURE response', async function () {
it('adds a database with E_INVALID_SIGNATURE response', async function () {
await orbiter.auth.add(lander.orbitdb.identity.id)

const identities = await Identities({ path: './lander/identities', ipfs: lander.ipfs })
Expand All @@ -73,10 +73,10 @@ describe('Messages', function () {
}
}

await pipe(pinDBs({ signer: { sign: createInvalidSignature } }), handleRequest(orbiter), sink)
await pipe(addDBs({ signer: { sign: createInvalidSignature } }), handleRequest(orbiter), sink)
})

it('tries to pin a database with non-existent message', async function () {
it('tries to add a database with non-existent message', async function () {
await orbiter.auth.add(lander.orbitdb.identity.id)

const sink = async source => {
Expand All @@ -86,6 +86,6 @@ describe('Messages', function () {
}
}

await pipe(pinDBs({ type: 'UNKNOWN' }), handleRequest(orbiter), sink)
await pipe(addDBs({ type: 'UNKNOWN' }), handleRequest(orbiter), sink)
})
})
Loading

0 comments on commit 5c560f9

Please sign in to comment.