Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Jul 10, 2024
1 parent 63444ce commit 2a2db03
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,16 @@ Configuration can be set using environment variables
| `DWN_REGISTRATION_PROOF_OF_WORK_ENABLED` | Require new users to complete a proof-of-work challenge | `false` |
| `DWN_REGISTRATION_PROOF_OF_WORK_INITIAL_MAX_HASH` | Initial maximum allowed hash in 64 char HEX string. The more leading zeros (smaller number) the higher the difficulty. | `false` |
| `DWN_STORAGE` | URL to use for storage by default. See [Storage Options](#storage-options) for details | `level://data` |
| `DWN_STORAGE_MESSAGES` | URL to use for storage of messages. | value of `DWN_STORAGE` |
| `DWN_STORAGE_DATA` | URL to use for data storage | value of `DWN_STORAGE` |
| `DWN_STORAGE_EVENTS` | URL to use for event storage | value of `DWN_STORAGE` |
| `DWN_STORAGE_MESSAGES` | Connection URL or file path to custom plugin to use for the message store. | value of `DWN_STORAGE` |
| `DWN_STORAGE_DATA` | Connection URL or file path to custom plugin to use for the data store. | value of `DWN_STORAGE` |
| `DWN_STORAGE_RESUMABLE_TASKS` | Connection URL or file path to custom plugin to use for the resumable task store. | value of `DWN_STORAGE` |
| `DWN_STORAGE_EVENTS` | Connection URL or file path to custom plugin to use for the event store. | value of `DWN_STORAGE` |
| `DWN_TERMS_OF_SERVICE_FILE_PATH` | Required terms of service agreement if set. Value is path to the terms of service file. | unset |
| `DWN_TTL_CACHE_URL` | URL of the TTL cache used by the DWN. Currently only supports SQL databases. | `sqlite://` |

### Storage Options

Several storage formats are supported, and may be configured with the `DWN_STORAGE_*` environment variables:
Several built storage options are supported, and may be configured with the `DWN_STORAGE_*` environment variables:

| Database | Example | Notes |
| ---------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -305,6 +306,24 @@ Several storage formats are supported, and may be configured with the `DWN_STORA
| MySQL | `mysql://user:pass@host/db?debug=true&timezone=-0700` | [all URL options documented here](https://github.com/mysqljs/mysql#connection-options) |
| PostgreSQL | `postgres:///dwn` | any options other than the URL scheme (`postgres://`) may also be specified via [standard environment variables](https://node-postgres.com/features/connecting#environment-variables) |

### Plugins

Check failure on line 309 in README.md

View workflow job for this annotation

GitHub Actions / lint

Headings should be surrounded by blank lines

README.md:309 MD022/blanks-around-headings/blanks-around-headers Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### Plugins"] https://github.com/DavidAnson/markdownlint/blob/v0.31.1/doc/md022.md

Check failure on line 309 in README.md

View workflow job for this annotation

GitHub Actions / lint

Headings should be surrounded by blank lines

README.md:309 MD022/blanks-around-headings/blanks-around-headers Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### Plugins"] https://github.com/DavidAnson/markdownlint/blob/v0.31.1/doc/md022.md
In some scenarios, you may want to provide a custom implementation of a pluggable module for the DWN Server. The following interfaces defined in `dwn-sdk-js` package are supported:

- `DataStore`
- `MessageStore`
- `ResumableDataStore`
- `EventLog`
- `EventStream`

To load your custom plugin, specify the absolute path to the `.js` file of your custom implementation using the corresponding environment variable. For instance, use `DWN_STORAGE_DATA` for a custom DWN Data Store.

Refer to the `tests/plugins/*.ts` files for examples of plugin implementations. In summary, you need to:

- Implement the corresponding interface from the `dwn-sdk-js` package. For example, implement the `DataStore` interface for a DWN Data Store.
- Ensure that the built `.js` file that will be referenced by the DWN Server config environment variable contains a class that:
1. Is a default export. This is how DWN Server locates the correct class for instantiation.
1. Has a public constructor that does not take any arguments. This is how DWN Server instantiates the plugin.

## Registration Requirements

There are multiple optional registration gates, each of which can be enabled (all are disabled by default). Tenants (DIDs) must comply with whatever
Expand Down
10 changes: 2 additions & 8 deletions src/plugin-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ export class PluginLoader {
public static async 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.`);
}
const instance: T = new module.default() as T;
return instance;
} catch (error) {
throw new Error(`Failed to load component at ${filePath}: ${error.message}`);
}
Expand Down

0 comments on commit 2a2db03

Please sign in to comment.