-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24199fa
commit c1078c6
Showing
10 changed files
with
325 additions
and
63 deletions.
There are no files selected for viewing
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
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,18 @@ | ||
/** | ||
* Dynamically loads a plugin from a file path by invoking the argument-less constructor of the default exported class. | ||
*/ | ||
export async function loadPlugin<T>(filePath: string): Promise<T> { | ||
try { | ||
const module = await import(filePath); | ||
|
||
// Check if the default export is a class | ||
if (typeof module.default === 'function') { | ||
const instance: T = new module.default() as T; | ||
return instance; | ||
} else { | ||
throw new Error(`Default export at ${filePath} is not a class.`); | ||
} | ||
} catch (error) { | ||
throw new Error(`Failed to load component at ${filePath}: ${error.message}`); | ||
} | ||
} |
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
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
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,26 @@ | ||
import type { DataStore } from "@tbd54566975/dwn-sdk-js"; | ||
import { DataStoreSql } from "@tbd54566975/dwn-sql-store"; | ||
import { getDialectFromUrl } from "../../src/storage.js"; | ||
|
||
/** | ||
* An example of a DataStore plugin that is used for testing. | ||
* The points to note are: | ||
* - The class must be a default export. | ||
* - The constructor must not take any arguments. | ||
*/ | ||
export default class DataStoreSqlite extends DataStoreSql implements DataStore { | ||
constructor() { | ||
const sqliteDialect = getDialectFromUrl(new URL('sqlite://')); | ||
super(sqliteDialect); | ||
|
||
// NOTE: the following line is added purely to test the constructor invocation. | ||
DataStoreSqlite.spyingTheConstructor(); | ||
} | ||
|
||
/** | ||
* NOTE: This method is introduced purely to indirectly test/spy invocation of the constructor. | ||
* As I was unable to find an easy way to directly spy the constructor. | ||
*/ | ||
public static spyingTheConstructor(): void { | ||
} | ||
} |
Oops, something went wrong.