@mobtakronio/capskit 0.1.2 → 0.1.3
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/dist/index.cjs +84 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -9
- package/dist/index.d.ts +2 -9
- package/dist/index.js +83 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -6,7 +6,7 @@ interface CapsuleManifest {
|
|
|
6
6
|
publishes?: string[];
|
|
7
7
|
subscribes?: EventSubscription[];
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
[key: string]: any;
|
|
10
10
|
}
|
|
11
11
|
type ActionPreHook = (payload: any, context: ActionContext) => Promise<void> | void;
|
|
12
12
|
type ActionPostHook = (payload: any, result: any, context: ActionContext) => Promise<any> | any;
|
|
@@ -31,13 +31,6 @@ interface EventSubscription {
|
|
|
31
31
|
event: string;
|
|
32
32
|
action: string;
|
|
33
33
|
}
|
|
34
|
-
interface RouteDefinition {
|
|
35
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
36
|
-
path: string;
|
|
37
|
-
action: string;
|
|
38
|
-
schema?: any;
|
|
39
|
-
traits?: Record<string, any>;
|
|
40
|
-
}
|
|
41
34
|
interface CapsKitConfig {
|
|
42
35
|
capsuleDirs?: string[];
|
|
43
36
|
dependencies?: Record<string, any>;
|
|
@@ -77,4 +70,4 @@ declare function createCapsKit(config: CapsKitConfig): Promise<any>;
|
|
|
77
70
|
|
|
78
71
|
declare function loadCapsules(capsulesDir: string): Promise<CapsuleManifest[]>;
|
|
79
72
|
|
|
80
|
-
export { type ActionContext, type ActionDefinition, type ActionHandler, type ActionInterceptor, type ActionPostHook, type ActionPreHook, CapsKit, type CapsKitConfig, type CapsuleManifest, type EventSubscription, type ICapsKit,
|
|
73
|
+
export { type ActionContext, type ActionDefinition, type ActionHandler, type ActionInterceptor, type ActionPostHook, type ActionPreHook, CapsKit, type CapsKitConfig, type CapsuleManifest, type EventSubscription, type ICapsKit, createCapsKit, loadCapsules };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ interface CapsuleManifest {
|
|
|
6
6
|
publishes?: string[];
|
|
7
7
|
subscribes?: EventSubscription[];
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
[key: string]: any;
|
|
10
10
|
}
|
|
11
11
|
type ActionPreHook = (payload: any, context: ActionContext) => Promise<void> | void;
|
|
12
12
|
type ActionPostHook = (payload: any, result: any, context: ActionContext) => Promise<any> | any;
|
|
@@ -31,13 +31,6 @@ interface EventSubscription {
|
|
|
31
31
|
event: string;
|
|
32
32
|
action: string;
|
|
33
33
|
}
|
|
34
|
-
interface RouteDefinition {
|
|
35
|
-
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
36
|
-
path: string;
|
|
37
|
-
action: string;
|
|
38
|
-
schema?: any;
|
|
39
|
-
traits?: Record<string, any>;
|
|
40
|
-
}
|
|
41
34
|
interface CapsKitConfig {
|
|
42
35
|
capsuleDirs?: string[];
|
|
43
36
|
dependencies?: Record<string, any>;
|
|
@@ -77,4 +70,4 @@ declare function createCapsKit(config: CapsKitConfig): Promise<any>;
|
|
|
77
70
|
|
|
78
71
|
declare function loadCapsules(capsulesDir: string): Promise<CapsuleManifest[]>;
|
|
79
72
|
|
|
80
|
-
export { type ActionContext, type ActionDefinition, type ActionHandler, type ActionInterceptor, type ActionPostHook, type ActionPreHook, CapsKit, type CapsKitConfig, type CapsuleManifest, type EventSubscription, type ICapsKit,
|
|
73
|
+
export { type ActionContext, type ActionDefinition, type ActionHandler, type ActionInterceptor, type ActionPostHook, type ActionPreHook, CapsKit, type CapsKitConfig, type CapsuleManifest, type EventSubscription, type ICapsKit, createCapsKit, loadCapsules };
|
package/dist/index.js
CHANGED
|
@@ -5357,6 +5357,7 @@ var require_dist = __commonJS({
|
|
|
5357
5357
|
|
|
5358
5358
|
// src/kernel/platform.ts
|
|
5359
5359
|
import * as path2 from "path";
|
|
5360
|
+
import { fileURLToPath } from "url";
|
|
5360
5361
|
|
|
5361
5362
|
// src/kernel/loader.ts
|
|
5362
5363
|
import * as fs from "fs";
|
|
@@ -21370,49 +21371,45 @@ function createElysiaRouter(capskit, traitHandlers = {}) {
|
|
|
21370
21371
|
manifests.forEach((manifest) => {
|
|
21371
21372
|
if (manifest.routes) {
|
|
21372
21373
|
manifest.routes.forEach((route) => {
|
|
21374
|
+
const path3 = route.path;
|
|
21375
|
+
let hooks = {};
|
|
21376
|
+
if (route.traits) {
|
|
21377
|
+
hooks.beforeHandle = [];
|
|
21378
|
+
for (const [traitName, traitValue] of Object.entries(route.traits)) {
|
|
21379
|
+
if (traitHandlers[traitName]) {
|
|
21380
|
+
hooks.beforeHandle.push((c) => traitHandlers[traitName](traitValue, c));
|
|
21381
|
+
} else {
|
|
21382
|
+
console.warn(`[HTTP Elysia] No handler provided for trait "${traitName}" on route ${route.method} ${path3}`);
|
|
21383
|
+
}
|
|
21384
|
+
}
|
|
21385
|
+
}
|
|
21373
21386
|
const handler = async ({ body, params, query, set }) => {
|
|
21374
21387
|
try {
|
|
21375
|
-
|
|
21388
|
+
return await capskit.call(`${manifest.name}.${route.action}`, {
|
|
21376
21389
|
body,
|
|
21377
21390
|
params,
|
|
21378
21391
|
query
|
|
21379
21392
|
});
|
|
21380
|
-
return result;
|
|
21381
21393
|
} catch (error) {
|
|
21382
21394
|
set.status = 500;
|
|
21383
21395
|
return { error: error.message };
|
|
21384
21396
|
}
|
|
21385
21397
|
};
|
|
21386
|
-
const path3 = route.path;
|
|
21387
|
-
let config = {};
|
|
21388
|
-
if (route.traits) {
|
|
21389
|
-
const beforeHandle = [];
|
|
21390
|
-
for (const [traitName, traitValue] of Object.entries(route.traits)) {
|
|
21391
|
-
if (traitHandlers[traitName]) {
|
|
21392
|
-
beforeHandle.push(async (c) => traitHandlers[traitName](traitValue, c));
|
|
21393
|
-
} else {
|
|
21394
|
-
console.warn(`[HTTP Elysia] No handler provided for trait "${traitName}" on route ${route.method} ${path3}`);
|
|
21395
|
-
}
|
|
21396
|
-
}
|
|
21397
|
-
if (beforeHandle.length > 0) {
|
|
21398
|
-
config.beforeHandle = beforeHandle;
|
|
21399
|
-
}
|
|
21400
|
-
}
|
|
21401
21398
|
switch (route.method) {
|
|
21402
21399
|
case "GET":
|
|
21403
|
-
app.get(path3, handler,
|
|
21400
|
+
app.get(path3, handler, hooks);
|
|
21404
21401
|
break;
|
|
21405
21402
|
case "POST":
|
|
21406
|
-
app.post(path3, handler,
|
|
21403
|
+
app.post(path3, handler, hooks);
|
|
21407
21404
|
break;
|
|
21408
21405
|
case "PUT":
|
|
21409
|
-
app.put(path3, handler,
|
|
21406
|
+
app.put(path3, handler, hooks);
|
|
21410
21407
|
break;
|
|
21411
21408
|
case "DELETE":
|
|
21412
|
-
app.delete(path3, handler,
|
|
21409
|
+
app.delete(path3, handler, hooks);
|
|
21413
21410
|
break;
|
|
21414
21411
|
case "PATCH":
|
|
21415
|
-
app.patch(path3, handler,
|
|
21412
|
+
app.patch(path3, handler, hooks);
|
|
21416
21413
|
break;
|
|
21417
21414
|
}
|
|
21418
21415
|
});
|
|
@@ -21489,6 +21486,57 @@ var service3 = {
|
|
|
21489
21486
|
}
|
|
21490
21487
|
};
|
|
21491
21488
|
|
|
21489
|
+
// src/capsules/websocket/src/adapters/elysia.ts
|
|
21490
|
+
function createElysiaSocket(capskit) {
|
|
21491
|
+
const manifests = capskit.getManifests();
|
|
21492
|
+
const sockets = {};
|
|
21493
|
+
manifests.forEach((manifest) => {
|
|
21494
|
+
if (manifest.sockets) {
|
|
21495
|
+
manifest.sockets.forEach((socket) => {
|
|
21496
|
+
sockets[socket.path] = {
|
|
21497
|
+
open: socket.open ? async (ws) => {
|
|
21498
|
+
await capskit.call(`${manifest.name}.${socket.open}`, { ws, body: ws.data });
|
|
21499
|
+
} : void 0,
|
|
21500
|
+
message: async (ws, message) => {
|
|
21501
|
+
const result = await capskit.call(`${manifest.name}.${socket.message}`, { ws, body: message });
|
|
21502
|
+
if (result) ws.send(result);
|
|
21503
|
+
},
|
|
21504
|
+
close: socket.close ? async (ws, code, message) => {
|
|
21505
|
+
await capskit.call(`${manifest.name}.${socket.close}`, { ws, code, message });
|
|
21506
|
+
} : void 0,
|
|
21507
|
+
drain: socket.drain ? async (ws) => {
|
|
21508
|
+
await capskit.call(`${manifest.name}.${socket.drain}`, { ws });
|
|
21509
|
+
} : void 0
|
|
21510
|
+
};
|
|
21511
|
+
});
|
|
21512
|
+
}
|
|
21513
|
+
});
|
|
21514
|
+
return sockets;
|
|
21515
|
+
}
|
|
21516
|
+
|
|
21517
|
+
// src/capsules/websocket/src/actions/buildSocket.ts
|
|
21518
|
+
var buildSocket = async (payload, context) => {
|
|
21519
|
+
const { adapter = "elysia" } = payload?.body || payload || {};
|
|
21520
|
+
const capskit = context.deps.capskit;
|
|
21521
|
+
if (adapter === "elysia") {
|
|
21522
|
+
const sockets = createElysiaSocket(capskit);
|
|
21523
|
+
return { sockets };
|
|
21524
|
+
}
|
|
21525
|
+
throw new Error(`Unsupported WebSocket adapter: ${adapter}`);
|
|
21526
|
+
};
|
|
21527
|
+
|
|
21528
|
+
// src/capsules/websocket/manifest.ts
|
|
21529
|
+
var service4 = {
|
|
21530
|
+
name: "websocket",
|
|
21531
|
+
requires: ["capskit"],
|
|
21532
|
+
actions: {
|
|
21533
|
+
buildSocket: {
|
|
21534
|
+
handler: buildSocket,
|
|
21535
|
+
description: "Returns a WebSocket configuration containing all platform capabilities mapped to WebSockets"
|
|
21536
|
+
}
|
|
21537
|
+
}
|
|
21538
|
+
};
|
|
21539
|
+
|
|
21492
21540
|
// src/kernel/platform.ts
|
|
21493
21541
|
var CapsKit = class {
|
|
21494
21542
|
constructor(config) {
|
|
@@ -21507,6 +21555,19 @@ var CapsKit = class {
|
|
|
21507
21555
|
this.registerCapsule(service);
|
|
21508
21556
|
this.registerCapsule(service2);
|
|
21509
21557
|
this.registerCapsule(service3);
|
|
21558
|
+
this.registerCapsule(service4);
|
|
21559
|
+
try {
|
|
21560
|
+
const currentDir = path2.dirname(fileURLToPath(import.meta.url));
|
|
21561
|
+
const builtinDir = path2.resolve(currentDir, "../capsules");
|
|
21562
|
+
const builtinManifests = await loadCapsules(builtinDir);
|
|
21563
|
+
for (const manifest of builtinManifests) {
|
|
21564
|
+
if (!this.manifests.has(manifest.name)) {
|
|
21565
|
+
this.registerCapsule(manifest);
|
|
21566
|
+
}
|
|
21567
|
+
}
|
|
21568
|
+
} catch (error) {
|
|
21569
|
+
console.warn("[CapsKit] Could not auto-load built-in capsules directory:", error);
|
|
21570
|
+
}
|
|
21510
21571
|
if (this.config.capsuleDirs) {
|
|
21511
21572
|
for (const dir of this.config.capsuleDirs) {
|
|
21512
21573
|
const absoluteDir = path2.resolve(dir);
|