diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 66ea3de..a5b12b6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 }} diff --git a/README.md b/README.md index 1f0edfd..851313f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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). diff --git a/docs/upgrading_v6_to_v7.md b/docs/upgrading_v6_to_v7.md new file mode 100644 index 0000000..bd83bdf --- /dev/null +++ b/docs/upgrading_v6_to_v7.md @@ -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) +``` diff --git a/package-lock.json b/package-lock.json index 83d5094..0e65566 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "@netacea/cloudfront": "^6.0.78" + "@netacea/cloudfront": "^7.0.1" }, "devDependencies": { "@types/aws-lambda": "^8.10.119", @@ -216,14 +216,13 @@ } }, "node_modules/@netacea/cloudfront": { - "version": "6.0.78", - "resolved": "https://registry.npmjs.org/@netacea/cloudfront/-/cloudfront-6.0.78.tgz", - "integrity": "sha512-AAVQlzlpih5yuLuIUJ1sA1cs6e1UPG50omoxF824QeyFLWLZ2TktAURX2tTTEWZYbpHE7OZjUW/tSTuLagND6Q==", + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/@netacea/cloudfront/-/cloudfront-7.0.1.tgz", + "integrity": "sha512-qAQkK1HPHDWWanCshU/omX3N73ipPkpJpOfTVO58mz5XqNJJntrfxSMYjzEgxB3cpenlT8pSVFgIsDfDjWeEfA==", "license": "ISC", "dependencies": { "@types/aws-lambda": "^8.10.138", "aws4": "1.11.0", - "axios": "^0.21.0", "jose": "^4.11.2", "uuid": "^10.0.0" } @@ -1104,15 +1103,6 @@ "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==", "license": "MIT" }, - "node_modules/axios": { - "version": "0.21.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz", - "integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==", - "license": "MIT", - "dependencies": { - "follow-redirects": "^1.14.0" - } - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -2879,26 +2869,6 @@ "dev": true, "license": "ISC" }, - "node_modules/follow-redirects": { - "version": "1.15.9", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", - "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "license": "MIT", - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, "node_modules/for-each": { "version": "0.3.5", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.5.tgz", diff --git a/package.json b/package.json index fb2500c..f3feb51 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "xo": "^0.55.0" }, "dependencies": { - "@netacea/cloudfront": "^6.0.78" + "@netacea/cloudfront": "^7.0.1" }, "xo": { "space": true, diff --git a/src/NetaceaUnified.ts b/src/NetaceaUnified.ts index c017809..eb82d2d 100644 --- a/src/NetaceaUnified.ts +++ b/src/NetaceaUnified.ts @@ -50,7 +50,7 @@ export const viewerRequestHandler: Handler = async ( context: Context, callback: Callback, ): Promise => { - const netaceaResponse = await worker.run(event) + const netaceaResponse = await worker.handleRequest(event) if (netaceaResponse.respondWith !== undefined) { callback(null, netaceaResponse.respondWith) @@ -66,8 +66,7 @@ export const originResponseHandler: Handler = async ( callback: Callback, ): Promise => { 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) @@ -79,8 +78,7 @@ export const viewerResponseHandler: Handler = async ( callback: Callback, ): Promise => { 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) diff --git a/src/OriginResponse.ts b/src/OriginResponse.ts index 5566abd..3cb24f4 100644 --- a/src/OriginResponse.ts +++ b/src/OriginResponse.ts @@ -19,11 +19,9 @@ export const handler: Handler = async ( callback: Callback, ): Promise => { 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) diff --git a/src/ViewerRequest.ts b/src/ViewerRequest.ts index fdfa1cc..aaec143 100644 --- a/src/ViewerRequest.ts +++ b/src/ViewerRequest.ts @@ -20,7 +20,9 @@ export const handler: Handler = async ( callback: Callback, ): Promise => { context.callbackWaitsForEmptyEventLoop = false - const netaceaResponse = await worker.run(event) + + const netaceaResponse = await worker.handleRequest(event) + if (netaceaResponse.respondWith !== undefined) { callback(null, netaceaResponse.respondWith) return diff --git a/src/ViewerResponse.ts b/src/ViewerResponse.ts index 89ef1a4..50df176 100644 --- a/src/ViewerResponse.ts +++ b/src/ViewerResponse.ts @@ -19,11 +19,9 @@ export const handler: Handler = async ( callback: Callback, ): Promise => { 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)