Skip to content

Commit

Permalink
fix: use a single type checking module (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Feb 12, 2019
1 parent 894cc5b commit fdaa27e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 43 deletions.
12 changes: 4 additions & 8 deletions lib/internal/checkUrl.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

const linkObj = require('./linkObj');
const reasons = require('./messages').reasons;
const simpleResponse = require('./simpleResponse');

const bhttp = require('bhttp');
const extend = require('extend');
const isString = require('is-string');
const is = require('@sindresorhus/is');

/*
Checks a URL to see if it's broken or not.
Expand All @@ -14,7 +13,7 @@ function checkUrl(link, baseUrl, cache, options, retry) {
let cached;

if (retry === undefined) {
if (isString(link) === true) {
if (is.string(link)) {
link = linkObj(link);
linkObj.resolve(link, baseUrl, options);
}
Expand All @@ -31,12 +30,9 @@ function checkUrl(link, baseUrl, cache, options, retry) {
if (cached !== undefined) {
return Promise.resolve(cached).then(response => {
// Cloned to avoid unexpected mutations as a result of user changes
response = extend({}, response);

response = Object.assign({}, response);
copyResponseData(response, link, options);

link.http.cached = true;

return link;
});
}
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/linkObj.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
const isString = require('is-string');

const is = require('@sindresorhus/is');
const urllib = require('url');
const urlobj = require('urlobj');

const hasOwnProperty = Object.prototype.hasOwnProperty;

function linkObj(url) {
if (url === undefined || isString(url) === false) {
if (url === undefined || is.string(url) === false) {
url = null;
}

Expand Down
7 changes: 3 additions & 4 deletions lib/internal/parseHtml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const isStream = require('is-stream');
const isString = require('is-string');
const is = require('@sindresorhus/is');
const parse5 = require('parse5');

const treeAdapter = Object.create(parse5.treeAdapters.default);
Expand Down Expand Up @@ -40,15 +39,15 @@ function getAttrMap(attrs) {
*/
function parseHtml(input) {
return new Promise((resolve, reject) => {
if (isStream(input) === true) {
if (is.nodeStream(input)) {
const parser = new parse5.ParserStream(options);

parser.on('finish', () => {
resolve(parser.document);
});

input.pipe(parser);
} else if (isString(input) === true) {
} else if (is.string(input) === true) {
resolve(parse5.parse(input, options));
} else {
reject('Invalid input');
Expand Down
13 changes: 1 addition & 12 deletions lib/internal/scrapeHtml.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const linkObj = require('./linkObj');
const tags = require('./tags');

const condenseWhitespace = require('condense-whitespace');
const parseMetaRefresh = require('http-equiv-refresh');
const RobotDirectives = require('robot-directives');
Expand Down Expand Up @@ -205,18 +205,14 @@ function getNthIndex(node) {
function getSelector(node) {
let name;
const selector = [];

while (node.nodeName !== '#document') {
name = node.nodeName;

// Only one of these are ever allowed -- so, index is unnecessary
if (name !== 'html' && (name !== 'body') & (name !== 'head')) {
name += ':nth-child(' + getNthIndex(node) + ')';
}

// Building backwards
selector.push(name);

node = node.parentNode;
}

Expand All @@ -243,29 +239,22 @@ function getText(node) {
function stringifyNode(node) {
let result = '<' + node.nodeName;
const numAttrs = node.attrs.length;

for (let i = 0; i < numAttrs; i++) {
result += ' ' + node.attrs[i].name + '="' + node.attrs[i].value + '"';
}

result += '>';

return result;
}

function walk(node, callback) {
let childNode, i;

if (callback(node) === false) return false;

if (node.childNodes) {
i = 0;
childNode = node.childNodes[i];
}

while (childNode) {
if (walk(childNode, callback) === false) return false;

childNode = node.childNodes[++i];
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/public/HtmlChecker.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

const is = require('@sindresorhus/is');
const linkTypes = require('link-types').map;
const maybeCallback = require('maybe-callback');
const RobotDirectives = require('robot-directives');

const linkObj = require('../internal/linkObj');
const matchUrl = require('../internal/matchUrl');
const parseHtml = require('../internal/parseHtml');
const parseOptions = require('../internal/parseOptions');
const scrapeHtml = require('../internal/scrapeHtml');

const UrlChecker = require('./UrlChecker');

const isString = require('is-string');
const linkTypes = require('link-types').map;
const maybeCallback = require('maybe-callback');
const RobotDirectives = require('robot-directives');

function HtmlChecker(options, handlers) {
const thisObj = this;

Expand Down Expand Up @@ -201,7 +201,7 @@ function excludeLink(link, instance) {
// Undocumented handler for custom constraints
const externalFilter = maybeCallback(instance.handlers._filter)(link);

if (isString(externalFilter) === true) {
if (is.string(externalFilter) === true) {
return externalFilter;
}
/*else if (externalFilter === false)
Expand Down
5 changes: 3 additions & 2 deletions lib/public/UrlChecker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const checkUrl = require('../internal/checkUrl');
const parseOptions = require('../internal/parseOptions');
const isString = require('is-string');
const is = require('@sindresorhus/is');
const maybeCallback = require('maybe-callback');
const RequestQueue = require('limited-request-queue');
const UrlCache = require('urlcache');
Expand Down Expand Up @@ -67,7 +68,7 @@ UrlChecker.prototype.dequeue = function(id) {

UrlChecker.prototype.enqueue = function(url, baseUrl, customData) {
// Undocumented internal use: enqueue(linkObj)
if (isString(url) === false && url.broken_link_checker === true) {
if (!is.string(url) && url.broken_link_checker) {
return this.linkQueue.enqueue({
url: url.url.parsed,
data: { customData: customData, linkObj: url }
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
"linkinator": "bin/blc"
},
"dependencies": {
"@sindresorhus/is": "^0.15.0",
"bhttp": "^1.2.4",
"calmcard": "^0.1.1",
"chalk": "^2.4.2",
"char-spinner": "^1.0.1",
"condense-whitespace": "^1.0.0",
"default-user-agent": "^1.0.0",
"errno": "^0.1.7",
"extend": "^3.0.2",
"gaxios": "^1.5.1",
"http-equiv-refresh": "^1.0.0",
"humanize-duration": "^3.17.0",
"is-stream": "^1.0.1",
"is-string": "^1.0.4",
"limited-request-queue": "^2.0.0",
"link-types": "^1.1.0",
"maybe-callback": "^2.1.0",
Expand Down
9 changes: 4 additions & 5 deletions test/3.internal.streamHtml.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

const messages = require('../lib/internal/messages');
const streamHtml = require('../lib/internal/streamHtml');

const helpers = require('./helpers');

const expect = require('chai').expect;
const isStream = require('is-stream');
const is = require('@sindresorhus/is');
const UrlCache = require('urlcache');

let conn;
Expand All @@ -27,7 +26,7 @@ describe('INTERNAL -- streamHtml', () => {
null,
helpers.options()
).then(result => {
expect(isStream(result.stream)).to.be.true;
expect(is.nodeStream(result.stream)).to.be.true;
expect(result.response.url).to.equal(
conn.absoluteUrl + '/normal/no-links.html'
);
Expand All @@ -40,7 +39,7 @@ describe('INTERNAL -- streamHtml', () => {
null,
helpers.options()
).then(result => {
expect(isStream(result.stream)).to.be.true;
expect(is.nodeStream(result.stream)).to.be.true;
expect(result.response.url).to.equal(
conn.absoluteUrl + '/redirect/redirected.html'
);
Expand Down

0 comments on commit fdaa27e

Please sign in to comment.