apify-client 3.0.0-beta.20 → 3.0.0-beta.22
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 +4 -2
- package/dist/apify_api_error.js +1 -2
- package/dist/apify_client.js +2 -2
- package/dist/body_parser.d.ts +3 -4
- package/dist/body_parser.js +17 -24
- package/dist/bundle.js +13 -13
- package/dist/bundle.js.map +1 -1
- package/dist/generated/schemas.d.ts +110 -110
- package/dist/http_client.js +22 -54
- package/dist/interceptors.js +3 -3
- package/dist/resource_clients/dataset.d.ts +8 -10
- package/dist/resource_clients/dataset.js +8 -10
- package/dist/resource_clients/key_value_store.js +3 -2
- package/dist/resource_clients/log.js +5 -4
- package/dist/resource_clients/run.js +3 -2
- package/dist/runtime/node.d.ts +6 -0
- package/dist/runtime/node.js +58 -0
- package/dist/runtime/types.d.ts +50 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/web.d.ts +5 -0
- package/dist/runtime/web.js +17 -0
- package/dist/utils.d.ts +18 -14
- package/dist/utils.js +47 -60
- package/package.json +7 -1
package/dist/utils.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { NotFoundError } from './apify_api_error.js';
|
|
3
3
|
import { parseArgument } from '@apify/validations';
|
|
4
|
+
import { runtime } from '#runtime';
|
|
4
5
|
import { ResponseValidationError } from './response_validation_error.js';
|
|
5
6
|
// @ts-ignore if we enable `resolveJsonModule`, we end up with a `src` folder in `dist`
|
|
6
7
|
import packageJson from '../package.json' with { type: 'json' };
|
|
7
8
|
const MIN_COMPRESS_BYTES = 1024;
|
|
9
|
+
const textEncoder = new TextEncoder();
|
|
10
|
+
// Only the version, so a bundler can drop the rest of the manifest.
|
|
11
|
+
export const { version } = packageJson;
|
|
8
12
|
/** Media type prefixes whose payloads carry their own compression, so compressing the request body is wasted work. */
|
|
9
13
|
const ALREADY_COMPRESSED_MEDIA_TYPE_PREFIXES = ['audio/', 'image/', 'video/'];
|
|
10
14
|
/** Exact media types whose payloads carry their own compression. */
|
|
@@ -126,39 +130,45 @@ export function catchNotFoundForResourceOrThrow(err, resourceId) {
|
|
|
126
130
|
export function stringifyWebhooksToBase64(webhooks) {
|
|
127
131
|
if (!webhooks)
|
|
128
132
|
return;
|
|
129
|
-
|
|
130
|
-
if (isNode()) {
|
|
131
|
-
return Buffer.from(webhooksJson, 'utf8').toString('base64');
|
|
132
|
-
}
|
|
133
|
-
const encoder = new TextEncoder();
|
|
134
|
-
const uint8Array = encoder.encode(webhooksJson);
|
|
135
|
-
return btoa(String.fromCharCode(...uint8Array));
|
|
133
|
+
return bytesToBase64(textEncoder.encode(JSON.stringify(webhooks)));
|
|
136
134
|
}
|
|
137
|
-
let brotliCompressPromisified;
|
|
138
135
|
/**
|
|
139
|
-
*
|
|
136
|
+
* Encodes bytes as base64. `btoa()` takes a binary string, and the bytes are turned into one in slices,
|
|
137
|
+
* because spreading them all into a single `String.fromCharCode()` call overflows the argument limit on
|
|
138
|
+
* inputs of a few tens of kilobytes.
|
|
140
139
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const options = { params: { [constants.BROTLI_PARAM_QUALITY]: 6 } };
|
|
147
|
-
brotliCompressPromisified = async (input) => compress(input, options);
|
|
140
|
+
export function bytesToBase64(bytes) {
|
|
141
|
+
const SLICE_LENGTH = 0x8000;
|
|
142
|
+
let binary = '';
|
|
143
|
+
for (let i = 0; i < bytes.length; i += SLICE_LENGTH) {
|
|
144
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + SLICE_LENGTH));
|
|
148
145
|
}
|
|
149
|
-
return
|
|
146
|
+
return btoa(binary);
|
|
150
147
|
}
|
|
151
|
-
let gzipPromisified;
|
|
152
148
|
/**
|
|
153
|
-
*
|
|
149
|
+
* Concatenates byte chunks into one array.
|
|
154
150
|
*/
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
151
|
+
export function concatBytes(chunks) {
|
|
152
|
+
const result = new Uint8Array(chunks.reduce((length, chunk) => length + chunk.length, 0));
|
|
153
|
+
let offset = 0;
|
|
154
|
+
for (const chunk of chunks) {
|
|
155
|
+
result.set(chunk, offset);
|
|
156
|
+
offset += chunk.length;
|
|
160
157
|
}
|
|
161
|
-
return
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Views a request body as bytes: a string is UTF-8 encoded, binary values are viewed in place. Anything else
|
|
162
|
+
* - a stream, a `Blob`, form data - is `undefined`.
|
|
163
|
+
*/
|
|
164
|
+
function toBytes(value) {
|
|
165
|
+
if (typeof value === 'string')
|
|
166
|
+
return textEncoder.encode(value);
|
|
167
|
+
if (!isBuffer(value))
|
|
168
|
+
return undefined;
|
|
169
|
+
return ArrayBuffer.isView(value)
|
|
170
|
+
? new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
|
|
171
|
+
: new Uint8Array(value);
|
|
162
172
|
}
|
|
163
173
|
/**
|
|
164
174
|
* Decides whether a request body with the given content type is worth compressing.
|
|
@@ -183,40 +193,28 @@ export function isCompressibleContentType(contentType) {
|
|
|
183
193
|
return !ALREADY_COMPRESSED_MEDIA_TYPE_PREFIXES.some((prefix) => mediaType.startsWith(prefix));
|
|
184
194
|
}
|
|
185
195
|
/**
|
|
186
|
-
*
|
|
187
|
-
* too small
|
|
196
|
+
* Compresses the passed value with the runtime's best available algorithm. Returns `undefined` if the data
|
|
197
|
+
* is too small, is not a string or binary value, or if the runtime does not offer compression.
|
|
188
198
|
*/
|
|
189
199
|
export async function maybeCompressValue(value) {
|
|
190
|
-
if (!isNode())
|
|
191
|
-
return undefined;
|
|
192
200
|
// Request compression is not that important so let's
|
|
193
201
|
// skip it instead of throwing for unsupported types.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const areDataLargeEnough = Buffer.byteLength(value) >= MIN_COMPRESS_BYTES;
|
|
197
|
-
if (!areDataLargeEnough)
|
|
202
|
+
const bytes = toBytes(value);
|
|
203
|
+
if (!bytes || bytes.byteLength < MIN_COMPRESS_BYTES)
|
|
198
204
|
return undefined;
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
try {
|
|
207
|
-
return { data: await gzipValue(value), encoding: 'gzip' };
|
|
208
|
-
}
|
|
209
|
-
catch {
|
|
210
|
-
// Same reasoning as above: compression is a best-effort optimization, so skip it instead
|
|
211
|
-
// of failing the request.
|
|
212
|
-
return undefined;
|
|
213
|
-
}
|
|
205
|
+
return runtime.compress(bytes);
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Reads an environment variable, on runtimes that have them.
|
|
209
|
+
*/
|
|
210
|
+
export function getEnv(name) {
|
|
211
|
+
return typeof process !== 'undefined' ? process.env?.[name] : undefined;
|
|
214
212
|
}
|
|
215
213
|
/**
|
|
216
214
|
* Returns the UTF-8 byte length of a string.
|
|
217
215
|
*/
|
|
218
216
|
export function utf8ByteLength(value) {
|
|
219
|
-
return
|
|
217
|
+
return textEncoder.encode(value).byteLength;
|
|
220
218
|
}
|
|
221
219
|
/**
|
|
222
220
|
* Splits JSON-serialized items into consecutive batches of at most `maxCount` items, each of which fits into a JSON
|
|
@@ -243,11 +241,6 @@ export function splitIntoJsonArrayBatches(items, { maxCount, maxByteLength }) {
|
|
|
243
241
|
batches.push(batch);
|
|
244
242
|
return batches;
|
|
245
243
|
}
|
|
246
|
-
export function isNode() {
|
|
247
|
-
if (typeof BROWSER_BUILD !== 'undefined')
|
|
248
|
-
return false;
|
|
249
|
-
return !!(typeof process !== 'undefined' && process.versions && process.versions.node);
|
|
250
|
-
}
|
|
251
244
|
export function isBuffer(value) {
|
|
252
245
|
// Tag checks rather than `instanceof`, to also match buffers from another realm. `isView()`
|
|
253
246
|
// additionally covers `DataView`, which is not raw binary content.
|
|
@@ -264,12 +257,6 @@ export function isStream(value) {
|
|
|
264
257
|
const { on, pipe } = value;
|
|
265
258
|
return typeof on === 'function' && typeof pipe === 'function';
|
|
266
259
|
}
|
|
267
|
-
export function getVersionData() {
|
|
268
|
-
if (typeof BROWSER_BUILD !== 'undefined') {
|
|
269
|
-
return { version: VERSION };
|
|
270
|
-
}
|
|
271
|
-
return packageJson;
|
|
272
|
-
}
|
|
273
260
|
/**
|
|
274
261
|
* Helper class to create async iterators from paginated list endpoints.
|
|
275
262
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apify-client",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.22",
|
|
4
4
|
"description": "Apify API client for JavaScript",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -10,6 +10,12 @@
|
|
|
10
10
|
"types": "dist/index.d.ts",
|
|
11
11
|
"browser": "dist/bundle.js",
|
|
12
12
|
"unpkg": "dist/bundle.js",
|
|
13
|
+
"imports": {
|
|
14
|
+
"#runtime": {
|
|
15
|
+
"node": "./dist/runtime/node.js",
|
|
16
|
+
"default": "./dist/runtime/web.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
13
19
|
"exports": {
|
|
14
20
|
"./package.json": "./package.json",
|
|
15
21
|
"./browser": "./dist/bundle.js",
|