@olenbetong/appframe-vite 1.0.3 → 1.0.7

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Ølen Betong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/lib/build.js CHANGED
@@ -2,17 +2,16 @@ import { visualizer } from "rollup-plugin-visualizer";
2
2
  import { viteExternalsPlugin } from "vite-plugin-externals";
3
3
  import { importJson } from "./importJson.js";
4
4
  export async function addAppframeBuildConfig(config) {
5
- var _a, _b;
6
5
  let appPkg = await importJson("./package.json", true);
7
6
  let { appframe } = appPkg;
8
- config.build = (_a = config.build) !== null && _a !== void 0 ? _a : {};
7
+ config.build = config.build ?? {};
9
8
  config.build = {
10
9
  ...config.build,
11
10
  manifest: true,
12
11
  sourcemap: true,
13
12
  cssCodeSplit: false,
14
13
  };
15
- config.plugins = (_b = config.plugins) !== null && _b !== void 0 ? _b : [];
14
+ config.plugins = config.plugins ?? [];
16
15
  if (appframe.build.externals !== false) {
17
16
  config.plugins.push(viteExternalsPlugin({
18
17
  react: "React",
@@ -1,6 +1,6 @@
1
1
  import { Application, RequestHandler, Router } from "express";
2
2
  import { ViteDevServer } from "vite";
3
- export declare function createProxyMiddleware(): Promise<Router>;
3
+ export declare function createProxyMiddleware(): Router;
4
4
  export declare function createHtmlTransformer(): (html: string) => Promise<string>;
5
- export declare function createDevMiddleware(vite: ViteDevServer): Promise<RequestHandler>;
5
+ export declare function createDevMiddleware(vite: ViteDevServer): RequestHandler;
6
6
  export declare function startDevServer(app: Application): Promise<void>;
package/lib/devServer.js CHANGED
@@ -4,17 +4,18 @@ import fs from "node:fs/promises";
4
4
  import path from "node:path";
5
5
  import open from "open";
6
6
  import tcpPortUsed from "tcp-port-used";
7
- import { Client } from "@olenbetong/data-object";
7
+ import { Client } from "@olenbetong/appframe-data";
8
8
  import { importJson } from "./importJson.js";
9
9
  import { createAppframeProxy } from "./proxy.js";
10
10
  dotenv.config();
11
11
  let appPkg = await importJson("./package.json", true);
12
12
  let { appframe } = appPkg;
13
13
  let userSession = null;
14
- async function getUserSession(hostname, username, password) {
14
+ async function getUserSession() {
15
15
  if (userSession) {
16
16
  return userSession;
17
17
  }
18
+ let { hostname, username, password } = getLoginInfo();
18
19
  let client = new Client(hostname);
19
20
  await client.login(username, password);
20
21
  let userResult = await client.afFetch("/api/system/usersession", {
@@ -23,15 +24,17 @@ async function getUserSession(hostname, username, password) {
23
24
  userSession = await userResult.json();
24
25
  return userSession;
25
26
  }
26
- export async function createProxyMiddleware() {
27
- var _a, _b, _c, _d, _e;
28
- const hostname = (_d = (_b = (_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : (_c = appframe.deploy) === null || _c === void 0 ? void 0 : _c.hostname) !== null && _d !== void 0 ? _d : "dev.obet.no";
27
+ function getLoginInfo() {
28
+ const hostname = appframe.proxy?.hostname ?? appframe.deploy?.hostname ?? "dev.obet.no";
29
29
  const { APPFRAME_LOGIN: username, APPFRAME_PWD: password } = process.env;
30
- const router = express.Router();
31
30
  if (!username || !password) {
32
31
  throw new Error("Your Appframe credentials are not set in the environment variables. Username should be set in APPFRAME_LOGIN and password in APPFRAME_PWD.");
33
32
  }
34
- const proxy = await createAppframeProxy({ hostname, username, password });
33
+ return { hostname, username, password };
34
+ }
35
+ export function createProxyMiddleware() {
36
+ let { hostname, username, password } = getLoginInfo();
37
+ const proxy = createAppframeProxy({ hostname, username, password });
35
38
  const proxyRoutes = [
36
39
  "/api/*",
37
40
  "/file/*",
@@ -47,24 +50,19 @@ export async function createProxyMiddleware() {
47
50
  "/rowcount/*",
48
51
  "/lib/*"
49
52
  ];
50
- if ((_e = appframe.proxy) === null || _e === void 0 ? void 0 : _e.routes) {
53
+ if (appframe.proxy?.routes) {
51
54
  proxyRoutes.push(...appframe.proxy.routes);
52
55
  }
56
+ const router = express.Router();
53
57
  proxyRoutes.forEach(route => {
54
58
  router.use(route, proxy);
55
59
  });
56
60
  return router;
57
61
  }
58
62
  export function createHtmlTransformer() {
59
- var _a, _b, _c, _d, _e, _f;
60
- const hostname = (_d = (_b = (_a = appframe.proxy) === null || _a === void 0 ? void 0 : _a.hostname) !== null && _b !== void 0 ? _b : (_c = appframe.deploy) === null || _c === void 0 ? void 0 : _c.hostname) !== null && _d !== void 0 ? _d : "dev.obet.no";
61
- const { APPFRAME_LOGIN: username, APPFRAME_PWD: password } = process.env;
62
- let isPartner = ((_f = (_e = appPkg.appframe) === null || _e === void 0 ? void 0 : _e.article) === null || _f === void 0 ? void 0 : _f.hostname) === "partner.olenbetong.no";
63
- if (!username || !password) {
64
- throw new Error("Your Appframe credentials are not set in the environment variables. Username should be set in APPFRAME_LOGIN and password in APPFRAME_PWD.");
65
- }
66
63
  return async (html) => {
67
- let userSession = await getUserSession(hostname, username, password);
64
+ let isPartner = appPkg.appframe?.article?.hostname === "partner.olenbetong.no";
65
+ let userSession = await getUserSession();
68
66
  return html
69
67
  .replace("<!-- {Appframe} -->", `<script>
70
68
  af = globalThis.af ?? {};
@@ -88,14 +86,14 @@ af.userSession = ${JSON.stringify(userSession)};
88
86
  <link rel="stylesheet" href="/file/site/style/ob.es.application.min.css" />`);
89
87
  };
90
88
  }
91
- export async function createDevMiddleware(vite) {
92
- let transformer = await createHtmlTransformer();
89
+ export function createDevMiddleware(vite) {
93
90
  return async (req, res, next) => {
91
+ let transformer = createHtmlTransformer();
94
92
  const url = req.originalUrl;
95
93
  try {
96
94
  let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf-8");
97
95
  template = await vite.transformIndexHtml(url, template);
98
- let html = transformer(template);
96
+ let html = await transformer(template);
99
97
  res.status(200).set({ "Content-Type": "text/html" }).end(html);
100
98
  }
101
99
  catch (error) {
package/lib/index.js CHANGED
@@ -17,9 +17,9 @@ export default function appframe() {
17
17
  return config;
18
18
  },
19
19
  async configureServer(_server) {
20
- _server.middlewares.use(await createProxyMiddleware());
21
- return async () => {
22
- _server.middlewares.use(await createDevMiddleware(_server));
20
+ _server.middlewares.use(createProxyMiddleware());
21
+ return () => {
22
+ _server.middlewares.use(createDevMiddleware(_server));
23
23
  };
24
24
  },
25
25
  transformIndexHtml
package/lib/proxy.d.ts CHANGED
@@ -11,4 +11,4 @@ export declare type createProxyOptions = {
11
11
  password: string;
12
12
  protocol?: "http" | "https";
13
13
  };
14
- export declare function createAppframeProxy(options: createProxyOptions): Promise<import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>>;
14
+ export declare function createAppframeProxy(options: createProxyOptions): import("express").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
package/lib/proxy.js CHANGED
@@ -32,10 +32,9 @@ export async function login(hostname, username, password) {
32
32
  };
33
33
  console.log("Authenticating...");
34
34
  let request = https.request(options, response => {
35
- var _a, _b;
36
- let status = (_a = response.statusCode) !== null && _a !== void 0 ? _a : 600;
35
+ let status = response.statusCode ?? 600;
37
36
  if (status < 400) {
38
- let cookies = (_b = response.headers["set-cookie"]) !== null && _b !== void 0 ? _b : [];
37
+ let cookies = response.headers["set-cookie"] ?? [];
39
38
  let cookieObj = {};
40
39
  for (let cookie of cookies) {
41
40
  let [keyValue] = cookie.split(";");
@@ -63,17 +62,16 @@ export async function login(hostname, username, password) {
63
62
  currentLogin = null;
64
63
  return result;
65
64
  }
66
- export async function createAppframeProxy(options) {
67
- const { autoLogin = true, hostname, password, protocol = "https", username } = options;
65
+ export function createAppframeProxy(options) {
66
+ const { hostname, password, protocol = "https", username } = options;
68
67
  const proxy = expressProxy(`${protocol}://${hostname}`, {
69
68
  proxyReqOptDecorator: async function (proxyReqOpts) {
70
- var _a, _b;
71
- if (!((_a = proxyReqOpts.path) === null || _a === void 0 ? void 0 : _a.startsWith("/login"))) {
69
+ if (!proxyReqOpts.path?.startsWith("/login")) {
72
70
  let authCookies = await login(hostname, username, password);
73
71
  let cookies = [];
74
72
  cookies.push(`AppframeWebAuth=${authCookies["AppframeWebAuth"]}`);
75
73
  cookies.push(`AppframeWebSession=${authCookies["AppframeWebSession"]}`);
76
- proxyReqOpts.headers = (_b = proxyReqOpts.headers) !== null && _b !== void 0 ? _b : {};
74
+ proxyReqOpts.headers = proxyReqOpts.headers ?? {};
77
75
  proxyReqOpts.headers["cookie"] = cookies.join(";");
78
76
  }
79
77
  return proxyReqOpts;
@@ -82,8 +80,5 @@ export async function createAppframeProxy(options) {
82
80
  return req.originalUrl;
83
81
  }
84
82
  });
85
- if (autoLogin) {
86
- await login(hostname, username, password);
87
- }
88
83
  return proxy;
89
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@olenbetong/appframe-vite",
3
- "version": "1.0.3",
3
+ "version": "1.0.7",
4
4
  "description": "Tools to use and deploy Vite applications to Appframe",
5
5
  "main": "./lib/index.js",
6
6
  "type": "module",
@@ -19,6 +19,7 @@
19
19
  "author": "Bjørnar Vister Hansen <bvh@olenbetong.no>",
20
20
  "license": "MIT",
21
21
  "dependencies": {
22
+ "@olenbetong/appframe-data": "^0.1.2",
22
23
  "dotenv": "^16.0.1",
23
24
  "rollup-plugin-visualizer": "^5.6.0",
24
25
  "vite-plugin-externals": "^0.5.0"
@@ -31,12 +32,12 @@
31
32
  "vite": "^2.9.9"
32
33
  },
33
34
  "optionalDependencies": {
34
- "@olenbetong/data-object": "^1.0.0-alpha.65",
35
35
  "@types/express": "^4.17.13",
36
36
  "@types/express-http-proxy": "^1.6.3",
37
37
  "@types/tcp-port-used": "^1.0.1",
38
38
  "express-http-proxy": "^1.6.3",
39
39
  "open": "^8.4.0",
40
40
  "tcp-port-used": "^1.0.2"
41
- }
41
+ },
42
+ "gitHead": "f7d752c4547e6f17d91fa83717175cd21d90b05f"
42
43
  }