apify-client 2.23.5-beta.24 → 2.23.5-beta.26

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.
@@ -122,7 +122,7 @@ class HttpClient {
122
122
  this.axios.defaults.httpsAgent = this.httpsAgent;
123
123
  // Works only in Node. Cannot be set in browser
124
124
  const isAtHome = !!process.env[consts_1.APIFY_ENV_VARS.IS_AT_HOME];
125
- let userAgent = `ApifyClient/${version} (${os.type()}; Node/${process.version}); isAtHome/${isAtHome}`;
125
+ let userAgent = `ApifyClient/${version} (${os.platform()}; Node/${process.version}); isAtHome/${isAtHome}`;
126
126
  if (this.userAgentSuffix) {
127
127
  userAgent += `; ${(0, utils_1.asArray)(this.userAgentSuffix).join('; ')}`;
128
128
  }
@@ -69,14 +69,14 @@ function stringifyWithFunctions(obj) {
69
69
  return typeof value === 'function' ? value.toString() : value;
70
70
  });
71
71
  }
72
- async function maybeGzipRequest(config) {
72
+ async function maybeCompressRequest(config) {
73
73
  if (config.headers?.['content-encoding'])
74
74
  return config;
75
- const maybeZippedData = await (0, utils_1.maybeGzipValue)(config.data);
76
- if (maybeZippedData) {
75
+ const maybeCompressed = await (0, utils_1.maybeCompressValue)(config.data);
76
+ if (maybeCompressed) {
77
77
  config.headers ??= {};
78
- config.headers['content-encoding'] = 'gzip';
79
- config.data = maybeZippedData;
78
+ config.headers['content-encoding'] = maybeCompressed.encoding;
79
+ config.data = maybeCompressed.data;
80
80
  }
81
81
  return config;
82
82
  }
@@ -103,7 +103,7 @@ function parseResponseData(response) {
103
103
  return response;
104
104
  }
105
105
  exports.requestInterceptors = [
106
- maybeGzipRequest,
106
+ maybeCompressRequest,
107
107
  serializeRequest,
108
108
  ensureHeadersPrototype,
109
109
  ];
@@ -149,7 +149,7 @@ export declare class ActorClient extends ResourceClient {
149
149
  * (e.g., `'latest'` or `'1.2.345'`). If not provided, uses the default build.
150
150
  * @param options.contentType - Content type of the input. If specified, input must be a string or Buffer.
151
151
  * @returns `true` if the input is valid. Invalid input causes the underlying API call to throw an `ApifyApiError`.
152
- * @see https://docs.apify.com/api/v2/act-input-validate-post
152
+ * @see https://docs.apify.com/api/v2/act-validate-input-post
153
153
  *
154
154
  * @example
155
155
  * ```javascript
@@ -234,7 +234,7 @@ class ActorClient extends resource_client_1.ResourceClient {
234
234
  * (e.g., `'latest'` or `'1.2.345'`). If not provided, uses the default build.
235
235
  * @param options.contentType - Content type of the input. If specified, input must be a string or Buffer.
236
236
  * @returns `true` if the input is valid. Invalid input causes the underlying API call to throw an `ApifyApiError`.
237
- * @see https://docs.apify.com/api/v2/act-input-validate-post
237
+ * @see https://docs.apify.com/api/v2/act-validate-input-post
238
238
  *
239
239
  * @example
240
240
  * ```javascript
package/dist/utils.d.ts CHANGED
@@ -41,10 +41,15 @@ export declare function parseDateFields(input: JsonValue, shouldParseField?: ((k
41
41
  * Helper function that converts array of webhooks to base64 string
42
42
  */
43
43
  export declare function stringifyWebhooksToBase64(webhooks: WebhookUpdateData[]): string | undefined;
44
+ export interface CompressedValue {
45
+ data: Buffer;
46
+ encoding: 'br' | 'gzip';
47
+ }
44
48
  /**
45
- * Gzip provided value, otherwise returns undefined.
49
+ * Compress the passed value using brotli if available or using gzip as a fallback. Returns undefined
50
+ * if the data is too small / wrong type.
46
51
  */
47
- export declare function maybeGzipValue(value: unknown): Promise<Buffer | undefined>;
52
+ export declare function maybeCompressValue(value: unknown): Promise<CompressedValue | undefined>;
48
53
  /**
49
54
  * Helper function slice the items from array to fit the max byte length.
50
55
  */
package/dist/utils.js CHANGED
@@ -5,7 +5,7 @@ exports.pluckData = pluckData;
5
5
  exports.catchNotFoundOrThrow = catchNotFoundOrThrow;
6
6
  exports.parseDateFields = parseDateFields;
7
7
  exports.stringifyWebhooksToBase64 = stringifyWebhooksToBase64;
8
- exports.maybeGzipValue = maybeGzipValue;
8
+ exports.maybeCompressValue = maybeCompressValue;
9
9
  exports.sliceArrayByByteLength = sliceArrayByByteLength;
10
10
  exports.isNode = isNode;
11
11
  exports.isBuffer = isBuffer;
@@ -19,7 +19,7 @@ const ow_1 = tslib_1.__importDefault(require("ow"));
19
19
  const NOT_FOUND_STATUS_CODE = 404;
20
20
  const RECORD_NOT_FOUND_TYPE = 'record-not-found';
21
21
  const RECORD_OR_TOKEN_NOT_FOUND_TYPE = 'record-or-token-not-found';
22
- const MIN_GZIP_BYTES = 1024;
22
+ const MIN_COMPRESS_BYTES = 1024;
23
23
  /**
24
24
  * Returns object's 'data' property or throws if parameter is not an object,
25
25
  * or an object without a 'data' property.
@@ -99,26 +99,59 @@ function stringifyWebhooksToBase64(webhooks) {
99
99
  }
100
100
  let gzipPromisified;
101
101
  /**
102
- * Gzip provided value, otherwise returns undefined.
102
+ * Gzip-compress the provided value.
103
103
  */
104
- async function maybeGzipValue(value) {
105
- if (!isNode())
106
- return;
107
- if (typeof value !== 'string' && !Buffer.isBuffer(value))
108
- return;
109
- // Request compression is not that important so let's
110
- // skip it instead of throwing for unsupported types.
111
- const areDataLargeEnough = Buffer.byteLength(value) >= MIN_GZIP_BYTES;
112
- if (areDataLargeEnough) {
113
- if (!gzipPromisified) {
114
- const { promisify } = await import('node:util');
115
- const { gzip } = await import('node:zlib');
116
- gzipPromisified = promisify(gzip);
104
+ async function gzipValue(value) {
105
+ if (!gzipPromisified) {
106
+ const { promisify } = await import('node:util');
107
+ const { gzip } = await import('node:zlib');
108
+ gzipPromisified = promisify(gzip);
109
+ }
110
+ return gzipPromisified(value);
111
+ }
112
+ // null = confirmed unavailable; undefined = not yet checked
113
+ let brotliCompressPromisified;
114
+ /**
115
+ * Brotli-compress the provided value, or return undefined if brotli is unavailable
116
+ * (Node.js < v10.16.0), this is a strict defensive guard.
117
+ */
118
+ async function maybeBrotliValue(value) {
119
+ if (brotliCompressPromisified === undefined) {
120
+ const { promisify } = await import('node:util');
121
+ const { brotliCompress, constants } = await import('node:zlib');
122
+ if (typeof brotliCompress === 'function') {
123
+ const compress = promisify(brotliCompress);
124
+ brotliCompressPromisified = async (value) => compress(value, { params: { [constants.BROTLI_PARAM_QUALITY]: 6 } });
117
125
  }
118
- return gzipPromisified(value);
126
+ else {
127
+ brotliCompressPromisified = null;
128
+ }
129
+ }
130
+ if (brotliCompressPromisified !== null) {
131
+ return brotliCompressPromisified(value);
119
132
  }
120
133
  return undefined;
121
134
  }
135
+ /**
136
+ * Compress the passed value using brotli if available or using gzip as a fallback. Returns undefined
137
+ * if the data is too small / wrong type.
138
+ */
139
+ async function maybeCompressValue(value) {
140
+ if (!isNode())
141
+ return undefined;
142
+ // Request compression is not that important so let's
143
+ // skip it instead of throwing for unsupported types.
144
+ if (typeof value !== 'string' && !Buffer.isBuffer(value))
145
+ return undefined;
146
+ const areDataLargeEnough = Buffer.byteLength(value) >= MIN_COMPRESS_BYTES;
147
+ if (!areDataLargeEnough)
148
+ return undefined;
149
+ const brotli = await maybeBrotliValue(value);
150
+ if (brotli)
151
+ return { data: brotli, encoding: 'br' };
152
+ const gzipped = await gzipValue(value);
153
+ return { data: gzipped, encoding: 'gzip' };
154
+ }
122
155
  /**
123
156
  * Helper function slice the items from array to fit the max byte length.
124
157
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apify-client",
3
- "version": "2.23.5-beta.24",
3
+ "version": "2.23.5-beta.26",
4
4
  "description": "Apify API client for JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",