Skip to content

Commit

Permalink
Initial Release v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Apr 22, 2021
1 parent 9947956 commit 0a006cb
Show file tree
Hide file tree
Showing 17 changed files with 617 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
end_of_line = lf
charset = utf-8
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"env": {
"es6": true,
"node": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 8
},
"extends": [
"eslint:recommended"
]
}
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Set the default behavior, in case people don't have `core.autocrlf` set.
* text=auto

# Declare files that will always have LF line endings on checkout.
*.js text eol=lf

# Remove files using `git archive`.
.* export-ignore
test/sitemap.js export-ignore
test/urls.js export-ignore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Dependency directories
node_modules

# misc
.idea
.coverage
.DS_Store
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
branches:
only:
- main
dist: xenial
language: node_js
node_js:
- 10
- 11
- 12
- 13
- 14
- node
os:
- linux
- osx
install:
npm install --dev
script:
- npm run lint
- npm run test:sitemap
- npm run test:urls
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# check-http-status

[![NPM version][npm-image]][npm-url]
[![Downloads][downloads-image]][npm-url]
[![Build Status][travis-image]][travis-url]

## Install

Via `npm`

```bash
npm install check-http-status --save-dev
```

Via Yarn

```bash
yarn add check-http-status --dev
```

## Usage

### Sitemap Example

```node
const checkHttpStatus = require('check-http-status');

checkHttpStatus({
'export': {
'format': 'xlsx',
'location': '/Users/trunkcode/Desktop/',
},
'options': {
'auth': {
'password': 'Testing1234',
'username': 'trunkcode'
},
'headers': {
'Accept': 'text/html',
},
},
'sitemap': 'https://www.trunkcode.com/page-sitemap.xml'
});
```

### URLs Example

```node
const checkHttpStatus = require('check-http-status');

checkHttpStatus({
'export': {
'format': 'xlsx',
'location': '/Users/trunkcode/Desktop/',
},
'options': {
'auth': {
'password': 'Testing1234',
'username': 'trunkcode'
},
'headers': {
'Accept': 'text/html',
},
},
'urls': [
'http://trunkcode.com/',
'https://example.com/',
'https://example1234.com/',
'https://www.trunkcode.com/',
'https://www.trunkcode.com/test/'
]
});
```

[npm-image]: https://img.shields.io/npm/v/check-http-status.svg
[npm-url]: https://www.npmjs.com/package/check-http-status
[downloads-image]: https://img.shields.io/npm/dt/check-http-status.svg

[travis-image]: https://api.travis-ci.com/trunkcode/check-http-status.svg?branch=main
[travis-url]: https://travis-ci.com/trunkcode/check-http-status
68 changes: 68 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const consoleColumns = require('./lib/console-columns');
const fetchFromSitemap = require('./lib/fetch-from-sitemap');
const fs = require('fs');
const generateExcel = require('./lib/generate-excel');
const httpList = require('./lib/http-list');

/**
* Main function that handles config, and trigger functions according to the
* config.
*/
async function checkHttpStatus(config) {
const allowedExportTypes = [
'csv',
'xlsx',
];
var urlsList = [];

if (!config) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing required Parameters.');
process.exit();
} else if (!config.options) {
config.options = {};
}

if (config.sitemap) {
urlsList = await fetchFromSitemap(config.sitemap);
} else if (config.urls && Array.isArray(config.urls)) {
urlsList = config.urls;
}

if (urlsList.length === 0) {
console.error('\x1b[31m%s\x1b[0m', 'Error: No URL(s) found.');
process.exit();
} else if (config.export && !config.export.location) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Missing export location.');
process.exit();
} else if (config.export && !fs.existsSync(config.export.location)) {
console.error('\x1b[31m%s\x1b[0m', 'Error: Export Location is undefined.');
process.exit();
}

const httpStatusList = await httpList(urlsList, config.options);

if (config.export && !config.export.format) {
config.export.format = 'xlsx';
}

if (config.export && allowedExportTypes.includes(config.export.format)) {
const urlLength = Math.max(...urlsList.map((el) => el.length));
const rowLength = {
'errorMessage': 50,
'requestedUrl': urlLength,
'url': urlLength + 20
};

generateExcel(httpStatusList, rowLength, config.export);
} else {
consoleColumns(httpStatusList);
}

// Add empty line
console.log();
console.log('\x1b[32m%s\x1b[0m', 'HTTP Status check completed!');
}

module.exports = checkHttpStatus;
120 changes: 120 additions & 0 deletions lib/check-status-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
'use strict';

const axios = require('axios');

/**
* Check HTTP Status as per settings and return status code and other
* information for further process.
*/
function axiosRequest(urlToCheck, axiosOptions, redirect) {
var httpStatus = [
'',
0,
'',
urlToCheck,
''
];

if (!redirect) {
httpStatus[0] = urlToCheck;
}

return new Promise((resolve) => {
axios.get(urlToCheck, axiosOptions)
.then((response) => {
httpStatus[2] = response.status;
if (redirect) {
httpStatus[1] = '';
}

resolve(httpStatus);
})
.catch(async (error) => {
var statusLists = [];

if (error.response && error.response.status) {
httpStatus[2] = error.response.status;
if (error.response.status >= 300 && error.response.status < 400) {
const redUrl = error.response.headers.location;
const checkType = await axiosRequest(redUrl, axiosOptions, true);

httpStatus[3] = error.response.headers.location;
statusLists[0] = httpStatus;

if (redirect) {
httpStatus[1] = '';
} else if (typeof checkType[0] === 'object') {
httpStatus[1] = checkType.length;
} else {
httpStatus[1] = 1;
}

if (!redirect && typeof checkType[0] === 'object') {
let childLinks = 1;
checkType.forEach((element) => {
statusLists[childLinks] = element;
childLinks += 1;
});
} else {
statusLists[1] = checkType;
}
} else {
statusLists = httpStatus;
}
} else {
httpStatus[1] = '';
httpStatus[3] = '';
if (error.message) {
httpStatus[4] = error.message;
} else {
httpStatus[4] = error;
}

statusLists = httpStatus;
}

resolve(statusLists);
});
});
}

/**
* Call main function to generate array with all the required information
* and await until that all are not completed.
*/
async function checkStatusCode(urlToCheck, options) {
const statusList = [];

var axiosOptions = {
'maxRedirects': 0
};
var statusArray = [];

// Custom headers to be sent.
if (options.headers) {
axiosOptions.headers = options.headers;
}

// Specifies the number of milliseconds before the request times out.
if (options.timeout) {
axiosOptions.timeout = options.timeout;
}

// Indicates that HTTP Basic auth should be used, and supplies credentials.
if (options.auth) {
axiosOptions.auth = options.auth;
}

statusArray = await axiosRequest(urlToCheck, axiosOptions);
if (typeof statusArray[0] === 'object') {
statusArray.forEach((row) => {
statusList.push(row);
});
} else {
statusList.push(statusArray);
}

return Promise.resolve(statusList);
}

module.exports = checkStatusCode;
36 changes: 36 additions & 0 deletions lib/console-columns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const columnify = require('columnify');

/**
* List down all the URLs with the status code in console.
*/
function terminalColumns(statusList) {
const data = [];

// Add empty line
console.log();

statusList.forEach((singleStatus) => {
data.push({
'# Redirects': singleStatus[1],
'Error Message': singleStatus[4],
'Requested URL': singleStatus[0],
'Status Code': singleStatus[2],
'URL': singleStatus[3],
});
});

console.log(columnify(data, {
'columnSplitter': '|',
'columns': [
'Requested URL',
'# Redirects',
'Status Code',
'URL',
'Error Message'
]
}));
}

module.exports = terminalColumns;
Loading

0 comments on commit 0a006cb

Please sign in to comment.