From 5c560f97d93f11193f85239d35f2d136e031def8 Mon Sep 17 00:00:00 2001 From: Hayden Young Date: Mon, 18 Nov 2024 23:00:10 +0000 Subject: [PATCH] refactor: Rename pin/unpin to add/remove. --- src/bin/cli.js | 6 ++--- src/lib/handle-request.js | 14 +++++----- src/lib/handlers/{pin.js => add.js} | 6 ++--- src/lib/handlers/{unpin.js => remove.js} | 6 ++--- src/lib/lander.js | 33 ++++++++++++------------ src/lib/messages/index.js | 4 +-- test/{pin.test.js => add.test.js} | 6 ++--- test/e2e-browser.test.js | 18 ++++++------- test/messages.test.js | 22 ++++++++-------- test/{unpin.test.js => remove.test.js} | 12 ++++----- test/stress-test.js | 4 +-- test/utils/create-pins.js | 2 +- 12 files changed, 67 insertions(+), 66 deletions(-) rename src/lib/handlers/{pin.js => add.js} (86%) rename src/lib/handlers/{unpin.js => remove.js} (83%) rename test/{pin.test.js => add.test.js} (94%) rename test/{unpin.test.js => remove.test.js} (81%) diff --git a/src/bin/cli.js b/src/bin/cli.js index 28257bf..1b2140b 100755 --- a/src/bin/cli.js +++ b/src/bin/cli.js @@ -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' }) }, @@ -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' }) }, @@ -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() diff --git a/src/lib/handle-request.js b/src/lib/handle-request.js index 48d20ac..e5829f3 100644 --- a/src/lib/handle-request.js +++ b/src/lib/handle-request.js @@ -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 => { @@ -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. @@ -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: diff --git a/src/lib/handlers/pin.js b/src/lib/handlers/add.js similarity index 86% rename from src/lib/handlers/pin.js rename to src/lib/handlers/add.js index d0dbfd6..fc442ee 100644 --- a/src/lib/handlers/pin.js +++ b/src/lib/handlers/add.js @@ -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) => { @@ -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 @@ -31,6 +31,6 @@ export default async ({ orbitdb, pins, dbs, id, addresses }) => { await pins.set(address, identities) - log('pinned', address) + log('pin added', address) } } diff --git a/src/lib/handlers/unpin.js b/src/lib/handlers/remove.js similarity index 83% rename from src/lib/handlers/unpin.js rename to src/lib/handlers/remove.js index 48f6e73..5546af1 100644 --- a/src/lib/handlers/unpin.js +++ b/src/lib/handlers/remove.js @@ -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) @@ -25,6 +25,6 @@ export default async ({ orbitdb, pins, dbs, id, addresses }) => { delete dbs[address] } - log('unpinned', address) + log('pin removed', address) } } diff --git a/src/lib/lander.js b/src/lib/lander.js index 2aac03e..1854fcf 100644 --- a/src/lib/lander.js +++ b/src/lib/lander.js @@ -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 } } diff --git a/src/lib/messages/index.js b/src/lib/messages/index.js index bfeb985..4b69021 100644 --- a/src/lib/messages/index.js +++ b/src/lib/messages/index.js @@ -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({ diff --git a/test/pin.test.js b/test/add.test.js similarity index 94% rename from test/pin.test.js rename to test/add.test.js index f3ef425..f56183f 100644 --- a/test/pin.test.js +++ b/test/add.test.js @@ -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) @@ -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) }) diff --git a/test/e2e-browser.test.js b/test/e2e-browser.test.js index 8e122f3..1fc2aa9 100644 --- a/test/e2e-browser.test.js +++ b/test/e2e-browser.test.js @@ -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 @@ -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') @@ -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 @@ -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') @@ -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 @@ -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() @@ -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') diff --git a/test/messages.test.js b/test/messages.test.js index 2774441..4377435 100644 --- a/test/messages.test.js +++ b/test/messages.test.js @@ -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 })() } @@ -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 => { @@ -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 }) @@ -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 => { @@ -86,6 +86,6 @@ describe('Messages', function () { } } - await pipe(pinDBs({ type: 'UNKNOWN' }), handleRequest(orbiter), sink) + await pipe(addDBs({ type: 'UNKNOWN' }), handleRequest(orbiter), sink) }) }) diff --git a/test/unpin.test.js b/test/remove.test.js similarity index 81% rename from test/unpin.test.js rename to test/remove.test.js index 4827275..e1063d7 100644 --- a/test/unpin.test.js +++ b/test/remove.test.js @@ -4,7 +4,7 @@ import { launchLander } from './utils/launch-lander.js' import { launchOrbiter } from './utils/launch-orbiter.js' import { createPins } from './utils/create-pins.js' -describe('Unpin', function () { +describe('Remove', function () { this.timeout(10000) let orbiter @@ -31,10 +31,10 @@ describe('Unpin', function () { await rimraf('./lander') }) - it('unpins a database', async function () { + it('removes a database', async function () { const { addresses } = await createPins(1, lander) - const unpinned = await lander.unpin(addresses) + const unpinned = await lander.remove(addresses) strictEqual(unpinned, true) strictEqual((await orbiter.pins.all()).length, 0) @@ -44,17 +44,17 @@ describe('Unpin', function () { it('unpins multiple databases', async function () { const { addresses } = await createPins(2, lander) - const unpinned = await lander.unpin(addresses) + const unpinned = await lander.remove(addresses) strictEqual(unpinned, true) strictEqual((await orbiter.pins.all()).length, 0) strictEqual(Object.values(orbiter.dbs).length, 0) }) - it('unpins a database when multiple databases have been pinned', async function () { + it('removes a database when multiple databases have been pinned', async function () { const { addresses } = await createPins(2, lander) - const unpinned = await lander.unpin(addresses.slice(0, 1)) + const unpinned = await lander.remove(addresses.slice(0, 1)) strictEqual(unpinned, true) strictEqual((await orbiter.pins.all()).length, 1) diff --git a/test/stress-test.js b/test/stress-test.js index e099dac..d383e5b 100644 --- a/test/stress-test.js +++ b/test/stress-test.js @@ -35,7 +35,7 @@ describe('End-to-End Browser Tests', function () { await rimraf('./orbiter3') }) - it('pin and replicate a database - lander1->orbiter1(nodejs)->orbiter2(nodejs)->lander2', async function () { + it('add and replicate a database - lander1->orbiter1(nodejs)->orbiter2(nodejs)->lander2', async function () { const rounds = 50 const entryAmount = 100 const addr = orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().shift() @@ -59,7 +59,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() diff --git a/test/utils/create-pins.js b/test/utils/create-pins.js index 08a2ae0..4aeaf19 100644 --- a/test/utils/create-pins.js +++ b/test/utils/create-pins.js @@ -6,7 +6,7 @@ export const createPins = async (howMany, lander) => { addresses.push(db.address) } - const pinned = await lander.pin(addresses) + const pinned = await lander.add(addresses) return { pinned, addresses } }