@crawlee/utils 4.0.0-beta.93 → 4.0.0-beta.95
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/internals/iterables.d.ts +0 -79
- package/internals/iterables.js +0 -134
- package/package.json +4 -4
package/internals/iterables.d.ts
CHANGED
|
@@ -26,85 +26,6 @@ export declare function isIterable<T>(value: unknown): value is Iterable<T>;
|
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
28
|
export declare function isAsyncIterable<T>(value: unknown): value is AsyncIterable<T>;
|
|
29
|
-
/**
|
|
30
|
-
* Converts any iterable or async iterable to an async iterable.
|
|
31
|
-
* @internal
|
|
32
|
-
*
|
|
33
|
-
* @yields Each item from the input iterable
|
|
34
|
-
*
|
|
35
|
-
* **Example usage:**
|
|
36
|
-
* ```ts
|
|
37
|
-
* const syncArray = [1, 2, 3];
|
|
38
|
-
* for await (const item of asyncifyIterable(syncArray)) {
|
|
39
|
-
* console.log(item); // 1, 2, 3
|
|
40
|
-
* }
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export declare function asyncifyIterable<T>(iterable: Iterable<T> | AsyncIterable<T>): AsyncIterable<T>;
|
|
44
|
-
/**
|
|
45
|
-
* Lazily splits the input async iterable into chunks of specified size.
|
|
46
|
-
* The last chunk may contain fewer items if the total number of items
|
|
47
|
-
* is not evenly divisible by the chunk size.
|
|
48
|
-
* @internal
|
|
49
|
-
*
|
|
50
|
-
* @yields Arrays of items, each containing up to chunkSize items
|
|
51
|
-
*
|
|
52
|
-
* **Example usage:**
|
|
53
|
-
* ```ts
|
|
54
|
-
* const numbers = async function* () {
|
|
55
|
-
* for (let i = 1; i <= 10; i++) yield i;
|
|
56
|
-
* };
|
|
57
|
-
*
|
|
58
|
-
* for await (const chunk of chunkedAsyncIterable(numbers(), 3)) {
|
|
59
|
-
* console.log(chunk); // [1, 2, 3], [4, 5, 6], [7, 8, 9], [10]
|
|
60
|
-
* }
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export declare function chunkedAsyncIterable<T>(iterable: AsyncIterable<T> | Iterable<T>, chunkSize: number | (() => number)): AsyncIterable<T[]>;
|
|
64
|
-
/**
|
|
65
|
-
* An async iterator that also supports peeking at the next value without consuming it.
|
|
66
|
-
* Extends both AsyncIterator and AsyncIterable interfaces.
|
|
67
|
-
* @internal
|
|
68
|
-
*/
|
|
69
|
-
export interface PeekableAsyncIterator<T> extends AsyncIterator<T>, AsyncIterable<T> {
|
|
70
|
-
/**
|
|
71
|
-
* Peeks at the next value without consuming it from the iterator.
|
|
72
|
-
* Subsequent calls to peek() will return the same value until next() is called.
|
|
73
|
-
*
|
|
74
|
-
* @returns Promise that resolves to the next value, or undefined if the iterator is exhausted
|
|
75
|
-
*/
|
|
76
|
-
peek(): Promise<T | undefined>;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* An async iterable that yields peekable async iterators.
|
|
80
|
-
* @internal
|
|
81
|
-
*/
|
|
82
|
-
export interface PeekableAsyncIterable<T> extends AsyncIterable<T> {
|
|
83
|
-
[Symbol.asyncIterator](): PeekableAsyncIterator<T>;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Wraps an async iterable to provide peek functionality, allowing you to look at
|
|
87
|
-
* the next value without consuming it from the iterator.
|
|
88
|
-
* @internal
|
|
89
|
-
*
|
|
90
|
-
* @param iterable - The async iterable to make peekable
|
|
91
|
-
*
|
|
92
|
-
* **Example usage:**
|
|
93
|
-
* ```ts
|
|
94
|
-
* const numbers = async function* () {
|
|
95
|
-
* yield 1; yield 2; yield 3;
|
|
96
|
-
* };
|
|
97
|
-
*
|
|
98
|
-
* const peekable = peekableAsyncIterable(numbers());
|
|
99
|
-
* const iterator = peekable[Symbol.asyncIterator]();
|
|
100
|
-
*
|
|
101
|
-
* console.log(await iterator.peek()); // 1 (doesn't consume)
|
|
102
|
-
* console.log(await iterator.peek()); // 1 (still doesn't consume)
|
|
103
|
-
* console.log(await iterator.next()); // { value: 1, done: false } (now consumed)
|
|
104
|
-
* console.log(await iterator.peek()); // 2 (next value)
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
export declare function peekableAsyncIterable<T>(iterable: AsyncIterable<T> | Iterable<T>): PeekableAsyncIterable<T>;
|
|
108
29
|
/**
|
|
109
30
|
* Merges multiple async iterables into a single async iterable, yielding values concurrently.
|
|
110
31
|
*
|
package/internals/iterables.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { inspect } from 'node:util';
|
|
2
1
|
/**
|
|
3
2
|
* Type guard that checks if a value is iterable (has Symbol.iterator).
|
|
4
3
|
* @internal
|
|
@@ -40,139 +39,6 @@ export function isAsyncIterable(value) {
|
|
|
40
39
|
}
|
|
41
40
|
return typeof Object(value)[Symbol.asyncIterator] === 'function';
|
|
42
41
|
}
|
|
43
|
-
/**
|
|
44
|
-
* Converts any iterable or async iterable to an async iterable.
|
|
45
|
-
* @internal
|
|
46
|
-
*
|
|
47
|
-
* @yields Each item from the input iterable
|
|
48
|
-
*
|
|
49
|
-
* **Example usage:**
|
|
50
|
-
* ```ts
|
|
51
|
-
* const syncArray = [1, 2, 3];
|
|
52
|
-
* for await (const item of asyncifyIterable(syncArray)) {
|
|
53
|
-
* console.log(item); // 1, 2, 3
|
|
54
|
-
* }
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
export async function* asyncifyIterable(iterable) {
|
|
58
|
-
yield* iterable;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Lazily splits the input async iterable into chunks of specified size.
|
|
62
|
-
* The last chunk may contain fewer items if the total number of items
|
|
63
|
-
* is not evenly divisible by the chunk size.
|
|
64
|
-
* @internal
|
|
65
|
-
*
|
|
66
|
-
* @yields Arrays of items, each containing up to chunkSize items
|
|
67
|
-
*
|
|
68
|
-
* **Example usage:**
|
|
69
|
-
* ```ts
|
|
70
|
-
* const numbers = async function* () {
|
|
71
|
-
* for (let i = 1; i <= 10; i++) yield i;
|
|
72
|
-
* };
|
|
73
|
-
*
|
|
74
|
-
* for await (const chunk of chunkedAsyncIterable(numbers(), 3)) {
|
|
75
|
-
* console.log(chunk); // [1, 2, 3], [4, 5, 6], [7, 8, 9], [10]
|
|
76
|
-
* }
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
export async function* chunkedAsyncIterable(iterable, chunkSize) {
|
|
80
|
-
const getChunkSize = typeof chunkSize === 'function' ? chunkSize : () => chunkSize;
|
|
81
|
-
if (typeof chunkSize === 'number' && chunkSize < 1) {
|
|
82
|
-
throw new Error(`Chunk size must be a positive number (${inspect(chunkSize)}) received`);
|
|
83
|
-
}
|
|
84
|
-
const iterator = Symbol.asyncIterator in iterable
|
|
85
|
-
? iterable[Symbol.asyncIterator]()
|
|
86
|
-
: iterable[Symbol.iterator]();
|
|
87
|
-
while (true) {
|
|
88
|
-
const currentSize = getChunkSize();
|
|
89
|
-
if (currentSize < 1)
|
|
90
|
-
break;
|
|
91
|
-
const chunk = [];
|
|
92
|
-
for (let i = 0; i < currentSize; i++) {
|
|
93
|
-
const next = await iterator.next();
|
|
94
|
-
if (next.done) {
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
chunk.push(next.value);
|
|
98
|
-
}
|
|
99
|
-
if (chunk.length === 0)
|
|
100
|
-
break;
|
|
101
|
-
yield chunk;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Wraps an async iterable to provide peek functionality, allowing you to look at
|
|
106
|
-
* the next value without consuming it from the iterator.
|
|
107
|
-
* @internal
|
|
108
|
-
*
|
|
109
|
-
* @param iterable - The async iterable to make peekable
|
|
110
|
-
*
|
|
111
|
-
* **Example usage:**
|
|
112
|
-
* ```ts
|
|
113
|
-
* const numbers = async function* () {
|
|
114
|
-
* yield 1; yield 2; yield 3;
|
|
115
|
-
* };
|
|
116
|
-
*
|
|
117
|
-
* const peekable = peekableAsyncIterable(numbers());
|
|
118
|
-
* const iterator = peekable[Symbol.asyncIterator]();
|
|
119
|
-
*
|
|
120
|
-
* console.log(await iterator.peek()); // 1 (doesn't consume)
|
|
121
|
-
* console.log(await iterator.peek()); // 1 (still doesn't consume)
|
|
122
|
-
* console.log(await iterator.next()); // { value: 1, done: false } (now consumed)
|
|
123
|
-
* console.log(await iterator.peek()); // 2 (next value)
|
|
124
|
-
* ```
|
|
125
|
-
*/
|
|
126
|
-
export function peekableAsyncIterable(iterable) {
|
|
127
|
-
const iterator = asyncifyIterable(iterable)[Symbol.asyncIterator]();
|
|
128
|
-
let peekedValue;
|
|
129
|
-
let isExhausted = false;
|
|
130
|
-
const peekableIterator = {
|
|
131
|
-
async next() {
|
|
132
|
-
// If we have peeked a value, return it and clear the peek
|
|
133
|
-
if (peekedValue !== undefined) {
|
|
134
|
-
const result = peekedValue;
|
|
135
|
-
peekedValue = undefined;
|
|
136
|
-
if (result.done) {
|
|
137
|
-
isExhausted = true;
|
|
138
|
-
return { done: true, value: undefined };
|
|
139
|
-
}
|
|
140
|
-
return { done: false, value: result.value };
|
|
141
|
-
}
|
|
142
|
-
if (isExhausted) {
|
|
143
|
-
return { done: true, value: undefined };
|
|
144
|
-
}
|
|
145
|
-
const result = await iterator.next();
|
|
146
|
-
if (result.done) {
|
|
147
|
-
isExhausted = true;
|
|
148
|
-
}
|
|
149
|
-
return result;
|
|
150
|
-
},
|
|
151
|
-
async peek() {
|
|
152
|
-
if (peekedValue !== undefined) {
|
|
153
|
-
return peekedValue.done ? undefined : peekedValue.value;
|
|
154
|
-
}
|
|
155
|
-
if (isExhausted) {
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
const result = await iterator.next();
|
|
159
|
-
peekedValue = { done: result.done ?? false, value: result.value };
|
|
160
|
-
if (result.done) {
|
|
161
|
-
isExhausted = true;
|
|
162
|
-
return undefined;
|
|
163
|
-
}
|
|
164
|
-
return result.value;
|
|
165
|
-
},
|
|
166
|
-
[Symbol.asyncIterator]() {
|
|
167
|
-
return this;
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
return {
|
|
171
|
-
[Symbol.asyncIterator]() {
|
|
172
|
-
return peekableIterator;
|
|
173
|
-
},
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
42
|
// Source - https://stackoverflow.com/a/71288323
|
|
177
43
|
/**
|
|
178
44
|
* Merges multiple async iterables into a single async iterable, yielding values concurrently.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/utils",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.95",
|
|
4
4
|
"description": "A set of shared utilities that can be used by crawlers",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@apify/ps-tree": "^1.2.0",
|
|
45
|
-
"@crawlee/http-client": "4.0.0-beta.
|
|
46
|
-
"@crawlee/types": "4.0.0-beta.
|
|
45
|
+
"@crawlee/http-client": "4.0.0-beta.95",
|
|
46
|
+
"@crawlee/types": "4.0.0-beta.95",
|
|
47
47
|
"@types/sax": "^1.2.7",
|
|
48
48
|
"cheerio": "^1.0.0",
|
|
49
49
|
"domhandler": "^5.0.3",
|
|
@@ -61,5 +61,5 @@
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "ba8602d011706fb5c9930232402156eb5b657eb3"
|
|
65
65
|
}
|