memory-extract 0.1.3 → 0.1.4
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 +1 -1
- package/tools/play.mjs +1 -1
- package/tools/playMemoryServer.mjs +48 -17
package/package.json
CHANGED
package/tools/play.mjs
CHANGED
|
@@ -16,6 +16,15 @@ export const normalizeUrlPath = (pathname) => {
|
|
|
16
16
|
export const urlPathToManifestPath = (urlPath) =>
|
|
17
17
|
urlPath === "/" ? "" : urlPath.slice(1);
|
|
18
18
|
|
|
19
|
+
export const resolveEntryMountPrefix = (entryPath) => {
|
|
20
|
+
const slash = entryPath.lastIndexOf("/");
|
|
21
|
+
if (slash <= 0) {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return entryPath.slice(0, slash + 1);
|
|
26
|
+
};
|
|
27
|
+
|
|
19
28
|
const hrefForEntry = (urlPath, name, isDirectory) => {
|
|
20
29
|
const base = urlPath === "/" ? "" : urlPath;
|
|
21
30
|
const href = `${base}/${name}${isDirectory ? "/" : ""}`.replace(/\/+/g, "/");
|
|
@@ -30,14 +39,12 @@ export const resolveManifestPathForUrl = (urlPath, manifestPaths, entryPath = ""
|
|
|
30
39
|
return direct;
|
|
31
40
|
}
|
|
32
41
|
|
|
33
|
-
const
|
|
34
|
-
? entryPath.slice(0, entryPath.lastIndexOf("/"))
|
|
35
|
-
: "";
|
|
42
|
+
const mountPrefix = resolveEntryMountPrefix(entryPath);
|
|
36
43
|
|
|
37
|
-
if (direct &&
|
|
38
|
-
const
|
|
39
|
-
if (manifestSet.has(
|
|
40
|
-
return
|
|
44
|
+
if (direct && mountPrefix) {
|
|
45
|
+
const mounted = `${mountPrefix}${direct}`.replace(/\/+/g, "/");
|
|
46
|
+
if (manifestSet.has(mounted)) {
|
|
47
|
+
return mounted;
|
|
41
48
|
}
|
|
42
49
|
}
|
|
43
50
|
|
|
@@ -68,27 +75,51 @@ const serveManifestFile = (response, manifest, fileBytes, manifestPath) => {
|
|
|
68
75
|
response.end(body);
|
|
69
76
|
};
|
|
70
77
|
|
|
78
|
+
const isHtmlEntryPath = (entryPath) =>
|
|
79
|
+
entryPath.endsWith(".html") || entryPath.endsWith(".htm");
|
|
80
|
+
|
|
71
81
|
export const createMemoryPlayHandler = (manifest, fileBytes, filePaths, entryPath) => {
|
|
72
82
|
const manifestPaths = filePaths ?? listManifestFiles(manifest);
|
|
83
|
+
const mountPrefix = resolveEntryMountPrefix(entryPath);
|
|
73
84
|
|
|
74
85
|
return (request, response) => {
|
|
75
86
|
try {
|
|
76
87
|
const url = new URL(request.url ?? "/", "http://localhost");
|
|
77
88
|
const urlPath = normalizeUrlPath(url.pathname);
|
|
78
89
|
|
|
90
|
+
if (mountPrefix) {
|
|
91
|
+
const mountRoot = `/${mountPrefix.replace(/\/$/, "")}`;
|
|
92
|
+
|
|
93
|
+
if (urlPath === mountRoot || urlPath.startsWith(`${mountRoot}/`)) {
|
|
94
|
+
const relative =
|
|
95
|
+
urlPath === mountRoot ? "/" : urlPath.slice(mountRoot.length);
|
|
96
|
+
response.writeHead(302, {
|
|
97
|
+
Location: relative.startsWith("/") ? relative : `/${relative}`,
|
|
98
|
+
});
|
|
99
|
+
response.end();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let manifestPath = null;
|
|
105
|
+
|
|
79
106
|
if (urlPath === "/") {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
107
|
+
manifestPath = entryPath;
|
|
108
|
+
} else {
|
|
109
|
+
manifestPath = resolveManifestPathForUrl(
|
|
110
|
+
urlPath,
|
|
111
|
+
manifestPaths,
|
|
112
|
+
entryPath
|
|
113
|
+
);
|
|
85
114
|
}
|
|
86
115
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
)
|
|
116
|
+
if (
|
|
117
|
+
!manifestPath &&
|
|
118
|
+
isHtmlEntryPath(entryPath) &&
|
|
119
|
+
request.method === "GET"
|
|
120
|
+
) {
|
|
121
|
+
manifestPath = entryPath;
|
|
122
|
+
}
|
|
92
123
|
|
|
93
124
|
if (!manifestPath) {
|
|
94
125
|
response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
|