@olenbetong/appframe-vite 4.0.1 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -2,17 +2,15 @@ import bodyParser from "body-parser";
2
2
  import { watch } from "chokidar";
3
3
  import { addAppframeBuildConfig } from "./build.js";
4
4
  import { createDevMiddleware, getLoginInfo, getProxyRoutes } from "./devServer.js";
5
- import { importJson } from "./importJson.js";
6
5
  import { getLastSession, login } from "./proxy.js";
7
6
  let interval;
8
7
  let server;
9
8
  let lastHostname;
9
+ let watcher;
10
10
  try {
11
11
  let appPkgUrl = `file://${process.cwd()}/package.json`;
12
- watch(appPkgUrl).on("all", async () => {
12
+ watcher = watch(appPkgUrl).on("all", async () => {
13
13
  if (server) {
14
- let appPkg = await importJson("./package.json", true);
15
- let config = appPkg.appframe;
16
14
  let { hostname } = await getLoginInfo();
17
15
  if (lastHostname !== hostname) {
18
16
  server.restart(false);
@@ -57,46 +55,49 @@ export default function appframe() {
57
55
  },
58
56
  };
59
57
  if (command === "build") {
58
+ watcher.close();
60
59
  await addAppframeBuildConfig(config);
61
60
  }
62
- let proxy = config.server?.proxy ?? {};
63
- let routes = getProxyRoutes();
64
- let { hostname, username, password } = await getLoginInfo();
65
- lastHostname = hostname;
66
- await login(hostname, username, password);
67
- if (interval)
68
- clearInterval(interval);
69
- // After changing the plugin to use the Vite proxy instead of a custom
70
- // express server with node-http-proxy, we can no longer ensure the requests
71
- // have a valid login. To reduce the amount of problems we get with expired
72
- // sessions, we run the login procedure every 5 minutes to renew the session.
73
- interval = setInterval(async () => {
61
+ else {
62
+ let proxy = config.server?.proxy ?? {};
63
+ let routes = getProxyRoutes();
64
+ let { hostname, username, password } = await getLoginInfo();
65
+ lastHostname = hostname;
74
66
  await login(hostname, username, password);
75
- }, 1000 * 60 * 5);
76
- for (let route of routes) {
77
- proxy[route] = {
78
- target: `https://${hostname}`,
79
- changeOrigin: true,
80
- configure(proxy, options) {
81
- proxy.on("proxyReq", (proxyReq, req, res, options) => {
82
- let authCookies = getLastSession(hostname)?.authCookies;
83
- if (authCookies) {
84
- let cookies = [];
85
- cookies.push(`AppframeWebAuth=${authCookies["AppframeWebAuth"]}`);
86
- cookies.push(`AppframeWebSession=${authCookies["AppframeWebSession"]}`);
87
- proxyReq.setHeader("cookie", cookies.join(";"));
88
- // If left unchanged, the origin will be the local IP address, which makes Appframe
89
- // return 403 errors
90
- proxyReq.setHeader("origin", `https://${hostname}`);
91
- }
92
- });
93
- },
67
+ if (interval)
68
+ clearInterval(interval);
69
+ // After changing the plugin to use the Vite proxy instead of a custom
70
+ // express server with node-http-proxy, we can no longer ensure the requests
71
+ // have a valid login. To reduce the amount of problems we get with expired
72
+ // sessions, we run the login procedure every 5 minutes to renew the session.
73
+ interval = setInterval(async () => {
74
+ await login(hostname, username, password);
75
+ }, 1000 * 60 * 5);
76
+ for (let route of routes) {
77
+ proxy[route] = {
78
+ target: `https://${hostname}`,
79
+ changeOrigin: true,
80
+ configure(proxy, options) {
81
+ proxy.on("proxyReq", (proxyReq, req, res, options) => {
82
+ let authCookies = getLastSession(hostname)?.authCookies;
83
+ if (authCookies) {
84
+ let cookies = [];
85
+ cookies.push(`AppframeWebAuth=${authCookies["AppframeWebAuth"]}`);
86
+ cookies.push(`AppframeWebSession=${authCookies["AppframeWebSession"]}`);
87
+ proxyReq.setHeader("cookie", cookies.join(";"));
88
+ // If left unchanged, the origin will be the local IP address, which makes Appframe
89
+ // return 403 errors
90
+ proxyReq.setHeader("origin", `https://${hostname}`);
91
+ }
92
+ });
93
+ },
94
+ };
95
+ }
96
+ config.server = {
97
+ ...config.server,
98
+ proxy,
94
99
  };
95
100
  }
96
- config.server = {
97
- ...config.server,
98
- proxy,
99
- };
100
101
  return config;
101
102
  },
102
103
  async configureServer(_server) {
package/lib/proxy.js CHANGED
@@ -2,6 +2,15 @@ import chalk from "chalk";
2
2
  import https from "node:https";
3
3
  const lastLogin = new Map();
4
4
  let currentLogin = null;
5
+ const isInteractive = process.stdout.isTTY;
6
+ function write(text) {
7
+ if (isInteractive) {
8
+ process.stdout.write?.(text);
9
+ }
10
+ else {
11
+ console.log(text);
12
+ }
13
+ }
5
14
  export function getLastSession(hostname) {
6
15
  return lastLogin.get(hostname);
7
16
  }
@@ -17,7 +26,7 @@ export async function login(hostname, username, password) {
17
26
  return;
18
27
  }
19
28
  else {
20
- process.stdout.write(`${chalk.bgBlueBright(hostname)}: Renewing authentication cookies...`);
29
+ write(`${chalk.bgBlueBright(hostname)}: Renewing authentication cookies...`);
21
30
  lastLogin.delete(hostname);
22
31
  }
23
32
  }
@@ -33,11 +42,13 @@ export async function login(hostname, username, password) {
33
42
  "Content-Length": data.length,
34
43
  },
35
44
  };
36
- process.stdout.clearLine(0);
37
- process.stdout.write(`\r${chalk.bgBlueBright(hostname)}: Authenticating`);
45
+ process.stdout.clearLine?.(0);
46
+ write(`\r${chalk.bgBlueBright(hostname)}: Authenticating`);
38
47
  let start = performance.now();
39
48
  let interval = setInterval(() => {
40
- process.stdout.write(`.`);
49
+ if (isInteractive) {
50
+ write(`.`);
51
+ }
41
52
  }, 100);
42
53
  let request = https.request(options, (response) => {
43
54
  clearInterval(interval);
@@ -52,8 +63,8 @@ export async function login(hostname, username, password) {
52
63
  cookieObj[key] = value;
53
64
  }
54
65
  }
55
- process.stdout.clearLine(0);
56
- process.stdout.write(`\r${chalk.bgBlueBright(hostname)}: Authenticated (${Math.floor(performance.now() - start)}ms)\n`);
66
+ process.stdout.clearLine?.(0);
67
+ write(`${chalk.bgBlueBright(hostname)}: Authenticated (${Math.floor(performance.now() - start)}ms)\n`);
57
68
  lastLogin.set(hostname, {
58
69
  timestamp: Date.now(),
59
70
  authCookies: cookieObj,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-vite",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "Tools to use and deploy Vite applications to Appframe",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",
@@ -19,7 +19,7 @@
19
19
  "author": "Bjørnar Vister Hansen <bvh@olenbetong.no>",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
- "@olenbetong/appframe-data": "^0.8.11",
22
+ "@olenbetong/appframe-data": "^0.9.0",
23
23
  "chalk": "^5.3.0",
24
24
  "chokidar": "^3.5.3",
25
25
  "dotenv": "^16.3.1",
@@ -43,5 +43,5 @@
43
43
  "open": "^9.1.0",
44
44
  "tcp-port-used": "^1.0.2"
45
45
  },
46
- "gitHead": "1d05d89bf9baa78f5da07e632830e05342522fef"
46
+ "gitHead": "accf7556ca04e48921926b526fb712a7b7ae0e53"
47
47
  }