@stacksjs/bun-router 0.0.7 → 0.0.9

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.js CHANGED
@@ -66,7 +66,7 @@ import {
66
66
  signRequest,
67
67
  singleFileUpload,
68
68
  verifySignature
69
- } from "./chunk-jhsk5c5b.js";
69
+ } from "./chunk-n5pvpz7q.js";
70
70
 
71
71
  // src/auth.ts
72
72
  function base64UrlEncode(str) {
@@ -2787,7 +2787,8 @@ function request() {
2787
2787
  // src/response/response-factory.ts
2788
2788
  var response = {
2789
2789
  json: (data, options = {}) => {
2790
- const { status = 200, headers = {}, pretty = false } = options;
2790
+ const opts = typeof options === "number" ? { status: options } : options;
2791
+ const { status = 200, headers = {}, pretty = false } = opts;
2791
2792
  const body = pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
2792
2793
  return new Response(body, {
2793
2794
  status,
@@ -9276,6 +9277,10 @@ class Router {
9276
9277
  enhancedReq.header = (name) => {
9277
9278
  return req.headers.get(name) || req.headers.get(name.toLowerCase()) || null;
9278
9279
  };
9280
+ enhancedReq.cookie = (name, defaultValue) => {
9281
+ const value = getCookies()[name];
9282
+ return value !== undefined ? value : defaultValue ?? null;
9283
+ };
9279
9284
  enhancedReq.getParam = (name, defaultValue) => {
9280
9285
  const value = params?.[name];
9281
9286
  return value !== undefined ? value : defaultValue;
@@ -9530,40 +9535,358 @@ class RouteGroupBuilder2 {
9530
9535
  return this;
9531
9536
  }
9532
9537
  }
9538
+ var _enhancementHost = null;
9539
+ function getEnhancementHost() {
9540
+ if (_enhancementHost === null)
9541
+ _enhancementHost = new Router;
9542
+ return _enhancementHost;
9543
+ }
9544
+ function applyRequestEnhancements(req, params = {}) {
9545
+ if (typeof req.bearerToken === "function")
9546
+ return req;
9547
+ return getEnhancementHost().enhanceRequest(req, params);
9548
+ }
9533
9549
 
9534
- // src/router/server.ts
9535
- function registerServerHandling(RouterClass) {
9536
- Object.defineProperties(RouterClass.prototype, {
9537
- serve: {
9538
- async value(options = {}) {
9539
- if (this._initFileRoutes) {
9540
- await this._initFileRoutes();
9541
- }
9542
- if (this._initApiRoutes) {
9543
- await this._initApiRoutes();
9544
- }
9545
- this.invalidateCache();
9546
- const serverOptions = {
9547
- idleTimeout: 255,
9548
- ...options,
9549
- fetch: this.handleRequest.bind(this)
9550
- };
9551
- const staticRouteMap = {};
9552
- if (this.staticResponses && this.staticResponses.size > 0) {
9553
- for (const [path, response2] of this.staticResponses) {
9554
- staticRouteMap[path] = response2;
9555
- }
9556
- }
9557
- if (options.static) {
9558
- Object.assign(staticRouteMap, options.static);
9559
- }
9560
- if (Object.keys(staticRouteMap).length > 0) {
9561
- serverOptions.static = staticRouteMap;
9562
- }
9563
- if (options.development !== undefined) {
9564
- serverOptions.development = options.development;
9550
+ // src/request/macros.ts
9551
+ class RequestMacroRegistry {
9552
+ macros = new Map;
9553
+ register(name, handler) {
9554
+ this.macros.set(name, { name, handler });
9555
+ }
9556
+ get(name) {
9557
+ return this.macros.get(name);
9558
+ }
9559
+ has(name) {
9560
+ return this.macros.has(name);
9561
+ }
9562
+ all() {
9563
+ return Array.from(this.macros.values());
9564
+ }
9565
+ remove(name) {
9566
+ return this.macros.delete(name);
9567
+ }
9568
+ clear() {
9569
+ this.macros.clear();
9570
+ }
9571
+ }
9572
+ var requestMacroRegistry = new RequestMacroRegistry;
9573
+
9574
+ class RequestWithMacros {
9575
+ static macro(name, handler) {
9576
+ requestMacroRegistry.register(name, handler);
9577
+ }
9578
+ static applyMacros(request2) {
9579
+ const macros = requestMacroRegistry.all();
9580
+ macros.forEach((macro) => {
9581
+ request2[macro.name] = macro.handler.bind(request2);
9582
+ });
9583
+ return request2;
9584
+ }
9585
+ static hasMacro(name) {
9586
+ return requestMacroRegistry.has(name);
9587
+ }
9588
+ }
9589
+ var BuiltInRequestMacros = {
9590
+ wantsJson() {
9591
+ const accept = this.headers.get("accept") || "";
9592
+ return accept.includes("application/json") || accept.includes("json");
9593
+ },
9594
+ wantsHtml() {
9595
+ const accept = this.headers.get("accept") || "";
9596
+ return accept.includes("text/html") || accept.includes("html");
9597
+ },
9598
+ wantsXml() {
9599
+ const accept = this.headers.get("accept") || "";
9600
+ return accept.includes("application/xml") || accept.includes("xml");
9601
+ },
9602
+ expectsJson() {
9603
+ return this.wantsJson() || this.isAjax() || this.isPjax();
9604
+ },
9605
+ isAjax() {
9606
+ return this.headers.get("x-requested-with") === "XMLHttpRequest";
9607
+ },
9608
+ isPjax() {
9609
+ return this.headers.get("x-pjax") === "true";
9610
+ },
9611
+ isMobile() {
9612
+ const userAgent = this.headers.get("user-agent") || "";
9613
+ const mobileRegex = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
9614
+ return mobileRegex.test(userAgent);
9615
+ },
9616
+ isBot() {
9617
+ const userAgent = this.headers.get("user-agent") || "";
9618
+ const botRegex = /bot|crawler|spider|crawling/i;
9619
+ return botRegex.test(userAgent);
9620
+ },
9621
+ isSecure() {
9622
+ return this.url.startsWith("https://") || this.headers.get("x-forwarded-proto") === "https" || this.headers.get("x-forwarded-ssl") === "on";
9623
+ },
9624
+ ip() {
9625
+ return this.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || this.headers.get("x-real-ip") || this.headers.get("cf-connecting-ip") || this.headers.get("x-client-ip") || "unknown";
9626
+ },
9627
+ ips() {
9628
+ const forwardedFor = this.headers.get("x-forwarded-for");
9629
+ if (forwardedFor) {
9630
+ return forwardedFor.split(",").map((ip) => ip.trim());
9631
+ }
9632
+ return [BuiltInRequestMacros.ip.call(this)];
9633
+ },
9634
+ userAgent() {
9635
+ return this.headers.get("user-agent") || "";
9636
+ },
9637
+ referer() {
9638
+ return this.headers.get("referer") || this.headers.get("referrer") || null;
9639
+ },
9640
+ bearerToken() {
9641
+ const auth2 = this.headers.get("authorization");
9642
+ if (auth2 && auth2.startsWith("Bearer ")) {
9643
+ return auth2.substring(7);
9644
+ }
9645
+ return null;
9646
+ },
9647
+ basicAuth() {
9648
+ const auth2 = this.headers.get("authorization");
9649
+ if (auth2 && auth2.startsWith("Basic ")) {
9650
+ try {
9651
+ const decoded = atob(auth2.substring(6));
9652
+ const [username, password] = decoded.split(":");
9653
+ return { username, password };
9654
+ } catch {
9655
+ return null;
9656
+ }
9657
+ }
9658
+ return null;
9659
+ },
9660
+ hasHeader(name) {
9661
+ return this.headers.has(name.toLowerCase());
9662
+ },
9663
+ header(name, defaultValue) {
9664
+ return this.headers.get(name.toLowerCase()) || defaultValue || null;
9665
+ },
9666
+ allHeaders() {
9667
+ const headers = {};
9668
+ this.headers.forEach((value, key) => {
9669
+ headers[key] = value;
9670
+ });
9671
+ return headers;
9672
+ },
9673
+ input(key, defaultValue) {
9674
+ if (this.validated && key in this.validated) {
9675
+ return this.validated[key];
9676
+ }
9677
+ if (this.query && key in this.query) {
9678
+ return this.query[key];
9679
+ }
9680
+ if (this.params && key in this.params) {
9681
+ return this.params[key];
9682
+ }
9683
+ if (this.jsonBody && typeof this.jsonBody === "object" && key in this.jsonBody) {
9684
+ return this.jsonBody[key];
9685
+ }
9686
+ if (this.formBody && typeof this.formBody === "object" && key in this.formBody) {
9687
+ return this.formBody[key];
9688
+ }
9689
+ return defaultValue;
9690
+ },
9691
+ all() {
9692
+ return {
9693
+ ...this.params,
9694
+ ...this.query,
9695
+ ...typeof this.jsonBody === "object" && this.jsonBody !== null ? this.jsonBody : {},
9696
+ ...typeof this.formBody === "object" && this.formBody !== null ? this.formBody : {},
9697
+ ...typeof this.validated === "object" && this.validated !== null ? this.validated : {}
9698
+ };
9699
+ },
9700
+ only(keys) {
9701
+ const all = this.all();
9702
+ const result = {};
9703
+ keys.forEach((key) => {
9704
+ if (key in all) {
9705
+ result[key] = all[key];
9706
+ }
9707
+ });
9708
+ return result;
9709
+ },
9710
+ except(keys) {
9711
+ const all = this.all();
9712
+ const result = {};
9713
+ Object.keys(all).forEach((key) => {
9714
+ if (!keys.includes(key)) {
9715
+ result[key] = all[key];
9716
+ }
9717
+ });
9718
+ return result;
9719
+ },
9720
+ has(key) {
9721
+ const all = this.all();
9722
+ return key in all && all[key] !== undefined && all[key] !== null && all[key] !== "";
9723
+ },
9724
+ hasAny(keys) {
9725
+ return keys.some((key) => this.has(key));
9726
+ },
9727
+ missing(key) {
9728
+ return !this.has(key);
9729
+ },
9730
+ filled(key) {
9731
+ const value = this.input(key);
9732
+ if (value === null || value === undefined)
9733
+ return false;
9734
+ if (typeof value === "string")
9735
+ return value.trim().length > 0;
9736
+ if (Array.isArray(value))
9737
+ return value.length > 0;
9738
+ if (typeof value === "object")
9739
+ return Object.keys(value).length > 0;
9740
+ return true;
9741
+ },
9742
+ getQuery(key, defaultValue) {
9743
+ if (!key)
9744
+ return this.query || {};
9745
+ return this.query?.[key] || defaultValue;
9746
+ },
9747
+ param(key, defaultValue) {
9748
+ return this.params?.[key] || defaultValue;
9749
+ },
9750
+ cookie(name, defaultValue) {
9751
+ const cookies = this.headers.get("cookie");
9752
+ if (!cookies)
9753
+ return defaultValue || null;
9754
+ const cookieArray = cookies.split(";");
9755
+ for (const cookie of cookieArray) {
9756
+ const [key, value] = cookie.trim().split("=");
9757
+ if (key === name) {
9758
+ return decodeURIComponent(value);
9759
+ }
9760
+ }
9761
+ return defaultValue || null;
9762
+ },
9763
+ cookies() {
9764
+ const cookies = {};
9765
+ const cookieHeader = this.headers.get("cookie");
9766
+ if (cookieHeader) {
9767
+ cookieHeader.split(";").forEach((cookie) => {
9768
+ const [key, value] = cookie.trim().split("=");
9769
+ if (key && value) {
9770
+ cookies[key] = decodeURIComponent(value);
9565
9771
  }
9566
- if (this.wsConfig) {
9772
+ });
9773
+ }
9774
+ return cookies;
9775
+ },
9776
+ file(name) {
9777
+ return this.files?.find((f) => f.fieldName === name);
9778
+ },
9779
+ hasFile(name) {
9780
+ return !!this.files?.find((f) => f.fieldName === name);
9781
+ },
9782
+ path() {
9783
+ return new URL(this.url).pathname;
9784
+ },
9785
+ fullUrl() {
9786
+ return this.url;
9787
+ },
9788
+ root() {
9789
+ const url2 = new URL(this.url);
9790
+ return `${url2.protocol}//${url2.host}`;
9791
+ },
9792
+ is(pattern) {
9793
+ const path = this.path();
9794
+ const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
9795
+ const regex = new RegExp(`^${regexPattern}$`);
9796
+ return regex.test(path);
9797
+ },
9798
+ route() {
9799
+ return this.routeName || null;
9800
+ },
9801
+ fingerprint() {
9802
+ const url2 = new URL(this.url);
9803
+ const data = {
9804
+ method: this.method,
9805
+ path: url2.pathname,
9806
+ query: url2.search,
9807
+ ip: BuiltInRequestMacros.ip.call(this)
9808
+ };
9809
+ return btoa(JSON.stringify(data));
9810
+ },
9811
+ signature(secret) {
9812
+ const data = `${this.method}${this.url}${BuiltInRequestMacros.ip.call(this)}${secret}`;
9813
+ let hash = 0;
9814
+ for (let i = 0;i < data.length; i++) {
9815
+ const char = data.charCodeAt(i);
9816
+ hash = (hash << 5) - hash + char;
9817
+ hash = hash & hash;
9818
+ }
9819
+ return Math.abs(hash).toString(36);
9820
+ },
9821
+ merge(data) {
9822
+ if (!this.validated) {
9823
+ this.validated = {};
9824
+ }
9825
+ Object.assign(this.validated, data);
9826
+ },
9827
+ replace(data) {
9828
+ this.validated = { ...data };
9829
+ },
9830
+ age() {
9831
+ const startTime = this.startTime || Date.now();
9832
+ return Date.now() - startTime;
9833
+ },
9834
+ isFromTrustedProxy(trustedProxies = []) {
9835
+ const ip = BuiltInRequestMacros.ip.call(this);
9836
+ return trustedProxies.includes(ip);
9837
+ },
9838
+ contentLength() {
9839
+ const length = this.headers.get("content-length");
9840
+ return length ? Number.parseInt(length, 10) : 0;
9841
+ },
9842
+ contentType() {
9843
+ return this.headers.get("content-type");
9844
+ },
9845
+ isContentType(type) {
9846
+ const contentType = this.contentType();
9847
+ return contentType ? contentType.includes(type) : false;
9848
+ }
9849
+ };
9850
+ function registerBuiltInRequestMacros() {
9851
+ Object.entries(BuiltInRequestMacros).forEach(([name, handler]) => {
9852
+ RequestWithMacros.macro(name, handler);
9853
+ });
9854
+ }
9855
+ registerBuiltInRequestMacros();
9856
+
9857
+ // src/router/server.ts
9858
+ function registerServerHandling(RouterClass) {
9859
+ Object.defineProperties(RouterClass.prototype, {
9860
+ serve: {
9861
+ async value(options = {}) {
9862
+ if (this._initFileRoutes) {
9863
+ await this._initFileRoutes();
9864
+ }
9865
+ if (this._initApiRoutes) {
9866
+ await this._initApiRoutes();
9867
+ }
9868
+ this.invalidateCache();
9869
+ const serverOptions = {
9870
+ idleTimeout: 255,
9871
+ ...options,
9872
+ fetch: this.handleRequest.bind(this)
9873
+ };
9874
+ const staticRouteMap = {};
9875
+ if (this.staticResponses && this.staticResponses.size > 0) {
9876
+ for (const [path, response2] of this.staticResponses) {
9877
+ staticRouteMap[path] = response2;
9878
+ }
9879
+ }
9880
+ if (options.static) {
9881
+ Object.assign(staticRouteMap, options.static);
9882
+ }
9883
+ if (Object.keys(staticRouteMap).length > 0) {
9884
+ serverOptions.static = staticRouteMap;
9885
+ }
9886
+ if (options.development !== undefined) {
9887
+ serverOptions.development = options.development;
9888
+ }
9889
+ if (this.wsConfig) {
9567
9890
  serverOptions.websocket = this.wsConfig;
9568
9891
  }
9569
9892
  this.serverInstance = Bun.serve(serverOptions);
@@ -9761,6 +10084,7 @@ function registerServerHandling(RouterClass) {
9761
10084
  _cookiesToSet: [],
9762
10085
  _cookiesToDelete: []
9763
10086
  });
10087
+ RequestWithMacros.applyMacros(enhancedReq);
9764
10088
  Object.assign(enhancedReq, { cookies: { ...getCookies(), ...cookies } });
9765
10089
  return enhancedReq;
9766
10090
  },
@@ -10038,313 +10362,6 @@ function registerWebSocketHandling(RouterClass) {
10038
10362
  }
10039
10363
  });
10040
10364
  }
10041
- // src/request/macros.ts
10042
- class RequestMacroRegistry {
10043
- macros = new Map;
10044
- register(name, handler) {
10045
- this.macros.set(name, { name, handler });
10046
- }
10047
- get(name) {
10048
- return this.macros.get(name);
10049
- }
10050
- has(name) {
10051
- return this.macros.has(name);
10052
- }
10053
- all() {
10054
- return Array.from(this.macros.values());
10055
- }
10056
- remove(name) {
10057
- return this.macros.delete(name);
10058
- }
10059
- clear() {
10060
- this.macros.clear();
10061
- }
10062
- }
10063
- var requestMacroRegistry = new RequestMacroRegistry;
10064
-
10065
- class RequestWithMacros {
10066
- static macro(name, handler) {
10067
- requestMacroRegistry.register(name, handler);
10068
- }
10069
- static applyMacros(request2) {
10070
- const macros = requestMacroRegistry.all();
10071
- macros.forEach((macro) => {
10072
- request2[macro.name] = macro.handler.bind(request2);
10073
- });
10074
- return request2;
10075
- }
10076
- static hasMacro(name) {
10077
- return requestMacroRegistry.has(name);
10078
- }
10079
- }
10080
- var BuiltInRequestMacros = {
10081
- wantsJson() {
10082
- const accept = this.headers.get("accept") || "";
10083
- return accept.includes("application/json") || accept.includes("json");
10084
- },
10085
- wantsHtml() {
10086
- const accept = this.headers.get("accept") || "";
10087
- return accept.includes("text/html") || accept.includes("html");
10088
- },
10089
- wantsXml() {
10090
- const accept = this.headers.get("accept") || "";
10091
- return accept.includes("application/xml") || accept.includes("xml");
10092
- },
10093
- expectsJson() {
10094
- return this.wantsJson() || this.isAjax() || this.isPjax();
10095
- },
10096
- isAjax() {
10097
- return this.headers.get("x-requested-with") === "XMLHttpRequest";
10098
- },
10099
- isPjax() {
10100
- return this.headers.get("x-pjax") === "true";
10101
- },
10102
- isMobile() {
10103
- const userAgent = this.headers.get("user-agent") || "";
10104
- const mobileRegex = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
10105
- return mobileRegex.test(userAgent);
10106
- },
10107
- isBot() {
10108
- const userAgent = this.headers.get("user-agent") || "";
10109
- const botRegex = /bot|crawler|spider|crawling/i;
10110
- return botRegex.test(userAgent);
10111
- },
10112
- isSecure() {
10113
- return this.url.startsWith("https://") || this.headers.get("x-forwarded-proto") === "https" || this.headers.get("x-forwarded-ssl") === "on";
10114
- },
10115
- ip() {
10116
- return this.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || this.headers.get("x-real-ip") || this.headers.get("cf-connecting-ip") || this.headers.get("x-client-ip") || "unknown";
10117
- },
10118
- ips() {
10119
- const forwardedFor = this.headers.get("x-forwarded-for");
10120
- if (forwardedFor) {
10121
- return forwardedFor.split(",").map((ip) => ip.trim());
10122
- }
10123
- return [BuiltInRequestMacros.ip.call(this)];
10124
- },
10125
- userAgent() {
10126
- return this.headers.get("user-agent") || "";
10127
- },
10128
- referer() {
10129
- return this.headers.get("referer") || this.headers.get("referrer") || null;
10130
- },
10131
- bearerToken() {
10132
- const auth2 = this.headers.get("authorization");
10133
- if (auth2 && auth2.startsWith("Bearer ")) {
10134
- return auth2.substring(7);
10135
- }
10136
- return null;
10137
- },
10138
- basicAuth() {
10139
- const auth2 = this.headers.get("authorization");
10140
- if (auth2 && auth2.startsWith("Basic ")) {
10141
- try {
10142
- const decoded = atob(auth2.substring(6));
10143
- const [username, password] = decoded.split(":");
10144
- return { username, password };
10145
- } catch {
10146
- return null;
10147
- }
10148
- }
10149
- return null;
10150
- },
10151
- hasHeader(name) {
10152
- return this.headers.has(name.toLowerCase());
10153
- },
10154
- header(name, defaultValue) {
10155
- return this.headers.get(name.toLowerCase()) || defaultValue || null;
10156
- },
10157
- allHeaders() {
10158
- const headers = {};
10159
- this.headers.forEach((value, key) => {
10160
- headers[key] = value;
10161
- });
10162
- return headers;
10163
- },
10164
- input(key, defaultValue) {
10165
- if (this.validated && key in this.validated) {
10166
- return this.validated[key];
10167
- }
10168
- if (this.query && key in this.query) {
10169
- return this.query[key];
10170
- }
10171
- if (this.params && key in this.params) {
10172
- return this.params[key];
10173
- }
10174
- if (this.jsonBody && typeof this.jsonBody === "object" && key in this.jsonBody) {
10175
- return this.jsonBody[key];
10176
- }
10177
- if (this.formBody && typeof this.formBody === "object" && key in this.formBody) {
10178
- return this.formBody[key];
10179
- }
10180
- return defaultValue;
10181
- },
10182
- all() {
10183
- return {
10184
- ...this.params,
10185
- ...this.query,
10186
- ...typeof this.jsonBody === "object" && this.jsonBody !== null ? this.jsonBody : {},
10187
- ...typeof this.formBody === "object" && this.formBody !== null ? this.formBody : {},
10188
- ...typeof this.validated === "object" && this.validated !== null ? this.validated : {}
10189
- };
10190
- },
10191
- only(keys) {
10192
- const all = this.all();
10193
- const result = {};
10194
- keys.forEach((key) => {
10195
- if (key in all) {
10196
- result[key] = all[key];
10197
- }
10198
- });
10199
- return result;
10200
- },
10201
- except(keys) {
10202
- const all = this.all();
10203
- const result = {};
10204
- Object.keys(all).forEach((key) => {
10205
- if (!keys.includes(key)) {
10206
- result[key] = all[key];
10207
- }
10208
- });
10209
- return result;
10210
- },
10211
- has(key) {
10212
- const all = this.all();
10213
- return key in all && all[key] !== undefined && all[key] !== null && all[key] !== "";
10214
- },
10215
- hasAny(keys) {
10216
- return keys.some((key) => this.has(key));
10217
- },
10218
- missing(key) {
10219
- return !this.has(key);
10220
- },
10221
- filled(key) {
10222
- const value = this.input(key);
10223
- if (value === null || value === undefined)
10224
- return false;
10225
- if (typeof value === "string")
10226
- return value.trim().length > 0;
10227
- if (Array.isArray(value))
10228
- return value.length > 0;
10229
- if (typeof value === "object")
10230
- return Object.keys(value).length > 0;
10231
- return true;
10232
- },
10233
- getQuery(key, defaultValue) {
10234
- if (!key)
10235
- return this.query || {};
10236
- return this.query?.[key] || defaultValue;
10237
- },
10238
- param(key, defaultValue) {
10239
- return this.params?.[key] || defaultValue;
10240
- },
10241
- cookie(name, defaultValue) {
10242
- const cookies = this.headers.get("cookie");
10243
- if (!cookies)
10244
- return defaultValue || null;
10245
- const cookieArray = cookies.split(";");
10246
- for (const cookie of cookieArray) {
10247
- const [key, value] = cookie.trim().split("=");
10248
- if (key === name) {
10249
- return decodeURIComponent(value);
10250
- }
10251
- }
10252
- return defaultValue || null;
10253
- },
10254
- cookies() {
10255
- const cookies = {};
10256
- const cookieHeader = this.headers.get("cookie");
10257
- if (cookieHeader) {
10258
- cookieHeader.split(";").forEach((cookie) => {
10259
- const [key, value] = cookie.trim().split("=");
10260
- if (key && value) {
10261
- cookies[key] = decodeURIComponent(value);
10262
- }
10263
- });
10264
- }
10265
- return cookies;
10266
- },
10267
- file(name) {
10268
- return this.files?.find((f) => f.fieldName === name);
10269
- },
10270
- hasFile(name) {
10271
- return !!this.files?.find((f) => f.fieldName === name);
10272
- },
10273
- path() {
10274
- return new URL(this.url).pathname;
10275
- },
10276
- fullUrl() {
10277
- return this.url;
10278
- },
10279
- root() {
10280
- const url2 = new URL(this.url);
10281
- return `${url2.protocol}//${url2.host}`;
10282
- },
10283
- is(pattern) {
10284
- const path = this.path();
10285
- const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
10286
- const regex = new RegExp(`^${regexPattern}$`);
10287
- return regex.test(path);
10288
- },
10289
- route() {
10290
- return this.routeName || null;
10291
- },
10292
- fingerprint() {
10293
- const url2 = new URL(this.url);
10294
- const data = {
10295
- method: this.method,
10296
- path: url2.pathname,
10297
- query: url2.search,
10298
- ip: BuiltInRequestMacros.ip.call(this)
10299
- };
10300
- return btoa(JSON.stringify(data));
10301
- },
10302
- signature(secret) {
10303
- const data = `${this.method}${this.url}${BuiltInRequestMacros.ip.call(this)}${secret}`;
10304
- let hash = 0;
10305
- for (let i = 0;i < data.length; i++) {
10306
- const char = data.charCodeAt(i);
10307
- hash = (hash << 5) - hash + char;
10308
- hash = hash & hash;
10309
- }
10310
- return Math.abs(hash).toString(36);
10311
- },
10312
- merge(data) {
10313
- if (!this.validated) {
10314
- this.validated = {};
10315
- }
10316
- Object.assign(this.validated, data);
10317
- },
10318
- replace(data) {
10319
- this.validated = { ...data };
10320
- },
10321
- age() {
10322
- const startTime = this.startTime || Date.now();
10323
- return Date.now() - startTime;
10324
- },
10325
- isFromTrustedProxy(trustedProxies = []) {
10326
- const ip = BuiltInRequestMacros.ip.call(this);
10327
- return trustedProxies.includes(ip);
10328
- },
10329
- contentLength() {
10330
- const length = this.headers.get("content-length");
10331
- return length ? Number.parseInt(length, 10) : 0;
10332
- },
10333
- contentType() {
10334
- return this.headers.get("content-type");
10335
- },
10336
- isContentType(type) {
10337
- const contentType = this.contentType();
10338
- return contentType ? contentType.includes(type) : false;
10339
- }
10340
- };
10341
- function registerBuiltInRequestMacros() {
10342
- Object.entries(BuiltInRequestMacros).forEach(([name, handler]) => {
10343
- RequestWithMacros.macro(name, handler);
10344
- });
10345
- }
10346
- registerBuiltInRequestMacros();
10347
-
10348
10365
  // src/response/macros.ts
10349
10366
  import process4 from "process";
10350
10367
 
@@ -13815,6 +13832,7 @@ export {
13815
13832
  authMocks,
13816
13833
  authHelpers,
13817
13834
  auth,
13835
+ applyRequestEnhancements,
13818
13836
  apiKeyAuth,
13819
13837
  WebSocketTester,
13820
13838
  ValidationMiddlewareBuilder,