-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-notes-directory.mjs
More file actions
141 lines (126 loc) · 4.31 KB
/
Copy pathadmin-notes-directory.mjs
File metadata and controls
141 lines (126 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fs from "node:fs/promises";
import path from "node:path";
import { pathToFileURL } from "node:url";
const ADMIN_NOTES_DIRECTORY = "dev/archive/legacy-docs-build/admin-notes";
const DIRECTORY_LIST_QUERY = "adminNotesDirectory";
const DIRECTORY_API_PATH = "/api/dev/admin-notes/directory";
const NOTE_INDEX_FILE = "index.txt";
function sendJson(response, statusCode, payload) {
response.statusCode = statusCode;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "accept, content-type");
response.setHeader("Content-Type", "application/json; charset=utf-8");
response.end(JSON.stringify(payload));
}
function repoRelativePath(value) {
return String(value || "")
.replace(/\\/g, "/")
.replace(/^\/+|\/+$/g, "");
}
function isInside(parentPath, childPath) {
const relativePath = path.relative(parentPath, childPath);
return relativePath === "" || (!relativePath.startsWith("..") && !path.isAbsolute(relativePath));
}
function safeAdminNotesFolder(repoRoot, requestPath) {
const relativeFolderPath = repoRelativePath(decodeURIComponent(requestPath || ""));
const hasTraversalSegment = relativeFolderPath
.split("/")
.some((segment) => segment === "." || segment === "..");
if (hasTraversalSegment) {
return null;
}
if (relativeFolderPath !== ADMIN_NOTES_DIRECTORY && !relativeFolderPath.startsWith(`${ADMIN_NOTES_DIRECTORY}/`)) {
return null;
}
const notesRoot = path.resolve(repoRoot, ADMIN_NOTES_DIRECTORY);
const folderPath = path.resolve(repoRoot, relativeFolderPath);
if (!isInside(notesRoot, folderPath)) {
return null;
}
return {
absolutePath: folderPath,
relativePath: relativeFolderPath,
};
}
async function adminNotesDirectoryResult(repoRoot, folderPath) {
const safeFolder = safeAdminNotesFolder(repoRoot, folderPath);
if (!safeFolder) {
return {
payload: {
entries: [],
error: "Admin Notes directory listing is restricted to dev/archive/legacy-docs-build/admin-notes/.",
ok: false,
},
statusCode: 403,
};
}
const stat = await fs.stat(safeFolder.absolutePath).catch(() => null);
if (!stat?.isDirectory()) {
return {
payload: {
entries: [],
error: `Admin Notes folder not found: ${safeFolder.relativePath}.`,
ok: false,
},
statusCode: 404,
};
}
const dirents = await fs.readdir(safeFolder.absolutePath, { withFileTypes: true });
const entries = (await Promise.all(dirents.map(async (dirent) => {
return fileEntry(safeFolder.relativePath, dirent) ||
await folderEntry(safeFolder.relativePath, safeFolder.absolutePath, dirent);
})))
.filter(Boolean)
.sort((left, right) => left.label.localeCompare(right.label));
return {
payload: {
entries,
folderFileUrl: pathToFileURL(safeFolder.absolutePath).href,
folderPath: safeFolder.relativePath,
ok: true,
},
statusCode: 200,
};
}
function fileEntry(folderPath, dirent) {
if (!dirent.isFile() || path.extname(dirent.name).toLowerCase() !== ".txt") {
return null;
}
return {
label: dirent.name,
path: `${folderPath}/${dirent.name}`,
type: "file",
};
}
async function folderEntry(folderPath, absoluteFolderPath, dirent) {
if (!dirent.isDirectory()) {
return null;
}
const indexPath = path.join(absoluteFolderPath, dirent.name, NOTE_INDEX_FILE);
const stat = await fs.stat(indexPath).catch(() => null);
if (!stat?.isFile()) {
return null;
}
return {
label: `${dirent.name}/`,
path: `${folderPath}/${dirent.name}/${NOTE_INDEX_FILE}`,
type: "folder",
};
}
export async function handleAdminNotesDirectoryApiRequest(requestUrl, response, { repoRoot }) {
if (requestUrl.pathname !== DIRECTORY_API_PATH) {
return false;
}
const folderPath = requestUrl.searchParams.get("folder") || ADMIN_NOTES_DIRECTORY;
const result = await adminNotesDirectoryResult(repoRoot, folderPath);
sendJson(response, result.statusCode, result.payload);
return true;
}
export async function handleAdminNotesDirectoryRequest(requestUrl, response, { repoRoot }) {
if (!requestUrl.searchParams.has(DIRECTORY_LIST_QUERY)) {
return false;
}
const result = await adminNotesDirectoryResult(repoRoot, requestUrl.pathname);
sendJson(response, result.statusCode, result.payload);
return true;
}