|
1 | | -import type { Context, MatchResult, Method, Middleware, MiddlewareRoute, Next, Route } from "./types"; |
| 1 | +import type { |
| 2 | + BunServerInstance, |
| 3 | + Context, |
| 4 | + DenoServerInstance, |
| 5 | + ListenOptions, |
| 6 | + MatchResult, |
| 7 | + Method, |
| 8 | + Middleware, |
| 9 | + MiddlewareRoute, |
| 10 | + Next, |
| 11 | + NodeServerInstance, |
| 12 | + Route, |
| 13 | + Server, |
| 14 | +} from "./types"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Runtime detection utilities for identifying the current JavaScript runtime environment. |
| 18 | + * @internal |
| 19 | + */ |
| 20 | +const Runtime = { |
| 21 | + /** True if running in Bun runtime */ |
| 22 | + isBun: typeof globalThis !== "undefined" && typeof (globalThis as any).Bun !== "undefined", |
| 23 | + /** True if running in Deno runtime */ |
| 24 | + isDeno: typeof globalThis !== "undefined" && typeof (globalThis as any).Deno !== "undefined", |
| 25 | + /** True if running in Node.js runtime */ |
| 26 | + isNode: |
| 27 | + typeof globalThis !== "undefined" && |
| 28 | + typeof (globalThis as any).process !== "undefined" && |
| 29 | + typeof (globalThis as any).Bun === "undefined" && |
| 30 | + typeof (globalThis as any).Deno === "undefined", |
| 31 | +}; |
2 | 32 |
|
3 | 33 | /** |
4 | 34 | * A node in the trie data structure used for efficient route matching. |
@@ -1711,7 +1741,206 @@ export class Web<T extends Record<string, unknown> = Record<string, unknown>> { |
1711 | 1741 | res.writeHead(500, { "Content-Type": "text/plain" }); |
1712 | 1742 | } |
1713 | 1743 | res.end("Internal Server Error"); |
1714 | | - console.error("Error in handleNode:", error); |
| 1744 | + } |
| 1745 | + } |
| 1746 | + |
| 1747 | + /** |
| 1748 | + * Starts a server with automatic runtime detection. |
| 1749 | + * Automatically selects the appropriate server implementation based on the runtime environment. |
| 1750 | + * |
| 1751 | + * @param {ListenOptions} options - Server configuration options |
| 1752 | + * @returns {Promise<Server>} Promise that resolves to a Server instance |
| 1753 | + * @throws {Error} If the runtime is not supported |
| 1754 | + * |
| 1755 | + * @example |
| 1756 | + * ```typescript |
| 1757 | + * // Basic usage with default options |
| 1758 | + * const server = await app.listen({ port: 3000 }); |
| 1759 | + * console.log(`Server running on ${server.runtime} at http://localhost:${server.port}`); |
| 1760 | + * |
| 1761 | + * // With startup callback |
| 1762 | + * await app.listen({ |
| 1763 | + * port: 8080, |
| 1764 | + * hostname: '0.0.0.0', |
| 1765 | + * onListen: ({ port, hostname, runtime }) => { |
| 1766 | + * console.log(`✅ ${runtime} server running at http://${hostname}:${port}`); |
| 1767 | + * } |
| 1768 | + * }); |
| 1769 | + * |
| 1770 | + * // With HTTPS in Node.js |
| 1771 | + * await app.listen({ |
| 1772 | + * port: 443, |
| 1773 | + * node: { |
| 1774 | + * https: true, |
| 1775 | + * key: fs.readFileSync('./key.pem'), |
| 1776 | + * cert: fs.readFileSync('./cert.pem') |
| 1777 | + * } |
| 1778 | + * }); |
| 1779 | + * |
| 1780 | + * // With TLS in Deno |
| 1781 | + * await app.listen({ |
| 1782 | + * port: 443, |
| 1783 | + * deno: { |
| 1784 | + * key: './key.pem', |
| 1785 | + * cert: './cert.pem' |
| 1786 | + * } |
| 1787 | + * }); |
| 1788 | + * |
| 1789 | + * // With TLS in Bun |
| 1790 | + * await app.listen({ |
| 1791 | + * port: 443, |
| 1792 | + * bun: { |
| 1793 | + * tls: { |
| 1794 | + * key: Bun.file('./key.pem'), |
| 1795 | + * cert: Bun.file('./cert.pem') |
| 1796 | + * } |
| 1797 | + * } |
| 1798 | + * }); |
| 1799 | + * |
| 1800 | + * // Gracefully stop the server |
| 1801 | + * await server.stop(); |
| 1802 | + * ``` |
| 1803 | + */ |
| 1804 | + async listen(options: ListenOptions = {}): Promise<Server> { |
| 1805 | + const { port = 3000, hostname = "localhost", onListen, node: nodeOptions = {}, deno: denoOptions = {}, bun: bunOptions = {} } = options; |
| 1806 | + |
| 1807 | + // Detect runtime and start appropriate server |
| 1808 | + if (Runtime.isBun) { |
| 1809 | + // Bun runtime |
| 1810 | + const bunGlobal = globalThis as any; |
| 1811 | + const serverConfig: Record<string, any> = { |
| 1812 | + port, |
| 1813 | + hostname, |
| 1814 | + fetch: this.handleBun.bind(this), |
| 1815 | + ...bunOptions, |
| 1816 | + }; |
| 1817 | + |
| 1818 | + const bunServer = bunGlobal.Bun.serve(serverConfig) as BunServerInstance; |
| 1819 | + |
| 1820 | + const server: Server = { |
| 1821 | + port: bunServer.port, |
| 1822 | + hostname: bunServer.hostname || hostname, |
| 1823 | + runtime: "bun", |
| 1824 | + instance: bunServer, |
| 1825 | + stop: async (): Promise<void> => { |
| 1826 | + bunServer.stop(); |
| 1827 | + }, |
| 1828 | + }; |
| 1829 | + |
| 1830 | + if (onListen) { |
| 1831 | + onListen({ |
| 1832 | + port: server.port, |
| 1833 | + hostname: server.hostname, |
| 1834 | + runtime: server.runtime, |
| 1835 | + }); |
| 1836 | + } |
| 1837 | + |
| 1838 | + return server; |
| 1839 | + } else if (Runtime.isDeno) { |
| 1840 | + // Deno runtime |
| 1841 | + const denoGlobal = globalThis as any; |
| 1842 | + const serverConfig: Record<string, any> = { |
| 1843 | + port, |
| 1844 | + hostname, |
| 1845 | + ...denoOptions, |
| 1846 | + }; |
| 1847 | + |
| 1848 | + const handler = (req: Request, info: unknown): Promise<Response> => { |
| 1849 | + return this.handleDeno(req, info); |
| 1850 | + }; |
| 1851 | + |
| 1852 | + const denoServer = denoGlobal.Deno.serve(serverConfig, handler) as DenoServerInstance; |
| 1853 | + |
| 1854 | + const server: Server = { |
| 1855 | + port, |
| 1856 | + hostname, |
| 1857 | + runtime: "deno", |
| 1858 | + instance: denoServer, |
| 1859 | + stop: async (): Promise<void> => { |
| 1860 | + await denoServer.shutdown(); |
| 1861 | + }, |
| 1862 | + }; |
| 1863 | + |
| 1864 | + if (onListen) { |
| 1865 | + onListen({ |
| 1866 | + port: server.port, |
| 1867 | + hostname: server.hostname, |
| 1868 | + runtime: server.runtime, |
| 1869 | + }); |
| 1870 | + } |
| 1871 | + |
| 1872 | + return server; |
| 1873 | + } else if (Runtime.isNode) { |
| 1874 | + // Node.js runtime |
| 1875 | + const module: "http" | "https" = nodeOptions.https ? "https" : "http"; |
| 1876 | + const { createServer } = await import(module); |
| 1877 | + |
| 1878 | + let nodeServer: NodeServerInstance; |
| 1879 | + |
| 1880 | + const requestHandler = (req: any, res: any): void => { |
| 1881 | + this.handleNode(req, res).catch((err: Error) => { |
| 1882 | + if (!res.headersSent) { |
| 1883 | + res.writeHead(500, { "Content-Type": "text/plain" }); |
| 1884 | + res.end("Internal Server Error"); |
| 1885 | + } |
| 1886 | + }); |
| 1887 | + }; |
| 1888 | + |
| 1889 | + if (nodeOptions.https) { |
| 1890 | + // HTTPS server |
| 1891 | + const httpsOptions: Record<string, any> = { |
| 1892 | + key: nodeOptions.key, |
| 1893 | + cert: nodeOptions.cert, |
| 1894 | + }; |
| 1895 | + nodeServer = (createServer as any)(httpsOptions, requestHandler) as NodeServerInstance; |
| 1896 | + } else { |
| 1897 | + // HTTP server |
| 1898 | + nodeServer = createServer(requestHandler) as NodeServerInstance; |
| 1899 | + } |
| 1900 | + |
| 1901 | + // Start listening |
| 1902 | + await new Promise<void>((resolve, reject) => { |
| 1903 | + const errorHandler = (err: Error): void => { |
| 1904 | + reject(err); |
| 1905 | + }; |
| 1906 | + |
| 1907 | + nodeServer.on("error", errorHandler); |
| 1908 | + |
| 1909 | + nodeServer.listen(port, hostname, () => { |
| 1910 | + nodeServer.off("error", errorHandler); |
| 1911 | + resolve(); |
| 1912 | + }); |
| 1913 | + }); |
| 1914 | + |
| 1915 | + const server: Server = { |
| 1916 | + port, |
| 1917 | + hostname, |
| 1918 | + runtime: "node", |
| 1919 | + instance: nodeServer, |
| 1920 | + stop: async (): Promise<void> => { |
| 1921 | + return new Promise<void>((resolve, reject) => { |
| 1922 | + nodeServer.close((err?: Error) => { |
| 1923 | + if (err) { |
| 1924 | + reject(err); |
| 1925 | + } else { |
| 1926 | + resolve(); |
| 1927 | + } |
| 1928 | + }); |
| 1929 | + }); |
| 1930 | + }, |
| 1931 | + }; |
| 1932 | + |
| 1933 | + if (onListen) { |
| 1934 | + onListen({ |
| 1935 | + port: server.port, |
| 1936 | + hostname: server.hostname, |
| 1937 | + runtime: server.runtime, |
| 1938 | + }); |
| 1939 | + } |
| 1940 | + |
| 1941 | + return server; |
| 1942 | + } else { |
| 1943 | + throw new Error(`Unsupported runtime. This framework supports Bun, Deno, and Node.js.`); |
1715 | 1944 | } |
1716 | 1945 | } |
1717 | 1946 | } |
|
0 commit comments