@crawlee/core 4.0.0-beta.81 → 4.0.0-beta.83
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/index.d.ts +1 -0
- package/index.js +1 -0
- package/owned_or_injected.d.ts +60 -0
- package/owned_or_injected.js +98 -0
- package/package.json +5 -5
package/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './crawlers/index.js';
|
|
|
6
6
|
export * from './enqueue_links/index.js';
|
|
7
7
|
export * from './events/index.js';
|
|
8
8
|
export * from './log.js';
|
|
9
|
+
export * from './owned_or_injected.js';
|
|
9
10
|
export * from './proxy_configuration.js';
|
|
10
11
|
export * from './request.js';
|
|
11
12
|
export * from './router.js';
|
package/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export * from './crawlers/index.js';
|
|
|
6
6
|
export * from './enqueue_links/index.js';
|
|
7
7
|
export * from './events/index.js';
|
|
8
8
|
export * from './log.js';
|
|
9
|
+
export * from './owned_or_injected.js';
|
|
9
10
|
export * from './proxy_configuration.js';
|
|
10
11
|
export * from './request.js';
|
|
11
12
|
export * from './router.js';
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Captures the "inject-or-default" ownership pattern for a crawler's stateful collaborators (session pool, browser
|
|
3
|
+
* pool, the crawler-opened request queue, ...). A collaborator is either **injected** by the user (borrowed — the
|
|
4
|
+
* crawler never drives its lifecycle) or **built by the crawler** as a default (owned — the crawler sequences its
|
|
5
|
+
* lifecycle). Owned-only lifecycle hooks are gated through a single {@link OwnedOrInjected.ifOwned|`ifOwned()`}.
|
|
6
|
+
*
|
|
7
|
+
* This is the deliberate complement to the {@link ServiceLocator}: ambient infrastructure (config/events/storage/
|
|
8
|
+
* logger) stays in the locator, while crawler-scoped stateful collaborators the crawler sequences go through this.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export declare class OwnedOrInjected<Injected, Owned extends Injected = Injected> {
|
|
13
|
+
private _value;
|
|
14
|
+
private readonly _owned;
|
|
15
|
+
private _present;
|
|
16
|
+
private constructor();
|
|
17
|
+
/**
|
|
18
|
+
* Resolves a collaborator from an optionally-injected instance. `Injected` is the public/borrowed type exposed via
|
|
19
|
+
* {@link OwnedOrInjected.value|`value`}; `Owned` is the concrete type the crawler builds (a subtype with extra
|
|
20
|
+
* lifecycle methods), which {@link OwnedOrInjected.set|`set()`} and {@link OwnedOrInjected.ifOwned|`ifOwned()`}
|
|
21
|
+
* deal in — so owned-only lifecycle hooks are statically typed with no casts.
|
|
22
|
+
*
|
|
23
|
+
* - `injected` provided → borrowed (present, not owned).
|
|
24
|
+
* - `injected` omitted → owned; `buildDefault` fills the slot eagerly if given, otherwise it stays empty until a
|
|
25
|
+
* later {@link OwnedOrInjected.set|`set()`} (the lazy case, e.g. a request queue opened on first use).
|
|
26
|
+
*/
|
|
27
|
+
static resolve<Injected, Owned extends Injected = Injected>(injected?: Injected, buildDefault?: () => Owned): OwnedOrInjected<Injected, Owned>;
|
|
28
|
+
/**
|
|
29
|
+
* Whether the crawler owns the instance and is therefore responsible for its lifecycle. `true` only for
|
|
30
|
+
* crawler-built defaults, `false` for user-injected instances.
|
|
31
|
+
*/
|
|
32
|
+
get isOwned(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Whether a value is currently available. `false` for an owned slot whose default hasn't been built yet
|
|
35
|
+
* (e.g. a lazily-opened request queue before its first use).
|
|
36
|
+
*/
|
|
37
|
+
get isPresent(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* The resolved instance, typed as the public `Injected` type. Throws if the value is not present yet — callers that
|
|
40
|
+
* expect a lazily-filled owned slot should read {@link OwnedOrInjected.maybeValue|`maybeValue`} instead.
|
|
41
|
+
*/
|
|
42
|
+
get value(): Injected;
|
|
43
|
+
/**
|
|
44
|
+
* The resolved instance, or `undefined` when a lazily-filled owned slot hasn't been built yet. The non-throwing
|
|
45
|
+
* counterpart to {@link OwnedOrInjected.value|`value`} — pairs naturally with `?? fallback` so callers can read
|
|
46
|
+
* a possibly-empty slot without the `isPresent ? value : …` dance.
|
|
47
|
+
*/
|
|
48
|
+
get maybeValue(): Injected | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Fills the (owned) slot with the crawler-built default, returning it for convenience. Only valid on an owned,
|
|
51
|
+
* not-yet-filled slot: borrowed instances are never replaced and an owned slot is filled exactly once (re-setting
|
|
52
|
+
* would silently orphan the previous instance's lifecycle).
|
|
53
|
+
*/
|
|
54
|
+
set(value: Owned): Owned;
|
|
55
|
+
/**
|
|
56
|
+
* Runs an owned-only lifecycle hook, invoked (with the value typed as the concrete `Owned`) only when the crawler
|
|
57
|
+
* owns a present instance — a no-op for a borrowed instance or an owned-but-not-yet-built slot.
|
|
58
|
+
*/
|
|
59
|
+
ifOwned<R>(fn: (value: Owned) => R | Promise<R>): Promise<R | undefined>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Captures the "inject-or-default" ownership pattern for a crawler's stateful collaborators (session pool, browser
|
|
3
|
+
* pool, the crawler-opened request queue, ...). A collaborator is either **injected** by the user (borrowed — the
|
|
4
|
+
* crawler never drives its lifecycle) or **built by the crawler** as a default (owned — the crawler sequences its
|
|
5
|
+
* lifecycle). Owned-only lifecycle hooks are gated through a single {@link OwnedOrInjected.ifOwned|`ifOwned()`}.
|
|
6
|
+
*
|
|
7
|
+
* This is the deliberate complement to the {@link ServiceLocator}: ambient infrastructure (config/events/storage/
|
|
8
|
+
* logger) stays in the locator, while crawler-scoped stateful collaborators the crawler sequences go through this.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
export class OwnedOrInjected {
|
|
13
|
+
_value;
|
|
14
|
+
_owned;
|
|
15
|
+
_present;
|
|
16
|
+
constructor(value, owned, present) {
|
|
17
|
+
this._value = value;
|
|
18
|
+
this._owned = owned;
|
|
19
|
+
this._present = present;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolves a collaborator from an optionally-injected instance. `Injected` is the public/borrowed type exposed via
|
|
23
|
+
* {@link OwnedOrInjected.value|`value`}; `Owned` is the concrete type the crawler builds (a subtype with extra
|
|
24
|
+
* lifecycle methods), which {@link OwnedOrInjected.set|`set()`} and {@link OwnedOrInjected.ifOwned|`ifOwned()`}
|
|
25
|
+
* deal in — so owned-only lifecycle hooks are statically typed with no casts.
|
|
26
|
+
*
|
|
27
|
+
* - `injected` provided → borrowed (present, not owned).
|
|
28
|
+
* - `injected` omitted → owned; `buildDefault` fills the slot eagerly if given, otherwise it stays empty until a
|
|
29
|
+
* later {@link OwnedOrInjected.set|`set()`} (the lazy case, e.g. a request queue opened on first use).
|
|
30
|
+
*/
|
|
31
|
+
static resolve(injected, buildDefault) {
|
|
32
|
+
if (injected !== undefined) {
|
|
33
|
+
return new OwnedOrInjected(injected, false, true);
|
|
34
|
+
}
|
|
35
|
+
if (buildDefault !== undefined) {
|
|
36
|
+
return new OwnedOrInjected(buildDefault(), true, true);
|
|
37
|
+
}
|
|
38
|
+
return new OwnedOrInjected(undefined, true, false);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether the crawler owns the instance and is therefore responsible for its lifecycle. `true` only for
|
|
42
|
+
* crawler-built defaults, `false` for user-injected instances.
|
|
43
|
+
*/
|
|
44
|
+
get isOwned() {
|
|
45
|
+
return this._owned;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Whether a value is currently available. `false` for an owned slot whose default hasn't been built yet
|
|
49
|
+
* (e.g. a lazily-opened request queue before its first use).
|
|
50
|
+
*/
|
|
51
|
+
get isPresent() {
|
|
52
|
+
return this._present;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The resolved instance, typed as the public `Injected` type. Throws if the value is not present yet — callers that
|
|
56
|
+
* expect a lazily-filled owned slot should read {@link OwnedOrInjected.maybeValue|`maybeValue`} instead.
|
|
57
|
+
*/
|
|
58
|
+
get value() {
|
|
59
|
+
if (!this._present) {
|
|
60
|
+
throw new Error('OwnedOrInjected value is not initialized yet');
|
|
61
|
+
}
|
|
62
|
+
return this._value;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The resolved instance, or `undefined` when a lazily-filled owned slot hasn't been built yet. The non-throwing
|
|
66
|
+
* counterpart to {@link OwnedOrInjected.value|`value`} — pairs naturally with `?? fallback` so callers can read
|
|
67
|
+
* a possibly-empty slot without the `isPresent ? value : …` dance.
|
|
68
|
+
*/
|
|
69
|
+
get maybeValue() {
|
|
70
|
+
return this._present ? this._value : undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Fills the (owned) slot with the crawler-built default, returning it for convenience. Only valid on an owned,
|
|
74
|
+
* not-yet-filled slot: borrowed instances are never replaced and an owned slot is filled exactly once (re-setting
|
|
75
|
+
* would silently orphan the previous instance's lifecycle).
|
|
76
|
+
*/
|
|
77
|
+
set(value) {
|
|
78
|
+
if (!this._owned) {
|
|
79
|
+
throw new Error('Cannot set() a borrowed OwnedOrInjected value');
|
|
80
|
+
}
|
|
81
|
+
if (this._present) {
|
|
82
|
+
throw new Error('OwnedOrInjected value is already initialized');
|
|
83
|
+
}
|
|
84
|
+
this._value = value;
|
|
85
|
+
this._present = true;
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Runs an owned-only lifecycle hook, invoked (with the value typed as the concrete `Owned`) only when the crawler
|
|
90
|
+
* owns a present instance — a no-op for a borrowed instance or an owned-but-not-yet-built slot.
|
|
91
|
+
*/
|
|
92
|
+
async ifOwned(fn) {
|
|
93
|
+
if (!this._owned || !this._present) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
return fn(this._value);
|
|
97
|
+
}
|
|
98
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/core",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.83",
|
|
4
4
|
"description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -53,9 +53,9 @@
|
|
|
53
53
|
"@apify/pseudo_url": "^2.0.59",
|
|
54
54
|
"@apify/timeout": "^0.3.2",
|
|
55
55
|
"@apify/utilities": "^2.15.5",
|
|
56
|
-
"@crawlee/fs-storage": "4.0.0-beta.
|
|
57
|
-
"@crawlee/types": "4.0.0-beta.
|
|
58
|
-
"@crawlee/utils": "4.0.0-beta.
|
|
56
|
+
"@crawlee/fs-storage": "4.0.0-beta.83",
|
|
57
|
+
"@crawlee/types": "4.0.0-beta.83",
|
|
58
|
+
"@crawlee/utils": "4.0.0-beta.83",
|
|
59
59
|
"@sapphire/async-queue": "^1.5.5",
|
|
60
60
|
"@sapphire/shapeshift": "^4.0.0",
|
|
61
61
|
"@vladfrangu/async_event_emitter": "^2.4.6",
|
|
@@ -79,5 +79,5 @@
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "5f25b90c4914d7e097732cf761e440aeba799137"
|
|
83
83
|
}
|