Skip to content

Commit

Permalink
Updates to match lastest standard requirements (patch)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrrrzzt committed Jan 16, 2020
1 parent 0084a66 commit fe620e0
Show file tree
Hide file tree
Showing 6 changed files with 772 additions and 1,233 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ If you need to see the logs you can add `debug` to your environment.
(async () => {
process.env.debug = true
const wq = require('@alheimsins/webquality')
const results = await wq('https://www.alheimsins.net')
const results = await wq('https://www.google.com')
console.log(results)
})()
```
Expand Down
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ const checkLighthouse = require('./lib/check-lighthouse')
const checkSecurityheaders = require('./lib/check-securityheaders')

module.exports = async url => {
try {
const [lighthouse, securityheaders] = await Promise.all([checkLighthouse(url), checkSecurityheaders(url)])
return [
...lighthouse,
...securityheaders
]
} catch (error) {
throw error
}
const [lighthouse, securityheaders] = await Promise.all([checkLighthouse(url), checkSecurityheaders(url)])
return [
...lighthouse,
...securityheaders
]
}
12 changes: 6 additions & 6 deletions lib/calculate-observatory-score.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const convert = {
'a+': 100,
'a': 95,
a: 95,
'a-': 90,
'b+': 85,
'b': 80,
b: 80,
'b-': 75,
'c+': 70,
'c': 65,
c: 65,
'c-': 60,
'd+': 55,
'd': 50,
d: 50,
'd-': 45,
'e+': 40,
'e': 35,
e: 35,
'e-': 30,
'f+': 25,
'f': 0
f: 0
}

module.exports = grade => convert[grade.toLowerCase()]
75 changes: 39 additions & 36 deletions lib/check-observatory.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
/*
This module is not used at the moment.
If is to be included later 'then-sleep' must be included as a dependency
*/

const axios = require('axios')
const sleep = require('then-sleep')
const qs = require('querystring')
const apiUrl = 'https://http-observatory.security.mozilla.org/api/v1'
const calculateScore = require('./calculate-observatory-score')
const logger = require('./logger')

module.exports = site => {
return new Promise(async (resolve, reject) => {
const siteUrl = new URL(site)
const host = siteUrl.host
const observatoryUrl = `${apiUrl}/analyze?host=${host}`
const options = {
hidden: true,
rescan: true
}
logger('info', ['check-observatory', host, 'initialize'])
await axios.post(observatoryUrl, qs.stringify(options))
logger('info', ['check-observatory', host, 'got data'])
module.exports = async site => {
const siteUrl = new URL(site)
const host = siteUrl.host
const observatoryUrl = `${apiUrl}/analyze?host=${host}`
const options = {
hidden: true,
rescan: true
}
logger('info', ['check-observatory', host, 'initialize'])
await axios.post(observatoryUrl, qs.stringify(options))
logger('info', ['check-observatory', host, 'got data'])

const scan = async () => {
await sleep(2500)
const { data } = await axios.get(observatoryUrl)
if (data.state === 'FINISHED') {
logger('info', ['check-observatory', host, 'finished'])
return resolve([
{
id: 'observatory',
title: 'Security',
score: calculateScore(data.grade),
grade: data.grade,
description: 'The Mozilla Observatory teaches you how to configure your sites safely and securely. https://observatory.mozilla.org'
}
])
} else if (data.error === 'site down') {
logger('error', ['check-observatory', host, data.error])
return reject(new Error(data.error))
} else {
logger('info', ['check-observatory', host, 'not finished'])
logger('info', ['check-observatory', host, JSON.stringify(data, null, 2)])
await scan()
}
const scan = async () => {
await sleep(2500)
const { data } = await axios.get(observatoryUrl)
if (data.state === 'FINISHED') {
logger('info', ['check-observatory', host, 'finished'])
return [
{
id: 'observatory',
title: 'Security',
score: calculateScore(data.grade),
grade: data.grade,
description: 'The Mozilla Observatory teaches you how to configure your sites safely and securely. https://observatory.mozilla.org'
}
]
} else if (data.error === 'site down') {
logger('error', ['check-observatory', host, data.error])
throw new Error(data.error)
} else {
logger('info', ['check-observatory', host, 'not finished'])
logger('info', ['check-observatory', host, JSON.stringify(data, null, 2)])
await scan()
}
await scan()
})
}
await scan()
}
Loading

0 comments on commit fe620e0

Please sign in to comment.