Skip to content

Commit 0b5aa35

Browse files
committed
Start of simple plugin system
1 parent 743426b commit 0b5aa35

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const phpPath = path.resolve(__dirname, '../php-files/php');
1515
const phpIniPath = path.resolve(__dirname, '../php-files/php.ini');
1616
const cwd = path.resolve(__dirname, '../php-files');
1717

18+
const plugins = require('./plugins');
19+
1820
async function handler(data) {
1921
await validate(data);
2022

@@ -98,7 +100,6 @@ async function handler(data) {
98100
urlPath = event.rawPath;
99101
}
100102

101-
102103
let requestHeaders;
103104
if (event.cookies) {
104105
let cookielist = '';
@@ -176,7 +177,6 @@ async function handler(data) {
176177
}
177178
}
178179

179-
180180
if (!headers['cache-control'] && response.status === 200 && (!data.hasOwnProperty('skipCacheControl') || (data.hasOwnProperty('skipCacheControl') && !data.skipCacheControl))) {
181181
let cacheControl = 'max-age=3600, s-maxage=86400';
182182

@@ -237,7 +237,6 @@ async function validate(data) {
237237
throw new Error("The event property cannot be empty.");
238238
}
239239

240-
241240
if (!data.hasOwnProperty("docRoot")) {
242241
throw new Error("The docRoot or routerScript property is required.");
243242
}
@@ -265,4 +264,6 @@ async function exists(path) {
265264
}
266265

267266
module.exports = handler;
268-
module.exports.validate = validate;
267+
module.exports.validate = validate;
268+
module.exports.registerPlugin = plugins.register;
269+
module.exports.getPlugins = plugins.getPlugins;

src/plugins.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
let plugins = [];
2+
3+
function register(plugin) {
4+
//@TODO: validate the plugin
5+
plugins.push(plugin);
6+
}
7+
8+
function getPlugins() {
9+
return plugins;
10+
}
11+
12+
async function executePreRequest(event) {
13+
let response = null;
14+
//@TODO: only loop through plugins with preRequest.
15+
//@TODO: allow multiple reponses, currently last one wins.
16+
for (let i = 0; i < plugins.length; i++) {
17+
let plugin = plugins[i];
18+
if (plugin.preRequest) {
19+
response = await plugin.preRequest(event);
20+
}
21+
}
22+
23+
return response;
24+
}
25+
26+
async function executePostRequest() {
27+
28+
}
29+
30+
module.exports.register = register;
31+
module.exports.getPlugins = getPlugins;
32+
module.exports.executePreRequest = executePreRequest;

tests/index.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ test('Error with missing event property', () => {
55
expect(async () => await serverlesswp.validate(args)).rejects.toThrow();
66
});
77

8-
98
test('Error with empty event property', () => {
109
const args = {event: '', docRoot: '/'}
1110
expect(async () => await serverlesswp.validate(args)).rejects.toThrow();
@@ -24,4 +23,12 @@ test('Error with invalid docRoot property', () => {
2423
test('Error with invalid routerScript property', () => {
2524
const args = {event: {}, docRoot: '/', routerScript: '/invalid'}
2625
expect(async () => await serverlesswp.validate(args)).rejects.toThrow();
26+
});
27+
28+
test('Plugin registration', () => {
29+
serverlesswp.registerPlugin({name: 'foo'});
30+
serverlesswp.registerPlugin({name: 'bar'});
31+
32+
const plugins = serverlesswp.getPlugins();
33+
expect(plugins).toHaveLength(2);
2734
});

tests/plugins.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const plugins = require('../src/plugins');
2+
3+
test('Plugin registration', () => {
4+
plugins.register({name: 'foo'});
5+
plugins.register({name: 'bar'});
6+
7+
const pluginList = plugins.getPlugins();
8+
expect(pluginList).toHaveLength(2);
9+
});
10+
11+
test('Do not execute non-existent preRequest', async () => {
12+
plugins.register({name: 'Test'});
13+
14+
const response = await plugins.executePreRequest({test: 'test'});
15+
expect(response).toBeNull();
16+
});
17+
18+
test('Execute preRequest', async () => {
19+
const statusCode = 200;
20+
const body = 'foo';
21+
22+
plugins.register({
23+
name: 'Hello World',
24+
preRequest: async function(event) {
25+
return {
26+
statusCode: 200,
27+
body: event.test,
28+
}
29+
},
30+
});
31+
32+
const response = await plugins.executePreRequest({test: body});
33+
expect(response.body).toEqual(body);
34+
expect(response.statusCode).toEqual(statusCode);
35+
});

0 commit comments

Comments
 (0)