tezx 1.0.57 → 1.0.59

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/adapter/bun.d.ts CHANGED
@@ -1,4 +1,98 @@
1
1
  import { TezX } from "../core/server.js";
2
- export declare function bunAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>): {
3
- listen: (port: number, callback?: (message: string) => void) => any;
2
+ export declare function bunAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>, options?: {
3
+ /**
4
+ * If set, the HTTP server will listen on a unix socket instead of a port.
5
+ * (Cannot be used with hostname+port)
6
+ */
7
+ unix: string;
8
+ } | {
9
+ /**
10
+ * What is the maximum size of a request body? (in bytes)
11
+ * @default 1024 * 1024 * 128 // 128MB
12
+ */
13
+ maxRequestBodySize?: number;
14
+ /**
15
+ * Render contextual errors? This enables bun's error page
16
+ * @default process.env.NODE_ENV !== 'production'
17
+ */
18
+ development?: boolean | {
19
+ /**
20
+ * Enable Hot Module Replacement for routes (including React Fast Refresh, if React is in use)
21
+ *
22
+ * @default true if process.env.NODE_ENV !== 'production'
23
+ *
24
+ */
25
+ hmr?: boolean;
26
+ };
27
+ error?: (s: Bun.Serve, error: Error) => Response | Promise<Response> | void | Promise<void>;
28
+ /**
29
+ * Uniquely identify a server instance with an ID
30
+ *
31
+ * ### When bun is started with the `--hot` flag
32
+ *
33
+ * This string will be used to hot reload the server without interrupting
34
+ * pending requests or websockets. If not provided, a value will be
35
+ * generated. To disable hot reloading, set this value to `null`.
36
+ *
37
+ * ### When bun is not started with the `--hot` flag
38
+ *
39
+ * This string will currently do nothing. But in the future it could be useful for logs or metrics.
40
+ */
41
+ id?: string | null;
42
+ /**
43
+ * What port should the server listen on?
44
+ * @default process.env.PORT || "3000"
45
+ */
46
+ port?: string | number;
47
+ /**
48
+ * Whether the `SO_REUSEPORT` flag should be set.
49
+ *
50
+ * This allows multiple processes to bind to the same port, which is useful for load balancing.
51
+ *
52
+ * @default false
53
+ */
54
+ reusePort?: boolean;
55
+ /**
56
+ * Whether the `IPV6_V6ONLY` flag should be set.
57
+ * @default false
58
+ */
59
+ ipv6Only?: boolean;
60
+ /**
61
+ * What hostname should the server listen on?
62
+ *
63
+ * @default
64
+ * ```js
65
+ * "0.0.0.0" // listen on all interfaces
66
+ * ```
67
+ * @example
68
+ * ```js
69
+ * "127.0.0.1" // Only listen locally
70
+ * ```
71
+ * @example
72
+ * ```js
73
+ * "remix.run" // Only listen on remix.run
74
+ * ````
75
+ *
76
+ * note: hostname should not include a {@link port}
77
+ */
78
+ hostname?: string;
79
+ /**
80
+ * If set, the HTTP server will listen on a unix socket instead of a port.
81
+ * (Cannot be used with hostname+port)
82
+ */
83
+ unix?: never;
84
+ /**
85
+ * Sets the the number of seconds to wait before timing out a connection
86
+ * due to inactivity.
87
+ *
88
+ * Default is `10` seconds.
89
+ */
90
+ idleTimeout?: number;
91
+ tls?: Bun.TLSOptions | Bun.TLSOptions[];
92
+ }): {
93
+ listen: {
94
+ (callback?: (message: string) => void): any;
95
+ (port?: number): any;
96
+ (port?: number, callback?: (message: string) => void): any;
97
+ };
4
98
  };
package/adapter/bun.js CHANGED
@@ -1,72 +1,90 @@
1
1
  import { GlobalConfig } from "../core/config.js";
2
2
  import { Context } from "../core/context.js";
3
- export function bunAdapter(TezX) {
4
- function listen(port, callback) {
3
+ export function bunAdapter(TezX, options = {}) {
4
+ function listen(...arg) {
5
+ let port = typeof arg?.[0] === "number" ? arg?.[0] : undefined;
6
+ let callback = typeof arg[0] == "function" ? arg[0] : arg?.[1];
5
7
  const serve = typeof Bun !== "undefined" ? Bun.serve : null;
6
8
  try {
7
9
  if (!serve) {
8
10
  throw new Error("Bun is not find");
9
11
  }
10
12
  GlobalConfig.adapter = "bun";
11
- const server = serve({
12
- port: port,
13
- async fetch(req) {
14
- let options = {
15
- connInfo: {
16
- remoteAddr: server.requestIP(req),
17
- localAddr: server.address,
13
+ let server;
14
+ server =
15
+ options?.unix || typeof options?.unix == "string"
16
+ ? serve({
17
+ unix: options?.unix,
18
+ fetch: () => {
19
+ return new Response("4004");
20
+ },
21
+ })
22
+ : serve({
23
+ error: (error) => {
24
+ return options?.error?.(server, error);
25
+ },
26
+ development: options?.development,
27
+ hostname: options?.hostname,
28
+ id: options?.id,
29
+ idleTimeout: options?.idleTimeout,
30
+ ipv6Only: options?.ipv6Only,
31
+ maxRequestBodySize: options?.maxRequestBodySize,
32
+ reusePort: options?.reusePort,
33
+ tls: options?.tls,
34
+ port: options?.port || port,
35
+ async fetch(req) {
36
+ let options = {
37
+ connInfo: {
38
+ remoteAddr: server.requestIP(req),
39
+ localAddr: server.address,
40
+ },
41
+ };
42
+ const response = await TezX.serve(req, options);
43
+ if (typeof response?.websocket == "function" &&
44
+ response.ctx instanceof Context &&
45
+ response.ctx.wsProtocol) {
46
+ let websocket = response?.websocket(response?.ctx);
47
+ const upgradeSuccess = server.upgrade(req, {
48
+ data: { ...websocket },
49
+ });
50
+ if (upgradeSuccess)
51
+ return undefined;
52
+ }
53
+ if (response instanceof Response) {
54
+ return response;
55
+ }
56
+ return new Response(response.body ?? null, {
57
+ status: response.status ?? 200,
58
+ statusText: response.statusText || "",
59
+ headers: new Headers(response.headers ?? {}),
60
+ });
61
+ },
62
+ websocket: {
63
+ open(ws) {
64
+ return ws.data?.open?.(ws);
65
+ },
66
+ message(ws, msg) {
67
+ return ws.data?.message?.(ws, msg);
68
+ },
69
+ close(ws, code, reason) {
70
+ return ws.data?.close?.(ws, { code, reason });
71
+ },
72
+ ping(ws, data) {
73
+ return ws.data?.ping?.(ws, data);
74
+ },
75
+ pong(ws, data) {
76
+ return ws.data?.pong?.(ws, data);
77
+ },
78
+ drain(ws) {
79
+ return ws.data?.drain?.(ws);
80
+ },
18
81
  },
19
- };
20
- const response = await TezX.serve(req, options);
21
- if (typeof response?.websocket == "function" &&
22
- response.ctx instanceof Context &&
23
- response.ctx.wsProtocol) {
24
- let websocket = response?.websocket(response?.ctx);
25
- const upgradeSuccess = server.upgrade(req, {
26
- data: { ...websocket },
27
- });
28
- if (upgradeSuccess)
29
- return undefined;
30
- }
31
- if (response instanceof Response) {
32
- return response;
33
- }
34
- return new Response(response.body ?? null, {
35
- status: response.status ?? 200,
36
- statusText: response.statusText || "",
37
- headers: new Headers(response.headers ?? {}),
38
82
  });
39
- },
40
- websocket: {
41
- open(ws) {
42
- return ws.data?.open?.(ws);
43
- },
44
- message(ws, msg) {
45
- return ws.data?.message?.(ws, msg);
46
- },
47
- close(ws, code, reason) {
48
- return ws.data?.close?.(ws, { code, reason });
49
- },
50
- ping(ws, data) {
51
- return ws.data?.ping?.(ws, data);
52
- },
53
- pong(ws, data) {
54
- return ws.data?.pong?.(ws, data);
55
- },
56
- drain(ws) {
57
- return ws.data?.drain?.(ws);
58
- },
59
- },
60
- });
61
83
  GlobalConfig.server = server;
62
- const protocol = "\x1b[1;34mhttp\x1b[0m";
63
- const message = `\x1b[1m Bun TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
64
- if (typeof callback == "function") {
65
- callback(message);
66
- }
67
- else {
68
- GlobalConfig.debugging.success(message);
69
- }
84
+ const message = `\x1b[1m Bun TezX Server running at ${server.url}\x1b[0m`;
85
+ GlobalConfig.debugging.success(message);
86
+ if (typeof callback == "function")
87
+ callback();
70
88
  return server;
71
89
  }
72
90
  catch (err) {
package/adapter/deno.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import { TezX } from "../core/server.js";
2
- export declare function denoAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>): {
3
- listen: (port: number, callback?: (message: string) => void) => any;
2
+ export declare function denoAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>, options?: Deno.ServeUnixOptions | Deno.ServeTcpOptions | (Deno.ServeTcpOptions & Deno.TlsCertifiedKeyPem)): {
3
+ listen: {
4
+ (callback?: (message: string) => void): any;
5
+ (port?: number): any;
6
+ (port?: number, callback?: (message: string) => void): any;
7
+ };
4
8
  };
package/adapter/deno.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import { GlobalConfig } from "../core/config.js";
2
- export function denoAdapter(TezX) {
3
- function listen(port, callback) {
2
+ export function denoAdapter(TezX, options = {}) {
3
+ function listen(...arg) {
4
+ let port = typeof arg?.[0] === "number" ? arg?.[0] : undefined;
5
+ let callback = typeof arg[0] == "function" ? arg[0] : arg?.[1];
4
6
  const isDeno = typeof Deno !== "undefined";
5
7
  try {
6
8
  async function handleRequest(req, connInfo) {
7
9
  let remoteAddr = connInfo.remoteAddr;
8
- let localAddr = { ...server.addr };
10
+ let localAddr = { ...server?.addr };
9
11
  let address = {
10
12
  remoteAddr: {
11
13
  port: remoteAddr?.port,
@@ -33,20 +35,23 @@ export function denoAdapter(TezX) {
33
35
  headers: new Headers(response.headers ?? {}),
34
36
  });
35
37
  }
36
- const server = isDeno ? Deno.serve({ port }, handleRequest) : null;
38
+ const server = isDeno
39
+ ? options.transport === "unix"
40
+ ? Deno.serve(options, handleRequest)
41
+ : Deno.serve({ ...options, port }, handleRequest)
42
+ : null;
37
43
  if (!server) {
38
44
  throw new Error("Deno is not find");
39
45
  }
40
46
  GlobalConfig.adapter = "deno";
41
47
  GlobalConfig.server = server;
42
- const protocol = "\x1b[1;34mhttp\x1b[0m";
43
- const message = `\x1b[1m🚀 Deno TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
44
- if (typeof callback === "function") {
45
- callback(message);
46
- }
47
- else {
48
- GlobalConfig.debugging.success(message);
49
- }
48
+ const protocol = `\x1b[1;34m${options?.cert && options?.key ? "https" : "http"}\x1b[0m`;
49
+ const message = options?.transport !== "unix"
50
+ ? `\x1b[1m🚀 Deno TezX Server running at ${protocol}://${(server?.addr).hostname}:${server?.addr?.port}/\x1b[0m`
51
+ : `\x1b[1m🚀 Deno TezX Server running at ${server?.addr?.transport}://${server?.addr?.path}\x1b[0m`;
52
+ GlobalConfig.debugging.success(message);
53
+ if (typeof callback === "function")
54
+ callback();
50
55
  return server;
51
56
  }
52
57
  catch (err) {
package/adapter/node.d.ts CHANGED
@@ -1,4 +1,19 @@
1
+ import type { ServerOptions } from "node:http";
2
+ import type { TlsOptions } from "node:tls";
1
3
  import { TezX } from "../core/server.js";
2
- export declare function nodeAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>): {
3
- listen: (port: number, callback?: (message: string) => void) => void;
4
+ type UnixSocketOptions = ServerOptions & {
5
+ unix?: string;
6
+ enableSSL?: false;
4
7
  };
8
+ type SSLOptions = ServerOptions & TlsOptions & {
9
+ enableSSL: true;
10
+ };
11
+ type TezXServerOptions = UnixSocketOptions | SSLOptions;
12
+ export declare function nodeAdapter<T extends Record<string, any> = {}>(TezX: TezX<T>, options?: TezXServerOptions): {
13
+ listen: {
14
+ (callback?: () => void): any;
15
+ (port?: number): any;
16
+ (port?: number, callback?: () => void): any;
17
+ };
18
+ };
19
+ export {};
package/adapter/node.js CHANGED
@@ -1,11 +1,13 @@
1
+ import { Buffer } from "node:buffer";
1
2
  import { GlobalConfig } from "../core/config.js";
2
3
  import { Context } from "../core/context.js";
3
- export function nodeAdapter(TezX) {
4
- function listen(port, callback) {
5
- import("http")
4
+ export function nodeAdapter(TezX, options = {}) {
5
+ function listen(...arg) {
6
+ let ssl = options?.enableSSL;
7
+ import(ssl ? "node:https" : 'node:http')
6
8
  .then((r) => {
7
9
  GlobalConfig.adapter = "node";
8
- let server = r.createServer(async (req, res) => {
10
+ let server = r.createServer(options, async (req, res) => {
9
11
  let address = {};
10
12
  if (req.socket) {
11
13
  address = {
@@ -42,28 +44,32 @@ export function nodeAdapter(TezX) {
42
44
  res.statusMessage = statusText;
43
45
  }
44
46
  res.writeHead(response.status, headers);
45
- const { Readable } = await import("stream");
47
+ const { Readable } = await import("node:stream");
46
48
  if (response.body instanceof Readable) {
47
- response.body.pipe(res);
49
+ return response.body.pipe(res);
48
50
  }
49
51
  else {
50
- const body = await response.arrayBuffer();
51
- if (body.byteLength > 0) {
52
- return res.end(Buffer.from(body));
52
+ const buffer = await response.arrayBuffer();
53
+ if (buffer.byteLength > 0) {
54
+ return res.end(Buffer.from(buffer));
55
+ }
56
+ else {
57
+ return res.end();
53
58
  }
54
- res.end();
55
59
  }
56
60
  });
57
- server.listen(port, () => {
58
- const protocol = "\x1b[1;34mhttp\x1b[0m";
59
- const message = `\x1b[1m NodeJS TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
61
+ const port = typeof arg[0] === "number" ? arg[0] : undefined;
62
+ const callback = typeof arg[0] === "function" ? arg[0] : arg[1];
63
+ server.listen(options?.unix || port || 0, () => {
64
+ const protocol = ssl ? "\x1b[1;35mhttps\x1b[0m" : "\x1b[1;34mhttp\x1b[0m";
65
+ const address = server.address();
66
+ const message = typeof address === "string"
67
+ ? `\x1b[1mNodeJS TezX Server running at unix://${address}\x1b[0m`
68
+ : `\x1b[1mNodeJS TezX Server running at ${protocol}://localhost:${address?.port}/\x1b[0m`;
60
69
  GlobalConfig.server = server;
61
- if (typeof callback == "function") {
62
- callback(message);
63
- }
64
- else {
65
- GlobalConfig.debugging.success(message);
66
- }
70
+ GlobalConfig.debugging.success(message);
71
+ if (typeof callback == "function")
72
+ callback();
67
73
  return server;
68
74
  });
69
75
  })
@@ -3,73 +3,91 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.bunAdapter = bunAdapter;
4
4
  const config_js_1 = require("../core/config.js");
5
5
  const context_js_1 = require("../core/context.js");
6
- function bunAdapter(TezX) {
7
- function listen(port, callback) {
6
+ function bunAdapter(TezX, options = {}) {
7
+ function listen(...arg) {
8
+ let port = typeof arg?.[0] === "number" ? arg?.[0] : undefined;
9
+ let callback = typeof arg[0] == "function" ? arg[0] : arg?.[1];
8
10
  const serve = typeof Bun !== "undefined" ? Bun.serve : null;
9
11
  try {
10
12
  if (!serve) {
11
13
  throw new Error("Bun is not find");
12
14
  }
13
15
  config_js_1.GlobalConfig.adapter = "bun";
14
- const server = serve({
15
- port: port,
16
- async fetch(req) {
17
- let options = {
18
- connInfo: {
19
- remoteAddr: server.requestIP(req),
20
- localAddr: server.address,
16
+ let server;
17
+ server =
18
+ options?.unix || typeof options?.unix == "string"
19
+ ? serve({
20
+ unix: options?.unix,
21
+ fetch: () => {
22
+ return new Response("4004");
23
+ },
24
+ })
25
+ : serve({
26
+ error: (error) => {
27
+ return options?.error?.(server, error);
28
+ },
29
+ development: options?.development,
30
+ hostname: options?.hostname,
31
+ id: options?.id,
32
+ idleTimeout: options?.idleTimeout,
33
+ ipv6Only: options?.ipv6Only,
34
+ maxRequestBodySize: options?.maxRequestBodySize,
35
+ reusePort: options?.reusePort,
36
+ tls: options?.tls,
37
+ port: options?.port || port,
38
+ async fetch(req) {
39
+ let options = {
40
+ connInfo: {
41
+ remoteAddr: server.requestIP(req),
42
+ localAddr: server.address,
43
+ },
44
+ };
45
+ const response = await TezX.serve(req, options);
46
+ if (typeof response?.websocket == "function" &&
47
+ response.ctx instanceof context_js_1.Context &&
48
+ response.ctx.wsProtocol) {
49
+ let websocket = response?.websocket(response?.ctx);
50
+ const upgradeSuccess = server.upgrade(req, {
51
+ data: { ...websocket },
52
+ });
53
+ if (upgradeSuccess)
54
+ return undefined;
55
+ }
56
+ if (response instanceof Response) {
57
+ return response;
58
+ }
59
+ return new Response(response.body ?? null, {
60
+ status: response.status ?? 200,
61
+ statusText: response.statusText || "",
62
+ headers: new Headers(response.headers ?? {}),
63
+ });
64
+ },
65
+ websocket: {
66
+ open(ws) {
67
+ return ws.data?.open?.(ws);
68
+ },
69
+ message(ws, msg) {
70
+ return ws.data?.message?.(ws, msg);
71
+ },
72
+ close(ws, code, reason) {
73
+ return ws.data?.close?.(ws, { code, reason });
74
+ },
75
+ ping(ws, data) {
76
+ return ws.data?.ping?.(ws, data);
77
+ },
78
+ pong(ws, data) {
79
+ return ws.data?.pong?.(ws, data);
80
+ },
81
+ drain(ws) {
82
+ return ws.data?.drain?.(ws);
83
+ },
21
84
  },
22
- };
23
- const response = await TezX.serve(req, options);
24
- if (typeof response?.websocket == "function" &&
25
- response.ctx instanceof context_js_1.Context &&
26
- response.ctx.wsProtocol) {
27
- let websocket = response?.websocket(response?.ctx);
28
- const upgradeSuccess = server.upgrade(req, {
29
- data: { ...websocket },
30
- });
31
- if (upgradeSuccess)
32
- return undefined;
33
- }
34
- if (response instanceof Response) {
35
- return response;
36
- }
37
- return new Response(response.body ?? null, {
38
- status: response.status ?? 200,
39
- statusText: response.statusText || "",
40
- headers: new Headers(response.headers ?? {}),
41
85
  });
42
- },
43
- websocket: {
44
- open(ws) {
45
- return ws.data?.open?.(ws);
46
- },
47
- message(ws, msg) {
48
- return ws.data?.message?.(ws, msg);
49
- },
50
- close(ws, code, reason) {
51
- return ws.data?.close?.(ws, { code, reason });
52
- },
53
- ping(ws, data) {
54
- return ws.data?.ping?.(ws, data);
55
- },
56
- pong(ws, data) {
57
- return ws.data?.pong?.(ws, data);
58
- },
59
- drain(ws) {
60
- return ws.data?.drain?.(ws);
61
- },
62
- },
63
- });
64
86
  config_js_1.GlobalConfig.server = server;
65
- const protocol = "\x1b[1;34mhttp\x1b[0m";
66
- const message = `\x1b[1m Bun TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
67
- if (typeof callback == "function") {
68
- callback(message);
69
- }
70
- else {
71
- config_js_1.GlobalConfig.debugging.success(message);
72
- }
87
+ const message = `\x1b[1m Bun TezX Server running at ${server.url}\x1b[0m`;
88
+ config_js_1.GlobalConfig.debugging.success(message);
89
+ if (typeof callback == "function")
90
+ callback();
73
91
  return server;
74
92
  }
75
93
  catch (err) {
@@ -2,13 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.denoAdapter = denoAdapter;
4
4
  const config_js_1 = require("../core/config.js");
5
- function denoAdapter(TezX) {
6
- function listen(port, callback) {
5
+ function denoAdapter(TezX, options = {}) {
6
+ function listen(...arg) {
7
+ let port = typeof arg?.[0] === "number" ? arg?.[0] : undefined;
8
+ let callback = typeof arg[0] == "function" ? arg[0] : arg?.[1];
7
9
  const isDeno = typeof Deno !== "undefined";
8
10
  try {
9
11
  async function handleRequest(req, connInfo) {
10
12
  let remoteAddr = connInfo.remoteAddr;
11
- let localAddr = { ...server.addr };
13
+ let localAddr = { ...server?.addr };
12
14
  let address = {
13
15
  remoteAddr: {
14
16
  port: remoteAddr?.port,
@@ -36,20 +38,23 @@ function denoAdapter(TezX) {
36
38
  headers: new Headers(response.headers ?? {}),
37
39
  });
38
40
  }
39
- const server = isDeno ? Deno.serve({ port }, handleRequest) : null;
41
+ const server = isDeno
42
+ ? options.transport === "unix"
43
+ ? Deno.serve(options, handleRequest)
44
+ : Deno.serve({ ...options, port }, handleRequest)
45
+ : null;
40
46
  if (!server) {
41
47
  throw new Error("Deno is not find");
42
48
  }
43
49
  config_js_1.GlobalConfig.adapter = "deno";
44
50
  config_js_1.GlobalConfig.server = server;
45
- const protocol = "\x1b[1;34mhttp\x1b[0m";
46
- const message = `\x1b[1m🚀 Deno TezX Server running at ${protocol}://localhost:${port}/\x1b[0m`;
47
- if (typeof callback === "function") {
48
- callback(message);
49
- }
50
- else {
51
- config_js_1.GlobalConfig.debugging.success(message);
52
- }
51
+ const protocol = `\x1b[1;34m${options?.cert && options?.key ? "https" : "http"}\x1b[0m`;
52
+ const message = options?.transport !== "unix"
53
+ ? `\x1b[1m🚀 Deno TezX Server running at ${protocol}://${(server?.addr).hostname}:${server?.addr?.port}/\x1b[0m`
54
+ : `\x1b[1m🚀 Deno TezX Server running at ${server?.addr?.transport}://${server?.addr?.path}\x1b[0m`;
55
+ config_js_1.GlobalConfig.debugging.success(message);
56
+ if (typeof callback === "function")
57
+ callback();
53
58
  return server;
54
59
  }
55
60
  catch (err) {