srvx 0.11.3 → 0.11.4

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.
@@ -10,7 +10,7 @@ declare class BunServer implements Server<BunFetchHandler> {
10
10
  readonly runtime = "bun";
11
11
  readonly options: Server["options"];
12
12
  readonly bun: Server["bun"];
13
- readonly serveOptions: bun.Serve.Options<any>;
13
+ readonly serveOptions: bun.Serve.Options<any> | undefined;
14
14
  readonly fetch: BunFetchHandler;
15
15
  constructor(options: ServerOptions);
16
16
  serve(): Promise<this>;
@@ -20,10 +20,16 @@ var BunServer = class {
20
20
  for (const plugin of options.plugins || []) plugin(this);
21
21
  gracefulShutdownPlugin(this);
22
22
  const fetchHandler = wrapFetch(this);
23
+ const loader = globalThis.__srvxLoader__;
24
+ if (loader) {
25
+ this.fetch = fetchHandler;
26
+ loader(fetchHandler);
27
+ return;
28
+ }
23
29
  this.#wait = createWaitUntil();
24
30
  this.fetch = (request, server) => {
25
31
  Object.defineProperties(request, {
26
- waitUntil: { value: this.#wait.waitUntil },
32
+ waitUntil: { value: this.#wait?.waitUntil },
27
33
  runtime: {
28
34
  enumerable: true,
29
35
  value: {
@@ -72,7 +78,7 @@ var BunServer = class {
72
78
  return Promise.resolve(this);
73
79
  }
74
80
  async close(closeAll) {
75
- await Promise.all([this.#wait.wait(), Promise.resolve(this.bun?.server?.stop(closeAll))]);
81
+ await Promise.all([this.#wait?.wait(), Promise.resolve(this.bun?.server?.stop(closeAll))]);
76
82
  }
77
83
  };
78
84
  export { FastResponse, FastURL, serve };
@@ -9,7 +9,7 @@ declare class DenoServer implements Server<DenoFetchHandler> {
9
9
  readonly runtime = "deno";
10
10
  readonly options: Server["options"];
11
11
  readonly deno: Server["deno"];
12
- readonly serveOptions: Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem);
12
+ readonly serveOptions: Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem) | undefined;
13
13
  readonly fetch: DenoFetchHandler;
14
14
  constructor(options: ServerOptions);
15
15
  serve(): Promise<this>;
@@ -22,10 +22,16 @@ var DenoServer = class {
22
22
  for (const plugin of options.plugins || []) plugin(this);
23
23
  gracefulShutdownPlugin(this);
24
24
  const fetchHandler = wrapFetch(this);
25
+ const loader = globalThis.__srvxLoader__;
26
+ if (loader) {
27
+ this.fetch = fetchHandler;
28
+ loader(fetchHandler);
29
+ return;
30
+ }
25
31
  this.#wait = createWaitUntil();
26
32
  this.fetch = (request, info) => {
27
33
  Object.defineProperties(request, {
28
- waitUntil: { value: this.#wait.waitUntil },
34
+ waitUntil: { value: this.#wait?.waitUntil },
29
35
  runtime: {
30
36
  enumerable: true,
31
37
  value: {
@@ -81,7 +87,7 @@ var DenoServer = class {
81
87
  return Promise.resolve(this.#listeningPromise).then(() => this);
82
88
  }
83
89
  async close() {
84
- await Promise.all([this.#wait.wait(), Promise.resolve(this.deno?.server?.shutdown())]);
90
+ await Promise.all([this.#wait?.wait(), Promise.resolve(this.deno?.server?.shutdown())]);
85
91
  }
86
92
  };
87
93
  export { FastResponse, FastURL, serve };
@@ -670,15 +670,20 @@ var NodeServer = class {
670
670
  };
671
671
  for (const plugin of options.plugins || []) plugin(this);
672
672
  errorPlugin(this);
673
- gracefulShutdownPlugin(this);
674
673
  const fetchHandler = this.fetch = wrapFetch(this);
674
+ const loader = globalThis.__srvxLoader__;
675
+ if (loader) {
676
+ loader(fetchHandler);
677
+ return;
678
+ }
679
+ gracefulShutdownPlugin(this);
675
680
  this.#wait = createWaitUntil();
676
681
  const handler = (nodeReq, nodeRes) => {
677
682
  const request = new NodeRequest({
678
683
  req: nodeReq,
679
684
  res: nodeRes
680
685
  });
681
- request.waitUntil = this.#wait.waitUntil;
686
+ request.waitUntil = this.#wait?.waitUntil;
682
687
  const res = fetchHandler(request);
683
688
  return res instanceof Promise ? res.then((resolvedRes) => sendNodeResponse(nodeRes, resolvedRes)) : sendNodeResponse(nodeRes, res);
684
689
  };
@@ -728,7 +733,7 @@ var NodeServer = class {
728
733
  return Promise.resolve(this.#listeningPromise).then(() => this);
729
734
  }
730
735
  async close(closeAll) {
731
- await Promise.all([this.#wait.wait(), new Promise((resolve, reject) => {
736
+ await Promise.all([this.#wait?.wait(), new Promise((resolve, reject) => {
732
737
  const server = this.node?.server;
733
738
  if (server && closeAll && "closeAllConnections" in server) server.closeAllConnections();
734
739
  if (!server || !server.listening) return resolve();
package/dist/loader.mjs CHANGED
@@ -34,12 +34,14 @@ async function loadServerEntry(opts) {
34
34
  }
35
35
  const url = entry.startsWith("file://") ? entry : pathToFileURL(resolve(entry)).href;
36
36
  let mod;
37
- let listenHandler;
37
+ let interceptedNodeHandler;
38
+ let interceptedFetchHandler;
38
39
  try {
39
40
  if (opts.interceptHttpListen !== false) {
40
41
  const loaded = await interceptListen(() => import(url));
41
42
  mod = loaded.res;
42
- listenHandler = loaded.listenHandler;
43
+ interceptedNodeHandler = loaded.listenHandler;
44
+ interceptedFetchHandler = loaded.fetchHandler;
43
45
  } else mod = await import(url);
44
46
  } catch (error) {
45
47
  if (error?.code === "ERR_UNKNOWN_FILE_EXTENSION") {
@@ -50,11 +52,11 @@ async function loadServerEntry(opts) {
50
52
  throw error;
51
53
  }
52
54
  mod = await opts?.onLoad?.(mod) || mod;
53
- let fetchHandler = mod?.fetch || mod?.default?.fetch || mod?.default?.default?.fetch;
55
+ let fetchHandler = mod?.fetch || mod?.default?.fetch || mod?.default?.default?.fetch || interceptedFetchHandler;
54
56
  if (!fetchHandler && typeof mod?.default === "function" && mod.default.length < 2) fetchHandler = mod.default;
55
57
  let nodeCompat = false;
56
58
  if (!fetchHandler && opts.nodeCompat !== false) {
57
- const nodeHandler = listenHandler || (typeof mod?.default === "function" ? mod.default : void 0);
59
+ const nodeHandler = interceptedNodeHandler || (typeof mod?.default === "function" ? mod.default : void 0);
58
60
  if (nodeHandler) {
59
61
  nodeCompat = true;
60
62
  const { fetchNodeHandler } = await import("srvx/node");
@@ -74,6 +76,10 @@ async function interceptListen(cb) {
74
76
  const originalListen = nodeHTTP$1.Server.prototype.listen;
75
77
  let res;
76
78
  let listenHandler;
79
+ let fetchHandler;
80
+ globalThis.__srvxLoader__ = (handler) => {
81
+ fetchHandler = handler;
82
+ };
77
83
  try {
78
84
  nodeHTTP$1.Server.prototype.listen = function(arg1, arg2) {
79
85
  listenHandler = this._events.request;
@@ -96,10 +102,12 @@ async function interceptListen(cb) {
96
102
  res = await cb();
97
103
  } finally {
98
104
  nodeHTTP$1.Server.prototype.listen = originalListen;
105
+ delete globalThis.__srvxLoader__;
99
106
  }
100
107
  return {
101
108
  res,
102
- listenHandler
109
+ listenHandler,
110
+ fetchHandler
103
111
  };
104
112
  });
105
113
  _interceptQueue = result.catch(() => {});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "srvx",
3
- "version": "0.11.3",
3
+ "version": "0.11.4",
4
4
  "description": "Universal Server.",
5
5
  "homepage": "https://srvx.h3.dev",
6
6
  "license": "MIT",