@tylix/core 0.1.0 → 0.1.2
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 +30 -0
- package/package.json +13 -6
- package/src/cache/Cache.d.ts +7 -0
- package/src/cache/CacheRecord.d.ts +7 -0
- package/src/channels/ChannelServer.d.ts +15 -0
- package/src/discovery/discoverFeatures.d.ts +20 -0
- package/src/discovery/discoverFeatures.js +34 -22
- package/src/discovery/loadCustomRoutes.d.ts +3 -0
- package/src/discovery/loadCustomRoutes.js +5 -1
- package/src/discovery/registerFeatureRoutes.d.ts +8 -0
- package/src/http/Server.d.ts +13 -0
- package/src/http/enhanceResponse.d.ts +19 -0
- package/src/http/parseBody.d.ts +1 -0
- package/src/http/parseCookies.d.ts +3 -0
- package/src/http/parseQuery.d.ts +1 -0
- package/src/http/requireAuth.d.ts +3 -0
- package/src/http/types.d.ts +19 -0
- package/src/index.d.ts +17 -0
- package/src/queue/JobRecord.d.ts +7 -0
- package/src/queue/dispatch.d.ts +4 -0
- package/src/router/Router.d.ts +29 -0
- package/src/scheduling/Scheduler.d.ts +5 -0
- package/src/types.ts +26 -0
- package/tsconfig.json +13 -0
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# @tylix/core
|
|
2
|
+
|
|
3
|
+
The Tylix framework runtime — HTTP server, routing, feature discovery,
|
|
4
|
+
queues, scheduling, and channels.
|
|
5
|
+
|
|
6
|
+
## What's included
|
|
7
|
+
|
|
8
|
+
- **HTTP** — `Server`, `enhanceResponse`, `parseBody`, `parseQuery`,
|
|
9
|
+
`parseCookies`, `requireAuth`
|
|
10
|
+
- **Routing** — `Router`
|
|
11
|
+
- **Discovery** — `discoverFeatures`, `registerFeatureRoutes`,
|
|
12
|
+
`loadCustomRoutes` — auto-wires generated features into routes
|
|
13
|
+
- **Queue** — `JobRecord`, `dispatch`, run via `tylix queue:work`
|
|
14
|
+
- **Scheduling** — `Scheduler`, run via `tylix schedule:work`
|
|
15
|
+
- **Channels** — `ChannelServer` (WebSocket-based), run via
|
|
16
|
+
`tylix channels:work`
|
|
17
|
+
- **Cache** — `CacheRecord`, `Cache`
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import { Server, Router } from '@tylix/core'
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Most of this runs implicitly under `tylix dev` / `tylix build` rather
|
|
26
|
+
than being wired up by hand.
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tylix/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"test": "node --test src/**/*.test.js"
|
|
8
9
|
},
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@tylix/auth": "^0.1.0",
|
|
12
|
+
"@tylix/compiler": "^0.1.0",
|
|
13
|
+
"@tylix/orm": "^0.1.0",
|
|
14
|
+
"@tylix/shared": "^0.1.0",
|
|
15
|
+
"ws": "^8.18.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^26.1.2",
|
|
19
|
+
"@types/ws": "^8.18.1"
|
|
20
|
+
}
|
|
14
21
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const cache: {
|
|
2
|
+
set(key: string, value: unknown, ttlSeconds?: number | null): Promise<unknown>
|
|
3
|
+
get<T = unknown>(key: string): Promise<T | null>
|
|
4
|
+
has(key: string): Promise<boolean>
|
|
5
|
+
forget(key: string): Promise<boolean>
|
|
6
|
+
clear(prefix?: string | null): Promise<number>
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { WebSocket, WebSocketServer } from 'ws'
|
|
2
|
+
|
|
3
|
+
export interface ChannelHandlers {
|
|
4
|
+
onConnect?: (ws: WebSocket) => void
|
|
5
|
+
onMessage?: (ws: WebSocket, data: unknown) => void
|
|
6
|
+
onDisconnect?: (ws: WebSocket) => void
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export declare class ChannelServer {
|
|
10
|
+
channels: Map<string, { handlers: ChannelHandlers; clients: Set<WebSocket> }>
|
|
11
|
+
wss: WebSocketServer | null
|
|
12
|
+
channel(name: string, handlers: ChannelHandlers): this
|
|
13
|
+
broadcast(name: string, data: unknown): void
|
|
14
|
+
listen(port: number): WebSocketServer
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface FeatureManifest {
|
|
2
|
+
name: string
|
|
3
|
+
version: string
|
|
4
|
+
table: string
|
|
5
|
+
model: string
|
|
6
|
+
controller: string
|
|
7
|
+
validator: string
|
|
8
|
+
language: 'javascript' | 'typescript'
|
|
9
|
+
fields: unknown[]
|
|
10
|
+
relations: unknown[]
|
|
11
|
+
auth: boolean
|
|
12
|
+
permissions: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface DiscoveredFeature {
|
|
16
|
+
manifest: FeatureManifest
|
|
17
|
+
controller: unknown
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export declare function discoverFeatures(baseDir: string): Promise<DiscoveredFeature[]>
|
|
@@ -1,30 +1,42 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import path from
|
|
3
|
-
import { pathToFileURL } from
|
|
1
|
+
import fs from 'node:fs/promises'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { pathToFileURL } from 'node:url'
|
|
4
4
|
|
|
5
5
|
export async function discoverFeatures(baseDir) {
|
|
6
|
-
const featuresDir = path.join(baseDir,
|
|
7
|
-
const exists = await fs
|
|
8
|
-
|
|
6
|
+
const featuresDir = path.join(baseDir, 'app', 'Features')
|
|
7
|
+
const exists = await fs
|
|
8
|
+
.access(featuresDir)
|
|
9
|
+
.then(() => true)
|
|
10
|
+
.catch(() => false)
|
|
11
|
+
if (!exists) return []
|
|
9
12
|
|
|
10
|
-
const entries = await fs.readdir(featuresDir, { withFileTypes: true })
|
|
11
|
-
const features = []
|
|
13
|
+
const entries = await fs.readdir(featuresDir, { withFileTypes: true })
|
|
14
|
+
const features = []
|
|
12
15
|
|
|
13
16
|
for (const entry of entries) {
|
|
14
|
-
if (!entry.isDirectory()) continue
|
|
15
|
-
|
|
16
|
-
const manifestPath = path.join(featuresDir, entry.name,
|
|
17
|
-
const manifestExists = await fs
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
if (!entry.isDirectory()) continue
|
|
18
|
+
|
|
19
|
+
const manifestPath = path.join(featuresDir, entry.name, 'feature.json')
|
|
20
|
+
const manifestExists = await fs
|
|
21
|
+
.access(manifestPath)
|
|
22
|
+
.then(() => true)
|
|
23
|
+
.catch(() => false)
|
|
24
|
+
if (!manifestExists) continue
|
|
25
|
+
|
|
26
|
+
const manifest = JSON.parse(await fs.readFile(manifestPath, 'utf-8'))
|
|
27
|
+
|
|
28
|
+
const ext = manifest.language === 'typescript' ? 'ts' : 'js'
|
|
29
|
+
const controllerPath = path.join(
|
|
30
|
+
baseDir,
|
|
31
|
+
'app',
|
|
32
|
+
'controllers',
|
|
33
|
+
`${manifest.controller}.${ext}`,
|
|
34
|
+
)
|
|
35
|
+
const controllerModule = await import(pathToFileURL(controllerPath).href)
|
|
36
|
+
const ControllerClass = controllerModule[manifest.controller]
|
|
37
|
+
|
|
38
|
+
features.push({ manifest, controller: new ControllerClass() })
|
|
27
39
|
}
|
|
28
40
|
|
|
29
|
-
return features
|
|
41
|
+
return features
|
|
30
42
|
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import fs from 'node:fs/promises'
|
|
2
2
|
import path from 'node:path'
|
|
3
3
|
import { pathToFileURL } from 'node:url'
|
|
4
|
+
import { detectLanguage } from '@tylix/shared'
|
|
4
5
|
|
|
5
6
|
export async function loadCustomRoutes(router, baseDir) {
|
|
6
|
-
const
|
|
7
|
+
const language = await detectLanguage(baseDir)
|
|
8
|
+
const ext = language === 'typescript' ? 'ts' : 'js'
|
|
9
|
+
const routesPath = path.join(baseDir, 'app', 'routes', `web.${ext}`)
|
|
10
|
+
|
|
7
11
|
const exists = await fs
|
|
8
12
|
.access(routesPath)
|
|
9
13
|
.then(() => true)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Router } from '../router/Router.js'
|
|
2
|
+
|
|
3
|
+
export type NotFoundHandler = (req: any, res: any) => boolean | void | Promise<boolean | void>
|
|
4
|
+
|
|
5
|
+
export interface ServerOptions {
|
|
6
|
+
notFoundHandler?: NotFoundHandler | null
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export declare class Server {
|
|
10
|
+
constructor(router: Router, options?: ServerOptions)
|
|
11
|
+
createHandler(): (req: any, res: any) => Promise<void>
|
|
12
|
+
listen(port: number, callback?: () => void): import('node:http').Server
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface CookieOptions {
|
|
2
|
+
maxAge?: number
|
|
3
|
+
expires?: Date
|
|
4
|
+
path?: string
|
|
5
|
+
domain?: string
|
|
6
|
+
httpOnly?: boolean
|
|
7
|
+
secure?: boolean
|
|
8
|
+
sameSite?: 'Lax' | 'Strict' | 'None'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export declare function enhanceResponse<T extends import('node:http').ServerResponse>(
|
|
12
|
+
res: T,
|
|
13
|
+
): T & {
|
|
14
|
+
status(code: number): T
|
|
15
|
+
json(data: unknown): T
|
|
16
|
+
send(data?: unknown): T
|
|
17
|
+
cookie(name: string, value: string, options?: CookieOptions): T
|
|
18
|
+
clearCookie(name: string, options?: CookieOptions): T
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseBody(req: import('node:http').IncomingMessage): Promise<Record<string, unknown>>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseQuery(url: string): Record<string, string>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
3
|
+
import type { CookieOptions } from './enhanceResponse.js'
|
|
4
|
+
|
|
5
|
+
export interface TylixRequest extends IncomingMessage {
|
|
6
|
+
params: Record<string, string>
|
|
7
|
+
query: Record<string, string>
|
|
8
|
+
cookies: Record<string, string>
|
|
9
|
+
body: Record<string, unknown>
|
|
10
|
+
user?: Record<string, unknown>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface TylixResponse extends ServerResponse {
|
|
14
|
+
status(code: number): TylixResponse
|
|
15
|
+
json(data: unknown): TylixResponse
|
|
16
|
+
send(data?: unknown): TylixResponse
|
|
17
|
+
cookie(name: string, value: string, options?: CookieOptions): TylixResponse
|
|
18
|
+
clearCookie(name: string, options?: CookieOptions): TylixResponse
|
|
19
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from './router/Router.js'
|
|
2
|
+
export * from './http/Server.js'
|
|
3
|
+
export * from './http/enhanceResponse.js'
|
|
4
|
+
export * from './http/parseBody.js'
|
|
5
|
+
export * from './http/parseQuery.js'
|
|
6
|
+
export * from './http/parseCookies.js'
|
|
7
|
+
export * from './http/requireAuth.js'
|
|
8
|
+
export * from './discovery/discoverFeatures.js'
|
|
9
|
+
export * from './discovery/registerFeatureRoutes.js'
|
|
10
|
+
export * from './discovery/loadCustomRoutes.js'
|
|
11
|
+
export * from './queue/JobRecord.js'
|
|
12
|
+
export * from './queue/dispatch.js'
|
|
13
|
+
export * from './scheduling/Scheduler.js'
|
|
14
|
+
export * from './channels/ChannelServer.js'
|
|
15
|
+
export * from './cache/CacheRecord.js'
|
|
16
|
+
export * from './cache/Cache.js'
|
|
17
|
+
export * from './http/types.js'
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type Handler = (
|
|
2
|
+
req: any,
|
|
3
|
+
res: any,
|
|
4
|
+
next: () => Promise<void>,
|
|
5
|
+
) => void | Promise<void>
|
|
6
|
+
|
|
7
|
+
interface Route {
|
|
8
|
+
method: string
|
|
9
|
+
pattern: string
|
|
10
|
+
regex: RegExp
|
|
11
|
+
keys: string[]
|
|
12
|
+
routeMiddleware: Handler[]
|
|
13
|
+
finalHandler: Handler
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export declare class Router {
|
|
17
|
+
routes: Route[]
|
|
18
|
+
middlewares: Handler[]
|
|
19
|
+
use(fn: Handler): this
|
|
20
|
+
register(method: string, pattern: string, ...handlers: Handler[]): this
|
|
21
|
+
get(pattern: string, ...handlers: Handler[]): this
|
|
22
|
+
post(pattern: string, ...handlers: Handler[]): this
|
|
23
|
+
put(pattern: string, ...handlers: Handler[]): this
|
|
24
|
+
delete(pattern: string, ...handlers: Handler[]): this
|
|
25
|
+
match(
|
|
26
|
+
method: string,
|
|
27
|
+
url: string,
|
|
28
|
+
): { handler: Handler; params: Record<string, string> } | null
|
|
29
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
|
|
4
|
+
export interface AuthTokenPayload {
|
|
5
|
+
userId: number;
|
|
6
|
+
/** Present only if signToken() included an expiry (most tokens do). */
|
|
7
|
+
exp?: number;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface TylixRequest extends IncomingMessage {
|
|
12
|
+
params: Record<string, string>;
|
|
13
|
+
query: Record<string, string>;
|
|
14
|
+
cookies: Record<string, string>;
|
|
15
|
+
body: unknown;
|
|
16
|
+
/** Set by requireAuth() from the verified token's payload. */
|
|
17
|
+
user?: AuthTokenPayload;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TylixResponse extends ServerResponse {
|
|
21
|
+
status(code: number): TylixResponse;
|
|
22
|
+
json(data: unknown): void;
|
|
23
|
+
send(data?: unknown): TylixResponse;
|
|
24
|
+
cookie(name: string, value: string, options?: Record<string, unknown>): TylixResponse;
|
|
25
|
+
clearCookie(name: string, options?: Record<string, unknown>): TylixResponse;
|
|
26
|
+
}
|
package/tsconfig.json
ADDED