apify-client 3.0.0-beta.96 → 3.0.0-beta.98

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.
@@ -384,3 +384,8 @@ export declare abstract class HttpClient {
384
384
  */
385
385
  protected computeTimeoutMillis(attempt: number, timeoutSecs?: Timeout): number | undefined;
386
386
  }
387
+ /**
388
+ * Merges two header records, header names compared case-insensitively. A header from `override` replaces a
389
+ * same-named header in `base` whatever the casing of either, and keeps the casing it was passed with.
390
+ */
391
+ export declare function mergeHeaders(base: Record<string, string>, override?: Record<string, string>): Record<string, string>;
@@ -481,7 +481,7 @@ function getHeader(headers, name) {
481
481
  * Merges two header records, header names compared case-insensitively. A header from `override` replaces a
482
482
  * same-named header in `base` whatever the casing of either, and keeps the casing it was passed with.
483
483
  */
484
- function mergeHeaders(base, override) {
484
+ export function mergeHeaders(base, override) {
485
485
  const merged = { ...base };
486
486
  for (const [key, value] of Object.entries(override ?? {})) {
487
487
  for (const existing of Object.keys(merged)) {
@@ -4,3 +4,5 @@ export type { BrotliHttpCompressorOptions } from './brotli.js';
4
4
  export { GzipHttpCompressor } from './gzip.js';
5
5
  export type { GzipHttpCompressorOptions } from './gzip.js';
6
6
  export type { HttpCompressionAlgorithm } from './resolve.js';
7
+ export { ZstdHttpCompressor } from './zstd.js';
8
+ export type { ZstdHttpCompressorOptions } from './zstd.js';
@@ -1,2 +1,3 @@
1
1
  export { BrotliHttpCompressor } from './brotli.js';
2
2
  export { GzipHttpCompressor } from './gzip.js';
3
+ export { ZstdHttpCompressor } from './zstd.js';
@@ -3,10 +3,10 @@ import type { HttpCompressor } from './base.js';
3
3
  /**
4
4
  * Compression algorithms the `compression` option of {@link ApifyClient} accepts by name. Each name selects the
5
5
  * matching built-in compressor with its default quality: `'brotli'` a {@link BrotliHttpCompressor}, `'gzip'` a
6
- * {@link GzipHttpCompressor}. The Apify API also accepts `deflate` and `identity`, which a custom
7
- * {@link HttpCompressor} covers.
6
+ * {@link GzipHttpCompressor}, `'zstd'` a {@link ZstdHttpCompressor}. The Apify API also accepts `deflate` and
7
+ * `identity`, which a custom {@link HttpCompressor} covers.
8
8
  */
9
- export type HttpCompressionAlgorithm = 'brotli' | 'gzip';
9
+ export type HttpCompressionAlgorithm = 'brotli' | 'gzip' | 'zstd';
10
10
  /**
11
11
  * Schema of the `compression` option: an algorithm name or an {@link HttpCompressor}.
12
12
  * @internal
@@ -14,6 +14,7 @@ export type HttpCompressionAlgorithm = 'brotli' | 'gzip';
14
14
  export declare const compressionSchema: z.ZodUnion<readonly [z.ZodEnum<{
15
15
  brotli: "brotli";
16
16
  gzip: "gzip";
17
+ zstd: "zstd";
17
18
  }>, z.ZodCustom<HttpCompressor, HttpCompressor>]>;
18
19
  /**
19
20
  * Turns the `compression` option into a ready-to-use {@link HttpCompressor}: a name into the matching built-in
@@ -2,12 +2,13 @@ import { z } from 'zod';
2
2
  import { httpCompressorSchema } from './base.js';
3
3
  import { BrotliHttpCompressor } from './brotli.js';
4
4
  import { GzipHttpCompressor } from './gzip.js';
5
+ import { ZstdHttpCompressor } from './zstd.js';
5
6
  /**
6
7
  * Schema of the `compression` option: an algorithm name or an {@link HttpCompressor}.
7
8
  * @internal
8
9
  */
9
10
  export const compressionSchema = z.union([
10
- z.enum(['brotli', 'gzip']),
11
+ z.enum(['brotli', 'gzip', 'zstd']),
11
12
  httpCompressorSchema,
12
13
  ]);
13
14
  /**
@@ -23,5 +24,7 @@ export function resolveCompressor(compression) {
23
24
  return new GzipHttpCompressor();
24
25
  case 'brotli':
25
26
  return new BrotliHttpCompressor();
27
+ case 'zstd':
28
+ return new ZstdHttpCompressor();
26
29
  }
27
30
  }
@@ -0,0 +1,34 @@
1
+ import type { HttpCompressor } from './base.js';
2
+ /**
3
+ * Compresses request bodies using zstd.
4
+ *
5
+ * Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
6
+ *
7
+ * @example
8
+ * ```javascript
9
+ * import { ApifyClient, ZstdHttpCompressor } from 'apify-client';
10
+ *
11
+ * const client = new ApifyClient({ token: 'my-token', compression: new ZstdHttpCompressor({ quality: 19 }) });
12
+ * ```
13
+ */
14
+ export declare class ZstdHttpCompressor implements HttpCompressor {
15
+ #private;
16
+ readonly contentEncoding = "zstd";
17
+ /**
18
+ * @param options - Compressor options.
19
+ * @throws {ArgumentValidationError} If `quality` is out of the valid range.
20
+ */
21
+ constructor(options?: ZstdHttpCompressorOptions);
22
+ compress(data: Buffer): Promise<Buffer>;
23
+ }
24
+ /**
25
+ * Options for {@link ZstdHttpCompressor}.
26
+ */
27
+ export interface ZstdHttpCompressorOptions {
28
+ /**
29
+ * Compression level, from `1` (the fastest) to `19` (the best compression). zstd's levels 20 to 22 are not
30
+ * accepted: their window exceeds the 8 MB that HTTP decoders are required to support.
31
+ * @default 3
32
+ */
33
+ quality?: number;
34
+ }
@@ -0,0 +1,43 @@
1
+ import { z } from 'zod';
2
+ import { runtime } from '#runtime';
3
+ import { parseArgument } from '../utils.js';
4
+ /** Lowest valid zstd quality, the fastest with the least compression. */
5
+ const MIN_QUALITY = 1;
6
+ /**
7
+ * Highest quality the compressor accepts. zstd itself goes up to 22, but levels 20 to 22 use a window above the 8 MB
8
+ * that RFC 9659 requires HTTP decoders to support, so a record stored at those levels could fail to decode in a
9
+ * browser that accepts zstd. Level 19 is the last one within the limit.
10
+ */
11
+ const MAX_QUALITY = 19;
12
+ /** zstd's own default. Fast, with a ratio close to gzip at its best level. */
13
+ const DEFAULT_QUALITY = 3;
14
+ const optionsSchema = z.strictObject({
15
+ quality: z.int().min(MIN_QUALITY).max(MAX_QUALITY).default(DEFAULT_QUALITY),
16
+ });
17
+ /**
18
+ * Compresses request bodies using zstd.
19
+ *
20
+ * Built on the `node:zlib` module, so it works wherever the client runs on Node.js.
21
+ *
22
+ * @example
23
+ * ```javascript
24
+ * import { ApifyClient, ZstdHttpCompressor } from 'apify-client';
25
+ *
26
+ * const client = new ApifyClient({ token: 'my-token', compression: new ZstdHttpCompressor({ quality: 19 }) });
27
+ * ```
28
+ */
29
+ export class ZstdHttpCompressor {
30
+ contentEncoding = 'zstd';
31
+ #quality;
32
+ /**
33
+ * @param options - Compressor options.
34
+ * @throws {ArgumentValidationError} If `quality` is out of the valid range.
35
+ */
36
+ constructor(options = {}) {
37
+ const { quality } = parseArgument(options, optionsSchema, 'ZstdHttpCompressorOptions');
38
+ this.#quality = quality;
39
+ }
40
+ async compress(data) {
41
+ return runtime.compress(data, { algorithm: 'zstd', quality: this.#quality });
42
+ }
43
+ }
@@ -85,6 +85,8 @@ export declare class ActorServerClient extends ResourceClient {
85
85
  * @param options.headers - Additional headers of the request.
86
86
  * @param options.data - Request body, serialized the same way as for API calls: a plain object or array is sent
87
87
  * as JSON, and a string or binary value as it is.
88
+ * @param options.parentRequestId - The `X-Actor-Request-ID` header of the request your Server Actor is handling,
89
+ * sent on as the same header so the called Actor charges at that request's pricing tier.
88
90
  * @param options.compression - Compression algorithm for the request body, as a name or an {@link HttpCompressor}.
89
91
  * The body is compressed under the same conditions as for API calls. Set it only if the Actor's server decodes
90
92
  * the resulting `Content-Encoding`.
@@ -127,6 +129,12 @@ export interface ActorServerRequestOptions extends TimeoutOptions {
127
129
  * or binary value as it is.
128
130
  */
129
131
  data?: unknown;
132
+ /**
133
+ * The `X-Actor-Request-ID` header of the request your Server Actor is handling. It's sent on as the same header,
134
+ * so the called Actor charges at that request's pricing tier.
135
+ * @see https://docs.apify.com/platform/actors/development/programming-interface/standby#compose-server-actors
136
+ */
137
+ parentRequestId?: string;
130
138
  /**
131
139
  * Compression algorithm for the request body, as a name or an {@link HttpCompressor}. The body is sent
132
140
  * uncompressed when it's not set, since the Actor's server may not decode a compressed body.
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { ApifyClientError } from '../apify_client_error.js';
3
3
  import { ResourceClient } from '../base/resource_client.js';
4
+ import { mergeHeaders } from '../http_clients/base.js';
4
5
  import { compressionSchema, resolveCompressor } from '../http_compressors/resolve.js';
5
6
  import * as schemas from '../schemas.js';
6
7
  import { timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
@@ -38,6 +39,7 @@ const requestOptionsSchema = z.strictObject({
38
39
  params: z.record(z.string(), z.unknown()).optional(),
39
40
  headers: z.record(z.string(), z.string()).optional(),
40
41
  data: z.unknown().optional(),
42
+ parentRequestId: z.string().optional(),
41
43
  compression: compressionSchema.optional(),
42
44
  responseType: z.enum(['parsed', 'buffer', 'stream']).optional(),
43
45
  ...timeoutOptionsShape,
@@ -151,7 +153,7 @@ export class ActorServerClient extends ResourceClient {
151
153
  }
152
154
  async request(path = '/', options = {}) {
153
155
  parseArgument(path, z.string());
154
- const { method = 'GET', params, headers, data, compression, responseType, timeoutSecs = 'long', signal, } = parseArgument(options, requestOptionsSchema, 'ActorServerRequestOptions');
156
+ const { method = 'GET', params, headers, data, parentRequestId, compression, responseType, timeoutSecs = 'long', signal, } = parseArgument(options, requestOptionsSchema, 'ActorServerRequestOptions');
155
157
  if (responseType === 'stream' && !runtime.isNode) {
156
158
  throw new Error("The 'stream' response type can only be used in Node.js environment.");
157
159
  }
@@ -161,7 +163,9 @@ export class ActorServerClient extends ResourceClient {
161
163
  url: `${serverUrl.replace(/\/+$/, '')}/${serverPath}`,
162
164
  method,
163
165
  params,
164
- headers,
166
+ headers: parentRequestId === undefined
167
+ ? headers
168
+ : mergeHeaders(headers ?? {}, { 'X-Actor-Request-ID': parentRequestId }),
165
169
  data,
166
170
  compression: compression === undefined ? false : resolveCompressor(compression),
167
171
  authorizationHeader: 'X-Apify-Authorization',
@@ -232,12 +232,12 @@ export declare class KeyValueStoreClient extends ResourceClient {
232
232
  * @param record.contentEncoding - Optional encoding already applied to `value`, sent as the `Content-Encoding`
233
233
  * header. Set it to upload a pre-compressed value: the client then sends the
234
234
  * bytes as they are and does not compress them again. The API accepts `gzip`,
235
- * `br`, `deflate`, and `identity`, and stores the record exactly as uploaded, so
236
- * this is also the encoding the record is served with. Only binary values and
237
- * streams can carry a compression, so any encoding other than `identity` throws
238
- * a `TypeError` when `value` is a string, or a plain object, array, or other
239
- * value that the client serializes to JSON. Node.js only, since browsers cannot
240
- * send the header to the API.
235
+ * `br`, `zstd`, `deflate`, and `identity`, and stores the record exactly as
236
+ * uploaded, so this is also the encoding the record is served with. Only binary
237
+ * values and streams can carry a compression, so any encoding other than
238
+ * `identity` throws a `TypeError` when `value` is a string, or a plain object,
239
+ * array, or other value that the client serializes to JSON. Node.js only, since
240
+ * browsers cannot send the header to the API.
241
241
  * @param options - Storage options
242
242
  * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
243
243
  * @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
@@ -386,12 +386,12 @@ export class KeyValueStoreClient extends ResourceClient {
386
386
  * @param record.contentEncoding - Optional encoding already applied to `value`, sent as the `Content-Encoding`
387
387
  * header. Set it to upload a pre-compressed value: the client then sends the
388
388
  * bytes as they are and does not compress them again. The API accepts `gzip`,
389
- * `br`, `deflate`, and `identity`, and stores the record exactly as uploaded, so
390
- * this is also the encoding the record is served with. Only binary values and
391
- * streams can carry a compression, so any encoding other than `identity` throws
392
- * a `TypeError` when `value` is a string, or a plain object, array, or other
393
- * value that the client serializes to JSON. Node.js only, since browsers cannot
394
- * send the header to the API.
389
+ * `br`, `zstd`, `deflate`, and `identity`, and stores the record exactly as
390
+ * uploaded, so this is also the encoding the record is served with. Only binary
391
+ * values and streams can carry a compression, so any encoding other than
392
+ * `identity` throws a `TypeError` when `value` is a string, or a plain object,
393
+ * array, or other value that the client serializes to JSON. Node.js only, since
394
+ * browsers cannot send the header to the API.
395
395
  * @param options - Storage options
396
396
  * @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
397
397
  * @param options.doNotRetryTimeouts - If `true`, don't retry on timeout errors. Default is `false`.
@@ -1,6 +1,6 @@
1
1
  import os from 'node:os';
2
2
  import { Readable } from 'node:stream';
3
- import { brotliCompress, constants, gzip } from 'node:zlib';
3
+ import { brotliCompress, constants, gzip, zstdCompress } from 'node:zlib';
4
4
  /**
5
5
  * The Node.js implementation of {@link Runtime}. Deno and Bun resolve the `node` condition too and
6
6
  * provide the built-ins it uses.
@@ -11,11 +11,18 @@ export const runtime = {
11
11
  async compress(data, { algorithm, quality }) {
12
12
  return new Promise((resolve, reject) => {
13
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);
14
+ switch (algorithm) {
15
+ case 'br':
16
+ brotliCompress(data, { params: { [constants.BROTLI_PARAM_QUALITY]: quality } }, done);
17
+ break;
18
+ case 'gzip':
19
+ gzip(data, { level: quality }, done);
20
+ break;
21
+ case 'zstd':
22
+ zstdCompress(data, { params: { [constants.ZSTD_c_compressionLevel]: quality } }, done);
23
+ break;
24
+ default:
25
+ reject(new Error(`Unrecognized compression algorithm: ${String(algorithm)}`));
19
26
  }
20
27
  });
21
28
  },
@@ -7,7 +7,7 @@ import type { Readable } from 'node:stream';
7
7
  */
8
8
  export interface CompressionOptions {
9
9
  /** Algorithm to compress with, named by the `Content-Encoding` value it produces. */
10
- algorithm: 'br' | 'gzip';
10
+ algorithm: 'br' | 'gzip' | 'zstd';
11
11
  /** Compression level, in the range the algorithm defines. */
12
12
  quality: number;
13
13
  }
@@ -5,7 +5,7 @@ export const runtime = {
5
5
  isNode: false,
6
6
  // Browsers do not let a page set the `User-Agent` header.
7
7
  platform: undefined,
8
- // No request compression: brotli and gzip have no Web API, and a `content-encoding` request header is not
8
+ // No request compression: brotli, gzip and zstd have no Web API, and a `content-encoding` request header is not
9
9
  // on the list of headers the Apify API allows in a cross-origin request, so a browser would fail the
10
10
  // preflight. The client compresses in Node.js alone, so nothing reaches this.
11
11
  async compress() {
package/dist/utils.d.ts CHANGED
@@ -73,8 +73,8 @@ export declare function toBytes(value: unknown): Uint8Array | undefined;
73
73
  /**
74
74
  * Decides whether a request body with the given content type is worth compressing.
75
75
  *
76
- * Images, audio, video and archives already carry their own compression. Running them through brotli or gzip
77
- * burns CPU, holds a second full copy of the body in memory, and usually produces output slightly larger than
76
+ * Images, audio, video and archives already carry their own compression. Running them through brotli, gzip or
77
+ * zstd burns CPU, holds a second full copy of the body in memory, and usually produces output slightly larger than
78
78
  * the input. Formats that are raw despite such a media type, for example `image/bmp` or `audio/wav`, are still
79
79
  * compressed. A body with no content type is assumed to be compressible.
80
80
  * @internal
package/dist/utils.js CHANGED
@@ -173,8 +173,8 @@ export function toBytes(value) {
173
173
  /**
174
174
  * Decides whether a request body with the given content type is worth compressing.
175
175
  *
176
- * Images, audio, video and archives already carry their own compression. Running them through brotli or gzip
177
- * burns CPU, holds a second full copy of the body in memory, and usually produces output slightly larger than
176
+ * Images, audio, video and archives already carry their own compression. Running them through brotli, gzip or
177
+ * zstd burns CPU, holds a second full copy of the body in memory, and usually produces output slightly larger than
178
178
  * the input. Formats that are raw despite such a media type, for example `image/bmp` or `audio/wav`, are still
179
179
  * compressed. A body with no content type is assumed to be compressible.
180
180
  * @internal
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "apify-client",
3
- "version": "3.0.0-beta.96",
3
+ "version": "3.0.0-beta.98",
4
4
  "description": "Apify API client for JavaScript",
5
5
  "engines": {
6
- "node": ">=22.13.0"
6
+ "node": ">=22.15.0"
7
7
  },
8
8
  "type": "module",
9
9
  "main": "dist/index.js",