From 2a2db0302746897bfed1b77048a98d1be9ca08b2 Mon Sep 17 00:00:00 2001 From: Henry Tsai Date: Wed, 10 Jul 2024 11:37:56 -0700 Subject: [PATCH] Added documentation --- README.md | 27 +++++++++++++++++++++++---- src/plugin-loader.ts | 10 ++-------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5490ef2..54b7d45 100644 --- a/README.md +++ b/README.md @@ -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 | | ---------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | @@ -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 +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 diff --git a/src/plugin-loader.ts b/src/plugin-loader.ts index fd5bc4f..44a3089 100644 --- a/src/plugin-loader.ts +++ b/src/plugin-loader.ts @@ -8,14 +8,8 @@ export class PluginLoader { public static async loadPlugin(filePath: string): Promise { 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}`); }