Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
feat(pipeline): Add advanced extensibility support
Browse files Browse the repository at this point in the history
Adds the ability to extend the pipeline through `before` and
`after` extension points in `pre.js`
  • Loading branch information
trieloff committed Mar 18, 2019
1 parent 816ede5 commit 7aa691b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/defaults/html.pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const htmlpipe = (cont, payload, action) => {
.when(esi) // flag ESI when there is ESI in the response
.error(selectStatus(production()));


// pipe.attach.before(foo, 'fetch');
// pipe.attach.after(bar, 'flag');

action.logger.log('debug', 'Running HTML pipeline');
return pipe.run(payload);
};
Expand Down
42 changes: 35 additions & 7 deletions src/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,37 @@ class Pipeline {
this._posts = [];
// functions that are executed before each step
this._taps = [];

this.attach = function attach(ext) {
if (this.sealed) {
return;
}
if (ext && ext.before && typeof ext.before === 'object') {
Object.keys(ext.before).map(key => this.before(ext.before[key], key));
}
if (ext && ext.after && typeof ext.after === 'object') {
Object.keys(ext.after).map(key => this.after(ext.after[key], key));
}
this.sealed = true;
};

this.attach.generic = (f, name, offset = 0) => {
const re = new RegExp(`^.*\\:${name} from `);
const foundpres = this._pres
.filter(pre => pre.alias)
.findIndex(pre => re.test(pre.alias));
const foundposts = this._posts
.filter(post => post.alias)
.findIndex(post => re.test(post.alias));
if (foundpres !== -1) {
this._pres.splice(foundpres + offset, 0, f);
} else if (foundposts !== -1) {
this._posts.splice(foundpres + offset, 0, f);
}
};
this.attach.before = (f, name) => this.attach.generic(f, name, 0);

this.attach.after = (f, name) => this.attach.generic(f, name, 1);
}

/**
Expand Down Expand Up @@ -159,6 +190,7 @@ class Pipeline {
}
return args[0];
};
wrappedfunc.alias = lastfunc.alias;
this._last.push(wrappedfunc);
} else {
throw new Error('when() needs function to operate on.');
Expand Down Expand Up @@ -215,14 +247,8 @@ class Pipeline {
* - the name and code location of the function that called the function before
* @param {Function} f
*/
// eslint-disable-next-line class-methods-use-this
describe(f) {
if (!this._action
|| !this._action.logger
|| !this._action.logger.level
|| this._action.logger.level !== 'silly') {
// skip capturing the stack trace when it is mever read
return 'anonymous';
}
if (f.alias) {
return f.alias;
}
Expand Down Expand Up @@ -250,6 +276,8 @@ class Pipeline {
* context.
*/
async run(context = {}) {
// register all custom attachers to the pipeline
this.attach(this._oncef);
/**
* Reduction function used to process the pipeline functions and merge the context parameters.
* @param {Object} currContext Accumulated context
Expand Down
35 changes: 29 additions & 6 deletions test/testHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,33 @@ describe('Testing HTML Pipeline', () => {
assert.equal(result.response.body, '<p>Hello World</p>');
});

it('html.pipe detects ESI in response body', async () => {
const result = await pipe(
({ content }) => ({ response: { body: `${content.document.body.innerHTML}<esi:include src="foo.html">` } }),
it('html.pipe can be extended', async () => {
const myfunc = ({ content }) => ({ response: { body: `${content.document.body.innerHTML}<esi:include src="foo.html">` } });

let calledfoo = false;
let calledbar = false;
function foo() {
assert.equal(calledfoo, false, 'foo has not yet been called');
assert.equal(calledbar, false, 'bar has not yet been called');
calledfoo = true;
}

function bar() {
assert.equal(calledfoo, true, 'foo has been called');
assert.equal(calledbar, false, 'bar has not yet been called');
calledbar = true;
}

myfunc.before = {
fetch: foo,
};

myfunc.after = {
flag: bar,
};

await pipe(
myfunc,
{
content: {
body: 'Hello World',
Expand All @@ -171,9 +195,8 @@ describe('Testing HTML Pipeline', () => {
},
);

assert.equal(result.response.headers['X-ESI'], 'enabled');
assert.equal(result.response.headers['Content-Type'], 'text/html');
assert.equal(result.response.body, '<p>Hello World</p><esi:include src="foo.html">');
assert.equal(calledfoo, true, 'foo has been called');
assert.equal(calledbar, true, 'bar has been called');
});

it('html.pipe renders index.md from helix-cli correctly', async () => {
Expand Down

0 comments on commit 7aa691b

Please sign in to comment.