srvx 0.2.0 → 0.2.1

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.
@@ -1,17 +1,23 @@
1
- import { ServerOptions, Server } from '../types.mjs';
1
+ import { ServerOptions, Server, BunFetchandler } from '../types.mjs';
2
2
  import * as Bun from 'bun';
3
3
  import 'node:http';
4
4
  import 'node:net';
5
5
  import '@cloudflare/workers-types';
6
6
 
7
- type BunHandler = (request: Request, server?: Bun.Server) => Response | Promise<Response>;
7
+ declare const Response: {
8
+ new (body?: BodyInit | null, init?: ResponseInit): Response;
9
+ prototype: Response;
10
+ error(): Response;
11
+ json(data: any, init?: ResponseInit): Response;
12
+ redirect(url: string | URL, status?: number): Response;
13
+ };
8
14
  declare function serve(options: ServerOptions): BunServer;
9
- declare class BunServer implements Server<BunHandler> {
15
+ declare class BunServer implements Server<BunFetchandler> {
10
16
  readonly runtime = "bun";
11
17
  readonly options: ServerOptions;
12
18
  readonly bun: Server["bun"];
13
19
  readonly serveOptions: Bun.ServeOptions;
14
- readonly fetch: BunHandler;
20
+ readonly fetch: BunFetchandler;
15
21
  constructor(options: ServerOptions);
16
22
  serve(): Promise<Awaited<this>>;
17
23
  get url(): string | undefined;
@@ -19,4 +25,4 @@ declare class BunServer implements Server<BunHandler> {
19
25
  close(closeAll?: boolean): Promise<void | undefined>;
20
26
  }
21
27
 
22
- export { serve };
28
+ export { Response, serve };
@@ -1,6 +1,7 @@
1
1
  import { r as resolvePort } from '../shared/srvx.PbkQy9Ck.mjs';
2
2
  import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
3
3
 
4
+ const Response = globalThis.Response;
4
5
  function serve(options) {
5
6
  return new BunServer(options);
6
7
  }
@@ -48,4 +49,4 @@ class BunServer {
48
49
  }
49
50
  }
50
51
 
51
- export { serve };
52
+ export { Response, serve };
@@ -4,6 +4,13 @@ import 'node:http';
4
4
  import 'node:net';
5
5
  import 'bun';
6
6
 
7
+ declare const Response: {
8
+ new (body?: BodyInit | null, init?: ResponseInit): Response;
9
+ prototype: Response;
10
+ error(): Response;
11
+ json(data: any, init?: ResponseInit): Response;
12
+ redirect(url: string | URL, status?: number): Response;
13
+ };
7
14
  declare function serve(options: ServerOptions): Server<CF.ExportedHandlerFetchHandler>;
8
15
 
9
- export { serve };
16
+ export { Response, serve };
@@ -1,5 +1,6 @@
1
1
  import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
2
 
3
+ const Response = globalThis.Response;
3
4
  function serve(options) {
4
5
  return new CloudflareServer(options);
5
6
  }
@@ -41,4 +42,4 @@ class CloudflareServer {
41
42
  }
42
43
  }
43
44
 
44
- export { serve };
45
+ export { Response, serve };
@@ -1,18 +1,24 @@
1
- import { ServerOptions, Server } from '../types.mjs';
1
+ import { ServerOptions, Server, DenoFetchHandler } from '../types.mjs';
2
2
  import 'node:http';
3
3
  import 'node:net';
4
4
  import 'bun';
5
5
  import '@cloudflare/workers-types';
6
6
 
7
- type DenoHandler = (request: Request, info?: Deno.ServeHandlerInfo<Deno.NetAddr>) => Response | Promise<Response>;
7
+ declare const Response: {
8
+ new (body?: BodyInit | null, init?: ResponseInit): Response;
9
+ prototype: Response;
10
+ error(): Response;
11
+ json(data: any, init?: ResponseInit): Response;
12
+ redirect(url: string | URL, status?: number): Response;
13
+ };
8
14
  declare function serve(options: ServerOptions): DenoServer;
9
- declare class DenoServer implements Server<DenoHandler> {
15
+ declare class DenoServer implements Server<DenoFetchHandler> {
10
16
  #private;
11
17
  readonly runtime = "deno";
12
18
  readonly options: ServerOptions;
13
19
  readonly deno: Server["deno"];
14
20
  readonly serveOptions: Deno.ServeTcpOptions;
15
- readonly fetch: DenoHandler;
21
+ readonly fetch: DenoFetchHandler;
16
22
  constructor(options: ServerOptions);
17
23
  serve(): Promise<this>;
18
24
  get url(): string | undefined;
@@ -20,4 +26,4 @@ declare class DenoServer implements Server<DenoHandler> {
20
26
  close(): Promise<void | undefined>;
21
27
  }
22
28
 
23
- export { serve };
29
+ export { Response, serve };
@@ -1,6 +1,7 @@
1
1
  import { r as resolvePort, f as fmtURL } from '../shared/srvx.PbkQy9Ck.mjs';
2
2
  import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
3
3
 
4
+ const Response = globalThis.Response;
4
5
  function serve(options) {
5
6
  return new DenoServer(options);
6
7
  }
@@ -64,4 +65,4 @@ class DenoServer {
64
65
  }
65
66
  }
66
67
 
67
- export { serve };
68
+ export { Response, serve };
@@ -1,4 +1,4 @@
1
- import { ServerOptions, Server } from '../types.mjs';
1
+ import { ServerOptions, Server, FetchHandler, NodeHttpHandler } from '../types.mjs';
2
2
  import NodeHttp__default from 'node:http';
3
3
  import 'node:net';
4
4
  import 'bun';
@@ -7,8 +7,8 @@ import '@cloudflare/workers-types';
7
7
  type NodeFastResponse = InstanceType<typeof NodeFastResponse>;
8
8
  declare const NodeFastResponse: {
9
9
  new (body?: BodyInit | null, init?: ResponseInit): {
10
- "__#4112@#body"?: BodyInit | null;
11
- "__#4112@#init"?: ResponseInit;
10
+ "__#4200@#body"?: BodyInit | null;
11
+ "__#4200@#init"?: ResponseInit;
12
12
  /**
13
13
  * Prepare Node.js response object
14
14
  */
@@ -19,11 +19,11 @@ declare const NodeFastResponse: {
19
19
  body: string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | Buffer<ArrayBufferLike> | DataView<ArrayBufferLike> | null | undefined;
20
20
  };
21
21
  /** Lazy initialized response instance */
22
- "__#4112@#responseObj"?: Response;
22
+ "__#4200@#responseObj"?: Response;
23
23
  /** Lazy initialized headers instance */
24
- "__#4112@#headersObj"?: Headers;
24
+ "__#4200@#headersObj"?: Headers;
25
25
  clone(): Response;
26
- readonly "__#4112@#response": Response;
26
+ readonly "__#4200@#response": Response;
27
27
  readonly headers: Headers;
28
28
  readonly ok: boolean;
29
29
  readonly redirected: boolean;
@@ -31,7 +31,7 @@ declare const NodeFastResponse: {
31
31
  readonly statusText: string;
32
32
  readonly type: ResponseType;
33
33
  readonly url: string;
34
- "__#4112@#fastBody"<T extends object>(as: new (...args: any[]) => T): T | null | false;
34
+ "__#4200@#fastBody"<T extends object>(as: new (...args: any[]) => T): T | null | false;
35
35
  readonly body: ReadableStream<Uint8Array> | null;
36
36
  readonly bodyUsed: boolean;
37
37
  arrayBuffer(): Promise<ArrayBuffer>;
@@ -44,5 +44,6 @@ declare const NodeFastResponse: {
44
44
  };
45
45
 
46
46
  declare function serve(options: ServerOptions): Server;
47
+ declare function toNodeHandler(fetchHandler: FetchHandler): NodeHttpHandler;
47
48
 
48
- export { NodeFastResponse as Response, serve };
49
+ export { NodeFastResponse as Response, serve, toNodeHandler };
@@ -724,6 +724,14 @@ const NodeFastResponse = /* @__PURE__ */ (() => (
724
724
  function serve(options) {
725
725
  return new NodeServer(options);
726
726
  }
727
+ function toNodeHandler(fetchHandler) {
728
+ return (nodeReq, nodeRes) => {
729
+ const request = new NodeRequestProxy(nodeReq);
730
+ request.node = { req: nodeReq, res: nodeRes };
731
+ const res = fetchHandler(request);
732
+ return res instanceof Promise ? res.then((resolvedRes) => sendNodeResponse(nodeRes, resolvedRes)) : sendNodeResponse(nodeRes, res);
733
+ };
734
+ }
727
735
  class NodeServer {
728
736
  constructor(options) {
729
737
  this.runtime = "node";
@@ -779,4 +787,4 @@ class NodeServer {
779
787
  }
780
788
  }
781
789
 
782
- export { NodeFastResponse as Response, serve };
790
+ export { NodeFastResponse as Response, serve, toNodeHandler };
package/dist/types.d.mts CHANGED
@@ -166,5 +166,10 @@ interface ServerRequest extends Request {
166
166
  context: CF.ExecutionContext;
167
167
  };
168
168
  }
169
+ type FetchHandler = (request: Request) => Response | Promise<Response>;
170
+ type BunFetchandler = (request: Request, server?: Bun.Server) => Response | Promise<Response>;
171
+ type DenoFetchHandler = (request: Request, info?: Deno.ServeHandlerInfo<Deno.NetAddr>) => Response | Promise<Response>;
172
+ type NodeHttpHandler = (nodeReq: NodeHttp.IncomingMessage, nodeRes: NodeHttp.ServerResponse) => void | Promise<void>;
173
+ type CloudflareFetchHandler = CF.ExportedHandlerFetchHandler;
169
174
 
170
- export { type Server, type ServerHandler, type ServerOptions, type ServerPlugin, type ServerPluginInstance, type ServerRequest, serve };
175
+ export { type BunFetchandler, type CloudflareFetchHandler, type DenoFetchHandler, type FetchHandler, type NodeHttpHandler, type Server, type ServerHandler, type ServerOptions, type ServerPlugin, type ServerPluginInstance, type ServerRequest, serve };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "srvx",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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",