@ereo/server 0.1.6
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/README.md +76 -0
- package/dist/bun-server.d.ts +177 -0
- package/dist/bun-server.d.ts.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1080 -0
- package/dist/middleware.d.ts +98 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/static.d.ts +39 -0
- package/dist/static.d.ts.map +1 -0
- package/dist/streaming.d.ts +88 -0
- package/dist/streaming.d.ts.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @ereo/server
|
|
2
|
+
|
|
3
|
+
High-performance Bun HTTP server for the EreoJS framework. Includes streaming SSR, middleware, static file serving, and compression.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @ereo/server
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createServer, serve } from '@ereo/server';
|
|
15
|
+
import { createFileRouter } from '@ereo/router';
|
|
16
|
+
|
|
17
|
+
const router = createFileRouter({ routesDir: './src/routes' });
|
|
18
|
+
|
|
19
|
+
const server = createServer({
|
|
20
|
+
router,
|
|
21
|
+
port: 3000,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
serve(server);
|
|
25
|
+
// Server running at http://localhost:3000
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Key Features
|
|
29
|
+
|
|
30
|
+
- **High Performance** - Built on Bun's native HTTP server for maximum speed
|
|
31
|
+
- **Streaming SSR** - Stream React components with `renderToStream` and Suspense support
|
|
32
|
+
- **Middleware Stack** - Composable middleware with `createMiddlewareChain`
|
|
33
|
+
- **Static Files** - Efficient static file serving with `serveStatic` and MIME type detection
|
|
34
|
+
- **Built-in Middleware** - Logger, CORS, security headers, compression, and rate limiting
|
|
35
|
+
- **Shell Templates** - Customizable HTML shells for SSR with `createShell`
|
|
36
|
+
|
|
37
|
+
## Middleware Example
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { createMiddlewareChain, logger, cors, securityHeaders, compress } from '@ereo/server';
|
|
41
|
+
|
|
42
|
+
const middleware = createMiddlewareChain([
|
|
43
|
+
logger(),
|
|
44
|
+
cors({ origin: 'https://example.com' }),
|
|
45
|
+
securityHeaders(),
|
|
46
|
+
compress(),
|
|
47
|
+
]);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Streaming SSR
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { renderToStream, createShell } from '@ereo/server';
|
|
54
|
+
|
|
55
|
+
const shell = createShell({
|
|
56
|
+
head: '<title>My App</title>',
|
|
57
|
+
scripts: ['/client.js'],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const stream = await renderToStream(<App />, { shell });
|
|
61
|
+
return new Response(stream, {
|
|
62
|
+
headers: { 'Content-Type': 'text/html' },
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Documentation
|
|
67
|
+
|
|
68
|
+
For full documentation, visit [https://ereojs.dev/docs/server](https://ereojs.dev/docs/server)
|
|
69
|
+
|
|
70
|
+
## Part of EreoJS
|
|
71
|
+
|
|
72
|
+
This package is part of the [EreoJS](https://github.com/ereojs/ereo) monorepo - a modern full-stack framework built for Bun.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ereo/server - Bun HTTP Server
|
|
3
|
+
*
|
|
4
|
+
* High-performance HTTP server using Bun.serve().
|
|
5
|
+
* Designed for 5-6x faster performance than Node.js.
|
|
6
|
+
*/
|
|
7
|
+
import type { Server } from 'bun';
|
|
8
|
+
import type { MiddlewareHandler } from '@ereo/core';
|
|
9
|
+
import { EreoApp } from '@ereo/core';
|
|
10
|
+
import { FileRouter } from '@ereo/router';
|
|
11
|
+
import { cors, securityHeaders } from './middleware';
|
|
12
|
+
import { type StaticOptions } from './static';
|
|
13
|
+
import { type ShellTemplate } from './streaming';
|
|
14
|
+
/**
|
|
15
|
+
* Server render mode options.
|
|
16
|
+
*
|
|
17
|
+
* This type is distinct from the route-level RenderMode in @ereo/core.
|
|
18
|
+
* - ServerRenderMode: How the server renders React components ('streaming' vs 'string')
|
|
19
|
+
* - RenderMode (core): What type of rendering a route uses ('ssg', 'ssr', 'csr', etc.)
|
|
20
|
+
*/
|
|
21
|
+
export type ServerRenderMode = 'streaming' | 'string';
|
|
22
|
+
/**
|
|
23
|
+
* Server options.
|
|
24
|
+
*/
|
|
25
|
+
export interface ServerOptions {
|
|
26
|
+
/** Port to listen on */
|
|
27
|
+
port?: number;
|
|
28
|
+
/** Hostname to bind to */
|
|
29
|
+
hostname?: string;
|
|
30
|
+
/** Development mode */
|
|
31
|
+
development?: boolean;
|
|
32
|
+
/** Static file options */
|
|
33
|
+
static?: StaticOptions;
|
|
34
|
+
/** Enable logging */
|
|
35
|
+
logging?: boolean;
|
|
36
|
+
/** Enable CORS */
|
|
37
|
+
cors?: boolean | Parameters<typeof cors>[0];
|
|
38
|
+
/** Enable security headers */
|
|
39
|
+
security?: boolean | Parameters<typeof securityHeaders>[0];
|
|
40
|
+
/** Custom request handler */
|
|
41
|
+
handler?: (request: Request) => Response | Promise<Response>;
|
|
42
|
+
/** WebSocket handler */
|
|
43
|
+
websocket?: Parameters<typeof Bun.serve>[0]['websocket'];
|
|
44
|
+
/** TLS options */
|
|
45
|
+
tls?: {
|
|
46
|
+
cert: string;
|
|
47
|
+
key: string;
|
|
48
|
+
};
|
|
49
|
+
/** Render mode: 'streaming' for React 18 streaming SSR, 'string' for traditional SSR */
|
|
50
|
+
renderMode?: ServerRenderMode;
|
|
51
|
+
/** Base path for client assets */
|
|
52
|
+
assetsPath?: string;
|
|
53
|
+
/** Client entry script path */
|
|
54
|
+
clientEntry?: string;
|
|
55
|
+
/** Default shell template */
|
|
56
|
+
shell?: ShellTemplate;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Bun server instance.
|
|
60
|
+
*/
|
|
61
|
+
export declare class BunServer {
|
|
62
|
+
private server;
|
|
63
|
+
private app;
|
|
64
|
+
private router;
|
|
65
|
+
private middleware;
|
|
66
|
+
private staticHandler;
|
|
67
|
+
private options;
|
|
68
|
+
constructor(options?: ServerOptions);
|
|
69
|
+
/**
|
|
70
|
+
* Setup default middleware.
|
|
71
|
+
*/
|
|
72
|
+
private setupMiddleware;
|
|
73
|
+
/**
|
|
74
|
+
* Set the EreoJS app instance.
|
|
75
|
+
*/
|
|
76
|
+
setApp(app: EreoApp): void;
|
|
77
|
+
/**
|
|
78
|
+
* Set the file router.
|
|
79
|
+
*/
|
|
80
|
+
setRouter(router: FileRouter): void;
|
|
81
|
+
/**
|
|
82
|
+
* Add middleware.
|
|
83
|
+
*/
|
|
84
|
+
use(handler: MiddlewareHandler): void;
|
|
85
|
+
use(path: string, handler: MiddlewareHandler): void;
|
|
86
|
+
/**
|
|
87
|
+
* Handle incoming request.
|
|
88
|
+
*/
|
|
89
|
+
private handleRequest;
|
|
90
|
+
/**
|
|
91
|
+
* Handle a matched route.
|
|
92
|
+
*/
|
|
93
|
+
private handleRoute;
|
|
94
|
+
/**
|
|
95
|
+
* Render a full HTML page with the route component and layouts.
|
|
96
|
+
*/
|
|
97
|
+
private renderPage;
|
|
98
|
+
/**
|
|
99
|
+
* Render page using React 18 streaming SSR.
|
|
100
|
+
* Uses renderToReadableStream for Bun environments with native Web Streams API.
|
|
101
|
+
*/
|
|
102
|
+
private renderStreamingPage;
|
|
103
|
+
/**
|
|
104
|
+
* Render page using traditional string-based SSR.
|
|
105
|
+
*/
|
|
106
|
+
private renderStringPage;
|
|
107
|
+
/**
|
|
108
|
+
* Render page directly using streaming when layout provides HTML structure.
|
|
109
|
+
* The layout component is expected to render the full HTML document.
|
|
110
|
+
*/
|
|
111
|
+
private renderStreamingPageDirect;
|
|
112
|
+
/**
|
|
113
|
+
* Render page directly using string when layout provides HTML structure.
|
|
114
|
+
*/
|
|
115
|
+
private renderStringPageDirect;
|
|
116
|
+
/**
|
|
117
|
+
* Render a minimal HTML page when no component is available.
|
|
118
|
+
*/
|
|
119
|
+
private renderMinimalPage;
|
|
120
|
+
/**
|
|
121
|
+
* Render an error page.
|
|
122
|
+
*/
|
|
123
|
+
private renderErrorPage;
|
|
124
|
+
/**
|
|
125
|
+
* Build meta descriptors from route's meta function.
|
|
126
|
+
*/
|
|
127
|
+
private buildMeta;
|
|
128
|
+
/**
|
|
129
|
+
* Extract title from meta descriptors.
|
|
130
|
+
*/
|
|
131
|
+
private extractTitle;
|
|
132
|
+
/**
|
|
133
|
+
* Extract meta tags from meta descriptors (excluding title).
|
|
134
|
+
*/
|
|
135
|
+
private extractMetaTags;
|
|
136
|
+
/**
|
|
137
|
+
* Escape HTML special characters.
|
|
138
|
+
*/
|
|
139
|
+
private escapeHtml;
|
|
140
|
+
/**
|
|
141
|
+
* Handle errors.
|
|
142
|
+
*/
|
|
143
|
+
private handleError;
|
|
144
|
+
/**
|
|
145
|
+
* Start the server.
|
|
146
|
+
*/
|
|
147
|
+
start(): Promise<Server<unknown>>;
|
|
148
|
+
/**
|
|
149
|
+
* Stop the server.
|
|
150
|
+
*/
|
|
151
|
+
stop(): void;
|
|
152
|
+
/**
|
|
153
|
+
* Reload the server (for HMR).
|
|
154
|
+
*/
|
|
155
|
+
reload(): Promise<void>;
|
|
156
|
+
/**
|
|
157
|
+
* Get the server instance.
|
|
158
|
+
*/
|
|
159
|
+
getServer(): Server<unknown> | null;
|
|
160
|
+
/**
|
|
161
|
+
* Get server info.
|
|
162
|
+
*/
|
|
163
|
+
getInfo(): {
|
|
164
|
+
port: number;
|
|
165
|
+
hostname: string;
|
|
166
|
+
development: boolean;
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Create a Bun server.
|
|
171
|
+
*/
|
|
172
|
+
export declare function createServer(options?: ServerOptions): BunServer;
|
|
173
|
+
/**
|
|
174
|
+
* Quick start helper.
|
|
175
|
+
*/
|
|
176
|
+
export declare function serve(options?: ServerOptions): Promise<BunServer>;
|
|
177
|
+
//# sourceMappingURL=bun-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bun-server.d.ts","sourceRoot":"","sources":["../src/bun-server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAClC,OAAO,KAAK,EAAmE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACrH,OAAO,EAAiC,OAAO,EAAE,MAAM,YAAY,CAAC;AACpE,OAAO,EAAE,UAAU,EAAwD,MAAM,cAAc,CAAC;AAChG,OAAO,EAIL,IAAI,EACJ,eAAe,EAChB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAe,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AAC3D,OAAO,EAA+C,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAuD9F;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,QAAQ,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,wBAAwB;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,0BAA0B;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,uBAAuB;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,0BAA0B;IAC1B,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,qBAAqB;IACrB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,kBAAkB;IAClB,IAAI,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5C,8BAA8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3B;;OAEG;YACW,WAAW;IAuDzB;;OAEG;YACW,UAAU;IAmExB;;;OAGG;YACW,mBAAmB;IAiEjC;;OAEG;YACW,gBAAgB;IA8B9B;;;OAGG;YACW,yBAAyB;IA+DvC;;OAEG;YACW,sBAAsB;IAgCpC;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAgCzB;;OAEG;IACH,OAAO,CAAC,eAAe;IA+BvB;;OAEG;IACH,OAAO,CAAC,SAAS;IA0BjB;;OAEG;IACH,OAAO,CAAC,YAAY;IASpB;;OAEG;IACH,OAAO,CAAC,eAAe;IAcvB;;OAEG;IACH,OAAO,CAAC,UAAU;IASlB;;OAEG;IACH,OAAO,CAAC,WAAW;IAsBnB;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IA0BvC;;OAEG;IACH,IAAI,IAAI,IAAI;IAOZ;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ7B;;OAEG;IACH,SAAS,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI;IAInC;;OAEG;IACH,OAAO,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,OAAO,CAAA;KAAE;CAOpE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,SAAS,CAE/D;AAED;;GAEG;AACH,wBAAsB,KAAK,CAAC,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,SAAS,CAAC,CAIvE"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @ereo/server
|
|
3
|
+
*
|
|
4
|
+
* High-performance Bun HTTP server for the EreoJS framework.
|
|
5
|
+
*/
|
|
6
|
+
export { BunServer, createServer, serve, } from './bun-server';
|
|
7
|
+
export type { ServerOptions, ServerRenderMode } from './bun-server';
|
|
8
|
+
export { MiddlewareChain, createMiddlewareChain, logger, cors, securityHeaders, compress, rateLimit, } from './middleware';
|
|
9
|
+
export type { MiddlewareDefinition, CorsOptions, SecurityHeadersOptions, RateLimitOptions, } from './middleware';
|
|
10
|
+
export type { MiddlewareHandler, NextFunction, Middleware, AppContext, } from '@ereo/core';
|
|
11
|
+
export { serveStatic, staticMiddleware, getMimeType, } from './static';
|
|
12
|
+
export type { StaticOptions } from './static';
|
|
13
|
+
export { createShell, renderToStream, renderToString, createResponse, createSuspenseStream, } from './streaming';
|
|
14
|
+
export type { RenderOptions, ShellTemplate, RenderResult, } from './streaming';
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,SAAS,EACT,YAAY,EACZ,KAAK,GACN,MAAM,cAAc,CAAC;AAEtB,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGpE,OAAO,EACL,eAAe,EACf,qBAAqB,EACrB,MAAM,EACN,IAAI,EACJ,eAAe,EACf,QAAQ,EACR,SAAS,GACV,MAAM,cAAc,CAAC;AAEtB,YAAY,EACV,oBAAoB,EACpB,WAAW,EACX,sBAAsB,EACtB,gBAAgB,GACjB,MAAM,cAAc,CAAC;AAItB,YAAY,EACV,iBAAiB,EACjB,YAAY,EACZ,UAAU,EACV,UAAU,GACX,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,WAAW,GACZ,MAAM,UAAU,CAAC;AAElB,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAG9C,OAAO,EACL,WAAW,EACX,cAAc,EACd,cAAc,EACd,cAAc,EACd,oBAAoB,GACrB,MAAM,aAAa,CAAC;AAErB,YAAY,EACV,aAAa,EACb,aAAa,EACb,YAAY,GACb,MAAM,aAAa,CAAC"}
|