1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 17:30:28 -04:00

stats lambda: better caching (or lack thereof) & error handling

This commit is contained in:
Jake Jarvis 2020-08-19 20:56:07 -04:00
parent c55b32b221
commit ea758d5004
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
2 changed files with 74 additions and 54 deletions

View File

@ -1,60 +1,80 @@
const axios = require("axios");
exports.handler = function (event, context, callback) {
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
const noScript = event.queryStringParameters["noscript"] === "true";
try {
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
const noScript = event.queryStringParameters["noscript"] === "true";
// https://docs.simpleanalytics.com/without-javascript
const endpointPath = noScript ? "noscript.gif" : "simple.gif";
const endpointUrl = "https://queue.simpleanalyticscdn.com/" + endpointPath;
// https://docs.simpleanalytics.com/without-javascript
const endpointHost = "queue.simpleanalyticscdn.com";
const endpointPath = noScript ? "noscript.gif" : "simple.gif";
const endpointUrl = "https://" + endpointHost + "/" + endpointPath;
// pass these headers along to SA
const reqHeaders = {
referer: event.headers["referer"] || "",
"user-agent": event.headers["user-agent"] || "",
};
// pass these headers along to SA
const reqHeaders = {
referer: event.headers["referer"] || "",
"user-agent": event.headers["user-agent"] || "",
};
// pass these URL parameters along to SA
const reqQuery = event.queryStringParameters;
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
// pass these URL parameters along to SA
const reqQuery = event.queryStringParameters;
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
const reqConfig = {
method: "GET",
url: endpointUrl,
headers: reqHeaders,
params: reqQuery,
responseType: "arraybuffer",
timeout: 3000,
};
const reqConfig = {
method: "GET",
url: endpointUrl,
headers: reqHeaders,
params: reqQuery,
responseType: "arraybuffer",
timeout: 2000,
};
console.info(`Proxying ${reqQuery["type"]} to ${endpointPath} ...`);
console.info(`Proxying ${reqQuery["type"]} to ${endpointPath} ...`);
axios
.request(reqConfig)
.then(function (response) {
console.info(response.status, response.headers["simple-analytics-feedback"]);
axios
.request(reqConfig)
.then(function (response) {
// parse the feedback message from SA
const apiFeedback = response.headers["simple-analytics-feedback"] || "No feedback from Simple Analytics.";
const shortFeedback = apiFeedback.toLowerCase().includes("thanks for sending") ? "OK" : `ERROR: ${apiFeedback}`;
// imitate the headers that would normally be sent back from SA's pixel
const resHeaders = {
"cache-control": "no-cache, no-store, must-revalidate",
"content-type": response.headers["content-type"],
"x-api-feedback": response.headers["simple-analytics-feedback"],
"x-api-destination": response.headers["simple-analytics-location"],
};
console.info(response.status, apiFeedback);
callback(null, {
statusCode: response.status,
headers: resHeaders,
body: response.data.toString("base64"),
isBase64Encoded: true,
// imitate the headers that would normally be sent back from SA's pixel
const resHeaders = {
"Content-Type": "image/gif",
"Cache-Control": "private, no-cache, no-store, must-revalidate",
Expires: "0",
Pragma: "no-cache",
"X-API-Response": shortFeedback,
"X-API-Endpoint": endpointHost,
};
// reasoning for base64 encoding:
// https://community.netlify.com/t/debugging-a-function-returns-502/429/12
callback(null, {
statusCode: response.status,
headers: resHeaders,
body: response.data.toString("base64"),
isBase64Encoded: true,
});
})
.catch(function (error) {
// this indicates a function error, NOT an error returned from SA
throw error;
});
})
.catch(function (error) {
// this indicates a function error, NOT an error returned from SA
console.log(error);
} catch (error) {
// something went VERY wrong...
console.log(error.message);
callback(null, {
statusCode: 500,
});
callback(null, {
statusCode: 500,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
error: error.message,
}),
});
}
};

View File

@ -1808,14 +1808,14 @@ camelcase@^5.0.0, camelcase@^5.3.1:
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
caniuse-db@^1.0.30001090:
version "1.0.30001115"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001115.tgz#fe52adf78535b8956b3df8b36870593b12cba7a1"
integrity sha512-g1ZFLCOzTIk6QKTqqxRYV3GlW9SfeSAh2J1HYmeuj6YVnOBe7uCHybWJyWVlhCwjtQtq9B84WN1hIQMa3D5ghg==
version "1.0.30001116"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001116.tgz#77635640080024292f9f239042e7d1acd5cb88a7"
integrity sha512-v7zj0MenYwLid3ENDLNlTMBOoIQdWYcShqnXbZZuuaq11xnn2y9DfL5VtZhiIb9PInT3sWpP11GVjmC8hPFBcA==
caniuse-lite@^1.0.30000887, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111:
version "1.0.30001115"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001115.tgz#c04cd828883ba47f6f776312e0817bcc9040cfa4"
integrity sha512-NZrG0439ePYna44lJX8evHX2L7Z3/z3qjVLnHgbBb/duNEnGo348u+BQS5o4HTWcrb++100dHFrU36IesIrC1Q==
version "1.0.30001116"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001116.tgz#f3a3dea347f9294a3bdc4292309039cc84117fb8"
integrity sha512-f2lcYnmAI5Mst9+g0nkMIznFGsArRmZ0qU+dnq8l91hymdc2J3SFbiPhOJEeDqC1vtE8nc1qNQyklzB8veJefQ==
caw@^2.0.0, caw@^2.0.1:
version "2.0.1"
@ -2753,9 +2753,9 @@ ee-first@1.1.1:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
electron-to-chromium@^1.3.523:
version "1.3.534"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.534.tgz#fc7af8518dd00a5b22a24aed3f116b5d097e2330"
integrity sha512-7x2S3yUrspNHQOoPk+Eo+iHViSiJiEGPI6BpmLy1eT2KRNGCkBt/NUYqjfXLd1DpDCQp7n3+LfA1RkbG+LqTZQ==
version "1.3.539"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.539.tgz#9952fb0bf3fb4295282e7df35f6e7a2a8b89d3fd"
integrity sha512-rM0LWDIstdqfaRUADZetNrL6+zd/0NBmavbMEhBXgc2u/CC1d1GaDyN5hho29fFvBiOVFwrSWZkzmNcZnCEDog==
elliptic@^6.5.3:
version "6.5.3"