@strapi/data-transfer 0.0.0-experimental.62ce06180fe9a772eaeb3d43d238b26644f39f7c → 0.0.0-experimental.6340da17068c3bed7fb006e7bcc554c71129e282
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -4
- package/dist/directory/index.d.ts +2 -0
- package/dist/directory/index.d.ts.map +1 -0
- package/dist/directory/index.js +8 -0
- package/dist/directory/index.js.map +1 -0
- package/dist/directory/index.mjs +3 -0
- package/dist/directory/index.mjs.map +1 -0
- package/dist/directory/providers/destination/index.d.ts +38 -0
- package/dist/directory/providers/destination/index.d.ts.map +1 -0
- package/dist/directory/providers/destination/index.js +184 -0
- package/dist/directory/providers/destination/index.js.map +1 -0
- package/dist/directory/providers/destination/index.mjs +177 -0
- package/dist/directory/providers/destination/index.mjs.map +1 -0
- package/dist/directory/providers/destination/utils.d.ts +8 -0
- package/dist/directory/providers/destination/utils.d.ts.map +1 -0
- package/dist/directory/providers/destination/utils.js +62 -0
- package/dist/directory/providers/destination/utils.js.map +1 -0
- package/dist/directory/providers/destination/utils.mjs +56 -0
- package/dist/directory/providers/destination/utils.mjs.map +1 -0
- package/dist/directory/providers/index.d.ts +3 -0
- package/dist/directory/providers/index.d.ts.map +1 -0
- package/dist/directory/providers/index.js +10 -0
- package/dist/directory/providers/index.js.map +1 -0
- package/dist/directory/providers/index.mjs +3 -0
- package/dist/directory/providers/index.mjs.map +1 -0
- package/dist/directory/providers/source/index.d.ts +27 -0
- package/dist/directory/providers/source/index.d.ts.map +1 -0
- package/dist/directory/providers/source/index.js +266 -0
- package/dist/directory/providers/source/index.js.map +1 -0
- package/dist/directory/providers/source/index.mjs +259 -0
- package/dist/directory/providers/source/index.mjs.map +1 -0
- package/dist/engine/errors.js +39 -0
- package/dist/engine/errors.js.map +1 -0
- package/dist/engine/errors.mjs +34 -0
- package/dist/engine/errors.mjs.map +1 -0
- package/dist/engine/index.d.ts +2 -3
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +922 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/index.mjs +917 -0
- package/dist/engine/index.mjs.map +1 -0
- package/dist/engine/validation/provider.d.ts +2 -2
- package/dist/engine/validation/provider.d.ts.map +1 -1
- package/dist/engine/validation/provider.js +19 -0
- package/dist/engine/validation/provider.js.map +1 -0
- package/dist/engine/validation/provider.mjs +17 -0
- package/dist/engine/validation/provider.mjs.map +1 -0
- package/dist/engine/validation/schemas/index.d.ts.map +1 -1
- package/dist/engine/validation/schemas/index.js +59 -0
- package/dist/engine/validation/schemas/index.js.map +1 -0
- package/dist/engine/validation/schemas/index.mjs +57 -0
- package/dist/engine/validation/schemas/index.mjs.map +1 -0
- package/dist/errors/base.js +13 -0
- package/dist/errors/base.js.map +1 -0
- package/dist/errors/base.mjs +11 -0
- package/dist/errors/base.mjs.map +1 -0
- package/dist/errors/constants.d.ts +1 -1
- package/dist/errors/constants.d.ts.map +1 -1
- package/dist/errors/constants.js +9 -0
- package/dist/errors/constants.js.map +1 -0
- package/dist/errors/constants.mjs +7 -0
- package/dist/errors/constants.mjs.map +1 -0
- package/dist/errors/providers.d.ts +1 -1
- package/dist/errors/providers.d.ts.map +1 -1
- package/dist/errors/providers.js +41 -0
- package/dist/errors/providers.js.map +1 -0
- package/dist/errors/providers.mjs +36 -0
- package/dist/errors/providers.mjs.map +1 -0
- package/dist/file/index.js +8 -0
- package/dist/file/index.js.map +1 -0
- package/dist/file/index.mjs +3 -0
- package/dist/file/index.mjs.map +1 -0
- package/dist/file/providers/destination/index.d.ts +3 -5
- package/dist/file/providers/destination/index.d.ts.map +1 -1
- package/dist/file/providers/destination/index.js +254 -0
- package/dist/file/providers/destination/index.js.map +1 -0
- package/dist/file/providers/destination/index.mjs +246 -0
- package/dist/file/providers/destination/index.mjs.map +1 -0
- package/dist/file/providers/destination/utils.d.ts +0 -1
- package/dist/file/providers/destination/utils.d.ts.map +1 -1
- package/dist/file/providers/destination/utils.js +63 -0
- package/dist/file/providers/destination/utils.js.map +1 -0
- package/dist/file/providers/destination/utils.mjs +60 -0
- package/dist/file/providers/destination/utils.mjs.map +1 -0
- package/dist/file/providers/index.js +10 -0
- package/dist/file/providers/index.js.map +1 -0
- package/dist/file/providers/index.mjs +3 -0
- package/dist/file/providers/index.mjs.map +1 -0
- package/dist/file/providers/source/index.d.ts +3 -3
- package/dist/file/providers/source/index.d.ts.map +1 -1
- package/dist/file/providers/source/index.js +336 -0
- package/dist/file/providers/source/index.js.map +1 -0
- package/dist/file/providers/source/index.mjs +328 -0
- package/dist/file/providers/source/index.mjs.map +1 -0
- package/dist/file/providers/source/utils.d.ts.map +1 -1
- package/dist/file/providers/source/utils.js +60 -0
- package/dist/file/providers/source/utils.js.map +1 -0
- package/dist/file/providers/source/utils.mjs +52 -0
- package/dist/file/providers/source/utils.mjs.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -4408
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -4382
- package/dist/index.mjs.map +1 -1
- package/dist/strapi/index.js +12 -0
- package/dist/strapi/index.js.map +1 -0
- package/dist/strapi/index.mjs +7 -0
- package/dist/strapi/index.mjs.map +1 -0
- package/dist/strapi/providers/index.js +18 -0
- package/dist/strapi/providers/index.js.map +1 -0
- package/dist/strapi/providers/index.mjs +8 -0
- package/dist/strapi/providers/index.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/assets-destination-writable.d.ts +26 -0
- package/dist/strapi/providers/local-destination/assets-destination-writable.d.ts.map +1 -0
- package/dist/strapi/providers/local-destination/assets-destination-writable.js +125 -0
- package/dist/strapi/providers/local-destination/assets-destination-writable.js.map +1 -0
- package/dist/strapi/providers/local-destination/assets-destination-writable.mjs +123 -0
- package/dist/strapi/providers/local-destination/assets-destination-writable.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/index.d.ts +5 -3
- package/dist/strapi/providers/local-destination/index.d.ts.map +1 -1
- package/dist/strapi/providers/local-destination/index.js +362 -0
- package/dist/strapi/providers/local-destination/index.js.map +1 -0
- package/dist/strapi/providers/local-destination/index.mjs +335 -0
- package/dist/strapi/providers/local-destination/index.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.d.ts +1 -2
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.d.ts.map +1 -1
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.js +58 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.mjs +51 -0
- package/dist/strapi/providers/local-destination/strategies/restore/configuration.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.d.ts +1 -2
- package/dist/strapi/providers/local-destination/strategies/restore/entities.d.ts.map +1 -1
- package/dist/strapi/providers/local-destination/strategies/restore/entities.js +54 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.mjs +52 -0
- package/dist/strapi/providers/local-destination/strategies/restore/entities.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.d.ts.map +1 -1
- package/dist/strapi/providers/local-destination/strategies/restore/index.js +124 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.mjs +120 -0
- package/dist/strapi/providers/local-destination/strategies/restore/index.mjs.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.d.ts +1 -2
- package/dist/strapi/providers/local-destination/strategies/restore/links.d.ts.map +1 -1
- package/dist/strapi/providers/local-destination/strategies/restore/links.js +79 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.js.map +1 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.mjs +77 -0
- package/dist/strapi/providers/local-destination/strategies/restore/links.mjs.map +1 -0
- package/dist/strapi/providers/local-source/assets.d.ts +8 -2
- package/dist/strapi/providers/local-source/assets.d.ts.map +1 -1
- package/dist/strapi/providers/local-source/assets.js +151 -0
- package/dist/strapi/providers/local-source/assets.js.map +1 -0
- package/dist/strapi/providers/local-source/assets.mjs +147 -0
- package/dist/strapi/providers/local-source/assets.mjs.map +1 -0
- package/dist/strapi/providers/local-source/configuration.d.ts +0 -1
- package/dist/strapi/providers/local-source/configuration.d.ts.map +1 -1
- package/dist/strapi/providers/local-source/configuration.js +47 -0
- package/dist/strapi/providers/local-source/configuration.js.map +1 -0
- package/dist/strapi/providers/local-source/configuration.mjs +45 -0
- package/dist/strapi/providers/local-source/configuration.mjs.map +1 -0
- package/dist/strapi/providers/local-source/entities.d.ts +3 -2
- package/dist/strapi/providers/local-source/entities.d.ts.map +1 -1
- package/dist/strapi/providers/local-source/entities.js +66 -0
- package/dist/strapi/providers/local-source/entities.js.map +1 -0
- package/dist/strapi/providers/local-source/entities.mjs +63 -0
- package/dist/strapi/providers/local-source/entities.mjs.map +1 -0
- package/dist/strapi/providers/local-source/estimate-asset-totals.d.ts +11 -0
- package/dist/strapi/providers/local-source/estimate-asset-totals.d.ts.map +1 -0
- package/dist/strapi/providers/local-source/estimate-asset-totals.js +130 -0
- package/dist/strapi/providers/local-source/estimate-asset-totals.js.map +1 -0
- package/dist/strapi/providers/local-source/estimate-asset-totals.mjs +128 -0
- package/dist/strapi/providers/local-source/estimate-asset-totals.mjs.map +1 -0
- package/dist/strapi/providers/local-source/index.d.ts +6 -4
- package/dist/strapi/providers/local-source/index.d.ts.map +1 -1
- package/dist/strapi/providers/local-source/index.js +186 -0
- package/dist/strapi/providers/local-source/index.js.map +1 -0
- package/dist/strapi/providers/local-source/index.mjs +183 -0
- package/dist/strapi/providers/local-source/index.mjs.map +1 -0
- package/dist/strapi/providers/local-source/links.d.ts +0 -1
- package/dist/strapi/providers/local-source/links.d.ts.map +1 -1
- package/dist/strapi/providers/local-source/links.js +26 -0
- package/dist/strapi/providers/local-source/links.js.map +1 -0
- package/dist/strapi/providers/local-source/links.mjs +24 -0
- package/dist/strapi/providers/local-source/links.mjs.map +1 -0
- package/dist/strapi/providers/remote-destination/index.d.ts +13 -5
- package/dist/strapi/providers/remote-destination/index.d.ts.map +1 -1
- package/dist/strapi/providers/remote-destination/index.js +482 -0
- package/dist/strapi/providers/remote-destination/index.js.map +1 -0
- package/dist/strapi/providers/remote-destination/index.mjs +480 -0
- package/dist/strapi/providers/remote-destination/index.mjs.map +1 -0
- package/dist/strapi/providers/remote-source/index.d.ts +11 -6
- package/dist/strapi/providers/remote-source/index.d.ts.map +1 -1
- package/dist/strapi/providers/remote-source/index.js +538 -0
- package/dist/strapi/providers/remote-source/index.js.map +1 -0
- package/dist/strapi/providers/remote-source/index.mjs +536 -0
- package/dist/strapi/providers/remote-source/index.mjs.map +1 -0
- package/dist/strapi/providers/utils.d.ts +22 -16
- package/dist/strapi/providers/utils.d.ts.map +1 -1
- package/dist/strapi/providers/utils.js +178 -0
- package/dist/strapi/providers/utils.js.map +1 -0
- package/dist/strapi/providers/utils.mjs +174 -0
- package/dist/strapi/providers/utils.mjs.map +1 -0
- package/dist/strapi/queries/entity.d.ts.map +1 -1
- package/dist/strapi/queries/entity.js +125 -0
- package/dist/strapi/queries/entity.js.map +1 -0
- package/dist/strapi/queries/entity.mjs +123 -0
- package/dist/strapi/queries/entity.mjs.map +1 -0
- package/dist/strapi/queries/index.js +10 -0
- package/dist/strapi/queries/index.js.map +1 -0
- package/dist/strapi/queries/index.mjs +5 -0
- package/dist/strapi/queries/index.mjs.map +1 -0
- package/dist/strapi/queries/link.d.ts +1 -1
- package/dist/strapi/queries/link.d.ts.map +1 -1
- package/dist/strapi/queries/link.js +298 -0
- package/dist/strapi/queries/link.js.map +1 -0
- package/dist/strapi/queries/link.mjs +295 -0
- package/dist/strapi/queries/link.mjs.map +1 -0
- package/dist/strapi/remote/constants.d.ts.map +1 -1
- package/dist/strapi/remote/constants.js +11 -0
- package/dist/strapi/remote/constants.js.map +1 -0
- package/dist/strapi/remote/constants.mjs +8 -0
- package/dist/strapi/remote/constants.mjs.map +1 -0
- package/dist/strapi/remote/flows/default.d.ts.map +1 -1
- package/dist/strapi/remote/flows/default.js +43 -0
- package/dist/strapi/remote/flows/default.js.map +1 -0
- package/dist/strapi/remote/flows/default.mjs +41 -0
- package/dist/strapi/remote/flows/default.mjs.map +1 -0
- package/dist/strapi/remote/flows/index.d.ts +1 -1
- package/dist/strapi/remote/flows/index.d.ts.map +1 -1
- package/dist/strapi/remote/flows/index.js +54 -0
- package/dist/strapi/remote/flows/index.js.map +1 -0
- package/dist/strapi/remote/flows/index.mjs +52 -0
- package/dist/strapi/remote/flows/index.mjs.map +1 -0
- package/dist/strapi/remote/handlers/abstract.d.ts +4 -1
- package/dist/strapi/remote/handlers/abstract.d.ts.map +1 -1
- package/dist/strapi/remote/handlers/constants.js +10 -0
- package/dist/strapi/remote/handlers/constants.js.map +1 -0
- package/dist/strapi/remote/handlers/constants.mjs +8 -0
- package/dist/strapi/remote/handlers/constants.mjs.map +1 -0
- package/dist/strapi/remote/handlers/index.js +12 -0
- package/dist/strapi/remote/handlers/index.js.map +1 -0
- package/dist/strapi/remote/handlers/index.mjs +4 -0
- package/dist/strapi/remote/handlers/index.mjs.map +1 -0
- package/dist/strapi/remote/handlers/pull.d.ts +2 -3
- package/dist/strapi/remote/handlers/pull.d.ts.map +1 -1
- package/dist/strapi/remote/handlers/pull.js +369 -0
- package/dist/strapi/remote/handlers/pull.js.map +1 -0
- package/dist/strapi/remote/handlers/pull.mjs +367 -0
- package/dist/strapi/remote/handlers/pull.mjs.map +1 -0
- package/dist/strapi/remote/handlers/push.d.ts +16 -5
- package/dist/strapi/remote/handlers/push.d.ts.map +1 -1
- package/dist/strapi/remote/handlers/push.js +454 -0
- package/dist/strapi/remote/handlers/push.js.map +1 -0
- package/dist/strapi/remote/handlers/push.mjs +452 -0
- package/dist/strapi/remote/handlers/push.mjs.map +1 -0
- package/dist/strapi/remote/handlers/utils.d.ts +1 -2
- package/dist/strapi/remote/handlers/utils.d.ts.map +1 -1
- package/dist/strapi/remote/handlers/utils.js +318 -0
- package/dist/strapi/remote/handlers/utils.js.map +1 -0
- package/dist/strapi/remote/handlers/utils.mjs +312 -0
- package/dist/strapi/remote/handlers/utils.mjs.map +1 -0
- package/dist/strapi/remote/index.js +10 -0
- package/dist/strapi/remote/index.js.map +1 -0
- package/dist/strapi/remote/index.mjs +5 -0
- package/dist/strapi/remote/index.mjs.map +1 -0
- package/dist/strapi/utils/project-settings-logos.d.ts +29 -0
- package/dist/strapi/utils/project-settings-logos.d.ts.map +1 -0
- package/dist/strapi/utils/project-settings-logos.js +174 -0
- package/dist/strapi/utils/project-settings-logos.js.map +1 -0
- package/dist/strapi/utils/project-settings-logos.mjs +169 -0
- package/dist/strapi/utils/project-settings-logos.mjs.map +1 -0
- package/dist/types/common-entities.d.ts +149 -0
- package/dist/types/common-entities.d.ts.map +1 -0
- package/dist/types/encryption.d.ts +13 -0
- package/dist/types/encryption.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/providers.d.ts +49 -0
- package/dist/types/providers.d.ts.map +1 -0
- package/dist/types/remote/index.d.ts +2 -0
- package/dist/types/remote/index.d.ts.map +1 -0
- package/dist/types/remote/protocol/auth.d.ts +5 -0
- package/dist/types/remote/protocol/auth.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/commands.d.ts +37 -0
- package/dist/types/remote/protocol/client/commands.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/index.d.ts +9 -0
- package/dist/types/remote/protocol/client/index.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/transfer/action.d.ts +5 -0
- package/dist/types/remote/protocol/client/transfer/action.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/transfer/index.d.ts +12 -0
- package/dist/types/remote/protocol/client/transfer/index.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/transfer/pull.d.ts +25 -0
- package/dist/types/remote/protocol/client/transfer/pull.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/transfer/push.d.ts +29 -0
- package/dist/types/remote/protocol/client/transfer/push.d.ts.map +1 -0
- package/dist/types/remote/protocol/client/transfer/utils.d.ts +47 -0
- package/dist/types/remote/protocol/client/transfer/utils.d.ts.map +1 -0
- package/dist/types/remote/protocol/index.d.ts +4 -0
- package/dist/types/remote/protocol/index.d.ts.map +1 -0
- package/dist/types/remote/protocol/server/error.d.ts +18 -0
- package/dist/types/remote/protocol/server/error.d.ts.map +1 -0
- package/dist/types/remote/protocol/server/index.d.ts +3 -0
- package/dist/types/remote/protocol/server/index.d.ts.map +1 -0
- package/dist/types/remote/protocol/server/messaging.d.ts +37 -0
- package/dist/types/remote/protocol/server/messaging.d.ts.map +1 -0
- package/dist/types/transfer-engine.d.ts +148 -0
- package/dist/types/transfer-engine.d.ts.map +1 -0
- package/dist/types/utils.d.ts +82 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils/components.d.ts +23 -3
- package/dist/utils/components.d.ts.map +1 -1
- package/dist/utils/components.js +230 -0
- package/dist/utils/components.js.map +1 -0
- package/dist/utils/components.mjs +219 -0
- package/dist/utils/components.mjs.map +1 -0
- package/dist/{engine → utils}/diagnostic.d.ts +1 -0
- package/dist/utils/diagnostic.d.ts.map +1 -0
- package/dist/utils/diagnostic.js +51 -0
- package/dist/utils/diagnostic.js.map +1 -0
- package/dist/utils/diagnostic.mjs +49 -0
- package/dist/utils/diagnostic.mjs.map +1 -0
- package/dist/utils/encryption/decrypt.d.ts +6 -5
- package/dist/utils/encryption/decrypt.d.ts.map +1 -1
- package/dist/utils/encryption/decrypt.js +47 -0
- package/dist/utils/encryption/decrypt.js.map +1 -0
- package/dist/utils/encryption/decrypt.mjs +45 -0
- package/dist/utils/encryption/decrypt.mjs.map +1 -0
- package/dist/utils/encryption/encrypt.d.ts +6 -5
- package/dist/utils/encryption/encrypt.d.ts.map +1 -1
- package/dist/utils/encryption/encrypt.js +47 -0
- package/dist/utils/encryption/encrypt.js.map +1 -0
- package/dist/utils/encryption/encrypt.mjs +45 -0
- package/dist/utils/encryption/encrypt.mjs.map +1 -0
- package/dist/utils/encryption/index.js +10 -0
- package/dist/utils/encryption/index.js.map +1 -0
- package/dist/utils/encryption/index.mjs +3 -0
- package/dist/utils/encryption/index.mjs.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +28 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +17 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/json.d.ts.map +1 -1
- package/dist/utils/json.js +96 -0
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/json.mjs +94 -0
- package/dist/utils/json.mjs.map +1 -0
- package/dist/utils/middleware.d.ts +1 -1
- package/dist/utils/middleware.d.ts.map +1 -1
- package/dist/utils/middleware.js +14 -0
- package/dist/utils/middleware.js.map +1 -0
- package/dist/utils/middleware.mjs +12 -0
- package/dist/utils/middleware.mjs.map +1 -0
- package/dist/utils/providers.js +12 -0
- package/dist/utils/providers.js.map +1 -0
- package/dist/utils/providers.mjs +10 -0
- package/dist/utils/providers.mjs.map +1 -0
- package/dist/utils/schema.d.ts.map +1 -1
- package/dist/utils/schema.js +32 -0
- package/dist/utils/schema.js.map +1 -0
- package/dist/utils/schema.mjs +29 -0
- package/dist/utils/schema.mjs.map +1 -0
- package/dist/utils/stream.d.ts +0 -1
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/stream.js +96 -0
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/stream.mjs +92 -0
- package/dist/utils/stream.mjs.map +1 -0
- package/dist/utils/transaction.d.ts +1 -1
- package/dist/utils/transaction.d.ts.map +1 -1
- package/dist/utils/transaction.js +93 -0
- package/dist/utils/transaction.js.map +1 -0
- package/dist/utils/transaction.mjs +91 -0
- package/dist/utils/transaction.mjs.map +1 -0
- package/dist/utils/transfer-asset-chunk.d.ts +61 -0
- package/dist/utils/transfer-asset-chunk.d.ts.map +1 -0
- package/dist/utils/transfer-asset-chunk.js +118 -0
- package/dist/utils/transfer-asset-chunk.js.map +1 -0
- package/dist/utils/transfer-asset-chunk.mjs +112 -0
- package/dist/utils/transfer-asset-chunk.mjs.map +1 -0
- package/dist/utils/transfer-websocket-json.d.ts +24 -0
- package/dist/utils/transfer-websocket-json.d.ts.map +1 -0
- package/dist/utils/transfer-websocket-json.js +67 -0
- package/dist/utils/transfer-websocket-json.js.map +1 -0
- package/dist/utils/transfer-websocket-json.mjs +63 -0
- package/dist/utils/transfer-websocket-json.mjs.map +1 -0
- package/dist/utils/writable-async-write.d.ts +16 -0
- package/dist/utils/writable-async-write.d.ts.map +1 -0
- package/dist/utils/writable-async-write.js +61 -0
- package/dist/utils/writable-async-write.js.map +1 -0
- package/dist/utils/writable-async-write.mjs +59 -0
- package/dist/utils/writable-async-write.mjs.map +1 -0
- package/package.json +40 -26
- package/dist/engine/diagnostic.d.ts.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4383 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
import * as
|
|
10
|
-
|
|
11
|
-
import _ from "lodash";
|
|
12
|
-
import { contentTypes, async } from "@strapi/utils";
|
|
13
|
-
import chalk from "chalk";
|
|
14
|
-
import * as webStream from "stream/web";
|
|
15
|
-
import { WebSocket } from "ws";
|
|
16
|
-
import zip$1 from "zlib";
|
|
17
|
-
import tar from "tar";
|
|
18
|
-
import { parser } from "stream-json/jsonl/Parser";
|
|
19
|
-
import tar$1 from "tar-stream";
|
|
20
|
-
import { stringer } from "stream-json/jsonl/Stringer";
|
|
21
|
-
const getEncryptionStrategy = (algorithm) => {
|
|
22
|
-
const strategies2 = {
|
|
23
|
-
"aes-128-ecb"(key) {
|
|
24
|
-
const hashedKey = scryptSync(key, "", 16);
|
|
25
|
-
const initVector = null;
|
|
26
|
-
const securityKey = hashedKey;
|
|
27
|
-
return createCipheriv(algorithm, securityKey, initVector);
|
|
28
|
-
},
|
|
29
|
-
aes128(key) {
|
|
30
|
-
const hashedKey = scryptSync(key, "", 32);
|
|
31
|
-
const initVector = hashedKey.slice(16);
|
|
32
|
-
const securityKey = hashedKey.slice(0, 16);
|
|
33
|
-
return createCipheriv(algorithm, securityKey, initVector);
|
|
34
|
-
},
|
|
35
|
-
aes192(key) {
|
|
36
|
-
const hashedKey = scryptSync(key, "", 40);
|
|
37
|
-
const initVector = hashedKey.slice(24);
|
|
38
|
-
const securityKey = hashedKey.slice(0, 24);
|
|
39
|
-
return createCipheriv(algorithm, securityKey, initVector);
|
|
40
|
-
},
|
|
41
|
-
aes256(key) {
|
|
42
|
-
const hashedKey = scryptSync(key, "", 48);
|
|
43
|
-
const initVector = hashedKey.slice(32);
|
|
44
|
-
const securityKey = hashedKey.slice(0, 32);
|
|
45
|
-
return createCipheriv(algorithm, securityKey, initVector);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
return strategies2[algorithm];
|
|
49
|
-
};
|
|
50
|
-
const createEncryptionCipher = (key, algorithm = "aes-128-ecb") => {
|
|
51
|
-
return getEncryptionStrategy(algorithm)(key);
|
|
52
|
-
};
|
|
53
|
-
const getDecryptionStrategy = (algorithm) => {
|
|
54
|
-
const strategies2 = {
|
|
55
|
-
"aes-128-ecb"(key) {
|
|
56
|
-
const hashedKey = scryptSync(key, "", 16);
|
|
57
|
-
const initVector = null;
|
|
58
|
-
const securityKey = hashedKey;
|
|
59
|
-
return createDecipheriv(algorithm, securityKey, initVector);
|
|
60
|
-
},
|
|
61
|
-
aes128(key) {
|
|
62
|
-
const hashedKey = scryptSync(key, "", 32);
|
|
63
|
-
const initVector = hashedKey.slice(16);
|
|
64
|
-
const securityKey = hashedKey.slice(0, 16);
|
|
65
|
-
return createDecipheriv(algorithm, securityKey, initVector);
|
|
66
|
-
},
|
|
67
|
-
aes192(key) {
|
|
68
|
-
const hashedKey = scryptSync(key, "", 40);
|
|
69
|
-
const initVector = hashedKey.slice(24);
|
|
70
|
-
const securityKey = hashedKey.slice(0, 24);
|
|
71
|
-
return createDecipheriv(algorithm, securityKey, initVector);
|
|
72
|
-
},
|
|
73
|
-
aes256(key) {
|
|
74
|
-
const hashedKey = scryptSync(key, "", 48);
|
|
75
|
-
const initVector = hashedKey.slice(32);
|
|
76
|
-
const securityKey = hashedKey.slice(0, 32);
|
|
77
|
-
return createDecipheriv(algorithm, securityKey, initVector);
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
return strategies2[algorithm];
|
|
81
|
-
};
|
|
82
|
-
const createDecryptionCipher = (key, algorithm = "aes-128-ecb") => {
|
|
83
|
-
return getDecryptionStrategy(algorithm)(key);
|
|
84
|
-
};
|
|
85
|
-
const index$9 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
86
|
-
__proto__: null,
|
|
87
|
-
createDecryptionCipher,
|
|
88
|
-
createEncryptionCipher
|
|
89
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
90
|
-
const filter = (predicate, options = { objectMode: true }) => {
|
|
91
|
-
return new Transform({
|
|
92
|
-
...options,
|
|
93
|
-
async transform(chunk, _encoding, callback) {
|
|
94
|
-
const keep = await predicate(chunk);
|
|
95
|
-
callback(null, keep ? chunk : void 0);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
const map = (predicate, options = { objectMode: true }) => {
|
|
100
|
-
return new Transform({
|
|
101
|
-
...options,
|
|
102
|
-
async transform(chunk, _encoding, callback) {
|
|
103
|
-
const mappedValue = await predicate(chunk);
|
|
104
|
-
callback(null, mappedValue);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
};
|
|
108
|
-
const collect = (stream2, options = { destroy: true }) => {
|
|
109
|
-
const chunks = [];
|
|
110
|
-
return new Promise((resolve, reject2) => {
|
|
111
|
-
stream2.on("close", () => resolve(chunks)).on("error", reject2).on("data", (chunk) => chunks.push(chunk)).on("end", () => {
|
|
112
|
-
if (options.destroy) {
|
|
113
|
-
stream2.destroy();
|
|
114
|
-
}
|
|
115
|
-
resolve(chunks);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
const stream = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
120
|
-
__proto__: null,
|
|
121
|
-
collect,
|
|
122
|
-
filter,
|
|
123
|
-
map
|
|
124
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
125
|
-
const createContext = () => ({ path: [] });
|
|
126
|
-
const diff = (a, b, ctx = createContext()) => {
|
|
127
|
-
const diffs = [];
|
|
128
|
-
const { path: path2 } = ctx;
|
|
129
|
-
const aType = typeof a;
|
|
130
|
-
const bType = typeof b;
|
|
131
|
-
const added = () => {
|
|
132
|
-
diffs.push({ kind: "added", path: path2, type: bType, value: b });
|
|
133
|
-
return diffs;
|
|
134
|
-
};
|
|
135
|
-
const deleted = () => {
|
|
136
|
-
diffs.push({ kind: "deleted", path: path2, type: aType, value: a });
|
|
137
|
-
return diffs;
|
|
138
|
-
};
|
|
139
|
-
const modified = () => {
|
|
140
|
-
diffs.push({
|
|
141
|
-
kind: "modified",
|
|
142
|
-
path: path2,
|
|
143
|
-
types: [aType, bType],
|
|
144
|
-
values: [a, b]
|
|
145
|
-
});
|
|
146
|
-
return diffs;
|
|
147
|
-
};
|
|
148
|
-
if (isArray(a) && isArray(b)) {
|
|
149
|
-
let k = 0;
|
|
150
|
-
for (const [aItem, bItem] of zip(a, b)) {
|
|
151
|
-
const kCtx = { path: [...path2, k.toString()] };
|
|
152
|
-
const kDiffs = diff(aItem, bItem, kCtx);
|
|
153
|
-
diffs.push(...kDiffs);
|
|
154
|
-
k += 1;
|
|
155
|
-
}
|
|
156
|
-
return diffs;
|
|
157
|
-
}
|
|
158
|
-
if (isObject(a) && isObject(b)) {
|
|
159
|
-
const keys = uniq(Object.keys(a).concat(Object.keys(b)));
|
|
160
|
-
for (const key of keys) {
|
|
161
|
-
const aValue = a[key];
|
|
162
|
-
const bValue = b[key];
|
|
163
|
-
const nestedDiffs = diff(aValue, bValue, { path: [...path2, key] });
|
|
164
|
-
diffs.push(...nestedDiffs);
|
|
165
|
-
}
|
|
166
|
-
return diffs;
|
|
167
|
-
}
|
|
168
|
-
if (!isEqual(a, b)) {
|
|
169
|
-
if (aType === "undefined") {
|
|
170
|
-
return added();
|
|
171
|
-
}
|
|
172
|
-
if (bType === "undefined") {
|
|
173
|
-
return deleted();
|
|
174
|
-
}
|
|
175
|
-
return modified();
|
|
176
|
-
}
|
|
177
|
-
return diffs;
|
|
178
|
-
};
|
|
179
|
-
const json = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
180
|
-
__proto__: null,
|
|
181
|
-
diff
|
|
182
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
183
|
-
const VALID_SCHEMA_PROPERTIES = [
|
|
184
|
-
"collectionName",
|
|
185
|
-
"info",
|
|
186
|
-
"options",
|
|
187
|
-
"pluginOptions",
|
|
188
|
-
"attributes",
|
|
189
|
-
"kind",
|
|
190
|
-
"modelType",
|
|
191
|
-
"modelName",
|
|
192
|
-
"uid",
|
|
193
|
-
"plugin",
|
|
194
|
-
"globalId"
|
|
195
|
-
];
|
|
196
|
-
const mapSchemasValues = (schemas) => {
|
|
197
|
-
return mapValues(pick(VALID_SCHEMA_PROPERTIES), schemas);
|
|
198
|
-
};
|
|
199
|
-
const schemasToValidJSON = (schemas) => {
|
|
200
|
-
return JSON.parse(JSON.stringify(schemas));
|
|
201
|
-
};
|
|
202
|
-
const schema = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
203
|
-
__proto__: null,
|
|
204
|
-
mapSchemasValues,
|
|
205
|
-
schemasToValidJSON
|
|
206
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
207
|
-
const createTransaction = (strapi2) => {
|
|
208
|
-
const fns = [];
|
|
209
|
-
let done = false;
|
|
210
|
-
let resume = null;
|
|
211
|
-
const e = new EventEmitter();
|
|
212
|
-
e.on("spawn", (uuid, cb) => {
|
|
213
|
-
fns.push({ fn: cb, uuid });
|
|
214
|
-
resume?.();
|
|
215
|
-
});
|
|
216
|
-
e.on("close", () => {
|
|
217
|
-
e.removeAllListeners("rollback");
|
|
218
|
-
e.removeAllListeners("spawn");
|
|
219
|
-
done = true;
|
|
220
|
-
resume?.();
|
|
221
|
-
});
|
|
222
|
-
strapi2.db.transaction(async ({ trx, rollback }) => {
|
|
223
|
-
e.once("rollback", async () => {
|
|
224
|
-
e.removeAllListeners("close");
|
|
225
|
-
e.removeAllListeners("spawn");
|
|
226
|
-
try {
|
|
227
|
-
await rollback();
|
|
228
|
-
e.emit("rollback_completed");
|
|
229
|
-
} catch {
|
|
230
|
-
e.emit("rollback_failed");
|
|
231
|
-
} finally {
|
|
232
|
-
done = true;
|
|
233
|
-
resume?.();
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
while (!done) {
|
|
237
|
-
while (fns.length) {
|
|
238
|
-
const item = fns.shift();
|
|
239
|
-
if (item) {
|
|
240
|
-
const { fn, uuid } = item;
|
|
241
|
-
try {
|
|
242
|
-
const res = await fn(trx);
|
|
243
|
-
e.emit(uuid, { data: res });
|
|
244
|
-
} catch (error) {
|
|
245
|
-
e.emit(uuid, { error });
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
if (!done && !fns.length) {
|
|
250
|
-
await new Promise((resolve) => {
|
|
251
|
-
resume = resolve;
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
});
|
|
256
|
-
return {
|
|
257
|
-
async attach(callback) {
|
|
258
|
-
const uuid = randomUUID();
|
|
259
|
-
e.emit("spawn", uuid, callback);
|
|
260
|
-
return new Promise((resolve, reject2) => {
|
|
261
|
-
e.on(uuid, ({ data, error }) => {
|
|
262
|
-
if (data) {
|
|
263
|
-
resolve(data);
|
|
264
|
-
}
|
|
265
|
-
if (error) {
|
|
266
|
-
reject2(error);
|
|
267
|
-
}
|
|
268
|
-
resolve(void 0);
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
},
|
|
272
|
-
end() {
|
|
273
|
-
return e.emit("close");
|
|
274
|
-
},
|
|
275
|
-
rollback() {
|
|
276
|
-
return new Promise((resolve) => {
|
|
277
|
-
e.emit("rollback");
|
|
278
|
-
e.once("rollback_failed", () => resolve(false));
|
|
279
|
-
e.once("rollback_completed", () => resolve(true));
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
};
|
|
284
|
-
const transaction = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
285
|
-
__proto__: null,
|
|
286
|
-
createTransaction
|
|
287
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
288
|
-
const runMiddleware = async (context, middlewares) => {
|
|
289
|
-
if (!middlewares.length) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
const cb = middlewares[0];
|
|
293
|
-
await cb(context, async (newContext) => {
|
|
294
|
-
await runMiddleware(newContext, middlewares.slice(1));
|
|
295
|
-
});
|
|
296
|
-
};
|
|
297
|
-
const middleware = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
298
|
-
__proto__: null,
|
|
299
|
-
runMiddleware
|
|
300
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
301
|
-
const index$8 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
302
|
-
__proto__: null,
|
|
303
|
-
encryption: index$9,
|
|
304
|
-
json,
|
|
305
|
-
middleware,
|
|
306
|
-
schema,
|
|
307
|
-
stream,
|
|
308
|
-
transaction
|
|
309
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
310
|
-
const OPTIONAL_CONTENT_TYPES = ["audit-log"];
|
|
311
|
-
const isAttributeIgnorable = (diff2) => {
|
|
312
|
-
return diff2.path.length === 3 && // Root property must be attributes
|
|
313
|
-
diff2.path[0] === "attributes" && // Need a valid string attribute name
|
|
314
|
-
typeof diff2.path[1] === "string" && // The diff must be on ignorable attribute properties
|
|
315
|
-
["private", "required", "configurable", "default"].includes(diff2.path[2]);
|
|
316
|
-
};
|
|
317
|
-
const isOptionalAdminType = (diff2) => {
|
|
318
|
-
if ("value" in diff2 && isObject(diff2.value)) {
|
|
319
|
-
const name = diff2?.value?.info?.singularName;
|
|
320
|
-
return OPTIONAL_CONTENT_TYPES.includes(name);
|
|
321
|
-
}
|
|
322
|
-
if ("values" in diff2 && isArray(diff2.values) && isObject(diff2.values[0])) {
|
|
323
|
-
const name = diff2?.values[0]?.info?.singularName;
|
|
324
|
-
return OPTIONAL_CONTENT_TYPES.includes(name);
|
|
325
|
-
}
|
|
326
|
-
return false;
|
|
327
|
-
};
|
|
328
|
-
const isIgnorableStrict = (diff2) => isAttributeIgnorable(diff2) || isOptionalAdminType(diff2);
|
|
329
|
-
const strategies = {
|
|
330
|
-
// No diffs
|
|
331
|
-
exact(diffs) {
|
|
332
|
-
return diffs;
|
|
333
|
-
},
|
|
334
|
-
// Strict: all content types must match except:
|
|
335
|
-
// - the property within a content type is an ignorable one
|
|
336
|
-
// - those that are (not transferrable and optionally available), for example EE features such as audit logs
|
|
337
|
-
strict(diffs) {
|
|
338
|
-
return reject$1(isIgnorableStrict, diffs);
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
const compareSchemas = (a, b, strategy) => {
|
|
342
|
-
const diffs = diff(a, b);
|
|
343
|
-
return strategies[strategy](diffs);
|
|
344
|
-
};
|
|
345
|
-
const SeverityKind = {
|
|
346
|
-
FATAL: "fatal",
|
|
347
|
-
ERROR: "error",
|
|
348
|
-
SILLY: "silly"
|
|
349
|
-
};
|
|
350
|
-
class DataTransferError extends Error {
|
|
351
|
-
origin;
|
|
352
|
-
severity;
|
|
353
|
-
details;
|
|
354
|
-
constructor(origin2, severity, message, details) {
|
|
355
|
-
super(message);
|
|
356
|
-
this.origin = origin2;
|
|
357
|
-
this.severity = severity;
|
|
358
|
-
this.details = details ?? null;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
class TransferEngineError extends DataTransferError {
|
|
362
|
-
constructor(severity, message, details) {
|
|
363
|
-
super("engine", severity, message, details);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
class TransferEngineInitializationError extends TransferEngineError {
|
|
367
|
-
constructor(message) {
|
|
368
|
-
super(SeverityKind.FATAL, message, { step: "initialization" });
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
class TransferEngineValidationError extends TransferEngineError {
|
|
372
|
-
constructor(message, details) {
|
|
373
|
-
super(SeverityKind.FATAL, message, { step: "validation", details });
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
class TransferEngineTransferError extends TransferEngineError {
|
|
377
|
-
constructor(message, details) {
|
|
378
|
-
super(SeverityKind.FATAL, message, { step: "transfer", details });
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
const errors = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
382
|
-
__proto__: null,
|
|
383
|
-
TransferEngineError,
|
|
384
|
-
TransferEngineInitializationError,
|
|
385
|
-
TransferEngineTransferError,
|
|
386
|
-
TransferEngineValidationError
|
|
387
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
388
|
-
const reject = (reason) => {
|
|
389
|
-
throw new TransferEngineValidationError(`Invalid provider supplied. ${reason}`);
|
|
390
|
-
};
|
|
391
|
-
const validateProvider = (type, provider) => {
|
|
392
|
-
if (!provider) {
|
|
393
|
-
return reject(
|
|
394
|
-
`Expected an instance of "${capitalize(type)}Provider", but got "${typeof provider}" instead.`
|
|
395
|
-
);
|
|
396
|
-
}
|
|
397
|
-
if (provider.type !== type) {
|
|
398
|
-
return reject(
|
|
399
|
-
`Expected the provider to be of type "${type}" but got "${provider.type}" instead.`
|
|
400
|
-
);
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
const createDiagnosticReporter = (options = {}) => {
|
|
404
|
-
const { stackSize = -1 } = options;
|
|
405
|
-
const emitter = new EventEmitter();
|
|
406
|
-
const stack = [];
|
|
407
|
-
const addListener = (event, listener) => {
|
|
408
|
-
emitter.on(event, listener);
|
|
409
|
-
};
|
|
410
|
-
const isDiagnosticValid = (diagnostic) => {
|
|
411
|
-
if (!diagnostic.kind || !diagnostic.details || !diagnostic.details.message) {
|
|
412
|
-
return false;
|
|
413
|
-
}
|
|
414
|
-
return true;
|
|
415
|
-
};
|
|
416
|
-
return {
|
|
417
|
-
stack: {
|
|
418
|
-
get size() {
|
|
419
|
-
return stack.length;
|
|
420
|
-
},
|
|
421
|
-
get items() {
|
|
422
|
-
return stack;
|
|
423
|
-
}
|
|
424
|
-
},
|
|
425
|
-
report(diagnostic) {
|
|
426
|
-
if (!isDiagnosticValid(diagnostic)) {
|
|
427
|
-
return this;
|
|
428
|
-
}
|
|
429
|
-
emitter.emit("diagnostic", diagnostic);
|
|
430
|
-
emitter.emit(`diagnostic.${diagnostic.kind}`, diagnostic);
|
|
431
|
-
if (stackSize !== -1 && stack.length >= stackSize) {
|
|
432
|
-
stack.shift();
|
|
433
|
-
}
|
|
434
|
-
stack.push(diagnostic);
|
|
435
|
-
return this;
|
|
436
|
-
},
|
|
437
|
-
onDiagnostic(listener) {
|
|
438
|
-
addListener("diagnostic", listener);
|
|
439
|
-
return this;
|
|
440
|
-
},
|
|
441
|
-
on(kind, listener) {
|
|
442
|
-
addListener(`diagnostic.${kind}`, listener);
|
|
443
|
-
return this;
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
};
|
|
447
|
-
class ProviderError extends DataTransferError {
|
|
448
|
-
constructor(severity, message, details) {
|
|
449
|
-
super("provider", severity, message, details);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
class ProviderInitializationError extends ProviderError {
|
|
453
|
-
constructor(message) {
|
|
454
|
-
super(SeverityKind.FATAL, message, { step: "initialization" });
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
class ProviderValidationError extends ProviderError {
|
|
458
|
-
constructor(message, details) {
|
|
459
|
-
super(SeverityKind.SILLY, message, { step: "validation", details });
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
class ProviderTransferError extends ProviderError {
|
|
463
|
-
constructor(message, details) {
|
|
464
|
-
super(SeverityKind.FATAL, message, { step: "transfer", details });
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
const TRANSFER_STAGES = Object.freeze([
|
|
468
|
-
"entities",
|
|
469
|
-
"links",
|
|
470
|
-
"assets",
|
|
471
|
-
"schemas",
|
|
472
|
-
"configuration"
|
|
473
|
-
]);
|
|
474
|
-
const TransferGroupPresets = {
|
|
475
|
-
content: {
|
|
476
|
-
links: true,
|
|
477
|
-
// Example: content includes the entire links stage
|
|
478
|
-
entities: true
|
|
479
|
-
// TODO: If we need to implement filtering on a running stage, it would be done like this, but we still need to implement it
|
|
480
|
-
// [
|
|
481
|
-
// // Example: content processes the entities stage, but filters individual entities
|
|
482
|
-
// {
|
|
483
|
-
// filter(data) {
|
|
484
|
-
// return shouldIncludeThisData(data);
|
|
485
|
-
// },
|
|
486
|
-
// },
|
|
487
|
-
// ],
|
|
488
|
-
},
|
|
489
|
-
files: {
|
|
490
|
-
assets: true
|
|
491
|
-
},
|
|
492
|
-
config: {
|
|
493
|
-
configuration: true
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
const DEFAULT_VERSION_STRATEGY = "ignore";
|
|
497
|
-
const DEFAULT_SCHEMA_STRATEGY = "strict";
|
|
498
|
-
class TransferEngine {
|
|
499
|
-
sourceProvider;
|
|
500
|
-
destinationProvider;
|
|
501
|
-
options;
|
|
502
|
-
#metadata = {};
|
|
503
|
-
#schema = {};
|
|
504
|
-
// Progress of the current stage
|
|
505
|
-
progress;
|
|
506
|
-
diagnostics;
|
|
507
|
-
#handlers = {
|
|
508
|
-
schemaDiff: [],
|
|
509
|
-
errors: {}
|
|
510
|
-
};
|
|
511
|
-
onSchemaDiff(handler) {
|
|
512
|
-
this.#handlers?.schemaDiff?.push(handler);
|
|
513
|
-
}
|
|
514
|
-
addErrorHandler(handlerName, handler) {
|
|
515
|
-
if (!this.#handlers.errors[handlerName]) {
|
|
516
|
-
this.#handlers.errors[handlerName] = [];
|
|
517
|
-
}
|
|
518
|
-
this.#handlers.errors[handlerName]?.push(handler);
|
|
519
|
-
}
|
|
520
|
-
async attemptResolveError(error) {
|
|
521
|
-
const context = {};
|
|
522
|
-
if (error instanceof ProviderTransferError && error.details?.details.code) {
|
|
523
|
-
const errorCode = error.details?.details.code;
|
|
524
|
-
if (!this.#handlers.errors[errorCode]) {
|
|
525
|
-
this.#handlers.errors[errorCode] = [];
|
|
526
|
-
}
|
|
527
|
-
await runMiddleware(context ?? {}, this.#handlers.errors[errorCode] ?? []);
|
|
528
|
-
}
|
|
529
|
-
return !!context.ignore;
|
|
530
|
-
}
|
|
531
|
-
// Save the currently open stream so that we can access it at any time
|
|
532
|
-
#currentStream;
|
|
533
|
-
constructor(sourceProvider, destinationProvider, options) {
|
|
534
|
-
this.diagnostics = createDiagnosticReporter();
|
|
535
|
-
validateProvider("source", sourceProvider);
|
|
536
|
-
validateProvider("destination", destinationProvider);
|
|
537
|
-
this.sourceProvider = sourceProvider;
|
|
538
|
-
this.destinationProvider = destinationProvider;
|
|
539
|
-
this.options = options;
|
|
540
|
-
this.progress = { data: {}, stream: new PassThrough({ objectMode: true }) };
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Report a fatal error and throw it
|
|
544
|
-
*/
|
|
545
|
-
panic(error) {
|
|
546
|
-
this.reportError(error, "fatal");
|
|
547
|
-
throw error;
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Report an error diagnostic
|
|
551
|
-
*/
|
|
552
|
-
reportError(error, severity) {
|
|
553
|
-
this.diagnostics.report({
|
|
554
|
-
kind: "error",
|
|
555
|
-
details: {
|
|
556
|
-
severity,
|
|
557
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
558
|
-
name: error.name,
|
|
559
|
-
message: error.message,
|
|
560
|
-
error
|
|
561
|
-
}
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
/**
|
|
565
|
-
* Report a warning diagnostic
|
|
566
|
-
*/
|
|
567
|
-
reportWarning(message, origin2) {
|
|
568
|
-
this.diagnostics.report({
|
|
569
|
-
kind: "warning",
|
|
570
|
-
details: { createdAt: /* @__PURE__ */ new Date(), message, origin: origin2 }
|
|
571
|
-
});
|
|
572
|
-
}
|
|
573
|
-
/**
|
|
574
|
-
* Report an info diagnostic
|
|
575
|
-
*/
|
|
576
|
-
reportInfo(message, params) {
|
|
577
|
-
this.diagnostics.report({
|
|
578
|
-
kind: "info",
|
|
579
|
-
details: { createdAt: /* @__PURE__ */ new Date(), message, params }
|
|
580
|
-
});
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Create and return a transform stream based on the given stage and options.
|
|
584
|
-
*
|
|
585
|
-
* Allowed transformations includes 'filter' and 'map'.
|
|
586
|
-
*/
|
|
587
|
-
#createStageTransformStream(key, options = {}) {
|
|
588
|
-
const { includeGlobal = true } = options;
|
|
589
|
-
const { throttle } = this.options;
|
|
590
|
-
const { global: globalTransforms, [key]: stageTransforms } = this.options?.transforms ?? {};
|
|
591
|
-
let stream2 = new PassThrough({ objectMode: true });
|
|
592
|
-
const applyTransforms = (transforms = []) => {
|
|
593
|
-
const chainTransforms = [];
|
|
594
|
-
for (const transform of transforms) {
|
|
595
|
-
if ("filter" in transform) {
|
|
596
|
-
chainTransforms.push(filter(transform.filter));
|
|
597
|
-
}
|
|
598
|
-
if ("map" in transform) {
|
|
599
|
-
chainTransforms.push(map(transform.map));
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
if (chainTransforms.length) {
|
|
603
|
-
stream2 = stream2.pipe(chain(chainTransforms));
|
|
604
|
-
}
|
|
605
|
-
};
|
|
606
|
-
if (includeGlobal) {
|
|
607
|
-
applyTransforms(globalTransforms);
|
|
608
|
-
}
|
|
609
|
-
if (isNumber(throttle) && throttle > 0) {
|
|
610
|
-
stream2 = stream2.pipe(
|
|
611
|
-
new PassThrough({
|
|
612
|
-
objectMode: true,
|
|
613
|
-
async transform(data, _encoding, callback) {
|
|
614
|
-
await new Promise((resolve) => {
|
|
615
|
-
setTimeout(resolve, throttle);
|
|
616
|
-
});
|
|
617
|
-
callback(null, data);
|
|
618
|
-
}
|
|
619
|
-
})
|
|
620
|
-
);
|
|
621
|
-
}
|
|
622
|
-
applyTransforms(stageTransforms);
|
|
623
|
-
return stream2;
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Update the Engine's transfer progress data for a given stage.
|
|
627
|
-
*
|
|
628
|
-
* Providing aggregate options enable custom computation to get the size (bytes) or the aggregate key associated with the data
|
|
629
|
-
*/
|
|
630
|
-
#updateTransferProgress(stage, data, aggregate) {
|
|
631
|
-
if (!this.progress.data[stage]) {
|
|
632
|
-
this.progress.data[stage] = { count: 0, bytes: 0, startTime: Date.now() };
|
|
633
|
-
}
|
|
634
|
-
const stageProgress = this.progress.data[stage];
|
|
635
|
-
if (!stageProgress) {
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
const size2 = aggregate?.size?.(data) ?? JSON.stringify(data).length;
|
|
639
|
-
const key = aggregate?.key?.(data);
|
|
640
|
-
stageProgress.count += 1;
|
|
641
|
-
stageProgress.bytes += size2;
|
|
642
|
-
if (key) {
|
|
643
|
-
if (!stageProgress.aggregates) {
|
|
644
|
-
stageProgress.aggregates = {};
|
|
645
|
-
}
|
|
646
|
-
const { aggregates } = stageProgress;
|
|
647
|
-
if (!aggregates[key]) {
|
|
648
|
-
aggregates[key] = { count: 0, bytes: 0 };
|
|
649
|
-
}
|
|
650
|
-
aggregates[key].count += 1;
|
|
651
|
-
aggregates[key].bytes += size2;
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Create and return a PassThrough stream.
|
|
656
|
-
*
|
|
657
|
-
* Upon writing data into it, it'll update the Engine's transfer progress data and trigger stage update events.
|
|
658
|
-
*/
|
|
659
|
-
#progressTracker(stage, aggregate) {
|
|
660
|
-
return new PassThrough({
|
|
661
|
-
objectMode: true,
|
|
662
|
-
transform: (data, _encoding, callback) => {
|
|
663
|
-
this.#updateTransferProgress(stage, data, aggregate);
|
|
664
|
-
this.#emitStageUpdate("progress", stage);
|
|
665
|
-
callback(null, data);
|
|
666
|
-
}
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
/**
|
|
670
|
-
* Shorthand method used to trigger transfer update events to every listeners
|
|
671
|
-
*/
|
|
672
|
-
#emitTransferUpdate(type, payload) {
|
|
673
|
-
this.progress.stream.emit(`transfer::${type}`, payload);
|
|
674
|
-
}
|
|
675
|
-
/**
|
|
676
|
-
* Shorthand method used to trigger stage update events to every listeners
|
|
677
|
-
*/
|
|
678
|
-
#emitStageUpdate(type, transferStage) {
|
|
679
|
-
this.progress.stream.emit(`stage::${type}`, {
|
|
680
|
-
data: this.progress.data,
|
|
681
|
-
stage: transferStage
|
|
682
|
-
});
|
|
683
|
-
}
|
|
684
|
-
/**
|
|
685
|
-
* Run a version check between two strapi version (source and destination) using the strategy given to the engine during initialization.
|
|
686
|
-
*
|
|
687
|
-
* If there is a mismatch, throws a validation error.
|
|
688
|
-
*/
|
|
689
|
-
#assertStrapiVersionIntegrity(sourceVersion, destinationVersion) {
|
|
690
|
-
const strategy = this.options.versionStrategy || DEFAULT_VERSION_STRATEGY;
|
|
691
|
-
const reject2 = () => {
|
|
692
|
-
throw new TransferEngineValidationError(
|
|
693
|
-
`The source and destination provide are targeting incompatible Strapi versions (using the "${strategy}" strategy). The source (${this.sourceProvider.name}) version is ${sourceVersion} and the destination (${this.destinationProvider.name}) version is ${destinationVersion}`,
|
|
694
|
-
{
|
|
695
|
-
check: "strapi.version",
|
|
696
|
-
strategy,
|
|
697
|
-
versions: { source: sourceVersion, destination: destinationVersion }
|
|
698
|
-
}
|
|
699
|
-
);
|
|
700
|
-
};
|
|
701
|
-
if (!sourceVersion || !destinationVersion || strategy === "ignore" || destinationVersion === sourceVersion) {
|
|
702
|
-
return;
|
|
703
|
-
}
|
|
704
|
-
let diff2;
|
|
705
|
-
try {
|
|
706
|
-
diff2 = diff$1(sourceVersion, destinationVersion);
|
|
707
|
-
} catch {
|
|
708
|
-
reject2();
|
|
709
|
-
}
|
|
710
|
-
if (!diff2) {
|
|
711
|
-
return;
|
|
712
|
-
}
|
|
713
|
-
const validPatch = ["prelease", "build"];
|
|
714
|
-
const validMinor = [...validPatch, "patch", "prepatch"];
|
|
715
|
-
const validMajor = [...validMinor, "minor", "preminor"];
|
|
716
|
-
if (strategy === "patch" && validPatch.includes(diff2)) {
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
if (strategy === "minor" && validMinor.includes(diff2)) {
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
if (strategy === "major" && validMajor.includes(diff2)) {
|
|
723
|
-
return;
|
|
724
|
-
}
|
|
725
|
-
reject2();
|
|
726
|
-
}
|
|
727
|
-
/**
|
|
728
|
-
* Run a check between two set of schemas (source and destination) using the strategy given to the engine during initialization.
|
|
729
|
-
*
|
|
730
|
-
* If there are differences and/or incompatibilities between source and destination schemas, then throw a validation error.
|
|
731
|
-
*/
|
|
732
|
-
#assertSchemasMatching(sourceSchemas, destinationSchemas) {
|
|
733
|
-
const strategy = this.options.schemaStrategy || DEFAULT_SCHEMA_STRATEGY;
|
|
734
|
-
if (strategy === "ignore") {
|
|
735
|
-
return;
|
|
736
|
-
}
|
|
737
|
-
const keys = uniq(Object.keys(sourceSchemas).concat(Object.keys(destinationSchemas)));
|
|
738
|
-
const diffs = {};
|
|
739
|
-
keys.forEach((key) => {
|
|
740
|
-
const sourceSchema = sourceSchemas[key];
|
|
741
|
-
const destinationSchema = destinationSchemas[key];
|
|
742
|
-
const schemaDiffs = compareSchemas(sourceSchema, destinationSchema, strategy);
|
|
743
|
-
if (schemaDiffs.length) {
|
|
744
|
-
diffs[key] = schemaDiffs;
|
|
745
|
-
}
|
|
746
|
-
});
|
|
747
|
-
if (!isEmpty(diffs)) {
|
|
748
|
-
const formattedDiffs = Object.entries(diffs).map(([uid, ctDiffs]) => {
|
|
749
|
-
let msg = `- ${uid}:${EOL}`;
|
|
750
|
-
msg += ctDiffs.sort((a, b) => a.kind > b.kind ? -1 : 1).map((diff2) => {
|
|
751
|
-
const path2 = diff2.path.join(".");
|
|
752
|
-
if (diff2.kind === "added") {
|
|
753
|
-
return `${path2} exists in destination schema but not in source schema and the data will not be transferred.`;
|
|
754
|
-
}
|
|
755
|
-
if (diff2.kind === "deleted") {
|
|
756
|
-
return `${path2} exists in source schema but not in destination schema and the data will not be transferred.`;
|
|
757
|
-
}
|
|
758
|
-
if (diff2.kind === "modified") {
|
|
759
|
-
if (diff2.types[0] === diff2.types[1]) {
|
|
760
|
-
return `Schema value changed at "${path2}": "${diff2.values[0]}" (${diff2.types[0]}) => "${diff2.values[1]}" (${diff2.types[1]})`;
|
|
761
|
-
}
|
|
762
|
-
return `Schema has differing data types at "${path2}": "${diff2.values[0]}" (${diff2.types[0]}) => "${diff2.values[1]}" (${diff2.types[1]})`;
|
|
763
|
-
}
|
|
764
|
-
throw new TransferEngineValidationError(`Invalid diff found for "${uid}"`, {
|
|
765
|
-
check: `schema on ${uid}`
|
|
766
|
-
});
|
|
767
|
-
}).map((line) => ` - ${line}`).join(EOL);
|
|
768
|
-
return msg;
|
|
769
|
-
}).join(EOL);
|
|
770
|
-
throw new TransferEngineValidationError(
|
|
771
|
-
`Invalid schema changes detected during integrity checks (using the ${strategy} strategy). Please find a summary of the changes below:
|
|
772
|
-
${formattedDiffs}`,
|
|
773
|
-
{
|
|
774
|
-
check: "schema.changes",
|
|
775
|
-
strategy,
|
|
776
|
-
diffs
|
|
777
|
-
}
|
|
778
|
-
);
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
shouldSkipStage(stage) {
|
|
782
|
-
const { exclude, only } = this.options;
|
|
783
|
-
if (stage === "schemas") {
|
|
784
|
-
return false;
|
|
785
|
-
}
|
|
786
|
-
let included = isEmpty(only);
|
|
787
|
-
if (only && only.length > 0) {
|
|
788
|
-
included = only.some((transferGroup) => {
|
|
789
|
-
return TransferGroupPresets[transferGroup][stage];
|
|
790
|
-
});
|
|
791
|
-
}
|
|
792
|
-
if (exclude && exclude.length > 0) {
|
|
793
|
-
if (included) {
|
|
794
|
-
included = !exclude.some((transferGroup) => {
|
|
795
|
-
return TransferGroupPresets[transferGroup][stage];
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
return !included;
|
|
800
|
-
}
|
|
801
|
-
async #transferStage(options) {
|
|
802
|
-
const { stage, source, destination, transform, tracker } = options;
|
|
803
|
-
const updateEndTime = () => {
|
|
804
|
-
const stageData = this.progress.data[stage];
|
|
805
|
-
if (stageData) {
|
|
806
|
-
stageData.endTime = Date.now();
|
|
807
|
-
}
|
|
808
|
-
};
|
|
809
|
-
if (!source || !destination || this.shouldSkipStage(stage)) {
|
|
810
|
-
const results = await Promise.allSettled(
|
|
811
|
-
[source, destination].map((stream2) => {
|
|
812
|
-
if (!stream2 || stream2.destroyed) {
|
|
813
|
-
return Promise.resolve();
|
|
814
|
-
}
|
|
815
|
-
return new Promise((resolve, reject2) => {
|
|
816
|
-
stream2.on("close", resolve).on("error", reject2).destroy();
|
|
817
|
-
});
|
|
818
|
-
})
|
|
819
|
-
);
|
|
820
|
-
results.forEach((state) => {
|
|
821
|
-
if (state.status === "rejected") {
|
|
822
|
-
this.reportWarning(state.reason, `transfer(${stage})`);
|
|
823
|
-
}
|
|
824
|
-
});
|
|
825
|
-
this.#emitStageUpdate("skip", stage);
|
|
826
|
-
return;
|
|
827
|
-
}
|
|
828
|
-
this.#emitStageUpdate("start", stage);
|
|
829
|
-
await new Promise((resolve, reject2) => {
|
|
830
|
-
let stream2 = source;
|
|
831
|
-
if (transform) {
|
|
832
|
-
stream2 = stream2.pipe(transform);
|
|
833
|
-
}
|
|
834
|
-
if (tracker) {
|
|
835
|
-
stream2 = stream2.pipe(tracker);
|
|
836
|
-
}
|
|
837
|
-
this.#currentStream = stream2.pipe(destination).on("error", (e) => {
|
|
838
|
-
updateEndTime();
|
|
839
|
-
this.#emitStageUpdate("error", stage);
|
|
840
|
-
this.reportError(e, "error");
|
|
841
|
-
destination.destroy(e);
|
|
842
|
-
reject2(e);
|
|
843
|
-
}).on("close", () => {
|
|
844
|
-
this.#currentStream = void 0;
|
|
845
|
-
updateEndTime();
|
|
846
|
-
resolve();
|
|
847
|
-
});
|
|
848
|
-
});
|
|
849
|
-
this.#emitStageUpdate("finish", stage);
|
|
850
|
-
}
|
|
851
|
-
// Cause an ongoing transfer to abort gracefully
|
|
852
|
-
async abortTransfer() {
|
|
853
|
-
const err = new TransferEngineError("fatal", "Transfer aborted.");
|
|
854
|
-
if (!this.#currentStream) {
|
|
855
|
-
throw err;
|
|
856
|
-
}
|
|
857
|
-
this.#currentStream.destroy(err);
|
|
858
|
-
}
|
|
859
|
-
async init() {
|
|
860
|
-
await this.#resolveProviderResource();
|
|
861
|
-
const { source: sourceMetadata } = this.#metadata;
|
|
862
|
-
if (sourceMetadata) {
|
|
863
|
-
this.destinationProvider.setMetadata?.("source", sourceMetadata);
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
/**
|
|
867
|
-
* Run the bootstrap method in both source and destination providers
|
|
868
|
-
*/
|
|
869
|
-
async bootstrap() {
|
|
870
|
-
const results = await Promise.allSettled([
|
|
871
|
-
this.sourceProvider.bootstrap?.(),
|
|
872
|
-
this.destinationProvider.bootstrap?.()
|
|
873
|
-
]);
|
|
874
|
-
results.forEach((result) => {
|
|
875
|
-
if (result.status === "rejected") {
|
|
876
|
-
this.panic(result.reason);
|
|
877
|
-
}
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
/**
|
|
881
|
-
* Run the close method in both source and destination providers
|
|
882
|
-
*/
|
|
883
|
-
async close() {
|
|
884
|
-
const results = await Promise.allSettled([
|
|
885
|
-
this.sourceProvider.close?.(),
|
|
886
|
-
this.destinationProvider.close?.()
|
|
887
|
-
]);
|
|
888
|
-
results.forEach((result) => {
|
|
889
|
-
if (result.status === "rejected") {
|
|
890
|
-
this.panic(result.reason);
|
|
891
|
-
}
|
|
892
|
-
});
|
|
893
|
-
}
|
|
894
|
-
async #resolveProviderResource() {
|
|
895
|
-
const sourceMetadata = await this.sourceProvider.getMetadata();
|
|
896
|
-
const destinationMetadata = await this.destinationProvider.getMetadata();
|
|
897
|
-
if (sourceMetadata) {
|
|
898
|
-
this.#metadata.source = sourceMetadata;
|
|
899
|
-
}
|
|
900
|
-
if (destinationMetadata) {
|
|
901
|
-
this.#metadata.destination = destinationMetadata;
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
async #getSchemas() {
|
|
905
|
-
if (!this.#schema.source) {
|
|
906
|
-
this.#schema.source = await this.sourceProvider.getSchemas?.();
|
|
907
|
-
}
|
|
908
|
-
if (!this.#schema.destination) {
|
|
909
|
-
this.#schema.destination = await this.destinationProvider.getSchemas?.();
|
|
910
|
-
}
|
|
911
|
-
return {
|
|
912
|
-
sourceSchemas: this.#schema.source,
|
|
913
|
-
destinationSchemas: this.#schema.destination
|
|
914
|
-
};
|
|
915
|
-
}
|
|
916
|
-
async integrityCheck() {
|
|
917
|
-
const sourceMetadata = await this.sourceProvider.getMetadata();
|
|
918
|
-
const destinationMetadata = await this.destinationProvider.getMetadata();
|
|
919
|
-
if (sourceMetadata && destinationMetadata) {
|
|
920
|
-
this.#assertStrapiVersionIntegrity(
|
|
921
|
-
sourceMetadata?.strapi?.version,
|
|
922
|
-
destinationMetadata?.strapi?.version
|
|
923
|
-
);
|
|
924
|
-
}
|
|
925
|
-
const { sourceSchemas, destinationSchemas } = await this.#getSchemas();
|
|
926
|
-
try {
|
|
927
|
-
if (sourceSchemas && destinationSchemas) {
|
|
928
|
-
this.#assertSchemasMatching(sourceSchemas, destinationSchemas);
|
|
929
|
-
}
|
|
930
|
-
} catch (error) {
|
|
931
|
-
if (error instanceof TransferEngineValidationError && error.details?.details?.diffs) {
|
|
932
|
-
const schemaDiffs = error.details?.details?.diffs;
|
|
933
|
-
const context = {
|
|
934
|
-
ignoredDiffs: {},
|
|
935
|
-
diffs: schemaDiffs,
|
|
936
|
-
source: this.sourceProvider,
|
|
937
|
-
destination: this.destinationProvider
|
|
938
|
-
};
|
|
939
|
-
if (isEmpty(this.#handlers.schemaDiff)) {
|
|
940
|
-
throw error;
|
|
941
|
-
}
|
|
942
|
-
await runMiddleware(
|
|
943
|
-
context,
|
|
944
|
-
this.#handlers.schemaDiff
|
|
945
|
-
);
|
|
946
|
-
const unresolvedDiffs = diff(context.diffs, context.ignoredDiffs);
|
|
947
|
-
if (unresolvedDiffs.length) {
|
|
948
|
-
this.panic(
|
|
949
|
-
new TransferEngineValidationError("Unresolved differences in schema", {
|
|
950
|
-
check: "schema.changes",
|
|
951
|
-
unresolvedDiffs
|
|
952
|
-
})
|
|
953
|
-
);
|
|
954
|
-
}
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
throw error;
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
async transfer() {
|
|
961
|
-
this.progress.data = {};
|
|
962
|
-
try {
|
|
963
|
-
this.#emitTransferUpdate("init");
|
|
964
|
-
await this.bootstrap();
|
|
965
|
-
await this.init();
|
|
966
|
-
await this.integrityCheck();
|
|
967
|
-
this.#emitTransferUpdate("start");
|
|
968
|
-
await this.beforeTransfer();
|
|
969
|
-
await this.transferSchemas();
|
|
970
|
-
await this.transferEntities();
|
|
971
|
-
await this.transferAssets();
|
|
972
|
-
await this.transferLinks();
|
|
973
|
-
await this.transferConfiguration();
|
|
974
|
-
await this.close();
|
|
975
|
-
this.#emitTransferUpdate("finish");
|
|
976
|
-
} catch (e) {
|
|
977
|
-
this.#emitTransferUpdate("error", { error: e });
|
|
978
|
-
const lastDiagnostic = last(this.diagnostics.stack.items);
|
|
979
|
-
if (e instanceof Error && (!lastDiagnostic || lastDiagnostic.kind !== "error" || lastDiagnostic.details.error !== e)) {
|
|
980
|
-
this.reportError(e, e.severity || "fatal");
|
|
981
|
-
}
|
|
982
|
-
await this.destinationProvider.rollback?.(e);
|
|
983
|
-
throw e;
|
|
984
|
-
}
|
|
985
|
-
return {
|
|
986
|
-
source: this.sourceProvider.results,
|
|
987
|
-
destination: this.destinationProvider.results,
|
|
988
|
-
engine: this.progress.data
|
|
989
|
-
};
|
|
990
|
-
}
|
|
991
|
-
async beforeTransfer() {
|
|
992
|
-
const runWithDiagnostic = async (provider) => {
|
|
993
|
-
try {
|
|
994
|
-
await provider.beforeTransfer?.();
|
|
995
|
-
} catch (error) {
|
|
996
|
-
if (error instanceof Error) {
|
|
997
|
-
const resolved = await this.attemptResolveError(error);
|
|
998
|
-
if (resolved) {
|
|
999
|
-
return;
|
|
1000
|
-
}
|
|
1001
|
-
this.panic(error);
|
|
1002
|
-
} else {
|
|
1003
|
-
this.panic(
|
|
1004
|
-
new Error(`Unknwon error when executing "beforeTransfer" on the ${origin} provider`)
|
|
1005
|
-
);
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
};
|
|
1009
|
-
await runWithDiagnostic(this.sourceProvider);
|
|
1010
|
-
await runWithDiagnostic(this.destinationProvider);
|
|
1011
|
-
}
|
|
1012
|
-
async transferSchemas() {
|
|
1013
|
-
const stage = "schemas";
|
|
1014
|
-
if (this.shouldSkipStage(stage)) {
|
|
1015
|
-
return;
|
|
1016
|
-
}
|
|
1017
|
-
const source = await this.sourceProvider.createSchemasReadStream?.();
|
|
1018
|
-
const destination = await this.destinationProvider.createSchemasWriteStream?.();
|
|
1019
|
-
const transform = this.#createStageTransformStream(stage);
|
|
1020
|
-
const tracker = this.#progressTracker(stage, {
|
|
1021
|
-
key: (value) => value.modelType
|
|
1022
|
-
});
|
|
1023
|
-
await this.#transferStage({ stage, source, destination, transform, tracker });
|
|
1024
|
-
}
|
|
1025
|
-
async transferEntities() {
|
|
1026
|
-
const stage = "entities";
|
|
1027
|
-
if (this.shouldSkipStage(stage)) {
|
|
1028
|
-
return;
|
|
1029
|
-
}
|
|
1030
|
-
const source = await this.sourceProvider.createEntitiesReadStream?.();
|
|
1031
|
-
const destination = await this.destinationProvider.createEntitiesWriteStream?.();
|
|
1032
|
-
const transform = chain([
|
|
1033
|
-
this.#createStageTransformStream(stage),
|
|
1034
|
-
new Transform({
|
|
1035
|
-
objectMode: true,
|
|
1036
|
-
transform: async (entity2, _encoding, callback) => {
|
|
1037
|
-
const { destinationSchemas: schemas } = await this.#getSchemas();
|
|
1038
|
-
if (!schemas) {
|
|
1039
|
-
return callback(null, entity2);
|
|
1040
|
-
}
|
|
1041
|
-
const availableContentTypes = Object.entries(schemas).filter(([, schema2]) => schema2.modelType === "contentType").map(([uid]) => uid);
|
|
1042
|
-
if (!availableContentTypes.includes(entity2.type)) {
|
|
1043
|
-
return callback(null, void 0);
|
|
1044
|
-
}
|
|
1045
|
-
const { type, data } = entity2;
|
|
1046
|
-
const attributes = schemas[type].attributes;
|
|
1047
|
-
const attributesToKeep = Object.keys(attributes).concat("documentId");
|
|
1048
|
-
const updatedEntity = set("data", pick(attributesToKeep, data), entity2);
|
|
1049
|
-
callback(null, updatedEntity);
|
|
1050
|
-
}
|
|
1051
|
-
})
|
|
1052
|
-
]);
|
|
1053
|
-
const tracker = this.#progressTracker(stage, { key: (value) => value.type });
|
|
1054
|
-
await this.#transferStage({ stage, source, destination, transform, tracker });
|
|
1055
|
-
}
|
|
1056
|
-
async transferLinks() {
|
|
1057
|
-
const stage = "links";
|
|
1058
|
-
if (this.shouldSkipStage(stage)) {
|
|
1059
|
-
return;
|
|
1060
|
-
}
|
|
1061
|
-
const source = await this.sourceProvider.createLinksReadStream?.();
|
|
1062
|
-
const destination = await this.destinationProvider.createLinksWriteStream?.();
|
|
1063
|
-
const transform = chain([
|
|
1064
|
-
this.#createStageTransformStream(stage),
|
|
1065
|
-
new Transform({
|
|
1066
|
-
objectMode: true,
|
|
1067
|
-
transform: async (link2, _encoding, callback) => {
|
|
1068
|
-
const { destinationSchemas: schemas } = await this.#getSchemas();
|
|
1069
|
-
if (!schemas) {
|
|
1070
|
-
return callback(null, link2);
|
|
1071
|
-
}
|
|
1072
|
-
const availableContentTypes = Object.keys(schemas);
|
|
1073
|
-
const isValidType = (uid) => availableContentTypes.includes(uid);
|
|
1074
|
-
if (!isValidType(link2.left.type) || !isValidType(link2.right.type)) {
|
|
1075
|
-
return callback(null, void 0);
|
|
1076
|
-
}
|
|
1077
|
-
callback(null, link2);
|
|
1078
|
-
}
|
|
1079
|
-
})
|
|
1080
|
-
]);
|
|
1081
|
-
const tracker = this.#progressTracker(stage);
|
|
1082
|
-
await this.#transferStage({ stage, source, destination, transform, tracker });
|
|
1083
|
-
}
|
|
1084
|
-
async transferAssets() {
|
|
1085
|
-
const stage = "assets";
|
|
1086
|
-
if (this.shouldSkipStage(stage)) {
|
|
1087
|
-
return;
|
|
1088
|
-
}
|
|
1089
|
-
const source = await this.sourceProvider.createAssetsReadStream?.();
|
|
1090
|
-
const destination = await this.destinationProvider.createAssetsWriteStream?.();
|
|
1091
|
-
const transform = this.#createStageTransformStream(stage);
|
|
1092
|
-
const tracker = this.#progressTracker(stage, {
|
|
1093
|
-
size: (value) => value.stats.size,
|
|
1094
|
-
key: (value) => extname(value.filename) || "No extension"
|
|
1095
|
-
});
|
|
1096
|
-
await this.#transferStage({ stage, source, destination, transform, tracker });
|
|
1097
|
-
}
|
|
1098
|
-
async transferConfiguration() {
|
|
1099
|
-
const stage = "configuration";
|
|
1100
|
-
if (this.shouldSkipStage(stage)) {
|
|
1101
|
-
return;
|
|
1102
|
-
}
|
|
1103
|
-
const source = await this.sourceProvider.createConfigurationReadStream?.();
|
|
1104
|
-
const destination = await this.destinationProvider.createConfigurationWriteStream?.();
|
|
1105
|
-
const transform = this.#createStageTransformStream(stage);
|
|
1106
|
-
const tracker = this.#progressTracker(stage);
|
|
1107
|
-
await this.#transferStage({ stage, source, destination, transform, tracker });
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
const createTransferEngine = (sourceProvider, destinationProvider, options) => {
|
|
1111
|
-
return new TransferEngine(sourceProvider, destinationProvider, options);
|
|
1112
|
-
};
|
|
1113
|
-
const index$7 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1114
|
-
__proto__: null,
|
|
1115
|
-
DEFAULT_SCHEMA_STRATEGY,
|
|
1116
|
-
DEFAULT_VERSION_STRATEGY,
|
|
1117
|
-
TRANSFER_STAGES,
|
|
1118
|
-
TransferGroupPresets,
|
|
1119
|
-
createTransferEngine,
|
|
1120
|
-
errors
|
|
1121
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
1122
|
-
const isDialectMySQL = () => strapi.db?.dialect.client === "mysql";
|
|
1123
|
-
function omitComponentData(contentType, data) {
|
|
1124
|
-
const { attributes } = contentType;
|
|
1125
|
-
const componentAttributes = Object.keys(attributes).filter(
|
|
1126
|
-
(attributeName) => contentTypes.isComponentAttribute(attributes[attributeName])
|
|
1127
|
-
);
|
|
1128
|
-
return omit(componentAttributes, data);
|
|
1129
|
-
}
|
|
1130
|
-
const createComponents = async (uid, data) => {
|
|
1131
|
-
const { attributes = {} } = strapi.getModel(uid);
|
|
1132
|
-
const componentBody = {};
|
|
1133
|
-
const attributeNames = Object.keys(attributes);
|
|
1134
|
-
for (const attributeName of attributeNames) {
|
|
1135
|
-
const attribute = attributes[attributeName];
|
|
1136
|
-
if (!has(attributeName, data) || !contentTypes.isComponentAttribute(attribute)) {
|
|
1137
|
-
continue;
|
|
1138
|
-
}
|
|
1139
|
-
if (attribute.type === "component") {
|
|
1140
|
-
const { component: componentUID, repeatable = false } = attribute;
|
|
1141
|
-
const componentValue = data[attributeName];
|
|
1142
|
-
if (componentValue === null) {
|
|
1143
|
-
continue;
|
|
1144
|
-
}
|
|
1145
|
-
if (repeatable === true) {
|
|
1146
|
-
if (!Array.isArray(componentValue)) {
|
|
1147
|
-
throw new Error("Expected an array to create repeatable component");
|
|
1148
|
-
}
|
|
1149
|
-
const components = await async.map(
|
|
1150
|
-
componentValue,
|
|
1151
|
-
(value) => createComponent(componentUID, value),
|
|
1152
|
-
{ concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }
|
|
1153
|
-
);
|
|
1154
|
-
componentBody[attributeName] = components.map(({ id }) => {
|
|
1155
|
-
return {
|
|
1156
|
-
id,
|
|
1157
|
-
__pivot: {
|
|
1158
|
-
field: attributeName,
|
|
1159
|
-
component_type: componentUID
|
|
1160
|
-
}
|
|
1161
|
-
};
|
|
1162
|
-
});
|
|
1163
|
-
} else {
|
|
1164
|
-
const component = await createComponent(
|
|
1165
|
-
componentUID,
|
|
1166
|
-
componentValue
|
|
1167
|
-
);
|
|
1168
|
-
componentBody[attributeName] = {
|
|
1169
|
-
id: component.id,
|
|
1170
|
-
__pivot: {
|
|
1171
|
-
field: attributeName,
|
|
1172
|
-
component_type: componentUID
|
|
1173
|
-
}
|
|
1174
|
-
};
|
|
1175
|
-
}
|
|
1176
|
-
continue;
|
|
1177
|
-
}
|
|
1178
|
-
if (attribute.type === "dynamiczone") {
|
|
1179
|
-
const dynamiczoneValues = data[attributeName];
|
|
1180
|
-
if (!Array.isArray(dynamiczoneValues)) {
|
|
1181
|
-
throw new Error("Expected an array to create repeatable component");
|
|
1182
|
-
}
|
|
1183
|
-
const createDynamicZoneComponents = async (value) => {
|
|
1184
|
-
const { id } = await createComponent(value.__component, value);
|
|
1185
|
-
return {
|
|
1186
|
-
id,
|
|
1187
|
-
__component: value.__component,
|
|
1188
|
-
__pivot: {
|
|
1189
|
-
field: attributeName
|
|
1190
|
-
}
|
|
1191
|
-
};
|
|
1192
|
-
};
|
|
1193
|
-
componentBody[attributeName] = await async.map(
|
|
1194
|
-
dynamiczoneValues,
|
|
1195
|
-
createDynamicZoneComponents,
|
|
1196
|
-
{ concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }
|
|
1197
|
-
);
|
|
1198
|
-
continue;
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
return componentBody;
|
|
1202
|
-
};
|
|
1203
|
-
const getComponents = async (uid, entity2) => {
|
|
1204
|
-
const componentAttributes = contentTypes.getComponentAttributes(strapi.getModel(uid));
|
|
1205
|
-
if (_.isEmpty(componentAttributes)) {
|
|
1206
|
-
return {};
|
|
1207
|
-
}
|
|
1208
|
-
return strapi.db.query(uid).load(entity2, componentAttributes);
|
|
1209
|
-
};
|
|
1210
|
-
const deleteComponents = async (uid, entityToDelete, { loadComponents = true } = {}) => {
|
|
1211
|
-
const { attributes = {} } = strapi.getModel(uid);
|
|
1212
|
-
const attributeNames = Object.keys(attributes);
|
|
1213
|
-
for (const attributeName of attributeNames) {
|
|
1214
|
-
const attribute = attributes[attributeName];
|
|
1215
|
-
if (attribute.type === "component" || attribute.type === "dynamiczone") {
|
|
1216
|
-
let value;
|
|
1217
|
-
if (loadComponents) {
|
|
1218
|
-
value = await strapi.db.query(uid).load(entityToDelete, attributeName);
|
|
1219
|
-
} else {
|
|
1220
|
-
value = entityToDelete[attributeName];
|
|
1221
|
-
}
|
|
1222
|
-
if (!value) {
|
|
1223
|
-
continue;
|
|
1224
|
-
}
|
|
1225
|
-
if (attribute.type === "component") {
|
|
1226
|
-
const { component: componentUID } = attribute;
|
|
1227
|
-
await async.map(
|
|
1228
|
-
_.castArray(value),
|
|
1229
|
-
(subValue) => deleteComponent(componentUID, subValue),
|
|
1230
|
-
{
|
|
1231
|
-
concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity
|
|
1232
|
-
}
|
|
1233
|
-
);
|
|
1234
|
-
} else {
|
|
1235
|
-
await async.map(
|
|
1236
|
-
_.castArray(value),
|
|
1237
|
-
(subValue) => deleteComponent(subValue.__component, subValue),
|
|
1238
|
-
{ concurrency: isDialectMySQL() && !strapi.db?.inTransaction() ? 1 : Infinity }
|
|
1239
|
-
);
|
|
1240
|
-
}
|
|
1241
|
-
continue;
|
|
1242
|
-
}
|
|
1243
|
-
}
|
|
1244
|
-
};
|
|
1245
|
-
const createComponent = async (uid, data) => {
|
|
1246
|
-
const model = strapi.getModel(uid);
|
|
1247
|
-
const componentData = await createComponents(uid, data);
|
|
1248
|
-
const transform = pipe(
|
|
1249
|
-
// Make sure we don't save the component with a pre-defined ID
|
|
1250
|
-
omit("id"),
|
|
1251
|
-
// Remove the component data from the original data object ...
|
|
1252
|
-
(payload) => omitComponentData(model, payload),
|
|
1253
|
-
// ... and assign the newly created component instead
|
|
1254
|
-
assign(componentData)
|
|
1255
|
-
);
|
|
1256
|
-
return strapi.db.query(uid).create({ data: transform(data) });
|
|
1257
|
-
};
|
|
1258
|
-
const deleteComponent = async (uid, componentToDelete) => {
|
|
1259
|
-
await deleteComponents(uid, componentToDelete);
|
|
1260
|
-
await strapi.db.query(uid).delete({ where: { id: componentToDelete.id } });
|
|
1261
|
-
};
|
|
1262
|
-
const resolveComponentUID = ({
|
|
1263
|
-
paths,
|
|
1264
|
-
strapi: strapi2,
|
|
1265
|
-
data,
|
|
1266
|
-
contentType
|
|
1267
|
-
}) => {
|
|
1268
|
-
let value = data;
|
|
1269
|
-
let cType = contentType;
|
|
1270
|
-
for (const path2 of paths) {
|
|
1271
|
-
value = get(path2, value);
|
|
1272
|
-
if (typeof cType === "function") {
|
|
1273
|
-
cType = cType(value);
|
|
1274
|
-
}
|
|
1275
|
-
if (path2 in cType.attributes) {
|
|
1276
|
-
const attribute = cType.attributes[path2];
|
|
1277
|
-
if (attribute.type === "component") {
|
|
1278
|
-
cType = strapi2.getModel(attribute.component);
|
|
1279
|
-
}
|
|
1280
|
-
if (attribute.type === "dynamiczone") {
|
|
1281
|
-
cType = ({ __component }) => strapi2.getModel(__component);
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
}
|
|
1285
|
-
if ("uid" in cType) {
|
|
1286
|
-
return cType.uid;
|
|
1287
|
-
}
|
|
1288
|
-
return void 0;
|
|
1289
|
-
};
|
|
1290
|
-
const sanitizeComponentLikeAttributes = (model, data) => {
|
|
1291
|
-
const { attributes } = model;
|
|
1292
|
-
const componentLikeAttributesKey = Object.entries(attributes).filter(([, attribute]) => attribute.type === "component" || attribute.type === "dynamiczone").map(([key]) => key);
|
|
1293
|
-
return omit(componentLikeAttributesKey, data);
|
|
1294
|
-
};
|
|
1295
|
-
const omitInvalidCreationAttributes$1 = omit(["id"]);
|
|
1296
|
-
const createEntityQuery = (strapi2) => {
|
|
1297
|
-
const components = {
|
|
1298
|
-
async assignToEntity(uid, data) {
|
|
1299
|
-
const model = strapi2.getModel(uid);
|
|
1300
|
-
const entityComponents = await createComponents(uid, data);
|
|
1301
|
-
const dataWithoutComponents = sanitizeComponentLikeAttributes(model, data);
|
|
1302
|
-
return assign(entityComponents, dataWithoutComponents);
|
|
1303
|
-
},
|
|
1304
|
-
async get(uid, entity2) {
|
|
1305
|
-
return getComponents(uid, entity2);
|
|
1306
|
-
},
|
|
1307
|
-
delete(uid, componentsToDelete) {
|
|
1308
|
-
return deleteComponents(uid, componentsToDelete, {
|
|
1309
|
-
loadComponents: false
|
|
1310
|
-
});
|
|
1311
|
-
}
|
|
1312
|
-
};
|
|
1313
|
-
const query = (uid) => {
|
|
1314
|
-
const create = async (params) => {
|
|
1315
|
-
const dataWithComponents = await components.assignToEntity(uid, params.data);
|
|
1316
|
-
const sanitizedData = omitInvalidCreationAttributes$1(dataWithComponents);
|
|
1317
|
-
return strapi2.db.query(uid).create({ ...params, data: sanitizedData });
|
|
1318
|
-
};
|
|
1319
|
-
const createMany = async (params) => {
|
|
1320
|
-
return Promise.resolve(params.data).then(map$1((data) => components.assignToEntity(uid, data))).then(map$1(omitInvalidCreationAttributes$1)).then((data) => strapi2.db.query(uid).createMany({ ...params, data }));
|
|
1321
|
-
};
|
|
1322
|
-
const deleteMany = async (params) => {
|
|
1323
|
-
const entitiesToDelete = await strapi2.db.query(uid).findMany(params ?? {});
|
|
1324
|
-
if (!entitiesToDelete.length) {
|
|
1325
|
-
return null;
|
|
1326
|
-
}
|
|
1327
|
-
const componentsToDelete = await Promise.all(
|
|
1328
|
-
entitiesToDelete.map((entityToDelete) => components.get(uid, entityToDelete))
|
|
1329
|
-
);
|
|
1330
|
-
const deletedEntities = await strapi2.db.query(uid).deleteMany(params);
|
|
1331
|
-
await Promise.all(componentsToDelete.map((compos) => components.delete(uid, compos)));
|
|
1332
|
-
return deletedEntities;
|
|
1333
|
-
};
|
|
1334
|
-
const getDeepPopulateComponentLikeQuery = (contentType, params = { select: "*" }) => {
|
|
1335
|
-
const { attributes } = contentType;
|
|
1336
|
-
const populate = {};
|
|
1337
|
-
const entries = Object.entries(attributes);
|
|
1338
|
-
for (const [key, attribute] of entries) {
|
|
1339
|
-
if (attribute.type === "component") {
|
|
1340
|
-
const component = strapi2.getModel(attribute.component);
|
|
1341
|
-
const subPopulate = getDeepPopulateComponentLikeQuery(component, params);
|
|
1342
|
-
if ((isArray(subPopulate) || isObject(subPopulate)) && size(subPopulate) > 0) {
|
|
1343
|
-
populate[key] = { ...params, populate: subPopulate };
|
|
1344
|
-
}
|
|
1345
|
-
if (isArray(subPopulate) && isEmpty(subPopulate)) {
|
|
1346
|
-
populate[key] = { ...params };
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
if (attribute.type === "dynamiczone") {
|
|
1350
|
-
const { components: componentsUID } = attribute;
|
|
1351
|
-
const on = {};
|
|
1352
|
-
for (const componentUID of componentsUID) {
|
|
1353
|
-
const component = strapi2.getModel(componentUID);
|
|
1354
|
-
const subPopulate = getDeepPopulateComponentLikeQuery(component, params);
|
|
1355
|
-
if ((isArray(subPopulate) || isObject(subPopulate)) && size(subPopulate) > 0) {
|
|
1356
|
-
on[componentUID] = { ...params, populate: subPopulate };
|
|
1357
|
-
}
|
|
1358
|
-
if (isArray(subPopulate) && isEmpty(subPopulate)) {
|
|
1359
|
-
on[componentUID] = { ...params };
|
|
1360
|
-
}
|
|
1361
|
-
}
|
|
1362
|
-
populate[key] = size(on) > 0 ? { on } : true;
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
const values = Object.values(populate);
|
|
1366
|
-
if (values.every((value) => value === true)) {
|
|
1367
|
-
return Object.keys(populate);
|
|
1368
|
-
}
|
|
1369
|
-
return populate;
|
|
1370
|
-
};
|
|
1371
|
-
return {
|
|
1372
|
-
create,
|
|
1373
|
-
createMany,
|
|
1374
|
-
deleteMany,
|
|
1375
|
-
getDeepPopulateComponentLikeQuery,
|
|
1376
|
-
get deepPopulateComponentLikeQuery() {
|
|
1377
|
-
const contentType = strapi2.getModel(uid);
|
|
1378
|
-
return getDeepPopulateComponentLikeQuery(contentType);
|
|
1379
|
-
}
|
|
1380
|
-
};
|
|
1381
|
-
};
|
|
1382
|
-
return query;
|
|
1383
|
-
};
|
|
1384
|
-
const entity = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1385
|
-
__proto__: null,
|
|
1386
|
-
createEntityQuery
|
|
1387
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
1388
|
-
const createLinkQuery = (strapi2, trx) => {
|
|
1389
|
-
const query = () => {
|
|
1390
|
-
const { connection } = strapi2.db;
|
|
1391
|
-
const addSchema = (tableName) => {
|
|
1392
|
-
const schemaName = connection.client.connectionSettings.schema;
|
|
1393
|
-
return schemaName ? `${schemaName}.${tableName}` : tableName;
|
|
1394
|
-
};
|
|
1395
|
-
async function* generateAllForAttribute(uid, fieldName) {
|
|
1396
|
-
const metadata = strapi2.db.metadata.get(uid);
|
|
1397
|
-
if (!metadata) {
|
|
1398
|
-
throw new Error(`No metadata found for ${uid}`);
|
|
1399
|
-
}
|
|
1400
|
-
const attributes = filterValidRelationalAttributes(metadata.attributes);
|
|
1401
|
-
if (!(fieldName in attributes)) {
|
|
1402
|
-
throw new Error(`${fieldName} is not a valid relational attribute name`);
|
|
1403
|
-
}
|
|
1404
|
-
const attribute = attributes[fieldName];
|
|
1405
|
-
const kind = getLinkKind(attribute, uid);
|
|
1406
|
-
const { relation, target } = attribute;
|
|
1407
|
-
if (attribute.joinColumn) {
|
|
1408
|
-
const joinColumnName = attribute.joinColumn.name;
|
|
1409
|
-
const qb = connection.queryBuilder().select("id", joinColumnName).from(addSchema(metadata.tableName));
|
|
1410
|
-
if (trx) {
|
|
1411
|
-
qb.transacting(trx);
|
|
1412
|
-
}
|
|
1413
|
-
const entries = await qb;
|
|
1414
|
-
for (const entry of entries) {
|
|
1415
|
-
const ref = entry[joinColumnName];
|
|
1416
|
-
if (ref !== null) {
|
|
1417
|
-
yield {
|
|
1418
|
-
kind,
|
|
1419
|
-
relation,
|
|
1420
|
-
left: { type: uid, ref: entry.id, field: fieldName },
|
|
1421
|
-
right: { type: target, ref }
|
|
1422
|
-
};
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
}
|
|
1426
|
-
if (attribute.joinTable) {
|
|
1427
|
-
const {
|
|
1428
|
-
name,
|
|
1429
|
-
joinColumn,
|
|
1430
|
-
inverseJoinColumn,
|
|
1431
|
-
orderColumnName,
|
|
1432
|
-
morphColumn,
|
|
1433
|
-
inverseOrderColumnName
|
|
1434
|
-
} = attribute.joinTable;
|
|
1435
|
-
const qb = connection.queryBuilder().from(addSchema(name));
|
|
1436
|
-
const columns = {
|
|
1437
|
-
left: { ref: null },
|
|
1438
|
-
right: { ref: null }
|
|
1439
|
-
};
|
|
1440
|
-
const left = { type: uid, field: fieldName };
|
|
1441
|
-
const right = {};
|
|
1442
|
-
if (kind === "relation.basic" || kind === "relation.circular") {
|
|
1443
|
-
right.type = attribute.target;
|
|
1444
|
-
right.field = attribute.inversedBy;
|
|
1445
|
-
columns.left.ref = joinColumn.name;
|
|
1446
|
-
columns.right.ref = inverseJoinColumn.name;
|
|
1447
|
-
if (orderColumnName) {
|
|
1448
|
-
columns.left.order = orderColumnName;
|
|
1449
|
-
}
|
|
1450
|
-
if (inverseOrderColumnName) {
|
|
1451
|
-
columns.right.order = inverseOrderColumnName;
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
if (kind === "relation.morph") {
|
|
1455
|
-
columns.left.ref = joinColumn.name;
|
|
1456
|
-
columns.right.ref = morphColumn.idColumn.name;
|
|
1457
|
-
columns.right.type = morphColumn.typeColumn.name;
|
|
1458
|
-
columns.right.field = "field";
|
|
1459
|
-
columns.right.order = "order";
|
|
1460
|
-
}
|
|
1461
|
-
const validColumns = [
|
|
1462
|
-
// Left
|
|
1463
|
-
columns.left.ref,
|
|
1464
|
-
columns.left.order,
|
|
1465
|
-
// Right
|
|
1466
|
-
columns.right.ref,
|
|
1467
|
-
columns.right.type,
|
|
1468
|
-
columns.right.field,
|
|
1469
|
-
columns.right.order
|
|
1470
|
-
].filter((column) => !isNil(column));
|
|
1471
|
-
qb.select(validColumns);
|
|
1472
|
-
if (trx) {
|
|
1473
|
-
qb.transacting(trx);
|
|
1474
|
-
}
|
|
1475
|
-
const entries = await qb;
|
|
1476
|
-
for (const entry of entries) {
|
|
1477
|
-
if (columns.left.ref) {
|
|
1478
|
-
left.ref = entry[columns.left.ref];
|
|
1479
|
-
}
|
|
1480
|
-
if (columns.right.ref) {
|
|
1481
|
-
right.ref = entry[columns.right.ref];
|
|
1482
|
-
}
|
|
1483
|
-
if (columns.left.order) {
|
|
1484
|
-
left.pos = entry[columns.left.order];
|
|
1485
|
-
}
|
|
1486
|
-
if (columns.right.order) {
|
|
1487
|
-
right.pos = entry[columns.right.order];
|
|
1488
|
-
}
|
|
1489
|
-
if (columns.right.type) {
|
|
1490
|
-
right.type = entry[columns.right.type];
|
|
1491
|
-
}
|
|
1492
|
-
if (columns.right.field) {
|
|
1493
|
-
right.field = entry[columns.right.field];
|
|
1494
|
-
}
|
|
1495
|
-
const link2 = {
|
|
1496
|
-
kind,
|
|
1497
|
-
relation,
|
|
1498
|
-
left: clone(left),
|
|
1499
|
-
right: clone(right)
|
|
1500
|
-
};
|
|
1501
|
-
yield link2;
|
|
1502
|
-
}
|
|
1503
|
-
}
|
|
1504
|
-
if (attribute.morphColumn) {
|
|
1505
|
-
const { typeColumn, idColumn } = attribute.morphColumn;
|
|
1506
|
-
const qb = connection.queryBuilder().select("id", typeColumn.name, idColumn.name).from(addSchema(metadata.tableName)).whereNotNull(typeColumn.name).whereNotNull(idColumn.name);
|
|
1507
|
-
if (trx) {
|
|
1508
|
-
qb.transacting(trx);
|
|
1509
|
-
}
|
|
1510
|
-
const entries = await qb;
|
|
1511
|
-
for (const entry of entries) {
|
|
1512
|
-
const ref = entry[idColumn.name];
|
|
1513
|
-
yield {
|
|
1514
|
-
kind,
|
|
1515
|
-
relation,
|
|
1516
|
-
left: { type: uid, ref: entry.id, field: fieldName },
|
|
1517
|
-
right: { type: entry[typeColumn.name], ref }
|
|
1518
|
-
};
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
async function* generateAll(uid) {
|
|
1523
|
-
const metadata = strapi2.db.metadata.get(uid);
|
|
1524
|
-
if (!metadata) {
|
|
1525
|
-
throw new Error(`No metadata found for ${uid}`);
|
|
1526
|
-
}
|
|
1527
|
-
const attributes = filterValidRelationalAttributes(metadata.attributes);
|
|
1528
|
-
for (const fieldName of Object.keys(attributes)) {
|
|
1529
|
-
for await (const link2 of generateAllForAttribute(uid, fieldName)) {
|
|
1530
|
-
yield link2;
|
|
1531
|
-
}
|
|
1532
|
-
}
|
|
1533
|
-
}
|
|
1534
|
-
const insert = async (link2) => {
|
|
1535
|
-
const { kind, left, right } = link2;
|
|
1536
|
-
const metadata = strapi2.db.metadata.get(left.type);
|
|
1537
|
-
const attribute = metadata.attributes[left.field];
|
|
1538
|
-
const payload = {};
|
|
1539
|
-
if (!attribute) {
|
|
1540
|
-
return;
|
|
1541
|
-
}
|
|
1542
|
-
if (attribute.type !== "relation") {
|
|
1543
|
-
throw new Error(`Attribute ${left.field} is not a relation`);
|
|
1544
|
-
}
|
|
1545
|
-
if ("joinColumn" in attribute && attribute.joinColumn) {
|
|
1546
|
-
const joinColumnName = attribute.joinColumn.name;
|
|
1547
|
-
const qb = connection(addSchema(metadata.tableName)).where("id", left.ref).update({ [joinColumnName]: right.ref });
|
|
1548
|
-
if (trx) {
|
|
1549
|
-
qb.transacting(trx);
|
|
1550
|
-
}
|
|
1551
|
-
await qb;
|
|
1552
|
-
}
|
|
1553
|
-
if ("joinTable" in attribute && attribute.joinTable) {
|
|
1554
|
-
const { joinTable } = attribute;
|
|
1555
|
-
if (joinTable.joinColumn) {
|
|
1556
|
-
Object.assign(payload, { [joinTable.joinColumn.name]: left.ref });
|
|
1557
|
-
}
|
|
1558
|
-
const assignInverseColumn = () => {
|
|
1559
|
-
if ("inverseJoinColumn" in joinTable && joinTable.inverseJoinColumn) {
|
|
1560
|
-
Object.assign(payload, {
|
|
1561
|
-
[joinTable.inverseJoinColumn.name]: right.ref
|
|
1562
|
-
});
|
|
1563
|
-
}
|
|
1564
|
-
};
|
|
1565
|
-
const assignOrderColumns = () => {
|
|
1566
|
-
if ("orderColumnName" in joinTable && joinTable.orderColumnName) {
|
|
1567
|
-
Object.assign(payload, { [joinTable.orderColumnName]: left.pos ?? null });
|
|
1568
|
-
}
|
|
1569
|
-
if ("inverseOrderColumnName" in joinTable && joinTable.inverseOrderColumnName) {
|
|
1570
|
-
Object.assign(payload, { [joinTable.inverseOrderColumnName]: right.pos ?? null });
|
|
1571
|
-
}
|
|
1572
|
-
};
|
|
1573
|
-
const assignMorphColumns = () => {
|
|
1574
|
-
if ("morphColumn" in joinTable && joinTable.morphColumn) {
|
|
1575
|
-
const { idColumn, typeColumn } = joinTable.morphColumn ?? {};
|
|
1576
|
-
if (idColumn) {
|
|
1577
|
-
Object.assign(payload, { [idColumn.name]: right.ref });
|
|
1578
|
-
}
|
|
1579
|
-
if (typeColumn) {
|
|
1580
|
-
Object.assign(payload, { [typeColumn.name]: right.type });
|
|
1581
|
-
}
|
|
1582
|
-
Object.assign(payload, { order: right.pos ?? null, field: right.field ?? null });
|
|
1583
|
-
}
|
|
1584
|
-
};
|
|
1585
|
-
if (kind === "relation.basic" || kind === "relation.circular") {
|
|
1586
|
-
assignInverseColumn();
|
|
1587
|
-
}
|
|
1588
|
-
if (kind === "relation.morph") {
|
|
1589
|
-
assignMorphColumns();
|
|
1590
|
-
}
|
|
1591
|
-
assignOrderColumns();
|
|
1592
|
-
const qb = connection.insert(payload).into(addSchema(joinTable.name));
|
|
1593
|
-
if (trx) {
|
|
1594
|
-
await trx.transaction(async (nestedTrx) => {
|
|
1595
|
-
await qb.transacting(nestedTrx);
|
|
1596
|
-
});
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
if ("morphColumn" in attribute && attribute.morphColumn) {
|
|
1600
|
-
const { morphColumn } = attribute;
|
|
1601
|
-
const qb = connection(addSchema(metadata.tableName)).where("id", left.ref).update({
|
|
1602
|
-
[morphColumn.idColumn.name]: right.ref,
|
|
1603
|
-
[morphColumn.typeColumn.name]: right.type
|
|
1604
|
-
});
|
|
1605
|
-
if (trx) {
|
|
1606
|
-
qb.transacting(trx);
|
|
1607
|
-
}
|
|
1608
|
-
await qb;
|
|
1609
|
-
}
|
|
1610
|
-
};
|
|
1611
|
-
return { generateAll, generateAllForAttribute, insert };
|
|
1612
|
-
};
|
|
1613
|
-
return query;
|
|
1614
|
-
};
|
|
1615
|
-
const filterValidRelationalAttributes = (attributes) => {
|
|
1616
|
-
const isOwner = (attribute) => {
|
|
1617
|
-
return attribute.owner || !attribute.mappedBy && !attribute.morphBy;
|
|
1618
|
-
};
|
|
1619
|
-
const isComponentLike = (attribute) => attribute.joinTable?.name.endsWith("_cmps");
|
|
1620
|
-
return Object.entries(attributes).filter(([, attribute]) => {
|
|
1621
|
-
return attribute.type === "relation" && isOwner(attribute) && !isComponentLike(attribute);
|
|
1622
|
-
}).reduce((acc, [key, attribute]) => ({ ...acc, [key]: attribute }), {});
|
|
1623
|
-
};
|
|
1624
|
-
const getLinkKind = (attribute, uid) => {
|
|
1625
|
-
if (attribute.relation.startsWith("morph")) {
|
|
1626
|
-
return "relation.morph";
|
|
1627
|
-
}
|
|
1628
|
-
if (attribute.target === uid) {
|
|
1629
|
-
return "relation.circular";
|
|
1630
|
-
}
|
|
1631
|
-
return "relation.basic";
|
|
1632
|
-
};
|
|
1633
|
-
const link = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1634
|
-
__proto__: null,
|
|
1635
|
-
createLinkQuery,
|
|
1636
|
-
filterValidRelationalAttributes
|
|
1637
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
1638
|
-
const index$6 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1639
|
-
__proto__: null,
|
|
1640
|
-
entity,
|
|
1641
|
-
link
|
|
1642
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
1643
|
-
const createEntitiesWriteStream = (options) => {
|
|
1644
|
-
const { strapi: strapi2, updateMappingTable, transaction: transaction2 } = options;
|
|
1645
|
-
const query = createEntityQuery(strapi2);
|
|
1646
|
-
return new Writable({
|
|
1647
|
-
objectMode: true,
|
|
1648
|
-
async write(entity2, _encoding, callback) {
|
|
1649
|
-
await transaction2?.attach(async () => {
|
|
1650
|
-
const { type, id, data } = entity2;
|
|
1651
|
-
const { create, getDeepPopulateComponentLikeQuery } = query(type);
|
|
1652
|
-
const contentType = strapi2.getModel(type);
|
|
1653
|
-
try {
|
|
1654
|
-
const created = await create({
|
|
1655
|
-
data,
|
|
1656
|
-
populate: getDeepPopulateComponentLikeQuery(contentType, { select: "id" }),
|
|
1657
|
-
select: "id"
|
|
1658
|
-
});
|
|
1659
|
-
const diffs = diff(data, created);
|
|
1660
|
-
updateMappingTable(type, id, created.id);
|
|
1661
|
-
diffs.forEach((diff2) => {
|
|
1662
|
-
if (diff2.kind === "modified" && last(diff2.path) === "id" && "kind" in contentType) {
|
|
1663
|
-
const target = resolveComponentUID({ paths: diff2.path, data, contentType, strapi: strapi2 });
|
|
1664
|
-
if (!target) {
|
|
1665
|
-
return;
|
|
1666
|
-
}
|
|
1667
|
-
const [oldID, newID] = diff2.values;
|
|
1668
|
-
updateMappingTable(target, oldID, newID);
|
|
1669
|
-
}
|
|
1670
|
-
});
|
|
1671
|
-
} catch (e) {
|
|
1672
|
-
if (e instanceof Error) {
|
|
1673
|
-
return callback(e);
|
|
1674
|
-
}
|
|
1675
|
-
return callback(new ProviderTransferError(`Failed to create "${type}" (${id})`));
|
|
1676
|
-
}
|
|
1677
|
-
return callback(null);
|
|
1678
|
-
});
|
|
1679
|
-
}
|
|
1680
|
-
});
|
|
1681
|
-
};
|
|
1682
|
-
const omitInvalidCreationAttributes = omit(["id"]);
|
|
1683
|
-
const restoreCoreStore = async (strapi2, values) => {
|
|
1684
|
-
const data = omitInvalidCreationAttributes(values);
|
|
1685
|
-
return strapi2.db.query("strapi::core-store").create({
|
|
1686
|
-
data: {
|
|
1687
|
-
...data,
|
|
1688
|
-
value: JSON.stringify(data.value)
|
|
1689
|
-
}
|
|
1690
|
-
});
|
|
1691
|
-
};
|
|
1692
|
-
const restoreWebhooks = async (strapi2, values) => {
|
|
1693
|
-
const data = omitInvalidCreationAttributes(values);
|
|
1694
|
-
return strapi2.db.query("strapi::webhook").create({ data });
|
|
1695
|
-
};
|
|
1696
|
-
const restoreConfigs = async (strapi2, config) => {
|
|
1697
|
-
if (config.type === "core-store") {
|
|
1698
|
-
return restoreCoreStore(strapi2, config.value);
|
|
1699
|
-
}
|
|
1700
|
-
if (config.type === "webhook") {
|
|
1701
|
-
return restoreWebhooks(strapi2, config.value);
|
|
1702
|
-
}
|
|
1703
|
-
};
|
|
1704
|
-
const createConfigurationWriteStream = async (strapi2, transaction2) => {
|
|
1705
|
-
return new Writable({
|
|
1706
|
-
objectMode: true,
|
|
1707
|
-
async write(config, _encoding, callback) {
|
|
1708
|
-
await transaction2?.attach(async () => {
|
|
1709
|
-
try {
|
|
1710
|
-
await restoreConfigs(strapi2, config);
|
|
1711
|
-
} catch (error) {
|
|
1712
|
-
return callback(
|
|
1713
|
-
new ProviderTransferError(
|
|
1714
|
-
`Failed to import ${chalk.yellowBright(config.type)} (${chalk.greenBright(
|
|
1715
|
-
config.value.id
|
|
1716
|
-
)}`
|
|
1717
|
-
)
|
|
1718
|
-
);
|
|
1719
|
-
}
|
|
1720
|
-
callback();
|
|
1721
|
-
});
|
|
1722
|
-
}
|
|
1723
|
-
});
|
|
1724
|
-
};
|
|
1725
|
-
const isErrorWithCode = (error) => {
|
|
1726
|
-
return error && typeof error.code === "string";
|
|
1727
|
-
};
|
|
1728
|
-
const isForeignKeyConstraintError = (e) => {
|
|
1729
|
-
const MYSQL_FK_ERROR_CODES = ["1452", "1557", "1216", "1217", "1451"];
|
|
1730
|
-
const POSTGRES_FK_ERROR_CODE = "23503";
|
|
1731
|
-
const SQLITE_FK_ERROR_CODE = "SQLITE_CONSTRAINT_FOREIGNKEY";
|
|
1732
|
-
if (isErrorWithCode(e) && e.code) {
|
|
1733
|
-
return [SQLITE_FK_ERROR_CODE, POSTGRES_FK_ERROR_CODE, ...MYSQL_FK_ERROR_CODES].includes(e.code);
|
|
1734
|
-
}
|
|
1735
|
-
return e.message.toLowerCase().includes("foreign key constraint");
|
|
1736
|
-
};
|
|
1737
|
-
const createLinksWriteStream = (mapID, strapi2, transaction2, onWarning) => {
|
|
1738
|
-
return new Writable({
|
|
1739
|
-
objectMode: true,
|
|
1740
|
-
async write(link2, _encoding, callback) {
|
|
1741
|
-
await transaction2?.attach(async (trx) => {
|
|
1742
|
-
const { left, right } = link2;
|
|
1743
|
-
const query = createLinkQuery(strapi2, trx);
|
|
1744
|
-
const originalLeftRef = left.ref;
|
|
1745
|
-
const originalRightRef = right.ref;
|
|
1746
|
-
left.ref = mapID(left.type, originalLeftRef) ?? originalLeftRef;
|
|
1747
|
-
right.ref = mapID(right.type, originalRightRef) ?? originalRightRef;
|
|
1748
|
-
try {
|
|
1749
|
-
await query().insert(link2);
|
|
1750
|
-
} catch (e) {
|
|
1751
|
-
if (e instanceof Error) {
|
|
1752
|
-
if (isForeignKeyConstraintError(e)) {
|
|
1753
|
-
onWarning?.(
|
|
1754
|
-
`Skipping link ${left.type}:${originalLeftRef} -> ${right.type}:${originalRightRef} due to a foreign key constraint.`
|
|
1755
|
-
);
|
|
1756
|
-
return callback(null);
|
|
1757
|
-
}
|
|
1758
|
-
return callback(e);
|
|
1759
|
-
}
|
|
1760
|
-
return callback(
|
|
1761
|
-
new ProviderTransferError(
|
|
1762
|
-
`An error happened while trying to import a ${left.type} link.`
|
|
1763
|
-
)
|
|
1764
|
-
);
|
|
1765
|
-
}
|
|
1766
|
-
callback(null);
|
|
1767
|
-
});
|
|
1768
|
-
}
|
|
1769
|
-
});
|
|
1770
|
-
};
|
|
1771
|
-
const deleteRecords = async (strapi2, options) => {
|
|
1772
|
-
const entities = await deleteEntitiesRecords(strapi2, options);
|
|
1773
|
-
const configuration = await deleteConfigurationRecords(strapi2, options);
|
|
1774
|
-
return {
|
|
1775
|
-
count: entities.count + configuration.count,
|
|
1776
|
-
entities,
|
|
1777
|
-
configuration
|
|
1778
|
-
};
|
|
1779
|
-
};
|
|
1780
|
-
const deleteEntitiesRecords = async (strapi2, options = {}) => {
|
|
1781
|
-
const { entities } = options;
|
|
1782
|
-
const models = strapi2.get("models").get();
|
|
1783
|
-
const contentTypes2 = Object.values(strapi2.contentTypes);
|
|
1784
|
-
const contentTypesToClear = contentTypes2.filter((contentType) => {
|
|
1785
|
-
let removeThisContentType = true;
|
|
1786
|
-
if (entities?.include) {
|
|
1787
|
-
removeThisContentType = entities.include.includes(contentType.uid);
|
|
1788
|
-
}
|
|
1789
|
-
if (entities?.exclude && entities.exclude.includes(contentType.uid)) {
|
|
1790
|
-
removeThisContentType = false;
|
|
1791
|
-
}
|
|
1792
|
-
if (entities?.filters) {
|
|
1793
|
-
removeThisContentType = entities.filters.every((filter2) => filter2(contentType));
|
|
1794
|
-
}
|
|
1795
|
-
return removeThisContentType;
|
|
1796
|
-
}).map((contentType) => contentType.uid);
|
|
1797
|
-
const modelsToClear = models.filter((model) => {
|
|
1798
|
-
if (contentTypesToClear.includes(model.uid)) {
|
|
1799
|
-
return false;
|
|
1800
|
-
}
|
|
1801
|
-
let removeThisModel = true;
|
|
1802
|
-
if (entities?.include) {
|
|
1803
|
-
removeThisModel = entities.include.includes(model.uid);
|
|
1804
|
-
}
|
|
1805
|
-
if (entities?.exclude && entities.exclude.includes(model.uid)) {
|
|
1806
|
-
removeThisModel = false;
|
|
1807
|
-
}
|
|
1808
|
-
return removeThisModel;
|
|
1809
|
-
}).map((model) => model.uid);
|
|
1810
|
-
const [results, updateResults] = useResults([...contentTypesToClear, ...modelsToClear]);
|
|
1811
|
-
const contentTypeQuery = createEntityQuery(strapi2);
|
|
1812
|
-
const contentTypePromises = contentTypesToClear.map(async (uid) => {
|
|
1813
|
-
const result = await contentTypeQuery(uid).deleteMany(entities?.params);
|
|
1814
|
-
if (result) {
|
|
1815
|
-
updateResults(result.count || 0, uid);
|
|
1816
|
-
}
|
|
1817
|
-
});
|
|
1818
|
-
const modelsPromises = modelsToClear.map(async (uid) => {
|
|
1819
|
-
const result = await strapi2.db.query(uid).deleteMany({});
|
|
1820
|
-
if (result) {
|
|
1821
|
-
updateResults(result.count || 0, uid);
|
|
1822
|
-
}
|
|
1823
|
-
});
|
|
1824
|
-
await Promise.all([...contentTypePromises, ...modelsPromises]);
|
|
1825
|
-
return results;
|
|
1826
|
-
};
|
|
1827
|
-
const deleteConfigurationRecords = async (strapi2, options = {}) => {
|
|
1828
|
-
const { coreStore = true, webhook = true } = options?.configuration ?? {};
|
|
1829
|
-
const models = [];
|
|
1830
|
-
if (coreStore) {
|
|
1831
|
-
models.push("strapi::core-store");
|
|
1832
|
-
}
|
|
1833
|
-
if (webhook) {
|
|
1834
|
-
models.push("strapi::webhook");
|
|
1835
|
-
}
|
|
1836
|
-
const [results, updateResults] = useResults(models);
|
|
1837
|
-
const deletePromises = models.map(async (uid) => {
|
|
1838
|
-
const result = await strapi2.db.query(uid).deleteMany({});
|
|
1839
|
-
if (result) {
|
|
1840
|
-
updateResults(result.count, uid);
|
|
1841
|
-
}
|
|
1842
|
-
});
|
|
1843
|
-
await Promise.all(deletePromises);
|
|
1844
|
-
return results;
|
|
1845
|
-
};
|
|
1846
|
-
const useResults = (keys) => {
|
|
1847
|
-
const results = {
|
|
1848
|
-
count: 0,
|
|
1849
|
-
aggregate: keys.reduce((acc, key) => ({ ...acc, [key]: { count: 0 } }), {})
|
|
1850
|
-
};
|
|
1851
|
-
const update = (count, key) => {
|
|
1852
|
-
if (key) {
|
|
1853
|
-
if (!(key in results.aggregate)) {
|
|
1854
|
-
throw new ProviderTransferError(`Unknown key "${key}" provided in results update`);
|
|
1855
|
-
}
|
|
1856
|
-
results.aggregate[key].count += count;
|
|
1857
|
-
}
|
|
1858
|
-
results.count += count;
|
|
1859
|
-
};
|
|
1860
|
-
return [results, update];
|
|
1861
|
-
};
|
|
1862
|
-
const assertValidStrapi = (strapi2, msg = "") => {
|
|
1863
|
-
if (!strapi2) {
|
|
1864
|
-
throw new ProviderInitializationError(`${msg}. Strapi instance not found.`);
|
|
1865
|
-
}
|
|
1866
|
-
};
|
|
1867
|
-
const VALID_CONFLICT_STRATEGIES = ["restore"];
|
|
1868
|
-
const DEFAULT_CONFLICT_STRATEGY = "restore";
|
|
1869
|
-
class LocalStrapiDestinationProvider {
|
|
1870
|
-
name = "destination::local-strapi";
|
|
1871
|
-
type = "destination";
|
|
1872
|
-
options;
|
|
1873
|
-
strapi;
|
|
1874
|
-
transaction;
|
|
1875
|
-
uploadsBackupDirectoryName;
|
|
1876
|
-
onWarning;
|
|
1877
|
-
/**
|
|
1878
|
-
* The entities mapper is used to map old entities to their new IDs
|
|
1879
|
-
*/
|
|
1880
|
-
#entitiesMapper;
|
|
1881
|
-
constructor(options) {
|
|
1882
|
-
this.options = options;
|
|
1883
|
-
this.#entitiesMapper = {};
|
|
1884
|
-
this.uploadsBackupDirectoryName = `uploads_backup_${Date.now()}`;
|
|
1885
|
-
}
|
|
1886
|
-
async bootstrap() {
|
|
1887
|
-
this.#validateOptions();
|
|
1888
|
-
this.strapi = await this.options.getStrapi();
|
|
1889
|
-
if (!this.strapi) {
|
|
1890
|
-
throw new ProviderInitializationError("Could not access local strapi");
|
|
1891
|
-
}
|
|
1892
|
-
this.transaction = createTransaction(this.strapi);
|
|
1893
|
-
}
|
|
1894
|
-
// TODO: either move this to restore strategy, or restore strategy should given access to these instead of repeating the logic possibly in a different way
|
|
1895
|
-
#areAssetsIncluded = () => {
|
|
1896
|
-
return this.options.restore?.assets;
|
|
1897
|
-
};
|
|
1898
|
-
#isContentTypeIncluded = (type) => {
|
|
1899
|
-
const notIncluded = this.options.restore?.entities?.include && !this.options.restore?.entities?.include?.includes(type);
|
|
1900
|
-
const excluded = this.options.restore?.entities?.exclude && this.options.restore?.entities.exclude.includes(type);
|
|
1901
|
-
return !excluded && !notIncluded;
|
|
1902
|
-
};
|
|
1903
|
-
async close() {
|
|
1904
|
-
const { autoDestroy } = this.options;
|
|
1905
|
-
this.transaction?.end();
|
|
1906
|
-
if (autoDestroy === void 0 || autoDestroy === true) {
|
|
1907
|
-
await this.strapi?.destroy();
|
|
1908
|
-
}
|
|
1909
|
-
}
|
|
1910
|
-
#validateOptions() {
|
|
1911
|
-
if (!VALID_CONFLICT_STRATEGIES.includes(this.options.strategy)) {
|
|
1912
|
-
throw new ProviderValidationError(`Invalid strategy ${this.options.strategy}`, {
|
|
1913
|
-
check: "strategy",
|
|
1914
|
-
strategy: this.options.strategy,
|
|
1915
|
-
validStrategies: VALID_CONFLICT_STRATEGIES
|
|
1916
|
-
});
|
|
1917
|
-
}
|
|
1918
|
-
if (this.options.strategy === "restore" && !this.options.restore) {
|
|
1919
|
-
throw new ProviderValidationError("Missing restore options");
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
async #deleteFromRestoreOptions() {
|
|
1923
|
-
assertValidStrapi(this.strapi);
|
|
1924
|
-
if (!this.options.restore) {
|
|
1925
|
-
throw new ProviderValidationError("Missing restore options");
|
|
1926
|
-
}
|
|
1927
|
-
return deleteRecords(this.strapi, this.options.restore);
|
|
1928
|
-
}
|
|
1929
|
-
async #deleteAllAssets(trx) {
|
|
1930
|
-
assertValidStrapi(this.strapi);
|
|
1931
|
-
if (!this.#areAssetsIncluded()) {
|
|
1932
|
-
return;
|
|
1933
|
-
}
|
|
1934
|
-
const stream2 = this.strapi.db.queryBuilder("plugin::upload.file").select("*").transacting(trx).stream();
|
|
1935
|
-
for await (const file of stream2) {
|
|
1936
|
-
await this.strapi.plugin("upload").provider.delete(file);
|
|
1937
|
-
if (file.formats) {
|
|
1938
|
-
for (const fileFormat of Object.values(file.formats)) {
|
|
1939
|
-
await this.strapi.plugin("upload").provider.delete(fileFormat);
|
|
1940
|
-
}
|
|
1941
|
-
}
|
|
1942
|
-
}
|
|
1943
|
-
}
|
|
1944
|
-
async rollback() {
|
|
1945
|
-
await this.transaction?.rollback();
|
|
1946
|
-
}
|
|
1947
|
-
async beforeTransfer() {
|
|
1948
|
-
if (!this.strapi) {
|
|
1949
|
-
throw new Error("Strapi instance not found");
|
|
1950
|
-
}
|
|
1951
|
-
await this.transaction?.attach(async (trx) => {
|
|
1952
|
-
try {
|
|
1953
|
-
if (this.options.strategy === "restore") {
|
|
1954
|
-
await this.#handleAssetsBackup();
|
|
1955
|
-
await this.#deleteAllAssets(trx);
|
|
1956
|
-
await this.#deleteFromRestoreOptions();
|
|
1957
|
-
}
|
|
1958
|
-
} catch (error) {
|
|
1959
|
-
throw new Error(`restore failed ${error}`);
|
|
1960
|
-
}
|
|
1961
|
-
});
|
|
1962
|
-
}
|
|
1963
|
-
getMetadata() {
|
|
1964
|
-
assertValidStrapi(this.strapi, "Not able to get Schemas");
|
|
1965
|
-
const strapiVersion = this.strapi.config.get("info.strapi");
|
|
1966
|
-
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1967
|
-
return {
|
|
1968
|
-
createdAt,
|
|
1969
|
-
strapi: {
|
|
1970
|
-
version: strapiVersion
|
|
1971
|
-
}
|
|
1972
|
-
};
|
|
1973
|
-
}
|
|
1974
|
-
getSchemas() {
|
|
1975
|
-
assertValidStrapi(this.strapi, "Not able to get Schemas");
|
|
1976
|
-
const schemas = schemasToValidJSON({
|
|
1977
|
-
...this.strapi.contentTypes,
|
|
1978
|
-
...this.strapi.components
|
|
1979
|
-
});
|
|
1980
|
-
return mapSchemasValues(schemas);
|
|
1981
|
-
}
|
|
1982
|
-
createEntitiesWriteStream() {
|
|
1983
|
-
assertValidStrapi(this.strapi, "Not able to import entities");
|
|
1984
|
-
const { strategy } = this.options;
|
|
1985
|
-
const updateMappingTable = (type, oldID, newID) => {
|
|
1986
|
-
if (!this.#entitiesMapper[type]) {
|
|
1987
|
-
this.#entitiesMapper[type] = {};
|
|
1988
|
-
}
|
|
1989
|
-
Object.assign(this.#entitiesMapper[type], { [oldID]: newID });
|
|
1990
|
-
};
|
|
1991
|
-
if (strategy === "restore") {
|
|
1992
|
-
return createEntitiesWriteStream({
|
|
1993
|
-
strapi: this.strapi,
|
|
1994
|
-
updateMappingTable,
|
|
1995
|
-
transaction: this.transaction
|
|
1996
|
-
});
|
|
1997
|
-
}
|
|
1998
|
-
throw new ProviderValidationError(`Invalid strategy ${this.options.strategy}`, {
|
|
1999
|
-
check: "strategy",
|
|
2000
|
-
strategy: this.options.strategy,
|
|
2001
|
-
validStrategies: VALID_CONFLICT_STRATEGIES
|
|
2002
|
-
});
|
|
2003
|
-
}
|
|
2004
|
-
async #handleAssetsBackup() {
|
|
2005
|
-
assertValidStrapi(this.strapi, "Not able to create the assets backup");
|
|
2006
|
-
if (!this.#areAssetsIncluded()) {
|
|
2007
|
-
return;
|
|
2008
|
-
}
|
|
2009
|
-
if (this.strapi.config.get("plugin::upload").provider === "local") {
|
|
2010
|
-
const assetsDirectory = path.join(this.strapi.dirs.static.public, "uploads");
|
|
2011
|
-
const backupDirectory = path.join(
|
|
2012
|
-
this.strapi.dirs.static.public,
|
|
2013
|
-
this.uploadsBackupDirectoryName
|
|
2014
|
-
);
|
|
2015
|
-
try {
|
|
2016
|
-
await fse.access(
|
|
2017
|
-
assetsDirectory,
|
|
2018
|
-
// eslint-disable-next-line no-bitwise
|
|
2019
|
-
fse.constants.W_OK | fse.constants.R_OK | fse.constants.F_OK
|
|
2020
|
-
);
|
|
2021
|
-
await fse.access(path.join(assetsDirectory, ".."), fse.constants.W_OK | fse.constants.R_OK);
|
|
2022
|
-
await fse.move(assetsDirectory, backupDirectory);
|
|
2023
|
-
await fse.mkdir(assetsDirectory);
|
|
2024
|
-
await fse.outputFile(path.join(assetsDirectory, ".gitkeep"), "");
|
|
2025
|
-
} catch (err) {
|
|
2026
|
-
throw new ProviderTransferError(
|
|
2027
|
-
"The backup folder for the assets could not be created inside the public folder. Please ensure Strapi has write permissions on the public directory",
|
|
2028
|
-
{
|
|
2029
|
-
code: "ASSETS_DIRECTORY_ERR"
|
|
2030
|
-
}
|
|
2031
|
-
);
|
|
2032
|
-
}
|
|
2033
|
-
return backupDirectory;
|
|
2034
|
-
}
|
|
2035
|
-
}
|
|
2036
|
-
async #removeAssetsBackup() {
|
|
2037
|
-
assertValidStrapi(this.strapi, "Not able to remove Assets");
|
|
2038
|
-
if (!this.#areAssetsIncluded()) {
|
|
2039
|
-
return;
|
|
2040
|
-
}
|
|
2041
|
-
if (this.strapi.config.get("plugin::upload").provider === "local") {
|
|
2042
|
-
assertValidStrapi(this.strapi);
|
|
2043
|
-
const backupDirectory = path.join(
|
|
2044
|
-
this.strapi.dirs.static.public,
|
|
2045
|
-
this.uploadsBackupDirectoryName
|
|
2046
|
-
);
|
|
2047
|
-
await fse.rm(backupDirectory, { recursive: true, force: true });
|
|
2048
|
-
}
|
|
2049
|
-
}
|
|
2050
|
-
// TODO: Move this logic to the restore strategy
|
|
2051
|
-
async createAssetsWriteStream() {
|
|
2052
|
-
assertValidStrapi(this.strapi, "Not able to stream Assets");
|
|
2053
|
-
if (!this.#areAssetsIncluded()) {
|
|
2054
|
-
throw new ProviderTransferError(
|
|
2055
|
-
"Attempting to transfer assets when `assets` is not set in restore options"
|
|
2056
|
-
);
|
|
2057
|
-
}
|
|
2058
|
-
const removeAssetsBackup = this.#removeAssetsBackup.bind(this);
|
|
2059
|
-
const strapi2 = this.strapi;
|
|
2060
|
-
const transaction2 = this.transaction;
|
|
2061
|
-
const backupDirectory = this.uploadsBackupDirectoryName;
|
|
2062
|
-
const fileEntitiesMapper = this.#entitiesMapper["plugin::upload.file"];
|
|
2063
|
-
const restoreMediaEntitiesContent = this.#isContentTypeIncluded("plugin::upload.file");
|
|
2064
|
-
return new Writable({
|
|
2065
|
-
objectMode: true,
|
|
2066
|
-
async final(next) {
|
|
2067
|
-
await removeAssetsBackup();
|
|
2068
|
-
next();
|
|
2069
|
-
},
|
|
2070
|
-
async write(chunk, _encoding, callback) {
|
|
2071
|
-
await transaction2?.attach(async () => {
|
|
2072
|
-
if (!chunk.metadata) {
|
|
2073
|
-
const assetsDirectory = path.join(strapi2.dirs.static.public, "uploads");
|
|
2074
|
-
const entryPath = path.join(assetsDirectory, chunk.filename);
|
|
2075
|
-
const writableStream = fse.createWriteStream(entryPath);
|
|
2076
|
-
chunk.stream.pipe(writableStream).on("close", () => {
|
|
2077
|
-
callback(null);
|
|
2078
|
-
}).on("error", async (error) => {
|
|
2079
|
-
const errorMessage = error.code === "ENOSPC" ? " Your server doesn't have space to proceed with the import. " : " ";
|
|
2080
|
-
try {
|
|
2081
|
-
await fse.rm(assetsDirectory, { recursive: true, force: true });
|
|
2082
|
-
this.destroy(
|
|
2083
|
-
new ProviderTransferError(
|
|
2084
|
-
`There was an error during the transfer process.${errorMessage}The original files have been restored to ${assetsDirectory}`
|
|
2085
|
-
)
|
|
2086
|
-
);
|
|
2087
|
-
} catch (err) {
|
|
2088
|
-
throw new ProviderTransferError(
|
|
2089
|
-
`There was an error doing the rollback process. The original files are in ${backupDirectory}, but we failed to restore them to ${assetsDirectory}`
|
|
2090
|
-
);
|
|
2091
|
-
} finally {
|
|
2092
|
-
callback(error);
|
|
2093
|
-
}
|
|
2094
|
-
});
|
|
2095
|
-
return;
|
|
2096
|
-
}
|
|
2097
|
-
const uploadData = {
|
|
2098
|
-
...chunk.metadata,
|
|
2099
|
-
stream: Readable.from(chunk.stream),
|
|
2100
|
-
buffer: chunk?.buffer
|
|
2101
|
-
};
|
|
2102
|
-
const provider = strapi2.config.get("plugin::upload").provider;
|
|
2103
|
-
try {
|
|
2104
|
-
await strapi2.plugin("upload").provider.uploadStream(uploadData);
|
|
2105
|
-
if (!restoreMediaEntitiesContent) {
|
|
2106
|
-
return callback();
|
|
2107
|
-
}
|
|
2108
|
-
if (uploadData?.type) {
|
|
2109
|
-
const condition = uploadData?.id ? { id: fileEntitiesMapper[uploadData.id] } : { hash: uploadData.mainHash };
|
|
2110
|
-
const entry2 = await strapi2.db.query("plugin::upload.file").findOne({
|
|
2111
|
-
where: condition
|
|
2112
|
-
});
|
|
2113
|
-
const specificFormat = entry2?.formats?.[uploadData.type];
|
|
2114
|
-
if (specificFormat) {
|
|
2115
|
-
specificFormat.url = uploadData.url;
|
|
2116
|
-
}
|
|
2117
|
-
await strapi2.db.query("plugin::upload.file").update({
|
|
2118
|
-
where: { id: entry2.id },
|
|
2119
|
-
data: {
|
|
2120
|
-
formats: entry2.formats,
|
|
2121
|
-
provider
|
|
2122
|
-
}
|
|
2123
|
-
});
|
|
2124
|
-
return callback();
|
|
2125
|
-
}
|
|
2126
|
-
const entry = await strapi2.db.query("plugin::upload.file").findOne({
|
|
2127
|
-
where: { id: fileEntitiesMapper[uploadData.id] }
|
|
2128
|
-
});
|
|
2129
|
-
entry.url = uploadData.url;
|
|
2130
|
-
await strapi2.db.query("plugin::upload.file").update({
|
|
2131
|
-
where: { id: entry.id },
|
|
2132
|
-
data: {
|
|
2133
|
-
url: entry.url,
|
|
2134
|
-
provider
|
|
2135
|
-
}
|
|
2136
|
-
});
|
|
2137
|
-
callback();
|
|
2138
|
-
} catch (error) {
|
|
2139
|
-
callback(new Error(`Error while uploading asset ${chunk.filename} ${error}`));
|
|
2140
|
-
}
|
|
2141
|
-
});
|
|
2142
|
-
}
|
|
2143
|
-
});
|
|
2144
|
-
}
|
|
2145
|
-
async createConfigurationWriteStream() {
|
|
2146
|
-
assertValidStrapi(this.strapi, "Not able to stream Configurations");
|
|
2147
|
-
const { strategy } = this.options;
|
|
2148
|
-
if (strategy === "restore") {
|
|
2149
|
-
return createConfigurationWriteStream(this.strapi, this.transaction);
|
|
2150
|
-
}
|
|
2151
|
-
throw new ProviderValidationError(`Invalid strategy ${strategy}`, {
|
|
2152
|
-
check: "strategy",
|
|
2153
|
-
strategy,
|
|
2154
|
-
validStrategies: VALID_CONFLICT_STRATEGIES
|
|
2155
|
-
});
|
|
2156
|
-
}
|
|
2157
|
-
async createLinksWriteStream() {
|
|
2158
|
-
if (!this.strapi) {
|
|
2159
|
-
throw new Error("Not able to stream links. Strapi instance not found");
|
|
2160
|
-
}
|
|
2161
|
-
const { strategy } = this.options;
|
|
2162
|
-
const mapID = (uid, id) => this.#entitiesMapper[uid]?.[id];
|
|
2163
|
-
if (strategy === "restore") {
|
|
2164
|
-
return createLinksWriteStream(mapID, this.strapi, this.transaction, this.onWarning);
|
|
2165
|
-
}
|
|
2166
|
-
throw new ProviderValidationError(`Invalid strategy ${strategy}`, {
|
|
2167
|
-
check: "strategy",
|
|
2168
|
-
strategy,
|
|
2169
|
-
validStrategies: VALID_CONFLICT_STRATEGIES
|
|
2170
|
-
});
|
|
2171
|
-
}
|
|
2172
|
-
}
|
|
2173
|
-
const createLocalStrapiDestinationProvider = (options) => {
|
|
2174
|
-
return new LocalStrapiDestinationProvider(options);
|
|
2175
|
-
};
|
|
2176
|
-
const createEntitiesStream = (strapi2) => {
|
|
2177
|
-
const contentTypes2 = Object.values(strapi2.contentTypes);
|
|
2178
|
-
async function* contentTypeStreamGenerator() {
|
|
2179
|
-
for (const contentType of contentTypes2) {
|
|
2180
|
-
const query = createEntityQuery(strapi2).call(null, contentType.uid);
|
|
2181
|
-
const stream2 = strapi2.db.queryBuilder(contentType.uid).select("*").populate(query.deepPopulateComponentLikeQuery).stream();
|
|
2182
|
-
yield { contentType, stream: stream2 };
|
|
2183
|
-
}
|
|
2184
|
-
}
|
|
2185
|
-
return Readable.from(
|
|
2186
|
-
async function* entitiesGenerator() {
|
|
2187
|
-
for await (const { stream: stream2, contentType } of contentTypeStreamGenerator()) {
|
|
2188
|
-
try {
|
|
2189
|
-
for await (const entity2 of stream2) {
|
|
2190
|
-
yield { entity: entity2, contentType };
|
|
2191
|
-
}
|
|
2192
|
-
} catch {
|
|
2193
|
-
} finally {
|
|
2194
|
-
stream2.destroy();
|
|
2195
|
-
}
|
|
2196
|
-
}
|
|
2197
|
-
}()
|
|
2198
|
-
);
|
|
2199
|
-
};
|
|
2200
|
-
const createEntitiesTransformStream = () => {
|
|
2201
|
-
return new Transform({
|
|
2202
|
-
objectMode: true,
|
|
2203
|
-
transform(data, _encoding, callback) {
|
|
2204
|
-
const { entity: entity2, contentType } = data;
|
|
2205
|
-
const { id, ...attributes } = entity2;
|
|
2206
|
-
callback(null, {
|
|
2207
|
-
type: contentType.uid,
|
|
2208
|
-
id,
|
|
2209
|
-
data: attributes
|
|
2210
|
-
});
|
|
2211
|
-
}
|
|
2212
|
-
});
|
|
2213
|
-
};
|
|
2214
|
-
const createLinksStream = (strapi2) => {
|
|
2215
|
-
const uids = [...Object.keys(strapi2.contentTypes), ...Object.keys(strapi2.components)];
|
|
2216
|
-
return Readable.from(
|
|
2217
|
-
async function* linkGenerator() {
|
|
2218
|
-
const query = createLinkQuery(strapi2);
|
|
2219
|
-
for (const uid of uids) {
|
|
2220
|
-
const generator = query().generateAll(uid);
|
|
2221
|
-
for await (const link2 of generator) {
|
|
2222
|
-
yield link2;
|
|
2223
|
-
}
|
|
2224
|
-
}
|
|
2225
|
-
}()
|
|
2226
|
-
);
|
|
2227
|
-
};
|
|
2228
|
-
const createConfigurationStream = (strapi2) => {
|
|
2229
|
-
return Readable.from(
|
|
2230
|
-
async function* configurationGenerator() {
|
|
2231
|
-
const coreStoreStream = chain([
|
|
2232
|
-
strapi2.db.queryBuilder("strapi::core-store").stream(),
|
|
2233
|
-
(data) => set("value", JSON.parse(data.value), data),
|
|
2234
|
-
wrapConfigurationItem("core-store")
|
|
2235
|
-
]);
|
|
2236
|
-
const webhooksStream = chain([
|
|
2237
|
-
strapi2.db.queryBuilder("strapi::webhook").stream(),
|
|
2238
|
-
wrapConfigurationItem("webhook")
|
|
2239
|
-
]);
|
|
2240
|
-
const streams = [coreStoreStream, webhooksStream];
|
|
2241
|
-
for (const stream2 of streams) {
|
|
2242
|
-
for await (const item of stream2) {
|
|
2243
|
-
yield item;
|
|
2244
|
-
}
|
|
2245
|
-
}
|
|
2246
|
-
}()
|
|
2247
|
-
);
|
|
2248
|
-
};
|
|
2249
|
-
const wrapConfigurationItem = (type) => (value) => ({
|
|
2250
|
-
type,
|
|
2251
|
-
value
|
|
2252
|
-
});
|
|
2253
|
-
function getFileStream(filepath, strapi2, isLocal = false) {
|
|
2254
|
-
if (isLocal) {
|
|
2255
|
-
return createReadStream(filepath);
|
|
2256
|
-
}
|
|
2257
|
-
const readableStream = new PassThrough();
|
|
2258
|
-
strapi2.fetch(filepath).then((res) => {
|
|
2259
|
-
if (res.status !== 200) {
|
|
2260
|
-
readableStream.emit("error", new Error(`Request failed with status code ${res.status}`));
|
|
2261
|
-
return;
|
|
2262
|
-
}
|
|
2263
|
-
if (res.body) {
|
|
2264
|
-
Readable.fromWeb(new webStream.ReadableStream(res.body)).pipe(readableStream);
|
|
2265
|
-
} else {
|
|
2266
|
-
readableStream.emit("error", new Error("Empty data found for file"));
|
|
2267
|
-
}
|
|
2268
|
-
}).catch((error) => {
|
|
2269
|
-
readableStream.emit("error", error);
|
|
2270
|
-
});
|
|
2271
|
-
return readableStream;
|
|
2272
|
-
}
|
|
2273
|
-
function getFileStats(filepath, strapi2, isLocal = false) {
|
|
2274
|
-
if (isLocal) {
|
|
2275
|
-
return stat(filepath);
|
|
2276
|
-
}
|
|
2277
|
-
return new Promise((resolve, reject2) => {
|
|
2278
|
-
strapi2.fetch(filepath).then((res) => {
|
|
2279
|
-
if (res.status !== 200) {
|
|
2280
|
-
reject2(new Error(`Request failed with status code ${res.status}`));
|
|
2281
|
-
return;
|
|
2282
|
-
}
|
|
2283
|
-
const contentLength = res.headers.get("content-length");
|
|
2284
|
-
const stats = {
|
|
2285
|
-
size: contentLength ? parseInt(contentLength, 10) : 0
|
|
2286
|
-
};
|
|
2287
|
-
resolve(stats);
|
|
2288
|
-
}).catch((error) => {
|
|
2289
|
-
reject2(error);
|
|
2290
|
-
});
|
|
2291
|
-
});
|
|
2292
|
-
}
|
|
2293
|
-
async function signFile(file) {
|
|
2294
|
-
const { provider } = strapi.plugins.upload;
|
|
2295
|
-
const { provider: providerName } = strapi.config.get("plugin.upload");
|
|
2296
|
-
const isPrivate = await provider.isPrivate();
|
|
2297
|
-
if (file?.provider === providerName && isPrivate) {
|
|
2298
|
-
const signUrl = async (file2) => {
|
|
2299
|
-
const signedUrl = await provider.getSignedUrl(file2);
|
|
2300
|
-
file2.url = signedUrl.url;
|
|
2301
|
-
};
|
|
2302
|
-
await signUrl(file);
|
|
2303
|
-
if (file.formats) {
|
|
2304
|
-
for (const format of Object.keys(file.formats)) {
|
|
2305
|
-
await signUrl(file.formats[format]);
|
|
2306
|
-
}
|
|
2307
|
-
}
|
|
2308
|
-
}
|
|
2309
|
-
}
|
|
2310
|
-
const createAssetsStream = (strapi2) => {
|
|
2311
|
-
const generator = async function* () {
|
|
2312
|
-
const stream2 = strapi2.db.queryBuilder("plugin::upload.file").select("*").stream();
|
|
2313
|
-
for await (const file of stream2) {
|
|
2314
|
-
const isLocalProvider = file.provider === "local";
|
|
2315
|
-
if (!isLocalProvider) {
|
|
2316
|
-
await signFile(file);
|
|
2317
|
-
}
|
|
2318
|
-
const filepath = isLocalProvider ? join(strapi2.dirs.static.public, file.url) : file.url;
|
|
2319
|
-
const stats = await getFileStats(filepath, strapi2, isLocalProvider);
|
|
2320
|
-
const stream22 = getFileStream(filepath, strapi2, isLocalProvider);
|
|
2321
|
-
yield {
|
|
2322
|
-
metadata: file,
|
|
2323
|
-
filepath,
|
|
2324
|
-
filename: file.hash + file.ext,
|
|
2325
|
-
stream: stream22,
|
|
2326
|
-
stats: { size: stats.size }
|
|
2327
|
-
};
|
|
2328
|
-
if (file.formats) {
|
|
2329
|
-
for (const format of Object.keys(file.formats)) {
|
|
2330
|
-
const fileFormat = file.formats[format];
|
|
2331
|
-
const fileFormatFilepath = isLocalProvider ? join(strapi2.dirs.static.public, fileFormat.url) : fileFormat.url;
|
|
2332
|
-
const fileFormatStats = await getFileStats(fileFormatFilepath, strapi2, isLocalProvider);
|
|
2333
|
-
const fileFormatStream = getFileStream(fileFormatFilepath, strapi2, isLocalProvider);
|
|
2334
|
-
const metadata = { ...fileFormat, type: format, id: file.id, mainHash: file.hash };
|
|
2335
|
-
yield {
|
|
2336
|
-
metadata,
|
|
2337
|
-
filepath: fileFormatFilepath,
|
|
2338
|
-
filename: fileFormat.hash + fileFormat.ext,
|
|
2339
|
-
stream: fileFormatStream,
|
|
2340
|
-
stats: { size: fileFormatStats.size }
|
|
2341
|
-
};
|
|
2342
|
-
}
|
|
2343
|
-
}
|
|
2344
|
-
}
|
|
2345
|
-
};
|
|
2346
|
-
return Duplex.from(generator());
|
|
2347
|
-
};
|
|
2348
|
-
const createLocalStrapiSourceProvider = (options) => {
|
|
2349
|
-
return new LocalStrapiSourceProvider(options);
|
|
2350
|
-
};
|
|
2351
|
-
class LocalStrapiSourceProvider {
|
|
2352
|
-
name = "source::local-strapi";
|
|
2353
|
-
type = "source";
|
|
2354
|
-
options;
|
|
2355
|
-
strapi;
|
|
2356
|
-
constructor(options) {
|
|
2357
|
-
this.options = options;
|
|
2358
|
-
}
|
|
2359
|
-
async bootstrap() {
|
|
2360
|
-
this.strapi = await this.options.getStrapi();
|
|
2361
|
-
}
|
|
2362
|
-
async close() {
|
|
2363
|
-
const { autoDestroy } = this.options;
|
|
2364
|
-
if (autoDestroy === void 0 || autoDestroy === true) {
|
|
2365
|
-
await this.strapi?.destroy();
|
|
2366
|
-
}
|
|
2367
|
-
}
|
|
2368
|
-
getMetadata() {
|
|
2369
|
-
const strapiVersion = strapi.config.get("info.strapi");
|
|
2370
|
-
const createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2371
|
-
return {
|
|
2372
|
-
createdAt,
|
|
2373
|
-
strapi: {
|
|
2374
|
-
version: strapiVersion
|
|
2375
|
-
}
|
|
2376
|
-
};
|
|
2377
|
-
}
|
|
2378
|
-
async createEntitiesReadStream() {
|
|
2379
|
-
assertValidStrapi(this.strapi, "Not able to stream entities");
|
|
2380
|
-
return chain([
|
|
2381
|
-
// Entities stream
|
|
2382
|
-
createEntitiesStream(this.strapi),
|
|
2383
|
-
// Transform stream
|
|
2384
|
-
createEntitiesTransformStream()
|
|
2385
|
-
]);
|
|
2386
|
-
}
|
|
2387
|
-
createLinksReadStream() {
|
|
2388
|
-
assertValidStrapi(this.strapi, "Not able to stream links");
|
|
2389
|
-
return createLinksStream(this.strapi);
|
|
2390
|
-
}
|
|
2391
|
-
createConfigurationReadStream() {
|
|
2392
|
-
assertValidStrapi(this.strapi, "Not able to stream configuration");
|
|
2393
|
-
return createConfigurationStream(this.strapi);
|
|
2394
|
-
}
|
|
2395
|
-
getSchemas() {
|
|
2396
|
-
assertValidStrapi(this.strapi, "Not able to get Schemas");
|
|
2397
|
-
const schemas = schemasToValidJSON({
|
|
2398
|
-
...this.strapi.contentTypes,
|
|
2399
|
-
...this.strapi.components
|
|
2400
|
-
});
|
|
2401
|
-
return mapSchemasValues(schemas);
|
|
2402
|
-
}
|
|
2403
|
-
createSchemasReadStream() {
|
|
2404
|
-
return Readable.from(Object.values(this.getSchemas()));
|
|
2405
|
-
}
|
|
2406
|
-
createAssetsReadStream() {
|
|
2407
|
-
assertValidStrapi(this.strapi, "Not able to stream assets");
|
|
2408
|
-
return createAssetsStream(this.strapi);
|
|
2409
|
-
}
|
|
2410
|
-
}
|
|
2411
|
-
const createDispatcher = (ws, retryMessageOptions = {
|
|
2412
|
-
retryMessageMaxRetries: 5,
|
|
2413
|
-
retryMessageTimeout: 3e4
|
|
2414
|
-
}) => {
|
|
2415
|
-
const state = {};
|
|
2416
|
-
const dispatch = async (message, options = {}) => {
|
|
2417
|
-
if (!ws) {
|
|
2418
|
-
throw new Error("No websocket connection found");
|
|
2419
|
-
}
|
|
2420
|
-
return new Promise((resolve, reject2) => {
|
|
2421
|
-
const uuid = randomUUID();
|
|
2422
|
-
const payload = { ...message, uuid };
|
|
2423
|
-
let numberOfTimesMessageWasSent = 0;
|
|
2424
|
-
if (options.attachTransfer) {
|
|
2425
|
-
Object.assign(payload, { transferID: state.transfer?.id });
|
|
2426
|
-
}
|
|
2427
|
-
const stringifiedPayload = JSON.stringify(payload);
|
|
2428
|
-
ws.send(stringifiedPayload, (error) => {
|
|
2429
|
-
if (error) {
|
|
2430
|
-
reject2(error);
|
|
2431
|
-
}
|
|
2432
|
-
});
|
|
2433
|
-
const { retryMessageMaxRetries, retryMessageTimeout } = retryMessageOptions;
|
|
2434
|
-
const sendPeriodically = () => {
|
|
2435
|
-
if (numberOfTimesMessageWasSent <= retryMessageMaxRetries) {
|
|
2436
|
-
numberOfTimesMessageWasSent += 1;
|
|
2437
|
-
ws.send(stringifiedPayload, (error) => {
|
|
2438
|
-
if (error) {
|
|
2439
|
-
reject2(error);
|
|
2440
|
-
}
|
|
2441
|
-
});
|
|
2442
|
-
} else {
|
|
2443
|
-
reject2(new ProviderError("error", "Request timed out"));
|
|
2444
|
-
}
|
|
2445
|
-
};
|
|
2446
|
-
const interval = setInterval(sendPeriodically, retryMessageTimeout);
|
|
2447
|
-
const onResponse = (raw) => {
|
|
2448
|
-
const response = JSON.parse(raw.toString());
|
|
2449
|
-
if (response.uuid === uuid) {
|
|
2450
|
-
clearInterval(interval);
|
|
2451
|
-
if (response.error) {
|
|
2452
|
-
const message2 = response.error.message;
|
|
2453
|
-
const details = response.error.details?.details;
|
|
2454
|
-
const step = response.error.details?.step;
|
|
2455
|
-
let error = new ProviderError("error", message2, details);
|
|
2456
|
-
if (step === "transfer") {
|
|
2457
|
-
error = new ProviderTransferError(message2, details);
|
|
2458
|
-
} else if (step === "validation") {
|
|
2459
|
-
error = new ProviderValidationError(message2, details);
|
|
2460
|
-
} else if (step === "initialization") {
|
|
2461
|
-
error = new ProviderInitializationError(message2);
|
|
2462
|
-
}
|
|
2463
|
-
return reject2(error);
|
|
2464
|
-
}
|
|
2465
|
-
resolve(response.data ?? null);
|
|
2466
|
-
} else {
|
|
2467
|
-
ws.once("message", onResponse);
|
|
2468
|
-
}
|
|
2469
|
-
};
|
|
2470
|
-
ws.once("message", onResponse);
|
|
2471
|
-
});
|
|
2472
|
-
};
|
|
2473
|
-
const dispatchCommand = (payload) => {
|
|
2474
|
-
return dispatch({ type: "command", ...payload });
|
|
2475
|
-
};
|
|
2476
|
-
const dispatchTransferAction = async (action) => {
|
|
2477
|
-
const payload = { type: "transfer", kind: "action", action };
|
|
2478
|
-
return dispatch(payload, { attachTransfer: true }) ?? Promise.resolve(null);
|
|
2479
|
-
};
|
|
2480
|
-
const dispatchTransferStep = async (payload) => {
|
|
2481
|
-
const message = {
|
|
2482
|
-
type: "transfer",
|
|
2483
|
-
kind: "step",
|
|
2484
|
-
...payload
|
|
2485
|
-
};
|
|
2486
|
-
return dispatch(message, { attachTransfer: true }) ?? Promise.resolve(null);
|
|
2487
|
-
};
|
|
2488
|
-
const setTransferProperties = (properties) => {
|
|
2489
|
-
state.transfer = { ...properties };
|
|
2490
|
-
};
|
|
2491
|
-
return {
|
|
2492
|
-
get transferID() {
|
|
2493
|
-
return state.transfer?.id;
|
|
2494
|
-
},
|
|
2495
|
-
get transferKind() {
|
|
2496
|
-
return state.transfer?.kind;
|
|
2497
|
-
},
|
|
2498
|
-
setTransferProperties,
|
|
2499
|
-
dispatch,
|
|
2500
|
-
dispatchCommand,
|
|
2501
|
-
dispatchTransferAction,
|
|
2502
|
-
dispatchTransferStep
|
|
2503
|
-
};
|
|
2504
|
-
};
|
|
2505
|
-
const connectToWebsocket = (address, options) => {
|
|
2506
|
-
return new Promise((resolve, reject2) => {
|
|
2507
|
-
const server = new WebSocket(address, options);
|
|
2508
|
-
server.once("open", () => {
|
|
2509
|
-
resolve(server);
|
|
2510
|
-
});
|
|
2511
|
-
server.on("unexpected-response", (_req, res) => {
|
|
2512
|
-
if (res.statusCode === 401) {
|
|
2513
|
-
return reject2(
|
|
2514
|
-
new ProviderInitializationError(
|
|
2515
|
-
"Failed to initialize the connection: Authentication Error"
|
|
2516
|
-
)
|
|
2517
|
-
);
|
|
2518
|
-
}
|
|
2519
|
-
if (res.statusCode === 403) {
|
|
2520
|
-
return reject2(
|
|
2521
|
-
new ProviderInitializationError(
|
|
2522
|
-
"Failed to initialize the connection: Authorization Error"
|
|
2523
|
-
)
|
|
2524
|
-
);
|
|
2525
|
-
}
|
|
2526
|
-
if (res.statusCode === 404) {
|
|
2527
|
-
return reject2(
|
|
2528
|
-
new ProviderInitializationError(
|
|
2529
|
-
"Failed to initialize the connection: Data transfer is not enabled on the remote host"
|
|
2530
|
-
)
|
|
2531
|
-
);
|
|
2532
|
-
}
|
|
2533
|
-
return reject2(
|
|
2534
|
-
new ProviderInitializationError(
|
|
2535
|
-
`Failed to initialize the connection: Unexpected server response ${res.statusCode}`
|
|
2536
|
-
)
|
|
2537
|
-
);
|
|
2538
|
-
});
|
|
2539
|
-
server.once("error", (err) => {
|
|
2540
|
-
reject2(
|
|
2541
|
-
new ProviderTransferError(err.message, {
|
|
2542
|
-
details: {
|
|
2543
|
-
error: err.message
|
|
2544
|
-
}
|
|
2545
|
-
})
|
|
2546
|
-
);
|
|
2547
|
-
});
|
|
2548
|
-
});
|
|
2549
|
-
};
|
|
2550
|
-
const trimTrailingSlash = (input) => {
|
|
2551
|
-
return input.replace(/\/$/, "");
|
|
2552
|
-
};
|
|
2553
|
-
const TRANSFER_PATH = "/transfer/runner";
|
|
2554
|
-
const TRANSFER_METHODS = ["push", "pull"];
|
|
2555
|
-
const constants = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
2556
|
-
__proto__: null,
|
|
2557
|
-
TRANSFER_METHODS,
|
|
2558
|
-
TRANSFER_PATH
|
|
2559
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
2560
|
-
const jsonLength = (obj) => Buffer.byteLength(JSON.stringify(obj));
|
|
2561
|
-
class RemoteStrapiDestinationProvider {
|
|
2562
|
-
name = "destination::remote-strapi";
|
|
2563
|
-
type = "destination";
|
|
2564
|
-
options;
|
|
2565
|
-
ws;
|
|
2566
|
-
dispatcher;
|
|
2567
|
-
transferID;
|
|
2568
|
-
constructor(options) {
|
|
2569
|
-
this.options = options;
|
|
2570
|
-
this.ws = null;
|
|
2571
|
-
this.dispatcher = null;
|
|
2572
|
-
this.transferID = null;
|
|
2573
|
-
}
|
|
2574
|
-
async initTransfer() {
|
|
2575
|
-
const { strategy, restore } = this.options;
|
|
2576
|
-
const query = this.dispatcher?.dispatchCommand({
|
|
2577
|
-
command: "init",
|
|
2578
|
-
params: { options: { strategy, restore }, transfer: "push" }
|
|
2579
|
-
});
|
|
2580
|
-
const res = await query;
|
|
2581
|
-
if (!res?.transferID) {
|
|
2582
|
-
throw new ProviderTransferError("Init failed, invalid response from the server");
|
|
2583
|
-
}
|
|
2584
|
-
return res.transferID;
|
|
2585
|
-
}
|
|
2586
|
-
#startStepOnce(stage) {
|
|
2587
|
-
return once(() => this.#startStep(stage));
|
|
2588
|
-
}
|
|
2589
|
-
async #startStep(step) {
|
|
2590
|
-
try {
|
|
2591
|
-
await this.dispatcher?.dispatchTransferStep({ action: "start", step });
|
|
2592
|
-
} catch (e) {
|
|
2593
|
-
if (e instanceof Error) {
|
|
2594
|
-
return e;
|
|
2595
|
-
}
|
|
2596
|
-
if (typeof e === "string") {
|
|
2597
|
-
return new ProviderTransferError(e);
|
|
2598
|
-
}
|
|
2599
|
-
return new ProviderTransferError("Unexpected error");
|
|
2600
|
-
}
|
|
2601
|
-
return null;
|
|
2602
|
-
}
|
|
2603
|
-
async #endStep(step) {
|
|
2604
|
-
try {
|
|
2605
|
-
await this.dispatcher?.dispatchTransferStep({ action: "end", step });
|
|
2606
|
-
} catch (e) {
|
|
2607
|
-
if (e instanceof Error) {
|
|
2608
|
-
return e;
|
|
2609
|
-
}
|
|
2610
|
-
if (typeof e === "string") {
|
|
2611
|
-
return new ProviderTransferError(e);
|
|
2612
|
-
}
|
|
2613
|
-
return new ProviderTransferError("Unexpected error");
|
|
2614
|
-
}
|
|
2615
|
-
return null;
|
|
2616
|
-
}
|
|
2617
|
-
async #streamStep(step, data) {
|
|
2618
|
-
try {
|
|
2619
|
-
await this.dispatcher?.dispatchTransferStep({ action: "stream", step, data });
|
|
2620
|
-
} catch (e) {
|
|
2621
|
-
if (e instanceof Error) {
|
|
2622
|
-
return e;
|
|
2623
|
-
}
|
|
2624
|
-
if (typeof e === "string") {
|
|
2625
|
-
return new ProviderTransferError(e);
|
|
2626
|
-
}
|
|
2627
|
-
return new ProviderTransferError("Unexpected error");
|
|
2628
|
-
}
|
|
2629
|
-
return null;
|
|
2630
|
-
}
|
|
2631
|
-
#writeStream(step) {
|
|
2632
|
-
const batchSize = 1024 * 1024;
|
|
2633
|
-
const startTransferOnce = this.#startStepOnce(step);
|
|
2634
|
-
let batch = [];
|
|
2635
|
-
const batchLength = () => jsonLength(batch);
|
|
2636
|
-
return new Writable({
|
|
2637
|
-
objectMode: true,
|
|
2638
|
-
final: async (callback) => {
|
|
2639
|
-
if (batch.length > 0) {
|
|
2640
|
-
const streamError = await this.#streamStep(step, batch);
|
|
2641
|
-
batch = [];
|
|
2642
|
-
if (streamError) {
|
|
2643
|
-
return callback(streamError);
|
|
2644
|
-
}
|
|
2645
|
-
}
|
|
2646
|
-
const e = await this.#endStep(step);
|
|
2647
|
-
callback(e);
|
|
2648
|
-
},
|
|
2649
|
-
write: async (chunk, _encoding, callback) => {
|
|
2650
|
-
const startError = await startTransferOnce();
|
|
2651
|
-
if (startError) {
|
|
2652
|
-
return callback(startError);
|
|
2653
|
-
}
|
|
2654
|
-
batch.push(chunk);
|
|
2655
|
-
if (batchLength() >= batchSize) {
|
|
2656
|
-
const streamError = await this.#streamStep(step, batch);
|
|
2657
|
-
batch = [];
|
|
2658
|
-
if (streamError) {
|
|
2659
|
-
return callback(streamError);
|
|
2660
|
-
}
|
|
2661
|
-
}
|
|
2662
|
-
callback();
|
|
2663
|
-
}
|
|
2664
|
-
});
|
|
2665
|
-
}
|
|
2666
|
-
async bootstrap() {
|
|
2667
|
-
const { url, auth } = this.options;
|
|
2668
|
-
const validProtocols = ["https:", "http:"];
|
|
2669
|
-
let ws;
|
|
2670
|
-
if (!validProtocols.includes(url.protocol)) {
|
|
2671
|
-
throw new ProviderValidationError(`Invalid protocol "${url.protocol}"`, {
|
|
2672
|
-
check: "url",
|
|
2673
|
-
details: {
|
|
2674
|
-
protocol: url.protocol,
|
|
2675
|
-
validProtocols
|
|
2676
|
-
}
|
|
2677
|
-
});
|
|
2678
|
-
}
|
|
2679
|
-
const wsProtocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
2680
|
-
const wsUrl = `${wsProtocol}//${url.host}${trimTrailingSlash(
|
|
2681
|
-
url.pathname
|
|
2682
|
-
)}${TRANSFER_PATH}/push`;
|
|
2683
|
-
if (!auth) {
|
|
2684
|
-
ws = await connectToWebsocket(wsUrl);
|
|
2685
|
-
} else if (auth.type === "token") {
|
|
2686
|
-
const headers = { Authorization: `Bearer ${auth.token}` };
|
|
2687
|
-
ws = await connectToWebsocket(wsUrl, { headers });
|
|
2688
|
-
} else {
|
|
2689
|
-
throw new ProviderValidationError("Auth method not available", {
|
|
2690
|
-
check: "auth.type",
|
|
2691
|
-
details: {
|
|
2692
|
-
auth: auth.type
|
|
2693
|
-
}
|
|
2694
|
-
});
|
|
2695
|
-
}
|
|
2696
|
-
this.ws = ws;
|
|
2697
|
-
const { retryMessageOptions } = this.options;
|
|
2698
|
-
this.dispatcher = createDispatcher(this.ws, retryMessageOptions);
|
|
2699
|
-
this.transferID = await this.initTransfer();
|
|
2700
|
-
this.dispatcher.setTransferProperties({ id: this.transferID, kind: "push" });
|
|
2701
|
-
await this.dispatcher.dispatchTransferAction("bootstrap");
|
|
2702
|
-
}
|
|
2703
|
-
async close() {
|
|
2704
|
-
if (this.transferID && this.dispatcher) {
|
|
2705
|
-
await this.dispatcher.dispatchTransferAction("close");
|
|
2706
|
-
await this.dispatcher.dispatchCommand({
|
|
2707
|
-
command: "end",
|
|
2708
|
-
params: { transferID: this.transferID }
|
|
2709
|
-
});
|
|
2710
|
-
}
|
|
2711
|
-
await new Promise((resolve) => {
|
|
2712
|
-
const { ws } = this;
|
|
2713
|
-
if (!ws || ws.CLOSED) {
|
|
2714
|
-
resolve();
|
|
2715
|
-
return;
|
|
2716
|
-
}
|
|
2717
|
-
ws.on("close", () => resolve()).close();
|
|
2718
|
-
});
|
|
2719
|
-
}
|
|
2720
|
-
getMetadata() {
|
|
2721
|
-
return this.dispatcher?.dispatchTransferAction("getMetadata") ?? null;
|
|
2722
|
-
}
|
|
2723
|
-
async beforeTransfer() {
|
|
2724
|
-
await this.dispatcher?.dispatchTransferAction("beforeTransfer");
|
|
2725
|
-
}
|
|
2726
|
-
async rollback() {
|
|
2727
|
-
await this.dispatcher?.dispatchTransferAction("rollback");
|
|
2728
|
-
}
|
|
2729
|
-
getSchemas() {
|
|
2730
|
-
if (!this.dispatcher) {
|
|
2731
|
-
return Promise.resolve(null);
|
|
2732
|
-
}
|
|
2733
|
-
return this.dispatcher.dispatchTransferAction("getSchemas");
|
|
2734
|
-
}
|
|
2735
|
-
createEntitiesWriteStream() {
|
|
2736
|
-
return this.#writeStream("entities");
|
|
2737
|
-
}
|
|
2738
|
-
createLinksWriteStream() {
|
|
2739
|
-
return this.#writeStream("links");
|
|
2740
|
-
}
|
|
2741
|
-
createConfigurationWriteStream() {
|
|
2742
|
-
return this.#writeStream("configuration");
|
|
2743
|
-
}
|
|
2744
|
-
createAssetsWriteStream() {
|
|
2745
|
-
let batch = [];
|
|
2746
|
-
let hasStarted = false;
|
|
2747
|
-
const batchSize = 1024 * 1024;
|
|
2748
|
-
const batchLength = () => {
|
|
2749
|
-
return batch.reduce(
|
|
2750
|
-
(acc, chunk) => chunk.action === "stream" ? acc + chunk.data.byteLength : acc,
|
|
2751
|
-
0
|
|
2752
|
-
);
|
|
2753
|
-
};
|
|
2754
|
-
const startAssetsTransferOnce = this.#startStepOnce("assets");
|
|
2755
|
-
const flush = async () => {
|
|
2756
|
-
const streamError = await this.#streamStep("assets", batch);
|
|
2757
|
-
batch = [];
|
|
2758
|
-
return streamError;
|
|
2759
|
-
};
|
|
2760
|
-
const safePush = async (chunk) => {
|
|
2761
|
-
batch.push(chunk);
|
|
2762
|
-
if (batchLength() >= batchSize) {
|
|
2763
|
-
const streamError = await flush();
|
|
2764
|
-
if (streamError) {
|
|
2765
|
-
throw streamError;
|
|
2766
|
-
}
|
|
2767
|
-
}
|
|
2768
|
-
};
|
|
2769
|
-
return new Writable({
|
|
2770
|
-
objectMode: true,
|
|
2771
|
-
final: async (callback) => {
|
|
2772
|
-
if (batch.length > 0) {
|
|
2773
|
-
await flush();
|
|
2774
|
-
}
|
|
2775
|
-
if (hasStarted) {
|
|
2776
|
-
const endStepError = await this.#endStep("assets");
|
|
2777
|
-
if (endStepError) {
|
|
2778
|
-
return callback(endStepError);
|
|
2779
|
-
}
|
|
2780
|
-
}
|
|
2781
|
-
return callback(null);
|
|
2782
|
-
},
|
|
2783
|
-
async write(asset, _encoding, callback) {
|
|
2784
|
-
const startError = await startAssetsTransferOnce();
|
|
2785
|
-
if (startError) {
|
|
2786
|
-
return callback(startError);
|
|
2787
|
-
}
|
|
2788
|
-
hasStarted = true;
|
|
2789
|
-
const assetID = randomUUID();
|
|
2790
|
-
const { filename, filepath, stats, stream: stream2, metadata } = asset;
|
|
2791
|
-
try {
|
|
2792
|
-
await safePush({
|
|
2793
|
-
action: "start",
|
|
2794
|
-
assetID,
|
|
2795
|
-
data: { filename, filepath, stats, metadata }
|
|
2796
|
-
});
|
|
2797
|
-
for await (const chunk of stream2) {
|
|
2798
|
-
await safePush({ action: "stream", assetID, data: chunk });
|
|
2799
|
-
}
|
|
2800
|
-
await safePush({ action: "end", assetID });
|
|
2801
|
-
callback();
|
|
2802
|
-
} catch (error) {
|
|
2803
|
-
if (error instanceof Error) {
|
|
2804
|
-
callback(error);
|
|
2805
|
-
}
|
|
2806
|
-
}
|
|
2807
|
-
}
|
|
2808
|
-
});
|
|
2809
|
-
}
|
|
2810
|
-
}
|
|
2811
|
-
const createRemoteStrapiDestinationProvider = (options) => {
|
|
2812
|
-
return new RemoteStrapiDestinationProvider(options);
|
|
2813
|
-
};
|
|
2814
|
-
class RemoteStrapiSourceProvider {
|
|
2815
|
-
name = "source::remote-strapi";
|
|
2816
|
-
type = "source";
|
|
2817
|
-
options;
|
|
2818
|
-
ws;
|
|
2819
|
-
dispatcher;
|
|
2820
|
-
constructor(options) {
|
|
2821
|
-
this.options = options;
|
|
2822
|
-
this.ws = null;
|
|
2823
|
-
this.dispatcher = null;
|
|
2824
|
-
}
|
|
2825
|
-
results;
|
|
2826
|
-
async #createStageReadStream(stage) {
|
|
2827
|
-
const startResult = await this.#startStep(stage);
|
|
2828
|
-
if (startResult instanceof Error) {
|
|
2829
|
-
throw startResult;
|
|
2830
|
-
}
|
|
2831
|
-
const { id: processID } = startResult;
|
|
2832
|
-
const stream2 = new PassThrough({ objectMode: true });
|
|
2833
|
-
const listener = async (raw) => {
|
|
2834
|
-
const parsed = JSON.parse(raw.toString());
|
|
2835
|
-
if (!parsed.uuid || parsed?.data?.type !== "transfer" || parsed?.data?.id !== processID) {
|
|
2836
|
-
this.ws?.once("message", listener);
|
|
2837
|
-
return;
|
|
2838
|
-
}
|
|
2839
|
-
const { uuid, data: message } = parsed;
|
|
2840
|
-
const { ended, error, data } = message;
|
|
2841
|
-
if (error) {
|
|
2842
|
-
await this.#respond(uuid);
|
|
2843
|
-
stream2.destroy(error);
|
|
2844
|
-
return;
|
|
2845
|
-
}
|
|
2846
|
-
if (ended) {
|
|
2847
|
-
await this.#respond(uuid);
|
|
2848
|
-
await this.#endStep(stage);
|
|
2849
|
-
stream2.end();
|
|
2850
|
-
return;
|
|
2851
|
-
}
|
|
2852
|
-
for (const item of castArray(data)) {
|
|
2853
|
-
stream2.push(item);
|
|
2854
|
-
}
|
|
2855
|
-
this.ws?.once("message", listener);
|
|
2856
|
-
await this.#respond(uuid);
|
|
2857
|
-
};
|
|
2858
|
-
this.ws?.once("message", listener);
|
|
2859
|
-
return stream2;
|
|
2860
|
-
}
|
|
2861
|
-
createEntitiesReadStream() {
|
|
2862
|
-
return this.#createStageReadStream("entities");
|
|
2863
|
-
}
|
|
2864
|
-
createLinksReadStream() {
|
|
2865
|
-
return this.#createStageReadStream("links");
|
|
2866
|
-
}
|
|
2867
|
-
writeAsync = (stream2, data) => {
|
|
2868
|
-
return new Promise((resolve, reject2) => {
|
|
2869
|
-
stream2.write(data, (error) => {
|
|
2870
|
-
if (error) {
|
|
2871
|
-
reject2(error);
|
|
2872
|
-
}
|
|
2873
|
-
resolve();
|
|
2874
|
-
});
|
|
2875
|
-
});
|
|
2876
|
-
};
|
|
2877
|
-
async createAssetsReadStream() {
|
|
2878
|
-
const assets = {};
|
|
2879
|
-
const stream2 = await this.#createStageReadStream("assets");
|
|
2880
|
-
const pass = new PassThrough({ objectMode: true });
|
|
2881
|
-
stream2.on("data", async (payload) => {
|
|
2882
|
-
for (const item of payload) {
|
|
2883
|
-
const { action } = item;
|
|
2884
|
-
if (action === "start") {
|
|
2885
|
-
assets[item.assetID] = { ...item.data, stream: new PassThrough() };
|
|
2886
|
-
await this.writeAsync(pass, assets[item.assetID]);
|
|
2887
|
-
} else if (action === "stream") {
|
|
2888
|
-
const rawBuffer = item.data;
|
|
2889
|
-
const chunk = Buffer.from(rawBuffer.data);
|
|
2890
|
-
await this.writeAsync(assets[item.assetID].stream, chunk);
|
|
2891
|
-
} else if (action === "end") {
|
|
2892
|
-
await new Promise((resolve, reject2) => {
|
|
2893
|
-
const { stream: assetStream } = assets[item.assetID];
|
|
2894
|
-
assetStream.on("close", () => {
|
|
2895
|
-
delete assets[item.assetID];
|
|
2896
|
-
resolve();
|
|
2897
|
-
}).on("error", reject2).end();
|
|
2898
|
-
});
|
|
2899
|
-
}
|
|
2900
|
-
}
|
|
2901
|
-
}).on("close", () => {
|
|
2902
|
-
pass.end();
|
|
2903
|
-
});
|
|
2904
|
-
return pass;
|
|
2905
|
-
}
|
|
2906
|
-
createConfigurationReadStream() {
|
|
2907
|
-
return this.#createStageReadStream("configuration");
|
|
2908
|
-
}
|
|
2909
|
-
async getMetadata() {
|
|
2910
|
-
const metadata = await this.dispatcher?.dispatchTransferAction("getMetadata");
|
|
2911
|
-
return metadata ?? null;
|
|
2912
|
-
}
|
|
2913
|
-
assertValidProtocol(url) {
|
|
2914
|
-
const validProtocols = ["https:", "http:"];
|
|
2915
|
-
if (!validProtocols.includes(url.protocol)) {
|
|
2916
|
-
throw new ProviderValidationError(`Invalid protocol "${url.protocol}"`, {
|
|
2917
|
-
check: "url",
|
|
2918
|
-
details: {
|
|
2919
|
-
protocol: url.protocol,
|
|
2920
|
-
validProtocols
|
|
2921
|
-
}
|
|
2922
|
-
});
|
|
2923
|
-
}
|
|
2924
|
-
}
|
|
2925
|
-
async initTransfer() {
|
|
2926
|
-
const query = this.dispatcher?.dispatchCommand({
|
|
2927
|
-
command: "init"
|
|
2928
|
-
});
|
|
2929
|
-
const res = await query;
|
|
2930
|
-
if (!res?.transferID) {
|
|
2931
|
-
throw new ProviderTransferError("Init failed, invalid response from the server");
|
|
2932
|
-
}
|
|
2933
|
-
return res.transferID;
|
|
2934
|
-
}
|
|
2935
|
-
async bootstrap() {
|
|
2936
|
-
const { url, auth } = this.options;
|
|
2937
|
-
let ws;
|
|
2938
|
-
this.assertValidProtocol(url);
|
|
2939
|
-
const wsProtocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
2940
|
-
const wsUrl = `${wsProtocol}//${url.host}${trimTrailingSlash(
|
|
2941
|
-
url.pathname
|
|
2942
|
-
)}${TRANSFER_PATH}/pull`;
|
|
2943
|
-
if (!auth) {
|
|
2944
|
-
ws = await connectToWebsocket(wsUrl);
|
|
2945
|
-
} else if (auth.type === "token") {
|
|
2946
|
-
const headers = { Authorization: `Bearer ${auth.token}` };
|
|
2947
|
-
ws = await connectToWebsocket(wsUrl, { headers });
|
|
2948
|
-
} else {
|
|
2949
|
-
throw new ProviderValidationError("Auth method not available", {
|
|
2950
|
-
check: "auth.type",
|
|
2951
|
-
details: {
|
|
2952
|
-
auth: auth.type
|
|
2953
|
-
}
|
|
2954
|
-
});
|
|
2955
|
-
}
|
|
2956
|
-
this.ws = ws;
|
|
2957
|
-
const { retryMessageOptions } = this.options;
|
|
2958
|
-
this.dispatcher = createDispatcher(this.ws, retryMessageOptions);
|
|
2959
|
-
const transferID = await this.initTransfer();
|
|
2960
|
-
this.dispatcher.setTransferProperties({ id: transferID, kind: "pull" });
|
|
2961
|
-
await this.dispatcher.dispatchTransferAction("bootstrap");
|
|
2962
|
-
}
|
|
2963
|
-
async close() {
|
|
2964
|
-
await this.dispatcher?.dispatchTransferAction("close");
|
|
2965
|
-
await new Promise((resolve) => {
|
|
2966
|
-
const { ws } = this;
|
|
2967
|
-
if (!ws || ws.CLOSED) {
|
|
2968
|
-
resolve();
|
|
2969
|
-
return;
|
|
2970
|
-
}
|
|
2971
|
-
ws.on("close", () => resolve()).close();
|
|
2972
|
-
});
|
|
2973
|
-
}
|
|
2974
|
-
async getSchemas() {
|
|
2975
|
-
const schemas = await this.dispatcher?.dispatchTransferAction("getSchemas");
|
|
2976
|
-
return schemas ?? null;
|
|
2977
|
-
}
|
|
2978
|
-
async #startStep(step) {
|
|
2979
|
-
try {
|
|
2980
|
-
return await this.dispatcher?.dispatchTransferStep({ action: "start", step });
|
|
2981
|
-
} catch (e) {
|
|
2982
|
-
if (e instanceof Error) {
|
|
2983
|
-
return e;
|
|
2984
|
-
}
|
|
2985
|
-
if (typeof e === "string") {
|
|
2986
|
-
return new ProviderTransferError(e);
|
|
2987
|
-
}
|
|
2988
|
-
return new ProviderTransferError("Unexpected error");
|
|
2989
|
-
}
|
|
2990
|
-
}
|
|
2991
|
-
async #respond(uuid) {
|
|
2992
|
-
return new Promise((resolve, reject2) => {
|
|
2993
|
-
this.ws?.send(JSON.stringify({ uuid }), (e) => {
|
|
2994
|
-
if (e) {
|
|
2995
|
-
reject2(e);
|
|
2996
|
-
} else {
|
|
2997
|
-
resolve(e);
|
|
2998
|
-
}
|
|
2999
|
-
});
|
|
3000
|
-
});
|
|
3001
|
-
}
|
|
3002
|
-
async #endStep(step) {
|
|
3003
|
-
try {
|
|
3004
|
-
await this.dispatcher?.dispatchTransferStep({ action: "end", step });
|
|
3005
|
-
} catch (e) {
|
|
3006
|
-
if (e instanceof Error) {
|
|
3007
|
-
return e;
|
|
3008
|
-
}
|
|
3009
|
-
if (typeof e === "string") {
|
|
3010
|
-
return new ProviderTransferError(e);
|
|
3011
|
-
}
|
|
3012
|
-
return new ProviderTransferError("Unexpected error");
|
|
3013
|
-
}
|
|
3014
|
-
return null;
|
|
3015
|
-
}
|
|
3016
|
-
}
|
|
3017
|
-
const createRemoteStrapiSourceProvider = (options) => {
|
|
3018
|
-
return new RemoteStrapiSourceProvider(options);
|
|
3019
|
-
};
|
|
3020
|
-
const index$5 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
3021
|
-
__proto__: null,
|
|
3022
|
-
DEFAULT_CONFLICT_STRATEGY,
|
|
3023
|
-
VALID_CONFLICT_STRATEGIES,
|
|
3024
|
-
createLocalStrapiDestinationProvider,
|
|
3025
|
-
createLocalStrapiSourceProvider,
|
|
3026
|
-
createRemoteStrapiDestinationProvider,
|
|
3027
|
-
createRemoteStrapiSourceProvider
|
|
3028
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
3029
|
-
const DEFAULT_TRANSFER_FLOW = [
|
|
3030
|
-
{
|
|
3031
|
-
kind: "action",
|
|
3032
|
-
action: "bootstrap"
|
|
3033
|
-
},
|
|
3034
|
-
{
|
|
3035
|
-
kind: "action",
|
|
3036
|
-
action: "init"
|
|
3037
|
-
},
|
|
3038
|
-
{
|
|
3039
|
-
kind: "action",
|
|
3040
|
-
action: "beforeTransfer"
|
|
3041
|
-
},
|
|
3042
|
-
{
|
|
3043
|
-
kind: "transfer",
|
|
3044
|
-
stage: "schemas"
|
|
3045
|
-
},
|
|
3046
|
-
{
|
|
3047
|
-
kind: "transfer",
|
|
3048
|
-
stage: "entities"
|
|
3049
|
-
},
|
|
3050
|
-
{
|
|
3051
|
-
kind: "transfer",
|
|
3052
|
-
stage: "assets"
|
|
3053
|
-
},
|
|
3054
|
-
{
|
|
3055
|
-
kind: "transfer",
|
|
3056
|
-
stage: "links"
|
|
3057
|
-
},
|
|
3058
|
-
{
|
|
3059
|
-
kind: "transfer",
|
|
3060
|
-
stage: "configuration"
|
|
3061
|
-
},
|
|
3062
|
-
{
|
|
3063
|
-
kind: "action",
|
|
3064
|
-
action: "close"
|
|
3065
|
-
}
|
|
3066
|
-
];
|
|
3067
|
-
const createFlow = (flow) => {
|
|
3068
|
-
const state = { step: null };
|
|
3069
|
-
const stepEqual = (stepA, stepB) => {
|
|
3070
|
-
if (stepA.kind === "action" && stepB.kind === "action") {
|
|
3071
|
-
return stepA.action === stepB.action;
|
|
3072
|
-
}
|
|
3073
|
-
if (stepA.kind === "transfer" && stepB.kind === "transfer") {
|
|
3074
|
-
return stepA.stage === stepB.stage;
|
|
3075
|
-
}
|
|
3076
|
-
return false;
|
|
3077
|
-
};
|
|
3078
|
-
const findStepIndex = (step) => flow.findIndex((flowStep) => stepEqual(step, flowStep));
|
|
3079
|
-
return {
|
|
3080
|
-
has(step) {
|
|
3081
|
-
return findStepIndex(step) !== -1;
|
|
3082
|
-
},
|
|
3083
|
-
can(step) {
|
|
3084
|
-
if (state.step === null) {
|
|
3085
|
-
return true;
|
|
3086
|
-
}
|
|
3087
|
-
const indexesDifference = findStepIndex(step) - findStepIndex(state.step);
|
|
3088
|
-
if (indexesDifference === 0 && step.kind === "transfer") {
|
|
3089
|
-
return true;
|
|
3090
|
-
}
|
|
3091
|
-
return indexesDifference > 0;
|
|
3092
|
-
},
|
|
3093
|
-
cannot(step) {
|
|
3094
|
-
return !this.can(step);
|
|
3095
|
-
},
|
|
3096
|
-
set(step) {
|
|
3097
|
-
const canSwitch = this.can(step);
|
|
3098
|
-
if (!canSwitch) {
|
|
3099
|
-
throw new Error("Impossible to proceed to the given step");
|
|
3100
|
-
}
|
|
3101
|
-
state.step = step;
|
|
3102
|
-
return this;
|
|
3103
|
-
},
|
|
3104
|
-
get() {
|
|
3105
|
-
return state.step;
|
|
3106
|
-
}
|
|
3107
|
-
};
|
|
3108
|
-
};
|
|
3109
|
-
const VALID_TRANSFER_COMMANDS = ["init", "end", "status"];
|
|
3110
|
-
const transformUpgradeHeader = (header = "") => {
|
|
3111
|
-
return header.split(",").map((s) => s.trim().toLowerCase());
|
|
3112
|
-
};
|
|
3113
|
-
let timeouts;
|
|
3114
|
-
const hasHttpServer = () => {
|
|
3115
|
-
return typeof strapi !== "undefined" && !!strapi?.server?.httpServer;
|
|
3116
|
-
};
|
|
3117
|
-
const disableTimeouts = () => {
|
|
3118
|
-
if (!hasHttpServer()) {
|
|
3119
|
-
return;
|
|
3120
|
-
}
|
|
3121
|
-
const { httpServer } = strapi.server;
|
|
3122
|
-
if (!timeouts) {
|
|
3123
|
-
timeouts = {
|
|
3124
|
-
headersTimeout: httpServer.headersTimeout,
|
|
3125
|
-
requestTimeout: httpServer.requestTimeout
|
|
3126
|
-
};
|
|
3127
|
-
}
|
|
3128
|
-
httpServer.headersTimeout = 0;
|
|
3129
|
-
httpServer.requestTimeout = 0;
|
|
3130
|
-
strapi.log.info("[Data transfer] Disabling http timeouts");
|
|
3131
|
-
};
|
|
3132
|
-
const resetTimeouts = () => {
|
|
3133
|
-
if (!hasHttpServer() || !timeouts) {
|
|
3134
|
-
return;
|
|
3135
|
-
}
|
|
3136
|
-
const { httpServer } = strapi.server;
|
|
3137
|
-
strapi.log.info("[Data transfer] Restoring http timeouts");
|
|
3138
|
-
httpServer.headersTimeout = timeouts.headersTimeout;
|
|
3139
|
-
httpServer.requestTimeout = timeouts.requestTimeout;
|
|
3140
|
-
};
|
|
3141
|
-
const assertValidHeader = (ctx) => {
|
|
3142
|
-
if (ctx.headers.upgrade === "websocket") {
|
|
3143
|
-
return;
|
|
3144
|
-
}
|
|
3145
|
-
const upgradeHeader = transformUpgradeHeader(ctx.headers.upgrade);
|
|
3146
|
-
const logSafeUpgradeHeader = JSON.stringify(ctx.headers.upgrade)?.replace(/[^a-z0-9\s.,|]/gi, "").substring(0, 50);
|
|
3147
|
-
if (!upgradeHeader.includes("websocket")) {
|
|
3148
|
-
throw new Error(
|
|
3149
|
-
`Transfer Upgrade header expected 'websocket', found '${logSafeUpgradeHeader}'. Please ensure that your server or proxy is not modifying the Upgrade header.`
|
|
3150
|
-
);
|
|
3151
|
-
}
|
|
3152
|
-
strapi.log.info(
|
|
3153
|
-
`Transfer Upgrade header expected only 'websocket', found unexpected values: ${logSafeUpgradeHeader}`
|
|
3154
|
-
);
|
|
3155
|
-
};
|
|
3156
|
-
const isDataTransferMessage = (message) => {
|
|
3157
|
-
if (!message || typeof message !== "object") {
|
|
3158
|
-
return false;
|
|
3159
|
-
}
|
|
3160
|
-
const { uuid, type } = message;
|
|
3161
|
-
if (typeof uuid !== "string" || typeof type !== "string") {
|
|
3162
|
-
return false;
|
|
3163
|
-
}
|
|
3164
|
-
if (!["command", "transfer"].includes(type)) {
|
|
3165
|
-
return false;
|
|
3166
|
-
}
|
|
3167
|
-
return true;
|
|
3168
|
-
};
|
|
3169
|
-
const handleWSUpgrade = (wss, ctx, callback) => {
|
|
3170
|
-
assertValidHeader(ctx);
|
|
3171
|
-
wss.handleUpgrade(ctx.req, ctx.request.socket, Buffer.alloc(0), (client, request) => {
|
|
3172
|
-
if (!client) {
|
|
3173
|
-
ctx.request.socket.destroy();
|
|
3174
|
-
return;
|
|
3175
|
-
}
|
|
3176
|
-
disableTimeouts();
|
|
3177
|
-
wss.emit("connection", client, ctx.req);
|
|
3178
|
-
callback(client, request);
|
|
3179
|
-
});
|
|
3180
|
-
ctx.respond = false;
|
|
3181
|
-
};
|
|
3182
|
-
const handlerControllerFactory = (implementation) => (options) => {
|
|
3183
|
-
const { verify, server: serverOptions } = options ?? {};
|
|
3184
|
-
const wss = new WebSocket.Server({ ...serverOptions, noServer: true });
|
|
3185
|
-
return async (ctx) => {
|
|
3186
|
-
const cb = (ws) => {
|
|
3187
|
-
const state = { id: void 0 };
|
|
3188
|
-
const messageUUIDs = /* @__PURE__ */ new Set();
|
|
3189
|
-
const cannotRespondHandler = (err) => {
|
|
3190
|
-
strapi?.log?.error(
|
|
3191
|
-
"[Data transfer] Cannot send error response to client, closing connection"
|
|
3192
|
-
);
|
|
3193
|
-
strapi?.log?.error(err);
|
|
3194
|
-
try {
|
|
3195
|
-
ws.terminate();
|
|
3196
|
-
ctx.req.socket.destroy();
|
|
3197
|
-
} catch (err2) {
|
|
3198
|
-
strapi?.log?.error("[Data transfer] Failed to close socket on error");
|
|
3199
|
-
}
|
|
3200
|
-
};
|
|
3201
|
-
const prototype = {
|
|
3202
|
-
// Transfer ID
|
|
3203
|
-
get transferID() {
|
|
3204
|
-
return state.id;
|
|
3205
|
-
},
|
|
3206
|
-
set transferID(id) {
|
|
3207
|
-
state.id = id;
|
|
3208
|
-
},
|
|
3209
|
-
// Started at
|
|
3210
|
-
get startedAt() {
|
|
3211
|
-
return state.startedAt;
|
|
3212
|
-
},
|
|
3213
|
-
set startedAt(timestamp) {
|
|
3214
|
-
state.startedAt = timestamp;
|
|
3215
|
-
},
|
|
3216
|
-
get response() {
|
|
3217
|
-
return state.response;
|
|
3218
|
-
},
|
|
3219
|
-
set response(response) {
|
|
3220
|
-
state.response = response;
|
|
3221
|
-
},
|
|
3222
|
-
addUUID(uuid) {
|
|
3223
|
-
messageUUIDs.add(uuid);
|
|
3224
|
-
},
|
|
3225
|
-
hasUUID(uuid) {
|
|
3226
|
-
return messageUUIDs.has(uuid);
|
|
3227
|
-
},
|
|
3228
|
-
isTransferStarted() {
|
|
3229
|
-
return this.transferID !== void 0 && this.startedAt !== void 0;
|
|
3230
|
-
},
|
|
3231
|
-
assertValidTransfer() {
|
|
3232
|
-
const isStarted = this.isTransferStarted();
|
|
3233
|
-
if (!isStarted) {
|
|
3234
|
-
throw new Error("Invalid Transfer Process");
|
|
3235
|
-
}
|
|
3236
|
-
},
|
|
3237
|
-
assertValidTransferCommand(command) {
|
|
3238
|
-
const isDefined = typeof this[command] === "function";
|
|
3239
|
-
const isValidTransferCommand = VALID_TRANSFER_COMMANDS.includes(command);
|
|
3240
|
-
if (!isDefined || !isValidTransferCommand) {
|
|
3241
|
-
throw new Error("Invalid transfer command");
|
|
3242
|
-
}
|
|
3243
|
-
},
|
|
3244
|
-
async respond(uuid, e, data) {
|
|
3245
|
-
let details = {};
|
|
3246
|
-
return new Promise((resolve, reject2) => {
|
|
3247
|
-
if (!uuid && !e) {
|
|
3248
|
-
reject2(new Error("Missing uuid for this message"));
|
|
3249
|
-
return;
|
|
3250
|
-
}
|
|
3251
|
-
this.response = {
|
|
3252
|
-
uuid,
|
|
3253
|
-
data,
|
|
3254
|
-
e
|
|
3255
|
-
};
|
|
3256
|
-
if (e instanceof ProviderError) {
|
|
3257
|
-
details = e.details;
|
|
3258
|
-
}
|
|
3259
|
-
const payload = JSON.stringify({
|
|
3260
|
-
uuid,
|
|
3261
|
-
data: data ?? null,
|
|
3262
|
-
error: e ? {
|
|
3263
|
-
code: e?.name ?? "ERR",
|
|
3264
|
-
message: e?.message,
|
|
3265
|
-
details
|
|
3266
|
-
} : null
|
|
3267
|
-
});
|
|
3268
|
-
this.send(payload, (error) => error ? reject2(error) : resolve());
|
|
3269
|
-
});
|
|
3270
|
-
},
|
|
3271
|
-
send(message, cb2) {
|
|
3272
|
-
ws.send(message, cb2);
|
|
3273
|
-
},
|
|
3274
|
-
confirm(message) {
|
|
3275
|
-
return new Promise((resolve, reject2) => {
|
|
3276
|
-
const uuid = randomUUID();
|
|
3277
|
-
const payload = JSON.stringify({ uuid, data: message });
|
|
3278
|
-
this.send(payload, (error) => {
|
|
3279
|
-
if (error) {
|
|
3280
|
-
reject2(error);
|
|
3281
|
-
}
|
|
3282
|
-
});
|
|
3283
|
-
const onResponse = (raw) => {
|
|
3284
|
-
const response = JSON.parse(raw.toString());
|
|
3285
|
-
if (response.uuid === uuid) {
|
|
3286
|
-
resolve(response.data ?? null);
|
|
3287
|
-
} else {
|
|
3288
|
-
ws.once("message", onResponse);
|
|
3289
|
-
}
|
|
3290
|
-
};
|
|
3291
|
-
ws.once("message", onResponse);
|
|
3292
|
-
});
|
|
3293
|
-
},
|
|
3294
|
-
async executeAndRespond(uuid, fn) {
|
|
3295
|
-
try {
|
|
3296
|
-
const response = await fn();
|
|
3297
|
-
await this.respond(uuid, null, response);
|
|
3298
|
-
} catch (e) {
|
|
3299
|
-
if (e instanceof Error) {
|
|
3300
|
-
await this.respond(uuid, e).catch(cannotRespondHandler);
|
|
3301
|
-
} else if (typeof e === "string") {
|
|
3302
|
-
await this.respond(uuid, new ProviderTransferError(e)).catch(cannotRespondHandler);
|
|
3303
|
-
} else {
|
|
3304
|
-
await this.respond(
|
|
3305
|
-
uuid,
|
|
3306
|
-
new ProviderTransferError("Unexpected error", {
|
|
3307
|
-
error: e
|
|
3308
|
-
})
|
|
3309
|
-
).catch(cannotRespondHandler);
|
|
3310
|
-
}
|
|
3311
|
-
}
|
|
3312
|
-
},
|
|
3313
|
-
cleanup() {
|
|
3314
|
-
this.transferID = void 0;
|
|
3315
|
-
this.startedAt = void 0;
|
|
3316
|
-
this.response = void 0;
|
|
3317
|
-
},
|
|
3318
|
-
teardown() {
|
|
3319
|
-
this.cleanup();
|
|
3320
|
-
},
|
|
3321
|
-
verifyAuth(scope) {
|
|
3322
|
-
return verify(ctx, scope);
|
|
3323
|
-
},
|
|
3324
|
-
// Transfer commands
|
|
3325
|
-
init() {
|
|
3326
|
-
},
|
|
3327
|
-
end() {
|
|
3328
|
-
},
|
|
3329
|
-
status() {
|
|
3330
|
-
},
|
|
3331
|
-
// Default prototype implementation for events
|
|
3332
|
-
onMessage() {
|
|
3333
|
-
},
|
|
3334
|
-
onError() {
|
|
3335
|
-
},
|
|
3336
|
-
onClose() {
|
|
3337
|
-
}
|
|
3338
|
-
};
|
|
3339
|
-
const handler = Object.assign(Object.create(prototype), implementation(prototype));
|
|
3340
|
-
ws.on("close", async (...args) => {
|
|
3341
|
-
try {
|
|
3342
|
-
await handler.onClose(...args);
|
|
3343
|
-
} catch (err) {
|
|
3344
|
-
strapi?.log?.error("[Data transfer] Uncaught error closing connection");
|
|
3345
|
-
strapi?.log?.error(err);
|
|
3346
|
-
cannotRespondHandler(err);
|
|
3347
|
-
} finally {
|
|
3348
|
-
resetTimeouts();
|
|
3349
|
-
}
|
|
3350
|
-
});
|
|
3351
|
-
ws.on("error", async (...args) => {
|
|
3352
|
-
try {
|
|
3353
|
-
await handler.onError(...args);
|
|
3354
|
-
} catch (err) {
|
|
3355
|
-
strapi?.log?.error("[Data transfer] Uncaught error in error handling");
|
|
3356
|
-
strapi?.log?.error(err);
|
|
3357
|
-
cannotRespondHandler(err);
|
|
3358
|
-
}
|
|
3359
|
-
});
|
|
3360
|
-
ws.on("message", async (...args) => {
|
|
3361
|
-
try {
|
|
3362
|
-
await handler.onMessage(...args);
|
|
3363
|
-
} catch (err) {
|
|
3364
|
-
strapi?.log?.error("[Data transfer] Uncaught error in message handling");
|
|
3365
|
-
strapi?.log?.error(err);
|
|
3366
|
-
cannotRespondHandler(err);
|
|
3367
|
-
}
|
|
3368
|
-
});
|
|
3369
|
-
};
|
|
3370
|
-
try {
|
|
3371
|
-
handleWSUpgrade(wss, ctx, cb);
|
|
3372
|
-
} catch (err) {
|
|
3373
|
-
strapi?.log?.error("[Data transfer] Error in websocket upgrade request");
|
|
3374
|
-
strapi?.log?.error(err);
|
|
3375
|
-
}
|
|
3376
|
-
};
|
|
3377
|
-
};
|
|
3378
|
-
const VALID_TRANSFER_ACTIONS$1 = [
|
|
3379
|
-
"bootstrap",
|
|
3380
|
-
"close",
|
|
3381
|
-
"rollback",
|
|
3382
|
-
"beforeTransfer",
|
|
3383
|
-
"getMetadata",
|
|
3384
|
-
"getSchemas"
|
|
3385
|
-
];
|
|
3386
|
-
const TRANSFER_KIND$1 = "push";
|
|
3387
|
-
const writeAsync = (stream2, data) => {
|
|
3388
|
-
return new Promise((resolve, reject2) => {
|
|
3389
|
-
stream2.write(data, (error) => {
|
|
3390
|
-
if (error) {
|
|
3391
|
-
reject2(error);
|
|
3392
|
-
}
|
|
3393
|
-
resolve();
|
|
3394
|
-
});
|
|
3395
|
-
});
|
|
3396
|
-
};
|
|
3397
|
-
const createPushController = handlerControllerFactory((proto) => ({
|
|
3398
|
-
isTransferStarted() {
|
|
3399
|
-
return proto.isTransferStarted.call(this) && this.provider !== void 0;
|
|
3400
|
-
},
|
|
3401
|
-
verifyAuth() {
|
|
3402
|
-
return proto.verifyAuth.call(this, TRANSFER_KIND$1);
|
|
3403
|
-
},
|
|
3404
|
-
cleanup() {
|
|
3405
|
-
proto.cleanup.call(this);
|
|
3406
|
-
this.streams = {};
|
|
3407
|
-
this.assets = {};
|
|
3408
|
-
delete this.flow;
|
|
3409
|
-
delete this.provider;
|
|
3410
|
-
},
|
|
3411
|
-
teardown() {
|
|
3412
|
-
if (this.provider) {
|
|
3413
|
-
this.provider.rollback();
|
|
3414
|
-
}
|
|
3415
|
-
proto.teardown.call(this);
|
|
3416
|
-
},
|
|
3417
|
-
assertValidTransfer() {
|
|
3418
|
-
proto.assertValidTransfer.call(this);
|
|
3419
|
-
if (this.provider === void 0) {
|
|
3420
|
-
throw new Error("Invalid Transfer Process");
|
|
3421
|
-
}
|
|
3422
|
-
},
|
|
3423
|
-
assertValidTransferAction(action) {
|
|
3424
|
-
if (VALID_TRANSFER_ACTIONS$1.includes(action)) {
|
|
3425
|
-
return;
|
|
3426
|
-
}
|
|
3427
|
-
throw new ProviderTransferError(`Invalid action provided: "${action}"`, {
|
|
3428
|
-
action,
|
|
3429
|
-
validActions: Object.keys(VALID_TRANSFER_ACTIONS$1)
|
|
3430
|
-
});
|
|
3431
|
-
},
|
|
3432
|
-
assertValidStreamTransferStep(stage) {
|
|
3433
|
-
const currentStep = this.flow?.get();
|
|
3434
|
-
const nextStep = { kind: "transfer", stage };
|
|
3435
|
-
if (currentStep?.kind === "transfer" && !currentStep.locked) {
|
|
3436
|
-
throw new ProviderTransferError(
|
|
3437
|
-
`You need to initialize the transfer stage (${nextStep}) before starting to stream data`
|
|
3438
|
-
);
|
|
3439
|
-
}
|
|
3440
|
-
if (this.flow?.cannot(nextStep)) {
|
|
3441
|
-
throw new ProviderTransferError(`Invalid stage (${nextStep}) provided for the current flow`, {
|
|
3442
|
-
step: nextStep
|
|
3443
|
-
});
|
|
3444
|
-
}
|
|
3445
|
-
},
|
|
3446
|
-
async createWritableStreamForStep(step) {
|
|
3447
|
-
const mapper = {
|
|
3448
|
-
entities: () => this.provider?.createEntitiesWriteStream(),
|
|
3449
|
-
links: () => this.provider?.createLinksWriteStream(),
|
|
3450
|
-
configuration: () => this.provider?.createConfigurationWriteStream(),
|
|
3451
|
-
assets: () => this.provider?.createAssetsWriteStream()
|
|
3452
|
-
};
|
|
3453
|
-
if (!(step in mapper)) {
|
|
3454
|
-
throw new Error("Invalid transfer step, impossible to create a stream");
|
|
3455
|
-
}
|
|
3456
|
-
if (!this.streams) {
|
|
3457
|
-
throw new Error("Invalid transfer state");
|
|
3458
|
-
}
|
|
3459
|
-
this.streams[step] = await mapper[step]();
|
|
3460
|
-
},
|
|
3461
|
-
async onMessage(raw) {
|
|
3462
|
-
const msg = JSON.parse(raw.toString());
|
|
3463
|
-
if (!isDataTransferMessage(msg)) {
|
|
3464
|
-
return;
|
|
3465
|
-
}
|
|
3466
|
-
if (!msg.uuid) {
|
|
3467
|
-
await this.respond(void 0, new Error("Missing uuid in message"));
|
|
3468
|
-
}
|
|
3469
|
-
if (proto.hasUUID(msg.uuid)) {
|
|
3470
|
-
const previousResponse = proto.response;
|
|
3471
|
-
if (previousResponse?.uuid === msg.uuid) {
|
|
3472
|
-
await this.respond(previousResponse?.uuid, previousResponse.e, previousResponse.data);
|
|
3473
|
-
}
|
|
3474
|
-
return;
|
|
3475
|
-
}
|
|
3476
|
-
const { uuid, type } = msg;
|
|
3477
|
-
proto.addUUID(uuid);
|
|
3478
|
-
if (type === "command") {
|
|
3479
|
-
const { command } = msg;
|
|
3480
|
-
await this.executeAndRespond(uuid, () => {
|
|
3481
|
-
this.assertValidTransferCommand(command);
|
|
3482
|
-
if (command === "status") {
|
|
3483
|
-
return this.status();
|
|
3484
|
-
}
|
|
3485
|
-
return this[command](msg.params);
|
|
3486
|
-
});
|
|
3487
|
-
} else if (type === "transfer") {
|
|
3488
|
-
await this.executeAndRespond(uuid, async () => {
|
|
3489
|
-
await this.verifyAuth();
|
|
3490
|
-
this.assertValidTransfer();
|
|
3491
|
-
return this.onTransferMessage(msg);
|
|
3492
|
-
});
|
|
3493
|
-
} else {
|
|
3494
|
-
await this.respond(uuid, new Error("Bad Request"));
|
|
3495
|
-
}
|
|
3496
|
-
},
|
|
3497
|
-
async onTransferMessage(msg) {
|
|
3498
|
-
const { kind } = msg;
|
|
3499
|
-
if (kind === "action") {
|
|
3500
|
-
return this.onTransferAction(msg);
|
|
3501
|
-
}
|
|
3502
|
-
if (kind === "step") {
|
|
3503
|
-
return this.onTransferStep(msg);
|
|
3504
|
-
}
|
|
3505
|
-
},
|
|
3506
|
-
lockTransferStep(stage) {
|
|
3507
|
-
const currentStep = this.flow?.get();
|
|
3508
|
-
const nextStep = { kind: "transfer", stage };
|
|
3509
|
-
if (currentStep?.kind === "transfer" && currentStep.locked) {
|
|
3510
|
-
throw new ProviderTransferError(
|
|
3511
|
-
`It's not possible to start a new transfer stage (${stage}) while another one is in progress (${currentStep.stage})`
|
|
3512
|
-
);
|
|
3513
|
-
}
|
|
3514
|
-
if (this.flow?.cannot(nextStep)) {
|
|
3515
|
-
throw new ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {
|
|
3516
|
-
step: nextStep
|
|
3517
|
-
});
|
|
3518
|
-
}
|
|
3519
|
-
this.flow?.set({ ...nextStep, locked: true });
|
|
3520
|
-
},
|
|
3521
|
-
unlockTransferStep(stage) {
|
|
3522
|
-
const currentStep = this.flow?.get();
|
|
3523
|
-
const nextStep = { kind: "transfer", stage };
|
|
3524
|
-
if (currentStep?.kind === "transfer" && !currentStep.locked) {
|
|
3525
|
-
throw new ProviderTransferError(
|
|
3526
|
-
`You need to initialize the transfer stage (${stage}) before ending it`
|
|
3527
|
-
);
|
|
3528
|
-
}
|
|
3529
|
-
if (this.flow?.cannot(nextStep)) {
|
|
3530
|
-
throw new ProviderTransferError(`Invalid stage (${stage}) provided for the current flow`, {
|
|
3531
|
-
step: nextStep
|
|
3532
|
-
});
|
|
3533
|
-
}
|
|
3534
|
-
this.flow?.set({ ...nextStep, locked: false });
|
|
3535
|
-
},
|
|
3536
|
-
async onTransferStep(msg) {
|
|
3537
|
-
const { step: stage } = msg;
|
|
3538
|
-
if (msg.action === "start") {
|
|
3539
|
-
this.lockTransferStep(stage);
|
|
3540
|
-
if (this.streams?.[stage] instanceof Writable) {
|
|
3541
|
-
throw new Error("Stream already created, something went wrong");
|
|
3542
|
-
}
|
|
3543
|
-
await this.createWritableStreamForStep(stage);
|
|
3544
|
-
return { ok: true };
|
|
3545
|
-
}
|
|
3546
|
-
if (msg.action === "stream") {
|
|
3547
|
-
this.assertValidStreamTransferStep(stage);
|
|
3548
|
-
const stream2 = this.streams?.[stage];
|
|
3549
|
-
if (!stream2) {
|
|
3550
|
-
throw new Error("You need to init first");
|
|
3551
|
-
}
|
|
3552
|
-
if (stage === "assets") {
|
|
3553
|
-
return this.streamAsset(msg.data);
|
|
3554
|
-
}
|
|
3555
|
-
await Promise.all(msg.data.map((item) => writeAsync(stream2, item)));
|
|
3556
|
-
}
|
|
3557
|
-
if (msg.action === "end") {
|
|
3558
|
-
this.unlockTransferStep(stage);
|
|
3559
|
-
const stream2 = this.streams?.[stage];
|
|
3560
|
-
if (stream2 && !stream2.closed) {
|
|
3561
|
-
await new Promise((resolve, reject2) => {
|
|
3562
|
-
stream2.on("close", resolve).on("error", reject2).end();
|
|
3563
|
-
});
|
|
3564
|
-
}
|
|
3565
|
-
delete this.streams?.[stage];
|
|
3566
|
-
return { ok: true };
|
|
3567
|
-
}
|
|
3568
|
-
},
|
|
3569
|
-
async onTransferAction(msg) {
|
|
3570
|
-
const { action } = msg;
|
|
3571
|
-
this.assertValidTransferAction(action);
|
|
3572
|
-
const step = { kind: "action", action };
|
|
3573
|
-
const isStepRegistered = this.flow?.has(step);
|
|
3574
|
-
if (isStepRegistered) {
|
|
3575
|
-
if (this.flow?.cannot(step)) {
|
|
3576
|
-
throw new ProviderTransferError(`Invalid action "${action}" found for the current flow `, {
|
|
3577
|
-
action
|
|
3578
|
-
});
|
|
3579
|
-
}
|
|
3580
|
-
this.flow?.set(step);
|
|
3581
|
-
}
|
|
3582
|
-
return this.provider?.[action]();
|
|
3583
|
-
},
|
|
3584
|
-
async streamAsset(payload) {
|
|
3585
|
-
const assetsStream = this.streams?.assets;
|
|
3586
|
-
if (payload === null) {
|
|
3587
|
-
this.streams?.assets?.end();
|
|
3588
|
-
return;
|
|
3589
|
-
}
|
|
3590
|
-
for (const item of payload) {
|
|
3591
|
-
const { action, assetID } = item;
|
|
3592
|
-
if (!assetsStream) {
|
|
3593
|
-
throw new Error("Stream not defined");
|
|
3594
|
-
}
|
|
3595
|
-
if (action === "start") {
|
|
3596
|
-
this.assets[assetID] = { ...item.data, stream: new PassThrough() };
|
|
3597
|
-
writeAsync(assetsStream, this.assets[assetID]);
|
|
3598
|
-
}
|
|
3599
|
-
if (action === "stream") {
|
|
3600
|
-
const rawBuffer = item.data;
|
|
3601
|
-
const chunk = Buffer.from(rawBuffer.data);
|
|
3602
|
-
await writeAsync(this.assets[assetID].stream, chunk);
|
|
3603
|
-
}
|
|
3604
|
-
if (action === "end") {
|
|
3605
|
-
await new Promise((resolve, reject2) => {
|
|
3606
|
-
const { stream: assetStream } = this.assets[assetID];
|
|
3607
|
-
assetStream.on("close", () => {
|
|
3608
|
-
delete this.assets[assetID];
|
|
3609
|
-
resolve();
|
|
3610
|
-
}).on("error", reject2).end();
|
|
3611
|
-
});
|
|
3612
|
-
}
|
|
3613
|
-
}
|
|
3614
|
-
},
|
|
3615
|
-
onClose() {
|
|
3616
|
-
this.teardown();
|
|
3617
|
-
},
|
|
3618
|
-
onError(err) {
|
|
3619
|
-
this.teardown();
|
|
3620
|
-
strapi.log.error(err);
|
|
3621
|
-
},
|
|
3622
|
-
// Commands
|
|
3623
|
-
async init(params) {
|
|
3624
|
-
if (this.transferID || this.provider) {
|
|
3625
|
-
throw new Error("Transfer already in progress");
|
|
3626
|
-
}
|
|
3627
|
-
await this.verifyAuth();
|
|
3628
|
-
this.transferID = randomUUID();
|
|
3629
|
-
this.startedAt = Date.now();
|
|
3630
|
-
this.assets = {};
|
|
3631
|
-
this.streams = {};
|
|
3632
|
-
this.flow = createFlow(DEFAULT_TRANSFER_FLOW);
|
|
3633
|
-
this.provider = createLocalStrapiDestinationProvider({
|
|
3634
|
-
...params.options,
|
|
3635
|
-
autoDestroy: false,
|
|
3636
|
-
getStrapi: () => strapi
|
|
3637
|
-
});
|
|
3638
|
-
this.provider.onWarning = (message) => {
|
|
3639
|
-
strapi.log.warn(message);
|
|
3640
|
-
};
|
|
3641
|
-
return { transferID: this.transferID };
|
|
3642
|
-
},
|
|
3643
|
-
async status() {
|
|
3644
|
-
const isStarted = this.isTransferStarted();
|
|
3645
|
-
if (isStarted) {
|
|
3646
|
-
const startedAt = this.startedAt;
|
|
3647
|
-
return {
|
|
3648
|
-
active: true,
|
|
3649
|
-
kind: TRANSFER_KIND$1,
|
|
3650
|
-
startedAt,
|
|
3651
|
-
elapsed: Date.now() - startedAt
|
|
3652
|
-
};
|
|
3653
|
-
}
|
|
3654
|
-
return { active: false, kind: null, elapsed: null, startedAt: null };
|
|
3655
|
-
},
|
|
3656
|
-
async end(params) {
|
|
3657
|
-
await this.verifyAuth();
|
|
3658
|
-
if (this.transferID !== params?.transferID) {
|
|
3659
|
-
throw new ProviderTransferError("Bad transfer ID provided");
|
|
3660
|
-
}
|
|
3661
|
-
this.cleanup();
|
|
3662
|
-
return { ok: true };
|
|
3663
|
-
}
|
|
3664
|
-
}));
|
|
3665
|
-
const TRANSFER_KIND = "pull";
|
|
3666
|
-
const VALID_TRANSFER_ACTIONS = ["bootstrap", "close", "getMetadata", "getSchemas"];
|
|
3667
|
-
const createPullController = handlerControllerFactory((proto) => ({
|
|
3668
|
-
isTransferStarted() {
|
|
3669
|
-
return proto.isTransferStarted.call(this) && this.provider !== void 0;
|
|
3670
|
-
},
|
|
3671
|
-
verifyAuth() {
|
|
3672
|
-
return proto.verifyAuth.call(this, TRANSFER_KIND);
|
|
3673
|
-
},
|
|
3674
|
-
cleanup() {
|
|
3675
|
-
proto.cleanup.call(this);
|
|
3676
|
-
this.streams = {};
|
|
3677
|
-
delete this.provider;
|
|
3678
|
-
},
|
|
3679
|
-
assertValidTransferAction(action) {
|
|
3680
|
-
const validActions = VALID_TRANSFER_ACTIONS;
|
|
3681
|
-
if (validActions.includes(action)) {
|
|
3682
|
-
return;
|
|
3683
|
-
}
|
|
3684
|
-
throw new ProviderTransferError(`Invalid action provided: "${action}"`, {
|
|
3685
|
-
action,
|
|
3686
|
-
validActions: Object.keys(VALID_TRANSFER_ACTIONS)
|
|
3687
|
-
});
|
|
3688
|
-
},
|
|
3689
|
-
async onMessage(raw) {
|
|
3690
|
-
const msg = JSON.parse(raw.toString());
|
|
3691
|
-
if (!isDataTransferMessage(msg)) {
|
|
3692
|
-
return;
|
|
3693
|
-
}
|
|
3694
|
-
if (!msg.uuid) {
|
|
3695
|
-
await this.respond(void 0, new Error("Missing uuid in message"));
|
|
3696
|
-
}
|
|
3697
|
-
if (proto.hasUUID(msg.uuid)) {
|
|
3698
|
-
const previousResponse = proto.response;
|
|
3699
|
-
if (previousResponse?.uuid === msg.uuid) {
|
|
3700
|
-
await this.respond(previousResponse?.uuid, previousResponse.e, previousResponse.data);
|
|
3701
|
-
}
|
|
3702
|
-
return;
|
|
3703
|
-
}
|
|
3704
|
-
const { uuid, type } = msg;
|
|
3705
|
-
proto.addUUID(uuid);
|
|
3706
|
-
if (type === "command") {
|
|
3707
|
-
const { command } = msg;
|
|
3708
|
-
await this.executeAndRespond(uuid, () => {
|
|
3709
|
-
this.assertValidTransferCommand(command);
|
|
3710
|
-
if (command === "status") {
|
|
3711
|
-
return this.status();
|
|
3712
|
-
}
|
|
3713
|
-
return this[command](msg.params);
|
|
3714
|
-
});
|
|
3715
|
-
} else if (type === "transfer") {
|
|
3716
|
-
await this.executeAndRespond(uuid, async () => {
|
|
3717
|
-
await this.verifyAuth();
|
|
3718
|
-
this.assertValidTransfer();
|
|
3719
|
-
return this.onTransferMessage(msg);
|
|
3720
|
-
});
|
|
3721
|
-
} else {
|
|
3722
|
-
await this.respond(uuid, new Error("Bad Request"));
|
|
3723
|
-
}
|
|
3724
|
-
},
|
|
3725
|
-
async onTransferMessage(msg) {
|
|
3726
|
-
const { kind } = msg;
|
|
3727
|
-
if (kind === "action") {
|
|
3728
|
-
return this.onTransferAction(msg);
|
|
3729
|
-
}
|
|
3730
|
-
if (kind === "step") {
|
|
3731
|
-
return this.onTransferStep(msg);
|
|
3732
|
-
}
|
|
3733
|
-
},
|
|
3734
|
-
async onTransferAction(msg) {
|
|
3735
|
-
const { action } = msg;
|
|
3736
|
-
this.assertValidTransferAction(action);
|
|
3737
|
-
return this.provider?.[action]();
|
|
3738
|
-
},
|
|
3739
|
-
async flush(stage, id) {
|
|
3740
|
-
const batchSize = 1024 * 1024;
|
|
3741
|
-
let batch = [];
|
|
3742
|
-
const stream2 = this.streams?.[stage];
|
|
3743
|
-
const batchLength = () => Buffer.byteLength(JSON.stringify(batch));
|
|
3744
|
-
const sendBatch = async () => {
|
|
3745
|
-
await this.confirm({
|
|
3746
|
-
type: "transfer",
|
|
3747
|
-
data: batch,
|
|
3748
|
-
ended: false,
|
|
3749
|
-
error: null,
|
|
3750
|
-
id
|
|
3751
|
-
});
|
|
3752
|
-
};
|
|
3753
|
-
if (!stream2) {
|
|
3754
|
-
throw new ProviderTransferError(`No available stream found for ${stage}`);
|
|
3755
|
-
}
|
|
3756
|
-
try {
|
|
3757
|
-
for await (const chunk of stream2) {
|
|
3758
|
-
if (stage !== "assets") {
|
|
3759
|
-
batch.push(chunk);
|
|
3760
|
-
if (batchLength() >= batchSize) {
|
|
3761
|
-
await sendBatch();
|
|
3762
|
-
batch = [];
|
|
3763
|
-
}
|
|
3764
|
-
} else {
|
|
3765
|
-
await this.confirm({
|
|
3766
|
-
type: "transfer",
|
|
3767
|
-
data: [chunk],
|
|
3768
|
-
ended: false,
|
|
3769
|
-
error: null,
|
|
3770
|
-
id
|
|
3771
|
-
});
|
|
3772
|
-
}
|
|
3773
|
-
}
|
|
3774
|
-
if (batch.length > 0 && stage !== "assets") {
|
|
3775
|
-
await sendBatch();
|
|
3776
|
-
batch = [];
|
|
3777
|
-
}
|
|
3778
|
-
await this.confirm({ type: "transfer", data: null, ended: true, error: null, id });
|
|
3779
|
-
} catch (e) {
|
|
3780
|
-
await this.confirm({ type: "transfer", data: null, ended: true, error: e, id });
|
|
3781
|
-
}
|
|
3782
|
-
},
|
|
3783
|
-
async onTransferStep(msg) {
|
|
3784
|
-
const { step, action } = msg;
|
|
3785
|
-
if (action === "start") {
|
|
3786
|
-
if (this.streams?.[step] instanceof Readable) {
|
|
3787
|
-
throw new Error("Stream already created, something went wrong");
|
|
3788
|
-
}
|
|
3789
|
-
const flushUUID = randomUUID();
|
|
3790
|
-
await this.createReadableStreamForStep(step);
|
|
3791
|
-
this.flush(step, flushUUID);
|
|
3792
|
-
return { ok: true, id: flushUUID };
|
|
3793
|
-
}
|
|
3794
|
-
if (action === "end") {
|
|
3795
|
-
const stream2 = this.streams?.[step];
|
|
3796
|
-
if (stream2?.readableEnded === false) {
|
|
3797
|
-
await new Promise((resolve) => {
|
|
3798
|
-
stream2?.on("close", resolve).destroy();
|
|
3799
|
-
});
|
|
3800
|
-
}
|
|
3801
|
-
delete this.streams?.[step];
|
|
3802
|
-
return { ok: true };
|
|
3803
|
-
}
|
|
3804
|
-
},
|
|
3805
|
-
async createReadableStreamForStep(step) {
|
|
3806
|
-
const mapper = {
|
|
3807
|
-
entities: () => this.provider?.createEntitiesReadStream(),
|
|
3808
|
-
links: () => this.provider?.createLinksReadStream(),
|
|
3809
|
-
configuration: () => this.provider?.createConfigurationReadStream(),
|
|
3810
|
-
assets: () => {
|
|
3811
|
-
const assets = this.provider?.createAssetsReadStream();
|
|
3812
|
-
let batch = [];
|
|
3813
|
-
const batchLength = () => {
|
|
3814
|
-
return batch.reduce(
|
|
3815
|
-
(acc, chunk) => chunk.action === "stream" ? acc + chunk.data.byteLength : acc,
|
|
3816
|
-
0
|
|
3817
|
-
);
|
|
3818
|
-
};
|
|
3819
|
-
const BATCH_MAX_SIZE = 1024 * 1024;
|
|
3820
|
-
if (!assets) {
|
|
3821
|
-
throw new Error("bad");
|
|
3822
|
-
}
|
|
3823
|
-
async function* generator(stream2) {
|
|
3824
|
-
let hasStarted = false;
|
|
3825
|
-
let assetID = "";
|
|
3826
|
-
for await (const chunk of stream2) {
|
|
3827
|
-
const { stream: assetStream, ...assetData } = chunk;
|
|
3828
|
-
if (!hasStarted) {
|
|
3829
|
-
assetID = randomUUID();
|
|
3830
|
-
batch.push({ action: "start", assetID, data: assetData });
|
|
3831
|
-
hasStarted = true;
|
|
3832
|
-
}
|
|
3833
|
-
for await (const assetChunk of assetStream) {
|
|
3834
|
-
batch.push({ action: "stream", assetID, data: assetChunk });
|
|
3835
|
-
if (batchLength() >= BATCH_MAX_SIZE) {
|
|
3836
|
-
yield batch;
|
|
3837
|
-
batch = [];
|
|
3838
|
-
}
|
|
3839
|
-
}
|
|
3840
|
-
hasStarted = false;
|
|
3841
|
-
batch.push({ action: "end", assetID });
|
|
3842
|
-
yield batch;
|
|
3843
|
-
batch = [];
|
|
3844
|
-
}
|
|
3845
|
-
}
|
|
3846
|
-
return Readable.from(generator(assets));
|
|
3847
|
-
}
|
|
3848
|
-
};
|
|
3849
|
-
if (!(step in mapper)) {
|
|
3850
|
-
throw new Error("Invalid transfer step, impossible to create a stream");
|
|
3851
|
-
}
|
|
3852
|
-
if (!this.streams) {
|
|
3853
|
-
throw new Error("Invalid transfer state");
|
|
3854
|
-
}
|
|
3855
|
-
this.streams[step] = await mapper[step]();
|
|
3856
|
-
},
|
|
3857
|
-
// Commands
|
|
3858
|
-
async init() {
|
|
3859
|
-
if (this.transferID || this.provider) {
|
|
3860
|
-
throw new Error("Transfer already in progress");
|
|
3861
|
-
}
|
|
3862
|
-
await this.verifyAuth();
|
|
3863
|
-
this.transferID = randomUUID();
|
|
3864
|
-
this.startedAt = Date.now();
|
|
3865
|
-
this.streams = {};
|
|
3866
|
-
this.provider = createLocalStrapiSourceProvider({
|
|
3867
|
-
autoDestroy: false,
|
|
3868
|
-
getStrapi: () => strapi
|
|
3869
|
-
});
|
|
3870
|
-
return { transferID: this.transferID };
|
|
3871
|
-
},
|
|
3872
|
-
async end(params) {
|
|
3873
|
-
await this.verifyAuth();
|
|
3874
|
-
if (this.transferID !== params?.transferID) {
|
|
3875
|
-
throw new ProviderTransferError("Bad transfer ID provided");
|
|
3876
|
-
}
|
|
3877
|
-
this.cleanup();
|
|
3878
|
-
return { ok: true };
|
|
3879
|
-
},
|
|
3880
|
-
async status() {
|
|
3881
|
-
const isStarted = this.isTransferStarted();
|
|
3882
|
-
if (!isStarted) {
|
|
3883
|
-
const startedAt = this.startedAt;
|
|
3884
|
-
return {
|
|
3885
|
-
active: true,
|
|
3886
|
-
kind: TRANSFER_KIND,
|
|
3887
|
-
startedAt,
|
|
3888
|
-
elapsed: Date.now() - startedAt
|
|
3889
|
-
};
|
|
3890
|
-
}
|
|
3891
|
-
return { active: false, kind: null, elapsed: null, startedAt: null };
|
|
3892
|
-
}
|
|
3893
|
-
}));
|
|
3894
|
-
const index$4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
3895
|
-
__proto__: null,
|
|
3896
|
-
createPullController,
|
|
3897
|
-
createPushController,
|
|
3898
|
-
handlerControllerFactory
|
|
3899
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
3900
|
-
const index$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
3901
|
-
__proto__: null,
|
|
3902
|
-
constants,
|
|
3903
|
-
handlers: index$4
|
|
3904
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
3905
|
-
const index$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
3906
|
-
__proto__: null,
|
|
3907
|
-
providers: index$5,
|
|
3908
|
-
queries: index$6,
|
|
3909
|
-
remote: index$3
|
|
3910
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
3911
|
-
const isFilePathInDirname = (posixDirName, filePath) => {
|
|
3912
|
-
const normalizedDir = path.posix.dirname(unknownPathToPosix(filePath));
|
|
3913
|
-
return isPathEquivalent(posixDirName, normalizedDir);
|
|
3914
|
-
};
|
|
3915
|
-
const isPathEquivalent = (pathA, pathB) => {
|
|
3916
|
-
const normalizedPathA = path.posix.normalize(unknownPathToPosix(pathA));
|
|
3917
|
-
const normalizedPathB = path.posix.normalize(unknownPathToPosix(pathB));
|
|
3918
|
-
return !path.posix.relative(normalizedPathB, normalizedPathA).length;
|
|
3919
|
-
};
|
|
3920
|
-
const unknownPathToPosix = (filePath) => {
|
|
3921
|
-
if (filePath.includes(path.posix.sep)) {
|
|
3922
|
-
return filePath;
|
|
3923
|
-
}
|
|
3924
|
-
return path.normalize(filePath).split(path.win32.sep).join(path.posix.sep);
|
|
3925
|
-
};
|
|
3926
|
-
const METADATA_FILE_PATH = "metadata.json";
|
|
3927
|
-
const createLocalFileSourceProvider = (options) => {
|
|
3928
|
-
return new LocalFileSourceProvider(options);
|
|
3929
|
-
};
|
|
3930
|
-
class LocalFileSourceProvider {
|
|
3931
|
-
type = "source";
|
|
3932
|
-
name = "source::local-file";
|
|
3933
|
-
options;
|
|
3934
|
-
#metadata;
|
|
3935
|
-
constructor(options) {
|
|
3936
|
-
this.options = options;
|
|
3937
|
-
const { encryption } = this.options;
|
|
3938
|
-
if (encryption.enabled && encryption.key === void 0) {
|
|
3939
|
-
throw new Error("Missing encryption key");
|
|
3940
|
-
}
|
|
3941
|
-
}
|
|
3942
|
-
/**
|
|
3943
|
-
* Pre flight checks regarding the provided options, making sure that the file can be opened (decrypted, decompressed), etc.
|
|
3944
|
-
*/
|
|
3945
|
-
async bootstrap() {
|
|
3946
|
-
const { path: filePath } = this.options.file;
|
|
3947
|
-
try {
|
|
3948
|
-
await this.#loadMetadata();
|
|
3949
|
-
} catch (e) {
|
|
3950
|
-
if (this.options?.encryption?.enabled) {
|
|
3951
|
-
throw new ProviderInitializationError(
|
|
3952
|
-
`Key is incorrect or the file '${filePath}' is not a valid Strapi data file.`
|
|
3953
|
-
);
|
|
3954
|
-
}
|
|
3955
|
-
throw new ProviderInitializationError(`File '${filePath}' is not a valid Strapi data file.`);
|
|
3956
|
-
}
|
|
3957
|
-
if (!this.#metadata) {
|
|
3958
|
-
throw new ProviderInitializationError("Could not load metadata from Strapi data file.");
|
|
3959
|
-
}
|
|
3960
|
-
}
|
|
3961
|
-
async #loadMetadata() {
|
|
3962
|
-
const backupStream = this.#getBackupStream();
|
|
3963
|
-
this.#metadata = await this.#parseJSONFile(backupStream, METADATA_FILE_PATH);
|
|
3964
|
-
}
|
|
3965
|
-
async #loadAssetMetadata(path2) {
|
|
3966
|
-
const backupStream = this.#getBackupStream();
|
|
3967
|
-
return this.#parseJSONFile(backupStream, path2);
|
|
3968
|
-
}
|
|
3969
|
-
async getMetadata() {
|
|
3970
|
-
if (!this.#metadata) {
|
|
3971
|
-
await this.#loadMetadata();
|
|
3972
|
-
}
|
|
3973
|
-
return this.#metadata ?? null;
|
|
3974
|
-
}
|
|
3975
|
-
async getSchemas() {
|
|
3976
|
-
const schemaCollection = await collect(
|
|
3977
|
-
this.createSchemasReadStream()
|
|
3978
|
-
);
|
|
3979
|
-
if (isEmpty(schemaCollection)) {
|
|
3980
|
-
throw new ProviderInitializationError("Could not load schemas from Strapi data file.");
|
|
3981
|
-
}
|
|
3982
|
-
const schemas = keyBy("uid", schemaCollection);
|
|
3983
|
-
return schemasToValidJSON(schemas);
|
|
3984
|
-
}
|
|
3985
|
-
createEntitiesReadStream() {
|
|
3986
|
-
return this.#streamJsonlDirectory("entities");
|
|
3987
|
-
}
|
|
3988
|
-
createSchemasReadStream() {
|
|
3989
|
-
return this.#streamJsonlDirectory("schemas");
|
|
3990
|
-
}
|
|
3991
|
-
createLinksReadStream() {
|
|
3992
|
-
return this.#streamJsonlDirectory("links");
|
|
3993
|
-
}
|
|
3994
|
-
createConfigurationReadStream() {
|
|
3995
|
-
return this.#streamJsonlDirectory("configuration");
|
|
3996
|
-
}
|
|
3997
|
-
createAssetsReadStream() {
|
|
3998
|
-
const inStream = this.#getBackupStream();
|
|
3999
|
-
const outStream = new PassThrough({ objectMode: true });
|
|
4000
|
-
const loadAssetMetadata = this.#loadAssetMetadata.bind(this);
|
|
4001
|
-
pipeline(
|
|
4002
|
-
[
|
|
4003
|
-
inStream,
|
|
4004
|
-
new tar.Parse({
|
|
4005
|
-
// find only files in the assets/uploads folder
|
|
4006
|
-
filter(filePath, entry) {
|
|
4007
|
-
if (entry.type !== "File") {
|
|
4008
|
-
return false;
|
|
4009
|
-
}
|
|
4010
|
-
return isFilePathInDirname("assets/uploads", filePath);
|
|
4011
|
-
},
|
|
4012
|
-
async onentry(entry) {
|
|
4013
|
-
const { path: filePath, size: size2 = 0 } = entry;
|
|
4014
|
-
const normalizedPath = unknownPathToPosix(filePath);
|
|
4015
|
-
const file = path.basename(normalizedPath);
|
|
4016
|
-
let metadata;
|
|
4017
|
-
try {
|
|
4018
|
-
metadata = await loadAssetMetadata(`assets/metadata/${file}.json`);
|
|
4019
|
-
} catch (error) {
|
|
4020
|
-
console.warn(
|
|
4021
|
-
` Failed to read metadata for ${file}, Strapi will try to fix this issue automatically`
|
|
4022
|
-
);
|
|
4023
|
-
}
|
|
4024
|
-
const asset = {
|
|
4025
|
-
metadata,
|
|
4026
|
-
filename: file,
|
|
4027
|
-
filepath: normalizedPath,
|
|
4028
|
-
stats: { size: size2 },
|
|
4029
|
-
stream: entry
|
|
4030
|
-
};
|
|
4031
|
-
outStream.write(asset);
|
|
4032
|
-
}
|
|
4033
|
-
})
|
|
4034
|
-
],
|
|
4035
|
-
() => outStream.end()
|
|
4036
|
-
);
|
|
4037
|
-
return outStream;
|
|
4038
|
-
}
|
|
4039
|
-
#getBackupStream() {
|
|
4040
|
-
const { file, encryption, compression } = this.options;
|
|
4041
|
-
const streams = [];
|
|
4042
|
-
try {
|
|
4043
|
-
streams.push(fse__default.createReadStream(file.path));
|
|
4044
|
-
} catch (e) {
|
|
4045
|
-
throw new Error(`Could not read backup file path provided at "${this.options.file.path}"`);
|
|
4046
|
-
}
|
|
4047
|
-
if (encryption.enabled && encryption.key) {
|
|
4048
|
-
streams.push(createDecryptionCipher(encryption.key));
|
|
4049
|
-
}
|
|
4050
|
-
if (compression.enabled) {
|
|
4051
|
-
streams.push(zip$1.createGunzip());
|
|
4052
|
-
}
|
|
4053
|
-
return chain(streams);
|
|
4054
|
-
}
|
|
4055
|
-
// `directory` must be posix formatted path
|
|
4056
|
-
#streamJsonlDirectory(directory) {
|
|
4057
|
-
const inStream = this.#getBackupStream();
|
|
4058
|
-
const outStream = new PassThrough({ objectMode: true });
|
|
4059
|
-
pipeline(
|
|
4060
|
-
[
|
|
4061
|
-
inStream,
|
|
4062
|
-
new tar.Parse({
|
|
4063
|
-
filter(filePath, entry) {
|
|
4064
|
-
if (entry.type !== "File") {
|
|
4065
|
-
return false;
|
|
4066
|
-
}
|
|
4067
|
-
return isFilePathInDirname(directory, filePath);
|
|
4068
|
-
},
|
|
4069
|
-
async onentry(entry) {
|
|
4070
|
-
const transforms = [
|
|
4071
|
-
// JSONL parser to read the data chunks one by one (line by line)
|
|
4072
|
-
parser({
|
|
4073
|
-
checkErrors: true
|
|
4074
|
-
}),
|
|
4075
|
-
// The JSONL parser returns each line as key/value
|
|
4076
|
-
(line) => line.value
|
|
4077
|
-
];
|
|
4078
|
-
const stream2 = entry.pipe(chain(transforms));
|
|
4079
|
-
try {
|
|
4080
|
-
for await (const chunk of stream2) {
|
|
4081
|
-
outStream.write(chunk);
|
|
4082
|
-
}
|
|
4083
|
-
} catch (e) {
|
|
4084
|
-
outStream.destroy(
|
|
4085
|
-
new ProviderTransferError(
|
|
4086
|
-
`Error parsing backup files from backup file ${entry.path}: ${e.message}`,
|
|
4087
|
-
{
|
|
4088
|
-
details: {
|
|
4089
|
-
error: e
|
|
4090
|
-
}
|
|
4091
|
-
}
|
|
4092
|
-
)
|
|
4093
|
-
);
|
|
4094
|
-
}
|
|
4095
|
-
}
|
|
4096
|
-
})
|
|
4097
|
-
],
|
|
4098
|
-
async () => {
|
|
4099
|
-
outStream.end();
|
|
4100
|
-
}
|
|
4101
|
-
);
|
|
4102
|
-
return outStream;
|
|
4103
|
-
}
|
|
4104
|
-
// For collecting an entire JSON file then parsing it, not for streaming JSONL
|
|
4105
|
-
async #parseJSONFile(fileStream, filePath) {
|
|
4106
|
-
return new Promise((resolve, reject2) => {
|
|
4107
|
-
pipeline(
|
|
4108
|
-
[
|
|
4109
|
-
fileStream,
|
|
4110
|
-
// Custom backup archive parsing
|
|
4111
|
-
new tar.Parse({
|
|
4112
|
-
/**
|
|
4113
|
-
* Filter the parsed entries to only keep the one that matches the given filepath
|
|
4114
|
-
*/
|
|
4115
|
-
filter(entryPath, entry) {
|
|
4116
|
-
if (entry.type !== "File") {
|
|
4117
|
-
return false;
|
|
4118
|
-
}
|
|
4119
|
-
return isPathEquivalent(entryPath, filePath);
|
|
4120
|
-
},
|
|
4121
|
-
async onentry(entry) {
|
|
4122
|
-
const content = await entry.collect();
|
|
4123
|
-
try {
|
|
4124
|
-
const parsedContent = JSON.parse(Buffer.concat(content).toString());
|
|
4125
|
-
resolve(parsedContent);
|
|
4126
|
-
} catch (e) {
|
|
4127
|
-
reject2(e);
|
|
4128
|
-
} finally {
|
|
4129
|
-
entry.destroy();
|
|
4130
|
-
}
|
|
4131
|
-
}
|
|
4132
|
-
})
|
|
4133
|
-
],
|
|
4134
|
-
() => {
|
|
4135
|
-
reject2(new Error(`File "${filePath}" not found`));
|
|
4136
|
-
}
|
|
4137
|
-
);
|
|
4138
|
-
});
|
|
4139
|
-
}
|
|
4140
|
-
}
|
|
4141
|
-
const createFilePathFactory = (type) => (fileIndex = 0) => {
|
|
4142
|
-
return posix.join(
|
|
4143
|
-
// "{type}" directory
|
|
4144
|
-
type,
|
|
4145
|
-
// "${type}_XXXXX.jsonl" file
|
|
4146
|
-
`${type}_${String(fileIndex).padStart(5, "0")}.jsonl`
|
|
4147
|
-
);
|
|
4148
|
-
};
|
|
4149
|
-
const createTarEntryStream = (archive, pathFactory, maxSize = 256e6) => {
|
|
4150
|
-
let fileIndex = 0;
|
|
4151
|
-
let buffer = "";
|
|
4152
|
-
const flush = async () => {
|
|
4153
|
-
if (!buffer) {
|
|
4154
|
-
return;
|
|
4155
|
-
}
|
|
4156
|
-
fileIndex += 1;
|
|
4157
|
-
const name = pathFactory(fileIndex);
|
|
4158
|
-
const size2 = buffer.length;
|
|
4159
|
-
await new Promise((resolve, reject2) => {
|
|
4160
|
-
archive.entry({ name, size: size2 }, buffer, (err) => {
|
|
4161
|
-
if (err) {
|
|
4162
|
-
reject2(err);
|
|
4163
|
-
}
|
|
4164
|
-
resolve();
|
|
4165
|
-
});
|
|
4166
|
-
});
|
|
4167
|
-
buffer = "";
|
|
4168
|
-
};
|
|
4169
|
-
const push = (chunk) => {
|
|
4170
|
-
buffer += chunk;
|
|
4171
|
-
};
|
|
4172
|
-
return new Writable({
|
|
4173
|
-
async destroy(err, callback) {
|
|
4174
|
-
await flush();
|
|
4175
|
-
callback(err);
|
|
4176
|
-
},
|
|
4177
|
-
async write(chunk, _encoding, callback) {
|
|
4178
|
-
const size2 = chunk.length;
|
|
4179
|
-
if (chunk.length > maxSize) {
|
|
4180
|
-
callback(new Error(`payload too large: ${chunk.length}>${maxSize}`));
|
|
4181
|
-
return;
|
|
4182
|
-
}
|
|
4183
|
-
if (buffer.length + size2 > maxSize) {
|
|
4184
|
-
await flush();
|
|
4185
|
-
}
|
|
4186
|
-
push(chunk);
|
|
4187
|
-
callback(null);
|
|
4188
|
-
}
|
|
4189
|
-
});
|
|
4190
|
-
};
|
|
4191
|
-
const createLocalFileDestinationProvider = (options) => {
|
|
4192
|
-
return new LocalFileDestinationProvider(options);
|
|
4193
|
-
};
|
|
4194
|
-
class LocalFileDestinationProvider {
|
|
4195
|
-
name = "destination::local-file";
|
|
4196
|
-
type = "destination";
|
|
4197
|
-
options;
|
|
4198
|
-
results = {};
|
|
4199
|
-
#providersMetadata = {};
|
|
4200
|
-
#archive = {};
|
|
4201
|
-
constructor(options) {
|
|
4202
|
-
this.options = options;
|
|
4203
|
-
}
|
|
4204
|
-
get #archivePath() {
|
|
4205
|
-
const { encryption, compression, file } = this.options;
|
|
4206
|
-
let filePath = `${file.path}.tar`;
|
|
4207
|
-
if (compression.enabled) {
|
|
4208
|
-
filePath += ".gz";
|
|
4209
|
-
}
|
|
4210
|
-
if (encryption.enabled) {
|
|
4211
|
-
filePath += ".enc";
|
|
4212
|
-
}
|
|
4213
|
-
return filePath;
|
|
4214
|
-
}
|
|
4215
|
-
setMetadata(target, metadata) {
|
|
4216
|
-
this.#providersMetadata[target] = metadata;
|
|
4217
|
-
return this;
|
|
4218
|
-
}
|
|
4219
|
-
createGzip() {
|
|
4220
|
-
return zip$1.createGzip();
|
|
4221
|
-
}
|
|
4222
|
-
bootstrap() {
|
|
4223
|
-
const { compression, encryption } = this.options;
|
|
4224
|
-
if (encryption.enabled && !encryption.key) {
|
|
4225
|
-
throw new Error("Can't encrypt without a key");
|
|
4226
|
-
}
|
|
4227
|
-
this.#archive.stream = tar$1.pack();
|
|
4228
|
-
const outStream = createWriteStream(this.#archivePath);
|
|
4229
|
-
outStream.on("error", (err) => {
|
|
4230
|
-
if (err.code === "ENOSPC") {
|
|
4231
|
-
throw new ProviderTransferError(
|
|
4232
|
-
"Your server doesn't have space to proceed with the import."
|
|
4233
|
-
);
|
|
4234
|
-
}
|
|
4235
|
-
throw err;
|
|
4236
|
-
});
|
|
4237
|
-
const archiveTransforms = [];
|
|
4238
|
-
if (compression.enabled) {
|
|
4239
|
-
archiveTransforms.push(this.createGzip());
|
|
4240
|
-
}
|
|
4241
|
-
if (encryption.enabled && encryption.key) {
|
|
4242
|
-
archiveTransforms.push(createEncryptionCipher(encryption.key));
|
|
4243
|
-
}
|
|
4244
|
-
this.#archive.pipeline = chain([this.#archive.stream, ...archiveTransforms, outStream]);
|
|
4245
|
-
this.results.file = { path: this.#archivePath };
|
|
4246
|
-
}
|
|
4247
|
-
async close() {
|
|
4248
|
-
const { stream: stream2, pipeline: pipeline2 } = this.#archive;
|
|
4249
|
-
if (!stream2) {
|
|
4250
|
-
return;
|
|
4251
|
-
}
|
|
4252
|
-
await this.#writeMetadata();
|
|
4253
|
-
stream2.finalize();
|
|
4254
|
-
if (pipeline2 && !pipeline2.closed) {
|
|
4255
|
-
await new Promise((resolve, reject2) => {
|
|
4256
|
-
pipeline2.on("close", resolve).on("error", reject2);
|
|
4257
|
-
});
|
|
4258
|
-
}
|
|
4259
|
-
}
|
|
4260
|
-
async rollback() {
|
|
4261
|
-
await this.close();
|
|
4262
|
-
await rm(this.#archivePath, { force: true });
|
|
4263
|
-
}
|
|
4264
|
-
getMetadata() {
|
|
4265
|
-
return null;
|
|
4266
|
-
}
|
|
4267
|
-
async #writeMetadata() {
|
|
4268
|
-
const metadata = this.#providersMetadata.source;
|
|
4269
|
-
if (metadata) {
|
|
4270
|
-
await new Promise((resolve) => {
|
|
4271
|
-
const outStream = this.#getMetadataStream();
|
|
4272
|
-
const data = JSON.stringify(metadata, null, 2);
|
|
4273
|
-
Readable.from(data).pipe(outStream).on("close", resolve);
|
|
4274
|
-
});
|
|
4275
|
-
}
|
|
4276
|
-
}
|
|
4277
|
-
#getMetadataStream() {
|
|
4278
|
-
const { stream: stream2 } = this.#archive;
|
|
4279
|
-
if (!stream2) {
|
|
4280
|
-
throw new Error("Archive stream is unavailable");
|
|
4281
|
-
}
|
|
4282
|
-
return createTarEntryStream(stream2, () => "metadata.json");
|
|
4283
|
-
}
|
|
4284
|
-
createSchemasWriteStream() {
|
|
4285
|
-
if (!this.#archive.stream) {
|
|
4286
|
-
throw new Error("Archive stream is unavailable");
|
|
4287
|
-
}
|
|
4288
|
-
const filePathFactory = createFilePathFactory("schemas");
|
|
4289
|
-
const entryStream = createTarEntryStream(
|
|
4290
|
-
this.#archive.stream,
|
|
4291
|
-
filePathFactory,
|
|
4292
|
-
this.options.file.maxSizeJsonl
|
|
4293
|
-
);
|
|
4294
|
-
return chain([stringer(), entryStream]);
|
|
4295
|
-
}
|
|
4296
|
-
createEntitiesWriteStream() {
|
|
4297
|
-
if (!this.#archive.stream) {
|
|
4298
|
-
throw new Error("Archive stream is unavailable");
|
|
4299
|
-
}
|
|
4300
|
-
const filePathFactory = createFilePathFactory("entities");
|
|
4301
|
-
const entryStream = createTarEntryStream(
|
|
4302
|
-
this.#archive.stream,
|
|
4303
|
-
filePathFactory,
|
|
4304
|
-
this.options.file.maxSizeJsonl
|
|
4305
|
-
);
|
|
4306
|
-
return chain([stringer(), entryStream]);
|
|
4307
|
-
}
|
|
4308
|
-
createLinksWriteStream() {
|
|
4309
|
-
if (!this.#archive.stream) {
|
|
4310
|
-
throw new Error("Archive stream is unavailable");
|
|
4311
|
-
}
|
|
4312
|
-
const filePathFactory = createFilePathFactory("links");
|
|
4313
|
-
const entryStream = createTarEntryStream(
|
|
4314
|
-
this.#archive.stream,
|
|
4315
|
-
filePathFactory,
|
|
4316
|
-
this.options.file.maxSizeJsonl
|
|
4317
|
-
);
|
|
4318
|
-
return chain([stringer(), entryStream]);
|
|
4319
|
-
}
|
|
4320
|
-
createConfigurationWriteStream() {
|
|
4321
|
-
if (!this.#archive.stream) {
|
|
4322
|
-
throw new Error("Archive stream is unavailable");
|
|
4323
|
-
}
|
|
4324
|
-
const filePathFactory = createFilePathFactory("configuration");
|
|
4325
|
-
const entryStream = createTarEntryStream(
|
|
4326
|
-
this.#archive.stream,
|
|
4327
|
-
filePathFactory,
|
|
4328
|
-
this.options.file.maxSizeJsonl
|
|
4329
|
-
);
|
|
4330
|
-
return chain([stringer(), entryStream]);
|
|
4331
|
-
}
|
|
4332
|
-
createAssetsWriteStream() {
|
|
4333
|
-
const { stream: archiveStream } = this.#archive;
|
|
4334
|
-
if (!archiveStream) {
|
|
4335
|
-
throw new Error("Archive stream is unavailable");
|
|
4336
|
-
}
|
|
4337
|
-
return new Writable({
|
|
4338
|
-
objectMode: true,
|
|
4339
|
-
write(data, _encoding, callback) {
|
|
4340
|
-
const entryPath = path.posix.join("assets", "uploads", data.filename);
|
|
4341
|
-
const entryMetadataPath = path.posix.join("assets", "metadata", `${data.filename}.json`);
|
|
4342
|
-
const stringifiedMetadata = JSON.stringify(data.metadata);
|
|
4343
|
-
archiveStream.entry(
|
|
4344
|
-
{
|
|
4345
|
-
name: entryMetadataPath,
|
|
4346
|
-
size: stringifiedMetadata.length
|
|
4347
|
-
},
|
|
4348
|
-
stringifiedMetadata
|
|
4349
|
-
);
|
|
4350
|
-
const entry = archiveStream.entry({
|
|
4351
|
-
name: entryPath,
|
|
4352
|
-
size: data.stats.size
|
|
4353
|
-
});
|
|
4354
|
-
if (!entry) {
|
|
4355
|
-
callback(new Error(`Failed to created an asset tar entry for ${entryPath}`));
|
|
4356
|
-
return;
|
|
4357
|
-
}
|
|
4358
|
-
data.stream.pipe(entry);
|
|
4359
|
-
entry.on("finish", () => {
|
|
4360
|
-
callback(null);
|
|
4361
|
-
}).on("error", (error) => {
|
|
4362
|
-
callback(error);
|
|
4363
|
-
});
|
|
4364
|
-
}
|
|
4365
|
-
});
|
|
4366
|
-
}
|
|
4367
|
-
}
|
|
4368
|
-
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
4369
|
-
__proto__: null,
|
|
4370
|
-
createLocalFileDestinationProvider,
|
|
4371
|
-
createLocalFileSourceProvider
|
|
4372
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
4373
|
-
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
4374
|
-
__proto__: null,
|
|
4375
|
-
providers: index$1
|
|
4376
|
-
}, Symbol.toStringTag, { value: "Module" }));
|
|
4377
|
-
export {
|
|
4378
|
-
index$7 as engine,
|
|
4379
|
-
index as file,
|
|
4380
|
-
index$2 as strapi,
|
|
4381
|
-
index$8 as utils
|
|
4382
|
-
};
|
|
1
|
+
import * as index$1 from './engine/index.mjs';
|
|
2
|
+
export { index$1 as engine };
|
|
3
|
+
import * as index$3 from './strapi/index.mjs';
|
|
4
|
+
export { index$3 as strapi };
|
|
5
|
+
import * as index$2 from './file/index.mjs';
|
|
6
|
+
export { index$2 as file };
|
|
7
|
+
import * as index from './directory/index.mjs';
|
|
8
|
+
export { index as directory };
|
|
9
|
+
import * as index$4 from './utils/index.mjs';
|
|
10
|
+
export { index$4 as utils };
|
|
4383
11
|
//# sourceMappingURL=index.mjs.map
|