@ryopc/koshi 0.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ryopc-org
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,419 @@
1
+ # ๐Ÿ„ koshi โ€” Terminal-Native Decentralized SNS
2
+
3
+ **Version 0.2.0** ยท MIT License ยท by [game_ryo](https://github.com/ryopc)
4
+
5
+ > A terminal-native, decentralized social network powered by ed25519 cryptography.
6
+
7
+ ---
8
+
9
+ ## โœจ Features
10
+
11
+ - **๐Ÿ” Decentralized Auth** โ€” ed25519 keypair authentication (Nostr-inspired)
12
+ - **๐Ÿ“ Posts** โ€” Create and view posts on the koshi board
13
+ - **๐Ÿ‘ฅ Follow System** โ€” Follow/unfollow other users
14
+ - **โœ‰๏ธ Direct Messages** โ€” Signed, private DMs
15
+ - **๐Ÿ“ก Real-time** โ€” WebSocket-powered live updates
16
+ - **๐Ÿ’ป Terminal-native** โ€” Beautiful CLI with chalk colors and spinners
17
+ - **๐Ÿณ Docker-ready** โ€” Containerized for easy deployment
18
+ - **โ˜๏ธ Render.com** โ€” One-click deploy to Render.com
19
+ - **๐Ÿ—„๏ธ Neon.tech** โ€” Serverless PostgreSQL 15 database
20
+
21
+ ---
22
+
23
+ ## ๐Ÿ“‹ Table of Contents
24
+
25
+ - [Quick Start](#quick-start)
26
+ - [Installation](#installation)
27
+ - [CLI Usage](#cli-usage)
28
+ - [Server Setup](#server-setup)
29
+ - [API Reference](#api-reference)
30
+ - [Deployment](#deployment)
31
+ - [Development](#development)
32
+ - [Security](#security)
33
+ - [License](#license)
34
+
35
+ ---
36
+
37
+ ## ๐Ÿš€ Quick Start
38
+
39
+ ```bash
40
+ # 1. Start the server (requires PostgreSQL)
41
+ DATABASE_URL=postgresql://user:pass@localhost:5432/koshi \
42
+ JWT_SECRET=$(node -e "console.log(require('crypto').randomBytes(32).toString('hex'))") \
43
+ node bin/server.js
44
+
45
+ # 2. Register a new account
46
+ kb register alice
47
+
48
+ # 3. Post to the koshi board
49
+ kb post "Hello, koshi! ๐ŸŒŠ"
50
+
51
+ # 4. View your feed
52
+ kb feed
53
+
54
+ # 5. Start the real-time stream
55
+ kb realtime
56
+ ```
57
+
58
+ ---
59
+
60
+ ## ๐Ÿ“ฆ Installation
61
+
62
+ ### Global Install (CLI only)
63
+
64
+ ```bash
65
+ # From npm (coming soon)
66
+ npm install -g @ryopc/koshi
67
+
68
+ # Or from source
69
+ git clone https://github.com/ryopc/koshi.git
70
+ cd koshi
71
+ npm install
72
+ npm link
73
+ ```
74
+
75
+ ### Dependencies
76
+
77
+ - **Node.js** >= 18.0.0
78
+ - **PostgreSQL** >= 15.0 (hosted on [Neon.tech](https://neon.tech) in production)
79
+
80
+ ---
81
+
82
+ ## ๐Ÿ’ป CLI Usage
83
+
84
+ The `kb` command is your gateway to the koshi board.
85
+
86
+ ### Commands
87
+
88
+ | Command | Description |
89
+ |---------|-------------|
90
+ | `kb register <username>` | Create a new account with ed25519 keypair |
91
+ | `kb login <username>` | Authenticate using existing keypair |
92
+ | `kb whoami` | Show your profile information |
93
+ | `kb post <message>` | Create a new post on the koshi board |
94
+ | `kb feed [--limit=20]` | Display your post feed |
95
+ | `kb follow <username>` | Follow a user |
96
+ | `kb unfollow <username>` | Unfollow a user |
97
+ | `kb dm <username> <message>` | Send a direct message |
98
+ | `kb dms [--unread]` | View your direct messages |
99
+ | `kb profile [username]` | View a user profile |
100
+ | `kb search <query>` | Search users by username |
101
+ | `kb realtime` | Connect to the real-time event stream |
102
+ | `kb help [command]` | Show help |
103
+
104
+ ### Examples
105
+
106
+ ```bash
107
+ # Register a new user
108
+ kb register alice
109
+
110
+ # Login with existing keys
111
+ kb login alice
112
+
113
+ # Post something
114
+ kb post "Just joined koshi! ๐ŸŒŠ"
115
+
116
+ # View feed
117
+ kb feed --limit=30
118
+
119
+ # Follow someone
120
+ kb follow bob
121
+
122
+ # Send a DM
123
+ kb dm bob "Hey, how's it going?"
124
+
125
+ # Search for users
126
+ kb search alice
127
+
128
+ # Live stream
129
+ kb realtime
130
+ ```
131
+
132
+ ### Configuration
133
+
134
+ Credentials are stored in `~/.config/koshi/config.json` and `~/.snsrc`.
135
+
136
+ Environment variables:
137
+
138
+ | Variable | Default | Description |
139
+ |----------|---------|-------------|
140
+ | `KOSHI_API_URL` | `https://koshi-api.ryopc.f5.si` | API base URL |
141
+ | `KOSHI_WS_URL` | `wss://koshi-api.ryopc.f5.si` | WebSocket URL |
142
+
143
+ ---
144
+
145
+ ## ๐Ÿ–ฅ๏ธ Server Setup
146
+
147
+ ### Local Development
148
+
149
+ ```bash
150
+ # 1. Clone and install
151
+ git clone https://github.com/ryopc/koshi.git
152
+ cd koshi
153
+ npm install
154
+
155
+ # 2. Set up PostgreSQL database
156
+ createdb koshi
157
+
158
+ # 3. Run database migrations
159
+ DATABASE_URL=postgresql://postgres:postgres@localhost:5432/koshi \
160
+ JWT_SECRET=dev-secret-change-in-production \
161
+ node src/db/migrate.js
162
+
163
+ # 4. Start the server
164
+ DATABASE_URL=postgresql://postgres:postgres@localhost:5432/koshi \
165
+ JWT_SECRET=dev-secret-change-in-production \
166
+ node bin/server.js
167
+ ```
168
+
169
+ Or use the .env file:
170
+
171
+ ```bash
172
+ cp .env.example .env
173
+ # Edit .env with your database credentials
174
+ npm run migrate
175
+ npm start
176
+ ```
177
+
178
+ ### Environment Variables
179
+
180
+ | Variable | Required | Description |
181
+ |----------|----------|-------------|
182
+ | `DATABASE_URL` | โœ… | PostgreSQL connection string |
183
+ | `JWT_SECRET` | โœ… | Secret key for JWT signing |
184
+ | `PORT` | โŒ | Server port (default: 3000) |
185
+ | `NODE_ENV` | โŒ | `development` or `production` |
186
+ | `LOG_LEVEL` | โŒ | Log level (default: debug in dev, info in prod) |
187
+
188
+ ---
189
+
190
+ ## ๐Ÿ“ก API Reference
191
+
192
+ ### Base URL
193
+
194
+ Development: `http://localhost:3000/api`
195
+ Production: `https://koshi-api.ryopc.f5.si/api`
196
+
197
+ ### Authentication
198
+
199
+ All authenticated endpoints require a JWT Bearer token:
200
+
201
+ ```
202
+ Authorization: Bearer <token>
203
+ ```
204
+
205
+ ### Endpoints
206
+
207
+ #### Auth
208
+
209
+ | Method | Path | Auth | Description |
210
+ |--------|------|------|-------------|
211
+ | POST | `/api/auth/register` | No | Register a new user |
212
+ | POST | `/api/auth/login` | No | Login with signature |
213
+
214
+ #### Users
215
+
216
+ | Method | Path | Auth | Description |
217
+ |--------|------|------|-------------|
218
+ | GET | `/api/users/:username` | No | Get user profile |
219
+ | PUT | `/api/users/me` | Yes | Update own profile |
220
+ | GET | `/api/users/:id/followers` | No | Get followers |
221
+ | GET | `/api/users/:id/following` | No | Get following |
222
+ | POST | `/api/users/:id/follow` | Yes | Follow a user |
223
+ | DELETE | `/api/users/:id/follow` | Yes | Unfollow a user |
224
+ | GET | `/api/users/search/:query` | No | Search users |
225
+
226
+ #### Posts
227
+
228
+ | Method | Path | Auth | Description |
229
+ |--------|------|------|-------------|
230
+ | GET | `/api/posts/feed` | Optional | Get post feed |
231
+ | POST | `/api/posts` | Yes | Create a post |
232
+ | GET | `/api/posts/:id` | No | Get a single post |
233
+
234
+ #### DMs
235
+
236
+ | Method | Path | Auth | Description |
237
+ |--------|------|------|-------------|
238
+ | GET | `/api/dms` | Yes | Get DM inbox |
239
+ | POST | `/api/dms/:userId` | Yes | Send a DM |
240
+ | PUT | `/api/dms/:id/read` | Yes | Mark DM as read |
241
+ | GET | `/api/dms/unread/count` | Yes | Count unread DMs |
242
+
243
+ #### WebSocket
244
+
245
+ Connect: `ws://host:port/ws?token={jwt}`
246
+
247
+ Events: `message_sent`, `dm_received`, `user_online`, `user_offline`, `post_created`, `follow_notification`
248
+
249
+ #### Health
250
+
251
+ | Method | Path | Description |
252
+ |--------|------|-------------|
253
+ | GET | `/api/health` | Health check |
254
+
255
+ ---
256
+
257
+ ## โ˜๏ธ Deployment
258
+
259
+ ### Render.com + Neon.tech (Recommended)
260
+
261
+ #### 1. Set up Neon.tech Database
262
+
263
+ 1. Go to [Neon.tech](https://neon.tech) and create an account
264
+ 2. Create a new project (PostgreSQL 15)
265
+ 3. Get your connection string from **Connection Details** โ†’ `DATABASE_URL`
266
+ 4. For production, use the **pooled connection string** (with `?pgbouncer=true`)
267
+
268
+ #### 2. Deploy on Render.com
269
+
270
+ 1. Push this repo to GitHub
271
+ 2. Go to [Render.com](https://render.com) and connect your GitHub repo
272
+ 3. Render will auto-detect [`render.yaml`](render.yaml) (Blueprint) and create the service
273
+ 4. In Render dashboard, add environment variables:
274
+ - `DATABASE_URL` โ€” your Neon.tech connection string
275
+ - `JWT_SECRET` โ€” generate with: `node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"`
276
+ 5. Render will auto-deploy on every push to `main`
277
+
278
+ #### Alternative: Manual Setup on Render
279
+
280
+ If you prefer not to use Blueprint:
281
+
282
+ 1. Create a new **Web Service** on Render
283
+ 2. Connect your GitHub repo
284
+ 3. Configure:
285
+ - **Name**: `koshi-api`
286
+ - **Environment**: `Node`
287
+ - **Build Command**: `npm ci`
288
+ - **Start Command**: `node bin/server.js`
289
+ - **Health Check Path**: `/api/health`
290
+ - **Pre-Deploy Command**: `node src/db/migrate.js`
291
+ 4. Add environment variables (see above)
292
+ 5. Deploy!
293
+
294
+ ### Docker
295
+
296
+ ```bash
297
+ # Build the image
298
+ docker build -t koshi-api .
299
+
300
+ # Run the container
301
+ docker run -d \
302
+ --name koshi-api \
303
+ -p 3000:3000 \
304
+ -e DATABASE_URL=postgresql://... \
305
+ -e JWT_SECRET=... \
306
+ koshi-api
307
+ ```
308
+
309
+ ### Docker Compose
310
+
311
+ ```yaml
312
+ version: '3.8'
313
+ services:
314
+ db:
315
+ image: postgres:15-alpine
316
+ environment:
317
+ POSTGRES_DB: koshi
318
+ POSTGRES_PASSWORD: postgres
319
+ volumes:
320
+ - pgdata:/var/lib/postgresql/data
321
+
322
+ api:
323
+ build: .
324
+ ports:
325
+ - "3000:3000"
326
+ environment:
327
+ DATABASE_URL: postgresql://postgres:postgres@db:5432/koshi
328
+ JWT_SECRET: change-this-in-production
329
+ depends_on:
330
+ - db
331
+
332
+ volumes:
333
+ pgdata:
334
+ ```
335
+
336
+ ---
337
+
338
+ ## ๐Ÿ”’ Security
339
+
340
+ - **Ed25519 Signatures** โ€” All posts and DMs are signed for authenticity
341
+ - **JWT Auth** โ€” Tokens expire after 24 hours
342
+ - **Rate Limiting** โ€” Auth endpoints limited to 10 req/min per IP
343
+ - **SQL Injection Prevention** โ€” Parameterized queries throughout
344
+ - **Input Validation** โ€” All endpoints validate input
345
+ - **Helmet** โ€” Security headers enabled
346
+ - **No Hardcoded Secrets** โ€” Everything via environment variables
347
+ - **CORS** โ€” Restricted to CLI client origins
348
+
349
+ ---
350
+
351
+ ## ๐Ÿงช Development
352
+
353
+ ```bash
354
+ # Run tests
355
+ npm test
356
+
357
+ # Run linter
358
+ npm run lint
359
+
360
+ # Run with auto-reload
361
+ npm run dev
362
+
363
+ # Run migration
364
+ npm run migrate
365
+ ```
366
+
367
+ ### Project Structure
368
+
369
+ ```
370
+ koshi/
371
+ โ”œโ”€โ”€ bin/
372
+ โ”‚ โ”œโ”€โ”€ cli.js # CLI/TUI entry point
373
+ โ”‚ โ””โ”€โ”€ server.js # Express + WebSocket server
374
+ โ”œโ”€โ”€ src/
375
+ โ”‚ โ”œโ”€โ”€ api/
376
+ โ”‚ โ”‚ โ”œโ”€โ”€ auth.js # Auth routes (register/login)
377
+ โ”‚ โ”‚ โ”œโ”€โ”€ users.js # User management routes
378
+ โ”‚ โ”‚ โ”œโ”€โ”€ posts.js # Posts routes (koshi board)
379
+ โ”‚ โ”‚ โ””โ”€โ”€ dms.js # Direct messages routes
380
+ โ”‚ โ”œโ”€โ”€ auth/
381
+ โ”‚ โ”‚ โ”œโ”€โ”€ ed25519.js # Ed25519 crypto utilities
382
+ โ”‚ โ”‚ โ”œโ”€โ”€ jwt.js # JWT token utilities
383
+ โ”‚ โ”‚ โ””โ”€โ”€ utils.js # Hex encoding utilities
384
+ โ”‚ โ”œโ”€โ”€ db/
385
+ โ”‚ โ”‚ โ”œโ”€โ”€ schema.sql # PostgreSQL schema
386
+ โ”‚ โ”‚ โ”œโ”€โ”€ migrate.js # Migration script
387
+ โ”‚ โ”‚ โ””โ”€โ”€ pool.js # Database connection pool
388
+ โ”‚ โ”œโ”€โ”€ middleware/
389
+ โ”‚ โ”‚ โ”œโ”€โ”€ auth.js # Auth middleware
390
+ โ”‚ โ”‚ โ””โ”€โ”€ rateLimit.js # Rate limiting middleware
391
+ โ”‚ โ”œโ”€โ”€ ws/
392
+ โ”‚ โ”‚ โ”œโ”€โ”€ index.js # WebSocket server
393
+ โ”‚ โ”‚ โ””โ”€โ”€ handlers.js # WebSocket message handlers
394
+ โ”‚ โ””โ”€โ”€ index.js # Express app setup + logger
395
+ โ”œโ”€โ”€ package.json
396
+ โ”œโ”€โ”€ Dockerfile
397
+ โ”œโ”€โ”€ render.yaml # Render Blueprint (deployment config)
398
+ โ”œโ”€โ”€ .env.example
399
+ โ””โ”€โ”€ .github/workflows/deploy.yml
400
+ ```
401
+
402
+ ---
403
+
404
+ ## ๐Ÿ“„ License
405
+
406
+ MIT โ€” see [LICENSE](LICENSE).
407
+
408
+ ---
409
+
410
+ ## ๐Ÿ™ Acknowledgments
411
+
412
+ - [@noble/ed25519](https://github.com/paulmillr/noble-ed25519) โ€” Pure JS ed25519 implementation
413
+ - [tweetnacl](https://tweetnacl.js.org/) โ€” Cryptographic primitives
414
+ - [Ink](https://github.com/vadimdemedes/ink) โ€” React for CLI
415
+ - [Chalk](https://github.com/chalk/chalk) โ€” Terminal styling
416
+ - [Express](https://expressjs.com/) โ€” Web framework
417
+ - [PostgreSQL](https://www.postgresql.org/) โ€” Database
418
+ - [Render](https://render.com) โ€” Cloud hosting
419
+ - [Neon](https://neon.tech) โ€” Serverless PostgreSQL