Make Nunjucks rendering work in the main process#9893
Draft
jackkav wants to merge 2 commits intoKong:developfrom
Draft
Make Nunjucks rendering work in the main process#9893jackkav wants to merge 2 commits intoKong:developfrom
jackkav wants to merge 2 commits intoKong:developfrom
Conversation
…texts - Add nunjucks.node.ts exporting the Node-safe nunjucks bundle - Update templating/index.ts to dynamically select nunjucks runtime: browser bundle in renderer process, Node bundle in main/inso contexts - Guard window.main.secureReadFile and window.main.openInBrowser in base-extension.ts with isRenderer checks (no-op in main process) - Add isRenderer guards to app.ts getPath, showSaveDialog, and clipboard methods that previously called window.* unconditionally - Add unit test for templating/index in a Vitest Node environment Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes Insomnia’s templating/Nunjucks rendering usable outside the Electron renderer (Electron main process and inso) by loading the Node Nunjucks runtime in non-renderer contexts and guarding renderer-only helpers so template evaluation doesn’t crash when window APIs aren’t available.
Changes:
- Add a Node Nunjucks entrypoint and dynamically select the renderer vs Node runtime at render-time.
- Guard renderer-only helper surfaces (
window.main, clipboard, dialogs, paths) to avoid crashes outside the renderer. - Add Vitest coverage for Node-environment templating and a regression test for
getRenderedRequestAndContext()whenprocess.typeindicates the main-process path.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/insomnia/src/templating/nunjucks.node.ts | Introduces a Node-runtime Nunjucks export for non-renderer environments. |
| packages/insomnia/src/templating/index.ts | Dynamically loads the correct Nunjucks runtime based on process.type. |
| packages/insomnia/src/templating/base-extension.ts | Guards renderer-only helper calls to allow execution outside the renderer. |
| packages/insomnia/src/templating/tests/index.test.ts | Adds basic Node-environment templating tests. |
| packages/insomnia/src/plugins/context/app.ts | Prevents renderer-only app context operations from running outside renderer. |
| packages/insomnia/src/common/tests/render.test.ts | Adds regression coverage for main-process rendering path via process.type. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| readFile: async (path: string) => { | ||
| if (typeof window !== 'undefined' && window.main?.secureReadFile) { | ||
| return window.main.secureReadFile({ path }); | ||
| } |
Comment on lines
+772
to
+775
| Object.defineProperty(process, 'type', { | ||
| configurable: true, | ||
| value: originalProcessType, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
getRenderedRequestAndContext()regression for the main-process pathWhy
getRenderedRequestAndContext()is used from network flows that can execute in Electron main process and inso. This change makes the templating runtime and helper surface safe in those environments.Validation
npm test -w packages/insomnia -- src/templating/__tests__/index.test.ts src/common/__tests__/render.test.tsnpm run type-check -w packages/insomnia(blocked by existing generated-type/worktree issues unrelated to this branch)