Skip to content

Commit eb925c3

Browse files
committed
WIP
1 parent 5ac005b commit eb925c3

File tree

29 files changed

+1199
-904
lines changed

29 files changed

+1199
-904
lines changed

docs/routing.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Phases
2+
3+
1. filesystem
4+
Check if the file exists in the filesystem
5+
2. resource
6+
Don't know
7+
3. miss
8+
Don't know
9+
4. rewrite
10+
Rules for rewriting the route
11+
5. hit
12+
Don't know
13+
6. error
14+
Don't know

examples/next-image/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ provider "aws" {
2121
}
2222

2323
module "tf_next" {
24-
source = "dealmore/next-js/aws"
24+
# source = "dealmore/next-js/aws"
2525

2626
deployment_name = "terraform-next-js-example-image"
2727

@@ -30,8 +30,8 @@ module "tf_next" {
3030
}
3131

3232
# Uncomment when using in the cloned monorepo for tf-next development
33-
# source = "../.."
34-
# debug_use_local_packages = true
33+
source = "../.."
34+
debug_use_local_packages = true
3535
}
3636

3737
output "cloudfront_domain_name" {

jest.e2e.config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,4 @@ module.exports = {
88
'<rootDir>/test/**/__tests__/**/*.[jt]s?(x)',
99
'<rootDir>/test/**/?(*.)+(spec|test).[jt]s?(x)',
1010
],
11-
// We use an increased timeout here because in the worst case
12-
// AWS SAM needs to download a docker image before the test can run
13-
testTimeout: 60000,
1411
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
"@types/hjson": "^2.4.2",
1919
"@types/jest": "^26.0.19",
2020
"@types/node": "^14.0.0",
21+
"@types/unzipper": "^0.10.3",
2122
"aws-sdk": "*",
2223
"dotenv": "^8.2.0",
24+
"etl": "^0.6.12",
2325
"fs-extra": "^9.1.0",
2426
"hjson": "^3.2.2",
2527
"jest": "^26.6.3",
2628
"prettier": "^2.0.5",
2729
"tmp": "^0.2.1",
2830
"ts-jest": "^26.5.5",
29-
"typescript": "^4.2.4"
31+
"typescript": "^4.2.4",
32+
"unzipper": "^0.10.11"
3033
},
3134
"resolutions": {
3235
"aws-sdk": "2.804.0",

packages/proxy/src/__test__/handler.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,124 @@ describe('[proxy] Handler', () => {
925925
},
926926
TIMEOUT
927927
);
928+
929+
const staticFileRoutingProbes = [
930+
// ['/en/dynamic/hello', '/en/dynamic/[slug]'],
931+
// ['/en-US/dynamic/hello', '/en-US/dynamic/[slug]'],
932+
// ['/fr-BE/dynamic/hello', '/fr-BE/dynamic/[slug]'],
933+
// ['/fr/dynamic/hello', '/fr/dynamic/[slug]'],
934+
// ['/nl-BE/dynamic/hello', '/nl-BE/dynamic/[slug]'],
935+
// ['/nl-NL/dynamic/hello', '/nl-NL/dynamic/[slug]'],
936+
// ['/nl/dynamic/hello', '/nl/dynamic/[slug]'],
937+
['/hello.txt', '/hello.txt'],
938+
];
939+
test.each(staticFileRoutingProbes)(
940+
'i18n: Correct static file routing %s -> %s',
941+
async (requestPath, targetPath) => {
942+
const proxyConfig: ProxyConfig = {
943+
staticRoutes: [
944+
'/en-US/dynamic/[slug]',
945+
'/en/dynamic/[slug]',
946+
'/fr-BE/dynamic/[slug]',
947+
'/fr/dynamic/[slug]',
948+
'/nl-BE/dynamic/[slug]',
949+
'/nl-NL/dynamic/[slug]',
950+
'/nl/dynamic/[slug]',
951+
'/hello.txt',
952+
],
953+
lambdaRoutes: [],
954+
routes: [
955+
{
956+
src:
957+
'^/(?!(?:_next/.*|en\\-US|nl\\-NL|nl\\-BE|nl|fr\\-BE|fr|en)(?:/.*|$))(.*)$',
958+
dest: '$wildcard/$1',
959+
continue: true,
960+
},
961+
962+
{
963+
handle: 'filesystem',
964+
},
965+
966+
{
967+
handle: 'resource',
968+
},
969+
970+
{
971+
handle: 'miss',
972+
},
973+
{
974+
handle: 'rewrite',
975+
},
976+
{
977+
src:
978+
'^[/]?(?<nextLocale>en\\-US|nl\\-NL|nl\\-BE|nl|fr\\-BE|fr|en)?/dynamic/(?<slug>[^/]+?)(?:/)?$',
979+
dest: '/$nextLocale/dynamic/[slug]?slug=$slug',
980+
check: true,
981+
},
982+
],
983+
prerenders: {},
984+
};
985+
986+
// Prepare configServer
987+
configServer.proxyConfig = proxyConfig;
988+
989+
// Origin Request
990+
// https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#example-origin-request
991+
const event: CloudFrontRequestEvent = {
992+
Records: [
993+
{
994+
cf: {
995+
config: {
996+
distributionDomainName: 'd111111abcdef8.cloudfront.net',
997+
distributionId: 'EDFDVBD6EXAMPLE',
998+
eventType: 'origin-request',
999+
requestId:
1000+
'4TyzHTaYWb1GX1qTfsHhEqV6HUDd_BzoBZnwfnvQc_1oF26ClkoUSEQ==',
1001+
},
1002+
request: {
1003+
clientIp: '203.0.113.178',
1004+
headers: {},
1005+
method: 'GET',
1006+
origin: {
1007+
s3: {
1008+
customHeaders: {
1009+
'x-env-config-endpoint': [
1010+
{
1011+
key: 'x-env-config-endpoint',
1012+
value: configEndpoint,
1013+
},
1014+
],
1015+
'x-env-api-endpoint': [
1016+
{
1017+
key: 'x-env-api-endpoint',
1018+
value: 'example.localhost',
1019+
},
1020+
],
1021+
},
1022+
region: 'us-east-1',
1023+
authMethod: 'origin-access-identity',
1024+
domainName: 's3.localhost',
1025+
path: '',
1026+
},
1027+
},
1028+
querystring: '',
1029+
uri: requestPath,
1030+
},
1031+
},
1032+
},
1033+
],
1034+
};
1035+
1036+
const result = (await handler(event)) as CloudFrontRequest;
1037+
1038+
expect(result.origin?.s3).toEqual(
1039+
expect.objectContaining({
1040+
domainName: 's3.localhost',
1041+
path: '',
1042+
})
1043+
);
1044+
expect(result.uri).toBe(targetPath);
1045+
},
1046+
TIMEOUT
1047+
);
9281048
});

packages/proxy/src/proxy.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export class Proxy {
142142
continue;
143143
}
144144

145+
// Skip miss phase entirely because we don't support it
146+
if (phase === 'miss') {
147+
continue;
148+
}
149+
145150
// Special case to allow redirect to kick in when a continue route was touched before
146151
if (phase === 'error' && isContinue) {
147152
break;
@@ -207,8 +212,10 @@ export class Proxy {
207212
}
208213
}
209214

210-
const keys = [...Object.keys(match.groups ?? {}), 'wildcard'];
215+
const keys = [...Object.keys([...match]), 'wildcard'];
211216
const matches = [...match, wildcard];
217+
console.log('keys', keys);
218+
console.log('matches', matches);
212219

213220
isContinue = false;
214221
// The path that should be sent to the target system (lambda or filesystem)

0 commit comments

Comments
 (0)