Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ jobs:
- name: install node dependencies 🔨
run: cd cdk && npm install

- name: build and test the cdk solution 🧪
run: cd cdk && npm run build && npm run test

- name: deploy it all! 🙈
run: cd cdk && npm run cdk deploy
3 changes: 0 additions & 3 deletions cdk/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ node_modules
# CDK asset staging directory
.cdk.staging
cdk.out

# Allow cloudfront distribution function to be picked up
!/cfd-fn/*.js
1 change: 1 addition & 0 deletions cdk/bin/cdk.js.map

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

1 change: 1 addition & 0 deletions cdk/cfd-fn/url-rewrite.js.map

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

16 changes: 8 additions & 8 deletions cdk/cfd-fn/url-rewrite.js → cdk/cfd-fn/url-rewrite.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// url-rewrite.js
// See other AWS examples at https://github.com/aws-samples/amazon-cloudfront-functions/)

function handler(event) {
var request = event.request;
var uri = request.uri;
const handler = (event: AWSCloudFrontFunction.Event): AWSCloudFrontFunction.Request =>
{
const { request } = event;
const { uri } = request;

// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
Expand All @@ -13,6 +11,8 @@ function handler(event) {
else if (!uri.includes('.')) {
request.uri += '/index.html';
}

return request;
}
};

export default handler;
1 change: 1 addition & 0 deletions cdk/lib/static-site-stack.js.map

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

47 changes: 35 additions & 12 deletions cdk/package-lock.json

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

3 changes: 2 additions & 1 deletion cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"@types/jest": "^27.5.2",
"@types/node": "10.17.27",
"@types/prettier": "2.6.0",
"aws-cdk": "2.53.0",
"jest": "^27.5.1",
"ts-jest": "^27.1.4",
"aws-cdk": "2.53.0",
"ts-node": "^10.9.1",
"typescript": "~3.9.7"
},
"dependencies": {
"@types/aws-cloudfront-function": "^1.0.2",
"aws-cdk-lib": "2.53.0",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
Expand Down
1 change: 1 addition & 0 deletions cdk/test/cfd-fn/url-rewrite.test.js.map

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

90 changes: 90 additions & 0 deletions cdk/test/cfd-fn/url-rewrite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import handler from '../../cfd-fn/url-rewrite';

const cffValue: AWSCloudFrontFunction.ValueObject = {
key1: { value: 'text/plain' },
};

const cffResponseCookie: AWSCloudFrontFunction.ResponseCookie = {
id: {
value: 'text/plain',
attributes: "Expires=Wed, 05 Apr 2021 07:28:00 GMT",
},
cookie1: {
value: 'val1',
attributes: "Secure; Domain=example.com; Expires=Wed, 05 Apr 2021 07:28:00 GMT",
multiValue: [
{
value: 'val1',
attributes: "Secure; Domain=example.com; Expires=Wed, 05 Apr 2021 07:28:00 GMT",
},
{
value: "val2",
attributes: "Path=/cat; Domain=example.com; Expires=Wed, 10 Jan 2021 07:28:00 GMT",
}
]
},
};

const cffResponse: AWSCloudFrontFunction.Response = {
statusCode: 200,
statusDescription: 'OK',
headers: cffValue,
cookies: cffResponseCookie,
};

const cffViewer: AWSCloudFrontFunction.Viewer = {
ip: '192.168.0.1',
};

const cffContext: AWSCloudFrontFunction.Context = {
distributionDomainName: 'd111111abcdef8.cloudfront.net',
distributionId: 'EDFDVBD6EXAMPLE',
eventType: 'viewer-response',
requestId: 'EXAMPLEntjQpEXAMPLE_SG5Z-EXAMPLEPmPfEXAMPLEu3EqEXAMPLE==',
};

function BuildEvent(uri: string) : AWSCloudFrontFunction.Event {
const cffRequest: AWSCloudFrontFunction.Request = {
method: 'GET',
uri: uri,
querystring: cffValue,
headers: cffValue,
cookies: cffValue
};

return {
version: '1.0',
context: cffContext,
viewer: cffViewer,
request: cffRequest,
response: cffResponse,
};
}

describe('testing the CloudFront Function', () => {

test('when the uri ends in / then index.html should be appended', () => {
const event = BuildEvent('/test/');
const returnedRequest = handler(event);

expect(returnedRequest).toBe(event.request);
expect(returnedRequest.uri).toBe('/test/index.html');
});

test('when the uri does not contain . then /index.html should be appended', () => {
const event = BuildEvent('/test');
const returnedRequest = handler(event);

expect(returnedRequest).toBe(event.request);
expect(returnedRequest.uri).toBe('/test/index.html');
});

test('when the uri has a file extension it should be unaltered', () => {
const event = BuildEvent('/test/file.png');
const returnedRequest = handler(event);

expect(returnedRequest).toBe(event.request);
expect(returnedRequest.uri).toBe('/test/file.png');
});

});
31 changes: 31 additions & 0 deletions cdk/tsconfig-ignore.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"lib": [
"ES2015"
],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"node_modules",
"cdk.out"
]
}

33 changes: 7 additions & 26 deletions cdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"lib": [
"es2020"
],
"declaration": true,
"target": "es5",
"module": "CommonJS",
"moduleResolution": "node",
"lib": ["es2015", "es5"],
"sourceMap": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"node_modules",
"cdk.out"
]
"allowSyntheticDefaultImports": true
}
}