Skip to content

Commit c99e69b

Browse files
authored
Merge pull request #1432 from UziTech/worker-docs
add docs for workers
2 parents 67b91cf + 0d1f923 commit c99e69b

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

docs/USING_ADVANCED.md

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ marked(markdownString [,options] [,callback])
1414

1515
```js
1616
// Create reference instance
17-
var myMarked = require('marked');
17+
const marked = require('marked');
1818

1919
// Set options
2020
// `highlight` example uses `highlight.js`
21-
myMarked.setOptions({
22-
renderer: new myMarked.Renderer(),
21+
marked.setOptions({
22+
renderer: new marked.Renderer(),
2323
highlight: function(code) {
2424
return require('highlight.js').highlightAuto(code).value;
2525
},
@@ -34,7 +34,7 @@ myMarked.setOptions({
3434
});
3535

3636
// Compile
37-
console.log(myMarked('I am using __markdown__.'));
37+
console.log(marked(markdownString));
3838
```
3939

4040
<h2 id="options">Options</h2>
@@ -64,15 +64,93 @@ console.log(myMarked('I am using __markdown__.'));
6464
Unlike `highlight.js` the `pygmentize.js` library uses asynchronous highlighting. This example demonstrates that marked is agnostic when it comes to the highlighter you use.
6565

6666
```js
67-
myMarked.setOptions({
67+
marked.setOptions({
6868
highlight: function(code, lang, callback) {
6969
require('pygmentize-bundled') ({ lang: lang, format: 'html' }, code, function (err, result) {
7070
callback(err, result.toString());
7171
});
7272
}
7373
});
7474

75-
console.log(myMarked(markdownString));
75+
console.log(marked(markdownString));
7676
```
7777

7878
In both examples, `code` is a `string` representing the section of code to pass to the highlighter. In this example, `lang` is a `string` informing the highlighter what programming lnaguage to use for the `code` and `callback` is the `function` the asynchronous highlighter will call once complete.
79+
80+
<h2 id="workers">Workers</h2>
81+
82+
To prevent ReDoS attacks you can run marked on a worker and terminate it when parsing takes longer than usual.
83+
84+
Marked can be run in a [worker thread](https://nodejs.org/api/worker_threads.html) on a node server, or a [web worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) in a browser.
85+
86+
### Node Worker Thread
87+
88+
> 🚨 Node Worker Threads are [experimental](https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads) 🚨
89+
>
90+
> This implementation may change.
91+
92+
```js
93+
// markedWorker.js
94+
95+
const marked = require('marked');
96+
const { parentPort } = require('worker_threads');
97+
98+
parentPort.on('message', (markdownString) => {
99+
parentPort.postMessage(marked(markdownString));
100+
});
101+
```
102+
103+
```js
104+
// index.js
105+
106+
const { Worker } = require('worker_threads');
107+
const markedWorker = new Worker('./markedWorker.js');
108+
109+
const markedTimeout = setTimeout(() => {
110+
markedWorker.terminate();
111+
throw new Error('Marked took too long!');
112+
}, timeoutLimit);
113+
114+
markedWorker.on('message', (html) => {
115+
clearTimeout(markedTimeout);
116+
console.log(html);
117+
markedWorker.terminate();
118+
});
119+
120+
markedWorker.postMessage(markdownString);
121+
```
122+
123+
### Web Worker
124+
125+
> **NOTE**: Web Workers send the payload from `postMessage` in an object with the payload in a `.data` property
126+
127+
```js
128+
// markedWorker.js
129+
130+
importScripts('path/to/marked.min.js');
131+
132+
onmessage = (e) => {
133+
const markdownString = e.data
134+
postMessage(marked(markdownString));
135+
};
136+
```
137+
138+
```js
139+
// script.js
140+
141+
const markedWorker = new Worker('./markedWorker.js');
142+
143+
const markedTimeout = setTimeout(() => {
144+
markedWorker.terminate();
145+
throw new Error('Marked took too long!');
146+
}, timeoutLimit);
147+
148+
markedWorker.onmessage = (e) => {
149+
clearTimeout(markedTimeout);
150+
const html = e.data;
151+
console.log(html);
152+
markedWorker.terminate();
153+
};
154+
155+
markedWorker.postMessage(markdownString);
156+
```

docs/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ <h1>Marked.js Documentation</h1>
148148
<ul>
149149
<li><a href="#/USING_ADVANCED.md#options">Options</a></li>
150150
<li><a href="#/USING_ADVANCED.md#highlight">Highlighting</a></li>
151+
<li><a href="#/USING_ADVANCED.md#workers">Workers</a></li>
151152
</ul>
152153
</li>
153154
<li>

0 commit comments

Comments
 (0)