taglib-wasm 0.3.3 → 0.3.9

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 (68) hide show
  1. package/CONTRIBUTING.md +293 -0
  2. package/NOTICE +34 -0
  3. package/README.md +122 -511
  4. package/dist/index.d.ts +132 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +137 -0
  7. package/dist/index.ts +220 -0
  8. package/dist/src/constants.d.ts +201 -0
  9. package/dist/src/constants.d.ts.map +1 -0
  10. package/dist/src/constants.ts +227 -0
  11. package/dist/src/errors.d.ts +89 -0
  12. package/dist/src/errors.d.ts.map +1 -0
  13. package/dist/src/errors.ts +237 -0
  14. package/dist/src/file-utils.d.ts +205 -0
  15. package/dist/src/file-utils.d.ts.map +1 -0
  16. package/dist/src/file-utils.ts +467 -0
  17. package/dist/src/file.js +47 -0
  18. package/dist/src/global.d.ts +10 -0
  19. package/dist/src/mod.d.ts +9 -0
  20. package/dist/src/mod.d.ts.map +1 -0
  21. package/dist/src/mod.ts +19 -0
  22. package/dist/src/simple.d.ts +347 -0
  23. package/dist/src/simple.d.ts.map +1 -0
  24. package/dist/src/simple.ts +659 -0
  25. package/dist/src/taglib.d.ts +502 -0
  26. package/dist/src/taglib.d.ts.map +1 -0
  27. package/dist/src/taglib.ts +959 -0
  28. package/dist/src/types.d.ts +323 -0
  29. package/dist/src/types.d.ts.map +1 -0
  30. package/dist/src/types.ts +538 -0
  31. package/dist/src/utils/file.d.ts +15 -0
  32. package/dist/src/utils/file.d.ts.map +1 -0
  33. package/dist/src/utils/file.ts +82 -0
  34. package/dist/src/utils/write.d.ts +15 -0
  35. package/dist/src/utils/write.d.ts.map +1 -0
  36. package/dist/src/utils/write.ts +61 -0
  37. package/dist/src/wasm-workers.d.ts +33 -0
  38. package/dist/src/wasm-workers.d.ts.map +1 -0
  39. package/dist/src/wasm-workers.ts +176 -0
  40. package/dist/src/wasm.d.ts +97 -0
  41. package/dist/src/wasm.d.ts.map +1 -0
  42. package/dist/src/wasm.ts +133 -0
  43. package/dist/src/web-utils.d.ts +180 -0
  44. package/dist/src/web-utils.d.ts.map +1 -0
  45. package/dist/src/web-utils.ts +347 -0
  46. package/dist/src/workers.d.ts +219 -0
  47. package/dist/src/workers.d.ts.map +1 -0
  48. package/dist/src/workers.ts +465 -0
  49. package/dist/src/write.js +33 -0
  50. package/dist/taglib-wrapper.d.ts +5 -0
  51. package/dist/taglib-wrapper.js +14 -0
  52. package/dist/taglib.wasm +0 -0
  53. package/index.ts +100 -7
  54. package/package.json +40 -16
  55. package/src/errors.ts +237 -0
  56. package/src/file-utils.ts +467 -0
  57. package/src/global.d.ts +10 -0
  58. package/src/simple.ts +399 -84
  59. package/src/taglib.ts +522 -28
  60. package/src/types.ts +1 -1
  61. package/src/utils/file.ts +82 -0
  62. package/src/utils/write.ts +61 -0
  63. package/src/wasm-workers.ts +13 -4
  64. package/src/wasm.ts +1 -1
  65. package/src/web-utils.ts +347 -0
  66. package/src/workers.ts +32 -13
  67. package/build/taglib.js +0 -2407
  68. package/build/taglib.wasm +0 -0
@@ -0,0 +1,227 @@
1
+ /**
2
+ * Standard tag property names used by TagLib.
3
+ * These constants provide type-safe access to tag properties with IDE autocomplete.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * import { Tags } from 'taglib-wasm';
8
+ *
9
+ * // Read tags
10
+ * const title = file.tag.properties.get(Tags.Title);
11
+ * const artist = file.tag.properties.get(Tags.Artist);
12
+ *
13
+ * // Write tags
14
+ * file.tag.properties.set(Tags.Album, "Dark Side of the Moon");
15
+ * file.tag.properties.set(Tags.Year, "1973");
16
+ * ```
17
+ */
18
+ export const Tags = {
19
+ // Basic Properties
20
+ /** Track/song title */
21
+ Title: "TITLE",
22
+ /** Primary performer(s) */
23
+ Artist: "ARTIST",
24
+ /** Album/collection name */
25
+ Album: "ALBUM",
26
+ /** Date of recording (year) */
27
+ Date: "DATE",
28
+ /** Track number on album */
29
+ TrackNumber: "TRACKNUMBER",
30
+ /** Musical genre */
31
+ Genre: "GENRE",
32
+ /** Comments/notes */
33
+ Comment: "COMMENT",
34
+
35
+ // Extended Properties
36
+ /** Band/orchestra/ensemble */
37
+ AlbumArtist: "ALBUMARTIST",
38
+ /** Original composer(s) */
39
+ Composer: "COMPOSER",
40
+ /** Copyright information */
41
+ Copyright: "COPYRIGHT",
42
+ /** Encoding software/person */
43
+ EncodedBy: "ENCODEDBY",
44
+ /** Disc number for multi-disc sets */
45
+ DiscNumber: "DISCNUMBER",
46
+ /** Beats per minute */
47
+ Bpm: "BPM",
48
+ /** Lyrics/text writer(s) */
49
+ Lyricist: "LYRICIST",
50
+ /** Conductor */
51
+ Conductor: "CONDUCTOR",
52
+ /** Person who remixed */
53
+ Remixer: "REMIXEDBY",
54
+ /** Language of vocals/lyrics */
55
+ Language: "LANGUAGE",
56
+ /** Publisher */
57
+ Publisher: "PUBLISHER",
58
+ /** Mood/atmosphere */
59
+ Mood: "MOOD",
60
+ /** Media type (CD, vinyl, etc.) */
61
+ Media: "MEDIA",
62
+ /** Radio station owner */
63
+ RadioStationOwner: "RADIOSTATIONOWNER",
64
+ /** Producer */
65
+ Producer: "PRODUCER",
66
+ /** Album subtitle */
67
+ Subtitle: "SUBTITLE",
68
+ /** Release label */
69
+ Label: "LABEL",
70
+
71
+ // Sorting Properties
72
+ /** Sort name for title */
73
+ TitleSort: "TITLESORT",
74
+ /** Sort name for artist */
75
+ ArtistSort: "ARTISTSORT",
76
+ /** Sort name for album artist */
77
+ AlbumArtistSort: "ALBUMARTISTSORT",
78
+ /** Sort name for album */
79
+ AlbumSort: "ALBUMSORT",
80
+ /** Sort name for composer */
81
+ ComposerSort: "COMPOSERSORT",
82
+
83
+ // Identifiers
84
+ /** International Standard Recording Code */
85
+ Isrc: "ISRC",
86
+ /** Amazon Standard Identification Number */
87
+ Asin: "ASIN",
88
+ /** Catalog number */
89
+ CatalogNumber: "CATALOGNUMBER",
90
+ /** Barcode (EAN/UPC) */
91
+ Barcode: "BARCODE",
92
+
93
+ // MusicBrainz Identifiers
94
+ /** MusicBrainz Artist ID */
95
+ MusicBrainzArtistId: "MUSICBRAINZ_ARTISTID",
96
+ /** MusicBrainz Release Artist ID */
97
+ MusicBrainzReleaseArtistId: "MUSICBRAINZ_ALBUMARTISTID",
98
+ /** MusicBrainz Work ID */
99
+ MusicBrainzWorkId: "MUSICBRAINZ_WORKID",
100
+ /** MusicBrainz Release ID */
101
+ MusicBrainzReleaseId: "MUSICBRAINZ_ALBUMID",
102
+ /** MusicBrainz Recording ID */
103
+ MusicBrainzRecordingId: "MUSICBRAINZ_TRACKID",
104
+ /** MusicBrainz Track ID (deprecated, use RecordingId) */
105
+ MusicBrainzTrackId: "MUSICBRAINZ_TRACKID",
106
+ /** MusicBrainz Release Group ID */
107
+ MusicBrainzReleaseGroupId: "MUSICBRAINZ_RELEASEGROUPID",
108
+ /** MusicBrainz Release Track ID */
109
+ MusicBrainzReleaseTrackId: "MUSICBRAINZ_RELEASETRACKID",
110
+
111
+ // Podcast Properties
112
+ /** Podcast identifier */
113
+ PodcastId: "PODCASTID",
114
+ /** Podcast URL */
115
+ PodcastUrl: "PODCASTURL",
116
+
117
+ // Grouping and Work
118
+ /** Content group/work */
119
+ Grouping: "GROUPING",
120
+ /** Work name */
121
+ Work: "WORK",
122
+
123
+ // Additional Metadata
124
+ /** Lyrics content */
125
+ Lyrics: "LYRICS",
126
+ /** Album gain (ReplayGain) */
127
+ AlbumGain: "REPLAYGAIN_ALBUM_GAIN",
128
+ /** Album peak (ReplayGain) */
129
+ AlbumPeak: "REPLAYGAIN_ALBUM_PEAK",
130
+ /** Track gain (ReplayGain) */
131
+ TrackGain: "REPLAYGAIN_TRACK_GAIN",
132
+ /** Track peak (ReplayGain) */
133
+ TrackPeak: "REPLAYGAIN_TRACK_PEAK",
134
+
135
+ // Special handling
136
+ /** Original artist for covers */
137
+ OriginalArtist: "ORIGINALARTIST",
138
+ /** Original album */
139
+ OriginalAlbum: "ORIGINALALBUM",
140
+ /** Original release date */
141
+ OriginalDate: "ORIGINALDATE",
142
+ /** Script/writing system */
143
+ Script: "SCRIPT",
144
+ /** Involved people list */
145
+ InvolvedPeopleList: "INVOLVEDPEOPLELIST",
146
+
147
+ // Technical Properties
148
+ /** Encoder settings/software */
149
+ EncoderSettings: "ENCODERSETTINGS",
150
+ /** Source media */
151
+ SourceMedia: "SOURCEMEDIA",
152
+ } as const;
153
+
154
+ /**
155
+ * Type representing all valid tag property names
156
+ */
157
+ export type TagName = typeof Tags[keyof typeof Tags];
158
+
159
+ /**
160
+ * Type guard to check if a string is a valid tag name
161
+ */
162
+ export function isValidTagName(name: string): name is TagName {
163
+ return Object.values(Tags).includes(name as TagName);
164
+ }
165
+
166
+ /**
167
+ * Get all available tag names as an array
168
+ */
169
+ export function getAllTagNames(): readonly TagName[] {
170
+ return Object.values(Tags);
171
+ }
172
+
173
+ /**
174
+ * Format-specific tag mappings (for reference only - TagLib handles these automatically)
175
+ * This shows how standard property names map to format-specific identifiers.
176
+ */
177
+ export const FormatMappings = {
178
+ Title: {
179
+ id3v2: "TIT2",
180
+ mp4: "©nam",
181
+ vorbis: "TITLE",
182
+ ape: "Title",
183
+ riff: "INAM",
184
+ },
185
+ Artist: {
186
+ id3v2: "TPE1",
187
+ mp4: "©ART",
188
+ vorbis: "ARTIST",
189
+ ape: "Artist",
190
+ riff: "IART",
191
+ },
192
+ Album: {
193
+ id3v2: "TALB",
194
+ mp4: "©alb",
195
+ vorbis: "ALBUM",
196
+ ape: "Album",
197
+ riff: "IPRD",
198
+ },
199
+ Date: {
200
+ id3v2: "TDRC",
201
+ mp4: "©day",
202
+ vorbis: "DATE",
203
+ ape: "Year",
204
+ riff: "ICRD",
205
+ },
206
+ Genre: {
207
+ id3v2: "TCON",
208
+ mp4: "©gen",
209
+ vorbis: "GENRE",
210
+ ape: "Genre",
211
+ riff: "IGNR",
212
+ },
213
+ Comment: {
214
+ id3v2: "COMM",
215
+ mp4: "©cmt",
216
+ vorbis: "COMMENT",
217
+ ape: "Comment",
218
+ riff: "ICMT",
219
+ },
220
+ TrackNumber: {
221
+ id3v2: "TRCK",
222
+ mp4: "trkn",
223
+ vorbis: "TRACKNUMBER",
224
+ ape: "Track",
225
+ riff: "ITRK",
226
+ },
227
+ } as const;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Custom error types for taglib-wasm with enhanced context and debugging information
3
+ */
4
+ /**
5
+ * List of audio formats supported by taglib-wasm
6
+ */
7
+ export declare const SUPPORTED_FORMATS: readonly ["MP3", "MP4", "M4A", "FLAC", "OGG", "WAV"];
8
+ /**
9
+ * Error codes for programmatic error handling
10
+ */
11
+ export declare enum TagLibErrorCode {
12
+ INITIALIZATION_FAILED = "INITIALIZATION_FAILED",
13
+ INVALID_FORMAT = "INVALID_FORMAT",
14
+ UNSUPPORTED_FORMAT = "UNSUPPORTED_FORMAT",
15
+ FILE_OPERATION_FAILED = "FILE_OPERATION_FAILED",
16
+ METADATA_ERROR = "METADATA_ERROR",
17
+ MEMORY_ERROR = "MEMORY_ERROR",
18
+ ENVIRONMENT_ERROR = "ENVIRONMENT_ERROR"
19
+ }
20
+ /**
21
+ * Base error class for all taglib-wasm errors
22
+ */
23
+ export declare class TagLibError extends Error {
24
+ readonly code: TagLibErrorCode;
25
+ readonly context?: Record<string, unknown> | undefined;
26
+ constructor(message: string, code: TagLibErrorCode, context?: Record<string, unknown> | undefined);
27
+ }
28
+ /**
29
+ * Error thrown when the Wasm module fails to initialize
30
+ */
31
+ export declare class TagLibInitializationError extends TagLibError {
32
+ constructor(message: string, context?: Record<string, unknown>);
33
+ }
34
+ /**
35
+ * Error thrown when an audio file format is invalid or corrupted
36
+ */
37
+ export declare class InvalidFormatError extends TagLibError {
38
+ readonly bufferSize?: number | undefined;
39
+ constructor(message: string, bufferSize?: number | undefined, context?: Record<string, unknown>);
40
+ }
41
+ /**
42
+ * Error thrown when an audio format is recognized but not supported
43
+ */
44
+ export declare class UnsupportedFormatError extends TagLibError {
45
+ readonly format: string;
46
+ readonly supportedFormats: readonly string[];
47
+ constructor(format: string, supportedFormats?: readonly string[], context?: Record<string, unknown>);
48
+ }
49
+ /**
50
+ * Error thrown during file operations (read, write, save)
51
+ */
52
+ export declare class FileOperationError extends TagLibError {
53
+ readonly operation: 'read' | 'write' | 'save';
54
+ readonly path?: string | undefined;
55
+ constructor(operation: 'read' | 'write' | 'save', message: string, path?: string | undefined, context?: Record<string, unknown>);
56
+ }
57
+ /**
58
+ * Error thrown when metadata operations fail
59
+ */
60
+ export declare class MetadataError extends TagLibError {
61
+ readonly operation: 'read' | 'write';
62
+ readonly field?: string | undefined;
63
+ constructor(operation: 'read' | 'write', message: string, field?: string | undefined, context?: Record<string, unknown>);
64
+ }
65
+ /**
66
+ * Error thrown when Wasm memory operations fail
67
+ */
68
+ export declare class MemoryError extends TagLibError {
69
+ constructor(message: string, context?: Record<string, unknown>);
70
+ }
71
+ /**
72
+ * Error thrown when the environment doesn't support required features
73
+ */
74
+ export declare class EnvironmentError extends TagLibError {
75
+ readonly environment: string;
76
+ readonly requiredFeature?: string | undefined;
77
+ constructor(environment: string, message: string, requiredFeature?: string | undefined, context?: Record<string, unknown>);
78
+ }
79
+ /**
80
+ * Type guards for error handling
81
+ */
82
+ export declare function isTagLibError(error: unknown): error is TagLibError;
83
+ export declare function isInvalidFormatError(error: unknown): error is InvalidFormatError;
84
+ export declare function isUnsupportedFormatError(error: unknown): error is UnsupportedFormatError;
85
+ export declare function isFileOperationError(error: unknown): error is FileOperationError;
86
+ export declare function isMetadataError(error: unknown): error is MetadataError;
87
+ export declare function isMemoryError(error: unknown): error is MemoryError;
88
+ export declare function isEnvironmentError(error: unknown): error is EnvironmentError;
89
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,eAAO,MAAM,iBAAiB,sDAAuD,CAAC;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I,WAAW,CAElE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAE5E"}
@@ -0,0 +1,237 @@
1
+ /**
2
+ * Custom error types for taglib-wasm with enhanced context and debugging information
3
+ */
4
+
5
+ /**
6
+ * List of audio formats supported by taglib-wasm
7
+ */
8
+ export const SUPPORTED_FORMATS = ['MP3', 'MP4', 'M4A', 'FLAC', 'OGG', 'WAV'] as const;
9
+
10
+ /**
11
+ * Error codes for programmatic error handling
12
+ */
13
+ export enum TagLibErrorCode {
14
+ INITIALIZATION_FAILED = 'INITIALIZATION_FAILED',
15
+ INVALID_FORMAT = 'INVALID_FORMAT',
16
+ UNSUPPORTED_FORMAT = 'UNSUPPORTED_FORMAT',
17
+ FILE_OPERATION_FAILED = 'FILE_OPERATION_FAILED',
18
+ METADATA_ERROR = 'METADATA_ERROR',
19
+ MEMORY_ERROR = 'MEMORY_ERROR',
20
+ ENVIRONMENT_ERROR = 'ENVIRONMENT_ERROR',
21
+ }
22
+
23
+ /**
24
+ * Base error class for all taglib-wasm errors
25
+ */
26
+ export class TagLibError extends Error {
27
+ constructor(
28
+ message: string,
29
+ public readonly code: TagLibErrorCode,
30
+ public readonly context?: Record<string, unknown>
31
+ ) {
32
+ super(message);
33
+ this.name = 'TagLibError';
34
+ Object.setPrototypeOf(this, TagLibError.prototype);
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Error thrown when the Wasm module fails to initialize
40
+ */
41
+ export class TagLibInitializationError extends TagLibError {
42
+ constructor(message: string, context?: Record<string, unknown>) {
43
+ super(
44
+ createErrorMessage('Failed to initialize TagLib Wasm module', message),
45
+ TagLibErrorCode.INITIALIZATION_FAILED,
46
+ context
47
+ );
48
+ this.name = 'TagLibInitializationError';
49
+ Object.setPrototypeOf(this, TagLibInitializationError.prototype);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Error thrown when an audio file format is invalid or corrupted
55
+ */
56
+ export class InvalidFormatError extends TagLibError {
57
+ constructor(
58
+ message: string,
59
+ public readonly bufferSize?: number,
60
+ context?: Record<string, unknown>
61
+ ) {
62
+ const details = [`Invalid audio file format: ${message}`];
63
+
64
+ if (bufferSize !== undefined) {
65
+ details.push(`Buffer size: ${formatFileSize(bufferSize)}`);
66
+ if (bufferSize < 1024) {
67
+ details.push('Audio files must be at least 1KB to contain valid headers.');
68
+ }
69
+ }
70
+
71
+ super(
72
+ details.join('. '),
73
+ TagLibErrorCode.INVALID_FORMAT,
74
+ { ...context, bufferSize }
75
+ );
76
+ this.name = 'InvalidFormatError';
77
+ Object.setPrototypeOf(this, InvalidFormatError.prototype);
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Error thrown when an audio format is recognized but not supported
83
+ */
84
+ export class UnsupportedFormatError extends TagLibError {
85
+ constructor(
86
+ public readonly format: string,
87
+ public readonly supportedFormats: readonly string[] = SUPPORTED_FORMATS,
88
+ context?: Record<string, unknown>
89
+ ) {
90
+ super(
91
+ `Unsupported audio format: ${format}. Supported formats: ${supportedFormats.join(', ')}`,
92
+ TagLibErrorCode.UNSUPPORTED_FORMAT,
93
+ { ...context, format, supportedFormats }
94
+ );
95
+ this.name = 'UnsupportedFormatError';
96
+ Object.setPrototypeOf(this, UnsupportedFormatError.prototype);
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Error thrown during file operations (read, write, save)
102
+ */
103
+ export class FileOperationError extends TagLibError {
104
+ constructor(
105
+ public readonly operation: 'read' | 'write' | 'save',
106
+ message: string,
107
+ public readonly path?: string,
108
+ context?: Record<string, unknown>
109
+ ) {
110
+ const details = [`Failed to ${operation} file`];
111
+ if (path) {
112
+ details.push(`Path: ${path}`);
113
+ }
114
+ details.push(message);
115
+
116
+ super(
117
+ details.join('. '),
118
+ TagLibErrorCode.FILE_OPERATION_FAILED,
119
+ { ...context, operation, path }
120
+ );
121
+ this.name = 'FileOperationError';
122
+ Object.setPrototypeOf(this, FileOperationError.prototype);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Error thrown when metadata operations fail
128
+ */
129
+ export class MetadataError extends TagLibError {
130
+ constructor(
131
+ public readonly operation: 'read' | 'write',
132
+ message: string,
133
+ public readonly field?: string,
134
+ context?: Record<string, unknown>
135
+ ) {
136
+ const details = [`Failed to ${operation} metadata`];
137
+ if (field) {
138
+ details.push(`Field: ${field}`);
139
+ }
140
+ details.push(message);
141
+
142
+ super(
143
+ details.join('. '),
144
+ TagLibErrorCode.METADATA_ERROR,
145
+ { ...context, operation, field }
146
+ );
147
+ this.name = 'MetadataError';
148
+ Object.setPrototypeOf(this, MetadataError.prototype);
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Error thrown when Wasm memory operations fail
154
+ */
155
+ export class MemoryError extends TagLibError {
156
+ constructor(message: string, context?: Record<string, unknown>) {
157
+ super(
158
+ createErrorMessage('Memory allocation failed', message),
159
+ TagLibErrorCode.MEMORY_ERROR,
160
+ context
161
+ );
162
+ this.name = 'MemoryError';
163
+ Object.setPrototypeOf(this, MemoryError.prototype);
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Error thrown when the environment doesn't support required features
169
+ */
170
+ export class EnvironmentError extends TagLibError {
171
+ constructor(
172
+ public readonly environment: string,
173
+ message: string,
174
+ public readonly requiredFeature?: string,
175
+ context?: Record<string, unknown>
176
+ ) {
177
+ const details = [`Environment '${environment}' ${message}`];
178
+ if (requiredFeature) {
179
+ details.push(`Required feature: ${requiredFeature}`);
180
+ }
181
+
182
+ super(
183
+ details.join('. '),
184
+ TagLibErrorCode.ENVIRONMENT_ERROR,
185
+ { ...context, environment, requiredFeature }
186
+ );
187
+ this.name = 'EnvironmentError';
188
+ Object.setPrototypeOf(this, EnvironmentError.prototype);
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Helper function to create consistent error messages
194
+ */
195
+ function createErrorMessage(prefix: string, details: string): string {
196
+ return `${prefix}: ${details}`;
197
+ }
198
+
199
+ /**
200
+ * Format file size in human-readable format
201
+ */
202
+ function formatFileSize(bytes: number): string {
203
+ if (bytes < 1024) return `${bytes} bytes`;
204
+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
205
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
206
+ }
207
+
208
+ /**
209
+ * Type guards for error handling
210
+ */
211
+ export function isTagLibError(error: unknown): error is TagLibError {
212
+ return error instanceof TagLibError;
213
+ }
214
+
215
+ export function isInvalidFormatError(error: unknown): error is InvalidFormatError {
216
+ return error instanceof InvalidFormatError;
217
+ }
218
+
219
+ export function isUnsupportedFormatError(error: unknown): error is UnsupportedFormatError {
220
+ return error instanceof UnsupportedFormatError;
221
+ }
222
+
223
+ export function isFileOperationError(error: unknown): error is FileOperationError {
224
+ return error instanceof FileOperationError;
225
+ }
226
+
227
+ export function isMetadataError(error: unknown): error is MetadataError {
228
+ return error instanceof MetadataError;
229
+ }
230
+
231
+ export function isMemoryError(error: unknown): error is MemoryError {
232
+ return error instanceof MemoryError;
233
+ }
234
+
235
+ export function isEnvironmentError(error: unknown): error is EnvironmentError {
236
+ return error instanceof EnvironmentError;
237
+ }