Skip to content

Commit

Permalink
added URL formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sairaj-mote committed Dec 8, 2023
1 parent ef092d1 commit ee94988
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const axios = require('axios');
const { createHash } = require('crypto');
const archiver = require('archiver');
const rateLimit = require('express-rate-limit');
const { parse: parseUrl } = require('url');
const { parse: parseUrl, URL } = require('url');
const { parse: parseHtml } = require('node-html-parser');

// Set up the allowed domains (replace with your specific domains)
Expand Down Expand Up @@ -36,6 +36,19 @@ app.use(
app.get('/', (req, res) => {
res.send('Hello There!');
})

function parseUrlWithoutHashAndQuery(fullUrl) {
const parsedUrl = new URL(fullUrl);

// Set the hash and search/query to empty strings
parsedUrl.hash = '';
parsedUrl.search = '';

// Reconstruct the URL without hash and query
const urlWithoutHashAndQuery = parsedUrl.toString();

return urlWithoutHashAndQuery;
}
// hashContent function to hash the content of a file
async function hashContent(content) {
const hash = createHash('sha256');
Expand Down Expand Up @@ -85,8 +98,10 @@ app.post('/hash', async (req, res) => {
if (!Array.isArray(urls))
urls = [urls];

const promises = urls.map(async (urls) => {
const hashedContent = await fetchAndHashContent(urls);
const promises = urls.map(async (url) => {
const urlWithoutHashAndQuery = parseUrlWithoutHashAndQuery(url);
console.log(url, `Fetching and hashing ${urlWithoutHashAndQuery}`);
const hashedContent = await fetchAndHashContent(urlWithoutHashAndQuery);
const fileHash = await hashContent(Buffer.from(hashedContent, 'utf-8'));
return { urls, fileHash };
});
Expand Down
2 changes: 1 addition & 1 deletion index.min.js

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

0 comments on commit ee94988

Please sign in to comment.