treesap 0.1.13 → 0.2.1
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 +31 -192
- package/dist/app.d.ts +28 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +184 -0
- package/dist/app.js.map +1 -0
- package/dist/context.d.ts +36 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +95 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +5 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -9
- package/dist/index.js.map +1 -1
- package/dist/middleware/cors.d.ts +11 -0
- package/dist/middleware/cors.d.ts.map +1 -0
- package/dist/middleware/cors.js +34 -0
- package/dist/middleware/cors.js.map +1 -0
- package/dist/middleware/serve-static.d.ts +6 -0
- package/dist/middleware/serve-static.d.ts.map +1 -0
- package/dist/middleware/serve-static.js +68 -0
- package/dist/middleware/serve-static.js.map +1 -0
- package/dist/node.d.ts +8 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +52 -0
- package/dist/node.js.map +1 -0
- package/dist/path.d.ts +10 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +45 -0
- package/dist/path.js.map +1 -0
- package/dist/vite.d.ts +31 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +278 -0
- package/dist/vite.js.map +1 -0
- package/package.json +33 -40
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -137
- package/dist/cli.js.map +0 -1
- package/dist/components/BaseHead.d.ts +0 -5
- package/dist/components/BaseHead.d.ts.map +0 -1
- package/dist/components/BaseHead.js +0 -161
- package/dist/components/BaseHead.js.map +0 -1
- package/dist/components/ChatInput.d.ts +0 -7
- package/dist/components/ChatInput.d.ts.map +0 -1
- package/dist/components/ChatInput.js +0 -11
- package/dist/components/ChatInput.js.map +0 -1
- package/dist/components/Sidebar.d.ts +0 -8
- package/dist/components/Sidebar.d.ts.map +0 -1
- package/dist/components/Sidebar.js +0 -7
- package/dist/components/Sidebar.js.map +0 -1
- package/dist/components/SimpleLivePreview.d.ts +0 -7
- package/dist/components/SimpleLivePreview.d.ts.map +0 -1
- package/dist/components/SimpleLivePreview.js +0 -7
- package/dist/components/SimpleLivePreview.js.map +0 -1
- package/dist/components/Terminal.d.ts +0 -7
- package/dist/components/Terminal.d.ts.map +0 -1
- package/dist/components/Terminal.js +0 -14
- package/dist/components/Terminal.js.map +0 -1
- package/dist/components/VoiceRecorder.d.ts +0 -4
- package/dist/components/VoiceRecorder.d.ts.map +0 -1
- package/dist/components/VoiceRecorder.js +0 -5
- package/dist/components/VoiceRecorder.js.map +0 -1
- package/dist/components/icons/GeminiLogo.d.ts +0 -7
- package/dist/components/icons/GeminiLogo.d.ts.map +0 -1
- package/dist/components/icons/GeminiLogo.js +0 -5
- package/dist/components/icons/GeminiLogo.js.map +0 -1
- package/dist/components/icons/OllamaLogo.d.ts +0 -2
- package/dist/components/icons/OllamaLogo.d.ts.map +0 -1
- package/dist/components/icons/OllamaLogo.js +0 -5
- package/dist/components/icons/OllamaLogo.js.map +0 -1
- package/dist/layouts/Layout.d.ts +0 -9
- package/dist/layouts/Layout.d.ts.map +0 -1
- package/dist/layouts/Layout.js +0 -9
- package/dist/layouts/Layout.js.map +0 -1
- package/dist/layouts/NotFoundLayout.d.ts +0 -2
- package/dist/layouts/NotFoundLayout.d.ts.map +0 -1
- package/dist/layouts/NotFoundLayout.js +0 -6
- package/dist/layouts/NotFoundLayout.js.map +0 -1
- package/dist/pages/Code.d.ts +0 -7
- package/dist/pages/Code.d.ts.map +0 -1
- package/dist/pages/Code.js +0 -8
- package/dist/pages/Code.js.map +0 -1
- package/dist/pages/Home.d.ts +0 -7
- package/dist/pages/Home.d.ts.map +0 -1
- package/dist/pages/Home.js +0 -8
- package/dist/pages/Home.js.map +0 -1
- package/dist/pages/Welcome.d.ts +0 -2
- package/dist/pages/Welcome.d.ts.map +0 -1
- package/dist/pages/Welcome.js +0 -6
- package/dist/pages/Welcome.js.map +0 -1
- package/dist/server.d.ts +0 -11
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -434
- package/dist/server.js.map +0 -1
- package/dist/services/dev-server.d.ts +0 -29
- package/dist/services/dev-server.d.ts.map +0 -1
- package/dist/services/dev-server.js +0 -201
- package/dist/services/dev-server.js.map +0 -1
- package/dist/services/terminal.d.ts +0 -46
- package/dist/services/terminal.d.ts.map +0 -1
- package/dist/services/terminal.js +0 -264
- package/dist/services/terminal.js.map +0 -1
- package/dist/services/websocket.d.ts +0 -48
- package/dist/services/websocket.d.ts.map +0 -1
- package/dist/services/websocket.js +0 -332
- package/dist/services/websocket.js.map +0 -1
- package/dist/static/components/ChatInput.js +0 -237
- package/dist/static/components/Sidebar.js +0 -225
- package/dist/static/components/SimpleLivePreview.js +0 -305
- package/dist/static/components/Terminal.js +0 -461
- package/dist/static/components/TerminalTabs.js +0 -383
- package/dist/static/favicon.svg +0 -14
- package/dist/static/signals/LivePreviewSignal.js +0 -71
- package/dist/static/signals/SidebarSignal.js +0 -123
- package/dist/static/signals/TerminalSignal.js +0 -273
- package/dist/static/styles/main.css +0 -1761
- package/src/cli.ts +0 -155
- package/src/components/BaseHead.ts +0 -164
- package/src/components/ChatInput.tsx +0 -56
- package/src/components/Sidebar.tsx +0 -99
- package/src/components/SimpleLivePreview.tsx +0 -40
- package/src/components/Terminal.tsx +0 -40
- package/src/components/VoiceRecorder.tsx +0 -33
- package/src/components/icons/GeminiLogo.tsx +0 -10
- package/src/components/icons/OllamaLogo.tsx +0 -5
- package/src/index.tsx +0 -12
- package/src/layouts/Layout.tsx +0 -41
- package/src/layouts/NotFoundLayout.tsx +0 -15
- package/src/pages/Code.tsx +0 -34
- package/src/pages/Welcome.tsx +0 -56
- package/src/server.tsx +0 -519
- package/src/services/dev-server.ts +0 -234
- package/src/services/terminal.ts +0 -325
- package/src/services/websocket.ts +0 -405
- package/src/static/components/ChatInput.js +0 -237
- package/src/static/components/Sidebar.js +0 -225
- package/src/static/components/SimpleLivePreview.js +0 -305
- package/src/static/components/Terminal.js +0 -461
- package/src/static/components/TerminalTabs.js +0 -383
- package/src/static/favicon.svg +0 -14
- package/src/static/signals/LivePreviewSignal.js +0 -71
- package/src/static/signals/SidebarSignal.js +0 -123
- package/src/static/signals/TerminalSignal.js +0 -273
- package/src/static/styles/main.css +0 -1761
- package/src/styles/input.css +0 -3
- package/tailwind.config.ts +0 -22
- package/tsconfig.json +0 -37
package/README.md
CHANGED
|
@@ -1,217 +1,56 @@
|
|
|
1
1
|
# Treesap
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Treesap is a Vite-first Node SSR framework.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This package provides:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- a small request/response app runtime for Node
|
|
8
|
+
- router and middleware primitives
|
|
9
|
+
- static file serving with sensible cache defaults
|
|
10
|
+
- a `treesap/vite` entry for Vite dev and production builds
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
- **Real-Time Cross-Session Monitoring** - External agents can observe and control multiple terminal sessions
|
|
11
|
-
- **Mobile-Responsive Terminal Interface** - Chat-style input for better mobile development experience
|
|
12
|
-
- **WebSocket-Based Architecture** - Real-time bidirectional communication between terminals, web UI, and external agents
|
|
13
|
-
- **Framework Agnostic** - Works with any development stack (Hono, Vite, React, etc.)
|
|
14
|
-
- **Persistent Terminal Sessions** - Sessions survive across browser refreshes and disconnections
|
|
15
|
-
- **Live Preview Integration** - Built-in development server with live reload
|
|
16
|
-
|
|
17
|
-
## 🚀 Quick Start
|
|
18
|
-
|
|
19
|
-
### Installation
|
|
12
|
+
## Install
|
|
20
13
|
|
|
21
14
|
```bash
|
|
22
|
-
npm install treesap
|
|
15
|
+
npm install treesap vite hono
|
|
23
16
|
```
|
|
24
17
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
1. Create a `treesap.config.ts` file in your project root:
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
import type { TreesapConfig } from 'treesap';
|
|
31
|
-
|
|
32
|
-
const config: TreesapConfig = {
|
|
33
|
-
port: 1235, // Treesap server port
|
|
34
|
-
previewPort: 5173, // Your app's dev server port
|
|
35
|
-
devCommand: "npm run dev", // Command to start your dev server
|
|
36
|
-
devPort: 5173, // Port your dev server runs on
|
|
37
|
-
projectRoot: process.cwd()
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export default config;
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
2. Add scripts to your `package.json`:
|
|
44
|
-
|
|
45
|
-
```json
|
|
46
|
-
{
|
|
47
|
-
"scripts": {
|
|
48
|
-
"dev:treesap": "treesap dev",
|
|
49
|
-
"treesap": "treesap start"
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
```
|
|
18
|
+
## Vite Config
|
|
53
19
|
|
|
54
|
-
|
|
20
|
+
```ts
|
|
21
|
+
import react from "@vitejs/plugin-react";
|
|
22
|
+
import { defineTreesapConfig } from "treesap/vite";
|
|
55
23
|
|
|
56
|
-
|
|
57
|
-
|
|
24
|
+
export default defineTreesapConfig({
|
|
25
|
+
appEntry: "src/server/app.tsx",
|
|
26
|
+
plugins: [react()],
|
|
27
|
+
});
|
|
58
28
|
```
|
|
59
29
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
## 🏗️ Core Concepts
|
|
63
|
-
|
|
64
|
-
### Terminal Sessions
|
|
65
|
-
Each terminal session is persistent and can be shared across multiple clients (browsers, agents, etc.). Sessions survive disconnections and can be resumed at any time.
|
|
30
|
+
## Server App
|
|
66
31
|
|
|
67
|
-
|
|
68
|
-
|
|
32
|
+
```ts
|
|
33
|
+
import { renderToString } from "hono/jsx/dom/server";
|
|
34
|
+
import { createApp } from "treesap";
|
|
69
35
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
- Monitor multiple terminal sessions simultaneously
|
|
73
|
-
- Send commands to specific terminals
|
|
74
|
-
- Receive real-time output from all monitored sessions
|
|
75
|
-
- Coordinate complex multi-step tasks across multiple environments
|
|
36
|
+
export function createServerApp() {
|
|
37
|
+
const app = createApp();
|
|
76
38
|
|
|
77
|
-
|
|
39
|
+
app.get("/", (ctx) => {
|
|
40
|
+
return ctx.html(renderToString(<h1>Hello</h1>));
|
|
41
|
+
});
|
|
78
42
|
|
|
79
|
-
|
|
80
|
-
- **Chat-style input** - Familiar textarea interface for mobile users
|
|
81
|
-
- **Two-step command execution** - Send to input field, then execute
|
|
82
|
-
- **Automatic terminal resizing** - Adapts to mobile screen sizes and orientation changes
|
|
83
|
-
- **Touch-optimized controls** - Better interaction on mobile devices
|
|
84
|
-
|
|
85
|
-
## ⚙️ Configuration
|
|
86
|
-
|
|
87
|
-
The `TreesapConfig` interface supports these options:
|
|
88
|
-
|
|
89
|
-
```typescript
|
|
90
|
-
interface TreesapConfig {
|
|
91
|
-
port?: number; // Treesap server port (default: 1235)
|
|
92
|
-
previewPort?: number; // Preview iframe port (default: 3000)
|
|
93
|
-
devCommand?: string; // Command to auto-start dev server
|
|
94
|
-
devPort?: number; // Dev server port to auto-start
|
|
95
|
-
projectRoot?: string; // Project root directory
|
|
43
|
+
return app;
|
|
96
44
|
}
|
|
97
45
|
```
|
|
98
46
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
Treesap enables unprecedented AI agent collaboration:
|
|
102
|
-
|
|
103
|
-
### Agent Orchestration
|
|
104
|
-
- **Supervisor Agents** - Monitor multiple coding agents across different terminals
|
|
105
|
-
- **Specialized Agents** - Each agent can work in its dedicated terminal session
|
|
106
|
-
- **Cross-Agent Communication** - Agents can coordinate through the WebSocket infrastructure
|
|
107
|
-
|
|
108
|
-
### Human-AI Collaboration
|
|
109
|
-
- **Seamless Handoffs** - Switch between human and AI control of terminal sessions
|
|
110
|
-
- **Real-time Monitoring** - Watch AI agents work while maintaining override capability
|
|
111
|
-
- **Collaborative Debugging** - Multiple agents can work on the same problem simultaneously
|
|
112
|
-
|
|
113
|
-
## 🛠️ Architecture
|
|
114
|
-
|
|
115
|
-
### WebSocket Terminal Service
|
|
116
|
-
- Persistent terminal sessions using `node-pty`
|
|
117
|
-
- Multi-client session sharing
|
|
118
|
-
- Cross-session monitoring and control
|
|
119
|
-
- Real-time input/output streaming
|
|
120
|
-
|
|
121
|
-
### Sapling Islands Components
|
|
122
|
-
- Reactive terminal interface
|
|
123
|
-
- Live preview integration
|
|
124
|
-
- Mobile-responsive design
|
|
125
|
-
- Chat-style input system
|
|
126
|
-
|
|
127
|
-
### External Agent Integration
|
|
128
|
-
```typescript
|
|
129
|
-
// Connect to WebSocket for monitoring
|
|
130
|
-
const ws = new WebSocket('ws://localhost:1235/terminal/ws');
|
|
131
|
-
|
|
132
|
-
// Join a terminal session
|
|
133
|
-
ws.send(JSON.stringify({
|
|
134
|
-
type: 'join',
|
|
135
|
-
sessionId: 'terminal-1',
|
|
136
|
-
terminalId: 'terminal-1'
|
|
137
|
-
}));
|
|
138
|
-
|
|
139
|
-
// Send commands
|
|
140
|
-
ws.send(JSON.stringify({
|
|
141
|
-
type: 'input',
|
|
142
|
-
sessionId: 'terminal-1',
|
|
143
|
-
data: 'npm test\r'
|
|
144
|
-
}));
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
## 🎯 Use Cases
|
|
148
|
-
|
|
149
|
-
### AI Development Teams
|
|
150
|
-
- Multiple AI agents working on different parts of a large codebase
|
|
151
|
-
- Coordinated testing and deployment across multiple environments
|
|
152
|
-
- Real-time code review and collaboration between AI agents
|
|
153
|
-
|
|
154
|
-
### Educational Platforms
|
|
155
|
-
- Instructors monitoring multiple student coding sessions
|
|
156
|
-
- AI tutors providing real-time assistance
|
|
157
|
-
- Collaborative coding exercises with mixed human-AI teams
|
|
158
|
-
|
|
159
|
-
### DevOps Orchestration
|
|
160
|
-
- Coordinated deployments across multiple servers
|
|
161
|
-
- Real-time monitoring and intervention capabilities
|
|
162
|
-
- Automated testing with human oversight
|
|
163
|
-
|
|
164
|
-
## 📚 API Reference
|
|
165
|
-
|
|
166
|
-
### WebSocket Message Types
|
|
167
|
-
- `join` - Join a terminal session
|
|
168
|
-
- `leave` - Leave a terminal session
|
|
169
|
-
- `input` - Send input to terminal
|
|
170
|
-
- `resize` - Resize terminal dimensions
|
|
171
|
-
- `ping/pong` - Connection health checks
|
|
172
|
-
|
|
173
|
-
### Terminal Service Methods
|
|
174
|
-
- `getActiveSessions()` - List all active sessions
|
|
175
|
-
- `sendCommandToSession(sessionId, command)` - Send command to session
|
|
176
|
-
- `getSessionClients(sessionId)` - Get connected clients
|
|
177
|
-
- `closeSession(sessionId)` - Terminate a session
|
|
178
|
-
|
|
179
|
-
## 🔧 Development
|
|
180
|
-
|
|
181
|
-
### Prerequisites
|
|
182
|
-
- Node.js >=18.0.0
|
|
183
|
-
- TypeScript support
|
|
47
|
+
Build with `vite build`, then run the server bundle with:
|
|
184
48
|
|
|
185
|
-
### Building from Source
|
|
186
49
|
```bash
|
|
187
|
-
|
|
188
|
-
cd treesap/packages/treesap
|
|
189
|
-
npm install
|
|
190
|
-
npm run build
|
|
50
|
+
node dist/server/main.js
|
|
191
51
|
```
|
|
192
52
|
|
|
193
|
-
|
|
194
|
-
```bash
|
|
195
|
-
npm run dev # Start with hot reload
|
|
196
|
-
npm run dev:css # Watch CSS changes
|
|
197
|
-
npm run build # Build for production
|
|
198
|
-
npm run clean # Clean build artifacts
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
## 🤝 Contributing
|
|
202
|
-
|
|
203
|
-
We welcome contributions! Treesap is building the foundation for the future of AI-powered development.
|
|
204
|
-
|
|
205
|
-
## 📄 License
|
|
206
|
-
|
|
207
|
-
MIT License - see LICENSE file for details.
|
|
208
|
-
|
|
209
|
-
## 🔗 Links
|
|
210
|
-
|
|
211
|
-
- [GitHub Repository](https://github.com/withtreesap/treesap)
|
|
212
|
-
- [Issues & Bug Reports](https://github.com/withtreesap/treesap/issues)
|
|
213
|
-
- [NPM Package](https://www.npmjs.com/package/treesap)
|
|
214
|
-
|
|
215
|
-
---
|
|
53
|
+
## Exports
|
|
216
54
|
|
|
217
|
-
|
|
55
|
+
- `treesap`: `createApp`, `createRouter`, `serve`, `Context`, `cors`, `serveStatic`
|
|
56
|
+
- `treesap/vite`: `defineTreesapConfig`, `getViteEntryAssets`, `treesap`
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Context } from "./context.ts";
|
|
2
|
+
export type Next = () => Promise<Response>;
|
|
3
|
+
export type HandlerResult = Response | void | Promise<Response | void>;
|
|
4
|
+
export type Handler = (ctx: Context, next: Next) => HandlerResult;
|
|
5
|
+
export interface CreateAppOptions {
|
|
6
|
+
rootDir?: string;
|
|
7
|
+
publicDir?: string | false;
|
|
8
|
+
}
|
|
9
|
+
export declare class ServerApp {
|
|
10
|
+
private readonly frameworkMiddlewares;
|
|
11
|
+
private readonly middlewares;
|
|
12
|
+
private readonly routes;
|
|
13
|
+
private notFoundHandler;
|
|
14
|
+
constructor(options?: CreateAppOptions);
|
|
15
|
+
use(pathOrHandler: string | Handler, ...handlers: Handler[]): this;
|
|
16
|
+
on(methods: string[] | readonly string[], path: string, ...handlers: Handler[]): this;
|
|
17
|
+
get(path: string, ...handlers: Handler[]): this;
|
|
18
|
+
post(path: string, ...handlers: Handler[]): this;
|
|
19
|
+
patch(path: string, ...handlers: Handler[]): this;
|
|
20
|
+
delete(path: string, ...handlers: Handler[]): this;
|
|
21
|
+
route(prefix: string, child: ServerApp): this;
|
|
22
|
+
mount(prefix: string, child: ServerApp): this;
|
|
23
|
+
notFound(handler: Handler): this;
|
|
24
|
+
fetch(request: Request | string | URL, init?: RequestInit): Promise<Response>;
|
|
25
|
+
}
|
|
26
|
+
export declare function createApp(options?: CreateAppOptions): ServerApp;
|
|
27
|
+
export declare function createRouter(): ServerApp;
|
|
28
|
+
//# sourceMappingURL=app.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":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M,EAAE,KAAK,EAAE,SAAS;IAqBtC,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS;IAItC,QAAQ,CAAC,OAAO,EAAE,OAAO;IAKnB,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW;CA8ChE;AAED,wBAAgB,SAAS,CAAC,OAAO,CAAC,EAAE,gBAAgB,aAEnD;AAED,wBAAgB,YAAY,cAE3B"}
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { Context } from "./context.js";
|
|
4
|
+
import { serveStatic } from "./middleware/serve-static.js";
|
|
5
|
+
import { compilePath, joinPaths } from "./path.js";
|
|
6
|
+
function isProductionRuntime() {
|
|
7
|
+
const viteEnv = import.meta.env;
|
|
8
|
+
return viteEnv?.PROD === true || process.env.NODE_ENV === "production";
|
|
9
|
+
}
|
|
10
|
+
function resolveDefaultPublicRoot(rootDir) {
|
|
11
|
+
const builtClientDir = path.resolve(rootDir, "dist/client");
|
|
12
|
+
const sourcePublicDir = path.resolve(rootDir, "public");
|
|
13
|
+
if (isProductionRuntime() && existsSync(builtClientDir)) {
|
|
14
|
+
return builtClientDir;
|
|
15
|
+
}
|
|
16
|
+
if (existsSync(sourcePublicDir)) {
|
|
17
|
+
return sourcePublicDir;
|
|
18
|
+
}
|
|
19
|
+
if (existsSync(builtClientDir)) {
|
|
20
|
+
return builtClientDir;
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
function applyResponseDefaults(response) {
|
|
25
|
+
const contentType = response.headers.get("Content-Type")?.toLowerCase() ?? "";
|
|
26
|
+
if (!contentType.startsWith("text/html") ||
|
|
27
|
+
response.headers.has("Cache-Control")) {
|
|
28
|
+
return response;
|
|
29
|
+
}
|
|
30
|
+
const headers = new Headers(response.headers);
|
|
31
|
+
headers.set("Cache-Control", "public, max-age=0, must-revalidate");
|
|
32
|
+
return new Response(response.body, {
|
|
33
|
+
status: response.status,
|
|
34
|
+
statusText: response.statusText,
|
|
35
|
+
headers,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async function composeHandlers(handlers, ctx, finalNext) {
|
|
39
|
+
let index = -1;
|
|
40
|
+
async function dispatch(position) {
|
|
41
|
+
if (position <= index) {
|
|
42
|
+
throw new Error("next() called multiple times");
|
|
43
|
+
}
|
|
44
|
+
index = position;
|
|
45
|
+
const handler = handlers[position];
|
|
46
|
+
if (!handler) {
|
|
47
|
+
return finalNext();
|
|
48
|
+
}
|
|
49
|
+
let downstream;
|
|
50
|
+
const response = await handler(ctx, async () => {
|
|
51
|
+
downstream = await dispatch(position + 1);
|
|
52
|
+
return downstream;
|
|
53
|
+
});
|
|
54
|
+
if (response) {
|
|
55
|
+
return response;
|
|
56
|
+
}
|
|
57
|
+
if (downstream) {
|
|
58
|
+
return downstream;
|
|
59
|
+
}
|
|
60
|
+
return new Response(null, { status: 204 });
|
|
61
|
+
}
|
|
62
|
+
return dispatch(0);
|
|
63
|
+
}
|
|
64
|
+
export class ServerApp {
|
|
65
|
+
frameworkMiddlewares = [];
|
|
66
|
+
middlewares = [];
|
|
67
|
+
routes = [];
|
|
68
|
+
notFoundHandler = (ctx) => ctx.text("Not Found", { status: 404 });
|
|
69
|
+
constructor(options = {}) {
|
|
70
|
+
if (options.publicDir === false) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const rootDir = path.resolve(options.rootDir ?? process.cwd());
|
|
74
|
+
const staticRoot = typeof options.publicDir === "string"
|
|
75
|
+
? path.resolve(rootDir, options.publicDir)
|
|
76
|
+
: resolveDefaultPublicRoot(rootDir);
|
|
77
|
+
if (!staticRoot || !existsSync(staticRoot)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.frameworkMiddlewares.push({
|
|
81
|
+
path: "/*",
|
|
82
|
+
matcher: compilePath("/*"),
|
|
83
|
+
handler: serveStatic({ root: staticRoot }),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
use(pathOrHandler, ...handlers) {
|
|
87
|
+
const path = typeof pathOrHandler === "string" ? pathOrHandler : "*";
|
|
88
|
+
const list = typeof pathOrHandler === "string"
|
|
89
|
+
? handlers
|
|
90
|
+
: [pathOrHandler, ...handlers];
|
|
91
|
+
for (const handler of list) {
|
|
92
|
+
this.middlewares.push({
|
|
93
|
+
path,
|
|
94
|
+
matcher: compilePath(path),
|
|
95
|
+
handler,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
on(methods, path, ...handlers) {
|
|
101
|
+
this.routes.push({
|
|
102
|
+
path,
|
|
103
|
+
methods: [...methods].map((method) => method.toUpperCase()),
|
|
104
|
+
matcher: compilePath(path),
|
|
105
|
+
handlers,
|
|
106
|
+
});
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
get(path, ...handlers) {
|
|
110
|
+
return this.on(["GET"], path, ...handlers);
|
|
111
|
+
}
|
|
112
|
+
post(path, ...handlers) {
|
|
113
|
+
return this.on(["POST"], path, ...handlers);
|
|
114
|
+
}
|
|
115
|
+
patch(path, ...handlers) {
|
|
116
|
+
return this.on(["PATCH"], path, ...handlers);
|
|
117
|
+
}
|
|
118
|
+
delete(path, ...handlers) {
|
|
119
|
+
return this.on(["DELETE"], path, ...handlers);
|
|
120
|
+
}
|
|
121
|
+
route(prefix, child) {
|
|
122
|
+
child.middlewares.forEach((record) => {
|
|
123
|
+
this.middlewares.push({
|
|
124
|
+
path: joinPaths(prefix, record.path),
|
|
125
|
+
matcher: compilePath(joinPaths(prefix, record.path)),
|
|
126
|
+
handler: record.handler,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
child.routes.forEach((record) => {
|
|
130
|
+
this.routes.push({
|
|
131
|
+
path: joinPaths(prefix, record.path),
|
|
132
|
+
methods: [...record.methods],
|
|
133
|
+
matcher: compilePath(joinPaths(prefix, record.path)),
|
|
134
|
+
handlers: [...record.handlers],
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
mount(prefix, child) {
|
|
140
|
+
return this.route(prefix, child);
|
|
141
|
+
}
|
|
142
|
+
notFound(handler) {
|
|
143
|
+
this.notFoundHandler = handler;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
async fetch(request, init) {
|
|
147
|
+
const normalizedRequest = request instanceof Request ? request : new Request(request, init);
|
|
148
|
+
const url = new URL(normalizedRequest.url);
|
|
149
|
+
const method = normalizedRequest.method.toUpperCase();
|
|
150
|
+
const middlewareStack = this.middlewares
|
|
151
|
+
.filter((record) => record.matcher.test(url.pathname))
|
|
152
|
+
.concat(this.frameworkMiddlewares.filter((record) => record.matcher.test(url.pathname)))
|
|
153
|
+
.map((record) => record.handler);
|
|
154
|
+
const route = this.routes.find((record) => {
|
|
155
|
+
if (!record.methods.includes(method) && !record.methods.includes("ALL")) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
return Boolean(record.matcher.test(url.pathname));
|
|
159
|
+
});
|
|
160
|
+
const params = route?.matcher.test(url.pathname)?.params ?? {};
|
|
161
|
+
const ctx = new Context(normalizedRequest, params);
|
|
162
|
+
const notFound = async () => {
|
|
163
|
+
const response = await this.notFoundHandler(ctx, async () => new Response(null, { status: 404 }));
|
|
164
|
+
if (response instanceof Response) {
|
|
165
|
+
return response;
|
|
166
|
+
}
|
|
167
|
+
return new Response(null, { status: 404 });
|
|
168
|
+
};
|
|
169
|
+
if (!route) {
|
|
170
|
+
const response = await composeHandlers(middlewareStack, ctx, notFound);
|
|
171
|
+
return applyResponseDefaults(response);
|
|
172
|
+
}
|
|
173
|
+
const handlers = [...middlewareStack, ...route.handlers];
|
|
174
|
+
const response = await composeHandlers(handlers, ctx, notFound);
|
|
175
|
+
return applyResponseDefaults(response);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
export function createApp(options) {
|
|
179
|
+
return new ServerApp(options);
|
|
180
|
+
}
|
|
181
|
+
export function createRouter() {
|
|
182
|
+
return new ServerApp({ publicDir: false });
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=app.js.map
|
package/dist/app.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,SAAS,EAAqB,MAAM,WAAW,CAAC;AAWtE,SAAS,mBAAmB;IAC1B,MAAM,OAAO,GAAI,MAAM,CAAC,IAEtB,CAAC,GAAG,CAAC;IAEP,OAAO,OAAO,EAAE,IAAI,KAAK,IAAI,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,CAAC;AACzE,CAAC;AAED,SAAS,wBAAwB,CAAC,OAAe;IAC/C,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;IAC5D,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAExD,IAAI,mBAAmB,EAAE,IAAI,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;QACxD,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,IAAI,UAAU,CAAC,eAAe,CAAC,EAAE,CAAC;QAChC,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,IAAI,UAAU,CAAC,cAAc,CAAC,EAAE,CAAC;QAC/B,OAAO,cAAc,CAAC;IACxB,CAAC;IAED,OAAO,IAAI,CAAC;AACd,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3
|
+
export type Renderer = (content: Renderable, ctx: Context, init?: ResponseInit) => Response | Promise<Response>;
|
|
4
|
+
export declare class Context {
|
|
5
|
+
readonly request: Request;
|
|
6
|
+
readonly url: URL;
|
|
7
|
+
readonly var: Map<string, unknown>;
|
|
8
|
+
readonly res: {
|
|
9
|
+
headers: Headers;
|
|
10
|
+
status: number | undefined;
|
|
11
|
+
};
|
|
12
|
+
private renderer?;
|
|
13
|
+
private readonly params;
|
|
14
|
+
readonly req: {
|
|
15
|
+
raw: Request;
|
|
16
|
+
url: string;
|
|
17
|
+
json: <T = unknown>() => Promise<T>;
|
|
18
|
+
text: () => Promise<string>;
|
|
19
|
+
formData: () => Promise<FormData>;
|
|
20
|
+
param: (name?: string) => string | Record<string, string> | undefined;
|
|
21
|
+
header: (name: string) => string | undefined;
|
|
22
|
+
query: (name?: string) => string | Record<string, string> | undefined;
|
|
23
|
+
};
|
|
24
|
+
constructor(request: Request, params?: Record<string, string>);
|
|
25
|
+
set<T>(key: string, value: T): void;
|
|
26
|
+
get<T>(key: string): T;
|
|
27
|
+
status(code: number): void;
|
|
28
|
+
header(name: string, value: string): void;
|
|
29
|
+
json(data: unknown, init?: ResponseInit): Response;
|
|
30
|
+
text(value: string, init?: ResponseInit): Response;
|
|
31
|
+
html(value: string, init?: ResponseInit): Response;
|
|
32
|
+
redirect(location: string, status?: number): Response;
|
|
33
|
+
setRenderer(renderer: Renderer): void;
|
|
34
|
+
render(content: Renderable, init?: ResponseInit): Response | Promise<Response>;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEtC,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,CAAC;AACxC,MAAM,MAAM,QAAQ,GAAG,CACrB,OAAO,EAAE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C,IAAI,EAAE,MAAM;IAInB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAIlC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,YAAY;IAMvC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,YAAY;IAMvC,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,YAAY;IAMvC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAM;IAMvC,WAAW,CAAC,QAAQ,EAAE,QAAQ;IAI9B,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,YAAY;CAWhD"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { renderToString } from "hono/jsx/dom/server";
|
|
2
|
+
function applyResponseDefaults(base, init) {
|
|
3
|
+
const headers = new Headers(base.headers);
|
|
4
|
+
if (init?.headers) {
|
|
5
|
+
new Headers(init.headers).forEach((value, key) => {
|
|
6
|
+
headers.set(key, value);
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
return {
|
|
10
|
+
status: init?.status ?? base.status,
|
|
11
|
+
headers,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export class Context {
|
|
15
|
+
request;
|
|
16
|
+
url;
|
|
17
|
+
var = new Map();
|
|
18
|
+
res = {
|
|
19
|
+
headers: new Headers(),
|
|
20
|
+
status: undefined,
|
|
21
|
+
};
|
|
22
|
+
renderer;
|
|
23
|
+
params;
|
|
24
|
+
req;
|
|
25
|
+
constructor(request, params = {}) {
|
|
26
|
+
this.request = request;
|
|
27
|
+
this.url = new URL(request.url);
|
|
28
|
+
this.params = params;
|
|
29
|
+
this.req = {
|
|
30
|
+
raw: request,
|
|
31
|
+
url: request.url,
|
|
32
|
+
json: () => request.json(),
|
|
33
|
+
text: () => request.text(),
|
|
34
|
+
formData: () => request.formData(),
|
|
35
|
+
param: (name) => {
|
|
36
|
+
if (!name) {
|
|
37
|
+
return { ...this.params };
|
|
38
|
+
}
|
|
39
|
+
return this.params[name];
|
|
40
|
+
},
|
|
41
|
+
header: (name) => request.headers.get(name) ?? undefined,
|
|
42
|
+
query: (name) => {
|
|
43
|
+
if (!name) {
|
|
44
|
+
return Object.fromEntries(this.url.searchParams.entries());
|
|
45
|
+
}
|
|
46
|
+
return this.url.searchParams.get(name) ?? undefined;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
set(key, value) {
|
|
51
|
+
this.var.set(key, value);
|
|
52
|
+
}
|
|
53
|
+
get(key) {
|
|
54
|
+
return this.var.get(key);
|
|
55
|
+
}
|
|
56
|
+
status(code) {
|
|
57
|
+
this.res.status = code;
|
|
58
|
+
}
|
|
59
|
+
header(name, value) {
|
|
60
|
+
this.res.headers.set(name, value);
|
|
61
|
+
}
|
|
62
|
+
json(data, init) {
|
|
63
|
+
const responseInit = applyResponseDefaults(this.res, init);
|
|
64
|
+
responseInit.headers.set("Content-Type", "application/json; charset=UTF-8");
|
|
65
|
+
return new Response(JSON.stringify(data), responseInit);
|
|
66
|
+
}
|
|
67
|
+
text(value, init) {
|
|
68
|
+
const responseInit = applyResponseDefaults(this.res, init);
|
|
69
|
+
responseInit.headers.set("Content-Type", "text/plain; charset=UTF-8");
|
|
70
|
+
return new Response(value, responseInit);
|
|
71
|
+
}
|
|
72
|
+
html(value, init) {
|
|
73
|
+
const responseInit = applyResponseDefaults(this.res, init);
|
|
74
|
+
responseInit.headers.set("Content-Type", "text/html; charset=UTF-8");
|
|
75
|
+
return new Response(value, responseInit);
|
|
76
|
+
}
|
|
77
|
+
redirect(location, status = 302) {
|
|
78
|
+
const responseInit = applyResponseDefaults(this.res, { status });
|
|
79
|
+
responseInit.headers.set("Location", location);
|
|
80
|
+
return new Response(null, responseInit);
|
|
81
|
+
}
|
|
82
|
+
setRenderer(renderer) {
|
|
83
|
+
this.renderer = renderer;
|
|
84
|
+
}
|
|
85
|
+
render(content, init) {
|
|
86
|
+
if (this.renderer) {
|
|
87
|
+
return this.renderer(content, this, init);
|
|
88
|
+
}
|
|
89
|
+
if (typeof content === "string") {
|
|
90
|
+
return this.html(content, init);
|
|
91
|
+
}
|
|
92
|
+
return this.html(renderToString(content), init);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAUrD,SAAS,qBAAqB,CAC5B,IAA2C,EAC3C,IAAmB;IAEnB,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC1C,IAAI,IAAI,EAAE,OAAO,EAAE,CAAC;QAClB,IAAI,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;YAC/C,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,OAAO;QACL,MAAM,EAAE,IAAI,EAAE,MAAM,IAAI,IAAI,CAAC,MAAM;QACnC,OAAO;KACR,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,OAAO;IAsBG;IArBZ,GAAG,CAAM;IACT,GAAG,GAAG,IAAI,GAAG,EAAmB,CAAC;IACjC,GAAG,GAAG;QACb,OAAO,EAAE,IAAI,OAAO,EAAE;QACtB,MAAM,EAAE,SAA+B;KACxC,CAAC;IAEM,QAAQ,CAAY;IACX,MAAM,CAAyB;IAEvC,GAAG,CASV;IAEF,YAAqB,OAAgB,EAAE,SAAiC,EAAE;QAArD,YAAO,GAAP,OAAO,CAAS;QACnC,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAA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Q,CAAC,CAAC;QAC/C,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IAC1C,CAAC;IAED,WAAW,CAAC,QAAkB;QAC5B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,MAAM,CAAC,OAAmB,EAAE,IAAmB;QAC7C,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC5C,CAAC;QAED,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAChC,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;QAED,OAAO,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export { Code } from './pages/Code.js';
|
|
7
|
-
export { DevServerManager } from './services/dev-server.js';
|
|
1
|
+
export { Context, type Renderable } from "./context.ts";
|
|
2
|
+
export { createApp, createRouter, type CreateAppOptions, type Handler, type Next, type HandlerResult, ServerApp, } from "./app.ts";
|
|
3
|
+
export { cors } from "./middleware/cors.ts";
|
|
4
|
+
export { serveStatic } from "./middleware/serve-static.ts";
|
|
5
|
+
export { serve, sendNodeResponse, toWebRequest } from "./node.ts";
|
|
8
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EACL,SAAS,EACT,YAAY,EACZ,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACZ,KAAK,IAAI,EACT,KAAK,aAAa,EAClB,SAAS,GACV,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
export {
|
|
6
|
-
export { default as NotFoundLayout } from './layouts/NotFoundLayout.js';
|
|
7
|
-
export { Welcome } from './pages/Welcome.js';
|
|
8
|
-
export { Code } from './pages/Code.js';
|
|
9
|
-
export { DevServerManager } from './services/dev-server.js';
|
|
1
|
+
export { Context } from "./context.js";
|
|
2
|
+
export { createApp, createRouter, ServerApp, } from "./app.js";
|
|
3
|
+
export { cors } from "./middleware/cors.js";
|
|
4
|
+
export { serveStatic } from "./middleware/serve-static.js";
|
|
5
|
+
export { serve, sendNodeResponse, toWebRequest } from "./node.js";
|
|
10
6
|
//# sourceMappingURL=index.js.map
|