Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'cloneData' option to buildMemoryStorage #581

Merged
merged 18 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
19 changes: 16 additions & 3 deletions src/storage/memory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildStorage, canStale, isExpired } from './build';
import type { AxiosStorage, StorageValue } from './types';
import type { AxiosStorage, NotEmptyStorageValue, StorageValue } from './types';

/**
* Modern function to natively deep clone.
Expand Down Expand Up @@ -31,19 +31,22 @@ declare const structuredClone: (<T>(value: T) => T) | undefined;
* ```
*
* @param {boolean} cloneData If the data returned by `find()` should be cloned to avoid
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
* mutating the original data outside the `set()` method.
* mutating the original data outside the `set()` method. Disabled is default
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
* @param {number | false} cleanupInterval The interval in milliseconds to run a
* setInterval job of cleaning old entries. If false, the job will not be created.
* Disabled is default
* @param {number | false} maxEntries The maximum number of entries to keep in the
* storage. Its hard to determine the size of the entries, so a smart FIFO order is used
* to determine eviction. If false, no check will be done and you may grow up memory
* usage. Disabled is default
* @param {boolean} storeClone If the data stored by `set()` should be cloned to prevent
* future mutation from affecting cached data. Disabled is default
*/
export function buildMemoryStorage(
cloneData = false,
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
cleanupInterval: number | false = false,
maxEntries: number | false = false
maxEntries: number | false = false,
storeClone = false
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
) {
const storage = buildStorage({
set: (key, value) => {
Expand All @@ -67,6 +70,16 @@ export function buildMemoryStorage(
}
}

if (storeClone && value !== undefined) {
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
// Clone the value before storing to prevent future mutations
// from affecting cached data.
if (typeof structuredClone === 'function') {
value = structuredClone(value);
} else {
value = JSON.parse(JSON.stringify(value)) as NotEmptyStorageValue;
}
}

storage.data[key] = value;
},

Expand Down
24 changes: 24 additions & 0 deletions test/storage/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ describe('tests memory storage', () => {
expect(result2.data?.data).toBe('data');
});

// Expects that a when value saved using storage.set() is has his inner properties updated,
// a request to storage.get() should return unmodified value.
//
// https://github.com/arthurfiorette/axios-cache-interceptor/issues/580
it('not allow changes by value reference before', async () => {
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved
const storage = buildMemoryStorage(false, false, false, true);
bhallionOhbibi marked this conversation as resolved.
Show resolved Hide resolved

const data = { ...EMPTY_RESPONSE, data: 'data' };
await storage.set('key', {
arthurfiorette marked this conversation as resolved.
Show resolved Hide resolved
state: 'cached',
createdAt: Date.now(),
ttl: 1000 * 60 * 5, // 5 Minutes
data: data
});

data.data = 'another data';

arthurfiorette marked this conversation as resolved.
Show resolved Hide resolved
const result = (await storage.get('key')) as CachedStorageValue;

expect(result).not.toBeNull();
expect(result.state).toBe('cached');
expect(result.data.data).toBe('data');
});

it('tests cleanup function', async () => {
const storage = buildMemoryStorage(false, 500);

Expand Down