Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20]
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ A simple Cloudfront worker with Netacea built in.

## 💡 Getting Started

Please use NodeJs 22 for both development and production deployments.

Install required dependencies:

```bash
Expand All @@ -21,9 +23,13 @@ npm ci
If you're upgrading from a previous version of the Netacea CloudFront Worker Template,
most of the time you should be able to upgrade by running `npm install --save @netacea/cloudfront@latest`.

However, if you are upgrading from v5 to v6, then please see the
If you are upgrading from v5 to v6, then please see the
[v5 to v6 upgrade guide.](./docs/upgrading_v5_to_v6.md)

For an upgrade from v6 to v7, see the
[v6 to v7 upgrade guide.](./docs/upgrading_v6_to_v7.md)


## 💻 Developing

If you need to extend or enhance the functionality of the Cloudfront Worker, the documentation can be found [here](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html).
Expand Down
46 changes: 46 additions & 0 deletions docs/upgrading_v6_to_v7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ⬆ Netacea CloudFront v6 to v7 Upgrade Guide

This template has been updated to support the **v7** release of `@netacea/cloudfront`.
This version introduces changes to the interface,
requiring updates to how the module is used.

This guide walks you through the required changes and best practices when using v7.

You can install v7 using `npm install --save @netacea/cloudfront@7`.

## What's Changed?

### 1. New Request Handler Method

Requests should now be processed by the `handleRequest` method on the
Netacea worker. This is named to be consistent with the
new `handleResponse` method - see below.

**Before (v6):**
```ts
const netaceaResponse = await worker.run(event)
```

**After (v7):**
```ts
const netaceaResponse = await worker.handleRequest(event)
```

### 2. New Response Handler Method

Responses should now be processed by the `handleResponse` method on the
Netacea worker. This new method is asynchronous and therefore must be waited.
This updated interface allows the worker to use asynchronous APIs,
such as the encryption methods available in NodeJs, during the response handler
phase - which was not possible with the old interface.

**Before (v6):**
```ts
worker.addNetaceaCookiesToResponse(event)
void worker.ingest(event)
```

**After (v7):**
```ts
await worker.handleResponse(event)
```
38 changes: 4 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"xo": "^0.55.0"
},
"dependencies": {
"@netacea/cloudfront": "^6.0.78"
"@netacea/cloudfront": "^7.0.1"
},
"xo": {
"space": true,
Expand Down
8 changes: 3 additions & 5 deletions src/NetaceaUnified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const viewerRequestHandler: Handler = async (
context: Context,
callback: Callback<CloudFrontRequest | CloudFrontResultResponse>,
): Promise<void> => {
const netaceaResponse = await worker.run(event)
const netaceaResponse = await worker.handleRequest(event)

if (netaceaResponse.respondWith !== undefined) {
callback(null, netaceaResponse.respondWith)
Expand All @@ -66,8 +66,7 @@ export const originResponseHandler: Handler = async (
callback: Callback<CloudFrontResponse>,
): Promise<void> => {
if (Number(event.Records[0].cf.response.status) >= 400) {
worker.addNetaceaCookiesToResponse(event)
void worker.ingest(event)
await worker.handleResponse(event)
}

callback(null, event.Records[0].cf.response)
Expand All @@ -79,8 +78,7 @@ export const viewerResponseHandler: Handler = async (
callback: Callback<CloudFrontResponse>,
): Promise<void> => {
if (Number(event.Records[0].cf.response.status) < 400) {
worker.addNetaceaCookiesToResponse(event)
void worker.ingest(event)
await worker.handleResponse(event)
}

callback(null, event.Records[0].cf.response)
Expand Down
6 changes: 2 additions & 4 deletions src/OriginResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export const handler: Handler = async (
callback: Callback<CloudFrontResponse>,
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false
// Your code here
// These should be ran at the very end of the ViewerResponse, just before calling the callback.

if (Number(event.Records[0].cf.response.status) >= 400) {
worker.addNetaceaCookiesToResponse(event)
void worker.ingest(event)
await worker.handleResponse(event)
}

callback(null, event.Records[0].cf.response)
Expand Down
4 changes: 3 additions & 1 deletion src/ViewerRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const handler: Handler = async (
callback: Callback<CloudFrontRequest | CloudFrontResultResponse>,
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false
const netaceaResponse = await worker.run(event)

const netaceaResponse = await worker.handleRequest(event)

if (netaceaResponse.respondWith !== undefined) {
callback(null, netaceaResponse.respondWith)
return
Expand Down
6 changes: 2 additions & 4 deletions src/ViewerResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ export const handler: Handler = async (
callback: Callback<CloudFrontResponse>,
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false
// Your code here
// These should be ran at the very end of the ViewerResponse, just before calling the callback.

if (Number(event.Records[0].cf.response.status) < 400) {
worker.addNetaceaCookiesToResponse(event)
void worker.ingest(event)
await worker.handleResponse(event)
}

callback(null, event.Records[0].cf.response)
Expand Down