@olenbetong/appframe-vite 3.0.11 → 3.1.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.
- package/lib/devServer.js +14 -1
- package/lib/index.js +2 -1
- package/lib/proxy.d.ts +5 -3
- package/lib/proxy.js +12 -3
- package/package.json +4 -3
package/lib/devServer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Client } from "@olenbetong/appframe-data";
|
|
2
|
+
import { watch } from "chokidar";
|
|
2
3
|
import dotenv from "dotenv";
|
|
3
4
|
import express from "express";
|
|
4
5
|
import fs from "node:fs/promises";
|
|
@@ -15,7 +16,7 @@ async function getUserSession() {
|
|
|
15
16
|
if (userSession) {
|
|
16
17
|
return userSession;
|
|
17
18
|
}
|
|
18
|
-
let { hostname, username, password } = getLoginInfo();
|
|
19
|
+
let { hostname, username, password } = await getLoginInfo();
|
|
19
20
|
let client = new Client(hostname);
|
|
20
21
|
await client.login(username, password);
|
|
21
22
|
let userResult = await client.afFetch("/api/system/usersession", {
|
|
@@ -35,6 +36,18 @@ function getLoginInfo() {
|
|
|
35
36
|
export function createProxyMiddleware() {
|
|
36
37
|
let { hostname, username, password } = getLoginInfo();
|
|
37
38
|
const proxy = createAppframeProxy({ hostname, username, password });
|
|
39
|
+
try {
|
|
40
|
+
let appPkgUrl = `file://${process.cwd()}/package.json`;
|
|
41
|
+
watch(appPkgUrl).on("all", async () => {
|
|
42
|
+
appPkg = await importJson("./package.json", true);
|
|
43
|
+
appframe = appPkg.appframe;
|
|
44
|
+
let { hostname } = getLoginInfo();
|
|
45
|
+
proxy.setHostname(hostname);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.log(`Failed to watch package.json: ${error.message}`);
|
|
50
|
+
}
|
|
38
51
|
const proxyRoutes = [
|
|
39
52
|
"/api/*",
|
|
40
53
|
"/file/*",
|
package/lib/index.js
CHANGED
|
@@ -29,7 +29,8 @@ export default function appframe() {
|
|
|
29
29
|
return config;
|
|
30
30
|
},
|
|
31
31
|
async configureServer(_server) {
|
|
32
|
-
|
|
32
|
+
let proxyMiddleware = createProxyMiddleware();
|
|
33
|
+
_server.middlewares.use(proxyMiddleware);
|
|
33
34
|
return () => {
|
|
34
35
|
_server.middlewares.use(createDevMiddleware(_server));
|
|
35
36
|
};
|
package/lib/proxy.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/// <reference types="express" />
|
|
1
|
+
import e from "express";
|
|
3
2
|
type CookieObject = Record<string, string>;
|
|
4
3
|
export declare function login(hostname: string, username: string, password: string): Promise<CookieObject>;
|
|
5
4
|
export type createProxyOptions = {
|
|
@@ -12,5 +11,8 @@ export type createProxyOptions = {
|
|
|
12
11
|
password: string;
|
|
13
12
|
protocol?: "http" | "https";
|
|
14
13
|
};
|
|
15
|
-
export
|
|
14
|
+
export type AppframeProxy = e.RequestHandler & {
|
|
15
|
+
setHostname: (hostname: string) => Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
export declare function createAppframeProxy(options: createProxyOptions): AppframeProxy;
|
|
16
18
|
export {};
|
package/lib/proxy.js
CHANGED
|
@@ -4,7 +4,7 @@ const lastLogin = new Map();
|
|
|
4
4
|
let currentLogin = null;
|
|
5
5
|
export async function login(hostname, username, password) {
|
|
6
6
|
if (currentLogin) {
|
|
7
|
-
|
|
7
|
+
await currentLogin;
|
|
8
8
|
}
|
|
9
9
|
let loginPromise = new Promise((resolve, reject) => {
|
|
10
10
|
if (lastLogin.has(hostname)) {
|
|
@@ -72,8 +72,10 @@ export async function login(hostname, username, password) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
export function createAppframeProxy(options) {
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
let { hostname, password, protocol = "https", username } = options;
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
const proxy = expressProxy(() => `${protocol}://${hostname}`, {
|
|
78
|
+
memoizeHost: false,
|
|
77
79
|
proxyReqOptDecorator: async function (proxyReqOpts) {
|
|
78
80
|
if (!proxyReqOpts.path?.startsWith("/login")) {
|
|
79
81
|
let authCookies = await login(hostname, username, password);
|
|
@@ -89,5 +91,12 @@ export function createAppframeProxy(options) {
|
|
|
89
91
|
return req.originalUrl;
|
|
90
92
|
},
|
|
91
93
|
});
|
|
94
|
+
proxy.setHostname = async (newHostname) => {
|
|
95
|
+
if (hostname !== newHostname) {
|
|
96
|
+
hostname = newHostname;
|
|
97
|
+
console.log(`Changing proxy host to '${newHostname}'...`);
|
|
98
|
+
await login(hostname, username, password);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
92
101
|
return proxy;
|
|
93
102
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/appframe-vite",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
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,8 @@
|
|
|
19
19
|
"author": "Bjørnar Vister Hansen <bvh@olenbetong.no>",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@olenbetong/appframe-data": "^0.7.
|
|
22
|
+
"@olenbetong/appframe-data": "^0.7.16",
|
|
23
|
+
"chokidar": "^3.5.3",
|
|
23
24
|
"dotenv": "^16.0.3",
|
|
24
25
|
"rollup-plugin-visualizer": "^5.9.0",
|
|
25
26
|
"vite-plugin-externals": "^0.5.1"
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"open": "^8.4.0",
|
|
40
41
|
"tcp-port-used": "^1.0.2"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "49054954b937cf12215f72bda93dc2acd3c89846"
|
|
43
44
|
}
|