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 CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "memory-extract",
3
3
  "description": "Tool for embedding data into PNG images",
4
4
  "license": "MIT",
5
- "version": "0.1.3",
5
+ "version": "0.1.4",
6
6
  "author": "semigarden",
7
7
  "repository": {
8
8
  "type": "git",
package/tools/play.mjs CHANGED
@@ -153,7 +153,7 @@ const playMemoryPng = async (input, label = input) => {
153
153
  filePaths,
154
154
  playMode.entry
155
155
  );
156
- const openUrl = new URL(playMode.entry, baseUrl).href;
156
+ const openUrl = baseUrl;
157
157
 
158
158
  await runServerSession({
159
159
  input,
@@ -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 entryDir = entryPath.includes("/")
34
- ? entryPath.slice(0, entryPath.lastIndexOf("/"))
35
- : "";
42
+ const mountPrefix = resolveEntryMountPrefix(entryPath);
36
43
 
37
- if (direct && entryDir) {
38
- const joined = `${entryDir}/${direct}`.replace(/\/+/g, "/");
39
- if (manifestSet.has(joined)) {
40
- return joined;
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
- response.writeHead(302, {
81
- Location: `/${entryPath}`,
82
- });
83
- response.end();
84
- return;
107
+ manifestPath = entryPath;
108
+ } else {
109
+ manifestPath = resolveManifestPathForUrl(
110
+ urlPath,
111
+ manifestPaths,
112
+ entryPath
113
+ );
85
114
  }
86
115
 
87
- const manifestPath = resolveManifestPathForUrl(
88
- urlPath,
89
- manifestPaths,
90
- entryPath
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" });