@zapier/zapier-sdk 0.18.4 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/README.md +43 -31
- package/dist/index.cjs +365 -64
- package/dist/index.d.mts +220 -33
- package/dist/index.d.ts +2541 -40
- package/dist/index.mjs +345 -63
- package/package.json +14 -13
- package/dist/api/auth.d.ts +0 -19
- package/dist/api/auth.d.ts.map +0 -1
- package/dist/api/auth.js +0 -70
- package/dist/api/auth.test.d.ts +0 -2
- package/dist/api/auth.test.d.ts.map +0 -1
- package/dist/api/auth.test.js +0 -220
- package/dist/api/client.d.ts +0 -9
- package/dist/api/client.d.ts.map +0 -1
- package/dist/api/client.js +0 -356
- package/dist/api/client.test.d.ts +0 -2
- package/dist/api/client.test.d.ts.map +0 -1
- package/dist/api/client.test.js +0 -96
- package/dist/api/debug.d.ts +0 -14
- package/dist/api/debug.d.ts.map +0 -1
- package/dist/api/debug.js +0 -131
- package/dist/api/debug.test.d.ts +0 -2
- package/dist/api/debug.test.d.ts.map +0 -1
- package/dist/api/debug.test.js +0 -59
- package/dist/api/index.d.ts +0 -30
- package/dist/api/index.d.ts.map +0 -1
- package/dist/api/index.js +0 -43
- package/dist/api/polling.d.ts +0 -46
- package/dist/api/polling.d.ts.map +0 -1
- package/dist/api/polling.js +0 -139
- package/dist/api/polling.test.d.ts +0 -2
- package/dist/api/polling.test.d.ts.map +0 -1
- package/dist/api/polling.test.js +0 -318
- package/dist/api/schemas.d.ts +0 -422
- package/dist/api/schemas.d.ts.map +0 -1
- package/dist/api/schemas.js +0 -322
- package/dist/api/types.d.ts +0 -83
- package/dist/api/types.d.ts.map +0 -1
- package/dist/api/types.js +0 -1
- package/dist/auth.d.ts +0 -52
- package/dist/auth.d.ts.map +0 -1
- package/dist/auth.js +0 -72
- package/dist/auth.test.d.ts +0 -2
- package/dist/auth.test.d.ts.map +0 -1
- package/dist/auth.test.js +0 -102
- package/dist/constants.d.ts +0 -14
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -13
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -42
- package/dist/plugins/api/index.d.ts +0 -12
- package/dist/plugins/api/index.d.ts.map +0 -1
- package/dist/plugins/api/index.js +0 -24
- package/dist/plugins/apps/index.d.ts +0 -15
- package/dist/plugins/apps/index.d.ts.map +0 -1
- package/dist/plugins/apps/index.js +0 -112
- package/dist/plugins/apps/schemas.d.ts +0 -42
- package/dist/plugins/apps/schemas.d.ts.map +0 -1
- package/dist/plugins/apps/schemas.js +0 -14
- package/dist/plugins/eventEmission/builders.d.ts +0 -14
- package/dist/plugins/eventEmission/builders.d.ts.map +0 -1
- package/dist/plugins/eventEmission/builders.js +0 -109
- package/dist/plugins/eventEmission/index.d.ts +0 -38
- package/dist/plugins/eventEmission/index.d.ts.map +0 -1
- package/dist/plugins/eventEmission/index.js +0 -336
- package/dist/plugins/eventEmission/index.test.d.ts +0 -5
- package/dist/plugins/eventEmission/index.test.d.ts.map +0 -1
- package/dist/plugins/eventEmission/index.test.js +0 -581
- package/dist/plugins/eventEmission/transport.d.ts +0 -27
- package/dist/plugins/eventEmission/transport.d.ts.map +0 -1
- package/dist/plugins/eventEmission/transport.js +0 -104
- package/dist/plugins/eventEmission/transport.test.d.ts +0 -5
- package/dist/plugins/eventEmission/transport.test.d.ts.map +0 -1
- package/dist/plugins/eventEmission/transport.test.js +0 -164
- package/dist/plugins/eventEmission/types.d.ts +0 -63
- package/dist/plugins/eventEmission/types.d.ts.map +0 -1
- package/dist/plugins/eventEmission/types.js +0 -1
- package/dist/plugins/eventEmission/utils.d.ts +0 -45
- package/dist/plugins/eventEmission/utils.d.ts.map +0 -1
- package/dist/plugins/eventEmission/utils.js +0 -114
- package/dist/plugins/fetch/index.d.ts +0 -36
- package/dist/plugins/fetch/index.d.ts.map +0 -1
- package/dist/plugins/fetch/index.js +0 -62
- package/dist/plugins/fetch/schemas.d.ts +0 -19
- package/dist/plugins/fetch/schemas.d.ts.map +0 -1
- package/dist/plugins/fetch/schemas.js +0 -31
- package/dist/plugins/findFirstAuthentication/index.d.ts +0 -21
- package/dist/plugins/findFirstAuthentication/index.d.ts.map +0 -1
- package/dist/plugins/findFirstAuthentication/index.js +0 -36
- package/dist/plugins/findFirstAuthentication/index.test.d.ts +0 -2
- package/dist/plugins/findFirstAuthentication/index.test.d.ts.map +0 -1
- package/dist/plugins/findFirstAuthentication/index.test.js +0 -177
- package/dist/plugins/findFirstAuthentication/schemas.d.ts +0 -20
- package/dist/plugins/findFirstAuthentication/schemas.d.ts.map +0 -1
- package/dist/plugins/findFirstAuthentication/schemas.js +0 -18
- package/dist/plugins/findUniqueAuthentication/index.d.ts +0 -21
- package/dist/plugins/findUniqueAuthentication/index.d.ts.map +0 -1
- package/dist/plugins/findUniqueAuthentication/index.js +0 -39
- package/dist/plugins/findUniqueAuthentication/index.test.d.ts +0 -2
- package/dist/plugins/findUniqueAuthentication/index.test.d.ts.map +0 -1
- package/dist/plugins/findUniqueAuthentication/index.test.js +0 -159
- package/dist/plugins/findUniqueAuthentication/schemas.d.ts +0 -20
- package/dist/plugins/findUniqueAuthentication/schemas.d.ts.map +0 -1
- package/dist/plugins/findUniqueAuthentication/schemas.js +0 -18
- package/dist/plugins/getAction/index.d.ts +0 -25
- package/dist/plugins/getAction/index.d.ts.map +0 -1
- package/dist/plugins/getAction/index.js +0 -42
- package/dist/plugins/getAction/index.test.d.ts +0 -2
- package/dist/plugins/getAction/index.test.d.ts.map +0 -1
- package/dist/plugins/getAction/index.test.js +0 -211
- package/dist/plugins/getAction/schemas.d.ts +0 -30
- package/dist/plugins/getAction/schemas.d.ts.map +0 -1
- package/dist/plugins/getAction/schemas.js +0 -12
- package/dist/plugins/getApp/index.d.ts +0 -21
- package/dist/plugins/getApp/index.d.ts.map +0 -1
- package/dist/plugins/getApp/index.js +0 -44
- package/dist/plugins/getApp/index.test.d.ts +0 -2
- package/dist/plugins/getApp/index.test.d.ts.map +0 -1
- package/dist/plugins/getApp/index.test.js +0 -157
- package/dist/plugins/getApp/schemas.d.ts +0 -16
- package/dist/plugins/getApp/schemas.d.ts.map +0 -1
- package/dist/plugins/getApp/schemas.js +0 -8
- package/dist/plugins/getAuthentication/index.d.ts +0 -21
- package/dist/plugins/getAuthentication/index.d.ts.map +0 -1
- package/dist/plugins/getAuthentication/index.js +0 -29
- package/dist/plugins/getAuthentication/index.test.d.ts +0 -2
- package/dist/plugins/getAuthentication/index.test.d.ts.map +0 -1
- package/dist/plugins/getAuthentication/index.test.js +0 -106
- package/dist/plugins/getAuthentication/schemas.d.ts +0 -7
- package/dist/plugins/getAuthentication/schemas.d.ts.map +0 -1
- package/dist/plugins/getAuthentication/schemas.js +0 -1
- package/dist/plugins/getInputFieldsSchema/index.d.ts +0 -23
- package/dist/plugins/getInputFieldsSchema/index.d.ts.map +0 -1
- package/dist/plugins/getInputFieldsSchema/index.js +0 -53
- package/dist/plugins/getInputFieldsSchema/index.test.d.ts +0 -2
- package/dist/plugins/getInputFieldsSchema/index.test.d.ts.map +0 -1
- package/dist/plugins/getInputFieldsSchema/index.test.js +0 -291
- package/dist/plugins/getInputFieldsSchema/schemas.d.ts +0 -28
- package/dist/plugins/getInputFieldsSchema/schemas.d.ts.map +0 -1
- package/dist/plugins/getInputFieldsSchema/schemas.js +0 -13
- package/dist/plugins/getProfile/index.d.ts +0 -25
- package/dist/plugins/getProfile/index.d.ts.map +0 -1
- package/dist/plugins/getProfile/index.js +0 -39
- package/dist/plugins/getProfile/schemas.d.ts +0 -13
- package/dist/plugins/getProfile/schemas.d.ts.map +0 -1
- package/dist/plugins/getProfile/schemas.js +0 -6
- package/dist/plugins/listActions/index.d.ts +0 -30
- package/dist/plugins/listActions/index.d.ts.map +0 -1
- package/dist/plugins/listActions/index.js +0 -75
- package/dist/plugins/listActions/index.test.d.ts +0 -2
- package/dist/plugins/listActions/index.test.d.ts.map +0 -1
- package/dist/plugins/listActions/index.test.js +0 -453
- package/dist/plugins/listActions/schemas.d.ts +0 -35
- package/dist/plugins/listActions/schemas.d.ts.map +0 -1
- package/dist/plugins/listActions/schemas.js +0 -22
- package/dist/plugins/listApps/index.d.ts +0 -19
- package/dist/plugins/listApps/index.d.ts.map +0 -1
- package/dist/plugins/listApps/index.js +0 -67
- package/dist/plugins/listApps/index.test.d.ts +0 -2
- package/dist/plugins/listApps/index.test.d.ts.map +0 -1
- package/dist/plugins/listApps/index.test.js +0 -121
- package/dist/plugins/listApps/schemas.d.ts +0 -47
- package/dist/plugins/listApps/schemas.d.ts.map +0 -1
- package/dist/plugins/listApps/schemas.js +0 -49
- package/dist/plugins/listAuthentications/index.d.ts +0 -24
- package/dist/plugins/listAuthentications/index.d.ts.map +0 -1
- package/dist/plugins/listAuthentications/index.js +0 -77
- package/dist/plugins/listAuthentications/index.test.d.ts +0 -2
- package/dist/plugins/listAuthentications/index.test.d.ts.map +0 -1
- package/dist/plugins/listAuthentications/index.test.js +0 -848
- package/dist/plugins/listAuthentications/schemas.d.ts +0 -30
- package/dist/plugins/listAuthentications/schemas.d.ts.map +0 -1
- package/dist/plugins/listAuthentications/schemas.js +0 -32
- package/dist/plugins/listInputFieldChoices/index.d.ts +0 -33
- package/dist/plugins/listInputFieldChoices/index.d.ts.map +0 -1
- package/dist/plugins/listInputFieldChoices/index.js +0 -115
- package/dist/plugins/listInputFieldChoices/index.test.d.ts +0 -2
- package/dist/plugins/listInputFieldChoices/index.test.d.ts.map +0 -1
- package/dist/plugins/listInputFieldChoices/index.test.js +0 -717
- package/dist/plugins/listInputFieldChoices/schemas.d.ts +0 -43
- package/dist/plugins/listInputFieldChoices/schemas.d.ts.map +0 -1
- package/dist/plugins/listInputFieldChoices/schemas.js +0 -65
- package/dist/plugins/listInputFields/index.d.ts +0 -34
- package/dist/plugins/listInputFields/index.d.ts.map +0 -1
- package/dist/plugins/listInputFields/index.js +0 -204
- package/dist/plugins/listInputFields/index.test.d.ts +0 -2
- package/dist/plugins/listInputFields/index.test.d.ts.map +0 -1
- package/dist/plugins/listInputFields/index.test.js +0 -359
- package/dist/plugins/listInputFields/schemas.d.ts +0 -35
- package/dist/plugins/listInputFields/schemas.d.ts.map +0 -1
- package/dist/plugins/listInputFields/schemas.js +0 -23
- package/dist/plugins/manifest/index.d.ts +0 -85
- package/dist/plugins/manifest/index.d.ts.map +0 -1
- package/dist/plugins/manifest/index.js +0 -376
- package/dist/plugins/manifest/index.test.d.ts +0 -2
- package/dist/plugins/manifest/index.test.d.ts.map +0 -1
- package/dist/plugins/manifest/index.test.js +0 -1139
- package/dist/plugins/manifest/schemas.d.ts +0 -57
- package/dist/plugins/manifest/schemas.d.ts.map +0 -1
- package/dist/plugins/manifest/schemas.js +0 -50
- package/dist/plugins/registry/index.d.ts +0 -21
- package/dist/plugins/registry/index.d.ts.map +0 -1
- package/dist/plugins/registry/index.js +0 -117
- package/dist/plugins/request/index.d.ts +0 -21
- package/dist/plugins/request/index.d.ts.map +0 -1
- package/dist/plugins/request/index.js +0 -76
- package/dist/plugins/request/index.test.d.ts +0 -2
- package/dist/plugins/request/index.test.d.ts.map +0 -1
- package/dist/plugins/request/index.test.js +0 -337
- package/dist/plugins/request/schemas.d.ts +0 -54
- package/dist/plugins/request/schemas.d.ts.map +0 -1
- package/dist/plugins/request/schemas.js +0 -41
- package/dist/plugins/runAction/index.d.ts +0 -33
- package/dist/plugins/runAction/index.d.ts.map +0 -1
- package/dist/plugins/runAction/index.js +0 -108
- package/dist/plugins/runAction/index.test.d.ts +0 -2
- package/dist/plugins/runAction/index.test.d.ts.map +0 -1
- package/dist/plugins/runAction/index.test.js +0 -333
- package/dist/plugins/runAction/schemas.d.ts +0 -34
- package/dist/plugins/runAction/schemas.d.ts.map +0 -1
- package/dist/plugins/runAction/schemas.js +0 -23
- package/dist/resolvers/actionKey.d.ts +0 -15
- package/dist/resolvers/actionKey.d.ts.map +0 -1
- package/dist/resolvers/actionKey.js +0 -19
- package/dist/resolvers/actionType.d.ts +0 -10
- package/dist/resolvers/actionType.d.ts.map +0 -1
- package/dist/resolvers/actionType.js +0 -21
- package/dist/resolvers/appKey.d.ts +0 -3
- package/dist/resolvers/appKey.d.ts.map +0 -1
- package/dist/resolvers/appKey.js +0 -5
- package/dist/resolvers/authenticationId.d.ts +0 -9
- package/dist/resolvers/authenticationId.d.ts.map +0 -1
- package/dist/resolvers/authenticationId.js +0 -42
- package/dist/resolvers/index.d.ts +0 -8
- package/dist/resolvers/index.d.ts.map +0 -1
- package/dist/resolvers/index.js +0 -8
- package/dist/resolvers/inputFieldKey.d.ts +0 -11
- package/dist/resolvers/inputFieldKey.d.ts.map +0 -1
- package/dist/resolvers/inputFieldKey.js +0 -47
- package/dist/resolvers/inputs.d.ts +0 -17
- package/dist/resolvers/inputs.d.ts.map +0 -1
- package/dist/resolvers/inputs.js +0 -50
- package/dist/schemas/Action.d.ts +0 -24
- package/dist/schemas/Action.d.ts.map +0 -1
- package/dist/schemas/Action.js +0 -40
- package/dist/schemas/App.d.ts +0 -57
- package/dist/schemas/App.d.ts.map +0 -1
- package/dist/schemas/App.js +0 -26
- package/dist/schemas/Auth.d.ts +0 -55
- package/dist/schemas/Auth.d.ts.map +0 -1
- package/dist/schemas/Auth.js +0 -33
- package/dist/schemas/Field.d.ts +0 -61
- package/dist/schemas/Field.d.ts.map +0 -1
- package/dist/schemas/Field.js +0 -116
- package/dist/schemas/Run.d.ts +0 -3
- package/dist/schemas/Run.d.ts.map +0 -1
- package/dist/schemas/Run.js +0 -31
- package/dist/schemas/UserProfile.d.ts +0 -13
- package/dist/schemas/UserProfile.d.ts.map +0 -1
- package/dist/schemas/UserProfile.js +0 -32
- package/dist/sdk.d.ts +0 -146
- package/dist/sdk.d.ts.map +0 -1
- package/dist/sdk.js +0 -112
- package/dist/sdk.test.d.ts +0 -2
- package/dist/sdk.test.d.ts.map +0 -1
- package/dist/sdk.test.js +0 -258
- package/dist/services/implementations.d.ts +0 -63
- package/dist/services/implementations.d.ts.map +0 -1
- package/dist/services/implementations.js +0 -80
- package/dist/types/domain.d.ts +0 -52
- package/dist/types/domain.d.ts.map +0 -1
- package/dist/types/domain.js +0 -1
- package/dist/types/domain.test.d.ts +0 -2
- package/dist/types/domain.test.d.ts.map +0 -1
- package/dist/types/domain.test.js +0 -39
- package/dist/types/errors.d.ts +0 -143
- package/dist/types/errors.d.ts.map +0 -1
- package/dist/types/errors.js +0 -187
- package/dist/types/events.d.ts +0 -38
- package/dist/types/events.d.ts.map +0 -1
- package/dist/types/events.js +0 -7
- package/dist/types/functions.d.ts +0 -27
- package/dist/types/functions.d.ts.map +0 -1
- package/dist/types/functions.js +0 -1
- package/dist/types/plugin.d.ts +0 -75
- package/dist/types/plugin.d.ts.map +0 -1
- package/dist/types/plugin.js +0 -9
- package/dist/types/properties.d.ts +0 -33
- package/dist/types/properties.d.ts.map +0 -1
- package/dist/types/properties.js +0 -52
- package/dist/types/sdk.d.ts +0 -69
- package/dist/types/sdk.d.ts.map +0 -1
- package/dist/types/sdk.js +0 -4
- package/dist/types/telemetry-events.d.ts +0 -105
- package/dist/types/telemetry-events.d.ts.map +0 -1
- package/dist/types/telemetry-events.js +0 -8
- package/dist/utils/array-utils.d.ts +0 -31
- package/dist/utils/array-utils.d.ts.map +0 -1
- package/dist/utils/array-utils.js +0 -36
- package/dist/utils/array-utils.test.d.ts +0 -2
- package/dist/utils/array-utils.test.d.ts.map +0 -1
- package/dist/utils/array-utils.test.js +0 -107
- package/dist/utils/batch-utils.d.ts +0 -72
- package/dist/utils/batch-utils.d.ts.map +0 -1
- package/dist/utils/batch-utils.js +0 -162
- package/dist/utils/batch-utils.test.d.ts +0 -2
- package/dist/utils/batch-utils.test.d.ts.map +0 -1
- package/dist/utils/batch-utils.test.js +0 -476
- package/dist/utils/domain-utils.d.ts +0 -66
- package/dist/utils/domain-utils.d.ts.map +0 -1
- package/dist/utils/domain-utils.js +0 -164
- package/dist/utils/domain-utils.test.d.ts +0 -2
- package/dist/utils/domain-utils.test.d.ts.map +0 -1
- package/dist/utils/domain-utils.test.js +0 -346
- package/dist/utils/file-utils.d.ts +0 -4
- package/dist/utils/file-utils.d.ts.map +0 -1
- package/dist/utils/file-utils.js +0 -74
- package/dist/utils/file-utils.test.d.ts +0 -2
- package/dist/utils/file-utils.test.d.ts.map +0 -1
- package/dist/utils/file-utils.test.js +0 -51
- package/dist/utils/function-utils.d.ts +0 -73
- package/dist/utils/function-utils.d.ts.map +0 -1
- package/dist/utils/function-utils.js +0 -245
- package/dist/utils/function-utils.test.d.ts +0 -2
- package/dist/utils/function-utils.test.d.ts.map +0 -1
- package/dist/utils/function-utils.test.js +0 -110
- package/dist/utils/id-utils.d.ts +0 -13
- package/dist/utils/id-utils.d.ts.map +0 -1
- package/dist/utils/id-utils.js +0 -22
- package/dist/utils/id-utils.test.d.ts +0 -2
- package/dist/utils/id-utils.test.d.ts.map +0 -1
- package/dist/utils/id-utils.test.js +0 -22
- package/dist/utils/pagination-utils.d.ts +0 -37
- package/dist/utils/pagination-utils.d.ts.map +0 -1
- package/dist/utils/pagination-utils.js +0 -165
- package/dist/utils/pagination-utils.test.d.ts +0 -17
- package/dist/utils/pagination-utils.test.d.ts.map +0 -1
- package/dist/utils/pagination-utils.test.js +0 -461
- package/dist/utils/retry-utils.d.ts +0 -45
- package/dist/utils/retry-utils.d.ts.map +0 -1
- package/dist/utils/retry-utils.js +0 -51
- package/dist/utils/retry-utils.test.d.ts +0 -2
- package/dist/utils/retry-utils.test.d.ts.map +0 -1
- package/dist/utils/retry-utils.test.js +0 -90
- package/dist/utils/schema-utils.d.ts +0 -69
- package/dist/utils/schema-utils.d.ts.map +0 -1
- package/dist/utils/schema-utils.js +0 -72
- package/dist/utils/string-utils.d.ts +0 -40
- package/dist/utils/string-utils.d.ts.map +0 -1
- package/dist/utils/string-utils.js +0 -69
- package/dist/utils/string-utils.test.d.ts +0 -2
- package/dist/utils/string-utils.test.d.ts.map +0 -1
- package/dist/utils/string-utils.test.js +0 -59
- package/dist/utils/telemetry-utils.d.ts +0 -44
- package/dist/utils/telemetry-utils.d.ts.map +0 -1
- package/dist/utils/telemetry-utils.js +0 -55
- package/dist/utils/telemetry-utils.test.d.ts +0 -2
- package/dist/utils/telemetry-utils.test.d.ts.map +0 -1
- package/dist/utils/telemetry-utils.test.js +0 -94
- package/dist/utils/url-utils.d.ts +0 -19
- package/dist/utils/url-utils.d.ts.map +0 -1
- package/dist/utils/url-utils.js +0 -62
- package/dist/utils/url-utils.test.d.ts +0 -2
- package/dist/utils/url-utils.test.d.ts.map +0 -1
- package/dist/utils/url-utils.test.js +0 -103
- package/dist/utils/validation.d.ts +0 -4
- package/dist/utils/validation.d.ts.map +0 -1
- package/dist/utils/validation.js +0 -30
- package/dist/utils/validation.test.d.ts +0 -2
- package/dist/utils/validation.test.d.ts.map +0 -1
- package/dist/utils/validation.test.js +0 -44
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Batch Operation Utilities
|
|
3
|
-
*
|
|
4
|
-
* This module provides utilities for executing multiple async operations
|
|
5
|
-
* with concurrency control and retry logic.
|
|
6
|
-
*/
|
|
7
|
-
import { setTimeout } from "timers/promises";
|
|
8
|
-
import { calculateWaitTime, MAX_CONSECUTIVE_ERRORS } from "./retry-utils";
|
|
9
|
-
import { ZapierTimeoutError } from "../types/errors";
|
|
10
|
-
/**
|
|
11
|
-
* Default number of concurrent operations
|
|
12
|
-
* Chosen to be "polite" to APIs while still providing good throughput
|
|
13
|
-
*/
|
|
14
|
-
const DEFAULT_CONCURRENCY = 10;
|
|
15
|
-
/**
|
|
16
|
-
* Delay between starting each batch of concurrent operations (milliseconds)
|
|
17
|
-
* This prevents burst patterns that might trigger rate limiting
|
|
18
|
-
*/
|
|
19
|
-
const BATCH_START_DELAY_MS = 25;
|
|
20
|
-
/**
|
|
21
|
-
* Default timeout for entire batch operation (milliseconds)
|
|
22
|
-
* Set to 3 minutes to match polling default timeout
|
|
23
|
-
*/
|
|
24
|
-
const DEFAULT_BATCH_TIMEOUT_MS = 180000;
|
|
25
|
-
/**
|
|
26
|
-
* Execute multiple async operations with concurrency limiting and retry logic
|
|
27
|
-
*
|
|
28
|
-
* This prevents overwhelming APIs by:
|
|
29
|
-
* 1. Limiting concurrent operations (worker pool pattern)
|
|
30
|
-
* 2. Adding small delays between batches to avoid burst detection
|
|
31
|
-
* 3. Retrying failed operations with exponential backoff
|
|
32
|
-
*
|
|
33
|
-
* Problem Solved:
|
|
34
|
-
* - Rate limit prevention: Steady stream instead of burst requests
|
|
35
|
-
* - Connection pool management: Stays within browser/Node limits (~6-8 per domain)
|
|
36
|
-
* - Resilience: Transient failures are retried automatically
|
|
37
|
-
* - Observability: Returns detailed success/failure info for each operation
|
|
38
|
-
*
|
|
39
|
-
* Example Usage:
|
|
40
|
-
* ```typescript
|
|
41
|
-
* // Instead of Promise.allSettled (fires all at once):
|
|
42
|
-
* const results = await Promise.allSettled(
|
|
43
|
-
* actions.map(a => sdk.listInputFields(a))
|
|
44
|
-
* );
|
|
45
|
-
*
|
|
46
|
-
* // Use batch (controlled concurrency):
|
|
47
|
-
* const results = await batch(
|
|
48
|
-
* actions.map(a => () => sdk.listInputFields(a)),
|
|
49
|
-
* { concurrency: 10, retry: true }
|
|
50
|
-
* );
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* @param tasks - Array of functions that return promises (NOT promises themselves!)
|
|
54
|
-
* @param options - Configuration for concurrency and retry behavior
|
|
55
|
-
* @returns Promise resolving to array of settled results (same as Promise.allSettled)
|
|
56
|
-
*/
|
|
57
|
-
export async function batch(tasks, options = {}) {
|
|
58
|
-
const { concurrency = DEFAULT_CONCURRENCY, retry = true, batchDelay = BATCH_START_DELAY_MS, timeoutMs = DEFAULT_BATCH_TIMEOUT_MS, taskTimeoutMs, } = options;
|
|
59
|
-
// Validate inputs
|
|
60
|
-
if (concurrency <= 0) {
|
|
61
|
-
throw new Error("Concurrency must be greater than 0");
|
|
62
|
-
}
|
|
63
|
-
if (timeoutMs <= 0) {
|
|
64
|
-
throw new Error("Timeout must be greater than 0");
|
|
65
|
-
}
|
|
66
|
-
if (taskTimeoutMs !== undefined && taskTimeoutMs <= 0) {
|
|
67
|
-
throw new Error("Task timeout must be greater than 0");
|
|
68
|
-
}
|
|
69
|
-
if (tasks.length === 0) {
|
|
70
|
-
return [];
|
|
71
|
-
}
|
|
72
|
-
// Track overall batch timing
|
|
73
|
-
const startTime = Date.now();
|
|
74
|
-
// Pre-allocate results array to maintain input order
|
|
75
|
-
const results = new Array(tasks.length);
|
|
76
|
-
// Create task queue with retry state
|
|
77
|
-
const taskQueue = tasks.map((task, index) => ({
|
|
78
|
-
index,
|
|
79
|
-
task,
|
|
80
|
-
errorCount: 0,
|
|
81
|
-
}));
|
|
82
|
-
/**
|
|
83
|
-
* Execute a single task with retry logic and optional per-task timeout
|
|
84
|
-
*/
|
|
85
|
-
async function executeTask(taskState) {
|
|
86
|
-
const { index, task, errorCount } = taskState;
|
|
87
|
-
try {
|
|
88
|
-
let result;
|
|
89
|
-
// Apply per-task timeout if specified
|
|
90
|
-
if (taskTimeoutMs !== undefined) {
|
|
91
|
-
const timeoutPromise = setTimeout(taskTimeoutMs).then(() => {
|
|
92
|
-
throw new ZapierTimeoutError(`Task timed out after ${taskTimeoutMs}ms`);
|
|
93
|
-
});
|
|
94
|
-
result = await Promise.race([task(), timeoutPromise]);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
result = await task();
|
|
98
|
-
}
|
|
99
|
-
results[index] = { status: "fulfilled", value: result };
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
const newErrorCount = errorCount + 1;
|
|
103
|
-
// Don't retry timeout errors - they're unlikely to succeed on retry
|
|
104
|
-
const isTimeout = error instanceof ZapierTimeoutError;
|
|
105
|
-
// If retry is enabled and we haven't hit max errors and it's not a timeout, retry with backoff
|
|
106
|
-
if (retry && !isTimeout && newErrorCount < MAX_CONSECUTIVE_ERRORS) {
|
|
107
|
-
// Calculate backoff delay (base 1000ms with jitter and error scaling)
|
|
108
|
-
const waitTime = calculateWaitTime(1000, newErrorCount);
|
|
109
|
-
await setTimeout(waitTime);
|
|
110
|
-
// Re-queue the task with incremented error count
|
|
111
|
-
taskQueue.push({
|
|
112
|
-
index,
|
|
113
|
-
task,
|
|
114
|
-
errorCount: newErrorCount,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
// Max retries reached, retry disabled, or timeout error - mark as rejected
|
|
119
|
-
results[index] = { status: "rejected", reason: error };
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Worker that continuously picks up and executes tasks from the queue
|
|
125
|
-
* Checks overall batch timeout before processing each task
|
|
126
|
-
*/
|
|
127
|
-
async function worker() {
|
|
128
|
-
while (taskQueue.length > 0) {
|
|
129
|
-
// Check overall batch timeout before processing next task
|
|
130
|
-
const elapsedTime = Date.now() - startTime;
|
|
131
|
-
if (elapsedTime >= timeoutMs) {
|
|
132
|
-
throw new ZapierTimeoutError(`Batch operation timed out after ${Math.floor(elapsedTime / 1000)}s. ${taskQueue.length} task(s) not completed.`);
|
|
133
|
-
}
|
|
134
|
-
const taskState = taskQueue.shift();
|
|
135
|
-
if (!taskState)
|
|
136
|
-
break;
|
|
137
|
-
await executeTask(taskState);
|
|
138
|
-
// Small delay to prevent burst patterns within a worker
|
|
139
|
-
// Only delay if there are more tasks to process
|
|
140
|
-
if (taskQueue.length > 0 && batchDelay > 0) {
|
|
141
|
-
await setTimeout(batchDelay);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Start workers in small batches to avoid initial burst
|
|
147
|
-
* This helps avoid rate limit triggers when starting many workers
|
|
148
|
-
*/
|
|
149
|
-
const workerCount = Math.min(concurrency, tasks.length);
|
|
150
|
-
const workers = [];
|
|
151
|
-
for (let i = 0; i < workerCount; i++) {
|
|
152
|
-
workers.push(worker());
|
|
153
|
-
// Add small delay between starting workers to stagger the initial requests
|
|
154
|
-
// Skip delay for last worker
|
|
155
|
-
if (i < workerCount - 1 && batchDelay > 0) {
|
|
156
|
-
await setTimeout(batchDelay / 10); // 10ms between worker starts
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
// Wait for all workers to complete
|
|
160
|
-
await Promise.all(workers);
|
|
161
|
-
return results;
|
|
162
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"batch-utils.test.d.ts","sourceRoot":"","sources":["../../src/utils/batch-utils.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,476 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
-
import { batch } from "./batch-utils";
|
|
3
|
-
// Mock the timers/promises module
|
|
4
|
-
vi.mock("timers/promises", () => ({
|
|
5
|
-
setTimeout: vi.fn(() => Promise.resolve()),
|
|
6
|
-
}));
|
|
7
|
-
// Mock retry-utils to control backoff behavior in tests
|
|
8
|
-
vi.mock("./retry-utils", () => ({
|
|
9
|
-
calculateWaitTime: vi.fn(() => 0), // No delay in tests
|
|
10
|
-
MAX_CONSECUTIVE_ERRORS: 3,
|
|
11
|
-
}));
|
|
12
|
-
describe("batch", () => {
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
vi.clearAllMocks();
|
|
15
|
-
});
|
|
16
|
-
afterEach(() => {
|
|
17
|
-
vi.clearAllMocks();
|
|
18
|
-
});
|
|
19
|
-
describe("basic functionality", () => {
|
|
20
|
-
it("should execute all tasks and return settled results", async () => {
|
|
21
|
-
const tasks = [
|
|
22
|
-
() => Promise.resolve("result1"),
|
|
23
|
-
() => Promise.resolve("result2"),
|
|
24
|
-
() => Promise.resolve("result3"),
|
|
25
|
-
];
|
|
26
|
-
const results = await batch(tasks);
|
|
27
|
-
expect(results).toEqual([
|
|
28
|
-
{ status: "fulfilled", value: "result1" },
|
|
29
|
-
{ status: "fulfilled", value: "result2" },
|
|
30
|
-
{ status: "fulfilled", value: "result3" },
|
|
31
|
-
]);
|
|
32
|
-
});
|
|
33
|
-
it("should handle empty task array", async () => {
|
|
34
|
-
const results = await batch([]);
|
|
35
|
-
expect(results).toEqual([]);
|
|
36
|
-
});
|
|
37
|
-
it("should handle single task", async () => {
|
|
38
|
-
const tasks = [() => Promise.resolve("single")];
|
|
39
|
-
const results = await batch(tasks);
|
|
40
|
-
expect(results).toEqual([{ status: "fulfilled", value: "single" }]);
|
|
41
|
-
});
|
|
42
|
-
it("should maintain result order matching input order", async () => {
|
|
43
|
-
// Create tasks that resolve in reverse order but should maintain input order
|
|
44
|
-
const tasks = [
|
|
45
|
-
() => new Promise((resolve) => setTimeout(() => resolve("first"), 30)),
|
|
46
|
-
() => new Promise((resolve) => setTimeout(() => resolve("second"), 20)),
|
|
47
|
-
() => new Promise((resolve) => setTimeout(() => resolve("third"), 10)),
|
|
48
|
-
];
|
|
49
|
-
const results = await batch(tasks);
|
|
50
|
-
expect(results[0]).toEqual({ status: "fulfilled", value: "first" });
|
|
51
|
-
expect(results[1]).toEqual({ status: "fulfilled", value: "second" });
|
|
52
|
-
expect(results[2]).toEqual({ status: "fulfilled", value: "third" });
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
describe("concurrency limiting", () => {
|
|
56
|
-
it("should limit concurrent execution", async () => {
|
|
57
|
-
let concurrentCount = 0;
|
|
58
|
-
let maxConcurrent = 0;
|
|
59
|
-
const createTask = () => () => {
|
|
60
|
-
concurrentCount++;
|
|
61
|
-
maxConcurrent = Math.max(maxConcurrent, concurrentCount);
|
|
62
|
-
return new Promise((resolve) => {
|
|
63
|
-
setTimeout(() => {
|
|
64
|
-
concurrentCount--;
|
|
65
|
-
resolve("done");
|
|
66
|
-
}, 10);
|
|
67
|
-
});
|
|
68
|
-
};
|
|
69
|
-
const tasks = Array.from({ length: 20 }, createTask);
|
|
70
|
-
await batch(tasks, { concurrency: 5 });
|
|
71
|
-
// Max concurrent should not exceed limit
|
|
72
|
-
expect(maxConcurrent).toBeLessThanOrEqual(5);
|
|
73
|
-
});
|
|
74
|
-
it("should respect custom concurrency limit", async () => {
|
|
75
|
-
let concurrentCount = 0;
|
|
76
|
-
let maxConcurrent = 0;
|
|
77
|
-
const createTask = () => () => {
|
|
78
|
-
concurrentCount++;
|
|
79
|
-
maxConcurrent = Math.max(maxConcurrent, concurrentCount);
|
|
80
|
-
return new Promise((resolve) => {
|
|
81
|
-
setTimeout(() => {
|
|
82
|
-
concurrentCount--;
|
|
83
|
-
resolve("done");
|
|
84
|
-
}, 10);
|
|
85
|
-
});
|
|
86
|
-
};
|
|
87
|
-
const tasks = Array.from({ length: 20 }, createTask);
|
|
88
|
-
await batch(tasks, { concurrency: 3 });
|
|
89
|
-
expect(maxConcurrent).toBeLessThanOrEqual(3);
|
|
90
|
-
});
|
|
91
|
-
it("should handle concurrency limit greater than task count", async () => {
|
|
92
|
-
const tasks = [() => Promise.resolve(1), () => Promise.resolve(2)];
|
|
93
|
-
const results = await batch(tasks, { concurrency: 10 });
|
|
94
|
-
expect(results).toHaveLength(2);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
describe("error handling", () => {
|
|
98
|
-
it("should handle failing tasks without stopping batch", async () => {
|
|
99
|
-
const tasks = [
|
|
100
|
-
() => Promise.resolve("success1"),
|
|
101
|
-
() => Promise.reject(new Error("failed")),
|
|
102
|
-
() => Promise.resolve("success2"),
|
|
103
|
-
];
|
|
104
|
-
const results = await batch(tasks, { retry: false });
|
|
105
|
-
expect(results[0]).toEqual({ status: "fulfilled", value: "success1" });
|
|
106
|
-
expect(results[1]).toEqual({
|
|
107
|
-
status: "rejected",
|
|
108
|
-
reason: expect.any(Error),
|
|
109
|
-
});
|
|
110
|
-
expect(results[2]).toEqual({ status: "fulfilled", value: "success2" });
|
|
111
|
-
});
|
|
112
|
-
it("should retry failed tasks when retry is enabled", async () => {
|
|
113
|
-
let attemptCount = 0;
|
|
114
|
-
const failTwiceThenSucceed = () => {
|
|
115
|
-
attemptCount++;
|
|
116
|
-
if (attemptCount <= 2) {
|
|
117
|
-
return Promise.reject(new Error("temporary failure"));
|
|
118
|
-
}
|
|
119
|
-
return Promise.resolve("success");
|
|
120
|
-
};
|
|
121
|
-
const tasks = [failTwiceThenSucceed];
|
|
122
|
-
const results = await batch(tasks, { retry: true });
|
|
123
|
-
expect(results[0]).toEqual({ status: "fulfilled", value: "success" });
|
|
124
|
-
expect(attemptCount).toBe(3); // Initial + 2 retries
|
|
125
|
-
});
|
|
126
|
-
it("should give up after MAX_CONSECUTIVE_ERRORS retries", async () => {
|
|
127
|
-
let attemptCount = 0;
|
|
128
|
-
const alwaysFail = () => {
|
|
129
|
-
attemptCount++;
|
|
130
|
-
return Promise.reject(new Error("always fails"));
|
|
131
|
-
};
|
|
132
|
-
const tasks = [alwaysFail];
|
|
133
|
-
const results = await batch(tasks, { retry: true });
|
|
134
|
-
expect(results[0]).toEqual({
|
|
135
|
-
status: "rejected",
|
|
136
|
-
reason: expect.any(Error),
|
|
137
|
-
});
|
|
138
|
-
expect(attemptCount).toBe(3); // MAX_CONSECUTIVE_ERRORS
|
|
139
|
-
});
|
|
140
|
-
it("should not retry when retry is disabled", async () => {
|
|
141
|
-
let attemptCount = 0;
|
|
142
|
-
const failTask = () => {
|
|
143
|
-
attemptCount++;
|
|
144
|
-
return Promise.reject(new Error("failed"));
|
|
145
|
-
};
|
|
146
|
-
const tasks = [failTask];
|
|
147
|
-
const results = await batch(tasks, { retry: false });
|
|
148
|
-
expect(results[0]).toEqual({
|
|
149
|
-
status: "rejected",
|
|
150
|
-
reason: expect.any(Error),
|
|
151
|
-
});
|
|
152
|
-
expect(attemptCount).toBe(1); // No retries
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
describe("options validation", () => {
|
|
156
|
-
it("should throw error for invalid concurrency", async () => {
|
|
157
|
-
const tasks = [() => Promise.resolve("test")];
|
|
158
|
-
await expect(batch(tasks, { concurrency: 0 })).rejects.toThrow("Concurrency must be greater than 0");
|
|
159
|
-
await expect(batch(tasks, { concurrency: -1 })).rejects.toThrow("Concurrency must be greater than 0");
|
|
160
|
-
});
|
|
161
|
-
it("should use default options when not provided", async () => {
|
|
162
|
-
const tasks = [() => Promise.resolve("test")];
|
|
163
|
-
const results = await batch(tasks);
|
|
164
|
-
expect(results).toEqual([{ status: "fulfilled", value: "test" }]);
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
describe("mixed success and failure scenarios", () => {
|
|
168
|
-
it("should handle alternating success and failure", async () => {
|
|
169
|
-
const tasks = [
|
|
170
|
-
() => Promise.resolve("success1"),
|
|
171
|
-
() => Promise.reject(new Error("error1")),
|
|
172
|
-
() => Promise.resolve("success2"),
|
|
173
|
-
() => Promise.reject(new Error("error2")),
|
|
174
|
-
() => Promise.resolve("success3"),
|
|
175
|
-
];
|
|
176
|
-
const results = await batch(tasks, { retry: false });
|
|
177
|
-
expect(results[0].status).toBe("fulfilled");
|
|
178
|
-
expect(results[1].status).toBe("rejected");
|
|
179
|
-
expect(results[2].status).toBe("fulfilled");
|
|
180
|
-
expect(results[3].status).toBe("rejected");
|
|
181
|
-
expect(results[4].status).toBe("fulfilled");
|
|
182
|
-
});
|
|
183
|
-
it("should handle some tasks succeeding after retry", async () => {
|
|
184
|
-
let task1Attempts = 0;
|
|
185
|
-
let task2Attempts = 0;
|
|
186
|
-
const task1 = () => {
|
|
187
|
-
task1Attempts++;
|
|
188
|
-
if (task1Attempts === 1) {
|
|
189
|
-
return Promise.reject(new Error("task1 first attempt"));
|
|
190
|
-
}
|
|
191
|
-
return Promise.resolve("task1 success");
|
|
192
|
-
};
|
|
193
|
-
const task2 = () => {
|
|
194
|
-
task2Attempts++;
|
|
195
|
-
return Promise.resolve("task2 success");
|
|
196
|
-
};
|
|
197
|
-
const tasks = [task1, task2];
|
|
198
|
-
const results = await batch(tasks, { retry: true });
|
|
199
|
-
expect(results[0]).toEqual({
|
|
200
|
-
status: "fulfilled",
|
|
201
|
-
value: "task1 success",
|
|
202
|
-
});
|
|
203
|
-
expect(results[1]).toEqual({
|
|
204
|
-
status: "fulfilled",
|
|
205
|
-
value: "task2 success",
|
|
206
|
-
});
|
|
207
|
-
expect(task1Attempts).toBe(2); // Initial + 1 retry
|
|
208
|
-
expect(task2Attempts).toBe(1); // No retry needed
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
describe("performance characteristics", () => {
|
|
212
|
-
it("should complete faster with higher concurrency", async () => {
|
|
213
|
-
const delay = 50; // ms
|
|
214
|
-
const taskCount = 10;
|
|
215
|
-
const tasks = Array.from({ length: taskCount }, () => () => new Promise((resolve) => setTimeout(() => resolve("done"), delay)));
|
|
216
|
-
// Low concurrency (sequential-ish)
|
|
217
|
-
const start1 = Date.now();
|
|
218
|
-
await batch([...tasks], { concurrency: 1 });
|
|
219
|
-
const duration1 = Date.now() - start1;
|
|
220
|
-
// High concurrency (parallel)
|
|
221
|
-
const start2 = Date.now();
|
|
222
|
-
await batch([...tasks], { concurrency: 10 });
|
|
223
|
-
const duration2 = Date.now() - start2;
|
|
224
|
-
// High concurrency should be significantly faster
|
|
225
|
-
// Allow some margin for timing variability
|
|
226
|
-
expect(duration2).toBeLessThan(duration1 * 0.8);
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
describe("result ordering guarantees", () => {
|
|
230
|
-
it("should maintain exact order with out-of-order completion and mixed results", async () => {
|
|
231
|
-
// This is the ultimate ordering test - combining:
|
|
232
|
-
// 1. Tasks that complete in reverse order (via delays)
|
|
233
|
-
// 2. Mixed success/failure
|
|
234
|
-
// 3. High concurrency (multiple workers racing)
|
|
235
|
-
const executionLog = [];
|
|
236
|
-
const tasks = [
|
|
237
|
-
// Task 0: Slow success
|
|
238
|
-
() => new Promise((resolve) => {
|
|
239
|
-
setTimeout(() => {
|
|
240
|
-
executionLog.push("task0-complete");
|
|
241
|
-
resolve("result0");
|
|
242
|
-
}, 100);
|
|
243
|
-
}),
|
|
244
|
-
// Task 1: Fast failure
|
|
245
|
-
() => new Promise((_, reject) => {
|
|
246
|
-
setTimeout(() => {
|
|
247
|
-
executionLog.push("task1-complete");
|
|
248
|
-
reject(new Error("error1"));
|
|
249
|
-
}, 10);
|
|
250
|
-
}),
|
|
251
|
-
// Task 2: Medium success
|
|
252
|
-
() => new Promise((resolve) => {
|
|
253
|
-
setTimeout(() => {
|
|
254
|
-
executionLog.push("task2-complete");
|
|
255
|
-
resolve("result2");
|
|
256
|
-
}, 50);
|
|
257
|
-
}),
|
|
258
|
-
// Task 3: Instant failure
|
|
259
|
-
() => new Promise((_, reject) => {
|
|
260
|
-
executionLog.push("task3-complete");
|
|
261
|
-
reject(new Error("error3"));
|
|
262
|
-
}),
|
|
263
|
-
// Task 4: Medium-slow success
|
|
264
|
-
() => new Promise((resolve) => {
|
|
265
|
-
setTimeout(() => {
|
|
266
|
-
executionLog.push("task4-complete");
|
|
267
|
-
resolve("result4");
|
|
268
|
-
}, 75);
|
|
269
|
-
}),
|
|
270
|
-
];
|
|
271
|
-
const results = await batch(tasks, { concurrency: 5, retry: false });
|
|
272
|
-
// Verify all tasks completed
|
|
273
|
-
expect(executionLog).toHaveLength(5);
|
|
274
|
-
expect(executionLog).toContain("task0-complete");
|
|
275
|
-
expect(executionLog).toContain("task1-complete");
|
|
276
|
-
expect(executionLog).toContain("task2-complete");
|
|
277
|
-
expect(executionLog).toContain("task3-complete");
|
|
278
|
-
expect(executionLog).toContain("task4-complete");
|
|
279
|
-
// BUT results MUST maintain input order despite out-of-order completion
|
|
280
|
-
expect(results).toHaveLength(5);
|
|
281
|
-
// Task 0: position 0, slow success
|
|
282
|
-
expect(results[0]).toEqual({
|
|
283
|
-
status: "fulfilled",
|
|
284
|
-
value: "result0",
|
|
285
|
-
});
|
|
286
|
-
// Task 1: position 1, fast failure
|
|
287
|
-
expect(results[1]).toEqual({
|
|
288
|
-
status: "rejected",
|
|
289
|
-
reason: expect.objectContaining({
|
|
290
|
-
message: "error1",
|
|
291
|
-
}),
|
|
292
|
-
});
|
|
293
|
-
// Task 2: position 2, medium success
|
|
294
|
-
expect(results[2]).toEqual({
|
|
295
|
-
status: "fulfilled",
|
|
296
|
-
value: "result2",
|
|
297
|
-
});
|
|
298
|
-
// Task 3: position 3, instant failure
|
|
299
|
-
expect(results[3]).toEqual({
|
|
300
|
-
status: "rejected",
|
|
301
|
-
reason: expect.objectContaining({
|
|
302
|
-
message: "error3",
|
|
303
|
-
}),
|
|
304
|
-
});
|
|
305
|
-
// Task 4: position 4, medium-slow success
|
|
306
|
-
expect(results[4]).toEqual({
|
|
307
|
-
status: "fulfilled",
|
|
308
|
-
value: "result4",
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
it("should match Promise.allSettled ordering behavior exactly", async () => {
|
|
312
|
-
// This test ensures our batch function has identical ordering to Promise.allSettled
|
|
313
|
-
const tasks = [
|
|
314
|
-
() => new Promise((resolve) => setTimeout(() => resolve("a"), 30)),
|
|
315
|
-
() => new Promise((_, reject) => setTimeout(() => reject(new Error("b")), 10)),
|
|
316
|
-
() => new Promise((resolve) => setTimeout(() => resolve("c"), 20)),
|
|
317
|
-
];
|
|
318
|
-
// Run both in parallel
|
|
319
|
-
const [batchResults, settledResults] = await Promise.all([
|
|
320
|
-
batch(tasks.map((t) => t), { concurrency: 3, retry: false }),
|
|
321
|
-
Promise.allSettled(tasks.map((t) => t())),
|
|
322
|
-
]);
|
|
323
|
-
// Results should have same structure
|
|
324
|
-
expect(batchResults).toHaveLength(settledResults.length);
|
|
325
|
-
// Compare each result
|
|
326
|
-
for (let i = 0; i < batchResults.length; i++) {
|
|
327
|
-
const batchResult = batchResults[i];
|
|
328
|
-
const settledResult = settledResults[i];
|
|
329
|
-
expect(batchResult.status).toBe(settledResult.status);
|
|
330
|
-
if (batchResult.status === "fulfilled" &&
|
|
331
|
-
settledResult.status === "fulfilled") {
|
|
332
|
-
expect(batchResult.value).toBe(settledResult.value);
|
|
333
|
-
}
|
|
334
|
-
if (batchResult.status === "rejected" &&
|
|
335
|
-
settledResult.status === "rejected") {
|
|
336
|
-
expect(batchResult.reason).toBeInstanceOf(Error);
|
|
337
|
-
expect(settledResult.reason).toBeInstanceOf(Error);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
describe("timeout handling", () => {
|
|
343
|
-
beforeEach(async () => {
|
|
344
|
-
// Unmock setTimeout for timeout tests to allow real timing
|
|
345
|
-
vi.unmock("timers/promises");
|
|
346
|
-
// Re-import batch to get the non-mocked version
|
|
347
|
-
vi.resetModules();
|
|
348
|
-
});
|
|
349
|
-
afterEach(() => {
|
|
350
|
-
// Restore mocks after timeout tests
|
|
351
|
-
vi.doMock("timers/promises", () => ({
|
|
352
|
-
setTimeout: vi.fn(() => Promise.resolve()),
|
|
353
|
-
}));
|
|
354
|
-
});
|
|
355
|
-
it("should throw error when overall batch timeout is exceeded", async () => {
|
|
356
|
-
const { batch } = await import("./batch-utils");
|
|
357
|
-
const tasks = Array.from({ length: 10 }, () => () => new Promise((resolve) => setTimeout(() => resolve("done"), 100)));
|
|
358
|
-
await expect(batch(tasks, { concurrency: 2, timeoutMs: 50 })).rejects.toThrow("Batch operation timed out");
|
|
359
|
-
});
|
|
360
|
-
it("should timeout individual tasks when taskTimeoutMs is set", async () => {
|
|
361
|
-
const { batch } = await import("./batch-utils");
|
|
362
|
-
const tasks = [
|
|
363
|
-
() => Promise.resolve("instant"), // Instant task - should not timeout
|
|
364
|
-
() => new Promise((resolve) => setTimeout(() => resolve("slow"), 500)), // Slow task - should timeout
|
|
365
|
-
() => Promise.resolve("instant2"), // Another instant task - should not timeout
|
|
366
|
-
];
|
|
367
|
-
const results = await batch(tasks, {
|
|
368
|
-
taskTimeoutMs: 100, // 100ms timeout
|
|
369
|
-
retry: false,
|
|
370
|
-
batchDelay: 0, // Disable batch delay for timing reliability
|
|
371
|
-
});
|
|
372
|
-
expect(results[0].status).toBe("fulfilled");
|
|
373
|
-
expect(results[1].status).toBe("rejected");
|
|
374
|
-
if (results[1].status === "rejected") {
|
|
375
|
-
expect(results[1].reason).toBeInstanceOf(Error);
|
|
376
|
-
expect(results[1].reason.message).toContain("timed out");
|
|
377
|
-
}
|
|
378
|
-
expect(results[2].status).toBe("fulfilled");
|
|
379
|
-
});
|
|
380
|
-
it("should not retry tasks that timeout", async () => {
|
|
381
|
-
const { batch } = await import("./batch-utils");
|
|
382
|
-
let attemptCount = 0;
|
|
383
|
-
const slowTask = () => {
|
|
384
|
-
attemptCount++;
|
|
385
|
-
return new Promise((resolve) => setTimeout(() => resolve("done"), 200));
|
|
386
|
-
};
|
|
387
|
-
const results = await batch([slowTask], {
|
|
388
|
-
taskTimeoutMs: 50,
|
|
389
|
-
retry: true,
|
|
390
|
-
});
|
|
391
|
-
expect(results[0].status).toBe("rejected");
|
|
392
|
-
expect(attemptCount).toBe(1); // Should not retry timeout errors
|
|
393
|
-
});
|
|
394
|
-
it("should validate timeout parameters", async () => {
|
|
395
|
-
const { batch } = await import("./batch-utils");
|
|
396
|
-
const tasks = [() => Promise.resolve("test")];
|
|
397
|
-
await expect(batch(tasks, { timeoutMs: 0 })).rejects.toThrow("Timeout must be greater than 0");
|
|
398
|
-
await expect(batch(tasks, { timeoutMs: -100 })).rejects.toThrow("Timeout must be greater than 0");
|
|
399
|
-
await expect(batch(tasks, { taskTimeoutMs: 0 })).rejects.toThrow("Task timeout must be greater than 0");
|
|
400
|
-
await expect(batch(tasks, { taskTimeoutMs: -100 })).rejects.toThrow("Task timeout must be greater than 0");
|
|
401
|
-
});
|
|
402
|
-
it("should complete tasks that finish before overall timeout", async () => {
|
|
403
|
-
const { batch } = await import("./batch-utils");
|
|
404
|
-
const tasks = [
|
|
405
|
-
() => new Promise((resolve) => setTimeout(() => resolve("task1"), 10)),
|
|
406
|
-
() => new Promise((resolve) => setTimeout(() => resolve("task2"), 20)),
|
|
407
|
-
() => new Promise((resolve) => setTimeout(() => resolve("task3"), 30)),
|
|
408
|
-
];
|
|
409
|
-
const results = await batch(tasks, {
|
|
410
|
-
timeoutMs: 100,
|
|
411
|
-
concurrency: 3,
|
|
412
|
-
});
|
|
413
|
-
expect(results).toHaveLength(3);
|
|
414
|
-
expect(results[0].status).toBe("fulfilled");
|
|
415
|
-
expect(results[1].status).toBe("fulfilled");
|
|
416
|
-
expect(results[2].status).toBe("fulfilled");
|
|
417
|
-
});
|
|
418
|
-
it("should report tasks remaining when batch timeout occurs", async () => {
|
|
419
|
-
const { batch } = await import("./batch-utils");
|
|
420
|
-
const tasks = Array.from({ length: 20 }, () => () => new Promise((resolve) => setTimeout(() => resolve("done"), 100)));
|
|
421
|
-
try {
|
|
422
|
-
await batch(tasks, { concurrency: 2, timeoutMs: 50 });
|
|
423
|
-
expect.fail("Should have thrown timeout error");
|
|
424
|
-
}
|
|
425
|
-
catch (error) {
|
|
426
|
-
expect(error).toBeInstanceOf(Error);
|
|
427
|
-
expect(error.message).toContain("task(s) not completed");
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
it("should allow taskTimeoutMs without overall timeout affecting quick tasks", async () => {
|
|
431
|
-
const { batch } = await import("./batch-utils");
|
|
432
|
-
const tasks = [
|
|
433
|
-
() => Promise.resolve("instant1"),
|
|
434
|
-
() => Promise.resolve("instant2"),
|
|
435
|
-
() => Promise.resolve("instant3"),
|
|
436
|
-
];
|
|
437
|
-
const results = await batch(tasks, {
|
|
438
|
-
taskTimeoutMs: 1000,
|
|
439
|
-
timeoutMs: 5000,
|
|
440
|
-
});
|
|
441
|
-
expect(results).toHaveLength(3);
|
|
442
|
-
expect(results.every((r) => r.status === "fulfilled")).toBe(true);
|
|
443
|
-
});
|
|
444
|
-
it("should handle mix of timed out and successful tasks", async () => {
|
|
445
|
-
const { batch } = await import("./batch-utils");
|
|
446
|
-
const tasks = [
|
|
447
|
-
() => Promise.resolve("fast1"), // Instant - should not timeout
|
|
448
|
-
() => new Promise((resolve) => setTimeout(() => resolve("slow1"), 500)), // Slow - should timeout
|
|
449
|
-
() => Promise.resolve("fast2"), // Instant - should not timeout
|
|
450
|
-
() => new Promise((resolve) => setTimeout(() => resolve("slow2"), 500)), // Slow - should timeout
|
|
451
|
-
];
|
|
452
|
-
const results = await batch(tasks, {
|
|
453
|
-
taskTimeoutMs: 100,
|
|
454
|
-
retry: false,
|
|
455
|
-
concurrency: 4,
|
|
456
|
-
batchDelay: 0, // Disable batch delay for timing reliability
|
|
457
|
-
});
|
|
458
|
-
expect(results[0].status).toBe("fulfilled");
|
|
459
|
-
expect(results[1].status).toBe("rejected");
|
|
460
|
-
expect(results[2].status).toBe("fulfilled");
|
|
461
|
-
expect(results[3].status).toBe("rejected");
|
|
462
|
-
});
|
|
463
|
-
it("should use default timeout when not specified", async () => {
|
|
464
|
-
const { batch } = await import("./batch-utils");
|
|
465
|
-
const tasks = [
|
|
466
|
-
() => Promise.resolve("test1"),
|
|
467
|
-
() => Promise.resolve("test2"),
|
|
468
|
-
];
|
|
469
|
-
// Should not throw with default 3-minute timeout
|
|
470
|
-
const results = await batch(tasks);
|
|
471
|
-
expect(results).toHaveLength(2);
|
|
472
|
-
expect(results[0].status).toBe("fulfilled");
|
|
473
|
-
expect(results[1].status).toBe("fulfilled");
|
|
474
|
-
});
|
|
475
|
-
});
|
|
476
|
-
});
|