Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ declare const self: WorkerGlobalScope;

describe("Tests to assert install/uninstall flows", function () {
beforeAll(() => {
self.importScripts = jest.fn(() => {
self.importScripts = jest.fn((url: string) => {
if (url.includes("jspdf-autotable")) {
const defaultVar = function () {};

defaultVar.Cell = function () {};
self.Cell = function () {};
self.default = defaultVar;

return;
}

self.lodash = {};
});

Expand Down Expand Up @@ -150,4 +160,28 @@ describe("Tests to assert install/uninstall flows", function () {
method: "Hello",
});
});

it("should install a library with default export", async function () {
const res = await installLibrary({
data: {
url: "https://cdn.jsdelivr.net/npm/jspdf-autotable@3.5.28/dist/jspdf.plugin.autotable.js",
takenAccessors: [],
takenNamesMap: {},
},
method: EVAL_WORKER_ASYNC_ACTION.INSTALL_LIBRARY,
webworkerTelemetry: {},
});

expect(self.importScripts).toHaveBeenCalled();
expect(mod.makeTernDefs).toHaveBeenCalledWith({});

expect(res).toEqual({
accessor: ["jspdf_plugin_autotable_js"],
defs: {
"!name": "LIB/jspdf_plugin_autotable_js",
jspdf_plugin_autotable_js: undefined,
},
success: true,
});
});
});
34 changes: 32 additions & 2 deletions app/client/src/workers/Evaluation/handlers/jsLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,40 @@ export async function installLibrary(
// Find keys add that were installed to the global scope.
const keysAfterInstallation = Object.keys(self);

accessors.push(
...difference(keysAfterInstallation, envKeysBeforeInstallation),
const differentiatingKeys = difference(
keysAfterInstallation,
envKeysBeforeInstallation,
);

if (
differentiatingKeys.length > 0 &&
differentiatingKeys.includes("default")
) {
// Changing default export to library specific name
const uniqueName = generateUniqueAccessor(
url,
takenAccessors,
takenNamesMap,
);

// mapping default functionality to library name accessor
self[uniqueName] = self["default"];
// deleting the reference of default key from the self object
delete self["default"];
Comment thread
AmanAgarwal041 marked this conversation as resolved.
// mapping all the references of differentiating keys from the self object to the self[uniqueName] key object
differentiatingKeys.map((key) => {
if (key !== "default") {
self[uniqueName][key] = self[key];
// deleting the references from the self object
delete self[key];
}
});
Comment thread
rishabhrathod01 marked this conversation as resolved.
// pushing the uniqueName to the accessor array
accessors.push(uniqueName);
} else {
accessors.push(...differentiatingKeys);
}

/**
* Check the list of installed library to see if their values have changed.
* This is to check if the newly installed library overwrites an already existing one
Expand Down