Skip to content

Commit

Permalink
fix(sdk): some of external modules are missing (#7315)
Browse files Browse the repository at this point in the history
* fix: some external modules are missing

* chore: update @types/tv4

* chore: use map instead of switch-case statement

* test(scripting): check using external libs in the critical test suite
  • Loading branch information
ihexxa committed Apr 26, 2024
1 parent c4dd36c commit 69fb149
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 19 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/insomnia-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"homepage": "https://github.com/Kong/insomnia#readme",
"dependencies": {
"@types/tv4": "^1.2.33",
"@types/xml2js": "^0.4.14",
"ajv": "^8.12.0",
"chai": "^5.1.0",
Expand Down
37 changes: 37 additions & 0 deletions packages/insomnia-smoke-test/fixtures/pre-request-collection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1077,3 +1077,40 @@ resources:
text: |-
{}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df16
parentId: fld_01de564274824ecaad272330339ea6b2
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: use external modules
description: ""
method: POST
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1636141014553
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
preRequestScript: |-
const ajv = require("ajv")
const chai = require('chai');
const lodash = require('lodash');
const tv4 = require('tv4');
const uuid = require('uuid');
const xml2js = require('xml2js');
const csv = require('csv-parse/lib/sync');
const cheerio = require('cheerio');
const crypto = require('crypto-js');
const moment = require('moment');
body:
mimeType: "application/json"
text: |-
{}
_type: request
23 changes: 23 additions & 0 deletions packages/insomnia-smoke-test/tests/critical/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ test('can use node-libcurl, httpsnippet, hidden browser window', async ({ app, p
await expect(statusTag).toContainText('200 OK');
await page.getByRole('tab', { name: 'Timeline' }).click();
});

test('can use external modules in scripts ', async ({ app, page }) => {
const text = await loadFixture('pre-request-collection.yaml');

// import collection
await app.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), text);
await page.getByRole('button', { name: 'Create in project' }).click();
await page.getByRole('menuitemradio', { name: 'Import' }).click();
await page.locator('[data-test-id="import-from-clipboard"]').click();
await page.getByRole('button', { name: 'Scan' }).click();
await page.getByRole('dialog').getByRole('button', { name: 'Import' }).click();

// select request
await page.getByLabel('Pre-request Scripts').click();
await page.getByLabel('Request Collection').getByTestId('use external modules').press('Enter');

// send
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

// verify
const statusTag = page.locator('[data-testid="response-status-tag"]:visible');
await expect(statusTag).toContainText('200 OK');
});
30 changes: 27 additions & 3 deletions packages/insomnia/src/hidden-window-preload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import * as fs from 'node:fs';

import ajv from 'ajv';
import chai from 'chai';
import * as cheerio from 'cheerio';
import cryptojs from 'crypto-js';
import * as csvParseSync from 'csv-parse/sync';
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
import { asyncTasksAllSettled, Collection as CollectionModule, OriginalPromise, ProxiedPromise, RequestContext, resetAsyncTasks, stopMonitorAsyncTasks } from 'insomnia-sdk';
import lodash from 'lodash';
import moment from 'moment';
import tv4 from 'tv4';
import * as uuid from 'uuid';
import xml2js from 'xml2js';

import type { Compression } from './models/response';

const externalModules = new Map<string, object>([
['ajv', ajv],
['chai', chai],
['cheerio', cheerio],
['crypto-js', cryptojs],
['csv-parse/lib/sync', csvParseSync],
['lodash', lodash],
['moment', moment],
['tv4', tv4],
['uuid', uuid],
['xml2js', xml2js],
]);

export interface HiddenBrowserWindowToMainBridgeAPI {
requireInterceptor: (module: string) => any;
onmessage: (listener: (data: any, callback: (result: any) => void) => void) => void;
Expand Down Expand Up @@ -75,10 +98,11 @@ const bridge: HiddenBrowserWindowToMainBridgeAPI = {
'xml2js',
].includes(moduleName)
) {
if (moduleName === 'csv-parse/lib/sync') {
return require('csv-parse/sync');
const externalModule = externalModules.get(moduleName);
if (!externalModule) {
throw Error(`no module is found for "${moduleName}"`);
}
return require(moduleName);
return externalModule;
} else if (moduleName === 'insomnia-collection' || moduleName === 'postman-collection') {
return CollectionModule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ export const WebSocketRequestPane: FC<Props> = ({ environment }) => {
<div className="p-4">
<div className="text-xs max-h-32 flex flex-col overflow-y-auto min-h-[2em] bg-[--hl-xs] px-2 py-1 border border-solid border-[--hl-sm]">
<label className="label--small no-pad-top">Url Preview</label>
<ErrorBoundary
key={uniqueKey}
errorClassName="tall wide vertically-align font-error pad text-center"
>
<RenderedQueryString request={activeRequest} />
<ErrorBoundary
key={uniqueKey}
errorClassName="tall wide vertically-align font-error pad text-center"
>
<RenderedQueryString request={activeRequest} />
</ErrorBoundary>
</div>
</div>
Expand All @@ -313,18 +313,18 @@ export const WebSocketRequestPane: FC<Props> = ({ environment }) => {
<div className='flex items-center w-full p-4 h-4 justify-between'>
<Heading className='text-xs font-bold uppercase text-[--hl]'>Query parameters</Heading>
</div>
<ErrorBoundary
key={uniqueKey}
errorClassName="tall wide vertically-align font-error pad text-center"
>
<RequestParametersEditor
bulk={useBulkParametersEditor}
disabled={disabled}
/>
</ErrorBoundary>
</div>
<ErrorBoundary
key={uniqueKey}
errorClassName="tall wide vertically-align font-error pad text-center"
>
<RequestParametersEditor
bulk={useBulkParametersEditor}
disabled={disabled}
/>
</ErrorBoundary>
</div>
<div className='flex-1 flex flex-col gap-4 p-4 overflow-y-auto'>
<Heading className='text-xs font-bold uppercase text-[--hl]'>Path parameters</Heading>
<Heading className='text-xs font-bold uppercase text-[--hl]'>Path parameters</Heading>
{pathParameters.length > 0 && (
<div className="pr-[72.73px] w-full">
<div className='grid gap-x-[20.8px] grid-cols-2 flex-shrink-0 w-full rounded-sm overflow-hidden'>
Expand Down

0 comments on commit 69fb149

Please sign in to comment.