srvx 0.1.2 → 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
@@ -1,4 +1,4 @@
1
- import { S as Server, r as resolvePort } from './shared/srvx.4f681732.mjs';
1
+ import { S as Server, r as resolvePort } from './shared/srvx.-ZdI-RlW.mjs';
2
2
 
3
3
  function serve(options) {
4
4
  return new BunServer(options);
package/dist/deno.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { S as Server, r as resolvePort } from './shared/srvx.4f681732.mjs';
1
+ import { S as Server, r as resolvePort } from './shared/srvx.-ZdI-RlW.mjs';
2
2
 
3
3
  function serve(options) {
4
4
  return new DenoServer(options);
@@ -20,14 +20,10 @@ declare const NodeRequestProxy: {
20
20
  referrerPolicy: ReferrerPolicy;
21
21
  headers: xHeaders;
22
22
  bodyUsed: boolean;
23
- xNode: {
24
- req: NodeHttp__default.IncomingMessage;
25
- res: NodeHttp__default.ServerResponse;
26
- } | undefined;
27
- "__#448@#url": URL;
23
+ _url: URL;
28
24
  "__#448@#abortSignal"?: AbortController;
29
25
  "__#448@#hasBody": boolean | undefined;
30
- "__#448@#rawBody"?: Promise<Uint8Array>;
26
+ "__#448@#bodyBytes"?: Promise<Uint8Array>;
31
27
  "__#448@#blobBody"?: Promise<Blob>;
32
28
  "__#448@#formDataBody"?: Promise<FormData>;
33
29
  "__#448@#jsonBody"?: Promise<any>;
@@ -40,12 +36,12 @@ declare const NodeRequestProxy: {
40
36
  readonly signal: AbortSignal;
41
37
  readonly _hasBody: boolean;
42
38
  readonly body: ReadableStream<Uint8Array<ArrayBufferLike>> | null;
43
- arrayBuffer(): Promise<ArrayBuffer>;
44
39
  bytes(): Promise<Uint8Array>;
40
+ arrayBuffer(): Promise<ArrayBuffer>;
45
41
  blob(): Promise<Blob>;
46
42
  formData(): Promise<FormData>;
47
- json(): Promise<any>;
48
43
  text(): Promise<string>;
44
+ json(): Promise<any>;
49
45
  [kNodeReq]: NodeHttp__default.IncomingMessage;
50
46
  readonly [Symbol.toStringTag]: string;
51
47
  [kNodeInspect](): {
@@ -68,7 +64,7 @@ declare const NodeFastResponse: {
68
64
  status: number;
69
65
  statusText: string;
70
66
  headers: NodeHttp__default.OutgoingHttpHeader[];
71
- body: string | Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | DataView<ArrayBufferLike> | null | undefined;
67
+ body: string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | Buffer<ArrayBufferLike> | DataView<ArrayBufferLike> | null | undefined;
72
68
  };
73
69
  /** Lazy initialized response instance */
74
70
  "__#449@#responseObj"?: Response;
@@ -20,14 +20,10 @@ declare const NodeRequestProxy: {
20
20
  referrerPolicy: ReferrerPolicy;
21
21
  headers: xHeaders;
22
22
  bodyUsed: boolean;
23
- xNode: {
24
- req: NodeHttp__default.IncomingMessage;
25
- res: NodeHttp__default.ServerResponse;
26
- } | undefined;
27
- "__#448@#url": URL;
23
+ _url: URL;
28
24
  "__#448@#abortSignal"?: AbortController;
29
25
  "__#448@#hasBody": boolean | undefined;
30
- "__#448@#rawBody"?: Promise<Uint8Array>;
26
+ "__#448@#bodyBytes"?: Promise<Uint8Array>;
31
27
  "__#448@#blobBody"?: Promise<Blob>;
32
28
  "__#448@#formDataBody"?: Promise<FormData>;
33
29
  "__#448@#jsonBody"?: Promise<any>;
@@ -40,12 +36,12 @@ declare const NodeRequestProxy: {
40
36
  readonly signal: AbortSignal;
41
37
  readonly _hasBody: boolean;
42
38
  readonly body: ReadableStream<Uint8Array<ArrayBufferLike>> | null;
43
- arrayBuffer(): Promise<ArrayBuffer>;
44
39
  bytes(): Promise<Uint8Array>;
40
+ arrayBuffer(): Promise<ArrayBuffer>;
45
41
  blob(): Promise<Blob>;
46
42
  formData(): Promise<FormData>;
47
- json(): Promise<any>;
48
43
  text(): Promise<string>;
44
+ json(): Promise<any>;
49
45
  [kNodeReq]: NodeHttp__default.IncomingMessage;
50
46
  readonly [Symbol.toStringTag]: string;
51
47
  [kNodeInspect](): {
@@ -68,7 +64,7 @@ declare const NodeFastResponse: {
68
64
  status: number;
69
65
  statusText: string;
70
66
  headers: NodeHttp__default.OutgoingHttpHeader[];
71
- body: string | Buffer<ArrayBufferLike> | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | DataView<ArrayBufferLike> | null | undefined;
67
+ body: string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | Buffer<ArrayBufferLike> | DataView<ArrayBufferLike> | null | undefined;
72
68
  };
73
69
  /** Lazy initialized response instance */
74
70
  "__#449@#responseObj"?: Response;
@@ -1,4 +1,4 @@
1
- export { a as NodeReqHeadersProxy, N as NodeRequestProxy, b as NodeResHeadersProxy, s as sendNodeResponse } from '../shared/srvx.2143ff37.mjs';
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 = void 0;
66
- this.#init = void 0;
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 = void 0;
89
- this.#init = void 0;
90
- this.#headersObj = void 0;
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 === void 0) {
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 === void 0) {
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.4f681732.mjs';
3
- import { N as NodeRequestProxy, s as sendNodeResponse } from './shared/srvx.2143ff37.mjs';
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 === void 0) {
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
- this[kNodeReq].headers[name] = void 0;
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 = void 0;
183
- this._port = void 0;
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 === void 0) {
193
+ if (this._hostname === undefined) {
189
194
  const [hostname, port] = parseHost(this[kNodeReq].headers.host);
190
- if (this._port === void 0 && 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 === void 0) {
207
+ if (this._port === undefined) {
203
208
  const [hostname, port] = parseHost(this[kNodeReq].headers.host);
204
- if (this._hostname === void 0 && 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 === void 0) {
221
+ if (this._pathname === undefined) {
217
222
  const [pathname, search] = parsePath(this[kNodeReq].url || "/");
218
223
  this._pathname = pathname;
219
- if (this._search === void 0) {
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 === void 0) {
242
+ if (this._search === undefined) {
238
243
  const [pathname, search] = parsePath(this[kNodeReq].url || "/");
239
244
  this._search = search;
240
- if (this._pathname === void 0) {
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 = void 0;
261
+ this._searchParams = undefined;
257
262
  this[kNodeReq].url = this.pathname + value;
258
263
  }
259
264
  // searchParams
@@ -337,13 +342,12 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
337
342
  this.referrerPolicy = "";
338
343
  this.bodyUsed = false;
339
344
  this[kNodeReq] = nodeReq;
340
- this.#url = new NodeReqURLProxy(nodeReq);
345
+ this._url = new NodeReqURLProxy(nodeReq);
341
346
  this.headers = new NodeReqHeadersProxy(nodeReq);
342
347
  }
343
- #url;
344
348
  #abortSignal;
345
349
  #hasBody;
346
- #rawBody;
350
+ #bodyBytes;
347
351
  #blobBody;
348
352
  #formDataBody;
349
353
  #jsonBody;
@@ -356,7 +360,7 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
356
360
  return new NodeRequestProxy2(this[kNodeReq]);
357
361
  }
358
362
  get url() {
359
- return this.#url.href;
363
+ return this._url.href;
360
364
  }
361
365
  get method() {
362
366
  return this[kNodeReq].method || "GET";
@@ -368,7 +372,7 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
368
372
  return this.#abortSignal.signal;
369
373
  }
370
374
  get _hasBody() {
371
- if (this.#hasBody !== void 0) {
375
+ if (this.#hasBody !== undefined) {
372
376
  return this.#hasBody;
373
377
  }
374
378
  const method = this[kNodeReq].method?.toUpperCase();
@@ -386,7 +390,7 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
386
390
  return true;
387
391
  }
388
392
  get body() {
389
- if (!this.#hasBody) {
393
+ if (!this._hasBody) {
390
394
  return null;
391
395
  }
392
396
  if (!this.#bodyStream) {
@@ -408,24 +412,25 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
408
412
  }
409
413
  return this.#bodyStream;
410
414
  }
411
- arrayBuffer() {
412
- if (!this.#rawBody) {
413
- const _bodyStream = this.body;
414
- return _bodyStream ? _readStream(_bodyStream).then((buff) => buff.buffer) : Promise.resolve(new ArrayBuffer(0));
415
- }
416
- return this.#rawBody.then((buff) => buff.buffer);
417
- }
418
415
  bytes() {
419
- if (!this.#rawBody) {
416
+ if (!this.#bodyBytes) {
420
417
  const _bodyStream = this.body;
421
- return _bodyStream ? _readStream(_bodyStream) : Promise.resolve(new Uint8Array());
418
+ this.#bodyBytes = _bodyStream ? _readStream(_bodyStream) : Promise.resolve(new Uint8Array());
422
419
  }
423
- return this.#rawBody;
420
+ return this.#bodyBytes;
421
+ }
422
+ arrayBuffer() {
423
+ return this.bytes().then((buff) => {
424
+ return buff.buffer.slice(
425
+ buff.byteOffset,
426
+ buff.byteOffset + buff.byteLength
427
+ );
428
+ });
424
429
  }
425
430
  blob() {
426
431
  if (!this.#blobBody) {
427
- this.#blobBody = this.arrayBuffer().then((buff) => {
428
- return new Blob([buff], {
432
+ this.#blobBody = this.bytes().then((bytes) => {
433
+ return new Blob([bytes], {
429
434
  type: this[kNodeReq].headers["content-type"]
430
435
  });
431
436
  });
@@ -440,6 +445,14 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
440
445
  }
441
446
  return this.#formDataBody;
442
447
  }
448
+ text() {
449
+ if (!this.#textBody) {
450
+ this.#textBody = this.bytes().then((bytes) => {
451
+ return new TextDecoder().decode(bytes);
452
+ });
453
+ }
454
+ return this.#textBody;
455
+ }
443
456
  json() {
444
457
  if (!this.#jsonBody) {
445
458
  this.#jsonBody = this.text().then((txt) => {
@@ -448,14 +461,6 @@ const NodeRequestProxy = /* @__PURE__ */ (() => class NodeRequestProxy2 {
448
461
  }
449
462
  return this.#jsonBody;
450
463
  }
451
- text() {
452
- if (!this.#textBody) {
453
- this.#textBody = this.arrayBuffer().then((buff) => {
454
- return new TextDecoder().decode(buff);
455
- });
456
- }
457
- return this.#textBody;
458
- }
459
464
  get [(Symbol.toStringTag)]() {
460
465
  return "Request";
461
466
  }
@@ -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).finally(
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).finally(() => endNodeResponse(nodeRes)) : endNodeResponse(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
- async function streamBody(stream, nodeRes) {
517
+ function streamBody(stream, nodeRes) {
518
+ if (nodeRes.destroyed) {
519
+ stream.cancel();
520
+ return;
521
+ }
515
522
  const reader = stream.getReader();
516
- try {
517
- while (true) {
518
- const { done, value } = await reader.read();
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
- break;
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
- nodeRes.write(value);
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.2",
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.4.1",
46
- "@types/bun": "^1.1.14",
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.1",
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.16.0",
52
+ "eslint": "^9.18.0",
53
53
  "eslint-config-unjs": "^0.4.2",
54
- "execa": "^9.5.1",
54
+ "execa": "^9.5.2",
55
55
  "get-port-please": "^3.1.2",
56
- "jiti": "^2.4.1",
57
- "prettier": "^3.4.1",
58
- "srvx": "^0.1.1",
59
- "typescript": "^5.7.2",
60
- "unbuild": "^2.0.0",
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.14.4"
63
+ "packageManager": "pnpm@9.15.4"
64
64
  }