@wooksjs/event-http 0.7.6 → 0.7.8

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/index.cjs CHANGED
@@ -28,9 +28,9 @@ const node_util = __toESM(require("node:util"));
28
28
  const node_zlib = __toESM(require("node:zlib"));
29
29
  const url = __toESM(require("url"));
30
30
  const http = __toESM(require("http"));
31
- const net = __toESM(require("net"));
32
- const wooks = __toESM(require("wooks"));
33
31
  const stream = __toESM(require("stream"));
32
+ const wooks = __toESM(require("wooks"));
33
+ const net = __toESM(require("net"));
34
34
 
35
35
  //#region packages/event-http/src/http-kind.ts
36
36
  /** Event kind definition for HTTP requests. Provides typed context slots for `req`, `response`, and `requestLimits`. */
@@ -1449,7 +1449,7 @@ function error_tl_default(ctx) {
1449
1449
  //#endregion
1450
1450
  //#region packages/event-http/src/response/wooks-http-response.ts
1451
1451
  let framework = {
1452
- version: "0.7.5",
1452
+ version: "0.7.7",
1453
1453
  poweredBy: "wooksjs",
1454
1454
  link: "https://wooks.moost.org/",
1455
1455
  image: "https://wooks.moost.org/wooks-full-logo.svg"
@@ -1673,14 +1673,9 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1673
1673
  }, () => {
1674
1674
  const ctx = (0, __wooksjs_event_core.current)();
1675
1675
  const handlers = this.wooks.lookupHandlers(method, url$1, ctx);
1676
- if (handlers || notFoundHandler) {
1677
- const result = this.processHandlers(handlers || [notFoundHandler], ctx, response);
1678
- if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
1679
- this.logger.error("Internal error, please report", error);
1680
- this.respond(error, response, ctx);
1681
- });
1682
- return result;
1683
- } else if (onNoMatch) onNoMatch(req, res);
1676
+ if (handlers) return this.processAndCatch(handlers, ctx, response);
1677
+ else if (onNoMatch) onNoMatch(req, res);
1678
+ else if (notFoundHandler) return this.processAndCatch([notFoundHandler], ctx, response);
1684
1679
  else {
1685
1680
  this.logger.debug(`404 Not found (${method})${url$1}`);
1686
1681
  const error = new HttpError(404);
@@ -1742,6 +1737,15 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1742
1737
  }
1743
1738
  }
1744
1739
  }
1740
+ /** Runs handlers and attaches a `.catch()` for async results to avoid unhandled rejections. */
1741
+ processAndCatch(handlers, ctx, response) {
1742
+ const result = this.processHandlers(handlers, ctx, response);
1743
+ if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
1744
+ this.logger.error("Internal error, please report", error);
1745
+ this.respond(error, response, ctx);
1746
+ });
1747
+ return result;
1748
+ }
1745
1749
  processHandlers(handlers, ctx, response) {
1746
1750
  for (let i = 0; i < handlers.length; i++) {
1747
1751
  const handler = handlers[i];
@@ -1812,24 +1816,32 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1812
1816
  } catch {}
1813
1817
  const fakeReq = createFakeIncomingMessage(request, pathname, callerReq, this.opts?.forwardHeaders);
1814
1818
  const fakeRes = new http.ServerResponse(fakeReq);
1815
- const rawChunks = [];
1816
- const rawHeaders = {};
1819
+ let rawChunks;
1820
+ let rawHeaders;
1817
1821
  let rawStatusCode = 0;
1818
1822
  fakeRes.writeHead = (...args) => {
1819
1823
  rawStatusCode = args[0];
1820
- for (const arg of args) if (typeof arg === "object" && arg !== null) for (const [k, v] of Object.entries(arg)) rawHeaders[k] = v;
1824
+ for (const arg of args) if (typeof arg === "object" && arg !== null) {
1825
+ if (!rawHeaders) rawHeaders = {};
1826
+ for (const [k, v] of Object.entries(arg)) rawHeaders[k] = v;
1827
+ }
1821
1828
  return fakeRes;
1822
1829
  };
1823
- fakeRes.write = (chunk, ...args) => {
1824
- if (chunk !== null && chunk !== void 0) rawChunks.push(typeof chunk === "string" ? buffer.Buffer.from(chunk) : chunk);
1825
- const cb = args.find((a) => typeof a === "function");
1826
- if (cb) cb();
1830
+ fakeRes.write = (chunk, _encoding, cb) => {
1831
+ if (chunk !== null && chunk !== void 0) {
1832
+ if (!rawChunks) rawChunks = [];
1833
+ rawChunks.push(typeof chunk === "string" ? buffer.Buffer.from(chunk) : chunk);
1834
+ }
1835
+ if (typeof cb === "function") cb();
1827
1836
  return true;
1828
1837
  };
1829
- fakeRes.end = (chunk, ...args) => {
1830
- if (chunk !== null && chunk !== void 0 && typeof chunk !== "function") rawChunks.push(typeof chunk === "string" ? buffer.Buffer.from(chunk) : chunk);
1831
- const cb = args.find((a) => typeof a === "function");
1832
- if (cb) cb();
1838
+ fakeRes.end = (chunk, _encoding, cb) => {
1839
+ if (chunk !== null && chunk !== void 0 && typeof chunk !== "function") {
1840
+ if (!rawChunks) rawChunks = [];
1841
+ rawChunks.push(typeof chunk === "string" ? buffer.Buffer.from(chunk) : chunk);
1842
+ }
1843
+ if (typeof chunk === "function") chunk();
1844
+ else if (typeof cb === "function") cb();
1833
1845
  return fakeRes;
1834
1846
  };
1835
1847
  const response = new this.ResponseClass(fakeRes, fakeReq, this.logger, this.opts?.defaultHeaders, true);
@@ -1837,7 +1849,6 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1837
1849
  if (request.body) bodyBuffer = buffer.Buffer.from(await request.bytes());
1838
1850
  const ctxOptions = this.eventContextOptions;
1839
1851
  const requestLimits = this.opts?.requestLimits;
1840
- const notFoundHandler = this.opts?.onNotFound;
1841
1852
  return createHttpContext(ctxOptions, {
1842
1853
  req: fakeReq,
1843
1854
  response,
@@ -1847,8 +1858,8 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1847
1858
  if (bodyBuffer) ctx.set(rawBodySlot, Promise.resolve(bodyBuffer));
1848
1859
  try {
1849
1860
  const handlers = this.wooks.lookupHandlers(method, pathname, ctx);
1850
- if (handlers || notFoundHandler) {
1851
- const result = this.processHandlers(handlers || [notFoundHandler], ctx, response);
1861
+ if (handlers) {
1862
+ const result = this.processHandlers(handlers, ctx, response);
1852
1863
  if (result !== null && result !== void 0 && typeof result.then === "function") await result.catch((error) => {
1853
1864
  if (!response.responded) this.respond(error, response, ctx);
1854
1865
  });
@@ -1860,11 +1871,11 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1860
1871
  fakeRes.destroy();
1861
1872
  }
1862
1873
  let webResponse;
1863
- if (rawChunks.length > 0 || rawStatusCode > 0) {
1864
- const body = buffer.Buffer.concat(rawChunks);
1865
- webResponse = new Response(body.length > 0 ? body : null, {
1874
+ if (rawChunks || rawStatusCode > 0) {
1875
+ const body = rawChunks ? buffer.Buffer.concat(rawChunks) : null;
1876
+ webResponse = new Response(body && body.length > 0 ? body : null, {
1866
1877
  status: rawStatusCode || 200,
1867
- headers: recordToWebHeaders(rawHeaders)
1878
+ headers: rawHeaders ? recordToWebHeaders(rawHeaders) : void 0
1868
1879
  });
1869
1880
  } else webResponse = response.toWebResponse();
1870
1881
  if (callerReq) try {
@@ -1889,8 +1900,16 @@ var WooksHttp = class extends wooks.WooksAdapterBase {
1889
1900
  return this.fetch(req);
1890
1901
  }
1891
1902
  };
1903
+ var NoopSocket = class extends stream.Duplex {
1904
+ remoteAddress = "127.0.0.1";
1905
+ _read() {}
1906
+ _write(_chunk, _enc, cb) {
1907
+ cb();
1908
+ }
1909
+ };
1910
+ const NOOP_SOCKET = new NoopSocket();
1892
1911
  function createFakeIncomingMessage(request, pathname, forwardFrom, forwardHeaders) {
1893
- const req = new http.IncomingMessage(new net.Socket({}));
1912
+ const req = new http.IncomingMessage(NOOP_SOCKET);
1894
1913
  req.method = request.method;
1895
1914
  req.url = pathname;
1896
1915
  const headers = {};
@@ -1901,9 +1920,7 @@ function createFakeIncomingMessage(request, pathname, forwardFrom, forwardHeader
1901
1920
  if (typeof val === "string" && val) headers[h] = val;
1902
1921
  }
1903
1922
  }
1904
- request.headers.forEach((value, key) => {
1905
- headers[key] = value;
1906
- });
1923
+ for (const [key, value] of request.headers) headers[key] = value;
1907
1924
  req.headers = headers;
1908
1925
  return req;
1909
1926
  }
package/dist/index.d.ts CHANGED
@@ -610,6 +610,8 @@ declare class WooksHttp extends WooksAdapterBase {
610
610
  */
611
611
  getUpgradeCb(): (req: IncomingMessage, socket: Duplex, head: Buffer) => void;
612
612
  protected processUpgradeHandlers(handlers: TWooksHandler[], ctx: EventContext, socket: Duplex): void | Promise<unknown>;
613
+ /** Runs handlers and attaches a `.catch()` for async results to avoid unhandled rejections. */
614
+ private processAndCatch;
613
615
  protected processHandlers(handlers: TWooksHandler[], ctx: EventContext, response: HttpResponse): void | Promise<unknown>;
614
616
  private processAsyncResult;
615
617
  /**
package/dist/index.mjs CHANGED
@@ -5,9 +5,9 @@ import { promisify } from "node:util";
5
5
  import { createBrotliCompress, createBrotliDecompress, createDeflate, createGunzip, createGzip, createInflate } from "node:zlib";
6
6
  import { URLSearchParams } from "url";
7
7
  import http, { IncomingMessage, ServerResponse } from "http";
8
- import { Socket } from "net";
8
+ import { Duplex, Readable as Readable$1 } from "stream";
9
9
  import { WooksAdapterBase } from "wooks";
10
- import { Readable as Readable$1 } from "stream";
10
+ import { Socket } from "net";
11
11
 
12
12
  //#region packages/event-http/src/http-kind.ts
13
13
  /** Event kind definition for HTTP requests. Provides typed context slots for `req`, `response`, and `requestLimits`. */
@@ -1426,7 +1426,7 @@ function error_tl_default(ctx) {
1426
1426
  //#endregion
1427
1427
  //#region packages/event-http/src/response/wooks-http-response.ts
1428
1428
  let framework = {
1429
- version: "0.7.5",
1429
+ version: "0.7.7",
1430
1430
  poweredBy: "wooksjs",
1431
1431
  link: "https://wooks.moost.org/",
1432
1432
  image: "https://wooks.moost.org/wooks-full-logo.svg"
@@ -1650,14 +1650,9 @@ var WooksHttp = class extends WooksAdapterBase {
1650
1650
  }, () => {
1651
1651
  const ctx = current();
1652
1652
  const handlers = this.wooks.lookupHandlers(method, url, ctx);
1653
- if (handlers || notFoundHandler) {
1654
- const result = this.processHandlers(handlers || [notFoundHandler], ctx, response);
1655
- if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
1656
- this.logger.error("Internal error, please report", error);
1657
- this.respond(error, response, ctx);
1658
- });
1659
- return result;
1660
- } else if (onNoMatch) onNoMatch(req, res);
1653
+ if (handlers) return this.processAndCatch(handlers, ctx, response);
1654
+ else if (onNoMatch) onNoMatch(req, res);
1655
+ else if (notFoundHandler) return this.processAndCatch([notFoundHandler], ctx, response);
1661
1656
  else {
1662
1657
  this.logger.debug(`404 Not found (${method})${url}`);
1663
1658
  const error = new HttpError(404);
@@ -1719,6 +1714,15 @@ var WooksHttp = class extends WooksAdapterBase {
1719
1714
  }
1720
1715
  }
1721
1716
  }
1717
+ /** Runs handlers and attaches a `.catch()` for async results to avoid unhandled rejections. */
1718
+ processAndCatch(handlers, ctx, response) {
1719
+ const result = this.processHandlers(handlers, ctx, response);
1720
+ if (result !== null && result !== void 0 && typeof result.then === "function") result.catch((error) => {
1721
+ this.logger.error("Internal error, please report", error);
1722
+ this.respond(error, response, ctx);
1723
+ });
1724
+ return result;
1725
+ }
1722
1726
  processHandlers(handlers, ctx, response) {
1723
1727
  for (let i = 0; i < handlers.length; i++) {
1724
1728
  const handler = handlers[i];
@@ -1789,24 +1793,32 @@ var WooksHttp = class extends WooksAdapterBase {
1789
1793
  } catch {}
1790
1794
  const fakeReq = createFakeIncomingMessage(request, pathname, callerReq, this.opts?.forwardHeaders);
1791
1795
  const fakeRes = new ServerResponse(fakeReq);
1792
- const rawChunks = [];
1793
- const rawHeaders = {};
1796
+ let rawChunks;
1797
+ let rawHeaders;
1794
1798
  let rawStatusCode = 0;
1795
1799
  fakeRes.writeHead = (...args) => {
1796
1800
  rawStatusCode = args[0];
1797
- for (const arg of args) if (typeof arg === "object" && arg !== null) for (const [k, v] of Object.entries(arg)) rawHeaders[k] = v;
1801
+ for (const arg of args) if (typeof arg === "object" && arg !== null) {
1802
+ if (!rawHeaders) rawHeaders = {};
1803
+ for (const [k, v] of Object.entries(arg)) rawHeaders[k] = v;
1804
+ }
1798
1805
  return fakeRes;
1799
1806
  };
1800
- fakeRes.write = (chunk, ...args) => {
1801
- if (chunk !== null && chunk !== void 0) rawChunks.push(typeof chunk === "string" ? Buffer$1.from(chunk) : chunk);
1802
- const cb = args.find((a) => typeof a === "function");
1803
- if (cb) cb();
1807
+ fakeRes.write = (chunk, _encoding, cb) => {
1808
+ if (chunk !== null && chunk !== void 0) {
1809
+ if (!rawChunks) rawChunks = [];
1810
+ rawChunks.push(typeof chunk === "string" ? Buffer$1.from(chunk) : chunk);
1811
+ }
1812
+ if (typeof cb === "function") cb();
1804
1813
  return true;
1805
1814
  };
1806
- fakeRes.end = (chunk, ...args) => {
1807
- if (chunk !== null && chunk !== void 0 && typeof chunk !== "function") rawChunks.push(typeof chunk === "string" ? Buffer$1.from(chunk) : chunk);
1808
- const cb = args.find((a) => typeof a === "function");
1809
- if (cb) cb();
1815
+ fakeRes.end = (chunk, _encoding, cb) => {
1816
+ if (chunk !== null && chunk !== void 0 && typeof chunk !== "function") {
1817
+ if (!rawChunks) rawChunks = [];
1818
+ rawChunks.push(typeof chunk === "string" ? Buffer$1.from(chunk) : chunk);
1819
+ }
1820
+ if (typeof chunk === "function") chunk();
1821
+ else if (typeof cb === "function") cb();
1810
1822
  return fakeRes;
1811
1823
  };
1812
1824
  const response = new this.ResponseClass(fakeRes, fakeReq, this.logger, this.opts?.defaultHeaders, true);
@@ -1814,7 +1826,6 @@ var WooksHttp = class extends WooksAdapterBase {
1814
1826
  if (request.body) bodyBuffer = Buffer$1.from(await request.bytes());
1815
1827
  const ctxOptions = this.eventContextOptions;
1816
1828
  const requestLimits = this.opts?.requestLimits;
1817
- const notFoundHandler = this.opts?.onNotFound;
1818
1829
  return createHttpContext(ctxOptions, {
1819
1830
  req: fakeReq,
1820
1831
  response,
@@ -1824,8 +1835,8 @@ var WooksHttp = class extends WooksAdapterBase {
1824
1835
  if (bodyBuffer) ctx.set(rawBodySlot, Promise.resolve(bodyBuffer));
1825
1836
  try {
1826
1837
  const handlers = this.wooks.lookupHandlers(method, pathname, ctx);
1827
- if (handlers || notFoundHandler) {
1828
- const result = this.processHandlers(handlers || [notFoundHandler], ctx, response);
1838
+ if (handlers) {
1839
+ const result = this.processHandlers(handlers, ctx, response);
1829
1840
  if (result !== null && result !== void 0 && typeof result.then === "function") await result.catch((error) => {
1830
1841
  if (!response.responded) this.respond(error, response, ctx);
1831
1842
  });
@@ -1837,11 +1848,11 @@ var WooksHttp = class extends WooksAdapterBase {
1837
1848
  fakeRes.destroy();
1838
1849
  }
1839
1850
  let webResponse;
1840
- if (rawChunks.length > 0 || rawStatusCode > 0) {
1841
- const body = Buffer$1.concat(rawChunks);
1842
- webResponse = new Response(body.length > 0 ? body : null, {
1851
+ if (rawChunks || rawStatusCode > 0) {
1852
+ const body = rawChunks ? Buffer$1.concat(rawChunks) : null;
1853
+ webResponse = new Response(body && body.length > 0 ? body : null, {
1843
1854
  status: rawStatusCode || 200,
1844
- headers: recordToWebHeaders(rawHeaders)
1855
+ headers: rawHeaders ? recordToWebHeaders(rawHeaders) : void 0
1845
1856
  });
1846
1857
  } else webResponse = response.toWebResponse();
1847
1858
  if (callerReq) try {
@@ -1866,8 +1877,16 @@ var WooksHttp = class extends WooksAdapterBase {
1866
1877
  return this.fetch(req);
1867
1878
  }
1868
1879
  };
1880
+ var NoopSocket = class extends Duplex {
1881
+ remoteAddress = "127.0.0.1";
1882
+ _read() {}
1883
+ _write(_chunk, _enc, cb) {
1884
+ cb();
1885
+ }
1886
+ };
1887
+ const NOOP_SOCKET = new NoopSocket();
1869
1888
  function createFakeIncomingMessage(request, pathname, forwardFrom, forwardHeaders) {
1870
- const req = new IncomingMessage(new Socket({}));
1889
+ const req = new IncomingMessage(NOOP_SOCKET);
1871
1890
  req.method = request.method;
1872
1891
  req.url = pathname;
1873
1892
  const headers = {};
@@ -1878,9 +1897,7 @@ function createFakeIncomingMessage(request, pathname, forwardFrom, forwardHeader
1878
1897
  if (typeof val === "string" && val) headers[h] = val;
1879
1898
  }
1880
1899
  }
1881
- request.headers.forEach((value, key) => {
1882
- headers[key] = value;
1883
- });
1900
+ for (const [key, value] of request.headers) headers[key] = value;
1884
1901
  req.headers = headers;
1885
1902
  return req;
1886
1903
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/event-http",
3
- "version": "0.7.6",
3
+ "version": "0.7.8",
4
4
  "description": "@wooksjs/event-http",
5
5
  "keywords": [
6
6
  "api",
@@ -47,14 +47,14 @@
47
47
  "devDependencies": {
48
48
  "typescript": "^5.9.3",
49
49
  "vitest": "^3.2.4",
50
- "@wooksjs/event-core": "^0.7.6",
51
- "wooks": "^0.7.6"
50
+ "@wooksjs/event-core": "^0.7.8",
51
+ "wooks": "^0.7.8"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "@prostojs/logger": "^0.4.3",
55
55
  "@prostojs/router": "^0.3.3",
56
- "@wooksjs/event-core": "^0.7.6",
57
- "wooks": "^0.7.6"
56
+ "@wooksjs/event-core": "^0.7.8",
57
+ "wooks": "^0.7.8"
58
58
  },
59
59
  "scripts": {
60
60
  "build": "rolldown -c ../../rolldown.config.mjs",