create-pardx-scaffold 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/README.md +45 -0
- package/cli.js +104 -0
- package/package.json +15 -0
- package/template/.cursorrules +479 -0
- package/template/.prettierignore +29 -0
- package/template/.prettierrc +11 -0
- package/template/.vscode/extensions.json +8 -0
- package/template/.vscode/settings.json +122 -0
- package/template/CLAUDE.md +762 -0
- package/template/README.md +125 -0
- package/template/apps/api/.env.example +11 -0
- package/template/apps/api/.eslintrc.js +222 -0
- package/template/apps/api/config.local.yaml +397 -0
- package/template/apps/api/libs/domain/auth/package.json +11 -0
- package/template/apps/api/libs/domain/auth/src/README.md +189 -0
- package/template/apps/api/libs/domain/auth/src/auth-validation.service.ts +37 -0
- package/template/apps/api/libs/domain/auth/src/auth.guard.ts +173 -0
- package/template/apps/api/libs/domain/auth/src/auth.module.ts +23 -0
- package/template/apps/api/libs/domain/auth/src/auth.service.ts +198 -0
- package/template/apps/api/libs/domain/auth/src/auth.ts +66 -0
- package/template/apps/api/libs/domain/auth/src/decorators/presets.decorator.ts +50 -0
- package/template/apps/api/libs/domain/auth/src/decorators/rbac.decorator.ts +67 -0
- package/template/apps/api/libs/domain/auth/src/decorators/resource-owner.decorator.ts +67 -0
- package/template/apps/api/libs/domain/auth/src/dto/auth.dto.ts +10 -0
- package/template/apps/api/libs/domain/auth/src/guards/streaming-asr-session.guard.ts +179 -0
- package/template/apps/api/libs/domain/auth/src/index.ts +12 -0
- package/template/apps/api/libs/domain/auth/src/types/auth.interface.ts +52 -0
- package/template/apps/api/libs/domain/auth/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/domain/db/package.json +11 -0
- package/template/apps/api/libs/domain/db/src/index.ts +14 -0
- package/template/apps/api/libs/domain/db/src/modules/country-code/country-code.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/country-code/country-code.service.ts +140 -0
- package/template/apps/api/libs/domain/db/src/modules/country-code/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/discord-auth/discord-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/discord-auth/discord-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/discord-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/email-auth/email-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/email-auth/email-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/email-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/file-source/file-source.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/file-source/file-source.service.ts +109 -0
- package/template/apps/api/libs/domain/db/src/modules/file-source/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/google-auth/google-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/google-auth/google-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/google-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/message/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/message/message.module.ts +10 -0
- package/template/apps/api/libs/domain/db/src/modules/message/message.service.ts +314 -0
- package/template/apps/api/libs/domain/db/src/modules/mobile-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/mobile-auth/mobile-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/mobile-auth/mobile-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/risk-detection-record/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/risk-detection-record/risk-detection-record.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/risk-detection-record/risk-detection-record.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/system-task-queue/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/system-task-queue/system-task-queue.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/system-task-queue/system-task-queue.service.ts +101 -0
- package/template/apps/api/libs/domain/db/src/modules/user-info/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/user-info/user-info.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/user-info/user-info.service.ts +139 -0
- package/template/apps/api/libs/domain/db/src/modules/wechat-auth/index.ts +2 -0
- package/template/apps/api/libs/domain/db/src/modules/wechat-auth/wechat-auth.module.ts +12 -0
- package/template/apps/api/libs/domain/db/src/modules/wechat-auth/wechat-auth.service.ts +101 -0
- package/template/apps/api/libs/domain/db/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/clients/internal/ai/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/ai/risk-detection.client.ts +301 -0
- package/template/apps/api/libs/infra/clients/internal/ai/risk-detection.module.ts +22 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/crypt-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/crypt.client.ts +37 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/crypt.module.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/crypt/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/email/dto/email.dto.ts +75 -0
- package/template/apps/api/libs/infra/clients/internal/email/index.ts +11 -0
- package/template/apps/api/libs/infra/clients/internal/email/sendcloud.client.ts +400 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/README.md +255 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/dto/file-cdn.dto.ts +96 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/file-cdn-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/file-cdn.client.ts +620 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/file-cdn.module.ts +19 -0
- package/template/apps/api/libs/infra/clients/internal/file-cdn/index.ts +40 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/config/file.config.ts +14 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/dto/file.dto.ts +127 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-gcs.client.ts +154 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-qiniu.client.ts +729 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-s3.client.ts +1097 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-storage.interface.ts +114 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-tos.client.ts +767 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/file-us3.client.ts +176 -0
- package/template/apps/api/libs/infra/clients/internal/file-storage/index.ts +16 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/dto/ocr.dto.ts +61 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/index.ts +3 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/ocr.client.ts +123 -0
- package/template/apps/api/libs/infra/clients/internal/ocr/ocr.module.ts +15 -0
- package/template/apps/api/libs/infra/clients/internal/openai/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/openai/openai.client.ts +135 -0
- package/template/apps/api/libs/infra/clients/internal/openai/openai.module.ts +17 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/README.md +508 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/index.ts +44 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/openspeech.client.ts +441 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/openspeech.factory.ts +450 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/openspeech.module.ts +56 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/aliyun.provider.ts +308 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/base.provider.ts +114 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/index.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/volcengine-streaming.provider.ts +1689 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/providers/volcengine.provider.ts +387 -0
- package/template/apps/api/libs/infra/clients/internal/openspeech/types.ts +467 -0
- package/template/apps/api/libs/infra/clients/internal/sms/dto/sms.dto.ts +97 -0
- package/template/apps/api/libs/infra/clients/internal/sms/index.ts +15 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-aliyun.client.ts +52 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-http.client.ts +111 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-tencent.client.ts +54 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-volcengine.client.ts +165 -0
- package/template/apps/api/libs/infra/clients/internal/sms/sms-zxjc.client.ts +47 -0
- package/template/apps/api/libs/infra/clients/internal/sse/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/sse/sse-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/sse/sse.client.ts +360 -0
- package/template/apps/api/libs/infra/clients/internal/sse/sse.module.ts +17 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/third-party-sse.client.ts +51 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/third-party-sse.module.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/third-party-sse/third-party-sse.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/verify/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/verify/verify.client.ts +42 -0
- package/template/apps/api/libs/infra/clients/internal/verify/verify.module.ts +10 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/dto/tts.dto.ts +64 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/index.ts +3 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/volcengine-tts.client.ts +846 -0
- package/template/apps/api/libs/infra/clients/internal/volcengine-tts/volcengine-tts.module.ts +21 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/index.ts +2 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/wechat-client.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/wechat.client.ts +4 -0
- package/template/apps/api/libs/infra/clients/internal/wechat/wechat.module.ts +8 -0
- package/template/apps/api/libs/infra/clients/plugin/decorators/inject-client.decorator.ts +60 -0
- package/template/apps/api/libs/infra/clients/plugin/index.ts +21 -0
- package/template/apps/api/libs/infra/clients/plugin/interceptors/http-logging.interceptor.ts +130 -0
- package/template/apps/api/libs/infra/clients/plugin/interfaces/client.interface.ts +86 -0
- package/template/apps/api/libs/infra/clients/plugin/utils/retry.util.ts +157 -0
- package/template/apps/api/libs/infra/common/adapters/base.adapter.ts +94 -0
- package/template/apps/api/libs/infra/common/adapters/index.ts +38 -0
- package/template/apps/api/libs/infra/common/config/README.md +254 -0
- package/template/apps/api/libs/infra/common/config/agentx.config.ts +91 -0
- package/template/apps/api/libs/infra/common/config/configuration.ts +289 -0
- package/template/apps/api/libs/infra/common/config/constant/config.constants.ts +92 -0
- package/template/apps/api/libs/infra/common/config/dto/config.dto.ts +282 -0
- package/template/apps/api/libs/infra/common/config/package.json +11 -0
- package/template/apps/api/libs/infra/common/config/validation/env.validation.ts +161 -0
- package/template/apps/api/libs/infra/common/config/validation/index.ts +188 -0
- package/template/apps/api/libs/infra/common/config/validation/keys.validation.ts +564 -0
- package/template/apps/api/libs/infra/common/config/validation/yaml.validation.ts +582 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.controller.ts +135 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.interceptor.ts +36 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.module.ts +27 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/app-version.service.ts +252 -0
- package/template/apps/api/libs/infra/common/decorators/app-version/index.ts +13 -0
- package/template/apps/api/libs/infra/common/decorators/cache/cache.decorator.ts +437 -0
- package/template/apps/api/libs/infra/common/decorators/cache/cache.interceptor.ts +268 -0
- package/template/apps/api/libs/infra/common/decorators/cache/cache.module.ts +24 -0
- package/template/apps/api/libs/infra/common/decorators/cache/index.ts +33 -0
- package/template/apps/api/libs/infra/common/decorators/event/event.decorator.ts +229 -0
- package/template/apps/api/libs/infra/common/decorators/event/event.interceptor.ts +155 -0
- package/template/apps/api/libs/infra/common/decorators/event/event.module.ts +47 -0
- package/template/apps/api/libs/infra/common/decorators/event/handlers/cache-event.handler.ts +159 -0
- package/template/apps/api/libs/infra/common/decorators/event/index.ts +29 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.decorator.ts +221 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.interceptor.ts +150 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.module.ts +27 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.service.spec.ts +330 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/feature-flag.service.ts +423 -0
- package/template/apps/api/libs/infra/common/decorators/feature-flag/index.ts +28 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/dto/rate-limit.dto.ts +201 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/index.ts +54 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.decorator.ts +216 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.exception.ts +74 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.module.ts +37 -0
- package/template/apps/api/libs/infra/common/decorators/rate-limit/rate-limit.service.ts +430 -0
- package/template/apps/api/libs/infra/common/decorators/response.decorator.ts +67 -0
- package/template/apps/api/libs/infra/common/decorators/skip-version-check.decorator.ts +27 -0
- package/template/apps/api/libs/infra/common/decorators/transaction/index.ts +12 -0
- package/template/apps/api/libs/infra/common/decorators/transaction/transactional.decorator.ts +677 -0
- package/template/apps/api/libs/infra/common/decorators/ts-rest-controller.decorator.ts +63 -0
- package/template/apps/api/libs/infra/common/decorators/validation.decorator.ts +120 -0
- package/template/apps/api/libs/infra/common/decorators/version/index.ts +24 -0
- package/template/apps/api/libs/infra/common/decorators/version/version.decorator.ts +168 -0
- package/template/apps/api/libs/infra/common/decorators/version/version.interceptor.ts +97 -0
- package/template/apps/api/libs/infra/common/decorators/version/version.module.ts +21 -0
- package/template/apps/api/libs/infra/common/enums/action.enum.ts +7 -0
- package/template/apps/api/libs/infra/common/enums/error-codes.ts +71 -0
- package/template/apps/api/libs/infra/common/enums/role.enum.ts +4 -0
- package/template/apps/api/libs/infra/common/filter/exception/api.exception.ts +168 -0
- package/template/apps/api/libs/infra/common/filter/exception/exception.ts +47 -0
- package/template/apps/api/libs/infra/common/filter/exception/http.exception.ts +126 -0
- package/template/apps/api/libs/infra/common/guards/index.ts +1 -0
- package/template/apps/api/libs/infra/common/guards/version.guard.ts +312 -0
- package/template/apps/api/libs/infra/common/interceptor/mask/index.ts +1 -0
- package/template/apps/api/libs/infra/common/interceptor/mask/mask.interceptor.ts +242 -0
- package/template/apps/api/libs/infra/common/interceptor/rate-limit/no-rate-limit.interceptor.ts +14 -0
- package/template/apps/api/libs/infra/common/interceptor/rate-limit/rate-limit.interceptor.ts +230 -0
- package/template/apps/api/libs/infra/common/interceptor/transform/transform.interceptor.spec.ts +7 -0
- package/template/apps/api/libs/infra/common/interceptor/transform/transform.interceptor.ts +75 -0
- package/template/apps/api/libs/infra/common/interceptor/version/index.ts +1 -0
- package/template/apps/api/libs/infra/common/interceptor/version/version-header.interceptor.ts +62 -0
- package/template/apps/api/libs/infra/common/middleware/request.middleware.ts +109 -0
- package/template/apps/api/libs/infra/common/package.json +11 -0
- package/template/apps/api/libs/infra/common/pipes/transform-root.pipe.ts +12 -0
- package/template/apps/api/libs/infra/common/ts-rest/index.ts +26 -0
- package/template/apps/api/libs/infra/common/ts-rest/response.helper.ts +233 -0
- package/template/apps/api/libs/infra/i18n/en/errors.json +77 -0
- package/template/apps/api/libs/infra/i18n/en/events.json +1 -0
- package/template/apps/api/libs/infra/i18n/package.json +11 -0
- package/template/apps/api/libs/infra/i18n/zh-CN/errors.json +77 -0
- package/template/apps/api/libs/infra/i18n/zh-CN/events.json +1 -0
- package/template/apps/api/libs/infra/jwt/dto/jwt.dto.ts +1 -0
- package/template/apps/api/libs/infra/jwt/jwt.module.ts +26 -0
- package/template/apps/api/libs/infra/jwt/package.json +11 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/package.json +11 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/src/db-metrics.module.ts +141 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/src/db-metrics.service.ts +456 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/src/index.ts +2 -0
- package/template/apps/api/libs/infra/prisma/db-metrics/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/prisma/middleware/soft-delete.middleware.ts +179 -0
- package/template/apps/api/libs/infra/prisma/package.json +11 -0
- package/template/apps/api/libs/infra/prisma/prisma/index.ts +3 -0
- package/template/apps/api/libs/infra/prisma/prisma/prisma.module.ts +12 -0
- package/template/apps/api/libs/infra/prisma/prisma/prisma.service.ts +18 -0
- package/template/apps/api/libs/infra/prisma/prisma/types.ts +6 -0
- package/template/apps/api/libs/infra/prisma/prisma-read/prisma-read.module.ts +11 -0
- package/template/apps/api/libs/infra/prisma/prisma-read/prisma-read.service.ts +280 -0
- package/template/apps/api/libs/infra/prisma/prisma-write/prisma-write.module.ts +11 -0
- package/template/apps/api/libs/infra/prisma/prisma-write/prisma-write.service.ts +278 -0
- package/template/apps/api/libs/infra/prisma/prometheus/index.ts +1 -0
- package/template/apps/api/libs/infra/prisma/prometheus/prometheus.module.ts +231 -0
- package/template/apps/api/libs/infra/rabbitmq/package.json +11 -0
- package/template/apps/api/libs/infra/rabbitmq/src/dto/rabbitmq.dto.ts +13 -0
- package/template/apps/api/libs/infra/rabbitmq/src/index.ts +5 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq-events.module.ts +132 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq-events.service.ts +199 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq.module.ts +101 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/rabbitmq/src/rabbitmq.service.ts +543 -0
- package/template/apps/api/libs/infra/rabbitmq/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/redis/dto/redis.dto.ts +3 -0
- package/template/apps/api/libs/infra/redis/package.json +11 -0
- package/template/apps/api/libs/infra/redis/src/index.ts +2 -0
- package/template/apps/api/libs/infra/redis/src/redis.module.ts +63 -0
- package/template/apps/api/libs/infra/redis/src/redis.service.spec.ts +18 -0
- package/template/apps/api/libs/infra/redis/src/redis.service.ts +730 -0
- package/template/apps/api/libs/infra/redis/tsconfig.lib.json +9 -0
- package/template/apps/api/libs/infra/shared-db/index.ts +14 -0
- package/template/apps/api/libs/infra/shared-db/transaction-context.ts +51 -0
- package/template/apps/api/libs/infra/shared-db/transaction.module.ts +15 -0
- package/template/apps/api/libs/infra/shared-db/transaction.perf.spec.ts +226 -0
- package/template/apps/api/libs/infra/shared-db/transactional-service.base.ts +102 -0
- package/template/apps/api/libs/infra/shared-db/unit-of-work.service.ts +142 -0
- package/template/apps/api/libs/infra/shared-services/email/dto/email.dto.ts +87 -0
- package/template/apps/api/libs/infra/shared-services/email/email.module.ts +27 -0
- package/template/apps/api/libs/infra/shared-services/email/email.service.ts +258 -0
- package/template/apps/api/libs/infra/shared-services/email/index.ts +5 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/README.md +376 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/bucket-resolver.ts +306 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/file-storage.factory.ts +347 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/file-storage.module.ts +62 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/file-storage.service.ts +849 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/index.ts +57 -0
- package/template/apps/api/libs/infra/shared-services/file-storage/types.ts +210 -0
- package/template/apps/api/libs/infra/shared-services/ip-info/index.ts +2 -0
- package/template/apps/api/libs/infra/shared-services/ip-info/ip-info.module.ts +18 -0
- package/template/apps/api/libs/infra/shared-services/ip-info/ip-info.service.ts +118 -0
- package/template/apps/api/libs/infra/shared-services/sms/index.ts +11 -0
- package/template/apps/api/libs/infra/shared-services/sms/sms.factory.ts +367 -0
- package/template/apps/api/libs/infra/shared-services/sms/sms.module.ts +27 -0
- package/template/apps/api/libs/infra/shared-services/sms/sms.service.ts +315 -0
- package/template/apps/api/libs/infra/shared-services/sms/types.ts +297 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/index.ts +50 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/streaming-asr.module.ts +47 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/streaming-asr.service.ts +1336 -0
- package/template/apps/api/libs/infra/shared-services/streaming-asr/types.ts +208 -0
- package/template/apps/api/libs/infra/shared-services/system-health/index.ts +3 -0
- package/template/apps/api/libs/infra/shared-services/system-health/system-health.controller.ts +61 -0
- package/template/apps/api/libs/infra/shared-services/system-health/system-health.module.ts +16 -0
- package/template/apps/api/libs/infra/shared-services/system-health/system-health.service.ts +69 -0
- package/template/apps/api/libs/infra/shared-services/uploader/index.ts +2 -0
- package/template/apps/api/libs/infra/shared-services/uploader/uploader.module.ts +11 -0
- package/template/apps/api/libs/infra/shared-services/uploader/uploader.service.ts +265 -0
- package/template/apps/api/libs/infra/utils/array-buffer.util.ts +8 -0
- package/template/apps/api/libs/infra/utils/array.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/bcrypt.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/bigint.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/bytes.convert.util.ts +13 -0
- package/template/apps/api/libs/infra/utils/crypto.util.ts +206 -0
- package/template/apps/api/libs/infra/utils/download.ts +21 -0
- package/template/apps/api/libs/infra/utils/enviroment.util.ts +130 -0
- package/template/apps/api/libs/infra/utils/ffmpeg.util.ts +29 -0
- package/template/apps/api/libs/infra/utils/file.util.ts +448 -0
- package/template/apps/api/libs/infra/utils/folder.util.ts +11 -0
- package/template/apps/api/libs/infra/utils/frame.util.ts +24 -0
- package/template/apps/api/libs/infra/utils/http-client.ts +133 -0
- package/template/apps/api/libs/infra/utils/ip.util.ts +22 -0
- package/template/apps/api/libs/infra/utils/json.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/load-env.util.ts +53 -0
- package/template/apps/api/libs/infra/utils/logger.util.ts +121 -0
- package/template/apps/api/libs/infra/utils/object.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/package.json +11 -0
- package/template/apps/api/libs/infra/utils/prisma-error.util.ts +397 -0
- package/template/apps/api/libs/infra/utils/response.ts +23 -0
- package/template/apps/api/libs/infra/utils/serialize.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/string.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/timer.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/urlencode.util.ts +3 -0
- package/template/apps/api/libs/infra/utils/validate.util.ts +3 -0
- package/template/apps/api/nest-cli.json +25 -0
- package/template/apps/api/package.json +174 -0
- package/template/apps/api/prisma/schema.prisma +352 -0
- package/template/apps/api/prisma/seed.ts +30 -0
- package/template/apps/api/scripts/generate-db-crud.js +344 -0
- package/template/apps/api/scripts/insert-country-codes.ts +325 -0
- package/template/apps/api/scripts/link-prisma.js +44 -0
- package/template/apps/api/scripts/validate-api-versions.ts +273 -0
- package/template/apps/api/src/app.module.ts +208 -0
- package/template/apps/api/src/main.ts +298 -0
- package/template/apps/api/src/modules/health/health.controller.ts +13 -0
- package/template/apps/api/src/modules/health/health.module.ts +7 -0
- package/template/apps/api/tsconfig.build.json +4 -0
- package/template/apps/api/tsconfig.json +123 -0
- package/template/apps/web/.env.example +5 -0
- package/template/apps/web/app/globals.css +27 -0
- package/template/apps/web/app/layout.tsx +19 -0
- package/template/apps/web/app/page.tsx +42 -0
- package/template/apps/web/hooks/useAspectRatioSize.ts +187 -0
- package/template/apps/web/hooks/useDebouncedValue.ts +25 -0
- package/template/apps/web/hooks/useErrorHandler.ts +113 -0
- package/template/apps/web/hooks/useHotkeys.ts +251 -0
- package/template/apps/web/hooks/useI18nToast.ts +240 -0
- package/template/apps/web/hooks/useI18nValidation.ts +262 -0
- package/template/apps/web/hooks/useNotificationSSE.ts +270 -0
- package/template/apps/web/hooks/useOperationFeedback.ts +108 -0
- package/template/apps/web/hooks/usePerformanceMonitor.ts +105 -0
- package/template/apps/web/hooks/usePermissions.ts +17 -0
- package/template/apps/web/hooks/useTask.ts +489 -0
- package/template/apps/web/hooks/useVersionCheck.ts +329 -0
- package/template/apps/web/i18n/config.ts +50 -0
- package/template/apps/web/i18n/index.ts +30 -0
- package/template/apps/web/i18n/navigation.ts +26 -0
- package/template/apps/web/i18n/request.ts +50 -0
- package/template/apps/web/i18n/routing.ts +21 -0
- package/template/apps/web/i18n/types.ts +57 -0
- package/template/apps/web/lib/actions/auth.ts +81 -0
- package/template/apps/web/lib/actions/chat.ts +129 -0
- package/template/apps/web/lib/actions/common.ts +13 -0
- package/template/apps/web/lib/actions/task.ts +20 -0
- package/template/apps/web/lib/agent/chat-client.ts +42 -0
- package/template/apps/web/lib/agent/prompts.ts +43 -0
- package/template/apps/web/lib/analytics/components/PageTracker.tsx +137 -0
- package/template/apps/web/lib/analytics/hooks/usePageTracking.ts +137 -0
- package/template/apps/web/lib/analytics/index.ts +180 -0
- package/template/apps/web/lib/api/agents.ts +7 -0
- package/template/apps/web/lib/api/agno-chat.ts +263 -0
- package/template/apps/web/lib/api/auth-server.ts +244 -0
- package/template/apps/web/lib/api/avatar-upload.ts +96 -0
- package/template/apps/web/lib/api/cache-config.ts +236 -0
- package/template/apps/web/lib/api/client.ts +649 -0
- package/template/apps/web/lib/api/contracts/client.ts +336 -0
- package/template/apps/web/lib/api/contracts/hooks/index.ts +25 -0
- package/template/apps/web/lib/api/contracts/hooks/notification.ts +180 -0
- package/template/apps/web/lib/api/contracts/hooks/setting.ts +33 -0
- package/template/apps/web/lib/api/contracts/index.ts +18 -0
- package/template/apps/web/lib/api/contracts/server-client.ts +145 -0
- package/template/apps/web/lib/api/hooks/use-python-task.ts +154 -0
- package/template/apps/web/lib/api/queries/analytics.ts +51 -0
- package/template/apps/web/lib/api/queries/message.ts +75 -0
- package/template/apps/web/lib/api.ts +179 -0
- package/template/apps/web/lib/aspect-ratio.ts +10 -0
- package/template/apps/web/lib/audio-buffer-queue.ts +273 -0
- package/template/apps/web/lib/config.ts +163 -0
- package/template/apps/web/lib/data/industry.json +369 -0
- package/template/apps/web/lib/data/region.json +501 -0
- package/template/apps/web/lib/errors/error-handler.ts +194 -0
- package/template/apps/web/lib/errors/index.ts +16 -0
- package/template/apps/web/lib/errors/streaming-asr-errors.ts +434 -0
- package/template/apps/web/lib/form/index.ts +23 -0
- package/template/apps/web/lib/form/use-form.ts +143 -0
- package/template/apps/web/lib/icons-usage.md +99 -0
- package/template/apps/web/lib/icons.tsx +395 -0
- package/template/apps/web/lib/performance/monitor.ts +225 -0
- package/template/apps/web/lib/requests.ts +177 -0
- package/template/apps/web/lib/storage/index.ts +158 -0
- package/template/apps/web/lib/upload/api.ts +260 -0
- package/template/apps/web/lib/upload/batch-uploader.ts +286 -0
- package/template/apps/web/lib/upload/errors.ts +44 -0
- package/template/apps/web/lib/upload/folder-utils.ts +295 -0
- package/template/apps/web/lib/upload/uploader.ts +439 -0
- package/template/apps/web/lib/utils/reconnect.ts +223 -0
- package/template/apps/web/lib/utils/transcript-export.ts +321 -0
- package/template/apps/web/lib/version-mismatch.ts +147 -0
- package/template/apps/web/lib/version.ts +60 -0
- package/template/apps/web/next-env.d.ts +6 -0
- package/template/apps/web/next.config.ts +97 -0
- package/template/apps/web/package.json +89 -0
- package/template/apps/web/providers/app-provider.tsx +45 -0
- package/template/apps/web/providers/index.tsx +45 -0
- package/template/apps/web/providers/query-provider.tsx +181 -0
- package/template/apps/web/providers/theme-provider.tsx +26 -0
- package/template/apps/web/tsconfig.json +30 -0
- package/template/package.json +91 -0
- package/template/packages/config/eslint/base.js +32 -0
- package/template/packages/config/eslint/next.js +134 -0
- package/template/packages/config/eslint/react-internal.js +41 -0
- package/template/packages/config/eslint.config.mjs +26 -0
- package/template/packages/config/eslint.nestjs.config.mjs +62 -0
- package/template/packages/config/index.ts +2 -0
- package/template/packages/config/package.json +44 -0
- package/template/packages/config/postcss.config.mjs +8 -0
- package/template/packages/config/prettier.config.mjs +14 -0
- package/template/packages/config/tsconfig.json +19 -0
- package/template/packages/config/typescript/base.json +20 -0
- package/template/packages/config/typescript/nextjs.json +12 -0
- package/template/packages/config/typescript/react-library.json +8 -0
- package/template/packages/constants/README.md +111 -0
- package/template/packages/constants/package.json +25 -0
- package/template/packages/constants/src/index.ts +243 -0
- package/template/packages/constants/tsconfig.build.json +13 -0
- package/template/packages/constants/tsconfig.json +12 -0
- package/template/packages/contracts/ERROR-MIGRATION.md +179 -0
- package/template/packages/contracts/README.md +203 -0
- package/template/packages/contracts/jest.config.js +11 -0
- package/template/packages/contracts/package.json +60 -0
- package/template/packages/contracts/src/api/analytics.contract.ts +45 -0
- package/template/packages/contracts/src/api/download.contract.ts +66 -0
- package/template/packages/contracts/src/api/index.ts +12 -0
- package/template/packages/contracts/src/api/message.contract.ts +70 -0
- package/template/packages/contracts/src/api/risk-words.contract.ts +44 -0
- package/template/packages/contracts/src/api/setting.contract.ts +127 -0
- package/template/packages/contracts/src/api/sign.contract.ts +269 -0
- package/template/packages/contracts/src/api/sms.contract.ts +95 -0
- package/template/packages/contracts/src/api/system.contract.ts +52 -0
- package/template/packages/contracts/src/api/task.contract.ts +58 -0
- package/template/packages/contracts/src/api/uploader.contract.ts +93 -0
- package/template/packages/contracts/src/api/user.contract.ts +60 -0
- package/template/packages/contracts/src/api/webhook.contract.ts +73 -0
- package/template/packages/contracts/src/base.ts +319 -0
- package/template/packages/contracts/src/errors/codes.ts +55 -0
- package/template/packages/contracts/src/errors/domains/common.errors.ts +212 -0
- package/template/packages/contracts/src/errors/domains/index.ts +7 -0
- package/template/packages/contracts/src/errors/domains/user.errors.ts +51 -0
- package/template/packages/contracts/src/errors/error-response.ts +145 -0
- package/template/packages/contracts/src/errors/index.ts +16 -0
- package/template/packages/contracts/src/errors/messages.ts +240 -0
- package/template/packages/contracts/src/index.ts +16 -0
- package/template/packages/contracts/src/schemas/analytics.schema.ts +81 -0
- package/template/packages/contracts/src/schemas/download.schema.ts +59 -0
- package/template/packages/contracts/src/schemas/index.ts +18 -0
- package/template/packages/contracts/src/schemas/message.schema.ts +83 -0
- package/template/packages/contracts/src/schemas/risk-words.schema.ts +25 -0
- package/template/packages/contracts/src/schemas/setting.schema.ts +84 -0
- package/template/packages/contracts/src/schemas/sign.schema.ts +171 -0
- package/template/packages/contracts/src/schemas/sms.schema.ts +53 -0
- package/template/packages/contracts/src/schemas/sse.schema.ts +30 -0
- package/template/packages/contracts/src/schemas/system.schema.ts +26 -0
- package/template/packages/contracts/src/schemas/tag.schema.ts +65 -0
- package/template/packages/contracts/src/schemas/task.schema.ts +47 -0
- package/template/packages/contracts/src/schemas/uploader.schema.ts +121 -0
- package/template/packages/contracts/src/schemas/user.schema.ts +75 -0
- package/template/packages/contracts/src/schemas/webhook.schema.ts +72 -0
- package/template/packages/contracts/tsconfig.build.json +20 -0
- package/template/packages/contracts/tsconfig.json +12 -0
- package/template/packages/types/README.md +143 -0
- package/template/packages/types/ai.ts +30 -0
- package/template/packages/types/auth.ts +99 -0
- package/template/packages/types/common.ts +13 -0
- package/template/packages/types/creative.ts +68 -0
- package/template/packages/types/image-factory.ts +122 -0
- package/template/packages/types/index.ts +8 -0
- package/template/packages/types/package.json +21 -0
- package/template/packages/types/task.ts +27 -0
- package/template/packages/types/tsconfig.json +11 -0
- package/template/packages/ui/README.md +30 -0
- package/template/packages/ui/components.json +22 -0
- package/template/packages/ui/eslint.config.js +4 -0
- package/template/packages/ui/package.json +58 -0
- package/template/packages/ui/postcss.config.mjs +6 -0
- package/template/packages/ui/src/components/accordion.tsx +66 -0
- package/template/packages/ui/src/components/alert.tsx +61 -0
- package/template/packages/ui/src/components/avatar.tsx +57 -0
- package/template/packages/ui/src/components/badge.tsx +38 -0
- package/template/packages/ui/src/components/button.tsx +60 -0
- package/template/packages/ui/src/components/calendar.tsx +71 -0
- package/template/packages/ui/src/components/card.tsx +92 -0
- package/template/packages/ui/src/components/carousel.tsx +241 -0
- package/template/packages/ui/src/components/checkbox.tsx +32 -0
- package/template/packages/ui/src/components/command.tsx +184 -0
- package/template/packages/ui/src/components/dialog.tsx +134 -0
- package/template/packages/ui/src/components/dropdown-menu.tsx +257 -0
- package/template/packages/ui/src/components/empty.tsx +104 -0
- package/template/packages/ui/src/components/field.tsx +248 -0
- package/template/packages/ui/src/components/form.tsx +172 -0
- package/template/packages/ui/src/components/input-group.tsx +170 -0
- package/template/packages/ui/src/components/input.tsx +21 -0
- package/template/packages/ui/src/components/item.tsx +193 -0
- package/template/packages/ui/src/components/label.tsx +24 -0
- package/template/packages/ui/src/components/password-strength.tsx +248 -0
- package/template/packages/ui/src/components/popover.tsx +48 -0
- package/template/packages/ui/src/components/progress.tsx +35 -0
- package/template/packages/ui/src/components/scroll-area.tsx +48 -0
- package/template/packages/ui/src/components/select.tsx +190 -0
- package/template/packages/ui/src/components/separator.tsx +28 -0
- package/template/packages/ui/src/components/sheet.tsx +139 -0
- package/template/packages/ui/src/components/sidebar.tsx +729 -0
- package/template/packages/ui/src/components/skeleton.tsx +13 -0
- package/template/packages/ui/src/components/slider.tsx +87 -0
- package/template/packages/ui/src/components/sonner.tsx +40 -0
- package/template/packages/ui/src/components/switch.tsx +31 -0
- package/template/packages/ui/src/components/tabs.tsx +66 -0
- package/template/packages/ui/src/components/textarea.tsx +18 -0
- package/template/packages/ui/src/components/tooltip.tsx +61 -0
- package/template/packages/ui/src/hooks/use-mobile.ts +21 -0
- package/template/packages/ui/src/index.ts +38 -0
- package/template/packages/ui/src/lib/utils.ts +6 -0
- package/template/packages/ui/src/styles/globals.css +134 -0
- package/template/packages/ui/tsconfig.json +11 -0
- package/template/packages/ui/tsconfig.lint.json +8 -0
- package/template/packages/utils/README.md +173 -0
- package/template/packages/utils/array.util.ts +335 -0
- package/template/packages/utils/bcrypt.util.ts +10 -0
- package/template/packages/utils/bigint.util.ts +111 -0
- package/template/packages/utils/cn.ts +6 -0
- package/template/packages/utils/encrypt.ts +104 -0
- package/template/packages/utils/fetch.ts +170 -0
- package/template/packages/utils/file.ts +275 -0
- package/template/packages/utils/headers.ts +116 -0
- package/template/packages/utils/index.ts +22 -0
- package/template/packages/utils/jest.config.js +28 -0
- package/template/packages/utils/json.util.ts +9 -0
- package/template/packages/utils/mask.util.ts +348 -0
- package/template/packages/utils/object.util.ts +149 -0
- package/template/packages/utils/package.json +112 -0
- package/template/packages/utils/serialize.util.ts +17 -0
- package/template/packages/utils/string.util.ts +159 -0
- package/template/packages/utils/timer.util.ts +210 -0
- package/template/packages/utils/tsconfig.build.json +17 -0
- package/template/packages/utils/tsconfig.json +13 -0
- package/template/packages/utils/urlencode.util.ts +18 -0
- package/template/packages/utils/validate.util.ts +25 -0
- package/template/packages/validators/README.md +149 -0
- package/template/packages/validators/jest.config.js +20 -0
- package/template/packages/validators/package.json +32 -0
- package/template/packages/validators/src/index.ts +178 -0
- package/template/packages/validators/tsconfig.build.json +19 -0
- package/template/packages/validators/tsconfig.json +12 -0
- package/template/pnpm-lock.yaml +21574 -0
- package/template/pnpm-workspace.yaml +4 -0
- package/template/scripts/generate-i18n-errors.ts +371 -0
- package/template/scripts/generate-prisma-enums.js +170 -0
- package/template/scripts/generate-prisma-enums.ts +172 -0
- package/template/scripts/init-project.js +232 -0
- package/template/turbo.json +55 -0
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getUploadTokenPrivate,
|
|
3
|
+
initMultipartUpload,
|
|
4
|
+
getChunkUploadToken,
|
|
5
|
+
completeUpload,
|
|
6
|
+
abortUpload,
|
|
7
|
+
type UploadTokenData,
|
|
8
|
+
type InitMultipartUploadData,
|
|
9
|
+
type CompleteUploadData,
|
|
10
|
+
type UploadMetadata,
|
|
11
|
+
} from './api';
|
|
12
|
+
import { UploadError, UploadErrorCode } from './errors';
|
|
13
|
+
import { encryptParams } from '@repo/utils/encrypt';
|
|
14
|
+
import { calculateSHA256, createFileChunks } from '@repo/utils/file';
|
|
15
|
+
|
|
16
|
+
// 上传结果类型
|
|
17
|
+
export type UploadResult =
|
|
18
|
+
| UploadTokenData
|
|
19
|
+
| InitMultipartUploadData
|
|
20
|
+
| CompleteUploadData;
|
|
21
|
+
|
|
22
|
+
export interface UploadProgress {
|
|
23
|
+
loaded: number;
|
|
24
|
+
total: number;
|
|
25
|
+
percentage: number;
|
|
26
|
+
partNumber?: number; // 分片编号
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface UploadCallbacks {
|
|
30
|
+
onStart?: (fileId: string, uploadId?: string) => void;
|
|
31
|
+
onCalculating?: (progress: number) => void; // SHA256 计算进度 (0-100)
|
|
32
|
+
onProgress?: (progress: UploadProgress) => void;
|
|
33
|
+
onComplete?: (result: UploadResult) => void;
|
|
34
|
+
onError?: (error: Error) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface UploadParams {
|
|
38
|
+
file: File;
|
|
39
|
+
folderId: string;
|
|
40
|
+
spaceId: string;
|
|
41
|
+
callbacks?: UploadCallbacks;
|
|
42
|
+
metadata?: UploadMetadata; // 秒传后处理元数据
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 上传文件到 S3
|
|
47
|
+
*/
|
|
48
|
+
async function uploadToS3(
|
|
49
|
+
url: string,
|
|
50
|
+
chunk: Blob | File,
|
|
51
|
+
partNumber?: number,
|
|
52
|
+
onProgress?: (progress: UploadProgress) => void,
|
|
53
|
+
): Promise<{ ETag: string; PartNumber: number }> {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
const xhr = new XMLHttpRequest();
|
|
56
|
+
|
|
57
|
+
const progressHandler = (event: ProgressEvent) => {
|
|
58
|
+
if (event.lengthComputable && onProgress) {
|
|
59
|
+
// 确保进度值在 0-100 范围内
|
|
60
|
+
const percentage = Math.min(
|
|
61
|
+
100,
|
|
62
|
+
Math.max(0, Math.round((event.loaded / event.total) * 100)),
|
|
63
|
+
);
|
|
64
|
+
onProgress({
|
|
65
|
+
loaded: event.loaded,
|
|
66
|
+
total: event.total,
|
|
67
|
+
percentage,
|
|
68
|
+
partNumber,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const cleanup = () => {
|
|
74
|
+
xhr.upload.removeEventListener('progress', progressHandler);
|
|
75
|
+
xhr.removeEventListener('load', loadHandler);
|
|
76
|
+
xhr.removeEventListener('error', errorHandler);
|
|
77
|
+
xhr.removeEventListener('abort', abortHandler);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const loadHandler = () => {
|
|
81
|
+
cleanup();
|
|
82
|
+
if (xhr.status === 200) {
|
|
83
|
+
let etag = '';
|
|
84
|
+
|
|
85
|
+
// 尝试从响应头获取 ETag
|
|
86
|
+
// 注意:如果服务器没有设置 Access-Control-Expose-Headers: ETag,
|
|
87
|
+
// 浏览器会阻止访问 ETag 头,导致抛出错误
|
|
88
|
+
try {
|
|
89
|
+
const etagHeader = xhr.getResponseHeader('ETag');
|
|
90
|
+
if (etagHeader) {
|
|
91
|
+
etag = etagHeader.replace(/"/g, '');
|
|
92
|
+
}
|
|
93
|
+
} catch (error) {
|
|
94
|
+
// CORS 限制:浏览器阻止访问 ETag 头
|
|
95
|
+
// 这通常发生在服务器没有设置 Access-Control-Expose-Headers: ETag 时
|
|
96
|
+
console.warn('无法从响应头获取 ETag(CORS 限制):', error);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 如果响应头中没有 ETag,尝试从响应体中解析
|
|
100
|
+
if (!etag) {
|
|
101
|
+
try {
|
|
102
|
+
const responseText = xhr.responseText;
|
|
103
|
+
if (responseText) {
|
|
104
|
+
// 尝试解析 JSON 响应(如果服务器在响应体中返回 ETag)
|
|
105
|
+
const response = JSON.parse(responseText);
|
|
106
|
+
if (response.ETag || response.etag) {
|
|
107
|
+
etag = String(response.ETag || response.etag).replace(/"/g, '');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} catch {
|
|
111
|
+
// 响应体可能不是 JSON 或为空
|
|
112
|
+
// 忽略错误,继续处理
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 如果仍然没有 ETag
|
|
117
|
+
if (!etag) {
|
|
118
|
+
console.error('Failed to get ETag from response');
|
|
119
|
+
reject(new UploadError(UploadErrorCode.GET_ETAG_FAILED));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
resolve({
|
|
124
|
+
ETag: etag,
|
|
125
|
+
PartNumber: partNumber || 1,
|
|
126
|
+
});
|
|
127
|
+
} else {
|
|
128
|
+
reject(
|
|
129
|
+
new UploadError(UploadErrorCode.UPLOAD_STATUS_ERROR, undefined, {
|
|
130
|
+
status: xhr.status,
|
|
131
|
+
}),
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const errorHandler = () => {
|
|
137
|
+
cleanup();
|
|
138
|
+
reject(new UploadError(UploadErrorCode.NETWORK_ERROR));
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const abortHandler = () => {
|
|
142
|
+
cleanup();
|
|
143
|
+
reject(new UploadError(UploadErrorCode.UPLOAD_CANCELLED));
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
xhr.upload.addEventListener('progress', progressHandler);
|
|
147
|
+
xhr.addEventListener('load', loadHandler);
|
|
148
|
+
xhr.addEventListener('error', errorHandler);
|
|
149
|
+
xhr.addEventListener('abort', abortHandler);
|
|
150
|
+
|
|
151
|
+
xhr.open('PUT', url);
|
|
152
|
+
if (partNumber) {
|
|
153
|
+
xhr.setRequestHeader('PartNumber', partNumber.toString());
|
|
154
|
+
}
|
|
155
|
+
xhr.setRequestHeader(
|
|
156
|
+
'Content-Type',
|
|
157
|
+
chunk.type || 'application/octet-stream',
|
|
158
|
+
);
|
|
159
|
+
xhr.send(chunk);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 普通上传(文件 <= 8MB)
|
|
165
|
+
*/
|
|
166
|
+
async function uploadFullFile(
|
|
167
|
+
file: File,
|
|
168
|
+
filename: string,
|
|
169
|
+
fsize: number,
|
|
170
|
+
folderId: string,
|
|
171
|
+
spaceId: string,
|
|
172
|
+
sha256: string,
|
|
173
|
+
callbacks?: UploadCallbacks,
|
|
174
|
+
metadata?: UploadMetadata,
|
|
175
|
+
): Promise<UploadResult> {
|
|
176
|
+
try {
|
|
177
|
+
// 获取上传 Token
|
|
178
|
+
const signature = encryptParams(filename, sha256);
|
|
179
|
+
const tokenResponse = await getUploadTokenPrivate({
|
|
180
|
+
filename,
|
|
181
|
+
signature,
|
|
182
|
+
fsize,
|
|
183
|
+
folderId,
|
|
184
|
+
spaceId,
|
|
185
|
+
metadata,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// 提取 fileId:优先从 fileSystem.id 获取(秒传情况),否则从 fileId 字段获取
|
|
189
|
+
const fileId = tokenResponse.fileSystem?.id || tokenResponse.fileId;
|
|
190
|
+
|
|
191
|
+
if (!fileId) {
|
|
192
|
+
throw new UploadError(UploadErrorCode.GET_FILE_ID_FAILED);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// 检查是否秒传(文件已存在)
|
|
196
|
+
if (tokenResponse.success && tokenResponse.fileSystem) {
|
|
197
|
+
// 文件已存在,直接返回完整响应
|
|
198
|
+
callbacks?.onStart?.(fileId);
|
|
199
|
+
callbacks?.onProgress?.({
|
|
200
|
+
loaded: fsize,
|
|
201
|
+
total: fsize,
|
|
202
|
+
percentage: 100,
|
|
203
|
+
});
|
|
204
|
+
callbacks?.onComplete?.(tokenResponse);
|
|
205
|
+
return tokenResponse;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 文件不存在,需要上传
|
|
209
|
+
if (!tokenResponse.token || !tokenResponse.key) {
|
|
210
|
+
throw new UploadError(UploadErrorCode.GET_UPLOAD_TOKEN_FAILED);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
callbacks?.onStart?.(fileId);
|
|
214
|
+
|
|
215
|
+
// 上传到 S3
|
|
216
|
+
await uploadToS3(tokenResponse.token, file, undefined, (progress) => {
|
|
217
|
+
callbacks?.onProgress?.(progress);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// 完成上传
|
|
221
|
+
const completeSignature = encryptParams(filename, sha256, fileId);
|
|
222
|
+
const completeResponse = await completeUpload({
|
|
223
|
+
signature: completeSignature,
|
|
224
|
+
filename,
|
|
225
|
+
fileId: fileId,
|
|
226
|
+
fsize,
|
|
227
|
+
spaceId,
|
|
228
|
+
key: tokenResponse.key,
|
|
229
|
+
metadata,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
callbacks?.onComplete?.(completeResponse);
|
|
233
|
+
return completeResponse;
|
|
234
|
+
} catch (error) {
|
|
235
|
+
const uploadError =
|
|
236
|
+
error instanceof UploadError
|
|
237
|
+
? error
|
|
238
|
+
: new UploadError(UploadErrorCode.UPLOAD_FAILED);
|
|
239
|
+
callbacks?.onError?.(uploadError);
|
|
240
|
+
throw uploadError;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 分片上传(文件 > 8MB)
|
|
246
|
+
*/
|
|
247
|
+
async function uploadChunkFile(
|
|
248
|
+
file: File,
|
|
249
|
+
filename: string,
|
|
250
|
+
fsize: number,
|
|
251
|
+
folderId: string,
|
|
252
|
+
spaceId: string,
|
|
253
|
+
sha256: string,
|
|
254
|
+
callbacks?: UploadCallbacks,
|
|
255
|
+
metadata?: UploadMetadata,
|
|
256
|
+
): Promise<UploadResult> {
|
|
257
|
+
try {
|
|
258
|
+
// 初始化分片上传
|
|
259
|
+
const signature = encryptParams(filename, sha256);
|
|
260
|
+
const initResponse = await initMultipartUpload({
|
|
261
|
+
signature,
|
|
262
|
+
filename,
|
|
263
|
+
fsize,
|
|
264
|
+
spaceId,
|
|
265
|
+
folderId,
|
|
266
|
+
metadata,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
// 提取 fileId:优先从 fileSystem.id 获取(秒传情况),否则从 fileId 字段获取
|
|
270
|
+
const fileId = initResponse.fileSystem?.id || initResponse.fileId;
|
|
271
|
+
|
|
272
|
+
if (!fileId) {
|
|
273
|
+
throw new UploadError(UploadErrorCode.GET_FILE_ID_FAILED);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// 检查是否秒传(文件已存在,没有 uploadId 表示秒传)
|
|
277
|
+
if (!initResponse.uploadId) {
|
|
278
|
+
callbacks?.onStart?.(fileId);
|
|
279
|
+
callbacks?.onProgress?.({
|
|
280
|
+
loaded: fsize,
|
|
281
|
+
total: fsize,
|
|
282
|
+
percentage: 100,
|
|
283
|
+
});
|
|
284
|
+
callbacks?.onComplete?.(initResponse);
|
|
285
|
+
return initResponse;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const uploadId = initResponse.uploadId;
|
|
289
|
+
callbacks?.onStart?.(fileId, uploadId);
|
|
290
|
+
|
|
291
|
+
// 创建分片
|
|
292
|
+
const chunks = createFileChunks(file);
|
|
293
|
+
const chunkProgress: number[] = new Array(chunks.length).fill(0);
|
|
294
|
+
|
|
295
|
+
// 上传所有分片(串行,避免进度计算问题)
|
|
296
|
+
const parts: { ETag: string; PartNumber: number }[] = [];
|
|
297
|
+
|
|
298
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
299
|
+
const chunk = chunks[i];
|
|
300
|
+
if (!chunk) continue;
|
|
301
|
+
const partNumber = i + 1;
|
|
302
|
+
|
|
303
|
+
// 获取分片上传 Token
|
|
304
|
+
const chunkSignature = encryptParams(filename);
|
|
305
|
+
const chunkTokenResponse = await getChunkUploadToken({
|
|
306
|
+
signature: chunkSignature,
|
|
307
|
+
filename,
|
|
308
|
+
uploadId,
|
|
309
|
+
partNumber,
|
|
310
|
+
fsize,
|
|
311
|
+
spaceId,
|
|
312
|
+
folderId,
|
|
313
|
+
key: initResponse.key,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
// 上传分片
|
|
317
|
+
const part = await uploadToS3(
|
|
318
|
+
chunkTokenResponse.token,
|
|
319
|
+
chunk,
|
|
320
|
+
partNumber,
|
|
321
|
+
(progress) => {
|
|
322
|
+
// 更新当前分片进度
|
|
323
|
+
chunkProgress[i] = progress.loaded;
|
|
324
|
+
// 计算总进度,确保在 0-99 范围内
|
|
325
|
+
const totalLoaded = chunkProgress.reduce((sum, p) => sum + p, 0);
|
|
326
|
+
const percentage = Math.min(
|
|
327
|
+
99,
|
|
328
|
+
Math.max(0, Math.round((totalLoaded / fsize) * 100)),
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
callbacks?.onProgress?.({
|
|
332
|
+
loaded: totalLoaded,
|
|
333
|
+
total: fsize,
|
|
334
|
+
percentage,
|
|
335
|
+
partNumber,
|
|
336
|
+
});
|
|
337
|
+
},
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
parts.push(part);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// 完成上传
|
|
344
|
+
const completeSignature = encryptParams(filename, sha256, fileId);
|
|
345
|
+
const completeResponse = await completeUpload({
|
|
346
|
+
signature: completeSignature,
|
|
347
|
+
filename,
|
|
348
|
+
fileId: fileId,
|
|
349
|
+
fsize,
|
|
350
|
+
spaceId,
|
|
351
|
+
uploadId,
|
|
352
|
+
parts,
|
|
353
|
+
key: initResponse.key,
|
|
354
|
+
metadata,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
callbacks?.onComplete?.(completeResponse);
|
|
358
|
+
return completeResponse;
|
|
359
|
+
} catch (error) {
|
|
360
|
+
const uploadError =
|
|
361
|
+
error instanceof UploadError
|
|
362
|
+
? error
|
|
363
|
+
: new UploadError(UploadErrorCode.UPLOAD_FAILED);
|
|
364
|
+
callbacks?.onError?.(uploadError);
|
|
365
|
+
throw uploadError;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* 自动上传文件(根据文件大小选择普通上传或分片上传)
|
|
371
|
+
*/
|
|
372
|
+
export async function uploadFile(params: UploadParams): Promise<UploadResult> {
|
|
373
|
+
const { file, folderId, spaceId, callbacks, metadata } = params;
|
|
374
|
+
const filename = file.name;
|
|
375
|
+
const fsize = file.size;
|
|
376
|
+
const CHUNK_SIZE_THRESHOLD = 8 * 1024 * 1024; // 8MB
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
// 计算 SHA256
|
|
380
|
+
callbacks?.onCalculating?.(0);
|
|
381
|
+
|
|
382
|
+
const sha256 = await calculateSHA256(file, (progress) => {
|
|
383
|
+
// calculateSHA256 已经返回 0-100 的进度值,无需再乘以 100
|
|
384
|
+
callbacks?.onCalculating?.(progress);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
callbacks?.onCalculating?.(100);
|
|
388
|
+
|
|
389
|
+
// 根据文件大小选择上传方式
|
|
390
|
+
if (fsize > CHUNK_SIZE_THRESHOLD) {
|
|
391
|
+
return await uploadChunkFile(
|
|
392
|
+
file,
|
|
393
|
+
filename,
|
|
394
|
+
fsize,
|
|
395
|
+
folderId,
|
|
396
|
+
spaceId,
|
|
397
|
+
sha256,
|
|
398
|
+
callbacks,
|
|
399
|
+
metadata,
|
|
400
|
+
);
|
|
401
|
+
} else {
|
|
402
|
+
return await uploadFullFile(
|
|
403
|
+
file,
|
|
404
|
+
filename,
|
|
405
|
+
fsize,
|
|
406
|
+
folderId,
|
|
407
|
+
spaceId,
|
|
408
|
+
sha256,
|
|
409
|
+
callbacks,
|
|
410
|
+
metadata,
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
} catch (error) {
|
|
414
|
+
const uploadError =
|
|
415
|
+
error instanceof UploadError
|
|
416
|
+
? error
|
|
417
|
+
: new UploadError(UploadErrorCode.UPLOAD_FAILED);
|
|
418
|
+
callbacks?.onError?.(uploadError);
|
|
419
|
+
throw uploadError;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* 取消上传
|
|
425
|
+
*/
|
|
426
|
+
export async function cancelUpload(
|
|
427
|
+
filename: string,
|
|
428
|
+
fileId: string,
|
|
429
|
+
uploadId: string,
|
|
430
|
+
spaceId: string,
|
|
431
|
+
): Promise<void> {
|
|
432
|
+
const signature = encryptParams(filename, '', fileId);
|
|
433
|
+
await abortUpload({
|
|
434
|
+
signature,
|
|
435
|
+
uploadId,
|
|
436
|
+
spaceId,
|
|
437
|
+
fileId,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reconnection Strategy Configuration
|
|
3
|
+
*/
|
|
4
|
+
export interface ReconnectConfig {
|
|
5
|
+
/** Maximum number of retry attempts (default: 5) */
|
|
6
|
+
maxRetries?: number;
|
|
7
|
+
/** Initial delay in milliseconds (default: 1000) */
|
|
8
|
+
initialDelay?: number;
|
|
9
|
+
/** Maximum delay in milliseconds (default: 30000) */
|
|
10
|
+
maxDelay?: number;
|
|
11
|
+
/** Exponential backoff multiplier (default: 2) */
|
|
12
|
+
backoffMultiplier?: number;
|
|
13
|
+
/** Add random jitter to delay (default: true) */
|
|
14
|
+
jitter?: boolean;
|
|
15
|
+
/** Callback when attempting reconnect */
|
|
16
|
+
onReconnecting?: (attempt: number, delay: number) => void;
|
|
17
|
+
/** Callback when reconnection succeeds */
|
|
18
|
+
onReconnected?: () => void;
|
|
19
|
+
/** Callback when all retries exhausted */
|
|
20
|
+
onMaxRetriesReached?: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reconnection state
|
|
25
|
+
*/
|
|
26
|
+
export interface ReconnectState {
|
|
27
|
+
/** Current retry attempt */
|
|
28
|
+
attempt: number;
|
|
29
|
+
/** Whether currently reconnecting */
|
|
30
|
+
isReconnecting: boolean;
|
|
31
|
+
/** Next retry delay in ms */
|
|
32
|
+
nextDelay: number;
|
|
33
|
+
/** Whether max retries reached */
|
|
34
|
+
maxRetriesReached: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Reconnection Manager
|
|
39
|
+
*
|
|
40
|
+
* Handles automatic reconnection with exponential backoff
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const reconnect = new ReconnectManager({
|
|
45
|
+
* maxRetries: 5,
|
|
46
|
+
* onReconnecting: (attempt, delay) => {
|
|
47
|
+
* console.log(`Reconnecting in ${delay}ms (attempt ${attempt})`);
|
|
48
|
+
* },
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* // On disconnect
|
|
52
|
+
* reconnect.scheduleReconnect(async () => {
|
|
53
|
+
* await connectToServer();
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* // On successful connection
|
|
57
|
+
* reconnect.reset();
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export class ReconnectManager {
|
|
61
|
+
private config: Required<ReconnectConfig>;
|
|
62
|
+
private attempt = 0;
|
|
63
|
+
private timeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
64
|
+
private isReconnecting = false;
|
|
65
|
+
|
|
66
|
+
constructor(config: ReconnectConfig = {}) {
|
|
67
|
+
this.config = {
|
|
68
|
+
maxRetries: config.maxRetries ?? 5,
|
|
69
|
+
initialDelay: config.initialDelay ?? 1000,
|
|
70
|
+
maxDelay: config.maxDelay ?? 30000,
|
|
71
|
+
backoffMultiplier: config.backoffMultiplier ?? 2,
|
|
72
|
+
jitter: config.jitter ?? true,
|
|
73
|
+
onReconnecting: config.onReconnecting ?? (() => {}),
|
|
74
|
+
onReconnected: config.onReconnected ?? (() => {}),
|
|
75
|
+
onMaxRetriesReached: config.onMaxRetriesReached ?? (() => {}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Calculate delay for current attempt
|
|
81
|
+
*/
|
|
82
|
+
private calculateDelay(): number {
|
|
83
|
+
const { initialDelay, maxDelay, backoffMultiplier, jitter } = this.config;
|
|
84
|
+
|
|
85
|
+
let delay = initialDelay * Math.pow(backoffMultiplier, this.attempt);
|
|
86
|
+
delay = Math.min(delay, maxDelay);
|
|
87
|
+
|
|
88
|
+
if (jitter) {
|
|
89
|
+
// Add random jitter (±25%)
|
|
90
|
+
const jitterAmount = delay * 0.25;
|
|
91
|
+
delay += (Math.random() - 0.5) * 2 * jitterAmount;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return Math.round(delay);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get current state
|
|
99
|
+
*/
|
|
100
|
+
getState(): ReconnectState {
|
|
101
|
+
return {
|
|
102
|
+
attempt: this.attempt,
|
|
103
|
+
isReconnecting: this.isReconnecting,
|
|
104
|
+
nextDelay: this.calculateDelay(),
|
|
105
|
+
maxRetriesReached: this.attempt >= this.config.maxRetries,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Schedule a reconnection attempt
|
|
111
|
+
*/
|
|
112
|
+
async scheduleReconnect(reconnectFn: () => Promise<void>): Promise<boolean> {
|
|
113
|
+
if (this.isReconnecting) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (this.attempt >= this.config.maxRetries) {
|
|
118
|
+
this.config.onMaxRetriesReached();
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this.isReconnecting = true;
|
|
123
|
+
const delay = this.calculateDelay();
|
|
124
|
+
this.attempt++;
|
|
125
|
+
|
|
126
|
+
this.config.onReconnecting(this.attempt, delay);
|
|
127
|
+
|
|
128
|
+
return new Promise((resolve) => {
|
|
129
|
+
this.timeoutId = setTimeout(async () => {
|
|
130
|
+
try {
|
|
131
|
+
await reconnectFn();
|
|
132
|
+
this.config.onReconnected();
|
|
133
|
+
this.reset();
|
|
134
|
+
resolve(true);
|
|
135
|
+
} catch {
|
|
136
|
+
this.isReconnecting = false;
|
|
137
|
+
// Try again
|
|
138
|
+
const success = await this.scheduleReconnect(reconnectFn);
|
|
139
|
+
resolve(success);
|
|
140
|
+
}
|
|
141
|
+
}, delay);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Cancel pending reconnection
|
|
147
|
+
*/
|
|
148
|
+
cancel(): void {
|
|
149
|
+
if (this.timeoutId) {
|
|
150
|
+
clearTimeout(this.timeoutId);
|
|
151
|
+
this.timeoutId = null;
|
|
152
|
+
}
|
|
153
|
+
this.isReconnecting = false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Reset reconnection state (call on successful connection)
|
|
158
|
+
*/
|
|
159
|
+
reset(): void {
|
|
160
|
+
this.cancel();
|
|
161
|
+
this.attempt = 0;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Online/Offline detection hook utilities
|
|
167
|
+
*/
|
|
168
|
+
export function isOnline(): boolean {
|
|
169
|
+
return typeof navigator !== 'undefined' ? navigator.onLine : true;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Create online status listener
|
|
174
|
+
*/
|
|
175
|
+
export function createOnlineListener(
|
|
176
|
+
onOnline: () => void,
|
|
177
|
+
onOffline: () => void,
|
|
178
|
+
): () => void {
|
|
179
|
+
if (typeof window === 'undefined') {
|
|
180
|
+
return () => {};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
window.addEventListener('online', onOnline);
|
|
184
|
+
window.addEventListener('offline', onOffline);
|
|
185
|
+
|
|
186
|
+
return () => {
|
|
187
|
+
window.removeEventListener('online', onOnline);
|
|
188
|
+
window.removeEventListener('offline', onOffline);
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Retry a function with exponential backoff
|
|
194
|
+
*/
|
|
195
|
+
export async function retryWithBackoff<T>(
|
|
196
|
+
fn: () => Promise<T>,
|
|
197
|
+
config: ReconnectConfig = {},
|
|
198
|
+
): Promise<T> {
|
|
199
|
+
const manager = new ReconnectManager(config);
|
|
200
|
+
|
|
201
|
+
const attempt = async (): Promise<T> => {
|
|
202
|
+
try {
|
|
203
|
+
return await fn();
|
|
204
|
+
} catch (error) {
|
|
205
|
+
const state = manager.getState();
|
|
206
|
+
if (state.maxRetriesReached) {
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return new Promise((resolve, reject) => {
|
|
211
|
+
manager.scheduleReconnect(async () => {
|
|
212
|
+
try {
|
|
213
|
+
resolve(await fn());
|
|
214
|
+
} catch (retryError) {
|
|
215
|
+
reject(retryError);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
return attempt();
|
|
223
|
+
}
|