-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcontext.ts
More file actions
190 lines (165 loc) · 6.07 KB
/
context.ts
File metadata and controls
190 lines (165 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
import type {
KeyValueStore,
RecordOptions,
Request,
RequestOptions,
RequestQueueOperationOptions,
RequestQueueV2,
} from '@crawlee/core';
import type { Dictionary } from '@crawlee/utils';
import type { ApifyEnv } from 'apify';
import { Actor } from 'apify';
import type { MediaType } from 'content-type';
import contentTypeParser from 'content-type';
import log from '@apify/log';
import { saveSnapshot } from './browser_tools.js';
import type { SnapshotOptions } from './browser_tools.js';
import { META_KEY } from './consts.js';
import type { RequestMetadata } from './tools.js';
export interface MapLike<K, V> extends Omit<
Map<K, V>,
'values' | 'keys' | 'entries' | 'set'
> {
keys: () => K[];
values: () => V[];
entries: () => [K, V][];
set: (key: K, value: V) => MapLike<K, V>;
}
export interface CrawlerSetupOptions {
rawInput: string;
env: ApifyEnv;
globalStore: Map<string, unknown> | MapLike<string, unknown>;
requestQueue: RequestQueueV2;
keyValueStore: KeyValueStore;
customData: unknown;
}
export interface ContextOptions {
crawlerSetup: CrawlerSetupOptions;
pageFunctionArguments: Dictionary;
}
interface InternalState {
skipLinks: boolean;
}
const setup = Symbol('crawler-setup');
const internalState = Symbol('request-internal-state');
/**
* Context represents everything that is available to the user
* via Page Function. A class is used instead of a simple object
* to avoid having to create new instances of functions with each
* request.
*
* Some properties need to be accessible to the Context,
* but should not be exposed to the user thus they are hidden
* using a Symbol to prevent the user from easily accessing
* and manipulating them.
*/
class Context<
Options extends ContextOptions = ContextOptions,
// oxlint-disable-next-line no-unused-vars -- declaration-merged interface below uses this generic
ExtraFields = Options['pageFunctionArguments'],
> {
private readonly [setup]: CrawlerSetupOptions;
private readonly [internalState]: InternalState;
readonly Actor = Actor;
readonly Apify = Actor; // for back compatibility
readonly log = log;
readonly input: any;
readonly env: ApifyEnv;
readonly customData: unknown;
readonly globalStore: Map<string, unknown> | MapLike<string, unknown>;
constructor(options: Options) {
const { crawlerSetup, pageFunctionArguments } = options;
// Private
this[setup] = crawlerSetup;
this[internalState] = {
skipLinks: false,
};
this.input = JSON.parse(crawlerSetup.rawInput);
this.env = { ...crawlerSetup.env };
this.customData = crawlerSetup.customData;
this.globalStore = crawlerSetup.globalStore;
// Page function arguments are directly passed from CrawlerSetup
// and differ between Puppeteer and Cheerio Scrapers.
// We must use properties and descriptors not to trigger getters / setters.
Object.defineProperties(
this,
Object.getOwnPropertyDescriptors(pageFunctionArguments),
);
// Bind this to allow destructuring off context in pageFunction.
this.saveSnapshot = this.saveSnapshot.bind(this);
this.skipLinks = this.skipLinks.bind(this);
this.enqueueRequest = this.enqueueRequest.bind(this);
}
async getValue<T>(...args: Parameters<KeyValueStore['getValue']>) {
return this[setup].keyValueStore.getValue<T>(...(args as [string, T]));
}
async setValue<T>(...args: Parameters<KeyValueStore['setValue']>) {
return this[setup].keyValueStore.setValue<T>(
...(args as [
key: string,
value: T | null,
options?: RecordOptions,
]),
);
}
async saveSnapshot() {
return saveSnapshot({
page: this.page as SnapshotOptions['page'],
body: this.body as SnapshotOptions['body'],
contentType: this.contentType
? contentTypeParser.format(this.contentType as MediaType)
: null,
json: this.json,
});
}
skipLinks() {
this[internalState].skipLinks = true;
}
async enqueueRequest(
requestOpts: RequestOptions = {} as RequestOptions,
options: RequestQueueOperationOptions = {},
): ReturnType<RequestQueueV2['addRequest']> {
const defaultRequestOpts = {
useExtendedUniqueKey: true,
keepUrlFragment: this.input.keepUrlFragments,
};
const newRequest = {
...defaultRequestOpts,
...requestOpts,
} as RequestOptions;
const castedRequest = this.request as Request;
const defaultUserData = {
[META_KEY]: {
parentRequestId: castedRequest.id || castedRequest.uniqueKey,
depth:
(castedRequest.userData?.[META_KEY] as RequestMetadata)
.depth ?? 0 + 1,
},
};
newRequest.userData = { ...defaultUserData, ...requestOpts.userData };
return this[setup].requestQueue.addRequest(newRequest, options);
}
}
// @ts-expect-error -- Extensions actually work but TS complains
// eslint-disable-next-line @typescript-eslint/no-empty-object-type -- intentional for better type inference
interface Context<
Options extends ContextOptions = ContextOptions,
ExtraFields extends ContextOptions['pageFunctionArguments'] =
Options['pageFunctionArguments'],
> extends ExtraFields {}
/**
* Creates a Context by passing all arguments to its constructor
* and returns it, along with a reference to its state object.
*/
export function createContext<
Options extends ContextOptions = ContextOptions,
ExtraFields extends ContextOptions['pageFunctionArguments'] =
Options['pageFunctionArguments'],
>(contextOptions: Options) {
const context = new Context<Options, ExtraFields>(contextOptions);
return {
context,
state: context[internalState],
};
}