ttc-origin-server 0.1.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 +21 -0
- package/README.md +157 -0
- package/package.json +63 -0
- package/src/index.ts +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tentarcles
|
|
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,157 @@
|
|
|
1
|
+
# TTC Origin Server
|
|
2
|
+
|
|
3
|
+
A lightweight, decorator‑based RPC server for the Tentarcles Origins platform. Quickly expose TypeScript classes as RPC endpoints with Zod validation and automatic API discovery.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Decorator‑based endpoints** – Use `@ttc.describe()` to expose methods
|
|
8
|
+
- **Zod validation** – Type‑safe input/output schemas
|
|
9
|
+
- **Automatic API discovery** – `/rpc/functions` endpoint lists all available methods
|
|
10
|
+
- **Authentication hooks** – Customizable auth middleware
|
|
11
|
+
- **Express‑compatible** – Use your own Express app or let the library create one
|
|
12
|
+
- **Bun/Node.js** – Runs on Bun (recommended) or Node.js
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# Using Bun
|
|
18
|
+
bun add ttc-origin-server
|
|
19
|
+
|
|
20
|
+
# Using npm
|
|
21
|
+
npm install ttc-origin-server
|
|
22
|
+
|
|
23
|
+
# Using yarn
|
|
24
|
+
yarn add ttc-origin-server
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
1. **Create a module**
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// src/modules/hello.ts
|
|
33
|
+
import { ttc } from 'ttc-rpc';
|
|
34
|
+
import { z } from 'zod';
|
|
35
|
+
|
|
36
|
+
export class HelloService {
|
|
37
|
+
@ttc.describe({
|
|
38
|
+
name: 'greet',
|
|
39
|
+
description: 'Returns a personalized greeting',
|
|
40
|
+
input: z.object({
|
|
41
|
+
name: z.string().optional().default('World')
|
|
42
|
+
}),
|
|
43
|
+
output: z.string()
|
|
44
|
+
})
|
|
45
|
+
greet(input: { name?: string }): string {
|
|
46
|
+
return `Hello, ${input.name}!`;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
2. **Start the server**
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// src/index.ts
|
|
55
|
+
import { runServer } from 'ttc-origin-server';
|
|
56
|
+
import { HelloService } from './modules/hello';
|
|
57
|
+
|
|
58
|
+
runServer({
|
|
59
|
+
name: 'my-service',
|
|
60
|
+
description: 'A demo TTC Origins service',
|
|
61
|
+
modules: [HelloService],
|
|
62
|
+
port: 3000,
|
|
63
|
+
authCb: async (req) => {
|
|
64
|
+
// Your authentication logic
|
|
65
|
+
return true; // Allow all requests
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
3. **Run it**
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
bun run src/index.ts
|
|
74
|
+
# or with hot reload
|
|
75
|
+
bun run dev
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API Reference
|
|
79
|
+
|
|
80
|
+
### `runServer(config)`
|
|
81
|
+
|
|
82
|
+
Starts the TTC Origin server.
|
|
83
|
+
|
|
84
|
+
**Configuration:**
|
|
85
|
+
|
|
86
|
+
| Option | Type | Description |
|
|
87
|
+
|--------|------|-------------|
|
|
88
|
+
| `name` | `string` | Service name (spaces replaced with underscores) |
|
|
89
|
+
| `description` | `string` | Optional service description |
|
|
90
|
+
| `modules` | `any[]` | Array of classes with `@ttc.describe` decorators |
|
|
91
|
+
| `port` | `number` | Port to listen on |
|
|
92
|
+
| `authCb` | `(req) => Promise<boolean>` | Authentication callback |
|
|
93
|
+
| `app` | `express.Express` | Optional existing Express app |
|
|
94
|
+
| `requireAuth` | `boolean` | Whether authentication is required |
|
|
95
|
+
| `modifyable` | `boolean` | If `true`, exposes the working directory |
|
|
96
|
+
|
|
97
|
+
### Endpoints
|
|
98
|
+
|
|
99
|
+
- `GET /rpc/info` – Returns service metadata
|
|
100
|
+
- `GET /rpc/functions` – Lists all available RPC methods with schemas
|
|
101
|
+
- `POST /rpc/:module/:method` – Invokes an RPC method (handled by `ttc-rpc`)
|
|
102
|
+
|
|
103
|
+
## Example Module
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { ttc } from 'ttc-rpc';
|
|
107
|
+
import { z } from 'zod';
|
|
108
|
+
|
|
109
|
+
export class Calculator {
|
|
110
|
+
@ttc.describe({
|
|
111
|
+
name: 'add',
|
|
112
|
+
description: 'Adds two numbers',
|
|
113
|
+
input: z.object({
|
|
114
|
+
a: z.number(),
|
|
115
|
+
b: z.number()
|
|
116
|
+
}),
|
|
117
|
+
output: z.number()
|
|
118
|
+
})
|
|
119
|
+
add(input: { a: number; b: number }): number {
|
|
120
|
+
return input.a + input.b;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@ttc.describe({
|
|
124
|
+
name: 'multiply',
|
|
125
|
+
description: 'Multiplies two numbers',
|
|
126
|
+
input: z.object({
|
|
127
|
+
a: z.number(),
|
|
128
|
+
b: z.number()
|
|
129
|
+
}),
|
|
130
|
+
output: z.number()
|
|
131
|
+
})
|
|
132
|
+
multiply(input: { a: number; b: number }): number {
|
|
133
|
+
return input.a * input.b;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Development
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
# Clone the repository
|
|
142
|
+
git clone https://github.com/tentarcles/ttc-origin-server.git
|
|
143
|
+
cd ttc-origin-server
|
|
144
|
+
|
|
145
|
+
# Install dependencies
|
|
146
|
+
bun install
|
|
147
|
+
|
|
148
|
+
# Run in development mode (hot reload)
|
|
149
|
+
bun run dev
|
|
150
|
+
|
|
151
|
+
# Build for production
|
|
152
|
+
bun run build
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
MIT © Tentarcles
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ttc-origin-server",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lightweight server for TTC Origins - run RPC services with decorator-based endpoints",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "src/index.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"private": false,
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"src",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"start": "bun run src/index.ts",
|
|
17
|
+
"dev": "bun --watch src/index.ts",
|
|
18
|
+
"test": "bun run src/modules/logModule.ts",
|
|
19
|
+
"build": "tsc",
|
|
20
|
+
"prepublishOnly": "bun run build"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ttc",
|
|
24
|
+
"origins",
|
|
25
|
+
"rpc",
|
|
26
|
+
"server",
|
|
27
|
+
"decorators",
|
|
28
|
+
"typescript",
|
|
29
|
+
"express"
|
|
30
|
+
],
|
|
31
|
+
"author": "Tentarcles",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/tentarcles/ttc-origin-server.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/tentarcles/ttc-origin-server/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/tentarcles/ttc-origin-server#readme",
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"express": "^5.2.1",
|
|
46
|
+
"reflect-metadata": "^0.2.2",
|
|
47
|
+
"ttc-rpc": "^2.1.4",
|
|
48
|
+
"zod": "^4.3.6"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/bun": "latest",
|
|
52
|
+
"@types/express": "^5.0.0",
|
|
53
|
+
"@types/node": "^20.0.0",
|
|
54
|
+
"typescript": "^5.0.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"typescript": "^5"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=18.0.0",
|
|
61
|
+
"bun": ">=1.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { generateRPCMethodsInfo, ttc, type RPCMethodInfo } from 'ttc-rpc';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export const runServer = (config: {
|
|
7
|
+
name: string,
|
|
8
|
+
description?: string,
|
|
9
|
+
modules: any[],
|
|
10
|
+
port: number,
|
|
11
|
+
requireAuth?: boolean,
|
|
12
|
+
modifyable?: boolean,
|
|
13
|
+
authCb: (req: any) => Promise<boolean>,
|
|
14
|
+
app?: express.Express
|
|
15
|
+
}) => {
|
|
16
|
+
const { modules, port, app } = config;
|
|
17
|
+
const appInstance = app || express();
|
|
18
|
+
|
|
19
|
+
config.name = config.name.replace(/\s+/g, '_').toLowerCase();
|
|
20
|
+
|
|
21
|
+
// authenticate to the ttc server
|
|
22
|
+
// the server will get the definition and Qualities and url etc
|
|
23
|
+
appInstance.get('/rpc/info', (req, res) => {
|
|
24
|
+
res.json({
|
|
25
|
+
name: config.name,
|
|
26
|
+
description: config.description || '',
|
|
27
|
+
requireAuth: config.requireAuth,
|
|
28
|
+
directory: config.modifyable ? process.cwd() : undefined
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
appInstance.get('/rpc/functions', (req, res) => {
|
|
33
|
+
const queryParams = req.query;
|
|
34
|
+
const assistant = queryParams.assistant as string | undefined;
|
|
35
|
+
console.log(`${assistant} is accessing the RPC definitions..`);
|
|
36
|
+
const info: Record<string, RPCMethodInfo[]> = generateRPCMethodsInfo();
|
|
37
|
+
const finalFunctions: RPCMethodInfo[] = [];
|
|
38
|
+
Object.keys(info).forEach((module: string) => {
|
|
39
|
+
const functions = info[module]?.map((func: RPCMethodInfo) => {
|
|
40
|
+
return {
|
|
41
|
+
...func,
|
|
42
|
+
name: `${config.name}.${module}.${func.name}`
|
|
43
|
+
}
|
|
44
|
+
}) || [];
|
|
45
|
+
finalFunctions.push(...functions);
|
|
46
|
+
});
|
|
47
|
+
console.log(JSON.stringify(finalFunctions, null, 2));
|
|
48
|
+
res.json({
|
|
49
|
+
functions: finalFunctions
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
ttc.init({
|
|
54
|
+
app: appInstance,
|
|
55
|
+
modules: modules,
|
|
56
|
+
generate_client: false,
|
|
57
|
+
authCb: async (req) => {
|
|
58
|
+
// authorization for fetching functions will come through here
|
|
59
|
+
if(config.authCb){
|
|
60
|
+
return await config.authCb(req);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
socketCb: async (socket) => { }
|
|
64
|
+
}).listen(port);
|
|
65
|
+
}
|