miqro 6.1.4 → 6.2.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.
Files changed (53) hide show
  1. package/build/editor.bundle.js +10 -2
  2. package/build/esm/editor/components/editor.js +8 -0
  3. package/build/esm/editor/components/file-editor.js +1 -1
  4. package/build/esm/editor/components/highlight-text-area.js +2 -1
  5. package/build/esm/src/common/arguments.d.ts +7 -0
  6. package/build/esm/src/common/arguments.js +103 -2
  7. package/build/esm/src/common/help.d.ts +1 -1
  8. package/build/esm/src/common/help.js +4 -0
  9. package/build/esm/src/common/jwt.d.ts +49 -0
  10. package/build/esm/src/common/jwt.js +76 -0
  11. package/build/esm/src/inflate/inflate-sea.js +9 -8
  12. package/build/esm/src/inflate/setup-http.d.ts +1 -0
  13. package/build/esm/src/inflate/setup-http.js +8 -0
  14. package/build/esm/src/lib.d.ts +1 -1
  15. package/build/esm/src/lib.js +2 -2
  16. package/build/esm/src/main.js +4 -1
  17. package/build/esm/src/services/app.d.ts +5 -0
  18. package/build/esm/src/services/app.js +33 -7
  19. package/build/esm/src/services/globals.js +45 -1
  20. package/build/esm/src/services/utils/cluster-ws.d.ts +4 -2
  21. package/build/esm/src/services/utils/cluster-ws.js +10 -1
  22. package/build/esm/src/services/utils/server-interface.d.ts +4 -30
  23. package/build/esm/src/services/utils/server-interface.js +53 -64
  24. package/build/esm/src/services/utils/websocketmanager.d.ts +3 -0
  25. package/build/esm/src/services/utils/websocketmanager.js +8 -2
  26. package/build/esm/src/types.d.ts +85 -5
  27. package/build/lib.cjs +3026 -367
  28. package/editor/components/editor.tsx +8 -0
  29. package/editor/components/file-editor.tsx +1 -1
  30. package/editor/components/highlight-text-area.tsx +2 -1
  31. package/package.json +5 -4
  32. package/sea/install-nodejs.sh +1 -1
  33. package/sea/node.version.tag +1 -1
  34. package/sea/types.json +1 -1
  35. package/src/common/arguments.ts +118 -2
  36. package/src/common/help.ts +4 -0
  37. package/src/common/jwt.ts +85 -0
  38. package/src/inflate/inflate-sea.ts +9 -8
  39. package/src/inflate/setup-http.ts +9 -0
  40. package/src/lib.ts +2 -2
  41. package/src/main.ts +4 -1
  42. package/src/services/app.ts +38 -7
  43. package/src/services/globals.ts +45 -2
  44. package/src/services/utils/cluster-ws.ts +6 -1
  45. package/src/services/utils/server-interface.ts +53 -140
  46. package/src/services/utils/websocketmanager.ts +10 -2
  47. package/src/types/browser.globals.d.ts +0 -8
  48. package/src/types/cookie.d.ts +2 -0
  49. package/src/types/jose.d.ts +2 -0
  50. package/src/types/jsx.globals.d.ts +1 -0
  51. package/src/types/miqro.d.ts +101 -2
  52. package/src/types/server.globals.d.ts +4 -29
  53. package/src/types.ts +87 -5
@@ -8835,8 +8835,8 @@ function HighlightTextArea({ content, language, oncontentchange, tabChar, disabl
8835
8835
  ev.preventDefault();
8836
8836
  if (elementRef.current) {
8837
8837
  if (content !== elementRef.current.textContent) {
8838
- elementRef.current.innerHTML = runHighlight(elementRef.current.textContent, language);
8839
8838
  }
8839
+ elementRef.current.innerHTML = runHighlight(elementRef.current.textContent, language);
8840
8840
  }
8841
8841
  },
8842
8842
  onkeydown: (ev) => {
@@ -9272,7 +9272,7 @@ function FileEditor({ disableLog, disablePreview, disableReload, togglePanel, is
9272
9272
  apiPreview
9273
9273
  }
9274
9274
  ) : /* @__PURE__ */ JSX.createElement(JSX.Fragment, null),
9275
- previewPath ? /* @__PURE__ */ JSX.createElement(
9275
+ previewPath && isPanelVisible("right") ? /* @__PURE__ */ JSX.createElement(
9276
9276
  "div",
9277
9277
  {
9278
9278
  class: `file-editor-preview`,
@@ -10521,6 +10521,14 @@ function Editor(props) {
10521
10521
  const { files: files2, services: services2 } = await r.json();
10522
10522
  setfiles(files2);
10523
10523
  setservices(services2);
10524
+ for (const file of files2) {
10525
+ if (opened[file.filePath]) {
10526
+ opened[file.filePath] = {
10527
+ content: opened[file.filePath].content,
10528
+ ...file
10529
+ };
10530
+ }
10531
+ }
10524
10532
  }
10525
10533
  }
10526
10534
  function isDirCollapsed(dir) {
@@ -147,6 +147,14 @@ export function Editor(props) {
147
147
  const { files, services } = await r.json();
148
148
  setfiles(files);
149
149
  setservices(services);
150
+ for (const file of files) {
151
+ if (opened[file.filePath]) {
152
+ opened[file.filePath] = {
153
+ content: opened[file.filePath].content,
154
+ ...file
155
+ };
156
+ }
157
+ }
150
158
  }
151
159
  }
152
160
  function isDirCollapsed(dir) {
@@ -50,7 +50,7 @@ export function FileEditor({ disableLog, disablePreview, disableReload, togglePa
50
50
  }
51
51
  }, content: content, language: language }) : JSX.createElement("p", null, "binary data not supported")),
52
52
  apiPreview && apiPreview.length > 0 ? JSX.createElement(APIPReview, { isPanelVisible: isPanelVisible, apiPreview: apiPreview }) : JSX.createElement(JSX.Fragment, null),
53
- previewPath ? JSX.createElement("div", { class: `file-editor-preview`, style: `${!isPanelVisible("right") ? "display: none;" : ""}` },
53
+ previewPath && isPanelVisible("right") ? JSX.createElement("div", { class: `file-editor-preview`, style: `${!isPanelVisible("right") ? "display: none;" : ""}` },
54
54
  JSX.createElement("div", { class: "file-editor-preview-path" },
55
55
  JSX.createElement("a", { href: previewPath, target: "_blank" }, "open in new window")),
56
56
  JSX.createElement("iframe", { ref: iFrameRef, src: previewPath, sandbox: "allow-scripts allow-same-origin" })) : JSX.createElement(JSX.Fragment, null))));
@@ -94,8 +94,9 @@ export function HighlightTextArea({ content, language, oncontentchange, tabChar,
94
94
  if (elementRef.current) {
95
95
  if (content !== elementRef.current.textContent) {
96
96
  //setlastContent(String(elementRef.current.textContent));
97
- elementRef.current.innerHTML = runHighlight(elementRef.current.textContent, language);
97
+ //elementRef.current.innerHTML = runHighlight(elementRef.current.textContent, language);
98
98
  }
99
+ elementRef.current.innerHTML = runHighlight(elementRef.current.textContent, language);
99
100
  }
100
101
  }, onkeydown: ev => {
101
102
  if (ev.keyCode === 9) {
@@ -1,3 +1,4 @@
1
+ import { ServerOptions } from "node:https";
1
2
  interface MiqroJSON {
2
3
  services?: string[];
3
4
  port?: string | number;
@@ -6,6 +7,9 @@ interface MiqroJSON {
6
7
  browser?: string | boolean;
7
8
  logFile?: string | boolean;
8
9
  editor?: boolean;
10
+ https?: boolean;
11
+ serverOptions?: ServerOptions;
12
+ httpsRedirect?: number;
9
13
  }
10
14
  export declare function importMiqroJSON(inFile: string): MiqroJSON;
11
15
  export declare function getPORT(): string;
@@ -33,6 +37,9 @@ export interface Arguments {
33
37
  services: string[];
34
38
  editor: boolean;
35
39
  hotreload: boolean;
40
+ https: boolean;
41
+ serverOptions: ServerOptions;
42
+ httpsRedirect?: number;
36
43
  }
37
44
  /**
38
45
  * parse process.argv arguments
@@ -21,7 +21,10 @@ const MiqroJSONSchema = {
21
21
  inflateDir: "string?",
22
22
  browser: "boolean?|string?",
23
23
  logFile: "boolean?|string?",
24
- editor: "boolean?"
24
+ editor: "boolean?",
25
+ https: "boolean?",
26
+ serverOptions: "any?",
27
+ httpsRedirect: "number?"
25
28
  }
26
29
  };
27
30
  export function importMiqroJSON(inFile) {
@@ -44,6 +47,9 @@ export function parseArguments() {
44
47
  //env["LOG_FILE"] = env["LOG_FILE"] ? env["LOG_FILE"] : "./server.log";
45
48
  const args = cluster.isPrimary ? process.argv.slice(2, process.argv.length) : process.argv.slice(3, process.argv.length);
46
49
  const flags = {
50
+ httpsRedirect: null,
51
+ https: null,
52
+ serverOptions: {},
47
53
  name: null,
48
54
  browser: null,
49
55
  logFile: null,
@@ -130,6 +136,59 @@ export function parseArguments() {
130
136
  }
131
137
  flags.hotreload = true;
132
138
  continue;
139
+ case "--https":
140
+ if (flags.https !== null) {
141
+ console.error("bad arguments.");
142
+ console.error(usage);
143
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
144
+ }
145
+ flags.https = true;
146
+ continue;
147
+ case "--https-redirect":
148
+ if (flags.httpsRedirect !== null) {
149
+ console.error("bad arguments.");
150
+ console.error(usage);
151
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
152
+ }
153
+ const httpsRedirectPort = parseInt(String(args[i + 1]), 10);
154
+ if (isNaN(httpsRedirectPort)) {
155
+ console.error("bad arguments. --https-redirect must be a number.");
156
+ console.error(usage);
157
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
158
+ }
159
+ flags.httpsRedirect = httpsRedirectPort;
160
+ i++;
161
+ continue;
162
+ case "--https-cert":
163
+ if (flags.serverOptions.cert) {
164
+ console.error("bad arguments.");
165
+ console.error(usage);
166
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
167
+ }
168
+ const httpsCertPath = args[i + 1];
169
+ if (typeof httpsCertPath !== "string") {
170
+ console.error("bad arguments. --https-cert must be a string.");
171
+ console.error(usage);
172
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
173
+ }
174
+ flags.serverOptions.cert = readFileSync(httpsCertPath);
175
+ i++;
176
+ continue;
177
+ case "--https-key":
178
+ if (flags.serverOptions.key) {
179
+ console.error("bad arguments.");
180
+ console.error(usage);
181
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
182
+ }
183
+ const httpsKeyPath = args[i + 1];
184
+ if (typeof httpsKeyPath !== "string") {
185
+ console.error("bad arguments. --https-key must be a string.");
186
+ console.error(usage);
187
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
188
+ }
189
+ flags.serverOptions.key = readFileSync(httpsKeyPath);
190
+ i++;
191
+ continue;
133
192
  case "--port":
134
193
  if (flags.port !== null) {
135
194
  console.error("bad arguments.");
@@ -356,6 +415,7 @@ export function parseArguments() {
356
415
  flags.inflateDir = flags.inflateDir ? flags.inflateDir : undefined;
357
416
  const miqroJSONPath = !flags.disableMiqroJSON ? flags.miqroJSONPath ? resolve(flags.miqroJSONPath) : getMiqroJSONPath() : false;
358
417
  const miqroRC = miqroJSONPath ? importMiqroJSON(miqroJSONPath) : {};
418
+ //console.dir(miqroRC.serverOptions);
359
419
  // try to load .miqrorc
360
420
  if (!flags.disableMiqroJSON && miqroJSONPath) {
361
421
  if (services.length === 0) {
@@ -365,6 +425,32 @@ export function parseArguments() {
365
425
  }
366
426
  }
367
427
  }
428
+ if (flags.https === null) {
429
+ if (miqroRC.https) {
430
+ flags.https = miqroRC.https;
431
+ }
432
+ }
433
+ if (flags.httpsRedirect === null) {
434
+ if (miqroRC.httpsRedirect && flags.https) {
435
+ /*console.log("reading key from " + String(miqroRC.serverOptions?.key));
436
+ flags.serverOptions.key = readFileSync(String(miqroRC.serverOptions?.key));*/
437
+ flags.httpsRedirect = miqroRC.httpsRedirect;
438
+ }
439
+ }
440
+ if (!flags.serverOptions.key) {
441
+ if (miqroRC.serverOptions?.key) {
442
+ /*console.log("reading key from " + String(miqroRC.serverOptions?.key));
443
+ flags.serverOptions.key = readFileSync(String(miqroRC.serverOptions?.key));*/
444
+ flags.serverOptions.key = miqroRC.serverOptions?.key;
445
+ }
446
+ }
447
+ if (!flags.serverOptions.cert) {
448
+ if (miqroRC.serverOptions?.cert) {
449
+ /*console.log("reading cert from " + String(miqroRC.serverOptions?.cert));
450
+ flags.serverOptions.cert = readFileSync(String(miqroRC.serverOptions?.cert));*/
451
+ flags.serverOptions.cert = miqroRC.serverOptions?.cert;
452
+ }
453
+ }
368
454
  if (flags.port === null) {
369
455
  if (miqroRC.port) {
370
456
  flags.port = String(miqroRC.port);
@@ -460,6 +546,18 @@ export function parseArguments() {
460
546
  process.exit(EXIT_CODES.BAD_ARGUMENTS);
461
547
  }
462
548
  const generateDocType = flags.generateDocType ? flags.generateDocType : "MD";
549
+ if (flags.https && (!flags.serverOptions.key)) {
550
+ console.error("bad arguments. cannot use --https without --https-key. or set values in miqro.json file.");
551
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
552
+ }
553
+ if (flags.https && (!flags.serverOptions.cert)) {
554
+ console.error("bad arguments. cannot use --https without --https-cert. or set values in miqro.json file.");
555
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
556
+ }
557
+ if (flags.httpsRedirect && !flags.https) {
558
+ console.error("bad arguments. cannot use --https-redirect without --https. or set values in miqro.json file.");
559
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
560
+ }
463
561
  return {
464
562
  name: flags.name ? flags.name : undefined,
465
563
  browser: flags.browser !== null ? flags.browser : undefined,
@@ -483,6 +581,9 @@ export function parseArguments() {
483
581
  generateDocOut: flags.generateDocOut ? resolve(process.cwd(), flags.generateDocOut) : generateDocType === "MD" ? resolve(process.cwd(), "API.md") : resolve(process.cwd(), "API.json"),
484
582
  generateDocType,
485
583
  services,
486
- editor: flags.editor ? true : false
584
+ editor: flags.editor ? true : false,
585
+ https: flags.https ? true : false,
586
+ httpsRedirect: flags.httpsRedirect ? flags.httpsRedirect : undefined,
587
+ serverOptions: flags.serverOptions
487
588
  };
488
589
  }
@@ -1,3 +1,3 @@
1
1
  export declare const BIN_NAME = "miqro";
2
2
  export declare const usage = "usage: miqro [...FLAGS] --service app/\n\n==examples==\n\nmiqro --watch --service front/\nPORT=8181 miqro --service api/ --service front/\nmiqro --test --service front/\nmiqro --inflate --service front/\nmiqro --generate-doc --generate-doc-out API.md --service front/\nCLUSTER_COUNT=10 miqro-cluster --service api/";
3
- export declare const help = "\n==flags==\n\n-v, --version\n\toutputs the version number\n-h, --help\n\toutputs this page.\n--watch\n\tuse to enable the hot-reload functionality.\n--test\n\trun the tests for a service.\n--migrate-up\n\tmigrations up.\n--migrate-down\n\tmigrations down.\n--inflate\n\tinflates the application.\n--inflate-dir\n\tto set the output directory of the --inflate command. default value is inflated/.\n--editor\n\truns the application with a built-in editor.\n--generate-doc\n\tgenerates a documentation for the api endpoints of the service.\n--generate-doc-out\n\tthe output file for the generated documentation. default value is API.md.\n--generate-doc-type\n\tthe format of the generated documentation. it can be JSON or MD. default value is MD.\n--generate-doc-all\n\toutputs all the server routes in the documentation output.\n--compile\n\tinflates the application and tries to create a NODE SEA binary.\n--inflate-sea\n\tinflates the application with sea compilation scripts.\n--install-tsconfig\n\tcreates a tsconfig.json configured to use with --install-types.\n--install-types\n\tcreates and updates the .types/ folder use together with --install-tsconfig.\n--install-miqrojson\n\tcreates a default miqro.json file.\n--disable-miqrojson\n\tdisables the load of miqro.json file.\n--log-file\n\toverrides the default log file from LOG_FILE.\n--browser\n\toverrides the default browser from BROWSER.\n--config\n\toverrides the default miqro.json path.\n--port\n\toverrides the default port from PORT.\n--name\n\toverrides the default name of the server.\n\n==environment variables==\n\nPORT\n\toverride the default 8080 port.\nBROWSER\n\toverride the default browser. change to none to disable.\".\nLOG_FILE\n\toverride the default ./server.log file\nDB\n\tenable the server.db features\nDB_STORAGE\n\toverride the default local db location ./db.sqlite3\nDB_DIALECT\n\toverride the default node:sqlite\nDB_CONNECTION\n\toverride the default connection url\nCLEAR_JSX_CACHE\n\tset to 1 or 0 to enable or disable the clearing of the esbuild cache defaults to 1.\nJSX_TMP\n\tset custom location of esbuild builds defaults to /tmp/jsx_tmp.\n";
3
+ export declare const help = "\n==flags==\n\n-v, --version\n\toutputs the version number\n-h, --help\n\toutputs this page.\n--watch\n\tuse to enable the hot-reload functionality.\n--test\n\trun the tests for a service.\n--migrate-up\n\tmigrations up.\n--migrate-down\n\tmigrations down.\n--inflate\n\tinflates the application.\n--inflate-dir\n\tto set the output directory of the --inflate command. default value is inflated/.\n--editor\n\truns the application with a built-in editor.\n--generate-doc\n\tgenerates a documentation for the api endpoints of the service.\n--generate-doc-out\n\tthe output file for the generated documentation. default value is API.md.\n--generate-doc-type\n\tthe format of the generated documentation. it can be JSON or MD. default value is MD.\n--generate-doc-all\n\toutputs all the server routes in the documentation output.\n--compile\n\tinflates the application and tries to create a NODE SEA binary.\n--inflate-sea\n\tinflates the application with sea compilation scripts.\n--install-tsconfig\n\tcreates a tsconfig.json configured to use with --install-types.\n--install-types\n\tcreates and updates the .types/ folder use together with --install-tsconfig.\n--install-miqrojson\n\tcreates a default miqro.json file.\n--disable-miqrojson\n\tdisables the load of miqro.json file.\n--log-file\n\toverrides the default log file from LOG_FILE.\n--browser\n\toverrides the default browser from BROWSER.\n--config\n\toverrides the default miqro.json path.\n--port\n\toverrides the default port from PORT.\n--name\n\toverrides the default name of the server.\n--https\n\tserves the server in https instead of http\n--https-key\n\tpoint to a server.key file for https.\n--https-cert\n\tpoint to a server.cert file for https.\n--https-redirect\n\tserves an aditional http server that redirects to https. it needs a port number.\n\n==environment variables==\n\nPORT\n\toverride the default 8080 port.\nBROWSER\n\toverride the default browser. change to none to disable.\".\nLOG_FILE\n\toverride the default ./server.log file\nDB\n\tenable the server.db features\nDB_STORAGE\n\toverride the default local db location ./db.sqlite3\nDB_DIALECT\n\toverride the default node:sqlite\nDB_CONNECTION\n\toverride the default connection url\nCLEAR_JSX_CACHE\n\tset to 1 or 0 to enable or disable the clearing of the esbuild cache defaults to 1.\nJSX_TMP\n\tset custom location of esbuild builds defaults to /tmp/jsx_tmp.\n";
@@ -37,6 +37,10 @@ export const help = `
37
37
  --config\n\toverrides the default miqro.json path.
38
38
  --port\n\toverrides the default port from PORT.
39
39
  --name\n\toverrides the default name of the server.
40
+ --https\n\tserves the server in https instead of http
41
+ --https-key\n\tpoint to a server.key file for https.
42
+ --https-cert\n\tpoint to a server.cert file for https.
43
+ --https-redirect\n\tserves an aditional http server that redirects to https. it needs a port number.
40
44
 
41
45
  ==environment variables==
42
46
 
@@ -0,0 +1,49 @@
1
+ import { JWTDecryptOptions, JWTDecryptResult, JWTPayload, JWTVerifyOptions, JWTVerifyResult, ProtectedHeaderParameters } from "jose";
2
+ import { KeyObject } from "node:crypto";
3
+ import { EncryptJWTOptions, JWTSignOptions } from "../types.js";
4
+ /**
5
+ * creates a JWT encrypted token with jose
6
+ *
7
+ * @param payload the payload to encrypt
8
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
9
+ * @param options options like expiratation date, issuer and audience
10
+ * @returns
11
+ */
12
+ export declare function encryptJWT(payload: JWTPayload, secret: KeyObject, options?: Partial<EncryptJWTOptions>): Promise<string>;
13
+ /**
14
+ * decrypts a JWT token with jose
15
+ * @param jwt the JWT token
16
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
17
+ * @param options options like issuer and audience
18
+ * @returns
19
+ */
20
+ export declare function decryptJWT<PayloadType = JWTPayload>(jwt: string, secret: KeyObject, options?: Partial<JWTDecryptOptions>): Promise<JWTDecryptResult<PayloadType>>;
21
+ /**
22
+ * verify a JWT token with jose
23
+ * @param jwt the JWT token
24
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
25
+ * @param options options like issuer and audience
26
+ * @returns
27
+ */
28
+ export declare function verifyJWT<PayloadType = JWTPayload>(jwt: string, secret: KeyObject, options?: Partial<JWTVerifyOptions>): Promise<JWTVerifyResult<PayloadType>>;
29
+ /**
30
+ * creates a signed JWT with jose
31
+ *
32
+ * @param payload the payload to encrypt
33
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
34
+ * @param options options like expiratation date, issuer and audience
35
+ * @returns
36
+ */
37
+ export declare function signJWT(payload: JWTPayload, secret: KeyObject, options?: Partial<JWTSignOptions>): Promise<string>;
38
+ /**
39
+ * decodes a protected header with jose
40
+ * @param token
41
+ * @returns
42
+ */
43
+ export declare function decodeProtectedHeaderJWT(token: string | object): ProtectedHeaderParameters;
44
+ /**
45
+ * decodes a jwt token
46
+ * @param jwt
47
+ * @returns
48
+ */
49
+ export declare function decodeJWT<PayloadType = JWTPayload>(jwt: string): PayloadType & JWTPayload;
@@ -0,0 +1,76 @@
1
+ import { EncryptJWT, SignJWT, decodeJwt, decodeProtectedHeader, jwtDecrypt as joseJWTDecrypt, jwtVerify } from "jose";
2
+ /**
3
+ * creates a JWT encrypted token with jose
4
+ *
5
+ * @param payload the payload to encrypt
6
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
7
+ * @param options options like expiratation date, issuer and audience
8
+ * @returns
9
+ */
10
+ export async function encryptJWT(payload, secret, options) {
11
+ const en = new EncryptJWT(payload)
12
+ .setProtectedHeader({
13
+ alg: options?.alg ? options?.alg : 'dir',
14
+ enc: options?.enc ? options?.enc : 'A128CBC-HS256'
15
+ })
16
+ .setIssuedAt(options?.iat)
17
+ .setIssuer(options?.iss ? options?.iss : 'urn:example:issuer')
18
+ .setAudience(options?.aud ? options?.aud : 'urn:example:audience')
19
+ .setExpirationTime(options?.exp ? options?.exp : '2h');
20
+ return await en.encrypt(secret, options?.options);
21
+ }
22
+ /**
23
+ * decrypts a JWT token with jose
24
+ * @param jwt the JWT token
25
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
26
+ * @param options options like issuer and audience
27
+ * @returns
28
+ */
29
+ export async function decryptJWT(jwt, secret, options) {
30
+ return await joseJWTDecrypt(jwt, secret, options);
31
+ }
32
+ /**
33
+ * verify a JWT token with jose
34
+ * @param jwt the JWT token
35
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
36
+ * @param options options like issuer and audience
37
+ * @returns
38
+ */
39
+ export async function verifyJWT(jwt, secret, options) {
40
+ return await jwtVerify(jwt, secret, options);
41
+ }
42
+ /**
43
+ * creates a signed JWT with jose
44
+ *
45
+ * @param payload the payload to encrypt
46
+ * @param secret the secret example. const secret = createSecretKey(process.env.JWT_SECRET, 'utf-8');
47
+ * @param options options like expiratation date, issuer and audience
48
+ * @returns
49
+ */
50
+ export async function signJWT(payload, secret, options) {
51
+ const sign = new SignJWT(payload)
52
+ .setProtectedHeader({
53
+ alg: options?.alg ? options?.alg : 'HS256',
54
+ })
55
+ .setIssuedAt(options?.iat)
56
+ .setIssuer(options?.iss ? options?.iss : 'urn:example:issuer')
57
+ .setAudience(options?.aud ? options?.aud : 'urn:example:audience')
58
+ .setExpirationTime(options?.exp ? options?.exp : '2h');
59
+ return sign.sign(secret, options?.options);
60
+ }
61
+ /**
62
+ * decodes a protected header with jose
63
+ * @param token
64
+ * @returns
65
+ */
66
+ export function decodeProtectedHeaderJWT(token) {
67
+ return decodeProtectedHeader(token);
68
+ }
69
+ /**
70
+ * decodes a jwt token
71
+ * @param jwt
72
+ * @returns
73
+ */
74
+ export function decodeJWT(jwt) {
75
+ return decodeJwt(jwt);
76
+ }
@@ -1,5 +1,5 @@
1
1
  import { chmodSync, constants, mkdirSync, writeFileSync } from "node:fs";
2
- import { dirname, extname, join, relative, resolve } from "node:path";
2
+ import { basename, dirname, extname, join, relative, resolve } from "node:path";
3
3
  import { cwd, platform } from "node:process";
4
4
  import { getAuthConfigPath, getCORSConfigPath, getDBConfigPath, getErrorConfigPath, getMiddlewareConfigPath, getMigrationsPath, getServerConfigPath, getWSConfigPath } from "../common/paths.js";
5
5
  import { getAsset } from "../common/assets.js";
@@ -54,22 +54,22 @@ export async function inflateAppForSea(logger, inflateDir, services) {
54
54
  })`;
55
55
  }).join(",");
56
56
  writeFile(logger, join(inflateDir, "sea", "package.json"), `{ "type": "module", "private": true }`);
57
- writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { ServerInterfaceImpl, ServerRequestHandler, WebSocketManager, initGlobals, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
57
+ writeFile(logger, join(inflateDir, "sea", "app.cjs"), `const { createServerInterface, ServerRequestHandler, WebSocketManager, initGlobals, DBManager, App, LoggerHandler, LogProvider, LocalCache, ClusterCache } = require("./lib.cjs");
58
58
 
59
59
  async function main() {
60
60
  const PORT = process.env["PORT"] ? process.env["PORT"] : 8080;
61
- const logProvider = new LogProvider();
61
+ const loggerProvider = new LogProvider();
62
62
  const localCache = new LocalCache();
63
63
  const cache = new ClusterCache();
64
64
  const webSocketManager = new WebSocketManager();
65
65
  const dbManager = new DBManager();
66
66
  await initGlobals();
67
- const serverInterface = new ServerInterfaceImpl({
67
+ const serverInterface = createServerInterface({
68
68
  cache,
69
69
  localCache,
70
- logProvider,
70
+ loggerProvider,
71
71
  wsManager: webSocketManager,
72
- logger: logProvider.getLogger("server"),
72
+ logger: loggerProvider.getLogger("server"),
73
73
  dbManager,
74
74
  port: PORT
75
75
  });
@@ -118,9 +118,10 @@ ${Object.keys(serviceRouteFileMap)
118
118
  .map(filePath => serviceRouteFileMap[filePath])
119
119
  .filter(data => data.previewMethod === "api")
120
120
  .map(data => data.routes.map(r => {
121
- const rPath = r.inflatePath;
121
+ const rPath = join(relative(cwd(), dirname(data.filePath)), basename(data.filePath));
122
122
  if (rPath) {
123
- const apiInflatedPath = join("..", "..", service, "http", rPath + ".api.js");
123
+ const rPathExt = extname(rPath);
124
+ const apiInflatedPath = join("..", "..", rPath.substring(0, rPath.length - rPathExt.length) + ".js");
124
125
  return ` await appendAPIModule(router, "../../${service}/http", "./${apiInflatedPath}", (await import("./${apiInflatedPath}")).default);`;
125
126
  }
126
127
  else {
@@ -9,6 +9,7 @@ export interface RouteFileMap {
9
9
  options?: RouterHandlerOptions;
10
10
  inflatePath?: string;
11
11
  }[];
12
+ filePath: string;
12
13
  service: string;
13
14
  previewMethod: "api" | "html" | null;
14
15
  };
@@ -55,6 +55,7 @@ function createStaticRoute(service, logger, router, dir, file, inflateDir, route
55
55
  path: normalizePath(path)
56
56
  }],
57
57
  service,
58
+ filePath: file.filePath,
58
59
  previewMethod: "html"
59
60
  };
60
61
  if (inflateDir) {
@@ -137,6 +138,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
137
138
  routeFileMap[file.filePath] = {
138
139
  routes,
139
140
  service,
141
+ filePath: file.filePath,
140
142
  previewMethod: "api"
141
143
  };
142
144
  const inflatedCode = inflateDir ? await inflateJSX(file.filePath, {
@@ -175,6 +177,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
175
177
  routeFileMap[file.filePath] = {
176
178
  routes,
177
179
  service,
180
+ filePath: file.filePath,
178
181
  previewMethod: "html"
179
182
  };
180
183
  for (const r of routes) {
@@ -228,6 +231,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
228
231
  const routes = getRoutes(join("/", dirname(relative(dir, file.filePath))), file.subName + ".html", module.apiOptions);
229
232
  routeFileMap[file.filePath] = {
230
233
  routes,
234
+ filePath: file.filePath,
231
235
  service,
232
236
  previewMethod: "html"
233
237
  };
@@ -296,6 +300,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
296
300
  method: "GET",
297
301
  path
298
302
  }],
303
+ filePath: file.filePath,
299
304
  service,
300
305
  previewMethod: "html"
301
306
  };
@@ -345,6 +350,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
345
350
  path
346
351
  }],
347
352
  service,
353
+ filePath: file.filePath,
348
354
  previewMethod: "html"
349
355
  };
350
356
  if (inflateDir) {
@@ -402,6 +408,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
402
408
  method: "GET",
403
409
  path
404
410
  }],
411
+ filePath: file.filePath,
405
412
  service,
406
413
  previewMethod: "html"
407
414
  };
@@ -459,6 +466,7 @@ async function createRouterFromDirectory(server, hotreload, service, logger, dir
459
466
  path
460
467
  }],
461
468
  service,
469
+ filePath: file.filePath,
462
470
  previewMethod: "html"
463
471
  };
464
472
  if (inflateDir) {
@@ -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 { ServerInterfaceImpl, ServerInterfaceImplOptions } from "./services/utils/server-interface.js";
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";
@@ -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 { ServerInterfaceImpl } from "./services/utils/server-interface.js";
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";
@@ -21,7 +21,10 @@ async function main(args) {
21
21
  services: args.services,
22
22
  browser: args.browser,
23
23
  logFile: args.logFile,
24
- hotreload: args.test ? false : args.hotreload
24
+ hotreload: args.test ? false : args.hotreload,
25
+ https: args.test ? false : args.https,
26
+ serverOptions: args.serverOptions,
27
+ httpRedirect: args.test ? undefined : args.httpsRedirect
25
28
  });
26
29
  // check arguments
27
30
  if (args.generateDoc) {
@@ -11,6 +11,7 @@ import { EditorAdminInterface } from "../../editor/common/admin-interface.js";
11
11
  import { LogProvider, LogProviderOptions } from "./utils/log.js";
12
12
  import { MigrationModule } from "../inflate/setup-db.js";
13
13
  import { LogConfigMap } from "../inflate/setup-log.js";
14
+ import { ServerOptions } from "node:https";
14
15
  export interface MiqroOptions {
15
16
  name: string;
16
17
  logger?: Logger;
@@ -21,6 +22,9 @@ export interface MiqroOptions {
21
22
  browser?: string | boolean;
22
23
  logFile?: string | boolean;
23
24
  hotreload?: boolean;
25
+ serverOptions?: ServerOptions<any, any>;
26
+ https?: boolean;
27
+ httpRedirect?: number;
24
28
  }
25
29
  export interface InflateOptions {
26
30
  inflateDir?: string;
@@ -50,6 +54,7 @@ export declare class Miqro {
50
54
  status: "stopped" | "starting" | "stopping" | "reloading" | "started";
51
55
  options: MiqroOptions;
52
56
  server?: App | null;
57
+ httpsRedirectServer?: App | null;
53
58
  cache: ClusterCache;
54
59
  localCache: LocalCache;
55
60
  webSocketManager: WebSocketManager;