javascript-solid-server 0.0.56 → 0.0.57

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 CHANGED
@@ -6,7 +6,7 @@ A minimal, fast, JSON-LD native Solid server.
6
6
 
7
7
  ## Features
8
8
 
9
- ### Implemented (v0.0.56)
9
+ ### Implemented (v0.0.57)
10
10
 
11
11
  - **LDP CRUD Operations** - GET, PUT, POST, DELETE, HEAD
12
12
  - **N3 Patch** - Solid's native patch format for RDF updates
@@ -30,6 +30,7 @@ A minimal, fast, JSON-LD native Solid server.
30
30
  - **CORS Support** - Full cross-origin resource sharing
31
31
  - **Git HTTP Backend** - Clone and push to containers via `git` protocol
32
32
  - **Invite-Only Registration** - CLI-managed invite codes for controlled signups
33
+ - **Storage Quotas** - Per-user storage limits with CLI management
33
34
  - **Security** - Blocks access to dotfiles (`.git/`, `.env`, etc.) except Solid-specific ones
34
35
 
35
36
  ### HTTP Methods
@@ -78,6 +79,7 @@ jss start --port 8443 --ssl-key ./key.pem --ssl-cert ./cert.pem
78
79
  jss start [options] # Start the server
79
80
  jss init [options] # Initialize configuration
80
81
  jss invite <cmd> # Manage invite codes (create, list, revoke)
82
+ jss quota <cmd> # Manage storage quotas (set, show, reconcile)
81
83
  jss --help # Show help
82
84
  ```
83
85
 
@@ -102,6 +104,7 @@ jss --help # Show help
102
104
  | `--mashlib-version <ver>` | Mashlib CDN version | 2.0.0 |
103
105
  | `--git` | Enable Git HTTP backend | false |
104
106
  | `--invite-only` | Require invite code for registration | false |
107
+ | `--default-quota <size>` | Default storage quota per pod (e.g., 50MB) | 50MB |
105
108
  | `-q, --quiet` | Suppress logs | false |
106
109
 
107
110
  ### Environment Variables
@@ -117,6 +120,7 @@ export JSS_SUBDOMAINS=true
117
120
  export JSS_BASE_DOMAIN=example.com
118
121
  export JSS_MASHLIB=true
119
122
  export JSS_INVITE_ONLY=true
123
+ export JSS_DEFAULT_QUOTA=100MB
120
124
  jss start
121
125
  ```
122
126
 
@@ -427,6 +431,43 @@ When `--invite-only` is enabled:
427
431
 
428
432
  Invite codes are stored in `.server/invites.json` in your data directory.
429
433
 
434
+ ## Storage Quotas
435
+
436
+ Limit storage per pod to prevent abuse and manage resources:
437
+
438
+ ```bash
439
+ jss start --default-quota 50MB
440
+ ```
441
+
442
+ ### Managing Quotas
443
+
444
+ ```bash
445
+ # Set quota for a user (overrides default)
446
+ jss quota set alice 100MB
447
+
448
+ # Show quota info
449
+ jss quota show alice
450
+ # alice:
451
+ # Used: 12.5 MB
452
+ # Limit: 100 MB
453
+ # Free: 87.5 MB
454
+ # Usage: 12%
455
+
456
+ # Recalculate from actual disk usage
457
+ jss quota reconcile alice
458
+ ```
459
+
460
+ ### How It Works
461
+
462
+ - Quotas are tracked incrementally on PUT, POST, and DELETE operations
463
+ - When quota is exceeded, the server returns HTTP 507 Insufficient Storage
464
+ - Each pod stores its quota in `/{pod}/.quota.json`
465
+ - Use `reconcile` to fix quota drift from manual file changes
466
+
467
+ ### Size Formats
468
+
469
+ Supported formats: `50MB`, `1GB`, `500KB`, `1TB`
470
+
430
471
  ## Authentication
431
472
 
432
473
  ### Simple Tokens (Development)
@@ -688,7 +729,8 @@ src/
688
729
  │ ├── container.js # POST, pod creation
689
730
  │ └── git.js # Git HTTP backend
690
731
  ├── storage/
691
- └── filesystem.js # File operations
732
+ ├── filesystem.js # File operations
733
+ │ └── quota.js # Storage quota management
692
734
  ├── auth/
693
735
  │ ├── middleware.js # Auth hook
694
736
  │ ├── token.js # Simple token auth
package/bin/jss.js CHANGED
@@ -12,6 +12,8 @@ import { Command } from 'commander';
12
12
  import { createServer } from '../src/server.js';
13
13
  import { loadConfig, saveConfig, printConfig, defaults } from '../src/config.js';
14
14
  import { createInvite, listInvites, revokeInvite } from '../src/idp/invites.js';
15
+ import { setQuotaLimit, getQuotaInfo, reconcileQuota, formatBytes } from '../src/storage/quota.js';
16
+ import { parseSize } from '../src/config.js';
15
17
  import fs from 'fs-extra';
16
18
  import path from 'path';
17
19
  import { fileURLToPath } from 'url';
@@ -307,6 +309,92 @@ inviteCmd
307
309
  }
308
310
  });
309
311
 
312
+ /**
313
+ * Quota command - manage storage quotas
314
+ */
315
+ const quotaCmd = program
316
+ .command('quota')
317
+ .description('Manage storage quotas for pods');
318
+
319
+ quotaCmd
320
+ .command('set <username> <size>')
321
+ .description('Set quota limit for a user (e.g., 50MB, 1GB)')
322
+ .option('-r, --root <path>', 'Data directory')
323
+ .action(async (username, size, options) => {
324
+ try {
325
+ if (options.root) {
326
+ process.env.DATA_ROOT = path.resolve(options.root);
327
+ }
328
+
329
+ const bytes = parseSize(size);
330
+ if (bytes === 0) {
331
+ console.error('Invalid size format. Use e.g., 50MB, 1GB');
332
+ process.exit(1);
333
+ }
334
+
335
+ const quota = await setQuotaLimit(username, bytes);
336
+ console.log(`\nQuota set for ${username}: ${formatBytes(quota.limit)}`);
337
+ console.log(`Current usage: ${formatBytes(quota.used)} (${Math.round(quota.used / quota.limit * 100)}%)\n`);
338
+ } catch (err) {
339
+ console.error(`Error: ${err.message}`);
340
+ process.exit(1);
341
+ }
342
+ });
343
+
344
+ quotaCmd
345
+ .command('show <username>')
346
+ .description('Show quota info for a user')
347
+ .option('-r, --root <path>', 'Data directory')
348
+ .action(async (username, options) => {
349
+ try {
350
+ if (options.root) {
351
+ process.env.DATA_ROOT = path.resolve(options.root);
352
+ }
353
+
354
+ const quota = await getQuotaInfo(username);
355
+
356
+ if (quota.limit === 0) {
357
+ console.log(`\n${username}: No quota set (unlimited)\n`);
358
+ } else {
359
+ console.log(`\n${username}:`);
360
+ console.log(` Used: ${formatBytes(quota.used)}`);
361
+ console.log(` Limit: ${formatBytes(quota.limit)}`);
362
+ console.log(` Free: ${formatBytes(quota.limit - quota.used)}`);
363
+ console.log(` Usage: ${quota.percent}%\n`);
364
+ }
365
+ } catch (err) {
366
+ console.error(`Error: ${err.message}`);
367
+ process.exit(1);
368
+ }
369
+ });
370
+
371
+ quotaCmd
372
+ .command('reconcile <username>')
373
+ .description('Recalculate quota usage from actual disk usage')
374
+ .option('-r, --root <path>', 'Data directory')
375
+ .action(async (username, options) => {
376
+ try {
377
+ if (options.root) {
378
+ process.env.DATA_ROOT = path.resolve(options.root);
379
+ }
380
+
381
+ console.log(`Calculating actual disk usage for ${username}...`);
382
+ const quota = await reconcileQuota(username);
383
+
384
+ if (quota.limit === 0) {
385
+ console.log(`\n${username}: No quota configured\n`);
386
+ } else {
387
+ console.log(`\nReconciled ${username}:`);
388
+ console.log(` Used: ${formatBytes(quota.used)}`);
389
+ console.log(` Limit: ${formatBytes(quota.limit)}`);
390
+ console.log(` Usage: ${Math.round(quota.used / quota.limit * 100)}%\n`);
391
+ }
392
+ } catch (err) {
393
+ console.error(`Error: ${err.message}`);
394
+ process.exit(1);
395
+ }
396
+ });
397
+
310
398
  /**
311
399
  * Helper: Prompt for input
312
400
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-solid-server",
3
- "version": "0.0.56",
3
+ "version": "0.0.57",
4
4
  "description": "A minimal, fast Solid server",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/config.js CHANGED
@@ -48,6 +48,9 @@ export const defaults = {
48
48
  // Invite-only registration
49
49
  inviteOnly: false,
50
50
 
51
+ // Storage quota (bytes) - 50MB default
52
+ defaultQuota: 50 * 1024 * 1024,
53
+
51
54
  // Logging
52
55
  logger: true,
53
56
  quiet: false,
@@ -79,8 +82,22 @@ const envMap = {
79
82
  JSS_MASHLIB_VERSION: 'mashlibVersion',
80
83
  JSS_GIT: 'git',
81
84
  JSS_INVITE_ONLY: 'inviteOnly',
85
+ JSS_DEFAULT_QUOTA: 'defaultQuota',
82
86
  };
83
87
 
88
+ /**
89
+ * Parse a size string like "50MB" or "1GB" to bytes
90
+ */
91
+ export function parseSize(str) {
92
+ const match = str.match(/^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB|TB)?$/i);
93
+ if (!match) return parseInt(str, 10) || 0;
94
+
95
+ const num = parseFloat(match[1]);
96
+ const unit = (match[2] || 'B').toUpperCase();
97
+ const multipliers = { B: 1, KB: 1024, MB: 1024**2, GB: 1024**3, TB: 1024**4 };
98
+ return Math.floor(num * (multipliers[unit] || 1));
99
+ }
100
+
84
101
  /**
85
102
  * Parse a value from environment variable string
86
103
  */
@@ -96,6 +113,11 @@ function parseEnvValue(value, key) {
96
113
  return parseInt(value, 10);
97
114
  }
98
115
 
116
+ // Size values (quota)
117
+ if (key === 'defaultQuota') {
118
+ return parseSize(value);
119
+ }
120
+
99
121
  return value;
100
122
  }
101
123
 
@@ -1,6 +1,7 @@
1
1
  import * as storage from '../storage/filesystem.js';
2
+ import { initializeQuota, checkQuota, updateQuotaUsage } from '../storage/quota.js';
2
3
  import { getAllHeaders } from '../ldp/headers.js';
3
- import { isContainer, getEffectiveUrlPath } from '../utils/url.js';
4
+ import { isContainer, getEffectiveUrlPath, getPodName } from '../utils/url.js';
4
5
  import { generateProfile, generatePreferences, generateTypeIndex, serialize } from '../webid/profile.js';
5
6
  import { generateOwnerAcl, generatePrivateAcl, generateInboxAcl, generatePublicFolderAcl, serializeAcl } from '../wac/parser.js';
6
7
  import { createToken } from '../auth/token.js';
@@ -106,7 +107,21 @@ export async function handlePost(request, reply) {
106
107
  }
107
108
  }
108
109
 
110
+ // Check storage quota before writing
111
+ const podName = getPodName(request);
112
+ if (podName) {
113
+ const { allowed, error } = await checkQuota(podName, content.length, request.defaultQuota || 0);
114
+ if (!allowed) {
115
+ return reply.code(507).send({ error: 'Insufficient Storage', message: error });
116
+ }
117
+ }
118
+
109
119
  success = await storage.write(newStoragePath, content);
120
+
121
+ // Update quota usage after successful write
122
+ if (success && podName) {
123
+ await updateQuotaUsage(podName, content.length);
124
+ }
110
125
  }
111
126
 
112
127
  if (!success) {
@@ -139,8 +154,9 @@ export async function handlePost(request, reply) {
139
154
  * @param {string} webId - User's WebID URI
140
155
  * @param {string} podUri - Pod root URI (e.g., https://alice.example.com/ or https://example.com/alice/)
141
156
  * @param {string} issuer - OIDC issuer URI
157
+ * @param {number} defaultQuota - Default storage quota in bytes (optional)
142
158
  */
143
- export async function createPodStructure(name, webId, podUri, issuer) {
159
+ export async function createPodStructure(name, webId, podUri, issuer, defaultQuota = 0) {
144
160
  const podPath = `/${name}/`;
145
161
 
146
162
  // Create pod directory structure
@@ -193,6 +209,11 @@ export async function createPodStructure(name, webId, podUri, issuer) {
193
209
  const profileAcl = generatePublicFolderAcl(`${podUri}profile/`, webId);
194
210
  await storage.write(`${podPath}profile/.acl`, serializeAcl(profileAcl));
195
211
 
212
+ // Initialize storage quota if configured
213
+ if (defaultQuota > 0) {
214
+ await initializeQuota(name, defaultQuota);
215
+ }
216
+
196
217
  return { podPath, podUri };
197
218
  }
198
219
 
@@ -1,7 +1,8 @@
1
1
  import * as storage from '../storage/filesystem.js';
2
+ import { checkQuota, updateQuotaUsage } from '../storage/quota.js';
2
3
  import { getAllHeaders, getNotFoundHeaders } from '../ldp/headers.js';
3
4
  import { generateContainerJsonLd, serializeJsonLd } from '../ldp/container.js';
4
- import { isContainer, getContentType, isRdfContentType, getEffectiveUrlPath, safeJsonParse } from '../utils/url.js';
5
+ import { isContainer, getContentType, isRdfContentType, getEffectiveUrlPath, safeJsonParse, getPodName } from '../utils/url.js';
5
6
  import { parseN3Patch, applyN3Patch, validatePatch } from '../patch/n3-patch.js';
6
7
  import { parseSparqlUpdate, applySparqlUpdate } from '../patch/sparql-update.js';
7
8
  import {
@@ -504,11 +505,28 @@ export async function handlePut(request, reply) {
504
505
  }
505
506
  }
506
507
 
508
+ // Check storage quota before writing
509
+ const podName = getPodName(request);
510
+ const oldSize = stats?.size || 0;
511
+ const sizeDelta = content.length - oldSize;
512
+
513
+ if (podName && sizeDelta > 0) {
514
+ const { allowed, error } = await checkQuota(podName, sizeDelta, request.defaultQuota || 0);
515
+ if (!allowed) {
516
+ return reply.code(507).send({ error: 'Insufficient Storage', message: error });
517
+ }
518
+ }
519
+
507
520
  const success = await storage.write(storagePath, content);
508
521
  if (!success) {
509
522
  return reply.code(500).send({ error: 'Write failed' });
510
523
  }
511
524
 
525
+ // Update quota usage after successful write
526
+ if (podName && sizeDelta !== 0) {
527
+ await updateQuotaUsage(podName, sizeDelta);
528
+ }
529
+
512
530
  const origin = request.headers.origin;
513
531
  const headers = getAllHeaders({ isContainer: false, origin, resourceUrl, connegEnabled });
514
532
  headers['Location'] = resourceUrl;
@@ -549,11 +567,20 @@ export async function handleDelete(request, reply) {
549
567
  }
550
568
  }
551
569
 
570
+ // Get file size before deletion for quota update
571
+ const fileSize = stats.size || 0;
572
+
552
573
  const success = await storage.remove(storagePath);
553
574
  if (!success) {
554
575
  return reply.code(500).send({ error: 'Delete failed' });
555
576
  }
556
577
 
578
+ // Update quota usage (subtract deleted file size)
579
+ const podName = getPodName(request);
580
+ if (podName && fileSize > 0) {
581
+ await updateQuotaUsage(podName, -fileSize);
582
+ }
583
+
557
584
  const origin = request.headers.origin;
558
585
  const headers = getAllHeaders({ isContainer: false, origin, resourceUrl });
559
586
  Object.entries(headers).forEach(([k, v]) => reply.header(k, v));
package/src/server.js CHANGED
@@ -48,6 +48,8 @@ export function createServer(options = {}) {
48
48
  const gitEnabled = options.git ?? false;
49
49
  // Invite-only registration is OFF by default - open registration
50
50
  const inviteOnly = options.inviteOnly ?? false;
51
+ // Default storage quota per pod (50MB default, 0 = unlimited)
52
+ const defaultQuota = options.defaultQuota ?? 50 * 1024 * 1024;
51
53
 
52
54
  // Set data root via environment variable if provided
53
55
  if (options.root) {
@@ -95,6 +97,7 @@ export function createServer(options = {}) {
95
97
  fastify.decorateRequest('mashlibEnabled', null);
96
98
  fastify.decorateRequest('mashlibCdn', null);
97
99
  fastify.decorateRequest('mashlibVersion', null);
100
+ fastify.decorateRequest('defaultQuota', null);
98
101
  fastify.addHook('onRequest', async (request) => {
99
102
  request.connegEnabled = connegEnabled;
100
103
  request.notificationsEnabled = notificationsEnabled;
@@ -104,6 +107,7 @@ export function createServer(options = {}) {
104
107
  request.mashlibEnabled = mashlibEnabled;
105
108
  request.mashlibCdn = mashlibCdn;
106
109
  request.mashlibVersion = mashlibVersion;
110
+ request.defaultQuota = defaultQuota;
107
111
 
108
112
  // Extract pod name from subdomain if enabled
109
113
  if (subdomainsEnabled && baseDomain) {
@@ -0,0 +1,202 @@
1
+ /**
2
+ * Storage quota management
3
+ * Tracks and enforces per-pod storage limits
4
+ */
5
+
6
+ import { promises as fs } from 'fs';
7
+ import { join } from 'path';
8
+ import { getDataRoot } from '../utils/url.js';
9
+
10
+ const QUOTA_FILE = '.quota.json';
11
+
12
+ /**
13
+ * Get quota file path for a pod
14
+ */
15
+ function getQuotaPath(podName) {
16
+ return join(getDataRoot(), podName, QUOTA_FILE);
17
+ }
18
+
19
+ /**
20
+ * Load quota data for a pod
21
+ * @param {string} podName - The pod name
22
+ * @returns {Promise<{limit: number, used: number}>}
23
+ */
24
+ export async function loadQuota(podName) {
25
+ try {
26
+ const data = await fs.readFile(getQuotaPath(podName), 'utf-8');
27
+ return JSON.parse(data);
28
+ } catch (err) {
29
+ if (err.code === 'ENOENT') {
30
+ // No quota file - return defaults (will be initialized on first write)
31
+ return { limit: 0, used: 0 };
32
+ }
33
+ throw err;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Save quota data for a pod
39
+ * @param {string} podName - The pod name
40
+ * @param {object} quota - Quota data
41
+ */
42
+ export async function saveQuota(podName, quota) {
43
+ await fs.writeFile(getQuotaPath(podName), JSON.stringify(quota, null, 2));
44
+ }
45
+
46
+ /**
47
+ * Initialize quota for a new pod
48
+ * @param {string} podName - The pod name
49
+ * @param {number} limit - Quota limit in bytes
50
+ */
51
+ export async function initializeQuota(podName, limit) {
52
+ const quota = { limit, used: 0 };
53
+ await saveQuota(podName, quota);
54
+ return quota;
55
+ }
56
+
57
+ /**
58
+ * Calculate actual disk usage for a pod (for reconciliation)
59
+ * @param {string} podName - The pod name
60
+ * @returns {Promise<number>} Total bytes used
61
+ */
62
+ export async function calculatePodSize(podName) {
63
+ const podPath = join(getDataRoot(), podName);
64
+ return calculateDirSize(podPath);
65
+ }
66
+
67
+ /**
68
+ * Recursively calculate directory size
69
+ */
70
+ async function calculateDirSize(dirPath) {
71
+ let total = 0;
72
+
73
+ try {
74
+ const entries = await fs.readdir(dirPath, { withFileTypes: true });
75
+
76
+ for (const entry of entries) {
77
+ const fullPath = join(dirPath, entry.name);
78
+
79
+ // Skip quota file itself
80
+ if (entry.name === QUOTA_FILE) continue;
81
+
82
+ if (entry.isDirectory()) {
83
+ total += await calculateDirSize(fullPath);
84
+ } else if (entry.isFile()) {
85
+ const stat = await fs.stat(fullPath);
86
+ total += stat.size;
87
+ }
88
+ }
89
+ } catch (err) {
90
+ // Directory might not exist or be inaccessible
91
+ if (err.code !== 'ENOENT') {
92
+ throw err;
93
+ }
94
+ }
95
+
96
+ return total;
97
+ }
98
+
99
+ /**
100
+ * Check if a write operation would exceed quota
101
+ * @param {string} podName - The pod name
102
+ * @param {number} additionalBytes - Bytes to be added
103
+ * @param {number} defaultQuota - Default quota limit
104
+ * @returns {Promise<{allowed: boolean, quota: object, error?: string}>}
105
+ */
106
+ export async function checkQuota(podName, additionalBytes, defaultQuota) {
107
+ let quota = await loadQuota(podName);
108
+
109
+ // Initialize if no quota set
110
+ if (quota.limit === 0 && defaultQuota > 0) {
111
+ quota = await initializeQuota(podName, defaultQuota);
112
+ }
113
+
114
+ // No quota enforcement if limit is 0
115
+ if (quota.limit === 0) {
116
+ return { allowed: true, quota };
117
+ }
118
+
119
+ const projectedUsage = quota.used + additionalBytes;
120
+
121
+ if (projectedUsage > quota.limit) {
122
+ const usedMB = (quota.used / (1024 * 1024)).toFixed(2);
123
+ const limitMB = (quota.limit / (1024 * 1024)).toFixed(2);
124
+ return {
125
+ allowed: false,
126
+ quota,
127
+ error: `Storage quota exceeded. Used: ${usedMB}MB / ${limitMB}MB`
128
+ };
129
+ }
130
+
131
+ return { allowed: true, quota };
132
+ }
133
+
134
+ /**
135
+ * Update quota usage after a write
136
+ * @param {string} podName - The pod name
137
+ * @param {number} bytesChange - Bytes added (positive) or removed (negative)
138
+ */
139
+ export async function updateQuotaUsage(podName, bytesChange) {
140
+ const quota = await loadQuota(podName);
141
+
142
+ // Skip if no quota initialized
143
+ if (quota.limit === 0) return quota;
144
+
145
+ quota.used = Math.max(0, quota.used + bytesChange);
146
+ await saveQuota(podName, quota);
147
+ return quota;
148
+ }
149
+
150
+ /**
151
+ * Set quota limit for a pod
152
+ * @param {string} podName - The pod name
153
+ * @param {number} limit - New limit in bytes
154
+ */
155
+ export async function setQuotaLimit(podName, limit) {
156
+ let quota = await loadQuota(podName);
157
+
158
+ // If no quota exists, calculate current usage
159
+ if (quota.limit === 0) {
160
+ quota.used = await calculatePodSize(podName);
161
+ }
162
+
163
+ quota.limit = limit;
164
+ await saveQuota(podName, quota);
165
+ return quota;
166
+ }
167
+
168
+ /**
169
+ * Get quota info for a pod
170
+ * @param {string} podName - The pod name
171
+ * @returns {Promise<{limit: number, used: number, percent: number}>}
172
+ */
173
+ export async function getQuotaInfo(podName) {
174
+ const quota = await loadQuota(podName);
175
+ const percent = quota.limit > 0 ? Math.round((quota.used / quota.limit) * 100) : 0;
176
+ return { ...quota, percent };
177
+ }
178
+
179
+ /**
180
+ * Reconcile quota with actual disk usage
181
+ * @param {string} podName - The pod name
182
+ */
183
+ export async function reconcileQuota(podName) {
184
+ const quota = await loadQuota(podName);
185
+ if (quota.limit === 0) return quota;
186
+
187
+ const actualUsed = await calculatePodSize(podName);
188
+ quota.used = actualUsed;
189
+ await saveQuota(podName, quota);
190
+ return quota;
191
+ }
192
+
193
+ /**
194
+ * Format bytes as human-readable string
195
+ */
196
+ export function formatBytes(bytes) {
197
+ if (bytes === 0) return '0 B';
198
+ const k = 1024;
199
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
200
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
201
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
202
+ }
package/src/utils/url.js CHANGED
@@ -143,6 +143,43 @@ export function getResourceName(urlPath) {
143
143
  return parts[parts.length - 1];
144
144
  }
145
145
 
146
+ /**
147
+ * Extract pod name from URL path or request
148
+ * @param {string|object} pathOrRequest - URL path string or Fastify request object
149
+ * @returns {string|null} - Pod name or null if not found
150
+ */
151
+ export function getPodName(pathOrRequest) {
152
+ // If it's a request object
153
+ if (typeof pathOrRequest === 'object') {
154
+ // Subdomain mode: pod name from hostname
155
+ if (pathOrRequest.subdomainsEnabled && pathOrRequest.podName) {
156
+ return pathOrRequest.podName;
157
+ }
158
+ // Path mode: extract from URL
159
+ const urlPath = pathOrRequest.url?.split('?')[0] || '';
160
+ return getPodNameFromPath(urlPath);
161
+ }
162
+
163
+ // If it's a string path
164
+ return getPodNameFromPath(pathOrRequest);
165
+ }
166
+
167
+ /**
168
+ * Extract pod name from URL path
169
+ * @param {string} urlPath - URL path (e.g., /alice/public/file.txt)
170
+ * @returns {string|null} - Pod name or null
171
+ */
172
+ function getPodNameFromPath(urlPath) {
173
+ const parts = urlPath.split('/').filter(Boolean);
174
+ if (parts.length === 0) return null;
175
+
176
+ // First segment is the pod name (skip system paths)
177
+ const firstPart = parts[0];
178
+ if (firstPart.startsWith('.')) return null; // .well-known, .acl, etc.
179
+
180
+ return firstPart;
181
+ }
182
+
146
183
  /**
147
184
  * Determine content type from file extension
148
185
  * @param {string} filePath