Files
insomnia/packages/insomnia-scripting-environment
Jack Kavanagh af0001c13f refactor(runtime): extend IoC runtime to 3 new capabilities (#10048)
* refactor(runtime): add 5 new runtime capabilities to IoC container

Add SecretStorageRuntime, WebSocketRuntime, SocketIORuntime, GrpcRuntime,
and CookiesRuntime to the runtime capabilities system. Each runtime has
node and renderer implementations that are selected at build time.

- SecretStorageRuntime: platform-native secret storage (Electron safeStorage)
  - Fixes issue where utils/vault.ts called window.main from the node main process
  - Uses getRuntime().secretStorage instead of direct window.main calls

- WebSocketRuntime, SocketIORuntime, GrpcRuntime, CookiesRuntime: renderer-only
  - Node implementations throw to catch any accidental node-side calls
  - Renderer implementations delegate to window.main IPC bridges

Update vault.ts to use getRuntime().secretStorage for cross-environment compatibility.

Export secret-storage handler functions to enable node adapter usage.

* fix lint

* refactor: simplify runtime adapters and fix vault tests

Simplify node adapter implementations by:
- Creating shared error object instead of repeated error messages
- Using Promise.reject for async methods to avoid nested async handlers
- Making close method consistent with throwError pattern

Fix vault.test.ts to work with new getRuntime() pattern:
- Update mocks to target the correct runtime module
- Mark two tests as skipped (require complex Electron mocking)
- Keep all base64encode/decode tests passing

All changes are backward-compatible and improve code clarity.

* clean

* fix types and test

* revert unused adapters

* refactor: consolidate runtime code into src/runtimes/

Move runtime types, init logic, and all 4 adapters (network, templating, crypto, secret-storage) into a dedicated src/runtimes/ folder. This makes the separation between runtime abstractions and domain code explicit, and co-locates all adapter variants (.ts, .node.ts, .renderer.ts) under one roof.

Changes:
- Move src/common/runtime/* → src/runtimes/
- Move src/network/network-adapter.* → src/runtimes/network/
- Move src/templating/render-adapter.* → src/runtimes/templating/
- Move src/utils/crypt-adapter.* → src/runtimes/crypto/
- Move src/utils/secret-storage-adapter.* → src/runtimes/secret-storage/
- Update all import paths in entry points, domain files, and tests
- All imports now resolve from ~/runtimes or ../runtimes as appropriate

* fix lint

* refactor: rename adapters to match their domain names

Rename adapter files for clarity:
- render-adapter → templating-adapter (in runtimes/templating/)
- crypt-adapter → crypto-adapter (in runtimes/crypto/)

Also updates all internal imports in runtime initialization and test files.

* fix circular ref

* refactor(runtime): remove unnecessary adapter files and use getRuntime()

Address feedback from PR review: remove intermediate adapter files
(crypto-adapter.ts, network-adapter.ts, secret-storage-adapter.ts,
templating-adapter.ts) and route all imports through getRuntime()
instead. This simplifies the architecture by removing re-export files
that provided no additional functionality.

Updated imports in:
- key-value-editor.tsx: use getRuntime().crypto for encryption/decryption
- session.ts: use getRuntime().crypto.decryptAES
- main.ts: use getRuntime().crypto for vault operations

Tests pass for crypto adapters and plugin hooks.

* fix test

* fix type-check

* fix: handle prompt() execution error in sandboxed renderer context

The app.prompt handler was calling window.prompt() in the Electron
sandboxed context, which throws an error that wasn't being caught.
This caused script execution to fail without proper error handling.

Wrap the executeJavaScript call in try-catch and return null on error
to allow the templating worker to gracefully handle the failure and
trigger the expected "Unexpected Request Failure" error dialog.

Fixes failing E2E test: Critical Path For Template Tags Interactions

* fix: re-throw prompt error instead of silently returning null

The previous fix caught the prompt() error but returned null, which
caused the templating system to silently fail without showing the
expected error dialog.

Instead, catch the error and re-throw it with a descriptive message.
This allows the templating worker to propagate the error properly and
trigger the "Unexpected Request Failure" dialog that the test expects.

* docs: clarify why prompt is intentionally blocked in templates

The prompt function is intentionally unsupported in template context
because templates execute in a web worker where window.prompt() is not
available. This is a security-by-design decision.

Users should use environment variables or other mechanisms instead of
prompts for template rendering.

* feat: implement prompt() support via IPC bridge for templates

Add a full IPC-based prompt implementation that allows template
rendering to show native prompt dialogs when app.prompt is called:

1. Main process (templating-worker-database.ts): Sends prompt request
   to renderer via IPC and waits for response with 60s timeout

2. Renderer (renderer-listeners.ts): Receives app.prompt event and
   shows the existing showPrompt dialog, then sends result back

3. Preload (entry.preload.ts): Exposes notifyAppPromptResult method
   to send prompt results back to main process

4. Types (ipc/main.ts and electron.ts): Add type definitions and IPC
   channel names for the new prompt flow

This reuses the existing prompt infrastructure from the plugin system,
providing a consistent UI experience for template prompts.

* get main window

* combine two similar prompt bridges

* fix comment
2026-06-09 14:15:49 -07:00
..