@zero-server/fetch 0.9.0 → 0.9.2
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 +21 -21
- package/README.md +1 -1
- package/index.js +2 -5
- package/lib/fetch/index.js +256 -0
- package/package.json +10 -4
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Tony Wiedman
|
|
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.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tony Wiedman
|
|
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.
|
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ This package narrows `@zero-server/sdk` to **1** exports. See the [scope page](h
|
|
|
30
30
|
|
|
31
31
|
- [Scope page](https://github.com/tonywied17/zero-server/blob/main/docs/scopes/fetch.md)
|
|
32
32
|
- [Full API reference](https://github.com/tonywied17/zero-server/blob/main/API.md)
|
|
33
|
-
- [Live docs](https://z-server.
|
|
33
|
+
- [Live docs](https://z-server.dev)
|
|
34
34
|
|
|
35
35
|
## License
|
|
36
36
|
|
package/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
// AUTO-GENERATED by .tools/generate-package-stubs.js — edit .tools/scope-manifest.js and re-run `npm run packages:generate`.
|
|
2
2
|
'use strict';
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
fetch: sdk.fetch,
|
|
7
|
-
};
|
|
3
|
+
const fetch = require('./lib/fetch');
|
|
4
|
+
module.exports = { fetch };
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module fetch
|
|
3
|
+
* @description Minimal, zero-dependency server-side `fetch()` replacement.
|
|
4
|
+
* Supports HTTP/HTTPS, JSON/URLSearchParams/Buffer/stream bodies,
|
|
5
|
+
* download & upload progress callbacks, timeouts, and AbortSignal.
|
|
6
|
+
*/
|
|
7
|
+
const http = require('http');
|
|
8
|
+
const https = require('https');
|
|
9
|
+
const { URL } = require('url');
|
|
10
|
+
|
|
11
|
+
const STATUS_CODES = http.STATUS_CODES;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Perform an HTTP(S) request.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} url - Absolute URL to fetch.
|
|
17
|
+
* @param {object} [opts] - Configuration options.
|
|
18
|
+
* @param {string} [opts.method='GET'] - HTTP method.
|
|
19
|
+
* @param {object} [opts.headers] - Request headers.
|
|
20
|
+
* @param {string|Buffer|object|ReadableStream} [opts.body] - Request body.
|
|
21
|
+
* @param {number} [opts.timeout] - Request timeout in ms.
|
|
22
|
+
* @param {AbortSignal} [opts.signal] - Abort signal for cancellation.
|
|
23
|
+
* @param {import('http').Agent} [opts.agent] - Custom HTTP agent.
|
|
24
|
+
* @param {Function} [opts.onDownloadProgress] - `({ loaded, total }) => void` download progress callback.
|
|
25
|
+
* @param {Function} [opts.onUploadProgress] - `({ loaded, total }) => void` upload progress callback.
|
|
26
|
+
* @param {boolean} [opts.rejectUnauthorized] - Reject connections with unverified certs (default: Node default `true`). TLS option — passed to `https.request()`.
|
|
27
|
+
* @param {string|Buffer|Array} [opts.ca] - Override default CA certificates.
|
|
28
|
+
* @param {string|Buffer} [opts.cert] - Client certificate (PEM) for mutual TLS.
|
|
29
|
+
* @param {string|Buffer} [opts.key] - Private key (PEM) for mutual TLS.
|
|
30
|
+
* @param {string|Buffer} [opts.pfx] - PFX / PKCS12 bundle (alternative to cert+key).
|
|
31
|
+
* @param {string} [opts.passphrase] - Passphrase for the key or PFX.
|
|
32
|
+
* @param {string} [opts.servername] - SNI server name override.
|
|
33
|
+
* @param {string} [opts.ciphers] - Colon-separated cipher list.
|
|
34
|
+
* @param {string} [opts.secureProtocol] - SSL/TLS protocol method name.
|
|
35
|
+
* @param {string} [opts.minVersion] - Minimum TLS version (`'TLSv1.2'`, etc.).
|
|
36
|
+
* @param {string} [opts.maxVersion] - Maximum TLS version.
|
|
37
|
+
*
|
|
38
|
+
* @returns {Promise<{ status: number, statusText: string, ok: boolean, secure: boolean, url: string, headers: object, arrayBuffer: Function, text: Function, json: Function }>} Resolves with a response object containing status info, headers, and body-reading helpers (`text()`, `json()`, `arrayBuffer()`).
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const res = await fetch('https://api.example.com/data');
|
|
42
|
+
* const body = await res.json();
|
|
43
|
+
*
|
|
44
|
+
* // POST with JSON body & timeout
|
|
45
|
+
* const res2 = await fetch('https://api.example.com/items', {
|
|
46
|
+
* method: 'POST',
|
|
47
|
+
* body: { name: 'widget' },
|
|
48
|
+
* timeout: 5000,
|
|
49
|
+
* });
|
|
50
|
+
*/
|
|
51
|
+
function miniFetch(url, opts = {})
|
|
52
|
+
{
|
|
53
|
+
return new Promise((resolve, reject) =>
|
|
54
|
+
{
|
|
55
|
+
try
|
|
56
|
+
{
|
|
57
|
+
const u = new URL(url);
|
|
58
|
+
const lib = u.protocol === 'https:' ? https : http;
|
|
59
|
+
const method = (opts.method || 'GET').toUpperCase();
|
|
60
|
+
const headers = Object.assign({}, opts.headers || {});
|
|
61
|
+
|
|
62
|
+
// Normalize body
|
|
63
|
+
let body = opts.body;
|
|
64
|
+
if (body && typeof body === 'object' && typeof body.toString === 'function' && body.constructor && body.constructor.name === 'URLSearchParams')
|
|
65
|
+
{
|
|
66
|
+
if (!headers['Content-Type'] && !headers['content-type']) headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
67
|
+
body = body.toString();
|
|
68
|
+
}
|
|
69
|
+
else if (body && typeof body === 'object' && !Buffer.isBuffer(body) && !(body instanceof ArrayBuffer) && !(body instanceof Uint8Array) && !(body && typeof body.pipe === 'function'))
|
|
70
|
+
{
|
|
71
|
+
if (!headers['Content-Type'] && !headers['content-type']) headers['Content-Type'] = 'application/json';
|
|
72
|
+
body = Buffer.from(JSON.stringify(body), 'utf8');
|
|
73
|
+
}
|
|
74
|
+
else if (body instanceof ArrayBuffer)
|
|
75
|
+
{
|
|
76
|
+
body = Buffer.from(body);
|
|
77
|
+
}
|
|
78
|
+
else if (body instanceof Uint8Array && !Buffer.isBuffer(body))
|
|
79
|
+
{
|
|
80
|
+
body = Buffer.from(body);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Set Content-Length for known-size bodies
|
|
84
|
+
if ((Buffer.isBuffer(body) || typeof body === 'string') && !headers['Content-Length'] && !headers['content-length'])
|
|
85
|
+
{
|
|
86
|
+
headers['Content-Length'] = String(Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const options = { method, headers };
|
|
90
|
+
if (opts.agent) options.agent = opts.agent;
|
|
91
|
+
|
|
92
|
+
// Pass through TLS options for HTTPS requests
|
|
93
|
+
if (lib === https)
|
|
94
|
+
{
|
|
95
|
+
const tlsKeys = [
|
|
96
|
+
'rejectUnauthorized', 'ca', 'cert', 'key', 'pfx', 'passphrase',
|
|
97
|
+
'servername', 'ciphers', 'secureProtocol', 'minVersion', 'maxVersion'
|
|
98
|
+
];
|
|
99
|
+
for (const k of tlsKeys)
|
|
100
|
+
{
|
|
101
|
+
if (opts[k] !== undefined) options[k] = opts[k];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const req = lib.request(u, options, (res) =>
|
|
106
|
+
{
|
|
107
|
+
const chunks = [];
|
|
108
|
+
let downloaded = 0;
|
|
109
|
+
const total = parseInt(res.headers['content-length'] || '0', 10) || null;
|
|
110
|
+
|
|
111
|
+
res.on('data', (c) =>
|
|
112
|
+
{
|
|
113
|
+
chunks.push(c);
|
|
114
|
+
downloaded += c.length;
|
|
115
|
+
if (typeof opts.onDownloadProgress === 'function')
|
|
116
|
+
{
|
|
117
|
+
try { opts.onDownloadProgress({ loaded: downloaded, total }); } catch (e) { }
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
res.on('end', () =>
|
|
122
|
+
{
|
|
123
|
+
const buf = Buffer.concat(chunks);
|
|
124
|
+
const status = res.statusCode;
|
|
125
|
+
const rawHeaders = res.headers || {};
|
|
126
|
+
const responseHeaders = {
|
|
127
|
+
get(name)
|
|
128
|
+
{
|
|
129
|
+
if (!name) return undefined;
|
|
130
|
+
const v = rawHeaders[name.toLowerCase()];
|
|
131
|
+
return Array.isArray(v) ? v.join(', ') : v;
|
|
132
|
+
},
|
|
133
|
+
raw: rawHeaders,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
resolve({
|
|
137
|
+
status,
|
|
138
|
+
statusText: STATUS_CODES[status] || '',
|
|
139
|
+
ok: status >= 200 && status < 300,
|
|
140
|
+
secure: u.protocol === 'https:',
|
|
141
|
+
url: u.href,
|
|
142
|
+
headers: responseHeaders,
|
|
143
|
+
arrayBuffer: () => Promise.resolve(buf),
|
|
144
|
+
text: () => Promise.resolve(buf.toString('utf8')),
|
|
145
|
+
json: () =>
|
|
146
|
+
{
|
|
147
|
+
try { return Promise.resolve(JSON.parse(buf.toString('utf8'))); }
|
|
148
|
+
catch (e) { return Promise.reject(e); }
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
req.on('error', reject);
|
|
155
|
+
|
|
156
|
+
// Timeout
|
|
157
|
+
if (typeof opts.timeout === 'number' && opts.timeout > 0)
|
|
158
|
+
{
|
|
159
|
+
req.setTimeout(opts.timeout, () =>
|
|
160
|
+
{
|
|
161
|
+
const err = new Error('Request timed out');
|
|
162
|
+
err.code = 'ETIMEOUT';
|
|
163
|
+
req.destroy(err);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// AbortSignal support
|
|
168
|
+
let abortHandler;
|
|
169
|
+
if (opts.signal)
|
|
170
|
+
{
|
|
171
|
+
if (opts.signal.aborted)
|
|
172
|
+
{
|
|
173
|
+
const err = new Error('Request aborted');
|
|
174
|
+
err.name = 'AbortError';
|
|
175
|
+
req.destroy(err);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
abortHandler = () =>
|
|
179
|
+
{
|
|
180
|
+
const err = new Error('Request aborted');
|
|
181
|
+
err.name = 'AbortError';
|
|
182
|
+
req.destroy(err);
|
|
183
|
+
};
|
|
184
|
+
if (typeof opts.signal.addEventListener === 'function') opts.signal.addEventListener('abort', abortHandler);
|
|
185
|
+
else if (typeof opts.signal.on === 'function') opts.signal.on('abort', abortHandler);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Cleanup signal listener
|
|
189
|
+
const cleanup = () =>
|
|
190
|
+
{
|
|
191
|
+
if (opts.signal && abortHandler)
|
|
192
|
+
{
|
|
193
|
+
try
|
|
194
|
+
{
|
|
195
|
+
if (typeof opts.signal.removeEventListener === 'function') opts.signal.removeEventListener('abort', abortHandler);
|
|
196
|
+
else if (typeof opts.signal.off === 'function') opts.signal.off('abort', abortHandler);
|
|
197
|
+
}
|
|
198
|
+
catch (e) { }
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
req.on('close', cleanup);
|
|
202
|
+
|
|
203
|
+
// Write body
|
|
204
|
+
if (body && typeof body.pipe === 'function')
|
|
205
|
+
{
|
|
206
|
+
let uploaded = 0;
|
|
207
|
+
body.on('data', (chunk) =>
|
|
208
|
+
{
|
|
209
|
+
uploaded += chunk.length;
|
|
210
|
+
if (typeof opts.onUploadProgress === 'function')
|
|
211
|
+
{
|
|
212
|
+
try { opts.onUploadProgress({ loaded: uploaded, total: headers['Content-Length'] ? Number(headers['Content-Length']) : null }); } catch (e) { }
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
body.on('error', (err) => req.destroy(err));
|
|
216
|
+
body.pipe(req);
|
|
217
|
+
}
|
|
218
|
+
else if (Buffer.isBuffer(body) || typeof body === 'string')
|
|
219
|
+
{
|
|
220
|
+
const buf = Buffer.isBuffer(body) ? body : Buffer.from(body);
|
|
221
|
+
const total = buf.length;
|
|
222
|
+
const CHUNK = 64 * 1024;
|
|
223
|
+
let sent = 0;
|
|
224
|
+
|
|
225
|
+
function writeNext()
|
|
226
|
+
{
|
|
227
|
+
if (sent >= total) { req.end(); return; }
|
|
228
|
+
const slice = buf.slice(sent, Math.min(sent + CHUNK, total));
|
|
229
|
+
const ok = req.write(slice, () =>
|
|
230
|
+
{
|
|
231
|
+
sent += slice.length;
|
|
232
|
+
if (typeof opts.onUploadProgress === 'function')
|
|
233
|
+
{
|
|
234
|
+
try { opts.onUploadProgress({ loaded: sent, total }); } catch (e) { }
|
|
235
|
+
}
|
|
236
|
+
writeNext();
|
|
237
|
+
});
|
|
238
|
+
if (!ok) req.once('drain', writeNext);
|
|
239
|
+
}
|
|
240
|
+
writeNext();
|
|
241
|
+
}
|
|
242
|
+
else if (body == null)
|
|
243
|
+
{
|
|
244
|
+
req.end();
|
|
245
|
+
}
|
|
246
|
+
else
|
|
247
|
+
{
|
|
248
|
+
req.write(String(body));
|
|
249
|
+
req.end();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
catch (e) { reject(e); }
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
module.exports = miniFetch;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zero-server/fetch",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.2",
|
|
4
4
|
"description": "Server-side fetch with mTLS, timeouts, AbortSignal.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"zero-server",
|
|
7
|
-
"zero-
|
|
7
|
+
"zero-server",
|
|
8
8
|
"fetch"
|
|
9
9
|
],
|
|
10
10
|
"author": "Anthony Wiedman",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"./package.json": "./package.json"
|
|
21
21
|
},
|
|
22
22
|
"files": [
|
|
23
|
+
"lib",
|
|
23
24
|
"index.js",
|
|
24
25
|
"index.d.ts",
|
|
25
26
|
"README.md",
|
|
@@ -42,7 +43,12 @@
|
|
|
42
43
|
"access": "public"
|
|
43
44
|
},
|
|
44
45
|
"sideEffects": false,
|
|
45
|
-
"
|
|
46
|
-
"@zero-server/sdk": "0.9.
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@zero-server/sdk": ">=0.9.2"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@zero-server/sdk": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
47
53
|
}
|
|
48
54
|
}
|