apify-client 3.0.0-beta.20 → 3.0.0-beta.21

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/README.md CHANGED
@@ -145,13 +145,15 @@ property to get only a subset of results. Other props are also available, depend
145
145
 
146
146
  ## Bundled environments
147
147
 
148
- The package includes a pre-built browser bundle that is automatically resolved by bundlers targeting browser environments. You can also import it explicitly via
148
+ The package includes a pre-built browser bundle that bundlers targeting browsers resolve automatically. You can also import it explicitly:
149
149
 
150
150
  ```typescript
151
151
  import { ApifyClient } from 'apify-client/browser';
152
152
  ```
153
153
 
154
- For edge runtimes like Cloudflare Workers, you may need to enable Node compatibility (e.g. `node_compat = true` in `wrangler.toml`). Note that some Node-specific features (streaming, proxy support) are not available in the bundle.
154
+ Only two parts of the client need Node.js built-ins: the HTTP agents and request compression. The `node` condition selects the Node.js implementation of those, and every other target gets one built on Web APIs. Log streaming, proxy support, and request compression are only available in Node.js.
155
+
156
+ For details, see [Bundled environments](https://docs.apify.com/api/client/js/docs/concepts/bundled-environments).
155
157
 
156
158
  ## API Reference
157
159
 
@@ -1,4 +1,3 @@
1
- import { isomorphicBufferToString } from './body_parser.js';
2
1
  import { isBuffer } from './utils.js';
3
2
  /**
4
3
  * Examples of capturing groups for "...at ActorCollectionClient.listResources (/Users/..."
@@ -73,7 +72,7 @@ export class ApifyApiError extends Error {
73
72
  // A `forceBuffer` request (e.g. `downloadItems()`) and a failed streaming request, whose body `HttpClient`
74
73
  // has read into a buffer, both arrive unparsed. Parse the body here to get at the error.
75
74
  if (isBuffer(responseData)) {
76
- const body = isomorphicBufferToString(response.data, 'utf-8');
75
+ const body = new TextDecoder().decode(response.data);
77
76
  try {
78
77
  responseData = JSON.parse(body);
79
78
  }
@@ -27,7 +27,7 @@ import { WebhookDispatchClient } from './resource_clients/webhook_dispatch.js';
27
27
  import { WebhookDispatchCollectionClient } from './resource_clients/webhook_dispatch_collection.js';
28
28
  import { Statistics } from './statistics.js';
29
29
  import { DEFAULT_TIMEOUT_LONG_SECS, DEFAULT_TIMEOUT_MAX_SECS, DEFAULT_TIMEOUT_MEDIUM_SECS, DEFAULT_TIMEOUT_SHORT_SECS, } from './timeouts.js';
30
- import { parseArgument } from './utils.js';
30
+ import { getEnv, parseArgument } from './utils.js';
31
31
  const clientOptionsSchema = z.strictObject({
32
32
  baseUrl: z.string().default('https://api.apify.com'),
33
33
  publicBaseUrl: z.string().default('https://api.apify.com'),
@@ -496,7 +496,7 @@ export class ApifyClient {
496
496
  * @since Added in 2.7.0
497
497
  */
498
498
  async setStatusMessage(message, options) {
499
- const runId = process.env[ACTOR_ENV_VARS.RUN_ID];
499
+ const runId = getEnv(ACTOR_ENV_VARS.RUN_ID);
500
500
  if (!runId) {
501
501
  throw new Error(`Environment variable ${ACTOR_ENV_VARS.RUN_ID} is not set!`);
502
502
  }
@@ -1,13 +1,12 @@
1
1
  import type { JsonArray, JsonObject } from 'type-fest';
2
2
  /**
3
- * Parses a Buffer or ArrayBuffer using the provided content type header.
3
+ * Parses a binary response body using the provided content type header.
4
4
  *
5
5
  * - application/json is returned as a parsed object.
6
6
  * - application/*xml and text/* are returned as strings.
7
7
  * - everything else is returned as original body.
8
8
  *
9
9
  * If the header includes a charset, the body will be stringified only
10
- * if the charset represents a known encoding to Node.js or Browser.
10
+ * if the charset is an encoding `TextDecoder` knows.
11
11
  */
12
- export declare function maybeParseBody(body: Buffer | ArrayBuffer, contentTypeHeader: string): string | Buffer | ArrayBuffer | JsonObject | JsonArray;
13
- export declare function isomorphicBufferToString(buffer: Buffer | ArrayBuffer, encoding: BufferEncoding): string;
12
+ export declare function maybeParseBody(body: ArrayBuffer | ArrayBufferView, contentTypeHeader: string): string | ArrayBuffer | ArrayBufferView | JsonObject | JsonArray;
@@ -1,16 +1,15 @@
1
1
  import contentTypeParser from 'content-type';
2
- import { isNode } from './utils.js';
3
2
  const CONTENT_TYPE_JSON = 'application/json';
4
3
  const STRINGIFIABLE_CONTENT_TYPE_RXS = [new RegExp(`^${CONTENT_TYPE_JSON}`, 'i'), /^application\/.*xml$/i, /^text\//i];
5
4
  /**
6
- * Parses a Buffer or ArrayBuffer using the provided content type header.
5
+ * Parses a binary response body using the provided content type header.
7
6
  *
8
7
  * - application/json is returned as a parsed object.
9
8
  * - application/*xml and text/* are returned as strings.
10
9
  * - everything else is returned as original body.
11
10
  *
12
11
  * If the header includes a charset, the body will be stringified only
13
- * if the charset represents a known encoding to Node.js or Browser.
12
+ * if the charset is an encoding `TextDecoder` knows.
14
13
  */
15
14
  export function maybeParseBody(body, contentTypeHeader) {
16
15
  let contentType;
@@ -24,35 +23,29 @@ export function maybeParseBody(body, contentTypeHeader) {
24
23
  // can't parse, keep original body
25
24
  return body;
26
25
  }
27
- // If we can't successfully parse it, we return
26
+ if (!isContentTypeStringifiable(contentType))
27
+ return body;
28
+ // If we can't successfully decode it, we return
28
29
  // the original buffer rather than a mangled string.
29
- if (!areDataStringifiable(contentType, charset))
30
+ const decoder = createDecoder(charset);
31
+ if (!decoder)
30
32
  return body;
31
- const dataString = isomorphicBufferToString(body, charset);
33
+ const dataString = decoder.decode(body);
32
34
  return contentType === CONTENT_TYPE_JSON ? JSON.parse(dataString) : dataString;
33
35
  }
34
- export function isomorphicBufferToString(buffer, encoding) {
35
- if (buffer.constructor.name !== ArrayBuffer.name) {
36
- return buffer.toString(encoding);
36
+ function createDecoder(charset) {
37
+ try {
38
+ // No charset defaults to utf-8. A leading BOM is kept, so a BOM-prefixed CSV stored in a key-value store
39
+ // round-trips through `getRecord()` unchanged.
40
+ return new TextDecoder(charset, { ignoreBOM: true });
41
+ }
42
+ catch {
43
+ // `TextDecoder` throws a `RangeError` for a label it does not know.
44
+ return undefined;
37
45
  }
38
- // Browser decoding only works with UTF-8.
39
- const utf8decoder = new TextDecoder();
40
- return utf8decoder.decode(new Uint8Array(buffer));
41
- }
42
- function isCharsetStringifiable(charset) {
43
- if (!charset)
44
- return true; // hope that it's utf-8
45
- if (isNode())
46
- return Buffer.isEncoding(charset);
47
- const normalizedCharset = charset.toLowerCase().replace('-', '');
48
- // Browsers only support decoding utf-8 buffers.
49
- return normalizedCharset === 'utf8';
50
46
  }
51
47
  function isContentTypeStringifiable(contentType) {
52
48
  if (!contentType)
53
49
  return false; // keep buffer
54
50
  return STRINGIFIABLE_CONTENT_TYPE_RXS.some((rx) => rx.test(contentType));
55
51
  }
56
- function areDataStringifiable(contentType, charset) {
57
- return isContentTypeStringifiable(contentType) && isCharsetStringifiable(charset);
58
- }