@putkoff/abstract-utilities 1.0.103 → 1.0.124

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.
@@ -0,0 +1,897 @@
1
+ import * as fs from 'node:fs';
2
+ import * as fsp from 'node:fs/promises';
3
+ import * as path from 'node:path';
4
+
5
+ /** True if token is structurally bad or its exp ≤ now. */
6
+ function isTokenExpired(token) {
7
+ try {
8
+ const payload = decodeJwt(token);
9
+ return Date.now() / 1000 >= payload.exp;
10
+ }
11
+ catch {
12
+ return true; // treat malformed token as expired
13
+ }
14
+ }
15
+ /* ------------------------------------------------------------------ */
16
+ /* internals */
17
+ /* ------------------------------------------------------------------ */
18
+ function decodeJwt(token) {
19
+ const [header, payload, signature] = token.split(".");
20
+ if (!header || !payload || !signature) {
21
+ throw new Error("Malformed JWT");
22
+ }
23
+ // Handle URL-safe Base64
24
+ let b64 = payload.replace(/-/g, "+").replace(/_/g, "/");
25
+ // Add padding if necessary
26
+ b64 = b64.padEnd(Math.ceil(b64.length / 4) * 4, "=");
27
+ const jsonText = atob(b64);
28
+ return JSON.parse(jsonText);
29
+ }
30
+
31
+ function assertPath(path) {
32
+ if (typeof path !== 'string') {
33
+ throw new TypeError('Path must be a string. Received ' + JSON.stringify(path));
34
+ }
35
+ }
36
+
37
+ // Resolves . and .. elements in a path with directory names
38
+ function normalizeStringPosix(path, allowAboveRoot) {
39
+ var res = '';
40
+ var lastSegmentLength = 0;
41
+ var lastSlash = -1;
42
+ var dots = 0;
43
+ var code;
44
+ for (var i = 0; i <= path.length; ++i) {
45
+ if (i < path.length)
46
+ code = path.charCodeAt(i);
47
+ else if (code === 47 /*/*/)
48
+ break;
49
+ else
50
+ code = 47 /*/*/;
51
+ if (code === 47 /*/*/) {
52
+ if (lastSlash === i - 1 || dots === 1) ; else if (lastSlash !== i - 1 && dots === 2) {
53
+ if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== 46 /*.*/ || res.charCodeAt(res.length - 2) !== 46 /*.*/) {
54
+ if (res.length > 2) {
55
+ var lastSlashIndex = res.lastIndexOf('/');
56
+ if (lastSlashIndex !== res.length - 1) {
57
+ if (lastSlashIndex === -1) {
58
+ res = '';
59
+ lastSegmentLength = 0;
60
+ } else {
61
+ res = res.slice(0, lastSlashIndex);
62
+ lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
63
+ }
64
+ lastSlash = i;
65
+ dots = 0;
66
+ continue;
67
+ }
68
+ } else if (res.length === 2 || res.length === 1) {
69
+ res = '';
70
+ lastSegmentLength = 0;
71
+ lastSlash = i;
72
+ dots = 0;
73
+ continue;
74
+ }
75
+ }
76
+ if (allowAboveRoot) {
77
+ if (res.length > 0)
78
+ res += '/..';
79
+ else
80
+ res = '..';
81
+ lastSegmentLength = 2;
82
+ }
83
+ } else {
84
+ if (res.length > 0)
85
+ res += '/' + path.slice(lastSlash + 1, i);
86
+ else
87
+ res = path.slice(lastSlash + 1, i);
88
+ lastSegmentLength = i - lastSlash - 1;
89
+ }
90
+ lastSlash = i;
91
+ dots = 0;
92
+ } else if (code === 46 /*.*/ && dots !== -1) {
93
+ ++dots;
94
+ } else {
95
+ dots = -1;
96
+ }
97
+ }
98
+ return res;
99
+ }
100
+
101
+ function _format(sep, pathObject) {
102
+ var dir = pathObject.dir || pathObject.root;
103
+ var base = pathObject.base || (pathObject.name || '') + (pathObject.ext || '');
104
+ if (!dir) {
105
+ return base;
106
+ }
107
+ if (dir === pathObject.root) {
108
+ return dir + base;
109
+ }
110
+ return dir + sep + base;
111
+ }
112
+
113
+ var posix = {
114
+ // path.resolve([from ...], to)
115
+ resolve: function resolve() {
116
+ var resolvedPath = '';
117
+ var resolvedAbsolute = false;
118
+ var cwd;
119
+
120
+ for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
121
+ var path;
122
+ if (i >= 0)
123
+ path = arguments[i];
124
+ else {
125
+ if (cwd === undefined)
126
+ cwd = process.cwd();
127
+ path = cwd;
128
+ }
129
+
130
+ assertPath(path);
131
+
132
+ // Skip empty entries
133
+ if (path.length === 0) {
134
+ continue;
135
+ }
136
+
137
+ resolvedPath = path + '/' + resolvedPath;
138
+ resolvedAbsolute = path.charCodeAt(0) === 47 /*/*/;
139
+ }
140
+
141
+ // At this point the path should be resolved to a full absolute path, but
142
+ // handle relative paths to be safe (might happen when process.cwd() fails)
143
+
144
+ // Normalize the path
145
+ resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
146
+
147
+ if (resolvedAbsolute) {
148
+ if (resolvedPath.length > 0)
149
+ return '/' + resolvedPath;
150
+ else
151
+ return '/';
152
+ } else if (resolvedPath.length > 0) {
153
+ return resolvedPath;
154
+ } else {
155
+ return '.';
156
+ }
157
+ },
158
+
159
+ normalize: function normalize(path) {
160
+ assertPath(path);
161
+
162
+ if (path.length === 0) return '.';
163
+
164
+ var isAbsolute = path.charCodeAt(0) === 47 /*/*/;
165
+ var trailingSeparator = path.charCodeAt(path.length - 1) === 47 /*/*/;
166
+
167
+ // Normalize the path
168
+ path = normalizeStringPosix(path, !isAbsolute);
169
+
170
+ if (path.length === 0 && !isAbsolute) path = '.';
171
+ if (path.length > 0 && trailingSeparator) path += '/';
172
+
173
+ if (isAbsolute) return '/' + path;
174
+ return path;
175
+ },
176
+
177
+ isAbsolute: function isAbsolute(path) {
178
+ assertPath(path);
179
+ return path.length > 0 && path.charCodeAt(0) === 47 /*/*/;
180
+ },
181
+
182
+ join: function join() {
183
+ if (arguments.length === 0)
184
+ return '.';
185
+ var joined;
186
+ for (var i = 0; i < arguments.length; ++i) {
187
+ var arg = arguments[i];
188
+ assertPath(arg);
189
+ if (arg.length > 0) {
190
+ if (joined === undefined)
191
+ joined = arg;
192
+ else
193
+ joined += '/' + arg;
194
+ }
195
+ }
196
+ if (joined === undefined)
197
+ return '.';
198
+ return posix.normalize(joined);
199
+ },
200
+
201
+ relative: function relative(from, to) {
202
+ assertPath(from);
203
+ assertPath(to);
204
+
205
+ if (from === to) return '';
206
+
207
+ from = posix.resolve(from);
208
+ to = posix.resolve(to);
209
+
210
+ if (from === to) return '';
211
+
212
+ // Trim any leading backslashes
213
+ var fromStart = 1;
214
+ for (; fromStart < from.length; ++fromStart) {
215
+ if (from.charCodeAt(fromStart) !== 47 /*/*/)
216
+ break;
217
+ }
218
+ var fromEnd = from.length;
219
+ var fromLen = fromEnd - fromStart;
220
+
221
+ // Trim any leading backslashes
222
+ var toStart = 1;
223
+ for (; toStart < to.length; ++toStart) {
224
+ if (to.charCodeAt(toStart) !== 47 /*/*/)
225
+ break;
226
+ }
227
+ var toEnd = to.length;
228
+ var toLen = toEnd - toStart;
229
+
230
+ // Compare paths to find the longest common path from root
231
+ var length = fromLen < toLen ? fromLen : toLen;
232
+ var lastCommonSep = -1;
233
+ var i = 0;
234
+ for (; i <= length; ++i) {
235
+ if (i === length) {
236
+ if (toLen > length) {
237
+ if (to.charCodeAt(toStart + i) === 47 /*/*/) {
238
+ // We get here if `from` is the exact base path for `to`.
239
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
240
+ return to.slice(toStart + i + 1);
241
+ } else if (i === 0) {
242
+ // We get here if `from` is the root
243
+ // For example: from='/'; to='/foo'
244
+ return to.slice(toStart + i);
245
+ }
246
+ } else if (fromLen > length) {
247
+ if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
248
+ // We get here if `to` is the exact base path for `from`.
249
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
250
+ lastCommonSep = i;
251
+ } else if (i === 0) {
252
+ // We get here if `to` is the root.
253
+ // For example: from='/foo'; to='/'
254
+ lastCommonSep = 0;
255
+ }
256
+ }
257
+ break;
258
+ }
259
+ var fromCode = from.charCodeAt(fromStart + i);
260
+ var toCode = to.charCodeAt(toStart + i);
261
+ if (fromCode !== toCode)
262
+ break;
263
+ else if (fromCode === 47 /*/*/)
264
+ lastCommonSep = i;
265
+ }
266
+
267
+ var out = '';
268
+ // Generate the relative path based on the path difference between `to`
269
+ // and `from`
270
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
271
+ if (i === fromEnd || from.charCodeAt(i) === 47 /*/*/) {
272
+ if (out.length === 0)
273
+ out += '..';
274
+ else
275
+ out += '/..';
276
+ }
277
+ }
278
+
279
+ // Lastly, append the rest of the destination (`to`) path that comes after
280
+ // the common path parts
281
+ if (out.length > 0)
282
+ return out + to.slice(toStart + lastCommonSep);
283
+ else {
284
+ toStart += lastCommonSep;
285
+ if (to.charCodeAt(toStart) === 47 /*/*/)
286
+ ++toStart;
287
+ return to.slice(toStart);
288
+ }
289
+ },
290
+
291
+ _makeLong: function _makeLong(path) {
292
+ return path;
293
+ },
294
+
295
+ dirname: function dirname(path) {
296
+ assertPath(path);
297
+ if (path.length === 0) return '.';
298
+ var code = path.charCodeAt(0);
299
+ var hasRoot = code === 47 /*/*/;
300
+ var end = -1;
301
+ var matchedSlash = true;
302
+ for (var i = path.length - 1; i >= 1; --i) {
303
+ code = path.charCodeAt(i);
304
+ if (code === 47 /*/*/) {
305
+ if (!matchedSlash) {
306
+ end = i;
307
+ break;
308
+ }
309
+ } else {
310
+ // We saw the first non-path separator
311
+ matchedSlash = false;
312
+ }
313
+ }
314
+
315
+ if (end === -1) return hasRoot ? '/' : '.';
316
+ if (hasRoot && end === 1) return '//';
317
+ return path.slice(0, end);
318
+ },
319
+
320
+ basename: function basename(path, ext) {
321
+ if (ext !== undefined && typeof ext !== 'string') throw new TypeError('"ext" argument must be a string');
322
+ assertPath(path);
323
+
324
+ var start = 0;
325
+ var end = -1;
326
+ var matchedSlash = true;
327
+ var i;
328
+
329
+ if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
330
+ if (ext.length === path.length && ext === path) return '';
331
+ var extIdx = ext.length - 1;
332
+ var firstNonSlashEnd = -1;
333
+ for (i = path.length - 1; i >= 0; --i) {
334
+ var code = path.charCodeAt(i);
335
+ if (code === 47 /*/*/) {
336
+ // If we reached a path separator that was not part of a set of path
337
+ // separators at the end of the string, stop now
338
+ if (!matchedSlash) {
339
+ start = i + 1;
340
+ break;
341
+ }
342
+ } else {
343
+ if (firstNonSlashEnd === -1) {
344
+ // We saw the first non-path separator, remember this index in case
345
+ // we need it if the extension ends up not matching
346
+ matchedSlash = false;
347
+ firstNonSlashEnd = i + 1;
348
+ }
349
+ if (extIdx >= 0) {
350
+ // Try to match the explicit extension
351
+ if (code === ext.charCodeAt(extIdx)) {
352
+ if (--extIdx === -1) {
353
+ // We matched the extension, so mark this as the end of our path
354
+ // component
355
+ end = i;
356
+ }
357
+ } else {
358
+ // Extension does not match, so our result is the entire path
359
+ // component
360
+ extIdx = -1;
361
+ end = firstNonSlashEnd;
362
+ }
363
+ }
364
+ }
365
+ }
366
+
367
+ if (start === end) end = firstNonSlashEnd;else if (end === -1) end = path.length;
368
+ return path.slice(start, end);
369
+ } else {
370
+ for (i = path.length - 1; i >= 0; --i) {
371
+ if (path.charCodeAt(i) === 47 /*/*/) {
372
+ // If we reached a path separator that was not part of a set of path
373
+ // separators at the end of the string, stop now
374
+ if (!matchedSlash) {
375
+ start = i + 1;
376
+ break;
377
+ }
378
+ } else if (end === -1) {
379
+ // We saw the first non-path separator, mark this as the end of our
380
+ // path component
381
+ matchedSlash = false;
382
+ end = i + 1;
383
+ }
384
+ }
385
+
386
+ if (end === -1) return '';
387
+ return path.slice(start, end);
388
+ }
389
+ },
390
+
391
+ extname: function extname(path) {
392
+ assertPath(path);
393
+ var startDot = -1;
394
+ var startPart = 0;
395
+ var end = -1;
396
+ var matchedSlash = true;
397
+ // Track the state of characters (if any) we see before our first dot and
398
+ // after any path separator we find
399
+ var preDotState = 0;
400
+ for (var i = path.length - 1; i >= 0; --i) {
401
+ var code = path.charCodeAt(i);
402
+ if (code === 47 /*/*/) {
403
+ // If we reached a path separator that was not part of a set of path
404
+ // separators at the end of the string, stop now
405
+ if (!matchedSlash) {
406
+ startPart = i + 1;
407
+ break;
408
+ }
409
+ continue;
410
+ }
411
+ if (end === -1) {
412
+ // We saw the first non-path separator, mark this as the end of our
413
+ // extension
414
+ matchedSlash = false;
415
+ end = i + 1;
416
+ }
417
+ if (code === 46 /*.*/) {
418
+ // If this is our first dot, mark it as the start of our extension
419
+ if (startDot === -1)
420
+ startDot = i;
421
+ else if (preDotState !== 1)
422
+ preDotState = 1;
423
+ } else if (startDot !== -1) {
424
+ // We saw a non-dot and non-path separator before our dot, so we should
425
+ // have a good chance at having a non-empty extension
426
+ preDotState = -1;
427
+ }
428
+ }
429
+
430
+ if (startDot === -1 || end === -1 ||
431
+ // We saw a non-dot character immediately before the dot
432
+ preDotState === 0 ||
433
+ // The (right-most) trimmed path component is exactly '..'
434
+ preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
435
+ return '';
436
+ }
437
+ return path.slice(startDot, end);
438
+ },
439
+
440
+ format: function format(pathObject) {
441
+ if (pathObject === null || typeof pathObject !== 'object') {
442
+ throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof pathObject);
443
+ }
444
+ return _format('/', pathObject);
445
+ },
446
+
447
+ parse: function parse(path) {
448
+ assertPath(path);
449
+
450
+ var ret = { root: '', dir: '', base: '', ext: '', name: '' };
451
+ if (path.length === 0) return ret;
452
+ var code = path.charCodeAt(0);
453
+ var isAbsolute = code === 47 /*/*/;
454
+ var start;
455
+ if (isAbsolute) {
456
+ ret.root = '/';
457
+ start = 1;
458
+ } else {
459
+ start = 0;
460
+ }
461
+ var startDot = -1;
462
+ var startPart = 0;
463
+ var end = -1;
464
+ var matchedSlash = true;
465
+ var i = path.length - 1;
466
+
467
+ // Track the state of characters (if any) we see before our first dot and
468
+ // after any path separator we find
469
+ var preDotState = 0;
470
+
471
+ // Get non-dir info
472
+ for (; i >= start; --i) {
473
+ code = path.charCodeAt(i);
474
+ if (code === 47 /*/*/) {
475
+ // If we reached a path separator that was not part of a set of path
476
+ // separators at the end of the string, stop now
477
+ if (!matchedSlash) {
478
+ startPart = i + 1;
479
+ break;
480
+ }
481
+ continue;
482
+ }
483
+ if (end === -1) {
484
+ // We saw the first non-path separator, mark this as the end of our
485
+ // extension
486
+ matchedSlash = false;
487
+ end = i + 1;
488
+ }
489
+ if (code === 46 /*.*/) {
490
+ // If this is our first dot, mark it as the start of our extension
491
+ if (startDot === -1) startDot = i;else if (preDotState !== 1) preDotState = 1;
492
+ } else if (startDot !== -1) {
493
+ // We saw a non-dot and non-path separator before our dot, so we should
494
+ // have a good chance at having a non-empty extension
495
+ preDotState = -1;
496
+ }
497
+ }
498
+
499
+ if (startDot === -1 || end === -1 ||
500
+ // We saw a non-dot character immediately before the dot
501
+ preDotState === 0 ||
502
+ // The (right-most) trimmed path component is exactly '..'
503
+ preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
504
+ if (end !== -1) {
505
+ if (startPart === 0 && isAbsolute) ret.base = ret.name = path.slice(1, end);else ret.base = ret.name = path.slice(startPart, end);
506
+ }
507
+ } else {
508
+ if (startPart === 0 && isAbsolute) {
509
+ ret.name = path.slice(1, startDot);
510
+ ret.base = path.slice(1, end);
511
+ } else {
512
+ ret.name = path.slice(startPart, startDot);
513
+ ret.base = path.slice(startPart, end);
514
+ }
515
+ ret.ext = path.slice(startDot, end);
516
+ }
517
+
518
+ if (startPart > 0) ret.dir = path.slice(0, startPart - 1);else if (isAbsolute) ret.dir = '/';
519
+
520
+ return ret;
521
+ },
522
+
523
+ sep: '/',
524
+ delimiter: ':',
525
+ win32: null,
526
+ posix: null
527
+ };
528
+
529
+ posix.posix = posix;
530
+
531
+ var pathBrowserify = posix;
532
+
533
+ // mediaTypes.ts
534
+ /** ---- Data: large but explicit, mirrors your Python mapping ---- */
535
+ const MIME_TYPES = {
536
+ image: {
537
+ ".jpg": "image/jpeg",
538
+ ".jpeg": "image/jpeg",
539
+ ".png": "image/png",
540
+ ".gif": "image/gif",
541
+ ".bmp": "image/bmp",
542
+ ".tiff": "image/tiff",
543
+ ".webp": "image/webp",
544
+ ".svg": "image/svg+xml",
545
+ ".ico": "image/vnd.microsoft.icon",
546
+ ".heic": "image/heic",
547
+ ".psd": "image/vnd.adobe.photoshop",
548
+ ".raw": "image/x-raw",
549
+ },
550
+ video: {
551
+ ".mp4": "video/mp4",
552
+ ".webm": "video/webm",
553
+ ".ogg": "video/ogg",
554
+ ".mov": "video/quicktime",
555
+ ".avi": "video/x-msvideo",
556
+ ".mkv": "video/x-matroska",
557
+ ".flv": "video/x-flv",
558
+ ".wmv": "video/x-ms-wmv",
559
+ ".3gp": "video/3gpp",
560
+ ".ts": "video/mp2t",
561
+ ".mpeg": "video/mpeg",
562
+ ".mpg": "video/mpg",
563
+ },
564
+ audio: {
565
+ ".mp3": "audio/mpeg",
566
+ ".wav": "audio/wav",
567
+ ".flac": "audio/flac",
568
+ ".aac": "audio/aac",
569
+ ".ogg": "audio/ogg",
570
+ ".m4a": "audio/mp4",
571
+ ".opus": "audio/opus",
572
+ },
573
+ document: {
574
+ ".pdf": "application/pdf",
575
+ ".doc": "application/msword",
576
+ ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
577
+ ".odt": "application/vnd.oasis.opendocument.text",
578
+ ".txt": "text/plain",
579
+ ".rtf": "application/rtf",
580
+ ".md": "text/markdown",
581
+ ".markdown": "text/markdown",
582
+ ".tex": "application/x-tex",
583
+ ".log": "text/plain",
584
+ ".json": "application/json",
585
+ ".xml": "application/xml",
586
+ ".yaml": "application/x-yaml",
587
+ ".yml": "application/x-yaml",
588
+ ".ini": "text/plain",
589
+ ".cfg": "text/plain",
590
+ ".toml": "application/toml",
591
+ ".csv": "text/csv",
592
+ ".tsv": "text/tab-separated-values",
593
+ },
594
+ presentation: {
595
+ ".ppt": "application/vnd.ms-powerpoint",
596
+ ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
597
+ ".odp": "application/vnd.oasis.opendocument.presentation",
598
+ },
599
+ spreadsheet: {
600
+ ".xls": "application/vnd.ms-excel",
601
+ ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
602
+ ".ods": "application/vnd.oasis.opendocument.spreadsheet",
603
+ ".csv": "text/csv",
604
+ ".tsv": "text/tab-separated-values",
605
+ },
606
+ code: {
607
+ ".py": "text/x-python",
608
+ ".java": "text/x-java-source",
609
+ ".c": "text/x-c",
610
+ ".cpp": "text/x-c++",
611
+ ".h": "text/x-c",
612
+ ".hpp": "text/x-c++",
613
+ ".js": "application/javascript",
614
+ ".cjs": "application/javascript",
615
+ ".mjs": "application/javascript",
616
+ ".jsx": "application/javascript",
617
+ ".ts": "application/typescript",
618
+ ".tsx": "application/typescript",
619
+ ".rb": "text/x-ruby",
620
+ ".php": "application/x-php",
621
+ ".go": "text/x-go",
622
+ ".rs": "text/rust",
623
+ ".swift": "text/x-swift",
624
+ ".kt": "text/x-kotlin",
625
+ ".sh": "application/x-shellscript",
626
+ ".bash": "application/x-shellscript",
627
+ ".ps1": "application/x-powershell",
628
+ ".sql": "application/sql",
629
+ ".yml": "application/x-yaml",
630
+ ".coffee": "text/coffeescript",
631
+ ".lua": "text/x-lua",
632
+ },
633
+ archive: {
634
+ ".zip": "application/zip",
635
+ ".tar": "application/x-tar",
636
+ ".gz": "application/gzip",
637
+ ".tgz": "application/gzip",
638
+ ".bz2": "application/x-bzip2",
639
+ ".xz": "application/x-xz",
640
+ ".rar": "application/vnd.rar",
641
+ ".7z": "application/x-7z-compressed",
642
+ ".iso": "application/x-iso9660-image",
643
+ ".dmg": "application/x-apple-diskimage",
644
+ ".jar": "application/java-archive",
645
+ ".war": "application/java-archive",
646
+ ".whl": "application/python-wheel",
647
+ ".egg": "application/python-egg",
648
+ },
649
+ font: {
650
+ ".ttf": "font/ttf",
651
+ ".otf": "font/otf",
652
+ ".woff": "font/woff",
653
+ ".woff2": "font/woff2",
654
+ ".eot": "application/vnd.ms-fontobject",
655
+ },
656
+ executable: {
657
+ ".exe": "application/vnd.microsoft.portable-executable",
658
+ ".dll": "application/vnd.microsoft.portable-executable",
659
+ ".bin": "application/octet-stream",
660
+ ".deb": "application/vnd.debian.binary-package",
661
+ ".rpm": "application/x-rpm",
662
+ },
663
+ };
664
+ /** Mirror of MEDIA_TYPES in Python: category -> Set of extensions */
665
+ const MEDIA_TYPES = Object.fromEntries(Object.entries(MIME_TYPES).map(([cat, mapping]) => [
666
+ cat,
667
+ new Set(Object.keys(mapping)),
668
+ ]));
669
+ /** ---- Helpers ---- */
670
+ function toCategorySet(categories) {
671
+ const allCats = new Set(Object.keys(MIME_TYPES));
672
+ if (!categories) {
673
+ // all categories
674
+ return new Set(allCats);
675
+ }
676
+ const out = new Set();
677
+ for (const c of categories) {
678
+ const key = String(c);
679
+ if (allCats.has(key))
680
+ out.add(key);
681
+ }
682
+ return out.size ? out : new Set(allCats);
683
+ }
684
+ function normalizeCategories(categories, opts) {
685
+ const selected = categories ?? opts?.media_types ?? null;
686
+ return toCategorySet(selected);
687
+ }
688
+ function extOf(input) {
689
+ // Behaves like pathlib.Path(...).suffix.lower(): last extension only; lowercased.
690
+ let ext = path.extname(input || "");
691
+ if (!ext && input && input.startsWith(".")) {
692
+ // user passed ".jpg" directly
693
+ ext = input;
694
+ }
695
+ return (ext || "").toLowerCase();
696
+ }
697
+ function unionExts(categories) {
698
+ const out = new Set();
699
+ for (const c of categories) {
700
+ const set = MEDIA_TYPES[c];
701
+ for (const e of set)
702
+ out.add(e);
703
+ }
704
+ return out;
705
+ }
706
+ /** ---- API (Python parity) ---- */
707
+ /**
708
+ * Return a sub-map of MEDIA_TYPES for the given categories.
709
+ * If categories is falsy, returns all categories.
710
+ */
711
+ function getMediaMap(categories, opts) {
712
+ const cats = normalizeCategories(categories, opts);
713
+ const result = {};
714
+ for (const c of cats)
715
+ result[c] = new Set(MEDIA_TYPES[c]);
716
+ return result;
717
+ }
718
+ /**
719
+ * Return a flat, sorted list of all extensions for the given categories.
720
+ */
721
+ function getMediaExts(categories, opts) {
722
+ const cats = normalizeCategories(categories, opts);
723
+ return Array.from(unionExts(cats)).sort();
724
+ }
725
+ /**
726
+ * Given a file path or extension, return its media category (e.g. "image") or null.
727
+ * Mirrors Python's confirm_type.
728
+ */
729
+ function confirmType(pathOrExt, categories, opts) {
730
+ const cats = normalizeCategories(categories, opts);
731
+ const ext = extOf(pathOrExt);
732
+ // Preserve object insertion order like Python dict iteration
733
+ for (const [category, exts] of Object.entries(MEDIA_TYPES)) {
734
+ if (!cats.has(category))
735
+ continue;
736
+ if (ext && exts.has(ext))
737
+ return category;
738
+ }
739
+ return null;
740
+ }
741
+ /**
742
+ * True if the given file path or extension belongs to one of the categories.
743
+ */
744
+ function isMediaType(pathOrExt, categories, opts) {
745
+ return confirmType(pathOrExt, categories, opts) !== null;
746
+ }
747
+ /**
748
+ * Look up the MIME type by extension; fall back to 'application/octet-stream'.
749
+ */
750
+ function getMimeType(pathOrExt) {
751
+ const ext = extOf(pathOrExt);
752
+ for (const mapping of Object.values(MIME_TYPES)) {
753
+ if (ext && mapping[ext]) {
754
+ return mapping[ext];
755
+ }
756
+ }
757
+ return "application/octet-stream";
758
+ }
759
+ function getAllFileTypesSync(directory, categories, opts) {
760
+ // 🧩 Skip entirely if fs isn't available
761
+ if (!fs || !path)
762
+ return [];
763
+ try {
764
+ const stat = fs.statSync(directory);
765
+ if (!stat.isDirectory())
766
+ return [];
767
+ const cats = normalizeCategories(categories, opts);
768
+ const wanted = unionExts(cats);
769
+ const results = [];
770
+ function walkSync(dir) {
771
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
772
+ for (const ent of entries) {
773
+ const full = path.join(dir, ent.name);
774
+ if (ent.isDirectory()) {
775
+ walkSync(full);
776
+ }
777
+ else if (ent.isFile()) {
778
+ const ext = path.extname(ent.name).toLowerCase();
779
+ if (wanted.has(ext))
780
+ results.push(full);
781
+ }
782
+ }
783
+ }
784
+ walkSync(directory);
785
+ return results;
786
+ }
787
+ catch {
788
+ return [];
789
+ }
790
+ }
791
+ async function getAllFileTypes(directory, categories, opts) {
792
+ // 🧩 Skip entirely if fsp isn't available
793
+ if (!fsp || !path)
794
+ return [];
795
+ try {
796
+ const stat = await fsp.stat(directory);
797
+ if (!stat.isDirectory())
798
+ return [];
799
+ const cats = normalizeCategories(categories, opts);
800
+ const wanted = unionExts(cats);
801
+ const results = [];
802
+ async function walkAsync(dir) {
803
+ const entries = await fsp.readdir(dir, { withFileTypes: true });
804
+ for (const ent of entries) {
805
+ const full = path.join(dir, ent.name);
806
+ if (ent.isDirectory()) {
807
+ await walkAsync(full);
808
+ }
809
+ else if (ent.isFile()) {
810
+ const ext = path.extname(ent.name).toLowerCase();
811
+ if (wanted.has(ext))
812
+ results.push(full);
813
+ }
814
+ }
815
+ }
816
+ await walkAsync(directory);
817
+ return results;
818
+ }
819
+ catch {
820
+ return [];
821
+ }
822
+ }
823
+ /** Optional convenience re-exports that mirror your Python names */
824
+ const get_all_file_types = getAllFileTypes;
825
+ const get_media_map = getMediaMap;
826
+ const get_media_exts = getMediaExts;
827
+ const confirm_type = confirmType;
828
+ const is_media_type = isMediaType;
829
+ const get_mime_type = getMimeType;
830
+
831
+ /**
832
+ * Safely walk `globalThis` (or window/document) by a chain of property names.
833
+ * Returns `undefined` if any step is missing.
834
+ */
835
+ function safeGlobalProp(...path) {
836
+ let obj = globalThis;
837
+ for (const key of path) {
838
+ if (obj == null || typeof obj !== "object" || !(key in obj)) {
839
+ return undefined;
840
+ }
841
+ obj = obj[key];
842
+ }
843
+ return obj;
844
+ }
845
+
846
+ /**
847
+ * Returns `window` if running in a browser, otherwise `undefined`.
848
+ */
849
+ function getSafeLocalStorage() {
850
+ if (typeof window === 'undefined')
851
+ return undefined;
852
+ try {
853
+ return window.localStorage;
854
+ }
855
+ catch {
856
+ return undefined; // e.g. Safari private-mode block
857
+ }
858
+ }
859
+ /**
860
+ * Call a Storage method by name, silencing any errors or missing storage.
861
+ *
862
+ * @param method One of the keys of the Storage interface: "getItem", "setItem", etc.
863
+ * @param args The arguments you’d normally pass to that method.
864
+ * @returns The method’s return value, or undefined if storage/method isn’t available.
865
+ */
866
+ function callStorage(method, ...args) {
867
+ const storage = getSafeLocalStorage();
868
+ if (!storage)
869
+ return undefined;
870
+ const fn = storage[method];
871
+ if (typeof fn !== 'function')
872
+ return undefined;
873
+ try {
874
+ return fn.apply(storage, args);
875
+ }
876
+ catch {
877
+ return undefined;
878
+ }
879
+ }
880
+ /**
881
+ * Safely call storage methods (`localStorage` or `sessionStorage`) without blowing up.
882
+ * Returns `undefined` on any error.
883
+ */
884
+ function safeStorage(storageName, method, ...args) {
885
+ try {
886
+ const store = safeGlobalProp(storageName);
887
+ if (!store || typeof store[method] !== "function")
888
+ return undefined;
889
+ return store[method](...args);
890
+ }
891
+ catch {
892
+ return undefined;
893
+ }
894
+ }
895
+
896
+ export { MIME_TYPES as M, MEDIA_TYPES as a, getMediaExts as b, confirmType as c, decodeJwt as d, isMediaType as e, getMimeType as f, getMediaMap as g, getAllFileTypesSync as h, isTokenExpired as i, getAllFileTypes as j, get_all_file_types as k, get_media_map as l, get_media_exts as m, confirm_type as n, is_media_type as o, get_mime_type as p, getSafeLocalStorage as q, callStorage as r, safeStorage as s, safeGlobalProp as t, pathBrowserify as u };
897
+ //# sourceMappingURL=safe_storage-D4QfcTw0.js.map