-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Enable extension to specify storeName for persisting data #7107
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4849f62
Enable extension to specify storeName for persisting data
panuhorsmalahti 1dce6b2
Add unit tests and update injectables
apialaviiva 17cb331
Merge branch 'master' into feature/extension-store-name
apialaviiva 2318875
Remove unnecessary mock
apialaviiva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/core/src/extensions/__tests__/configurable-directories.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
import { runInAction } from "mobx"; | ||
import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; | ||
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; | ||
import type { FakeExtensionOptions } from "../../renderer/components/test-utils/get-extension-fake"; | ||
import getHashInjectable from "../../extensions/extension-loader/file-system-provisioner-store/get-hash.injectable"; | ||
import fsInjectable from "../../common/fs/fs.injectable"; | ||
|
||
describe("configurable directories for extension files", () => { | ||
let builder: ApplicationBuilder; | ||
|
||
beforeEach(async () => { | ||
builder = getApplicationBuilder(); | ||
|
||
builder.beforeApplicationStart( | ||
(mainDi) => { | ||
runInAction(() => { | ||
mainDi.override(getHashInjectable, () => x => x); | ||
}); | ||
}, | ||
); | ||
|
||
await builder.startHidden(); | ||
|
||
const window = builder.applicationWindow.create("some-window-id"); | ||
|
||
await window.start(); | ||
|
||
}); | ||
|
||
describe("when extension with a specific store name is enabled", () => { | ||
let testExtensionOptions: FakeExtensionOptions; | ||
|
||
beforeEach(() => { | ||
|
||
testExtensionOptions = { | ||
id: "some-extension", | ||
name: "some-extension-name", | ||
|
||
mainOptions: { | ||
manifest: { | ||
name: "irrelevant", | ||
storeName: "some-specific-store-name", | ||
version: "0", | ||
engines: { | ||
lens: "0", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
builder.extensions.enable(testExtensionOptions); | ||
}); | ||
|
||
it("creates extension directory for specific store name", async () => { | ||
const fs = builder.mainDi.inject(fsInjectable); | ||
|
||
await builder.extensions.get("some-extension").main.getExtensionFileFolder(); | ||
|
||
const nonHashedExtensionDirectories = await fs.readdir("/some-directory-for-app-data/some-product-name/extension_data"); | ||
|
||
expect(nonHashedExtensionDirectories).toContain("some-specific-store-name"); | ||
}); | ||
}); | ||
|
||
describe("when extension with no specific store name is enabled", () => { | ||
let testExtensionOptions: FakeExtensionOptions; | ||
|
||
beforeEach(() => { | ||
|
||
testExtensionOptions = { | ||
id: "some-extension", | ||
name: "some-extension-name", | ||
|
||
mainOptions: { | ||
manifest: { | ||
name: "some-package-name", | ||
storeName: undefined, | ||
version: "0", | ||
engines: { | ||
lens: "0", | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
builder.extensions.enable(testExtensionOptions); | ||
}); | ||
|
||
it("creates extension directory for package name", async () => { | ||
const fs = builder.mainDi.inject(fsInjectable); | ||
|
||
await builder.extensions.get("some-extension").main.getExtensionFileFolder(); | ||
|
||
const nonHashedExtensionDirectories = await fs.readdir("/some-directory-for-app-data/some-product-name/extension_data"); | ||
|
||
expect(nonHashedExtensionDirectories).toContain("some-package-name"); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
...-loader/file-system-provisioner-store/ensure-hashed-directory-for-extension.injectable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
|
||
import type { ObservableMap } from "mobx"; | ||
import { getInjectable } from "@ogre-tools/injectable"; | ||
|
||
import { getOrInsertWithAsync } from "../../../common/utils"; | ||
import randomBytesInjectable from "../../../common/utils/random-bytes.injectable"; | ||
import joinPathsInjectable from "../../../common/path/join-paths.injectable"; | ||
import directoryForExtensionDataInjectable from "./directory-for-extension-data.injectable"; | ||
import ensureDirInjectable from "../../../common/fs/ensure-dir.injectable"; | ||
import getHashInjectable from "./get-hash.injectable"; | ||
|
||
export type EnsureHashedDirectoryForExtension = (extensionName: string, registeredExtensions: ObservableMap<string, string>) => Promise<string>; | ||
|
||
const ensureHashedDirectoryForExtensionInjectable = getInjectable({ | ||
id: "ensure-hashed-directory-for-extension", | ||
|
||
instantiate: (di): EnsureHashedDirectoryForExtension => { | ||
const randomBytes = di.inject(randomBytesInjectable); | ||
const joinPaths = di.inject(joinPathsInjectable); | ||
const directoryForExtensionData = di.inject(directoryForExtensionDataInjectable); | ||
const ensureDirectory = di.inject(ensureDirInjectable); | ||
const getHash = di.inject(getHashInjectable); | ||
|
||
return async (extensionName, registeredExtensions) => { | ||
const dirPath = await getOrInsertWithAsync(registeredExtensions, extensionName, async () => { | ||
const salt = (await randomBytes(32)).toString("hex"); | ||
const hashedName = getHash(`${extensionName}/${salt}`); | ||
|
||
return joinPaths(directoryForExtensionData, hashedName); | ||
}); | ||
|
||
await ensureDirectory(dirPath); | ||
|
||
return dirPath; | ||
}; | ||
}, | ||
}); | ||
|
||
export default ensureHashedDirectoryForExtensionInjectable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...core/src/extensions/extension-loader/file-system-provisioner-store/get-hash.injectable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Copyright (c) OpenLens Authors. All rights reserved. | ||
* Licensed under MIT License. See LICENSE in root directory for more information. | ||
*/ | ||
|
||
import { getInjectable } from "@ogre-tools/injectable"; | ||
import { SHA256 } from "crypto-js"; | ||
|
||
const getHashInjectable = getInjectable({ | ||
id: "get-hash", | ||
|
||
instantiate: () => (text: string) => SHA256(text).toString(), | ||
}); | ||
|
||
export default getHashInjectable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,6 +27,10 @@ export interface LensExtensionManifest extends PackageJson { | |||||
npm?: string; | ||||||
node?: string; | ||||||
}; | ||||||
|
||||||
// Specify extension name used for persisting data. | ||||||
// Useful if extension is renamed but the data should not be lost. | ||||||
storeName?: string; | ||||||
} | ||||||
|
||||||
export const lensExtensionDependencies = Symbol("lens-extension-dependencies"); | ||||||
|
@@ -62,7 +66,10 @@ export class LensExtension< | |||||
|
||||||
constructor({ id, manifest, manifestPath, isBundled }: InstalledExtension) { | ||||||
makeObservable(this); | ||||||
|
||||||
// id is the name of the manifest | ||||||
this.id = id; | ||||||
|
||||||
this.manifest = manifest; | ||||||
this.manifestPath = manifestPath; | ||||||
this.isBundled = !!isBundled; | ||||||
|
@@ -80,6 +87,11 @@ export class LensExtension< | |||||
return this.manifest.description; | ||||||
} | ||||||
|
||||||
// Name of extension for persisting data | ||||||
get storeName() { | ||||||
return this.manifest.storeName || this.name; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we want this. If |
||||||
} | ||||||
|
||||||
/** | ||||||
* @ignore | ||||||
*/ | ||||||
|
@@ -93,7 +105,8 @@ export class LensExtension< | |||||
* folder name. | ||||||
*/ | ||||||
async getExtensionFileFolder(): Promise<string> { | ||||||
return this[lensExtensionDependencies].fileSystemProvisionerStore.requestDirectory(this.id); | ||||||
// storeName is read from the manifest and has a fallback to the manifest name, which equals id | ||||||
return this[lensExtensionDependencies].fileSystemProvisionerStore.requestDirectory(this.storeName); | ||||||
} | ||||||
|
||||||
@action | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add
causesSideEffects: true
since used external imported package?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In don't think that is necessary because a hash should be stable given its input