Skip to content

Commit 9cda15b

Browse files
committed
More progress on simple plugin system
1 parent fb35973 commit 9cda15b

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

.github/workflows/e2e.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ jobs:
1515
- run: ./run-test.sh
1616
working-directory: ./tests/e2e
1717

18-
- run: curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/hi"}'
18+
- run: curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php"}' | grep -q '{"statusCode":200,"headers"'
19+
20+
- run: curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php", "postRequestPlugin": "1"}' | grep -q '{"statusCode":201,"body":"Foo"}'
21+
22+
- run: curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php", "preRequestPlugin": "1"}' | grep -q '{"statusCode":200,"body":"Foo"}'
23+
24+
- run: curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/static.css"}' | grep -q 'background-color: blue'

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"jest": "^29.5.0"
2727
},
2828
"scripts": {
29-
"test": "jest --testPathIgnorePatterns 'e2e'"
29+
"test": "jest --testPathIgnorePatterns 'e2e'",
30+
"local-tests": "npm run test && cd tests/e2e && ./local.sh"
3031
}
3132
}

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ async function handler(data) {
2121
await validate(data);
2222

2323
const { event, docRoot } = data;
24+
25+
const preRequestResponse = await plugins.executePreRequest(event);
26+
if (preRequestResponse !== null) {
27+
return preRequestResponse;
28+
}
2429

2530
if (!php) {
2631
const env = {
@@ -208,7 +213,7 @@ async function handler(data) {
208213
returnResponse.multiValueHeaders = multiHeaders;
209214
}
210215

211-
return returnResponse;
216+
return await plugins.executePostRequest(event, returnResponse);
212217
}
213218
catch (err) {
214219
console.log(err);

src/plugins.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
let plugins = [];
22

33
function register(plugin) {
4-
//@TODO: validate the plugin
4+
// Require a plugin name.
5+
if (!plugin.hasOwnProperty("name") || !plugin.name) {
6+
throw new Error("Plugins are required to have a name");
7+
}
8+
9+
// Don't allow double registration.
10+
if (plugins.find((plug) => plug.name === plugin.name)) {
11+
throw new Error("Plugins can only be registered once");
12+
}
13+
514
plugins.push(plugin);
615
}
716

@@ -19,18 +28,25 @@ async function executePreRequest(event) {
1928
}
2029
}
2130

31+
//@TODO: validate response
2232
return response;
2333
}
2434

2535
async function executePostRequest(event, response) {
2636
//@TODO: only loop through plugins with postRequest.
2737
for (let i = 0; i < plugins.length; i++) {
2838
let plugin = plugins[i];
39+
let pluginResponse;
40+
2941
if (plugin.postRequest) {
30-
response = await plugin.postRequest(event, response);
42+
pluginResponse = await plugin.postRequest(event, response);
43+
if (pluginResponse) {
44+
response = pluginResponse;
45+
}
3146
}
3247
}
3348

49+
//@TODO: validate response
3450
return response;
3551
}
3652

tests/e2e/local.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
docker run -p 9000:8080 -d --name serverlesswp-local docker-lambda-serverlesswp
66

7-
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php"}'
7+
curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php"}' | grep -q '{"statusCode":200,"headers"'
88

9-
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/static.css"}'
9+
curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php", "postRequestPlugin": "1"}' | grep -q '{"statusCode":201,"body":"Foo"}'
10+
11+
curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/index.php", "preRequestPlugin": "1"}' | grep -q '{"statusCode":200,"body":"Foo"}'
12+
13+
curl -s -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"path":"/static.css"}' | grep -q 'background-color: blue'
1014

1115
docker stop serverlesswp-local
1216
docker rm serverlesswp-local

tests/e2e/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
const serverlesswp = require('./src/index');
22
const path = require('path');
33

4+
serverlesswp.registerPlugin({
5+
name: 'postRequest',
6+
postRequest: async function(event, pluginResponse) {
7+
// Bypass this plugin conditionally
8+
if (event.postRequestPlugin) {
9+
return {
10+
statusCode: pluginResponse.statusCode + 1,
11+
body: 'Foo'
12+
}
13+
}
14+
}
15+
});
16+
17+
serverlesswp.registerPlugin({
18+
name: 'preRequest',
19+
preRequest: async function(event) {
20+
// Bypass this plugin conditionally
21+
if (event.preRequestPlugin) {
22+
return {
23+
statusCode: 200,
24+
body: 'Foo'
25+
}
26+
}
27+
}
28+
});
29+
430
exports.handler = async function (event, context, callback) {
531
const docRoot = path.join(process.cwd(), 'wp');
632
const routerScript = path.join(process.cwd(), 'router.php');

tests/plugins.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ test('Plugin registration', () => {
88
expect(pluginList).toHaveLength(2);
99
});
1010

11+
test('Plugins need a name', () => {
12+
expect(() => { plugins.register({}) }).toThrow();
13+
expect(() => { plugins.register({name: ''}) }).toThrow();
14+
});
15+
16+
test('Plugins can only be registered once', () => {
17+
plugins.register({name: 'baz'});
18+
expect(() => { plugins.register({name: 'baz'}) }).toThrow();
19+
});
20+
1121
test('Do not execute non-existent preRequest', async () => {
1222
plugins.register({name: 'Test'});
1323

0 commit comments

Comments
 (0)