Skip to content

Commit

Permalink
Merge pull request #2349 from menghif/issue-2147
Browse files Browse the repository at this point in the history
Replace usage of valid-url package with URL parser
  • Loading branch information
manekenpix authored Oct 11, 2021
2 parents 683e20f + 6221b53 commit e1955f1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@
"rss-parser": "3.12.0",
"sanitize-html": "2.3.2",
"set-interval-async": "2.0.3",
"stoppable": "1.1.0",
"valid-url": "1.0.9"
"stoppable": "1.1.0"
},
"devDependencies": {
"@babel/core": "7.14.3",
Expand Down
3 changes: 1 addition & 2 deletions src/api/feed-discovery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"dependencies": {
"@senecacdot/satellite": "^1.x",
"cheerio": "^1.0.0-rc.5",
"got": "^11.8.1",
"valid-url": "^1.0.9"
"got": "^11.8.1"
},
"engines": {
"node": ">=12.0.0"
Expand Down
12 changes: 6 additions & 6 deletions src/api/feed-discovery/src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const got = require('got');
const cheerio = require('cheerio');
const validUrl = require('valid-url');
const { logger, createError } = require('@senecacdot/satellite');

// A middleware to check if Url provided is valid
module.exports.checkValidUrl = function checkValidUrl() {
return (req, res, next) => {
// If the URL is invalid, return 400 error
if (!validUrl.isUri(req.body.blogUrl)) {
try {
new URL(req.body.blogUrl);
// If the URL is valid, continue
next();
} catch {
// If the URL is invalid, return 400 error
next(createError(400, 'Invalid Blog URL'));
return;
}
// Else, continue
next();
};
};

Expand Down
8 changes: 4 additions & 4 deletions src/backend/utils/wiki-feed-parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fetch = require('node-fetch');
const jsdom = require('jsdom');
const { isWebUri } = require('valid-url');
const { setIntervalAsync, clearIntervalAsync } = require('set-interval-async/dynamic');

require('../lib/config');
Expand Down Expand Up @@ -90,12 +89,13 @@ module.exports = async function () {
// The name will follow the URL that goes with it, so add this feed now
// Make sure the URL for this feed is a valid http/https web URI,
// then process into a Feed object.
if (!isWebUri(currentFeedInfo.url)) {
try {
new URL(currentFeedInfo.url);
feeds.push(currentFeedInfo);
} catch {
logger.info(
`Skipping invalid wiki feed url ${currentFeedInfo.url} for author ${currentFeedInfo.author}`
);
} else {
feeds.push(currentFeedInfo);
}

currentFeedInfo = {};
Expand Down
14 changes: 8 additions & 6 deletions tools/migrate/migrate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const fetch = require('node-fetch');
const jsdom = require('jsdom');
const { isWebUri } = require('valid-url');
const fs = require('fs');

const { JSDOM } = jsdom;
const URL = 'https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List';
const FEED_URL = 'https://wiki.cdot.senecacollege.ca/wiki/Planet_CDOT_Feed_List';
const FILE = 'legacy_users.json';

const getWikiText = async (url) => {
Expand All @@ -24,8 +23,8 @@ const getWikiText = async (url) => {

// Try to fetch the feed list from 'FEED_URL'
try {
wikiText = await getWikiText(URL);
console.info(`Extracting users from ${URL}`);
wikiText = await getWikiText(FEED_URL);
console.info(`Extracting users from ${FEED_URL}`);
} catch (error) {
console.error(error);
process.exit(1);
Expand All @@ -45,10 +44,13 @@ const getWikiText = async (url) => {
if (!commentRegex.test(line) && line.startsWith('[')) {
feed = line.replace(/[[\]']/g, '');

if (feed.length && isWebUri(feed)) {
try {
new URL(feed);
// If the URL is valid, continue
[firstName, lastName] = lines[index + 1].replace(/^\s*name\s*=\s*/, '').split(' ');
users.push({ firstName, lastName, feed });
} else {
} catch {
// If the URL is invalid, display error message
console.error(`Skipping invalid wiki feed url ${feed} for author ${firstName} ${lastName}`);
}
}
Expand Down
3 changes: 1 addition & 2 deletions tools/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"license": "ISC",
"dependencies": {
"jsdom": "^16.5.2",
"node-fetch": "^2.6.1",
"valid-url": "^1.0.9"
"node-fetch": "^2.6.1"
}
}

0 comments on commit e1955f1

Please sign in to comment.