apify-client 3.0.0-beta.8 → 3.0.0-beta.80

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.
Files changed (120) hide show
  1. package/README.md +10 -2
  2. package/dist/apify_api_error.d.ts +13 -27
  3. package/dist/apify_api_error.js +33 -23
  4. package/dist/apify_client.d.ts +125 -10
  5. package/dist/apify_client.js +133 -48
  6. package/dist/apify_client_error.d.ts +10 -0
  7. package/dist/apify_client_error.js +10 -0
  8. package/dist/base/api_client.d.ts +7 -7
  9. package/dist/base/api_client.js +36 -36
  10. package/dist/base/resource_client.d.ts +21 -8
  11. package/dist/base/resource_client.js +67 -33
  12. package/dist/base/resource_collection_client.d.ts +12 -4
  13. package/dist/base/resource_collection_client.js +34 -11
  14. package/dist/body_parser.d.ts +3 -4
  15. package/dist/body_parser.js +19 -26
  16. package/dist/bundle.js +27 -22
  17. package/dist/bundle.js.map +1 -1
  18. package/dist/generated/api.d.ts +3033 -971
  19. package/dist/generated/schemas.d.ts +2340 -932
  20. package/dist/generated/schemas.js +570 -229
  21. package/dist/http_clients/axios.d.ts +60 -0
  22. package/dist/http_clients/axios.js +117 -0
  23. package/dist/http_clients/base.d.ts +336 -0
  24. package/dist/http_clients/base.js +612 -0
  25. package/dist/http_clients/index.d.ts +4 -0
  26. package/dist/http_clients/index.js +2 -0
  27. package/dist/http_compressors/base.d.ts +40 -0
  28. package/dist/http_compressors/base.js +9 -0
  29. package/dist/http_compressors/brotli.d.ts +33 -0
  30. package/dist/http_compressors/brotli.js +39 -0
  31. package/dist/http_compressors/gzip.d.ts +33 -0
  32. package/dist/http_compressors/gzip.js +39 -0
  33. package/dist/http_compressors/index.d.ts +6 -0
  34. package/dist/http_compressors/index.js +2 -0
  35. package/dist/http_compressors/resolve.d.ts +23 -0
  36. package/dist/http_compressors/resolve.js +27 -0
  37. package/dist/index.d.ts +5 -1
  38. package/dist/index.js +4 -1
  39. package/dist/invalid_response_body_error.d.ts +14 -0
  40. package/dist/invalid_response_body_error.js +18 -0
  41. package/dist/models.d.ts +166 -243
  42. package/dist/models.js +6 -4
  43. package/dist/resource_clients/actor.d.ts +74 -34
  44. package/dist/resource_clients/actor.js +87 -56
  45. package/dist/resource_clients/actor_collection.d.ts +18 -12
  46. package/dist/resource_clients/actor_collection.js +11 -3
  47. package/dist/resource_clients/actor_env_var.d.ts +11 -4
  48. package/dist/resource_clients/actor_env_var.js +16 -6
  49. package/dist/resource_clients/actor_env_var_collection.d.ts +9 -4
  50. package/dist/resource_clients/actor_env_var_collection.js +11 -4
  51. package/dist/resource_clients/actor_version.d.ts +20 -5
  52. package/dist/resource_clients/actor_version.js +19 -9
  53. package/dist/resource_clients/actor_version_collection.d.ts +11 -4
  54. package/dist/resource_clients/actor_version_collection.js +13 -4
  55. package/dist/resource_clients/build.d.ts +20 -7
  56. package/dist/resource_clients/build.js +35 -15
  57. package/dist/resource_clients/build_collection.d.ts +7 -5
  58. package/dist/resource_clients/build_collection.js +4 -1
  59. package/dist/resource_clients/dataset.d.ts +81 -36
  60. package/dist/resource_clients/dataset.js +109 -120
  61. package/dist/resource_clients/dataset_collection.d.ts +10 -6
  62. package/dist/resource_clients/dataset_collection.js +13 -2
  63. package/dist/resource_clients/key_value_store.d.ts +106 -25
  64. package/dist/resource_clients/key_value_store.js +131 -50
  65. package/dist/resource_clients/key_value_store_collection.d.ts +10 -6
  66. package/dist/resource_clients/key_value_store_collection.js +13 -2
  67. package/dist/resource_clients/log.d.ts +40 -21
  68. package/dist/resource_clients/log.js +144 -66
  69. package/dist/resource_clients/request_queue.d.ts +76 -31
  70. package/dist/resource_clients/request_queue.js +178 -93
  71. package/dist/resource_clients/request_queue_collection.d.ts +9 -5
  72. package/dist/resource_clients/request_queue_collection.js +9 -3
  73. package/dist/resource_clients/run.d.ts +126 -28
  74. package/dist/resource_clients/run.js +193 -49
  75. package/dist/resource_clients/run_collection.d.ts +5 -3
  76. package/dist/resource_clients/run_collection.js +4 -1
  77. package/dist/resource_clients/schedule.d.ts +16 -7
  78. package/dist/resource_clients/schedule.js +30 -22
  79. package/dist/resource_clients/schedule_collection.d.ts +6 -2
  80. package/dist/resource_clients/schedule_collection.js +9 -3
  81. package/dist/resource_clients/store_collection.d.ts +6 -4
  82. package/dist/resource_clients/store_collection.js +4 -1
  83. package/dist/resource_clients/task.d.ts +36 -13
  84. package/dist/resource_clients/task.js +79 -46
  85. package/dist/resource_clients/task_collection.d.ts +9 -5
  86. package/dist/resource_clients/task_collection.js +9 -3
  87. package/dist/resource_clients/user.d.ts +19 -10
  88. package/dist/resource_clients/user.js +42 -38
  89. package/dist/resource_clients/webhook.d.ts +17 -8
  90. package/dist/resource_clients/webhook.js +30 -22
  91. package/dist/resource_clients/webhook_collection.d.ts +8 -4
  92. package/dist/resource_clients/webhook_collection.js +9 -3
  93. package/dist/resource_clients/webhook_dispatch.d.ts +4 -1
  94. package/dist/resource_clients/webhook_dispatch.js +7 -2
  95. package/dist/resource_clients/webhook_dispatch_collection.d.ts +3 -1
  96. package/dist/resource_clients/webhook_dispatch_collection.js +4 -1
  97. package/dist/response_validation_error.d.ts +2 -1
  98. package/dist/response_validation_error.js +2 -1
  99. package/dist/runtime/node.d.ts +6 -0
  100. package/dist/runtime/node.js +80 -0
  101. package/dist/runtime/types.d.ts +60 -0
  102. package/dist/runtime/types.js +1 -0
  103. package/dist/runtime/web.d.ts +5 -0
  104. package/dist/runtime/web.js +21 -0
  105. package/dist/schemas.d.ts +368 -4
  106. package/dist/schemas.js +26 -4
  107. package/dist/statistics.d.ts +1 -6
  108. package/dist/statistics.js +2 -2
  109. package/dist/timeouts.d.ts +84 -0
  110. package/dist/timeouts.js +37 -0
  111. package/dist/timezones.d.ts +1 -1
  112. package/dist/utils.d.ts +93 -58
  113. package/dist/utils.js +226 -158
  114. package/dist/wait_for_resources.d.ts +8 -0
  115. package/dist/wait_for_resources.js +39 -0
  116. package/package.json +32 -29
  117. package/dist/http_client.d.ts +0 -88
  118. package/dist/http_client.js +0 -255
  119. package/dist/interceptors.d.ts +0 -19
  120. package/dist/interceptors.js +0 -105
@@ -1,6 +1,7 @@
1
1
  import { ResourceClient } from '../base/resource_client.js';
2
2
  import * as schemas from '../schemas.js';
3
- import { anyObjectSchema, catchNotFoundOrThrow, parseArgument, parseResponse } from '../utils.js';
3
+ import { timeoutOptionsSchema } from '../timeouts.js';
4
+ import { anyObjectSchema, parseArgument, parseResponse } from '../utils.js';
4
5
  import { WebhookDispatchCollectionClient } from './webhook_dispatch_collection.js';
5
6
  /**
6
7
  * Client for managing a specific webhook.
@@ -43,51 +44,58 @@ export class WebhookClient extends ResourceClient {
43
44
  /**
44
45
  * Retrieves the webhook.
45
46
  *
47
+ * @param options - Request options
48
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
46
49
  * @returns The webhook object, or `undefined` if it does not exist.
47
50
  * @see https://docs.apify.com/api/v2/webhook-get
48
51
  */
49
- async get() {
50
- return this._get(schemas.Webhook());
52
+ async get(options = {}) {
53
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
54
+ return this.getResource(schemas.WebhookResource(), {}, timeoutSecs, signal);
51
55
  }
52
56
  /**
53
57
  * Updates the webhook with the specified fields.
54
58
  *
55
59
  * @param newFields - Fields to update.
60
+ * @param options - Request options
61
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
56
62
  * @returns The updated webhook object.
57
63
  * @see https://docs.apify.com/api/v2/webhook-put
58
64
  */
59
- async update(newFields) {
65
+ async update(newFields, options = {}) {
60
66
  parseArgument(newFields, anyObjectSchema);
61
- return this._update(schemas.Webhook(), newFields);
67
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
68
+ return this.updateResource(schemas.WebhookResource(), newFields, timeoutSecs, signal);
62
69
  }
63
70
  /**
64
71
  * Deletes the webhook.
65
72
  *
73
+ * @param options - Request options
74
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
66
75
  * @see https://docs.apify.com/api/v2/webhook-delete
67
76
  */
68
- async delete() {
69
- return this._delete();
77
+ async delete(options = {}) {
78
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
79
+ return this.deleteResource(timeoutSecs, signal);
70
80
  }
71
81
  /**
72
82
  * Tests the webhook by dispatching a test event.
73
83
  *
74
- * @returns The webhook dispatch object, or `undefined` if the test fails.
84
+ * @param options - Request options
85
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
86
+ * @returns The webhook dispatch object.
75
87
  * @see https://docs.apify.com/api/v2/webhook-test-post
76
88
  */
77
- async test() {
78
- const request = {
79
- url: this._url('test'),
89
+ async test(options = {}) {
90
+ const { timeoutSecs = 'medium', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
91
+ const response = await this.httpClient.call({
92
+ url: this.buildUrl('test'),
80
93
  method: 'POST',
81
- params: this._params(),
82
- };
83
- try {
84
- const response = await this.httpClient.call(request);
85
- return parseResponse(response, schemas.WebhookDispatch());
86
- }
87
- catch (err) {
88
- catchNotFoundOrThrow(err);
89
- }
90
- return undefined;
94
+ params: this.buildParams(),
95
+ timeoutSecs,
96
+ signal,
97
+ });
98
+ return parseResponse(response, schemas.WebhookDispatch());
91
99
  }
92
100
  /**
93
101
  * Returns a client for the dispatches of this webhook.
@@ -96,7 +104,7 @@ export class WebhookClient extends ResourceClient {
96
104
  * @see https://docs.apify.com/api/v2/webhook-webhook-dispatches-get
97
105
  */
98
106
  dispatches() {
99
- return new WebhookDispatchCollectionClient(this._subResourceOptions({
107
+ return new WebhookDispatchCollectionClient(this.subResourceOptions({
100
108
  resourcePath: 'dispatches',
101
109
  }));
102
110
  }
@@ -1,7 +1,8 @@
1
1
  import type { ApiClientSubResourceOptions } from '../base/api_client.js';
2
2
  import { ResourceCollectionClient } from '../base/resource_collection_client.js';
3
+ import type { TimeoutOptions } from '../timeouts.js';
3
4
  import type { PaginatedIterator, PaginationOptions } from '../utils.js';
4
- import type { Webhook, WebhookUpdateData } from './webhook.js';
5
+ import type { WebhookResource, WebhookUpdateData } from './webhook.js';
5
6
  /**
6
7
  * Client for managing the collection of Webhooks.
7
8
  *
@@ -47,19 +48,22 @@ export declare class WebhookCollectionClient extends ResourceCollectionClient {
47
48
  * ```
48
49
  *
49
50
  * @param options - Pagination and sorting options.
51
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
50
52
  * @returns A paginated iterator of webhooks.
51
53
  * @see https://docs.apify.com/api/v2/webhooks-get
52
54
  */
53
- list(options?: WebhookCollectionListOptions): PaginatedIterator<Omit<Webhook, 'payloadTemplate' | 'headersTemplate'>>;
55
+ list(options?: WebhookCollectionListOptions): PaginatedIterator<Omit<WebhookResource, 'payloadTemplate' | 'headersTemplate'>>;
54
56
  /**
55
57
  * Creates a new webhook.
56
58
  *
57
59
  * @param webhook - The webhook data.
60
+ * @param options - Request options
61
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
58
62
  * @returns The created webhook object.
59
63
  * @see https://docs.apify.com/api/v2/webhooks-post
60
64
  */
61
- create(webhook?: WebhookUpdateData): Promise<Webhook>;
65
+ create(webhook?: WebhookUpdateData, options?: TimeoutOptions): Promise<WebhookResource>;
62
66
  }
63
- export interface WebhookCollectionListOptions extends PaginationOptions {
67
+ export interface WebhookCollectionListOptions extends PaginationOptions, TimeoutOptions {
64
68
  desc?: boolean;
65
69
  }
@@ -1,10 +1,12 @@
1
1
  import { z } from 'zod';
2
2
  import { ResourceCollectionClient } from '../base/resource_collection_client.js';
3
3
  import * as schemas from '../schemas.js';
4
+ import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
4
5
  import { anyObjectSchema, paginationOptionsShape, parseArgument } from '../utils.js';
5
6
  const listOptionsSchema = z.strictObject({
6
7
  ...paginationOptionsShape,
7
8
  desc: z.boolean().optional(),
9
+ ...timeoutOptionsShape,
8
10
  });
9
11
  const webhookCreateSchema = anyObjectSchema.optional();
10
12
  /**
@@ -57,22 +59,26 @@ export class WebhookCollectionClient extends ResourceCollectionClient {
57
59
  * ```
58
60
  *
59
61
  * @param options - Pagination and sorting options.
62
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
60
63
  * @returns A paginated iterator of webhooks.
61
64
  * @see https://docs.apify.com/api/v2/webhooks-get
62
65
  */
63
66
  list(options = {}) {
64
67
  const parsed = parseArgument(options, listOptionsSchema, 'WebhookCollectionListOptions');
65
- return this._listPaginated(schemas.ListOfWebhooks(), parsed);
68
+ return this.listResourcesPaginated(schemas.ListOfWebhooks(), parsed, 'medium');
66
69
  }
67
70
  /**
68
71
  * Creates a new webhook.
69
72
  *
70
73
  * @param webhook - The webhook data.
74
+ * @param options - Request options
75
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
71
76
  * @returns The created webhook object.
72
77
  * @see https://docs.apify.com/api/v2/webhooks-post
73
78
  */
74
- async create(webhook) {
79
+ async create(webhook, options = {}) {
75
80
  parseArgument(webhook, webhookCreateSchema);
76
- return this._create(schemas.Webhook(), webhook);
81
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
82
+ return this.createResource(schemas.WebhookResource(), webhook, timeoutSecs, signal);
77
83
  }
78
84
  }
@@ -1,6 +1,7 @@
1
1
  import type { ApiClientSubResourceOptions } from '../base/api_client.js';
2
2
  import { ResourceClient } from '../base/resource_client.js';
3
3
  import type { WebhookDispatch } from '../models.js';
4
+ import type { TimeoutOptions } from '../timeouts.js';
4
5
  export type { WebhookDispatch, WebhookDispatchCall, WebhookDispatchEventData, WebhookDispatchWebhookSummary, } from '../models.js';
5
6
  export { WebhookDispatchStatus } from '../models.js';
6
7
  /**
@@ -29,8 +30,10 @@ export declare class WebhookDispatchClient extends ResourceClient {
29
30
  /**
30
31
  * Retrieves the webhook dispatch.
31
32
  *
33
+ * @param options - Request options
34
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
32
35
  * @returns The webhook dispatch object, or `undefined` if it does not exist.
33
36
  * @see https://docs.apify.com/api/v2/webhook-dispatch-get
34
37
  */
35
- get(): Promise<WebhookDispatch | undefined>;
38
+ get(options?: TimeoutOptions): Promise<WebhookDispatch | undefined>;
36
39
  }
@@ -1,5 +1,7 @@
1
1
  import { ResourceClient } from '../base/resource_client.js';
2
2
  import * as schemas from '../schemas.js';
3
+ import { timeoutOptionsSchema } from '../timeouts.js';
4
+ import { parseArgument } from '../utils.js';
3
5
  export { WebhookDispatchStatus } from '../models.js';
4
6
  /**
5
7
  * Client for managing a specific webhook dispatch.
@@ -32,10 +34,13 @@ export class WebhookDispatchClient extends ResourceClient {
32
34
  /**
33
35
  * Retrieves the webhook dispatch.
34
36
  *
37
+ * @param options - Request options
38
+ * @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
35
39
  * @returns The webhook dispatch object, or `undefined` if it does not exist.
36
40
  * @see https://docs.apify.com/api/v2/webhook-dispatch-get
37
41
  */
38
- async get() {
39
- return this._get(schemas.WebhookDispatch());
42
+ async get(options = {}) {
43
+ const { timeoutSecs = 'short', signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
44
+ return this.getResource(schemas.WebhookDispatch(), {}, timeoutSecs, signal);
40
45
  }
41
46
  }
@@ -1,5 +1,6 @@
1
1
  import type { ApiClientSubResourceOptions } from '../base/api_client.js';
2
2
  import { ResourceCollectionClient } from '../base/resource_collection_client.js';
3
+ import type { TimeoutOptions } from '../timeouts.js';
3
4
  import type { PaginatedIterator, PaginationOptions } from '../utils.js';
4
5
  import type { WebhookDispatch } from './webhook_dispatch.js';
5
6
  /**
@@ -42,11 +43,12 @@ export declare class WebhookDispatchCollectionClient extends ResourceCollectionC
42
43
  * ```
43
44
  *
44
45
  * @param options - Pagination and sorting options.
46
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
45
47
  * @returns A paginated iterator of webhook dispatches.
46
48
  * @see https://docs.apify.com/api/v2/webhook-dispatches-get
47
49
  */
48
50
  list(options?: WebhookDispatchCollectionListOptions): PaginatedIterator<WebhookDispatch>;
49
51
  }
50
- export interface WebhookDispatchCollectionListOptions extends PaginationOptions {
52
+ export interface WebhookDispatchCollectionListOptions extends PaginationOptions, TimeoutOptions {
51
53
  desc?: boolean;
52
54
  }
@@ -1,10 +1,12 @@
1
1
  import { z } from 'zod';
2
2
  import { ResourceCollectionClient } from '../base/resource_collection_client.js';
3
3
  import * as schemas from '../schemas.js';
4
+ import { timeoutOptionsShape } from '../timeouts.js';
4
5
  import { paginationOptionsShape, parseArgument } from '../utils.js';
5
6
  const listOptionsSchema = z.strictObject({
6
7
  ...paginationOptionsShape,
7
8
  desc: z.boolean().optional(),
9
+ ...timeoutOptionsShape,
8
10
  });
9
11
  /**
10
12
  * Client for managing the collection of webhook dispatches.
@@ -51,11 +53,12 @@ export class WebhookDispatchCollectionClient extends ResourceCollectionClient {
51
53
  * ```
52
54
  *
53
55
  * @param options - Pagination and sorting options.
56
+ * @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
54
57
  * @returns A paginated iterator of webhook dispatches.
55
58
  * @see https://docs.apify.com/api/v2/webhook-dispatches-get
56
59
  */
57
60
  list(options = {}) {
58
61
  const parsed = parseArgument(options, listOptionsSchema, 'WebhookDispatchCollectionListOptions');
59
- return this._listPaginated(schemas.ListOfWebhookDispatches(), parsed);
62
+ return this.listResourcesPaginated(schemas.ListOfWebhookDispatches(), parsed, 'medium');
60
63
  }
61
64
  }
@@ -1,4 +1,5 @@
1
1
  import type { z } from 'zod';
2
+ import { ApifyClientError } from './apify_client_error.js';
2
3
  /**
3
4
  * Thrown when an API response does not match the schema the client expects for it.
4
5
  *
@@ -12,7 +13,7 @@ import type { z } from 'zod';
12
13
  * {@link https://zod.dev | zod} issues are available on `issues`, and the original `ZodError` on `cause`,
13
14
  * for programmatic inspection.
14
15
  */
15
- export declare class ResponseValidationError extends Error {
16
+ export declare class ResponseValidationError extends ApifyClientError {
16
17
  /** Structured issues from the underlying schema check. */
17
18
  readonly issues: z.ZodError['issues'];
18
19
  /** HTTP method of the request whose response failed validation, upper-cased. */
@@ -1,4 +1,5 @@
1
1
  import { ArgumentValidationError } from '@apify/validations';
2
+ import { ApifyClientError } from './apify_client_error.js';
2
3
  const REPORT_HINT = 'The API returned something its OpenAPI specification does not describe. ' +
3
4
  'Please report this at https://github.com/apify/apify-client-js/issues.';
4
5
  /**
@@ -14,7 +15,7 @@ const REPORT_HINT = 'The API returned something its OpenAPI specification does n
14
15
  * {@link https://zod.dev | zod} issues are available on `issues`, and the original `ZodError` on `cause`,
15
16
  * for programmatic inspection.
16
17
  */
17
- export class ResponseValidationError extends Error {
18
+ export class ResponseValidationError extends ApifyClientError {
18
19
  /** Structured issues from the underlying schema check. */
19
20
  issues;
20
21
  /** HTTP method of the request whose response failed validation, upper-cased. */
@@ -0,0 +1,6 @@
1
+ import type { Runtime } from './types.js';
2
+ /**
3
+ * The Node.js implementation of {@link Runtime}. Deno and Bun resolve the `node` condition too and
4
+ * provide the built-ins it uses.
5
+ */
6
+ export declare const runtime: Runtime;
@@ -0,0 +1,80 @@
1
+ import os from 'node:os';
2
+ import { Readable } from 'node:stream';
3
+ import { brotliCompress, constants, gzip } from 'node:zlib';
4
+ /**
5
+ * The Node.js implementation of {@link Runtime}. Deno and Bun resolve the `node` condition too and
6
+ * provide the built-ins it uses.
7
+ */
8
+ export const runtime = {
9
+ isNode: true,
10
+ platform: `${os.platform()}; Node/${process.version}`,
11
+ async compress(data, { algorithm, quality }) {
12
+ return new Promise((resolve, reject) => {
13
+ const done = (error, result) => (error ? reject(error) : resolve(result));
14
+ if (algorithm === 'br') {
15
+ brotliCompress(data, { params: { [constants.BROTLI_PARAM_QUALITY]: quality } }, done);
16
+ }
17
+ else {
18
+ gzip(data, { level: quality }, done);
19
+ }
20
+ });
21
+ },
22
+ async createHttpAgents({ timeoutMillis }) {
23
+ // Loaded on the first request rather than with the client: the proxy support pulls in a sizeable
24
+ // dependency tree that a client which never sends a request should not pay for.
25
+ const { ProxyAgent } = await import('proxy-agent');
26
+ // We want to keep sockets alive for better performance.
27
+ const agentOptions = {
28
+ keepAlive: true,
29
+ // Timeout for inactive sockets
30
+ // Prevents socket leaks from idle connections
31
+ timeout: timeoutMillis,
32
+ // Keep alive timeout for free sockets (15 seconds)
33
+ // Node.js will close unused sockets after this period
34
+ keepAliveMsecs: 15_000,
35
+ // Maximum number of sockets per host
36
+ maxSockets: 256,
37
+ maxFreeSockets: 256,
38
+ // LIFO scheduling - reuse most recently used sockets for better performance
39
+ scheduling: 'lifo',
40
+ };
41
+ // ProxyAgent picks the proxy up from the environment variables and supports CONNECT tunneling.
42
+ const agent = new ProxyAgent(agentOptions);
43
+ // Disable Nagle's algorithm for lower latency
44
+ // This sends data immediately instead of buffering small packets
45
+ agent.on('socket', (socket) => {
46
+ socket.setNoDelay(true);
47
+ });
48
+ return { httpAgent: agent, httpsAgent: agent };
49
+ },
50
+ openBlobBody(blob, onSourceError) {
51
+ return Readable.from(readBlob(blob, onSourceError), { objectMode: false });
52
+ },
53
+ };
54
+ /**
55
+ * Yields the bytes of a blob. Ending the iteration early, which destroying the `Readable` around it does, cancels
56
+ * the read and releases a file the blob reads from. Only a failing read is reported: an error the `Readable` is
57
+ * destroyed with is thrown in at the `yield` and belongs to the transport. A `for await` loop would surface both at
58
+ * the same place, so the loop calls `next()` itself to catch only the read.
59
+ */
60
+ async function* readBlob(blob, onSourceError) {
61
+ const chunks = blob.stream()[Symbol.asyncIterator]();
62
+ try {
63
+ while (true) {
64
+ let chunk;
65
+ try {
66
+ chunk = await chunks.next();
67
+ }
68
+ catch (error) {
69
+ onSourceError(error);
70
+ throw error;
71
+ }
72
+ if (chunk.done)
73
+ return;
74
+ yield chunk.value;
75
+ }
76
+ }
77
+ finally {
78
+ await chunks.return?.();
79
+ }
80
+ }
@@ -0,0 +1,60 @@
1
+ import type http from 'node:http';
2
+ import type https from 'node:https';
3
+ import type { Readable } from 'node:stream';
4
+ /**
5
+ * What to compress a request body with, as the built-in `HttpCompressor` implementations ask for it.
6
+ * @internal
7
+ */
8
+ export interface CompressionOptions {
9
+ /** Algorithm to compress with, named by the `Content-Encoding` value it produces. */
10
+ algorithm: 'br' | 'gzip';
11
+ /** Compression level, in the range the algorithm defines. */
12
+ quality: number;
13
+ }
14
+ /**
15
+ * The agents axios's Node.js adapter sends requests through.
16
+ * @internal
17
+ */
18
+ export interface HttpAgents {
19
+ httpAgent: http.Agent;
20
+ httpsAgent: https.Agent;
21
+ }
22
+ /**
23
+ * The runtime-specific part of the client. `#runtime` resolves to the Node.js implementation under the `node`
24
+ * condition and to the Web API one everywhere else (see the `imports` field of `package.json`), so a bundler
25
+ * targeting a browser or an edge runtime never sees the Node.js built-ins the Node.js one uses.
26
+ * @internal
27
+ */
28
+ export interface Runtime {
29
+ /**
30
+ * Whether the Node.js implementation was selected, which the `node` condition settles when `#runtime` is
31
+ * resolved - so a Node.js application bundled for a browser or a neutral target reports `false`. Log
32
+ * streaming, the `stream` record option and request body compression are gated on it.
33
+ */
34
+ isNode: boolean;
35
+ /**
36
+ * The platform part of the `User-Agent` header, or `undefined` where the runtime does not let a client
37
+ * set that header (browsers).
38
+ */
39
+ platform: string | undefined;
40
+ /**
41
+ * Compresses a request body. The built-in compressors go through it, so a bundle for a browser or an edge
42
+ * runtime carries no `node:zlib`. Throws where the runtime has no compression, which the client never
43
+ * reaches, since it compresses only where {@link isNode} holds.
44
+ */
45
+ compress(data: Uint8Array, options: CompressionOptions): Promise<Buffer>;
46
+ /**
47
+ * Creates the agents axios's Node.js adapter sends requests through, or resolves to `undefined` where
48
+ * axios does not use agents.
49
+ */
50
+ createHttpAgents(options: {
51
+ timeoutMillis: number;
52
+ }): Promise<HttpAgents | undefined>;
53
+ /**
54
+ * Opens a `Blob` request body for one attempt. The Node.js implementation reads the blob into a `Readable` and
55
+ * reports an error the blob raises while it is read to `onSourceError` before the stream fails with it, so the
56
+ * pipeline can tell a failing source apart from a failing connection. The Web API one returns the blob itself,
57
+ * which `fetch` and `XMLHttpRequest` read on their own, and never calls `onSourceError`.
58
+ */
59
+ openBlobBody(blob: Blob, onSourceError: (error: unknown) => void): Readable | Blob;
60
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { Runtime } from './types.js';
2
+ /**
3
+ * The {@link Runtime} for browsers and edge runtimes, built on Web APIs only.
4
+ */
5
+ export declare const runtime: Runtime;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The {@link Runtime} for browsers and edge runtimes, built on Web APIs only.
3
+ */
4
+ export const runtime = {
5
+ isNode: false,
6
+ // Browsers do not let a page set the `User-Agent` header.
7
+ platform: undefined,
8
+ // No request compression: brotli and gzip have no Web API, and a `content-encoding` request header is not
9
+ // on the list of headers the Apify API allows in a cross-origin request, so a browser would fail the
10
+ // preflight. The client compresses in Node.js alone, so nothing reaches this.
11
+ async compress() {
12
+ throw new Error('Request body compression is only available in Node.js.');
13
+ },
14
+ // The XHR and fetch adapters of axios do not use agents.
15
+ async createHttpAgents() {
16
+ return undefined;
17
+ },
18
+ openBlobBody(blob) {
19
+ return blob;
20
+ },
21
+ };