Skip to content

Commit

Permalink
Hide random cache keys under feature-flag (#9804)
Browse files Browse the repository at this point in the history
* Hide random cache keys under feature-flag

The idea of this chqnge is to make it safer to bump parcel to the latest
version without risking leaving too many dangling cache files in case
the clean-up mechanisms do not work.

* Fix flow error

* Run unit-tests with randomLargeBlobKeys flag on

---------

Co-authored-by: pyamada (Remote Dev Environment) <[email protected]>
  • Loading branch information
yamadapc and pyamada-atlassian authored Jun 18, 2024
1 parent 9eff0bf commit 2ce4efe
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/core/cache/src/LMDBCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import stream from 'stream';
import path from 'path';
import {promisify} from 'util';
import {serialize, deserialize, registerSerializableClass} from '@parcel/core';
import {getFeatureFlag} from '@parcel/feature-flags';
import {NodeFS} from '@parcel/fs';
// flowlint-next-line untyped-import:off
import packageJson from '../package.json';
Expand Down Expand Up @@ -147,8 +148,10 @@ export class LMDBCache implements Cache {
await this.fsCache.deleteLargeBlob(previousEntry.largeBlobKey);
}

// $FlowFixMe flow libs are outdated but we only support node>16 so randomUUID is present
const largeBlobKey = `${key}_${crypto.randomUUID()}`;
const largeBlobKey = getFeatureFlag('randomLargeBlobKeys')
? // $FlowFixMe flow libs are outdated but we only support node>16 so randomUUID is present
`${key}_${crypto.randomUUID()}`
: key;
await this.fsCache.setLargeBlob(largeBlobKey, contents, options);
const entry: LargeBlobEntry = {type: 'LARGE_BLOB', largeBlobKey};
await this.set(key, entry);
Expand Down
12 changes: 12 additions & 0 deletions packages/core/cache/test/LMDBCache.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow

import {DEFAULT_FEATURE_FLAGS, setFeatureFlags} from '@parcel/feature-flags';
import * as mkdirp from 'mkdirp';
import * as tempy from 'tempy';
import fs from 'fs';
Expand All @@ -15,6 +16,17 @@ describe('LMDBCache', () => {
mkdirp.sync(tmpDir);
lmdbCache = new LMDBCache(tmpDir);
await lmdbCache.ensure();

setFeatureFlags({
...DEFAULT_FEATURE_FLAGS,
randomLargeBlobKeys: true,
});
});

afterEach(() => {
setFeatureFlags({
...DEFAULT_FEATURE_FLAGS,
});
});

it('LMDBCache::get / set will return key values', async () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/core/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {FSCache} from '@parcel/cache';
import tempy from 'tempy';
import path from 'path';
import {inputFS, outputFS} from '@parcel/test-utils';
import {DEFAULT_FEATURE_FLAGS} from '@parcel/feature-flags';
import {relativePath} from '@parcel/utils';
import {NodePackageManager} from '@parcel/package-manager';
import {createEnvironment} from '../src/Environment';
Expand Down Expand Up @@ -53,9 +54,7 @@ export const DEFAULT_OPTIONS: ParcelOptions = {
sourceMaps: false,
},
featureFlags: {
exampleFeature: false,
configKeyInvalidation: false,
parcelV3: false,
...DEFAULT_FEATURE_FLAGS,
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/core/feature-flags/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const DEFAULT_FEATURE_FLAGS: FeatureFlags = {
exampleFeature: false,
configKeyInvalidation: false,
parcelV3: false,
randomLargeBlobKeys: false,
};

let featureFlagValues: FeatureFlags = {...DEFAULT_FEATURE_FLAGS};
Expand Down
4 changes: 4 additions & 0 deletions packages/core/feature-flags/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export type FeatureFlags = {|
* Rust backed requests
*/
+parcelV3: boolean,
/**
* Store large blobs on randomly generated keys
*/
+randomLargeBlobKeys: boolean,
|};

0 comments on commit 2ce4efe

Please sign in to comment.