@quark-fw/plugin 0.0.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.
@@ -0,0 +1 @@
1
+ import "@quark-fw/core/src/global.js";
package/dist/http.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { type RequestHandler } from "express";
2
+ export declare const preRouter: import("express-serve-static-core").Router;
3
+ export declare const httpRouter: import("express-serve-static-core").Router;
4
+ export declare function registerHttpPreMiddleware(handler: RequestHandler): void;
5
+ export declare function registerHttpMiddleware(path: string, handler: RequestHandler): void;
package/dist/http.js ADDED
@@ -0,0 +1,16 @@
1
+ import { Router } from "express";
2
+ // Живой реестр HTTP-middleware: плагины регистрируют маршруты в любой
3
+ // момент (до или после старта сервера) — react-плагин монтирует роутеры
4
+ // один раз, а express позволяет дописывать в них маршруты после mount.
5
+ // Импортировать ТОЛЬКО этот модуль (не index плагина) — он без side-эффектов.
6
+ // preRouter монтируется ПЕРЕД httpRouter: сюда идут сквозные middleware
7
+ // (сессии, passport), которые обязаны отработать до обработчиков вроде
8
+ // /api/trpc — независимо от порядка импорта плагинов.
9
+ export const preRouter = Router();
10
+ export const httpRouter = Router();
11
+ export function registerHttpPreMiddleware(handler) {
12
+ preRouter.use(handler);
13
+ }
14
+ export function registerHttpMiddleware(path, handler) {
15
+ httpRouter.use(path, handler);
16
+ }
@@ -0,0 +1,14 @@
1
+ import { Atom } from "@quark-fw/atom";
2
+ export interface QuarkPlugin {
3
+ name: string;
4
+ dependsOn?: string[];
5
+ atoms?: Record<string, Atom<unknown>>;
6
+ events?: Partial<Events>;
7
+ extend?(quark: Quark): void;
8
+ onInit?(quark: Quark): void | Promise<void>;
9
+ onReady?(quark: Quark): void | Promise<void>;
10
+ onDestroy?(quark: Quark): void | Promise<void>;
11
+ onEvent?: {
12
+ [K in keyof Events]?: (...args: Events[K]) => void;
13
+ };
14
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { QuarkPlugin } from "./index.js";
2
+ export declare function loadPlugins(quark: Quark, plugins: QuarkPlugin[]): Promise<void>;
@@ -0,0 +1,42 @@
1
+ export async function loadPlugins(quark, plugins) {
2
+ const loaded = new Set();
3
+ async function load(plugin) {
4
+ if (loaded.has(plugin.name))
5
+ return;
6
+ // загружаем зависимости
7
+ if (plugin.dependsOn) {
8
+ for (const dep of plugin.dependsOn) {
9
+ const found = plugins.find((p) => p.name === dep);
10
+ if (!found)
11
+ throw new Error(`Plugin ${plugin.name} depends on missing plugin ${dep}`);
12
+ await load(found);
13
+ }
14
+ }
15
+ // Добавляем атомы: имена известны только в рантайме, объявлены они в
16
+ // аугментации Quark — статически связать нельзя.
17
+ if (plugin.atoms) {
18
+ const target = quark;
19
+ for (const [key, atom] of Object.entries(plugin.atoms)) {
20
+ target[key] = atom;
21
+ }
22
+ }
23
+ // расширяем quark
24
+ plugin.extend?.(quark);
25
+ // подписки на события
26
+ if (plugin.onEvent) {
27
+ for (const [event, handler] of Object.entries(plugin.onEvent)) {
28
+ // имя события — строка из объекта плагина
29
+ quark.ee.value.on(event, handler);
30
+ }
31
+ }
32
+ // lifecycle
33
+ await plugin.onInit?.(quark);
34
+ loaded.add(plugin.name);
35
+ }
36
+ for (const plugin of plugins) {
37
+ await load(plugin);
38
+ }
39
+ for (const plugin of plugins) {
40
+ await plugin.onReady?.(quark);
41
+ }
42
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@quark-fw/plugin",
3
+ "version": "0.0.1",
4
+ "description": "Plugin contract and loader for the Quark framework",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./package.json": "./package.json",
14
+ "./src/*": "./dist/*",
15
+ "./*": "./dist/*"
16
+ },
17
+ "files": ["dist"],
18
+ "scripts": {
19
+ "dev": "tsx watch src/index.ts",
20
+ "build": "tsc -p tsconfig.build.json && node ../../scripts/postbuild.mjs"
21
+ },
22
+ "keywords": ["quark"],
23
+ "author": "",
24
+ "license": "ISC",
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "dependencies": {
29
+ "@quark-fw/atom": "0.0.1"
30
+ },
31
+ "peerDependencies": {
32
+ "express": "^5.0.0"
33
+ },
34
+ "peerDependenciesMeta": {
35
+ "express": {
36
+ "optional": true
37
+ }
38
+ },
39
+ "devDependencies": {
40
+ "@types/express": "^5.0.6"
41
+ }
42
+ }