edge.libx.js 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
+ import express from "express";
1
2
  export declare function getExpress(): {
2
- app: any;
3
- router: any;
3
+ app: express.Express;
4
+ router: import("express-serve-static-core").Router;
4
5
  };
@@ -8,7 +8,7 @@ const body_parser_1 = __importDefault(require("body-parser"));
8
8
  const express_1 = __importDefault(require("express"));
9
9
  const cors_1 = __importDefault(require("cors"));
10
10
  function getExpress() {
11
- const app = (0, express_1.default)();
11
+ let app = (0, express_1.default)();
12
12
  app.set('json spaces', 4);
13
13
  var rawBodySaver = function (req, res, buf, encoding) {
14
14
  if (buf && buf.length) {
@@ -1 +1 @@
1
- {"version":3,"file":"getExpress.js","sourceRoot":"","sources":["../../src/helpers/getExpress.ts"],"names":[],"mappings":";;;;;AAIA,gCAoBC;AAxBD,8DAAqC;AACrC,sDAA8B;AAC9B,gDAAwB;AAExB,SAAgB,UAAU;IACzB,MAAM,GAAG,GAAG,IAAA,iBAAO,GAAE,CAAC;IAEtB,GAAG,CAAC,GAAG,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC;IAE1B,IAAI,YAAY,GAAG,UAAU,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ;QACnD,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;YACvB,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC;QAChD,CAAC;IACF,CAAC,CAAC;IAEF,GAAG,CAAC,GAAG,CAAC,qBAAU,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC;IACnD,GAAG,CAAC,GAAG,CAAC,qBAAU,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IACzE,GAAG,CAAC,GAAG,CAAC,qBAAU,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAE/D,GAAG,CAAC,GAAG,CAAC,IAAA,cAAI,GAAE,CAAC,CAAC;IAEhB,MAAM,MAAM,GAAG,iBAAO,CAAC,MAAM,EAAE,CAAC;IAEhC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC;AACxB,CAAC","sourcesContent":["import bodyParser from 'body-parser';\nimport express from 'express';\nimport cors from 'cors';\n\nexport function getExpress() {\n\tconst app = express();\n\n\tapp.set('json spaces', 4);\n\n\tvar rawBodySaver = function (req, res, buf, encoding) {\n\t\tif (buf && buf.length) {\n\t\t\treq.rawBody = buf.toString(encoding || 'utf8');\n\t\t}\n\t};\n\n\tapp.use(bodyParser.json({ verify: rawBodySaver }));\n\tapp.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true }));\n\tapp.use(bodyParser.raw({ verify: rawBodySaver, type: '*/*' }));\n\n\tapp.use(cors());\n\n\tconst router = express.Router();\n\n\treturn { app, router };\n}\n"]}
1
+ {"version":3,"file":"getExpress.js","sourceRoot":"","sources":["../../src/helpers/getExpress.ts"],"names":[],"mappings":";;;;;AAIA,gCAoBC;AAxBD,8DAAqC;AACrC,sDAA8D;AAC9D,gDAAwB;AAExB,SAAgB,UAAU;IACzB,IAAI,GAAG,GAAY,IAAA,iBAAO,GAAE,CAAC;IAE7B,GAAG,CAAC,GAAG,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC;IAE1B,IAAI,YAAY,GAAG,UAAU,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,QAAQ;QACnD,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;YACvB,GAAG,CAAC,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC;QAChD,CAAC;IACF,CAAC,CAAC;IAEF,GAAG,CAAC,GAAG,CAAC,qBAAU,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC;IACnD,GAAG,CAAC,GAAG,CAAC,qBAAU,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IACzE,GAAG,CAAC,GAAG,CAAC,qBAAU,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IAE/D,GAAG,CAAC,GAAG,CAAC,IAAA,cAAI,GAAE,CAAC,CAAC;IAEhB,MAAM,MAAM,GAAG,iBAAO,CAAC,MAAM,EAAE,CAAC;IAEhC,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC;AACxB,CAAC","sourcesContent":["import bodyParser from 'body-parser';\nimport express, { Express, Request, Response } from \"express\";\nimport cors from 'cors';\n\nexport function getExpress() {\n\tlet app: Express = express();\n\n\tapp.set('json spaces', 4);\n\n\tvar rawBodySaver = function (req, res, buf, encoding) {\n\t\tif (buf && buf.length) {\n\t\t\treq.rawBody = buf.toString(encoding || 'utf8');\n\t\t}\n\t};\n\n\tapp.use(bodyParser.json({ verify: rawBodySaver }));\n\tapp.use(bodyParser.urlencoded({ verify: rawBodySaver, extended: true }));\n\tapp.use(bodyParser.raw({ verify: rawBodySaver, type: '*/*' }));\n\n\tapp.use(cors());\n\n\tconst router = express.Router();\n\n\treturn { app, router };\n}\n"]}
@@ -2,7 +2,7 @@
2
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
- var _a;
5
+ var _a, _b, _c;
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const server_1 = require("@whatwg-node/server");
8
8
  require("isomorphic-fetch");
@@ -11,6 +11,8 @@ const fs_1 = __importDefault(require("fs"));
11
11
  const log_1 = require("libx.js/build/modules/log");
12
12
  const node_essentials_1 = require("libx.js/build/bundles/node.essentials");
13
13
  const path_1 = __importDefault(require("path"));
14
+ const https_1 = require("https");
15
+ const selfsigned_1 = __importDefault(require("selfsigned"));
14
16
  require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });
15
17
  log_1.log.isDebug = true;
16
18
  log_1.log.filterLevel = log_1.LogLevel.All;
@@ -42,14 +44,25 @@ if (['debug', 'debug:watch', 'api:debug'].indexOf(process.env.npm_lifecycle_even
42
44
  const { handler, prefix } = require(`${dir}/${entryPoint}`);
43
45
  app.use(prefix !== null && prefix !== void 0 ? prefix : '/api', (0, server_1.createServerAdapter)((request, env) => handler(request, env)));
44
46
  }
45
- const port = 8080;
47
+ const port = process.env.PORT || 8080;
46
48
  try {
47
- app.listen(port, () => {
48
- console.log(`Server listening on http://0.0.0.0:${port}`);
49
+ let server = app;
50
+ const isHttps = (_c = (_b = process.env.useHttps) !== null && _b !== void 0 ? _b : (node_essentials_1.libx.node.args.https != null)) !== null && _c !== void 0 ? _c : false;
51
+ if (isHttps) {
52
+ node_essentials_1.libx.log.v('localServer: using https');
53
+ const attrs = [{ name: 'commonName', value: 'localhost' }];
54
+ const pems = selfsigned_1.default.generate(attrs, { days: 365 });
55
+ server = (0, https_1.createServer)({
56
+ key: pems.private,
57
+ cert: pems.cert,
58
+ }, app);
59
+ }
60
+ server.listen(port, () => {
61
+ console.log(`Server listening on http${isHttps ? 's' : ''}://0.0.0.0:${port}`);
49
62
  });
50
63
  }
51
64
  catch (err) {
52
- console.warn(`LOCAL: Failed to start local server on port: ${port}`);
65
+ console.error(`LOCAL: Failed to start local server on port: ${port}`, err);
53
66
  }
54
67
  }
55
68
  //# sourceMappingURL=localServer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"localServer.js","sourceRoot":"","sources":["../../src/helpers/localServer.ts"],"names":[],"mappings":";;;;;;AAAA,gDAA0D;AAC1D,4BAA0B;AAG1B,6CAA0C;AAE1C,4CAAoB;AACpB,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AACZ,CAAC;AAED,sBAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,GAAG,EAAE,EAAE;IAC7B,OAAO,CAAC,KAAK,CAAC,gBAAgB,EAAE,GAAG,CAAC,CAAC;AACtC,CAAC,EAAE,KAAK,CAAC,CAAC;AAKV,IAAI,CAAC,OAAO,EAAE,aAAa,EAAE,WAAW,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;IAC3F,MAAM,GAAG,GAAG,MAAA,UAAU,EAAE,mCAAI,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;IAErC,KAAK,IAAI,UAAU,IAAI,WAAW,EAAE,CAAC;QACpC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QAC1B,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,GAAG,GAAG,IAAI,UAAU,EAAE,CAAC,CAAC;QAC5D,GAAG,CAAC,GAAG,CACN,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,MAAM,EAChB,IAAA,4BAAmB,EAAC,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CACpC,OAAO,CAAW,OAAO,EAAE,GAAG,CAAC,CAAC,CACjC,CAAC;IACH,CAAC;IASD,MAAM,IAAI,GAAG,IAAI,CAAC;IAClB,IAAI,CAAC;QACJ,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;YACrB,OAAO,CAAC,GAAG,CAAC,sCAAsC,IAAI,EAAE,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,IAAI,CAAC,gDAAgD,IAAI,EAAE,CAAC,CAAC;IACtE,CAAC;AACF,CAAC","sourcesContent":["import { createServerAdapter } from '@whatwg-node/server';\nimport 'isomorphic-fetch';\nimport { IRequest, Router, error, json } from 'itty-router';\nimport express from 'express';\nimport { getExpress } from './getExpress';\n// import { router, server } from '../v5';\nimport fs from 'fs';\nimport { log, LogLevel } from 'libx.js/build/modules/log';\nimport { libx } from 'libx.js/build/bundles/node.essentials';\nimport path from 'path';\n\nrequire('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });\n\nlog.isDebug = true;\nlog.filterLevel = LogLevel.All;\n\nconst envFile = '.env'; //'.dev.vars'\n\nconst entryPoints = libx.node.args._;\n\nconst { app } = getExpress();\n\n// node-wrangler bridge:\n// global.crypto = require('crypto');\n// global.TransformStream = require('web-streams-polyfill').TransformStream;\n\nfunction getEnvVars() {\n\tconst envFilePath = path.join(process.cwd(), envFile);\n\tif (!fs.existsSync(envFilePath)) return null;\n\tconst content = fs.readFileSync(envFilePath)?.toString();\n\tconst ret = {};\n\tfor (let line of content.split('\\n')) {\n\t\tconst parts = line.split('=');\n\t\tif (parts[0].trim().startsWith('#') || parts[1] == null) continue;\n\t\tret[parts[0].trim()] = parts[1].replace(/\\s*[\\\"\\'](.+)[\\\"\\']\\s*/gi, '$1');\n\t}\n\treturn ret;\n}\n\nlibx.node.catchErrors((err) => {\n\tconsole.error('!!!!!! ERROR: ', err);\n}, false);\n\n\n\n// only lunch manual local server if using 'dev' command\nif (['debug', 'debug:watch', 'api:debug'].indexOf(process.env.npm_lifecycle_event) !== -1) {\n\tconst env = getEnvVars() ?? { a: 1 };\n\n\tfor (let entryPoint of entryPoints) {\n\t\tconst dir = process.cwd(); // process.argv[1]\n\t\tconst { handler, prefix } = require(`${dir}/${entryPoint}`);\n\t\tapp.use(\n\t\t\tprefix ?? '/api',\n\t\t\tcreateServerAdapter((request, env) =>\n\t\t\t\thandler(<IRequest>request, env))\n\t\t);\n\t}\n\n\t// app.use(\n\t// \t'/v5-n',\n\t// \tcreateServerAdapter((request, env) =>\n\t// \t\thandler_node(<IRequest>request, env))\n\t// \t\t// handlerRedirect(<IRequest>request, env))\n\t// );\n\n\tconst port = 8080;\n\ttry {\n\t\tapp.listen(port, () => {\n\t\t\tconsole.log(`Server listening on http://0.0.0.0:${port}`);\n\t\t});\n\t} catch (err) {\n\t\tconsole.warn(`LOCAL: Failed to start local server on port: ${port}`);\n\t}\n}\n\n// export default server; //routes.fetch;"]}
1
+ {"version":3,"file":"localServer.js","sourceRoot":"","sources":["../../src/helpers/localServer.ts"],"names":[],"mappings":";;;;;;AACA,gDAA0D;AAC1D,4BAA0B;AAG1B,6CAA0C;AAE1C,4CAAoB;AACpB,mDAA0D;AAC1D,2EAA6D;AAC7D,gDAAwB;AACxB,iCAA4C;AAC5C,4DAAoC;AAEpC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,QAAQ,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;AAEnE,SAAG,CAAC,OAAO,GAAG,IAAI,CAAC;AACnB,SAAG,CAAC,WAAW,GAAG,cAAQ,CAAC,GAAG,CAAC;AAE/B,MAAM,OAAO,GAAG,MAAM,CAAC;AACvB,MAAM,WAAW,GAAG,sBAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;AAErC,MAAM,EAAE,GAAG,EAAE,GAAG,IAAA,uBAAU,GAAE,CAAC;AAM7B,SAAS,UAAU;;IAClB,MAAM,WAAW,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,CAAC;IACtD,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,WAAW,CAAC;QAAE,OAAO,IAAI,CAAC;IAC7C,MAAM,OAAO,GAAG,MAAA,YAAE,CAAC,YAAY,CAAC,WAAW,CAAC,0CAAE,QAAQ,EAAE,CAAC;IACzD,MAAM,GAAG,GAAG,EAAE,CAAC;IACf,KAAK,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;QACtC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC9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AI,MAAM,GAA2B,GAAG,CAAC;QAEzC,MAAM,OAAO,GAAG,MAAA,MAAA,OAAO,CAAC,GAAG,CAAC,QAAQ,mCAAI,CAAC,sBAAI,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,mCAAI,KAAK,CAAC;QAChF,IAAI,OAAO,EAAE,CAAC;YACb,sBAAI,CAAC,GAAG,CAAC,CAAC,CAAC,0BAA0B,CAAC,CAAC;YACvC,MAAM,KAAK,GAAG,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC;YAC3D,MAAM,IAAI,GAAG,oBAAU,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;YAEvD,MAAM,GAAG,IAAA,oBAAY,EAAC;gBACrB,GAAG,EAAE,IAAI,CAAC,OAAO;gBACjB,IAAI,EAAE,IAAI,CAAC,IAAI;aACf,EAAE,GAAG,CAAC,CAAC;QACT,CAAC;QAED,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,EAAE;YACxB,OAAO,CAAC,GAAG,CAAC,2BAA2B,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,cAAc,IAAI,EAAE,CAAC,CAAC;QAChF,CAAC,CAAC,CAAC;IACJ,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,KAAK,CAAC,gDAAgD,IAAI,EAAE,EAAE,GAAG,CAAC,CAAC;IAC5E,CAAC;AACF,CAAC","sourcesContent":["// $ npm_lifecycle_event=debug tsx --inspect -r dotenv/config ./src/helpers/localServer.ts --https\nimport { createServerAdapter } from '@whatwg-node/server';\nimport 'isomorphic-fetch';\nimport { IRequest, Router, error, json } from 'itty-router';\nimport express, { Express, Request, Response } from \"express\";\nimport { getExpress } from './getExpress';\n// import { router, server } from '../v5';\nimport fs from 'fs';\nimport { log, LogLevel } from 'libx.js/build/modules/log';\nimport { libx } from 'libx.js/build/bundles/node.essentials';\nimport path from 'path';\nimport https, { createServer } from 'https';\nimport selfsigned from 'selfsigned';\n\nrequire('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });\n\nlog.isDebug = true;\nlog.filterLevel = LogLevel.All;\n\nconst envFile = '.env'; //'.dev.vars'\nconst entryPoints = libx.node.args._;\n\nconst { app } = getExpress();\n\n// node-wrangler bridge:\n// global.crypto = require('crypto');\n// global.TransformStream = require('web-streams-polyfill').TransformStream;\n\nfunction getEnvVars() {\n\tconst envFilePath = path.join(process.cwd(), envFile);\n\tif (!fs.existsSync(envFilePath)) return null;\n\tconst content = fs.readFileSync(envFilePath)?.toString();\n\tconst ret = {};\n\tfor (let line of content.split('\\n')) {\n\t\tconst parts = line.split('=');\n\t\tif (parts[0].trim().startsWith('#') || parts[1] == null) continue;\n\t\tret[parts[0].trim()] = parts[1].replace(/\\s*[\\\"\\'](.+)[\\\"\\']\\s*/gi, '$1');\n\t}\n\treturn ret;\n}\n\nlibx.node.catchErrors((err) => {\n\tconsole.error('!!!!!! ERROR: ', err);\n}, false);\n\n\n// only lunch manual local server if using 'dev' command\nif (['debug', 'debug:watch', 'api:debug'].indexOf(process.env.npm_lifecycle_event) !== -1) {\n\tconst env = getEnvVars() ?? { a: 1 };\n\n\tfor (let entryPoint of entryPoints) {\n\t\tconst dir = process.cwd(); // process.argv[1]\n\t\tconst { handler, prefix } = require(`${dir}/${entryPoint}`);\n\t\tapp.use(\n\t\t\tprefix ?? '/api',\n\t\t\tcreateServerAdapter((request, env) =>\n\t\t\t\thandler(<IRequest>request, env))\n\t\t);\n\t}\n\n\t// app.use(\n\t// \t'/v5-n',\n\t// \tcreateServerAdapter((request, env) =>\n\t// \t\thandler_node(<IRequest>request, env))\n\t// \t\t// handlerRedirect(<IRequest>request, env))\n\t// );\n\n\tconst port = process.env.PORT || 8080;\n\ttry {\n\t\tlet server: https.Server | Express = app;\n\n\t\tconst isHttps = process.env.useHttps ?? (libx.node.args.https != null) ?? false;\n\t\tif (isHttps) {\n\t\t\tlibx.log.v('localServer: using https');\n\t\t\tconst attrs = [{ name: 'commonName', value: 'localhost' }];\n\t\t\tconst pems = selfsigned.generate(attrs, { days: 365 });\n\n\t\t\tserver = createServer({\n\t\t\t\tkey: pems.private,\n\t\t\t\tcert: pems.cert,\n\t\t\t}, app);\n\t\t}\n\n\t\tserver.listen(port, () => {\n\t\t\tconsole.log(`Server listening on http${isHttps ? 's' : ''}://0.0.0.0:${port}`);\n\t\t});\n\t} catch (err) {\n\t\tconsole.error(`LOCAL: Failed to start local server on port: ${port}`, err);\n\t}\n}\n\n// export default server; //routes.fetch;"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "edge.libx.js",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "main": "build/main.js",
5
5
  "license": "MIT",
6
6
  "author": "Elya Livshitz",
@@ -38,13 +38,16 @@
38
38
  "ua-parser-js": "^1.0.39"
39
39
  },
40
40
  "devDependencies": {
41
+ "@types/express": "^5.0.1",
41
42
  "@types/jest": "^29.5.5",
42
- "@types/node": "^20.8.0",
43
+ "@types/node": "^22.13.12",
43
44
  "dotenv": "^16.4.5",
45
+ "https": "^1.0.0",
44
46
  "jest": "^29.7.0",
45
47
  "jest-junit": "^16.0.0",
46
48
  "jest-junit-reporter": "^1.1.0",
47
49
  "prettier": "^3.0.3",
50
+ "selfsigned": "^2.4.1",
48
51
  "ts-jest": "^29.1.1",
49
52
  "typescript": "^5.2.2"
50
53
  },
@@ -1,9 +1,9 @@
1
1
  import bodyParser from 'body-parser';
2
- import express from 'express';
2
+ import express, { Express, Request, Response } from "express";
3
3
  import cors from 'cors';
4
4
 
5
5
  export function getExpress() {
6
- const app = express();
6
+ let app: Express = express();
7
7
 
8
8
  app.set('json spaces', 4);
9
9
 
@@ -1,13 +1,16 @@
1
+ // $ npm_lifecycle_event=debug tsx --inspect -r dotenv/config ./src/helpers/localServer.ts --https
1
2
  import { createServerAdapter } from '@whatwg-node/server';
2
3
  import 'isomorphic-fetch';
3
4
  import { IRequest, Router, error, json } from 'itty-router';
4
- import express from 'express';
5
+ import express, { Express, Request, Response } from "express";
5
6
  import { getExpress } from './getExpress';
6
7
  // import { router, server } from '../v5';
7
8
  import fs from 'fs';
8
9
  import { log, LogLevel } from 'libx.js/build/modules/log';
9
10
  import { libx } from 'libx.js/build/bundles/node.essentials';
10
11
  import path from 'path';
12
+ import https, { createServer } from 'https';
13
+ import selfsigned from 'selfsigned';
11
14
 
12
15
  require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });
13
16
 
@@ -15,7 +18,6 @@ log.isDebug = true;
15
18
  log.filterLevel = LogLevel.All;
16
19
 
17
20
  const envFile = '.env'; //'.dev.vars'
18
-
19
21
  const entryPoints = libx.node.args._;
20
22
 
21
23
  const { app } = getExpress();
@@ -42,7 +44,6 @@ libx.node.catchErrors((err) => {
42
44
  }, false);
43
45
 
44
46
 
45
-
46
47
  // only lunch manual local server if using 'dev' command
47
48
  if (['debug', 'debug:watch', 'api:debug'].indexOf(process.env.npm_lifecycle_event) !== -1) {
48
49
  const env = getEnvVars() ?? { a: 1 };
@@ -64,13 +65,27 @@ if (['debug', 'debug:watch', 'api:debug'].indexOf(process.env.npm_lifecycle_even
64
65
  // // handlerRedirect(<IRequest>request, env))
65
66
  // );
66
67
 
67
- const port = 8080;
68
+ const port = process.env.PORT || 8080;
68
69
  try {
69
- app.listen(port, () => {
70
- console.log(`Server listening on http://0.0.0.0:${port}`);
70
+ let server: https.Server | Express = app;
71
+
72
+ const isHttps = process.env.useHttps ?? (libx.node.args.https != null) ?? false;
73
+ if (isHttps) {
74
+ libx.log.v('localServer: using https');
75
+ const attrs = [{ name: 'commonName', value: 'localhost' }];
76
+ const pems = selfsigned.generate(attrs, { days: 365 });
77
+
78
+ server = createServer({
79
+ key: pems.private,
80
+ cert: pems.cert,
81
+ }, app);
82
+ }
83
+
84
+ server.listen(port, () => {
85
+ console.log(`Server listening on http${isHttps ? 's' : ''}://0.0.0.0:${port}`);
71
86
  });
72
87
  } catch (err) {
73
- console.warn(`LOCAL: Failed to start local server on port: ${port}`);
88
+ console.error(`LOCAL: Failed to start local server on port: ${port}`, err);
74
89
  }
75
90
  }
76
91