@olenbetong/appframe-vite 4.0.3 → 4.1.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/lib/devServer.d.ts +1 -0
- package/lib/devServer.js +8 -1
- package/lib/index.js +5 -0
- package/lib/localization.d.ts +7 -0
- package/lib/localization.js +71 -0
- package/lib/proxy.js +2 -0
- package/package.json +2 -2
package/lib/devServer.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export declare function getLoginInfo(): Promise<{
|
|
|
3
3
|
hostname: any;
|
|
4
4
|
username: string;
|
|
5
5
|
password: string;
|
|
6
|
+
appframe: any;
|
|
6
7
|
}>;
|
|
7
8
|
export declare function getProxyRoutes(): string[];
|
|
8
9
|
export declare function createDevMiddleware(vite: ViteDevServer): Connect.NextHandleFunction;
|
package/lib/devServer.js
CHANGED
|
@@ -4,6 +4,7 @@ import { exists } from "fs-extra";
|
|
|
4
4
|
import { JSDOM } from "jsdom";
|
|
5
5
|
import { resolve } from "node:path";
|
|
6
6
|
import { importJson } from "./importJson.js";
|
|
7
|
+
import { getStringCache } from "./localization.js";
|
|
7
8
|
dotenv.config();
|
|
8
9
|
let appPkg = await importJson("./package.json", true);
|
|
9
10
|
let { appframe } = appPkg;
|
|
@@ -14,7 +15,7 @@ export async function getLoginInfo() {
|
|
|
14
15
|
if (!username || !password) {
|
|
15
16
|
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.");
|
|
16
17
|
}
|
|
17
|
-
return { hostname, username, password };
|
|
18
|
+
return { hostname, username, password, appframe };
|
|
18
19
|
}
|
|
19
20
|
export function getProxyRoutes() {
|
|
20
21
|
const proxyRoutes = [
|
|
@@ -78,6 +79,12 @@ async function getArticleHtml() {
|
|
|
78
79
|
script.textContent?.includes("serviceWorker.register")) {
|
|
79
80
|
nodesToRemove.push(script);
|
|
80
81
|
}
|
|
82
|
+
else if (script.textContent?.includes("af.article.i18n")) {
|
|
83
|
+
let cachedStrings = getStringCache();
|
|
84
|
+
let localizeScript = dom.window.document.createElement("script");
|
|
85
|
+
localizeScript.textContent = `Object.assign(af.article.i18n, ${JSON.stringify(cachedStrings)})`;
|
|
86
|
+
script.insertAdjacentElement("afterend", localizeScript);
|
|
87
|
+
}
|
|
81
88
|
});
|
|
82
89
|
dom.window.document.querySelectorAll("link").forEach((link) => {
|
|
83
90
|
if (link.rel === "stylesheet" && link.href?.startsWith(`/file/article/style/${article}`)) {
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,8 @@ 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
|
+
import { localizeMiddleware } from "./localization.js";
|
|
5
7
|
import { getLastSession, login } from "./proxy.js";
|
|
6
8
|
let interval;
|
|
7
9
|
let server;
|
|
@@ -101,7 +103,10 @@ export default function appframe() {
|
|
|
101
103
|
return config;
|
|
102
104
|
},
|
|
103
105
|
async configureServer(_server) {
|
|
106
|
+
let { appframe } = await importJson("./package.json", true);
|
|
104
107
|
server = _server;
|
|
108
|
+
_server.middlewares.use(`/api/user/localize/new/${appframe.article.id}`, jsonParser);
|
|
109
|
+
_server.middlewares.use(`/api/user/localize/new/${appframe.article.id}`, localizeMiddleware);
|
|
105
110
|
_server.middlewares.use("/data/Logger/LogError", jsonParser);
|
|
106
111
|
_server.middlewares.use("/data/Logger/LogError", (req, res) => {
|
|
107
112
|
// @ts-ignore
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Connect } from "vite";
|
|
2
|
+
export declare function addStringToCache(text: string, translation: string): void;
|
|
3
|
+
export declare function getStringFromCache(text: string): string | undefined;
|
|
4
|
+
export declare const localizeMiddleware: Connect.NextHandleFunction;
|
|
5
|
+
export declare function getStringCache(): {
|
|
6
|
+
[k: string]: string;
|
|
7
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { exists } from "fs-extra";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { getLoginInfo } from "./devServer.js";
|
|
5
|
+
import { importJson } from "./importJson.js";
|
|
6
|
+
import { getLastSession } from "./proxy.js";
|
|
7
|
+
function debounce(callback, delay = 300) {
|
|
8
|
+
let timeout;
|
|
9
|
+
return (...args) => {
|
|
10
|
+
clearTimeout(timeout);
|
|
11
|
+
timeout = setTimeout(() => callback(...args), delay);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const stringCache = new Map();
|
|
15
|
+
const cachePath = resolve(process.cwd(), "./node_modules/.appframe");
|
|
16
|
+
const cacheFile = resolve(cachePath, "./localizeCache.json");
|
|
17
|
+
if (await exists(cacheFile)) {
|
|
18
|
+
let cache = await importJson(cacheFile, true);
|
|
19
|
+
for (let text in cache) {
|
|
20
|
+
stringCache.set(text, cache[text]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async function saveCacheToFile() {
|
|
24
|
+
if (!(await exists(cacheFile))) {
|
|
25
|
+
await mkdir(cachePath, { recursive: true });
|
|
26
|
+
}
|
|
27
|
+
let data = JSON.stringify(Object.fromEntries(stringCache));
|
|
28
|
+
await writeFile(cacheFile, data, { encoding: "utf-8" });
|
|
29
|
+
}
|
|
30
|
+
const debouncedSaveCacheToFile = debounce(saveCacheToFile, 1000);
|
|
31
|
+
export function addStringToCache(text, translation) {
|
|
32
|
+
stringCache.set(text, translation);
|
|
33
|
+
debouncedSaveCacheToFile();
|
|
34
|
+
}
|
|
35
|
+
export function getStringFromCache(text) {
|
|
36
|
+
return stringCache.get(text);
|
|
37
|
+
}
|
|
38
|
+
export const localizeMiddleware = async (req, res, next) => {
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
let text = req.body.text;
|
|
41
|
+
let { hostname, appframe } = await getLoginInfo();
|
|
42
|
+
let authCookies = getLastSession(hostname)?.authCookies;
|
|
43
|
+
if (authCookies) {
|
|
44
|
+
let cookies = [];
|
|
45
|
+
cookies.push(`AppframeWebAuth=${authCookies["AppframeWebAuth"]}`);
|
|
46
|
+
cookies.push(`AppframeWebSession=${authCookies["AppframeWebSession"]}`);
|
|
47
|
+
let cookieHeader = cookies.join(";");
|
|
48
|
+
let uri = `https://${hostname}/api/user/localize/new/${appframe.article.id}`;
|
|
49
|
+
let result = await fetch(uri, {
|
|
50
|
+
body: JSON.stringify({ text }),
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: {
|
|
53
|
+
Accept: "application/json",
|
|
54
|
+
"Content-Type": "application/json",
|
|
55
|
+
Cookie: cookieHeader,
|
|
56
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
let data = await result.json();
|
|
60
|
+
addStringToCache(text, data);
|
|
61
|
+
res.statusCode = 200;
|
|
62
|
+
res.end(JSON.stringify(data));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
res.statusCode = 200;
|
|
66
|
+
res.end(text);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
export function getStringCache() {
|
|
70
|
+
return Object.fromEntries(stringCache);
|
|
71
|
+
}
|
package/lib/proxy.js
CHANGED
|
@@ -43,6 +43,7 @@ export async function login(hostname, username, password) {
|
|
|
43
43
|
},
|
|
44
44
|
};
|
|
45
45
|
process.stdout.clearLine?.(0);
|
|
46
|
+
process.stdout.cursorTo?.(0);
|
|
46
47
|
write(`\r${chalk.bgBlueBright(hostname)}: Authenticating`);
|
|
47
48
|
let start = performance.now();
|
|
48
49
|
let interval = setInterval(() => {
|
|
@@ -64,6 +65,7 @@ export async function login(hostname, username, password) {
|
|
|
64
65
|
}
|
|
65
66
|
}
|
|
66
67
|
process.stdout.clearLine?.(0);
|
|
68
|
+
process.stdout.cursorTo?.(0);
|
|
67
69
|
write(`${chalk.bgBlueBright(hostname)}: Authenticated (${Math.floor(performance.now() - start)}ms)\n`);
|
|
68
70
|
lastLogin.set(hostname, {
|
|
69
71
|
timestamp: Date.now(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olenbetong/appframe-vite",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Tools to use and deploy Vite applications to Appframe",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"open": "^9.1.0",
|
|
44
44
|
"tcp-port-used": "^1.0.2"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "ba8b3dc19b519eee6a3732526f4d1c10d0e00649"
|
|
47
47
|
}
|