graceful-playwright 1.3.0 → 1.5.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/README.md +20 -1
- package/core.d.ts +14 -1
- package/core.js +36 -18
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -52,6 +52,8 @@ await browser.close()
|
|
|
52
52
|
|
|
53
53
|
## Typescript Signature
|
|
54
54
|
|
|
55
|
+
Main Class: `GracefulPage`
|
|
56
|
+
|
|
55
57
|
```typescript
|
|
56
58
|
import { Browser, BrowserContext, Page, Response } from 'playwright'
|
|
57
59
|
|
|
@@ -80,7 +82,10 @@ export class GracefulPage {
|
|
|
80
82
|
/** @description optimized version of page.close() */
|
|
81
83
|
close: Page['close']
|
|
82
84
|
|
|
83
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* @description graceful version of page.goto()
|
|
87
|
+
* @throws GotoError with response details when got 429 Too Many Requests without retry-after header
|
|
88
|
+
*/
|
|
84
89
|
goto(
|
|
85
90
|
url: string,
|
|
86
91
|
/**
|
|
@@ -103,6 +108,20 @@ export class GracefulPage {
|
|
|
103
108
|
}
|
|
104
109
|
```
|
|
105
110
|
|
|
111
|
+
Error Class: `GotoError`
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
export class GotoError extends Error {
|
|
115
|
+
constructor(message: string, public details: GotoErrorDetails)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type GotoErrorDetails = {
|
|
119
|
+
url: string
|
|
120
|
+
options?: Parameters<Page['goto']>[1]
|
|
121
|
+
response: Awaited<ReturnType<Page['goto']>>
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
106
125
|
## License
|
|
107
126
|
|
|
108
127
|
This project is licensed with [BSD-2-Clause](./LICENSE)
|
package/core.d.ts
CHANGED
|
@@ -31,7 +31,10 @@ export declare class GracefulPage {
|
|
|
31
31
|
restart(options?: Parameters<Page['close']>[0]): Promise<Page>;
|
|
32
32
|
/** @description optimized version of page.close() */
|
|
33
33
|
close: Page['close'];
|
|
34
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* @description graceful version of page.goto()
|
|
36
|
+
* @throws GotoError with response details when got 429 Too Many Requests without retry-after header
|
|
37
|
+
*/
|
|
35
38
|
goto(url: string,
|
|
36
39
|
/**
|
|
37
40
|
* @default { waitUtil: "domcontentloaded" }
|
|
@@ -55,3 +58,13 @@ export declare class GracefulPage {
|
|
|
55
58
|
/** @description proxy method to (await this.getPage()).innerText */
|
|
56
59
|
innerText: Page['innerText'];
|
|
57
60
|
}
|
|
61
|
+
export type GotoErrorDetails = {
|
|
62
|
+
url: string;
|
|
63
|
+
options?: Parameters<Page['goto']>[1];
|
|
64
|
+
response: Awaited<ReturnType<Page['goto']>>;
|
|
65
|
+
};
|
|
66
|
+
export declare class GotoError extends Error {
|
|
67
|
+
details: GotoErrorDetails;
|
|
68
|
+
constructor(message: string, details: GotoErrorDetails);
|
|
69
|
+
}
|
|
70
|
+
export declare function parseRetryAfter(headerValue: string | null): number | null;
|
package/core.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GracefulPage = void 0;
|
|
3
|
+
exports.GotoError = exports.GracefulPage = void 0;
|
|
4
|
+
exports.parseRetryAfter = parseRetryAfter;
|
|
4
5
|
class GracefulPage {
|
|
5
6
|
options;
|
|
6
7
|
constructor(options) {
|
|
@@ -36,7 +37,10 @@ class GracefulPage {
|
|
|
36
37
|
this.options.page = undefined;
|
|
37
38
|
return promise;
|
|
38
39
|
};
|
|
39
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* @description graceful version of page.goto()
|
|
42
|
+
* @throws GotoError with response details when got 429 Too Many Requests without retry-after header
|
|
43
|
+
*/
|
|
40
44
|
async goto(url,
|
|
41
45
|
/**
|
|
42
46
|
* @default { waitUtil: "domcontentloaded" }
|
|
@@ -50,24 +54,14 @@ class GracefulPage {
|
|
|
50
54
|
...options,
|
|
51
55
|
});
|
|
52
56
|
if (response && response.status() === 429) {
|
|
53
|
-
let
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
await sleep(
|
|
57
|
+
let headerValue = await response.headerValue('Retry-After');
|
|
58
|
+
let interval = parseRetryAfter(headerValue);
|
|
59
|
+
if (interval) {
|
|
60
|
+
await sleep(interval);
|
|
57
61
|
continue;
|
|
58
62
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
let target = new Date(retryAfter).getTime();
|
|
62
|
-
let now = Date.now();
|
|
63
|
-
let diff = target - now;
|
|
64
|
-
await sleep(diff);
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
let statusText = response.statusText() || 'Too Many Requests';
|
|
69
|
-
throw new Error(statusText);
|
|
70
|
-
}
|
|
63
|
+
let statusText = response.statusText() || 'Too Many Requests';
|
|
64
|
+
throw new GotoError(statusText, { url, options, response });
|
|
71
65
|
}
|
|
72
66
|
return response;
|
|
73
67
|
}
|
|
@@ -156,6 +150,30 @@ class GracefulPage {
|
|
|
156
150
|
};
|
|
157
151
|
}
|
|
158
152
|
exports.GracefulPage = GracefulPage;
|
|
153
|
+
class GotoError extends Error {
|
|
154
|
+
details;
|
|
155
|
+
constructor(message, details) {
|
|
156
|
+
super(message);
|
|
157
|
+
this.details = details;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
exports.GotoError = GotoError;
|
|
159
161
|
function sleep(ms) {
|
|
160
162
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
161
163
|
}
|
|
164
|
+
function parseRetryAfter(headerValue) {
|
|
165
|
+
if (!headerValue)
|
|
166
|
+
return null;
|
|
167
|
+
// e.g. 120 (seconds)
|
|
168
|
+
let seconds = +headerValue;
|
|
169
|
+
if (seconds) {
|
|
170
|
+
return seconds * 1000;
|
|
171
|
+
}
|
|
172
|
+
// e.g. "Wed, 21 Oct 2015 07:28:00 GMT"
|
|
173
|
+
let target = new Date(headerValue).getTime();
|
|
174
|
+
if (target) {
|
|
175
|
+
let diff = target - Date.now();
|
|
176
|
+
return diff;
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graceful-playwright",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Gracefully handle timeout and network error with auto retry.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graceful",
|
|
@@ -58,14 +58,13 @@
|
|
|
58
58
|
"@types/sinon": "^17.0.1",
|
|
59
59
|
"chai": "4",
|
|
60
60
|
"express": "^4.18.2",
|
|
61
|
-
"mocha": "^
|
|
61
|
+
"mocha": "^11.2.2",
|
|
62
62
|
"npm-run-all": "^4.1.5",
|
|
63
|
-
"playwright": "^1.
|
|
64
|
-
"rimraf": "^
|
|
65
|
-
"sinon": "^
|
|
66
|
-
"ts-mocha": "^
|
|
63
|
+
"playwright": "^1.52.0",
|
|
64
|
+
"rimraf": "^6.0.1",
|
|
65
|
+
"sinon": "^20.0.0",
|
|
66
|
+
"ts-mocha": "^11.1.0",
|
|
67
67
|
"ts-node": "^10.9.2",
|
|
68
|
-
"ts-node-dev": "^2.0.0",
|
|
69
68
|
"typescript": "^5.3.3"
|
|
70
69
|
}
|
|
71
70
|
}
|