Skip to content

Commit

Permalink
Start of simple plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmac committed Feb 12, 2024
1 parent 743426b commit 0b5aa35
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const phpPath = path.resolve(__dirname, '../php-files/php');
const phpIniPath = path.resolve(__dirname, '../php-files/php.ini');
const cwd = path.resolve(__dirname, '../php-files');

const plugins = require('./plugins');

async function handler(data) {
await validate(data);

Expand Down Expand Up @@ -98,7 +100,6 @@ async function handler(data) {
urlPath = event.rawPath;
}


let requestHeaders;
if (event.cookies) {
let cookielist = '';
Expand Down Expand Up @@ -176,7 +177,6 @@ async function handler(data) {
}
}


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

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


if (!data.hasOwnProperty("docRoot")) {
throw new Error("The docRoot or routerScript property is required.");
}
Expand Down Expand Up @@ -265,4 +264,6 @@ async function exists(path) {
}

module.exports = handler;
module.exports.validate = validate;
module.exports.validate = validate;
module.exports.registerPlugin = plugins.register;
module.exports.getPlugins = plugins.getPlugins;
32 changes: 32 additions & 0 deletions src/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let plugins = [];

function register(plugin) {
//@TODO: validate the plugin
plugins.push(plugin);
}

function getPlugins() {
return plugins;
}

async function executePreRequest(event) {
let response = null;
//@TODO: only loop through plugins with preRequest.
//@TODO: allow multiple reponses, currently last one wins.
for (let i = 0; i < plugins.length; i++) {
let plugin = plugins[i];
if (plugin.preRequest) {
response = await plugin.preRequest(event);
}
}

return response;
}

async function executePostRequest() {

}

module.exports.register = register;
module.exports.getPlugins = getPlugins;
module.exports.executePreRequest = executePreRequest;
9 changes: 8 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ test('Error with missing event property', () => {
expect(async () => await serverlesswp.validate(args)).rejects.toThrow();
});


test('Error with empty event property', () => {
const args = {event: '', docRoot: '/'}
expect(async () => await serverlesswp.validate(args)).rejects.toThrow();
Expand All @@ -24,4 +23,12 @@ test('Error with invalid docRoot property', () => {
test('Error with invalid routerScript property', () => {
const args = {event: {}, docRoot: '/', routerScript: '/invalid'}
expect(async () => await serverlesswp.validate(args)).rejects.toThrow();
});

test('Plugin registration', () => {
serverlesswp.registerPlugin({name: 'foo'});
serverlesswp.registerPlugin({name: 'bar'});

const plugins = serverlesswp.getPlugins();
expect(plugins).toHaveLength(2);
});
35 changes: 35 additions & 0 deletions tests/plugins.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const plugins = require('../src/plugins');

test('Plugin registration', () => {
plugins.register({name: 'foo'});
plugins.register({name: 'bar'});

const pluginList = plugins.getPlugins();
expect(pluginList).toHaveLength(2);
});

test('Do not execute non-existent preRequest', async () => {
plugins.register({name: 'Test'});

const response = await plugins.executePreRequest({test: 'test'});
expect(response).toBeNull();
});

test('Execute preRequest', async () => {
const statusCode = 200;
const body = 'foo';

plugins.register({
name: 'Hello World',
preRequest: async function(event) {
return {
statusCode: 200,
body: event.test,
}
},
});

const response = await plugins.executePreRequest({test: body});
expect(response.body).toEqual(body);
expect(response.statusCode).toEqual(statusCode);
});

0 comments on commit 0b5aa35

Please sign in to comment.