Skip to content

Commit

Permalink
chore: add more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sznowicki committed Apr 21, 2024
1 parent 6f0efd6 commit bccabfc
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 30 deletions.
1 change: 1 addition & 0 deletions index-sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export default {
'https://fastify.dev',
'https://nestjs.com',
'https://docs.drone.io',
'https://www.jenkins.io',

],
'magazines': [
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"scripts": {
"test": "node --test",
"cover": "c8 --all node --test",
"cover": "c8 --reporter lcov --all node --test",
"crawl:auto": "node -r dotenv/config src/crawl-auto.js",
"crawl:roots": "node -r dotenv/config src/crawl-roots.js",
"crawl:roots:all": "node -r dotenv/config src/crawl-roots.js --all",
Expand Down
39 changes: 38 additions & 1 deletion src/helpers/__tests__/urlHelpers.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import { hrefSeemsUseful } from '../urlHelpers.js';
import { hrefSeemsUseful, isForbidden } from '../urlHelpers.js';

describe('hrefSeemsUseful', () => {
it('should return true for relative links', () => {
Expand Down Expand Up @@ -48,3 +48,40 @@ describe('hrefSeemsUseful', () => {
assert.strictEqual(hrefSeemsUseful('data:foo/bar'), false);
});
});

describe('isForbidden', () => {
describe('curl.se', () => {
it('should allow home page', () => {
assert.strictEqual(isForbidden('https://curl.se/'), false);
assert.strictEqual(isForbidden('https://curl.se'), false);
});
it('should allow docs', () => {
assert.strictEqual(isForbidden('https://curl.se/docs'), false);
assert.strictEqual(isForbidden('https://curl.se/docs/'), false);
assert.strictEqual(isForbidden('https://curl.se/docs/foo'), false);
});

it('should deny else', () => {
assert.strictEqual(isForbidden('https://curl.se/foo'), true);
assert.strictEqual(isForbidden('https://curl.se/foo/bar'), true);
});
});

describe('jenkins.io', () => {
it('should allow /doc', () => {
assert.strictEqual(isForbidden('https://www.jenkins.io/doc'), false);
assert.strictEqual(isForbidden('https://www.jenkins.io/doc/'), false);
assert.strictEqual(isForbidden('https://www.jenkins.io/doc/foo'), false);
});
it('should allow /security', () => {
assert.strictEqual(isForbidden('https://www.jenkins.io/security'), false);
assert.strictEqual(isForbidden('https://www.jenkins.io/security/'), false);
assert.strictEqual(isForbidden('https://www.jenkins.io/security/foo'), false);
});
it('should deny /projects', () => {
assert.strictEqual(isForbidden('https://www.jenkins.io/projects'), true);
assert.strictEqual(isForbidden('https://www.jenkins.io/projects/'), true);
assert.strictEqual(isForbidden('https://www.jenkins.io/projects/foo'), true);
});
});
});
37 changes: 37 additions & 0 deletions src/helpers/filters/__tests__/genericFilter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { makeAllowList } from '../genericFilter.js';

describe('makeAllowList', () => {
describe('example.com', () => {
let tester;
it('should make a function', () => {
tester = makeAllowList('example.com', [
'/docs',
'/blog',
]);
assert.strictEqual(typeof tester, 'function');
});

it('should abstain when different host', () => {
const result = tester('https://example.org');
assert.strictEqual(result, false);
});

it('should deny when different pathname', () => {
const result = tester('https://example.com/about');
assert.strictEqual(result, true);
});

it('should allow when allowed pathname', () => {
const result = tester('https://example.com/blog');
assert.strictEqual(result, false);
});

it('should allow when pathname is long', () => {
const result = tester('https://example.com/blog/2021/12/31');
assert.strictEqual(result, false);
});

});
});
8 changes: 0 additions & 8 deletions src/helpers/filters/curlFilter.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/helpers/filters/genericFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ export const hrefSeemsUseful = (href) => {

return true;
};

export const makeAllowList = (hostname, allowedPathsInclude) => (url) => {
// Not from the same hostname
if (url.startsWith(`https://${hostname}`) === false) return false;

// Allow home page
if (url === `https://${hostname}/` || url === `https://${hostname}`) return false;

for (const path of allowedPathsInclude) {
if (url.includes(path)) return false;
}

// Deny
return true;
};
8 changes: 0 additions & 8 deletions src/helpers/filters/mongoFilter.js

This file was deleted.

8 changes: 0 additions & 8 deletions src/helpers/filters/postgresFilter.js

This file was deleted.

23 changes: 19 additions & 4 deletions src/helpers/urlHelpers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { w3Filter } from './filters/w3filter.js';
import { githubFilter } from './filters/githubFilter.js';
import { phpFilter } from './filters/phpFilter.js';
import { mongoFilter } from './filters/mongoFilter.js';
import { postgresFilter } from './filters/postgresFilter.js';
import { curlFilter } from './filters/curlFilter.js';

import { makeAllowList } from './filters/genericFilter.js';
export { hrefSeemsUseful } from './filters/genericFilter.js';

const curlFilter = makeAllowList('curl.se', [
'/docs',
]);

const mongoFilter = makeAllowList('www.mongodb.com', [
'/docs',
]);

const postgresFilter = makeAllowList('www.postgresql.org', [
'/docs',
]);

const jenkinsFilter = makeAllowList('www.jenkins.io', [
'/doc',
'/security'
]);

const domainFilters = [
w3Filter,
githubFilter,
phpFilter,
mongoFilter,
postgresFilter,
curlFilter,
jenkinsFilter,
];

export const isForbidden = (url) => {
Expand Down

0 comments on commit bccabfc

Please sign in to comment.