@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,29 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, use, useEffect, useMemo, useState, } from "react";
|
|
4
|
+
import { BroadcastClient } from "./app-sync-client.js";
|
|
5
|
+
export const BroadcastProvider = createContext(null);
|
|
6
|
+
export function BroadcastContextProvider({ urlAndHost, children, }) {
|
|
7
|
+
const [ws] = useState(useMemo(() => {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
return new BroadcastClient(urlAndHost);
|
|
10
|
+
}, [urlAndHost]));
|
|
11
|
+
const [connected, setConnected] = useState(false);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
async function connect() {
|
|
14
|
+
await ws.connect();
|
|
15
|
+
ws.connected = true;
|
|
16
|
+
setConnected(true);
|
|
17
|
+
}
|
|
18
|
+
connect().catch(console.error);
|
|
19
|
+
return () => ws.disconnect();
|
|
20
|
+
}, [ws]);
|
|
21
|
+
return (_jsx(BroadcastProvider.Provider, { value: { ws, connected }, children: children }));
|
|
22
|
+
}
|
|
23
|
+
export function useWebSocket() {
|
|
24
|
+
const context = use(BroadcastProvider);
|
|
25
|
+
if (!context) {
|
|
26
|
+
throw new Error("useWebSockets must be used within a <BroadcastProvider />");
|
|
27
|
+
}
|
|
28
|
+
return context.ws;
|
|
29
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChannelData } from "../../../../workloads/stateless/broadcast/channel-data.js";
|
|
2
|
+
import type { RouteParams } from "../../../../workloads/stateless/broadcast/types.js";
|
|
3
|
+
export declare function broadcastClient<C extends Record<string, ChannelData<any>>>(): {
|
|
4
|
+
useSubscription: <D extends keyof C & string>(channel: D, params: RouteParams<D>) => {
|
|
5
|
+
last: (D extends string ? C[D] extends ChannelData<infer P> ? P : never : never) | null;
|
|
6
|
+
all: (D extends string ? C[D] extends ChannelData<infer P> ? P : never : never)[];
|
|
7
|
+
};
|
|
8
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useReducer, useState } from "react";
|
|
3
|
+
import { useWebSocket } from "../../../../workloads/stateless/broadcast/client/broadcast-provider.js";
|
|
4
|
+
export function broadcastClient() {
|
|
5
|
+
const useSubscription = (channel, params) => {
|
|
6
|
+
const [last, setLast] = useState(null);
|
|
7
|
+
const [all, setAll] = useReducer((prevItems, newItem) => {
|
|
8
|
+
return prevItems.concat(newItem);
|
|
9
|
+
}, []);
|
|
10
|
+
const [subscribe, setSubscribed] = useState(false);
|
|
11
|
+
const client = useWebSocket();
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (client.connected && !subscribe) {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
client.subscribeTo(channel, params, (payload) => {
|
|
16
|
+
setLast(payload);
|
|
17
|
+
setAll(payload);
|
|
18
|
+
});
|
|
19
|
+
setSubscribed(true);
|
|
20
|
+
}
|
|
21
|
+
}, [client, channel, params, subscribe]);
|
|
22
|
+
return { last, all };
|
|
23
|
+
};
|
|
24
|
+
return { useSubscription };
|
|
25
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { AppSyncWebSocketMessage } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* @module ConnectionManager
|
|
4
|
+
* Manages the WebSocket connection lifecycle with the AWS AppSync Events API.
|
|
5
|
+
*/
|
|
6
|
+
type MessageHandler = (message: AppSyncWebSocketMessage) => void;
|
|
7
|
+
type CloseHandler = (event: CloseEvent) => void;
|
|
8
|
+
/**
|
|
9
|
+
* Manages the WebSocket connection to the AWS AppSync Events API.
|
|
10
|
+
* Handles connection establishment, sending initial messages, and receiving acknowledgments.
|
|
11
|
+
*/
|
|
12
|
+
export declare class ConnectionManager {
|
|
13
|
+
ws: WebSocket | null;
|
|
14
|
+
private readonly url;
|
|
15
|
+
private connectionTimeoutMs;
|
|
16
|
+
private keepAliveTimer;
|
|
17
|
+
private connectPromiseResolve;
|
|
18
|
+
private connectPromiseReject;
|
|
19
|
+
private messageHandler;
|
|
20
|
+
private closeHandler;
|
|
21
|
+
private reconnectAttempts;
|
|
22
|
+
private maxReconnectAttempts;
|
|
23
|
+
private reconnectDelay;
|
|
24
|
+
private maxReconnectDelay;
|
|
25
|
+
private _clientId?;
|
|
26
|
+
/**
|
|
27
|
+
* Creates an instance of ConnectionManager.
|
|
28
|
+
* @param url The WebSocket URL for the AWS AppSync Events API.
|
|
29
|
+
*/
|
|
30
|
+
constructor(url: string);
|
|
31
|
+
clientId(): string | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Establishes a WebSocket connection to the AWS AppSync Events API.
|
|
34
|
+
* @returns A Promise that resolves when the connection is successfully established and acknowledged.
|
|
35
|
+
*/
|
|
36
|
+
connect(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Registers a handler for incoming WebSocket messages.
|
|
39
|
+
* @param handler The function to call when a new message is received.
|
|
40
|
+
*/
|
|
41
|
+
onMessage(handler: MessageHandler): void;
|
|
42
|
+
/**
|
|
43
|
+
* Registers a handler for WebSocket close events.
|
|
44
|
+
* @param handler The function to call when the WebSocket connection is closed.
|
|
45
|
+
*/
|
|
46
|
+
onClose(handler: CloseHandler): void;
|
|
47
|
+
/**
|
|
48
|
+
* Sends a message over the WebSocket connection.
|
|
49
|
+
* @param message The message string to send.
|
|
50
|
+
*/
|
|
51
|
+
send(message: string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Closes the WebSocket connection gracefully.
|
|
54
|
+
* This method clears any active keep-alive timers and closes the underlying WebSocket.
|
|
55
|
+
*/
|
|
56
|
+
disconnect(): void;
|
|
57
|
+
private reconnect;
|
|
58
|
+
private startKeepAliveTimer;
|
|
59
|
+
private resetKeepAliveTimer;
|
|
60
|
+
private clearKeepAliveTimer;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
import { getAuthProtocol } from "./authenticator.js";
|
|
4
|
+
import { parseMessage } from "./parser.js";
|
|
5
|
+
/**
|
|
6
|
+
* Manages the WebSocket connection to the AWS AppSync Events API.
|
|
7
|
+
* Handles connection establishment, sending initial messages, and receiving acknowledgments.
|
|
8
|
+
*/
|
|
9
|
+
export class ConnectionManager {
|
|
10
|
+
ws = null;
|
|
11
|
+
url;
|
|
12
|
+
connectionTimeoutMs = 0;
|
|
13
|
+
keepAliveTimer = null;
|
|
14
|
+
connectPromiseResolve = null;
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
connectPromiseReject = null;
|
|
17
|
+
messageHandler = null;
|
|
18
|
+
closeHandler = null;
|
|
19
|
+
reconnectAttempts = 0;
|
|
20
|
+
maxReconnectAttempts = 10;
|
|
21
|
+
reconnectDelay = 1000; // 1 second
|
|
22
|
+
maxReconnectDelay = 30000; // 30 seconds
|
|
23
|
+
_clientId;
|
|
24
|
+
/**
|
|
25
|
+
* Creates an instance of ConnectionManager.
|
|
26
|
+
* @param url The WebSocket URL for the AWS AppSync Events API.
|
|
27
|
+
*/
|
|
28
|
+
constructor(url) {
|
|
29
|
+
this.url = url;
|
|
30
|
+
}
|
|
31
|
+
clientId() {
|
|
32
|
+
return this._clientId;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Establishes a WebSocket connection to the AWS AppSync Events API.
|
|
36
|
+
* @returns A Promise that resolves when the connection is successfully established and acknowledged.
|
|
37
|
+
*/
|
|
38
|
+
connect() {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
this.connectPromiseResolve = resolve;
|
|
41
|
+
this.connectPromiseReject = reject;
|
|
42
|
+
this._clientId = uuidv4();
|
|
43
|
+
this.ws = new WebSocket(this.url, [
|
|
44
|
+
"aws-appsync-event-ws",
|
|
45
|
+
getAuthProtocol({
|
|
46
|
+
clientId: this._clientId,
|
|
47
|
+
host: "",
|
|
48
|
+
Authorization: "",
|
|
49
|
+
}),
|
|
50
|
+
]);
|
|
51
|
+
this.ws.onopen = () => {
|
|
52
|
+
this.reconnectAttempts = 0;
|
|
53
|
+
this.ws?.send(JSON.stringify({ type: "connection_init" }));
|
|
54
|
+
};
|
|
55
|
+
this.ws.onmessage = (event) => {
|
|
56
|
+
const message = parseMessage(event.data);
|
|
57
|
+
switch (message.type) {
|
|
58
|
+
case "connection_ack":
|
|
59
|
+
this.connectionTimeoutMs = message.connectionTimeoutMs;
|
|
60
|
+
this.startKeepAliveTimer();
|
|
61
|
+
this.connectPromiseResolve?.();
|
|
62
|
+
break;
|
|
63
|
+
case "ka":
|
|
64
|
+
this.resetKeepAliveTimer();
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
// Pass other messages to the registered handler
|
|
68
|
+
if (this.messageHandler) {
|
|
69
|
+
this.messageHandler(message);
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
this.ws.onerror = (error) => {
|
|
75
|
+
this.clearKeepAliveTimer();
|
|
76
|
+
this.connectPromiseReject?.(error);
|
|
77
|
+
};
|
|
78
|
+
this.ws.onclose = (event) => {
|
|
79
|
+
this.clearKeepAliveTimer();
|
|
80
|
+
if (!event.wasClean) {
|
|
81
|
+
// Attempt to reconnect if the closure was not clean
|
|
82
|
+
this.reconnect();
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.connectPromiseReject?.(new Error(`WebSocket closed unexpectedly: ${event.code} - ${event.reason}`));
|
|
86
|
+
}
|
|
87
|
+
// Call the registered close handler
|
|
88
|
+
if (this.closeHandler) {
|
|
89
|
+
this.closeHandler(event);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Registers a handler for incoming WebSocket messages.
|
|
96
|
+
* @param handler The function to call when a new message is received.
|
|
97
|
+
*/
|
|
98
|
+
onMessage(handler) {
|
|
99
|
+
this.messageHandler = handler;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Registers a handler for WebSocket close events.
|
|
103
|
+
* @param handler The function to call when the WebSocket connection is closed.
|
|
104
|
+
*/
|
|
105
|
+
onClose(handler) {
|
|
106
|
+
this.closeHandler = handler;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Sends a message over the WebSocket connection.
|
|
110
|
+
* @param message The message string to send.
|
|
111
|
+
*/
|
|
112
|
+
send(message) {
|
|
113
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
114
|
+
this.ws.send(message);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
console.warn("WebSocket is not open. Message not sent.", message);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Closes the WebSocket connection gracefully.
|
|
122
|
+
* This method clears any active keep-alive timers and closes the underlying WebSocket.
|
|
123
|
+
*/
|
|
124
|
+
disconnect() {
|
|
125
|
+
this.clearKeepAliveTimer();
|
|
126
|
+
if (this.ws) {
|
|
127
|
+
this.ws.close();
|
|
128
|
+
this.ws = null;
|
|
129
|
+
this._clientId = "";
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
reconnect() {
|
|
133
|
+
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
134
|
+
this.reconnectAttempts++;
|
|
135
|
+
const delay = Math.min(this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1), this.maxReconnectDelay);
|
|
136
|
+
console.log(`Attempting to reconnect in ${delay}ms (attempt ${this.reconnectAttempts})...`);
|
|
137
|
+
setTimeout(() => {
|
|
138
|
+
// Re-call connect to attempt reconnection
|
|
139
|
+
this.connect();
|
|
140
|
+
}, delay);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
console.error("Max reconnect attempts reached. Connection permanently lost.");
|
|
144
|
+
this.connectPromiseReject?.(new Error("Max reconnect attempts reached"));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
startKeepAliveTimer() {
|
|
148
|
+
this.clearKeepAliveTimer();
|
|
149
|
+
this.keepAliveTimer = setTimeout(() => {
|
|
150
|
+
// If timer expires, no keep-alive message received
|
|
151
|
+
this.ws?.close();
|
|
152
|
+
this.connectPromiseReject?.(new Error("WebSocket closed due to keep-alive timeout"));
|
|
153
|
+
}, this.connectionTimeoutMs);
|
|
154
|
+
}
|
|
155
|
+
resetKeepAliveTimer() {
|
|
156
|
+
this.startKeepAliveTimer(); // Simply restart the timer
|
|
157
|
+
}
|
|
158
|
+
clearKeepAliveTimer() {
|
|
159
|
+
if (this.keepAliveTimer) {
|
|
160
|
+
clearTimeout(this.keepAliveTimer);
|
|
161
|
+
this.keepAliveTimer = null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module EventEmitter
|
|
3
|
+
* Implements a simple event emitter pattern for handling custom events.
|
|
4
|
+
*/
|
|
5
|
+
type Listener = (...args: any[]) => void;
|
|
6
|
+
/**
|
|
7
|
+
* A simple event emitter class that allows for subscribing to, unsubscribing from,
|
|
8
|
+
* and emitting custom events.
|
|
9
|
+
*/
|
|
10
|
+
export declare class EventEmitter {
|
|
11
|
+
private listeners;
|
|
12
|
+
constructor();
|
|
13
|
+
/**
|
|
14
|
+
* Registers a new listener for a specified event.
|
|
15
|
+
* @param eventName The name of the event to listen for.
|
|
16
|
+
* @param listener The callback function to be invoked when the event is emitted.
|
|
17
|
+
*/
|
|
18
|
+
on(eventName: string, listener: Listener): void;
|
|
19
|
+
/**
|
|
20
|
+
* Removes a registered listener for a specified event.
|
|
21
|
+
* @param eventName The name of the event to remove the listener from.
|
|
22
|
+
* @param listener The callback function to be removed.
|
|
23
|
+
*/
|
|
24
|
+
off(eventName: string, listener: Listener): void;
|
|
25
|
+
/**
|
|
26
|
+
* Emits an event, invoking all registered listeners for that event.
|
|
27
|
+
* @param eventName The name of the event to emit.
|
|
28
|
+
* @param args Any arguments to pass to the listeners.
|
|
29
|
+
*/
|
|
30
|
+
emit(eventName: string, ...args: any[]): void;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module EventEmitter
|
|
3
|
+
* Implements a simple event emitter pattern for handling custom events.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* A simple event emitter class that allows for subscribing to, unsubscribing from,
|
|
7
|
+
* and emitting custom events.
|
|
8
|
+
*/
|
|
9
|
+
export class EventEmitter {
|
|
10
|
+
listeners;
|
|
11
|
+
constructor() {
|
|
12
|
+
this.listeners = new Map();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Registers a new listener for a specified event.
|
|
16
|
+
* @param eventName The name of the event to listen for.
|
|
17
|
+
* @param listener The callback function to be invoked when the event is emitted.
|
|
18
|
+
*/
|
|
19
|
+
on(eventName, listener) {
|
|
20
|
+
if (!this.listeners.has(eventName)) {
|
|
21
|
+
this.listeners.set(eventName, new Set());
|
|
22
|
+
}
|
|
23
|
+
this.listeners.get(eventName)?.add(listener);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Removes a registered listener for a specified event.
|
|
27
|
+
* @param eventName The name of the event to remove the listener from.
|
|
28
|
+
* @param listener The callback function to be removed.
|
|
29
|
+
*/
|
|
30
|
+
off(eventName, listener) {
|
|
31
|
+
if (this.listeners.has(eventName)) {
|
|
32
|
+
this.listeners.get(eventName)?.delete(listener);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Emits an event, invoking all registered listeners for that event.
|
|
37
|
+
* @param eventName The name of the event to emit.
|
|
38
|
+
* @param args Any arguments to pass to the listeners.
|
|
39
|
+
*/
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
emit(eventName, ...args) {
|
|
42
|
+
if (this.listeners.has(eventName)) {
|
|
43
|
+
// Create a shallow copy to prevent issues if listeners modify the set during iteration
|
|
44
|
+
const listenersToCall = Array.from(this.listeners.get(eventName));
|
|
45
|
+
for (const listener of listenersToCall) {
|
|
46
|
+
listener(...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module messageBuilder
|
|
3
|
+
* Provides utility functions for constructing WebSocket messages
|
|
4
|
+
* according to the AWS AppSync Events API protocol.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Builds a 'subscribe' message for the AWS AppSync Events API.
|
|
8
|
+
*
|
|
9
|
+
* @param id A unique identifier for the subscription.
|
|
10
|
+
* @param channel The channel path to subscribe to (e.g., '/my/channel').
|
|
11
|
+
* @param authorization An object containing the authorization details for the subscription.
|
|
12
|
+
* @returns A JSON object representing the 'subscribe' message.
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildSubscribeMessage(id: string, channel: string, authorization: object): object;
|
|
15
|
+
/**
|
|
16
|
+
* Builds a 'publish' message for the AWS AppSync Events API.
|
|
17
|
+
*
|
|
18
|
+
* @param id A unique identifier for the publish operation.
|
|
19
|
+
* @param channel The channel path to publish to.
|
|
20
|
+
* @param events An array of stringified JSON events to be published.
|
|
21
|
+
* @param authorization An object containing the authorization details for the publish operation.
|
|
22
|
+
* @returns A JSON object representing the 'publish' message.
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildPublishMessage(id: string, channel: string, events: string[], authorization: object): object;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module messageBuilder
|
|
3
|
+
* Provides utility functions for constructing WebSocket messages
|
|
4
|
+
* according to the AWS AppSync Events API protocol.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Builds a 'subscribe' message for the AWS AppSync Events API.
|
|
8
|
+
*
|
|
9
|
+
* @param id A unique identifier for the subscription.
|
|
10
|
+
* @param channel The channel path to subscribe to (e.g., '/my/channel').
|
|
11
|
+
* @param authorization An object containing the authorization details for the subscription.
|
|
12
|
+
* @returns A JSON object representing the 'subscribe' message.
|
|
13
|
+
*/
|
|
14
|
+
export function buildSubscribeMessage(id, channel, authorization) {
|
|
15
|
+
return {
|
|
16
|
+
type: "subscribe",
|
|
17
|
+
id,
|
|
18
|
+
channel,
|
|
19
|
+
authorization,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Builds a 'publish' message for the AWS AppSync Events API.
|
|
24
|
+
*
|
|
25
|
+
* @param id A unique identifier for the publish operation.
|
|
26
|
+
* @param channel The channel path to publish to.
|
|
27
|
+
* @param events An array of stringified JSON events to be published.
|
|
28
|
+
* @param authorization An object containing the authorization details for the publish operation.
|
|
29
|
+
* @returns A JSON object representing the 'publish' message.
|
|
30
|
+
*/
|
|
31
|
+
export function buildPublishMessage(id, channel, events, authorization) {
|
|
32
|
+
return {
|
|
33
|
+
type: "publish",
|
|
34
|
+
id,
|
|
35
|
+
channel,
|
|
36
|
+
events,
|
|
37
|
+
authorization,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser
|
|
3
|
+
* Provides utility functions for parsing incoming WebSocket messages
|
|
4
|
+
* from the AWS AppSync Events API.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Parses a raw WebSocket message string into a structured JavaScript object.
|
|
8
|
+
* @param message The raw message string received from the WebSocket.
|
|
9
|
+
* @returns The parsed message object.
|
|
10
|
+
* @throws {Error} If the message is not valid JSON.
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseMessage(message: string): object;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module parser
|
|
3
|
+
* Provides utility functions for parsing incoming WebSocket messages
|
|
4
|
+
* from the AWS AppSync Events API.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Parses a raw WebSocket message string into a structured JavaScript object.
|
|
8
|
+
* @param message The raw message string received from the WebSocket.
|
|
9
|
+
* @returns The parsed message object.
|
|
10
|
+
* @throws {Error} If the message is not valid JSON.
|
|
11
|
+
*/
|
|
12
|
+
export function parseMessage(message) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(message);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
throw new Error(`Failed to parse message: ${message}. Error: ${error}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { BroadcastContextProvider } from "./broadcast-provider.js";
|
|
3
|
+
export function Broadcast({ children }) {
|
|
4
|
+
return (_jsx(BroadcastContextProvider, { urlAndHost: {
|
|
5
|
+
url: process.env.ML_BROADCAST_URL ?? "ws://localhost:9311",
|
|
6
|
+
host: process.env.ML_BROADCAST_HOST ?? "localhost",
|
|
7
|
+
}, children: children }));
|
|
8
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module SubscriptionManager
|
|
3
|
+
* Manages subscriptions to channels in the AWS AppSync Events API.
|
|
4
|
+
*/
|
|
5
|
+
import type { EventEmitter } from "./eventEmitter.js";
|
|
6
|
+
type SubscriberCallback = (data: object) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Represents a manager for handling subscriptions to various channels.
|
|
9
|
+
* It interacts with a connection manager to send subscription messages.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SubscriptionManager {
|
|
12
|
+
private connectionManager;
|
|
13
|
+
private subscribers;
|
|
14
|
+
private subscriptionPromises;
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of SubscriptionManager.
|
|
17
|
+
* @param connectionManager The connection manager responsible for sending messages.
|
|
18
|
+
*/
|
|
19
|
+
constructor(connectionManager: any, eventEmitter: EventEmitter);
|
|
20
|
+
/**
|
|
21
|
+
* Subscribes to a specified channel.
|
|
22
|
+
* @param channel The channel path to subscribe to.
|
|
23
|
+
* @param authorization The authorization object for the subscription.
|
|
24
|
+
* @param callback The callback function to invoke when data is received for this subscription.
|
|
25
|
+
* @returns The ID of the created subscription.
|
|
26
|
+
*/
|
|
27
|
+
subscribe(channel: string, authorization: object, callback: SubscriberCallback): string;
|
|
28
|
+
/**
|
|
29
|
+
* Unsubscribes from a previously subscribed channel.
|
|
30
|
+
* @param subscriptionId The ID of the subscription to unsubscribe from.
|
|
31
|
+
*/
|
|
32
|
+
unsubscribe(subscriptionId: string): void;
|
|
33
|
+
}
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module SubscriptionManager
|
|
3
|
+
* Manages subscriptions to channels in the AWS AppSync Events API.
|
|
4
|
+
*/
|
|
5
|
+
import { v4 as uuidv4 } from "uuid";
|
|
6
|
+
import { buildSubscribeMessage } from "./messageBuilder.js";
|
|
7
|
+
/**
|
|
8
|
+
* Represents a manager for handling subscriptions to various channels.
|
|
9
|
+
* It interacts with a connection manager to send subscription messages.
|
|
10
|
+
*/
|
|
11
|
+
export class SubscriptionManager {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
connectionManager; // Will be typed more strictly later
|
|
14
|
+
subscribers;
|
|
15
|
+
subscriptionPromises;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of SubscriptionManager.
|
|
18
|
+
* @param connectionManager The connection manager responsible for sending messages.
|
|
19
|
+
*/
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
+
constructor(connectionManager, eventEmitter) {
|
|
22
|
+
this.connectionManager = connectionManager;
|
|
23
|
+
this.subscribers = new Map();
|
|
24
|
+
this.subscriptionPromises = new Map();
|
|
25
|
+
// Register a handler for incoming messages from the ConnectionManager
|
|
26
|
+
eventEmitter.on("message", (message) => {
|
|
27
|
+
switch (message.type) {
|
|
28
|
+
case "data":
|
|
29
|
+
console.log("data", message);
|
|
30
|
+
if (this.subscribers.get(message.id)) {
|
|
31
|
+
// Event content is a stringified JSON, so parse it
|
|
32
|
+
this.subscribers.get(message.id)(JSON.parse(message.event));
|
|
33
|
+
}
|
|
34
|
+
break;
|
|
35
|
+
case "subscribe_success":
|
|
36
|
+
if (this.subscriptionPromises.get(message.id)) {
|
|
37
|
+
this.subscriptionPromises.get(message.id).resolve();
|
|
38
|
+
this.subscriptionPromises.delete(message.id);
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
case "subscribe_error":
|
|
42
|
+
if (this.subscriptionPromises.get(message.id)) {
|
|
43
|
+
this.subscriptionPromises
|
|
44
|
+
.get(message.id)
|
|
45
|
+
.reject(new Error(message.errors[0]?.message || "Subscription error"));
|
|
46
|
+
this.subscriptionPromises.delete(message.id);
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Subscribes to a specified channel.
|
|
54
|
+
* @param channel The channel path to subscribe to.
|
|
55
|
+
* @param authorization The authorization object for the subscription.
|
|
56
|
+
* @param callback The callback function to invoke when data is received for this subscription.
|
|
57
|
+
* @returns The ID of the created subscription.
|
|
58
|
+
*/
|
|
59
|
+
subscribe(channel, authorization, callback) {
|
|
60
|
+
// Changed return type to string
|
|
61
|
+
const id = uuidv4();
|
|
62
|
+
const subscribeMessage = buildSubscribeMessage(id, channel, authorization);
|
|
63
|
+
this.connectionManager.send(JSON.stringify(subscribeMessage));
|
|
64
|
+
this.subscribers.set(id, callback);
|
|
65
|
+
// Store the resolve/reject functions for this subscription ID
|
|
66
|
+
let resolvePromise;
|
|
67
|
+
let rejectPromise;
|
|
68
|
+
const promise = new Promise((resolve, reject) => {
|
|
69
|
+
resolvePromise = resolve;
|
|
70
|
+
rejectPromise = reject;
|
|
71
|
+
});
|
|
72
|
+
this.subscriptionPromises.set(id, {
|
|
73
|
+
resolve: resolvePromise,
|
|
74
|
+
reject: rejectPromise,
|
|
75
|
+
promise,
|
|
76
|
+
});
|
|
77
|
+
return id; // Return the id immediately
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Unsubscribes from a previously subscribed channel.
|
|
81
|
+
* @param subscriptionId The ID of the subscription to unsubscribe from.
|
|
82
|
+
*/
|
|
83
|
+
unsubscribe(subscriptionId) {
|
|
84
|
+
const unsubscribeMessage = {
|
|
85
|
+
type: "unsubscribe",
|
|
86
|
+
id: subscriptionId,
|
|
87
|
+
};
|
|
88
|
+
this.connectionManager.send(JSON.stringify(unsubscribeMessage));
|
|
89
|
+
this.subscribers.delete(subscriptionId);
|
|
90
|
+
}
|
|
91
|
+
}
|