Skip to content

Commit fbfa777

Browse files
committed
Allow round trips
Signed-off-by: Gabriel Indik <[email protected]>
1 parent 6ee3f81 commit fbfa777

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ build
33
coverage
44
.nyc_output
55
data/member-a/blobs
6-
data/member-b/blobs
6+
data/member-a/destinations
7+
data/member-b/blobs
8+
data/member-b/destinations

src/routers/api.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,24 @@ router.post('/messages', async (req, res, next) => {
174174
} else {
175175
throw new RequestError('Missing recipient', 400);
176176
}
177-
let recipientPeer = config.peers.find(peer => peer.id === recipientID);
178-
if (recipientPeer === undefined) {
179-
throw new RequestError(`Unknown recipient ${recipientID}`, 400);
180-
}
181-
if (recipientDestination !== undefined && !recipientPeer.destinations?.includes(recipientDestination)) {
182-
throw new RequestError(`Unknown recipient destination expected=${recipientPeer.destinations?.join('|') ?? 'none'} recieved=${recipientDestination}`, 400);
177+
let recipientEndpoint: string;
178+
if(recipientID === peerID) {
179+
recipientEndpoint = config.p2p.endpoint ?? `https://${config.p2p.hostname}:${config.p2p.port}`;
180+
} else {
181+
let recipientPeer = config.peers.find(peer => peer.id === recipientID);
182+
if (recipientPeer === undefined) {
183+
throw new RequestError(`Unknown recipient ${recipientID}`, 400);
184+
}
185+
recipientEndpoint = recipientPeer.endpoint;
186+
if (recipientDestination !== undefined && !recipientPeer.destinations?.includes(recipientDestination)) {
187+
throw new RequestError(`Unknown recipient destination expected=${recipientPeer.destinations?.join('|') ?? 'none'} recieved=${recipientDestination}`, 400);
188+
}
183189
}
184190
let requestId = uuidV4();
185191
if (typeof req.body.requestId === 'string') {
186192
requestId = req.body.requestId;
187193
}
188-
messagesHandler.sendMessage(req.body.message, recipientID, recipientPeer.endpoint, requestId, senderDestination, recipientDestination);
194+
messagesHandler.sendMessage(req.body.message, recipientID, recipientEndpoint, requestId, senderDestination, recipientDestination);
189195
res.send({ requestId });
190196
} catch (err) {
191197
next(err);

src/routers/p2p.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ router.post('/messages', async (req: Request, res, next) => {
3636
let sender = utils.extractPeerSenderFromRequest(req);
3737
const { senderDestination, recipientDestination, message } = await utils.extractMessageFromMultipartForm(req);
3838
if (senderDestination !== undefined) {
39-
const peer = config.peers.find(peer => peer.id === sender);
40-
if(peer === undefined) {
41-
throw new RequestError(`Unknown sender ${sender}`, 404);
42-
}
43-
if(!peer.destinations?.includes(senderDestination)) {
44-
throw new RequestError(`Unknown sender destination expected=${peer.destinations?.join('|') ?? 'none'} recieved=${senderDestination}`, 404);
39+
if (sender === peerID) {
40+
if (!config.destinations?.includes(senderDestination)) {
41+
throw new RequestError(`Unknown sender destination expected=${config.destinations?.join('|') ?? 'none'} recieved=${senderDestination}`, 404);
42+
}
43+
} else {
44+
const peer = config.peers.find(peer => peer.id === sender);
45+
if (peer === undefined) {
46+
throw new RequestError(`Unknown sender ${sender}`, 404);
47+
}
48+
if (!peer.destinations?.includes(senderDestination)) {
49+
throw new RequestError(`Unknown sender destination expected=${peer.destinations?.join('|') ?? 'none'} recieved=${senderDestination}`, 404);
50+
}
4551
}
4652
sender += '/' + senderDestination;
4753
}
4854
let recipient = peerID;
4955
if (recipientDestination !== undefined) {
50-
if(!config.destinations?.includes(recipientDestination)) {
56+
if (!config.destinations?.includes(recipientDestination)) {
5157
throw new RequestError(`Unknown recipient destination expected=${config.destinations?.join('|') ?? 'none'} recieved=${recipientDestination}`, 404);
5258
}
5359
recipient += '/' + recipientDestination;

0 commit comments

Comments
 (0)