MCPcopy Create free account
hub / github.com/Kong/insomnia / runScript

Function runScript

packages/insomnia/src/scripting/run-script.ts:13–137  ·  view source on GitHub ↗
({
  script,
  context,
  securityPolicy = defaultSecurityPolicy,
}: {
  script: string;
  context: RequestContext;
  securityPolicy?: ScriptSecurityPolicy;
})

Source from the content-addressed store, hash-verified

11import { defaultSecurityPolicy, prepareSandbox, ScriptSecurityPolicy } from './sandbox';
12
13export const runScript = async ({
14 script,
15 context,
16 securityPolicy = defaultSecurityPolicy,
17}: {
18 script: string;
19 context: RequestContext;
20 securityPolicy?: ScriptSecurityPolicy;
21}): Promise<RequestContext> => {
22 const activePolicy = context.settings.scriptSandboxEnabled !== false ? securityPolicy : new ScriptSecurityPolicy([]);
23
24 const { executionContext, scriptConsole, maskNames, maskValues, bridgeOps } = await prepareSandbox(
25 script,
26 context,
27 activePolicy,
28 );
29
30 const AsyncFunction = (async () => {}).constructor;
31 const scriptParams = [
32 'insomnia', // insomnia scripting API object
33 'console', // log console
34 '_', // lodash library
35 'setTimeout', // proxied setTimeout tracked by the async task monitor
36 '__waitForAllTestsDone__', // Drains pm.test() assertions before the script exits
37 '__bridgeReset__', // Clears the async task list and re-enables monitoring
38 '__bridgeStop__', // Stops recording new promises into the task list
39 '__bridgeSettle__', // Awaits all tracked promises before returning
40 ...maskNames, // Masked globals from the security policy (e.g. eval → undefined)
41 ];
42 const strictMode = context.settings.scriptStrictModeEnabled !== false;
43 const scriptBody = [
44 `__bridgeReset__();`, // Start with a clean async task slate for this script run
45 `await (async function() {`, // IIFE gives the user script its own lexical scope
46 ...(strictMode ? [` 'use strict';`] : []), // Strict mode: this === undefined, prevents silent errors
47 ` const $ = insomnia;`, // Postman-compat alias for the insomnia scripting object
48 ` ${script}`, // User script body
49 `})();`,
50 `await __waitForAllTestsDone__();`, // Wait for all pm.test() callbacks to resolve
51 `__bridgeStop__();`, // Stop tracking new promises (user script is done)
52 `await __bridgeSettle__();`, // Drain any fire-and-forget promises the script created
53 `return insomnia;`, // Return the (possibly mutated) insomnia context
54 ].join('\n');
55
56 // const scriptBody = [
57 // `const $ = insomnia;`,
58 // `__bridgeReset__();`,
59 // `try {`,
60 // ` ${script}`,
61 // ` await __waitForAllTestsDone__();`,
62 // `} finally {`,
63 // ` __bridgeStop__();`,
64 // ` await __bridgeSettle__();`,
65 // `}`,
66 // `return insomnia;`,
67 // ].join('\n');
68
69 const executeScript = AsyncFunction(...scriptParams, scriptBody);
70

Callers 1

Calls 6

prepareSandboxFunction · 0.90
mergeRequestsFunction · 0.90
mergeSettingsFunction · 0.90
mergeClientCertificatesFunction · 0.90
mergeCookieJarFunction · 0.90
toObjectMethod · 0.45

Tested by

no test coverage detected