@flow.os/server 0.0.1-dev.1771665310

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/core.ts ADDED
@@ -0,0 +1,140 @@
1
+ /// <reference types="node" />
2
+ import { readdirSync } from 'fs';
3
+ import { join } from 'path';
4
+ import type { FlowHandler, FlowRequest } from './index.js';
5
+
6
+ const METHOD_SUFFIXES = [
7
+ '.get',
8
+ '.post',
9
+ '.put',
10
+ '.delete',
11
+ '.patch',
12
+ '.head',
13
+ '.options',
14
+ ] as const;
15
+
16
+ export type RouteMeta = { path: string; method: string; filePath: string };
17
+
18
+ function fileSegmentToRoute(segment: string): string {
19
+ return segment.replace(/^\[([^\]]+)\]$/, ':$1');
20
+ }
21
+
22
+ function parseRoutePath(relativePath: string): { path: string; method: string } | null {
23
+ const lower = relativePath.toLowerCase();
24
+ for (const suffix of METHOD_SUFFIXES) {
25
+ const ext = lower.endsWith('.tsx') ? '.tsx' : '.ts';
26
+ if (lower.endsWith(suffix + ext)) {
27
+ const withoutExt = relativePath.slice(0, -(suffix.length + ext.length));
28
+ const segments = withoutExt.split(/[/\\]/).filter(Boolean);
29
+ const routeSegments = segments.map((s) => fileSegmentToRoute(s));
30
+ const path = '/' + routeSegments.join('/');
31
+ const pathNorm = path === '/index' ? '/' : path;
32
+ const method = suffix.slice(1).toUpperCase();
33
+ return { path: pathNorm, method };
34
+ }
35
+ }
36
+ return null;
37
+ }
38
+
39
+ export function walkRoutes(dir: string, base = ''): RouteMeta[] {
40
+ let entries: Array<{ name: string; isDirectory(): boolean; isFile(): boolean }>;
41
+ try {
42
+ entries = readdirSync(dir, { withFileTypes: true }) as Array<{
43
+ name: string;
44
+ isDirectory(): boolean;
45
+ isFile(): boolean;
46
+ }>;
47
+ } catch {
48
+ return [];
49
+ }
50
+ const routes: RouteMeta[] = [];
51
+ for (const e of entries) {
52
+ const name = e.name;
53
+ const rel = base ? `${base}/${name}` : name;
54
+ if (e.isDirectory()) {
55
+ routes.push(...walkRoutes(join(dir, name), rel));
56
+ } else if (e.isFile() && (name.endsWith('.ts') || name.endsWith('.tsx'))) {
57
+ const parsed = parseRoutePath(rel);
58
+ if (parsed) routes.push({ ...parsed, filePath: join(dir, name) });
59
+ }
60
+ }
61
+ return routes;
62
+ }
63
+
64
+ function routePatternToRegex(path: string): { re: RegExp; keys: string[] } {
65
+ const keys: string[] = [];
66
+ const pattern =
67
+ '^' +
68
+ path.replace(/:(\w+)/g, (_, key) => {
69
+ keys.push(key);
70
+ return '([^/]+)';
71
+ }) +
72
+ '$';
73
+ return { re: new RegExp(pattern), keys };
74
+ }
75
+
76
+ export type LoadHandler = (filePath: string) => Promise<{ default: FlowHandler }>;
77
+
78
+ type RouteEntry = {
79
+ method: string;
80
+ re: RegExp;
81
+ keys: string[];
82
+ handler: FlowHandler;
83
+ };
84
+
85
+ export async function createFlowServer(
86
+ routesDir: string,
87
+ loadHandler: LoadHandler
88
+ ): Promise<{
89
+ dispatch: (pathname: string, method: string, request: Request) => Promise<Response | null>;
90
+ matches: (pathname: string, method: string) => boolean;
91
+ }> {
92
+ const routeList = walkRoutes(routesDir);
93
+ const routes: RouteEntry[] = [];
94
+
95
+ for (const r of routeList) {
96
+ const mod = await loadHandler(r.filePath);
97
+ const handler = mod.default;
98
+ if (!handler) continue;
99
+ const { re, keys } = routePatternToRegex(r.path);
100
+ routes.push({ method: r.method, re, keys, handler });
101
+ }
102
+
103
+ function getMatch(
104
+ pathname: string,
105
+ method: string
106
+ ): { handler: FlowHandler; params: Record<string, string> } | null {
107
+ const norm = pathname.split('?')[0] || '/';
108
+ for (const route of routes) {
109
+ if (route.method !== method) continue;
110
+ const m = route.re.exec(norm);
111
+ if (!m) continue;
112
+ const params: Record<string, string> = {};
113
+ route.keys.forEach((k, i) => (params[k] = m[i + 1] ?? ''));
114
+ return { handler: route.handler, params };
115
+ }
116
+ return null;
117
+ }
118
+
119
+ async function dispatch(
120
+ pathname: string,
121
+ method: string,
122
+ request: Request
123
+ ): Promise<Response | null> {
124
+ const match = getMatch(pathname, method);
125
+ if (!match) return null;
126
+ const flowReq = request as FlowRequest;
127
+ flowReq.params = match.params;
128
+ const out = await match.handler(flowReq);
129
+ if (out instanceof Response) return out;
130
+ return new Response(JSON.stringify(out), {
131
+ headers: { 'Content-Type': 'application/json' },
132
+ });
133
+ }
134
+
135
+ function matches(pathname: string, method: string): boolean {
136
+ return getMatch(pathname, method) !== null;
137
+ }
138
+
139
+ return { dispatch, matches };
140
+ }
package/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * API native: Request/Response (Bun/Web). Zero dipendenze.
3
+ * Per route dinamiche usa request.params (iniettato dal router).
4
+ */
5
+
6
+ export { flowServer } from './plugin.js';
7
+ export type FlowRequest = Request & { params: Record<string, string> };
8
+
9
+ export type FlowHandler = (
10
+ request: FlowRequest
11
+ ) => Response | Promise<Response> | object | Promise<object>;
12
+
13
+ /**
14
+ * Wrapper per gli handler: ritorna oggetti come JSON, Response come sono.
15
+ */
16
+ export function defineHandler(fn: FlowHandler): FlowHandler {
17
+ return async (request: FlowRequest) => {
18
+ const out = await fn(request);
19
+ if (out instanceof Response) return out;
20
+ return new Response(JSON.stringify(out), {
21
+ headers: { 'Content-Type': 'application/json' },
22
+ });
23
+ };
24
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@flow.os/server",
3
+ "version": "0.0.1-dev.1771665310",
4
+ "type": "module",
5
+ "main": "./index.ts",
6
+ "types": "./index.ts",
7
+ "bin": "./start.ts",
8
+ "dependencies": {},
9
+ "peerDependencies": {
10
+ "vite": ">=5.0.0"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./index.ts",
15
+ "import": "./index.ts",
16
+ "default": "./index.ts"
17
+ },
18
+ "./vite": {
19
+ "types": "./plugin.ts",
20
+ "import": "./plugin.ts",
21
+ "default": "./plugin.ts"
22
+ },
23
+ "./production": {
24
+ "types": "./production.ts",
25
+ "import": "./production.ts",
26
+ "default": "./production.ts"
27
+ }
28
+ }
29
+ }
package/plugin.ts ADDED
@@ -0,0 +1,79 @@
1
+ /// <reference types="node" />
2
+ import { resolve } from 'path';
3
+ import { pathToFileURL } from 'url';
4
+ import type { Plugin } from 'vite';
5
+ import { createFlowServer, type LoadHandler } from './core.js';
6
+ import type { FlowHandler } from './index.js';
7
+
8
+ type NodeReq = import('http').IncomingMessage;
9
+ type NodeRes = import('http').ServerResponse;
10
+
11
+ function nodeRequestToWebRequest(req: NodeReq, baseUrl: string): Request {
12
+ const url = (req.url ?? '/').startsWith('http') ? req.url! : baseUrl + (req.url ?? '/');
13
+ const opts: RequestInit = {
14
+ method: req.method ?? 'GET',
15
+ headers: req.headers as HeadersInit,
16
+ };
17
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
18
+ (opts as RequestInit & { duplex?: string }).body = req as unknown as BodyInit;
19
+ (opts as RequestInit & { duplex?: string }).duplex = 'half';
20
+ }
21
+ return new Request(url, opts);
22
+ }
23
+
24
+ async function webResponseToNodeResponse(webRes: Response, res: NodeRes): Promise<void> {
25
+ res.writeHead(webRes.status, Object.fromEntries(webRes.headers));
26
+ if (webRes.body) {
27
+ const buf = Buffer.from(await webRes.arrayBuffer());
28
+ res.end(buf);
29
+ } else {
30
+ res.end();
31
+ }
32
+ }
33
+
34
+ export function flowServerMiddleware(
35
+ dispatch: (pathname: string, method: string, request: Request) => Promise<Response | null>,
36
+ matches: (pathname: string, method: string) => boolean
37
+ ): (req: NodeReq, res: NodeRes, next: () => void) => void {
38
+ return (req, res, next) => {
39
+ const pathname = req.url?.split('?')[0] ?? '/';
40
+ const method = req.method ?? 'GET';
41
+ if (!matches(pathname, method)) return next();
42
+ const baseUrl = `http://${req.headers.host ?? 'localhost'}`;
43
+ const request = nodeRequestToWebRequest(req, baseUrl);
44
+ dispatch(pathname, method, request)
45
+ .then((response) => {
46
+ if (response) return webResponseToNodeResponse(response, res);
47
+ next();
48
+ })
49
+ .catch((err) => {
50
+ res.writeHead(500, { 'Content-Type': 'text/plain' });
51
+ res.end(String(err?.message ?? err));
52
+ });
53
+ };
54
+ }
55
+
56
+ export type FlowServerOptions = {
57
+ /** Cartella route API (file-based). Default: server/routes */
58
+ routesDir?: string;
59
+ };
60
+
61
+ type ConnectStack = Array<{ route: string; handle: (req: NodeReq, res: NodeRes, next: () => void) => void }>;
62
+
63
+ export function flowServer(options?: FlowServerOptions): Plugin {
64
+ const routesDir = options?.routesDir ?? resolve(process.cwd(), 'server', 'routes');
65
+ return {
66
+ name: 'flow-server',
67
+ enforce: 'pre',
68
+ configureServer: async (server) => {
69
+ const loadHandler: LoadHandler = (filePath) =>
70
+ server.ssrLoadModule(pathToFileURL(filePath).href) as Promise<{ default: FlowHandler }>;
71
+ const { dispatch, matches } = await createFlowServer(routesDir, loadHandler);
72
+ const middleware = flowServerMiddleware(dispatch, matches);
73
+ (server.middlewares as unknown as { stack: ConnectStack }).stack.unshift({
74
+ route: '',
75
+ handle: middleware,
76
+ });
77
+ },
78
+ };
79
+ }
package/production.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { join, resolve } from 'path';
2
+ import { pathToFileURL } from 'url';
3
+ import { createFlowServer } from './core.js';
4
+
5
+ declare const Bun: {
6
+ file(path: string): { exists(): Promise<boolean> };
7
+ serve(options: { port: number; hostname: string; fetch(req: Request): Promise<Response> }): { hostname: string; port: number };
8
+ };
9
+
10
+ export type ProductionServerOptions = {
11
+ /** Cartella route (es. server/routes). Default: server/routes */
12
+ routesDir?: string;
13
+ /** Cartella statici (es. dist). Default: dist */
14
+ staticDir?: string;
15
+ /** Porta. Default: 3000 */
16
+ port?: number;
17
+ /** Hostname. Default: 0.0.0.0 */
18
+ hostname?: string;
19
+ };
20
+
21
+ /**
22
+ * Serve un file dalla cartella staticDir. pathname = / -> index.html.
23
+ * Se il file non esiste (SPA client-side routing) ritorna index.html.
24
+ */
25
+ async function serveStatic(staticDir: string, pathname: string): Promise<Response> {
26
+ const path = pathname === '/' ? '/index.html' : pathname;
27
+ const filePath = join(staticDir, path.replace(/^\//, ''));
28
+ try {
29
+ const file = Bun.file(filePath);
30
+ if (await file.exists()) {
31
+ return new Response(file as unknown as BodyInit, { headers: { 'Cache-Control': 'public, max-age=0' } });
32
+ }
33
+ } catch {
34
+ // ignore
35
+ }
36
+ // SPA fallback: qualsiasi path non trovato → index.html
37
+ const indexFile = Bun.file(join(staticDir, 'index.html'));
38
+ if (await indexFile.exists()) return new Response(indexFile as unknown as BodyInit);
39
+ return new Response('Not Found', { status: 404 });
40
+ }
41
+
42
+ /**
43
+ * Avvia il server di produzione: API da routesDir + statici da staticDir.
44
+ * Usa Bun.serve() (solo Bun).
45
+ */
46
+ export async function runProductionServer(options: ProductionServerOptions = {}): Promise<void> {
47
+ const cwd = process.cwd();
48
+ const routesDir = resolve(cwd, options.routesDir ?? 'server/routes');
49
+ const staticDir = resolve(cwd, options.staticDir ?? 'dist');
50
+ const port = options.port ?? 3000;
51
+ const hostname = options.hostname ?? '0.0.0.0';
52
+
53
+ const loadHandler = async (filePath: string) => {
54
+ return import(pathToFileURL(filePath).href) as Promise<{ default: import('./index.js').FlowHandler }>;
55
+ };
56
+
57
+ const { dispatch, matches } = await createFlowServer(routesDir, loadHandler);
58
+
59
+ const server = Bun.serve({
60
+ port,
61
+ hostname,
62
+ async fetch(req: Request) {
63
+ const url = new URL(req.url);
64
+ const pathname = url.pathname;
65
+ const method = req.method;
66
+
67
+ const apiRes = await dispatch(pathname, method, req);
68
+ if (apiRes) return apiRes;
69
+
70
+ return serveStatic(staticDir, pathname);
71
+ },
72
+ });
73
+
74
+ console.log(`Server: http://${server.hostname}:${server.port}`);
75
+ }
package/start.ts ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env bun
2
+ /**
3
+ * Entry produzione: lancia il server (statici + API).
4
+ * Uso: bunx @flow.os/server oppure "start": "bunx @flow.os/server" in package.json.
5
+ */
6
+ import { runProductionServer } from './production.js';
7
+
8
+ const port = process.env['PORT'] ? Number(process.env['PORT']) : 3000;
9
+
10
+ runProductionServer({
11
+ routesDir: 'server/routes',
12
+ staticDir: 'dist',
13
+ port,
14
+ hostname: '0.0.0.0',
15
+ });