@aurostack/stacks 0.1.0
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 +209 -0
- package/cli/src/extract.mjs +165 -0
- package/cli/src/fsx.mjs +197 -0
- package/cli/src/generate.mjs +235 -0
- package/cli/src/hooks.mjs +55 -0
- package/cli/src/manifest.mjs +163 -0
- package/cli/src/strip.mjs +208 -0
- package/cli/src/tokens.mjs +102 -0
- package/cli/stack.mjs +672 -0
- package/package.json +37 -0
- package/templates/nest-api/files/.bin/db.ts +123 -0
- package/templates/nest-api/files/.bin/dc.ts +74 -0
- package/templates/nest-api/files/.bin/helpers.ts +49 -0
- package/templates/nest-api/files/.bin/secrets.ts +68 -0
- package/templates/nest-api/files/.bin/start.ts +47 -0
- package/templates/nest-api/files/.dockerignore +7 -0
- package/templates/nest-api/files/.env.example +91 -0
- package/templates/nest-api/files/.env.test.example +75 -0
- package/templates/nest-api/files/.forgejo/workflows/build-and-push.yml +41 -0
- package/templates/nest-api/files/.forgejo/workflows/test-e2e.yml +83 -0
- package/templates/nest-api/files/.forgejo/workflows/test.yml +53 -0
- package/templates/nest-api/files/.npmrc.example +1 -0
- package/templates/nest-api/files/.nvmrc +1 -0
- package/templates/nest-api/files/.prettierrc +8 -0
- package/templates/nest-api/files/.swcrc +15 -0
- package/templates/nest-api/files/.watchmanconfig +3 -0
- package/templates/nest-api/files/.yarnrc.yml +1 -0
- package/templates/nest-api/files/Dockerfile +40 -0
- package/templates/nest-api/files/__mocks__/activation-twofa.js +7 -0
- package/templates/nest-api/files/__mocks__/activation.js +7 -0
- package/templates/nest-api/files/__mocks__/forgot-password.js +7 -0
- package/templates/nest-api/files/__mocks__/main-layout.js +7 -0
- package/templates/nest-api/files/__mocks__/password-changed.js +7 -0
- package/templates/nest-api/files/__mocks__/reset-password.js +7 -0
- package/templates/nest-api/files/__mocks__/set-password.js +7 -0
- package/templates/nest-api/files/__mocks__/welcome.js +7 -0
- package/templates/nest-api/files/_gitignore +62 -0
- package/templates/nest-api/files/compose.test.yml +33 -0
- package/templates/nest-api/files/compose.yml +39 -0
- package/templates/nest-api/files/emails/activation-twofa.tsx +58 -0
- package/templates/nest-api/files/emails/activation.tsx +36 -0
- package/templates/nest-api/files/emails/forgot-password.tsx +29 -0
- package/templates/nest-api/files/emails/main-layout.tsx +134 -0
- package/templates/nest-api/files/emails/password-changed.tsx +24 -0
- package/templates/nest-api/files/emails/reset-password.tsx +24 -0
- package/templates/nest-api/files/emails/set-password.tsx +25 -0
- package/templates/nest-api/files/emails/welcome.tsx +39 -0
- package/templates/nest-api/files/eslint.config.mjs +28 -0
- package/templates/nest-api/files/global.d.ts +5 -0
- package/templates/nest-api/files/nest-cli.json +8 -0
- package/templates/nest-api/files/package.json +150 -0
- package/templates/nest-api/files/prisma/schema/auth.prisma +75 -0
- package/templates/nest-api/files/prisma/schema/base.prisma +17 -0
- package/templates/nest-api/files/prisma/schema/enum.prisma +17 -0
- package/templates/nest-api/files/prisma/schema/notification.prisma +14 -0
- package/templates/nest-api/files/prisma.config.ts +13 -0
- package/templates/nest-api/files/public/.gitkeep +0 -0
- package/templates/nest-api/files/seeders/index.ts +27 -0
- package/templates/nest-api/files/seeders/util.ts +88 -0
- package/templates/nest-api/files/src/__tests__/app.controller.spec.ts +29 -0
- package/templates/nest-api/files/src/app.controller.ts +40 -0
- package/templates/nest-api/files/src/app.module.ts +268 -0
- package/templates/nest-api/files/src/app.resolver.ts +32 -0
- package/templates/nest-api/files/src/app.setup.ts +178 -0
- package/templates/nest-api/files/src/common/__tests__/auth.service.spec.ts +31 -0
- package/templates/nest-api/files/src/common/__tests__/cache.service.spec.ts +214 -0
- package/templates/nest-api/files/src/common/__tests__/config.service.spec.ts +56 -0
- package/templates/nest-api/files/src/common/__tests__/generator.service.spec.ts +114 -0
- package/templates/nest-api/files/src/common/__tests__/health.controller.spec.ts +72 -0
- package/templates/nest-api/files/src/common/__tests__/helpers.spec.ts +124 -0
- package/templates/nest-api/files/src/common/__tests__/mail.service.spec.ts +75 -0
- package/templates/nest-api/files/src/common/__tests__/prisma.indicator.spec.ts +39 -0
- package/templates/nest-api/files/src/common/__tests__/prisma.service.spec.ts +15 -0
- package/templates/nest-api/files/src/common/__tests__/records.spec.ts +87 -0
- package/templates/nest-api/files/src/common/__tests__/schemas.spec.ts +142 -0
- package/templates/nest-api/files/src/common/__tests__/throttler-storage.service.spec.ts +80 -0
- package/templates/nest-api/files/src/common/common.module.ts +72 -0
- package/templates/nest-api/files/src/common/constants/index.ts +2 -0
- package/templates/nest-api/files/src/common/constants/options.ts +52 -0
- package/templates/nest-api/files/src/common/constants/pino.ts +24 -0
- package/templates/nest-api/files/src/common/controllers/health.controller.ts +54 -0
- package/templates/nest-api/files/src/common/controllers/index.ts +2 -0
- package/templates/nest-api/files/src/common/controllers/metrics.controller.ts +21 -0
- package/templates/nest-api/files/src/common/decorators/index.ts +2 -0
- package/templates/nest-api/files/src/common/decorators/misc.decorator.ts +49 -0
- package/templates/nest-api/files/src/common/decorators/multi-part.decorator.ts +6 -0
- package/templates/nest-api/files/src/common/dto/filters.dto.ts +25 -0
- package/templates/nest-api/files/src/common/dto/index.ts +1 -0
- package/templates/nest-api/files/src/common/entity/index.ts +2 -0
- package/templates/nest-api/files/src/common/entity/ok.entity.ts +11 -0
- package/templates/nest-api/files/src/common/entity/pagination.entity.ts +24 -0
- package/templates/nest-api/files/src/common/exceptions/index.ts +1 -0
- package/templates/nest-api/files/src/common/exceptions/prisma.exception.ts +57 -0
- package/templates/nest-api/files/src/common/guards/feature-flag.guard.ts +51 -0
- package/templates/nest-api/files/src/common/guards/index.ts +1 -0
- package/templates/nest-api/files/src/common/interceptors/index.ts +1 -0
- package/templates/nest-api/files/src/common/interceptors/metrics.interceptor.ts +37 -0
- package/templates/nest-api/files/src/common/misc/helpers.ts +26 -0
- package/templates/nest-api/files/src/common/misc/index.ts +3 -0
- package/templates/nest-api/files/src/common/misc/origin-builder.ts +56 -0
- package/templates/nest-api/files/src/common/misc/prisma.indicator.ts +24 -0
- package/templates/nest-api/files/src/common/modules/index.ts +3 -0
- package/templates/nest-api/files/src/common/modules/logger.module.ts +104 -0
- package/templates/nest-api/files/src/common/modules/metrics.module.ts +28 -0
- package/templates/nest-api/files/src/common/modules/queue.module.ts +23 -0
- package/templates/nest-api/files/src/common/processors/index.ts +1 -0
- package/templates/nest-api/files/src/common/processors/mail/index.ts +47 -0
- package/templates/nest-api/files/src/common/processors/mail/process.ts +225 -0
- package/templates/nest-api/files/src/common/schemas/fields.ts +76 -0
- package/templates/nest-api/files/src/common/schemas/index.ts +1 -0
- package/templates/nest-api/files/src/common/services/auth-rate-limit.storage.ts +79 -0
- package/templates/nest-api/files/src/common/services/auth.service.ts +16 -0
- package/templates/nest-api/files/src/common/services/cache.service.ts +118 -0
- package/templates/nest-api/files/src/common/services/config.service.ts +388 -0
- package/templates/nest-api/files/src/common/services/feature-flag.service.ts +117 -0
- package/templates/nest-api/files/src/common/services/generator.service.ts +54 -0
- package/templates/nest-api/files/src/common/services/index.ts +10 -0
- package/templates/nest-api/files/src/common/services/logger.service.ts +14 -0
- package/templates/nest-api/files/src/common/services/mail.service.ts +44 -0
- package/templates/nest-api/files/src/common/services/prisma.service.ts +106 -0
- package/templates/nest-api/files/src/common/services/throttler-storage.service.ts +112 -0
- package/templates/nest-api/files/src/common/types/email.ts +55 -0
- package/templates/nest-api/files/src/common/types/index.ts +3 -0
- package/templates/nest-api/files/src/common/types/misc.ts +20 -0
- package/templates/nest-api/files/src/common/types/prisma.ts +18 -0
- package/templates/nest-api/files/src/common/utils/enum.ts +39 -0
- package/templates/nest-api/files/src/common/utils/helpers.ts +139 -0
- package/templates/nest-api/files/src/common/utils/index.ts +5 -0
- package/templates/nest-api/files/src/common/utils/ip.ts +22 -0
- package/templates/nest-api/files/src/common/utils/records.ts +66 -0
- package/templates/nest-api/files/src/common/utils/redis.keys.ts +31 -0
- package/templates/nest-api/files/src/lib/__tests__/access.spec.ts +33 -0
- package/templates/nest-api/files/src/lib/access.ts +114 -0
- package/templates/nest-api/files/src/lib/auth.ts +76 -0
- package/templates/nest-api/files/src/lib/rate-limit.ts +20 -0
- package/templates/nest-api/files/src/main.ts +28 -0
- package/templates/nest-api/files/src/media/__tests__/media.service.spec.ts +60 -0
- package/templates/nest-api/files/src/media/__tests__/s3.service.spec.ts +159 -0
- package/templates/nest-api/files/src/media/entity/file.entity.ts +7 -0
- package/templates/nest-api/files/src/media/entity/index.ts +1 -0
- package/templates/nest-api/files/src/media/media.module.ts +20 -0
- package/templates/nest-api/files/src/media/processors/index.ts +44 -0
- package/templates/nest-api/files/src/media/services/index.ts +2 -0
- package/templates/nest-api/files/src/media/services/media.service.ts +29 -0
- package/templates/nest-api/files/src/media/services/s3.service.ts +168 -0
- package/templates/nest-api/files/src/notifications/controllers/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/controllers/notifications.controller.ts +72 -0
- package/templates/nest-api/files/src/notifications/dto/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/dto/notification.dto.ts +11 -0
- package/templates/nest-api/files/src/notifications/entity/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/entity/notification.entity.ts +37 -0
- package/templates/nest-api/files/src/notifications/notifications.module.ts +11 -0
- package/templates/nest-api/files/src/notifications/services/index.ts +1 -0
- package/templates/nest-api/files/src/notifications/services/notifications.service.ts +98 -0
- package/templates/nest-api/files/src/realtime/__tests__/events.gateway.spec.ts +88 -0
- package/templates/nest-api/files/src/realtime/__tests__/rooms.spec.ts +29 -0
- package/templates/nest-api/files/src/realtime/__tests__/ws-permissions.guard.spec.ts +54 -0
- package/templates/nest-api/files/src/realtime/adapters/redis-io.adapter.ts +57 -0
- package/templates/nest-api/files/src/realtime/constants/index.ts +68 -0
- package/templates/nest-api/files/src/realtime/decorators/index.ts +1 -0
- package/templates/nest-api/files/src/realtime/decorators/ws-permissions.decorator.ts +14 -0
- package/templates/nest-api/files/src/realtime/gateways/events.gateway.ts +187 -0
- package/templates/nest-api/files/src/realtime/guards/index.ts +2 -0
- package/templates/nest-api/files/src/realtime/guards/ws-auth.guard.ts +20 -0
- package/templates/nest-api/files/src/realtime/guards/ws-permissions.guard.ts +35 -0
- package/templates/nest-api/files/src/realtime/misc/session-from-socket.ts +54 -0
- package/templates/nest-api/files/src/realtime/realtime.module.ts +26 -0
- package/templates/nest-api/files/src/realtime/services/presence.service.ts +56 -0
- package/templates/nest-api/files/src/realtime/services/realtime.service.ts +72 -0
- package/templates/nest-api/files/src/users/__tests__/current-user.controller.spec.ts +77 -0
- package/templates/nest-api/files/src/users/__tests__/current-user.service.spec.ts +159 -0
- package/templates/nest-api/files/src/users/__tests__/user.entity.spec.ts +175 -0
- package/templates/nest-api/files/src/users/__tests__/users.controller.spec.ts +89 -0
- package/templates/nest-api/files/src/users/__tests__/users.service.spec.ts +398 -0
- package/templates/nest-api/files/src/users/controllers/current-user.controller.ts +82 -0
- package/templates/nest-api/files/src/users/controllers/index.ts +2 -0
- package/templates/nest-api/files/src/users/controllers/users.controller.ts +40 -0
- package/templates/nest-api/files/src/users/decorators/index.ts +1 -0
- package/templates/nest-api/files/src/users/decorators/permissions.decorator.ts +24 -0
- package/templates/nest-api/files/src/users/dto/avatar.dto.ts +13 -0
- package/templates/nest-api/files/src/users/dto/filters.dto.ts +8 -0
- package/templates/nest-api/files/src/users/dto/index.ts +2 -0
- package/templates/nest-api/files/src/users/entity/index.ts +1 -0
- package/templates/nest-api/files/src/users/entity/user.entity.ts +53 -0
- package/templates/nest-api/files/src/users/services/current-user.service.ts +46 -0
- package/templates/nest-api/files/src/users/services/index.ts +2 -0
- package/templates/nest-api/files/src/users/services/users.service.ts +88 -0
- package/templates/nest-api/files/src/users/users.module.ts +10 -0
- package/templates/nest-api/files/test/app.spec.ts +102 -0
- package/templates/nest-api/files/test/auth.spec.ts +108 -0
- package/templates/nest-api/files/test/current-user.spec.ts +154 -0
- package/templates/nest-api/files/test/factory/app.ts +172 -0
- package/templates/nest-api/files/test/factory/constants.ts +22 -0
- package/templates/nest-api/files/test/factory/dto.ts +27 -0
- package/templates/nest-api/files/test/factory/entity.ts +70 -0
- package/templates/nest-api/files/test/factory/gen.ts +75 -0
- package/templates/nest-api/files/test/factory/util.ts +21 -0
- package/templates/nest-api/files/test/notifications.spec.ts +176 -0
- package/templates/nest-api/files/test/rate-limit.spec.ts +180 -0
- package/templates/nest-api/files/test/setup.ts +1 -0
- package/templates/nest-api/files/test/users.spec.ts +437 -0
- package/templates/nest-api/files/tsconfig.build.json +13 -0
- package/templates/nest-api/files/tsconfig.json +35 -0
- package/templates/nest-api/files/vitest.config.ts +53 -0
- package/templates/nest-api/template.json +452 -0
- package/templates/node-worker/files/.bin/db.ts +123 -0
- package/templates/node-worker/files/.bin/helpers.ts +49 -0
- package/templates/node-worker/files/.bin/secrets.ts +68 -0
- package/templates/node-worker/files/.dockerignore +5 -0
- package/templates/node-worker/files/.env.example +21 -0
- package/templates/node-worker/files/.forgejo/workflows/build-and-push.yml +41 -0
- package/templates/node-worker/files/.nvmrc +1 -0
- package/templates/node-worker/files/.prettierignore +4 -0
- package/templates/node-worker/files/.prettierrc +8 -0
- package/templates/node-worker/files/.yarnrc.yml +5 -0
- package/templates/node-worker/files/Dockerfile +63 -0
- package/templates/node-worker/files/Dockerfile.dev +59 -0
- package/templates/node-worker/files/_gitignore +48 -0
- package/templates/node-worker/files/ecosystem.config.cjs +10 -0
- package/templates/node-worker/files/eslint.config.js +37 -0
- package/templates/node-worker/files/package.json +67 -0
- package/templates/node-worker/files/prisma/_gitignore +1 -0
- package/templates/node-worker/files/prisma/schema/base.prisma +36 -0
- package/templates/node-worker/files/prisma.config.ts +13 -0
- package/templates/node-worker/files/src/config.ts +55 -0
- package/templates/node-worker/files/src/db.ts +118 -0
- package/templates/node-worker/files/src/index.ts +7 -0
- package/templates/node-worker/files/src/logger.ts +45 -0
- package/templates/node-worker/files/src/types.ts +23 -0
- package/templates/node-worker/files/src/utils/browser.ts +16 -0
- package/templates/node-worker/files/src/utils/date.ts +68 -0
- package/templates/node-worker/files/src/utils/helpers.ts +17 -0
- package/templates/node-worker/files/src/utils/number.ts +47 -0
- package/templates/node-worker/files/src/utils/worker.ts +29 -0
- package/templates/node-worker/files/src/workers/constants.ts +6 -0
- package/templates/node-worker/files/src/workers/example/index.ts +27 -0
- package/templates/node-worker/files/src/workers/index.ts +1 -0
- package/templates/node-worker/files/tsconfig.json +30 -0
- package/templates/node-worker/template.json +135 -0
- package/templates/py-worker/files/.dockerignore +176 -0
- package/templates/py-worker/files/.env.example +20 -0
- package/templates/py-worker/files/.forgejo/workflows/build-amd-push.yml +39 -0
- package/templates/py-worker/files/Dockerfile +10 -0
- package/templates/py-worker/files/README.md +3 -0
- package/templates/py-worker/files/_gitignore +176 -0
- package/templates/py-worker/files/config.py +89 -0
- package/templates/py-worker/files/models/decorators.py +42 -0
- package/templates/py-worker/files/models/enums.py +32 -0
- package/templates/py-worker/files/models/mixins.py +61 -0
- package/templates/py-worker/files/requirements.txt +18 -0
- package/templates/py-worker/files/utils/db.py +67 -0
- package/templates/py-worker/files/utils/logger.py +97 -0
- package/templates/py-worker/files/utils/misc.py +45 -0
- package/templates/py-worker/files/utils/worker.py +80 -0
- package/templates/py-worker/files/worker.py +41 -0
- package/templates/py-worker/files/workers/hello/process.py +22 -0
- package/templates/py-worker/template.json +87 -0
- package/templates/react-app/README.md +78 -0
- package/templates/react-app/derive.sh +128 -0
- package/templates/react-app/files/.bin/openapi.js +32 -0
- package/templates/react-app/files/.bin/secrets.js +47 -0
- package/templates/react-app/files/.dockerignore +16 -0
- package/templates/react-app/files/.editorconfig +12 -0
- package/templates/react-app/files/.env.example +34 -0
- package/templates/react-app/files/.forgejo/workflows/build-and-push.yml +55 -0
- package/templates/react-app/files/.forgejo/workflows/ci.yml +39 -0
- package/templates/react-app/files/.husky/pre-commit +1 -0
- package/templates/react-app/files/.nvmrc +1 -0
- package/templates/react-app/files/.prettierignore +6 -0
- package/templates/react-app/files/.prettierrc +9 -0
- package/templates/react-app/files/.yarnrc.yml +2 -0
- package/templates/react-app/files/Dockerfile +52 -0
- package/templates/react-app/files/_gitignore +33 -0
- package/templates/react-app/files/build.sh +53 -0
- package/templates/react-app/files/docker/nginx.conf +134 -0
- package/templates/react-app/files/eslint.config.mjs +75 -0
- package/templates/react-app/files/index.html +62 -0
- package/templates/react-app/files/openapi-ts.config.ts +22 -0
- package/templates/react-app/files/package.json +80 -0
- package/templates/react-app/files/src/app/nav.tsx +32 -0
- package/templates/react-app/files/src/app/providers.tsx +39 -0
- package/templates/react-app/files/src/app/router.tsx +142 -0
- package/templates/react-app/files/src/app/store.ts +20 -0
- package/templates/react-app/files/src/features/auth/components/auth-card.tsx +23 -0
- package/templates/react-app/files/src/features/auth/components/checkbox-field.tsx +32 -0
- package/templates/react-app/files/src/features/auth/components/google-button.tsx +41 -0
- package/templates/react-app/files/src/features/auth/components/or-divider.tsx +9 -0
- package/templates/react-app/files/src/features/auth/components/password-field.tsx +70 -0
- package/templates/react-app/files/src/features/auth/components/submit-button.tsx +16 -0
- package/templates/react-app/files/src/features/auth/lib/auth-actions.ts +68 -0
- package/templates/react-app/files/src/features/auth/lib/password-strength.ts +20 -0
- package/templates/react-app/files/src/features/auth/lib/redirect.ts +6 -0
- package/templates/react-app/files/src/features/auth/lib/schemas.ts +37 -0
- package/templates/react-app/files/src/features/seo/routes.ts +65 -0
- package/templates/react-app/files/src/lib/env.ts +28 -0
- package/templates/react-app/files/src/lib/paths.ts +58 -0
- package/templates/react-app/files/src/main.tsx +19 -0
- package/templates/react-app/files/src/routes/admin/home.tsx +24 -0
- package/templates/react-app/files/src/routes/auth/forgot-password.tsx +93 -0
- package/templates/react-app/files/src/routes/auth/login.tsx +87 -0
- package/templates/react-app/files/src/routes/auth/reset-password.tsx +83 -0
- package/templates/react-app/files/src/routes/auth/signup.tsx +117 -0
- package/templates/react-app/files/src/routes/auth/verified.tsx +31 -0
- package/templates/react-app/files/src/routes/auth/verify-email.tsx +97 -0
- package/templates/react-app/files/src/routes/home.tsx +17 -0
- package/templates/react-app/files/src/routes/legal/privacy.tsx +26 -0
- package/templates/react-app/files/src/routes/legal/terms.tsx +28 -0
- package/templates/react-app/files/src/routes/marketing/home.tsx +18 -0
- package/templates/react-app/files/src/routes/settings.tsx +10 -0
- package/templates/react-app/files/src/shared/api/base-api.ts +24 -0
- package/templates/react-app/files/src/shared/api/base-query.ts +29 -0
- package/templates/react-app/files/src/shared/api/config.ts +21 -0
- package/templates/react-app/files/src/shared/api/index.ts +15 -0
- package/templates/react-app/files/src/shared/api/realtime-events.ts +34 -0
- package/templates/react-app/files/src/shared/api/socket.ts +41 -0
- package/templates/react-app/files/src/shared/api/store.ts +39 -0
- package/templates/react-app/files/src/shared/api/upload-query.ts +115 -0
- package/templates/react-app/files/src/shared/auth/client.ts +27 -0
- package/templates/react-app/files/src/shared/auth/config.ts +28 -0
- package/templates/react-app/files/src/shared/auth/guards.tsx +75 -0
- package/templates/react-app/files/src/shared/auth/hooks.ts +13 -0
- package/templates/react-app/files/src/shared/auth/index.ts +20 -0
- package/templates/react-app/files/src/shared/auth/logout.ts +23 -0
- package/templates/react-app/files/src/shared/auth/provider.tsx +52 -0
- package/templates/react-app/files/src/shared/auth/redirect.ts +65 -0
- package/templates/react-app/files/src/shared/auth/slice.ts +54 -0
- package/templates/react-app/files/src/shared/hooks/index.ts +6 -0
- package/templates/react-app/files/src/shared/layouts/app-shell/app-shell.tsx +225 -0
- package/templates/react-app/files/src/shared/layouts/app-shell/user-menu.tsx +74 -0
- package/templates/react-app/files/src/shared/layouts/auth-layout.tsx +137 -0
- package/templates/react-app/files/src/shared/layouts/error-boundary.tsx +38 -0
- package/templates/react-app/files/src/shared/layouts/index.ts +23 -0
- package/templates/react-app/files/src/shared/layouts/marketing/marketing-layout.tsx +29 -0
- package/templates/react-app/files/src/shared/layouts/marketing/section-heading.tsx +38 -0
- package/templates/react-app/files/src/shared/layouts/marketing/section.tsx +30 -0
- package/templates/react-app/files/src/shared/layouts/states/app-shell-skeleton.tsx +47 -0
- package/templates/react-app/files/src/shared/layouts/states/empty.tsx +22 -0
- package/templates/react-app/files/src/shared/layouts/states/forbidden.tsx +14 -0
- package/templates/react-app/files/src/shared/layouts/states/loading.tsx +11 -0
- package/templates/react-app/files/src/shared/layouts/states/not-found.tsx +21 -0
- package/templates/react-app/files/src/shared/layouts/states/page-skeleton.tsx +39 -0
- package/templates/react-app/files/src/shared/layouts/states/route-fallback.tsx +26 -0
- package/templates/react-app/files/src/shared/layouts/states/state-screen.tsx +22 -0
- package/templates/react-app/files/src/shared/types/README.md +28 -0
- package/templates/react-app/files/src/shared/types/api/index.ts +9 -0
- package/templates/react-app/files/src/shared/types/api/zod.gen.ts +5 -0
- package/templates/react-app/files/src/shared/types/index.ts +4 -0
- package/templates/react-app/files/src/shared/ui/components/brand/logo.tsx +51 -0
- package/templates/react-app/files/src/shared/ui/components/brand/social-icons.tsx +37 -0
- package/templates/react-app/files/src/shared/ui/components/form/form.tsx +120 -0
- package/templates/react-app/files/src/shared/ui/components/ui/alert.tsx +25 -0
- package/templates/react-app/files/src/shared/ui/components/ui/button.tsx +43 -0
- package/templates/react-app/files/src/shared/ui/components/ui/calendar.tsx +78 -0
- package/templates/react-app/files/src/shared/ui/components/ui/card.tsx +44 -0
- package/templates/react-app/files/src/shared/ui/components/ui/checkbox.tsx +16 -0
- package/templates/react-app/files/src/shared/ui/components/ui/date-picker.tsx +76 -0
- package/templates/react-app/files/src/shared/ui/components/ui/input.tsx +22 -0
- package/templates/react-app/files/src/shared/ui/components/ui/label.tsx +19 -0
- package/templates/react-app/files/src/shared/ui/components/ui/modal.tsx +94 -0
- package/templates/react-app/files/src/shared/ui/components/ui/popover.tsx +31 -0
- package/templates/react-app/files/src/shared/ui/components/ui/progress.tsx +33 -0
- package/templates/react-app/files/src/shared/ui/components/ui/search-input.tsx +69 -0
- package/templates/react-app/files/src/shared/ui/components/ui/select.tsx +80 -0
- package/templates/react-app/files/src/shared/ui/components/ui/skeleton.tsx +12 -0
- package/templates/react-app/files/src/shared/ui/components/ui/sonner.tsx +23 -0
- package/templates/react-app/files/src/shared/ui/components/ui/spinner.tsx +8 -0
- package/templates/react-app/files/src/shared/ui/components/ui/textarea.tsx +22 -0
- package/templates/react-app/files/src/shared/ui/index.ts +45 -0
- package/templates/react-app/files/src/shared/ui/lib/utils.ts +7 -0
- package/templates/react-app/files/src/shared/ui/lib/zod-setup.ts +28 -0
- package/templates/react-app/files/src/shared/ui/meta/brand.ts +18 -0
- package/templates/react-app/files/src/shared/ui/meta/meta-context.ts +30 -0
- package/templates/react-app/files/src/shared/ui/meta/meta-provider.tsx +24 -0
- package/templates/react-app/files/src/shared/ui/meta/page-meta.tsx +69 -0
- package/templates/react-app/files/src/shared/ui/meta/use-meta-config.ts +11 -0
- package/templates/react-app/files/src/shared/ui/styles/globals.css +285 -0
- package/templates/react-app/files/src/shared/ui/styles/theme.css +48 -0
- package/templates/react-app/files/src/shared/ui/theme/init-script.ts +23 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-colors.ts +14 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-context.ts +60 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-provider.tsx +101 -0
- package/templates/react-app/files/src/shared/ui/theme/theme-toggle.tsx +40 -0
- package/templates/react-app/files/src/shared/ui/theme/use-theme.ts +11 -0
- package/templates/react-app/files/src/vite-env.d.ts +13 -0
- package/templates/react-app/files/tsconfig.json +26 -0
- package/templates/react-app/files/tsconfig.node.json +25 -0
- package/templates/react-app/files/vite.config.ts +344 -0
- package/templates/react-app/overrides/src/routes/auth/forgot-password.tsx +93 -0
- package/templates/react-app/overrides/src/routes/auth/login.tsx +87 -0
- package/templates/react-app/overrides/src/routes/auth/signup.tsx +117 -0
- package/templates/react-app/overrides/src/routes/auth/verified.tsx +31 -0
- package/templates/react-app/overrides/src/routes/auth/verify-email.tsx +97 -0
- package/templates/react-app/overrides/src/shared/auth/config.ts +28 -0
- package/templates/react-app/overrides/src/shared/auth/guards.tsx +75 -0
- package/templates/react-app/overrides/src/shared/auth/index.ts +20 -0
- package/templates/react-app/overrides/src/shared/auth/redirect.ts +65 -0
- package/templates/react-app/overrides/src/shared/hooks/index.ts +6 -0
- package/templates/react-app/overrides/src/shared/types/README.md +28 -0
- package/templates/react-app/template.json +222 -0
- package/templates/react-monorepo/files/.dockerignore +19 -0
- package/templates/react-monorepo/files/.editorconfig +12 -0
- package/templates/react-monorepo/files/.forgejo/workflows/build-and-push.yml +98 -0
- package/templates/react-monorepo/files/.forgejo/workflows/ci.yml +35 -0
- package/templates/react-monorepo/files/.husky/pre-commit +1 -0
- package/templates/react-monorepo/files/.nvmrc +1 -0
- package/templates/react-monorepo/files/.prettierignore +6 -0
- package/templates/react-monorepo/files/.prettierrc +9 -0
- package/templates/react-monorepo/files/.yarnrc.yml +2 -0
- package/templates/react-monorepo/files/Dockerfile +90 -0
- package/templates/react-monorepo/files/_gitignore +35 -0
- package/templates/react-monorepo/files/apps/admin/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/admin/.env.example +16 -0
- package/templates/react-monorepo/files/apps/admin/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/admin/index.html +57 -0
- package/templates/react-monorepo/files/apps/admin/package.json +33 -0
- package/templates/react-monorepo/files/apps/admin/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/admin/src/app/nav.tsx +14 -0
- package/templates/react-monorepo/files/apps/admin/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/admin/src/app/router.tsx +50 -0
- package/templates/react-monorepo/files/apps/admin/src/app/store.ts +15 -0
- package/templates/react-monorepo/files/apps/admin/src/lib/env.ts +12 -0
- package/templates/react-monorepo/files/apps/admin/src/main.tsx +14 -0
- package/templates/react-monorepo/files/apps/admin/src/routes/home.tsx +17 -0
- package/templates/react-monorepo/files/apps/admin/src/routes/settings.tsx +10 -0
- package/templates/react-monorepo/files/apps/admin/src/vite-env.d.ts +14 -0
- package/templates/react-monorepo/files/apps/admin/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/admin/vite.config.ts +13 -0
- package/templates/react-monorepo/files/apps/auth/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/auth/.env.example +16 -0
- package/templates/react-monorepo/files/apps/auth/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/auth/index.html +57 -0
- package/templates/react-monorepo/files/apps/auth/package.json +32 -0
- package/templates/react-monorepo/files/apps/auth/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/auth/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/auth/src/app/router.tsx +19 -0
- package/templates/react-monorepo/files/apps/auth/src/app/store.ts +15 -0
- package/templates/react-monorepo/files/apps/auth/src/components/auth-card.tsx +23 -0
- package/templates/react-monorepo/files/apps/auth/src/components/checkbox-field.tsx +32 -0
- package/templates/react-monorepo/files/apps/auth/src/components/google-button.tsx +41 -0
- package/templates/react-monorepo/files/apps/auth/src/components/or-divider.tsx +9 -0
- package/templates/react-monorepo/files/apps/auth/src/components/password-field.tsx +70 -0
- package/templates/react-monorepo/files/apps/auth/src/components/submit-button.tsx +16 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/auth-actions.ts +68 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/env.ts +8 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/password-strength.ts +20 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/redirect.ts +6 -0
- package/templates/react-monorepo/files/apps/auth/src/lib/schemas.ts +37 -0
- package/templates/react-monorepo/files/apps/auth/src/main.tsx +14 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/forgot-password.tsx +91 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/login.tsx +82 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/reset-password.tsx +83 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/signup.tsx +117 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/verified.tsx +30 -0
- package/templates/react-monorepo/files/apps/auth/src/routes/verify-email.tsx +96 -0
- package/templates/react-monorepo/files/apps/auth/src/vite-env.d.ts +14 -0
- package/templates/react-monorepo/files/apps/auth/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/auth/vite.config.ts +13 -0
- package/templates/react-monorepo/files/apps/client/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/client/.env.example +16 -0
- package/templates/react-monorepo/files/apps/client/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/client/index.html +57 -0
- package/templates/react-monorepo/files/apps/client/package.json +35 -0
- package/templates/react-monorepo/files/apps/client/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/client/src/app/nav.tsx +14 -0
- package/templates/react-monorepo/files/apps/client/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/client/src/app/router.tsx +50 -0
- package/templates/react-monorepo/files/apps/client/src/app/store.ts +15 -0
- package/templates/react-monorepo/files/apps/client/src/lib/env.ts +12 -0
- package/templates/react-monorepo/files/apps/client/src/main.tsx +19 -0
- package/templates/react-monorepo/files/apps/client/src/routes/home.tsx +17 -0
- package/templates/react-monorepo/files/apps/client/src/routes/settings.tsx +10 -0
- package/templates/react-monorepo/files/apps/client/src/vite-env.d.ts +15 -0
- package/templates/react-monorepo/files/apps/client/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/client/vite.config.ts +120 -0
- package/templates/react-monorepo/files/apps/landing/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/apps/landing/.env.example +20 -0
- package/templates/react-monorepo/files/apps/landing/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/apps/landing/index.html +51 -0
- package/templates/react-monorepo/files/apps/landing/package.json +33 -0
- package/templates/react-monorepo/files/apps/landing/src/App.tsx +3 -0
- package/templates/react-monorepo/files/apps/landing/src/app/providers.tsx +42 -0
- package/templates/react-monorepo/files/apps/landing/src/app/router.tsx +21 -0
- package/templates/react-monorepo/files/apps/landing/src/app/store.ts +18 -0
- package/templates/react-monorepo/files/apps/landing/src/features/seo/routes.ts +65 -0
- package/templates/react-monorepo/files/apps/landing/src/lib/env.ts +10 -0
- package/templates/react-monorepo/files/apps/landing/src/main.tsx +14 -0
- package/templates/react-monorepo/files/apps/landing/src/routes/home.tsx +18 -0
- package/templates/react-monorepo/files/apps/landing/src/vite-env.d.ts +15 -0
- package/templates/react-monorepo/files/apps/landing/tsconfig.json +10 -0
- package/templates/react-monorepo/files/apps/landing/vite.config.ts +169 -0
- package/templates/react-monorepo/files/build.sh +61 -0
- package/templates/react-monorepo/files/docker/nginx.conf +136 -0
- package/templates/react-monorepo/files/package.json +37 -0
- package/templates/react-monorepo/files/packages/.gitkeep +0 -0
- package/templates/react-monorepo/files/packages/api/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/api/package.json +30 -0
- package/templates/react-monorepo/files/packages/api/src/base-api.ts +24 -0
- package/templates/react-monorepo/files/packages/api/src/base-query.ts +29 -0
- package/templates/react-monorepo/files/packages/api/src/config.ts +21 -0
- package/templates/react-monorepo/files/packages/api/src/index.ts +15 -0
- package/templates/react-monorepo/files/packages/api/src/realtime-events.ts +34 -0
- package/templates/react-monorepo/files/packages/api/src/socket.ts +41 -0
- package/templates/react-monorepo/files/packages/api/src/store.ts +39 -0
- package/templates/react-monorepo/files/packages/api/src/upload-query.ts +115 -0
- package/templates/react-monorepo/files/packages/api/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/auth/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/auth/package.json +33 -0
- package/templates/react-monorepo/files/packages/auth/src/client.ts +27 -0
- package/templates/react-monorepo/files/packages/auth/src/config.ts +22 -0
- package/templates/react-monorepo/files/packages/auth/src/guards.tsx +61 -0
- package/templates/react-monorepo/files/packages/auth/src/hooks.ts +13 -0
- package/templates/react-monorepo/files/packages/auth/src/index.ts +20 -0
- package/templates/react-monorepo/files/packages/auth/src/logout.ts +23 -0
- package/templates/react-monorepo/files/packages/auth/src/provider.tsx +52 -0
- package/templates/react-monorepo/files/packages/auth/src/redirect.ts +37 -0
- package/templates/react-monorepo/files/packages/auth/src/slice.ts +54 -0
- package/templates/react-monorepo/files/packages/auth/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/config/README.md +47 -0
- package/templates/react-monorepo/files/packages/config/eslint/base.mjs +83 -0
- package/templates/react-monorepo/files/packages/config/package.json +37 -0
- package/templates/react-monorepo/files/packages/config/tailwind/theme.css +48 -0
- package/templates/react-monorepo/files/packages/config/tsconfig/base.json +22 -0
- package/templates/react-monorepo/files/packages/config/tsconfig/node.json +9 -0
- package/templates/react-monorepo/files/packages/config/tsconfig/react.json +9 -0
- package/templates/react-monorepo/files/packages/config/vite/index.d.ts +18 -0
- package/templates/react-monorepo/files/packages/config/vite/index.mjs +129 -0
- package/templates/react-monorepo/files/packages/hooks/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/hooks/package.json +16 -0
- package/templates/react-monorepo/files/packages/hooks/src/index.ts +3 -0
- package/templates/react-monorepo/files/packages/hooks/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/layouts/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/layouts/package.json +30 -0
- package/templates/react-monorepo/files/packages/layouts/src/app-shell/app-shell.tsx +225 -0
- package/templates/react-monorepo/files/packages/layouts/src/app-shell/user-menu.tsx +74 -0
- package/templates/react-monorepo/files/packages/layouts/src/auth-layout.tsx +137 -0
- package/templates/react-monorepo/files/packages/layouts/src/error-boundary.tsx +38 -0
- package/templates/react-monorepo/files/packages/layouts/src/index.ts +23 -0
- package/templates/react-monorepo/files/packages/layouts/src/marketing/marketing-layout.tsx +29 -0
- package/templates/react-monorepo/files/packages/layouts/src/marketing/section-heading.tsx +38 -0
- package/templates/react-monorepo/files/packages/layouts/src/marketing/section.tsx +30 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/app-shell-skeleton.tsx +47 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/empty.tsx +22 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/forbidden.tsx +14 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/loading.tsx +11 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/not-found.tsx +21 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/page-skeleton.tsx +39 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/route-fallback.tsx +26 -0
- package/templates/react-monorepo/files/packages/layouts/src/states/state-screen.tsx +22 -0
- package/templates/react-monorepo/files/packages/layouts/tsconfig.json +4 -0
- package/templates/react-monorepo/files/packages/types/.bin/openapi.js +32 -0
- package/templates/react-monorepo/files/packages/types/.bin/secrets.js +57 -0
- package/templates/react-monorepo/files/packages/types/README.md +28 -0
- package/templates/react-monorepo/files/packages/types/eslint.config.mjs +3 -0
- package/templates/react-monorepo/files/packages/types/openapi-ts.config.ts +22 -0
- package/templates/react-monorepo/files/packages/types/package.json +29 -0
- package/templates/react-monorepo/files/packages/types/src/index.ts +4 -0
- package/templates/react-monorepo/files/packages/types/src/types/api/index.ts +9 -0
- package/templates/react-monorepo/files/packages/types/src/types/api/zod.gen.ts +5 -0
- package/templates/react-monorepo/files/packages/types/tsconfig.json +7 -0
- package/templates/react-monorepo/files/packages/ui/components.json +21 -0
- package/templates/react-monorepo/files/packages/ui/eslint.config.mjs +13 -0
- package/templates/react-monorepo/files/packages/ui/package.json +44 -0
- package/templates/react-monorepo/files/packages/ui/src/components/brand/logo.tsx +51 -0
- package/templates/react-monorepo/files/packages/ui/src/components/brand/social-icons.tsx +37 -0
- package/templates/react-monorepo/files/packages/ui/src/components/form/form.tsx +120 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/alert.tsx +25 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/button.tsx +43 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/calendar.tsx +78 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/card.tsx +44 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/checkbox.tsx +16 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/date-picker.tsx +76 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/input.tsx +22 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/label.tsx +19 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/modal.tsx +94 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/popover.tsx +31 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/progress.tsx +33 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/search-input.tsx +69 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/select.tsx +80 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/skeleton.tsx +12 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/sonner.tsx +23 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/spinner.tsx +8 -0
- package/templates/react-monorepo/files/packages/ui/src/components/ui/textarea.tsx +22 -0
- package/templates/react-monorepo/files/packages/ui/src/index.ts +45 -0
- package/templates/react-monorepo/files/packages/ui/src/lib/utils.ts +7 -0
- package/templates/react-monorepo/files/packages/ui/src/lib/zod-setup.ts +28 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/brand.ts +18 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/meta-context.ts +30 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/meta-provider.tsx +24 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/page-meta.tsx +69 -0
- package/templates/react-monorepo/files/packages/ui/src/meta/use-meta-config.ts +11 -0
- package/templates/react-monorepo/files/packages/ui/src/styles/globals.css +285 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/init-script.ts +23 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-colors.ts +14 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-context.ts +60 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-provider.tsx +101 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/theme-toggle.tsx +40 -0
- package/templates/react-monorepo/files/packages/ui/src/theme/use-theme.ts +11 -0
- package/templates/react-monorepo/files/packages/ui/tsconfig.json +9 -0
- package/templates/react-monorepo/files/turbo.json +24 -0
- package/templates/react-monorepo/template.json +230 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
Injectable,
|
|
4
|
+
ServiceUnavailableException
|
|
5
|
+
} from '@nestjs/common';
|
|
6
|
+
import {
|
|
7
|
+
S3Client,
|
|
8
|
+
PutObjectCommand,
|
|
9
|
+
DeleteObjectCommand,
|
|
10
|
+
GetObjectCommand
|
|
11
|
+
} from '@aws-sdk/client-s3';
|
|
12
|
+
import path from 'path';
|
|
13
|
+
import {
|
|
14
|
+
CustomConfigService,
|
|
15
|
+
GeneratorService,
|
|
16
|
+
LoggerService
|
|
17
|
+
} from 'common/services';
|
|
18
|
+
import { Enum } from 'common/utils';
|
|
19
|
+
|
|
20
|
+
@Injectable()
|
|
21
|
+
export class S3Service {
|
|
22
|
+
private client: S3Client;
|
|
23
|
+
|
|
24
|
+
constructor(
|
|
25
|
+
private config: CustomConfigService,
|
|
26
|
+
private gen: GeneratorService,
|
|
27
|
+
private logger: LoggerService
|
|
28
|
+
) {
|
|
29
|
+
const { accessKeyId, region, endpoint, secretAccessKey } = this.config.s3;
|
|
30
|
+
this.client = new S3Client({
|
|
31
|
+
region,
|
|
32
|
+
endpoint,
|
|
33
|
+
credentials: {
|
|
34
|
+
accessKeyId,
|
|
35
|
+
secretAccessKey
|
|
36
|
+
},
|
|
37
|
+
forcePathStyle: true,
|
|
38
|
+
// Without these the SDK defaults leave a request outstanding for minutes
|
|
39
|
+
// when the storage host accepts the TCP connection but never answers —
|
|
40
|
+
// every caller (statement import, avatars, feedback attachments) inherits
|
|
41
|
+
// that hang. Bound it so an unreachable bucket surfaces as a fast error.
|
|
42
|
+
requestHandler: {
|
|
43
|
+
connectionTimeout: 5_000,
|
|
44
|
+
// Socket-inactivity timeout, so a steadily streaming large upload will
|
|
45
|
+
// not trip it. Sized with `maxAttempts` so the worst case (~30s) lands
|
|
46
|
+
// inside the client's own upload timeout — the browser must receive our
|
|
47
|
+
// error rather than give up first and show a generic "connection lost".
|
|
48
|
+
requestTimeout: 15_000,
|
|
49
|
+
// Without this the handler only logs a warning when requestTimeout
|
|
50
|
+
// elapses and lets the socket keep hanging — the timeout has to be
|
|
51
|
+
// opted into as an actual error.
|
|
52
|
+
throwOnRequestTimeout: true
|
|
53
|
+
},
|
|
54
|
+
maxAttempts: 2
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private get pathPrefix() {
|
|
59
|
+
const { endpoint, bucket } = this.config.s3;
|
|
60
|
+
return `${endpoint}/${bucket}/`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
getKey(
|
|
64
|
+
folder: Enum.UploadFolder,
|
|
65
|
+
file: Express.Multer.File,
|
|
66
|
+
name?: string
|
|
67
|
+
): string {
|
|
68
|
+
return `${folder}/${name ?? this.gen.cuid()}${path.extname(
|
|
69
|
+
file.originalname
|
|
70
|
+
)}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
getKeyFromUrl(url: string) {
|
|
74
|
+
return url.replace(this.pathPrefix, '');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
getUrl(key: string) {
|
|
78
|
+
return this.pathPrefix + key;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
isHostedUrl(url: string) {
|
|
82
|
+
return url.startsWith(this.pathPrefix);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Storage failures are not the caller's fault. A timeout / DNS / refused-connection
|
|
87
|
+
* error means the bucket host is unreachable — that is a 503 the client can retry,
|
|
88
|
+
* not a 400 telling the user their file was bad. Anything else (missing key, bad
|
|
89
|
+
* ACL) stays a 400 as before.
|
|
90
|
+
*/
|
|
91
|
+
private toException(error: any, action: 'store' | 'read') {
|
|
92
|
+
const transportCodes = [
|
|
93
|
+
'TimeoutError',
|
|
94
|
+
'ECONNREFUSED',
|
|
95
|
+
'ECONNRESET',
|
|
96
|
+
'ENOTFOUND',
|
|
97
|
+
'EAI_AGAIN',
|
|
98
|
+
'EPIPE',
|
|
99
|
+
'ETIMEDOUT'
|
|
100
|
+
];
|
|
101
|
+
const code = error?.name ?? error?.code;
|
|
102
|
+
const unreachable =
|
|
103
|
+
transportCodes.includes(code) ||
|
|
104
|
+
transportCodes.includes(error?.$metadata?.httpStatusCode) ||
|
|
105
|
+
error?.$metadata?.httpStatusCode >= 500;
|
|
106
|
+
|
|
107
|
+
if (unreachable) {
|
|
108
|
+
this.logger.error(
|
|
109
|
+
{ err: error, action, endpoint: this.config.s3.endpoint },
|
|
110
|
+
'File storage is unreachable.'
|
|
111
|
+
);
|
|
112
|
+
return new ServiceUnavailableException(
|
|
113
|
+
`We could not ${action} the file right now — storage is unavailable. Please try again shortly.`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
return new BadRequestException(error.message);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async uploadFile(file: Express.Multer.File, folder: Enum.UploadFolder) {
|
|
120
|
+
try {
|
|
121
|
+
const key = this.getKey(folder, file);
|
|
122
|
+
const acl = Enum.isPrivateUploadFolder(folder)
|
|
123
|
+
? 'private'
|
|
124
|
+
: 'public-read';
|
|
125
|
+
const command = new PutObjectCommand({
|
|
126
|
+
Bucket: this.config.s3.bucket,
|
|
127
|
+
Key: key,
|
|
128
|
+
Body: file.buffer,
|
|
129
|
+
ContentType: file.mimetype,
|
|
130
|
+
ACL: acl,
|
|
131
|
+
Metadata: {
|
|
132
|
+
originalName: file.originalname
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
await this.client.send(command);
|
|
136
|
+
return this.getUrl(key);
|
|
137
|
+
} catch (error: any) {
|
|
138
|
+
throw this.toException(error, 'store');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async getFileBuffer(key: string): Promise<Buffer> {
|
|
143
|
+
try {
|
|
144
|
+
const command = new GetObjectCommand({
|
|
145
|
+
Bucket: this.config.s3.bucket,
|
|
146
|
+
Key: key
|
|
147
|
+
});
|
|
148
|
+
const response = await this.client.send(command);
|
|
149
|
+
const bytes = await response.Body!.transformToByteArray();
|
|
150
|
+
return Buffer.from(bytes);
|
|
151
|
+
} catch (error: any) {
|
|
152
|
+
throw this.toException(error, 'read');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async deleteFile(key: string) {
|
|
157
|
+
try {
|
|
158
|
+
const command = new DeleteObjectCommand({
|
|
159
|
+
Bucket: this.config.s3.bucket,
|
|
160
|
+
Key: key
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
await this.client.send(command);
|
|
164
|
+
} catch (error: any) {
|
|
165
|
+
throw new BadRequestException(error.message);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './notifications.controller';
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Controller,
|
|
3
|
+
Get,
|
|
4
|
+
HttpCode,
|
|
5
|
+
Param,
|
|
6
|
+
Patch,
|
|
7
|
+
Post,
|
|
8
|
+
Query
|
|
9
|
+
} from '@nestjs/common';
|
|
10
|
+
import { ApiNotFoundResponse, ApiTags } from '@nestjs/swagger';
|
|
11
|
+
import { Session, UserSession } from '@thallesp/nestjs-better-auth';
|
|
12
|
+
import { IdParam, Op, Returns } from 'common/decorators';
|
|
13
|
+
import { OKEntity } from 'common/entity';
|
|
14
|
+
import { NotificationsService } from '../services';
|
|
15
|
+
import {
|
|
16
|
+
NotificationEntity,
|
|
17
|
+
PaginatedNotificationEntity,
|
|
18
|
+
UnreadCountEntity
|
|
19
|
+
} from '../entity';
|
|
20
|
+
import { NotificationFilters } from '../dto';
|
|
21
|
+
|
|
22
|
+
@ApiTags('Notifications')
|
|
23
|
+
@Controller({ path: 'notifications', version: '1' })
|
|
24
|
+
export class NotificationsController {
|
|
25
|
+
constructor(private readonly notifications: NotificationsService) {}
|
|
26
|
+
|
|
27
|
+
@Get()
|
|
28
|
+
@Op('listNotifications', '/v1/notifications', 'List your notifications')
|
|
29
|
+
@Returns(PaginatedNotificationEntity)
|
|
30
|
+
list(
|
|
31
|
+
@Session() { user }: UserSession,
|
|
32
|
+
@Query({ schema: NotificationFilters }) filters: NotificationFilters
|
|
33
|
+
) {
|
|
34
|
+
return this.notifications.list(user.id, filters);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Get('unread-count')
|
|
38
|
+
@Op(
|
|
39
|
+
'getUnreadNotificationCount',
|
|
40
|
+
'/v1/notifications/unread-count',
|
|
41
|
+
'Count your unread notifications'
|
|
42
|
+
)
|
|
43
|
+
@Returns(UnreadCountEntity)
|
|
44
|
+
unreadCount(@Session() { user }: UserSession) {
|
|
45
|
+
return this.notifications.unreadCount(user.id);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Patch(':id/read')
|
|
49
|
+
@Op(
|
|
50
|
+
'markNotificationRead',
|
|
51
|
+
'/v1/notifications/:id/read',
|
|
52
|
+
'Mark a single notification as read'
|
|
53
|
+
)
|
|
54
|
+
@IdParam()
|
|
55
|
+
@Returns(NotificationEntity)
|
|
56
|
+
@ApiNotFoundResponse({ description: 'Notification not found' })
|
|
57
|
+
markRead(@Session() { user }: UserSession, @Param('id') id: string) {
|
|
58
|
+
return this.notifications.markRead(user.id, id);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Post('read-all')
|
|
62
|
+
@Op(
|
|
63
|
+
'markAllNotificationsRead',
|
|
64
|
+
'/v1/notifications/read-all',
|
|
65
|
+
'Mark all your notifications as read'
|
|
66
|
+
)
|
|
67
|
+
@HttpCode(200)
|
|
68
|
+
@Returns(OKEntity)
|
|
69
|
+
markAllRead(@Session() { user }: UserSession) {
|
|
70
|
+
return this.notifications.markAllRead(user.id);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './notification.dto';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { CommonFilters } from 'common/dto';
|
|
3
|
+
|
|
4
|
+
export const NotificationFilters = CommonFilters.extend({
|
|
5
|
+
// "true"/"1"/"yes"/"on" and their opposites; anything else is rejected.
|
|
6
|
+
unread: z
|
|
7
|
+
.stringbool()
|
|
8
|
+
.optional()
|
|
9
|
+
.meta({ description: 'Return only unread notifications', example: true })
|
|
10
|
+
});
|
|
11
|
+
export type NotificationFilters = z.output<typeof NotificationFilters>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './notification.entity';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { NotificationType } from '@db/client';
|
|
3
|
+
import { dateTime } from 'common/schemas';
|
|
4
|
+
import { paginated } from 'common/entity';
|
|
5
|
+
|
|
6
|
+
export const NotificationEntity = z
|
|
7
|
+
.object({
|
|
8
|
+
id: z.string(),
|
|
9
|
+
userId: z.string(),
|
|
10
|
+
type: z.enum(NotificationType).meta({ example: 'reply' }),
|
|
11
|
+
// Free-form JSON, as stored; typed to accept Prisma's JsonValue.
|
|
12
|
+
data: z.unknown().meta({
|
|
13
|
+
type: 'object',
|
|
14
|
+
description:
|
|
15
|
+
'Type-specific payload for deep-linking, e.g. { postId, commentId, target }',
|
|
16
|
+
example: { postId: 'ckp...', commentId: 'ckq...' }
|
|
17
|
+
}),
|
|
18
|
+
readAt: dateTime().nullable(),
|
|
19
|
+
createdAt: dateTime()
|
|
20
|
+
})
|
|
21
|
+
.meta({ id: 'NotificationEntity' });
|
|
22
|
+
/** What a service hands back; the schema turns it into the response. */
|
|
23
|
+
export type NotificationEntity = z.input<typeof NotificationEntity>;
|
|
24
|
+
|
|
25
|
+
export const PaginatedNotificationEntity = paginated(
|
|
26
|
+
NotificationEntity,
|
|
27
|
+
'PaginatedNotificationEntity'
|
|
28
|
+
);
|
|
29
|
+
export type PaginatedNotificationEntity = z.input<
|
|
30
|
+
typeof PaginatedNotificationEntity
|
|
31
|
+
>;
|
|
32
|
+
|
|
33
|
+
/** Lightweight badge payload for the unread counter. */
|
|
34
|
+
export const UnreadCountEntity = z
|
|
35
|
+
.object({ count: z.number().meta({ example: 3 }) })
|
|
36
|
+
.meta({ id: 'UnreadCountEntity' });
|
|
37
|
+
export type UnreadCountEntity = z.input<typeof UnreadCountEntity>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { NotificationsController } from './controllers';
|
|
3
|
+
import { NotificationsService } from './services';
|
|
4
|
+
|
|
5
|
+
@Module({
|
|
6
|
+
controllers: [NotificationsController],
|
|
7
|
+
providers: [NotificationsService],
|
|
8
|
+
// Exported so any producing module can dispatch notifications.
|
|
9
|
+
exports: [NotificationsService]
|
|
10
|
+
})
|
|
11
|
+
export class NotificationsModule {}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './notifications.service';
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
2
|
+
import { NotificationType, Prisma } from '@db/client';
|
|
3
|
+
import { PrismaService, LoggerService } from 'common/services';
|
|
4
|
+
import { ok } from 'common/entity';
|
|
5
|
+
import { RealtimeService } from 'realtime/services/realtime.service';
|
|
6
|
+
import {
|
|
7
|
+
NotificationEntity,
|
|
8
|
+
PaginatedNotificationEntity,
|
|
9
|
+
UnreadCountEntity
|
|
10
|
+
} from '../entity';
|
|
11
|
+
import { NotificationFilters } from '../dto';
|
|
12
|
+
|
|
13
|
+
@Injectable()
|
|
14
|
+
export class NotificationsService {
|
|
15
|
+
constructor(
|
|
16
|
+
private readonly db: PrismaService,
|
|
17
|
+
private readonly realtime: RealtimeService,
|
|
18
|
+
private readonly logger: LoggerService
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Persist a notification, then push it to the recipient's live sockets.
|
|
23
|
+
*
|
|
24
|
+
* This is the write side other modules call: storing first
|
|
25
|
+
* means the alert survives an offline recipient — the socket emit is just the
|
|
26
|
+
* real-time bonus for anyone currently connected. Never throws: a notification
|
|
27
|
+
* failing must not fail the action that triggered it (a comment, a vote), so
|
|
28
|
+
* errors are swallowed and logged, mirroring `RealtimeService`'s emit policy.
|
|
29
|
+
*/
|
|
30
|
+
async dispatch(
|
|
31
|
+
userId: string,
|
|
32
|
+
type: NotificationType,
|
|
33
|
+
data: Prisma.InputJsonValue
|
|
34
|
+
): Promise<NotificationEntity | null> {
|
|
35
|
+
try {
|
|
36
|
+
const notification = await this.db.notification.create({
|
|
37
|
+
data: { userId, type, data }
|
|
38
|
+
});
|
|
39
|
+
// Sockets bypass the HTTP serializer; encode through the same schema so
|
|
40
|
+
// the event has exactly the shape the REST endpoints return.
|
|
41
|
+
this.realtime.notifyUser(userId, NotificationEntity.parse(notification));
|
|
42
|
+
return notification;
|
|
43
|
+
} catch (err) {
|
|
44
|
+
this.logger.warn(
|
|
45
|
+
{ err, userId, type },
|
|
46
|
+
'Failed to dispatch notification.'
|
|
47
|
+
);
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async list(
|
|
53
|
+
userId: string,
|
|
54
|
+
filters: NotificationFilters
|
|
55
|
+
): Promise<PaginatedNotificationEntity> {
|
|
56
|
+
const where: Prisma.NotificationWhereInput = { userId };
|
|
57
|
+
if (filters.unread) where.readAt = null;
|
|
58
|
+
|
|
59
|
+
const [list, meta] = await this.db.x.notification.paginate({
|
|
60
|
+
where,
|
|
61
|
+
page: filters.page,
|
|
62
|
+
limit: filters.limit,
|
|
63
|
+
orderBy: { createdAt: 'desc' }
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return { list, ...meta };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async unreadCount(userId: string): Promise<UnreadCountEntity> {
|
|
70
|
+
const count = await this.db.notification.count({
|
|
71
|
+
where: { userId, readAt: null }
|
|
72
|
+
});
|
|
73
|
+
return { count };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async markRead(userId: string, id: string): Promise<NotificationEntity> {
|
|
77
|
+
const existing = await this.db.notification.findFirst({
|
|
78
|
+
where: { id, userId }
|
|
79
|
+
});
|
|
80
|
+
if (!existing) throw new NotFoundException('Notification not found');
|
|
81
|
+
|
|
82
|
+
// Idempotent: re-reading an already-read notification is a no-op.
|
|
83
|
+
if (existing.readAt) return existing;
|
|
84
|
+
|
|
85
|
+
return this.db.notification.update({
|
|
86
|
+
where: { id },
|
|
87
|
+
data: { readAt: new Date() }
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async markAllRead(userId: string) {
|
|
92
|
+
await this.db.notification.updateMany({
|
|
93
|
+
where: { userId, readAt: null },
|
|
94
|
+
data: { readAt: new Date() }
|
|
95
|
+
});
|
|
96
|
+
return ok();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { WsException } from '@nestjs/websockets';
|
|
2
|
+
import { Socket } from 'socket.io';
|
|
3
|
+
import { EventsGateway } from '../gateways/events.gateway';
|
|
4
|
+
import { FEED_ROOM, channelRoom, userRoom } from '../constants';
|
|
5
|
+
|
|
6
|
+
const makeSocket = (rooms: string[] = []) =>
|
|
7
|
+
({
|
|
8
|
+
id: 'socket-1',
|
|
9
|
+
data: {
|
|
10
|
+
user: { id: 'me', name: 'Me', email: 'me@test.com', role: 'user' }
|
|
11
|
+
},
|
|
12
|
+
rooms: new Set(['socket-1', ...rooms]),
|
|
13
|
+
join: vi.fn().mockResolvedValue(undefined),
|
|
14
|
+
leave: vi.fn().mockResolvedValue(undefined),
|
|
15
|
+
to: vi.fn().mockReturnValue({ emit: vi.fn() })
|
|
16
|
+
}) as unknown as Socket;
|
|
17
|
+
|
|
18
|
+
const makeGateway = () =>
|
|
19
|
+
new EventsGateway(
|
|
20
|
+
{} as never,
|
|
21
|
+
{ online: vi.fn() } as never,
|
|
22
|
+
{ bindServer: vi.fn() } as never,
|
|
23
|
+
{ debug: vi.fn() } as never
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
describe('EventsGateway', () => {
|
|
27
|
+
describe('joinRoom', () => {
|
|
28
|
+
it('joins an allowed channel room', async () => {
|
|
29
|
+
const gateway = makeGateway();
|
|
30
|
+
const socket = makeSocket();
|
|
31
|
+
|
|
32
|
+
await expect(
|
|
33
|
+
gateway.joinRoom(socket, { room: channelRoom('clx1') })
|
|
34
|
+
).resolves.toEqual({ ok: true, room: channelRoom('clx1') });
|
|
35
|
+
expect(socket.join).toHaveBeenCalledWith(channelRoom('clx1'));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("refuses to join another user's notification room", async () => {
|
|
39
|
+
const gateway = makeGateway();
|
|
40
|
+
const socket = makeSocket();
|
|
41
|
+
|
|
42
|
+
await expect(
|
|
43
|
+
gateway.joinRoom(socket, { room: userRoom('victim') })
|
|
44
|
+
).rejects.toThrow(WsException);
|
|
45
|
+
expect(socket.join).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('refuses to join a room that is not on the allowlist', async () => {
|
|
49
|
+
const gateway = makeGateway();
|
|
50
|
+
const socket = makeSocket();
|
|
51
|
+
|
|
52
|
+
await expect(
|
|
53
|
+
gateway.joinRoom(socket, { room: 'admin:secrets' })
|
|
54
|
+
).rejects.toThrow(WsException);
|
|
55
|
+
expect(socket.join).not.toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe('typing / viewing', () => {
|
|
60
|
+
it('relays into a room the socket occupies', () => {
|
|
61
|
+
const gateway = makeGateway();
|
|
62
|
+
const socket = makeSocket([FEED_ROOM]);
|
|
63
|
+
|
|
64
|
+
gateway.typing(socket, { room: FEED_ROOM, isTyping: true });
|
|
65
|
+
expect(socket.to).toHaveBeenCalledWith(FEED_ROOM);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('refuses to forge a typing signal into a room it never joined', () => {
|
|
69
|
+
const gateway = makeGateway();
|
|
70
|
+
const socket = makeSocket();
|
|
71
|
+
|
|
72
|
+
expect(() =>
|
|
73
|
+
gateway.typing(socket, { room: userRoom('victim'), isTyping: true })
|
|
74
|
+
).toThrow(WsException);
|
|
75
|
+
expect(socket.to).not.toHaveBeenCalled();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('refuses to forge a viewing signal into a room it never joined', () => {
|
|
79
|
+
const gateway = makeGateway();
|
|
80
|
+
const socket = makeSocket();
|
|
81
|
+
|
|
82
|
+
expect(() =>
|
|
83
|
+
gateway.viewing(socket, { room: channelRoom('clx1'), viewing: true })
|
|
84
|
+
).toThrow(WsException);
|
|
85
|
+
expect(socket.to).not.toHaveBeenCalled();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { FEED_ROOM, isJoinableRoom, channelRoom, userRoom } from '../constants';
|
|
2
|
+
|
|
3
|
+
describe('isJoinableRoom', () => {
|
|
4
|
+
it('allows the feed and individual channel rooms', () => {
|
|
5
|
+
expect(isJoinableRoom(FEED_ROOM)).toBe(true);
|
|
6
|
+
expect(isJoinableRoom(channelRoom('clx123abc'))).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// The bug this closes: `room:join` used to call socket.join(body.room) with
|
|
10
|
+
// whatever string arrived, so any authenticated user could join another
|
|
11
|
+
// user's notification room and read their private notifications.
|
|
12
|
+
it("refuses another user's private notification room", () => {
|
|
13
|
+
expect(isJoinableRoom(userRoom('some-other-user-id'))).toBe(false);
|
|
14
|
+
expect(isJoinableRoom('user:')).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('refuses arbitrary and malformed room names', () => {
|
|
18
|
+
expect(isJoinableRoom('')).toBe(false);
|
|
19
|
+
expect(isJoinableRoom('feed:extra')).toBe(false);
|
|
20
|
+
expect(isJoinableRoom('channel:')).toBe(false);
|
|
21
|
+
expect(isJoinableRoom('channel:abc:def')).toBe(false);
|
|
22
|
+
expect(isJoinableRoom('anything-at-all')).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('is not fooled by a prefix that merely starts with a joinable room', () => {
|
|
26
|
+
expect(isJoinableRoom('feedxyz')).toBe(false);
|
|
27
|
+
expect(isJoinableRoom('xchannel:abc')).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import { WsException } from '@nestjs/websockets';
|
|
4
|
+
import { WsPermissionsGuard } from '../guards';
|
|
5
|
+
import { SocketUser } from '../misc/session-from-socket';
|
|
6
|
+
|
|
7
|
+
const contextFor = (user?: Partial<SocketUser>) =>
|
|
8
|
+
({
|
|
9
|
+
getHandler: () => vi.fn(),
|
|
10
|
+
getClass: () => vi.fn(),
|
|
11
|
+
switchToWs: () => ({
|
|
12
|
+
getClient: () => ({ data: user ? { user } : {} })
|
|
13
|
+
})
|
|
14
|
+
}) as unknown as ExecutionContext;
|
|
15
|
+
|
|
16
|
+
const guardWith = (required: unknown) => {
|
|
17
|
+
const reflector = {
|
|
18
|
+
getAllAndOverride: vi.fn().mockReturnValue(required)
|
|
19
|
+
} as unknown as Reflector;
|
|
20
|
+
return new WsPermissionsGuard(reflector);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('WsPermissionsGuard', () => {
|
|
24
|
+
it('allows a handler that declares no permission', () => {
|
|
25
|
+
const guard = guardWith(undefined);
|
|
26
|
+
expect(guard.canActivate(contextFor({ role: 'user' }))).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('allows a role that holds the required permission', () => {
|
|
30
|
+
const guard = guardWith({ session: ['list'] });
|
|
31
|
+
expect(guard.canActivate(contextFor({ role: 'user' }))).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('rejects a role that lacks the required permission', () => {
|
|
35
|
+
const guard = guardWith({ user: ['ban'] });
|
|
36
|
+
expect(() => guard.canActivate(contextFor({ role: 'user' }))).toThrow(
|
|
37
|
+
WsException
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('rejects an unauthenticated socket even when a permission is declared', () => {
|
|
42
|
+
const guard = guardWith({ session: ['list'] });
|
|
43
|
+
expect(() => guard.canActivate(contextFor())).toThrow(WsException);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('rejects an unauthenticated socket before consulting permissions', () => {
|
|
47
|
+
const guard = guardWith({ session: ['list'] });
|
|
48
|
+
try {
|
|
49
|
+
guard.canActivate(contextFor());
|
|
50
|
+
} catch (error) {
|
|
51
|
+
expect((error as WsException).message).toBe('Unauthorized');
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { INestApplicationContext } from '@nestjs/common';
|
|
2
|
+
import { IoAdapter } from '@nestjs/platform-socket.io';
|
|
3
|
+
import { ServerOptions } from 'socket.io';
|
|
4
|
+
import { createAdapter } from '@socket.io/redis-adapter';
|
|
5
|
+
import IoRedis from 'ioredis';
|
|
6
|
+
import { CustomConfigService } from 'common/services';
|
|
7
|
+
import { CORS_OPTIONS } from 'common/constants';
|
|
8
|
+
import { SOCKET_PATH } from '../constants';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Socket.io adapter backed by a Redis pub/sub pair.
|
|
12
|
+
*
|
|
13
|
+
* Every backend replica publishes emitted events onto Redis and subscribes to
|
|
14
|
+
* the others, so a `server.to(room).emit(...)` fans out to clients regardless
|
|
15
|
+
* of which instance holds their socket. This is the "Redis-backed adapter for
|
|
16
|
+
* horizontal scaling" called for in the PRD (§3.1).
|
|
17
|
+
*
|
|
18
|
+
* Wire it in `main.ts` before `app.listen()`:
|
|
19
|
+
*
|
|
20
|
+
* const adapter = new RedisIoAdapter(app);
|
|
21
|
+
* await adapter.connectToRedis(app.get(CustomConfigService));
|
|
22
|
+
* app.useWebSocketAdapter(adapter);
|
|
23
|
+
*/
|
|
24
|
+
export class RedisIoAdapter extends IoAdapter {
|
|
25
|
+
private adapterConstructor?: ReturnType<typeof createAdapter>;
|
|
26
|
+
|
|
27
|
+
constructor(private readonly app: INestApplicationContext) {
|
|
28
|
+
super(app);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async connectToRedis(config: CustomConfigService): Promise<void> {
|
|
32
|
+
const pubClient = new IoRedis({ ...config.redis });
|
|
33
|
+
const subClient = pubClient.duplicate();
|
|
34
|
+
this.adapterConstructor = createAdapter(pubClient, subClient);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
createIOServer(port: number, options?: ServerOptions) {
|
|
38
|
+
// IoAdapter types this as the full ServerOptions; socket.io fills the rest.
|
|
39
|
+
const server = super.createIOServer(port, {
|
|
40
|
+
...options,
|
|
41
|
+
path: SOCKET_PATH,
|
|
42
|
+
// Reuse the REST CORS allow-list so the browser can open the socket.
|
|
43
|
+
cors: {
|
|
44
|
+
origin: CORS_OPTIONS.origin,
|
|
45
|
+
credentials: true
|
|
46
|
+
}
|
|
47
|
+
} as ServerOptions);
|
|
48
|
+
|
|
49
|
+
// `connectToRedis` may not have run in envs where scaling is unnecessary
|
|
50
|
+
// (e.g. tests); fall back to the default in-memory adapter in that case.
|
|
51
|
+
if (this.adapterConstructor) {
|
|
52
|
+
server.adapter(this.adapterConstructor);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return server;
|
|
56
|
+
}
|
|
57
|
+
}
|