h3 1.8.0 → 1.8.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/README.md CHANGED
@@ -213,7 +213,7 @@ H3 has a concept of composable utilities that accept `event` (from `eventHandler
213
213
  #### Request
214
214
 
215
215
  - `getQuery(event)`
216
- - `getValidatedBody(event, validate)`
216
+ - `getValidatedQuery(event, validate)`
217
217
  - `getRouterParams(event)`
218
218
  - `getMethod(event, default?)`
219
219
  - `isMethod(event, expected, allowHead?)`
package/dist/index.cjs CHANGED
@@ -33,12 +33,29 @@ function useBase(base, handler) {
33
33
  });
34
34
  }
35
35
 
36
+ function hasProp(obj, prop) {
37
+ try {
38
+ return prop in obj;
39
+ } catch {
40
+ return false;
41
+ }
42
+ }
43
+
44
+ var __defProp$1 = Object.defineProperty;
45
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
46
+ var __publicField$1 = (obj, key, value) => {
47
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
48
+ return value;
49
+ };
36
50
  class H3Error extends Error {
37
51
  constructor(message, opts = {}) {
38
52
  super(message, opts);
39
- this.statusCode = 500;
40
- this.fatal = false;
41
- this.unhandled = false;
53
+ __publicField$1(this, "statusCode", 500);
54
+ __publicField$1(this, "fatal", false);
55
+ __publicField$1(this, "unhandled", false);
56
+ __publicField$1(this, "statusMessage");
57
+ __publicField$1(this, "data");
58
+ __publicField$1(this, "cause");
42
59
  if (opts.cause && !this.cause) {
43
60
  this.cause = opts.cause;
44
61
  }
@@ -57,7 +74,7 @@ class H3Error extends Error {
57
74
  return obj;
58
75
  }
59
76
  }
60
- H3Error.__h3_error__ = true;
77
+ __publicField$1(H3Error, "__h3_error__", true);
61
78
  function createError(input) {
62
79
  if (typeof input === "string") {
63
80
  return new H3Error(input);
@@ -68,7 +85,7 @@ function createError(input) {
68
85
  const err = new H3Error(input.message ?? input.statusMessage ?? "", {
69
86
  cause: input.cause || input
70
87
  });
71
- if ("stack" in input) {
88
+ if (hasProp(input, "stack")) {
72
89
  try {
73
90
  Object.defineProperty(err, "stack", {
74
91
  get() {
@@ -304,7 +321,7 @@ function getRequestProtocol(event, opts = {}) {
304
321
  if (opts.xForwardedProto !== false && event.node.req.headers["x-forwarded-proto"] === "https") {
305
322
  return "https";
306
323
  }
307
- return event.node.req.connection.encrypted ? "https" : "http";
324
+ return event.node.req.connection?.encrypted ? "https" : "http";
308
325
  }
309
326
  const DOUBLE_SLASH_RE = /[/\\]{2,}/g;
310
327
  function getRequestPath(event) {
@@ -409,7 +426,7 @@ function readRawBody(event, encoding = "utf8") {
409
426
  }
410
427
  async function readBody(event, options = {}) {
411
428
  const request = event.node.req;
412
- if (ParsedBodySymbol in request) {
429
+ if (hasProp(request, ParsedBodySymbol)) {
413
430
  return request[ParsedBodySymbol];
414
431
  }
415
432
  const contentType = request.headers["content-type"] || "";
@@ -485,7 +502,7 @@ function _parseURLEncodedBody(body) {
485
502
  const form = new URLSearchParams(body);
486
503
  const parsedForm = /* @__PURE__ */ Object.create(null);
487
504
  for (const [key, value] of form.entries()) {
488
- if (key in parsedForm) {
505
+ if (hasProp(parsedForm, key)) {
489
506
  if (!Array.isArray(parsedForm[key])) {
490
507
  parsedForm[key] = [parsedForm[key]];
491
508
  }
@@ -767,7 +784,7 @@ function sendStream(event, stream) {
767
784
  event._handled = true;
768
785
  return Promise.resolve();
769
786
  }
770
- if ("pipeTo" in stream) {
787
+ if (hasProp(stream, "pipeTo") && typeof stream.pipeTo === "function") {
771
788
  return stream.pipeTo(
772
789
  new WritableStream({
773
790
  write(chunk) {
@@ -778,7 +795,7 @@ function sendStream(event, stream) {
778
795
  event.node.res.end();
779
796
  });
780
797
  }
781
- if (typeof stream.pipe === "function") {
798
+ if (hasProp(stream, "pipe") && typeof stream.pipe === "function") {
782
799
  return new Promise((resolve, reject) => {
783
800
  stream.pipe(event.node.res);
784
801
  if (stream.on) {
@@ -1371,13 +1388,29 @@ function idSearchPaths(id, encodings, indexNames) {
1371
1388
  return ids;
1372
1389
  }
1373
1390
 
1391
+ var __defProp = Object.defineProperty;
1392
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1393
+ var __publicField = (obj, key, value) => {
1394
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1395
+ return value;
1396
+ };
1374
1397
  class H3Event {
1375
1398
  constructor(req, res) {
1376
- this["__is_event__"] = true;
1399
+ __publicField(this, "__is_event__", true);
1400
+ // Context
1401
+ __publicField(this, "node");
1402
+ // Node
1403
+ __publicField(this, "web");
1377
1404
  // Web
1378
- this.context = {};
1405
+ __publicField(this, "context", {});
1406
+ // Shared
1407
+ // Request
1408
+ __publicField(this, "_method");
1409
+ __publicField(this, "_path");
1410
+ __publicField(this, "_headers");
1411
+ __publicField(this, "_requestBody");
1379
1412
  // Response
1380
- this._handled = false;
1413
+ __publicField(this, "_handled", false);
1381
1414
  this.node = { req, res };
1382
1415
  }
1383
1416
  // --- Request ---
@@ -1423,7 +1456,7 @@ class H3Event {
1423
1456
  }
1424
1457
  }
1425
1458
  function isEvent(input) {
1426
- return "__is_event__" in input;
1459
+ return hasProp(input, "__is_event__");
1427
1460
  }
1428
1461
  function createEvent(req, res) {
1429
1462
  return new H3Event(req, res);
@@ -1484,7 +1517,7 @@ function defineResponseMiddleware(fn) {
1484
1517
  return fn;
1485
1518
  }
1486
1519
  function isEventHandler(input) {
1487
- return "__is_handler__" in input;
1520
+ return hasProp(input, "__is_handler__");
1488
1521
  }
1489
1522
  function toEventHandler(input, _, _route) {
1490
1523
  if (!isEventHandler(input)) {
@@ -1541,94 +1574,8 @@ function defineLazyEventHandler(factory) {
1541
1574
  }
1542
1575
  const lazyEventHandler = defineLazyEventHandler;
1543
1576
 
1544
- class H3Headers {
1545
- constructor(init) {
1546
- if (!init) {
1547
- this._headers = {};
1548
- } else if (Array.isArray(init)) {
1549
- this._headers = Object.fromEntries(
1550
- init.map(([key, value]) => [key.toLowerCase(), value])
1551
- );
1552
- } else if (init && "append" in init) {
1553
- this._headers = Object.fromEntries(init.entries());
1554
- } else {
1555
- this._headers = Object.fromEntries(
1556
- Object.entries(init).map(([key, value]) => [key.toLowerCase(), value])
1557
- );
1558
- }
1559
- }
1560
- [Symbol.iterator]() {
1561
- return this.entries();
1562
- }
1563
- entries() {
1564
- throw Object.entries(this._headers)[Symbol.iterator]();
1565
- }
1566
- keys() {
1567
- return Object.keys(this._headers)[Symbol.iterator]();
1568
- }
1569
- values() {
1570
- throw Object.values(this._headers)[Symbol.iterator]();
1571
- }
1572
- append(name, value) {
1573
- const _name = name.toLowerCase();
1574
- this.set(_name, [this.get(_name), value].filter(Boolean).join(", "));
1575
- }
1576
- delete(name) {
1577
- delete this._headers[name.toLowerCase()];
1578
- }
1579
- get(name) {
1580
- return this._headers[name.toLowerCase()];
1581
- }
1582
- has(name) {
1583
- return name.toLowerCase() in this._headers;
1584
- }
1585
- set(name, value) {
1586
- this._headers[name.toLowerCase()] = String(value);
1587
- }
1588
- forEach(callbackfn) {
1589
- for (const [key, value] of Object.entries(this._headers)) {
1590
- callbackfn(value, key, this);
1591
- }
1592
- }
1593
- }
1594
-
1595
- class H3Response {
1596
- constructor(body = null, init = {}) {
1597
- // TODO: yet to implement
1598
- this.body = null;
1599
- this.type = "default";
1600
- this.bodyUsed = false;
1601
- this.headers = new H3Headers(init.headers);
1602
- this.status = init.status ?? 200;
1603
- this.statusText = init.statusText || "";
1604
- this.redirected = !!init.status && [301, 302, 307, 308].includes(init.status);
1605
- this._body = body;
1606
- this.url = "";
1607
- this.ok = this.status < 300 && this.status > 199;
1608
- }
1609
- clone() {
1610
- return new H3Response(this.body, {
1611
- headers: this.headers,
1612
- status: this.status,
1613
- statusText: this.statusText
1614
- });
1615
- }
1616
- arrayBuffer() {
1617
- return Promise.resolve(this._body);
1618
- }
1619
- blob() {
1620
- return Promise.resolve(this._body);
1621
- }
1622
- formData() {
1623
- return Promise.resolve(this._body);
1624
- }
1625
- json() {
1626
- return Promise.resolve(this._body);
1627
- }
1628
- text() {
1629
- return Promise.resolve(this._body);
1630
- }
1631
- }
1577
+ const H3Headers = globalThis.Headers;
1578
+ const H3Response = globalThis.Response;
1632
1579
 
1633
1580
  function createApp(options = {}) {
1634
1581
  const stack = [];