Skip to content

Commit

Permalink
fix: partially freezes internal functions (JointlyTech#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simone Sacchi committed Apr 3, 2023
1 parent b3a98f4 commit 4b03b03
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 10 deletions.
58 changes: 58 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,62 @@ describe('Plugins', () => {
await sleep(EXECUTION_MARGIN);
expect(counter).toBe(1);
});

it('should throw if internal functions getDataCacheRecord is overridden', async () => {

const myPlugin = {
name: 'myPlugin',
hooks: [
{
hook: Hooks.INIT,
action: async (payload) => {
payload.internals.getDataCacheRecord = function () {
console.log("should not be possible");
};
}
}
]
};

const mockFn = (step: number) =>
new Promise((resolve) => {
resolve(step);
});

const wrappedMockFn = cacheCandidate(mockFn, {
plugins: [myPlugin]
});

await expect(wrappedMockFn(1)).rejects.toThrow();

});

it('should not throw if internal functions getDataCacheKey is overridden', async () => {

const myPlugin = {
name: 'myPlugin',
hooks: [
{
hook: Hooks.INIT,
action: async (payload) => {
payload.internals.getDataCacheKey = function () {
console.log("should be possible");
};
}
}
]
};

const mockFn = (step: number) =>
new Promise((resolve) => {
resolve(step);
});

const wrappedMockFn = cacheCandidate(mockFn, {
plugins: [myPlugin]
});

await expect(wrappedMockFn(1)).resolves.not.toThrowError();

});
});
41 changes: 31 additions & 10 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,35 @@ export function uniqid(length = 10) {
.substring(2, length + 2);
}

// returns a partially frozen object
function internalsFactory () {
const internals = {
getDataCacheKey,
getDataCacheRecord,
addDataCacheRecord,
deleteDataCacheRecord,
isDataCacheRecordExpired,
getExceedingAmount
};
Object.defineProperty(internals, 'getDataCacheRecord', {
value: getDataCacheRecord,
writable: false
});
Object.defineProperty(internals, 'addDataCacheRecord', {
value: addDataCacheRecord,
writable: false
});
Object.defineProperty(internals, 'deleteDataCacheRecord', {
value: deleteDataCacheRecord,
writable: false
});
Object.defineProperty(internals, 'isDataCacheRecordExpired', {
value: isDataCacheRecordExpired,
writable: false
});

return internals;
}
export async function letsCandidate({
options,
key,
Expand All @@ -292,23 +321,15 @@ export async function letsCandidate({
args: any[];
originalMethod: (...args: any[]) => Promise<unknown>;
}) {
// Make options.plugins freezed
//Object.freeze(options.plugins);

const HookPayload = {
options,
key,
timeoutCache,
runningQueryCache,
timeframeCache,
fnArgs: args,
internals: {
getDataCacheKey,
getDataCacheRecord,
addDataCacheRecord,
deleteDataCacheRecord,
isDataCacheRecordExpired,
getExceedingAmount
}
internals: internalsFactory()
};
await ExecuteHook(Hooks.INIT, options.plugins, HookPayload);
// Check if result exists in dataCache
Expand Down

0 comments on commit 4b03b03

Please sign in to comment.