miqro 6.1.2 → 6.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/build/esm/editor/auth.d.ts +6 -0
- package/build/esm/editor/auth.js +41 -0
- package/build/esm/editor/common/admin-interface.d.ts +36 -0
- package/build/esm/editor/common/admin-interface.js +44 -0
- package/build/esm/editor/common/constants.d.ts +4 -0
- package/build/esm/editor/common/constants.js +20 -0
- package/build/esm/editor/common/constants.server.d.ts +2 -0
- package/build/esm/editor/common/constants.server.js +4 -0
- package/build/esm/editor/common/editor-index.d.ts +2 -0
- package/build/esm/editor/common/editor-index.js +14 -0
- package/build/esm/editor/common/html-encode.d.ts +1 -0
- package/build/esm/editor/common/html-encode.js +14 -0
- package/build/esm/editor/common/log-socket.d.ts +15 -0
- package/build/esm/editor/common/log-socket.js +70 -0
- package/build/esm/editor/common/templates.d.ts +11 -0
- package/build/esm/editor/common/templates.js +461 -0
- package/build/esm/editor/components/api-preview.d.ts +11 -0
- package/build/esm/editor/components/api-preview.js +90 -0
- package/build/esm/editor/components/editor.d.ts +16 -0
- package/build/esm/editor/components/editor.js +357 -0
- package/build/esm/editor/components/file-browser.d.ts +37 -0
- package/build/esm/editor/components/file-browser.js +126 -0
- package/build/esm/editor/components/file-editor-toolbar.d.ts +22 -0
- package/build/esm/editor/components/file-editor-toolbar.js +93 -0
- package/build/esm/editor/components/file-editor.d.ts +32 -0
- package/build/esm/editor/components/file-editor.js +59 -0
- package/build/esm/editor/components/filter-query.d.ts +1 -0
- package/build/esm/editor/components/filter-query.js +22 -0
- package/build/esm/editor/components/highlight-text-area.d.ts +11 -0
- package/build/esm/editor/components/highlight-text-area.js +124 -0
- package/build/esm/editor/components/log-viewer.d.ts +6 -0
- package/build/esm/editor/components/log-viewer.js +69 -0
- package/build/esm/editor/components/new-file.d.ts +10 -0
- package/build/esm/editor/components/new-file.js +117 -0
- package/build/esm/editor/components/scroll-query.d.ts +7 -0
- package/build/esm/editor/components/scroll-query.js +21 -0
- package/build/esm/editor/components/start-page.d.ts +13 -0
- package/build/esm/editor/components/start-page.js +30 -0
- package/build/esm/editor/http/admin/editor/api/fs/delete.api.d.ts +3 -0
- package/build/esm/editor/http/admin/editor/api/fs/delete.api.js +29 -0
- package/build/esm/editor/http/admin/editor/api/fs/read.api.d.ts +5 -0
- package/build/esm/editor/http/admin/editor/api/fs/read.api.js +49 -0
- package/build/esm/editor/http/admin/editor/api/fs/rename.api.d.ts +4 -0
- package/build/esm/editor/http/admin/editor/api/fs/rename.api.js +39 -0
- package/build/esm/editor/http/admin/editor/api/fs/scan.api.d.ts +26 -0
- package/build/esm/editor/http/admin/editor/api/fs/scan.api.js +149 -0
- package/build/esm/editor/http/admin/editor/api/fs/write.api.d.ts +3 -0
- package/build/esm/editor/http/admin/editor/api/fs/write.api.js +38 -0
- package/build/esm/editor/http/admin/editor/api/server/reload.api.d.ts +10 -0
- package/build/esm/editor/http/admin/editor/api/server/reload.api.js +46 -0
- package/build/esm/editor/http/admin/editor/api/server/restart.api.d.ts +10 -0
- package/build/esm/editor/http/admin/editor/api/server/restart.api.js +45 -0
- package/build/esm/editor/http/admin/editor/editor.d.ts +1 -0
- package/build/esm/editor/http/admin/editor/editor.js +7 -0
- package/build/esm/editor/http/admin/editor/index.api.d.ts +3 -0
- package/build/esm/editor/http/admin/editor/index.api.js +21 -0
- package/build/esm/editor/server.d.ts +3 -0
- package/build/esm/editor/server.js +49 -0
- package/build/esm/editor/ws.d.ts +3 -0
- package/build/esm/editor/ws.js +11 -0
- package/editor/auth.ts +51 -0
- package/editor/common/admin-interface.ts +84 -0
- package/editor/common/constants.server.ts +5 -0
- package/editor/common/constants.ts +21 -0
- package/editor/common/editor-index.tsx +17 -0
- package/editor/common/html-encode.ts +14 -0
- package/editor/common/log-socket.tsx +85 -0
- package/editor/common/templates.ts +466 -0
- package/editor/components/api-preview.tsx +116 -0
- package/editor/components/editor.tsx +486 -0
- package/editor/components/file-browser.tsx +308 -0
- package/editor/components/file-editor-toolbar.tsx +191 -0
- package/editor/components/file-editor.tsx +122 -0
- package/editor/components/filter-query.tsx +22 -0
- package/editor/components/highlight-text-area.tsx +144 -0
- package/editor/components/log-viewer.tsx +110 -0
- package/editor/components/new-file.tsx +169 -0
- package/editor/components/scroll-query.tsx +22 -0
- package/editor/components/start-page.tsx +49 -0
- package/editor/http/admin/editor/api/fs/delete.api.tsx +32 -0
- package/editor/http/admin/editor/api/fs/read.api.tsx +55 -0
- package/editor/http/admin/editor/api/fs/rename.api.tsx +41 -0
- package/editor/http/admin/editor/api/fs/scan.api.tsx +181 -0
- package/editor/http/admin/editor/api/fs/write.api.tsx +41 -0
- package/editor/http/admin/editor/api/server/reload.api.ts +53 -0
- package/editor/http/admin/editor/api/server/restart.api.tsx +52 -0
- package/editor/http/admin/editor/editor.tsx +8 -0
- package/editor/http/admin/editor/index.api.tsx +39 -0
- package/editor/server.ts +57 -0
- package/editor/ws.ts +15 -0
- package/package.json +1 -1
- package/src/bin/compile.ts +35 -0
- package/src/bin/doc-md.ts +210 -0
- package/src/bin/generate-doc.ts +64 -0
- package/src/bin/test.ts +92 -0
- package/src/bin/types.ts +22 -0
- package/src/cluster.ts +27 -0
- package/src/common/arguments.ts +503 -0
- package/src/common/assets.ts +128 -0
- package/src/common/checksum.ts +58 -0
- package/src/common/constants.ts +18 -0
- package/src/common/content-type.ts +84 -0
- package/src/common/esbuild.ts +94 -0
- package/src/common/exit.ts +91 -0
- package/src/common/fs.ts +17 -0
- package/src/common/help.ts +51 -0
- package/src/common/jsx.ts +512 -0
- package/src/common/paths.ts +158 -0
- package/src/common/watch.ts +85 -0
- package/src/inflate/inflate-sea.ts +206 -0
- package/src/inflate/inflate.ts +99 -0
- package/src/inflate/md.ts +25 -0
- package/src/inflate/setup-auth.ts +40 -0
- package/src/inflate/setup-cors.ts +40 -0
- package/src/inflate/setup-db.ts +113 -0
- package/src/inflate/setup-error.ts +43 -0
- package/src/inflate/setup-http.ts +655 -0
- package/src/inflate/setup-log.ts +44 -0
- package/src/inflate/setup-middleware.ts +46 -0
- package/src/inflate/setup-server-config.ts +47 -0
- package/src/inflate/setup-test.ts +23 -0
- package/src/inflate/setup-ws.ts +48 -0
- package/src/inflate/setup.doc.ts +68 -0
- package/src/inflate/utils/sea-utils.ts +14 -0
- package/src/lib.ts +19 -0
- package/src/main.ts +87 -0
- package/src/services/app.ts +658 -0
- package/src/services/editor.tsx +101 -0
- package/src/services/globals.ts +143 -0
- package/src/services/hot-reload.ts +48 -0
- package/src/services/migrations.ts +66 -0
- package/src/services/utils/admin-interface.ts +37 -0
- package/src/services/utils/cache.ts +88 -0
- package/src/services/utils/cluster-cache.ts +230 -0
- package/src/services/utils/cluster-ws.ts +197 -0
- package/src/services/utils/db-manager.ts +92 -0
- package/src/services/utils/get-route.ts +70 -0
- package/src/services/utils/log-transport.ts +75 -0
- package/src/services/utils/log.ts +92 -0
- package/src/services/utils/server-interface.ts +172 -0
- package/src/services/utils/websocketmanager.ts +149 -0
- package/src/types/@esbuild.d.ts +1 -0
- package/src/types/@miqro/core.d.ts +2 -0
- package/src/types/@miqro/jsx.d.ts +2 -0
- package/src/types/@miqro/parser.d.ts +2 -0
- package/src/types/@miqro/query.d.ts +2 -0
- package/src/types/@miqro/request.d.ts +2 -0
- package/src/types/@miqro/test.d.ts +2 -0
- package/src/types/@miqro.d.ts +1 -0
- package/src/types/@types.d.ts +1 -0
- package/src/types/browser.globals.d.ts +9 -0
- package/src/types/globals.d.ts +2 -0
- package/src/types/jsx.globals.d.ts +37 -0
- package/src/types/miqro.d.ts +129 -0
- package/src/types/postject.d.ts +1 -0
- package/src/types/server.globals.d.ts +72 -0
- package/src/types.ts +222 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { ConsoleTransport, getEnvVariable, Logger, LoggerTransport, MinimalLogger, newURL, normalizePath, Request, WriteArgs } from "@miqro/core";
|
|
2
|
+
import { format } from "node:util";
|
|
3
|
+
|
|
4
|
+
const DEFAULT_ENV_NAME = "LOG_LEVEL";
|
|
5
|
+
|
|
6
|
+
export interface LogProviderOptions {
|
|
7
|
+
name?: string;
|
|
8
|
+
transports: LoggerTransport[];
|
|
9
|
+
formatter: (args: WriteArgs) => string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const DEFAULT_FORMATTER = ({ identifier, level, message, optionalParams }: WriteArgs) => format(`${new Date().toISOString()} PID[${process.pid}] ` +
|
|
13
|
+
`${identifier ? `[${identifier}] ` : ""}` +
|
|
14
|
+
`${level !== "info" ? (level === "error" || level === "warn" ? `[${level.toUpperCase()}] ` : `[${level}] `) : ""}` +
|
|
15
|
+
`${message}`, ...optionalParams)
|
|
16
|
+
|
|
17
|
+
export class LogProvider {
|
|
18
|
+
|
|
19
|
+
public options: LogProviderOptions;
|
|
20
|
+
public constructor(options?: LogProviderOptions) {
|
|
21
|
+
this.options = {
|
|
22
|
+
formatter: options && options.formatter ? options.formatter : DEFAULT_FORMATTER,
|
|
23
|
+
transports: options && options.transports ? options.transports : [ConsoleTransport()],
|
|
24
|
+
name: options?.name
|
|
25
|
+
};
|
|
26
|
+
this.requestLoggerFactory = this.requestLoggerFactory.bind(this);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public requestLoggerFactory(uuid: string, req: Request): MinimalLogger {
|
|
30
|
+
let path = "";
|
|
31
|
+
let query = "";
|
|
32
|
+
let urlParsingError;
|
|
33
|
+
let method = "GET";
|
|
34
|
+
let remoteAddress: string | undefined = "";
|
|
35
|
+
try {
|
|
36
|
+
const url = newURL(req.url ? req.url : "/") as URL;
|
|
37
|
+
query = url.searchParams.toString();
|
|
38
|
+
path = normalizePath(url.pathname); // normalized path
|
|
39
|
+
method = req.method ? req.method.toUpperCase() as string : "GET";
|
|
40
|
+
remoteAddress = req.socket.remoteAddress;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
urlParsingError = e;
|
|
43
|
+
}
|
|
44
|
+
const pathToEnv = path.replace(/\//ig, "_").replace(/\./ig, "_").replace(/-/ig, "_").toUpperCase();
|
|
45
|
+
const WORKER_IDENTIFIER = process.env["CLUSTER_NODE_NUMBER"] ? `WORKER_${process.env["CLUSTER_NODE_NUMBER"]}_` : "";
|
|
46
|
+
const identifier = `${pathToEnv === "_" ? "" : `${pathToEnv.substring(1)}`}${pathToEnv.charAt(pathToEnv.length - 1) !== "_" ? "_" : ""}${method.toUpperCase()}`;
|
|
47
|
+
const upgrade = req.headers.connection?.indexOf("Upgrade") !== -1;
|
|
48
|
+
|
|
49
|
+
const logger = this.getLogger(
|
|
50
|
+
`${WORKER_IDENTIFIER}${identifier}`, {
|
|
51
|
+
formatter: (args: WriteArgs) => {
|
|
52
|
+
args.message = `%s${upgrade ? " UPGRADE" : ""} %s%s [%s] (%s) ${args.message}`;
|
|
53
|
+
args.optionalParams = [method, path, query ? `?${query}` : "", uuid, remoteAddress].concat(args.optionalParams);
|
|
54
|
+
args.meta.push(req);
|
|
55
|
+
args.meta.push(uuid);
|
|
56
|
+
return this.options.formatter(args);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
if (urlParsingError) {
|
|
62
|
+
logger.error(urlParsingError);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return logger;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
public getLogger(identifier: string, options?: { level?: any; transports?: any[]; formatter?: any; }): Logger {
|
|
69
|
+
|
|
70
|
+
identifier = this.options.name ?
|
|
71
|
+
`${this.options.name}${identifier ?
|
|
72
|
+
`_${identifier}` : ""}`.toUpperCase() :
|
|
73
|
+
identifier ? identifier.toUpperCase() : "";
|
|
74
|
+
|
|
75
|
+
const envVarName = `LOG_LEVEL_${identifier}`;
|
|
76
|
+
const level = options && options.level ? options.level : getEnvVariable(envVarName) ? getEnvVariable(envVarName) : getEnvVariable(DEFAULT_ENV_NAME, "info");
|
|
77
|
+
|
|
78
|
+
const logger = new Logger(identifier, level, {
|
|
79
|
+
transports: [],
|
|
80
|
+
formatter: options && options.formatter ? options.formatter : this.options.formatter
|
|
81
|
+
});
|
|
82
|
+
for (const t of this.options.transports) {
|
|
83
|
+
logger.addTransport(t);
|
|
84
|
+
}
|
|
85
|
+
if (options && options.transports) {
|
|
86
|
+
for (const t of options.transports) {
|
|
87
|
+
logger.addTransport(t);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return logger;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { WebSocketServer, Logger } from "@miqro/core";
|
|
2
|
+
import { Database } from "@miqro/query";
|
|
3
|
+
import cluster from "node:cluster";
|
|
4
|
+
import { CacheInterface, MigrateOptions, NamedMigration, ServerInterface } from "../../types.js";
|
|
5
|
+
import { DBManager } from "./db-manager.js";
|
|
6
|
+
import { Miqro } from "../app.js";
|
|
7
|
+
import { WebSocketManager } from "./websocketmanager.js";
|
|
8
|
+
import { execSync } from "node:child_process";
|
|
9
|
+
import { LogProvider } from "./log.js";
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
{
|
|
13
|
+
cache: this.cache,
|
|
14
|
+
localCache: this.localCache,
|
|
15
|
+
db: {
|
|
16
|
+
get: (name: string) => {
|
|
17
|
+
return this.dbManager.getDB(name);
|
|
18
|
+
},
|
|
19
|
+
getMigrations: () => {
|
|
20
|
+
if (this.inflated) {
|
|
21
|
+
const ret: NamedMigration[] = [];
|
|
22
|
+
for (const d of this.inflated.dbList) {
|
|
23
|
+
ret.push(...(d.migrations.map(m => {
|
|
24
|
+
return {
|
|
25
|
+
name: m.name,
|
|
26
|
+
service: m.service,
|
|
27
|
+
dbName: m.dbName
|
|
28
|
+
}
|
|
29
|
+
})));
|
|
30
|
+
}
|
|
31
|
+
return ret;
|
|
32
|
+
}
|
|
33
|
+
return [];
|
|
34
|
+
},
|
|
35
|
+
migrate: (options) => {
|
|
36
|
+
return this.migrate(options);
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
ws: {
|
|
40
|
+
get: (path: string) => {
|
|
41
|
+
if (path === LOG_SOCKET_PATH) {
|
|
42
|
+
throw new Error("cannot use this path");
|
|
43
|
+
}
|
|
44
|
+
return this.webSocketManager.getWS(path);
|
|
45
|
+
},
|
|
46
|
+
disconnectAll: (path: string) => {
|
|
47
|
+
if (path === LOG_SOCKET_PATH) {
|
|
48
|
+
throw new Error("cannot use this path");
|
|
49
|
+
}
|
|
50
|
+
this.webSocketManager.disconnectAllFrom(path);
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
isPrimaryWorker: () => {
|
|
54
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === "0";
|
|
55
|
+
},
|
|
56
|
+
openBrowser: (path: string) => {
|
|
57
|
+
const PORT = this.options.port;
|
|
58
|
+
const URL = `http://localhost:${PORT}${path}`;
|
|
59
|
+
const DEFAULT_OPEN = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
60
|
+
const OPEN = process.env["BROWSER"] ? process.env["BROWSER"] === "none" ? false : process.env["BROWSER"] : DEFAULT_OPEN;
|
|
61
|
+
if (OPEN) {
|
|
62
|
+
const openCMD = `${OPEN} "${URL}"`;
|
|
63
|
+
this.logger?.info("opening browser with [%s]", openCMD);
|
|
64
|
+
execSync(openCMD);
|
|
65
|
+
} else {
|
|
66
|
+
this.logger?.warn("ignoring browser [%s]", process.env["BROWSER"]);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
logger: this.logger,
|
|
70
|
+
getLogger: (identifier: string, options?: { level?: any; transports?: any[]; formatter?: any; }) => {
|
|
71
|
+
return this.loggerProvider.getLogger(identifier, options);
|
|
72
|
+
}
|
|
73
|
+
}*/
|
|
74
|
+
|
|
75
|
+
export interface ServerInterfaceImplOptions {
|
|
76
|
+
cache: CacheInterface;
|
|
77
|
+
localCache: CacheInterface;
|
|
78
|
+
dbManager: DBManager;
|
|
79
|
+
wsManager: WebSocketManager;
|
|
80
|
+
logger?: Logger;
|
|
81
|
+
app?: Miqro;
|
|
82
|
+
port?: string;
|
|
83
|
+
loggerProvider?: LogProvider;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class ServerInterfaceImpl implements ServerInterface {
|
|
87
|
+
public cache: CacheInterface;
|
|
88
|
+
public localCache: CacheInterface;
|
|
89
|
+
public logger?: Logger;
|
|
90
|
+
public port?: string;
|
|
91
|
+
|
|
92
|
+
public db: {
|
|
93
|
+
get(name: string): Database | null;
|
|
94
|
+
getMigrations(): NamedMigration[];
|
|
95
|
+
migrate(options: MigrateOptions): Promise<void>;
|
|
96
|
+
};
|
|
97
|
+
public ws: {
|
|
98
|
+
get(path: string): WebSocketServer | undefined;
|
|
99
|
+
disconnectAll(path: string): void;
|
|
100
|
+
};
|
|
101
|
+
public loggerProvider?: LogProvider;
|
|
102
|
+
|
|
103
|
+
constructor(options: ServerInterfaceImplOptions) {
|
|
104
|
+
this.cache = options.cache;
|
|
105
|
+
this.localCache = options.localCache;
|
|
106
|
+
this.logger = options.logger ? options.logger : options.loggerProvider ? options.loggerProvider.getLogger("server") : undefined;
|
|
107
|
+
this.port = options.port;
|
|
108
|
+
|
|
109
|
+
const dbManager = options.dbManager;
|
|
110
|
+
const wsManager = options.wsManager;
|
|
111
|
+
this.loggerProvider = options.loggerProvider;
|
|
112
|
+
const app = options.app;
|
|
113
|
+
|
|
114
|
+
this.db = Object.freeze({
|
|
115
|
+
get: (name: string) => {
|
|
116
|
+
return dbManager.getDB(name);
|
|
117
|
+
},
|
|
118
|
+
getMigrations: () => {
|
|
119
|
+
if (app?.inflated) {
|
|
120
|
+
const ret: NamedMigration[] = [];
|
|
121
|
+
for (const d of app?.inflated.dbList) {
|
|
122
|
+
ret.push(...(d.migrations.map(m => {
|
|
123
|
+
return {
|
|
124
|
+
name: m.name,
|
|
125
|
+
service: m.service,
|
|
126
|
+
dbName: m.dbName
|
|
127
|
+
}
|
|
128
|
+
})));
|
|
129
|
+
}
|
|
130
|
+
return ret;
|
|
131
|
+
}
|
|
132
|
+
return [];
|
|
133
|
+
},
|
|
134
|
+
migrate: (options: MigrateOptions) => {
|
|
135
|
+
return app?.migrate(options);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
this.ws = Object.freeze({
|
|
139
|
+
get: (name: string) => {
|
|
140
|
+
return wsManager?.getWS(name);
|
|
141
|
+
},
|
|
142
|
+
disconnectAll: (path: string) => {
|
|
143
|
+
return wsManager?.disconnectAllButLOGSocket();
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
public getWorkerNumber(): number {
|
|
148
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === undefined ? 0 : parseInt(process.env["CLUSTER_NODE_NUMBER"], 10);
|
|
149
|
+
}
|
|
150
|
+
public getWorkerCount(): number {
|
|
151
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === undefined || process.env["CLUSTER_COUNT"] === undefined ? 1 : parseInt(process.env["CLUSTER_COUNT"], 10);
|
|
152
|
+
}
|
|
153
|
+
public isPrimaryWorker(): boolean {
|
|
154
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === "0";
|
|
155
|
+
}
|
|
156
|
+
public openBrowser(path: string): void {
|
|
157
|
+
const PORT = this.port;
|
|
158
|
+
const URL = `http://localhost${PORT ? `:${PORT}` : ""}${path}`;
|
|
159
|
+
const DEFAULT_OPEN = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
160
|
+
const OPEN = process.env["BROWSER"] ? process.env["BROWSER"] === "none" ? false : process.env["BROWSER"] : DEFAULT_OPEN;
|
|
161
|
+
if (OPEN) {
|
|
162
|
+
const openCMD = `${OPEN} "${URL}"`;
|
|
163
|
+
this.logger?.info("opening browser with [%s]", openCMD);
|
|
164
|
+
execSync(openCMD);
|
|
165
|
+
} else {
|
|
166
|
+
this.logger?.warn("ignoring browser [%s]", process.env["BROWSER"]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
public getLogger(identifier: string, options?: { level?: any; transports?: any[]; formatter?: any; }): Logger {
|
|
170
|
+
return this.loggerProvider?.getLogger(identifier, options);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { Logger } from "@miqro/core";
|
|
2
|
+
import { LOG_SOCKET_PATH } from "../../../editor/common/constants.js";
|
|
3
|
+
import { ClusterWebSocketServer2 } from "./cluster-ws.js";
|
|
4
|
+
import { WSConfig } from "../../types.js";
|
|
5
|
+
|
|
6
|
+
export interface WebSocketManagerOptions {
|
|
7
|
+
logger?: Logger | Console;
|
|
8
|
+
name?: string;
|
|
9
|
+
avoidLogSocket?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class WebSocketManager {
|
|
13
|
+
public runningGlobalWSMap = new Map<string, ClusterWebSocketServer2>();
|
|
14
|
+
public logger?: Logger | Console | null = null;
|
|
15
|
+
public name: string;
|
|
16
|
+
public avoidLogSocket: boolean;
|
|
17
|
+
constructor(options?: WebSocketManagerOptions) {
|
|
18
|
+
this.onUpgrade = this.onUpgrade.bind(this);
|
|
19
|
+
this.logger = options && options.logger ? options.logger : null;
|
|
20
|
+
this.name = options && options.name ? options.name : "WebSocketManager";
|
|
21
|
+
this.avoidLogSocket = options && options.avoidLogSocket ? options.avoidLogSocket : false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public deleteWS(path: string) {
|
|
25
|
+
const ws = this.runningGlobalWSMap.get(path);
|
|
26
|
+
this.disconnectAllFrom(path);
|
|
27
|
+
ws.dispose();
|
|
28
|
+
this.runningGlobalWSMap.delete(path);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public deleteAllWS() {
|
|
32
|
+
for (const path of this.runningGlobalWSMap.keys()) {
|
|
33
|
+
this.deleteWS(path);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public getWS(path: string): ClusterWebSocketServer2 | undefined {
|
|
38
|
+
return this.runningGlobalWSMap.get(path);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public replaceALLWS(list: WSConfig[]) {
|
|
42
|
+
this.deleteAllWS();
|
|
43
|
+
for (const wsConfig of list) {
|
|
44
|
+
if (!wsConfig.disabled) {
|
|
45
|
+
if (this.runningGlobalWSMap.has(wsConfig.path)) {
|
|
46
|
+
throw new Error(`ws on path ${wsConfig.path} already setup!`);
|
|
47
|
+
}
|
|
48
|
+
this.logger?.debug("setting up websocket on [%s]", wsConfig.path);
|
|
49
|
+
const server = new ClusterWebSocketServer2(wsConfig.path, wsConfig);
|
|
50
|
+
this.runningGlobalWSMap.set(wsConfig.path, server);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public replaceALLWSBuLOGSocket(list: WSConfig[]) {
|
|
56
|
+
for (const path of this.runningGlobalWSMap.keys()) {
|
|
57
|
+
if (path !== LOG_SOCKET_PATH || !this.avoidLogSocket) {
|
|
58
|
+
this.deleteWS(path);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const wsConfig of list) {
|
|
62
|
+
if (!wsConfig.disabled) {
|
|
63
|
+
if (wsConfig.path !== LOG_SOCKET_PATH || !this.avoidLogSocket) {
|
|
64
|
+
if (this.runningGlobalWSMap.has(wsConfig.path)) {
|
|
65
|
+
throw new Error(`ws on path ${wsConfig.path} already setup!`);
|
|
66
|
+
}
|
|
67
|
+
this.logger?.debug("setting up websocket on [%s]", wsConfig.path);
|
|
68
|
+
const server = new ClusterWebSocketServer2(this.name + wsConfig.path, wsConfig);
|
|
69
|
+
this.runningGlobalWSMap.set(wsConfig.path, server);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/*public setupWS(path: string, server: ClusterWebSocketServer2) {
|
|
76
|
+
if (this.runningGlobalWSMap.has(path)) {
|
|
77
|
+
throw new Error(`ws on path ${path} already setup!`);
|
|
78
|
+
}
|
|
79
|
+
this.runningGlobalWSMap.set(path, server);
|
|
80
|
+
}*/
|
|
81
|
+
|
|
82
|
+
public disconnectAllFrom(path: string) {
|
|
83
|
+
try {
|
|
84
|
+
this.logger?.debug("disconnect all from [%s]", path);
|
|
85
|
+
const ws = this.getWS(path);
|
|
86
|
+
if (ws) {
|
|
87
|
+
const clients = ws.clients.values();
|
|
88
|
+
if (clients) {
|
|
89
|
+
for (const client of clients) {
|
|
90
|
+
try {
|
|
91
|
+
client.socket.destroy();
|
|
92
|
+
} catch (e2) {
|
|
93
|
+
this.logger?.error("error disconnecting web socket client");
|
|
94
|
+
this.logger?.error(e2);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//ws.dispose();
|
|
99
|
+
}
|
|
100
|
+
} catch (e) {
|
|
101
|
+
this.logger?.error("error disconnecting web socket clients");
|
|
102
|
+
this.logger?.error(e.message);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public disconnectAllButLOGSocket() {
|
|
107
|
+
try {
|
|
108
|
+
for (const wsPath of this.runningGlobalWSMap.keys()) {
|
|
109
|
+
if (wsPath !== LOG_SOCKET_PATH || !this.avoidLogSocket) {
|
|
110
|
+
this.disconnectAllFrom(wsPath);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch (e) {
|
|
114
|
+
this.logger?.error("error disconnecting web socket clients");
|
|
115
|
+
this.logger?.error(e.message);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public disconnectAll() {
|
|
120
|
+
try {
|
|
121
|
+
for (const wsPath of this.runningGlobalWSMap.keys()) {
|
|
122
|
+
this.disconnectAllFrom(wsPath);
|
|
123
|
+
}
|
|
124
|
+
} catch (e) {
|
|
125
|
+
this.logger?.error("error disconnecting web socket clients");
|
|
126
|
+
this.logger?.error(e.message);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public onUpgrade(req, socket, head) {
|
|
131
|
+
try {
|
|
132
|
+
const wsServer = this.getWS(req.path);
|
|
133
|
+
if (wsServer) {
|
|
134
|
+
return wsServer.onUpgrade(req, socket, head);
|
|
135
|
+
} else {
|
|
136
|
+
socket.destroy();
|
|
137
|
+
}
|
|
138
|
+
} catch (e) {
|
|
139
|
+
this.logger?.error(e);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/*export interface WSMapConfig {
|
|
145
|
+
[path: string]: {
|
|
146
|
+
name: string;
|
|
147
|
+
options: WSConfig;
|
|
148
|
+
} | undefined;
|
|
149
|
+
}*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "../globals.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "../globals.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "../globals.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "./jsx.globals.js";
|
|
2
|
+
import { RuntimeElementDefinitionOptions, Component } from "@miqro/jsx/esm/lib.js";
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
// only available browser side
|
|
6
|
+
var jsx: {
|
|
7
|
+
define: (tagName: string, component: Component, options?: RuntimeElementDefinitionOptions) => void;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import * as jsxLib from "@miqro/jsx/esm/lib.js";
|
|
2
|
+
|
|
3
|
+
declare global {
|
|
4
|
+
// jsx only for the default value of tsconfig.json
|
|
5
|
+
var React: {};
|
|
6
|
+
var jsx: {
|
|
7
|
+
useRuntime: typeof jsxLib.useRuntime;
|
|
8
|
+
usePathname: typeof jsxLib.usePathname;
|
|
9
|
+
Link: typeof jsxLib.Link;
|
|
10
|
+
Router: typeof jsxLib.Router;
|
|
11
|
+
createContext: typeof jsxLib.createContext;
|
|
12
|
+
useContext: typeof jsxLib.useContext;
|
|
13
|
+
useState: typeof jsxLib.useState;
|
|
14
|
+
useEffect: typeof jsxLib.useEffect;
|
|
15
|
+
useQuery: typeof jsxLib.useQuery;
|
|
16
|
+
useRef: typeof jsxLib.useRef;
|
|
17
|
+
useElement: typeof jsxLib.useElement;
|
|
18
|
+
useRefresh: typeof jsxLib.useRefresh;
|
|
19
|
+
}
|
|
20
|
+
/*var useRuntime: typeof jsxLib.useRuntime;
|
|
21
|
+
var usePathname: typeof jsxLib.usePathname;
|
|
22
|
+
var Link: typeof jsxLib.Link;
|
|
23
|
+
var Router: typeof jsxLib.Router;
|
|
24
|
+
var createContext: typeof jsxLib.createContext;
|
|
25
|
+
var useContext: typeof jsxLib.useContext;
|
|
26
|
+
var useState: typeof jsxLib.useState;
|
|
27
|
+
var useEffect: typeof jsxLib.useEffect;
|
|
28
|
+
var useQuery: typeof jsxLib.useQuery;
|
|
29
|
+
var useRef: typeof jsxLib.useRef;
|
|
30
|
+
var useElement: typeof jsxLib.useElement;
|
|
31
|
+
var useRefresh: typeof jsxLib.useRefresh;*/
|
|
32
|
+
var JSX: {
|
|
33
|
+
createElement: typeof jsxLib.createElement;
|
|
34
|
+
Fragment: typeof jsxLib.Fragment;
|
|
35
|
+
enableDebugLog: typeof jsxLib.enableDebugLog;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import "./globals.js";
|
|
2
|
+
import { Database, Migration } from "@miqro/query/lib.js";
|
|
3
|
+
import { ErrorHandler, HandlerWithOptions, CORSOptions, LogLevel, LoggerTransportWriteArgs, Request, Response, WebSocketServer, Logger, WebSocketServerOptions, SessionHandlerOptions, RouteOptions, Handler } from "@miqro/core/lib.js";
|
|
4
|
+
import { ParserInterface } from "@miqro/parser/lib.js";
|
|
5
|
+
|
|
6
|
+
/*export * from "@miqro/core/lib.js";
|
|
7
|
+
export * from "@miqro/query/lib.js";*/
|
|
8
|
+
|
|
9
|
+
export interface AuthConfig extends SessionHandlerOptions {
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface MiddlewareConfig {
|
|
14
|
+
middleware?: Array<HandlerWithOptions | Handler>;
|
|
15
|
+
post?: Array<HandlerWithOptions | Handler>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CORSConfig extends CORSOptions {
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ErrorConfig {
|
|
23
|
+
catch?: Array<ErrorHandler>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WSConfig extends WebSocketServerOptions {
|
|
27
|
+
path: string;
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface DocConfig {
|
|
32
|
+
publish: {
|
|
33
|
+
[path: string]: {
|
|
34
|
+
type?: "HTML" | "MD" | "JSON",
|
|
35
|
+
all?: boolean;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ServerConfig {
|
|
41
|
+
start?: (server: ServerInterface) => Promise<void> | void;
|
|
42
|
+
preload?: (server: ServerInterface) => Promise<void> | void;
|
|
43
|
+
load?: (server: ServerInterface) => Promise<void> | void;
|
|
44
|
+
unload?: (server: ServerInterface) => void;
|
|
45
|
+
stop?: (server: ServerInterface) => void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface DBConfig {
|
|
49
|
+
dialect?: string;
|
|
50
|
+
storage?: string;
|
|
51
|
+
url?: string;
|
|
52
|
+
disabled?: boolean;
|
|
53
|
+
name: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface CacheInterface {
|
|
57
|
+
get<T = any>(key: string): T | undefined;
|
|
58
|
+
set: (key: string, value: unknown) => void;
|
|
59
|
+
unset: (key: string) => void;
|
|
60
|
+
has: (key: string) => boolean;
|
|
61
|
+
|
|
62
|
+
set_add: (key: string, value: unknown) => void;
|
|
63
|
+
set_delete: (key: string, value: unknown) => void;
|
|
64
|
+
set_has: (key: string, value: unknown) => boolean;
|
|
65
|
+
set_clear: (key: string) => void;
|
|
66
|
+
|
|
67
|
+
array_push: (key: string, value: unknown) => void;
|
|
68
|
+
array_clear: (key: string) => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface APIOptions extends Partial<RouteOptions> {
|
|
72
|
+
basePath?: string;
|
|
73
|
+
path?: string | string[];
|
|
74
|
+
method?: string | string[];
|
|
75
|
+
parser?: ParserInterface;
|
|
76
|
+
middleware?: Handler | Handler[];
|
|
77
|
+
session?: SessionHandlerOptions | Handler;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface NamedMigration {
|
|
81
|
+
name: string;
|
|
82
|
+
service: string;
|
|
83
|
+
dbName: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface LogConfig {
|
|
87
|
+
level?: LogLevel;
|
|
88
|
+
replaceConsoleTransport?: boolean;
|
|
89
|
+
replaceFileTransport?: boolean;
|
|
90
|
+
write: (args: LoggerTransportWriteArgs) => Promise<void> | void;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface MigrateOptions {
|
|
94
|
+
direction: "up" | "down";
|
|
95
|
+
service?: string;
|
|
96
|
+
dbName?: string;
|
|
97
|
+
name?: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ServerInterface {
|
|
101
|
+
// null values are if the feature has been disabled
|
|
102
|
+
db: {
|
|
103
|
+
get(name: string): Database | null;
|
|
104
|
+
getMigrations(): NamedMigration[];
|
|
105
|
+
migrate(options?: MigrateOptions): Promise<void>;
|
|
106
|
+
},
|
|
107
|
+
ws: {
|
|
108
|
+
get(path: string): WebSocketServer | undefined;
|
|
109
|
+
disconnectAll(path: string): void;
|
|
110
|
+
};
|
|
111
|
+
cache: CacheInterface;
|
|
112
|
+
localCache: CacheInterface;
|
|
113
|
+
logger: Logger;
|
|
114
|
+
isPrimaryWorker: () => boolean;
|
|
115
|
+
getWorkerNumber: () => number;
|
|
116
|
+
getWorkerCount: () => number;
|
|
117
|
+
openBrowser: (path: string) => void;
|
|
118
|
+
getLogger: (identifier: string, options?: { level?: any; transports?: any[]; formatter?: any; }) => Logger;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface ServerRequest extends Request {
|
|
122
|
+
server?: ServerInterface;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface ServerResponse extends Response {
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { App, LoggerHandler, Router, APIRoute } from "@miqro/core/lib.js";
|
|
129
|
+
export { migration, Migration } from "@miqro/query/lib.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "../globals.js";
|