@nshiab/setup-data-project 4.0.16 → 4.0.17
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/README.md +5 -5
- package/dist/main.mjs +49 -17
- package/dist/main.mjs.map +1 -1
- package/dist/preview.mjs +124 -0
- package/dist/preview.mjs.map +1 -0
- package/dist/sda.mjs +372 -106
- package/dist/sda.mjs.map +1 -1
- package/package.json +2 -1
package/dist/preview.mjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createReadStream, statSync, watch } from "node:fs";
|
|
3
|
+
import { extname, isAbsolute, join, relative, sep } from "node:path";
|
|
4
|
+
import { parseArgs } from "node:util";
|
|
5
|
+
import { readFile, realpath, stat } from "node:fs/promises";
|
|
6
|
+
import { createServer } from "node:http";
|
|
7
|
+
import { pipeline } from "node:stream/promises";
|
|
8
|
+
//#region src/preview.ts
|
|
9
|
+
const defaultSiteDirectory = "src/site";
|
|
10
|
+
const reloadPath = "/__sda_preview_reload";
|
|
11
|
+
const reloadScriptPath = `${reloadPath}.js`;
|
|
12
|
+
const reloadScript = `new EventSource(location.origin + "${reloadPath}").onmessage = () => location.reload();`;
|
|
13
|
+
const contentTypes = {
|
|
14
|
+
".html": "text/html; charset=utf-8",
|
|
15
|
+
".htm": "text/html; charset=utf-8",
|
|
16
|
+
".css": "text/css; charset=utf-8",
|
|
17
|
+
".js": "text/javascript; charset=utf-8",
|
|
18
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
19
|
+
".json": "application/json",
|
|
20
|
+
".geojson": "application/geo+json",
|
|
21
|
+
".csv": "text/csv; charset=utf-8",
|
|
22
|
+
".tsv": "text/tab-separated-values; charset=utf-8",
|
|
23
|
+
".txt": "text/plain; charset=utf-8",
|
|
24
|
+
".svg": "image/svg+xml",
|
|
25
|
+
".png": "image/png",
|
|
26
|
+
".jpg": "image/jpeg",
|
|
27
|
+
".jpeg": "image/jpeg",
|
|
28
|
+
".gif": "image/gif",
|
|
29
|
+
".webp": "image/webp",
|
|
30
|
+
".ico": "image/x-icon",
|
|
31
|
+
".woff": "font/woff",
|
|
32
|
+
".woff2": "font/woff2"
|
|
33
|
+
};
|
|
34
|
+
function fail(error) {
|
|
35
|
+
console.error(`Preview failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
async function main() {
|
|
39
|
+
const { values } = parseArgs({ options: {
|
|
40
|
+
dir: {
|
|
41
|
+
type: "string",
|
|
42
|
+
default: defaultSiteDirectory
|
|
43
|
+
},
|
|
44
|
+
help: {
|
|
45
|
+
type: "boolean",
|
|
46
|
+
short: "h"
|
|
47
|
+
}
|
|
48
|
+
} });
|
|
49
|
+
if (values.help) {
|
|
50
|
+
console.log(`Usage: sda-preview [--dir <path>]\nServe and watch ${defaultSiteDirectory}/ on localhost by default. Use --dir to preview a different directory.`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const directory = values.dir;
|
|
54
|
+
if (!statSync(directory, { throwIfNoEntry: false })?.isDirectory()) throw new Error(`Expected a site directory at "${directory}". Create it with an index.html page first.`);
|
|
55
|
+
const root = await realpath(directory);
|
|
56
|
+
const clients = /* @__PURE__ */ new Set();
|
|
57
|
+
let pendingReload;
|
|
58
|
+
watch(root, { recursive: true }, () => {
|
|
59
|
+
clearTimeout(pendingReload);
|
|
60
|
+
pendingReload = setTimeout(() => {
|
|
61
|
+
for (const client of clients) client.write("data: reload\n\n");
|
|
62
|
+
}, 80);
|
|
63
|
+
}).on("error", fail);
|
|
64
|
+
async function respond(request, response) {
|
|
65
|
+
response.setHeader("Cache-Control", "no-store");
|
|
66
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
67
|
+
response.writeHead(405, { Allow: "GET, HEAD" }).end();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const pathname = decodeURIComponent(new URL(request.url ?? "/", "http://localhost").pathname);
|
|
71
|
+
if (pathname === reloadScriptPath) {
|
|
72
|
+
response.writeHead(200, { "Content-Type": "text/javascript; charset=utf-8" });
|
|
73
|
+
response.end(request.method === "HEAD" ? void 0 : reloadScript);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (pathname === reloadPath && request.method === "GET") {
|
|
77
|
+
response.writeHead(200, { "Content-Type": "text/event-stream" });
|
|
78
|
+
response.write(": connected\n\n");
|
|
79
|
+
clients.add(response);
|
|
80
|
+
response.on("close", () => clients.delete(response));
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (/[\\:\0]/.test(pathname) || pathname.split("/").some((part) => part.startsWith("."))) throw new Error("Invalid path");
|
|
84
|
+
const file = await realpath(join(root, pathname.endsWith("/") ? pathname + "index.html" : pathname));
|
|
85
|
+
const withinRoot = relative(root, file);
|
|
86
|
+
if (isAbsolute(withinRoot) || withinRoot.split(sep).some((part) => part.startsWith("."))) throw new Error("File is outside the public site");
|
|
87
|
+
const info = await stat(file);
|
|
88
|
+
if (!info.isFile()) throw new Error("Expected a file");
|
|
89
|
+
const type = contentTypes[extname(file).toLowerCase()] ?? "application/octet-stream";
|
|
90
|
+
response.setHeader("Content-Type", type);
|
|
91
|
+
if (type.startsWith("text/html")) {
|
|
92
|
+
if (request.method === "HEAD") {
|
|
93
|
+
response.end();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const html = await readFile(file, "utf8");
|
|
97
|
+
const address = server.address();
|
|
98
|
+
if (!address || typeof address === "string") throw new Error("Preview server is not listening");
|
|
99
|
+
const { port } = address;
|
|
100
|
+
const host = request.headers.host?.toLowerCase() === `localhost:${port}` ? "localhost" : "127.0.0.1";
|
|
101
|
+
response.end(`${html}\n<script src="http://${host}:${port}${reloadScriptPath}"><\/script>`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
response.setHeader("Content-Length", info.size);
|
|
105
|
+
if (request.method === "HEAD") response.end();
|
|
106
|
+
else await pipeline(createReadStream(file), response);
|
|
107
|
+
}
|
|
108
|
+
const server = createServer((request, response) => {
|
|
109
|
+
respond(request, response).catch(() => {
|
|
110
|
+
if (response.headersSent) response.destroy();
|
|
111
|
+
else response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
server.on("error", fail);
|
|
115
|
+
server.listen(0, "127.0.0.1", () => {
|
|
116
|
+
const address = server.address();
|
|
117
|
+
if (address && typeof address === "object") console.log(`Previewing ${directory} at http://127.0.0.1:${address.port}`);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
main().catch(fail);
|
|
121
|
+
//#endregion
|
|
122
|
+
export {};
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=preview.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preview.mjs","names":[],"sources":["../src/preview.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { createReadStream, statSync, watch } from \"node:fs\";\nimport { readFile, realpath, stat } from \"node:fs/promises\";\nimport { createServer, type IncomingMessage, type ServerResponse } from \"node:http\";\nimport { extname, isAbsolute, join, relative, sep } from \"node:path\";\nimport { pipeline } from \"node:stream/promises\";\nimport { parseArgs } from \"node:util\";\n\nconst defaultSiteDirectory = \"src/site\";\nconst reloadPath = \"/__sda_preview_reload\";\nconst reloadScriptPath = `${reloadPath}.js`;\nconst reloadScript = `new EventSource(location.origin + \"${reloadPath}\").onmessage = () => location.reload();`;\nconst contentTypes: Record<string, string> = {\n \".html\": \"text/html; charset=utf-8\",\n \".htm\": \"text/html; charset=utf-8\",\n \".css\": \"text/css; charset=utf-8\",\n \".js\": \"text/javascript; charset=utf-8\",\n \".mjs\": \"text/javascript; charset=utf-8\",\n \".json\": \"application/json\",\n \".geojson\": \"application/geo+json\",\n \".csv\": \"text/csv; charset=utf-8\",\n \".tsv\": \"text/tab-separated-values; charset=utf-8\",\n \".txt\": \"text/plain; charset=utf-8\",\n \".svg\": \"image/svg+xml\",\n \".png\": \"image/png\",\n \".jpg\": \"image/jpeg\",\n \".jpeg\": \"image/jpeg\",\n \".gif\": \"image/gif\",\n \".webp\": \"image/webp\",\n \".ico\": \"image/x-icon\",\n \".woff\": \"font/woff\",\n \".woff2\": \"font/woff2\",\n};\n\nfunction fail(error: unknown) {\n console.error(`Preview failed: ${error instanceof Error ? error.message : String(error)}`);\n process.exit(1);\n}\n\nasync function main() {\n const { values } = parseArgs({\n options: {\n dir: { type: \"string\", default: defaultSiteDirectory },\n help: { type: \"boolean\", short: \"h\" },\n },\n });\n if (values.help) {\n console.log(\n `Usage: sda-preview [--dir <path>]\\nServe and watch ${defaultSiteDirectory}/ on localhost by default. Use --dir to preview a different directory.`,\n );\n return;\n }\n const directory = values.dir;\n if (!statSync(directory, { throwIfNoEntry: false })?.isDirectory()) {\n throw new Error(\n `Expected a site directory at \"${directory}\". Create it with an index.html page first.`,\n );\n }\n\n const root = await realpath(directory);\n const clients = new Set<ServerResponse>();\n let pendingReload: ReturnType<typeof setTimeout> | undefined;\n\n const watcher = watch(root, { recursive: true }, () => {\n clearTimeout(pendingReload);\n pendingReload = setTimeout(() => {\n for (const client of clients) client.write(\"data: reload\\n\\n\");\n }, 80);\n });\n watcher.on(\"error\", fail);\n\n async function respond(request: IncomingMessage, response: ServerResponse) {\n response.setHeader(\"Cache-Control\", \"no-store\");\n if (request.method !== \"GET\" && request.method !== \"HEAD\") {\n response.writeHead(405, { Allow: \"GET, HEAD\" }).end();\n return;\n }\n const pathname = decodeURIComponent(new URL(request.url ?? \"/\", \"http://localhost\").pathname);\n if (pathname === reloadScriptPath) {\n response.writeHead(200, { \"Content-Type\": \"text/javascript; charset=utf-8\" });\n response.end(request.method === \"HEAD\" ? undefined : reloadScript);\n return;\n }\n if (pathname === reloadPath && request.method === \"GET\") {\n response.writeHead(200, { \"Content-Type\": \"text/event-stream\" });\n response.write(\": connected\\n\\n\");\n clients.add(response);\n response.on(\"close\", () => clients.delete(response));\n return;\n }\n\n if (/[\\\\:\\0]/.test(pathname) || pathname.split(\"/\").some((part) => part.startsWith(\".\"))) {\n throw new Error(\"Invalid path\");\n }\n const file = await realpath(\n join(root, pathname.endsWith(\"/\") ? pathname + \"index.html\" : pathname),\n );\n const withinRoot = relative(root, file);\n // Check the resolved path too, including symlinks to files outside the site directory.\n if (isAbsolute(withinRoot) || withinRoot.split(sep).some((part) => part.startsWith(\".\"))) {\n throw new Error(\"File is outside the public site\");\n }\n const info = await stat(file);\n if (!info.isFile()) throw new Error(\"Expected a file\");\n\n const type = contentTypes[extname(file).toLowerCase()] ?? \"application/octet-stream\";\n response.setHeader(\"Content-Type\", type);\n if (type.startsWith(\"text/html\")) {\n if (request.method === \"HEAD\") {\n response.end();\n return;\n }\n const html = await readFile(file, \"utf8\");\n const address = server.address();\n if (!address || typeof address === \"string\")\n throw new Error(\"Preview server is not listening\");\n const { port } = address;\n const host =\n request.headers.host?.toLowerCase() === `localhost:${port}` ? \"localhost\" : \"127.0.0.1\";\n // The absolute URL keeps the reload script local even when the page has a <base> tag.\n response.end(`${html}\\n<script src=\"http://${host}:${port}${reloadScriptPath}\"></script>`);\n return;\n }\n\n response.setHeader(\"Content-Length\", info.size);\n if (request.method === \"HEAD\") response.end();\n else await pipeline(createReadStream(file), response);\n }\n\n const server = createServer((request, response) => {\n void respond(request, response).catch(() => {\n if (response.headersSent) response.destroy();\n else\n response.writeHead(404, { \"Content-Type\": \"text/plain; charset=utf-8\" }).end(\"Not found\");\n });\n });\n server.on(\"error\", fail);\n server.listen(0, \"127.0.0.1\", () => {\n const address = server.address();\n if (address && typeof address === \"object\") {\n console.log(`Previewing ${directory} at http://127.0.0.1:${address.port}`);\n }\n });\n}\n\nvoid main().catch(fail);\n"],"mappings":";;;;;;;;AAQA,MAAM,uBAAuB;AAC7B,MAAM,aAAa;AACnB,MAAM,mBAAmB,GAAG,WAAW;AACvC,MAAM,eAAe,sCAAsC,WAAW;AACtE,MAAM,eAAuC;CAC3C,SA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