appstage 0.2.28 → 0.3.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/dist/bin.js CHANGED
@@ -690,9 +690,9 @@ async function build(params) {
690
690
  let serverProcess = null;
691
691
  let inited = false;
692
692
  let nodeArgs = [`${params.serverDir}/server/index.js`];
693
+ let envFiles = null;
693
694
  if (params.useEnvFiles !== false) {
694
- let envFiles = await getEnvFiles();
695
- for (let envFile of envFiles) log(`Using ${envFile}`);
695
+ envFiles = await getEnvFiles();
696
696
  nodeArgs.unshift(...envFiles.map((file) => `--env-file=${file}`));
697
697
  }
698
698
  function handleServerRebuild() {
@@ -702,6 +702,9 @@ async function build(params) {
702
702
  }
703
703
  if (!inited) {
704
704
  log(`Build completed +${(0, import_dateshape.formatDuration)(Date.now() - startTime)}`);
705
+ if (envFiles) {
706
+ for (let envFile of envFiles) log(`Using ${envFile}`);
707
+ }
705
708
  inited = true;
706
709
  }
707
710
  if (params.start)
@@ -764,10 +767,8 @@ async function run() {
764
767
  if (scriptName === "build") return await cli(args);
765
768
  let nodeEnv = nodeEnvMap[scriptName];
766
769
  if (nodeEnv !== void 0) process.env.NODE_ENV = nodeEnv;
767
- if (args.length !== 0 && !(0, import_args_json2.isKey)(args[0])) {
768
- let [hostname, port] = args[0].split(":");
769
- if (hostname) process.env.APP_HOST = hostname;
770
- if (port) process.env.APP_PORT = port;
770
+ if (args.length !== 0 && args[0] && !(0, import_args_json2.isKey)(args[0])) {
771
+ process.env.SERVER_URL = args[0];
771
772
  args.shift();
772
773
  }
773
774
  if (!(0, import_args_json2.hasKey)("--client-dir")) args.push("--client-dir", "src/public/-");
package/dist/index.cjs CHANGED
@@ -612,9 +612,9 @@ async function build(params) {
612
612
  let serverProcess = null;
613
613
  let inited = false;
614
614
  let nodeArgs = [`${params.serverDir}/server/index.js`];
615
+ let envFiles = null;
615
616
  if (params.useEnvFiles !== false) {
616
- let envFiles = await getEnvFiles();
617
- for (let envFile of envFiles) log(`Using ${envFile}`);
617
+ envFiles = await getEnvFiles();
618
618
  nodeArgs.unshift(...envFiles.map((file) => `--env-file=${file}`));
619
619
  }
620
620
  function handleServerRebuild() {
@@ -624,6 +624,7 @@ async function build(params) {
624
624
  }
625
625
  if (!inited) {
626
626
  log(`Build completed +${(0, dateshape.formatDuration)(Date.now() - startTime)}`);
627
+ if (envFiles) for (let envFile of envFiles) log(`Using ${envFile}`);
627
628
  inited = true;
628
629
  }
629
630
  if (params.start) serverProcess = (0, node_child_process.spawn)("node", nodeArgs, { stdio: "inherit" });
@@ -671,6 +672,33 @@ async function cli(input = []) {
671
672
  await build(params);
672
673
  }
673
674
 
675
+ const defaultPorts = {
676
+ "http:": 80,
677
+ "https:": 443
678
+ };
679
+ function getServerURL() {
680
+ let { SERVER_URL: href, SERVER_HOSTNAME: hostname = "localhost", SERVER_PORT: port = "3000" } = process.env;
681
+ let protocol = "http:";
682
+ if (href) {
683
+ if (href.startsWith("//")) href = `${protocol}${href}`;
684
+ else if (!href.includes("://")) href = `${protocol}//${href}`;
685
+ else protocol = href.slice(0, href.indexOf("://") + 1);
686
+ let matches = href.match(/^\w+:\/\/([^/:]+)(:(\d+))?(\/|$)/);
687
+ if (matches?.[1]) hostname = matches[1];
688
+ if (matches?.[3]) port = matches[3];
689
+ } else href = `${protocol}//${hostname}:${port}`;
690
+ let parsedPort = Number(port);
691
+ let origin = `${protocol}//${hostname}`;
692
+ if (parsedPort !== defaultPorts[protocol]) origin += `:${port}`;
693
+ return {
694
+ href,
695
+ origin,
696
+ protocol,
697
+ hostname,
698
+ port: parsedPort
699
+ };
700
+ }
701
+
674
702
  const renderStatus = async (req, res) => {
675
703
  let { id, nonce } = req.ctx;
676
704
  let statusText = `${res.statusCode} ${node_http.STATUS_CODES[res.statusCode]}`;
@@ -681,11 +709,10 @@ const renderStatus = async (req, res) => {
681
709
  function createApp(callback) {
682
710
  let app = (0, express.default)();
683
711
  if (!app.events) app.events = new node_events.default();
684
- let host = process.env.APP_HOST || "localhost";
685
- let port = Number(process.env.APP_PORT) || 3e3;
686
712
  let listen = () => {
687
- app.listen(port, host, () => {
688
- emitLog(app, `Server running at ${`http://${host}:${port}/`} (${`NODE_ENV=${process.env.NODE_ENV}`})`);
713
+ let { href, protocol, hostname, port } = getServerURL();
714
+ if (protocol === "http:") app.listen(port, hostname, () => {
715
+ emitLog(app, `Server running at ${href} (NODE_ENV=${process.env.NODE_ENV})`);
689
716
  });
690
717
  };
691
718
  if (process.env.NODE_ENV === "development") app.events?.on("log", ({ message, ...payload }) => {
@@ -741,6 +768,7 @@ exports.emitLog = emitLog;
741
768
  exports.files = files;
742
769
  exports.getEffectiveLocale = getEffectiveLocale;
743
770
  exports.getLocales = getLocales;
771
+ exports.getServerURL = getServerURL;
744
772
  exports.getStatusMessage = getStatusMessage;
745
773
  exports.init = init;
746
774
  exports.injectNonce = injectNonce;
package/dist/index.d.ts CHANGED
@@ -1127,6 +1127,14 @@ declare const cspNonce: (req: IncomingMessage) => string;
1127
1127
 
1128
1128
  declare function emitLog(app: Application, message?: LogEventPayload["message"] | LogEventPayload, payload?: LogEventPayload): any;
1129
1129
 
1130
+ declare function getServerURL(): {
1131
+ href: string;
1132
+ origin: string;
1133
+ protocol: string;
1134
+ hostname: string;
1135
+ port: number;
1136
+ };
1137
+
1130
1138
  declare function getStatusMessage(prefix: string, statusCode: number): string;
1131
1139
 
1132
1140
  declare const injectNonce: TransformContent;
@@ -1142,4 +1150,4 @@ declare function servePipeableStream(req: Request, res: Response): ({
1142
1150
  pipe
1143
1151
  }: PipeableStream, error?: unknown) => Promise<void>;
1144
1152
 
1145
- export { Controller, ErrorController, FilesParams, LangParams, LogEventPayload, LogLevel, LogOptions, Middleware, MiddlewareSet, RedirectParams, RenderStatus, ReqCtx, TransformContent, build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang, log, redirect, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
1153
+ export { Controller, ErrorController, FilesParams, LangParams, LogEventPayload, LogLevel, LogOptions, Middleware, MiddlewareSet, RedirectParams, RenderStatus, ReqCtx, TransformContent, build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getServerURL, getStatusMessage, init, injectNonce, lang, log, redirect, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
package/dist/index.mjs CHANGED
@@ -586,9 +586,9 @@ async function build(params) {
586
586
  let serverProcess = null;
587
587
  let inited = false;
588
588
  let nodeArgs = [`${params.serverDir}/server/index.js`];
589
+ let envFiles = null;
589
590
  if (params.useEnvFiles !== false) {
590
- let envFiles = await getEnvFiles();
591
- for (let envFile of envFiles) log(`Using ${envFile}`);
591
+ envFiles = await getEnvFiles();
592
592
  nodeArgs.unshift(...envFiles.map((file) => `--env-file=${file}`));
593
593
  }
594
594
  function handleServerRebuild() {
@@ -598,6 +598,7 @@ async function build(params) {
598
598
  }
599
599
  if (!inited) {
600
600
  log(`Build completed +${formatDuration(Date.now() - startTime)}`);
601
+ if (envFiles) for (let envFile of envFiles) log(`Using ${envFile}`);
601
602
  inited = true;
602
603
  }
603
604
  if (params.start) serverProcess = spawn("node", nodeArgs, { stdio: "inherit" });
@@ -645,6 +646,33 @@ async function cli(input = []) {
645
646
  await build(params);
646
647
  }
647
648
 
649
+ const defaultPorts = {
650
+ "http:": 80,
651
+ "https:": 443
652
+ };
653
+ function getServerURL() {
654
+ let { SERVER_URL: href, SERVER_HOSTNAME: hostname = "localhost", SERVER_PORT: port = "3000" } = process.env;
655
+ let protocol = "http:";
656
+ if (href) {
657
+ if (href.startsWith("//")) href = `${protocol}${href}`;
658
+ else if (!href.includes("://")) href = `${protocol}//${href}`;
659
+ else protocol = href.slice(0, href.indexOf("://") + 1);
660
+ let matches = href.match(/^\w+:\/\/([^/:]+)(:(\d+))?(\/|$)/);
661
+ if (matches?.[1]) hostname = matches[1];
662
+ if (matches?.[3]) port = matches[3];
663
+ } else href = `${protocol}//${hostname}:${port}`;
664
+ let parsedPort = Number(port);
665
+ let origin = `${protocol}//${hostname}`;
666
+ if (parsedPort !== defaultPorts[protocol]) origin += `:${port}`;
667
+ return {
668
+ href,
669
+ origin,
670
+ protocol,
671
+ hostname,
672
+ port: parsedPort
673
+ };
674
+ }
675
+
648
676
  const renderStatus = async (req, res) => {
649
677
  let { id, nonce } = req.ctx;
650
678
  let statusText = `${res.statusCode} ${STATUS_CODES[res.statusCode]}`;
@@ -655,11 +683,10 @@ const renderStatus = async (req, res) => {
655
683
  function createApp(callback) {
656
684
  let app = express();
657
685
  if (!app.events) app.events = new EventEmitter();
658
- let host = process.env.APP_HOST || "localhost";
659
- let port = Number(process.env.APP_PORT) || 3e3;
660
686
  let listen = () => {
661
- app.listen(port, host, () => {
662
- emitLog(app, `Server running at ${`http://${host}:${port}/`} (${`NODE_ENV=${process.env.NODE_ENV}`})`);
687
+ let { href, protocol, hostname, port } = getServerURL();
688
+ if (protocol === "http:") app.listen(port, hostname, () => {
689
+ emitLog(app, `Server running at ${href} (NODE_ENV=${process.env.NODE_ENV})`);
663
690
  });
664
691
  };
665
692
  if (process.env.NODE_ENV === "development") app.events?.on("log", ({ message, ...payload }) => {
@@ -707,4 +734,4 @@ function servePipeableStream(req, res) {
707
734
  };
708
735
  }
709
736
 
710
- export { build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getStatusMessage, init, injectNonce, lang, log, redirect, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
737
+ export { build, cli, createApp, cspNonce, emitLog, files, getEffectiveLocale, getLocales, getServerURL, getStatusMessage, init, injectNonce, lang, log, redirect, renderStatus, requestEvents, serializeState, servePipeableStream, toLanguage, unhandledError, unhandledRoute };
package/index.ts CHANGED
@@ -24,6 +24,7 @@ export * from "./src/types/TransformContent.ts";
24
24
  export * from "./src/utils/createApp.ts";
25
25
  export * from "./src/utils/cspNonce.ts";
26
26
  export * from "./src/utils/emitLog.ts";
27
+ export * from "./src/utils/getServerURL.ts";
27
28
  export * from "./src/utils/getStatusMessage.ts";
28
29
  export * from "./src/utils/injectNonce.ts";
29
30
  export * from "./src/utils/renderStatus.ts";
package/package.json CHANGED
@@ -1,36 +1,36 @@
1
- {
2
- "name": "appstage",
3
- "version": "0.2.28",
4
- "description": "",
5
- "main": "./dist/index.cjs",
6
- "module": "./dist/index.mjs",
7
- "types": "./dist/index.d.ts",
8
- "type": "module",
9
- "bin": {
10
- "appstage": "./dist/bin.js"
11
- },
12
- "scripts": {
13
- "compile-bin": "esbuild src/scripts/bin.ts --bundle --outfile=dist/bin.js --platform=node --external:esbuild --format=esm",
14
- "preversion": "npx npm-run-all shape compile-bin",
15
- "shape": "npx codeshape",
16
- "typecheck": "npx codeshape typecheck"
17
- },
18
- "repository": {
19
- "type": "git",
20
- "url": "git+https://github.com/axtk/appstage.git"
21
- },
22
- "author": "axtk",
23
- "license": "MIT",
24
- "peerDependencies": {
25
- "express": ">=5"
26
- },
27
- "devDependencies": {
28
- "@types/express": "^5.0.6",
29
- "@types/node": "^25.4.0"
30
- },
31
- "dependencies": {
32
- "args-json": "^1.4.3",
33
- "dateshape": "^1.1.2",
34
- "esbuild": "^0.28.0"
35
- }
36
- }
1
+ {
2
+ "name": "appstage",
3
+ "version": "0.3.0",
4
+ "description": "",
5
+ "main": "./dist/index.cjs",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "type": "module",
9
+ "bin": {
10
+ "appstage": "./dist/bin.js"
11
+ },
12
+ "scripts": {
13
+ "compile-bin": "esbuild src/scripts/bin.ts --bundle --outfile=dist/bin.js --platform=node --external:esbuild --format=esm",
14
+ "preversion": "npx npm-run-all shape compile-bin",
15
+ "shape": "npx codeshape",
16
+ "typecheck": "npx codeshape typecheck"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/axtk/appstage.git"
21
+ },
22
+ "author": "axtk",
23
+ "license": "MIT",
24
+ "peerDependencies": {
25
+ "express": ">=5"
26
+ },
27
+ "devDependencies": {
28
+ "@types/express": "^5.0.6",
29
+ "@types/node": "^25.4.0"
30
+ },
31
+ "dependencies": {
32
+ "args-json": "^1.4.3",
33
+ "dateshape": "^1.1.2",
34
+ "esbuild": "^0.28.0"
35
+ }
36
+ }
@@ -23,12 +23,8 @@ async function run() {
23
23
 
24
24
  if (nodeEnv !== undefined) process.env.NODE_ENV = nodeEnv;
25
25
 
26
- if (args.length !== 0 && !isKey(args[0])) {
27
- let [hostname, port] = args[0].split(":");
28
-
29
- if (hostname) process.env.APP_HOST = hostname;
30
- if (port) process.env.APP_PORT = port;
31
-
26
+ if (args.length !== 0 && args[0] && !isKey(args[0])) {
27
+ process.env.SERVER_URL = args[0];
32
28
  args.shift();
33
29
  }
34
30
 
@@ -38,13 +38,12 @@ export async function build(params: BuildParams) {
38
38
 
39
39
  let serverProcess: ChildProcess | null = null;
40
40
  let inited = false;
41
+
41
42
  let nodeArgs = [`${params.serverDir}/server/index.js`];
43
+ let envFiles: string[] | null = null;
42
44
 
43
45
  if (params.useEnvFiles !== false) {
44
- let envFiles = await getEnvFiles();
45
-
46
- for (let envFile of envFiles) log(`Using ${envFile}`);
47
-
46
+ envFiles = await getEnvFiles();
48
47
  nodeArgs.unshift(...envFiles.map((file) => `--env-file=${file}`));
49
48
  }
50
49
 
@@ -56,6 +55,11 @@ export async function build(params: BuildParams) {
56
55
 
57
56
  if (!inited) {
58
57
  log(`Build completed +${formatDuration(Date.now() - startTime)}`);
58
+
59
+ if (envFiles) {
60
+ for (let envFile of envFiles) log(`Using ${envFile}`);
61
+ }
62
+
59
63
  inited = true;
60
64
  }
61
65
 
@@ -5,6 +5,7 @@ import { init } from "../middleware/init.ts";
5
5
  import { requestEvents } from "../middleware/requestEvents.ts";
6
6
  import type { LogEventPayload } from "../types/LogEventPayload.ts";
7
7
  import { emitLog } from "./emitLog.ts";
8
+ import { getServerURL } from "./getServerURL.ts";
8
9
  import { renderStatus } from "./renderStatus.ts";
9
10
 
10
11
  export function createApp(callback?: () => void | Promise<void>) {
@@ -12,16 +13,16 @@ export function createApp(callback?: () => void | Promise<void>) {
12
13
 
13
14
  if (!app.events) app.events = new EventEmitter();
14
15
 
15
- let host = process.env.APP_HOST || "localhost";
16
- let port = Number(process.env.APP_PORT) || 3000;
17
-
18
16
  let listen = () => {
19
- app.listen(port, host, () => {
20
- let location = `http://${host}:${port}/`;
21
- let env = `NODE_ENV=${process.env.NODE_ENV}`;
22
-
23
- emitLog(app, `Server running at ${location} (${env})`);
24
- });
17
+ let { href, protocol, hostname, port } = getServerURL();
18
+
19
+ if (protocol === "http:")
20
+ app.listen(port, hostname, () => {
21
+ emitLog(
22
+ app,
23
+ `Server running at ${href} (NODE_ENV=${process.env.NODE_ENV})`,
24
+ );
25
+ });
25
26
  };
26
27
 
27
28
  if (process.env.NODE_ENV === "development")
@@ -0,0 +1,38 @@
1
+ const defaultPorts: Record<string, number> = {
2
+ "http:": 80,
3
+ "https:": 443,
4
+ };
5
+
6
+ export function getServerURL() {
7
+ let {
8
+ SERVER_URL: href,
9
+ SERVER_HOSTNAME: hostname = "localhost",
10
+ SERVER_PORT: port = "3000",
11
+ } = process.env;
12
+
13
+ let protocol = "http:";
14
+
15
+ if (href) {
16
+ if (href.startsWith("//")) href = `${protocol}${href}`;
17
+ else if (!href.includes("://")) href = `${protocol}//${href}`;
18
+ else protocol = href.slice(0, href.indexOf("://") + 1);
19
+
20
+ let matches = href.match(/^\w+:\/\/([^/:]+)(:(\d+))?(\/|$)/);
21
+
22
+ if (matches?.[1]) hostname = matches[1];
23
+ if (matches?.[3]) port = matches[3];
24
+ } else href = `${protocol}//${hostname}:${port}`;
25
+
26
+ let parsedPort = Number(port);
27
+ let origin = `${protocol}//${hostname}`;
28
+
29
+ if (parsedPort !== defaultPorts[protocol]) origin += `:${port}`;
30
+
31
+ return {
32
+ href,
33
+ origin,
34
+ protocol,
35
+ hostname,
36
+ port: parsedPort,
37
+ };
38
+ }