[Working Prototype] Headless Renderer #180
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR explores the possibility of using puppeteer to headlessly render mechanic design function (i.e. from the command line or server-side).
What this does
@mechanic-design/headless
packagerender
command to@mechanic-design/cli
to allow headless rendering from the CLIPreview
mechanic-headless.mp4
How it works
The headless renderer expects to be pointed to the path of a built mechanic project. It will run a static express server in that directory and then dispatch a headless Chrome (using Puppeteer) to visit a specified design function and dispatch a render. The headless browser hooks into the events emitted from mechanic core to catch the render result, turn it into a Buffer and return it to the caller. Headless rendering can be called with an object of parameters that will be passed as the inputs to the design function.
In the case of the new
render
command (which is a caller of the headless package) the result will be saved to disk in a user specified file. The render command passes any flags along to the design function as inputs. Somechanic render textCanvas text.png --text "Enzo’s Red Ferrari"
will render thetextCanvas
function totext.png
with the input namedtext
set.It's using
puppeteer-core
to avoid shipping a Chromium binary with mechanic. Theheadless
package has a util to look for an installation of Google Chrome on a user's system. Alternatively the path to Chrome can be set in an ENV variable.What's more?
The idea of this being it's own package is to be flexible with use cases.
@mechanic-design/headless
can be used everywhere where you have access to node and a chrome (or chromium) executable. This includes (but is not limited to): CLIs, API endpoints, CI workflows, etc.I have created separate repo to test the headless rendering in Github Workflows, including experiments with the Web Video Codec API #178
What's next
This is mostly a working prototype to verify this approach is feasible. It needs a lot of love before it'll be ready to go in, including: