@joystick.js/node-canary 0.0.0-canary.28 → 0.0.0-canary.29
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.
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const parameters = Object.entries(req?.params);
|
|
4
|
-
const queryParameters = Object.entries(req?.query);
|
|
5
|
-
const htmlRegex = new RegExp(/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g);
|
|
6
|
-
const encodedOpenScriptTagRegex = new RegExp(/%3Cscript%3E/g);
|
|
7
|
-
const encodedCloseScriptTagRegex = new RegExp(/%3C\/script%3E/g);
|
|
1
|
+
import escapeHTML from "../../lib/escapeHTML.js";
|
|
2
|
+
const sanitizeParameterSet = (parameters = [], target = {}) => {
|
|
8
3
|
for (let i = 0; i < parameters?.length; i += 1) {
|
|
9
4
|
const [key, value] = parameters[i];
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (keyHTMLMatches?.length > 0 || valueHTMLMatches?.length > 0) {
|
|
13
|
-
delete req.params[key];
|
|
14
|
-
}
|
|
5
|
+
delete target[key];
|
|
6
|
+
target[escapeHTML(key)] = escapeHTML(value);
|
|
15
7
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
};
|
|
9
|
+
var sanitizeRequestParameters_default = (req, res, next) => {
|
|
10
|
+
const parameters = Object.entries(req?.params);
|
|
11
|
+
const queryParameters = Object.entries(req?.query);
|
|
12
|
+
sanitizeParameterSet(parameters, req?.params);
|
|
13
|
+
sanitizeParameterSet(queryParameters, req?.query);
|
|
14
|
+
if (req?.route?.path) {
|
|
15
|
+
req.route.path = escapeHTML(req?.route?.path);
|
|
23
16
|
}
|
|
24
17
|
next();
|
|
25
18
|
};
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import util from "util";
|
|
2
|
-
import
|
|
3
|
-
const escapeHTML = (string = "") => {
|
|
4
|
-
return String(string).replace(/[&<>"'`=\/]/g, function(match) {
|
|
5
|
-
return HTML_ENTITY_MAP[match];
|
|
6
|
-
});
|
|
7
|
-
};
|
|
2
|
+
import escapeHTML from "../lib/escapeHTML.js";
|
|
8
3
|
const sanitizeAPIResponse = (data = null) => {
|
|
9
4
|
let sanitizedData = data;
|
|
10
5
|
if (!util.isString(sanitizedData) && !util.isObject(sanitizedData) && !Array.isArray(sanitizedData)) {
|
package/package.json
CHANGED
|
@@ -1,211 +0,0 @@
|
|
|
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
|
-
};
|