Skip to content

Commit

Permalink
fix(transformation): release isolate in case of error while creating (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayachand committed Feb 7, 2023
1 parent 6fd1278 commit ea51e24
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/util/ivmFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,12 @@ async function createIvm(code, libraryVersionIds, versionId, testMode) {
const bootstrapScriptResult = await bootstrap.run(context);
// const customScript = await isolate.compileScript(`${library} ;\n; ${code}`);
const customScriptModule = await isolate.compileModule(`${codeWithWrapper}`);
await customScriptModule.instantiate(context, (spec) => {
await customScriptModule.instantiate(context, async (spec) => {
if (librariesMap[spec]) {
return compiledModules[spec].module;
}
// Release the isolate context before throwing an error
await context.release();
console.log(`import from ${spec} failed. Module not found.`);
throw new Error(`import from ${spec} failed. Module not found.`);
});
Expand Down
46 changes: 46 additions & 0 deletions test/__tests__/user_transformation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,52 @@ describe("User transformation", () => {
expect(output).toEqual(outputImport);
});

it(`Simple ${name} Test for invalid library import error`, async () => {
const versionId = randomID();
const libraryVersionId = randomID();
const inputData = require(`./data/${integration}_input.json`);

const respBody = {
code: `
import { add } from 'addLib';
import { sub } from 'somelib';
export async function transformEvent(event, metadata) {
event.add = add(1, 2);
event.sub = sub(1, 2);
return event;
}
`,
name: "import from non existing library",
codeVersion: "1"
};
respBody.versionId = versionId;
const transformerUrl = `https://api.rudderlabs.com/transformation/getByVersionId?versionId=${versionId}`;
when(fetch)
.calledWith(transformerUrl)
.mockResolvedValue({
status: 200,
json: jest.fn().mockResolvedValue(respBody)
});

const addLibCode = `
export function add(a, b) {
return a + b;
}
`;

const libraryUrl = `https://api.rudderlabs.com/transformationLibrary/getByVersionId?versionId=${libraryVersionId}`;
when(fetch)
.calledWith(libraryUrl)
.mockResolvedValue({
status: 200,
json: jest.fn().mockResolvedValue({ code: addLibCode, name: "addLib" })
});

await expect(async () => {
await userTransformHandler(inputData, versionId, [libraryVersionId]);
}).rejects.toThrow("import from somelib failed. Module not found.");
});

it(`Simple ${name} async test for V1 transformation code`, async () => {
const libraryVersionId = randomID();
const inputData = require(`./data/${integration}_input.json`);
Expand Down

0 comments on commit ea51e24

Please sign in to comment.