Skip to content

Commit

Permalink
fix: js library default export to a unique name instead of default ke…
Browse files Browse the repository at this point in the history
…y in self (#36483)
  • Loading branch information
AmanAgarwal041 committed Sep 24, 2024
1 parent 9731728 commit 2443b8c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
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/[email protected]/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"];
// 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];
}
});
// 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

0 comments on commit 2443b8c

Please sign in to comment.