srvx 0.1.3 → 0.1.4
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/dist/bun.mjs
CHANGED
package/dist/deno.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { a as NodeReqHeadersProxy, N as NodeRequestProxy, b as NodeResHeadersProxy, s as sendNodeResponse } from '../shared/srvx.
|
|
1
|
+
export { a as NodeReqHeadersProxy, N as NodeRequestProxy, b as NodeResHeadersProxy, s as sendNodeResponse } from '../shared/srvx.DNp8_Fs3.mjs';
|
|
2
2
|
import { splitSetCookieString } from 'cookie-es';
|
|
3
3
|
|
|
4
4
|
const NodeFastResponse = /* @__PURE__ */ (() => (
|
|
@@ -62,8 +62,8 @@ const NodeFastResponse = /* @__PURE__ */ (() => (
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
-
this.#body =
|
|
66
|
-
this.#init =
|
|
65
|
+
this.#body = undefined;
|
|
66
|
+
this.#init = undefined;
|
|
67
67
|
return {
|
|
68
68
|
status,
|
|
69
69
|
statusText,
|
|
@@ -85,9 +85,9 @@ const NodeFastResponse = /* @__PURE__ */ (() => (
|
|
|
85
85
|
get #response() {
|
|
86
86
|
if (!this.#responseObj) {
|
|
87
87
|
this.#responseObj = new Response(this.#body, this.#init);
|
|
88
|
-
this.#body =
|
|
89
|
-
this.#init =
|
|
90
|
-
this.#headersObj =
|
|
88
|
+
this.#body = undefined;
|
|
89
|
+
this.#init = undefined;
|
|
90
|
+
this.#headersObj = undefined;
|
|
91
91
|
}
|
|
92
92
|
return this.#responseObj;
|
|
93
93
|
}
|
|
@@ -140,7 +140,7 @@ const NodeFastResponse = /* @__PURE__ */ (() => (
|
|
|
140
140
|
// --- body ---
|
|
141
141
|
#fastBody(as) {
|
|
142
142
|
const bodyInit = this.#body;
|
|
143
|
-
if (bodyInit === null || bodyInit ===
|
|
143
|
+
if (bodyInit === null || bodyInit === undefined) {
|
|
144
144
|
return null;
|
|
145
145
|
}
|
|
146
146
|
if (bodyInit instanceof as) {
|
|
@@ -209,7 +209,7 @@ const NodeFastResponse = /* @__PURE__ */ (() => (
|
|
|
209
209
|
return this.#responseObj.text();
|
|
210
210
|
}
|
|
211
211
|
const bodyInit = this.#body;
|
|
212
|
-
if (bodyInit === null || bodyInit ===
|
|
212
|
+
if (bodyInit === null || bodyInit === undefined) {
|
|
213
213
|
return Promise.resolve("");
|
|
214
214
|
}
|
|
215
215
|
if (typeof bodyInit === "string") {
|
package/dist/node.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import NodeHttp from 'node:http';
|
|
2
|
-
import { S as Server, r as resolvePort } from './shared/srvx.
|
|
3
|
-
import { N as NodeRequestProxy, s as sendNodeResponse } from './shared/srvx.
|
|
2
|
+
import { S as Server, r as resolvePort } from './shared/srvx.-ZdI-RlW.mjs';
|
|
3
|
+
import { N as NodeRequestProxy, s as sendNodeResponse } from './shared/srvx.DNp8_Fs3.mjs';
|
|
4
4
|
import 'cookie-es';
|
|
5
5
|
|
|
6
6
|
function serve(options) {
|
|
@@ -103,7 +103,7 @@ class Server {
|
|
|
103
103
|
|
|
104
104
|
function resolvePort(portOptions, portEnv) {
|
|
105
105
|
const portInput = portOptions ?? portEnv;
|
|
106
|
-
if (portInput ===
|
|
106
|
+
if (portInput === undefined) {
|
|
107
107
|
return 3e3;
|
|
108
108
|
}
|
|
109
109
|
return typeof portInput === "number" ? portInput : Number.parseInt(portInput, 10);
|
|
@@ -11,6 +11,7 @@ const NodeReqHeadersProxy = /* @__PURE__ */ (() => class NodeReqHeadersProxy {
|
|
|
11
11
|
this[kNodeReq] = req;
|
|
12
12
|
}
|
|
13
13
|
append(name, value) {
|
|
14
|
+
name = name.toLowerCase();
|
|
14
15
|
const _headers = this[kNodeReq].headers;
|
|
15
16
|
const _current = _headers[name];
|
|
16
17
|
if (_current) {
|
|
@@ -24,9 +25,11 @@ const NodeReqHeadersProxy = /* @__PURE__ */ (() => class NodeReqHeadersProxy {
|
|
|
24
25
|
}
|
|
25
26
|
}
|
|
26
27
|
delete(name) {
|
|
27
|
-
|
|
28
|
+
name = name.toLowerCase();
|
|
29
|
+
this[kNodeReq].headers[name] = undefined;
|
|
28
30
|
}
|
|
29
31
|
get(name) {
|
|
32
|
+
name = name.toLowerCase();
|
|
30
33
|
return _normalizeValue(this[kNodeReq].headers[name]);
|
|
31
34
|
}
|
|
32
35
|
getSetCookie() {
|
|
@@ -37,9 +40,11 @@ const NodeReqHeadersProxy = /* @__PURE__ */ (() => class NodeReqHeadersProxy {
|
|
|
37
40
|
return splitSetCookieString(setCookie);
|
|
38
41
|
}
|
|
39
42
|
has(name) {
|
|
43
|
+
name = name.toLowerCase();
|
|
40
44
|
return !!this[kNodeReq].headers[name];
|
|
41
45
|
}
|
|
42
46
|
set(name, value) {
|
|
47
|
+
name = name.toLowerCase();
|
|
43
48
|
this[kNodeReq].headers[name] = value;
|
|
44
49
|
}
|
|
45
50
|
toJSON() {
|
|
@@ -179,15 +184,15 @@ const NodeReqURLProxy = /* @__PURE__ */ (() => class _NodeReqURLProxy {
|
|
|
179
184
|
return this[kNodeReq].headers.host || "";
|
|
180
185
|
}
|
|
181
186
|
set host(value) {
|
|
182
|
-
this._hostname =
|
|
183
|
-
this._port =
|
|
187
|
+
this._hostname = undefined;
|
|
188
|
+
this._port = undefined;
|
|
184
189
|
this[kNodeReq].headers.host = value;
|
|
185
190
|
}
|
|
186
191
|
// hostname
|
|
187
192
|
get hostname() {
|
|
188
|
-
if (this._hostname ===
|
|
193
|
+
if (this._hostname === undefined) {
|
|
189
194
|
const [hostname, port] = parseHost(this[kNodeReq].headers.host);
|
|
190
|
-
if (this._port ===
|
|
195
|
+
if (this._port === undefined && port) {
|
|
191
196
|
this._port = String(Number.parseInt(port) || "");
|
|
192
197
|
}
|
|
193
198
|
this._hostname = hostname || "localhost";
|
|
@@ -199,9 +204,9 @@ const NodeReqURLProxy = /* @__PURE__ */ (() => class _NodeReqURLProxy {
|
|
|
199
204
|
}
|
|
200
205
|
// port
|
|
201
206
|
get port() {
|
|
202
|
-
if (this._port ===
|
|
207
|
+
if (this._port === undefined) {
|
|
203
208
|
const [hostname, port] = parseHost(this[kNodeReq].headers.host);
|
|
204
|
-
if (this._hostname ===
|
|
209
|
+
if (this._hostname === undefined && hostname) {
|
|
205
210
|
this._hostname = hostname;
|
|
206
211
|
}
|
|
207
212
|
this._port = port || String(this[kNodeReq].socket?.localPort || "");
|
|
@@ -213,10 +218,10 @@ const NodeReqURLProxy = /* @__PURE__ */ (() => class _NodeReqURLProxy {
|
|
|
213
218
|
}
|
|
214
219
|
// pathname
|
|
215
220
|
get pathname() {
|
|
216
|
-
if (this._pathname ===
|
|
221
|
+
if (this._pathname === undefined) {
|
|
217
222
|
const [pathname, search] = parsePath(this[kNodeReq].url || "/");
|
|
218
223
|
this._pathname = pathname;
|
|
219
|
-
if (this._search ===
|
|
224
|
+
if (this._search === undefined) {
|
|
220
225
|
this._search = search;
|
|
221
226
|
}
|
|
222
227
|
}
|
|
@@ -234,10 +239,10 @@ const NodeReqURLProxy = /* @__PURE__ */ (() => class _NodeReqURLProxy {
|
|
|
234
239
|
}
|
|
235
240
|
// search
|
|
236
241
|
get search() {
|
|
237
|
-
if (this._search ===
|
|
242
|
+
if (this._search === undefined) {
|
|
238
243
|
const [pathname, search] = parsePath(this[kNodeReq].url || "/");
|
|
239
244
|
this._search = search;
|
|
240
|
-
if (this._pathname ===
|
|
245
|
+
if (this._pathname === undefined) {
|
|
241
246
|
this._pathname = pathname;
|
|
242
247
|
}
|
|
243
248
|
}
|
|
@@ -253,7 +258,7 @@ const NodeReqURLProxy = /* @__PURE__ */ (() => class _NodeReqURLProxy {
|
|
|
253
258
|
return;
|
|
254
259
|
}
|
|
255
260
|
this._search = value;
|
|
256
|
-
this._searchParams =
|
|
261
|
+
this._searchParams = undefined;
|
|
257
262
|
this[kNodeReq].url = this.pathname + value;
|
|
258
263
|
}
|
|
259
264
|
// searchParams
|
|
@@ -367,7 +372,7 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
|
|
|
367
372
|
return this.#abortSignal.signal;
|
|
368
373
|
}
|
|
369
374
|
get _hasBody() {
|
|
370
|
-
if (this.#hasBody !==
|
|
375
|
+
if (this.#hasBody !== undefined) {
|
|
371
376
|
return this.#hasBody;
|
|
372
377
|
}
|
|
373
378
|
const method = this[kNodeReq].method?.toUpperCase();
|
|
@@ -488,9 +493,7 @@ async function sendNodeResponse(nodeRes, webRes) {
|
|
|
488
493
|
const res = webRes.xNodeResponse();
|
|
489
494
|
nodeRes.writeHead(res.status, res.statusText, res.headers);
|
|
490
495
|
if (res.body instanceof ReadableStream) {
|
|
491
|
-
return streamBody(res.body, nodeRes)
|
|
492
|
-
() => endNodeResponse(nodeRes)
|
|
493
|
-
);
|
|
496
|
+
return streamBody(res.body, nodeRes);
|
|
494
497
|
}
|
|
495
498
|
nodeRes.write(res.body);
|
|
496
499
|
return endNodeResponse(nodeRes);
|
|
@@ -506,24 +509,50 @@ async function sendNodeResponse(nodeRes, webRes) {
|
|
|
506
509
|
}
|
|
507
510
|
}
|
|
508
511
|
nodeRes.writeHead(webRes.status || 200, webRes.statusText, headerEntries);
|
|
509
|
-
return webRes.body ? streamBody(webRes.body, nodeRes)
|
|
512
|
+
return webRes.body ? streamBody(webRes.body, nodeRes) : endNodeResponse(nodeRes);
|
|
510
513
|
}
|
|
511
514
|
function endNodeResponse(nodeRes) {
|
|
512
515
|
return new Promise((resolve) => nodeRes.end(resolve));
|
|
513
516
|
}
|
|
514
|
-
|
|
517
|
+
function streamBody(stream, nodeRes) {
|
|
518
|
+
if (nodeRes.destroyed) {
|
|
519
|
+
stream.cancel();
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
515
522
|
const reader = stream.getReader();
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
523
|
+
function streamCancel(error) {
|
|
524
|
+
reader.cancel(error).catch(() => {
|
|
525
|
+
});
|
|
526
|
+
if (error) {
|
|
527
|
+
nodeRes.destroy(error);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
function streamHandle({
|
|
531
|
+
done,
|
|
532
|
+
value
|
|
533
|
+
}) {
|
|
534
|
+
try {
|
|
519
535
|
if (done) {
|
|
520
|
-
|
|
536
|
+
nodeRes.end();
|
|
537
|
+
} else if (nodeRes.write(value)) {
|
|
538
|
+
reader.read().then(streamHandle, streamCancel);
|
|
539
|
+
} else {
|
|
540
|
+
nodeRes.once(
|
|
541
|
+
"drain",
|
|
542
|
+
() => reader.read().then(streamHandle, streamCancel)
|
|
543
|
+
);
|
|
521
544
|
}
|
|
522
|
-
|
|
545
|
+
} catch (error) {
|
|
546
|
+
streamCancel(error instanceof Error ? error : undefined);
|
|
523
547
|
}
|
|
524
|
-
} finally {
|
|
525
|
-
reader.releaseLock();
|
|
526
548
|
}
|
|
549
|
+
nodeRes.on("close", streamCancel);
|
|
550
|
+
nodeRes.on("error", streamCancel);
|
|
551
|
+
reader.read().then(streamHandle, streamCancel);
|
|
552
|
+
return reader.closed.finally(() => {
|
|
553
|
+
nodeRes.off("close", streamCancel);
|
|
554
|
+
nodeRes.off("error", streamCancel);
|
|
555
|
+
});
|
|
527
556
|
}
|
|
528
557
|
|
|
529
558
|
export { NodeRequestProxy as N, NodeReqHeadersProxy as a, NodeResHeadersProxy as b, sendNodeResponse as s };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "srvx",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Universal Server API based on web platform standards. Works seamlessly with Deno, Bun and Node.js.",
|
|
5
5
|
"repository": "unjs/srvx",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,23 +42,23 @@
|
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@hono/node-server": "^1.13.7",
|
|
45
|
-
"@mjackson/node-fetch-server": "^0.
|
|
46
|
-
"@types/bun": "^1.1.
|
|
45
|
+
"@mjackson/node-fetch-server": "^0.5.0",
|
|
46
|
+
"@types/bun": "^1.1.16",
|
|
47
47
|
"@types/deno": "^2.0.0",
|
|
48
|
-
"@types/node": "^22.10.
|
|
48
|
+
"@types/node": "^22.10.6",
|
|
49
49
|
"@vitest/coverage-v8": "^2.1.8",
|
|
50
50
|
"automd": "^0.3.12",
|
|
51
51
|
"changelogen": "^0.5.7",
|
|
52
|
-
"eslint": "^9.
|
|
52
|
+
"eslint": "^9.18.0",
|
|
53
53
|
"eslint-config-unjs": "^0.4.2",
|
|
54
|
-
"execa": "^9.5.
|
|
54
|
+
"execa": "^9.5.2",
|
|
55
55
|
"get-port-please": "^3.1.2",
|
|
56
|
-
"jiti": "^2.4.
|
|
57
|
-
"prettier": "^3.4.
|
|
58
|
-
"srvx": "^0.1.
|
|
59
|
-
"typescript": "^5.7.
|
|
60
|
-
"unbuild": "^
|
|
56
|
+
"jiti": "^2.4.2",
|
|
57
|
+
"prettier": "^3.4.2",
|
|
58
|
+
"srvx": "^0.1.3",
|
|
59
|
+
"typescript": "^5.7.3",
|
|
60
|
+
"unbuild": "^3.3.1",
|
|
61
61
|
"vitest": "^2.1.8"
|
|
62
62
|
},
|
|
63
|
-
"packageManager": "pnpm@9.
|
|
63
|
+
"packageManager": "pnpm@9.15.4"
|
|
64
64
|
}
|