@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,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module EventEmitter
|
|
4
|
+
* Implements a simple event emitter pattern for handling custom events.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.EventEmitter = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* A simple event emitter class that allows for subscribing to, unsubscribing from,
|
|
10
|
+
* and emitting custom events.
|
|
11
|
+
*/
|
|
12
|
+
class EventEmitter {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.listeners = new Map();
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Registers a new listener for a specified event.
|
|
18
|
+
* @param eventName The name of the event to listen for.
|
|
19
|
+
* @param listener The callback function to be invoked when the event is emitted.
|
|
20
|
+
*/
|
|
21
|
+
on(eventName, listener) {
|
|
22
|
+
if (!this.listeners.has(eventName)) {
|
|
23
|
+
this.listeners.set(eventName, new Set());
|
|
24
|
+
}
|
|
25
|
+
this.listeners.get(eventName)?.add(listener);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Removes a registered listener for a specified event.
|
|
29
|
+
* @param eventName The name of the event to remove the listener from.
|
|
30
|
+
* @param listener The callback function to be removed.
|
|
31
|
+
*/
|
|
32
|
+
off(eventName, listener) {
|
|
33
|
+
if (this.listeners.has(eventName)) {
|
|
34
|
+
this.listeners.get(eventName)?.delete(listener);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Emits an event, invoking all registered listeners for that event.
|
|
39
|
+
* @param eventName The name of the event to emit.
|
|
40
|
+
* @param args Any arguments to pass to the listeners.
|
|
41
|
+
*/
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
emit(eventName, ...args) {
|
|
44
|
+
if (this.listeners.has(eventName)) {
|
|
45
|
+
// Create a shallow copy to prevent issues if listeners modify the set during iteration
|
|
46
|
+
const listenersToCall = Array.from(this.listeners.get(eventName));
|
|
47
|
+
for (const listener of listenersToCall) {
|
|
48
|
+
listener(...args);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.EventEmitter = EventEmitter;
|
|
@@ -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,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module messageBuilder
|
|
4
|
+
* Provides utility functions for constructing WebSocket messages
|
|
5
|
+
* according to the AWS AppSync Events API protocol.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.buildSubscribeMessage = buildSubscribeMessage;
|
|
9
|
+
exports.buildPublishMessage = buildPublishMessage;
|
|
10
|
+
/**
|
|
11
|
+
* Builds a 'subscribe' message for the AWS AppSync Events API.
|
|
12
|
+
*
|
|
13
|
+
* @param id A unique identifier for the subscription.
|
|
14
|
+
* @param channel The channel path to subscribe to (e.g., '/my/channel').
|
|
15
|
+
* @param authorization An object containing the authorization details for the subscription.
|
|
16
|
+
* @returns A JSON object representing the 'subscribe' message.
|
|
17
|
+
*/
|
|
18
|
+
function buildSubscribeMessage(id, channel, authorization) {
|
|
19
|
+
return {
|
|
20
|
+
type: "subscribe",
|
|
21
|
+
id,
|
|
22
|
+
channel,
|
|
23
|
+
authorization,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Builds a 'publish' message for the AWS AppSync Events API.
|
|
28
|
+
*
|
|
29
|
+
* @param id A unique identifier for the publish operation.
|
|
30
|
+
* @param channel The channel path to publish to.
|
|
31
|
+
* @param events An array of stringified JSON events to be published.
|
|
32
|
+
* @param authorization An object containing the authorization details for the publish operation.
|
|
33
|
+
* @returns A JSON object representing the 'publish' message.
|
|
34
|
+
*/
|
|
35
|
+
function buildPublishMessage(id, channel, events, authorization) {
|
|
36
|
+
return {
|
|
37
|
+
type: "publish",
|
|
38
|
+
id,
|
|
39
|
+
channel,
|
|
40
|
+
events,
|
|
41
|
+
authorization,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -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,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module parser
|
|
4
|
+
* Provides utility functions for parsing incoming WebSocket messages
|
|
5
|
+
* from the AWS AppSync Events API.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.parseMessage = parseMessage;
|
|
9
|
+
/**
|
|
10
|
+
* Parses a raw WebSocket message string into a structured JavaScript object.
|
|
11
|
+
* @param message The raw message string received from the WebSocket.
|
|
12
|
+
* @returns The parsed message object.
|
|
13
|
+
* @throws {Error} If the message is not valid JSON.
|
|
14
|
+
*/
|
|
15
|
+
function parseMessage(message) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(message);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
throw new Error(`Failed to parse message: ${message}. Error: ${error}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Broadcast = Broadcast;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const broadcast_provider_js_1 = require("./broadcast-provider.js");
|
|
6
|
+
function Broadcast({ children }) {
|
|
7
|
+
return ((0, jsx_runtime_1.jsx)(broadcast_provider_js_1.BroadcastContextProvider, { urlAndHost: {
|
|
8
|
+
url: process.env.ML_BROADCAST_URL ?? "ws://localhost:9311",
|
|
9
|
+
host: process.env.ML_BROADCAST_HOST ?? "localhost",
|
|
10
|
+
}, children: children }));
|
|
11
|
+
}
|
|
@@ -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
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module SubscriptionManager
|
|
4
|
+
* Manages subscriptions to channels in the AWS AppSync Events API.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.SubscriptionManager = void 0;
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const messageBuilder_js_1 = require("./messageBuilder.js");
|
|
10
|
+
/**
|
|
11
|
+
* Represents a manager for handling subscriptions to various channels.
|
|
12
|
+
* It interacts with a connection manager to send subscription messages.
|
|
13
|
+
*/
|
|
14
|
+
class SubscriptionManager {
|
|
15
|
+
/**
|
|
16
|
+
* Creates an instance of SubscriptionManager.
|
|
17
|
+
* @param connectionManager The connection manager responsible for sending messages.
|
|
18
|
+
*/
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
constructor(connectionManager, eventEmitter) {
|
|
21
|
+
this.connectionManager = connectionManager;
|
|
22
|
+
this.subscribers = new Map();
|
|
23
|
+
this.subscriptionPromises = new Map();
|
|
24
|
+
// Register a handler for incoming messages from the ConnectionManager
|
|
25
|
+
eventEmitter.on("message", (message) => {
|
|
26
|
+
switch (message.type) {
|
|
27
|
+
case "data":
|
|
28
|
+
console.log("data", message);
|
|
29
|
+
if (this.subscribers.get(message.id)) {
|
|
30
|
+
// Event content is a stringified JSON, so parse it
|
|
31
|
+
this.subscribers.get(message.id)(JSON.parse(message.event));
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
case "subscribe_success":
|
|
35
|
+
if (this.subscriptionPromises.get(message.id)) {
|
|
36
|
+
this.subscriptionPromises.get(message.id).resolve();
|
|
37
|
+
this.subscriptionPromises.delete(message.id);
|
|
38
|
+
}
|
|
39
|
+
break;
|
|
40
|
+
case "subscribe_error":
|
|
41
|
+
if (this.subscriptionPromises.get(message.id)) {
|
|
42
|
+
this.subscriptionPromises
|
|
43
|
+
.get(message.id)
|
|
44
|
+
.reject(new Error(message.errors[0]?.message || "Subscription error"));
|
|
45
|
+
this.subscriptionPromises.delete(message.id);
|
|
46
|
+
}
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Subscribes to a specified channel.
|
|
53
|
+
* @param channel The channel path to subscribe to.
|
|
54
|
+
* @param authorization The authorization object for the subscription.
|
|
55
|
+
* @param callback The callback function to invoke when data is received for this subscription.
|
|
56
|
+
* @returns The ID of the created subscription.
|
|
57
|
+
*/
|
|
58
|
+
subscribe(channel, authorization, callback) {
|
|
59
|
+
// Changed return type to string
|
|
60
|
+
const id = (0, uuid_1.v4)();
|
|
61
|
+
const subscribeMessage = (0, messageBuilder_js_1.buildSubscribeMessage)(id, channel, authorization);
|
|
62
|
+
this.connectionManager.send(JSON.stringify(subscribeMessage));
|
|
63
|
+
this.subscribers.set(id, callback);
|
|
64
|
+
// Store the resolve/reject functions for this subscription ID
|
|
65
|
+
let resolvePromise;
|
|
66
|
+
let rejectPromise;
|
|
67
|
+
const promise = new Promise((resolve, reject) => {
|
|
68
|
+
resolvePromise = resolve;
|
|
69
|
+
rejectPromise = reject;
|
|
70
|
+
});
|
|
71
|
+
this.subscriptionPromises.set(id, {
|
|
72
|
+
resolve: resolvePromise,
|
|
73
|
+
reject: rejectPromise,
|
|
74
|
+
promise,
|
|
75
|
+
});
|
|
76
|
+
return id; // Return the id immediately
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Unsubscribes from a previously subscribed channel.
|
|
80
|
+
* @param subscriptionId The ID of the subscription to unsubscribe from.
|
|
81
|
+
*/
|
|
82
|
+
unsubscribe(subscriptionId) {
|
|
83
|
+
const unsubscribeMessage = {
|
|
84
|
+
type: "unsubscribe",
|
|
85
|
+
id: subscriptionId,
|
|
86
|
+
};
|
|
87
|
+
this.connectionManager.send(JSON.stringify(unsubscribeMessage));
|
|
88
|
+
this.subscribers.delete(subscriptionId);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
exports.SubscriptionManager = SubscriptionManager;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module types
|
|
3
|
+
* Defines TypeScript interfaces for the AWS AppSync Events WebSocket protocol messages.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Base interface for all AppSync WebSocket messages.
|
|
7
|
+
*/
|
|
8
|
+
export interface AppSyncMessage {
|
|
9
|
+
type: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Interface for the 'connection_ack' message.
|
|
13
|
+
*/
|
|
14
|
+
export interface ConnectionAckMessage extends AppSyncMessage {
|
|
15
|
+
type: "connection_ack";
|
|
16
|
+
connectionTimeoutMs: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Interface for the 'ka' (keep-alive) message.
|
|
20
|
+
*/
|
|
21
|
+
export interface KaMessage extends AppSyncMessage {
|
|
22
|
+
type: "ka";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Interface for 'subscribe_success' message.
|
|
26
|
+
*/
|
|
27
|
+
export interface SubscribeSuccessMessage extends AppSyncMessage {
|
|
28
|
+
type: "subscribe_success";
|
|
29
|
+
id: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Interface for 'subscribe_error' message.
|
|
33
|
+
*/
|
|
34
|
+
export interface SubscribeErrorMessage extends AppSyncMessage {
|
|
35
|
+
type: "subscribe_error";
|
|
36
|
+
id: string;
|
|
37
|
+
errors: any[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Interface for 'data' message.
|
|
41
|
+
*/
|
|
42
|
+
export interface DataMessage extends AppSyncMessage {
|
|
43
|
+
type: "data";
|
|
44
|
+
id: string;
|
|
45
|
+
event: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Interface for 'broadcast_error' message.
|
|
49
|
+
*/
|
|
50
|
+
export interface BroadcastErrorMessage extends AppSyncMessage {
|
|
51
|
+
type: "broadcast_error";
|
|
52
|
+
id: string;
|
|
53
|
+
errors: any[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Interface for 'publish_success' message.
|
|
57
|
+
*/
|
|
58
|
+
export interface PublishSuccessMessage extends AppSyncMessage {
|
|
59
|
+
type: "publish_success";
|
|
60
|
+
id: string;
|
|
61
|
+
successful: any[];
|
|
62
|
+
failed: any[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Interface for 'publish_error' message.
|
|
66
|
+
*/
|
|
67
|
+
export interface PublishErrorMessage extends AppSyncMessage {
|
|
68
|
+
type: "publish_error";
|
|
69
|
+
id: string;
|
|
70
|
+
errors: any[];
|
|
71
|
+
}
|
|
72
|
+
export type AppSyncWebSocketMessage = ConnectionAckMessage | KaMessage | SubscribeSuccessMessage | SubscribeErrorMessage | DataMessage | BroadcastErrorMessage | PublishSuccessMessage | PublishErrorMessage;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type RouteParams = {
|
|
2
|
+
[key: string]: string;
|
|
3
|
+
};
|
|
4
|
+
export declare class RouteMatcher {
|
|
5
|
+
private routes;
|
|
6
|
+
constructor(routes: string[]);
|
|
7
|
+
private buildRegex;
|
|
8
|
+
match(url: string): {
|
|
9
|
+
route: string;
|
|
10
|
+
params: RouteParams;
|
|
11
|
+
} | null;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RouteMatcher = void 0;
|
|
4
|
+
class RouteMatcher {
|
|
5
|
+
constructor(routes) {
|
|
6
|
+
this.routes = routes;
|
|
7
|
+
}
|
|
8
|
+
buildRegex(route) {
|
|
9
|
+
// Escape static segments and convert dynamic segments (e.g., [id]) into capture groups
|
|
10
|
+
const pattern = route
|
|
11
|
+
.split("/")
|
|
12
|
+
.map((segment) => {
|
|
13
|
+
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
14
|
+
// Capture dynamic segment as a parameter
|
|
15
|
+
return "([^/]+)";
|
|
16
|
+
}
|
|
17
|
+
// Escape static segments
|
|
18
|
+
return segment.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
|
19
|
+
})
|
|
20
|
+
.join("/");
|
|
21
|
+
return new RegExp(`^${pattern}$`);
|
|
22
|
+
}
|
|
23
|
+
match(url) {
|
|
24
|
+
for (const route of this.routes) {
|
|
25
|
+
const regex = this.buildRegex(route);
|
|
26
|
+
const match = url.match(regex);
|
|
27
|
+
if (match) {
|
|
28
|
+
// Extract dynamic parameters from the match
|
|
29
|
+
const params = {};
|
|
30
|
+
const segments = route
|
|
31
|
+
.split("/")
|
|
32
|
+
.filter((segment) => segment.startsWith("[") && segment.endsWith("]"));
|
|
33
|
+
const values = match.slice(1);
|
|
34
|
+
segments.forEach((segment, index) => {
|
|
35
|
+
const paramName = segment.slice(1, -1); // Remove the square brackets
|
|
36
|
+
if (values[index]) {
|
|
37
|
+
params[paramName] = values[index];
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return { route, params };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return null; // No match found
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.RouteMatcher = RouteMatcher;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Workload } from "../../../workloads/workload.js";
|
|
2
|
+
import type { RouteParams } from "./types.js";
|
|
3
|
+
export type IsValidSegment<S extends string> = S extends `${infer _Start}[${infer Param}]${infer After}` ? After extends `/${string}` | "" ? IsValidSegment<After> : false : true;
|
|
4
|
+
export type ValidateRouteFormat<T extends string> = IsValidSegment<T> extends true ? T : never;
|
|
5
|
+
export type ExtractRouteParamsTuple<T extends string, Acc extends string[] = []> = T extends `${string}[${infer Param}]${infer Rest}` ? ExtractRouteParamsTuple<Rest, [...Acc, Param]> : Acc;
|
|
6
|
+
export type HasDuplicates<T extends readonly any[]> = T extends [
|
|
7
|
+
infer F,
|
|
8
|
+
...infer R
|
|
9
|
+
] ? F extends R[number] ? true : HasDuplicates<R> : false;
|
|
10
|
+
export type ValidateUniqueParams<T extends string> = HasDuplicates<ExtractRouteParamsTuple<T>> extends true ? never : T;
|
|
11
|
+
export type ValidateRoute<T extends string> = ValidateRouteFormat<T> extends never ? never : ValidateUniqueParams<T> extends never ? never : T;
|
|
12
|
+
export type ValidateRoutes<T extends Record<string, any>> = {
|
|
13
|
+
[K in keyof T]: ValidateRoute<K & string> extends never ? never : T[K];
|
|
14
|
+
};
|
|
15
|
+
export type DrainOuterGeneric<T> = [T] extends [unknown] ? T : never;
|
|
16
|
+
export type Simplify<T> = DrainOuterGeneric<{
|
|
17
|
+
[K in keyof T]: T[K];
|
|
18
|
+
} & {}>;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a Broadcast workloads
|
|
21
|
+
*
|
|
22
|
+
* The constructore takes a session handler and a configuration object for channels. Each channel
|
|
23
|
+
* is mapped to a path and an optional authorization handler. The channel paths can
|
|
24
|
+
* include dynamic segments, like `/users/[id]`, which are parsed and passed to the
|
|
25
|
+
* authorization logic.
|
|
26
|
+
*
|
|
27
|
+
* @template T - A record mapping channel strings to their channel data types.
|
|
28
|
+
* @template S - The type of the session object, returned by the `session` function.
|
|
29
|
+
*
|
|
30
|
+
* @param session A function that resolves to a session object. This object is available
|
|
31
|
+
* within the authorization handlers. It receives an object with `cookies` as an argument.
|
|
32
|
+
* @param channels An object where keys are route strings and values are objects defining
|
|
33
|
+
* the channel and its authorization logic.
|
|
34
|
+
* - `auth`: An optional function to authorize "PUBLISH" or "SUBSCRIBE" operations.
|
|
35
|
+
* It receives the operation type, route parameters, and the session object.
|
|
36
|
+
* - `channel`: The channel instance for the route.
|
|
37
|
+
*
|
|
38
|
+
* @returns An object containing the configured channels, an internal authorization function (`authFn`),
|
|
39
|
+
* and the session handler.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { channel, Broadcast } from "@monolayer/sdk";
|
|
44
|
+
*
|
|
45
|
+
* const broadcast = new Broadcast(
|
|
46
|
+
* async () => ({ userId: 1 }),
|
|
47
|
+
* {
|
|
48
|
+
* "/todos": {
|
|
49
|
+
* channel: channel<{ message: string }>(),
|
|
50
|
+
* auth: async (ctx) => {
|
|
51
|
+
* console.log("User trying to access todos:", ctx.session.userId);
|
|
52
|
+
* return true; // Allow access
|
|
53
|
+
* },
|
|
54
|
+
* },
|
|
55
|
+
* "/users/[id]": {
|
|
56
|
+
* channel: channel<{ salute: string }>(),
|
|
57
|
+
* auth: async (ctx) => {
|
|
58
|
+
* // Only allow users to access their own channel
|
|
59
|
+
* return ctx.session.userId === Number(ctx.params.id);
|
|
60
|
+
* },
|
|
61
|
+
* },
|
|
62
|
+
* }
|
|
63
|
+
* );
|
|
64
|
+
*
|
|
65
|
+
* export default broadcast;
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare class Broadcast<T extends Record<string, any>, S> extends Workload {
|
|
69
|
+
session: (opts: {
|
|
70
|
+
cookies: Record<string, string>;
|
|
71
|
+
}) => Promise<S>;
|
|
72
|
+
channels: {
|
|
73
|
+
[channel in keyof T]: ValidateRoute<channel & string> extends never ? never : {
|
|
74
|
+
auth?: (ctx: {
|
|
75
|
+
operation: "PUBLISH" | "SUBSCRIBE";
|
|
76
|
+
params: channel extends string ? Simplify<RouteParams<channel>> : never;
|
|
77
|
+
session: S;
|
|
78
|
+
}) => Promise<boolean>;
|
|
79
|
+
data: T[channel];
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
_channelDataType: {
|
|
83
|
+
[K in keyof T]: T[K];
|
|
84
|
+
};
|
|
85
|
+
constructor(initOpts: {
|
|
86
|
+
session?: (opts: {
|
|
87
|
+
cookies: Record<string, string>;
|
|
88
|
+
}) => Promise<S>;
|
|
89
|
+
channels: {
|
|
90
|
+
[channel in keyof T]: ValidateRoute<channel & string> extends never ? never : {
|
|
91
|
+
auth?: (ctx: {
|
|
92
|
+
operation: "PUBLISH" | "SUBSCRIBE";
|
|
93
|
+
params: channel extends string ? Simplify<RouteParams<channel>> : never;
|
|
94
|
+
session: S;
|
|
95
|
+
}) => Promise<boolean>;
|
|
96
|
+
data: T[channel];
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
authFn(route: string, operation: "PUBLISH" | "SUBSCRIBE", session: S): Promise<boolean>;
|
|
101
|
+
get connectionStringEnvVar(): "ML_BROADCAST_URL";
|
|
102
|
+
}
|
|
103
|
+
export type AnyBroadcast = Broadcast<any, any>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Broadcast = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const workload_js_1 = require("../../../workloads/workload.js");
|
|
9
|
+
const matcher_js_1 = require("./matcher.js");
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Broadcast workloads
|
|
12
|
+
*
|
|
13
|
+
* The constructore takes a session handler and a configuration object for channels. Each channel
|
|
14
|
+
* is mapped to a path and an optional authorization handler. The channel paths can
|
|
15
|
+
* include dynamic segments, like `/users/[id]`, which are parsed and passed to the
|
|
16
|
+
* authorization logic.
|
|
17
|
+
*
|
|
18
|
+
* @template T - A record mapping channel strings to their channel data types.
|
|
19
|
+
* @template S - The type of the session object, returned by the `session` function.
|
|
20
|
+
*
|
|
21
|
+
* @param session A function that resolves to a session object. This object is available
|
|
22
|
+
* within the authorization handlers. It receives an object with `cookies` as an argument.
|
|
23
|
+
* @param channels An object where keys are route strings and values are objects defining
|
|
24
|
+
* the channel and its authorization logic.
|
|
25
|
+
* - `auth`: An optional function to authorize "PUBLISH" or "SUBSCRIBE" operations.
|
|
26
|
+
* It receives the operation type, route parameters, and the session object.
|
|
27
|
+
* - `channel`: The channel instance for the route.
|
|
28
|
+
*
|
|
29
|
+
* @returns An object containing the configured channels, an internal authorization function (`authFn`),
|
|
30
|
+
* and the session handler.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { channel, Broadcast } from "@monolayer/sdk";
|
|
35
|
+
*
|
|
36
|
+
* const broadcast = new Broadcast(
|
|
37
|
+
* async () => ({ userId: 1 }),
|
|
38
|
+
* {
|
|
39
|
+
* "/todos": {
|
|
40
|
+
* channel: channel<{ message: string }>(),
|
|
41
|
+
* auth: async (ctx) => {
|
|
42
|
+
* console.log("User trying to access todos:", ctx.session.userId);
|
|
43
|
+
* return true; // Allow access
|
|
44
|
+
* },
|
|
45
|
+
* },
|
|
46
|
+
* "/users/[id]": {
|
|
47
|
+
* channel: channel<{ salute: string }>(),
|
|
48
|
+
* auth: async (ctx) => {
|
|
49
|
+
* // Only allow users to access their own channel
|
|
50
|
+
* return ctx.session.userId === Number(ctx.params.id);
|
|
51
|
+
* },
|
|
52
|
+
* },
|
|
53
|
+
* }
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* export default broadcast;
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
class Broadcast extends workload_js_1.Workload {
|
|
61
|
+
constructor(initOpts) {
|
|
62
|
+
super("broadcast");
|
|
63
|
+
this.session =
|
|
64
|
+
initOpts.session ??
|
|
65
|
+
(async () => {
|
|
66
|
+
return {};
|
|
67
|
+
});
|
|
68
|
+
this.channels = initOpts.channels;
|
|
69
|
+
}
|
|
70
|
+
async authFn(route, operation, session) {
|
|
71
|
+
const auth = {};
|
|
72
|
+
for (const routeName of Object.keys(this.channels)) {
|
|
73
|
+
if (this.channels[routeName]) {
|
|
74
|
+
auth[`${path_1.default.join("default", routeName)}`] =
|
|
75
|
+
this.channels[routeName].auth;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const matcher = new matcher_js_1.RouteMatcher(Object.keys(this.channels).map((r) => path_1.default.join("default", r)));
|
|
79
|
+
const match = matcher.match(route);
|
|
80
|
+
if (match) {
|
|
81
|
+
const routeAuth = auth[route];
|
|
82
|
+
if (routeAuth === undefined)
|
|
83
|
+
return true;
|
|
84
|
+
return await routeAuth({
|
|
85
|
+
session: session,
|
|
86
|
+
params: match.params,
|
|
87
|
+
operation,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
get connectionStringEnvVar() {
|
|
95
|
+
return "ML_BROADCAST_URL";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.Broadcast = Broadcast;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Channel<T> = {
|
|
2
|
+
_data: T;
|
|
3
|
+
subscribed?: (ctx: any) => void;
|
|
4
|
+
unsubscribed?: (ctx: any) => void;
|
|
5
|
+
};
|
|
6
|
+
export type ChannelData<T extends Channel<any>> = T extends Channel<infer D> ? D : never;
|
|
7
|
+
export type Channels = Record<string, Channel<any>>;
|
|
8
|
+
export type ExtractRouteParamsTuple<T extends string, Acc extends string[] = []> = T extends `${string}[${infer Param}]${infer Rest}` ? ExtractRouteParamsTuple<Rest, [...Acc, Param]> : Acc;
|
|
9
|
+
export type HasDuplicates<T extends readonly any[]> = T extends [
|
|
10
|
+
infer F,
|
|
11
|
+
...infer R
|
|
12
|
+
] ? F extends R[number] ? true : HasDuplicates<R> : false;
|
|
13
|
+
export type ValidateUniqueParams<T extends string> = HasDuplicates<ExtractRouteParamsTuple<T>> extends true ? ["❌ Duplicate route params not allowed in", T] : T;
|
|
14
|
+
export type TupleToParamObject<T extends readonly string[]> = {
|
|
15
|
+
[K in T[number]]: string;
|
|
16
|
+
};
|
|
17
|
+
export type RouteParams<T extends string> = TupleToParamObject<ExtractRouteParamsTuple<T>>;
|