@digitaldefiance/node-express-suite 3.7.5 → 3.8.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 +38 -6
- package/package.json +9 -8
- package/src/__tests__/fixtures/{index.ts → index.d.ts} +1 -0
- package/src/__tests__/fixtures/index.d.ts.map +1 -0
- package/src/__tests__/fixtures/index.js +5 -0
- package/src/__tests__/fixtures/index.js.map +1 -0
- package/src/__tests__/fixtures/model-mocks.mock.d.ts +12 -0
- package/src/__tests__/fixtures/model-mocks.mock.d.ts.map +1 -0
- package/src/__tests__/fixtures/model-mocks.mock.js +102 -0
- package/src/__tests__/fixtures/model-mocks.mock.js.map +1 -0
- package/src/__tests__/helpers/application.mock.d.ts +8 -0
- package/src/__tests__/helpers/application.mock.d.ts.map +1 -0
- package/src/__tests__/helpers/application.mock.js +77 -0
- package/src/__tests__/helpers/application.mock.js.map +1 -0
- package/src/__tests__/helpers/{index.ts → index.d.ts} +1 -0
- package/src/__tests__/helpers/index.d.ts.map +1 -0
- package/src/__tests__/helpers/index.js +7 -0
- package/src/__tests__/helpers/index.js.map +1 -0
- package/src/__tests__/helpers/setup-test-env.d.ts +12 -0
- package/src/__tests__/helpers/setup-test-env.d.ts.map +1 -0
- package/src/__tests__/helpers/setup-test-env.js +121 -0
- package/src/__tests__/helpers/setup-test-env.js.map +1 -0
- package/src/__tests__/{index.ts → index.d.ts} +1 -0
- package/src/__tests__/index.d.ts.map +1 -0
- package/src/__tests__/index.js +6 -0
- package/src/__tests__/index.js.map +1 -0
- package/src/application-base.d.ts +128 -0
- package/src/application-base.d.ts.map +1 -0
- package/src/application-base.js +364 -0
- package/src/application-base.js.map +1 -0
- package/src/application-concrete.d.ts +26 -0
- package/src/application-concrete.d.ts.map +1 -0
- package/src/application-concrete.js +34 -0
- package/src/application-concrete.js.map +1 -0
- package/src/application.d.ts +34 -0
- package/src/application.d.ts.map +1 -0
- package/src/application.js +172 -0
- package/src/application.js.map +1 -0
- package/src/backup-code.d.ts +72 -0
- package/src/backup-code.d.ts.map +1 -0
- package/src/backup-code.js +243 -0
- package/src/backup-code.js.map +1 -0
- package/src/builders/application-builder.d.ts +47 -0
- package/src/builders/application-builder.d.ts.map +1 -0
- package/src/builders/application-builder.js +76 -0
- package/src/builders/application-builder.js.map +1 -0
- package/src/builders/{index.ts → index.d.ts} +1 -0
- package/src/builders/index.d.ts.map +1 -0
- package/src/builders/index.js +5 -0
- package/src/builders/index.js.map +1 -0
- package/src/constants.d.ts +21 -0
- package/src/constants.d.ts.map +1 -0
- package/src/constants.js +63 -0
- package/src/constants.js.map +1 -0
- package/src/container/{index.ts → index.d.ts} +1 -0
- package/src/container/index.d.ts.map +1 -0
- package/src/container/index.js +6 -0
- package/src/container/index.js.map +1 -0
- package/src/container/service-container.d.ts +45 -0
- package/src/container/service-container.d.ts.map +1 -0
- package/src/container/service-container.js +68 -0
- package/src/container/service-container.js.map +1 -0
- package/src/container/{service-definitions.ts → service-definitions.d.ts} +10 -11
- package/src/container/service-definitions.d.ts.map +1 -0
- package/src/container/service-definitions.js +21 -0
- package/src/container/service-definitions.js.map +1 -0
- package/src/controllers/base.d.ts +80 -0
- package/src/controllers/base.d.ts.map +1 -0
- package/src/controllers/base.js +318 -0
- package/src/controllers/base.js.map +1 -0
- package/src/controllers/{index.ts → index.d.ts} +1 -0
- package/src/controllers/index.d.ts.map +1 -0
- package/src/controllers/index.js +6 -0
- package/src/controllers/index.js.map +1 -0
- package/src/controllers/user.d.ts +66 -0
- package/src/controllers/user.d.ts.map +1 -0
- package/src/controllers/user.js +936 -0
- package/src/controllers/user.js.map +1 -0
- package/src/database/{database-initializer.ts → database-initializer.d.ts} +3 -4
- package/src/database/database-initializer.d.ts.map +1 -0
- package/src/database/database-initializer.js +8 -0
- package/src/database/database-initializer.js.map +1 -0
- package/src/database/{index.ts → index.d.ts} +1 -0
- package/src/database/index.d.ts.map +1 -0
- package/src/database/index.js +5 -0
- package/src/database/index.js.map +1 -0
- package/src/decorators/base-controller.d.ts +22 -0
- package/src/decorators/base-controller.d.ts.map +1 -0
- package/src/decorators/base-controller.js +71 -0
- package/src/decorators/base-controller.js.map +1 -0
- package/src/decorators/controller.d.ts +43 -0
- package/src/decorators/controller.d.ts.map +1 -0
- package/src/decorators/controller.js +73 -0
- package/src/decorators/controller.js.map +1 -0
- package/src/decorators/{index.ts → index.d.ts} +1 -0
- package/src/decorators/index.d.ts.map +1 -0
- package/src/decorators/index.js +7 -0
- package/src/decorators/index.js.map +1 -0
- package/src/decorators/zod-validation.d.ts +10 -0
- package/src/decorators/zod-validation.d.ts.map +1 -0
- package/src/decorators/zod-validation.js +53 -0
- package/src/decorators/zod-validation.js.map +1 -0
- package/src/defaults.d.ts +12 -0
- package/src/defaults.d.ts.map +1 -0
- package/src/defaults.js +212 -0
- package/src/defaults.js.map +1 -0
- package/src/documents/{base.ts → base.d.ts} +2 -4
- package/src/documents/base.d.ts.map +1 -0
- package/src/documents/base.js +8 -0
- package/src/documents/base.js.map +1 -0
- package/src/documents/{email-token.ts → email-token.d.ts} +3 -8
- package/src/documents/email-token.d.ts.map +1 -0
- package/src/documents/email-token.js +8 -0
- package/src/documents/email-token.js.map +1 -0
- package/src/documents/{index.ts → index.d.ts} +1 -0
- package/src/documents/index.d.ts.map +1 -0
- package/src/documents/index.js +3 -0
- package/src/documents/index.js.map +1 -0
- package/src/documents/{mnemonic.ts → mnemonic.d.ts} +2 -6
- package/src/documents/mnemonic.d.ts.map +1 -0
- package/src/documents/mnemonic.js +8 -0
- package/src/documents/mnemonic.js.map +1 -0
- package/src/documents/{role.ts → role.d.ts} +2 -6
- package/src/documents/role.d.ts.map +1 -0
- package/src/documents/role.js +8 -0
- package/src/documents/role.js.map +1 -0
- package/src/documents/{used-direct-login-token.ts → used-direct-login-token.d.ts} +2 -4
- package/src/documents/used-direct-login-token.d.ts.map +1 -0
- package/src/documents/used-direct-login-token.js +8 -0
- package/src/documents/used-direct-login-token.js.map +1 -0
- package/src/documents/{user-role.ts → user-role.d.ts} +2 -6
- package/src/documents/user-role.d.ts.map +1 -0
- package/src/documents/user-role.js +8 -0
- package/src/documents/user-role.js.map +1 -0
- package/src/documents/{user.ts → user.d.ts} +2 -6
- package/src/documents/user.d.ts.map +1 -0
- package/src/documents/user.js +8 -0
- package/src/documents/user.js.map +1 -0
- package/src/enumerations/base-model-name.d.ts +43 -0
- package/src/enumerations/base-model-name.d.ts.map +1 -0
- package/src/enumerations/base-model-name.js +39 -0
- package/src/enumerations/base-model-name.js.map +1 -0
- package/src/enumerations/{index.ts → index.d.ts} +1 -0
- package/src/enumerations/index.d.ts.map +1 -0
- package/src/enumerations/index.js +8 -0
- package/src/enumerations/index.js.map +1 -0
- package/src/enumerations/{length-encoding-type.ts → length-encoding-type.d.ts} +6 -6
- package/src/enumerations/length-encoding-type.d.ts.map +1 -0
- package/src/enumerations/length-encoding-type.js +20 -0
- package/src/enumerations/length-encoding-type.js.map +1 -0
- package/src/enumerations/schema-collection.d.ts +39 -0
- package/src/enumerations/schema-collection.d.ts.map +1 -0
- package/src/enumerations/schema-collection.js +43 -0
- package/src/enumerations/schema-collection.js.map +1 -0
- package/src/enumerations/{symmetric-error-type.ts → symmetric-error-type.d.ts} +4 -4
- package/src/enumerations/symmetric-error-type.d.ts.map +1 -0
- package/src/enumerations/symmetric-error-type.js +17 -0
- package/src/enumerations/symmetric-error-type.js.map +1 -0
- package/src/environment.d.ts +194 -0
- package/src/environment.d.ts.map +1 -0
- package/src/environment.js +649 -0
- package/src/environment.js.map +1 -0
- package/src/errors/express-validation.d.ts +24 -0
- package/src/errors/express-validation.d.ts.map +1 -0
- package/src/errors/express-validation.js +33 -0
- package/src/errors/express-validation.js.map +1 -0
- package/src/errors/{index.ts → index.d.ts} +1 -0
- package/src/errors/index.d.ts.map +1 -0
- package/src/errors/index.js +16 -0
- package/src/errors/index.js.map +1 -0
- package/src/errors/invalid-backup-code-version.d.ts +19 -0
- package/src/errors/invalid-backup-code-version.d.ts.map +1 -0
- package/src/errors/invalid-backup-code-version.js +29 -0
- package/src/errors/invalid-backup-code-version.js.map +1 -0
- package/src/errors/invalid-jwt-token.d.ts +17 -0
- package/src/errors/invalid-jwt-token.d.ts.map +1 -0
- package/src/errors/invalid-jwt-token.js +24 -0
- package/src/errors/invalid-jwt-token.js.map +1 -0
- package/src/errors/invalid-model.d.ts +18 -0
- package/src/errors/invalid-model.d.ts.map +1 -0
- package/src/errors/invalid-model.js +26 -0
- package/src/errors/invalid-model.js.map +1 -0
- package/src/errors/invalid-new-password.d.ts +19 -0
- package/src/errors/invalid-new-password.d.ts.map +1 -0
- package/src/errors/invalid-new-password.js +28 -0
- package/src/errors/invalid-new-password.js.map +1 -0
- package/src/errors/invalid-password.d.ts +19 -0
- package/src/errors/invalid-password.d.ts.map +1 -0
- package/src/errors/invalid-password.js +28 -0
- package/src/errors/invalid-password.js.map +1 -0
- package/src/errors/missing-validated-data.d.ts +24 -0
- package/src/errors/missing-validated-data.d.ts.map +1 -0
- package/src/errors/missing-validated-data.js +53 -0
- package/src/errors/missing-validated-data.js.map +1 -0
- package/src/errors/mnemonic-or-password-required.d.ts +17 -0
- package/src/errors/mnemonic-or-password-required.d.ts.map +1 -0
- package/src/errors/mnemonic-or-password-required.js +26 -0
- package/src/errors/mnemonic-or-password-required.js.map +1 -0
- package/src/errors/model-not-registered.d.ts +18 -0
- package/src/errors/model-not-registered.d.ts.map +1 -0
- package/src/errors/model-not-registered.js +26 -0
- package/src/errors/model-not-registered.js.map +1 -0
- package/src/errors/mongoose-validation.d.ts +28 -0
- package/src/errors/mongoose-validation.d.ts.map +1 -0
- package/src/errors/mongoose-validation.js +33 -0
- package/src/errors/mongoose-validation.js.map +1 -0
- package/src/errors/symmetric.d.ts +23 -0
- package/src/errors/symmetric.d.ts.map +1 -0
- package/src/errors/symmetric.js +37 -0
- package/src/errors/symmetric.js.map +1 -0
- package/src/errors/token-expired.d.ts +17 -0
- package/src/errors/token-expired.d.ts.map +1 -0
- package/src/errors/token-expired.js +24 -0
- package/src/errors/token-expired.js.map +1 -0
- package/src/get-language.d.ts +12 -0
- package/src/get-language.d.ts.map +1 -0
- package/src/get-language.js +40 -0
- package/src/get-language.js.map +1 -0
- package/src/get-timezone.d.ts +12 -0
- package/src/get-timezone.d.ts.map +1 -0
- package/src/get-timezone.js +53 -0
- package/src/get-timezone.js.map +1 -0
- package/src/{index.ts → index.d.ts} +2 -44
- package/src/index.d.ts.map +1 -0
- package/src/index.js +80 -0
- package/src/index.js.map +1 -0
- package/src/interfaces/{api-error-response.ts → api-error-response.d.ts} +2 -3
- package/src/interfaces/api-error-response.d.ts.map +1 -0
- package/src/interfaces/api-error-response.js +8 -0
- package/src/interfaces/api-error-response.js.map +1 -0
- package/src/interfaces/{api-express-validation-error-response.ts → api-express-validation-error-response.d.ts} +3 -4
- package/src/interfaces/api-express-validation-error-response.d.ts.map +1 -0
- package/src/interfaces/api-express-validation-error-response.js +8 -0
- package/src/interfaces/api-express-validation-error-response.js.map +1 -0
- package/src/interfaces/{api-message-response.ts → api-message-response.d.ts} +2 -2
- package/src/interfaces/api-message-response.d.ts.map +1 -0
- package/src/interfaces/api-message-response.js +8 -0
- package/src/interfaces/api-message-response.js.map +1 -0
- package/src/interfaces/{api-mongo-validation-error-response.ts → api-mongo-validation-error-response.d.ts} +2 -3
- package/src/interfaces/api-mongo-validation-error-response.d.ts.map +1 -0
- package/src/interfaces/api-mongo-validation-error-response.js +8 -0
- package/src/interfaces/api-mongo-validation-error-response.js.map +1 -0
- package/src/interfaces/api-responses/{backup-codes-response.ts → backup-codes-response.d.ts} +2 -3
- package/src/interfaces/api-responses/backup-codes-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/backup-codes-response.js +8 -0
- package/src/interfaces/api-responses/backup-codes-response.js.map +1 -0
- package/src/interfaces/api-responses/{challenge-response.ts → challenge-response.d.ts} +5 -6
- package/src/interfaces/api-responses/challenge-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/challenge-response.js +7 -0
- package/src/interfaces/api-responses/challenge-response.js.map +1 -0
- package/src/interfaces/api-responses/{code-count-response.ts → code-count-response.d.ts} +2 -3
- package/src/interfaces/api-responses/code-count-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/code-count-response.js +8 -0
- package/src/interfaces/api-responses/code-count-response.js.map +1 -0
- package/src/interfaces/api-responses/{index.ts → index.d.ts} +1 -0
- package/src/interfaces/api-responses/index.d.ts.map +1 -0
- package/src/interfaces/api-responses/index.js +12 -0
- package/src/interfaces/api-responses/index.js.map +1 -0
- package/src/interfaces/api-responses/{login-response.ts → login-response.d.ts} +4 -5
- package/src/interfaces/api-responses/login-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/login-response.js +8 -0
- package/src/interfaces/api-responses/login-response.js.map +1 -0
- package/src/interfaces/api-responses/{mnemonic-response.ts → mnemonic-response.d.ts} +3 -4
- package/src/interfaces/api-responses/mnemonic-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/mnemonic-response.js +7 -0
- package/src/interfaces/api-responses/mnemonic-response.js.map +1 -0
- package/src/interfaces/api-responses/{registration-response.ts → registration-response.d.ts} +5 -6
- package/src/interfaces/api-responses/registration-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/registration-response.js +7 -0
- package/src/interfaces/api-responses/registration-response.js.map +1 -0
- package/src/interfaces/api-responses/{request-user-response.ts → request-user-response.d.ts} +2 -3
- package/src/interfaces/api-responses/request-user-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/request-user-response.js +8 -0
- package/src/interfaces/api-responses/request-user-response.js.map +1 -0
- package/src/interfaces/api-responses/{user-settings-response.ts → user-settings-response.d.ts} +9 -10
- package/src/interfaces/api-responses/user-settings-response.d.ts.map +1 -0
- package/src/interfaces/api-responses/user-settings-response.js +8 -0
- package/src/interfaces/api-responses/user-settings-response.js.map +1 -0
- package/src/interfaces/application.d.ts +39 -0
- package/src/interfaces/application.d.ts.map +1 -0
- package/src/interfaces/application.js +8 -0
- package/src/interfaces/application.js.map +1 -0
- package/src/interfaces/backend-objects/{email-token.ts → email-token.d.ts} +3 -8
- package/src/interfaces/backend-objects/email-token.d.ts.map +1 -0
- package/src/interfaces/backend-objects/email-token.js +8 -0
- package/src/interfaces/backend-objects/email-token.js.map +1 -0
- package/src/interfaces/backend-objects/{index.ts → index.d.ts} +1 -0
- package/src/interfaces/backend-objects/index.d.ts.map +1 -0
- package/src/interfaces/backend-objects/index.js +8 -0
- package/src/interfaces/backend-objects/index.js.map +1 -0
- package/src/interfaces/backend-objects/{request-user.ts → request-user.d.ts} +2 -6
- package/src/interfaces/backend-objects/request-user.d.ts.map +1 -0
- package/src/interfaces/backend-objects/request-user.js +8 -0
- package/src/interfaces/backend-objects/request-user.js.map +1 -0
- package/src/interfaces/backend-objects/{role.ts → role.d.ts} +2 -7
- package/src/interfaces/backend-objects/role.d.ts.map +1 -0
- package/src/interfaces/backend-objects/role.js +8 -0
- package/src/interfaces/backend-objects/role.js.map +1 -0
- package/src/interfaces/backend-objects/{user.ts → user.d.ts} +2 -6
- package/src/interfaces/backend-objects/user.d.ts.map +1 -0
- package/src/interfaces/backend-objects/user.js +8 -0
- package/src/interfaces/backend-objects/user.js.map +1 -0
- package/src/interfaces/{checksum-config.ts → checksum-config.d.ts} +3 -3
- package/src/interfaces/checksum-config.d.ts.map +1 -0
- package/src/interfaces/checksum-config.js +8 -0
- package/src/interfaces/checksum-config.js.map +1 -0
- package/src/interfaces/checksum-consts.d.ts +20 -0
- package/src/interfaces/checksum-consts.d.ts.map +1 -0
- package/src/interfaces/checksum-consts.js +8 -0
- package/src/interfaces/checksum-consts.js.map +1 -0
- package/src/interfaces/constants.d.ts +66 -0
- package/src/interfaces/constants.d.ts.map +1 -0
- package/src/interfaces/constants.js +8 -0
- package/src/interfaces/constants.js.map +1 -0
- package/src/interfaces/{controller-config.ts → controller-config.d.ts} +15 -31
- package/src/interfaces/controller-config.d.ts.map +1 -0
- package/src/interfaces/controller-config.js +8 -0
- package/src/interfaces/controller-config.js.map +1 -0
- package/src/interfaces/{create-user-basics.ts → create-user-basics.d.ts} +13 -13
- package/src/interfaces/create-user-basics.d.ts.map +1 -0
- package/src/interfaces/create-user-basics.js +8 -0
- package/src/interfaces/create-user-basics.js.map +1 -0
- package/src/interfaces/{csp-config.ts → csp-config.d.ts} +5 -15
- package/src/interfaces/csp-config.d.ts.map +1 -0
- package/src/interfaces/csp-config.js +23 -0
- package/src/interfaces/csp-config.js.map +1 -0
- package/src/interfaces/{csp-definition.ts → csp-definition.d.ts} +9 -46
- package/src/interfaces/csp-definition.d.ts.map +1 -0
- package/src/interfaces/csp-definition.js +32 -0
- package/src/interfaces/csp-definition.js.map +1 -0
- package/src/interfaces/{db-init-result.ts → db-init-result.d.ts} +2 -3
- package/src/interfaces/db-init-result.d.ts.map +1 -0
- package/src/interfaces/db-init-result.js +8 -0
- package/src/interfaces/db-init-result.js.map +1 -0
- package/src/interfaces/{deep-partial.ts → deep-partial.d.ts} +2 -2
- package/src/interfaces/deep-partial.d.ts.map +1 -0
- package/src/interfaces/deep-partial.js +8 -0
- package/src/interfaces/deep-partial.js.map +1 -0
- package/src/interfaces/{discriminator-collections.ts → discriminator-collections.d.ts} +4 -8
- package/src/interfaces/discriminator-collections.d.ts.map +1 -0
- package/src/interfaces/discriminator-collections.js +8 -0
- package/src/interfaces/discriminator-collections.js.map +1 -0
- package/src/interfaces/email-service.d.ts +21 -0
- package/src/interfaces/email-service.d.ts.map +1 -0
- package/src/interfaces/email-service.js +8 -0
- package/src/interfaces/email-service.js.map +1 -0
- package/src/interfaces/environment-mongo.d.ts +85 -0
- package/src/interfaces/environment-mongo.d.ts.map +1 -0
- package/src/interfaces/environment-mongo.js +8 -0
- package/src/interfaces/environment-mongo.js.map +1 -0
- package/src/interfaces/environment.d.ts +190 -0
- package/src/interfaces/environment.d.ts.map +1 -0
- package/src/interfaces/environment.js +8 -0
- package/src/interfaces/environment.js.map +1 -0
- package/src/interfaces/{failable-result.ts → failable-result.d.ts} +5 -5
- package/src/interfaces/failable-result.d.ts.map +1 -0
- package/src/interfaces/failable-result.js +8 -0
- package/src/interfaces/failable-result.js.map +1 -0
- package/src/interfaces/{fec-consts.ts → fec-consts.d.ts} +3 -3
- package/src/interfaces/fec-consts.d.ts.map +1 -0
- package/src/interfaces/fec-consts.js +8 -0
- package/src/interfaces/fec-consts.js.map +1 -0
- package/src/interfaces/{flexible-csp.ts → flexible-csp.d.ts} +5 -16
- package/src/interfaces/flexible-csp.d.ts.map +1 -0
- package/src/interfaces/flexible-csp.js +24 -0
- package/src/interfaces/flexible-csp.js.map +1 -0
- package/src/interfaces/{handleable-error-options.ts → handleable-error-options.d.ts} +5 -5
- package/src/interfaces/handleable-error-options.d.ts.map +1 -0
- package/src/interfaces/handleable-error-options.js +8 -0
- package/src/interfaces/handleable-error-options.js.map +1 -0
- package/src/interfaces/{index.ts → index.d.ts} +1 -0
- package/src/interfaces/index.d.ts.map +1 -0
- package/src/interfaces/index.js +38 -0
- package/src/interfaces/index.js.map +1 -0
- package/src/interfaces/jwt-consts.d.ts +20 -0
- package/src/interfaces/jwt-consts.d.ts.map +1 -0
- package/src/interfaces/jwt-consts.js +8 -0
- package/src/interfaces/jwt-consts.js.map +1 -0
- package/src/interfaces/{jwt-sign-response.ts → jwt-sign-response.d.ts} +9 -18
- package/src/interfaces/jwt-sign-response.d.ts.map +1 -0
- package/src/interfaces/jwt-sign-response.js +8 -0
- package/src/interfaces/jwt-sign-response.js.map +1 -0
- package/src/interfaces/models/{email-token.ts → email-token.d.ts} +1 -2
- package/src/interfaces/models/email-token.d.ts.map +1 -0
- package/src/interfaces/models/email-token.js +8 -0
- package/src/interfaces/models/email-token.js.map +1 -0
- package/src/interfaces/models/{index.ts → index.d.ts} +1 -0
- package/src/interfaces/models/index.d.ts.map +1 -0
- package/src/interfaces/models/index.js +11 -0
- package/src/interfaces/models/index.js.map +1 -0
- package/src/interfaces/models/{mnemonic.ts → mnemonic.d.ts} +1 -2
- package/src/interfaces/models/mnemonic.d.ts.map +1 -0
- package/src/interfaces/models/mnemonic.js +8 -0
- package/src/interfaces/models/mnemonic.js.map +1 -0
- package/src/interfaces/models/{role.ts → role.d.ts} +1 -2
- package/src/interfaces/models/role.d.ts.map +1 -0
- package/src/interfaces/models/role.js +8 -0
- package/src/interfaces/models/role.js.map +1 -0
- package/src/interfaces/models/{token-role.ts → token-role.d.ts} +2 -6
- package/src/interfaces/models/token-role.d.ts.map +1 -0
- package/src/interfaces/models/token-role.js +8 -0
- package/src/interfaces/models/token-role.js.map +1 -0
- package/src/interfaces/models/{used-direct-login-token.ts → used-direct-login-token.d.ts} +2 -4
- package/src/interfaces/models/used-direct-login-token.d.ts.map +1 -0
- package/src/interfaces/models/used-direct-login-token.js +8 -0
- package/src/interfaces/models/used-direct-login-token.js.map +1 -0
- package/src/interfaces/models/{user-role.ts → user-role.d.ts} +2 -6
- package/src/interfaces/models/user-role.d.ts.map +1 -0
- package/src/interfaces/models/user-role.js +8 -0
- package/src/interfaces/models/user-role.js.map +1 -0
- package/src/interfaces/models/{user.ts → user.d.ts} +3 -12
- package/src/interfaces/models/user.d.ts.map +1 -0
- package/src/interfaces/models/user.js +8 -0
- package/src/interfaces/models/user.js.map +1 -0
- package/src/interfaces/{mongo-errors.ts → mongo-errors.d.ts} +2 -3
- package/src/interfaces/mongo-errors.d.ts.map +1 -0
- package/src/interfaces/mongo-errors.js +8 -0
- package/src/interfaces/mongo-errors.js.map +1 -0
- package/src/interfaces/request-user.d.ts +67 -0
- package/src/interfaces/request-user.d.ts.map +1 -0
- package/src/interfaces/request-user.js +8 -0
- package/src/interfaces/request-user.js.map +1 -0
- package/src/interfaces/required-string-keys.d.ts +28 -0
- package/src/interfaces/required-string-keys.d.ts.map +1 -0
- package/src/interfaces/required-string-keys.js +8 -0
- package/src/interfaces/required-string-keys.js.map +1 -0
- package/src/interfaces/{schema.ts → schema.d.ts} +22 -28
- package/src/interfaces/schema.d.ts.map +1 -0
- package/src/interfaces/schema.js +8 -0
- package/src/interfaces/schema.js.map +1 -0
- package/src/interfaces/server-init-result.d.ts +45 -0
- package/src/interfaces/server-init-result.d.ts.map +1 -0
- package/src/interfaces/server-init-result.js +8 -0
- package/src/interfaces/server-init-result.js.map +1 -0
- package/src/interfaces/{status-code-response.ts → status-code-response.d.ts} +4 -5
- package/src/interfaces/status-code-response.d.ts.map +1 -0
- package/src/interfaces/status-code-response.js +8 -0
- package/src/interfaces/status-code-response.js.map +1 -0
- package/src/interfaces/{symmetric-encryption-results.ts → symmetric-encryption-results.d.ts} +3 -3
- package/src/interfaces/symmetric-encryption-results.d.ts.map +1 -1
- package/src/interfaces/symmetric-encryption-results.js +5 -0
- package/src/interfaces/symmetric-encryption-results.js.map +1 -1
- package/src/interfaces/{test-environment.ts → test-environment.d.ts} +6 -7
- package/src/interfaces/test-environment.d.ts.map +1 -0
- package/src/interfaces/test-environment.js +8 -0
- package/src/interfaces/test-environment.js.map +1 -0
- package/src/interfaces/{token-response.ts → token-response.d.ts} +2 -3
- package/src/interfaces/token-response.d.ts.map +1 -0
- package/src/interfaces/token-response.js +8 -0
- package/src/interfaces/token-response.js.map +1 -0
- package/src/middleware-utils.d.ts +31 -0
- package/src/middleware-utils.d.ts.map +1 -0
- package/src/middleware-utils.js +117 -0
- package/src/middleware-utils.js.map +1 -0
- package/src/middlewares/authenticate-crypto.d.ts +27 -0
- package/src/middlewares/authenticate-crypto.d.ts.map +1 -0
- package/src/middlewares/authenticate-crypto.js +143 -0
- package/src/middlewares/authenticate-crypto.js.map +1 -0
- package/src/middlewares/authenticate-token.d.ts +34 -0
- package/src/middlewares/authenticate-token.d.ts.map +1 -0
- package/src/middlewares/authenticate-token.js +117 -0
- package/src/middlewares/authenticate-token.js.map +1 -0
- package/src/middlewares/cleanup-crypto.d.ts +16 -0
- package/src/middlewares/cleanup-crypto.d.ts.map +1 -0
- package/src/middlewares/cleanup-crypto.js +41 -0
- package/src/middlewares/cleanup-crypto.js.map +1 -0
- package/src/middlewares/{index.ts → index.d.ts} +1 -0
- package/src/middlewares/index.d.ts.map +1 -0
- package/src/middlewares/index.js +8 -0
- package/src/middlewares/index.js.map +1 -0
- package/src/middlewares/{set-global-context-language.ts → set-global-context-language.d.ts} +2 -24
- package/src/middlewares/set-global-context-language.d.ts.map +1 -0
- package/src/middlewares/set-global-context-language.js +27 -0
- package/src/middlewares/set-global-context-language.js.map +1 -0
- package/src/model-registry.d.ts +79 -0
- package/src/model-registry.d.ts.map +1 -0
- package/src/model-registry.js +97 -0
- package/src/model-registry.js.map +1 -0
- package/src/models/{email-token.ts → email-token.d.ts} +2 -27
- package/src/models/email-token.d.ts.map +1 -0
- package/src/models/email-token.js +16 -0
- package/src/models/email-token.js.map +1 -0
- package/src/models/{index.ts → index.d.ts} +1 -0
- package/src/models/index.d.ts.map +1 -0
- package/src/models/index.js +10 -0
- package/src/models/index.js.map +1 -0
- package/src/models/{mnemonic.ts → mnemonic.d.ts} +2 -20
- package/src/models/mnemonic.d.ts.map +1 -0
- package/src/models/mnemonic.js +27 -0
- package/src/models/mnemonic.js.map +1 -0
- package/src/models/{role.ts → role.d.ts} +2 -16
- package/src/models/role.d.ts.map +1 -0
- package/src/models/role.js +27 -0
- package/src/models/role.js.map +1 -0
- package/src/models/{used-direct-login-token.ts → used-direct-login-token.d.ts} +2 -27
- package/src/models/used-direct-login-token.d.ts.map +1 -0
- package/src/models/used-direct-login-token.js +16 -0
- package/src/models/used-direct-login-token.js.map +1 -0
- package/src/models/{user-role.ts → user-role.d.ts} +2 -19
- package/src/models/user-role.d.ts.map +1 -0
- package/src/models/user-role.js +26 -0
- package/src/models/user-role.js.map +1 -0
- package/src/models/{user.ts → user.d.ts} +2 -20
- package/src/models/user.d.ts.map +1 -0
- package/src/models/user.js +27 -0
- package/src/models/user.js.map +1 -0
- package/src/pipeline/{index.ts → index.d.ts} +1 -0
- package/src/pipeline/index.d.ts.map +1 -0
- package/src/pipeline/index.js +5 -0
- package/src/pipeline/index.js.map +1 -0
- package/src/pipeline/pipeline-builder.d.ts +16 -0
- package/src/pipeline/pipeline-builder.d.ts.map +1 -0
- package/src/pipeline/pipeline-builder.js +26 -0
- package/src/pipeline/pipeline-builder.js.map +1 -0
- package/src/plugins/{index.ts → index.d.ts} +1 -0
- package/src/plugins/index.d.ts.map +1 -0
- package/src/plugins/index.js +6 -0
- package/src/plugins/index.js.map +1 -0
- package/src/plugins/{plugin-interface.ts → plugin-interface.d.ts} +5 -6
- package/src/plugins/plugin-interface.d.ts.map +1 -0
- package/src/plugins/plugin-interface.js +8 -0
- package/src/plugins/plugin-interface.js.map +1 -0
- package/src/plugins/plugin-manager.d.ts +22 -0
- package/src/plugins/plugin-manager.d.ts.map +1 -0
- package/src/plugins/plugin-manager.js +46 -0
- package/src/plugins/plugin-manager.js.map +1 -0
- package/src/registry/email-service-registry.d.ts +49 -0
- package/src/registry/email-service-registry.d.ts.map +1 -0
- package/src/registry/email-service-registry.js +64 -0
- package/src/registry/email-service-registry.js.map +1 -0
- package/src/registry/{index.ts → index.d.ts} +1 -0
- package/src/registry/index.d.ts.map +1 -0
- package/src/registry/index.js +6 -0
- package/src/registry/index.js.map +1 -0
- package/src/responses/{index.ts → index.d.ts} +1 -0
- package/src/responses/index.d.ts.map +1 -0
- package/src/responses/index.js +5 -0
- package/src/responses/index.js.map +1 -0
- package/src/responses/response-builder.d.ts +103 -0
- package/src/responses/response-builder.d.ts.map +1 -0
- package/src/responses/response-builder.js +142 -0
- package/src/responses/response-builder.js.map +1 -0
- package/src/routers/api.d.ts +59 -0
- package/src/routers/api.d.ts.map +1 -0
- package/src/routers/api.js +110 -0
- package/src/routers/api.js.map +1 -0
- package/src/routers/app.d.ts +87 -0
- package/src/routers/app.d.ts.map +1 -0
- package/src/routers/app.js +285 -0
- package/src/routers/app.js.map +1 -0
- package/src/routers/{base.ts → base.d.ts} +11 -19
- package/src/routers/base.d.ts.map +1 -0
- package/src/routers/base.js +31 -0
- package/src/routers/base.js.map +1 -0
- package/src/routers/{index.ts → index.d.ts} +1 -0
- package/src/routers/index.d.ts.map +1 -0
- package/src/routers/index.js +7 -0
- package/src/routers/index.js.map +1 -0
- package/src/routers/router-config.d.ts +35 -0
- package/src/routers/router-config.d.ts.map +1 -0
- package/src/routers/router-config.js +16 -0
- package/src/routers/router-config.js.map +1 -0
- package/src/routing/index.d.ts +2 -0
- package/src/routing/index.d.ts.map +1 -0
- package/src/routing/index.js +5 -0
- package/src/routing/index.js.map +1 -0
- package/src/routing/route-builder.d.ts +121 -0
- package/src/routing/route-builder.d.ts.map +1 -0
- package/src/routing/route-builder.js +167 -0
- package/src/routing/route-builder.js.map +1 -0
- package/src/schemas/email-token.d.ts +65 -0
- package/src/schemas/email-token.d.ts.map +1 -0
- package/src/schemas/email-token.js +68 -0
- package/src/schemas/email-token.js.map +1 -0
- package/src/schemas/{index.ts → index.d.ts} +1 -0
- package/src/schemas/index.d.ts.map +1 -0
- package/src/schemas/index.js +11 -0
- package/src/schemas/index.js.map +1 -0
- package/src/schemas/mnemonic.d.ts +37 -0
- package/src/schemas/mnemonic.d.ts.map +1 -0
- package/src/schemas/mnemonic.js +41 -0
- package/src/schemas/mnemonic.js.map +1 -0
- package/src/schemas/role.d.ts +57 -0
- package/src/schemas/role.d.ts.map +1 -0
- package/src/schemas/role.js +102 -0
- package/src/schemas/role.js.map +1 -0
- package/src/schemas/schema.d.ts +62 -0
- package/src/schemas/schema.d.ts.map +1 -0
- package/src/schemas/schema.js +81 -0
- package/src/schemas/schema.js.map +1 -0
- package/src/schemas/used-direct-login-token.d.ts +49 -0
- package/src/schemas/used-direct-login-token.d.ts.map +1 -0
- package/src/schemas/used-direct-login-token.js +35 -0
- package/src/schemas/used-direct-login-token.js.map +1 -0
- package/src/schemas/user-role.d.ts +52 -0
- package/src/schemas/user-role.d.ts.map +1 -0
- package/src/schemas/user-role.js +67 -0
- package/src/schemas/user-role.js.map +1 -0
- package/src/schemas/user.d.ts +43 -0
- package/src/schemas/user.d.ts.map +1 -0
- package/src/schemas/user.js +214 -0
- package/src/schemas/user.js.map +1 -0
- package/src/services/backup-code.d.ts +80 -0
- package/src/services/backup-code.d.ts.map +1 -0
- package/src/services/backup-code.js +189 -0
- package/src/services/backup-code.js.map +1 -0
- package/src/services/base.d.ts +22 -0
- package/src/services/base.d.ts.map +1 -0
- package/src/services/base.js +26 -0
- package/src/services/base.js.map +1 -0
- package/src/services/checksum.d.ts +90 -0
- package/src/services/checksum.d.ts.map +1 -0
- package/src/services/checksum.js +166 -0
- package/src/services/checksum.js.map +1 -0
- package/src/services/database-initialization.d.ts +138 -0
- package/src/services/database-initialization.d.ts.map +1 -0
- package/src/services/database-initialization.js +904 -0
- package/src/services/database-initialization.js.map +1 -0
- package/src/services/{db-init-cache.ts → db-init-cache.d.ts} +6 -16
- package/src/services/db-init-cache.d.ts.map +1 -0
- package/src/services/db-init-cache.js +7 -0
- package/src/services/db-init-cache.js.map +1 -0
- package/src/services/direct-login-token.d.ts +28 -0
- package/src/services/direct-login-token.d.ts.map +1 -0
- package/src/services/direct-login-token.js +62 -0
- package/src/services/direct-login-token.js.map +1 -0
- package/src/services/dummy-email-service.d.ts +30 -0
- package/src/services/dummy-email-service.d.ts.map +1 -0
- package/src/services/dummy-email-service.js +35 -0
- package/src/services/dummy-email-service.js.map +1 -0
- package/src/services/fec-usage-example.d.ts +58 -0
- package/src/services/fec-usage-example.d.ts.map +1 -0
- package/src/services/fec-usage-example.js +95 -0
- package/src/services/fec-usage-example.js.map +1 -0
- package/src/services/fec.d.ts +88 -0
- package/src/services/fec.d.ts.map +1 -0
- package/src/services/fec.js +246 -0
- package/src/services/fec.js.map +1 -0
- package/src/services/{index.ts → index.d.ts} +1 -0
- package/src/services/index.d.ts.map +1 -0
- package/src/services/index.js +22 -0
- package/src/services/index.js.map +1 -0
- package/src/services/jwt.d.ts +45 -0
- package/src/services/jwt.d.ts.map +1 -0
- package/src/services/jwt.js +105 -0
- package/src/services/jwt.js.map +1 -0
- package/src/services/key-wrapping.d.ts +139 -0
- package/src/services/key-wrapping.d.ts.map +1 -0
- package/src/services/key-wrapping.js +372 -0
- package/src/services/key-wrapping.js.map +1 -0
- package/src/services/mnemonic.d.ts +68 -0
- package/src/services/mnemonic.d.ts.map +1 -0
- package/src/services/mnemonic.js +120 -0
- package/src/services/mnemonic.js.map +1 -0
- package/src/services/request-user.d.ts +45 -0
- package/src/services/request-user.d.ts.map +1 -0
- package/src/services/request-user.js +90 -0
- package/src/services/request-user.js.map +1 -0
- package/src/services/role.d.ts +97 -0
- package/src/services/role.d.ts.map +1 -0
- package/src/services/role.js +289 -0
- package/src/services/role.js.map +1 -0
- package/src/services/symmetric.d.ts +60 -0
- package/src/services/symmetric.d.ts.map +1 -0
- package/src/services/symmetric.js +125 -0
- package/src/services/symmetric.js.map +1 -0
- package/src/services/system-user.d.ts +22 -0
- package/src/services/system-user.d.ts.map +1 -0
- package/src/services/system-user.js +52 -0
- package/src/services/system-user.js.map +1 -0
- package/src/services/user.d.ts +368 -0
- package/src/services/user.d.ts.map +1 -0
- package/src/services/user.js +1470 -0
- package/src/services/user.js.map +1 -0
- package/src/services/xor.d.ts +28 -0
- package/src/services/xor.d.ts.map +1 -0
- package/src/services/xor.js +45 -0
- package/src/services/xor.js.map +1 -0
- package/src/{testing.ts → testing.d.ts} +1 -2
- package/src/testing.d.ts.map +1 -0
- package/src/testing.js +12 -0
- package/src/testing.js.map +1 -0
- package/src/transactions/{index.ts → index.d.ts} +1 -0
- package/src/transactions/index.d.ts.map +1 -0
- package/src/transactions/index.js +5 -0
- package/src/transactions/index.js.map +1 -0
- package/src/transactions/transaction-manager.d.ts +37 -0
- package/src/transactions/transaction-manager.d.ts.map +1 -0
- package/src/transactions/transaction-manager.js +50 -0
- package/src/transactions/transaction-manager.js.map +1 -0
- package/src/types/{app-config.ts → app-config.d.ts} +10 -16
- package/src/types/app-config.d.ts.map +1 -0
- package/src/types/app-config.js +8 -0
- package/src/types/app-config.js.map +1 -0
- package/src/types/{controller-config.ts → controller-config.d.ts} +7 -9
- package/src/types/controller-config.d.ts.map +1 -0
- package/src/types/controller-config.js +8 -0
- package/src/types/controller-config.js.map +1 -0
- package/src/types/{environment-variables.ts → environment-variables.d.ts} +5 -27
- package/src/types/environment-variables.d.ts.map +1 -0
- package/src/types/environment-variables.js +41 -0
- package/src/types/environment-variables.js.map +1 -0
- package/src/types/{index.ts → index.d.ts} +1 -0
- package/src/types/index.d.ts.map +1 -0
- package/src/types/index.js +6 -0
- package/src/types/index.js.map +1 -0
- package/src/types/{mongoose-helpers.ts → mongoose-helpers.d.ts} +2 -3
- package/src/types/mongoose-helpers.d.ts.map +1 -0
- package/src/types/mongoose-helpers.js +8 -0
- package/src/types/mongoose-helpers.js.map +1 -0
- package/src/types.d.ts +118 -0
- package/src/types.d.ts.map +1 -0
- package/src/types.js +28 -0
- package/src/types.js.map +1 -0
- package/src/utils.d.ts +240 -0
- package/src/utils.d.ts.map +1 -0
- package/src/utils.js +843 -0
- package/src/utils.js.map +1 -0
- package/src/validation/{index.ts → index.d.ts} +1 -0
- package/src/validation/index.d.ts.map +1 -0
- package/src/validation/index.js +5 -0
- package/src/validation/index.js.map +1 -0
- package/src/validation/validation-builder.d.ts +71 -0
- package/src/validation/validation-builder.d.ts.map +1 -0
- package/src/validation/validation-builder.js +120 -0
- package/src/validation/validation-builder.js.map +1 -0
- package/LICENSE +0 -21
- package/src/__tests__/fixtures/model-mocks.mock.ts +0 -164
- package/src/__tests__/helpers/application.mock.ts +0 -89
- package/src/__tests__/helpers/setup-test-env.ts +0 -202
- package/src/application-base.ts +0 -548
- package/src/application-concrete.ts +0 -62
- package/src/application.ts +0 -330
- package/src/backup-code.ts +0 -348
- package/src/builders/application-builder.ts +0 -147
- package/src/constants.ts +0 -89
- package/src/container/service-container.ts +0 -85
- package/src/controllers/base.ts +0 -512
- package/src/controllers/user.ts +0 -1734
- package/src/decorators/base-controller.ts +0 -91
- package/src/decorators/controller.ts +0 -152
- package/src/decorators/zod-validation.ts +0 -64
- package/src/defaults.ts +0 -259
- package/src/enumerations/base-model-name.ts +0 -47
- package/src/enumerations/schema-collection.ts +0 -39
- package/src/environment.ts +0 -859
- package/src/errors/express-validation.ts +0 -38
- package/src/errors/invalid-backup-code-version.ts +0 -30
- package/src/errors/invalid-jwt-token.ts +0 -24
- package/src/errors/invalid-model.ts +0 -24
- package/src/errors/invalid-new-password.ts +0 -33
- package/src/errors/invalid-password.ts +0 -28
- package/src/errors/missing-validated-data.ts +0 -55
- package/src/errors/mnemonic-or-password-required.ts +0 -26
- package/src/errors/model-not-registered.ts +0 -24
- package/src/errors/mongoose-validation.ts +0 -56
- package/src/errors/symmetric.ts +0 -53
- package/src/errors/token-expired.ts +0 -24
- package/src/get-language.ts +0 -64
- package/src/get-timezone.ts +0 -76
- package/src/interfaces/application.ts +0 -40
- package/src/interfaces/checksum-consts.ts +0 -23
- package/src/interfaces/constants.ts +0 -114
- package/src/interfaces/email-service.ts +0 -26
- package/src/interfaces/environment-mongo.ts +0 -86
- package/src/interfaces/environment.ts +0 -191
- package/src/interfaces/jwt-consts.ts +0 -33
- package/src/interfaces/request-user.ts +0 -80
- package/src/interfaces/required-string-keys.ts +0 -33
- package/src/interfaces/server-init-result.ts +0 -48
- package/src/middleware-utils.ts +0 -138
- package/src/middlewares/authenticate-crypto.ts +0 -237
- package/src/middlewares/authenticate-token.ts +0 -165
- package/src/middlewares/cleanup-crypto.ts +0 -47
- package/src/model-registry.ts +0 -142
- package/src/pipeline/pipeline-builder.ts +0 -27
- package/src/plugins/plugin-manager.ts +0 -53
- package/src/registry/email-service-registry.ts +0 -76
- package/src/responses/response-builder.ts +0 -166
- package/src/routers/api.ts +0 -233
- package/src/routers/app.ts +0 -395
- package/src/routers/router-config.ts +0 -34
- package/src/routing/index.ts +0 -1
- package/src/routing/route-builder.ts +0 -214
- package/src/schemas/email-token.ts +0 -112
- package/src/schemas/mnemonic.ts +0 -48
- package/src/schemas/role.ts +0 -153
- package/src/schemas/schema.ts +0 -185
- package/src/schemas/used-direct-login-token.ts +0 -58
- package/src/schemas/user-role.ts +0 -93
- package/src/schemas/user.ts +0 -244
- package/src/services/backup-code.ts +0 -327
- package/src/services/base.ts +0 -46
- package/src/services/checksum.ts +0 -189
- package/src/services/database-initialization.ts +0 -1653
- package/src/services/direct-login-token.ts +0 -83
- package/src/services/dummy-email-service.ts +0 -43
- package/src/services/fec-usage-example.ts +0 -123
- package/src/services/fec.ts +0 -399
- package/src/services/jwt.ts +0 -146
- package/src/services/key-wrapping.ts +0 -528
- package/src/services/mnemonic.ts +0 -174
- package/src/services/request-user.ts +0 -127
- package/src/services/role.ts +0 -417
- package/src/services/symmetric.ts +0 -164
- package/src/services/system-user.ts +0 -87
- package/src/services/user.ts +0 -2324
- package/src/services/xor.ts +0 -39
- package/src/transactions/transaction-manager.ts +0 -63
- package/src/types/mongoose-override.d.ts +0 -1
- package/src/types/mongoose.d.ts +0 -1
- package/src/types.ts +0 -189
- package/src/utils.ts +0 -1116
- package/src/validation/validation-builder.ts +0 -155
package/src/utils.ts
DELETED
|
@@ -1,1116 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Utility functions for Express application including validation, transactions, error handling, and data encoding.
|
|
3
|
-
* Provides comprehensive helpers for API responses, MongoDB transactions, validation, and cryptographic operations.
|
|
4
|
-
* @module utils
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { I18nEngine } from '@digitaldefiance/i18n-lib';
|
|
8
|
-
import {
|
|
9
|
-
ClientSession,
|
|
10
|
-
Connection,
|
|
11
|
-
Types,
|
|
12
|
-
} from '@digitaldefiance/mongoose-types';
|
|
13
|
-
import { NextFunction, Request, Response } from 'express';
|
|
14
|
-
import { Result, ValidationError } from 'express-validator';
|
|
15
|
-
import { existsSync, readdirSync, writeSync } from 'fs';
|
|
16
|
-
import { resolve } from 'path';
|
|
17
|
-
import { z, ZodType } from 'zod';
|
|
18
|
-
import { ExpressValidationError } from './errors/express-validation';
|
|
19
|
-
import { MongooseValidationError } from './errors/mongoose-validation';
|
|
20
|
-
import { IApiErrorResponse, IApplication } from './interfaces';
|
|
21
|
-
import { IApiExpressValidationErrorResponse } from './interfaces/api-express-validation-error-response';
|
|
22
|
-
import { IApiMongoValidationErrorResponse } from './interfaces/api-mongo-validation-error-response';
|
|
23
|
-
import { IMongoErrors } from './interfaces/mongo-errors';
|
|
24
|
-
import { RequiredStringKeys } from './interfaces/required-string-keys';
|
|
25
|
-
import { ApiResponse, SendFunction, TransactionCallback } from './types';
|
|
26
|
-
|
|
27
|
-
/** Debug message type for console output */
|
|
28
|
-
export type DEBUG_TYPE = 'error' | 'warn' | 'log';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Conditionally prints debug messages to console.
|
|
32
|
-
* @param debug Whether to print debug messages
|
|
33
|
-
* @param type Type of message (error, warn, or log)
|
|
34
|
-
* @param args Arguments to print
|
|
35
|
-
*/
|
|
36
|
-
export function debugLog(
|
|
37
|
-
debug: boolean,
|
|
38
|
-
type: DEBUG_TYPE = 'log',
|
|
39
|
-
...args: unknown[]
|
|
40
|
-
): void {
|
|
41
|
-
if (debug && type === 'error') {
|
|
42
|
-
console.error(...args);
|
|
43
|
-
} else if (debug && type === 'warn') {
|
|
44
|
-
console.warn(...args);
|
|
45
|
-
} else if (debug && type === 'log') {
|
|
46
|
-
console.log(...args);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Prints debug messages directly to stdout/stderr bypassing Node streams.
|
|
52
|
-
* Uses fs.writeSync to avoid Nx interception.
|
|
53
|
-
* @param debug Whether to print debug messages
|
|
54
|
-
* @param type Type of message (error, warn, or log)
|
|
55
|
-
* @param args Arguments to print
|
|
56
|
-
*/
|
|
57
|
-
export function directLog(
|
|
58
|
-
debug: boolean,
|
|
59
|
-
type: DEBUG_TYPE = 'log',
|
|
60
|
-
...args: any[]
|
|
61
|
-
): void {
|
|
62
|
-
if (!debug) return;
|
|
63
|
-
|
|
64
|
-
// Format the message
|
|
65
|
-
const message = args
|
|
66
|
-
.map((arg) =>
|
|
67
|
-
typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg),
|
|
68
|
-
)
|
|
69
|
-
.join(' ');
|
|
70
|
-
|
|
71
|
-
// Use fs.writeSync to write directly to the file descriptors
|
|
72
|
-
// This bypasses Node's stream handling and Nx's interception
|
|
73
|
-
const buffer = Buffer.from(message + '\n', 'utf8');
|
|
74
|
-
|
|
75
|
-
if (type === 'error' || type === 'warn') {
|
|
76
|
-
// File descriptor 2 is stderr
|
|
77
|
-
writeSync(2, buffer);
|
|
78
|
-
} else {
|
|
79
|
-
// File descriptor 1 is stdout
|
|
80
|
-
writeSync(1, buffer);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Gets value at a dotted path from an object.
|
|
86
|
-
* @param obj Object to traverse
|
|
87
|
-
* @param path Array of keys representing the path
|
|
88
|
-
* @returns Value at path or undefined
|
|
89
|
-
*/
|
|
90
|
-
export function getValueAtPath(
|
|
91
|
-
obj: unknown,
|
|
92
|
-
path: (string | number)[],
|
|
93
|
-
): unknown {
|
|
94
|
-
return path.reduce<unknown>((acc, key) => {
|
|
95
|
-
if (acc == null) return undefined;
|
|
96
|
-
try {
|
|
97
|
-
if (
|
|
98
|
-
typeof acc === 'object' &&
|
|
99
|
-
acc !== null &&
|
|
100
|
-
(typeof key === 'string' || typeof key === 'number')
|
|
101
|
-
) {
|
|
102
|
-
return (acc as Record<string | number, unknown>)[key];
|
|
103
|
-
}
|
|
104
|
-
return undefined;
|
|
105
|
-
} catch {
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
}, obj);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Maps Zod validation issues to express-validator ValidationError format.
|
|
113
|
-
* @param issues Zod validation issues
|
|
114
|
-
* @param source Source object being validated
|
|
115
|
-
* @param location Location of validation (body, query, params, etc.)
|
|
116
|
-
* @returns Array of express-validator ValidationErrors
|
|
117
|
-
*/
|
|
118
|
-
export function mapZodIssuesToValidationErrors(
|
|
119
|
-
issues: z.ZodError<unknown>['issues'],
|
|
120
|
-
source: unknown,
|
|
121
|
-
location: 'body' | 'cookies' | 'headers' | 'params' | 'query' = 'body',
|
|
122
|
-
): ValidationError[] {
|
|
123
|
-
return issues.map((issue) => ({
|
|
124
|
-
type: 'field',
|
|
125
|
-
location,
|
|
126
|
-
path: issue.path
|
|
127
|
-
.filter((p) => typeof p === 'string' || typeof p === 'number')
|
|
128
|
-
.join('.'),
|
|
129
|
-
value: getValueAtPath(source, issue.path as (string | number)[]),
|
|
130
|
-
msg: issue.message,
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Validates request body against Zod schema and executes callback with validated data.
|
|
136
|
-
* @template T Zod schema type
|
|
137
|
-
* @template TResult Callback return type
|
|
138
|
-
* @param req Express request
|
|
139
|
-
* @param schema Zod schema for validation
|
|
140
|
-
* @param callback Callback to execute with validated data
|
|
141
|
-
* @returns Promise resolving to callback result
|
|
142
|
-
* @throws {MissingValidatedDataError} If validated body is missing
|
|
143
|
-
* @throws {ExpressValidationError} If validation fails
|
|
144
|
-
*/
|
|
145
|
-
export async function requireValidatedFieldsAsync<
|
|
146
|
-
T extends ZodType<any, any, any>,
|
|
147
|
-
TResult = void,
|
|
148
|
-
>(
|
|
149
|
-
req: Request,
|
|
150
|
-
schema: T,
|
|
151
|
-
callback: (data: z.output<T>) => Promise<TResult>,
|
|
152
|
-
): Promise<TResult> {
|
|
153
|
-
if (req.validatedBody === undefined) {
|
|
154
|
-
throw new MissingValidatedDataError();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
try {
|
|
158
|
-
const validatedData = schema.parse(req.validatedBody) as z.output<T>;
|
|
159
|
-
return await callback(validatedData);
|
|
160
|
-
} catch (error) {
|
|
161
|
-
if (error instanceof z.ZodError) {
|
|
162
|
-
throw new ExpressValidationError(
|
|
163
|
-
mapZodIssuesToValidationErrors(
|
|
164
|
-
error.issues,
|
|
165
|
-
req.validatedBody ?? {},
|
|
166
|
-
'body',
|
|
167
|
-
),
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
throw error;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Checks if at least one of the required fields is present in validated body.
|
|
176
|
-
* @template T Callback return type
|
|
177
|
-
* @param req Express request
|
|
178
|
-
* @param fields Array of field names to check
|
|
179
|
-
* @param callback Callback to execute if validation passes
|
|
180
|
-
* @returns Promise resolving to callback result
|
|
181
|
-
* @throws {MissingValidatedDataError} If none of the fields are present
|
|
182
|
-
*/
|
|
183
|
-
export async function requireOneOfValidatedFieldsAsync<T = void>(
|
|
184
|
-
req: Request,
|
|
185
|
-
fields: string[],
|
|
186
|
-
callback: () => Promise<T>,
|
|
187
|
-
): Promise<T> {
|
|
188
|
-
if (req.validatedBody === undefined) {
|
|
189
|
-
throw new MissingValidatedDataError();
|
|
190
|
-
}
|
|
191
|
-
const validatedBody = req.validatedBody;
|
|
192
|
-
if (!fields.some((field) => validatedBody?.[field] !== undefined)) {
|
|
193
|
-
throw new MissingValidatedDataError(fields);
|
|
194
|
-
}
|
|
195
|
-
return await callback();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Validates required fields are present in validated body (synchronous).
|
|
200
|
-
* @template T Callback return type
|
|
201
|
-
* @param req Express request
|
|
202
|
-
* @param fields Array of required field names
|
|
203
|
-
* @param callback Callback to execute if validation passes
|
|
204
|
-
* @returns Callback result
|
|
205
|
-
* @throws {MissingValidatedDataError} If any required field is missing
|
|
206
|
-
*/
|
|
207
|
-
export function requireValidatedFieldsOrThrow<T = void>(
|
|
208
|
-
req: Request,
|
|
209
|
-
fields: string[],
|
|
210
|
-
callback: () => T,
|
|
211
|
-
): T {
|
|
212
|
-
if (req.validatedBody === undefined) {
|
|
213
|
-
throw new MissingValidatedDataError();
|
|
214
|
-
}
|
|
215
|
-
const validatedBody = req.validatedBody;
|
|
216
|
-
fields.forEach((field) => {
|
|
217
|
-
if (validatedBody[field] === undefined) {
|
|
218
|
-
throw new MissingValidatedDataError(field);
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
return callback();
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Checks if a value is a valid MongoDB ObjectId string.
|
|
226
|
-
* @param id Value to check
|
|
227
|
-
* @returns True if valid ObjectId string
|
|
228
|
-
*/
|
|
229
|
-
export function isValidStringObjectId(id: unknown): boolean {
|
|
230
|
-
return typeof id === 'string' && Types.ObjectId.isValid(id);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Default number of retry attempts for transactions.
|
|
235
|
-
* Uses fewer retries in test environment for faster feedback.
|
|
236
|
-
*/
|
|
237
|
-
export const DEFAULT_RETRY_ATTEMPTS =
|
|
238
|
-
process.env['NODE_ENV'] === 'test' ? 2 : 3;
|
|
239
|
-
/**
|
|
240
|
-
* Default transaction timeout in milliseconds.
|
|
241
|
-
* Uses shorter timeout in test environment for faster failure detection.
|
|
242
|
-
*/
|
|
243
|
-
export const DEFAULT_TRANSACTION_TIMEOUT =
|
|
244
|
-
process.env['NODE_ENV'] === 'test' ? 15000 : 60000;
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Default transaction lock request timeout in milliseconds.
|
|
248
|
-
*/
|
|
249
|
-
export const DEFAULT_TRANSACTION_LOCK_REQUEST_TIMEOUT =
|
|
250
|
-
process.env['NODE_ENV'] === 'test' ? 10000 : 30000;
|
|
251
|
-
|
|
252
|
-
/** Transaction configuration options */
|
|
253
|
-
export interface TransactionOptions<TID extends PlatformID = Buffer> {
|
|
254
|
-
application?: IApplication<TID>;
|
|
255
|
-
timeoutMs?: number;
|
|
256
|
-
retryAttempts?: number;
|
|
257
|
-
baseDelay?: number;
|
|
258
|
-
debugLogEnabled?: boolean;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Gets the default base delay for transaction retries from environment variables
|
|
263
|
-
* @returns The base delay in milliseconds
|
|
264
|
-
*/
|
|
265
|
-
export function getDefaultBaseDelay(): number {
|
|
266
|
-
const envValue = process.env['MONGO_TRANSACTION_RETRY_BASE_DELAY'];
|
|
267
|
-
if (envValue) {
|
|
268
|
-
const parsed = parseInt(envValue);
|
|
269
|
-
if (!isNaN(parsed) && parsed > 0) {
|
|
270
|
-
return parsed;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
// Fallback to hardcoded values if environment variable is not set or invalid
|
|
274
|
-
return process.env['NODE_ENV'] === 'test' ? 25 : 100;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Wraps a callback in a transaction if necessary
|
|
279
|
-
* @param connection The mongoose connection
|
|
280
|
-
* @param useTransaction Whether to use a transaction
|
|
281
|
-
* @param session The session to use
|
|
282
|
-
* @param callback The callback to wrap
|
|
283
|
-
* @param options Transaction options including timeout and retry attempts
|
|
284
|
-
* @param args The arguments to pass to the callback
|
|
285
|
-
* @returns The result of the callback
|
|
286
|
-
*/
|
|
287
|
-
export async function withTransaction<T, TID extends PlatformID = Buffer>(
|
|
288
|
-
connection: Connection,
|
|
289
|
-
useTransaction: boolean,
|
|
290
|
-
session: ClientSession | undefined,
|
|
291
|
-
callback: TransactionCallback<T>,
|
|
292
|
-
options: TransactionOptions<TID> = {},
|
|
293
|
-
...args: any
|
|
294
|
-
): Promise<T> {
|
|
295
|
-
const engine = getSuiteCoreI18nEngine(
|
|
296
|
-
options.application
|
|
297
|
-
? { constants: options.application.constants }
|
|
298
|
-
: undefined,
|
|
299
|
-
);
|
|
300
|
-
const isTestEnvironment = process.env['NODE_ENV'] === 'test';
|
|
301
|
-
const {
|
|
302
|
-
timeoutMs = DEFAULT_TRANSACTION_TIMEOUT,
|
|
303
|
-
retryAttempts = DEFAULT_RETRY_ATTEMPTS, // Use consistent retry attempts
|
|
304
|
-
baseDelay = getDefaultBaseDelay(),
|
|
305
|
-
debugLogEnabled,
|
|
306
|
-
} = options;
|
|
307
|
-
if (!useTransaction) {
|
|
308
|
-
return await callback(session, undefined, ...args);
|
|
309
|
-
}
|
|
310
|
-
const needSession = useTransaction && session === undefined;
|
|
311
|
-
const client = connection.getClient();
|
|
312
|
-
if (!client) {
|
|
313
|
-
// If no client is available, fall back to non-transactional execution
|
|
314
|
-
debugLog(
|
|
315
|
-
debugLogEnabled === true,
|
|
316
|
-
'warn',
|
|
317
|
-
engine.translate(
|
|
318
|
-
SuiteCoreComponentId,
|
|
319
|
-
SuiteCoreStringKey.Admin_NoMongoDbClientFoundFallingBack,
|
|
320
|
-
),
|
|
321
|
-
);
|
|
322
|
-
return await callback(session, undefined, ...args);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
let attempt = 0;
|
|
326
|
-
while (attempt < retryAttempts) {
|
|
327
|
-
const s = needSession ? await client.startSession() : session;
|
|
328
|
-
try {
|
|
329
|
-
if (needSession && s !== undefined) {
|
|
330
|
-
await s.startTransaction({
|
|
331
|
-
maxCommitTimeMS: timeoutMs,
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
// Race the callback against the timeout
|
|
336
|
-
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
337
|
-
setTimeout(() => {
|
|
338
|
-
reject(
|
|
339
|
-
new Error(
|
|
340
|
-
engine.translate(
|
|
341
|
-
SuiteCoreComponentId,
|
|
342
|
-
SuiteCoreStringKey.Admin_TransactionTimeoutTemplate,
|
|
343
|
-
{ timeMs: timeoutMs },
|
|
344
|
-
),
|
|
345
|
-
),
|
|
346
|
-
);
|
|
347
|
-
}, timeoutMs);
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
const result = await Promise.race([callback(s, ...args), timeoutPromise]);
|
|
351
|
-
|
|
352
|
-
if (needSession && s !== undefined) await s.commitTransaction();
|
|
353
|
-
return result;
|
|
354
|
-
} catch (error: any) {
|
|
355
|
-
if (needSession && s !== undefined && s.inTransaction())
|
|
356
|
-
await s.abortTransaction();
|
|
357
|
-
|
|
358
|
-
// Check if this is a transient transaction error that can be retried
|
|
359
|
-
const isTransientError =
|
|
360
|
-
error?.errorLabelSet?.has('TransientTransactionError') ||
|
|
361
|
-
error?.errorLabelSet?.has('UnknownTransactionCommitResult') ||
|
|
362
|
-
error?.code === 251 || // NoSuchTransaction
|
|
363
|
-
error?.code === 112 || // WriteConflict
|
|
364
|
-
error?.code === 11000 || // DuplicateKey - very common in concurrent initialization
|
|
365
|
-
error?.code === 16500 || // TransactionAborted
|
|
366
|
-
error?.code === 244 || // TransactionTooOld
|
|
367
|
-
error?.code === 246 || // ExceededTimeLimit
|
|
368
|
-
error?.code === 13436 || // TransactionTooLargeForCache
|
|
369
|
-
error?.code === 50 || // MaxTimeMSExpired
|
|
370
|
-
error?.message?.includes('Transaction') ||
|
|
371
|
-
error?.message?.includes('aborted') ||
|
|
372
|
-
error?.message?.includes('WriteConflict') ||
|
|
373
|
-
error?.message?.includes('NoSuchTransaction') ||
|
|
374
|
-
error?.message?.includes('TransactionTooOld') ||
|
|
375
|
-
error?.message?.includes('ExceededTimeLimit') ||
|
|
376
|
-
error?.message?.includes('duplicate key error') ||
|
|
377
|
-
error?.message?.includes('E11000') ||
|
|
378
|
-
(error?.code === 11000 && error?.message?.includes('duplicate')); // More specific duplicate key handling
|
|
379
|
-
|
|
380
|
-
if (isTransientError && attempt < retryAttempts - 1) {
|
|
381
|
-
attempt++;
|
|
382
|
-
const jitter = Math.random() * 0.3; // Reduced jitter
|
|
383
|
-
const actualBaseDelay = isTestEnvironment
|
|
384
|
-
? Math.floor(baseDelay * 0.5)
|
|
385
|
-
: baseDelay; // Shorter base delay in tests
|
|
386
|
-
const delay = Math.floor(
|
|
387
|
-
actualBaseDelay * (1 + attempt * 0.5) * (1 + jitter),
|
|
388
|
-
); // Linear backoff instead of exponential
|
|
389
|
-
debugLog(
|
|
390
|
-
debugLogEnabled === true,
|
|
391
|
-
'warn',
|
|
392
|
-
engine.translate(
|
|
393
|
-
SuiteCoreComponentId,
|
|
394
|
-
SuiteCoreStringKey.Admin_TransactionFailedTransientTemplate,
|
|
395
|
-
{
|
|
396
|
-
delayMs: delay,
|
|
397
|
-
attempt,
|
|
398
|
-
attempts: retryAttempts,
|
|
399
|
-
},
|
|
400
|
-
undefined,
|
|
401
|
-
),
|
|
402
|
-
);
|
|
403
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
404
|
-
continue;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
throw error;
|
|
408
|
-
} finally {
|
|
409
|
-
if (needSession && s !== undefined) await s.endSession();
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
const jitter = Math.random() * 0.3; // Reduced jitter
|
|
414
|
-
const actualBaseDelay = isTestEnvironment
|
|
415
|
-
? Math.floor(baseDelay * 0.5)
|
|
416
|
-
: baseDelay; // Shorter base delay in tests
|
|
417
|
-
const delay = Math.floor(
|
|
418
|
-
actualBaseDelay * (1 + attempt * 0.5) * (1 + jitter),
|
|
419
|
-
); // Linear backoff instead of exponential
|
|
420
|
-
|
|
421
|
-
throw new TranslatableSuiteError(
|
|
422
|
-
SuiteCoreStringKey.Admin_TransactionFailedTransientTemplate,
|
|
423
|
-
{
|
|
424
|
-
delayMs: delay,
|
|
425
|
-
attempt,
|
|
426
|
-
attempts: retryAttempts,
|
|
427
|
-
},
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Sends an API response with the given status and response object.
|
|
433
|
-
* @param status
|
|
434
|
-
* @param response
|
|
435
|
-
* @param res
|
|
436
|
-
*/
|
|
437
|
-
export function sendApiMessageResponse<T extends ApiResponse>(
|
|
438
|
-
status: number,
|
|
439
|
-
response: T,
|
|
440
|
-
res: Response<T>,
|
|
441
|
-
): void {
|
|
442
|
-
res.status(status).json(response);
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Sends an API response with the given status, message, and error.
|
|
447
|
-
* @param status
|
|
448
|
-
* @param message
|
|
449
|
-
* @param error
|
|
450
|
-
* @param res
|
|
451
|
-
*/
|
|
452
|
-
export function sendApiErrorResponse(
|
|
453
|
-
status: number,
|
|
454
|
-
message: string,
|
|
455
|
-
error: unknown,
|
|
456
|
-
res: Response,
|
|
457
|
-
): void {
|
|
458
|
-
sendApiMessageResponse<IApiErrorResponse>(status, { message, error }, res);
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Sends an API response with the given status and validation errors.
|
|
463
|
-
* @param status
|
|
464
|
-
* @param errors
|
|
465
|
-
* @param res
|
|
466
|
-
*/
|
|
467
|
-
export function sendApiExpressValidationErrorResponse(
|
|
468
|
-
status: number,
|
|
469
|
-
errors: ValidationError[],
|
|
470
|
-
res: Response,
|
|
471
|
-
application?: IApplication,
|
|
472
|
-
): void {
|
|
473
|
-
const engine = getSuiteCoreI18nEngine(
|
|
474
|
-
application ? { constants: application.constants } : undefined,
|
|
475
|
-
);
|
|
476
|
-
sendApiMessageResponse<IApiExpressValidationErrorResponse>(
|
|
477
|
-
status,
|
|
478
|
-
{
|
|
479
|
-
message: engine.translate(
|
|
480
|
-
SuiteCoreComponentId,
|
|
481
|
-
SuiteCoreStringKey.ValidationError,
|
|
482
|
-
),
|
|
483
|
-
errors,
|
|
484
|
-
},
|
|
485
|
-
res,
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* Sends an API response with the given status, message, and MongoDB validation errors.
|
|
491
|
-
* @param status
|
|
492
|
-
* @param message
|
|
493
|
-
* @param errors
|
|
494
|
-
* @param res
|
|
495
|
-
*/
|
|
496
|
-
export function sendApiMongoValidationErrorResponse(
|
|
497
|
-
status: number,
|
|
498
|
-
message: string,
|
|
499
|
-
errors: IMongoErrors,
|
|
500
|
-
res: Response,
|
|
501
|
-
): void {
|
|
502
|
-
sendApiMessageResponse<IApiMongoValidationErrorResponse>(
|
|
503
|
-
status,
|
|
504
|
-
{ message, errors },
|
|
505
|
-
res,
|
|
506
|
-
);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
/**
|
|
510
|
-
* Sends a raw JSON response with the given status and response object.
|
|
511
|
-
* @param status The status code
|
|
512
|
-
* @param response The response data
|
|
513
|
-
* @param res The response object
|
|
514
|
-
*/
|
|
515
|
-
export function sendRawJsonResponse<T>(
|
|
516
|
-
status: number,
|
|
517
|
-
response: T,
|
|
518
|
-
res: Response<T>,
|
|
519
|
-
) {
|
|
520
|
-
res.status(status).json(response);
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
function isRecursiveError(error: unknown): boolean {
|
|
524
|
-
return (
|
|
525
|
-
error !== null &&
|
|
526
|
-
typeof error === 'object' &&
|
|
527
|
-
'_handlingInProgress' in error &&
|
|
528
|
-
!!(error as { _handlingInProgress?: boolean })._handlingInProgress
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
function markErrorAsHandling(error: unknown): void {
|
|
533
|
-
if (error && typeof error === 'object') {
|
|
534
|
-
(error as { _handlingInProgress?: boolean })._handlingInProgress = true;
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
function getSafeErrorMessage(
|
|
539
|
-
message?: string,
|
|
540
|
-
application?: IApplication,
|
|
541
|
-
): string {
|
|
542
|
-
if (message && typeof message === 'string' && message.trim() !== '') {
|
|
543
|
-
return message;
|
|
544
|
-
}
|
|
545
|
-
const engine = getSuiteCoreI18nEngine(
|
|
546
|
-
application ? { constants: application.constants } : undefined,
|
|
547
|
-
);
|
|
548
|
-
try {
|
|
549
|
-
const translated = engine.translate(
|
|
550
|
-
SuiteCoreComponentId,
|
|
551
|
-
SuiteCoreStringKey.Common_UnexpectedError,
|
|
552
|
-
);
|
|
553
|
-
return translated &&
|
|
554
|
-
typeof translated === 'string' &&
|
|
555
|
-
translated.trim() !== ''
|
|
556
|
-
? translated
|
|
557
|
-
: 'An unexpected error occurred';
|
|
558
|
-
} catch {
|
|
559
|
-
return 'An unexpected error occurred';
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
function convertToHandleableError(error: unknown): {
|
|
564
|
-
handleableError: HandleableError;
|
|
565
|
-
alreadyHandled: boolean;
|
|
566
|
-
errorType: string;
|
|
567
|
-
} {
|
|
568
|
-
if (error instanceof HandleableError) {
|
|
569
|
-
return {
|
|
570
|
-
handleableError: error,
|
|
571
|
-
alreadyHandled: error.handled,
|
|
572
|
-
errorType: error.name,
|
|
573
|
-
};
|
|
574
|
-
}
|
|
575
|
-
if (error instanceof Error) {
|
|
576
|
-
return {
|
|
577
|
-
handleableError: new HandleableError(error),
|
|
578
|
-
alreadyHandled: false,
|
|
579
|
-
errorType: error.name,
|
|
580
|
-
};
|
|
581
|
-
}
|
|
582
|
-
const unknownMessage = getSafeErrorMessage(
|
|
583
|
-
typeof error === 'object' &&
|
|
584
|
-
error !== null &&
|
|
585
|
-
'message' in (error as object)
|
|
586
|
-
? ((error as Record<string, unknown>)['message'] as string | undefined)
|
|
587
|
-
: undefined,
|
|
588
|
-
);
|
|
589
|
-
const unknownError = new Error(unknownMessage);
|
|
590
|
-
return {
|
|
591
|
-
handleableError: new HandleableError(unknownError, { sourceData: error }),
|
|
592
|
-
alreadyHandled: false,
|
|
593
|
-
errorType: 'UnexpectedError',
|
|
594
|
-
};
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
function sendErrorResponse<TStringKey extends keyof RequiredStringKeys>(
|
|
598
|
-
error: unknown,
|
|
599
|
-
handleableError: HandleableError,
|
|
600
|
-
errorType: string,
|
|
601
|
-
send: SendFunction<
|
|
602
|
-
| IApiErrorResponse
|
|
603
|
-
| IApiExpressValidationErrorResponse
|
|
604
|
-
| IApiMongoValidationErrorResponse
|
|
605
|
-
>,
|
|
606
|
-
res: Response,
|
|
607
|
-
): void {
|
|
608
|
-
const engine = I18nEngine.getInstance();
|
|
609
|
-
if (error instanceof ExpressValidationError) {
|
|
610
|
-
// amazonq-ignore-next-line false positive
|
|
611
|
-
send(
|
|
612
|
-
handleableError.statusCode,
|
|
613
|
-
{
|
|
614
|
-
message: engine.translate('core', 'ValidationError' as TStringKey),
|
|
615
|
-
errors:
|
|
616
|
-
error.errors instanceof Result ? error.errors.array() : error.errors,
|
|
617
|
-
errorType: 'ExpressValidationError',
|
|
618
|
-
},
|
|
619
|
-
res,
|
|
620
|
-
);
|
|
621
|
-
} else if (error instanceof MongooseValidationError) {
|
|
622
|
-
// amazonq-ignore-next-line false positive
|
|
623
|
-
send(
|
|
624
|
-
handleableError.statusCode,
|
|
625
|
-
{
|
|
626
|
-
message: engine.translate('core', 'ValidationError' as TStringKey),
|
|
627
|
-
errors: error.errors,
|
|
628
|
-
errorType: 'MongooseValidationError',
|
|
629
|
-
},
|
|
630
|
-
res,
|
|
631
|
-
);
|
|
632
|
-
} else {
|
|
633
|
-
// amazonq-ignore-next-line false positive
|
|
634
|
-
send(
|
|
635
|
-
handleableError.statusCode,
|
|
636
|
-
{
|
|
637
|
-
message: handleableError.message,
|
|
638
|
-
error: {
|
|
639
|
-
message: handleableError.message,
|
|
640
|
-
statusCode: handleableError.statusCode,
|
|
641
|
-
...(handleableError.stack && { stack: handleableError.stack }),
|
|
642
|
-
},
|
|
643
|
-
errorType: errorType,
|
|
644
|
-
},
|
|
645
|
-
res,
|
|
646
|
-
);
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
export function handleError(
|
|
651
|
-
error: unknown,
|
|
652
|
-
res: Response,
|
|
653
|
-
send: SendFunction<
|
|
654
|
-
| IApiErrorResponse
|
|
655
|
-
| IApiExpressValidationErrorResponse
|
|
656
|
-
| IApiMongoValidationErrorResponse
|
|
657
|
-
>,
|
|
658
|
-
_next: NextFunction,
|
|
659
|
-
): void {
|
|
660
|
-
if (isRecursiveError(error)) {
|
|
661
|
-
const fallbackError = new HandleableError(
|
|
662
|
-
new Error('Recursive error handling detected'),
|
|
663
|
-
);
|
|
664
|
-
// amazonq-ignore-next-line false positive
|
|
665
|
-
send(
|
|
666
|
-
fallbackError.statusCode,
|
|
667
|
-
{
|
|
668
|
-
message: fallbackError.message,
|
|
669
|
-
error: fallbackError,
|
|
670
|
-
errorType: 'RecursiveError',
|
|
671
|
-
},
|
|
672
|
-
res,
|
|
673
|
-
);
|
|
674
|
-
return;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
markErrorAsHandling(error);
|
|
678
|
-
const {
|
|
679
|
-
handleableError,
|
|
680
|
-
alreadyHandled: _alreadyHandled,
|
|
681
|
-
errorType,
|
|
682
|
-
} = convertToHandleableError(error);
|
|
683
|
-
|
|
684
|
-
if (!(error instanceof ExpressValidationError)) {
|
|
685
|
-
console.error(
|
|
686
|
-
'[handleError]',
|
|
687
|
-
'type=' + errorType.replace(/[\r\n]/g, ''),
|
|
688
|
-
'status=' + String(handleableError.statusCode).replace(/[\r\n]/g, ''),
|
|
689
|
-
'message=' + (handleableError.message || '').replace(/[\r\n]/g, ''),
|
|
690
|
-
);
|
|
691
|
-
if (error instanceof Error && error.stack) {
|
|
692
|
-
debugLog(true, 'error', '[handleError] stack:', error.stack);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
if (!res.headersSent) {
|
|
697
|
-
sendErrorResponse(error, handleableError, errorType, send, res);
|
|
698
|
-
handleableError.handled = true;
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
export function locatePEMRoot(devRootDir: string): string | undefined {
|
|
703
|
-
try {
|
|
704
|
-
const normalizedDir = resolve(devRootDir);
|
|
705
|
-
if (
|
|
706
|
-
normalizedDir.includes('..') ||
|
|
707
|
-
!normalizedDir.startsWith(resolve('.'))
|
|
708
|
-
) {
|
|
709
|
-
return undefined;
|
|
710
|
-
}
|
|
711
|
-
const files = readdirSync(normalizedDir);
|
|
712
|
-
const pemFiles = files.filter(
|
|
713
|
-
(file: string) =>
|
|
714
|
-
// Prevent path traversal by rejecting files with path separators
|
|
715
|
-
!file.includes('/') &&
|
|
716
|
-
!file.includes('\\') &&
|
|
717
|
-
!file.includes('..') &&
|
|
718
|
-
(file.match(/localhost\+\d+-key\.pem$/) ||
|
|
719
|
-
file.match(/localhost\+\d+\.pem$/)),
|
|
720
|
-
);
|
|
721
|
-
if (pemFiles.length < 2) {
|
|
722
|
-
return undefined;
|
|
723
|
-
}
|
|
724
|
-
const roots = pemFiles.map((file: string) => {
|
|
725
|
-
const resolved = resolve(normalizedDir, file);
|
|
726
|
-
if (!resolved.startsWith(normalizedDir)) {
|
|
727
|
-
return undefined;
|
|
728
|
-
}
|
|
729
|
-
const result = /(.*)\/(localhost\+\d+)(.*)\.pem/.exec(resolved);
|
|
730
|
-
return result ? `${result[1]}/${result[2]}` : undefined;
|
|
731
|
-
});
|
|
732
|
-
if (roots.some((root) => root !== roots[0])) {
|
|
733
|
-
return undefined;
|
|
734
|
-
}
|
|
735
|
-
if (!existsSync(roots[0] + '.pem') || !existsSync(roots[0] + '-key.pem')) {
|
|
736
|
-
return undefined;
|
|
737
|
-
}
|
|
738
|
-
return roots[0]!;
|
|
739
|
-
} catch {
|
|
740
|
-
return undefined;
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/**
|
|
745
|
-
* Encodes the length of the data in the buffer
|
|
746
|
-
* @param buffer The buffer to encode
|
|
747
|
-
* @returns The encoded buffer
|
|
748
|
-
*/
|
|
749
|
-
export function lengthEncodeData(buffer: Buffer): Buffer {
|
|
750
|
-
const lengthType: LengthEncodingType = getLengthEncodingTypeForLength(
|
|
751
|
-
buffer.length,
|
|
752
|
-
);
|
|
753
|
-
const lengthTypeSize: number = getLengthForLengthType(lengthType);
|
|
754
|
-
const result: Buffer = Buffer.alloc(1 + lengthTypeSize + buffer.length);
|
|
755
|
-
result.writeUInt8(lengthType, 0);
|
|
756
|
-
switch (lengthType) {
|
|
757
|
-
case LengthEncodingType.UInt8:
|
|
758
|
-
result.writeUInt8(buffer.length, 1);
|
|
759
|
-
break;
|
|
760
|
-
case LengthEncodingType.UInt16:
|
|
761
|
-
result.writeUInt16BE(buffer.length, 1);
|
|
762
|
-
break;
|
|
763
|
-
case LengthEncodingType.UInt32:
|
|
764
|
-
result.writeUInt32BE(buffer.length, 1);
|
|
765
|
-
break;
|
|
766
|
-
case LengthEncodingType.UInt64:
|
|
767
|
-
result.writeBigUInt64BE(BigInt(buffer.length), 1);
|
|
768
|
-
break;
|
|
769
|
-
}
|
|
770
|
-
buffer.copy(result, 1 + lengthTypeSize);
|
|
771
|
-
return result;
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
export function decodeLengthEncodedData(buffer: Buffer): {
|
|
775
|
-
data: Buffer;
|
|
776
|
-
totalLength: number;
|
|
777
|
-
} {
|
|
778
|
-
if (buffer.length < 1) {
|
|
779
|
-
throw new RangeError('Buffer is too short to read length type.');
|
|
780
|
-
}
|
|
781
|
-
const lengthType: LengthEncodingType = getLengthEncodingTypeFromValue(
|
|
782
|
-
buffer.readUint8(0),
|
|
783
|
-
);
|
|
784
|
-
const lengthTypeSize: number = getLengthForLengthType(lengthType);
|
|
785
|
-
|
|
786
|
-
if (buffer.length < 1 + lengthTypeSize) {
|
|
787
|
-
throw new RangeError('Buffer is too short to read the full length value.');
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
let length: number | bigint;
|
|
791
|
-
switch (lengthType) {
|
|
792
|
-
case LengthEncodingType.UInt8:
|
|
793
|
-
length = buffer.readUint8(1);
|
|
794
|
-
break;
|
|
795
|
-
case LengthEncodingType.UInt16:
|
|
796
|
-
length = buffer.readUint16BE(1);
|
|
797
|
-
break;
|
|
798
|
-
case LengthEncodingType.UInt32:
|
|
799
|
-
length = buffer.readUint32BE(1);
|
|
800
|
-
break;
|
|
801
|
-
case LengthEncodingType.UInt64:
|
|
802
|
-
length = buffer.readBigUInt64BE(1);
|
|
803
|
-
if (Number(length) > Number.MAX_SAFE_INTEGER) {
|
|
804
|
-
throw new RangeError('Length exceeds maximum safe integer value');
|
|
805
|
-
}
|
|
806
|
-
break;
|
|
807
|
-
default:
|
|
808
|
-
throw new TranslatableSuiteError(
|
|
809
|
-
SuiteCoreStringKey.Error_LengthIsInvalidType,
|
|
810
|
-
);
|
|
811
|
-
}
|
|
812
|
-
|
|
813
|
-
const totalLength = 1 + lengthTypeSize + Number(length);
|
|
814
|
-
if (totalLength > buffer.length) {
|
|
815
|
-
throw new RangeError('Buffer is too short for declared data length');
|
|
816
|
-
}
|
|
817
|
-
return {
|
|
818
|
-
data: buffer.subarray(1 + lengthTypeSize, totalLength),
|
|
819
|
-
totalLength,
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
import { HandleableError } from '@digitaldefiance/i18n-lib';
|
|
824
|
-
import {
|
|
825
|
-
getSuiteCoreI18nEngine,
|
|
826
|
-
SuiteCoreComponentId,
|
|
827
|
-
SuiteCoreStringKey,
|
|
828
|
-
TranslatableSuiteError,
|
|
829
|
-
} from '@digitaldefiance/suite-core-lib';
|
|
830
|
-
import moment from 'moment-timezone';
|
|
831
|
-
import { BackupCode } from './backup-code';
|
|
832
|
-
import { LengthEncodingType } from './enumerations/length-encoding-type';
|
|
833
|
-
import { MissingValidatedDataError } from './errors';
|
|
834
|
-
import type { PlatformID } from '@digitaldefiance/node-ecies-lib';
|
|
835
|
-
|
|
836
|
-
export function isValidTimezone(timezone: string): boolean {
|
|
837
|
-
return moment.tz.zone(timezone) !== null;
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
/**
|
|
841
|
-
* Omits keys from an object
|
|
842
|
-
*/
|
|
843
|
-
export function omit<T extends object, K extends keyof T>(
|
|
844
|
-
obj: T,
|
|
845
|
-
keys: K[],
|
|
846
|
-
): Omit<T, K> {
|
|
847
|
-
const keysToOmit = new Set(keys);
|
|
848
|
-
return Object.keys(obj).reduce((result, key) => {
|
|
849
|
-
if (!keysToOmit.has(key as K)) {
|
|
850
|
-
result[key as keyof T] = obj[key as keyof T];
|
|
851
|
-
}
|
|
852
|
-
return result;
|
|
853
|
-
}, {} as T) as Omit<T, K>;
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
/**
|
|
857
|
-
* Validates that a collection contains exactly the keys from an enum
|
|
858
|
-
* @param collection The collection to validate
|
|
859
|
-
* @param enumObject The enum object to validate against
|
|
860
|
-
* @param collectionName Optional name for the collection (for better error messages)
|
|
861
|
-
* @param enumName Optional name for the enum (for better error messages)
|
|
862
|
-
* @throws Error if collection has missing, extra, or invalid keys
|
|
863
|
-
*/
|
|
864
|
-
export function validateEnumCollection<T extends Record<string, any>>(
|
|
865
|
-
collection: Record<string, unknown>,
|
|
866
|
-
enumObject: T,
|
|
867
|
-
collectionName?: string,
|
|
868
|
-
enumName?: string,
|
|
869
|
-
): void {
|
|
870
|
-
// For numeric enums, filter out reverse mappings (number -> string)
|
|
871
|
-
const enumKeys = Object.keys(enumObject).filter((key) => isNaN(Number(key)));
|
|
872
|
-
const allEnumValues = enumKeys.map((key) => enumObject[key]);
|
|
873
|
-
const collectionKeys = Object.keys(collection);
|
|
874
|
-
|
|
875
|
-
const collectionLabel = collectionName || 'Collection';
|
|
876
|
-
const enumLabel = enumName || `enum with keys [${enumKeys.join(', ')}]`;
|
|
877
|
-
|
|
878
|
-
if (collectionKeys.length !== allEnumValues.length) {
|
|
879
|
-
throw new Error(
|
|
880
|
-
`${collectionLabel} must contain exactly ${
|
|
881
|
-
allEnumValues.length
|
|
882
|
-
} keys to match ${enumLabel}. Found ${
|
|
883
|
-
collectionKeys.length
|
|
884
|
-
} keys: [${collectionKeys.join(', ')}]`,
|
|
885
|
-
);
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
const invalidKeys = collectionKeys.filter(
|
|
889
|
-
(key) => !allEnumValues.includes(key),
|
|
890
|
-
);
|
|
891
|
-
if (invalidKeys.length > 0) {
|
|
892
|
-
throw new Error(
|
|
893
|
-
`${collectionLabel} contains invalid keys for ${enumLabel}: [${invalidKeys.join(
|
|
894
|
-
', ',
|
|
895
|
-
)}]. Valid keys are: [${allEnumValues.join(', ')}]`,
|
|
896
|
-
);
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
const missingKeys = allEnumValues.filter((value) => !(value in collection));
|
|
900
|
-
if (missingKeys.length > 0) {
|
|
901
|
-
throw new Error(
|
|
902
|
-
`${collectionLabel} is missing required keys for ${enumLabel}: [${missingKeys.join(
|
|
903
|
-
', ',
|
|
904
|
-
)}]`,
|
|
905
|
-
);
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
export function uint8ArrayToBase64(uint8Array: Uint8Array): string {
|
|
910
|
-
let binaryString = '';
|
|
911
|
-
for (let i = 0; i < uint8Array.length; i++) {
|
|
912
|
-
binaryString += String.fromCharCode(uint8Array[i]);
|
|
913
|
-
}
|
|
914
|
-
return btoa(binaryString);
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
export function base64ToUint8Array(base64String: string): Uint8Array {
|
|
918
|
-
const binaryString = atob(base64String);
|
|
919
|
-
const len = binaryString.length;
|
|
920
|
-
const bytes = new Uint8Array(len);
|
|
921
|
-
for (let i = 0; i < len; i++) {
|
|
922
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
923
|
-
}
|
|
924
|
-
return bytes;
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
export function uint8ArrayToHex(uint8Array: Uint8Array): string {
|
|
928
|
-
return Array.from(uint8Array)
|
|
929
|
-
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
930
|
-
.join('');
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
export function hexToUint8Array(hexString: string): Uint8Array {
|
|
934
|
-
const len = hexString.length;
|
|
935
|
-
const bytes = new Uint8Array(len / 2);
|
|
936
|
-
for (let i = 0; i < len; i += 2) {
|
|
937
|
-
bytes[i / 2] = parseInt(hexString.substring(i, i + 2), 16);
|
|
938
|
-
}
|
|
939
|
-
return bytes;
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
/**
|
|
943
|
-
* Utility functions for browser ECIES implementation
|
|
944
|
-
*/
|
|
945
|
-
|
|
946
|
-
/**
|
|
947
|
-
* CRC16-CCITT implementation for data integrity checking
|
|
948
|
-
* Uses the same algorithm as the server-side implementation (CRC16-CCITT-FALSE)
|
|
949
|
-
*/
|
|
950
|
-
export function crc16(data: Uint8Array): Uint8Array {
|
|
951
|
-
let crc = 0xffff; // Initial value for CRC16-CCITT-FALSE
|
|
952
|
-
const polynomial = 0x1021; // CRC16-CCITT polynomial
|
|
953
|
-
|
|
954
|
-
for (let i = 0; i < data.length; i++) {
|
|
955
|
-
crc ^= data[i] << 8;
|
|
956
|
-
for (let j = 0; j < 8; j++) {
|
|
957
|
-
if (crc & 0x8000) {
|
|
958
|
-
crc = (crc << 1) ^ polynomial;
|
|
959
|
-
} else {
|
|
960
|
-
crc = crc << 1;
|
|
961
|
-
}
|
|
962
|
-
crc &= 0xffff; // Keep it 16-bit
|
|
963
|
-
}
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
const result = new Uint8Array(2);
|
|
967
|
-
result[0] = (crc >>> 8) & 0xff; // Big-endian
|
|
968
|
-
result[1] = crc & 0xff;
|
|
969
|
-
return result;
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
/**
|
|
973
|
-
* Convert string to Uint8Array (UTF-8 encoding)
|
|
974
|
-
*/
|
|
975
|
-
export function stringToUint8Array(str: string): Uint8Array {
|
|
976
|
-
return new TextEncoder().encode(str);
|
|
977
|
-
}
|
|
978
|
-
|
|
979
|
-
/**
|
|
980
|
-
* Convert Uint8Array to string (UTF-8 decoding)
|
|
981
|
-
*/
|
|
982
|
-
export function uint8ArrayToString(array: Uint8Array): string {
|
|
983
|
-
return new TextDecoder().decode(array);
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
/**
|
|
987
|
-
* Secure random bytes generation
|
|
988
|
-
*/
|
|
989
|
-
export function randomBytes(length: number): Uint8Array {
|
|
990
|
-
return crypto.getRandomValues(new Uint8Array(length));
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
/**
|
|
994
|
-
* Compare two Uint8Arrays for equality
|
|
995
|
-
*/
|
|
996
|
-
export function arraysEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
997
|
-
if (a.length !== b.length) return false;
|
|
998
|
-
for (let i = 0; i < a.length; i++) {
|
|
999
|
-
if (a[i] !== b[i]) return false;
|
|
1000
|
-
}
|
|
1001
|
-
return true;
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
/**
|
|
1005
|
-
* Concatenate multiple Uint8Arrays
|
|
1006
|
-
*/
|
|
1007
|
-
export function concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array {
|
|
1008
|
-
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
1009
|
-
const result = new Uint8Array(totalLength);
|
|
1010
|
-
let offset = 0;
|
|
1011
|
-
for (const array of arrays) {
|
|
1012
|
-
result.set(array, offset);
|
|
1013
|
-
offset += array.length;
|
|
1014
|
-
}
|
|
1015
|
-
return result;
|
|
1016
|
-
}
|
|
1017
|
-
|
|
1018
|
-
/**
|
|
1019
|
-
* Get the length encoding type for a given length
|
|
1020
|
-
* @param length The length to evaluate
|
|
1021
|
-
* @returns The corresponding LengthEncodingType
|
|
1022
|
-
*/
|
|
1023
|
-
export function getLengthEncodingTypeForLength(
|
|
1024
|
-
length: number | bigint,
|
|
1025
|
-
): LengthEncodingType {
|
|
1026
|
-
if (typeof length === 'number') {
|
|
1027
|
-
if (length < 256) {
|
|
1028
|
-
return LengthEncodingType.UInt8;
|
|
1029
|
-
} else if (length < 65536) {
|
|
1030
|
-
return LengthEncodingType.UInt16;
|
|
1031
|
-
} else if (length < 4294967296) {
|
|
1032
|
-
return LengthEncodingType.UInt32;
|
|
1033
|
-
} else if (length < Number.MAX_SAFE_INTEGER) {
|
|
1034
|
-
return LengthEncodingType.UInt64;
|
|
1035
|
-
} else {
|
|
1036
|
-
throw new TranslatableSuiteError(
|
|
1037
|
-
SuiteCoreStringKey.Error_LengthExceedsMaximum,
|
|
1038
|
-
);
|
|
1039
|
-
}
|
|
1040
|
-
} else if (typeof length === 'bigint') {
|
|
1041
|
-
if (length < 256n) {
|
|
1042
|
-
return LengthEncodingType.UInt8;
|
|
1043
|
-
} else if (length < 65536n) {
|
|
1044
|
-
return LengthEncodingType.UInt16;
|
|
1045
|
-
} else if (length < 4294967296n) {
|
|
1046
|
-
return LengthEncodingType.UInt32;
|
|
1047
|
-
} else if (length < 18446744073709551616n) {
|
|
1048
|
-
return LengthEncodingType.UInt64;
|
|
1049
|
-
} else {
|
|
1050
|
-
throw new TranslatableSuiteError(
|
|
1051
|
-
SuiteCoreStringKey.Error_LengthExceedsMaximum,
|
|
1052
|
-
);
|
|
1053
|
-
}
|
|
1054
|
-
} else {
|
|
1055
|
-
throw new TranslatableSuiteError(
|
|
1056
|
-
SuiteCoreStringKey.Error_LengthIsInvalidType,
|
|
1057
|
-
);
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
|
|
1061
|
-
/**
|
|
1062
|
-
* Get the length encoding type for a given value
|
|
1063
|
-
* @param value The value to evaluate
|
|
1064
|
-
* @returns The corresponding LengthEncodingType
|
|
1065
|
-
*/
|
|
1066
|
-
export function getLengthEncodingTypeFromValue(
|
|
1067
|
-
value: number,
|
|
1068
|
-
): LengthEncodingType {
|
|
1069
|
-
for (const length of Object.values(LengthEncodingType)) {
|
|
1070
|
-
if (length === value) {
|
|
1071
|
-
return length;
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
throw new TranslatableSuiteError(
|
|
1075
|
-
SuiteCoreStringKey.Error_LengthIsInvalidType,
|
|
1076
|
-
);
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
/**
|
|
1080
|
-
* Get the length in bytes for a given LengthEncodingType
|
|
1081
|
-
* @param type The LengthEncodingType to evaluate
|
|
1082
|
-
* @returns The length in bytes
|
|
1083
|
-
*/
|
|
1084
|
-
export function getLengthForLengthType(type: LengthEncodingType): number {
|
|
1085
|
-
switch (type) {
|
|
1086
|
-
case LengthEncodingType.UInt8:
|
|
1087
|
-
return 1;
|
|
1088
|
-
case LengthEncodingType.UInt16:
|
|
1089
|
-
return 2;
|
|
1090
|
-
case LengthEncodingType.UInt32:
|
|
1091
|
-
return 4;
|
|
1092
|
-
case LengthEncodingType.UInt64:
|
|
1093
|
-
return 8;
|
|
1094
|
-
default:
|
|
1095
|
-
throw new TranslatableSuiteError(
|
|
1096
|
-
SuiteCoreStringKey.Error_LengthIsInvalidType,
|
|
1097
|
-
);
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
export function parseBackupCodes(
|
|
1102
|
-
user: 'admin' | 'member' | 'system',
|
|
1103
|
-
environment: Record<string, string | undefined>,
|
|
1104
|
-
): BackupCode[] {
|
|
1105
|
-
const envVarMap: Record<'admin' | 'member' | 'system', string> = {
|
|
1106
|
-
admin: 'ADMIN_BACKUP_CODES',
|
|
1107
|
-
member: 'MEMBER_BACKUP_CODES',
|
|
1108
|
-
system: 'SYSTEM_BACKUP_CODES',
|
|
1109
|
-
};
|
|
1110
|
-
const envVar = envVarMap[user];
|
|
1111
|
-
const envValue = environment[envVar];
|
|
1112
|
-
const backupCodes =
|
|
1113
|
-
envValue?.split(',').map((code: string) => new BackupCode(code.trim())) ||
|
|
1114
|
-
[];
|
|
1115
|
-
return backupCodes;
|
|
1116
|
-
}
|