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

Commit 7d9f701

Browse files
authored
feat(pipe): uniform steps (#377)
BREAKING CHANGE: removed before(), once() and after() methods in favor of use() - added new uniform method pipe.use() - added new method pipe.attach.replace() to replace pipeline steps - pipe.attach() now public API, accepts object with before, replace and after keys - removed methods before(), once() and after()
1 parent b7fb224 commit 7d9f701

File tree

7 files changed

+299
-256
lines changed

7 files changed

+299
-256
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ module.exports.pipe = function(cont, context, action) {
5353
action.logger.log("debug", "Constructing Custom Pipeline");
5454

5555
return pipeline()
56-
.before(adjustContent)
57-
.once(cont) // required: execute the continuation function
58-
.after(cleanupContent)
56+
.use(adjustContent)
57+
.use(cont) // execute the continuation function
58+
.use(cleanupContent)
5959
}
6060
```
6161

62-
In a typical pipeline, you will add additional processing steps as `.before(require('some-module'))` or as `.after(require('some-module'))`.
62+
In a typical pipeline, you will add additional processing steps as `.use(require('some-module'))`.
6363

6464
### The Main Function
6565

@@ -138,11 +138,11 @@ Example:
138138

139139
```js
140140
new pipeline()
141-
.before(doSomething)
142-
.once(render)
143-
.after(cleanup)
141+
.use(doSomething)
142+
.use(render)
143+
.use(cleanup)
144144
.error(handleError)
145-
.after(done);
145+
.use(done);
146146
```
147147

148148
If in the above example, the `doSomething` causes an error, subsequently, `render` and `cleanup` will not be invoked. but `handleError` will. If `handleError` clears the error state (i.e. sets `context.error = null`), the `done` function will be invoked again.
@@ -151,16 +151,16 @@ If in the above example, none of the functions causes an error, the `handleError
151151

152152
### Extension Points
153153

154-
In addition to the (optional) wrapper function which can be invoked prior to the `once` function, pipeline creators can expose named extension points. These extension points allow users of a pipeline to inject additional functions that will be called right before or right after an extension point. To keep the extension points independent from the implementation (i.e. the name of the function), pipeline authors should use the `expose(name)` function to expose a particular extension point.
154+
In addition to the (optional) wrapper function which can be invoked prior to the `once` function, pipeline creators can expose named extension points. These extension points allow users of a pipeline to inject additional functions that will be called right before, right after or instead of an extension point. To keep the extension points independent from the implementation (i.e. the name of the function), pipeline authors should use the `expose(name)` function to expose a particular extension point.
155155

156156
Example:
157157

158158
```js
159159
new pipeline()
160-
.before(doSomething).expose('init')
161-
.once(render)
162-
.after(cleanup).expose('cleanup')
163-
.after(done);
160+
.use(doSomething).expose('init')
161+
.use(render)
162+
.use(cleanup).expose('cleanup')
163+
.use(done);
164164
```
165165

166166
In this example, two extension points, `init` and `cleanup` have been defined. Note how the name of the extension point can be the same as the name of the function (i.e. `cleanup`), but does not have to be the same (i.e. `init` vs. `doSomething`).
@@ -181,6 +181,7 @@ The easiest way to use extension points is by expanding on the [Wrapper Function
181181

182182
- a `before` object
183183
- an `after` object
184+
- a `replace` object
184185

185186
Each of these objects can have keys that correspond to the named extension points defined for the pipeline.
186187

@@ -198,6 +199,12 @@ module.exports.after = {
198199
// will get called after the "fetch" pipeline step
199200
}
200201
}
202+
203+
module.exports.replace = {
204+
meta: (context, action) => {
205+
// will get called instead of the "meta" pipeline step
206+
}
207+
}
201208
```
202209

203210
All functions that are using the `before` and `after` extension points need to follow the same interface that all other pipeline functions follow, i.e. they have access to `context` and `action` and they should return a modified `context` object.

src/defaults/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Pipeline = require('../pipeline.js');
2424
*/
2525
function pipe(next, context, action) {
2626
const mypipeline = new Pipeline(action);
27-
mypipeline.once(next);
27+
mypipeline.use(next);
2828
return mypipeline.run(context);
2929
}
3030

src/defaults/html.pipe.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,31 @@ const htmlpipe = (cont, context, action) => {
5959
.every(dump.record)
6060
.every(validate).when(() => !production())
6161
.every(timer.update)
62-
.before(resolveRef).expose('resolve').when(hascontent)
63-
.before(fetch).expose('fetch').when(hascontent)
64-
.before(parse).expose('parse')
65-
.before(parseFrontmatter)
66-
.before(embeds)
67-
.before(smartypants)
68-
.before(sections)
69-
.before(meta).expose('meta')
70-
.before(unwrapSoleImages)
71-
.before(selectstrain)
72-
.before(selecttest)
73-
.before(html).expose('html')
74-
.before(sanitize).when(paranoid)
75-
.once(cont)
76-
.after(type('text/html'))
77-
.after(cache).when(uncached)
78-
.after(key)
79-
.after(tovdom).expose('post') // start HTML post-processing
80-
.after(removeHlxProps).when(() => production())
81-
.after(rewriteLinks).when(production)
82-
.after(addHeaders)
83-
.after(tohtml) // end HTML post-processing
84-
.after(flag).expose('esi').when(esi) // flag ESI when there is ESI in the response
85-
.after(debug)
86-
.after(timer.report)
62+
.use(resolveRef).expose('resolve').when(hascontent)
63+
.use(fetch).expose('fetch').when(hascontent)
64+
.use(parse).expose('parse')
65+
.use(parseFrontmatter)
66+
.use(embeds)
67+
.use(smartypants)
68+
.use(sections)
69+
.use(meta).expose('meta')
70+
.use(unwrapSoleImages)
71+
.use(selectstrain)
72+
.use(selecttest)
73+
.use(html).expose('html')
74+
.use(sanitize).when(paranoid)
75+
.use(cont)
76+
.use(type('text/html'))
77+
.use(cache).when(uncached)
78+
.use(key)
79+
.use(tovdom).expose('post') // start HTML post-processing
80+
.use(removeHlxProps).when(() => production())
81+
.use(rewriteLinks).when(production)
82+
.use(addHeaders)
83+
.use(tohtml) // end HTML post-processing
84+
.use(flag).expose('esi').when(esi) // flag ESI when there is ESI in the response
85+
.use(debug)
86+
.use(timer.report)
8787
.error(dump.report)
8888
.error(selectStatus);
8989

src/defaults/json.pipe.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ const jsonpipe = (cont, context, action) => {
3737
.every(dump.record)
3838
.every(validate).when(() => !production())
3939
.every(timer.update)
40-
.before(fetch).expose('fetch')
41-
.before(parse).expose('parse')
42-
.before(parseFrontmatter)
43-
.before(smartypants)
44-
.before(sections)
45-
.before(meta).expose('meta')
46-
.once(cont)
47-
.after(emit).expose('json')
48-
.after(type('application/json'))
49-
.after(timer.report)
40+
.use(fetch).expose('fetch')
41+
.use(parse).expose('parse')
42+
.use(parseFrontmatter)
43+
.use(smartypants)
44+
.use(sections)
45+
.use(meta).expose('meta')
46+
.use(cont)
47+
.use(emit).expose('json')
48+
.use(type('application/json'))
49+
.use(timer.report)
5050
.error(dump.report)
5151
.error(selectStatus(production()));
5252

src/defaults/xml.pipe.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,20 @@ const xmlpipe = (cont, context, action) => {
4141
.every(dump.record)
4242
.every(validate).when(() => !production())
4343
.every(timer.update)
44-
.before(fetch).expose('fetch')
45-
.before(parse).expose('parse')
46-
.before(parseFrontmatter)
47-
.before(smartypants)
48-
.before(sections)
49-
.before(meta).expose('meta')
50-
.once(cont)
51-
.after(emit).expose('xml')
52-
.after(type('application/xml'))
53-
.after(check)
54-
.after(cache)
55-
.when(uncached)
56-
.after(key)
57-
.after(flag).expose('esi').when(esi) // flag ESI when there is ESI in the response
58-
.after(timer.report)
44+
.use(fetch).expose('fetch')
45+
.use(parse).expose('parse')
46+
.use(parseFrontmatter)
47+
.use(smartypants)
48+
.use(sections)
49+
.use(meta).expose('meta')
50+
.use(cont)
51+
.use(emit).expose('xml')
52+
.use(type('application/xml'))
53+
.use(check)
54+
.use(cache).when(uncached)
55+
.use(key)
56+
.use(flag).expose('esi').when(esi) // flag ESI when there is ESI in the response
57+
.use(timer.report)
5958
.error(dump.report)
6059
.error(selectStatus);
6160

0 commit comments

Comments
 (0)