Skip to content

Commit

Permalink
Merge pull request #4 from PRX/upgrades
Browse files Browse the repository at this point in the history
Upgrade to Node.js 20 and ES modules
  • Loading branch information
farski authored Feb 5, 2024
2 parents 51864ef + 8f01888 commit 8a1ea2a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sourceType": "module"
},
"rules": {
"no-console": ["off"]
"no-console": ["off"],
"import/prefer-default-export": "off",
}
}
6 changes: 3 additions & 3 deletions .github/workflows/check-project-std.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ jobs:
check-javascript:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18
node-version: 20
cache: yarn
- run: yarn install
- run: npm exec prettier -- --check "**/*.{js,json,yml}"
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 18.15.0 # Should match the AWS Lambda runtime being used
nodejs 20.9.0 # Should match the AWS Lambda runtime being used
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-alpine
FROM node:20-alpine

LABEL maintainer="PRX <[email protected]>"
LABEL org.prx.spire.publish.s3="LAMBDA_ZIP"
Expand All @@ -9,6 +9,7 @@ RUN apk add zip

RUN mkdir --parents /.prxci

ADD package.json .
ADD src/action.js .
ADD src/writer.js .

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "1.0.0",
"license": "AGPL-3.0",
"engines": {
"node": ">= 18.0.0"
"node": ">= 20.0.0"
},
"type": "module",
"repository": {
"type": "git",
"url": "git://github.com/PRX/the-count.git"
Expand Down
32 changes: 13 additions & 19 deletions src/action.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Kinesis } = require("@aws-sdk/client-kinesis");
const crypto = require("crypto");
import { KinesisClient, PutRecordCommand } from "@aws-sdk/client-kinesis";
import { createHash } from "node:crypto";

const kinesis = new Kinesis({ apiVersion: "2013-12-02" });
const kinesis = new KinesisClient({ apiVersion: "2013-12-02" });

// Base64 1x1 transparent GIF
const PIXEL = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
Expand Down Expand Up @@ -52,11 +52,7 @@ function getUserId(event) {
// Create a new user ID if there isn't one yet
// The user ID is a hash of the IP and the current timestamp
const msg = event.requestContext.http.sourceIp + new Date().getTime();
return crypto
.createHash("sha1")
.update(msg)
.digest("base64")
.substring(0, 27);
return createHash("sha1").update(msg).digest("base64").substring(0, 27);
}

function getSessionId(event, userId) {
Expand All @@ -76,11 +72,7 @@ function getSessionId(event, userId) {
new Date().getTime(),
event.requestContext.http.sourceIp,
].join("");
return crypto
.createHash("sha1")
.update(msg)
.digest("base64")
.substring(0, 27);
return createHash("sha1").update(msg).digest("base64").substring(0, 27);
}

function bakeCookie(cookieName, cookieValue, maxAge) {
Expand All @@ -96,7 +88,7 @@ function bakeCookie(cookieName, cookieValue, maxAge) {
return [cookieName, configuredValue].join("=");
}

exports.handler = async (event) => {
export const handler = async (event) => {
console.log(JSON.stringify(event));

if (event?.queryStringParameters?.persist === "false") {
Expand All @@ -112,11 +104,13 @@ exports.handler = async (event) => {
const logData = dataFromEvent(event, userId, sessionId);
const logCsv = formatToCSVLine(logData);

await kinesis.putRecord({
StreamName: process.env.ACTION_LOG_STREAM_NAME,
PartitionKey: crypto.createHash("md5").update(logCsv).digest("hex"),
Data: Buffer.from(logCsv),
});
await kinesis.send(
new PutRecordCommand({
StreamName: process.env.ACTION_LOG_STREAM_NAME,
PartitionKey: createHash("md5").update(logCsv).digest("hex"),
Data: Buffer.from(logCsv),
})
);

return {
isBase64Encoded: true,
Expand Down
6 changes: 3 additions & 3 deletions src/writer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require("fs");
import { writeFileSync } from "node:fs";

exports.handler = async (event, context) => {
export const handler = async (event, context) => {
console.log(JSON.stringify(event));

const logLines = event?.Records?.map((r) =>
Expand All @@ -11,6 +11,6 @@ exports.handler = async (event, context) => {
const filename = `actions-${+new Date()}-${context.awsRequestId}.log`;
// This directory must match the LocalMountPath of the function's
// configuration
fs.writeFileSync(`/mnt/count_files/${filename}`, logLines.join("\n"));
writeFileSync(`/mnt/count_files/${filename}`, logLines.join("\n"));
}
};

0 comments on commit 8a1ea2a

Please sign in to comment.