@stacksjs/bun-router 0.1.15 → 0.1.17

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.
@@ -100,10 +100,23 @@ export declare function compressResponse(response: Response, request: Request, o
100
100
  * the prefix, which is what makes a small answer distinguishable from the
101
101
  * beginning of a large one.
102
102
  */
103
- export declare function peekBody(source: ReadableStream<Uint8Array>, limit: number): Promise<{
104
- stream: ReadableStream<Uint8Array>;
103
+ export interface PeekedBody {
104
+ /**
105
+ * What to send.
106
+ *
107
+ * Bytes when the body ended inside the prefix, a stream when it did not.
108
+ * The distinction is the whole point: a body that ended was never a stream,
109
+ * it was a small answer that arrived whole, and rebuilding it as a stream
110
+ * asks Bun for a controller, a start microtask and a drain per response.
111
+ * Measured on a route returning seventeen bytes of JSON, that machinery was
112
+ * roughly three times the cost of answering the request.
113
+ */
114
+ body: ReadableStream<Uint8Array> | Uint8Array;
105
115
  size: number;
106
116
  ended: boolean;
107
- }>;
117
+ }
118
+ /** A stream again, for the branch that has to pipe what it peeked. */
119
+ export declare function peekedStream(peeked: PeekedBody): ReadableStream<Uint8Array>;
120
+ export declare function peekBody(source: ReadableStream<Uint8Array>, limit: number): Promise<PeekedBody>;
108
121
  /** Add to `Vary` without dropping what is already there. */
109
122
  export declare function appendVary(headers: Headers, value: string): void;
package/dist/types.d.ts CHANGED
@@ -832,7 +832,18 @@ export interface UploadedFile {
832
832
  mimetype: string;
833
833
  buffer: ArrayBuffer;
834
834
  }
835
- export type RouteHandler = (_req: EnhancedRequest) => Response | Promise<Response>;
835
+ /**
836
+ * What a handler may return.
837
+ *
838
+ * The resolver has always wrapped a plain value - `wrapResponse` turns a
839
+ * string, a number, a boolean, bytes, a stream, or an object into a Response,
840
+ * and the handler-resolver documents that as a supported shape. The types said
841
+ * otherwise, so the documented idiom did not compile and every example that
842
+ * returns an object had to be cast. Widening this is safe for existing
843
+ * handlers: a `Response` is still a `RouteResult`.
844
+ */
845
+ export type RouteResult = Response | string | number | boolean | ArrayBuffer | Uint8Array | ReadableStream | object | null | undefined;
846
+ export type RouteHandler = (_req: EnhancedRequest) => RouteResult | Promise<RouteResult>;
836
847
  /**
837
848
  * Interface for handling route actions.
838
849
  * All action handlers must implement this interface.
@@ -1226,7 +1237,7 @@ export type RequestFor<TPath extends string> = Omit<EnhancedRequest, 'params'> &
1226
1237
  params: ExtractRouteParams<TPath>;
1227
1238
  };
1228
1239
  export interface TypedRouteHandler<TPath extends string> {
1229
- (req: RequestFor<TPath>): Response | Promise<Response>;
1240
+ (req: RequestFor<TPath>): RouteResult | Promise<RouteResult>;
1230
1241
  }
1231
1242
  /**
1232
1243
  * Strongly typed route definition for specific HTTP methods
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/bun-router",
3
3
  "type": "module",
4
- "version": "0.1.15",
4
+ "version": "0.1.17",
5
5
  "description": "A fast, type-safe router for Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.com>",
7
7
  "license": "MIT",