@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.
Files changed (162) hide show
  1. package/dist/LICENSE.md +201 -0
  2. package/{README.md → dist/README.md} +0 -0
  3. package/dist/autoscaling/autoscaled_pool.d.ts +286 -0
  4. package/dist/autoscaling/autoscaled_pool.d.ts.map +1 -0
  5. package/dist/autoscaling/autoscaled_pool.js +624 -0
  6. package/dist/autoscaling/autoscaled_pool.js.map +1 -0
  7. package/dist/autoscaling/index.d.ts +4 -0
  8. package/dist/autoscaling/index.d.ts.map +1 -0
  9. package/dist/autoscaling/index.js +7 -0
  10. package/dist/autoscaling/index.js.map +1 -0
  11. package/dist/autoscaling/snapshotter.d.ts +193 -0
  12. package/dist/autoscaling/snapshotter.d.ts.map +1 -0
  13. package/dist/autoscaling/snapshotter.js +377 -0
  14. package/dist/autoscaling/snapshotter.js.map +1 -0
  15. package/dist/autoscaling/system_status.d.ts +181 -0
  16. package/dist/autoscaling/system_status.d.ts.map +1 -0
  17. package/dist/autoscaling/system_status.js +194 -0
  18. package/dist/autoscaling/system_status.js.map +1 -0
  19. package/dist/cache_container.d.ts +18 -0
  20. package/dist/cache_container.d.ts.map +1 -0
  21. package/dist/cache_container.js +42 -0
  22. package/dist/cache_container.js.map +1 -0
  23. package/dist/configuration.d.ts +150 -0
  24. package/dist/configuration.d.ts.map +1 -0
  25. package/dist/configuration.js +284 -0
  26. package/dist/configuration.js.map +1 -0
  27. package/dist/constants.d.ts +3 -0
  28. package/dist/constants.d.ts.map +1 -0
  29. package/dist/constants.js +6 -0
  30. package/dist/constants.js.map +1 -0
  31. package/dist/crawlers/crawler_commons.d.ts +23 -0
  32. package/dist/crawlers/crawler_commons.d.ts.map +1 -0
  33. package/dist/crawlers/crawler_commons.js +3 -0
  34. package/dist/crawlers/crawler_commons.js.map +1 -0
  35. package/dist/crawlers/crawler_extension.d.ts +11 -0
  36. package/dist/crawlers/crawler_extension.d.ts.map +1 -0
  37. package/dist/crawlers/crawler_extension.js +30 -0
  38. package/dist/crawlers/crawler_extension.js.map +1 -0
  39. package/dist/crawlers/crawler_utils.d.ts +25 -0
  40. package/dist/crawlers/crawler_utils.d.ts.map +1 -0
  41. package/dist/crawlers/crawler_utils.js +72 -0
  42. package/dist/crawlers/crawler_utils.js.map +1 -0
  43. package/dist/crawlers/index.d.ts +5 -0
  44. package/dist/crawlers/index.d.ts.map +1 -0
  45. package/dist/crawlers/index.js +8 -0
  46. package/dist/crawlers/index.js.map +1 -0
  47. package/dist/crawlers/statistics.d.ts +147 -0
  48. package/dist/crawlers/statistics.d.ts.map +1 -0
  49. package/dist/crawlers/statistics.js +360 -0
  50. package/dist/crawlers/statistics.js.map +1 -0
  51. package/dist/enqueue_links/enqueue_links.d.ts +138 -0
  52. package/dist/enqueue_links/enqueue_links.d.ts.map +1 -0
  53. package/dist/enqueue_links/enqueue_links.js +123 -0
  54. package/dist/enqueue_links/enqueue_links.js.map +1 -0
  55. package/dist/enqueue_links/index.d.ts +3 -0
  56. package/dist/enqueue_links/index.d.ts.map +1 -0
  57. package/dist/enqueue_links/index.js +6 -0
  58. package/dist/enqueue_links/index.js.map +1 -0
  59. package/dist/enqueue_links/shared.d.ts +60 -0
  60. package/dist/enqueue_links/shared.d.ts.map +1 -0
  61. package/dist/enqueue_links/shared.js +148 -0
  62. package/dist/enqueue_links/shared.js.map +1 -0
  63. package/dist/errors.d.ts +8 -0
  64. package/dist/errors.d.ts.map +1 -0
  65. package/dist/errors.js +12 -0
  66. package/dist/errors.js.map +1 -0
  67. package/dist/events/event_manager.d.ts +41 -0
  68. package/dist/events/event_manager.d.ts.map +1 -0
  69. package/dist/events/event_manager.js +103 -0
  70. package/dist/events/event_manager.js.map +1 -0
  71. package/dist/events/index.d.ts +3 -0
  72. package/dist/events/index.d.ts.map +1 -0
  73. package/dist/events/index.js +6 -0
  74. package/dist/events/index.js.map +1 -0
  75. package/dist/events/local_event_manager.d.ts +32 -0
  76. package/dist/events/local_event_manager.d.ts.map +1 -0
  77. package/dist/events/local_event_manager.js +114 -0
  78. package/dist/events/local_event_manager.js.map +1 -0
  79. package/dist/index.d.ts +14 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +17 -0
  82. package/dist/index.js.map +1 -0
  83. package/dist/index.mjs +63 -0
  84. package/dist/log.d.ts +3 -0
  85. package/dist/log.d.ts.map +1 -0
  86. package/dist/log.js +12 -0
  87. package/dist/log.js.map +1 -0
  88. package/dist/package.json +82 -0
  89. package/dist/proxy_configuration.d.ts +181 -0
  90. package/dist/proxy_configuration.d.ts.map +1 -0
  91. package/dist/proxy_configuration.js +209 -0
  92. package/dist/proxy_configuration.js.map +1 -0
  93. package/dist/request.d.ts +174 -0
  94. package/dist/request.d.ts.map +1 -0
  95. package/dist/request.js +270 -0
  96. package/dist/request.js.map +1 -0
  97. package/dist/serialization.d.ts +33 -0
  98. package/dist/serialization.d.ts.map +1 -0
  99. package/dist/serialization.js +146 -0
  100. package/dist/serialization.js.map +1 -0
  101. package/dist/session_pool/errors.d.ts +8 -0
  102. package/dist/session_pool/errors.d.ts.map +1 -0
  103. package/dist/session_pool/errors.js +20 -0
  104. package/dist/session_pool/errors.js.map +1 -0
  105. package/dist/session_pool/events.d.ts +3 -0
  106. package/dist/session_pool/events.d.ts.map +1 -0
  107. package/dist/session_pool/events.js +6 -0
  108. package/dist/session_pool/events.js.map +1 -0
  109. package/dist/session_pool/index.d.ts +6 -0
  110. package/dist/session_pool/index.d.ts.map +1 -0
  111. package/dist/session_pool/index.js +9 -0
  112. package/dist/session_pool/index.js.map +1 -0
  113. package/dist/session_pool/session.d.ts +212 -0
  114. package/dist/session_pool/session.d.ts.map +1 -0
  115. package/dist/session_pool/session.js +360 -0
  116. package/dist/session_pool/session.js.map +1 -0
  117. package/dist/session_pool/session_pool.d.ts +222 -0
  118. package/dist/session_pool/session_pool.d.ts.map +1 -0
  119. package/dist/session_pool/session_pool.js +389 -0
  120. package/dist/session_pool/session_pool.js.map +1 -0
  121. package/dist/session_pool/session_utils.d.ts +9 -0
  122. package/dist/session_pool/session_utils.d.ts.map +1 -0
  123. package/dist/session_pool/session_utils.js +19 -0
  124. package/dist/session_pool/session_utils.js.map +1 -0
  125. package/dist/storages/dataset.d.ts +358 -0
  126. package/dist/storages/dataset.d.ts.map +1 -0
  127. package/dist/storages/dataset.js +415 -0
  128. package/dist/storages/dataset.js.map +1 -0
  129. package/dist/storages/index.d.ts +7 -0
  130. package/dist/storages/index.d.ts.map +1 -0
  131. package/dist/storages/index.js +11 -0
  132. package/dist/storages/index.js.map +1 -0
  133. package/dist/storages/key_value_store.d.ts +242 -0
  134. package/dist/storages/key_value_store.d.ts.map +1 -0
  135. package/dist/storages/key_value_store.js +319 -0
  136. package/dist/storages/key_value_store.js.map +1 -0
  137. package/dist/storages/request_list.d.ts +461 -0
  138. package/dist/storages/request_list.d.ts.map +1 -0
  139. package/dist/storages/request_list.js +715 -0
  140. package/dist/storages/request_list.js.map +1 -0
  141. package/dist/storages/request_queue.d.ts +312 -0
  142. package/dist/storages/request_queue.d.ts.map +1 -0
  143. package/dist/storages/request_queue.js +708 -0
  144. package/dist/storages/request_queue.js.map +1 -0
  145. package/dist/storages/storage.d.ts +260 -0
  146. package/dist/storages/storage.d.ts.map +1 -0
  147. package/dist/storages/storage.js +3 -0
  148. package/dist/storages/storage.js.map +1 -0
  149. package/dist/storages/storage_manager.d.ts +40 -0
  150. package/dist/storages/storage_manager.d.ts.map +1 -0
  151. package/dist/storages/storage_manager.js +119 -0
  152. package/dist/storages/storage_manager.js.map +1 -0
  153. package/dist/tsconfig.build.tsbuildinfo +1 -0
  154. package/dist/typedefs.d.ts +12 -0
  155. package/dist/typedefs.d.ts.map +1 -0
  156. package/dist/typedefs.js +15 -0
  157. package/dist/typedefs.js.map +1 -0
  158. package/dist/validators.d.ts +21 -0
  159. package/dist/validators.d.ts.map +1 -0
  160. package/dist/validators.js +26 -0
  161. package/dist/validators.js.map +1 -0
  162. package/package.json +11 -10
@@ -0,0 +1,33 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
3
+ /**
4
+ * Uses Gzip compression to take an array of values, which can be anything
5
+ * from entries in a Dataset to Requests in a RequestList and compresses
6
+ * them to a Buffer in a memory-efficient way (streaming one by one). Ideally,
7
+ * the largest chunk of memory consumed will be the final compressed Buffer.
8
+ * This could be further improved by outputting a Stream, if and when
9
+ * apify-client supports streams.
10
+ * @internal
11
+ */
12
+ export declare function serializeArray<T>(data: T[]): Promise<Buffer>;
13
+ /**
14
+ * Decompresses a Buffer previously created with compressData (technically,
15
+ * any JSON that is an Array) and collects it into an Array of values
16
+ * in a memory-efficient way (streaming the array items one by one instead
17
+ * of creating a fully decompressed buffer -> full JSON -> full Array all
18
+ * in memory at once. Could be further optimized to ingest a Stream if and
19
+ * when apify-client supports streams.
20
+ * @internal
21
+ */
22
+ export declare function deserializeArray<T extends string | Buffer>(compressedData: Buffer): Promise<T[]>;
23
+ /**
24
+ * Creates a stream that decompresses a Buffer previously created with
25
+ * compressData (technically, any JSON that is an Array) and collects it
26
+ * into an Array of values in a memory-efficient way (streaming the array
27
+ * items one by one instead of creating a fully decompressed buffer
28
+ * -> full JSON -> full Array all in memory at once. Could be further
29
+ * optimized to ingest a Stream if and when apify-client supports streams.
30
+ * @internal
31
+ */
32
+ export declare function createDeserialize(compressedData: Buffer): Readable;
33
+ //# sourceMappingURL=serialization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../src/serialization.ts"],"names":[],"mappings":";AACA,OAAO,EAA8B,QAAQ,EAAY,MAAM,QAAQ,CAAC;AAkDxE;;;;;;;;GAQG;AACH,wBAAsB,cAAc,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAUlE;AAED;;;;;;;;GAQG;AACH,wBAAsB,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAWtG;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,cAAc,EAAE,MAAM,GAAG,QAAQ,CAelE"}
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDeserialize = exports.deserializeArray = exports.serializeArray = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const ow_1 = tslib_1.__importDefault(require("ow"));
6
+ const stream_1 = require("stream");
7
+ const StreamArray_1 = tslib_1.__importDefault(require("stream-json/streamers/StreamArray"));
8
+ const util_1 = tslib_1.__importDefault(require("util"));
9
+ const zlib_1 = tslib_1.__importDefault(require("zlib"));
10
+ const pipeline = util_1.default.promisify(stream_1.pipeline);
11
+ /**
12
+ * Transforms an array of items to a JSON in a streaming
13
+ * fashion to save memory. It operates in batches to speed
14
+ * up the process.
15
+ * @internal
16
+ */
17
+ class ArrayToJson extends stream_1.Readable {
18
+ constructor(data, options = {}) {
19
+ super({
20
+ ...options,
21
+ autoDestroy: true,
22
+ emitClose: true,
23
+ });
24
+ Object.defineProperty(this, "data", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: data
29
+ });
30
+ Object.defineProperty(this, "offset", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: 0
35
+ });
36
+ Object.defineProperty(this, "batchSize", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ const { batchSize = 10000 } = options;
43
+ this.batchSize = batchSize;
44
+ this.data = data;
45
+ this.push('[');
46
+ }
47
+ _read() {
48
+ try {
49
+ const items = this.data.slice(this.offset, this.offset + this.batchSize);
50
+ if (items.length) {
51
+ const json = JSON.stringify(items);
52
+ // Strip brackets to flatten the batch.
53
+ const itemString = json.substring(1, json.length - 1);
54
+ if (this.offset > 0)
55
+ this.push(',', 'utf8');
56
+ this.push(itemString, 'utf8');
57
+ this.offset += this.batchSize;
58
+ }
59
+ else {
60
+ this.push(']');
61
+ this.push(null);
62
+ }
63
+ }
64
+ catch (err) {
65
+ this.emit('error', err);
66
+ }
67
+ }
68
+ }
69
+ /**
70
+ * Uses Gzip compression to take an array of values, which can be anything
71
+ * from entries in a Dataset to Requests in a RequestList and compresses
72
+ * them to a Buffer in a memory-efficient way (streaming one by one). Ideally,
73
+ * the largest chunk of memory consumed will be the final compressed Buffer.
74
+ * This could be further improved by outputting a Stream, if and when
75
+ * apify-client supports streams.
76
+ * @internal
77
+ */
78
+ async function serializeArray(data) {
79
+ (0, ow_1.default)(data, ow_1.default.array);
80
+ const { chunks, collector } = createChunkCollector();
81
+ await pipeline(new ArrayToJson(data), zlib_1.default.createGzip(), collector);
82
+ return Buffer.concat(chunks);
83
+ }
84
+ exports.serializeArray = serializeArray;
85
+ /**
86
+ * Decompresses a Buffer previously created with compressData (technically,
87
+ * any JSON that is an Array) and collects it into an Array of values
88
+ * in a memory-efficient way (streaming the array items one by one instead
89
+ * of creating a fully decompressed buffer -> full JSON -> full Array all
90
+ * in memory at once. Could be further optimized to ingest a Stream if and
91
+ * when apify-client supports streams.
92
+ * @internal
93
+ */
94
+ async function deserializeArray(compressedData) {
95
+ (0, ow_1.default)(compressedData, ow_1.default.buffer);
96
+ const { chunks, collector } = createChunkCollector({ fromValuesStream: true });
97
+ await pipeline(stream_1.Readable.from([compressedData]), zlib_1.default.createGunzip(), StreamArray_1.default.withParser(), collector);
98
+ return chunks;
99
+ }
100
+ exports.deserializeArray = deserializeArray;
101
+ /**
102
+ * Creates a stream that decompresses a Buffer previously created with
103
+ * compressData (technically, any JSON that is an Array) and collects it
104
+ * into an Array of values in a memory-efficient way (streaming the array
105
+ * items one by one instead of creating a fully decompressed buffer
106
+ * -> full JSON -> full Array all in memory at once. Could be further
107
+ * optimized to ingest a Stream if and when apify-client supports streams.
108
+ * @internal
109
+ */
110
+ function createDeserialize(compressedData) {
111
+ (0, ow_1.default)(compressedData, ow_1.default.buffer);
112
+ const streamArray = StreamArray_1.default.withParser();
113
+ const destination = pluckValue(streamArray);
114
+ (0, stream_1.pipeline)(stream_1.Readable.from([compressedData]), zlib_1.default.createGunzip(), destination,
115
+ // TODO: figure this out
116
+ // @ts-expect-error Somethings wrong here, the types are wrong but tests fail if we correct the code to make them right
117
+ (err) => destination.emit(err));
118
+ return destination;
119
+ }
120
+ exports.createDeserialize = createDeserialize;
121
+ function createChunkCollector(options = {}) {
122
+ const { fromValuesStream = false } = options;
123
+ const chunks = [];
124
+ const collector = new stream_1.Writable({
125
+ decodeStrings: false,
126
+ objectMode: fromValuesStream,
127
+ write(chunk, _nil, callback) {
128
+ chunks.push(fromValuesStream ? chunk.value : chunk);
129
+ callback();
130
+ },
131
+ writev(chunkObjects, callback) {
132
+ const buffers = chunkObjects.map(({ chunk }) => {
133
+ return fromValuesStream ? chunk.value : chunk;
134
+ });
135
+ chunkObjects.push(...buffers);
136
+ callback();
137
+ },
138
+ });
139
+ return { collector, chunks };
140
+ }
141
+ function pluckValue(streamArray) {
142
+ const realPush = streamArray.push.bind(streamArray);
143
+ streamArray.push = (obj) => realPush(obj && obj.value);
144
+ return streamArray;
145
+ }
146
+ //# sourceMappingURL=serialization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.js","sourceRoot":"","sources":["../src/serialization.ts"],"names":[],"mappings":";;;;AAAA,oDAAoB;AACpB,mCAAwE;AAExE,4FAA4D;AAC5D,wDAAwB;AACxB,wDAAwB;AAExB,MAAM,QAAQ,GAAG,cAAI,CAAC,SAAS,CAAC,iBAAc,CAAC,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,WAAe,SAAQ,iBAAQ;IAIjC,YAAoB,IAAS,EAAE,UAAkC,EAAE;QAC/D,KAAK,CAAC;YACF,GAAG,OAAO;YACV,WAAW,EAAE,IAAI;YACjB,SAAS,EAAE,IAAI;SAClB,CAAC,CAAC;;;;;mB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C,EAAE;IACjG,MAAM,EAAE,gBAAgB,GAAG,KAAK,EAAE,GAAG,OAAO,CAAC;IAC7C,MAAM,MAAM,GAAQ,EAAE,CAAC;IACvB,MAAM,SAAS,GAAG,IAAI,iBAAQ,CAAC;QAC3B,aAAa,EAAE,KAAK;QACpB,UAAU,EAAE,gBAAgB;QAC5B,KAAK,CAAC,KAAK,EAAE,IAAI,EAAE,QAAQ;YACvB,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;YACpD,QAAQ,EAAE,CAAC;QACf,CAAC;QACD,MAAM,CAAC,YAAY,EAAE,QAAQ;YACzB,MAAM,OAAO,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE;gBAC3C,OAAO,gBAAgB,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;YAClD,CAAC,CAAC,CAAC;YACH,YAAY,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC;YAC9B,QAAQ,EAAE,CAAC;QACf,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;AACjC,CAAC;AAED,SAAS,UAAU,CAAC,WAAkB;IAClC,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACpD,WAAW,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IACvD,OAAO,WAAW,CAAC;AACvB,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @ignore
3
+ */
4
+ export declare class CookieParseError extends Error {
5
+ readonly cookieHeaderString: unknown;
6
+ constructor(cookieHeaderString: unknown);
7
+ }
8
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/session_pool/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IAC3B,QAAQ,CAAC,kBAAkB,EAAE,OAAO;gBAA3B,kBAAkB,EAAE,OAAO;CAInD"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CookieParseError = void 0;
4
+ /**
5
+ * @ignore
6
+ */
7
+ class CookieParseError extends Error {
8
+ constructor(cookieHeaderString) {
9
+ super(`Could not parse cookie header string: ${cookieHeaderString}`);
10
+ Object.defineProperty(this, "cookieHeaderString", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: cookieHeaderString
15
+ });
16
+ Error.captureStackTrace(this, CookieParseError);
17
+ }
18
+ }
19
+ exports.CookieParseError = CookieParseError;
20
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/session_pool/errors.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,gBAAiB,SAAQ,KAAK;IACvC,YAAqB,kBAA2B;QAC5C,KAAK,CAAC,yCAAyC,kBAAkB,EAAE,CAAC,CAAC;;;;;mBADpD;;QAEjB,KAAK,CAAC,iBAAiB,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC;IACpD,CAAC;CACJ;AALD,4CAKC"}
@@ -0,0 +1,3 @@
1
+ /** @internal */
2
+ export declare const EVENT_SESSION_RETIRED = "sessionRetired";
3
+ //# sourceMappingURL=events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/session_pool/events.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,eAAO,MAAM,qBAAqB,mBAAmB,CAAC"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EVENT_SESSION_RETIRED = void 0;
4
+ /** @internal */
5
+ exports.EVENT_SESSION_RETIRED = 'sessionRetired';
6
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/session_pool/events.ts"],"names":[],"mappings":";;;AAAA,gBAAgB;AACH,QAAA,qBAAqB,GAAG,gBAAgB,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from './errors';
2
+ export * from './events';
3
+ export * from './session';
4
+ export * from './session_pool';
5
+ export * from './session_utils';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/session_pool/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./errors"), exports);
5
+ tslib_1.__exportStar(require("./events"), exports);
6
+ tslib_1.__exportStar(require("./session"), exports);
7
+ tslib_1.__exportStar(require("./session_pool"), exports);
8
+ tslib_1.__exportStar(require("./session_utils"), exports);
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/session_pool/index.ts"],"names":[],"mappings":";;;AAAA,mDAAyB;AACzB,mDAAyB;AACzB,oDAA0B;AAC1B,yDAA+B;AAC/B,0DAAgC"}
@@ -0,0 +1,212 @@
1
+ /// <reference types="node" />
2
+ import { Log } from '@apify/log';
3
+ import { Cookie as BrowserPoolCookie } from '@crawlee/browser-pool';
4
+ import { IncomingMessage } from 'http';
5
+ import { HTTPResponse, Protocol } from 'puppeteer';
6
+ import { Cookie, CookieJar } from 'tough-cookie';
7
+ import { Dictionary } from '../typedefs';
8
+ import { SessionPool } from './session_pool';
9
+ export declare type PuppeteerCookie = Protocol.Network.Cookie | BrowserPoolCookie;
10
+ /**
11
+ * Persistable {@link Session} state.
12
+ */
13
+ export interface SessionState {
14
+ id: string;
15
+ cookieJar: CookieJar.Serialized;
16
+ userData: object;
17
+ errorScore: number;
18
+ maxErrorScore: number;
19
+ errorScoreDecrement: number;
20
+ usageCount: number;
21
+ maxUsageCount: number;
22
+ expiresAt: string;
23
+ createdAt: string;
24
+ }
25
+ export interface SessionOptions {
26
+ /** Id of session used for generating fingerprints. It is used as proxy session name. */
27
+ id?: string;
28
+ /**
29
+ * Number of seconds after which the session is considered as expired.
30
+ * @default 3000
31
+ */
32
+ maxAgeSecs?: number;
33
+ /** Object where custom user data can be stored. For example custom headers. */
34
+ userData?: Dictionary;
35
+ /**
36
+ * Maximum number of marking session as blocked usage.
37
+ * If the `errorScore` reaches the `maxErrorScore` session is marked as block and it is thrown away.
38
+ * It starts at 0. Calling the `markBad` function increases the `errorScore` by 1.
39
+ * Calling the `markGood` will decrease the `errorScore` by `errorScoreDecrement`
40
+ * @default 3
41
+ */
42
+ maxErrorScore?: number;
43
+ /**
44
+ * It is used for healing the session.
45
+ * For example: if your session is marked bad two times, but it is successful on the third attempt it's errorScore
46
+ * is decremented by this number.
47
+ * @default 0.5
48
+ */
49
+ errorScoreDecrement?: number;
50
+ /** Date of creation. */
51
+ createdAt?: Date;
52
+ /** Date of expiration. */
53
+ expiresAt?: Date;
54
+ /**
55
+ * Indicates how many times the session has been used.
56
+ * @default 0
57
+ */
58
+ usageCount?: number;
59
+ /**
60
+ * Session should be used only a limited amount of times.
61
+ * This number indicates how many times the session is going to be used, before it is thrown away.
62
+ * @default 50
63
+ */
64
+ maxUsageCount?: number;
65
+ /** SessionPool instance. Session will emit the `sessionRetired` event on this instance. */
66
+ sessionPool?: SessionPool;
67
+ log?: Log;
68
+ errorScore?: number;
69
+ cookieJar?: CookieJar;
70
+ }
71
+ /**
72
+ * Sessions are used to store information such as cookies and can be used for generating fingerprints and proxy sessions.
73
+ * You can imagine each session as a specific user, with its own cookies, IP (via proxy) and potentially a unique browser fingerprint.
74
+ * Session internal state can be enriched with custom user data for example some authorization tokens and specific headers in general.
75
+ * @category Scaling
76
+ */
77
+ export declare class Session {
78
+ readonly id: string;
79
+ private maxAgeSecs;
80
+ userData: Dictionary;
81
+ private maxErrorScore;
82
+ private errorScoreDecrement;
83
+ private createdAt;
84
+ private expiresAt;
85
+ private usageCount;
86
+ private maxUsageCount;
87
+ private sessionPool;
88
+ private errorScore;
89
+ private cookieJar;
90
+ private log;
91
+ /**
92
+ * Session configuration.
93
+ */
94
+ constructor(options: SessionOptions);
95
+ /**
96
+ * Indicates whether the session is blocked.
97
+ * Session is blocked once it reaches the `maxErrorScore`.
98
+ */
99
+ isBlocked(): boolean;
100
+ /**
101
+ * Indicates whether the session is expired.
102
+ * Session expiration is determined by the `maxAgeSecs`.
103
+ * Once the session is older than `createdAt + maxAgeSecs` the session is considered expired.
104
+ */
105
+ isExpired(): boolean;
106
+ /**
107
+ * Indicates whether the session is used maximum number of times.
108
+ * Session maximum usage count can be changed by `maxUsageCount` parameter.
109
+ */
110
+ isMaxUsageCountReached(): boolean;
111
+ /**
112
+ * Indicates whether the session can be used for next requests.
113
+ * Session is usable when it is not expired, not blocked and the maximum usage count has not be reached.
114
+ */
115
+ isUsable(): boolean;
116
+ /**
117
+ * This method should be called after a successful session usage.
118
+ * It increases `usageCount` and potentially lowers the `errorScore` by the `errorScoreDecrement`.
119
+ */
120
+ markGood(): void;
121
+ /**
122
+ * Gets session state for persistence in KeyValueStore.
123
+ * @returns Represents session internal state.
124
+ */
125
+ getState(): SessionState;
126
+ /**
127
+ * Marks session as blocked and emits event on the `SessionPool`
128
+ * This method should be used if the session usage was unsuccessful
129
+ * and you are sure that it is because of the session configuration and not any external matters.
130
+ * For example when server returns 403 status code.
131
+ * If the session does not work due to some external factors as server error such as 5XX you probably want to use `markBad` method.
132
+ */
133
+ retire(): void;
134
+ /**
135
+ * Increases usage and error count.
136
+ * Should be used when the session has been used unsuccessfully. For example because of timeouts.
137
+ */
138
+ markBad(): void;
139
+ /**
140
+ * With certain status codes: `401`, `403` or `429` we can be certain
141
+ * that the target website is blocking us. This function helps to do this conveniently
142
+ * by retiring the session when such code is received. Optionally the default status
143
+ * codes can be extended in the second parameter.
144
+ * @param statusCode HTTP status code.
145
+ * @param [blockedStatusCodes] Custom HTTP status codes that means blocking on particular website.
146
+ * @returns Whether the session was retired.
147
+ */
148
+ retireOnBlockedStatusCodes(statusCode: number, blockedStatusCodes?: number[]): boolean;
149
+ /**
150
+ * Saves cookies from an HTTP response to be used with the session.
151
+ * It expects an object with a `headers` property that's either an `Object`
152
+ * (typical Node.js responses) or a `Function` (Puppeteer Response).
153
+ *
154
+ * It then parses and saves the cookies from the `set-cookie` header, if available.
155
+ */
156
+ setCookiesFromResponse(response: IncomingMessage | HTTPResponse | {
157
+ headers: Dictionary<string | string[]>;
158
+ url: string;
159
+ }): void;
160
+ /**
161
+ * Saves an array with cookie objects to be used with the session.
162
+ * The objects should be in the format that
163
+ * [Puppeteer uses](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls),
164
+ * but you can also use this function to set cookies manually:
165
+ *
166
+ * ```
167
+ * [
168
+ * { name: 'cookie1', value: 'my-cookie' },
169
+ * { name: 'cookie2', value: 'your-cookie' }
170
+ * ]
171
+ * ```
172
+ */
173
+ setPuppeteerCookies(cookies: PuppeteerCookie[], url: string): void;
174
+ /**
175
+ * Returns cookies in a format compatible with puppeteer and ready to be used with `page.setCookie`.
176
+ * @param url website url. Only cookies stored for this url will be returned
177
+ */
178
+ getPuppeteerCookies(url: string): PuppeteerCookie[];
179
+ /**
180
+ * Returns cookies saved with the session in the typical
181
+ * key1=value1; key2=value2 format, ready to be used in
182
+ * a cookie header or elsewhere.
183
+ * @returns Represents `Cookie` header.
184
+ */
185
+ getCookieString(url: string): string;
186
+ /**
187
+ * Transforms puppeteer cookie to tough-cookie.
188
+ * @param puppeteerCookie Cookie from puppeteer `page.cookies` method.
189
+ */
190
+ protected _puppeteerCookieToTough(puppeteerCookie: PuppeteerCookie): Cookie;
191
+ /**
192
+ * Transforms tough-cookie to puppeteer cookie.
193
+ * @param toughCookie Cookie from CookieJar
194
+ * @return Cookie from Puppeteer
195
+ */
196
+ protected _toughCookieToPuppeteer(toughCookie: Cookie): PuppeteerCookie;
197
+ /**
198
+ * Sets cookies.
199
+ */
200
+ protected _setCookies(cookies: Cookie[], url: string): void;
201
+ /**
202
+ * Calculate cookie expiration date
203
+ * @param maxAgeSecs
204
+ * @returns Calculated date by session max age seconds.
205
+ */
206
+ protected _getDefaultCookieExpirationDate(maxAgeSecs: number): Date;
207
+ /**
208
+ * Checks if session is not usable. if it is not retires the session.
209
+ */
210
+ protected _maybeSelfRetire(): void;
211
+ }
212
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session_pool/session.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEjC,OAAO,EAAE,MAAM,IAAI,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAEvC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGjD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,oBAAY,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,GAAG,iBAAiB,CAAC;AAK1E;;GAEG;AACH,MAAM,WAAW,YAAY;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,E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