srvx 0.5.2 → 0.6.0

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,12 +1,12 @@
1
1
  import { ServerOptions, Server, BunFetchHandler } from '../types.mjs';
2
2
  import * as Bun from 'bun';
3
- export { F as URL } from '../shared/srvx.DEE2RO4O.mjs';
3
+ export { F as FastURL } from '../shared/srvx.DEE2RO4O.mjs';
4
4
  import 'node:http';
5
5
  import 'node:https';
6
6
  import 'node:net';
7
7
  import '@cloudflare/workers-types';
8
8
 
9
- declare const Response: typeof globalThis.Response;
9
+ declare const FastResponse: typeof globalThis.Response;
10
10
  declare function serve(options: ServerOptions): BunServer;
11
11
  declare class BunServer implements Server<BunFetchHandler> {
12
12
  readonly runtime = "bun";
@@ -21,4 +21,4 @@ declare class BunServer implements Server<BunFetchHandler> {
21
21
  close(closeAll?: boolean): Promise<void>;
22
22
  }
23
23
 
24
- export { Response, serve };
24
+ export { FastResponse, serve };
@@ -1,9 +1,10 @@
1
1
  import { r as resolveTLSOptions, a as resolvePortAndHost, p as printListening, f as fmtURL } from '../shared/srvx.Ctaz0clH.mjs';
2
- export { F as URL } from '../shared/srvx.Ctaz0clH.mjs';
3
- import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
+ export { F as FastURL } from '../shared/srvx.Ctaz0clH.mjs';
3
+ import { w as wrapFetch } from '../shared/srvx.zEohKxBQ.mjs';
4
+ import { w as wsUpgradePlugin } from '../shared/srvx.BMykKwGg.mjs';
4
5
  import 'node:fs';
5
6
 
6
- const Response = globalThis.Response;
7
+ const FastResponse = Response;
7
8
  function serve(options) {
8
9
  return new BunServer(options);
9
10
  }
@@ -12,7 +13,7 @@ class BunServer {
12
13
  this.runtime = "bun";
13
14
  this.bun = {};
14
15
  this.options = options;
15
- const fetchHandler = wrapFetch(this, this.options.fetch);
16
+ const fetchHandler = wrapFetch(this, [wsUpgradePlugin]);
16
17
  this.fetch = (request, server) => {
17
18
  Object.defineProperties(request, {
18
19
  runtime: {
@@ -32,7 +33,7 @@ class BunServer {
32
33
  this.serveOptions = {
33
34
  ...resolvePortAndHost(this.options),
34
35
  reusePort: this.options.reusePort,
35
- error: this.options.onError,
36
+ error: this.options.error,
36
37
  ...this.options.bun,
37
38
  tls: {
38
39
  cert: tls?.cert,
@@ -76,4 +77,4 @@ class BunServer {
76
77
  }
77
78
  }
78
79
 
79
- export { Response, serve };
80
+ export { FastResponse, serve };
@@ -5,8 +5,8 @@ import 'node:https';
5
5
  import 'node:net';
6
6
  import 'bun';
7
7
 
8
- declare const URL: typeof globalThis.URL;
9
- declare const Response: typeof globalThis.Response;
8
+ declare const FastURL: typeof globalThis.URL;
9
+ declare const FastResponse: typeof globalThis.Response;
10
10
  declare function serve(options: ServerOptions): Server<CF.ExportedHandlerFetchHandler>;
11
11
 
12
- export { Response, URL, serve };
12
+ export { FastResponse, FastURL, serve };
@@ -1,8 +1,9 @@
1
- import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
- import { w as wrapFetchOnError } from '../shared/srvx.BD9sRkkl.mjs';
1
+ import { w as wrapFetch } from '../shared/srvx.zEohKxBQ.mjs';
2
+ import { e as errorPlugin } from '../shared/srvx.CEIXM-sv.mjs';
3
+ import { w as wsUpgradePlugin } from '../shared/srvx.BMykKwGg.mjs';
3
4
 
4
- const URL = globalThis.URL;
5
- const Response = globalThis.Response;
5
+ const FastURL = URL;
6
+ const FastResponse = Response;
6
7
  function serve(options) {
7
8
  return new CloudflareServer(options);
8
9
  }
@@ -10,10 +11,10 @@ class CloudflareServer {
10
11
  constructor(options) {
11
12
  this.runtime = "cloudflare";
12
13
  this.options = options;
13
- const fetchHandler = wrapFetch(
14
- this,
15
- wrapFetchOnError(this.options.fetch, this.options.onError)
16
- );
14
+ const fetchHandler = wrapFetch(this, [
15
+ errorPlugin,
16
+ wsUpgradePlugin
17
+ ]);
17
18
  this.fetch = (request, env, context) => {
18
19
  Object.defineProperties(request, {
19
20
  runtime: {
@@ -50,4 +51,4 @@ class CloudflareServer {
50
51
  }
51
52
  }
52
53
 
53
- export { Response, URL, serve };
54
+ export { FastResponse, FastURL, serve };
@@ -1,12 +1,12 @@
1
1
  import { ServerOptions, Server, DenoFetchHandler } from '../types.mjs';
2
- export { F as URL } from '../shared/srvx.DEE2RO4O.mjs';
2
+ export { F as FastURL } from '../shared/srvx.DEE2RO4O.mjs';
3
3
  import 'node:http';
4
4
  import 'node:https';
5
5
  import 'node:net';
6
6
  import 'bun';
7
7
  import '@cloudflare/workers-types';
8
8
 
9
- declare const Response: typeof globalThis.Response;
9
+ declare const FastResponse: typeof globalThis.Response;
10
10
  declare function serve(options: ServerOptions): DenoServer;
11
11
  declare class DenoServer implements Server<DenoFetchHandler> {
12
12
  #private;
@@ -22,4 +22,4 @@ declare class DenoServer implements Server<DenoFetchHandler> {
22
22
  close(): Promise<void | undefined>;
23
23
  }
24
24
 
25
- export { Response, serve };
25
+ export { FastResponse, serve };
@@ -1,9 +1,10 @@
1
1
  import { r as resolveTLSOptions, a as resolvePortAndHost, p as printListening, f as fmtURL } from '../shared/srvx.Ctaz0clH.mjs';
2
- export { F as URL } from '../shared/srvx.Ctaz0clH.mjs';
3
- import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
+ export { F as FastURL } from '../shared/srvx.Ctaz0clH.mjs';
3
+ import { w as wrapFetch } from '../shared/srvx.zEohKxBQ.mjs';
4
+ import { w as wsUpgradePlugin } from '../shared/srvx.BMykKwGg.mjs';
4
5
  import 'node:fs';
5
6
 
6
- const Response = globalThis.Response;
7
+ const FastResponse = Response;
7
8
  function serve(options) {
8
9
  return new DenoServer(options);
9
10
  }
@@ -12,7 +13,7 @@ class DenoServer {
12
13
  this.runtime = "deno";
13
14
  this.deno = {};
14
15
  this.options = options;
15
- const fetchHandler = wrapFetch(this, this.options.fetch);
16
+ const fetchHandler = wrapFetch(this, [wsUpgradePlugin]);
16
17
  this.fetch = (request, info) => {
17
18
  Object.defineProperties(request, {
18
19
  runtime: {
@@ -32,7 +33,7 @@ class DenoServer {
32
33
  this.serveOptions = {
33
34
  ...resolvePortAndHost(this.options),
34
35
  reusePort: this.options.reusePort,
35
- onError: this.options.onError,
36
+ onError: this.options.error,
36
37
  ...tls ? { key: tls.key, cert: tls.cert, passphrase: tls.passphrase } : {},
37
38
  ...this.options.deno
38
39
  };
@@ -79,4 +80,4 @@ class DenoServer {
79
80
  }
80
81
  }
81
82
 
82
- export { Response, serve };
83
+ export { FastResponse, serve };
@@ -5,8 +5,8 @@ import 'node:net';
5
5
  import 'bun';
6
6
  import '@cloudflare/workers-types';
7
7
 
8
- declare const URL: typeof globalThis.URL;
9
- declare const Response: typeof globalThis.Response;
8
+ declare const FastURL: typeof globalThis.URL;
9
+ declare const FastResponse: typeof globalThis.Response;
10
10
  declare function serve(options: ServerOptions): Server;
11
11
 
12
- export { Response, URL, serve };
12
+ export { FastResponse, FastURL, serve };
@@ -1,8 +1,8 @@
1
- import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
- import { w as wrapFetchOnError } from '../shared/srvx.BD9sRkkl.mjs';
1
+ import { w as wrapFetch } from '../shared/srvx.zEohKxBQ.mjs';
2
+ import { e as errorPlugin } from '../shared/srvx.CEIXM-sv.mjs';
3
3
 
4
- const URL = globalThis.URL;
5
- const Response = globalThis.Response;
4
+ const FastURL = URL;
5
+ const FastResponse = Response;
6
6
  function serve(options) {
7
7
  return new GenericServer(options);
8
8
  }
@@ -10,10 +10,7 @@ class GenericServer {
10
10
  constructor(options) {
11
11
  this.runtime = "generic";
12
12
  this.options = options;
13
- const fetchHandler = wrapFetch(
14
- this,
15
- wrapFetchOnError(this.options.fetch, this.options.onError)
16
- );
13
+ const fetchHandler = wrapFetch(this, [errorPlugin]);
17
14
  this.fetch = (request) => {
18
15
  return Promise.resolve(fetchHandler(request));
19
16
  };
@@ -28,4 +25,4 @@ class GenericServer {
28
25
  }
29
26
  }
30
27
 
31
- export { Response, URL, serve };
28
+ export { FastResponse, FastURL, serve };
@@ -1,7 +1,7 @@
1
1
  import { ServerRequest, ServerOptions, Server, FetchHandler, NodeHttpHandler } from '../types.mjs';
2
- export { F as URL } from '../shared/srvx.DEE2RO4O.mjs';
2
+ export { F as FastURL } from '../shared/srvx.DEE2RO4O.mjs';
3
3
  import NodeHttp__default from 'node:http';
4
- import { Readable } from 'node:stream';
4
+ import NodeStream, { Readable } from 'node:stream';
5
5
  import 'node:https';
6
6
  import 'node:net';
7
7
  import 'bun';
@@ -24,11 +24,16 @@ declare const NodeResponse: {
24
24
  };
25
25
  };
26
26
 
27
+ type NodeRequestContext = {
28
+ req: NodeHttp__default.IncomingMessage;
29
+ res?: NodeHttp__default.ServerResponse;
30
+ upgrade?: {
31
+ socket: NodeStream.Duplex;
32
+ header: Buffer;
33
+ };
34
+ };
27
35
  declare const NodeRequest: {
28
- new (nodeCtx: {
29
- req: NodeHttp__default.IncomingMessage;
30
- res: NodeHttp__default.ServerResponse;
31
- }): ServerRequest;
36
+ new (nodeCtx: NodeRequestContext): ServerRequest;
32
37
  };
33
38
 
34
39
  declare const NodeRequestHeaders: {
@@ -47,4 +52,4 @@ declare const NodeResponseHeaders: {
47
52
  declare function serve(options: ServerOptions): Server;
48
53
  declare function toNodeHandler(fetchHandler: FetchHandler): NodeHttpHandler;
49
54
 
50
- export { NodeRequest, NodeRequestHeaders, NodeResponse, NodeResponseHeaders, NodeRequest as Request, NodeResponse as Response, serve, toNodeHandler };
55
+ export { NodeResponse as FastResponse, NodeRequest, NodeRequestHeaders, NodeResponse, NodeResponseHeaders, serve, toNodeHandler };
@@ -2,9 +2,9 @@ import NodeHttp from 'node:http';
2
2
  import NodeHttps from 'node:https';
3
3
  import { splitSetCookieString } from 'cookie-es';
4
4
  import { r as resolveTLSOptions, a as resolvePortAndHost, p as printListening, f as fmtURL } from '../shared/srvx.Ctaz0clH.mjs';
5
- export { F as URL } from '../shared/srvx.Ctaz0clH.mjs';
6
- import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
7
- import { w as wrapFetchOnError } from '../shared/srvx.BD9sRkkl.mjs';
5
+ export { F as FastURL } from '../shared/srvx.Ctaz0clH.mjs';
6
+ import { w as wrapFetch } from '../shared/srvx.zEohKxBQ.mjs';
7
+ import { e as errorPlugin } from '../shared/srvx.CEIXM-sv.mjs';
8
8
  import 'node:fs';
9
9
 
10
10
  async function sendNodeResponse(nodeRes, webRes) {
@@ -47,6 +47,23 @@ async function sendNodeResponse(nodeRes, webRes) {
47
47
  }
48
48
  return webRes.body ? streamBody(webRes.body, nodeRes) : endNodeResponse(nodeRes);
49
49
  }
50
+ async function sendNodeUpgradeResponse(socket, res) {
51
+ const head = [
52
+ `HTTP/1.1 ${res.status || 200} ${res.statusText || ""}`,
53
+ ...[...res.headers.entries()].map(
54
+ ([key, value]) => `${encodeURIComponent(key)}: ${encodeURIComponent(value)}`
55
+ )
56
+ ];
57
+ socket.write(head.join("\r\n") + "\r\n\r\n");
58
+ if (res.body) {
59
+ for await (const chunk of res.body) {
60
+ socket.write(chunk);
61
+ }
62
+ }
63
+ return new Promise((resolve) => {
64
+ socket.end(resolve);
65
+ });
66
+ }
50
67
  function endNodeResponse(nodeRes) {
51
68
  return new Promise((resolve) => nodeRes.end(resolve));
52
69
  }
@@ -689,7 +706,7 @@ const NodeResponse = /* @__PURE__ */ (() => {
689
706
  const headers = [];
690
707
  const headersInit = this.#init?.headers;
691
708
  if (headersInit) {
692
- const headerEntries = headersInit.entries ? headersInit.entries() : Object.entries(headersInit);
709
+ const headerEntries = Array.isArray(headersInit) ? headersInit : headersInit.entries ? headersInit.entries() : Object.entries(headersInit);
693
710
  for (const [key, value] of headerEntries) {
694
711
  if (key === "set-cookie") {
695
712
  for (const setCookie of splitSetCookieString(value)) {
@@ -700,6 +717,17 @@ const NodeResponse = /* @__PURE__ */ (() => {
700
717
  }
701
718
  }
702
719
  }
720
+ if (this.#headersObj) {
721
+ for (const [key, value] of this.#headersObj) {
722
+ if (key === "set-cookie") {
723
+ for (const setCookie of splitSetCookieString(value)) {
724
+ headers.push(["set-cookie", setCookie]);
725
+ }
726
+ } else {
727
+ headers.push([key, value]);
728
+ }
729
+ }
730
+ }
703
731
  const bodyInit = this.#body;
704
732
  let body;
705
733
  if (bodyInit) {
@@ -908,11 +936,7 @@ class NodeServer {
908
936
  constructor(options) {
909
937
  this.runtime = "node";
910
938
  this.options = options;
911
- const fetchHandler = wrapFetch(
912
- this,
913
- wrapFetchOnError(this.options.fetch, this.options.onError)
914
- );
915
- this.fetch = fetchHandler;
939
+ const fetchHandler = this.fetch = wrapFetch(this, [errorPlugin]);
916
940
  const handler = (nodeReq, nodeRes) => {
917
941
  const request = new NodeRequest({ req: nodeReq, res: nodeRes });
918
942
  const res = fetchHandler(request);
@@ -931,6 +955,19 @@ class NodeServer {
931
955
  this.serveOptions,
932
956
  handler
933
957
  ) : NodeHttp.createServer(this.serveOptions, handler);
958
+ const upgradeHandler = this.options.upgrade;
959
+ if (upgradeHandler) {
960
+ server.on("upgrade", (nodeReq, socket, header) => {
961
+ const request = new NodeRequest({
962
+ req: nodeReq,
963
+ upgrade: { socket, header }
964
+ });
965
+ const res = upgradeHandler(request);
966
+ return res instanceof Promise ? res.then(
967
+ (resolvedRes) => sendNodeUpgradeResponse(socket, resolvedRes)
968
+ ) : sendNodeUpgradeResponse(socket, res);
969
+ });
970
+ }
934
971
  this.node = { server, handler };
935
972
  if (!options.manual) {
936
973
  this.serve();
@@ -974,4 +1011,4 @@ class NodeServer {
974
1011
  }
975
1012
  }
976
1013
 
977
- export { NodeRequest, NodeRequestHeaders, NodeResponse, NodeResponseHeaders, NodeRequest as Request, NodeResponse as Response, serve, toNodeHandler };
1014
+ export { NodeResponse as FastResponse, NodeRequest, NodeRequestHeaders, NodeResponse, NodeResponseHeaders, serve, toNodeHandler };
@@ -5,10 +5,10 @@ import 'node:net';
5
5
  import 'bun';
6
6
  import '@cloudflare/workers-types';
7
7
 
8
- declare const URL: typeof globalThis.URL;
9
- declare const Response: typeof globalThis.Response;
8
+ declare const FastURL: typeof globalThis.URL;
9
+ declare const FastResponse: typeof globalThis.Response;
10
10
  type ServiceWorkerHandler = (request: ServerRequest, event: FetchEvent) => Response | Promise<Response>;
11
11
  declare function serve(options: ServerOptions): Server<ServiceWorkerHandler>;
12
12
 
13
- export { Response, URL, serve };
13
+ export { FastResponse, FastURL, serve };
14
14
  export type { ServiceWorkerHandler };
@@ -1,8 +1,8 @@
1
- import { w as wrapFetch } from '../shared/srvx.DhN4g5wJ.mjs';
2
- import { w as wrapFetchOnError } from '../shared/srvx.BD9sRkkl.mjs';
1
+ import { w as wrapFetch } from '../shared/srvx.zEohKxBQ.mjs';
2
+ import { e as errorPlugin } from '../shared/srvx.CEIXM-sv.mjs';
3
3
 
4
- const URL = globalThis.URL;
5
- const Response = globalThis.Response;
4
+ const FastURL = URL;
5
+ const FastResponse = Response;
6
6
  const isBrowserWindow = typeof window !== "undefined" && typeof navigator !== "undefined";
7
7
  const isServiceWorker = typeof self !== "undefined" && "skipWaiting" in self;
8
8
  function serve(options) {
@@ -12,10 +12,7 @@ class ServiceWorkerServer {
12
12
  constructor(options) {
13
13
  this.runtime = "service-worker";
14
14
  this.options = options;
15
- const fetchHandler = wrapFetch(
16
- this,
17
- wrapFetchOnError(this.options.fetch, this.options.onError)
18
- );
15
+ const fetchHandler = wrapFetch(this, [errorPlugin]);
19
16
  this.fetch = (request, event) => {
20
17
  Object.defineProperties(request, {
21
18
  runtime: {
@@ -95,4 +92,4 @@ class ServiceWorkerServer {
95
92
  }
96
93
  }
97
94
 
98
- export { Response, URL, serve };
95
+ export { FastResponse, FastURL, serve };
@@ -0,0 +1,16 @@
1
+ const wsUpgradePlugin = (server) => {
2
+ const upgradeHandler = server.options.upgrade;
3
+ if (!upgradeHandler) {
4
+ return {};
5
+ }
6
+ return {
7
+ fetch(request, next) {
8
+ if (request.headers.get("upgrade") === "websocket") {
9
+ return upgradeHandler(request);
10
+ }
11
+ return next();
12
+ }
13
+ };
14
+ };
15
+
16
+ export { wsUpgradePlugin as w };
@@ -0,0 +1,18 @@
1
+ const errorPlugin = (server) => {
2
+ const errorHandler = server.options.error;
3
+ if (!errorHandler) {
4
+ return {};
5
+ }
6
+ return {
7
+ fetch(_request, next) {
8
+ try {
9
+ const res = next();
10
+ return res instanceof Promise ? res.catch((error) => errorHandler(error)) : res;
11
+ } catch (error) {
12
+ return errorHandler(error);
13
+ }
14
+ }
15
+ };
16
+ };
17
+
18
+ export { errorPlugin as e };
@@ -0,0 +1,20 @@
1
+ function wrapFetch(server, basePlugins) {
2
+ const plugins = [
3
+ ...basePlugins || [],
4
+ ...server.options.plugins || []
5
+ ].map((plugin) => typeof plugin === "function" ? plugin(server) : plugin);
6
+ const middleware = plugins.filter((plugin) => plugin.fetch).map((plugin) => plugin.fetch);
7
+ const fetchHandler = server.options.fetch;
8
+ return middleware.length === 0 ? fetchHandler : (request) => callMiddleware(request, fetchHandler, middleware, 0);
9
+ }
10
+ function callMiddleware(request, fetchHandler, middleware, index) {
11
+ if (index === middleware.length) {
12
+ return fetchHandler(request);
13
+ }
14
+ return middleware[index](
15
+ request,
16
+ () => callMiddleware(request, fetchHandler, middleware, index + 1)
17
+ );
18
+ }
19
+
20
+ export { wrapFetch as w };
package/dist/types.d.mts CHANGED
@@ -5,8 +5,14 @@ import * as Bun from 'bun';
5
5
  import * as CF from '@cloudflare/workers-types';
6
6
 
7
7
  type MaybePromise<T> = T | Promise<T>;
8
- declare const Response: typeof globalThis.Response;
9
- declare const URL: typeof globalThis.URL;
8
+ /**
9
+ * Faster URL constructor with lazy access to pathname and search params (For Node, Deno, and Bun).
10
+ */
11
+ declare const FastURL: typeof globalThis.URL;
12
+ /**
13
+ * Faster Response constructor optimized for Node.js (same as Response for other runtimes).
14
+ */
15
+ declare const FastResponse: typeof globalThis.Response;
10
16
  /**
11
17
  * Create a new server instance.
12
18
  */
@@ -23,6 +29,16 @@ interface ServerOptions {
23
29
  * The fetch handler handles incoming requests.
24
30
  */
25
31
  fetch: ServerHandler;
32
+ /**
33
+ * Handle websocket upgrades.
34
+ */
35
+ upgrade?: ServerHandler;
36
+ /**
37
+ * Handle lifecycle errors.
38
+ *
39
+ * @note This handler will set built-in Bun and Deno error handler.
40
+ */
41
+ error?: ErrorHandler;
26
42
  /**
27
43
  * Server plugins.
28
44
  */
@@ -82,12 +98,6 @@ interface ServerOptions {
82
98
  */
83
99
  passphrase?: string;
84
100
  };
85
- /**
86
- * Runtime agnostic error handler (optional).
87
- *
88
- * @note This handler will take precedence over runtime specific error handlers.
89
- */
90
- onError?: ErrorHandler;
91
101
  /**
92
102
  * Node.js server options.
93
103
  */
@@ -170,21 +180,10 @@ interface Server<Handler = ServerHandler> {
170
180
  close(closeActiveConnections?: boolean): Promise<void>;
171
181
  }
172
182
  type ServerPlugin = (server: Server) => ServerPluginInstance;
183
+ type ServerMiddleware = (request: ServerRequest, next: () => Response | Promise<Response>) => Response | Promise<Response>;
173
184
  interface ServerPluginInstance {
174
- /**
175
- * Plugin display name
176
- */
177
185
  name?: string;
178
- /**
179
- * Hook to allow running logic before user fetch handler
180
- * If an response value is returned, user fetch handler and the next plugins will be skipped.
181
- */
182
- request?: (request: ServerRequest) => MaybePromise<Response | void>;
183
- /**
184
- * Hook to allow running logic after user fetch handler
185
- * If a response value is returned, user response and the next plugins will be skipped.
186
- */
187
- response?: (request: ServerRequest, response: Response) => MaybePromise<void | Response>;
186
+ fetch?: ServerMiddleware;
188
187
  }
189
188
  interface ServerRuntimeContext {
190
189
  name: "node" | "deno" | "bun" | "cloudflare" | (string & {});
@@ -232,5 +231,5 @@ type DenoFetchHandler = (request: Request, info?: Deno.ServeHandlerInfo<Deno.Net
232
231
  type NodeHttpHandler = (nodeReq: NodeHttp.IncomingMessage, nodeRes: NodeHttp.ServerResponse) => void | Promise<void>;
233
232
  type CloudflareFetchHandler = CF.ExportedHandlerFetchHandler;
234
233
 
235
- export { Response, URL, serve };
236
- export type { BunFetchHandler, CloudflareFetchHandler, DenoFetchHandler, ErrorHandler, FetchHandler, NodeHttpHandler, Server, ServerHandler, ServerOptions, ServerPlugin, ServerPluginInstance, ServerRequest, ServerRuntimeContext };
234
+ export { FastResponse, FastURL, serve };
235
+ export type { BunFetchHandler, CloudflareFetchHandler, DenoFetchHandler, ErrorHandler, FetchHandler, NodeHttpHandler, Server, ServerHandler, ServerMiddleware, ServerOptions, ServerPlugin, ServerPluginInstance, ServerRequest, ServerRuntimeContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "srvx",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "Universal Server API based on web platform standards. Works seamlessly with Deno, Bun and Node.js.",
5
5
  "homepage": "https://srvx.h3.dev",
6
6
  "repository": "h3js/srvx",
@@ -1,16 +0,0 @@
1
- function wrapFetchOnError(fetchHandler, onError) {
2
- if (!onError) return fetchHandler;
3
- return (...params) => {
4
- try {
5
- const result = fetchHandler(...params);
6
- if (result instanceof Promise) {
7
- return result.catch(onError);
8
- }
9
- return result;
10
- } catch (error) {
11
- return onError(error);
12
- }
13
- };
14
- }
15
-
16
- export { wrapFetchOnError as w };
@@ -1,76 +0,0 @@
1
- function wrapFetch(server, fetchHandler) {
2
- const plugins = server.options.plugins;
3
- if (!plugins?.length) {
4
- return fetchHandler;
5
- }
6
- const requestHooks = [];
7
- const responseHooks = [];
8
- for (const ctor of plugins) {
9
- const plugin = typeof ctor === "function" ? ctor(server) : ctor;
10
- if (plugin.request) {
11
- requestHooks.push(plugin.request);
12
- }
13
- if (plugin.response) {
14
- responseHooks.push(plugin.response);
15
- }
16
- }
17
- const hasRequestHooks = requestHooks.length > 0;
18
- const hasResponseHooks = responseHooks.length > 0;
19
- if (!hasRequestHooks && !hasResponseHooks) {
20
- return fetchHandler;
21
- }
22
- return (request) => {
23
- let resValue;
24
- let resPromise;
25
- if (hasRequestHooks) {
26
- for (const reqHook of requestHooks) {
27
- if (resPromise) {
28
- resPromise = resPromise.then((res) => res || reqHook(request));
29
- } else {
30
- const res = reqHook(request);
31
- if (res) {
32
- if (res instanceof Promise) {
33
- resPromise = res;
34
- } else {
35
- return res;
36
- }
37
- }
38
- }
39
- }
40
- }
41
- if (resPromise) {
42
- resPromise = resPromise.then((res) => res || fetchHandler(request));
43
- } else {
44
- const res = fetchHandler(request);
45
- if (res instanceof Promise) {
46
- resPromise = res;
47
- } else {
48
- resValue = res;
49
- }
50
- }
51
- if (hasResponseHooks) {
52
- for (const resHook of responseHooks) {
53
- if (resPromise) {
54
- resPromise = resPromise.then((res) => {
55
- if (res) {
56
- resValue = res;
57
- }
58
- return resHook(request, resValue);
59
- });
60
- } else {
61
- const res = resHook(request, resValue);
62
- if (res) {
63
- if (res instanceof Promise) {
64
- resPromise = res;
65
- } else {
66
- resValue = res;
67
- }
68
- }
69
- }
70
- }
71
- }
72
- return resPromise ? resPromise.then((res) => res || resValue) : resValue;
73
- };
74
- }
75
-
76
- export { wrapFetch as w };