mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-09-16 21:35:31 -04:00
stats lambda: better caching (or lack thereof) & error handling
This commit is contained in:
+65
-45
@@ -1,60 +1,80 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
|
|
||||||
exports.handler = function (event, context, callback) {
|
exports.handler = function (event, context, callback) {
|
||||||
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
|
try {
|
||||||
const noScript = event.queryStringParameters["noscript"] === "true";
|
// 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
|
// https://docs.simpleanalytics.com/without-javascript
|
||||||
const endpointPath = noScript ? "noscript.gif" : "simple.gif";
|
const endpointHost = "queue.simpleanalyticscdn.com";
|
||||||
const endpointUrl = "https://queue.simpleanalyticscdn.com/" + endpointPath;
|
const endpointPath = noScript ? "noscript.gif" : "simple.gif";
|
||||||
|
const endpointUrl = "https://" + endpointHost + "/" + endpointPath;
|
||||||
|
|
||||||
// pass these headers along to SA
|
// pass these headers along to SA
|
||||||
const reqHeaders = {
|
const reqHeaders = {
|
||||||
referer: event.headers["referer"] || "",
|
referer: event.headers["referer"] || "",
|
||||||
"user-agent": event.headers["user-agent"] || "",
|
"user-agent": event.headers["user-agent"] || "",
|
||||||
};
|
};
|
||||||
|
|
||||||
// pass these URL parameters along to SA
|
// pass these URL parameters along to SA
|
||||||
const reqQuery = event.queryStringParameters;
|
const reqQuery = event.queryStringParameters;
|
||||||
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
|
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
|
||||||
|
|
||||||
const reqConfig = {
|
const reqConfig = {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
url: endpointUrl,
|
url: endpointUrl,
|
||||||
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} ...`);
|
||||||
|
|
||||||
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}`;
|
||||||
|
|
||||||
// imitate the headers that would normally be sent back from SA's pixel
|
console.info(response.status, apiFeedback);
|
||||||
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"],
|
|
||||||
};
|
|
||||||
|
|
||||||
callback(null, {
|
// imitate the headers that would normally be sent back from SA's pixel
|
||||||
statusCode: response.status,
|
const resHeaders = {
|
||||||
headers: resHeaders,
|
"Content-Type": "image/gif",
|
||||||
body: response.data.toString("base64"),
|
"Cache-Control": "private, no-cache, no-store, must-revalidate",
|
||||||
isBase64Encoded: true,
|
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 (error) {
|
||||||
.catch(function (error) {
|
// something went VERY wrong...
|
||||||
// this indicates a function error, NOT an error returned from SA
|
console.log(error.message);
|
||||||
console.log(error);
|
|
||||||
|
|
||||||
callback(null, {
|
callback(null, {
|
||||||
statusCode: 500,
|
statusCode: 500,
|
||||||
});
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
error: error.message,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
Reference in New Issue
Block a user