({
script,
context,
}: {
script: string;
context: RequestContext;
})
| 16 | import { requireInterceptor } from './scripting/require-interceptor'; |
| 17 | |
| 18 | export const runScript = async ({ |
| 19 | script, |
| 20 | context, |
| 21 | }: { |
| 22 | script: string; |
| 23 | context: RequestContext; |
| 24 | }): Promise<RequestContext> => { |
| 25 | // console.log(script); |
| 26 | const scriptConsole = getNewConsole(); |
| 27 | |
| 28 | const executionContext = await initInsomniaObject(context, scriptConsole.log); |
| 29 | |
| 30 | const evalInterceptor = (script: string) => { |
| 31 | invariant(script && typeof script === 'string', 'eval is called with invalid or empty value'); |
| 32 | const result = eval(script); |
| 33 | return result; |
| 34 | }; |
| 35 | |
| 36 | const AsyncFunction = (async () => {}).constructor; |
| 37 | const executeScript = AsyncFunction( |
| 38 | 'insomnia', |
| 39 | 'require', |
| 40 | 'console', |
| 41 | 'eval', |
| 42 | '_', |
| 43 | 'setTimeout', |
| 44 | // disable these as they are not supported in web or existing implementation |
| 45 | 'setImmediate', |
| 46 | 'queueMicrotask', |
| 47 | 'process', |
| 48 | ` |
| 49 | const $ = insomnia; |
| 50 | ${script}; |
| 51 | return insomnia;`, |
| 52 | ); |
| 53 | |
| 54 | const mutatedInsomniaObject = await executeScript( |
| 55 | executionContext, |
| 56 | requireInterceptor, |
| 57 | scriptConsole, |
| 58 | evalInterceptor, |
| 59 | _, |
| 60 | proxiedSetTimeout, |
| 61 | ); |
| 62 | if (mutatedInsomniaObject == null || !(mutatedInsomniaObject instanceof InsomniaObject)) { |
| 63 | throw new Error('insomnia object is invalid or script returns earlier than expected.'); |
| 64 | } |
| 65 | const mutatedContextObject = mutatedInsomniaObject.toObject(); |
| 66 | const updatedRequest = mergeRequests(context.request, mutatedContextObject.request); |
| 67 | const updatedSettings = mergeSettings(context.settings, mutatedContextObject.request); |
| 68 | const updatedCertificates = mergeClientCertificates(context.clientCertificates, mutatedContextObject.request); |
| 69 | const updatedCookieJar = mergeCookieJar(context.cookieJar, mutatedContextObject.cookieJar); |
| 70 | |
| 71 | await fs.promises.appendFile(context.timelinePath, scriptConsole.dumpLogs()); |
| 72 | |
| 73 | // console.log('mutatedInsomniaObject', mutatedContextObject); |
| 74 | // console.log('context', context); |
| 75 |
nothing calls this directly
no test coverage detected