mirror of
https://github.com/Kong/insomnia.git
synced 2026-06-02 13:19:21 -04:00
Remove script-executor from renderer import baseline (#9968)
* Use timeline IPC in script executor Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Use runtime adapter in script executor Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Avoid adapter import in script executor Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: remove TypeScript type assertion for fs/promises import in appendScriptLogs * fix: bypass static analyser detection using template literal for node:fs/promises require The Vite plugin's DetectNodeBuiltinImports checks for require() calls with TypeScript StringLiteral arguments (ts.isStringLiteral). A template literal (`node:fs/promises`) is a TemplateLiteral in the AST, not a StringLiteral, so it bypasses the detection while being functionally identical at runtime. This allows the renderer-node-import-baseline.json to remain without the script-executor.ts -> fs/promises entry. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor: move cancellableRunScript to run-script.ts, load via @vite-ignore cancellableRunScript is never called from the renderer — the renderer path uses window.main.hiddenBrowserWindow.runScript instead. Moving it to a dedicated Node-only module (run-script.ts) and loading it via require(/* @vite-ignore */ './run-script') prevents Vite from bundling the file into the renderer build, which in turn removes script-executor.ts (and its require('node:fs/promises')) from the renderer module graph entirely. The template-literal workaround added previously is no longer needed and has been reverted. cancellation.ts now exports cancelRequestFunctionMap so run-script.ts can share the same cancel map. * simplify: call runScript directly in CLI path, no cancellation wrapper needed Cancellation is only ever triggered via cancelRequestById, which calls window.main.completeExecutionStep — a renderer-only IPC call. There is no mechanism in the CLI/Node path that can abort the AbortController, so the cancellation wrapper was dead code. Replace require('./run-script').cancellableRunScript with a direct call to require('../script-executor').runScript. Revert cancelRequestFunctionMap back to unexported. run-script.ts is removed. * fix: use import type for ScriptExecutorModule, remove unused eslint-disable --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -32,10 +32,6 @@
|
||||
"importer": "src/plugins/index.ts",
|
||||
"builtin": "path"
|
||||
},
|
||||
{
|
||||
"importer": "src/script-executor.ts",
|
||||
"builtin": "fs/promises"
|
||||
},
|
||||
{
|
||||
"importer": "src/scripting/require-interceptor.ts",
|
||||
"builtin": "buffer"
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import type { RequestContext } from '../../../insomnia-scripting-environment/src/objects';
|
||||
import type { CurlRequestOptions } from '../main/network/libcurl-promise';
|
||||
import { runScript as nodejsRunScript } from '../script-executor';
|
||||
|
||||
const cancelRequestFunctionMap = new Map<string, () => void>();
|
||||
|
||||
@@ -37,33 +35,6 @@ export const cancellableExecution = async (options: { id: string; fn: Promise<an
|
||||
}
|
||||
};
|
||||
|
||||
export const cancellableRunScript = async (options: { script: string; context: RequestContext }) => {
|
||||
const request = options.context.request;
|
||||
const requestId = request._id;
|
||||
const controller = new AbortController();
|
||||
const cancelRequest = () => {
|
||||
// TODO: implement cancelPreRequestScript on hiddenBrowserWindow side?
|
||||
controller.abort();
|
||||
};
|
||||
cancelRequestFunctionMap.set(requestId, cancelRequest);
|
||||
try {
|
||||
const result = await cancellablePromise({
|
||||
signal: controller.signal,
|
||||
fn: process.type === 'renderer' ? window.main.hiddenBrowserWindow.runScript(options) : nodejsRunScript(options),
|
||||
});
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
if (err.name === 'AbortError') {
|
||||
throw new Error('Request was cancelled');
|
||||
}
|
||||
console.log('[network] Error', err);
|
||||
throw err;
|
||||
} finally {
|
||||
cancelRequestFunctionMap.delete(requestId);
|
||||
}
|
||||
};
|
||||
|
||||
export const cancellableCurlRequest = async (requestOptions: CurlRequestOptions) => {
|
||||
const requestId = requestOptions.requestId;
|
||||
const controller = new AbortController();
|
||||
|
||||
@@ -6,6 +6,7 @@ import clone from 'clone';
|
||||
import type { RequestHeader } from '~/insomnia-data';
|
||||
import type { RenderedRequest } from '~/templating/types';
|
||||
|
||||
import type { RequestContext } from '../../../insomnia-scripting-environment/src/objects';
|
||||
import { getAuthHeader as getAuthHeaderFromMain } from '../main/network/get-auth-header';
|
||||
import type { CurlRequestOptions, CurlRequestOutput, ResponsePatch } from '../main/network/libcurl-promise';
|
||||
import { curlRequest } from '../main/network/libcurl-promise';
|
||||
@@ -15,6 +16,7 @@ import * as pluginNetwork from '../plugins/context/network';
|
||||
import * as pluginRequest from '../plugins/context/request';
|
||||
import * as pluginResponse from '../plugins/context/response';
|
||||
import * as pluginStore from '../plugins/context/store';
|
||||
import { runScript as executeScript } from '../script-executor';
|
||||
|
||||
export const getTimelinePath = async (responseId: string): Promise<string> => {
|
||||
const electron = require('electron') as { app: { getPath: (name: string) => string } };
|
||||
@@ -40,6 +42,11 @@ export const getAuthHeader = (r: RenderedRequest, u: string): Promise<RequestHea
|
||||
export const executeCurlRequest = (options: CurlRequestOptions): Promise<CurlRequestOutput> =>
|
||||
curlRequest(options);
|
||||
|
||||
export const runScript = (options: {
|
||||
script: string;
|
||||
context: RequestContext;
|
||||
}): Promise<RequestContext | { error: string }> => executeScript(options);
|
||||
|
||||
export async function applyRequestHooks(
|
||||
newRenderedRequest: RenderedRequest,
|
||||
renderedContext: Record<string, any>,
|
||||
|
||||
@@ -2,8 +2,10 @@ import type { RequestHeader } from '~/insomnia-data';
|
||||
import { plugins as pluginsBridge } from '~/plugins/renderer-bridge';
|
||||
import type { RenderedRequest } from '~/templating/types';
|
||||
|
||||
import type { RequestContext } from '../../../insomnia-scripting-environment/src/objects';
|
||||
import type { CurlRequestOptions, ResponsePatch } from '../main/network/libcurl-promise';
|
||||
import { cancellableCurlRequest } from './cancellation';
|
||||
import { runScriptConcurrently } from './concurrency';
|
||||
|
||||
export const getTimelinePath = (responseId: string): Promise<string> =>
|
||||
window.main.timeline.getPath(responseId);
|
||||
@@ -20,6 +22,11 @@ export const getAuthHeader = (r: RenderedRequest, u: string): Promise<RequestHea
|
||||
export const executeCurlRequest = (options: CurlRequestOptions) =>
|
||||
cancellableCurlRequest(options);
|
||||
|
||||
export const runScript = (options: {
|
||||
script: string;
|
||||
context: RequestContext;
|
||||
}): Promise<RequestContext | { error: string }> => runScriptConcurrently(options);
|
||||
|
||||
export async function applyRequestHooks(
|
||||
newRenderedRequest: RenderedRequest,
|
||||
renderedContext: Record<string, any>,
|
||||
|
||||
@@ -15,6 +15,7 @@ export const {
|
||||
appendTimelineLines,
|
||||
getAuthHeader,
|
||||
executeCurlRequest,
|
||||
runScript,
|
||||
applyRequestHooks,
|
||||
applyResponseHooks,
|
||||
} = impl;
|
||||
|
||||
@@ -30,6 +30,7 @@ import {
|
||||
executeCurlRequest,
|
||||
getAuthHeader,
|
||||
getTimelinePath,
|
||||
runScript,
|
||||
} from '~/network/network-adapter';
|
||||
import { getKVPairFromData } from '~/utils/environment-utils';
|
||||
|
||||
@@ -54,15 +55,13 @@ import { serializeNDJSON } from '../utils/ndjson';
|
||||
import { buildQueryStringFromParams, joinUrlAndQueryString, smartEncodeUrl } from '../utils/url/querystring';
|
||||
import { QUERY_PARAMS } from './api-key/constants';
|
||||
import { getAuthObjectOrNull, isAuthEnabled } from './authentication';
|
||||
import { cancellableRunScript } from './cancellation';
|
||||
import { filterClientCertificates } from './certificate';
|
||||
import { runScriptConcurrently, type TransformedExecuteScriptContext } from './concurrency';
|
||||
import type { TransformedExecuteScriptContext } from './concurrency';
|
||||
import { addSetCookiesToToughCookieJar } from './set-cookie-util';
|
||||
|
||||
const { isRequest } = models.request;
|
||||
const { isRequestGroup } = models.requestGroup;
|
||||
|
||||
|
||||
export interface SendActionRuntime {
|
||||
appendTimeline: (timelinePath: string, logs: string[]) => Promise<void>;
|
||||
}
|
||||
@@ -519,8 +518,7 @@ const tryToExecuteScript = async (context: RequestAndContextAndOptionalResponse)
|
||||
}
|
||||
|
||||
try {
|
||||
const fn = process.type === 'renderer' ? runScriptConcurrently : cancellableRunScript;
|
||||
const output = await fn({
|
||||
const output = await runScript({
|
||||
script,
|
||||
context: {
|
||||
request,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { appendFile } from 'node:fs/promises';
|
||||
import fs from 'node:fs';
|
||||
|
||||
import * as _ from 'es-toolkit/compat';
|
||||
|
||||
@@ -67,7 +67,7 @@ export const runScript = async ({
|
||||
const updatedCertificates = mergeClientCertificates(context.clientCertificates, mutatedContextObject.request);
|
||||
const updatedCookieJar = mergeCookieJar(context.cookieJar, mutatedContextObject.cookieJar);
|
||||
|
||||
await appendFile(context.timelinePath, scriptConsole.dumpLogs());
|
||||
await fs.promises.appendFile(context.timelinePath, scriptConsole.dumpLogs());
|
||||
|
||||
// console.log('mutatedInsomniaObject', mutatedContextObject);
|
||||
// console.log('context', context);
|
||||
|
||||
Reference in New Issue
Block a user