Skip to content

Commit

Permalink
adding hash get route
Browse files Browse the repository at this point in the history
  • Loading branch information
sairaj-mote committed Jan 12, 2024
1 parent d7bccff commit b73bdf2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
53 changes: 31 additions & 22 deletions routes/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,47 @@ async function fetchAndHashContent(url, visitedUrls = new Set()) {
}

const hashCache = new Map();
router.get("/", async (req, res) => {
const { url } = req.query;
if (!url) {
return res.status(400).json({ error: 'Missing <url> in the query parameters' });
}
res.json(await generateHash(url));
})
// API endpoint to start the recursive download and hashing
router.post('/', async (req, res) => {
try {
let { urls } = req.body;
const { urls } = req.body;
if (!urls) {
return res.status(400).json({ error: 'Missing <urls> in the request parameters' });
}
if (!Array.isArray(urls))
urls = [urls];
const promises = urls.map(async (url) => {
const urlWithoutHashAndQuery = parseUrlWithoutHashAndQuery(url);
let hash;
// regex to identify owner and repo name from https://owner.github.io/repo-name
const githubRepoRegex = /https?:\/\/([\w-]+)\.github\.io\/([\w-]+)/;
if (githubRepoRegex.test(urlWithoutHashAndQuery) && urlWithoutHashAndQuery.match(githubRepoRegex)[1] === 'ranchimall') {
if (!hashCache.has(urlWithoutHashAndQuery)) {
await fetchAndSaveAppHash(urlWithoutHashAndQuery)
}
hash = hashCache.get(urlWithoutHashAndQuery).hash;
} else {
const hashedContent = await fetchAndHashContent(urlWithoutHashAndQuery);
hash = await hashContent(Buffer.from(hashedContent, 'utf-8'));
}
return { url, hash };
});

const results = await Promise.all(promises);
res.json(results);
res.json(await generateHash(urls));
} catch (error) {
res.status(500).json({ error: error.message });
}
});
async function generateHash(urls = []) {
if (!Array.isArray(urls))
urls = [urls];
const promises = urls.map(async (url) => {
const urlWithoutHashAndQuery = parseUrlWithoutHashAndQuery(url);
let hash;
// regex to identify owner and repo name from https://owner.github.io/repo-name
const githubRepoRegex = /https?:\/\/([\w-]+)\.github\.io\/([\w-]+)/;
if (githubRepoRegex.test(urlWithoutHashAndQuery) && urlWithoutHashAndQuery.match(githubRepoRegex)[1] === 'ranchimall') {
if (!hashCache.has(urlWithoutHashAndQuery)) {
await fetchAndSaveAppHash(urlWithoutHashAndQuery)
}
hash = hashCache.get(urlWithoutHashAndQuery).hash;
} else {
const hashedContent = await fetchAndHashContent(urlWithoutHashAndQuery);
hash = await hashContent(Buffer.from(hashedContent, 'utf-8'));
}
return { url, hash };
});

return await Promise.all(promises);
}
async function fetchAndSaveAppHash(url, lastUpdated = Date.now()) {
const hashedContent = await fetchAndHashContent(url);
const hash = await hashContent(Buffer.from(hashedContent, 'utf-8'));
Expand Down
2 changes: 1 addition & 1 deletion routes/hash.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b73bdf2

Please sign in to comment.