@crawlee/core 3.0.0-alpha.2 → 3.0.0-alpha.3
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/dist/LICENSE.md +201 -0
- package/{README.md → dist/README.md} +0 -0
- package/dist/autoscaling/autoscaled_pool.d.ts +286 -0
- package/dist/autoscaling/autoscaled_pool.d.ts.map +1 -0
- package/dist/autoscaling/autoscaled_pool.js +624 -0
- package/dist/autoscaling/autoscaled_pool.js.map +1 -0
- package/dist/autoscaling/index.d.ts +4 -0
- package/dist/autoscaling/index.d.ts.map +1 -0
- package/dist/autoscaling/index.js +7 -0
- package/dist/autoscaling/index.js.map +1 -0
- package/dist/autoscaling/snapshotter.d.ts +193 -0
- package/dist/autoscaling/snapshotter.d.ts.map +1 -0
- package/dist/autoscaling/snapshotter.js +377 -0
- package/dist/autoscaling/snapshotter.js.map +1 -0
- package/dist/autoscaling/system_status.d.ts +181 -0
- package/dist/autoscaling/system_status.d.ts.map +1 -0
- package/dist/autoscaling/system_status.js +194 -0
- package/dist/autoscaling/system_status.js.map +1 -0
- package/dist/cache_container.d.ts +18 -0
- package/dist/cache_container.d.ts.map +1 -0
- package/dist/cache_container.js +42 -0
- package/dist/cache_container.js.map +1 -0
- package/dist/configuration.d.ts +150 -0
- package/dist/configuration.d.ts.map +1 -0
- package/dist/configuration.js +284 -0
- package/dist/configuration.js.map +1 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +6 -0
- package/dist/constants.js.map +1 -0
- package/dist/crawlers/crawler_commons.d.ts +23 -0
- package/dist/crawlers/crawler_commons.d.ts.map +1 -0
- package/dist/crawlers/crawler_commons.js +3 -0
- package/dist/crawlers/crawler_commons.js.map +1 -0
- package/dist/crawlers/crawler_extension.d.ts +11 -0
- package/dist/crawlers/crawler_extension.d.ts.map +1 -0
- package/dist/crawlers/crawler_extension.js +30 -0
- package/dist/crawlers/crawler_extension.js.map +1 -0
- package/dist/crawlers/crawler_utils.d.ts +25 -0
- package/dist/crawlers/crawler_utils.d.ts.map +1 -0
- package/dist/crawlers/crawler_utils.js +72 -0
- package/dist/crawlers/crawler_utils.js.map +1 -0
- package/dist/crawlers/index.d.ts +5 -0
- package/dist/crawlers/index.d.ts.map +1 -0
- package/dist/crawlers/index.js +8 -0
- package/dist/crawlers/index.js.map +1 -0
- package/dist/crawlers/statistics.d.ts +147 -0
- package/dist/crawlers/statistics.d.ts.map +1 -0
- package/dist/crawlers/statistics.js +360 -0
- package/dist/crawlers/statistics.js.map +1 -0
- package/dist/enqueue_links/enqueue_links.d.ts +138 -0
- package/dist/enqueue_links/enqueue_links.d.ts.map +1 -0
- package/dist/enqueue_links/enqueue_links.js +123 -0
- package/dist/enqueue_links/enqueue_links.js.map +1 -0
- package/dist/enqueue_links/index.d.ts +3 -0
- package/dist/enqueue_links/index.d.ts.map +1 -0
- package/dist/enqueue_links/index.js +6 -0
- package/dist/enqueue_links/index.js.map +1 -0
- package/dist/enqueue_links/shared.d.ts +60 -0
- package/dist/enqueue_links/shared.d.ts.map +1 -0
- package/dist/enqueue_links/shared.js +148 -0
- package/dist/enqueue_links/shared.js.map +1 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/events/event_manager.d.ts +41 -0
- package/dist/events/event_manager.d.ts.map +1 -0
- package/dist/events/event_manager.js +103 -0
- package/dist/events/event_manager.js.map +1 -0
- package/dist/events/index.d.ts +3 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +6 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/local_event_manager.d.ts +32 -0
- package/dist/events/local_event_manager.d.ts.map +1 -0
- package/dist/events/local_event_manager.js +114 -0
- package/dist/events/local_event_manager.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +63 -0
- package/dist/log.d.ts +3 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +12 -0
- package/dist/log.js.map +1 -0
- package/dist/package.json +82 -0
- package/dist/proxy_configuration.d.ts +181 -0
- package/dist/proxy_configuration.d.ts.map +1 -0
- package/dist/proxy_configuration.js +209 -0
- package/dist/proxy_configuration.js.map +1 -0
- package/dist/request.d.ts +174 -0
- package/dist/request.d.ts.map +1 -0
- package/dist/request.js +270 -0
- package/dist/request.js.map +1 -0
- package/dist/serialization.d.ts +33 -0
- package/dist/serialization.d.ts.map +1 -0
- package/dist/serialization.js +146 -0
- package/dist/serialization.js.map +1 -0
- package/dist/session_pool/errors.d.ts +8 -0
- package/dist/session_pool/errors.d.ts.map +1 -0
- package/dist/session_pool/errors.js +20 -0
- package/dist/session_pool/errors.js.map +1 -0
- package/dist/session_pool/events.d.ts +3 -0
- package/dist/session_pool/events.d.ts.map +1 -0
- package/dist/session_pool/events.js +6 -0
- package/dist/session_pool/events.js.map +1 -0
- package/dist/session_pool/index.d.ts +6 -0
- package/dist/session_pool/index.d.ts.map +1 -0
- package/dist/session_pool/index.js +9 -0
- package/dist/session_pool/index.js.map +1 -0
- package/dist/session_pool/session.d.ts +212 -0
- package/dist/session_pool/session.d.ts.map +1 -0
- package/dist/session_pool/session.js +360 -0
- package/dist/session_pool/session.js.map +1 -0
- package/dist/session_pool/session_pool.d.ts +222 -0
- package/dist/session_pool/session_pool.d.ts.map +1 -0
- package/dist/session_pool/session_pool.js +389 -0
- package/dist/session_pool/session_pool.js.map +1 -0
- package/dist/session_pool/session_utils.d.ts +9 -0
- package/dist/session_pool/session_utils.d.ts.map +1 -0
- package/dist/session_pool/session_utils.js +19 -0
- package/dist/session_pool/session_utils.js.map +1 -0
- package/dist/storages/dataset.d.ts +358 -0
- package/dist/storages/dataset.d.ts.map +1 -0
- package/dist/storages/dataset.js +415 -0
- package/dist/storages/dataset.js.map +1 -0
- package/dist/storages/index.d.ts +7 -0
- package/dist/storages/index.d.ts.map +1 -0
- package/dist/storages/index.js +11 -0
- package/dist/storages/index.js.map +1 -0
- package/dist/storages/key_value_store.d.ts +242 -0
- package/dist/storages/key_value_store.d.ts.map +1 -0
- package/dist/storages/key_value_store.js +319 -0
- package/dist/storages/key_value_store.js.map +1 -0
- package/dist/storages/request_list.d.ts +461 -0
- package/dist/storages/request_list.d.ts.map +1 -0
- package/dist/storages/request_list.js +715 -0
- package/dist/storages/request_list.js.map +1 -0
- package/dist/storages/request_queue.d.ts +312 -0
- package/dist/storages/request_queue.d.ts.map +1 -0
- package/dist/storages/request_queue.js +708 -0
- package/dist/storages/request_queue.js.map +1 -0
- package/dist/storages/storage.d.ts +260 -0
- package/dist/storages/storage.d.ts.map +1 -0
- package/dist/storages/storage.js +3 -0
- package/dist/storages/storage.js.map +1 -0
- package/dist/storages/storage_manager.d.ts +40 -0
- package/dist/storages/storage_manager.d.ts.map +1 -0
- package/dist/storages/storage_manager.js +119 -0
- package/dist/storages/storage_manager.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/typedefs.d.ts +12 -0
- package/dist/typedefs.d.ts.map +1 -0
- package/dist/typedefs.js +15 -0
- package/dist/typedefs.js.map +1 -0
- package/dist/validators.d.ts +21 -0
- package/dist/validators.d.ts.map +1 -0
- package/dist/validators.js +26 -0
- package/dist/validators.js.map +1 -0
- package/package.json +11 -10
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ProxyConfiguration = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const ow_1 = tslib_1.__importDefault(require("ow"));
|
|
6
|
+
const log_1 = tslib_1.__importDefault(require("@apify/log"));
|
|
7
|
+
const configuration_1 = require("./configuration");
|
|
8
|
+
/**
|
|
9
|
+
* Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking
|
|
10
|
+
* your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
11
|
+
* them to use the selected proxies for all connections. You can get information about the currently used proxy by inspecting
|
|
12
|
+
* the {@link ProxyInfo} property in your crawler's page function. There, you can inspect the proxy's URL and other attributes.
|
|
13
|
+
*
|
|
14
|
+
* If you want to use your own proxies, use the {@link ProxyConfigurationOptions.proxyUrls} option. Your list of proxy URLs will
|
|
15
|
+
* be rotated by the configuration if this option is provided.
|
|
16
|
+
*
|
|
17
|
+
* **Example usage:**
|
|
18
|
+
*
|
|
19
|
+
* ```javascript
|
|
20
|
+
*
|
|
21
|
+
* const proxyConfiguration = new ProxyConfiguration({
|
|
22
|
+
* proxyUrls: ['...', '...'],
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const crawler = new CheerioCrawler({
|
|
26
|
+
* // ...
|
|
27
|
+
* proxyConfiguration,
|
|
28
|
+
* handlePageFunction: ({ proxyInfo }) => {
|
|
29
|
+
* const usedProxyUrl = proxyInfo.url; // Getting the proxy URL
|
|
30
|
+
* }
|
|
31
|
+
* })
|
|
32
|
+
*
|
|
33
|
+
* ```
|
|
34
|
+
* @category Scaling
|
|
35
|
+
*/
|
|
36
|
+
class ProxyConfiguration {
|
|
37
|
+
/**
|
|
38
|
+
* Creates a {@link ProxyConfiguration} instance based on the provided options. Proxy servers are used to prevent target websites from
|
|
39
|
+
* blocking your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
|
|
40
|
+
* them to use the selected proxies for all connections.
|
|
41
|
+
*
|
|
42
|
+
* ```javascript
|
|
43
|
+
* const proxyConfiguration = new ProxyConfiguration({
|
|
44
|
+
* proxyUrls: ['http://user:pass@proxy-1.com', 'http://user:pass@proxy-2.com'],
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* const crawler = new CheerioCrawler({
|
|
48
|
+
* // ...
|
|
49
|
+
* proxyConfiguration,
|
|
50
|
+
* handlePageFunction: ({ proxyInfo }) => {
|
|
51
|
+
* const usedProxyUrl = proxyInfo.url; // Getting the proxy URL
|
|
52
|
+
* }
|
|
53
|
+
* })
|
|
54
|
+
*
|
|
55
|
+
* ```
|
|
56
|
+
* @param options
|
|
57
|
+
* @param config
|
|
58
|
+
* @param validateRequired internal flag to disable validation that requires one of `proxyUrls` or `newUrlFunction`, defaults to true
|
|
59
|
+
*/
|
|
60
|
+
constructor(options = {}, config = configuration_1.Configuration.getGlobalConfig(), validateRequired = true) {
|
|
61
|
+
Object.defineProperty(this, "config", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: config
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(this, "isManInTheMiddle", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: false
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "nextCustomUrlIndex", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: 0
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(this, "proxyUrls", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: void 0
|
|
84
|
+
});
|
|
85
|
+
Object.defineProperty(this, "usedProxyUrls", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: new Map()
|
|
90
|
+
});
|
|
91
|
+
Object.defineProperty(this, "newUrlFunction", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: void 0
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "log", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: log_1.default.child({ prefix: 'ProxyConfiguration' })
|
|
102
|
+
});
|
|
103
|
+
(0, ow_1.default)(options, ow_1.default.object.exactShape({
|
|
104
|
+
proxyUrls: ow_1.default.optional.array.nonEmpty.ofType(ow_1.default.string.url),
|
|
105
|
+
newUrlFunction: ow_1.default.optional.function,
|
|
106
|
+
}));
|
|
107
|
+
const { proxyUrls, newUrlFunction } = options;
|
|
108
|
+
if (proxyUrls && newUrlFunction)
|
|
109
|
+
this._throwCannotCombineCustomMethods();
|
|
110
|
+
if (!proxyUrls && !newUrlFunction && validateRequired)
|
|
111
|
+
this._throwNoOptionsProvided();
|
|
112
|
+
this.proxyUrls = proxyUrls;
|
|
113
|
+
this.newUrlFunction = newUrlFunction;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* This function creates a new {@link ProxyInfo} info object.
|
|
117
|
+
* It is used by CheerioCrawler and PuppeteerCrawler to generate proxy URLs and also to allow the user to inspect
|
|
118
|
+
* the currently used proxy via the handlePageFunction parameter: proxyInfo.
|
|
119
|
+
* Use it if you want to work with a rich representation of a proxy URL.
|
|
120
|
+
* If you need the URL string only, use {@link ProxyConfiguration.newUrl}.
|
|
121
|
+
* @param [sessionId]
|
|
122
|
+
* Represents the identifier of user {@link Session} that can be managed by the {@link SessionPool} or
|
|
123
|
+
* you can use the Apify Proxy [Session](https://docs.apify.com/proxy#sessions) identifier.
|
|
124
|
+
* When the provided sessionId is a number, it's converted to a string. Property sessionId of
|
|
125
|
+
* {@link ProxyInfo} is always returned as a type string.
|
|
126
|
+
*
|
|
127
|
+
* All the HTTP requests going through the proxy with the same session identifier
|
|
128
|
+
* will use the same target proxy server (i.e. the same IP address).
|
|
129
|
+
* The identifier must not be longer than 50 characters and include only the following: `0-9`, `a-z`, `A-Z`, `"."`, `"_"` and `"~"`.
|
|
130
|
+
* @return Represents information about used proxy and its configuration.
|
|
131
|
+
*/
|
|
132
|
+
newProxyInfo(sessionId) {
|
|
133
|
+
if (typeof sessionId === 'number')
|
|
134
|
+
sessionId = `${sessionId}`;
|
|
135
|
+
const url = this.newUrl(sessionId);
|
|
136
|
+
const { username, password, port, hostname } = new URL(url);
|
|
137
|
+
return {
|
|
138
|
+
sessionId,
|
|
139
|
+
url,
|
|
140
|
+
username,
|
|
141
|
+
password,
|
|
142
|
+
hostname,
|
|
143
|
+
port: port,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Returns a new proxy URL based on provided configuration options and the `sessionId` parameter.
|
|
148
|
+
* @param [sessionId]
|
|
149
|
+
* Represents the identifier of user {@link Session} that can be managed by the {@link SessionPool} or
|
|
150
|
+
* you can use the Apify Proxy [Session](https://docs.apify.com/proxy#sessions) identifier.
|
|
151
|
+
* When the provided sessionId is a number, it's converted to a string.
|
|
152
|
+
*
|
|
153
|
+
* All the HTTP requests going through the proxy with the same session identifier
|
|
154
|
+
* will use the same target proxy server (i.e. the same IP address).
|
|
155
|
+
* The identifier must not be longer than 50 characters and include only the following: `0-9`, `a-z`, `A-Z`, `"."`, `"_"` and `"~"`.
|
|
156
|
+
* @return A string with a proxy URL, including authentication credentials and port number.
|
|
157
|
+
* For example, `http://bob:password123@proxy.example.com:8000`
|
|
158
|
+
*/
|
|
159
|
+
newUrl(sessionId) {
|
|
160
|
+
if (typeof sessionId === 'number')
|
|
161
|
+
sessionId = `${sessionId}`;
|
|
162
|
+
if (this.newUrlFunction) {
|
|
163
|
+
return this._callNewUrlFunction(sessionId);
|
|
164
|
+
}
|
|
165
|
+
return this._handleCustomUrl(sessionId);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Handles custom url rotation with session
|
|
169
|
+
*/
|
|
170
|
+
_handleCustomUrl(sessionId) {
|
|
171
|
+
let customUrlToUse;
|
|
172
|
+
if (!sessionId) {
|
|
173
|
+
return this.proxyUrls[this.nextCustomUrlIndex++ % this.proxyUrls.length];
|
|
174
|
+
}
|
|
175
|
+
if (this.usedProxyUrls.has(sessionId)) {
|
|
176
|
+
customUrlToUse = this.usedProxyUrls.get(sessionId);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
customUrlToUse = this.proxyUrls[this.nextCustomUrlIndex++ % this.proxyUrls.length];
|
|
180
|
+
this.usedProxyUrls.set(sessionId, customUrlToUse);
|
|
181
|
+
}
|
|
182
|
+
return customUrlToUse;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Calls the custom newUrlFunction and checks format of its return value
|
|
186
|
+
*/
|
|
187
|
+
_callNewUrlFunction(sessionId) {
|
|
188
|
+
let proxyUrl;
|
|
189
|
+
try {
|
|
190
|
+
proxyUrl = this.newUrlFunction(sessionId);
|
|
191
|
+
new URL(proxyUrl); // eslint-disable-line no-new
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
this._throwNewUrlFunctionInvalid(err);
|
|
195
|
+
}
|
|
196
|
+
return proxyUrl;
|
|
197
|
+
}
|
|
198
|
+
_throwNewUrlFunctionInvalid(err) {
|
|
199
|
+
throw new Error(`The provided newUrlFunction did not return a valid URL.\nCause: ${err.message}`);
|
|
200
|
+
}
|
|
201
|
+
_throwCannotCombineCustomMethods() {
|
|
202
|
+
throw new Error('Cannot combine custom proxies "options.proxyUrls" with custom generating function "options.newUrlFunction".');
|
|
203
|
+
}
|
|
204
|
+
_throwNoOptionsProvided() {
|
|
205
|
+
throw new Error('One of "options.proxyUrls" or "options.newUrlFunction" needs to be provided.');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
exports.ProxyConfiguration = ProxyConfiguration;
|
|
209
|
+
//# sourceMappingURL=proxy_configuration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy_configuration.js","sourceRoot":"","sources":["../src/proxy_configuration.ts"],"names":[],"mappings":";;;;AAAA,oDAAoB;AACpB,6DAA6B;AAC7B,mDAAgD;AAsFhD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAa,kBAAkB;IAQ3B;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH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AAE;YACV,IAAI,CAAC,2BAA2B,CAAC,GAAY,CAAC,CAAC;SAClD;QAED,OAAO,QAAQ,CAAC;IACpB,CAAC;IAES,2BAA2B,CAAC,GAAU;QAC5C,MAAM,IAAI,KAAK,CAAC,mEAAmE,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;IACtG,CAAC;IAES,gCAAgC;QACtC,MAAM,IAAI,KAAK,CAAC,6GAA6G,CAAC,CAAC;IACnI,CAAC;IAES,uBAAuB;QAC7B,MAAM,IAAI,KAAK,CAAC,8EAA8E,CAAC,CAAC;IACpG,CAAC;CACJ;AArJD,gDAqJC"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { BinaryLike } from 'crypto';
|
|
3
|
+
import { AllowedHttpMethods } from './typedefs';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a URL to be crawled, optionally including HTTP method, headers, payload and other metadata.
|
|
6
|
+
* The `Request` object also stores information about errors that occurred during processing of the request.
|
|
7
|
+
*
|
|
8
|
+
* Each `Request` instance has the `uniqueKey` property, which can be either specified
|
|
9
|
+
* manually in the constructor or generated automatically from the URL. Two requests with the same `uniqueKey`
|
|
10
|
+
* are considered as pointing to the same web resource. This behavior applies to all Apify SDK classes,
|
|
11
|
+
* such as {@link RequestList}, {@link RequestQueue}, {@link PuppeteerCrawler} or {@link PlaywrightCrawler}.
|
|
12
|
+
*
|
|
13
|
+
* Example use:
|
|
14
|
+
*
|
|
15
|
+
* ```javascript
|
|
16
|
+
* const request = new Request({
|
|
17
|
+
* url: 'http://www.example.com',
|
|
18
|
+
* headers: { Accept: 'application/json' },
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* ...
|
|
22
|
+
*
|
|
23
|
+
* request.userData.foo = 'bar';
|
|
24
|
+
* request.pushErrorMessage(new Error('Request failed!'));
|
|
25
|
+
*
|
|
26
|
+
* ...
|
|
27
|
+
*
|
|
28
|
+
* const foo = request.userData.foo;
|
|
29
|
+
* ```
|
|
30
|
+
* @category Sources
|
|
31
|
+
*/
|
|
32
|
+
export declare class Request {
|
|
33
|
+
/** Request ID */
|
|
34
|
+
id?: string;
|
|
35
|
+
/** URL of the web page to crawl. */
|
|
36
|
+
url: string;
|
|
37
|
+
/**
|
|
38
|
+
* An actually loaded URL after redirects, if present. HTTP redirects are guaranteed
|
|
39
|
+
* to be included.
|
|
40
|
+
*
|
|
41
|
+
* When using {@link PuppeteerCrawler} or {@link PlaywrightCrawler}, meta tag and JavaScript redirects may,
|
|
42
|
+
* or may not be included, depending on their nature. This generally means that redirects,
|
|
43
|
+
* which happen immediately will most likely be included, but delayed redirects will not.
|
|
44
|
+
*/
|
|
45
|
+
loadedUrl?: string;
|
|
46
|
+
/**
|
|
47
|
+
* A unique key identifying the request.
|
|
48
|
+
* Two requests with the same `uniqueKey` are considered as pointing to the same URL.
|
|
49
|
+
*/
|
|
50
|
+
uniqueKey: string;
|
|
51
|
+
/** HTTP method, e.g. `GET` or `POST`. */
|
|
52
|
+
method: AllowedHttpMethods;
|
|
53
|
+
/** HTTP request payload, e.g. for POST requests. */
|
|
54
|
+
payload?: string;
|
|
55
|
+
/** The `true` value indicates that the request will not be automatically retried on error. */
|
|
56
|
+
noRetry: boolean;
|
|
57
|
+
/** Indicates the number of times the crawling of the request has been retried on error. */
|
|
58
|
+
retryCount: number;
|
|
59
|
+
/** An array of error messages from request processing. */
|
|
60
|
+
errorMessages: string[];
|
|
61
|
+
/** Object with HTTP headers. Key is header name, value is the value. */
|
|
62
|
+
headers?: Record<string, string>;
|
|
63
|
+
/** Custom user data assigned to the request. */
|
|
64
|
+
userData?: Record<string, unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* ISO datetime string that indicates the time when the request has been processed.
|
|
67
|
+
* Is `null` if the request has not been crawled yet.
|
|
68
|
+
*/
|
|
69
|
+
handledAt?: string;
|
|
70
|
+
/**
|
|
71
|
+
* `Request` parameters including the URL, HTTP method and headers, and others.
|
|
72
|
+
*/
|
|
73
|
+
constructor(options: RequestOptions);
|
|
74
|
+
/**
|
|
75
|
+
* Stores information about an error that occurred during processing of this request.
|
|
76
|
+
*
|
|
77
|
+
* You should always use Error instances when throwing errors in JavaScript.
|
|
78
|
+
*
|
|
79
|
+
* Nevertheless, to improve the debugging experience when using third party libraries
|
|
80
|
+
* that may not always throw an Error instance, the function performs a type
|
|
81
|
+
* inspection of the passed argument and attempts to extract as much information
|
|
82
|
+
* as possible, since just throwing a bad type error makes any debugging rather difficult.
|
|
83
|
+
*
|
|
84
|
+
* @param errorOrMessage Error object or error message to be stored in the request.
|
|
85
|
+
* @param [options]
|
|
86
|
+
*/
|
|
87
|
+
pushErrorMessage(errorOrMessage: unknown, options?: PushErrorMessageOptions): void;
|
|
88
|
+
protected _computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey }: ComputeUniqueKeyOptions): string;
|
|
89
|
+
protected _hashPayload(payload: BinaryLike): string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Specifies required and optional fields for constructing a {@link Request}.
|
|
93
|
+
*/
|
|
94
|
+
export interface RequestOptions {
|
|
95
|
+
/** URL of the web page to crawl. It must be a non-empty string. */
|
|
96
|
+
url: string;
|
|
97
|
+
/**
|
|
98
|
+
* A unique key identifying the request.
|
|
99
|
+
* Two requests with the same `uniqueKey` are considered as pointing to the same URL.
|
|
100
|
+
*
|
|
101
|
+
* If `uniqueKey` is not provided, then it is automatically generated by normalizing the URL.
|
|
102
|
+
* For example, the URL of `HTTP://www.EXAMPLE.com/something/` will produce the `uniqueKey`
|
|
103
|
+
* of `http://www.example.com/something`.
|
|
104
|
+
*
|
|
105
|
+
* The `keepUrlFragment` option determines whether URL hash fragment is included in the `uniqueKey` or not.
|
|
106
|
+
*
|
|
107
|
+
* The `useExtendedUniqueKey` options determines whether method and payload are included in the `uniqueKey`,
|
|
108
|
+
* producing a `uniqueKey` in the following format: `METHOD(payloadHash):normalizedUrl`. This is useful
|
|
109
|
+
* when requests point to the same URL, but with different methods and payloads. For example: form submits.
|
|
110
|
+
*
|
|
111
|
+
* Pass an arbitrary non-empty text value to the `uniqueKey` property
|
|
112
|
+
* to override the default behavior and specify which URLs shall be considered equal.
|
|
113
|
+
*/
|
|
114
|
+
uniqueKey?: string;
|
|
115
|
+
/** @default 'GET' */
|
|
116
|
+
method?: AllowedHttpMethods | Lowercase<AllowedHttpMethods>;
|
|
117
|
+
/** HTTP request payload, e.g. for POST requests. */
|
|
118
|
+
payload?: string;
|
|
119
|
+
/**
|
|
120
|
+
* HTTP headers in the following format:
|
|
121
|
+
* ```
|
|
122
|
+
* {
|
|
123
|
+
* Accept: 'text/html',
|
|
124
|
+
* 'Content-Type': 'application/json'
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
headers?: Record<string, string>;
|
|
129
|
+
/**
|
|
130
|
+
* Custom user data assigned to the request. Use this to save any request related data to the
|
|
131
|
+
* request's scope, keeping them accessible on retries, failures etc.
|
|
132
|
+
*/
|
|
133
|
+
userData?: Record<string, unknown>;
|
|
134
|
+
/**
|
|
135
|
+
* If `false` then the hash part of a URL is removed when computing the `uniqueKey` property.
|
|
136
|
+
* For example, this causes the `http://www.example.com#foo` and `http://www.example.com#bar` URLs
|
|
137
|
+
* to have the same `uniqueKey` of `http://www.example.com` and thus the URLs are considered equal.
|
|
138
|
+
* Note that this option only has an effect if `uniqueKey` is not set.
|
|
139
|
+
* @default false
|
|
140
|
+
*/
|
|
141
|
+
keepUrlFragment?: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* If `true` then the `uniqueKey` is computed not only from the URL, but also from the method and payload
|
|
144
|
+
* properties. This is useful when making requests to the same URL that are differentiated by method
|
|
145
|
+
* or payload, such as form submit navigations in browsers.
|
|
146
|
+
* @default false
|
|
147
|
+
*/
|
|
148
|
+
useExtendedUniqueKey?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* The `true` value indicates that the request will not be automatically retried on error.
|
|
151
|
+
* @default false
|
|
152
|
+
*/
|
|
153
|
+
noRetry?: boolean;
|
|
154
|
+
/** @internal */
|
|
155
|
+
id?: string;
|
|
156
|
+
/** @internal */
|
|
157
|
+
handledAt?: string;
|
|
158
|
+
}
|
|
159
|
+
export interface PushErrorMessageOptions {
|
|
160
|
+
/**
|
|
161
|
+
* Only push the error message without stack trace when true.
|
|
162
|
+
* @default false
|
|
163
|
+
*/
|
|
164
|
+
omitStack?: boolean;
|
|
165
|
+
}
|
|
166
|
+
interface ComputeUniqueKeyOptions {
|
|
167
|
+
url: string;
|
|
168
|
+
method: AllowedHttpMethods;
|
|
169
|
+
payload?: string | Buffer;
|
|
170
|
+
keepUrlFragment?: boolean;
|
|
171
|
+
useExtendedUniqueKey?: boolean;
|
|
172
|
+
}
|
|
173
|
+
export {};
|
|
174
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":";AACA,OAAe,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAI5C,OAAO,EAAE,kBAAkB,EAAQ,MAAM,YAAY,CAAC;AAqBtD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,OAAO;IAChB,iBAAiB;IACjB,EAAE,CAAC,EAAE,MAAM,CAAC;IAEZ,oCAAoC;IACpC,GAAG,EAAE,MAAM,CAAC;IAEZ;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB,yCAAyC;IACzC,MAAM,EAAE,kBAAkB,CAAC;IAE3B,oDAAoD;IACpD,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,8FAA8F;IAC9F,OAAO,EAAE,OAAO,CAAC;IAEjB,2FAA2F;IAC3F,UAAU,EAAE,MAAM,CAAC;IAEnB,0DAA0D;IAC1D,aAAa,EAAE,MAAM,EAAE,CAAC;I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AS,CAAC,EAAE,MAAM,CAAC;CAEtB;AAED,MAAM,WAAW,uBAAuB;IACpC;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,UAAU,uBAAuB;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,kBAAkB,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAClC"}
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Request = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const utilities_1 = require("@apify/utilities");
|
|
6
|
+
const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
|
7
|
+
const ow_1 = tslib_1.__importStar(require("ow"));
|
|
8
|
+
const util_1 = tslib_1.__importDefault(require("util"));
|
|
9
|
+
const log_1 = require("./log");
|
|
10
|
+
const typedefs_1 = require("./typedefs");
|
|
11
|
+
// new properties on the Request object breaks serialization
|
|
12
|
+
const log = log_1.log.child({ prefix: 'Request' });
|
|
13
|
+
const requestOptionalPredicates = {
|
|
14
|
+
id: ow_1.default.optional.string,
|
|
15
|
+
loadedUrl: ow_1.default.optional.string.url,
|
|
16
|
+
uniqueKey: ow_1.default.optional.string,
|
|
17
|
+
method: ow_1.default.optional.string,
|
|
18
|
+
payload: ow_1.default.optional.any(ow_1.default.string, ow_1.default.buffer),
|
|
19
|
+
noRetry: ow_1.default.optional.boolean,
|
|
20
|
+
retryCount: ow_1.default.optional.number,
|
|
21
|
+
errorMessages: ow_1.default.optional.array.ofType(ow_1.default.string),
|
|
22
|
+
headers: ow_1.default.optional.object,
|
|
23
|
+
userData: ow_1.default.optional.object,
|
|
24
|
+
handledAt: ow_1.default.optional.any(ow_1.default.string.date, ow_1.default.date),
|
|
25
|
+
keepUrlFragment: ow_1.default.optional.boolean,
|
|
26
|
+
useExtendedUniqueKey: ow_1.default.optional.boolean,
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Represents a URL to be crawled, optionally including HTTP method, headers, payload and other metadata.
|
|
30
|
+
* The `Request` object also stores information about errors that occurred during processing of the request.
|
|
31
|
+
*
|
|
32
|
+
* Each `Request` instance has the `uniqueKey` property, which can be either specified
|
|
33
|
+
* manually in the constructor or generated automatically from the URL. Two requests with the same `uniqueKey`
|
|
34
|
+
* are considered as pointing to the same web resource. This behavior applies to all Apify SDK classes,
|
|
35
|
+
* such as {@link RequestList}, {@link RequestQueue}, {@link PuppeteerCrawler} or {@link PlaywrightCrawler}.
|
|
36
|
+
*
|
|
37
|
+
* Example use:
|
|
38
|
+
*
|
|
39
|
+
* ```javascript
|
|
40
|
+
* const request = new Request({
|
|
41
|
+
* url: 'http://www.example.com',
|
|
42
|
+
* headers: { Accept: 'application/json' },
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* ...
|
|
46
|
+
*
|
|
47
|
+
* request.userData.foo = 'bar';
|
|
48
|
+
* request.pushErrorMessage(new Error('Request failed!'));
|
|
49
|
+
*
|
|
50
|
+
* ...
|
|
51
|
+
*
|
|
52
|
+
* const foo = request.userData.foo;
|
|
53
|
+
* ```
|
|
54
|
+
* @category Sources
|
|
55
|
+
*/
|
|
56
|
+
class Request {
|
|
57
|
+
/**
|
|
58
|
+
* `Request` parameters including the URL, HTTP method and headers, and others.
|
|
59
|
+
*/
|
|
60
|
+
constructor(options) {
|
|
61
|
+
/** Request ID */
|
|
62
|
+
Object.defineProperty(this, "id", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
/** URL of the web page to crawl. */
|
|
69
|
+
Object.defineProperty(this, "url", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: void 0
|
|
74
|
+
});
|
|
75
|
+
/**
|
|
76
|
+
* An actually loaded URL after redirects, if present. HTTP redirects are guaranteed
|
|
77
|
+
* to be included.
|
|
78
|
+
*
|
|
79
|
+
* When using {@link PuppeteerCrawler} or {@link PlaywrightCrawler}, meta tag and JavaScript redirects may,
|
|
80
|
+
* or may not be included, depending on their nature. This generally means that redirects,
|
|
81
|
+
* which happen immediately will most likely be included, but delayed redirects will not.
|
|
82
|
+
*/
|
|
83
|
+
Object.defineProperty(this, "loadedUrl", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* A unique key identifying the request.
|
|
91
|
+
* Two requests with the same `uniqueKey` are considered as pointing to the same URL.
|
|
92
|
+
*/
|
|
93
|
+
Object.defineProperty(this, "uniqueKey", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true,
|
|
97
|
+
value: void 0
|
|
98
|
+
});
|
|
99
|
+
/** HTTP method, e.g. `GET` or `POST`. */
|
|
100
|
+
Object.defineProperty(this, "method", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: void 0
|
|
105
|
+
});
|
|
106
|
+
/** HTTP request payload, e.g. for POST requests. */
|
|
107
|
+
Object.defineProperty(this, "payload", {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: void 0
|
|
112
|
+
});
|
|
113
|
+
/** The `true` value indicates that the request will not be automatically retried on error. */
|
|
114
|
+
Object.defineProperty(this, "noRetry", {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
writable: true,
|
|
118
|
+
value: void 0
|
|
119
|
+
});
|
|
120
|
+
/** Indicates the number of times the crawling of the request has been retried on error. */
|
|
121
|
+
Object.defineProperty(this, "retryCount", {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
configurable: true,
|
|
124
|
+
writable: true,
|
|
125
|
+
value: void 0
|
|
126
|
+
});
|
|
127
|
+
/** An array of error messages from request processing. */
|
|
128
|
+
Object.defineProperty(this, "errorMessages", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: void 0
|
|
133
|
+
});
|
|
134
|
+
/** Object with HTTP headers. Key is header name, value is the value. */
|
|
135
|
+
Object.defineProperty(this, "headers", {
|
|
136
|
+
enumerable: true,
|
|
137
|
+
configurable: true,
|
|
138
|
+
writable: true,
|
|
139
|
+
value: void 0
|
|
140
|
+
});
|
|
141
|
+
/** Custom user data assigned to the request. */
|
|
142
|
+
Object.defineProperty(this, "userData", {
|
|
143
|
+
enumerable: true,
|
|
144
|
+
configurable: true,
|
|
145
|
+
writable: true,
|
|
146
|
+
value: void 0
|
|
147
|
+
});
|
|
148
|
+
/**
|
|
149
|
+
* ISO datetime string that indicates the time when the request has been processed.
|
|
150
|
+
* Is `null` if the request has not been crawled yet.
|
|
151
|
+
*/
|
|
152
|
+
Object.defineProperty(this, "handledAt", {
|
|
153
|
+
enumerable: true,
|
|
154
|
+
configurable: true,
|
|
155
|
+
writable: true,
|
|
156
|
+
value: void 0
|
|
157
|
+
});
|
|
158
|
+
(0, ow_1.default)(options, 'RequestOptions', ow_1.default.object);
|
|
159
|
+
(0, ow_1.default)(options.url, 'RequestOptions.url', ow_1.default.string);
|
|
160
|
+
// 'ow' validation is slow, because it checks all predicates
|
|
161
|
+
// even if the validated object has only 1 property.
|
|
162
|
+
// This custom validation loop iterates only over existing
|
|
163
|
+
// properties and speeds up the validation cca 3-fold.
|
|
164
|
+
// See https://github.com/sindresorhus/ow/issues/193
|
|
165
|
+
(0, typedefs_1.keys)(options).forEach((prop) => {
|
|
166
|
+
const predicate = requestOptionalPredicates[prop];
|
|
167
|
+
const value = options[prop];
|
|
168
|
+
if (predicate) {
|
|
169
|
+
(0, ow_1.default)(value, `RequestOptions.${prop}`, predicate);
|
|
170
|
+
// 'url' is checked above because it's not optional
|
|
171
|
+
}
|
|
172
|
+
else if (prop !== 'url') {
|
|
173
|
+
const msg = `Did not expect property \`${prop}\` to exist, got \`${value}\` in object \`RequestOptions\``;
|
|
174
|
+
throw new ow_1.ArgumentError(msg, this.constructor);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
const { id, url, loadedUrl, uniqueKey, payload, noRetry = false, retryCount = 0, errorMessages = [], headers = {}, userData = {}, handledAt, keepUrlFragment = false, useExtendedUniqueKey = false, } = options;
|
|
178
|
+
let { method = 'GET', } = options;
|
|
179
|
+
method = method.toUpperCase();
|
|
180
|
+
if (method === 'GET' && payload)
|
|
181
|
+
throw new Error('Request with GET method cannot have a payload.');
|
|
182
|
+
this.id = id;
|
|
183
|
+
this.url = url;
|
|
184
|
+
this.loadedUrl = loadedUrl;
|
|
185
|
+
this.uniqueKey = uniqueKey || this._computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey });
|
|
186
|
+
this.method = method;
|
|
187
|
+
this.payload = payload;
|
|
188
|
+
this.noRetry = noRetry;
|
|
189
|
+
this.retryCount = retryCount;
|
|
190
|
+
this.errorMessages = [...errorMessages];
|
|
191
|
+
this.headers = { ...headers };
|
|
192
|
+
this.userData = { ...userData };
|
|
193
|
+
this.handledAt = handledAt instanceof Date ? handledAt.toISOString() : handledAt;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Stores information about an error that occurred during processing of this request.
|
|
197
|
+
*
|
|
198
|
+
* You should always use Error instances when throwing errors in JavaScript.
|
|
199
|
+
*
|
|
200
|
+
* Nevertheless, to improve the debugging experience when using third party libraries
|
|
201
|
+
* that may not always throw an Error instance, the function performs a type
|
|
202
|
+
* inspection of the passed argument and attempts to extract as much information
|
|
203
|
+
* as possible, since just throwing a bad type error makes any debugging rather difficult.
|
|
204
|
+
*
|
|
205
|
+
* @param errorOrMessage Error object or error message to be stored in the request.
|
|
206
|
+
* @param [options]
|
|
207
|
+
*/
|
|
208
|
+
pushErrorMessage(errorOrMessage, options = {}) {
|
|
209
|
+
const { omitStack } = options;
|
|
210
|
+
let message;
|
|
211
|
+
const type = typeof errorOrMessage;
|
|
212
|
+
if (type === 'object') {
|
|
213
|
+
if (!errorOrMessage) {
|
|
214
|
+
message = 'null';
|
|
215
|
+
}
|
|
216
|
+
else if (errorOrMessage instanceof Error) {
|
|
217
|
+
message = omitStack
|
|
218
|
+
? errorOrMessage.message
|
|
219
|
+
// .stack includes the message
|
|
220
|
+
: errorOrMessage.stack;
|
|
221
|
+
}
|
|
222
|
+
else if (Reflect.has(Object(errorOrMessage), 'message')) {
|
|
223
|
+
message = Reflect.get(Object(errorOrMessage), 'message');
|
|
224
|
+
}
|
|
225
|
+
else if (errorOrMessage.toString() !== '[object Object]') {
|
|
226
|
+
message = errorOrMessage.toString();
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
try {
|
|
230
|
+
message = util_1.default.inspect(errorOrMessage);
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
message = 'Unable to extract any message from the received object.';
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
else if (type === 'undefined') {
|
|
238
|
+
message = 'undefined';
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
message = errorOrMessage.toString();
|
|
242
|
+
}
|
|
243
|
+
this.errorMessages.push(message);
|
|
244
|
+
}
|
|
245
|
+
_computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey }) {
|
|
246
|
+
const normalizedMethod = method.toUpperCase();
|
|
247
|
+
const normalizedUrl = (0, utilities_1.normalizeUrl)(url, keepUrlFragment) || url; // It returns null when url is invalid, causing weird errors.
|
|
248
|
+
if (!useExtendedUniqueKey) {
|
|
249
|
+
if (normalizedMethod !== 'GET' && payload) {
|
|
250
|
+
// Using log.deprecated to log only once. We should add log.once or some such.
|
|
251
|
+
log.deprecated(`We've encountered a ${normalizedMethod} Request with a payload. `
|
|
252
|
+
+ 'This is fine. Just letting you know that if your requests point to the same URL '
|
|
253
|
+
+ 'and differ only in method and payload, you should see the "useExtendedUniqueKey" option of Request constructor.');
|
|
254
|
+
}
|
|
255
|
+
return normalizedUrl;
|
|
256
|
+
}
|
|
257
|
+
const payloadHash = payload ? this._hashPayload(payload) : '';
|
|
258
|
+
return `${normalizedMethod}(${payloadHash}):${normalizedUrl}`;
|
|
259
|
+
}
|
|
260
|
+
_hashPayload(payload) {
|
|
261
|
+
return crypto_1.default
|
|
262
|
+
.createHash('sha256')
|
|
263
|
+
.update(payload)
|
|
264
|
+
.digest('base64')
|
|
265
|
+
.replace(/[+/=]/g, '')
|
|
266
|
+
.substring(0, 8);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
exports.Request = Request;
|
|
270
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":";;;;AAAA,gDAAgD;AAChD,4DAA4C;AAC5C,iDAAsD;AACtD,wDAAwB;AACxB,+BAA0C;AAC1C,yCAAsD;AAEtD,4DAA4D;AAC5D,MAAM,GAAG,GAAG,SAAU,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AAEpD,MAAM,yBAAyB,GAAG;IAC9B,EAAE,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM;IACtB,SAAS,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG;IACjC,SAAS,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM;IAC7B,MAAM,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM;IAC1B,OAAO,EAAE,YAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,YAAE,CAAC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