@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,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module AppSyncEventsClient
|
|
4
|
+
* The main entry point for the AWS AppSync Events WebSocket client library.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.BroadcastClient = void 0;
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const connectionManager_js_1 = require("./connectionManager.js");
|
|
10
|
+
const eventEmitter_js_1 = require("./eventEmitter.js");
|
|
11
|
+
const messageBuilder_js_1 = require("./messageBuilder.js");
|
|
12
|
+
const subscriptionManager_js_1 = require("./subscriptionManager.js");
|
|
13
|
+
/**
|
|
14
|
+
* A client for interacting with the AWS AppSync Events WebSocket API.
|
|
15
|
+
* This client handles connection management, authentication, and provides
|
|
16
|
+
* an interface for subscribing to and publishing events.
|
|
17
|
+
*/
|
|
18
|
+
class BroadcastClient {
|
|
19
|
+
/**
|
|
20
|
+
* Creates an instance of AppSyncEventsClient.
|
|
21
|
+
* @param url The WebSocket URL for the AWS AppSync Events API.
|
|
22
|
+
* @param authorization The authorization object required for the WebSocket connection.
|
|
23
|
+
*/
|
|
24
|
+
constructor(opts) {
|
|
25
|
+
this.url = opts.url;
|
|
26
|
+
this.host = opts.host;
|
|
27
|
+
this.connectionManager = new connectionManager_js_1.ConnectionManager(this.url);
|
|
28
|
+
this.eventEmitter = new eventEmitter_js_1.EventEmitter();
|
|
29
|
+
this.subscriptionManager = new subscriptionManager_js_1.SubscriptionManager(this.connectionManager, this.eventEmitter);
|
|
30
|
+
this.connected = false;
|
|
31
|
+
// Register a handler for incoming messages from the ConnectionManager
|
|
32
|
+
this.connectionManager.onMessage((message) => {
|
|
33
|
+
switch (message.type) {
|
|
34
|
+
case "subscribe_error":
|
|
35
|
+
case "publish_error":
|
|
36
|
+
case "broadcast_error":
|
|
37
|
+
this.eventEmitter.emit("error", message);
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
this.eventEmitter.emit("message", message);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
// Register a handler for connection close events from the ConnectionManager
|
|
45
|
+
this.connectionManager.onClose(() => {
|
|
46
|
+
this.connected = false;
|
|
47
|
+
this.eventEmitter.emit("disconnected");
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Establishes a connection to the AWS AppSync Events WebSocket API.
|
|
52
|
+
* @returns A Promise that resolves when the connection is successfully established.
|
|
53
|
+
*/
|
|
54
|
+
async connect() {
|
|
55
|
+
await this.connectionManager.connect();
|
|
56
|
+
this.connected = true;
|
|
57
|
+
this.eventEmitter.emit("connected");
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Disconnects from the AWS AppSync Events WebSocket API.
|
|
61
|
+
*/
|
|
62
|
+
disconnect() {
|
|
63
|
+
this.connected = false;
|
|
64
|
+
this.connectionManager.disconnect();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Subscribes to a specified channel to receive real-time events.
|
|
68
|
+
* @param channel The channel path to subscribe to.
|
|
69
|
+
* @param authorization The authorization object for the subscription.
|
|
70
|
+
* @returns The ID of the created subscription.
|
|
71
|
+
*/
|
|
72
|
+
subscribe(channel, authorization,
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
callback) {
|
|
75
|
+
return this.subscriptionManager.subscribe(channel, authorization, callback); // Add a dummy callback
|
|
76
|
+
}
|
|
77
|
+
subscribeTo(channelName, params, callback) {
|
|
78
|
+
const path = channelName.replace(/\[([^\]]+)\]/g, (match, key) => {
|
|
79
|
+
return params[key] || match; // If key is not found, keep the placeholder
|
|
80
|
+
});
|
|
81
|
+
// Construct the final URL
|
|
82
|
+
return this.subscriptionManager.subscribe(`default${path}`, {
|
|
83
|
+
clientId: this.connectionManager.clientId,
|
|
84
|
+
host: this.host,
|
|
85
|
+
Authorization: "--",
|
|
86
|
+
},
|
|
87
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
|
+
callback); // Add a dummy callback
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Unsubscribes from a previously subscribed channel.
|
|
93
|
+
* @param subscriptionId The ID of the subscription to unsubscribe from.
|
|
94
|
+
*/
|
|
95
|
+
unsubscribe(subscriptionId) {
|
|
96
|
+
this.subscriptionManager.unsubscribe(subscriptionId);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Publishes events to a specified channel.
|
|
100
|
+
* @param channel The channel path to publish to.
|
|
101
|
+
* @param events An array of stringified JSON events to be published.
|
|
102
|
+
* @param authorization The authorization object for the publish operation.
|
|
103
|
+
*/
|
|
104
|
+
publish(channel, events, authorization) {
|
|
105
|
+
const id = (0, uuid_1.v4)();
|
|
106
|
+
const publishMessage = (0, messageBuilder_js_1.buildPublishMessage)(id, channel, events, authorization);
|
|
107
|
+
this.connectionManager.send(JSON.stringify(publishMessage));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Registers a listener for a specific event emitted by the client.
|
|
111
|
+
* @param eventName The name of the event to listen for (e.g., 'message', 'connected', 'disconnected').
|
|
112
|
+
* @param listener The callback function to be invoked when the event is emitted.
|
|
113
|
+
*/
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
115
|
+
on(eventName, listener) {
|
|
116
|
+
this.eventEmitter.on(eventName, listener);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Removes a registered listener for a specific event.
|
|
120
|
+
* @param eventName The name of the event to remove the listener from.
|
|
121
|
+
* @param listener The callback function to be removed.
|
|
122
|
+
*/
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
|
+
off(eventName, listener) {
|
|
125
|
+
this.eventEmitter.off(eventName, listener);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.BroadcastClient = BroadcastClient;
|
|
@@ -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,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module AppSyncEventsClient
|
|
4
|
+
* The main entry point for the AWS AppSync Events WebSocket client library.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.BroadcastPublisher = void 0;
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const connectionManager_js_1 = require("./connectionManager.js");
|
|
10
|
+
const eventEmitter_js_1 = require("./eventEmitter.js");
|
|
11
|
+
const messageBuilder_js_1 = require("./messageBuilder.js");
|
|
12
|
+
/**
|
|
13
|
+
* Publisher.
|
|
14
|
+
* Handles connection management, authentication, and provides
|
|
15
|
+
* an interface for publishing events.
|
|
16
|
+
*/
|
|
17
|
+
class BroadcastPublisher {
|
|
18
|
+
constructor() {
|
|
19
|
+
const urlFromEnv = process.env.ML_BROADCAST_URL ?? "ws://localhost:9311";
|
|
20
|
+
this.connectionManager = new connectionManager_js_1.ConnectionManager(urlFromEnv);
|
|
21
|
+
this.eventEmitter = new eventEmitter_js_1.EventEmitter();
|
|
22
|
+
// Register a handler for incoming messages from the ConnectionManager
|
|
23
|
+
this.connectionManager.onMessage((message) => {
|
|
24
|
+
switch (message.type) {
|
|
25
|
+
case "subscribe_error":
|
|
26
|
+
case "publish_error":
|
|
27
|
+
case "broadcast_error":
|
|
28
|
+
this.eventEmitter.emit("error", message);
|
|
29
|
+
break;
|
|
30
|
+
default:
|
|
31
|
+
this.eventEmitter.emit("message", message);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
// Register a handler for connection close events from the ConnectionManager
|
|
36
|
+
this.connectionManager.onClose(() => {
|
|
37
|
+
this.eventEmitter.emit("disconnected");
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Establishes a connection to the AWS AppSync Events WebSocket API.
|
|
42
|
+
* @returns A Promise that resolves when the connection is successfully established.
|
|
43
|
+
*/
|
|
44
|
+
async connect() {
|
|
45
|
+
await this.connectionManager.connect();
|
|
46
|
+
this.eventEmitter.emit("connected");
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Disconnects from the AWS AppSync Events WebSocket API.
|
|
50
|
+
*/
|
|
51
|
+
disconnect() {
|
|
52
|
+
this.connectionManager.disconnect();
|
|
53
|
+
}
|
|
54
|
+
async publishTo(channelName, params, data) {
|
|
55
|
+
try {
|
|
56
|
+
const path = channelName.replace(/\[([^\]]+)\]/g, (match, key) => {
|
|
57
|
+
return params[key] || match; // If key is not found, keep the placeholder
|
|
58
|
+
});
|
|
59
|
+
await this.connect();
|
|
60
|
+
for (const payload of data) {
|
|
61
|
+
const events = JSON.stringify(payload);
|
|
62
|
+
const publishMessage = (0, messageBuilder_js_1.buildPublishMessage)((0, uuid_1.v4)(), `default${path}`, [events], {
|
|
63
|
+
clientId: this.connectionManager.clientId,
|
|
64
|
+
host: process.env.ML_BROADCAST_HOST ?? "localhost",
|
|
65
|
+
Authorization: "--",
|
|
66
|
+
});
|
|
67
|
+
this.connectionManager.send(JSON.stringify(publishMessage));
|
|
68
|
+
}
|
|
69
|
+
this.disconnect();
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
console.error(e);
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.BroadcastPublisher = BroadcastPublisher;
|
|
@@ -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,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module authenticator
|
|
4
|
+
* This module provides functions for handling the specific authentication
|
|
5
|
+
* handshake required by the AWS AppSync Events WebSocket protocol.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.encodeAuthorization = encodeAuthorization;
|
|
9
|
+
exports.getAuthProtocol = getAuthProtocol;
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a JSON authorization object into a Base64URL string.
|
|
12
|
+
*
|
|
13
|
+
* The AWS AppSync WebSocket protocol requires authorization credentials to be
|
|
14
|
+
* passed in a custom subprotocol header. This function takes the authorization
|
|
15
|
+
* object, stringifies it, and then encodes it into a URL-safe Base64 format
|
|
16
|
+
* by replacing `+` with `-`, `/` with `_`, and removing padding.
|
|
17
|
+
*
|
|
18
|
+
* @param authorization The authorization object to encode.
|
|
19
|
+
* @returns A Base64URL encoded string representing the authorization object.
|
|
20
|
+
*/
|
|
21
|
+
function encodeAuthorization(authorization) {
|
|
22
|
+
const jsonString = JSON.stringify(authorization);
|
|
23
|
+
// Note: Using the browser's btoa function for simplicity. In a Node.js
|
|
24
|
+
// environment, this would be replaced with Buffer.from(str).toString('base64').
|
|
25
|
+
// However, for the purpose of this library, we assume a browser-like environment
|
|
26
|
+
// or a polyfill for btoa.
|
|
27
|
+
const base64 = btoa(jsonString);
|
|
28
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generates the full WebSocket subprotocol string for authorization.
|
|
32
|
+
*
|
|
33
|
+
* This function takes an authorization object, encodes it using `encodeAuthorization`,
|
|
34
|
+
* and then prepends the `header-` prefix as required by the AWS AppSync Events
|
|
35
|
+
* WebSocket protocol for the subprotocol header.
|
|
36
|
+
*
|
|
37
|
+
* @param authorization The authorization object to use for generating the protocol string.
|
|
38
|
+
* @returns The complete authorization subprotocol string (e.g., `header-encodedAuthString`).
|
|
39
|
+
*/
|
|
40
|
+
function getAuthProtocol(authorization) {
|
|
41
|
+
const encodedHeader = encodeAuthorization(authorization);
|
|
42
|
+
return `header-${encodedHeader}`;
|
|
43
|
+
}
|
|
@@ -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>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.BroadcastProvider = void 0;
|
|
5
|
+
exports.BroadcastContextProvider = BroadcastContextProvider;
|
|
6
|
+
exports.useWebSocket = useWebSocket;
|
|
7
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
8
|
+
const react_1 = require("react");
|
|
9
|
+
const app_sync_client_js_1 = require("./app-sync-client.js");
|
|
10
|
+
exports.BroadcastProvider = (0, react_1.createContext)(null);
|
|
11
|
+
function BroadcastContextProvider({ urlAndHost, children, }) {
|
|
12
|
+
const [ws] = (0, react_1.useState)((0, react_1.useMemo)(() => {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
return new app_sync_client_js_1.BroadcastClient(urlAndHost);
|
|
15
|
+
}, [urlAndHost]));
|
|
16
|
+
const [connected, setConnected] = (0, react_1.useState)(false);
|
|
17
|
+
(0, react_1.useEffect)(() => {
|
|
18
|
+
async function connect() {
|
|
19
|
+
await ws.connect();
|
|
20
|
+
ws.connected = true;
|
|
21
|
+
setConnected(true);
|
|
22
|
+
}
|
|
23
|
+
connect().catch(console.error);
|
|
24
|
+
return () => ws.disconnect();
|
|
25
|
+
}, [ws]);
|
|
26
|
+
return ((0, jsx_runtime_1.jsx)(exports.BroadcastProvider.Provider, { value: { ws, connected }, children: children }));
|
|
27
|
+
}
|
|
28
|
+
function useWebSocket() {
|
|
29
|
+
const context = (0, react_1.use)(exports.BroadcastProvider);
|
|
30
|
+
if (!context) {
|
|
31
|
+
throw new Error("useWebSockets must be used within a <BroadcastProvider />");
|
|
32
|
+
}
|
|
33
|
+
return context.ws;
|
|
34
|
+
}
|
|
@@ -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,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.broadcastClient = broadcastClient;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const broadcast_provider_js_1 = require("../../../../workloads/stateless/broadcast/client/broadcast-provider.js");
|
|
7
|
+
function broadcastClient() {
|
|
8
|
+
const useSubscription = (channel, params) => {
|
|
9
|
+
const [last, setLast] = (0, react_1.useState)(null);
|
|
10
|
+
const [all, setAll] = (0, react_1.useReducer)((prevItems, newItem) => {
|
|
11
|
+
return prevItems.concat(newItem);
|
|
12
|
+
}, []);
|
|
13
|
+
const [subscribe, setSubscribed] = (0, react_1.useState)(false);
|
|
14
|
+
const client = (0, broadcast_provider_js_1.useWebSocket)();
|
|
15
|
+
(0, react_1.useEffect)(() => {
|
|
16
|
+
if (client.connected && !subscribe) {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
client.subscribeTo(channel, params, (payload) => {
|
|
19
|
+
setLast(payload);
|
|
20
|
+
setAll(payload);
|
|
21
|
+
});
|
|
22
|
+
setSubscribed(true);
|
|
23
|
+
}
|
|
24
|
+
}, [client, channel, params, subscribe]);
|
|
25
|
+
return { last, all };
|
|
26
|
+
};
|
|
27
|
+
return { useSubscription };
|
|
28
|
+
}
|
|
@@ -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,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference lib="dom" />
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ConnectionManager = void 0;
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const authenticator_js_1 = require("./authenticator.js");
|
|
7
|
+
const parser_js_1 = require("./parser.js");
|
|
8
|
+
/**
|
|
9
|
+
* Manages the WebSocket connection to the AWS AppSync Events API.
|
|
10
|
+
* Handles connection establishment, sending initial messages, and receiving acknowledgments.
|
|
11
|
+
*/
|
|
12
|
+
class ConnectionManager {
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of ConnectionManager.
|
|
15
|
+
* @param url The WebSocket URL for the AWS AppSync Events API.
|
|
16
|
+
*/
|
|
17
|
+
constructor(url) {
|
|
18
|
+
this.ws = null;
|
|
19
|
+
this.connectionTimeoutMs = 0;
|
|
20
|
+
this.keepAliveTimer = null;
|
|
21
|
+
this.connectPromiseResolve = null;
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
this.connectPromiseReject = null;
|
|
24
|
+
this.messageHandler = null;
|
|
25
|
+
this.closeHandler = null;
|
|
26
|
+
this.reconnectAttempts = 0;
|
|
27
|
+
this.maxReconnectAttempts = 10;
|
|
28
|
+
this.reconnectDelay = 1000; // 1 second
|
|
29
|
+
this.maxReconnectDelay = 30000; // 30 seconds
|
|
30
|
+
this.url = url;
|
|
31
|
+
}
|
|
32
|
+
clientId() {
|
|
33
|
+
return this._clientId;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Establishes a WebSocket connection to the AWS AppSync Events API.
|
|
37
|
+
* @returns A Promise that resolves when the connection is successfully established and acknowledged.
|
|
38
|
+
*/
|
|
39
|
+
connect() {
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
this.connectPromiseResolve = resolve;
|
|
42
|
+
this.connectPromiseReject = reject;
|
|
43
|
+
this._clientId = (0, uuid_1.v4)();
|
|
44
|
+
this.ws = new WebSocket(this.url, [
|
|
45
|
+
"aws-appsync-event-ws",
|
|
46
|
+
(0, authenticator_js_1.getAuthProtocol)({
|
|
47
|
+
clientId: this._clientId,
|
|
48
|
+
host: "",
|
|
49
|
+
Authorization: "",
|
|
50
|
+
}),
|
|
51
|
+
]);
|
|
52
|
+
this.ws.onopen = () => {
|
|
53
|
+
this.reconnectAttempts = 0;
|
|
54
|
+
this.ws?.send(JSON.stringify({ type: "connection_init" }));
|
|
55
|
+
};
|
|
56
|
+
this.ws.onmessage = (event) => {
|
|
57
|
+
const message = (0, parser_js_1.parseMessage)(event.data);
|
|
58
|
+
switch (message.type) {
|
|
59
|
+
case "connection_ack":
|
|
60
|
+
this.connectionTimeoutMs = message.connectionTimeoutMs;
|
|
61
|
+
this.startKeepAliveTimer();
|
|
62
|
+
this.connectPromiseResolve?.();
|
|
63
|
+
break;
|
|
64
|
+
case "ka":
|
|
65
|
+
this.resetKeepAliveTimer();
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
// Pass other messages to the registered handler
|
|
69
|
+
if (this.messageHandler) {
|
|
70
|
+
this.messageHandler(message);
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
this.ws.onerror = (error) => {
|
|
76
|
+
this.clearKeepAliveTimer();
|
|
77
|
+
this.connectPromiseReject?.(error);
|
|
78
|
+
};
|
|
79
|
+
this.ws.onclose = (event) => {
|
|
80
|
+
this.clearKeepAliveTimer();
|
|
81
|
+
if (!event.wasClean) {
|
|
82
|
+
// Attempt to reconnect if the closure was not clean
|
|
83
|
+
this.reconnect();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
this.connectPromiseReject?.(new Error(`WebSocket closed unexpectedly: ${event.code} - ${event.reason}`));
|
|
87
|
+
}
|
|
88
|
+
// Call the registered close handler
|
|
89
|
+
if (this.closeHandler) {
|
|
90
|
+
this.closeHandler(event);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Registers a handler for incoming WebSocket messages.
|
|
97
|
+
* @param handler The function to call when a new message is received.
|
|
98
|
+
*/
|
|
99
|
+
onMessage(handler) {
|
|
100
|
+
this.messageHandler = handler;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Registers a handler for WebSocket close events.
|
|
104
|
+
* @param handler The function to call when the WebSocket connection is closed.
|
|
105
|
+
*/
|
|
106
|
+
onClose(handler) {
|
|
107
|
+
this.closeHandler = handler;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Sends a message over the WebSocket connection.
|
|
111
|
+
* @param message The message string to send.
|
|
112
|
+
*/
|
|
113
|
+
send(message) {
|
|
114
|
+
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
115
|
+
this.ws.send(message);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
console.warn("WebSocket is not open. Message not sent.", message);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Closes the WebSocket connection gracefully.
|
|
123
|
+
* This method clears any active keep-alive timers and closes the underlying WebSocket.
|
|
124
|
+
*/
|
|
125
|
+
disconnect() {
|
|
126
|
+
this.clearKeepAliveTimer();
|
|
127
|
+
if (this.ws) {
|
|
128
|
+
this.ws.close();
|
|
129
|
+
this.ws = null;
|
|
130
|
+
this._clientId = "";
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
reconnect() {
|
|
134
|
+
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
135
|
+
this.reconnectAttempts++;
|
|
136
|
+
const delay = Math.min(this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1), this.maxReconnectDelay);
|
|
137
|
+
console.log(`Attempting to reconnect in ${delay}ms (attempt ${this.reconnectAttempts})...`);
|
|
138
|
+
setTimeout(() => {
|
|
139
|
+
// Re-call connect to attempt reconnection
|
|
140
|
+
this.connect();
|
|
141
|
+
}, delay);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
console.error("Max reconnect attempts reached. Connection permanently lost.");
|
|
145
|
+
this.connectPromiseReject?.(new Error("Max reconnect attempts reached"));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
startKeepAliveTimer() {
|
|
149
|
+
this.clearKeepAliveTimer();
|
|
150
|
+
this.keepAliveTimer = setTimeout(() => {
|
|
151
|
+
// If timer expires, no keep-alive message received
|
|
152
|
+
this.ws?.close();
|
|
153
|
+
this.connectPromiseReject?.(new Error("WebSocket closed due to keep-alive timeout"));
|
|
154
|
+
}, this.connectionTimeoutMs);
|
|
155
|
+
}
|
|
156
|
+
resetKeepAliveTimer() {
|
|
157
|
+
this.startKeepAliveTimer(); // Simply restart the timer
|
|
158
|
+
}
|
|
159
|
+
clearKeepAliveTimer() {
|
|
160
|
+
if (this.keepAliveTimer) {
|
|
161
|
+
clearTimeout(this.keepAliveTimer);
|
|
162
|
+
this.keepAliveTimer = null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
exports.ConnectionManager = ConnectionManager;
|
|
@@ -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 {};
|