@monolayer/sdk 1.2.0-canary.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/bin/cli.js +21609 -0
- package/dist/cjs/configuration.d.ts +65 -0
- package/dist/cjs/configuration.js +45 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/introspection.d.ts +2 -0
- package/dist/cjs/introspection.js +11 -0
- package/dist/cjs/make/broadcast.d.ts +10 -0
- package/dist/cjs/make/broadcast.js +37 -0
- package/dist/cjs/make/config.d.ts +2 -0
- package/dist/cjs/make/config.js +44 -0
- package/dist/cjs/make/cron.d.ts +9 -0
- package/dist/cjs/make/cron.js +30 -0
- package/dist/cjs/make/make.d.ts +6 -0
- package/dist/cjs/make/make.js +134 -0
- package/dist/cjs/make/manifest.d.ts +202 -0
- package/dist/cjs/make/manifest.js +165 -0
- package/dist/cjs/make/required-filtes.d.ts +1 -0
- package/dist/cjs/make/required-filtes.js +17 -0
- package/dist/cjs/make/task.d.ts +9 -0
- package/dist/cjs/make/task.js +30 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/packages/docker/df.d.ts +543 -0
- package/dist/cjs/packages/docker/df.js +395 -0
- package/dist/cjs/packages/docker/validator.d.ts +7 -0
- package/dist/cjs/packages/docker/validator.js +18 -0
- package/dist/cjs/packages/utils/create-file.d.ts +1 -0
- package/dist/cjs/packages/utils/create-file.js +18 -0
- package/dist/cjs/packages/utils/date-string.d.ts +1 -0
- package/dist/cjs/packages/utils/date-string.js +12 -0
- package/dist/cjs/packages/utils/exception.d.ts +1 -0
- package/dist/cjs/packages/utils/exception.js +8 -0
- package/dist/cjs/packages/utils/hash-value.d.ts +16 -0
- package/dist/cjs/packages/utils/hash-value.js +25 -0
- package/dist/cjs/packages/utils/import-default.d.ts +1 -0
- package/dist/cjs/packages/utils/import-default.js +14 -0
- package/dist/cjs/packages/utils/import-file.d.ts +8 -0
- package/dist/cjs/packages/utils/import-file.js +41 -0
- package/dist/cjs/packages/utils/log-with-spinner.d.ts +1 -0
- package/dist/cjs/packages/utils/log-with-spinner.js +24 -0
- package/dist/cjs/packages/utils/path.d.ts +2 -0
- package/dist/cjs/packages/utils/path.js +21 -0
- package/dist/cjs/packages/utils/playground.d.ts +1 -0
- package/dist/cjs/packages/utils/playground.js +35 -0
- package/dist/cjs/scaffolding/add-env-var.d.ts +6 -0
- package/dist/cjs/scaffolding/add-env-var.js +65 -0
- package/dist/cjs/scaffolding/add-import.d.ts +16 -0
- package/dist/cjs/scaffolding/add-import.js +43 -0
- package/dist/cjs/scaffolding/add-script.d.ts +1 -0
- package/dist/cjs/scaffolding/add-script.js +35 -0
- package/dist/cjs/scaffolding/broadcast-workload.d.ts +1 -0
- package/dist/cjs/scaffolding/broadcast-workload.js +36 -0
- package/dist/cjs/scaffolding/bucket-workload.d.ts +6 -0
- package/dist/cjs/scaffolding/bucket-workload.js +23 -0
- package/dist/cjs/scaffolding/cron-workload.d.ts +6 -0
- package/dist/cjs/scaffolding/cron-workload.js +28 -0
- package/dist/cjs/scaffolding/drizzle.d.ts +4 -0
- package/dist/cjs/scaffolding/drizzle.js +22 -0
- package/dist/cjs/scaffolding/lifecycle.d.ts +9 -0
- package/dist/cjs/scaffolding/lifecycle.js +61 -0
- package/dist/cjs/scaffolding/postgres-database-workload.d.ts +15 -0
- package/dist/cjs/scaffolding/postgres-database-workload.js +39 -0
- package/dist/cjs/scaffolding/prisma.d.ts +4 -0
- package/dist/cjs/scaffolding/prisma.js +40 -0
- package/dist/cjs/scaffolding/replace-string.d.ts +10 -0
- package/dist/cjs/scaffolding/replace-string.js +37 -0
- package/dist/cjs/scaffolding/task-workload.d.ts +6 -0
- package/dist/cjs/scaffolding/task-workload.js +30 -0
- package/dist/cjs/scaffolding/workload.d.ts +1 -0
- package/dist/cjs/scaffolding/workload.js +18 -0
- package/dist/cjs/scan/project.d.ts +23 -0
- package/dist/cjs/scan/project.js +79 -0
- package/dist/cjs/scan/workload-imports.d.ts +21 -0
- package/dist/cjs/scan/workload-imports.js +188 -0
- package/dist/cjs/test-helpers/postgres.d.ts +13 -0
- package/dist/cjs/test-helpers/postgres.js +52 -0
- package/dist/cjs/test-helpers/redis.d.ts +13 -0
- package/dist/cjs/test-helpers/redis.js +46 -0
- package/dist/cjs/test-helpers/task.d.ts +14 -0
- package/dist/cjs/test-helpers/task.js +18 -0
- package/dist/cjs/test-helpers.d.ts +3 -0
- package/dist/cjs/test-helpers.js +10 -0
- package/dist/cjs/workloads/app-lifecycle/after-rollout.d.ts +22 -0
- package/dist/cjs/workloads/app-lifecycle/after-rollout.js +28 -0
- package/dist/cjs/workloads/app-lifecycle/before-rollout.d.ts +22 -0
- package/dist/cjs/workloads/app-lifecycle/before-rollout.js +28 -0
- package/dist/cjs/workloads/app-lifecycle/bootstrap.d.ts +22 -0
- package/dist/cjs/workloads/app-lifecycle/bootstrap.js +28 -0
- package/dist/cjs/workloads/app-lifecycle/lifecycle-workload.d.ts +11 -0
- package/dist/cjs/workloads/app-lifecycle/lifecycle-workload.js +11 -0
- package/dist/cjs/workloads/assertions.d.ts +13 -0
- package/dist/cjs/workloads/assertions.js +14 -0
- package/dist/cjs/workloads/stateful/bucket.d.ts +82 -0
- package/dist/cjs/workloads/stateful/bucket.js +95 -0
- package/dist/cjs/workloads/stateful/database.d.ts +25 -0
- package/dist/cjs/workloads/stateful/database.js +42 -0
- package/dist/cjs/workloads/stateful/postgres-database.d.ts +29 -0
- package/dist/cjs/workloads/stateful/postgres-database.js +39 -0
- package/dist/cjs/workloads/stateful/redis.d.ts +30 -0
- package/dist/cjs/workloads/stateful/redis.js +40 -0
- package/dist/cjs/workloads/stateful/stateful-workload.d.ts +25 -0
- package/dist/cjs/workloads/stateful/stateful-workload.js +41 -0
- package/dist/cjs/workloads/stateless/broadcast/channel-data.d.ts +3 -0
- package/dist/cjs/workloads/stateless/broadcast/channel-data.js +6 -0
- package/dist/cjs/workloads/stateless/broadcast/client/app-sync-client.d.ts +78 -0
- package/dist/cjs/workloads/stateless/broadcast/client/app-sync-client.js +128 -0
- package/dist/cjs/workloads/stateless/broadcast/client/app-sync-publisher.d.ts +34 -0
- package/dist/cjs/workloads/stateless/broadcast/client/app-sync-publisher.js +78 -0
- package/dist/cjs/workloads/stateless/broadcast/client/authenticator.d.ts +28 -0
- package/dist/cjs/workloads/stateless/broadcast/client/authenticator.js +43 -0
- package/dist/cjs/workloads/stateless/broadcast/client/broadcast-provider.d.ts +17 -0
- package/dist/cjs/workloads/stateless/broadcast/client/broadcast-provider.js +34 -0
- package/dist/cjs/workloads/stateless/broadcast/client/client.d.ts +8 -0
- package/dist/cjs/workloads/stateless/broadcast/client/client.js +28 -0
- package/dist/cjs/workloads/stateless/broadcast/client/connectionManager.d.ts +62 -0
- package/dist/cjs/workloads/stateless/broadcast/client/connectionManager.js +166 -0
- package/dist/cjs/workloads/stateless/broadcast/client/eventEmitter.d.ts +32 -0
- package/dist/cjs/workloads/stateless/broadcast/client/eventEmitter.js +53 -0
- package/dist/cjs/workloads/stateless/broadcast/client/messageBuilder.d.ts +24 -0
- package/dist/cjs/workloads/stateless/broadcast/client/messageBuilder.js +43 -0
- package/dist/cjs/workloads/stateless/broadcast/client/parser.d.ts +12 -0
- package/dist/cjs/workloads/stateless/broadcast/client/parser.js +22 -0
- package/dist/cjs/workloads/stateless/broadcast/client/provider.d.ts +4 -0
- package/dist/cjs/workloads/stateless/broadcast/client/provider.js +11 -0
- package/dist/cjs/workloads/stateless/broadcast/client/subscriptionManager.d.ts +34 -0
- package/dist/cjs/workloads/stateless/broadcast/client/subscriptionManager.js +91 -0
- package/dist/cjs/workloads/stateless/broadcast/client/types.d.ts +72 -0
- package/dist/cjs/workloads/stateless/broadcast/client/types.js +6 -0
- package/dist/cjs/workloads/stateless/broadcast/matcher.d.ts +13 -0
- package/dist/cjs/workloads/stateless/broadcast/matcher.js +46 -0
- package/dist/cjs/workloads/stateless/broadcast/router.d.ts +103 -0
- package/dist/cjs/workloads/stateless/broadcast/router.js +98 -0
- package/dist/cjs/workloads/stateless/broadcast/types.d.ts +17 -0
- package/dist/cjs/workloads/stateless/broadcast/types.js +2 -0
- package/dist/cjs/workloads/stateless/cron.d.ts +49 -0
- package/dist/cjs/workloads/stateless/cron.js +55 -0
- package/dist/cjs/workloads/stateless/stateless-workload.d.ts +3 -0
- package/dist/cjs/workloads/stateless/stateless-workload.js +7 -0
- package/dist/cjs/workloads/stateless/task/backoffs.d.ts +9 -0
- package/dist/cjs/workloads/stateless/task/backoffs.js +17 -0
- package/dist/cjs/workloads/stateless/task/dispatcher.d.ts +3 -0
- package/dist/cjs/workloads/stateless/task/dispatcher.js +22 -0
- package/dist/cjs/workloads/stateless/task/local.d.ts +14 -0
- package/dist/cjs/workloads/stateless/task/local.js +54 -0
- package/dist/cjs/workloads/stateless/task/perform-later.d.ts +2 -0
- package/dist/cjs/workloads/stateless/task/perform-later.js +7 -0
- package/dist/cjs/workloads/stateless/task/perform-now.d.ts +5 -0
- package/dist/cjs/workloads/stateless/task/perform-now.js +24 -0
- package/dist/cjs/workloads/stateless/task/perform.d.ts +7 -0
- package/dist/cjs/workloads/stateless/task/perform.js +10 -0
- package/dist/cjs/workloads/stateless/task/task.d.ts +88 -0
- package/dist/cjs/workloads/stateless/task/task.js +61 -0
- package/dist/cjs/workloads/stateless/task/validate-data-size.d.ts +1 -0
- package/dist/cjs/workloads/stateless/task/validate-data-size.js +10 -0
- package/dist/cjs/workloads/workload.d.ts +14 -0
- package/dist/cjs/workloads/workload.js +16 -0
- package/dist/cjs/workloads.d.ts +22 -0
- package/dist/cjs/workloads.js +30 -0
- package/dist/esm/configuration.d.ts +65 -0
- package/dist/esm/configuration.js +15 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/introspection.d.ts +2 -0
- package/dist/esm/introspection.js +2 -0
- package/dist/esm/make/broadcast.d.ts +10 -0
- package/dist/esm/make/broadcast.js +30 -0
- package/dist/esm/make/config.d.ts +2 -0
- package/dist/esm/make/config.js +41 -0
- package/dist/esm/make/cron.d.ts +9 -0
- package/dist/esm/make/cron.js +23 -0
- package/dist/esm/make/make.d.ts +6 -0
- package/dist/esm/make/make.js +120 -0
- package/dist/esm/make/manifest.d.ts +202 -0
- package/dist/esm/make/manifest.js +162 -0
- package/dist/esm/make/required-filtes.d.ts +1 -0
- package/dist/esm/make/required-filtes.js +11 -0
- package/dist/esm/make/task.d.ts +9 -0
- package/dist/esm/make/task.js +23 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/packages/docker/df.d.ts +543 -0
- package/dist/esm/packages/docker/df.js +381 -0
- package/dist/esm/packages/docker/validator.d.ts +7 -0
- package/dist/esm/packages/docker/validator.js +14 -0
- package/dist/esm/packages/utils/create-file.d.ts +1 -0
- package/dist/esm/packages/utils/create-file.js +11 -0
- package/dist/esm/packages/utils/date-string.d.ts +1 -0
- package/dist/esm/packages/utils/date-string.js +8 -0
- package/dist/esm/packages/utils/exception.d.ts +1 -0
- package/dist/esm/packages/utils/exception.js +4 -0
- package/dist/esm/packages/utils/hash-value.d.ts +16 -0
- package/dist/esm/packages/utils/hash-value.js +20 -0
- package/dist/esm/packages/utils/import-default.d.ts +1 -0
- package/dist/esm/packages/utils/import-default.js +10 -0
- package/dist/esm/packages/utils/import-file.d.ts +8 -0
- package/dist/esm/packages/utils/import-file.js +13 -0
- package/dist/esm/packages/utils/log-with-spinner.d.ts +1 -0
- package/dist/esm/packages/utils/log-with-spinner.js +17 -0
- package/dist/esm/packages/utils/path.d.ts +2 -0
- package/dist/esm/packages/utils/path.js +17 -0
- package/dist/esm/packages/utils/playground.d.ts +1 -0
- package/dist/esm/packages/utils/playground.js +11 -0
- package/dist/esm/scaffolding/add-env-var.d.ts +6 -0
- package/dist/esm/scaffolding/add-env-var.js +57 -0
- package/dist/esm/scaffolding/add-import.d.ts +16 -0
- package/dist/esm/scaffolding/add-import.js +39 -0
- package/dist/esm/scaffolding/add-script.d.ts +1 -0
- package/dist/esm/scaffolding/add-script.js +29 -0
- package/dist/esm/scaffolding/broadcast-workload.d.ts +1 -0
- package/dist/esm/scaffolding/broadcast-workload.js +30 -0
- package/dist/esm/scaffolding/bucket-workload.d.ts +6 -0
- package/dist/esm/scaffolding/bucket-workload.js +17 -0
- package/dist/esm/scaffolding/cron-workload.d.ts +6 -0
- package/dist/esm/scaffolding/cron-workload.js +22 -0
- package/dist/esm/scaffolding/drizzle.d.ts +4 -0
- package/dist/esm/scaffolding/drizzle.js +19 -0
- package/dist/esm/scaffolding/lifecycle.d.ts +9 -0
- package/dist/esm/scaffolding/lifecycle.js +53 -0
- package/dist/esm/scaffolding/postgres-database-workload.d.ts +15 -0
- package/dist/esm/scaffolding/postgres-database-workload.js +31 -0
- package/dist/esm/scaffolding/prisma.d.ts +4 -0
- package/dist/esm/scaffolding/prisma.js +34 -0
- package/dist/esm/scaffolding/replace-string.d.ts +10 -0
- package/dist/esm/scaffolding/replace-string.js +30 -0
- package/dist/esm/scaffolding/task-workload.d.ts +6 -0
- package/dist/esm/scaffolding/task-workload.js +24 -0
- package/dist/esm/scaffolding/workload.d.ts +1 -0
- package/dist/esm/scaffolding/workload.js +12 -0
- package/dist/esm/scan/project.d.ts +23 -0
- package/dist/esm/scan/project.js +69 -0
- package/dist/esm/scan/workload-imports.d.ts +21 -0
- package/dist/esm/scan/workload-imports.js +144 -0
- package/dist/esm/test-helpers/postgres.d.ts +13 -0
- package/dist/esm/test-helpers/postgres.js +26 -0
- package/dist/esm/test-helpers/redis.d.ts +13 -0
- package/dist/esm/test-helpers/redis.js +20 -0
- package/dist/esm/test-helpers/task.d.ts +14 -0
- package/dist/esm/test-helpers/task.js +14 -0
- package/dist/esm/test-helpers.d.ts +3 -0
- package/dist/esm/test-helpers.js +3 -0
- package/dist/esm/workloads/app-lifecycle/after-rollout.d.ts +22 -0
- package/dist/esm/workloads/app-lifecycle/after-rollout.js +24 -0
- package/dist/esm/workloads/app-lifecycle/before-rollout.d.ts +22 -0
- package/dist/esm/workloads/app-lifecycle/before-rollout.js +24 -0
- package/dist/esm/workloads/app-lifecycle/bootstrap.d.ts +22 -0
- package/dist/esm/workloads/app-lifecycle/bootstrap.js +24 -0
- package/dist/esm/workloads/app-lifecycle/lifecycle-workload.d.ts +11 -0
- package/dist/esm/workloads/app-lifecycle/lifecycle-workload.js +7 -0
- package/dist/esm/workloads/assertions.d.ts +13 -0
- package/dist/esm/workloads/assertions.js +6 -0
- package/dist/esm/workloads/stateful/bucket.d.ts +82 -0
- package/dist/esm/workloads/stateful/bucket.js +95 -0
- package/dist/esm/workloads/stateful/database.d.ts +25 -0
- package/dist/esm/workloads/stateful/database.js +42 -0
- package/dist/esm/workloads/stateful/postgres-database.d.ts +29 -0
- package/dist/esm/workloads/stateful/postgres-database.js +35 -0
- package/dist/esm/workloads/stateful/redis.d.ts +30 -0
- package/dist/esm/workloads/stateful/redis.js +36 -0
- package/dist/esm/workloads/stateful/stateful-workload.d.ts +25 -0
- package/dist/esm/workloads/stateful/stateful-workload.js +41 -0
- package/dist/esm/workloads/stateless/broadcast/channel-data.d.ts +3 -0
- package/dist/esm/workloads/stateless/broadcast/channel-data.js +2 -0
- package/dist/esm/workloads/stateless/broadcast/client/app-sync-client.d.ts +78 -0
- package/dist/esm/workloads/stateless/broadcast/client/app-sync-client.js +130 -0
- package/dist/esm/workloads/stateless/broadcast/client/app-sync-publisher.d.ts +34 -0
- package/dist/esm/workloads/stateless/broadcast/client/app-sync-publisher.js +76 -0
- package/dist/esm/workloads/stateless/broadcast/client/authenticator.d.ts +28 -0
- package/dist/esm/workloads/stateless/broadcast/client/authenticator.js +39 -0
- package/dist/esm/workloads/stateless/broadcast/client/broadcast-provider.d.ts +17 -0
- package/dist/esm/workloads/stateless/broadcast/client/broadcast-provider.js +29 -0
- package/dist/esm/workloads/stateless/broadcast/client/client.d.ts +8 -0
- package/dist/esm/workloads/stateless/broadcast/client/client.js +25 -0
- package/dist/esm/workloads/stateless/broadcast/client/connectionManager.d.ts +62 -0
- package/dist/esm/workloads/stateless/broadcast/client/connectionManager.js +164 -0
- package/dist/esm/workloads/stateless/broadcast/client/eventEmitter.d.ts +32 -0
- package/dist/esm/workloads/stateless/broadcast/client/eventEmitter.js +50 -0
- package/dist/esm/workloads/stateless/broadcast/client/messageBuilder.d.ts +24 -0
- package/dist/esm/workloads/stateless/broadcast/client/messageBuilder.js +39 -0
- package/dist/esm/workloads/stateless/broadcast/client/parser.d.ts +12 -0
- package/dist/esm/workloads/stateless/broadcast/client/parser.js +19 -0
- package/dist/esm/workloads/stateless/broadcast/client/provider.d.ts +4 -0
- package/dist/esm/workloads/stateless/broadcast/client/provider.js +8 -0
- package/dist/esm/workloads/stateless/broadcast/client/subscriptionManager.d.ts +34 -0
- package/dist/esm/workloads/stateless/broadcast/client/subscriptionManager.js +91 -0
- package/dist/esm/workloads/stateless/broadcast/client/types.d.ts +72 -0
- package/dist/esm/workloads/stateless/broadcast/client/types.js +5 -0
- package/dist/esm/workloads/stateless/broadcast/matcher.d.ts +13 -0
- package/dist/esm/workloads/stateless/broadcast/matcher.js +43 -0
- package/dist/esm/workloads/stateless/broadcast/router.d.ts +103 -0
- package/dist/esm/workloads/stateless/broadcast/router.js +93 -0
- package/dist/esm/workloads/stateless/broadcast/server/broadcast-server.d.ts +2 -0
- package/dist/esm/workloads/stateless/broadcast/server/broadcast-server.js +316 -0
- package/dist/esm/workloads/stateless/broadcast/types.d.ts +17 -0
- package/dist/esm/workloads/stateless/broadcast/types.js +1 -0
- package/dist/esm/workloads/stateless/cron.d.ts +49 -0
- package/dist/esm/workloads/stateless/cron.js +52 -0
- package/dist/esm/workloads/stateless/stateless-workload.d.ts +3 -0
- package/dist/esm/workloads/stateless/stateless-workload.js +8 -0
- package/dist/esm/workloads/stateless/task/backoffs.d.ts +9 -0
- package/dist/esm/workloads/stateless/task/backoffs.js +14 -0
- package/dist/esm/workloads/stateless/task/dispatcher.d.ts +3 -0
- package/dist/esm/workloads/stateless/task/dispatcher.js +18 -0
- package/dist/esm/workloads/stateless/task/local.d.ts +14 -0
- package/dist/esm/workloads/stateless/task/local.js +48 -0
- package/dist/esm/workloads/stateless/task/perform-later.d.ts +2 -0
- package/dist/esm/workloads/stateless/task/perform-later.js +4 -0
- package/dist/esm/workloads/stateless/task/perform-now.d.ts +5 -0
- package/dist/esm/workloads/stateless/task/perform-now.js +18 -0
- package/dist/esm/workloads/stateless/task/perform.d.ts +7 -0
- package/dist/esm/workloads/stateless/task/perform.js +8 -0
- package/dist/esm/workloads/stateless/task/task.d.ts +88 -0
- package/dist/esm/workloads/stateless/task/task.js +59 -0
- package/dist/esm/workloads/stateless/task/validate-data-size.d.ts +1 -0
- package/dist/esm/workloads/stateless/task/validate-data-size.js +7 -0
- package/dist/esm/workloads/workload.d.ts +14 -0
- package/dist/esm/workloads/workload.js +16 -0
- package/dist/esm/workloads.d.ts +22 -0
- package/dist/esm/workloads.js +13 -0
- package/package.json +155 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { StatefulWorkloadWithClient } from "../../workloads/stateful/stateful-workload.js";
|
|
2
|
+
/**
|
|
3
|
+
* Database workload.
|
|
4
|
+
*
|
|
5
|
+
* @group Abstract Classes
|
|
6
|
+
* @typeParam C - Client type
|
|
7
|
+
*/
|
|
8
|
+
export class Database extends StatefulWorkloadWithClient {
|
|
9
|
+
/**
|
|
10
|
+
* Database name.
|
|
11
|
+
*/
|
|
12
|
+
databaseName;
|
|
13
|
+
constructor(
|
|
14
|
+
/**
|
|
15
|
+
* Database name.
|
|
16
|
+
*/
|
|
17
|
+
databaseName) {
|
|
18
|
+
super(databaseName);
|
|
19
|
+
this.databaseName = databaseName;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Database ID
|
|
23
|
+
*
|
|
24
|
+
* **Note:**
|
|
25
|
+
* Alias of `ìd`.
|
|
26
|
+
*/
|
|
27
|
+
get databaseId() {
|
|
28
|
+
return this.id;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
get connStringComponents() {
|
|
34
|
+
return [
|
|
35
|
+
this.connStringPrefix(),
|
|
36
|
+
...(this.id === this.databaseName
|
|
37
|
+
? [this.id]
|
|
38
|
+
: [this.id, this.databaseName]),
|
|
39
|
+
"database",
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Database } from "../../workloads/stateful/database.js";
|
|
2
|
+
/**
|
|
3
|
+
* Workload for PostgreSQL databases.
|
|
4
|
+
*
|
|
5
|
+
* A `PostgresDatabase` workload is initialized with:
|
|
6
|
+
* - A valid database name.
|
|
7
|
+
*
|
|
8
|
+
* The environment variable with the connection string for the database is named after
|
|
9
|
+
* the `databaseName` and the `databaseId`. See examples.
|
|
10
|
+
*
|
|
11
|
+
* **NOTES**
|
|
12
|
+
*
|
|
13
|
+
* When launching the development or test containers with `npx monolayer start dev`, the environment
|
|
14
|
+
* variable with the connection string for the workload's Docker container
|
|
15
|
+
* will be written to the corresponding dotenv file (`.env.local` or `.env.local.test`)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { PostgreSQL } from "@monolayer/sdk";
|
|
20
|
+
* import pg from "pg";
|
|
21
|
+
*
|
|
22
|
+
* // Workloads on different database servers
|
|
23
|
+
* export const producstDb = new PostgresDatabase("products");
|
|
24
|
+
*
|
|
25
|
+
* export const analyticsDb = new PostgresDatabase("analytics");
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare class PostgresDatabase extends Database {
|
|
29
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Database } from "../../workloads/stateful/database.js";
|
|
2
|
+
/**
|
|
3
|
+
* Workload for PostgreSQL databases.
|
|
4
|
+
*
|
|
5
|
+
* A `PostgresDatabase` workload is initialized with:
|
|
6
|
+
* - A valid database name.
|
|
7
|
+
*
|
|
8
|
+
* The environment variable with the connection string for the database is named after
|
|
9
|
+
* the `databaseName` and the `databaseId`. See examples.
|
|
10
|
+
*
|
|
11
|
+
* **NOTES**
|
|
12
|
+
*
|
|
13
|
+
* When launching the development or test containers with `npx monolayer start dev`, the environment
|
|
14
|
+
* variable with the connection string for the workload's Docker container
|
|
15
|
+
* will be written to the corresponding dotenv file (`.env.local` or `.env.local.test`)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { PostgreSQL } from "@monolayer/sdk";
|
|
20
|
+
* import pg from "pg";
|
|
21
|
+
*
|
|
22
|
+
* // Workloads on different database servers
|
|
23
|
+
* export const producstDb = new PostgresDatabase("products");
|
|
24
|
+
*
|
|
25
|
+
* export const analyticsDb = new PostgresDatabase("analytics");
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export class PostgresDatabase extends Database {
|
|
29
|
+
/**
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
connStringPrefix() {
|
|
33
|
+
return "pg";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { StatefulWorkloadWithClient } from "../../workloads/stateful/stateful-workload.js";
|
|
2
|
+
/**
|
|
3
|
+
* Workload for Redis API compatible servers.
|
|
4
|
+
*
|
|
5
|
+
* The `Redis` workload is initialized with:
|
|
6
|
+
* - A stable ID.
|
|
7
|
+
* - A client constructor function providing the client of your choice.
|
|
8
|
+
* The {@link Redis.client | client } accessor will call this function and memoize its result.
|
|
9
|
+
* The expected envirnoment variable name with the connection string is passed as an argument.
|
|
10
|
+
*
|
|
11
|
+
* **NOTES**
|
|
12
|
+
*
|
|
13
|
+
* When launching the development or test containers with `npx monolayer start dev`, the environment
|
|
14
|
+
* variable with the connection string for the workload's Docker container
|
|
15
|
+
* will be written to the corresponding dotenv file (`.env` or `.env.test`)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { Redis } from "@monolayer/sdk";
|
|
20
|
+
* import { Redis as IORedis } from "ioredis";
|
|
21
|
+
*
|
|
22
|
+
* const cache = new Redis("cache", (envVarName) =>
|
|
23
|
+
* new IORedis(process.env[envVarName]!)
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @typeParam C - Client type
|
|
28
|
+
*/
|
|
29
|
+
export declare class Redis extends StatefulWorkloadWithClient {
|
|
30
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { StatefulWorkloadWithClient } from "../../workloads/stateful/stateful-workload.js";
|
|
2
|
+
/**
|
|
3
|
+
* Workload for Redis API compatible servers.
|
|
4
|
+
*
|
|
5
|
+
* The `Redis` workload is initialized with:
|
|
6
|
+
* - A stable ID.
|
|
7
|
+
* - A client constructor function providing the client of your choice.
|
|
8
|
+
* The {@link Redis.client | client } accessor will call this function and memoize its result.
|
|
9
|
+
* The expected envirnoment variable name with the connection string is passed as an argument.
|
|
10
|
+
*
|
|
11
|
+
* **NOTES**
|
|
12
|
+
*
|
|
13
|
+
* When launching the development or test containers with `npx monolayer start dev`, the environment
|
|
14
|
+
* variable with the connection string for the workload's Docker container
|
|
15
|
+
* will be written to the corresponding dotenv file (`.env` or `.env.test`)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { Redis } from "@monolayer/sdk";
|
|
20
|
+
* import { Redis as IORedis } from "ioredis";
|
|
21
|
+
*
|
|
22
|
+
* const cache = new Redis("cache", (envVarName) =>
|
|
23
|
+
* new IORedis(process.env[envVarName]!)
|
|
24
|
+
* );
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @typeParam C - Client type
|
|
28
|
+
*/
|
|
29
|
+
export class Redis extends StatefulWorkloadWithClient {
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
get connStringComponents() {
|
|
34
|
+
return ["redis", this.id];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Workload } from "../../workloads/workload.js";
|
|
2
|
+
/**
|
|
3
|
+
* @group Abstract Classes
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class StatefulWorkload extends Workload {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @group Abstract Classes
|
|
9
|
+
* @typeParam C - Client type
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class StatefulWorkloadWithClient extends StatefulWorkload {
|
|
12
|
+
constructor(
|
|
13
|
+
/**
|
|
14
|
+
* Unique ID.
|
|
15
|
+
*/
|
|
16
|
+
id: string);
|
|
17
|
+
/**
|
|
18
|
+
* Returns the unique environment variable name that should hold the connection string.
|
|
19
|
+
*/
|
|
20
|
+
get connectionStringEnvVar(): string;
|
|
21
|
+
/**
|
|
22
|
+
* Reads the value fron environment variable name that should hold the connection string.
|
|
23
|
+
*/
|
|
24
|
+
get connectionString(): string;
|
|
25
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { snakeCase } from "case-anything";
|
|
2
|
+
import { Workload } from "../../workloads/workload.js";
|
|
3
|
+
/**
|
|
4
|
+
* @group Abstract Classes
|
|
5
|
+
*/
|
|
6
|
+
export class StatefulWorkload extends Workload {
|
|
7
|
+
/**
|
|
8
|
+
* @hidden
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
stateful;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @group Abstract Classes
|
|
15
|
+
* @typeParam C - Client type
|
|
16
|
+
*/
|
|
17
|
+
export class StatefulWorkloadWithClient extends StatefulWorkload {
|
|
18
|
+
constructor(
|
|
19
|
+
/**
|
|
20
|
+
* Unique ID.
|
|
21
|
+
*/
|
|
22
|
+
id) {
|
|
23
|
+
super(id);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns the unique environment variable name that should hold the connection string.
|
|
27
|
+
*/
|
|
28
|
+
get connectionStringEnvVar() {
|
|
29
|
+
return snakeCase(["ml", ...this.connStringComponents, "url"].join("_")).toUpperCase();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Reads the value fron environment variable name that should hold the connection string.
|
|
33
|
+
*/
|
|
34
|
+
get connectionString() {
|
|
35
|
+
const value = process.env[this.connectionStringEnvVar];
|
|
36
|
+
if (value === undefined) {
|
|
37
|
+
throw new Error(`Expected environment variable with connection string ${this.connectionStringEnvVar}`);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module AppSyncEventsClient
|
|
3
|
+
* The main entry point for the AWS AppSync Events WebSocket client library.
|
|
4
|
+
*/
|
|
5
|
+
import type { ChannelData } from "../channel-data.js";
|
|
6
|
+
import type { RouteParams, ValidateUniqueParams } from "../types.js";
|
|
7
|
+
import { EventEmitter } from "./eventEmitter.js";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for the BroadcastClient.
|
|
10
|
+
*/
|
|
11
|
+
export interface BroadcastClientConfig {
|
|
12
|
+
url: string;
|
|
13
|
+
authorization: object;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A client for interacting with the AWS AppSync Events WebSocket API.
|
|
17
|
+
* This client handles connection management, authentication, and provides
|
|
18
|
+
* an interface for subscribing to and publishing events.
|
|
19
|
+
*/
|
|
20
|
+
export declare class BroadcastClient<C extends Record<string, ChannelData<any>>> {
|
|
21
|
+
private _channels;
|
|
22
|
+
private connectionManager;
|
|
23
|
+
private subscriptionManager;
|
|
24
|
+
eventEmitter: EventEmitter;
|
|
25
|
+
private readonly url;
|
|
26
|
+
private readonly host;
|
|
27
|
+
connected: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an instance of AppSyncEventsClient.
|
|
30
|
+
* @param url The WebSocket URL for the AWS AppSync Events API.
|
|
31
|
+
* @param authorization The authorization object required for the WebSocket connection.
|
|
32
|
+
*/
|
|
33
|
+
constructor(opts: {
|
|
34
|
+
url: string;
|
|
35
|
+
host: string;
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* Establishes a connection to the AWS AppSync Events WebSocket API.
|
|
39
|
+
* @returns A Promise that resolves when the connection is successfully established.
|
|
40
|
+
*/
|
|
41
|
+
connect(): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Disconnects from the AWS AppSync Events WebSocket API.
|
|
44
|
+
*/
|
|
45
|
+
disconnect(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Subscribes to a specified channel to receive real-time events.
|
|
48
|
+
* @param channel The channel path to subscribe to.
|
|
49
|
+
* @param authorization The authorization object for the subscription.
|
|
50
|
+
* @returns The ID of the created subscription.
|
|
51
|
+
*/
|
|
52
|
+
subscribe(channel: string, authorization: object, callback: (payload: any) => void): string;
|
|
53
|
+
subscribeTo<T extends keyof C & string>(channelName: ValidateUniqueParams<T>, params: RouteParams<T>, callback: (data: C[T] extends ChannelData<infer P> ? P : never) => void): string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Unsubscribes from a previously subscribed channel.
|
|
56
|
+
* @param subscriptionId The ID of the subscription to unsubscribe from.
|
|
57
|
+
*/
|
|
58
|
+
unsubscribe(subscriptionId: string): void;
|
|
59
|
+
/**
|
|
60
|
+
* Publishes events to a specified channel.
|
|
61
|
+
* @param channel The channel path to publish to.
|
|
62
|
+
* @param events An array of stringified JSON events to be published.
|
|
63
|
+
* @param authorization The authorization object for the publish operation.
|
|
64
|
+
*/
|
|
65
|
+
publish(channel: string, events: string[], authorization: object): void;
|
|
66
|
+
/**
|
|
67
|
+
* Registers a listener for a specific event emitted by the client.
|
|
68
|
+
* @param eventName The name of the event to listen for (e.g., 'message', 'connected', 'disconnected').
|
|
69
|
+
* @param listener The callback function to be invoked when the event is emitted.
|
|
70
|
+
*/
|
|
71
|
+
on(eventName: string, listener: (...args: any[]) => void): void;
|
|
72
|
+
/**
|
|
73
|
+
* Removes a registered listener for a specific event.
|
|
74
|
+
* @param eventName The name of the event to remove the listener from.
|
|
75
|
+
* @param listener The callback function to be removed.
|
|
76
|
+
*/
|
|
77
|
+
off(eventName: string, listener: (...args: any[]) => void): void;
|
|
78
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module AppSyncEventsClient
|
|
3
|
+
* The main entry point for the AWS AppSync Events WebSocket client library.
|
|
4
|
+
*/
|
|
5
|
+
import { v4 as uuidv4 } from "uuid";
|
|
6
|
+
import { ConnectionManager } from "./connectionManager.js";
|
|
7
|
+
import { EventEmitter } from "./eventEmitter.js";
|
|
8
|
+
import { buildPublishMessage } from "./messageBuilder.js";
|
|
9
|
+
import { SubscriptionManager } from "./subscriptionManager.js";
|
|
10
|
+
/**
|
|
11
|
+
* A client for interacting with the AWS AppSync Events WebSocket API.
|
|
12
|
+
* This client handles connection management, authentication, and provides
|
|
13
|
+
* an interface for subscribing to and publishing events.
|
|
14
|
+
*/
|
|
15
|
+
export class BroadcastClient {
|
|
16
|
+
connectionManager;
|
|
17
|
+
subscriptionManager;
|
|
18
|
+
eventEmitter;
|
|
19
|
+
url;
|
|
20
|
+
host;
|
|
21
|
+
connected;
|
|
22
|
+
/**
|
|
23
|
+
* Creates an instance of AppSyncEventsClient.
|
|
24
|
+
* @param url The WebSocket URL for the AWS AppSync Events API.
|
|
25
|
+
* @param authorization The authorization object required for the WebSocket connection.
|
|
26
|
+
*/
|
|
27
|
+
constructor(opts) {
|
|
28
|
+
this.url = opts.url;
|
|
29
|
+
this.host = opts.host;
|
|
30
|
+
this.connectionManager = new ConnectionManager(this.url);
|
|
31
|
+
this.eventEmitter = new EventEmitter();
|
|
32
|
+
this.subscriptionManager = new SubscriptionManager(this.connectionManager, this.eventEmitter);
|
|
33
|
+
this.connected = false;
|
|
34
|
+
// Register a handler for incoming messages from the ConnectionManager
|
|
35
|
+
this.connectionManager.onMessage((message) => {
|
|
36
|
+
switch (message.type) {
|
|
37
|
+
case "subscribe_error":
|
|
38
|
+
case "publish_error":
|
|
39
|
+
case "broadcast_error":
|
|
40
|
+
this.eventEmitter.emit("error", message);
|
|
41
|
+
break;
|
|
42
|
+
default:
|
|
43
|
+
this.eventEmitter.emit("message", message);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
// Register a handler for connection close events from the ConnectionManager
|
|
48
|
+
this.connectionManager.onClose(() => {
|
|
49
|
+
this.connected = false;
|
|
50
|
+
this.eventEmitter.emit("disconnected");
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Establishes a connection to the AWS AppSync Events WebSocket API.
|
|
55
|
+
* @returns A Promise that resolves when the connection is successfully established.
|
|
56
|
+
*/
|
|
57
|
+
async connect() {
|
|
58
|
+
await this.connectionManager.connect();
|
|
59
|
+
this.connected = true;
|
|
60
|
+
this.eventEmitter.emit("connected");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Disconnects from the AWS AppSync Events WebSocket API.
|
|
64
|
+
*/
|
|
65
|
+
disconnect() {
|
|
66
|
+
this.connected = false;
|
|
67
|
+
this.connectionManager.disconnect();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Subscribes to a specified channel to receive real-time events.
|
|
71
|
+
* @param channel The channel path to subscribe to.
|
|
72
|
+
* @param authorization The authorization object for the subscription.
|
|
73
|
+
* @returns The ID of the created subscription.
|
|
74
|
+
*/
|
|
75
|
+
subscribe(channel, authorization,
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
+
callback) {
|
|
78
|
+
return this.subscriptionManager.subscribe(channel, authorization, callback); // Add a dummy callback
|
|
79
|
+
}
|
|
80
|
+
subscribeTo(channelName, params, callback) {
|
|
81
|
+
const path = channelName.replace(/\[([^\]]+)\]/g, (match, key) => {
|
|
82
|
+
return params[key] || match; // If key is not found, keep the placeholder
|
|
83
|
+
});
|
|
84
|
+
// Construct the final URL
|
|
85
|
+
return this.subscriptionManager.subscribe(`default${path}`, {
|
|
86
|
+
clientId: this.connectionManager.clientId,
|
|
87
|
+
host: this.host,
|
|
88
|
+
Authorization: "--",
|
|
89
|
+
},
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
91
|
+
callback); // Add a dummy callback
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Unsubscribes from a previously subscribed channel.
|
|
96
|
+
* @param subscriptionId The ID of the subscription to unsubscribe from.
|
|
97
|
+
*/
|
|
98
|
+
unsubscribe(subscriptionId) {
|
|
99
|
+
this.subscriptionManager.unsubscribe(subscriptionId);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Publishes events to a specified channel.
|
|
103
|
+
* @param channel The channel path to publish to.
|
|
104
|
+
* @param events An array of stringified JSON events to be published.
|
|
105
|
+
* @param authorization The authorization object for the publish operation.
|
|
106
|
+
*/
|
|
107
|
+
publish(channel, events, authorization) {
|
|
108
|
+
const id = uuidv4();
|
|
109
|
+
const publishMessage = buildPublishMessage(id, channel, events, authorization);
|
|
110
|
+
this.connectionManager.send(JSON.stringify(publishMessage));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Registers a listener for a specific event emitted by the client.
|
|
114
|
+
* @param eventName The name of the event to listen for (e.g., 'message', 'connected', 'disconnected').
|
|
115
|
+
* @param listener The callback function to be invoked when the event is emitted.
|
|
116
|
+
*/
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
118
|
+
on(eventName, listener) {
|
|
119
|
+
this.eventEmitter.on(eventName, listener);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Removes a registered listener for a specific event.
|
|
123
|
+
* @param eventName The name of the event to remove the listener from.
|
|
124
|
+
* @param listener The callback function to be removed.
|
|
125
|
+
*/
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
127
|
+
off(eventName, listener) {
|
|
128
|
+
this.eventEmitter.off(eventName, listener);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module AppSyncEventsClient
|
|
3
|
+
* The main entry point for the AWS AppSync Events WebSocket client library.
|
|
4
|
+
*/
|
|
5
|
+
import type { ChannelData } from "../channel-data.js";
|
|
6
|
+
import type { RouteParams, ValidateUniqueParams } from "../types.js";
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for the BroadcastPublisher.
|
|
9
|
+
*/
|
|
10
|
+
export interface BroadcastPublisherConfig {
|
|
11
|
+
url: string;
|
|
12
|
+
authorization: object;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Publisher.
|
|
16
|
+
* Handles connection management, authentication, and provides
|
|
17
|
+
* an interface for publishing events.
|
|
18
|
+
*/
|
|
19
|
+
export declare class BroadcastPublisher<C extends Record<string, ChannelData<any>>> {
|
|
20
|
+
private _channels;
|
|
21
|
+
private connectionManager;
|
|
22
|
+
private eventEmitter;
|
|
23
|
+
constructor();
|
|
24
|
+
/**
|
|
25
|
+
* Establishes a connection to the AWS AppSync Events WebSocket API.
|
|
26
|
+
* @returns A Promise that resolves when the connection is successfully established.
|
|
27
|
+
*/
|
|
28
|
+
connect(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Disconnects from the AWS AppSync Events WebSocket API.
|
|
31
|
+
*/
|
|
32
|
+
disconnect(): void;
|
|
33
|
+
publishTo<T extends keyof C & string>(channelName: ValidateUniqueParams<T>, params: RouteParams<T>, data: C[T] extends ChannelData<infer P> ? P[] : never): Promise<boolean>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module AppSyncEventsClient
|
|
3
|
+
* The main entry point for the AWS AppSync Events WebSocket client library.
|
|
4
|
+
*/
|
|
5
|
+
import { v4 as uuidv4 } from "uuid";
|
|
6
|
+
import { ConnectionManager } from "./connectionManager.js";
|
|
7
|
+
import { EventEmitter } from "./eventEmitter.js";
|
|
8
|
+
import { buildPublishMessage } from "./messageBuilder.js";
|
|
9
|
+
/**
|
|
10
|
+
* Publisher.
|
|
11
|
+
* Handles connection management, authentication, and provides
|
|
12
|
+
* an interface for publishing events.
|
|
13
|
+
*/
|
|
14
|
+
export class BroadcastPublisher {
|
|
15
|
+
connectionManager;
|
|
16
|
+
eventEmitter;
|
|
17
|
+
constructor() {
|
|
18
|
+
const urlFromEnv = process.env.ML_BROADCAST_URL ?? "ws://localhost:9311";
|
|
19
|
+
this.connectionManager = new ConnectionManager(urlFromEnv);
|
|
20
|
+
this.eventEmitter = new EventEmitter();
|
|
21
|
+
// Register a handler for incoming messages from the ConnectionManager
|
|
22
|
+
this.connectionManager.onMessage((message) => {
|
|
23
|
+
switch (message.type) {
|
|
24
|
+
case "subscribe_error":
|
|
25
|
+
case "publish_error":
|
|
26
|
+
case "broadcast_error":
|
|
27
|
+
this.eventEmitter.emit("error", message);
|
|
28
|
+
break;
|
|
29
|
+
default:
|
|
30
|
+
this.eventEmitter.emit("message", message);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
// Register a handler for connection close events from the ConnectionManager
|
|
35
|
+
this.connectionManager.onClose(() => {
|
|
36
|
+
this.eventEmitter.emit("disconnected");
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Establishes a connection to the AWS AppSync Events WebSocket API.
|
|
41
|
+
* @returns A Promise that resolves when the connection is successfully established.
|
|
42
|
+
*/
|
|
43
|
+
async connect() {
|
|
44
|
+
await this.connectionManager.connect();
|
|
45
|
+
this.eventEmitter.emit("connected");
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Disconnects from the AWS AppSync Events WebSocket API.
|
|
49
|
+
*/
|
|
50
|
+
disconnect() {
|
|
51
|
+
this.connectionManager.disconnect();
|
|
52
|
+
}
|
|
53
|
+
async publishTo(channelName, params, data) {
|
|
54
|
+
try {
|
|
55
|
+
const path = channelName.replace(/\[([^\]]+)\]/g, (match, key) => {
|
|
56
|
+
return params[key] || match; // If key is not found, keep the placeholder
|
|
57
|
+
});
|
|
58
|
+
await this.connect();
|
|
59
|
+
for (const payload of data) {
|
|
60
|
+
const events = JSON.stringify(payload);
|
|
61
|
+
const publishMessage = buildPublishMessage(uuidv4(), `default${path}`, [events], {
|
|
62
|
+
clientId: this.connectionManager.clientId,
|
|
63
|
+
host: process.env.ML_BROADCAST_HOST ?? "localhost",
|
|
64
|
+
Authorization: "--",
|
|
65
|
+
});
|
|
66
|
+
this.connectionManager.send(JSON.stringify(publishMessage));
|
|
67
|
+
}
|
|
68
|
+
this.disconnect();
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
console.error(e);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module authenticator
|
|
3
|
+
* This module provides functions for handling the specific authentication
|
|
4
|
+
* handshake required by the AWS AppSync Events WebSocket protocol.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Encodes a JSON authorization object into a Base64URL string.
|
|
8
|
+
*
|
|
9
|
+
* The AWS AppSync WebSocket protocol requires authorization credentials to be
|
|
10
|
+
* passed in a custom subprotocol header. This function takes the authorization
|
|
11
|
+
* object, stringifies it, and then encodes it into a URL-safe Base64 format
|
|
12
|
+
* by replacing `+` with `-`, `/` with `_`, and removing padding.
|
|
13
|
+
*
|
|
14
|
+
* @param authorization The authorization object to encode.
|
|
15
|
+
* @returns A Base64URL encoded string representing the authorization object.
|
|
16
|
+
*/
|
|
17
|
+
export declare function encodeAuthorization(authorization: object): string;
|
|
18
|
+
/**
|
|
19
|
+
* Generates the full WebSocket subprotocol string for authorization.
|
|
20
|
+
*
|
|
21
|
+
* This function takes an authorization object, encodes it using `encodeAuthorization`,
|
|
22
|
+
* and then prepends the `header-` prefix as required by the AWS AppSync Events
|
|
23
|
+
* WebSocket protocol for the subprotocol header.
|
|
24
|
+
*
|
|
25
|
+
* @param authorization The authorization object to use for generating the protocol string.
|
|
26
|
+
* @returns The complete authorization subprotocol string (e.g., `header-encodedAuthString`).
|
|
27
|
+
*/
|
|
28
|
+
export declare function getAuthProtocol(authorization: object): string;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module authenticator
|
|
3
|
+
* This module provides functions for handling the specific authentication
|
|
4
|
+
* handshake required by the AWS AppSync Events WebSocket protocol.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Encodes a JSON authorization object into a Base64URL string.
|
|
8
|
+
*
|
|
9
|
+
* The AWS AppSync WebSocket protocol requires authorization credentials to be
|
|
10
|
+
* passed in a custom subprotocol header. This function takes the authorization
|
|
11
|
+
* object, stringifies it, and then encodes it into a URL-safe Base64 format
|
|
12
|
+
* by replacing `+` with `-`, `/` with `_`, and removing padding.
|
|
13
|
+
*
|
|
14
|
+
* @param authorization The authorization object to encode.
|
|
15
|
+
* @returns A Base64URL encoded string representing the authorization object.
|
|
16
|
+
*/
|
|
17
|
+
export function encodeAuthorization(authorization) {
|
|
18
|
+
const jsonString = JSON.stringify(authorization);
|
|
19
|
+
// Note: Using the browser's btoa function for simplicity. In a Node.js
|
|
20
|
+
// environment, this would be replaced with Buffer.from(str).toString('base64').
|
|
21
|
+
// However, for the purpose of this library, we assume a browser-like environment
|
|
22
|
+
// or a polyfill for btoa.
|
|
23
|
+
const base64 = btoa(jsonString);
|
|
24
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Generates the full WebSocket subprotocol string for authorization.
|
|
28
|
+
*
|
|
29
|
+
* This function takes an authorization object, encodes it using `encodeAuthorization`,
|
|
30
|
+
* and then prepends the `header-` prefix as required by the AWS AppSync Events
|
|
31
|
+
* WebSocket protocol for the subprotocol header.
|
|
32
|
+
*
|
|
33
|
+
* @param authorization The authorization object to use for generating the protocol string.
|
|
34
|
+
* @returns The complete authorization subprotocol string (e.g., `header-encodedAuthString`).
|
|
35
|
+
*/
|
|
36
|
+
export function getAuthProtocol(authorization) {
|
|
37
|
+
const encodedHeader = encodeAuthorization(authorization);
|
|
38
|
+
return `header-${encodedHeader}`;
|
|
39
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { ChannelData } from "../../../../workloads/stateless/broadcast/channel-data.js";
|
|
3
|
+
import { BroadcastClient } from "./app-sync-client.js";
|
|
4
|
+
export type BroadcastContext = {
|
|
5
|
+
ws: BroadcastClient<any>;
|
|
6
|
+
connected: boolean;
|
|
7
|
+
} | null;
|
|
8
|
+
export declare const BroadcastProvider: import("react").Context<BroadcastContext>;
|
|
9
|
+
export interface BroadcastContextProviderProps {
|
|
10
|
+
urlAndHost: {
|
|
11
|
+
url: string;
|
|
12
|
+
host: string;
|
|
13
|
+
};
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}
|
|
16
|
+
export declare function BroadcastContextProvider({ urlAndHost, children, }: BroadcastContextProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
17
|
+
export declare function useWebSocket<T extends Record<string, ChannelData<any>>>(): BroadcastClient<T>;
|