diff --git a/lib/index.js b/lib/index.js index 32b414a..95b9a27 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,13 +28,13 @@ const p = require('promisify-tuple') * issuer: ['account' | 'pubkey', string], * token: string, * }} PromiseFollowCommand - * @typedef {`promise.follow/${string}/${'account' | 'pubkey'}.${string}`} PromiseFollowCommandStr + * @typedef {`promise.follow/${'account' | 'pubkey'}.${string}/${string}`} PromiseFollowCommandStr * @typedef {{ * type: 'promise.account-add', * issuer: ['account' | 'pubkey', string], * token: string, * }} PromiseAccountAddCommand - * @typedef {`promise.account-add/${string}/${'account' | 'pubkey'}.${string}`} PromiseAccountAddCommandStr + * @typedef {`promise.account-add/${'account' | 'pubkey'}.${string}/${string}`} PromiseAccountAddCommandStr * @typedef {| JoinCommand * | FollowCommand * | TunnelConnectCommand @@ -152,15 +152,15 @@ function parseTunnelConnectCommand(pieces, uri) { * @returns {PromiseFollowCommand} */ function parsePromiseFollowCommand(pieces, uri) { - const [label, token, issuerAndType] = pieces - if (!token) { - // prettier-ignore - throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow token`) - } + const [label, issuerAndType, token] = pieces if (!issuerAndType) { // prettier-ignore throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow issuer`) } + if (!token) { + // prettier-ignore + throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.follow token`) + } pieces.shift() pieces.shift() pieces.shift() @@ -182,15 +182,15 @@ function parsePromiseFollowCommand(pieces, uri) { * @returns {PromiseAccountAddCommand} */ function parsePromiseAccountAddCommand(pieces, uri) { - const [label, token, issuerAndType] = pieces - if (!token) { - // prettier-ignore - throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add token`) - } + const [label, issuerAndType, token] = pieces if (!issuerAndType) { // prettier-ignore throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add issuer`) } + if (!token) { + // prettier-ignore + throw new Error(`Invalid URI "${uri}" for invite.parse, missing promise.account-add token`) + } pieces.shift() pieces.shift() pieces.shift() @@ -380,7 +380,7 @@ function initInvite(peer, config) { }) if (err2) return cb(err2) /** @type {PromiseFollowCommandStr} */ - const promiseCommand = `promise.follow/${token}/pubkey.${peer.shse.pubkey}` + const promiseCommand = `promise.follow/pubkey.${peer.shse.pubkey}/${token}` // Create follow command /** @type {FollowCommandStr} */ @@ -429,7 +429,7 @@ function initInvite(peer, config) { const [err3, token] = await p(peer.promise.create)(promise) if (err3) return cb(err3) /** @type {PromiseAccountAddCommandStr} */ - const promiseCommand = `promise.account-add/${token}/pubkey.${peer.shse.pubkey}` + const promiseCommand = `promise.account-add/pubkey.${peer.shse.pubkey}/${token}` // Create tunnel-connect command /** @type {TunnelConnectCommandStr} */ diff --git a/test/createForFriend.test.js b/test/createForFriend.test.js index a4840db..8ceb439 100644 --- a/test/createForFriend.test.js +++ b/test/createForFriend.test.js @@ -89,11 +89,11 @@ test('createForFriend()', async (t) => { }) assert.equal( uri, - `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/follow/MOCK_ID/promise.follow/MOCK_PROMISE/pubkey.${keypair.public}` + `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/follow/MOCK_ID/promise.follow/pubkey.${keypair.public}/MOCK_PROMISE` ) assert.equal( url, - `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ffollow%2FMOCK_ID%2Fpromise.follow%2FMOCK_PROMISE%2Fpubkey.${keypair.public}` + `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ffollow%2FMOCK_ID%2Fpromise.follow%2Fpubkey.${keypair.public}%2FMOCK_PROMISE` ) assert.ok(connectCalled) diff --git a/test/createForMyself.test.js b/test/createForMyself.test.js index be7b5ad..56e7692 100644 --- a/test/createForMyself.test.js +++ b/test/createForMyself.test.js @@ -87,11 +87,11 @@ test('createForMyself()', async (t) => { }) assert.equal( uri, - `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/tunnel-connect/HUB_PUBKEY/${local.shse.pubkey}/promise.account-add/MOCK_PROMISE/pubkey.${keypair.public}` + `ppppp://invite/join/dns/example.com/tcp/8008/shse/HUB_PUBKEY.MOCK_TOKEN/tunnel-connect/HUB_PUBKEY/${local.shse.pubkey}/promise.account-add/pubkey.${keypair.public}/MOCK_PROMISE` ) assert.equal( url, - `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ftunnel-connect%2FHUB_PUBKEY%2F${local.shse.pubkey}%2Fpromise.account-add%2FMOCK_PROMISE%2Fpubkey.${keypair.public}` + `https://example.com/invite#ppppp%3A%2F%2Finvite%2Fjoin%2Fdns%2Fexample.com%2Ftcp%2F8008%2Fshse%2FHUB_PUBKEY.MOCK_TOKEN%2Ftunnel-connect%2FHUB_PUBKEY%2F${local.shse.pubkey}%2Fpromise.account-add%2Fpubkey.${keypair.public}%2FMOCK_PROMISE` ) assert.ok(connectCalled) diff --git a/test/parse.test.js b/test/parse.test.js index a5e3ba9..d09f63a 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -16,7 +16,7 @@ test('parse() error cases', (t) => { test('parse() good friend invite', (t) => { const commands = plugin.parse( - 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/follow/ALICE/promise.follow/ALICE_TOKEN/pubkey.ALICE' + 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/follow/ALICE/promise.follow/pubkey.ALICE/ALICE_TOKEN' ) assert.deepEqual(commands, [ { @@ -37,7 +37,7 @@ test('parse() good friend invite', (t) => { test('parse() good myself invite', (t) => { const commands = plugin.parse( - 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/tunnel-connect/HUB_PUBKEY/OLD_PUBKEY/promise.account-add/OLD_TOKEN/pubkey.PUBKEY' + 'ppppp://invite/join/dns/example.com/tcp/8080/shse/PUBKEY.TOKEN/tunnel-connect/HUB_PUBKEY/OLD_PUBKEY/promise.account-add/pubkey.PUBKEY/OLD_TOKEN' ) assert.deepEqual(commands, [ {