@jsenv/server 12.3.3 → 12.4.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/package.json
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { urlToMeta, normalizeStructuredMetaMap } from "@jsenv/url-meta"
|
|
2
2
|
|
|
3
|
+
import { escapeRegexpSpecialCharacters } from "./regex_escape.js"
|
|
4
|
+
|
|
3
5
|
export const pluginRessourceAliases = (ressourceAliases) => {
|
|
4
|
-
const
|
|
6
|
+
const alias = {}
|
|
5
7
|
Object.keys(ressourceAliases).forEach((key) => {
|
|
6
|
-
|
|
8
|
+
alias[key] = {
|
|
9
|
+
pattern: key,
|
|
10
|
+
replacement: ressourceAliases[key],
|
|
11
|
+
}
|
|
7
12
|
})
|
|
8
13
|
const structuredMetaMap = normalizeStructuredMetaMap(
|
|
9
|
-
{
|
|
14
|
+
{
|
|
15
|
+
alias,
|
|
16
|
+
},
|
|
10
17
|
"file:///",
|
|
11
18
|
)
|
|
12
|
-
|
|
13
19
|
const getRessourceAlias = (pathname) => {
|
|
14
20
|
const { alias } = urlToMeta({
|
|
15
21
|
url: `file://${pathname}`,
|
|
@@ -25,14 +31,35 @@ export const pluginRessourceAliases = (ressourceAliases) => {
|
|
|
25
31
|
if (!ressourceAlias) {
|
|
26
32
|
return
|
|
27
33
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
const { pattern, replacement } = ressourceAlias
|
|
35
|
+
let newValue
|
|
36
|
+
const ressourceBeforeAlias = request.ressource
|
|
37
|
+
if (replacement.includes("*")) {
|
|
38
|
+
let regexsource = ""
|
|
39
|
+
let i = 0
|
|
40
|
+
while (i < pattern.length) {
|
|
41
|
+
const char = pattern[i]
|
|
42
|
+
i++
|
|
43
|
+
if (char === "*") {
|
|
44
|
+
regexsource += "(.+)"
|
|
45
|
+
} else {
|
|
46
|
+
regexsource += escapeRegexpSpecialCharacters(char)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const regex = new RegExp(regexsource)
|
|
50
|
+
const matchResult = ressourceBeforeAlias.match(regex)
|
|
51
|
+
const parts = replacement.split("*")
|
|
52
|
+
newValue = parts.reduce((previous, value, index) => {
|
|
53
|
+
return `${previous}${value}${
|
|
54
|
+
index === parts.length - 1 ? "" : matchResult[index + 1]
|
|
55
|
+
}`
|
|
56
|
+
}, "")
|
|
57
|
+
} else {
|
|
58
|
+
newValue = replacement
|
|
59
|
+
}
|
|
60
|
+
const ressource = replaceRessource(ressourceBeforeAlias, newValue)
|
|
34
61
|
redirectRequest({
|
|
35
|
-
ressourceBeforeAlias
|
|
62
|
+
ressourceBeforeAlias,
|
|
36
63
|
ressource,
|
|
37
64
|
})
|
|
38
65
|
},
|
|
@@ -40,16 +67,13 @@ export const pluginRessourceAliases = (ressourceAliases) => {
|
|
|
40
67
|
}
|
|
41
68
|
}
|
|
42
69
|
|
|
43
|
-
const
|
|
44
|
-
const urlObject = new URL(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
urlObject.pathname = ressource
|
|
49
|
-
return urlObject
|
|
70
|
+
const replaceRessource = (ressourceBeforeAlias, newValue) => {
|
|
71
|
+
const urlObject = new URL(ressourceBeforeAlias, "file://")
|
|
72
|
+
const searchSeparatorIndex = newValue.indexOf("?")
|
|
73
|
+
if (searchSeparatorIndex > -1) {
|
|
74
|
+
return newValue // let new value override search params
|
|
50
75
|
}
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
return urlObject
|
|
76
|
+
urlObject.pathname = newValue
|
|
77
|
+
const ressource = `${urlObject.pathname}${urlObject.search}`
|
|
78
|
+
return ressource
|
|
55
79
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// https://github.com/benjamingr/RegExp.escape/blob/master/polyfill.js
|
|
2
|
+
|
|
3
|
+
export const escapeRegexpSpecialCharacters = (string) => {
|
|
4
|
+
string = String(string)
|
|
5
|
+
let i = 0
|
|
6
|
+
let escapedString = ""
|
|
7
|
+
|
|
8
|
+
while (i < string.length) {
|
|
9
|
+
const char = string[i]
|
|
10
|
+
i++
|
|
11
|
+
escapedString += isRegExpSpecialChar(char) ? `\\${char}` : char
|
|
12
|
+
}
|
|
13
|
+
return escapedString
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const isRegExpSpecialChar = (char) => regexpSpecialChars.indexOf(char) > -1
|
|
17
|
+
|
|
18
|
+
const regexpSpecialChars = [
|
|
19
|
+
"/",
|
|
20
|
+
"^",
|
|
21
|
+
"\\",
|
|
22
|
+
"[",
|
|
23
|
+
"]",
|
|
24
|
+
"(",
|
|
25
|
+
")",
|
|
26
|
+
"{",
|
|
27
|
+
"}",
|
|
28
|
+
"?",
|
|
29
|
+
"+",
|
|
30
|
+
"*",
|
|
31
|
+
".",
|
|
32
|
+
"|",
|
|
33
|
+
"$",
|
|
34
|
+
]
|