Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/insomnia/src/network/cancellation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { appendFile } from 'node:fs/promises';

import type { RequestContext } from '../../../insomnia-scripting-environment/src/objects';
import type { CurlRequestOptions } from '../main/network/libcurl-promise';
import { runScript as nodejsRunScript } from '../script-executor';
Expand Down Expand Up @@ -49,7 +51,9 @@ export const cancellableRunScript = async (options: { script: string; context: R
try {
const result = await cancellablePromise({
signal: controller.signal,
fn: process.type === 'renderer' ? window.main.hiddenBrowserWindow.runScript(options) : nodejsRunScript(options),
fn: process.type === 'renderer'
? window.main.hiddenBrowserWindow.runScript(options)
: nodejsRunScript({ ...options, appendTimelineEntry: ({ timelinePath, data }) => appendFile(timelinePath, data) }),
});

return result;
Expand Down
18 changes: 14 additions & 4 deletions packages/insomnia/src/network/concurrency.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { appendFile } from 'node:fs/promises';

import type { queueAsPromised } from 'fastq';
import * as fastq from 'fastq';

Expand All @@ -11,6 +13,7 @@ import type {
} from '~/insomnia-data';

import type { RequestContext, RequestTestResult } from '../../../insomnia-scripting-environment/src/objects';
import { runScript } from '../script-executor';
import { cancellableExecution } from './cancellation';

export interface ExecuteScriptContext {
Expand Down Expand Up @@ -59,10 +62,17 @@ async function asyncWorker(arg: Task): Promise<any> {
const timeoutPromise = new Promise<{ error: string }>(resolve =>
setTimeout(resolve, timeoutValue, { error: `Executing script timeout: ${timeoutValue}` }),
);
const executionPromise = Promise.race([
window.main.hiddenBrowserWindow.runScript({ script: arg.script, context: arg.context }),
timeoutPromise,
]);

const scriptPromise =
process.type === 'renderer'
? window.main.hiddenBrowserWindow.runScript({ script: arg.script, context: arg.context })
: runScript({
script: arg.script,
context: arg.context,
appendTimelineEntry: ({ timelinePath, data }) => appendFile(timelinePath, data),
});

const executionPromise = Promise.race([scriptPromise, timeoutPromise]);
const result = await cancellableExecution({ id: arg.context.request._id, fn: executionPromise });
return result;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/insomnia/src/network/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ const tryToExecuteScript = async (context: RequestAndContextAndOptionalResponse)

if (runtime) {
await runtime.appendTimeline(timelinePath, output.logs);
} else if (process.type !== 'renderer' && output?.logs?.length) {
await fs.promises.appendFile(timelinePath, output.logs.join('\n'));
}

if (output?.transientVariables !== undefined) {
Expand Down
6 changes: 3 additions & 3 deletions packages/insomnia/src/script-executor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { appendFile } from 'node:fs/promises';

import * as _ from 'es-toolkit/compat';

import { initInsomniaObject, InsomniaObject } from '../../insomnia-scripting-environment/src/objects';
Expand All @@ -17,9 +15,11 @@ import { invariant } from './utils/invariant';
export const runScript = async ({
script,
context,
appendTimelineEntry,
}: {
script: string;
context: RequestContext;
appendTimelineEntry: (opts: { timelinePath: string; data: string }) => Promise<void>;
Comment on lines 15 to +22
}): Promise<RequestContext> => {
// console.log(script);
const scriptConsole = getNewConsole();
Expand Down Expand Up @@ -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 appendTimelineEntry({ timelinePath: context.timelinePath, data: scriptConsole.dumpLogs() });

// console.log('mutatedInsomniaObject', mutatedContextObject);
// console.log('context', context);
Expand Down
Loading