Skip to content

Commit a4945bc

Browse files
committed
Allow round trips for blobs
Signed-off-by: Gabriel Indik <[email protected]>
1 parent 7f21686 commit a4945bc

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/routers/api.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ router.post('/messages', async (req, res, next) => {
175175
throw new RequestError('Missing recipient', 400);
176176
}
177177
let recipientEndpoint: string;
178-
if(recipientID === peerID) {
178+
if (recipientID === peerID) {
179179
recipientEndpoint = config.p2p.endpoint ?? `https://${config.p2p.hostname}:${config.p2p.port}`;
180180
} else {
181181
let recipientPeer = config.peers.find(peer => peer.id === recipientID);
@@ -278,18 +278,23 @@ router.post('/transfers', async (req, res, next) => {
278278
} else {
279279
throw new RequestError('Missing recipient', 400);
280280
}
281-
let recipientPeer = config.peers.find(peer => peer.id === recipientID);
282-
if (recipientPeer === undefined) {
283-
throw new RequestError(`Unknown recipient`, 400);
284-
}
285-
if (recipientDestination !== undefined && !recipientPeer.destinations?.includes(recipientDestination)) {
286-
throw new RequestError(`Unknown recipient destination expected=${recipientPeer.destinations?.join('|')} recieved=${recipientDestination}`, 400);
281+
let recipientEndpoint: string;
282+
recipientEndpoint = config.p2p.endpoint ?? `https://${config.p2p.hostname}:${config.p2p.port}`;
283+
if (recipientID === peerID) {
284+
} else {
285+
let recipientPeer = config.peers.find(peer => peer.id === recipientID);
286+
if (recipientPeer === undefined) {
287+
throw new RequestError(`Unknown recipient`, 400);
288+
}
289+
if (recipientDestination !== undefined && !recipientPeer.destinations?.includes(recipientDestination)) {
290+
throw new RequestError(`Unknown recipient destination expected=${recipientPeer.destinations?.join('|')} recieved=${recipientDestination}`, 400);
291+
}
287292
}
288293
let requestId = uuidV4();
289294
if (typeof req.body.requestId === 'string') {
290295
requestId = req.body.requestId;
291296
}
292-
blobsHandler.sendBlob(req.body.path, recipientID, recipientPeer.endpoint, requestId, senderDestination, recipientDestination);
297+
blobsHandler.sendBlob(req.body.path, recipientID, recipientEndpoint, requestId, senderDestination, recipientDestination);
293298
res.send({ requestId });
294299
} catch (err) {
295300
next(err);

src/routers/p2p.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,7 @@ 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-
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-
}
51-
}
39+
validateSenderDestination(sender, senderDestination);
5240
sender += '/' + senderDestination;
5341
}
5442
let recipient = peerID;
@@ -76,10 +64,14 @@ router.put('/blobs/*', async (req: Request, res, next) => {
7664
let sender = utils.extractPeerSenderFromRequest(req);
7765
const { file, senderDestination, recipientDestination } = await utils.extractFileFromMultipartForm(req);
7866
if (senderDestination !== undefined) {
67+
validateSenderDestination(sender, senderDestination);
7968
sender += '/' + senderDestination;
8069
}
8170
let recipient = peerID;
8271
if (recipientDestination !== undefined) {
72+
if (!config.destinations?.includes(recipientDestination)) {
73+
throw new RequestError(`Unknown recipient destination expected=${config.destinations?.join('|') ?? 'none'} recieved=${recipientDestination}`, 404);
74+
}
8375
recipient += '/' + recipientDestination;
8476
}
8577
const blobPath = path.join(utils.constants.RECEIVED_BLOBS_SUBDIRECTORY, sender, req.params[0]);
@@ -99,3 +91,19 @@ router.put('/blobs/*', async (req: Request, res, next) => {
9991
next(err);
10092
}
10193
});
94+
95+
const validateSenderDestination = (sender: string, senderDestination: string) => {
96+
if (sender === peerID) {
97+
if (!config.destinations?.includes(senderDestination)) {
98+
throw new RequestError(`Unknown sender destination expected=${config.destinations?.join('|') ?? 'none'} recieved=${senderDestination}`, 404);
99+
}
100+
} else {
101+
const peer = config.peers.find(peer => peer.id === sender);
102+
if (peer === undefined) {
103+
throw new RequestError(`Unknown sender ${sender}`, 404);
104+
}
105+
if (!peer.destinations?.includes(senderDestination)) {
106+
throw new RequestError(`Unknown sender destination expected=${peer.destinations?.join('|') ?? 'none'} recieved=${senderDestination}`, 404);
107+
}
108+
}
109+
};

0 commit comments

Comments
 (0)