From 3ab43e460ccf2ea0374e3dfb49f44bd59c6b4bb2 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Fri, 1 Mar 2019 16:55:01 -0600 Subject: [PATCH 01/12] add docs for workers --- docs/USING_ADVANCED.md | 80 ++++++++++++++++++++++++++++++++++++++---- docs/index.html | 1 + 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 6490caa782..3bc4e32f74 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -14,12 +14,12 @@ marked(markdownString [,options] [,callback]) ```js // Create reference instance -var myMarked = require('marked'); +var marked = require('marked'); // Set options // `highlight` example uses `highlight.js` -myMarked.setOptions({ - renderer: new myMarked.Renderer(), +marked.setOptions({ + renderer: new marked.Renderer(), highlight: function(code) { return require('highlight.js').highlightAuto(code).value; }, @@ -34,7 +34,7 @@ myMarked.setOptions({ }); // Compile -console.log(myMarked('I am using __markdown__.')); +console.log(marked(markdownString)); ```

Options

@@ -64,7 +64,7 @@ console.log(myMarked('I am using __markdown__.')); 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. ```js -myMarked.setOptions({ +marked.setOptions({ highlight: function(code, lang, callback) { require('pygmentize-bundled') ({ lang: lang, format: 'html' }, code, function (err, result) { callback(err, result.toString()); @@ -72,7 +72,75 @@ myMarked.setOptions({ } }); -console.log(myMarked(markdownString)); +console.log(marked(markdownString)); ``` 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. + +

Workers

+ +To prevent ReDoS attacks you can run marked on a worker and shut it down when parsing takes longer than usual. + +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. + +### Node Worker Thread + +```js +// markedWorker.js + +const marked = require('marked'); +const { parentPort } = require('worker_threads'); + +parentPort.on('message', (markdownString) => { + parentPort.postMessage(marked(markdownString)); +}); +``` + +```js +// index.js + +const { Worker } = require('worker_threads'); +const markedWorker = new Worker('markedWorker.js'); + +const markedTimeout = setTimeout(() => { + markedWorker.terminate(); + throw new Error('Marked took too long!'); +}, timeoutLimit); + +markedWorker.on('message', (html) => { + clearTimeout(markedTimeout); + console.log(html); +}); + +markedWorker.postMessage(markdownString); +``` + +### Web Worker + +```js +// markedWorker.js + +importScripts('path/to/marked.min.js'); + +onmessage = function (markdownString) { + postMessage(marked(markdownString)); +}; +``` + +```html + +``` diff --git a/docs/index.html b/docs/index.html index 07a4d5ee4c..6aed62796c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -148,6 +148,7 @@

Marked.js Documentation

  • From bc812ccaf14518000ff66a93702a13eec36df03f Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 2 Mar 2019 15:04:28 -0600 Subject: [PATCH 02/12] Update docs/USING_ADVANCED.md Co-Authored-By: UziTech --- docs/USING_ADVANCED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 3bc4e32f74..c3797bcf30 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -136,7 +136,7 @@ var markedTimeout = setTimeout(() => { throw new Error('Marked took too long!'); }, timeoutLimit); -markedWorker.onmessage = function (html) => { +markedWorker.onmessage = (html) => { clearTimeout(markedTimeout); console.log(html); }); From b551b8809c23ba5439910a0d0f558aa1377373bd Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 2 Mar 2019 15:05:08 -0600 Subject: [PATCH 03/12] Update docs/USING_ADVANCED.md Co-Authored-By: UziTech --- docs/USING_ADVANCED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index c3797bcf30..4d3d7e2ad8 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -139,7 +139,7 @@ var markedTimeout = setTimeout(() => { markedWorker.onmessage = (html) => { clearTimeout(markedTimeout); console.log(html); -}); +}; markedWorker.postMessage(markdownString); From 6d37c182e2a6355e9af4314817cc4a8f9a950ea4 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 2 Mar 2019 15:05:20 -0600 Subject: [PATCH 04/12] Update docs/USING_ADVANCED.md Co-Authored-By: UziTech --- docs/USING_ADVANCED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 4d3d7e2ad8..4060135036 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -122,7 +122,7 @@ markedWorker.postMessage(markdownString); importScripts('path/to/marked.min.js'); -onmessage = function (markdownString) { +onmessage = (markdownString) => { postMessage(marked(markdownString)); }; ``` From 888a610a2f57691759f14927deff0bd263e818e1 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sat, 2 Mar 2019 23:55:07 -0600 Subject: [PATCH 05/12] terminate it --- docs/USING_ADVANCED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 4060135036..f600e121c1 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -79,7 +79,7 @@ In both examples, `code` is a `string` representing the section of code to pass

    Workers

    -To prevent ReDoS attacks you can run marked on a worker and shut it down when parsing takes longer than usual. +To prevent ReDoS attacks you can run marked on a worker and terminate it when parsing takes longer than usual. 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. From 431da38397fff3d64d83b0a18a9f4105cebb2740 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 25 Apr 2019 16:16:56 -0500 Subject: [PATCH 06/12] terminate on success --- docs/USING_ADVANCED.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index f600e121c1..859e5bd4b1 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -100,7 +100,7 @@ parentPort.on('message', (markdownString) => { // index.js const { Worker } = require('worker_threads'); -const markedWorker = new Worker('markedWorker.js'); +const markedWorker = new Worker('./markedWorker.js'); const markedTimeout = setTimeout(() => { markedWorker.terminate(); @@ -110,6 +110,7 @@ const markedTimeout = setTimeout(() => { markedWorker.on('message', (html) => { clearTimeout(markedTimeout); console.log(html); + markedWorker.terminate(); }); markedWorker.postMessage(markdownString); @@ -122,7 +123,8 @@ markedWorker.postMessage(markdownString); importScripts('path/to/marked.min.js'); -onmessage = (markdownString) => { +onmessage = (e) => { + var markdownString = e.data postMessage(marked(markdownString)); }; ``` @@ -136,9 +138,11 @@ var markedTimeout = setTimeout(() => { throw new Error('Marked took too long!'); }, timeoutLimit); -markedWorker.onmessage = (html) => { +markedWorker.onmessage = (e) => { + var html = e.data; clearTimeout(markedTimeout); console.log(html); + markedWorker.terminate(); }; markedWorker.postMessage(markdownString); From 64716578e6c21bdca67877356df7aad4d5ac0086 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 25 Apr 2019 16:20:21 -0500 Subject: [PATCH 07/12] clearTimeout first --- docs/USING_ADVANCED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 859e5bd4b1..6c6ef81054 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -139,8 +139,8 @@ var markedTimeout = setTimeout(() => { }, timeoutLimit); markedWorker.onmessage = (e) => { - var html = e.data; clearTimeout(markedTimeout); + var html = e.data; console.log(html); markedWorker.terminate(); }; From e493b75bf265fdf980729e4782e07bbf9f1da721 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Thu, 25 Apr 2019 16:22:24 -0500 Subject: [PATCH 08/12] use const --- docs/USING_ADVANCED.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 6c6ef81054..359942ba62 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -14,7 +14,7 @@ marked(markdownString [,options] [,callback]) ```js // Create reference instance -var marked = require('marked'); +const marked = require('marked'); // Set options // `highlight` example uses `highlight.js` @@ -124,23 +124,23 @@ markedWorker.postMessage(markdownString); importScripts('path/to/marked.min.js'); onmessage = (e) => { - var markdownString = e.data + const markdownString = e.data postMessage(marked(markdownString)); }; ``` ```html ``` From 0d1f92392b8696f1474334abc20d18f456f2090e Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 26 Apr 2019 20:28:05 -0500 Subject: [PATCH 12/12] relative markedWorker.js Co-Authored-By: UziTech --- docs/USING_ADVANCED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 116fbc6f2f..7978e33083 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -138,7 +138,7 @@ onmessage = (e) => { ```js // script.js -const markedWorker = new Worker('markedWorker.js'); +const markedWorker = new Worker('./markedWorker.js'); const markedTimeout = setTimeout(() => { markedWorker.terminate();