-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathurl.js
More file actions
151 lines (133 loc) · 4.11 KB
/
url.js
File metadata and controls
151 lines (133 loc) · 4.11 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
142
143
144
145
146
147
148
149
150
151
import path from 'path';
// Base directory for storing all pods
export const DATA_ROOT = process.env.DATA_ROOT || './data';
/**
* Convert URL path to filesystem path
* @param {string} urlPath - The URL path (e.g., /alice/profile/)
* @returns {string} - Filesystem path
*/
export function urlToPath(urlPath) {
// Normalize: remove leading slash, decode URI
let normalized = urlPath.startsWith('/') ? urlPath.slice(1) : urlPath;
normalized = decodeURIComponent(normalized);
// Security: prevent path traversal
normalized = normalized.replace(/\.\./g, '');
return path.join(DATA_ROOT, normalized);
}
/**
* Convert URL path to filesystem path in subdomain mode
* In subdomain mode, the pod is determined by the hostname, not the path
* @param {string} urlPath - The URL path (e.g., /public/file.txt)
* @param {string} podName - The pod name from subdomain (e.g., "alice")
* @returns {string} - Filesystem path (e.g., DATA_ROOT/alice/public/file.txt)
*/
export function urlToPathWithPod(urlPath, podName) {
// Normalize: remove leading slash, decode URI
let normalized = urlPath.startsWith('/') ? urlPath.slice(1) : urlPath;
normalized = decodeURIComponent(normalized);
// Security: prevent path traversal
normalized = normalized.replace(/\.\./g, '');
// Prepend pod name to path
return path.join(DATA_ROOT, podName, normalized);
}
/**
* Get the effective path for a request (subdomain-aware)
* @param {object} request - Fastify request object
* @returns {string} - Filesystem path
*/
export function getPathFromRequest(request) {
const urlPath = request.url.split('?')[0];
// In subdomain mode with a recognized pod subdomain
if (request.subdomainsEnabled && request.podName) {
return urlToPathWithPod(urlPath, request.podName);
}
// Path-based mode (default)
return urlToPath(urlPath);
}
/**
* Get the effective URL path for a request (with pod prefix in subdomain mode)
* @param {object} request - Fastify request object
* @returns {string} - URL path with pod prefix if needed
*/
export function getEffectiveUrlPath(request) {
const urlPath = request.url.split('?')[0];
// In subdomain mode with a recognized pod subdomain, prepend pod name
if (request.subdomainsEnabled && request.podName) {
return '/' + request.podName + urlPath;
}
return urlPath;
}
/**
* Check if URL path represents a container (ends with /)
* @param {string} urlPath
* @returns {boolean}
*/
export function isContainer(urlPath) {
return urlPath.endsWith('/');
}
/**
* Get the parent container path
* @param {string} urlPath
* @returns {string}
*/
export function getParentContainer(urlPath) {
const parts = urlPath.replace(/\/$/, '').split('/');
parts.pop();
return parts.join('/') + '/';
}
/**
* Get resource name from URL path
* @param {string} urlPath
* @returns {string}
*/
export function getResourceName(urlPath) {
const parts = urlPath.replace(/\/$/, '').split('/');
return parts[parts.length - 1];
}
/**
* Determine content type from file extension
* @param {string} filePath
* @returns {string}
*/
export function getContentType(filePath) {
const ext = path.extname(filePath).toLowerCase();
const types = {
'.jsonld': 'application/ld+json',
'.json': 'application/json',
'.html': 'text/html',
'.txt': 'text/plain',
'.css': 'text/css',
'.js': 'application/javascript',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.ttl': 'text/turtle',
'.n3': 'text/n3',
'.nt': 'application/n-triples',
'.rdf': 'application/rdf+xml',
'.nq': 'application/n-quads',
'.trig': 'application/trig'
};
return types[ext] || 'application/octet-stream';
}
/**
* Check if content type is RDF
* @param {string} contentType
* @returns {boolean}
*/
export function isRdfContentType(contentType) {
const rdfTypes = [
'application/ld+json',
'application/json',
'text/turtle',
'text/n3',
'application/n-triples',
'application/rdf+xml',
'application/n-quads',
'application/trig'
];
return rdfTypes.includes(contentType);
}