miqro 6.1.3 → 6.2.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/build/editor.bundle.js +26 -2
- package/build/esm/editor/common/templates.js +16 -0
- package/build/esm/editor/components/editor.js +8 -0
- package/build/esm/editor/components/file-editor.js +1 -1
- package/build/esm/editor/components/highlight-text-area.js +2 -1
- package/build/esm/src/bin/types.js +8 -0
- package/build/esm/src/common/arguments.d.ts +7 -1
- package/build/esm/src/common/arguments.js +72 -10
- package/build/esm/src/common/help.d.ts +1 -1
- package/build/esm/src/common/help.js +33 -29
- package/build/esm/src/common/jwt.d.ts +49 -0
- package/build/esm/src/common/jwt.js +76 -0
- package/build/esm/src/inflate/inflate-sea.js +9 -8
- package/build/esm/src/inflate/setup-http.d.ts +1 -0
- package/build/esm/src/inflate/setup-http.js +8 -0
- package/build/esm/src/inflate/setup-ws.d.ts +1 -1
- package/build/esm/src/inflate/setup-ws.js +2 -2
- package/build/esm/src/lib.d.ts +1 -1
- package/build/esm/src/lib.js +2 -2
- package/build/esm/src/main.js +3 -1
- package/build/esm/src/services/app.d.ts +2 -0
- package/build/esm/src/services/app.js +7 -6
- package/build/esm/src/services/globals.js +45 -1
- package/build/esm/src/services/utils/cluster-ws.d.ts +4 -2
- package/build/esm/src/services/utils/cluster-ws.js +10 -1
- package/build/esm/src/services/utils/log-transport.d.ts +2 -2
- package/build/esm/src/services/utils/log-transport.js +8 -3
- package/build/esm/src/services/utils/server-interface.d.ts +4 -30
- package/build/esm/src/services/utils/server-interface.js +44 -59
- package/build/esm/src/services/utils/websocketmanager.d.ts +3 -0
- package/build/esm/src/services/utils/websocketmanager.js +8 -2
- package/build/esm/src/types.d.ts +76 -5
- package/build/lib.cjs +2992 -369
- package/editor/common/templates.ts +15 -0
- package/editor/components/editor.tsx +8 -0
- package/editor/components/file-editor.tsx +1 -1
- package/editor/components/highlight-text-area.tsx +2 -1
- package/package.json +4 -3
- package/sea/install-nodejs.sh +1 -1
- package/sea/node.version.tag +1 -1
- package/sea/types.json +1 -1
- package/src/bin/types.ts +7 -0
- package/src/common/arguments.ts +84 -11
- package/src/common/help.ts +33 -29
- package/src/common/jwt.ts +85 -0
- package/src/inflate/inflate-sea.ts +9 -8
- package/src/inflate/setup-db.ts +3 -1
- package/src/inflate/setup-http.ts +9 -0
- package/src/inflate/setup-ws.ts +4 -3
- package/src/lib.ts +2 -2
- package/src/main.ts +3 -1
- package/src/services/app.ts +10 -6
- package/src/services/globals.ts +45 -2
- package/src/services/utils/cluster-ws.ts +6 -1
- package/src/services/utils/log-transport.ts +10 -4
- package/src/services/utils/server-interface.ts +44 -133
- package/src/services/utils/websocketmanager.ts +10 -2
- package/src/types/cookie.d.ts +2 -0
- package/src/types/jose.d.ts +2 -0
- package/src/types/miqro.d.ts +92 -2
- package/src/types/server.globals.d.ts +4 -29
- package/src/types.ts +78 -5
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Logger } from "@miqro/core";
|
|
2
2
|
import { InflateError } from "../common/jsx.js";
|
|
3
3
|
import { WSConfig } from "../types.js";
|
|
4
|
-
export declare function inflateWSConfig(logger: Logger, servicePath: string, service: string, wsConfigList: WSConfig[] | undefined, inflateDir: string | undefined | false, errors: InflateError[]): Promise<
|
|
4
|
+
export declare function inflateWSConfig(logger: Logger, servicePath: string, service: string, wsConfigList: WSConfig[] | undefined, inflateDir: string | undefined | false, errors: InflateError[]): Promise<void>;
|
|
@@ -30,7 +30,7 @@ export async function inflateWSConfig(logger, servicePath, service, wsConfigList
|
|
|
30
30
|
logger
|
|
31
31
|
}));
|
|
32
32
|
}
|
|
33
|
-
return
|
|
33
|
+
//return wsConfigList;
|
|
34
34
|
}
|
|
35
35
|
catch (e) {
|
|
36
36
|
errors.push({
|
|
@@ -39,7 +39,7 @@ export async function inflateWSConfig(logger, servicePath, service, wsConfigList
|
|
|
39
39
|
});
|
|
40
40
|
logger.error("error with " + wsPath);
|
|
41
41
|
logger.error(e);
|
|
42
|
-
return false;
|
|
42
|
+
//return false;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}
|
package/build/esm/src/lib.d.ts
CHANGED
|
@@ -8,6 +8,6 @@ export { LogProvider, LogProviderOptions } from "./services/utils/log.js";
|
|
|
8
8
|
export { Miqro, MiqroOptions, ServerRequestHandler } from "./services/app.js";
|
|
9
9
|
export { initGlobals, assertGlobalTampered } from "./services/globals.js";
|
|
10
10
|
export { appendAPIModule } from "./inflate/utils/sea-utils.js";
|
|
11
|
-
export {
|
|
11
|
+
export { createServerInterface, ServerInterfaceImplOptions } from "./services/utils/server-interface.js";
|
|
12
12
|
export { App, LoggerHandler, Router } from "@miqro/core";
|
|
13
13
|
export { migration } from "@miqro/query";
|
package/build/esm/src/lib.js
CHANGED
|
@@ -9,8 +9,8 @@ export { DBManager } from "./services/utils/db-manager.js";
|
|
|
9
9
|
export { LogProvider } from "./services/utils/log.js";
|
|
10
10
|
export { Miqro, ServerRequestHandler } from "./services/app.js";
|
|
11
11
|
export { initGlobals, assertGlobalTampered } from "./services/globals.js";
|
|
12
|
-
export { appendAPIModule } from "./inflate/utils/sea-utils.js";
|
|
13
12
|
//exported for --inflate-sea
|
|
14
|
-
export {
|
|
13
|
+
export { appendAPIModule } from "./inflate/utils/sea-utils.js";
|
|
14
|
+
export { createServerInterface } from "./services/utils/server-interface.js";
|
|
15
15
|
export { App, LoggerHandler, Router } from "@miqro/core";
|
|
16
16
|
export { migration } from "@miqro/query";
|
package/build/esm/src/main.js
CHANGED
|
@@ -9,7 +9,7 @@ import { Miqro } from "./services/app.js";
|
|
|
9
9
|
import { TEST_SOCKET } from "./common/paths.js";
|
|
10
10
|
import { Logger } from "@miqro/core";
|
|
11
11
|
async function main(args) {
|
|
12
|
-
if (args.installTypes || args.installTSConfig) {
|
|
12
|
+
if (args.installTypes || args.installTSConfig || args.installMiqroJSON) {
|
|
13
13
|
await installTypings(args, new Logger(""));
|
|
14
14
|
process.exit(EXIT_CODES.NORMAL_EXIT);
|
|
15
15
|
}
|
|
@@ -19,6 +19,8 @@ async function main(args) {
|
|
|
19
19
|
name: args.name ? args.name : undefined,
|
|
20
20
|
port: args.test ? TEST_SOCKET : args.port,
|
|
21
21
|
services: args.services,
|
|
22
|
+
browser: args.browser,
|
|
23
|
+
logFile: args.logFile,
|
|
22
24
|
hotreload: args.test ? false : args.hotreload
|
|
23
25
|
});
|
|
24
26
|
// check arguments
|
|
@@ -19,7 +19,7 @@ import { initAssets } from "../common/assets.js";
|
|
|
19
19
|
import { setupExitHandlers } from "../common/exit.js";
|
|
20
20
|
import { inflateDBConfig, inflateDBMigrations } from "../inflate/setup-db.js";
|
|
21
21
|
import { getServicePath } from "../common/paths.js";
|
|
22
|
-
import {
|
|
22
|
+
import { createServerInterface } from "./utils/server-interface.js";
|
|
23
23
|
import { getPORT, importMiqroJSON } from "../common/arguments.js";
|
|
24
24
|
import { createLogProviderOptions } from "./utils/log-transport.js";
|
|
25
25
|
import { createAdminInterface } from "./utils/admin-interface.js";
|
|
@@ -90,6 +90,7 @@ export class Miqro {
|
|
|
90
90
|
this.localCache = new LocalCache(`MiqroApplicationLocalCache[${this.options.name}]`, this.logger);
|
|
91
91
|
this.webSocketManager = new WebSocketManager({
|
|
92
92
|
logger: this.logger,
|
|
93
|
+
loggerProvider: this.loggerProvider,
|
|
93
94
|
name: `MiqroApplicationWebsocketManager[${this.options.name}]`,
|
|
94
95
|
avoidLogSocket: this.options.editor
|
|
95
96
|
});
|
|
@@ -100,11 +101,11 @@ export class Miqro {
|
|
|
100
101
|
loggerProvider: this.loggerProvider,
|
|
101
102
|
logger: this.logger
|
|
102
103
|
});
|
|
103
|
-
this.serverInterface =
|
|
104
|
+
this.serverInterface = createServerInterface({
|
|
104
105
|
cache: this.cache,
|
|
105
|
-
localCache: this.localCache,
|
|
106
106
|
dbManager: this.dbManager,
|
|
107
|
-
|
|
107
|
+
localCache: this.localCache,
|
|
108
|
+
webSocketManager: this.webSocketManager,
|
|
108
109
|
app: this,
|
|
109
110
|
logger: this.logger,
|
|
110
111
|
loggerProvider: this.loggerProvider,
|
|
@@ -183,9 +184,9 @@ export class Miqro {
|
|
|
183
184
|
async loadDBConfig(options) {
|
|
184
185
|
const errors = [];
|
|
185
186
|
const dbList = [];
|
|
186
|
-
const
|
|
187
|
+
const dbConfigListALL = [];
|
|
187
188
|
for (const service of this.options.services) {
|
|
188
|
-
const dbConfig = await inflateDBConfig(this.logger, service,
|
|
189
|
+
const dbConfig = await inflateDBConfig(this.logger, service, dbConfigListALL, options?.inflateSea ? options?.inflateDir : undefined, errors);
|
|
189
190
|
if (dbConfig) {
|
|
190
191
|
const migrations = await inflateDBMigrations(this.logger, service, dbConfig.name, options?.inflateSea ? options?.inflateDir : undefined, errors);
|
|
191
192
|
dbList.push({
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
//@ts-ignore
|
|
2
2
|
import { ReadBuffer, URLEncodedParser, JSONParser, TextParser, CORS, SessionHandler } from "@miqro/core";
|
|
3
|
+
import cluster from "node:cluster";
|
|
3
4
|
import { strictEqual } from "node:assert";
|
|
4
5
|
import { HTMLEncode } from "@miqro/jsx-node";
|
|
5
6
|
import { createElement as realCreateElement, enableDebugLog, useRuntime, Link, Router, usePathname, Fragment, useEffect, useRef, useState, useQuery, useRefresh, useElement, createContext, useContext } from "@miqro/jsx";
|
|
6
7
|
import { jsx2HTML } from "../common/jsx.js";
|
|
7
8
|
import { inflateMD2HTML } from "../inflate/md.js";
|
|
8
9
|
import { EXIT_CODES } from "../common/constants.js";
|
|
10
|
+
import { ClusterCache, LocalCache } from "../lib.js";
|
|
11
|
+
import { decodeJWT, decodeProtectedHeaderJWT, decryptJWT, encryptJWT, signJWT, verifyJWT } from "../common/jwt.js";
|
|
12
|
+
import { createSecretKey } from "node:crypto";
|
|
13
|
+
import { Parser } from "@miqro/parser";
|
|
9
14
|
/*const globaljsx: any = Object.freeze({
|
|
10
15
|
useContext,
|
|
11
16
|
useRuntime,
|
|
@@ -80,7 +85,46 @@ const globalServer = Object.freeze({
|
|
|
80
85
|
session: SessionHandler
|
|
81
86
|
}),
|
|
82
87
|
encodeHTML: HTMLEncode,
|
|
83
|
-
inflateMDtoHTML: inflateMD2HTML
|
|
88
|
+
inflateMDtoHTML: inflateMD2HTML,
|
|
89
|
+
createSecretKey,
|
|
90
|
+
newParser() {
|
|
91
|
+
return new Parser();
|
|
92
|
+
},
|
|
93
|
+
newClusterCache(name, logger) {
|
|
94
|
+
return new ClusterCache(name, logger);
|
|
95
|
+
},
|
|
96
|
+
newLocalCache(name, logger) {
|
|
97
|
+
return new LocalCache(name, logger);
|
|
98
|
+
},
|
|
99
|
+
getWorkerNumber() {
|
|
100
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === undefined ? 0 : parseInt(process.env["CLUSTER_NODE_NUMBER"], 10);
|
|
101
|
+
},
|
|
102
|
+
getWorkerCount() {
|
|
103
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === undefined || process.env["CLUSTER_COUNT"] === undefined ? 1 : parseInt(process.env["CLUSTER_COUNT"], 10);
|
|
104
|
+
},
|
|
105
|
+
isPrimaryWorker() {
|
|
106
|
+
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === "0";
|
|
107
|
+
},
|
|
108
|
+
jwt: {
|
|
109
|
+
decode(jwt) {
|
|
110
|
+
return decodeJWT(jwt);
|
|
111
|
+
},
|
|
112
|
+
decodeProtectedHeader(token) {
|
|
113
|
+
return decodeProtectedHeaderJWT(token);
|
|
114
|
+
},
|
|
115
|
+
decrypt(jwt, secret, options) {
|
|
116
|
+
return decryptJWT(jwt, secret, options);
|
|
117
|
+
},
|
|
118
|
+
encrypt(payload, secret, options) {
|
|
119
|
+
return encryptJWT(payload, secret, options);
|
|
120
|
+
},
|
|
121
|
+
sign(payload, secret, options) {
|
|
122
|
+
return signJWT(payload, secret, options);
|
|
123
|
+
},
|
|
124
|
+
verify(jwt, secret, options) {
|
|
125
|
+
return verifyJWT(jwt, secret, options);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
84
128
|
});
|
|
85
129
|
export function browserJSXGlobals(inFile, jsxPath = false, useExport = true) {
|
|
86
130
|
const PRE = `import { enableDebugLog, useRuntime, Link, usePathname, createContext, useContext, useElement, useRefresh, useQuery, define, Router, useState, useEffect, useRef, createElement, Fragment } from "${jsxPath}";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WebSocketServer, WebSocketServerOptions } from "@miqro/core";
|
|
1
|
+
import { Logger, WebSocketServer, WebSocketServerOptions } from "@miqro/core";
|
|
2
2
|
import { Serializable } from "node:child_process";
|
|
3
3
|
declare const ClusterWebSocketServer2MessageType = "$$$$ClusterWebSocketServer2Message$$$$$";
|
|
4
4
|
export interface ClusterWebSocketServer2Message {
|
|
@@ -13,9 +13,11 @@ export interface ClusterWebSocketServer2Message {
|
|
|
13
13
|
}
|
|
14
14
|
export declare class ClusterWebSocketServer2 extends WebSocketServer {
|
|
15
15
|
protected name: string;
|
|
16
|
+
path: string;
|
|
17
|
+
logger: Logger | Console;
|
|
16
18
|
remoteClients: Set<string>;
|
|
17
19
|
listener: (data: any) => Promise<void>;
|
|
18
|
-
constructor(name: string, options: WebSocketServerOptions);
|
|
20
|
+
constructor(name: string, path: string, logger: Logger | Console, options: WebSocketServerOptions);
|
|
19
21
|
connect(): void;
|
|
20
22
|
dispose(): void;
|
|
21
23
|
broadcast(payload: string, fromUUID?: string): Promise<void>;
|
|
@@ -2,14 +2,17 @@ import { WebSocketServer } from "@miqro/core";
|
|
|
2
2
|
const ClusterWebSocketServer2MessageType = "$$$$ClusterWebSocketServer2Message$$$$$";
|
|
3
3
|
export class ClusterWebSocketServer2 extends WebSocketServer {
|
|
4
4
|
name;
|
|
5
|
+
path;
|
|
6
|
+
logger;
|
|
5
7
|
remoteClients = new Set();
|
|
6
8
|
listener;
|
|
7
|
-
constructor(name, options) {
|
|
9
|
+
constructor(name, path, logger, options) {
|
|
8
10
|
super({
|
|
9
11
|
...options,
|
|
10
12
|
validate: (req) => {
|
|
11
13
|
if (this.options.maxConnections !== undefined &&
|
|
12
14
|
this.clients.size + this.remoteClients.size >= this.options.maxConnections) {
|
|
15
|
+
this.logger?.warn("[%s] max web socket connection reached! connection refused from [%s]", req.uuid, req.socket.remoteAddress);
|
|
13
16
|
return false;
|
|
14
17
|
}
|
|
15
18
|
else {
|
|
@@ -27,6 +30,8 @@ export class ClusterWebSocketServer2 extends WebSocketServer {
|
|
|
27
30
|
errorMessage: error.message
|
|
28
31
|
});
|
|
29
32
|
}
|
|
33
|
+
this.logger?.error("[%s] error from (%s) error [%s]", req.uuid, req.req.socket.remoteAddress, error);
|
|
34
|
+
this.logger?.error(error);
|
|
30
35
|
if (options.onError) {
|
|
31
36
|
options.onError(req, error);
|
|
32
37
|
}
|
|
@@ -41,6 +46,7 @@ export class ClusterWebSocketServer2 extends WebSocketServer {
|
|
|
41
46
|
clientUUID: req.uuid
|
|
42
47
|
});
|
|
43
48
|
}
|
|
49
|
+
this.logger?.log("[%s] new web socket connection from (%s)", req.uuid, req.req.socket.remoteAddress);
|
|
44
50
|
if (options.onConnection) {
|
|
45
51
|
options.onConnection(req);
|
|
46
52
|
}
|
|
@@ -55,12 +61,15 @@ export class ClusterWebSocketServer2 extends WebSocketServer {
|
|
|
55
61
|
clientUUID: req.uuid
|
|
56
62
|
});
|
|
57
63
|
}
|
|
64
|
+
this.logger?.log("[%s] [%s] web socket disconnection from (%s)", req.uuid, this.path, req.req.socket.remoteAddress);
|
|
58
65
|
if (options.onDisconnect) {
|
|
59
66
|
options.onDisconnect(req);
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
69
|
});
|
|
63
70
|
this.name = name;
|
|
71
|
+
this.path = path;
|
|
72
|
+
this.logger = logger;
|
|
64
73
|
this.listener = async (data) => {
|
|
65
74
|
try {
|
|
66
75
|
const msg = data;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { WriteArgs } from "@miqro/core";
|
|
1
|
+
import { LoggerTransport, WriteArgs } from "@miqro/core";
|
|
2
2
|
import { Miqro } from "../app.js";
|
|
3
3
|
export declare function createLogProviderOptions(app: Miqro): {
|
|
4
4
|
name: string;
|
|
5
|
-
transports:
|
|
5
|
+
transports: LoggerTransport[];
|
|
6
6
|
formatter: (args: WriteArgs) => string;
|
|
7
7
|
};
|
|
@@ -3,7 +3,12 @@ import { format } from "node:util";
|
|
|
3
3
|
import { LOG_SOCKET_PATH, LOG_WRITE_EVENT } from "../../../editor/common/constants.js";
|
|
4
4
|
export function createLogProviderOptions(app) {
|
|
5
5
|
const defaultConsole = ConsoleTransport();
|
|
6
|
-
|
|
6
|
+
//console.log("app.options.logFile [%s]", app.options.logFile);
|
|
7
|
+
const defaultFile = app.options.logFile !== true && app.options.logFile !== false && String(app.options.logFile).toUpperCase() !== "TRUE" && String(app.options.logFile).toUpperCase() !== "FALSE" &&
|
|
8
|
+
app.options.logFile ? FileTransport(app.options.logFile) :
|
|
9
|
+
String(app.options.logFile).toUpperCase() === "TRUE" || app.options.logFile === true || app.options.logFile === undefined ?
|
|
10
|
+
FileTransport() :
|
|
11
|
+
undefined;
|
|
7
12
|
const defaultWrite = async (args, level) => {
|
|
8
13
|
try {
|
|
9
14
|
const serviceNamesWithLogConfigReplaceConsole = level === undefined && app.inflated ?
|
|
@@ -12,9 +17,9 @@ export function createLogProviderOptions(app) {
|
|
|
12
17
|
Object.keys(app.inflated.logConfigMap).filter(serviceName => app.inflated.logConfigMap[serviceName].replaceFileTransport) : [];
|
|
13
18
|
await Promise.allSettled((level === undefined ?
|
|
14
19
|
[
|
|
15
|
-
level === undefined && serviceNamesWithLogConfigReplaceConsole.length === 0 ?
|
|
20
|
+
level === undefined && serviceNamesWithLogConfigReplaceConsole.length === 0 && defaultConsole ?
|
|
16
21
|
defaultConsole.write(args) : Promise.resolve(),
|
|
17
|
-
level === undefined && serviceNamesWithLogConfigReplaceFile.length === 0 ?
|
|
22
|
+
level === undefined && serviceNamesWithLogConfigReplaceFile.length === 0 && defaultFile ?
|
|
18
23
|
defaultFile.write(args) : Promise.resolve()
|
|
19
24
|
] : []).concat(app.inflated ?
|
|
20
25
|
Object.keys(app.inflated.logConfigMap).map(serviceName => app.inflated.logConfigMap[serviceName]).filter(c => c.level === level).map(c => c.write(args)) : []));
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { CacheInterface, MigrateOptions, NamedMigration, ServerInterface } from "../../types.js";
|
|
1
|
+
import { Logger } from "@miqro/core";
|
|
2
|
+
import { CacheInterface, ServerInterface } from "../../types.js";
|
|
4
3
|
import { DBManager } from "./db-manager.js";
|
|
5
4
|
import { Miqro } from "../app.js";
|
|
6
5
|
import { WebSocketManager } from "./websocketmanager.js";
|
|
@@ -9,35 +8,10 @@ export interface ServerInterfaceImplOptions {
|
|
|
9
8
|
cache: CacheInterface;
|
|
10
9
|
localCache: CacheInterface;
|
|
11
10
|
dbManager: DBManager;
|
|
12
|
-
|
|
11
|
+
webSocketManager: WebSocketManager;
|
|
13
12
|
logger?: Logger;
|
|
14
13
|
app?: Miqro;
|
|
15
14
|
port?: string;
|
|
16
15
|
loggerProvider?: LogProvider;
|
|
17
16
|
}
|
|
18
|
-
export declare
|
|
19
|
-
cache: CacheInterface;
|
|
20
|
-
localCache: CacheInterface;
|
|
21
|
-
logger?: Logger;
|
|
22
|
-
port?: string;
|
|
23
|
-
db: {
|
|
24
|
-
get(name: string): Database | null;
|
|
25
|
-
getMigrations(): NamedMigration[];
|
|
26
|
-
migrate(options: MigrateOptions): Promise<void>;
|
|
27
|
-
};
|
|
28
|
-
ws: {
|
|
29
|
-
get(path: string): WebSocketServer | undefined;
|
|
30
|
-
disconnectAll(path: string): void;
|
|
31
|
-
};
|
|
32
|
-
loggerProvider?: LogProvider;
|
|
33
|
-
constructor(options: ServerInterfaceImplOptions);
|
|
34
|
-
getWorkerNumber(): number;
|
|
35
|
-
getWorkerCount(): number;
|
|
36
|
-
isPrimaryWorker(): boolean;
|
|
37
|
-
openBrowser(path: string): void;
|
|
38
|
-
getLogger(identifier: string, options?: {
|
|
39
|
-
level?: any;
|
|
40
|
-
transports?: any[];
|
|
41
|
-
formatter?: any;
|
|
42
|
-
}): Logger;
|
|
43
|
-
}
|
|
17
|
+
export declare function createServerInterface(options: ServerInterfaceImplOptions): ServerInterface;
|
|
@@ -1,30 +1,19 @@
|
|
|
1
|
-
import cluster from "node:cluster";
|
|
2
1
|
import { execSync } from "node:child_process";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.localCache = options.localCache;
|
|
14
|
-
this.logger = options.logger ? options.logger : options.loggerProvider ? options.loggerProvider.getLogger("server") : undefined;
|
|
15
|
-
this.port = options.port;
|
|
16
|
-
const dbManager = options.dbManager;
|
|
17
|
-
const wsManager = options.wsManager;
|
|
18
|
-
this.loggerProvider = options.loggerProvider;
|
|
19
|
-
const app = options.app;
|
|
20
|
-
this.db = Object.freeze({
|
|
21
|
-
get: (name) => {
|
|
22
|
-
return dbManager.getDB(name);
|
|
2
|
+
import { initGlobals } from "../globals.js";
|
|
3
|
+
export function createServerInterface(options) {
|
|
4
|
+
initGlobals();
|
|
5
|
+
return Object.freeze({
|
|
6
|
+
cache: options.cache,
|
|
7
|
+
localCache: options.localCache,
|
|
8
|
+
logger: options.logger,
|
|
9
|
+
db: {
|
|
10
|
+
get(name) {
|
|
11
|
+
return options.dbManager.getDB(name);
|
|
23
12
|
},
|
|
24
|
-
getMigrations
|
|
25
|
-
if (app?.inflated) {
|
|
13
|
+
getMigrations() {
|
|
14
|
+
if (options.app?.inflated) {
|
|
26
15
|
const ret = [];
|
|
27
|
-
for (const d of app?.inflated.dbList) {
|
|
16
|
+
for (const d of options.app?.inflated.dbList) {
|
|
28
17
|
ret.push(...(d.migrations.map(m => {
|
|
29
18
|
return {
|
|
30
19
|
name: m.name,
|
|
@@ -37,43 +26,39 @@ export class ServerInterfaceImpl {
|
|
|
37
26
|
}
|
|
38
27
|
return [];
|
|
39
28
|
},
|
|
40
|
-
migrate
|
|
41
|
-
return app?.migrate(
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
29
|
+
migrate(migrateOptions) {
|
|
30
|
+
return options?.app?.migrate(migrateOptions);
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
ws: {
|
|
45
34
|
get: (name) => {
|
|
46
|
-
return
|
|
35
|
+
return options?.webSocketManager?.getWS(name);
|
|
47
36
|
},
|
|
48
37
|
disconnectAll: (path) => {
|
|
49
|
-
return
|
|
38
|
+
return options?.webSocketManager?.disconnectAllButLOGSocket();
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
openBrowser(path) {
|
|
42
|
+
const PORT = options.port;
|
|
43
|
+
const URL = `http://localhost${PORT ? `:${PORT}` : ""}${path}`;
|
|
44
|
+
const DEFAULT_OPEN = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
45
|
+
const OPEN = options?.app?.options.browser !== undefined && String(options?.app?.options.browser).toUpperCase() !== "TRUE" && String(options?.app?.options.browser).toUpperCase() !== "1" ?
|
|
46
|
+
String(options?.app.options.browser).toUpperCase() !== "0" && String(options?.app?.options.browser).toUpperCase() !== "FALSE" && String(options?.app?.options.browser).toUpperCase() !== "NONE" && options?.app?.options.browser ?
|
|
47
|
+
options?.app.options.browser : false :
|
|
48
|
+
process.env["BROWSER"] ?
|
|
49
|
+
process.env["BROWSER"] === "none" ? false : process.env["BROWSER"] : DEFAULT_OPEN;
|
|
50
|
+
if (OPEN) {
|
|
51
|
+
const openCMD = `${OPEN} "${URL}"`;
|
|
52
|
+
options?.logger?.info("opening browser with [%s]", openCMD);
|
|
53
|
+
execSync(openCMD);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
options?.logger?.warn("ignoring browser [%s]", OPEN);
|
|
50
57
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === undefined || process.env["CLUSTER_COUNT"] === undefined ? 1 : parseInt(process.env["CLUSTER_COUNT"], 10);
|
|
58
|
-
}
|
|
59
|
-
isPrimaryWorker() {
|
|
60
|
-
return cluster.isPrimary || process.env["CLUSTER_NODE_NUMBER"] === "0";
|
|
61
|
-
}
|
|
62
|
-
openBrowser(path) {
|
|
63
|
-
const PORT = this.port;
|
|
64
|
-
const URL = `http://localhost${PORT ? `:${PORT}` : ""}${path}`;
|
|
65
|
-
const DEFAULT_OPEN = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "open" : "xdg-open";
|
|
66
|
-
const OPEN = process.env["BROWSER"] ? process.env["BROWSER"] === "none" ? false : process.env["BROWSER"] : DEFAULT_OPEN;
|
|
67
|
-
if (OPEN) {
|
|
68
|
-
const openCMD = `${OPEN} "${URL}"`;
|
|
69
|
-
this.logger?.info("opening browser with [%s]", openCMD);
|
|
70
|
-
execSync(openCMD);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
this.logger?.warn("ignoring browser [%s]", process.env["BROWSER"]);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
getLogger(identifier, options) {
|
|
77
|
-
return this.loggerProvider?.getLogger(identifier, options);
|
|
78
|
-
}
|
|
58
|
+
},
|
|
59
|
+
getLogger(identifier, loggerOptions) {
|
|
60
|
+
return options?.loggerProvider?.getLogger(identifier, loggerOptions);
|
|
61
|
+
},
|
|
62
|
+
...server
|
|
63
|
+
});
|
|
79
64
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Logger } from "@miqro/core";
|
|
2
2
|
import { ClusterWebSocketServer2 } from "./cluster-ws.js";
|
|
3
3
|
import { WSConfig } from "../../types.js";
|
|
4
|
+
import { LogProvider } from "./log.js";
|
|
4
5
|
export interface WebSocketManagerOptions {
|
|
5
6
|
logger?: Logger | Console;
|
|
7
|
+
loggerProvider?: LogProvider;
|
|
6
8
|
name?: string;
|
|
7
9
|
avoidLogSocket?: boolean;
|
|
8
10
|
}
|
|
@@ -11,6 +13,7 @@ export declare class WebSocketManager {
|
|
|
11
13
|
logger?: Logger | Console | null;
|
|
12
14
|
name: string;
|
|
13
15
|
avoidLogSocket: boolean;
|
|
16
|
+
loggerProvider: LogProvider;
|
|
14
17
|
constructor(options?: WebSocketManagerOptions);
|
|
15
18
|
deleteWS(path: string): void;
|
|
16
19
|
deleteAllWS(): void;
|
|
@@ -5,10 +5,12 @@ export class WebSocketManager {
|
|
|
5
5
|
logger = null;
|
|
6
6
|
name;
|
|
7
7
|
avoidLogSocket;
|
|
8
|
+
loggerProvider;
|
|
8
9
|
constructor(options) {
|
|
9
10
|
this.onUpgrade = this.onUpgrade.bind(this);
|
|
10
11
|
this.logger = options && options.logger ? options.logger : null;
|
|
11
12
|
this.name = options && options.name ? options.name : "WebSocketManager";
|
|
13
|
+
this.loggerProvider = options.loggerProvider;
|
|
12
14
|
this.avoidLogSocket = options && options.avoidLogSocket ? options.avoidLogSocket : false;
|
|
13
15
|
}
|
|
14
16
|
deleteWS(path) {
|
|
@@ -33,7 +35,9 @@ export class WebSocketManager {
|
|
|
33
35
|
throw new Error(`ws on path ${wsConfig.path} already setup!`);
|
|
34
36
|
}
|
|
35
37
|
this.logger?.debug("setting up websocket on [%s]", wsConfig.path);
|
|
36
|
-
const
|
|
38
|
+
const identifier = wsConfig.path.replaceAll("/", "_").toUpperCase();
|
|
39
|
+
const logger = this.loggerProvider && identifier.length >= 0 ? this.loggerProvider.getLogger(identifier.substring(identifier.charAt(0) === "_" ? 1 : 0)) : this.logger;
|
|
40
|
+
const server = new ClusterWebSocketServer2(this.name + wsConfig.path, wsConfig.path, logger, wsConfig);
|
|
37
41
|
this.runningGlobalWSMap.set(wsConfig.path, server);
|
|
38
42
|
}
|
|
39
43
|
}
|
|
@@ -51,7 +55,9 @@ export class WebSocketManager {
|
|
|
51
55
|
throw new Error(`ws on path ${wsConfig.path} already setup!`);
|
|
52
56
|
}
|
|
53
57
|
this.logger?.debug("setting up websocket on [%s]", wsConfig.path);
|
|
54
|
-
const
|
|
58
|
+
const identifier = wsConfig.path.replaceAll("/", "_").toUpperCase();
|
|
59
|
+
const logger = this.loggerProvider && identifier.length >= 0 ? this.loggerProvider.getLogger(identifier.substring(identifier.charAt(0) === "_" ? 1 : 0)) : this.logger;
|
|
60
|
+
const server = new ClusterWebSocketServer2(this.name + wsConfig.path, wsConfig.path, logger, wsConfig);
|
|
55
61
|
this.runningGlobalWSMap.set(wsConfig.path, server);
|
|
56
62
|
}
|
|
57
63
|
}
|
package/build/esm/src/types.d.ts
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
import { Database } from "@miqro/query";
|
|
2
2
|
import { WebSocketServerOptions, SessionHandlerOptions, Logger, WebSocketServer, ReadBuffer, URLEncodedParser, JSONParser, TextParser, CORS, SessionHandler, RouteOptions, Handler, Request, Response, LogLevel, LoggerTransportWriteArgs, CORSOptions, HandlerWithOptions, ErrorHandler } from "@miqro/core";
|
|
3
3
|
import { request } from "@miqro/request";
|
|
4
|
-
import { ParserInterface } from "@miqro/parser";
|
|
4
|
+
import { Parser, ParserInterface } from "@miqro/parser";
|
|
5
5
|
import { RuntimeHTMLElement, Runtime, RuntimeContainer, RuntimeURL, RuntimeOptions, RuntimeShadowRootInit } from "@miqro/jsx";
|
|
6
6
|
import { RuntimeElementDefinitionOptions, Component, createElement, Fragment, enableDebugLog } from "@miqro/jsx";
|
|
7
7
|
import * as jsxLib from "@miqro/jsx";
|
|
8
|
+
import { EncryptOptions, JWTDecryptOptions, JWTDecryptResult, JWTPayload, JWTVerifyOptions, JWTVerifyResult, ProtectedHeaderParameters, SignOptions } from "jose";
|
|
9
|
+
import { KeyObject } from "node:crypto";
|
|
10
|
+
export interface EncryptJWTOptions {
|
|
11
|
+
alg?: string;
|
|
12
|
+
enc?: string;
|
|
13
|
+
iat?: number | string | Date;
|
|
14
|
+
iss?: string;
|
|
15
|
+
aud?: string;
|
|
16
|
+
exp?: number | string | Date;
|
|
17
|
+
options?: EncryptOptions;
|
|
18
|
+
}
|
|
19
|
+
export interface JWTSignOptions {
|
|
20
|
+
alg?: string;
|
|
21
|
+
iat?: number | string | Date;
|
|
22
|
+
iss?: string;
|
|
23
|
+
aud?: string;
|
|
24
|
+
exp?: number | string | Date;
|
|
25
|
+
options?: SignOptions;
|
|
26
|
+
}
|
|
8
27
|
declare global {
|
|
9
28
|
var JSX: {
|
|
10
29
|
createElement: typeof createElement;
|
|
@@ -42,6 +61,61 @@ export interface ServerGlobal {
|
|
|
42
61
|
cors: typeof CORS;
|
|
43
62
|
session: typeof SessionHandler;
|
|
44
63
|
};
|
|
64
|
+
newParser(): Parser;
|
|
65
|
+
newClusterCache: (name: string, logger?: Logger) => CacheInterface;
|
|
66
|
+
newLocalCache: (name: string, logger?: Logger) => CacheInterface;
|
|
67
|
+
createSecretKey: (key: string, encoding: BufferEncoding) => KeyObject;
|
|
68
|
+
getWorkerCount: () => number;
|
|
69
|
+
getWorkerNumber: () => number;
|
|
70
|
+
isPrimaryWorker: () => boolean;
|
|
71
|
+
jwt: {
|
|
72
|
+
/**
|
|
73
|
+
* creates a JWT encrypted token with jose
|
|
74
|
+
*
|
|
75
|
+
* @param payload the payload to encrypt
|
|
76
|
+
* @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
|
|
77
|
+
* @param options options like expiratation date, issuer and audience
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
encrypt: (payload: JWTPayload, secret: KeyObject, options?: Partial<EncryptJWTOptions>) => Promise<string>;
|
|
81
|
+
/**
|
|
82
|
+
* decrypts a JWT token with jose
|
|
83
|
+
* @param jwt the JWT token
|
|
84
|
+
* @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
|
|
85
|
+
* @param options options like issuer and audience
|
|
86
|
+
* @returns
|
|
87
|
+
*/
|
|
88
|
+
decrypt: <PayloadType = JWTPayload>(jwt: string, secret: KeyObject, options?: Partial<JWTDecryptOptions>) => Promise<JWTDecryptResult<PayloadType>>;
|
|
89
|
+
/**
|
|
90
|
+
* verify a JWT token with jose
|
|
91
|
+
* @param jwt the JWT token
|
|
92
|
+
* @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
|
|
93
|
+
* @param options options like issuer and audience
|
|
94
|
+
* @returns
|
|
95
|
+
*/
|
|
96
|
+
verify: <PayloadType = JWTPayload>(jwt: string, secret: KeyObject, options?: Partial<JWTVerifyOptions>) => Promise<JWTVerifyResult<PayloadType>>;
|
|
97
|
+
/**
|
|
98
|
+
* creates a signed JWT with jose
|
|
99
|
+
*
|
|
100
|
+
* @param payload the payload to encrypt
|
|
101
|
+
* @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
|
|
102
|
+
* @param options options like expiratation date, issuer and audience
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
sign: (payload: JWTPayload, secret: KeyObject, options?: Partial<JWTSignOptions>) => Promise<string>;
|
|
106
|
+
/**
|
|
107
|
+
* decodes a protected header with jose
|
|
108
|
+
* @param token
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
decodeProtectedHeader: (token: string | object) => ProtectedHeaderParameters;
|
|
112
|
+
/**
|
|
113
|
+
* decodes a jwt token
|
|
114
|
+
* @param jwt
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
decode: <PayloadType = JWTPayload>(jwt: string) => PayloadType & JWTPayload;
|
|
118
|
+
};
|
|
45
119
|
}
|
|
46
120
|
export interface CacheInterface {
|
|
47
121
|
get<T = any>(key: string): T | undefined;
|
|
@@ -72,7 +146,7 @@ export interface LogConfig {
|
|
|
72
146
|
replaceFileTransport?: boolean;
|
|
73
147
|
write: (args: LoggerTransportWriteArgs) => Promise<void> | void;
|
|
74
148
|
}
|
|
75
|
-
export interface ServerInterface {
|
|
149
|
+
export interface ServerInterface extends ServerGlobal {
|
|
76
150
|
db: {
|
|
77
151
|
get(name: string): Database | null;
|
|
78
152
|
getMigrations(): NamedMigration[];
|
|
@@ -85,9 +159,6 @@ export interface ServerInterface {
|
|
|
85
159
|
cache: CacheInterface;
|
|
86
160
|
localCache: CacheInterface;
|
|
87
161
|
logger?: Logger;
|
|
88
|
-
isPrimaryWorker: () => boolean;
|
|
89
|
-
getWorkerNumber: () => number;
|
|
90
|
-
getWorkerCount: () => number;
|
|
91
162
|
openBrowser: (path: string) => void;
|
|
92
163
|
getLogger: (identifier: string, options?: {
|
|
93
164
|
level?: any;
|