@joystick.js/node-canary 0.0.0-canary.30 → 0.0.0-canary.32
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.
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import dayjs from "dayjs";
|
|
3
|
+
import crypto from "crypto";
|
|
4
|
+
import ssr from "../../ssr/index.js";
|
|
5
|
+
import { isObject } from "../../validation/lib/typeValidators";
|
|
6
|
+
import settings from "../../settings";
|
|
7
|
+
import generateErrorPage from "../../lib/generateErrorPage.js";
|
|
8
|
+
import replaceFileProtocol from "../../lib/replaceFileProtocol.js";
|
|
9
|
+
import replaceBackslashesWithForwardSlashes from "../../lib/replaceBackslashesWithForwardSlashes.js";
|
|
10
|
+
import getBuildPath from "../../lib/getBuildPath.js";
|
|
11
|
+
const generateHash = (input = "") => {
|
|
12
|
+
return crypto.createHash("sha256").update(input).digest("hex");
|
|
13
|
+
};
|
|
14
|
+
const getCacheDiff = async (diffFunction = null) => {
|
|
15
|
+
if (diffFunction) {
|
|
16
|
+
const diff = await diffFunction();
|
|
17
|
+
const diffHash = typeof diff === "string" ? generateHash(diff) : null;
|
|
18
|
+
return diffHash;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
22
|
+
const writeCacheFileToDisk = ({
|
|
23
|
+
expiresAfterMinutes = "",
|
|
24
|
+
cachePath = "",
|
|
25
|
+
cacheFileName = "index",
|
|
26
|
+
currentDiff = null,
|
|
27
|
+
html = ""
|
|
28
|
+
}) => {
|
|
29
|
+
const expiresAt = dayjs().add(expiresAfterMinutes, "minutes").unix();
|
|
30
|
+
fs.mkdir(`${cachePath}/_cache`, { recursive: true }, () => {
|
|
31
|
+
fs.writeFile(`${cachePath}/_cache/${cacheFileName}_${expiresAt}.html`, html, (error) => {
|
|
32
|
+
if (error) {
|
|
33
|
+
console.warn(error);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
if (currentDiff) {
|
|
37
|
+
fs.writeFile(`${cachePath}/_cache/diff_${expiresAt}`, currentDiff, (error) => {
|
|
38
|
+
if (error) {
|
|
39
|
+
console.warn(error);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
const getCachedHTML = ({ cachePath, cacheFileName, currentDiff }) => {
|
|
46
|
+
const files = fs.existsSync(`${cachePath}/_cache`) ? fs.readdirSync(`${cachePath}/_cache`) : [];
|
|
47
|
+
const cacheFile = files?.find((file) => file?.includes(cacheFileName));
|
|
48
|
+
const cacheFileExpiresAtUnix = cacheFile?.replace(`${cacheFileName}_`, "").replace(".html", "");
|
|
49
|
+
const existingDiff = fs.existsSync(`${cachePath}/_cache/diff_${cacheFileExpiresAtUnix}`) ? fs.readFileSync(`${cachePath}/_cache/diff_${cacheFileExpiresAtUnix}`, "utf-8") : null;
|
|
50
|
+
const cacheFileDiffHasChanged = existingDiff !== currentDiff;
|
|
51
|
+
const cacheFileExpiresAtHasPassed = dayjs().isAfter(dayjs.unix(parseInt(cacheFileExpiresAtUnix)));
|
|
52
|
+
const cacheFileHasExpired = cacheFileDiffHasChanged || cacheFileExpiresAtHasPassed;
|
|
53
|
+
if (cacheFileDiffHasChanged || cacheFileExpiresAtHasPassed) {
|
|
54
|
+
fs.unlink(`${cachePath}/_cache/${cacheFile}`, (error) => {
|
|
55
|
+
if (error)
|
|
56
|
+
return;
|
|
57
|
+
});
|
|
58
|
+
fs.unlink(`${cachePath}/_cache/diff_${cacheFileExpiresAtUnix}`, (error) => {
|
|
59
|
+
if (error)
|
|
60
|
+
return;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return cacheFile && !cacheFileHasExpired ? fs.readFileSync(`${cachePath}/_cache/${cacheFile}`, "utf-8") : null;
|
|
64
|
+
};
|
|
65
|
+
const getUrl = (request = {}) => {
|
|
66
|
+
const [path = null] = request.url?.split("?");
|
|
67
|
+
return {
|
|
68
|
+
params: request.params,
|
|
69
|
+
query: request.query,
|
|
70
|
+
route: request.route.path,
|
|
71
|
+
path
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
const getFile = async (buildPath = "") => {
|
|
75
|
+
const file = await import(buildPath);
|
|
76
|
+
return file.default;
|
|
77
|
+
};
|
|
78
|
+
const getTranslationsFile = async (languageFilePath = "", paths = "") => {
|
|
79
|
+
const languageFile = await getFile(`${paths.build}/i18n/${languageFilePath}`);
|
|
80
|
+
const isValidLanguageFile = languageFile && isObject(languageFile);
|
|
81
|
+
if (isValidLanguageFile) {
|
|
82
|
+
const translationsForPage = languageFile[paths.page];
|
|
83
|
+
return translationsForPage ? translationsForPage : languageFile;
|
|
84
|
+
}
|
|
85
|
+
return {};
|
|
86
|
+
};
|
|
87
|
+
const getTranslations = async (paths = {}, languagePreferences = []) => {
|
|
88
|
+
const languageFiles = fs.readdirSync(`${paths.build}/i18n`);
|
|
89
|
+
let matchingFile = null;
|
|
90
|
+
for (let i = 0; i < languagePreferences.length; i += 1) {
|
|
91
|
+
const languageRegex = languagePreferences[i];
|
|
92
|
+
const match = languageFiles.find((languageFile) => !!languageFile.match(languageRegex));
|
|
93
|
+
if (match) {
|
|
94
|
+
matchingFile = match;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const translationsFile = await getTranslationsFile(matchingFile, paths);
|
|
99
|
+
return translationsFile;
|
|
100
|
+
};
|
|
101
|
+
const getLanguagePreferenceRegexes = (userLanguage = "", browserLanguages = []) => {
|
|
102
|
+
let languagePreferences = [];
|
|
103
|
+
if (userLanguage) {
|
|
104
|
+
languagePreferences.push(userLanguage);
|
|
105
|
+
}
|
|
106
|
+
const filteredBrowserLanguages = browserLanguages?.filter((language) => {
|
|
107
|
+
return !language?.includes("*");
|
|
108
|
+
});
|
|
109
|
+
languagePreferences.push(...filteredBrowserLanguages);
|
|
110
|
+
languagePreferences.push(settings?.config?.i18n?.defaultLanguage);
|
|
111
|
+
return languagePreferences?.flatMap((language) => {
|
|
112
|
+
const variants = [language];
|
|
113
|
+
if (language?.length === 2) {
|
|
114
|
+
variants.push(`${language.substring(0, 2)}-`);
|
|
115
|
+
}
|
|
116
|
+
if (language?.length > 2) {
|
|
117
|
+
variants.push(`${language?.split("-")[0]}`);
|
|
118
|
+
variants.push(`${language?.split("-")[0]}-`);
|
|
119
|
+
}
|
|
120
|
+
return variants;
|
|
121
|
+
})?.map((languageString) => {
|
|
122
|
+
const lastCharacter = languageString[languageString.length - 1];
|
|
123
|
+
if (lastCharacter === "-") {
|
|
124
|
+
return new RegExp(`^${languageString}[A-Z]+.js`, "g");
|
|
125
|
+
}
|
|
126
|
+
return new RegExp(`^${languageString}.js`, "g");
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
const parseBrowserLanguages = (languages = "") => {
|
|
130
|
+
const rawLanguages = languages.split(",");
|
|
131
|
+
return rawLanguages?.map((rawLanguage) => rawLanguage.split(";")[0]);
|
|
132
|
+
};
|
|
133
|
+
var render_default = (req, res, next, appInstance = {}) => {
|
|
134
|
+
res.render = async function(path = "", options = {}) {
|
|
135
|
+
const urlFormattedForCache = req?.url?.split("/")?.filter((part) => !!part)?.join("_");
|
|
136
|
+
const buildPathForEnvironment = getBuildPath();
|
|
137
|
+
const buildPath = replaceFileProtocol(replaceBackslashesWithForwardSlashes(`${process.cwd().replace(buildPathForEnvironment, "")}/${buildPathForEnvironment}`));
|
|
138
|
+
const pagePath = `${buildPath}${path}`;
|
|
139
|
+
const layoutPath = options.layout ? `${buildPath}${options.layout}` : null;
|
|
140
|
+
const pagePathParts = `${buildPathForEnvironment}${path}`?.split("/")?.filter((part) => !!part);
|
|
141
|
+
const cachePath = pagePathParts?.slice(0, pagePathParts.length - 1)?.join("/");
|
|
142
|
+
let currentDiff;
|
|
143
|
+
if (!fs.existsSync(pagePath)) {
|
|
144
|
+
return res.status(404).send(generateErrorPage({
|
|
145
|
+
type: "pageNotFound",
|
|
146
|
+
path: `res.render('${path}')`,
|
|
147
|
+
frame: null,
|
|
148
|
+
stack: `A page component at the path ${path} could not be found.`
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
if (layoutPath && !fs.existsSync(layoutPath)) {
|
|
152
|
+
return res.status(404).send(generateErrorPage({
|
|
153
|
+
type: "layoutNotFound",
|
|
154
|
+
path: `res.render('${path}', { layout: '${options.layout}' })`,
|
|
155
|
+
frame: null,
|
|
156
|
+
stack: `A layout component at the path ${options.layout} could not be found.`
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
if (options?.cache?.expiresAfterMinutes) {
|
|
160
|
+
currentDiff = typeof options?.cache?.diff === "function" ? await getCacheDiff(options?.cache?.diff) : null;
|
|
161
|
+
const cachedHTML = await getCachedHTML({
|
|
162
|
+
cachePath,
|
|
163
|
+
cacheFileName: urlFormattedForCache?.trim() === "" ? "index" : urlFormattedForCache,
|
|
164
|
+
currentDiff
|
|
165
|
+
});
|
|
166
|
+
if (cachedHTML) {
|
|
167
|
+
return res.send(cachedHTML);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const pageFile = await getFile(pagePath);
|
|
171
|
+
const Page = pageFile;
|
|
172
|
+
const layoutFile = layoutPath ? await getFile(layoutPath) : null;
|
|
173
|
+
const Layout = layoutFile;
|
|
174
|
+
const browserLanguages = parseBrowserLanguages(req?.headers["accept-language"]);
|
|
175
|
+
const languagePreferenceRegexes = getLanguagePreferenceRegexes(req?.context?.user?.language, browserLanguages);
|
|
176
|
+
const translations = await getTranslations({ build: buildPath, page: path }, languagePreferenceRegexes);
|
|
177
|
+
const url = getUrl(req);
|
|
178
|
+
const props = { ...options?.props || {} };
|
|
179
|
+
if (layoutPath && fs.existsSync(layoutPath)) {
|
|
180
|
+
props.page = Page;
|
|
181
|
+
}
|
|
182
|
+
const html = await ssr({
|
|
183
|
+
componentFunction: Layout || Page,
|
|
184
|
+
req,
|
|
185
|
+
props,
|
|
186
|
+
url,
|
|
187
|
+
translations,
|
|
188
|
+
attributes: options?.attributes,
|
|
189
|
+
email: false,
|
|
190
|
+
baseHTMLPath: null,
|
|
191
|
+
layoutComponentPath: options?.layout,
|
|
192
|
+
pageComponentPath: path?.substring(0, 1) === "/" ? path?.replace("/", "") : path,
|
|
193
|
+
head: options?.head,
|
|
194
|
+
api: appInstance?.options?.api
|
|
195
|
+
});
|
|
196
|
+
if (options?.cache?.expiresAfterMinutes) {
|
|
197
|
+
writeCacheFileToDisk({
|
|
198
|
+
expiresAfterMinutes: parseInt(options?.cache?.expiresAfterMinutes),
|
|
199
|
+
cachePath,
|
|
200
|
+
cacheFileName: urlFormattedForCache?.trim() === "" ? "index" : urlFormattedForCache,
|
|
201
|
+
currentDiff,
|
|
202
|
+
html
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return res.status(200).send(html);
|
|
206
|
+
};
|
|
207
|
+
next();
|
|
208
|
+
};
|
|
209
|
+
export {
|
|
210
|
+
render_default as default
|
|
211
|
+
};
|
|
@@ -14,6 +14,11 @@ var sanitizeRequestParameters_default = (req, res, next) => {
|
|
|
14
14
|
if (req?.route?.path) {
|
|
15
15
|
req.route.path = escapeHTML(req?.route?.path);
|
|
16
16
|
}
|
|
17
|
+
console.log({
|
|
18
|
+
params: req?.params,
|
|
19
|
+
query: req?.query,
|
|
20
|
+
route: req?.route
|
|
21
|
+
});
|
|
17
22
|
next();
|
|
18
23
|
};
|
|
19
24
|
export {
|