Skip to content

Instantly share code, notes, and snippets.

@joshwash joshwash/index.js Secret
Last active Jan 14, 2020

Embed
What would you like to do?
cache check
let request = require('async-request')
let tough = require('tough-cookie')
const Cookie = tough.Cookie
const url = 'https://www.example.org/'
async function start(event, context) {
const response = await request(url, {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'
}
});
let cookie = Cookie.parse(response.headers['set-cookie'].join('; '))
if(response.statusCode !== 200) {
return {
statusCode: 500,
body: new Error('Did not recieve HTTP OK (200)')
}
}
if(!response.headers.hasOwnProperty('x-litespeed-cache')) {
return {
statusCode: 500,
body: new Error('Did not find litespeed cache header')
}
}
if(response.headers['x-litespeed-cache'] !== 'hit') {
// Try again using the cookie we just got
const r2 = await request(url, {
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'
},
cookieJar: cookie
});
if(r2.headers['x-litespeed-cache'] !== 'hit') {
return {
statusCode: 500,
body: new Error('OLS Cache was not hit'),
b: r2.headers
}
}
}
return {
statusCode: 200, body: 'OK'
}
}
start().then(response => {
console.log(response)
})
module.exports = start
{
"name": "serverstatus",
"version": "1.0.0",
"description": "Checks status of things",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"app": "node index.js"
},
"author": "Josh",
"license": "ISC",
"dependencies": {
"async-request": "^1.2.0",
"tough-cookie": "^3.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.