graceful-playwright 1.2.0 → 1.4.0
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/core.d.ts +1 -0
- package/core.js +27 -0
- package/package.json +2 -2
package/core.d.ts
CHANGED
package/core.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.GracefulPage = void 0;
|
|
4
|
+
exports.parseRetryAfter = parseRetryAfter;
|
|
4
5
|
class GracefulPage {
|
|
5
6
|
options;
|
|
6
7
|
constructor(options) {
|
|
@@ -49,6 +50,16 @@ class GracefulPage {
|
|
|
49
50
|
waitUntil: 'domcontentloaded',
|
|
50
51
|
...options,
|
|
51
52
|
});
|
|
53
|
+
if (response && response.status() === 429) {
|
|
54
|
+
let headerValue = await response.headerValue('Retry-After');
|
|
55
|
+
let interval = parseRetryAfter(headerValue);
|
|
56
|
+
if (interval) {
|
|
57
|
+
await sleep(interval);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
let statusText = response.statusText() || 'Too Many Requests';
|
|
61
|
+
throw new Error(statusText);
|
|
62
|
+
}
|
|
52
63
|
return response;
|
|
53
64
|
}
|
|
54
65
|
catch (error) {
|
|
@@ -139,3 +150,19 @@ exports.GracefulPage = GracefulPage;
|
|
|
139
150
|
function sleep(ms) {
|
|
140
151
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
141
152
|
}
|
|
153
|
+
function parseRetryAfter(headerValue) {
|
|
154
|
+
if (!headerValue)
|
|
155
|
+
return null;
|
|
156
|
+
// e.g. 120 (seconds)
|
|
157
|
+
let seconds = +headerValue;
|
|
158
|
+
if (seconds) {
|
|
159
|
+
return seconds * 1000;
|
|
160
|
+
}
|
|
161
|
+
// e.g. "Wed, 21 Oct 2015 07:28:00 GMT"
|
|
162
|
+
let target = new Date(headerValue).getTime();
|
|
163
|
+
if (target) {
|
|
164
|
+
let diff = target - Date.now();
|
|
165
|
+
return diff;
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graceful-playwright",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "Gracefully handle timeout and network error with auto retry.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graceful",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"express": "^4.18.2",
|
|
61
61
|
"mocha": "^10.3.0",
|
|
62
62
|
"npm-run-all": "^4.1.5",
|
|
63
|
-
"playwright": "^1.
|
|
63
|
+
"playwright": "^1.50.0",
|
|
64
64
|
"rimraf": "^5.0.5",
|
|
65
65
|
"sinon": "^17.0.1",
|
|
66
66
|
"ts-mocha": "^10.0.0",
|