Skip to content

Commit

Permalink
feat: check for mirror before adding it
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Aug 19, 2022
1 parent b2130a5 commit 3b2bf19
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/datalayer/persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,50 @@ export const getRootDiff = async (storeId, root1, root2) => {
};

const _addMirror = async (storeId, url) => {
const mirrors = await getMirrors(storeId);

// Dont add the mirror if it already exists.
if (
mirrors.find(
(mirror) => mirror.launcher_id === storeId && mirror.urls.includes(url),
).length === 0
) {
return true;
}

try {
const options = {
url: `${rpcUrl}/add_mirror`,
body: JSON.stringify({
id: storeId,
urls: [url],
amount: 1,
}),
};

const response = await request(
Object.assign({}, getBaseOptions(), options),
);

const data = JSON.parse(response);

if (data.success) {
logger.info(`Adding mirror ${storeId} at ${url}`);
return true;
}

logger.error(`FAILED ADDING MIRROR FOR ${storeId}`);
return false;
} catch (error) {
return false;
}
};

const getMirrors = async (storeId) => {
const options = {
url: `${rpcUrl}/add_mirror`,
url: `${rpcUrl}/get_mirrors `,
body: JSON.stringify({
id: storeId,
urls: [url],
amount: 1,
}),
};

Expand All @@ -348,11 +386,10 @@ const _addMirror = async (storeId, url) => {
const data = JSON.parse(response);

if (data.success) {
logger.info(`Adding mirror ${storeId} at ${url}`);
return true;
return data.mirrors;
}

logger.error(`FAILED ADDING MIRROR FOR ${storeId}`);
logger.error(`FAILED GETTING MIRRORS FOR ${storeId}`);
return false;
} catch (error) {
return false;
Expand Down

0 comments on commit 3b2bf19

Please sign in to comment.