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

This commit is contained in:
jake committed 2020-08-19 20:56:07 -04:00
1 parent c55b32b221
commit ea758d5004
2 files changed
+38 -18

No files matched your search

+29 -9
View File
@@ -1,12 +1,14 @@
const axios = require("axios"); const axios = require("axios");
exports.handler = function (event, context, callback) { exports.handler = function (event, context, callback) {
try {
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true` // if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
const noScript = event.queryStringParameters["noscript"] === "true"; const noScript = event.queryStringParameters["noscript"] === "true";
// https://docs.simpleanalytics.com/without-javascript // https://docs.simpleanalytics.com/without-javascript
const endpointHost = "queue.simpleanalyticscdn.com";
const endpointPath = noScript ? "noscript.gif" : "simple.gif"; const endpointPath = noScript ? "noscript.gif" : "simple.gif";
const endpointUrl = "https://queue.simpleanalyticscdn.com/" + endpointPath; const endpointUrl = "https://" + endpointHost + "/" + endpointPath;
// pass these headers along to SA // pass these headers along to SA
const reqHeaders = { const reqHeaders = {
@@ -24,7 +26,7 @@ exports.handler = function (event, context, callback) {
headers: reqHeaders, headers: reqHeaders,
params: reqQuery, params: reqQuery,
responseType: "arraybuffer", responseType: "arraybuffer",
timeout: 3000, timeout: 2000,
}; };
console.info(`Proxying ${reqQuery["type"]} to ${endpointPath} ...`); console.info(`Proxying ${reqQuery["type"]} to ${endpointPath} ...`);
@@ -32,16 +34,24 @@ exports.handler = function (event, context, callback) {
axios axios
.request(reqConfig) .request(reqConfig)
.then(function (response) { .then(function (response) {
console.info(response.status, response.headers["simple-analytics-feedback"]); // 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}`;
console.info(response.status, apiFeedback);
// imitate the headers that would normally be sent back from SA's pixel // imitate the headers that would normally be sent back from SA's pixel
const resHeaders = { const resHeaders = {
"cache-control": "no-cache, no-store, must-revalidate", "Content-Type": "image/gif",
"content-type": response.headers["content-type"], "Cache-Control": "private, no-cache, no-store, must-revalidate",
"x-api-feedback": response.headers["simple-analytics-feedback"], Expires: "0",
"x-api-destination": response.headers["simple-analytics-location"], 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, { callback(null, {
statusCode: response.status, statusCode: response.status,
headers: resHeaders, headers: resHeaders,
@@ -51,10 +61,20 @@ exports.handler = function (event, context, callback) {
}) })
.catch(function (error) { .catch(function (error) {
// this indicates a function error, NOT an error returned from SA // this indicates a function error, NOT an error returned from SA
console.log(error); throw error;
});
} catch (error) {
// something went VERY wrong...
console.log(error.message);
callback(null, { callback(null, {
statusCode: 500, statusCode: 500,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
error: error.message,
}),
}); });
}); }
}; };
+9 -9
View File
@@ -1808,14 +1808,14 @@ camelcase@^5.0.0, camelcase@^5.3.1:
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg== integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
caniuse-db@^1.0.30001090: caniuse-db@^1.0.30001090:
version "1.0.30001115" version "1.0.30001116"
resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001115.tgz#fe52adf78535b8956b3df8b36870593b12cba7a1" resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30001116.tgz#77635640080024292f9f239042e7d1acd5cb88a7"
integrity sha512-g1ZFLCOzTIk6QKTqqxRYV3GlW9SfeSAh2J1HYmeuj6YVnOBe7uCHybWJyWVlhCwjtQtq9B84WN1hIQMa3D5ghg== integrity sha512-v7zj0MenYwLid3ENDLNlTMBOoIQdWYcShqnXbZZuuaq11xnn2y9DfL5VtZhiIb9PInT3sWpP11GVjmC8hPFBcA==
caniuse-lite@^1.0.30000887, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111: caniuse-lite@^1.0.30000887, caniuse-lite@^1.0.30001109, caniuse-lite@^1.0.30001111:
version "1.0.30001115" version "1.0.30001116"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001115.tgz#c04cd828883ba47f6f776312e0817bcc9040cfa4" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001116.tgz#f3a3dea347f9294a3bdc4292309039cc84117fb8"
integrity sha512-NZrG0439ePYna44lJX8evHX2L7Z3/z3qjVLnHgbBb/duNEnGo348u+BQS5o4HTWcrb++100dHFrU36IesIrC1Q== integrity sha512-f2lcYnmAI5Mst9+g0nkMIznFGsArRmZ0qU+dnq8l91hymdc2J3SFbiPhOJEeDqC1vtE8nc1qNQyklzB8veJefQ==
caw@^2.0.0, caw@^2.0.1: caw@^2.0.0, caw@^2.0.1:
version "2.0.1" version "2.0.1"
@@ -2753,9 +2753,9 @@ ee-first@1.1.1:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0= integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=
electron-to-chromium@^1.3.523: electron-to-chromium@^1.3.523:
version "1.3.534" version "1.3.539"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.534.tgz#fc7af8518dd00a5b22a24aed3f116b5d097e2330" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.539.tgz#9952fb0bf3fb4295282e7df35f6e7a2a8b89d3fd"
integrity sha512-7x2S3yUrspNHQOoPk+Eo+iHViSiJiEGPI6BpmLy1eT2KRNGCkBt/NUYqjfXLd1DpDCQp7n3+LfA1RkbG+LqTZQ== integrity sha512-rM0LWDIstdqfaRUADZetNrL6+zd/0NBmavbMEhBXgc2u/CC1d1GaDyN5hho29fFvBiOVFwrSWZkzmNcZnCEDog==
elliptic@^6.5.3: elliptic@^6.5.3:
version "6.5.3" version "6.5.3"