mbkbucket 3.3.0 → 3.3.2

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.
Files changed (39) hide show
  1. package/index.js +8 -18
  2. package/lib/cli.js +642 -0
  3. package/{public → lib/public}/bucketadmin.js +1 -1
  4. package/{src → lib/src}/app.js +37 -50
  5. package/{src → lib/src}/cli/auth-helper.js +24 -103
  6. package/lib/src/config/index.js +108 -0
  7. package/{src → lib/src}/config/validation.js +14 -30
  8. package/{src → lib/src}/controllers/api.controller.js +85 -176
  9. package/{src → lib/src}/controllers/dashboard.controller.js +12 -23
  10. package/{src → lib/src}/controllers/info.controller.js +17 -20
  11. package/{src → lib/src}/controllers/view.controller.js +23 -70
  12. package/{src → lib/src}/middleware/async-handler.js +1 -16
  13. package/{src → lib/src}/middleware/bucket-resolver.js +7 -13
  14. package/{src → lib/src}/middleware/error-handler.js +6 -14
  15. package/{src → lib/src}/middleware/rate-limiter.js +2 -10
  16. package/{src → lib/src}/middleware/security.js +1 -17
  17. package/lib/src/routes/api.routes.js +40 -0
  18. package/{src → lib/src}/routes/index.js +1 -9
  19. package/{src → lib/src}/routes/info.routes.js +2 -3
  20. package/{src → lib/src}/routes/view.routes.js +3 -8
  21. package/{src → lib/src}/services/s3.service.js +157 -349
  22. package/lib/src/utils/errors.js +45 -0
  23. package/lib/src/utils/helpers.js +251 -0
  24. package/package.json +18 -3
  25. package/.github/PACKAGE.md +0 -19
  26. package/.github/workflows/publish.yml +0 -88
  27. package/cli.js +0 -977
  28. package/index.d.ts +0 -444
  29. package/lib/index.js +0 -17
  30. package/src/config/index.js +0 -133
  31. package/src/routes/api.routes.js +0 -66
  32. package/src/utils/errors.js +0 -76
  33. package/src/utils/helpers.js +0 -197
  34. package/test/config-validation.test.js +0 -116
  35. /package/{public → lib/public}/bucketadmin.css +0 -0
  36. /package/{public → lib/public}/mbkbucket-helper-demo.html +0 -0
  37. /package/{public → lib/public}/mbkbucket-helper.css +0 -0
  38. /package/{public → lib/public}/mbkbucket-helper.js +0 -0
  39. /package/{src → lib/src}/utils/logger.js +0 -0
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Centralized error classification for API and View routes.
3
+ * Maps error messages to HTTP status codes and machine-readable error codes.
4
+ */
5
+
6
+ const API_ERROR_RULES = [
7
+ { match: 'bucketconnection environment variable is not set', status: 503, code: 'BUCKET_CONFIG_ERROR' },
8
+ { match: ['no bucket selected', 'not found in bucketconnection'], status: 400, code: 'INVALID_BUCKET' },
9
+ { match: 'already exists', status: 409, code: 'CONFLICT' },
10
+ { match: 'not found', status: 404, code: 'NOT_FOUND' },
11
+ { match: 'access denied', status: 403, code: 'ACCESS_DENIED' },
12
+ { match: ['required', 'invalid', 'must be'], status: 400, code: 'VALIDATION_ERROR' },
13
+ ];
14
+
15
+ const VIEW_ERROR_RULES = [
16
+ { match: ['no bucket selected', 'not found in bucketconnection'], status: 400, code: 'INVALID_BUCKET' },
17
+ { match: 'access denied', status: 403, code: 'ACCESS_DENIED' },
18
+ { match: 'not found', status: 404, code: 'NOT_FOUND' },
19
+ { match: ['invalid', 'required'], status: 400, code: 'VALIDATION_ERROR' },
20
+ ];
21
+
22
+ function matchError(rules, error, fallbackMessage) {
23
+ const message = String(error?.message || fallbackMessage);
24
+ const lower = message.toLowerCase();
25
+ const found = rules.find(r => Array.isArray(r.match) ? r.match.some(m => lower.includes(m)) : lower.includes(r.match));
26
+ return { status: found?.status ?? 500, code: found?.code ?? 'INTERNAL_ERROR', message };
27
+ }
28
+
29
+ export function classifyApiError(error, fallbackMessage = 'Request failed') {
30
+ return matchError(API_ERROR_RULES, error, fallbackMessage);
31
+ }
32
+
33
+ export function classifyViewError(error, fallbackMessage = 'Request failed') {
34
+ return matchError(VIEW_ERROR_RULES, error, fallbackMessage);
35
+ }
36
+
37
+ export function sendApiError(res, error, fallbackMessage = 'Request failed') {
38
+ const { status, code, message } = classifyApiError(error, fallbackMessage);
39
+ return res.status(status).json({ success: false, code, error: message });
40
+ }
41
+
42
+ export function sendViewError(res, error, fallbackMessage = 'Request failed') {
43
+ const { status, code, message } = classifyViewError(error, fallbackMessage);
44
+ return res.status(status).json({ code, message });
45
+ }
@@ -0,0 +1,251 @@
1
+ /**
2
+ * Shared helper utilities used across controllers, services, and CLI.
3
+ */
4
+
5
+ const HTML_ESCAPE_MAP = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
6
+
7
+ export function escapeHtml(value = '') {
8
+ return String(value ?? '').replace(/[&<>"']/g, c => HTML_ESCAPE_MAP[c]);
9
+ }
10
+
11
+ export function getBaseName(path = '') {
12
+ const value = String(path ?? '');
13
+ const lastSlashIndex = value.lastIndexOf('/');
14
+ return lastSlashIndex === -1 ? value : value.slice(lastSlashIndex + 1);
15
+ }
16
+
17
+ export function getFileExt(fileName = '') {
18
+ const name = getBaseName(fileName);
19
+ const dotIndex = name.lastIndexOf('.');
20
+ return dotIndex === -1 ? '' : name.slice(dotIndex + 1).toLowerCase();
21
+ }
22
+
23
+ export function trimSlashes(str = '') {
24
+ return String(str ?? '').replace(/^\/+/, '').replace(/\/+$/, '');
25
+ }
26
+
27
+ export function trimLeadingSlashes(str = '') {
28
+ return String(str ?? '').replace(/^\/+/, '');
29
+ }
30
+
31
+ export function getFolderPath(key = '') {
32
+ const s = String(key ?? '');
33
+ const lastSlash = s.lastIndexOf('/');
34
+ return lastSlash === -1 ? '' : s.slice(0, lastSlash + 1);
35
+ }
36
+
37
+ export function normalizeKeyParam(rawKey) {
38
+ return Array.isArray(rawKey) ? rawKey.join('/') : String(rawKey || '');
39
+ }
40
+
41
+ export function buildKey(prefix, fileName) {
42
+ const cleanPrefix = trimSlashes(prefix);
43
+ const cleanName = trimLeadingSlashes(fileName);
44
+ return cleanPrefix ? `${cleanPrefix}/${cleanName}` : cleanName;
45
+ }
46
+
47
+ export function nowIso() {
48
+ return new Date().toISOString();
49
+ }
50
+
51
+ export function formatBytes(bytes) {
52
+ if (!bytes || bytes === 0) return '0 B';
53
+ const units = ['B', 'KB', 'MB', 'GB', 'TB'];
54
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
55
+ return `${(bytes / Math.pow(1024, i)).toFixed(i > 0 ? 1 : 0)} ${units[i]}`;
56
+ }
57
+
58
+ export function formatDate(iso) {
59
+ try {
60
+ return new Date(iso).toLocaleString();
61
+ } catch {
62
+ return iso || '—';
63
+ }
64
+ }
65
+
66
+ export async function streamToBuffer(stream) {
67
+ if (Buffer.isBuffer(stream)) return stream;
68
+ const chunks = [];
69
+ if (stream?.[Symbol.asyncIterator] || stream?.transformToWeb) {
70
+ for await (const chunk of stream) {
71
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
72
+ }
73
+ return Buffer.concat(chunks);
74
+ }
75
+ if (stream?.getReader) {
76
+ const reader = stream.getReader();
77
+ while (true) {
78
+ const { done, value } = await reader.read();
79
+ if (done) break;
80
+ chunks.push(Buffer.isBuffer(value) ? value : Buffer.from(value));
81
+ }
82
+ return Buffer.concat(chunks);
83
+ }
84
+ return Buffer.concat(chunks);
85
+ }
86
+
87
+ export function destroyStream(stream) {
88
+ if (stream && typeof stream.destroy === 'function') {
89
+ stream.removeAllListeners?.();
90
+ stream.destroy();
91
+ }
92
+ }
93
+
94
+ // ---------------------------------------------------------------------------
95
+ // MIME type & file extension constants
96
+ // ---------------------------------------------------------------------------
97
+
98
+ export const VIEWABLE_TYPES = new Set([
99
+ 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico',
100
+ 'mp4', 'webm', 'ogg', 'avi', 'mov',
101
+ 'mp3', 'wav', 'flac', 'aac', 'm4a',
102
+ 'txt', 'md', 'json', 'xml', 'csv', 'log',
103
+ 'js', 'ts', 'html', 'htm', 'css', 'php', 'py', 'java', 'cpp', 'c', 'h',
104
+ 'cs', 'rb', 'go', 'rs', 'sql', 'sh', 'bat', 'ps1', 'yaml', 'yml',
105
+ 'toml', 'ini', 'conf', 'pdf'
106
+ ]);
107
+
108
+ export const VIEWABLE_TYPES_SORTED = [...VIEWABLE_TYPES].sort();
109
+ export const STATIC_ASSET_TYPES = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'ico', 'css', 'js']);
110
+ export const VIDEO_TYPES = new Set(['mp4', 'webm', 'ogg', 'avi', 'mov']);
111
+ export const AUDIO_TYPES = new Set(['mp3', 'wav', 'flac', 'aac', 'm4a']);
112
+
113
+ export const MIME_TYPES = {
114
+ // Images
115
+ jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png',
116
+ gif: 'image/gif', webp: 'image/webp', svg: 'image/svg+xml',
117
+ bmp: 'image/bmp', ico: 'image/x-icon',
118
+ // Videos
119
+ mp4: 'video/mp4', webm: 'video/webm', ogg: 'video/ogg',
120
+ avi: 'video/x-msvideo', mov: 'video/quicktime',
121
+ // Audio
122
+ mp3: 'audio/mpeg', wav: 'audio/wav', flac: 'audio/flac',
123
+ aac: 'audio/aac', m4a: 'audio/mp4',
124
+ // Text / Code
125
+ txt: 'text/plain; charset=utf-8', md: 'text/markdown; charset=utf-8',
126
+ json: 'application/json; charset=utf-8', xml: 'application/xml; charset=utf-8',
127
+ csv: 'text/csv; charset=utf-8', log: 'text/plain; charset=utf-8',
128
+ js: 'text/javascript; charset=utf-8', ts: 'text/typescript; charset=utf-8',
129
+ html: 'text/html; charset=utf-8', htm: 'text/html; charset=utf-8',
130
+ css: 'text/css; charset=utf-8', php: 'text/x-php; charset=utf-8',
131
+ py: 'text/x-python; charset=utf-8', java: 'text/x-java-source; charset=utf-8',
132
+ cpp: 'text/x-c++src; charset=utf-8', c: 'text/x-csrc; charset=utf-8',
133
+ h: 'text/x-chdr; charset=utf-8', cs: 'text/x-csharp; charset=utf-8',
134
+ rb: 'text/x-ruby; charset=utf-8', go: 'text/x-go; charset=utf-8',
135
+ rs: 'text/x-rust; charset=utf-8', sql: 'text/x-sql; charset=utf-8',
136
+ sh: 'text/x-shellscript; charset=utf-8', bat: 'text/x-msdos-batch; charset=utf-8',
137
+ ps1: 'text/x-powershell; charset=utf-8', yaml: 'text/x-yaml; charset=utf-8',
138
+ yml: 'text/x-yaml; charset=utf-8', toml: 'text/x-toml; charset=utf-8',
139
+ ini: 'text/x-ini; charset=utf-8', conf: 'text/x-conf; charset=utf-8',
140
+ // Archives & Documents
141
+ zip: 'application/zip', gz: 'application/gzip', tar: 'application/x-tar',
142
+ pdf: 'application/pdf'
143
+ };
144
+
145
+ export function getMimeType(fileName, fallback = 'application/octet-stream') {
146
+ return MIME_TYPES[getFileExt(fileName)] || fallback;
147
+ }
148
+
149
+ export function isSensitiveTextType(contentType = '') {
150
+ const type = String(contentType ?? '').toLowerCase();
151
+ return type.startsWith('text/') || /json|xml|javascript|typescript/.test(type);
152
+ }
153
+
154
+ export function buildCacheControl(contentType, { isStaticAsset = false, supportsRanges = false, publicCache = false } = {}) {
155
+ if (publicCache) {
156
+ if (isStaticAsset) return 'public, max-age=3600, immutable';
157
+ if (supportsRanges) return 'public, max-age=86400, must-revalidate';
158
+ return 'public, max-age=300, must-revalidate';
159
+ }
160
+ if (isSensitiveTextType(contentType)) return 'private, no-store';
161
+ return supportsRanges ? 'private, max-age=1800, must-revalidate' : 'private, max-age=300, must-revalidate';
162
+ }
163
+
164
+ // ---------------------------------------------------------------------------
165
+ // Range header parsing
166
+ // ---------------------------------------------------------------------------
167
+
168
+ const DEFAULT_CHUNK_SIZE = 8 * 1024 * 1024; // 8 MB
169
+
170
+ export function parseRangeHeader(rangeHeader, totalSize) {
171
+ if (!totalSize || totalSize <= 0) return null;
172
+
173
+ const match = /^bytes=(\d*)-(\d*)$/.exec(String(rangeHeader ?? '').trim());
174
+ if (!match) return null;
175
+
176
+ const [, startStr, endStr] = match;
177
+ let start;
178
+ let end;
179
+
180
+ if (startStr === '') {
181
+ const suffix = parseInt(endStr, 10);
182
+ if (isNaN(suffix) || suffix <= 0) return null;
183
+ start = Math.max(0, totalSize - suffix);
184
+ end = totalSize - 1;
185
+ } else {
186
+ start = parseInt(startStr, 10);
187
+ if (isNaN(start) || start < 0) return null;
188
+ if (endStr === '') {
189
+ end = Math.min(start + DEFAULT_CHUNK_SIZE - 1, totalSize - 1);
190
+ } else {
191
+ end = parseInt(endStr, 10);
192
+ if (isNaN(end) || end < start) return null;
193
+ }
194
+ }
195
+
196
+ if (start >= totalSize || start > end || end >= totalSize) return null;
197
+ return { start, end, total: totalSize, size: end - start + 1 };
198
+ }
199
+
200
+ // ---------------------------------------------------------------------------
201
+ // Player page HTML template
202
+ // ---------------------------------------------------------------------------
203
+
204
+ export function renderPlayerPage(safeFileName, encodedKey, bucketQuery) {
205
+ return `<!doctype html>
206
+ <html lang="en">
207
+ <head>
208
+ <meta charset="utf-8">
209
+ <meta name="viewport" content="width=device-width,initial-scale=1">
210
+ <title>Preview: ${safeFileName}</title>
211
+ <style>
212
+ * { margin: 0; padding: 0; box-sizing: border-box; }
213
+ body { margin: 0; display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #000; color: #fff; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
214
+ .player-container { width: 100%; max-width: 100vw; max-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
215
+ video { width: 100%; height: auto; max-width: 100%; max-height: calc(100vh - 40px); object-fit: contain; background: #000; }
216
+ .file-name { position: fixed; top: 10px; left: 10px; background: rgba(0, 0, 0, 0.8); padding: 8px 16px; border-radius: 4px; font-size: 14px; z-index: 10; }
217
+ </style>
218
+ </head>
219
+ <body>
220
+ <div class="file-name">${safeFileName}</div>
221
+ <div class="player-container">
222
+ <video controls preload="metadata" src="/mbkbucket/view/${encodedKey}${bucketQuery}">
223
+ Your browser does not support the video tag.
224
+ </video>
225
+ </div>
226
+ </body>
227
+ </html>`;
228
+ }
229
+
230
+ // ---------------------------------------------------------------------------
231
+ // Common Handlebars helpers
232
+ // ---------------------------------------------------------------------------
233
+
234
+ export const commonHandlebarsHelpers = {
235
+ eq: (a, b) => a === b,
236
+ neq: (a, b) => a !== b,
237
+ or: (...args) => args.slice(0, -1).some(Boolean),
238
+ and: (...args) => args.slice(0, -1).every(Boolean),
239
+ not: (val) => !val,
240
+ gt: (a, b) => a > b,
241
+ gte: (a, b) => a >= b,
242
+ lt: (a, b) => a < b,
243
+ lte: (a, b) => a <= b,
244
+ includes: (arr, val) => Array.isArray(arr) && arr.includes(val),
245
+ json: (obj) => JSON.stringify(obj, null, 2),
246
+ jsonStringify: (obj) => JSON.stringify(obj),
247
+ getInitials: (username) => {
248
+ if (!username) return '?';
249
+ return username.split(/[._]/).map(p => p.charAt(0)).join('').toUpperCase().slice(0, 2);
250
+ },
251
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mbkbucket",
3
- "version": "3.3.0",
3
+ "version": "3.3.2",
4
4
  "description": "Flexible S3/R2 bucket management — library, Express router, and CLI for mbktech.org applications. Provides upload, download, listing, multipart utilities, signed URLs, and a command-line interface with built-in configuration validation and health checks",
5
5
  "author": "Muhammad Bin Khalid",
6
6
  "homepage": "https://github.com/MIbnEKhalid/mbkbucket#readme",
@@ -9,12 +9,16 @@
9
9
  "type": "module",
10
10
  "types": "index.d.ts",
11
11
  "bin": {
12
- "mbkbucket": "./cli.js"
12
+ "mbkbucket": "./lib/cli.js"
13
13
  },
14
14
  "scripts": {
15
15
  "dev": "cross-env NODE_ENV=dev nodemon index.js",
16
16
  "test": "node --test"
17
17
  },
18
+ "imports": {
19
+ "#helpers": "./lib/src/utils/helpers.js",
20
+ "#logger": "./lib/src/utils/logger.js"
21
+ },
18
22
  "dependencies": {
19
23
  "@aws-sdk/client-s3": "^3.968.0",
20
24
  "@aws-sdk/s3-request-presigner": "^3.1000.0",
@@ -44,6 +48,17 @@
44
48
  "bugs": {
45
49
  "url": "https://github.com/MIbnEKhalid/mbkbucket/issues"
46
50
  },
51
+ "files": [
52
+ "lib",
53
+ "views",
54
+ ".env.example",
55
+ ".mbkbucket.config.example.json",
56
+ "cli.js",
57
+ "index.js",
58
+ "LICENSE",
59
+ "package.json",
60
+ "README.md"
61
+ ],
47
62
  "keywords": [
48
63
  "mbkbucket",
49
64
  "s3-bucket",
@@ -62,4 +77,4 @@
62
77
  "file-upload",
63
78
  "file-download"
64
79
  ]
65
- }
80
+ }
@@ -1,19 +0,0 @@
1
- # Package Information
2
-
3
- This package is published on npm: [mbkbucket](https://www.npmjs.com/package/mbkbucket)
4
-
5
- ## Installation
6
-
7
- ```bash
8
- npm install mbkbucket
9
- ```
10
-
11
- ## What's Included
12
-
13
- - **Library** — Full S3/R2 bucket management API (upload, download, list, delete, multipart, signed URLs)
14
- - **Express Router** — Ready-to-use admin dashboard with Handlebars templates
15
- - **CLI** — Command-line interface for managing buckets from the terminal ⚠️ *under development*
16
-
17
- ## Registry
18
-
19
- Published to: npm Registry (https://registry.npmjs.org/)
@@ -1,88 +0,0 @@
1
- name: Publish to npm and GitHub Packages
2
- on:
3
- push:
4
- branches:
5
- - main
6
- - master
7
-
8
- permissions:
9
- contents: read
10
- packages: write
11
-
12
- jobs:
13
- publish:
14
- runs-on: ubuntu-latest
15
- permissions:
16
- contents: read
17
- packages: write
18
- steps:
19
- - name: Checkout code
20
- uses: actions/checkout@v3
21
-
22
- - name: Setup Node.js for npm
23
- uses: actions/setup-node@v3
24
- with:
25
- node-version: '18'
26
- registry-url: 'https://registry.npmjs.org'
27
-
28
- - name: Cache Node.js modules
29
- uses: actions/cache@v3
30
- with:
31
- path: ~/.npm
32
- key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
33
- restore-keys: |
34
- ${{ runner.os }}-node-
35
-
36
- - name: Install dependencies
37
- run: |
38
- for i in {1..3}; do npm install && break || sleep 10; done
39
-
40
- - name: Check if version exists on npm
41
- id: check_version
42
- run: |
43
- PACKAGE_NAME=$(node -p "require('./package.json').name")
44
- VERSION=$(node -p "require('./package.json').version")
45
- if npm view $PACKAGE_NAME@$VERSION version 2>/dev/null; then
46
- echo "exists=true" >> $GITHUB_OUTPUT
47
- echo "Version $VERSION already exists on npm"
48
- else
49
- echo "exists=false" >> $GITHUB_OUTPUT
50
- echo "Version $VERSION does not exist, will publish"
51
- fi
52
-
53
- - name: Publish to npm
54
- if: steps.check_version.outputs.exists == 'false'
55
- run: npm publish
56
- env:
57
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
58
-
59
- - name: Check if version exists on GitHub Packages
60
- id: check_github_version
61
- run: |
62
- VERSION=$(node -p "require('./package.json').version")
63
- if curl -f -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
64
- https://npm.pkg.github.com/@mibnekhalid/mbkbucket/$VERSION 2>/dev/null; then
65
- echo "exists=true" >> $GITHUB_OUTPUT
66
- echo "Version $VERSION already exists on GitHub Packages"
67
- else
68
- echo "exists=false" >> $GITHUB_OUTPUT
69
- echo "Version $VERSION does not exist on GitHub Packages, will publish"
70
- fi
71
-
72
- - name: Modify package.json for GitHub Packages
73
- if: steps.check_github_version.outputs.exists == 'false'
74
- run: |
75
- node -e "const pkg = require('./package.json'); pkg.name = '@mibnekhalid/mbkbucket'; pkg.publishConfig = { registry: 'https://npm.pkg.github.com' }; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));"
76
-
77
- - name: Setup Node.js for GitHub Packages
78
- if: steps.check_github_version.outputs.exists == 'false'
79
- uses: actions/setup-node@v3
80
- with:
81
- node-version: '18'
82
- registry-url: 'https://npm.pkg.github.com'
83
-
84
- - name: Publish to GitHub Packages
85
- if: steps.check_github_version.outputs.exists == 'false'
86
- run: npm publish
87
- env:
88
- NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}