taglib-wasm 0.3.1 → 0.3.3
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 +190 -97
- package/build/taglib.js +2401 -8
- package/index.ts +96 -4
- package/package.json +4 -1
- package/src/constants.ts +227 -0
- package/src/mod.ts +6 -6
- package/src/simple.ts +105 -70
- package/src/taglib.ts +246 -70
- package/src/types.ts +166 -17
- package/src/wasm-workers.ts +8 -8
- package/src/wasm.ts +21 -12
- package/src/workers.ts +111 -31
package/src/workers.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Cloudflare Workers-specific TagLib API
|
|
3
|
+
*
|
|
4
|
+
* This module provides a specialized API for using TagLib in Cloudflare Workers
|
|
5
|
+
* and other edge computing environments where the standard Emscripten module
|
|
6
|
+
* loading may not work. It uses C-style function exports for compatibility.
|
|
7
|
+
*
|
|
8
|
+
* @module taglib-wasm/workers
|
|
3
9
|
*/
|
|
4
10
|
|
|
5
11
|
import type {
|
|
@@ -17,7 +23,24 @@ import {
|
|
|
17
23
|
} from "./wasm-workers.ts";
|
|
18
24
|
|
|
19
25
|
/**
|
|
20
|
-
* Represents an audio file with metadata and properties (Workers-compatible)
|
|
26
|
+
* Represents an audio file with metadata and properties (Workers-compatible).
|
|
27
|
+
* This implementation uses C-style function calls for Cloudflare Workers compatibility.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const file = taglib.openFile(audioBuffer);
|
|
32
|
+
*
|
|
33
|
+
* // Get metadata
|
|
34
|
+
* const tag = file.tag();
|
|
35
|
+
* console.log(tag.title);
|
|
36
|
+
*
|
|
37
|
+
* // Modify metadata
|
|
38
|
+
* file.setTitle("New Title");
|
|
39
|
+
* file.save();
|
|
40
|
+
*
|
|
41
|
+
* // Clean up
|
|
42
|
+
* file.dispose();
|
|
43
|
+
* ```
|
|
21
44
|
*/
|
|
22
45
|
export class AudioFileWorkers {
|
|
23
46
|
private module: TagLibModule;
|
|
@@ -33,14 +56,16 @@ export class AudioFileWorkers {
|
|
|
33
56
|
}
|
|
34
57
|
|
|
35
58
|
/**
|
|
36
|
-
* Check if the file is valid and was loaded successfully
|
|
59
|
+
* Check if the file is valid and was loaded successfully.
|
|
60
|
+
* @returns true if the file is valid and can be processed
|
|
37
61
|
*/
|
|
38
62
|
isValid(): boolean {
|
|
39
63
|
return this.module._taglib_file_is_valid?.(this.fileId) !== 0;
|
|
40
64
|
}
|
|
41
65
|
|
|
42
66
|
/**
|
|
43
|
-
* Get the file format
|
|
67
|
+
* Get the file format.
|
|
68
|
+
* @returns Audio format (e.g., "MP3", "FLAC", "OGG")
|
|
44
69
|
*/
|
|
45
70
|
format(): AudioFormat {
|
|
46
71
|
const formatPtr = this.module._taglib_file_format?.(this.fileId) || 0;
|
|
@@ -50,7 +75,8 @@ export class AudioFileWorkers {
|
|
|
50
75
|
}
|
|
51
76
|
|
|
52
77
|
/**
|
|
53
|
-
* Get basic tag information
|
|
78
|
+
* Get basic tag information.
|
|
79
|
+
* @returns Object containing title, artist, album, etc.
|
|
54
80
|
*/
|
|
55
81
|
tag(): Tag {
|
|
56
82
|
if (this.tagPtr === 0) return {};
|
|
@@ -75,13 +101,16 @@ export class AudioFileWorkers {
|
|
|
75
101
|
}
|
|
76
102
|
|
|
77
103
|
/**
|
|
78
|
-
* Get audio properties (duration, bitrate, etc.)
|
|
104
|
+
* Get audio properties (duration, bitrate, etc.).
|
|
105
|
+
* @returns Audio properties or null if unavailable
|
|
79
106
|
*/
|
|
80
107
|
audioProperties(): AudioProperties | null {
|
|
81
108
|
if (this.propsPtr === 0) return null;
|
|
82
109
|
|
|
83
|
-
const length =
|
|
84
|
-
|
|
110
|
+
const length =
|
|
111
|
+
this.module._taglib_audioproperties_length?.(this.propsPtr) || 0;
|
|
112
|
+
const bitrate =
|
|
113
|
+
this.module._taglib_audioproperties_bitrate?.(this.propsPtr) || 0;
|
|
85
114
|
const sampleRate = this.module._taglib_audioproperties_samplerate?.(
|
|
86
115
|
this.propsPtr,
|
|
87
116
|
) || 0;
|
|
@@ -99,7 +128,8 @@ export class AudioFileWorkers {
|
|
|
99
128
|
}
|
|
100
129
|
|
|
101
130
|
/**
|
|
102
|
-
* Set the title tag
|
|
131
|
+
* Set the title tag.
|
|
132
|
+
* @param title - New title value
|
|
103
133
|
*/
|
|
104
134
|
setTitle(title: string): void {
|
|
105
135
|
if (this.tagPtr === 0) return;
|
|
@@ -109,7 +139,8 @@ export class AudioFileWorkers {
|
|
|
109
139
|
}
|
|
110
140
|
|
|
111
141
|
/**
|
|
112
|
-
* Set the artist tag
|
|
142
|
+
* Set the artist tag.
|
|
143
|
+
* @param artist - New artist value
|
|
113
144
|
*/
|
|
114
145
|
setArtist(artist: string): void {
|
|
115
146
|
if (this.tagPtr === 0) return;
|
|
@@ -119,7 +150,8 @@ export class AudioFileWorkers {
|
|
|
119
150
|
}
|
|
120
151
|
|
|
121
152
|
/**
|
|
122
|
-
* Set the album tag
|
|
153
|
+
* Set the album tag.
|
|
154
|
+
* @param album - New album value
|
|
123
155
|
*/
|
|
124
156
|
setAlbum(album: string): void {
|
|
125
157
|
if (this.tagPtr === 0) return;
|
|
@@ -129,7 +161,8 @@ export class AudioFileWorkers {
|
|
|
129
161
|
}
|
|
130
162
|
|
|
131
163
|
/**
|
|
132
|
-
* Set the comment tag
|
|
164
|
+
* Set the comment tag.
|
|
165
|
+
* @param comment - New comment value
|
|
133
166
|
*/
|
|
134
167
|
setComment(comment: string): void {
|
|
135
168
|
if (this.tagPtr === 0) return;
|
|
@@ -139,7 +172,8 @@ export class AudioFileWorkers {
|
|
|
139
172
|
}
|
|
140
173
|
|
|
141
174
|
/**
|
|
142
|
-
* Set the genre tag
|
|
175
|
+
* Set the genre tag.
|
|
176
|
+
* @param genre - New genre value
|
|
143
177
|
*/
|
|
144
178
|
setGenre(genre: string): void {
|
|
145
179
|
if (this.tagPtr === 0) return;
|
|
@@ -149,7 +183,8 @@ export class AudioFileWorkers {
|
|
|
149
183
|
}
|
|
150
184
|
|
|
151
185
|
/**
|
|
152
|
-
* Set the year tag
|
|
186
|
+
* Set the year tag.
|
|
187
|
+
* @param year - Release year
|
|
153
188
|
*/
|
|
154
189
|
setYear(year: number): void {
|
|
155
190
|
if (this.tagPtr === 0) return;
|
|
@@ -157,7 +192,8 @@ export class AudioFileWorkers {
|
|
|
157
192
|
}
|
|
158
193
|
|
|
159
194
|
/**
|
|
160
|
-
* Set the track number tag
|
|
195
|
+
* Set the track number tag.
|
|
196
|
+
* @param track - Track number
|
|
161
197
|
*/
|
|
162
198
|
setTrack(track: number): void {
|
|
163
199
|
if (this.tagPtr === 0) return;
|
|
@@ -165,8 +201,9 @@ export class AudioFileWorkers {
|
|
|
165
201
|
}
|
|
166
202
|
|
|
167
203
|
/**
|
|
168
|
-
* Save changes to the file
|
|
169
|
-
* Note: In Workers context, this saves to the in-memory buffer only
|
|
204
|
+
* Save changes to the file.
|
|
205
|
+
* Note: In Workers context, this saves to the in-memory buffer only.
|
|
206
|
+
* @returns true if save was successful
|
|
170
207
|
*/
|
|
171
208
|
save(): boolean {
|
|
172
209
|
if (this.fileId !== 0) {
|
|
@@ -176,16 +213,22 @@ export class AudioFileWorkers {
|
|
|
176
213
|
}
|
|
177
214
|
|
|
178
215
|
/**
|
|
179
|
-
* Get the current file buffer after modifications
|
|
180
|
-
* Note: This is not implemented in the Workers API
|
|
216
|
+
* Get the current file buffer after modifications.
|
|
217
|
+
* Note: This is not implemented in the Workers API.
|
|
218
|
+
* @returns Empty Uint8Array (not implemented)
|
|
219
|
+
* @deprecated Use the Core API for this functionality
|
|
181
220
|
*/
|
|
182
221
|
getFileBuffer(): Uint8Array {
|
|
183
|
-
console.warn(
|
|
222
|
+
console.warn(
|
|
223
|
+
"getFileBuffer() is not implemented in Workers API. Use Core API for this functionality.",
|
|
224
|
+
);
|
|
184
225
|
return new Uint8Array(0);
|
|
185
226
|
}
|
|
186
227
|
|
|
187
228
|
/**
|
|
188
|
-
* Get extended metadata with format-agnostic field names
|
|
229
|
+
* Get extended metadata with format-agnostic field names.
|
|
230
|
+
* Note: Currently returns only basic fields in Workers API.
|
|
231
|
+
* @returns Extended tag object with basic fields populated
|
|
189
232
|
*/
|
|
190
233
|
extendedTag(): ExtendedTag {
|
|
191
234
|
const basicTag = this.tag();
|
|
@@ -218,7 +261,9 @@ export class AudioFileWorkers {
|
|
|
218
261
|
}
|
|
219
262
|
|
|
220
263
|
/**
|
|
221
|
-
* Set extended metadata using format-agnostic field names
|
|
264
|
+
* Set extended metadata using format-agnostic field names.
|
|
265
|
+
* Note: Currently only supports basic fields in Workers API.
|
|
266
|
+
* @param tag - Partial extended tag object with fields to update
|
|
222
267
|
*/
|
|
223
268
|
setExtendedTag(tag: Partial<ExtendedTag>): void {
|
|
224
269
|
if (tag.title !== undefined) this.setTitle(tag.title);
|
|
@@ -231,7 +276,8 @@ export class AudioFileWorkers {
|
|
|
231
276
|
}
|
|
232
277
|
|
|
233
278
|
/**
|
|
234
|
-
* Clean up resources
|
|
279
|
+
* Clean up resources.
|
|
280
|
+
* Always call this when done to prevent memory leaks.
|
|
235
281
|
*/
|
|
236
282
|
dispose(): void {
|
|
237
283
|
if (this.fileId !== 0) {
|
|
@@ -242,7 +288,22 @@ export class AudioFileWorkers {
|
|
|
242
288
|
}
|
|
243
289
|
|
|
244
290
|
/**
|
|
245
|
-
* Main TagLib class for Cloudflare Workers
|
|
291
|
+
* Main TagLib class for Cloudflare Workers.
|
|
292
|
+
* Provides methods to initialize the library and open audio files
|
|
293
|
+
* in edge computing environments.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```typescript
|
|
297
|
+
* import wasmBinary from "../build/taglib.wasm";
|
|
298
|
+
*
|
|
299
|
+
* // Initialize TagLib
|
|
300
|
+
* const taglib = await TagLibWorkers.initialize(wasmBinary);
|
|
301
|
+
*
|
|
302
|
+
* // Process audio file
|
|
303
|
+
* const file = taglib.openFile(audioBuffer);
|
|
304
|
+
* const metadata = file.tag();
|
|
305
|
+
* file.dispose();
|
|
306
|
+
* ```
|
|
246
307
|
*/
|
|
247
308
|
export class TagLibWorkers {
|
|
248
309
|
private module: TagLibModule;
|
|
@@ -252,10 +313,10 @@ export class TagLibWorkers {
|
|
|
252
313
|
}
|
|
253
314
|
|
|
254
315
|
/**
|
|
255
|
-
* Initialize TagLib for Workers with
|
|
316
|
+
* Initialize TagLib for Workers with Wasm binary
|
|
256
317
|
*
|
|
257
318
|
* @param wasmBinary - The WebAssembly binary as Uint8Array
|
|
258
|
-
* @param config - Optional configuration for the
|
|
319
|
+
* @param config - Optional configuration for the Wasm module
|
|
259
320
|
*
|
|
260
321
|
* @example
|
|
261
322
|
* ```typescript
|
|
@@ -276,11 +337,23 @@ export class TagLibWorkers {
|
|
|
276
337
|
}
|
|
277
338
|
|
|
278
339
|
/**
|
|
279
|
-
* Open an audio file from a buffer
|
|
340
|
+
* Open an audio file from a buffer.
|
|
341
|
+
*
|
|
342
|
+
* @param buffer - Audio file data as Uint8Array
|
|
343
|
+
* @returns AudioFileWorkers instance
|
|
344
|
+
* @throws {Error} If Wasm module is not initialized
|
|
345
|
+
* @throws {Error} If file format is invalid or unsupported
|
|
346
|
+
* @throws {Error} If Workers API C-style functions are not available
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```typescript
|
|
350
|
+
* const audioData = new Uint8Array(await request.arrayBuffer());
|
|
351
|
+
* const file = taglib.openFile(audioData);
|
|
352
|
+
* ```
|
|
280
353
|
*/
|
|
281
354
|
openFile(buffer: Uint8Array): AudioFileWorkers {
|
|
282
355
|
if (!this.module.HEAPU8) {
|
|
283
|
-
throw new Error("
|
|
356
|
+
throw new Error("Wasm module not properly initialized - missing HEAPU8");
|
|
284
357
|
}
|
|
285
358
|
|
|
286
359
|
// Use Emscripten's allocate function for proper memory management
|
|
@@ -293,9 +366,11 @@ export class TagLibWorkers {
|
|
|
293
366
|
}
|
|
294
367
|
|
|
295
368
|
if (!this.module._taglib_file_new_from_buffer) {
|
|
296
|
-
throw new Error(
|
|
369
|
+
throw new Error(
|
|
370
|
+
"Workers API requires C-style functions. Use Core API instead.",
|
|
371
|
+
);
|
|
297
372
|
}
|
|
298
|
-
|
|
373
|
+
|
|
299
374
|
const fileId = this.module._taglib_file_new_from_buffer(
|
|
300
375
|
dataPtr,
|
|
301
376
|
buffer.length,
|
|
@@ -317,7 +392,8 @@ export class TagLibWorkers {
|
|
|
317
392
|
}
|
|
318
393
|
|
|
319
394
|
/**
|
|
320
|
-
* Get the underlying
|
|
395
|
+
* Get the underlying Wasm module for advanced usage.
|
|
396
|
+
* @returns The initialized TagLib Wasm module
|
|
321
397
|
*/
|
|
322
398
|
getModule(): TagLibModule {
|
|
323
399
|
return this.module;
|
|
@@ -362,5 +438,9 @@ export async function processAudioMetadata(
|
|
|
362
438
|
}
|
|
363
439
|
}
|
|
364
440
|
|
|
365
|
-
|
|
441
|
+
/**
|
|
442
|
+
* Re-export commonly used types for convenience.
|
|
443
|
+
* These types define the structure of metadata, audio properties,
|
|
444
|
+
* and configuration options.
|
|
445
|
+
*/
|
|
366
446
|
export type { AudioFormat, AudioProperties, ExtendedTag, Tag, TagLibConfig };
|