taglib-wasm 0.3.13 → 0.3.15

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 (82) hide show
  1. package/README.md +22 -35
  2. package/dist/index.d.ts +10 -10
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +117 -134
  5. package/dist/index.js.map +1 -0
  6. package/dist/src/constants.js +211 -0
  7. package/dist/src/constants.js.map +1 -0
  8. package/dist/src/errors.js +180 -0
  9. package/dist/src/errors.js.map +1 -0
  10. package/dist/src/file-utils.d.ts +2 -2
  11. package/dist/src/file-utils.d.ts.map +1 -1
  12. package/dist/src/file-utils.js +394 -0
  13. package/dist/src/file-utils.js.map +1 -0
  14. package/dist/src/mod.d.ts +4 -4
  15. package/dist/src/mod.d.ts.map +1 -1
  16. package/dist/src/mod.js +7 -0
  17. package/dist/src/mod.js.map +1 -0
  18. package/dist/src/simple.d.ts +4 -4
  19. package/dist/src/simple.d.ts.map +1 -1
  20. package/dist/src/{simple.ts → simple.js} +193 -311
  21. package/dist/src/simple.js.map +1 -0
  22. package/dist/src/taglib.d.ts +3 -3
  23. package/dist/src/taglib.d.ts.map +1 -1
  24. package/dist/src/taglib.js +516 -0
  25. package/dist/src/taglib.js.map +1 -0
  26. package/dist/src/types.d.ts +2 -2
  27. package/dist/src/types.d.ts.map +1 -1
  28. package/dist/src/types.js +239 -0
  29. package/dist/src/types.js.map +1 -0
  30. package/dist/src/utils/file.js +65 -0
  31. package/dist/src/utils/file.js.map +1 -0
  32. package/dist/src/utils/write.js +49 -0
  33. package/dist/src/utils/write.js.map +1 -0
  34. package/dist/src/wasm-workers.d.ts +1 -1
  35. package/dist/src/wasm-workers.d.ts.map +1 -1
  36. package/dist/src/wasm-workers.js +148 -0
  37. package/dist/src/wasm-workers.js.map +1 -0
  38. package/dist/src/wasm.js +6 -0
  39. package/dist/src/wasm.js.map +1 -0
  40. package/dist/src/web-utils.d.ts +2 -2
  41. package/dist/src/web-utils.d.ts.map +1 -1
  42. package/{src/web-utils.ts → dist/src/web-utils.js} +102 -184
  43. package/dist/src/web-utils.js.map +1 -0
  44. package/dist/src/workers.d.ts +2 -2
  45. package/dist/src/workers.d.ts.map +1 -1
  46. package/dist/src/workers.js +389 -0
  47. package/dist/src/workers.js.map +1 -0
  48. package/dist/taglib-wrapper.js +8 -2528
  49. package/package.json +7 -10
  50. package/dist/index.ts +0 -221
  51. package/dist/src/constants.ts +0 -227
  52. package/dist/src/errors.ts +0 -254
  53. package/dist/src/file-utils.ts +0 -483
  54. package/dist/src/file.js +0 -52
  55. package/dist/src/global.d.ts +0 -12
  56. package/dist/src/mod.ts +0 -19
  57. package/dist/src/taglib.ts +0 -961
  58. package/dist/src/types.ts +0 -538
  59. package/dist/src/utils/file.ts +0 -86
  60. package/dist/src/utils/write.ts +0 -66
  61. package/dist/src/wasm-workers.ts +0 -176
  62. package/dist/src/wasm.ts +0 -133
  63. package/dist/src/web-utils.ts +0 -347
  64. package/dist/src/workers.ts +0 -461
  65. package/dist/src/write.js +0 -33
  66. package/index.ts +0 -221
  67. package/lib/taglib/COPYING.LGPL +0 -502
  68. package/lib/taglib/COPYING.MPL +0 -470
  69. package/lib/taglib/README.md +0 -24
  70. package/src/constants.ts +0 -227
  71. package/src/errors.ts +0 -254
  72. package/src/file-utils.ts +0 -483
  73. package/src/global.d.ts +0 -12
  74. package/src/mod.ts +0 -19
  75. package/src/simple.ts +0 -667
  76. package/src/taglib.ts +0 -961
  77. package/src/types.ts +0 -538
  78. package/src/utils/file.ts +0 -86
  79. package/src/utils/write.ts +0 -66
  80. package/src/wasm-workers.ts +0 -176
  81. package/src/wasm.ts +0 -133
  82. package/src/workers.ts +0 -461
package/src/workers.ts DELETED
@@ -1,461 +0,0 @@
1
- /**
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
9
- */
10
-
11
- import type {
12
- AudioFormat,
13
- AudioProperties,
14
- ExtendedTag,
15
- Tag,
16
- TagLibConfig,
17
- } from "./types.ts";
18
- import {
19
- cStringToJS,
20
- jsToCString,
21
- loadTagLibModuleForWorkers,
22
- type TagLibModule,
23
- } from "./wasm-workers.ts";
24
- import { EnvironmentError, InvalidFormatError, MemoryError } from "./errors.ts";
25
-
26
- /**
27
- * Represents an audio file with metadata and properties (Workers-compatible).
28
- * This implementation uses C-style function calls for Cloudflare Workers compatibility.
29
- *
30
- * @example
31
- * ```typescript
32
- * const file = taglib.openFile(audioBuffer);
33
- *
34
- * // Get metadata
35
- * const tag = file.tag();
36
- * console.log(tag.title);
37
- *
38
- * // Modify metadata
39
- * file.setTitle("New Title");
40
- * file.save();
41
- *
42
- * // Clean up
43
- * file.dispose();
44
- * ```
45
- */
46
- export class AudioFileWorkers {
47
- private module: TagLibModule;
48
- private fileId: number;
49
- private tagPtr: number;
50
- private propsPtr: number;
51
-
52
- constructor(module: TagLibModule, fileId: number) {
53
- this.module = module;
54
- this.fileId = fileId;
55
- this.tagPtr = module._taglib_file_tag?.(fileId) || 0;
56
- this.propsPtr = module._taglib_file_audioproperties?.(fileId) || 0;
57
- }
58
-
59
- /**
60
- * Check if the file is valid and was loaded successfully.
61
- * @returns true if the file is valid and can be processed
62
- */
63
- isValid(): boolean {
64
- return this.module._taglib_file_is_valid?.(this.fileId) !== 0;
65
- }
66
-
67
- /**
68
- * Get the file format.
69
- * @returns Audio format (e.g., "MP3", "FLAC", "OGG")
70
- */
71
- format(): AudioFormat {
72
- const formatPtr = this.module._taglib_file_format?.(this.fileId) || 0;
73
- if (formatPtr === 0) return "MP3"; // fallback
74
- const formatStr = cStringToJS(this.module, formatPtr);
75
- return formatStr as AudioFormat;
76
- }
77
-
78
- /**
79
- * Get basic tag information.
80
- * @returns Object containing title, artist, album, etc.
81
- */
82
- tag(): Tag {
83
- if (this.tagPtr === 0) return {};
84
-
85
- const title = this.module._taglib_tag_title?.(this.tagPtr) || 0;
86
- const artist = this.module._taglib_tag_artist?.(this.tagPtr) || 0;
87
- const album = this.module._taglib_tag_album?.(this.tagPtr) || 0;
88
- const comment = this.module._taglib_tag_comment?.(this.tagPtr) || 0;
89
- const genre = this.module._taglib_tag_genre?.(this.tagPtr) || 0;
90
- const year = this.module._taglib_tag_year?.(this.tagPtr) || 0;
91
- const track = this.module._taglib_tag_track?.(this.tagPtr) || 0;
92
-
93
- return {
94
- title: title ? cStringToJS(this.module, title) : undefined,
95
- artist: artist ? cStringToJS(this.module, artist) : undefined,
96
- album: album ? cStringToJS(this.module, album) : undefined,
97
- comment: comment ? cStringToJS(this.module, comment) : undefined,
98
- genre: genre ? cStringToJS(this.module, genre) : undefined,
99
- year: year || undefined,
100
- track: track || undefined,
101
- };
102
- }
103
-
104
- /**
105
- * Get audio properties (duration, bitrate, etc.).
106
- * @returns Audio properties or null if unavailable
107
- */
108
- audioProperties(): AudioProperties | null {
109
- if (this.propsPtr === 0) return null;
110
-
111
- const length =
112
- this.module._taglib_audioproperties_length?.(this.propsPtr) || 0;
113
- const bitrate =
114
- this.module._taglib_audioproperties_bitrate?.(this.propsPtr) || 0;
115
- const sampleRate = this.module._taglib_audioproperties_samplerate?.(
116
- this.propsPtr,
117
- ) || 0;
118
- const channels = this.module._taglib_audioproperties_channels?.(
119
- this.propsPtr,
120
- ) || 0;
121
-
122
- return {
123
- length,
124
- bitrate,
125
- sampleRate,
126
- channels,
127
- };
128
- }
129
-
130
- /**
131
- * Set the title tag.
132
- * @param title - New title value
133
- */
134
- setTitle(title: string): void {
135
- if (this.tagPtr === 0) return;
136
- const titlePtr = jsToCString(this.module, title);
137
- this.module._taglib_tag_set_title?.(this.tagPtr, titlePtr);
138
- this.module._free(titlePtr);
139
- }
140
-
141
- /**
142
- * Set the artist tag.
143
- * @param artist - New artist value
144
- */
145
- setArtist(artist: string): void {
146
- if (this.tagPtr === 0) return;
147
- const artistPtr = jsToCString(this.module, artist);
148
- this.module._taglib_tag_set_artist?.(this.tagPtr, artistPtr);
149
- this.module._free(artistPtr);
150
- }
151
-
152
- /**
153
- * Set the album tag.
154
- * @param album - New album value
155
- */
156
- setAlbum(album: string): void {
157
- if (this.tagPtr === 0) return;
158
- const albumPtr = jsToCString(this.module, album);
159
- this.module._taglib_tag_set_album?.(this.tagPtr, albumPtr);
160
- this.module._free(albumPtr);
161
- }
162
-
163
- /**
164
- * Set the comment tag.
165
- * @param comment - New comment value
166
- */
167
- setComment(comment: string): void {
168
- if (this.tagPtr === 0) return;
169
- const commentPtr = jsToCString(this.module, comment);
170
- this.module._taglib_tag_set_comment?.(this.tagPtr, commentPtr);
171
- this.module._free(commentPtr);
172
- }
173
-
174
- /**
175
- * Set the genre tag.
176
- * @param genre - New genre value
177
- */
178
- setGenre(genre: string): void {
179
- if (this.tagPtr === 0) return;
180
- const genrePtr = jsToCString(this.module, genre);
181
- this.module._taglib_tag_set_genre?.(this.tagPtr, genrePtr);
182
- this.module._free(genrePtr);
183
- }
184
-
185
- /**
186
- * Set the year tag.
187
- * @param year - Release year
188
- */
189
- setYear(year: number): void {
190
- if (this.tagPtr === 0) return;
191
- this.module._taglib_tag_set_year?.(this.tagPtr, year);
192
- }
193
-
194
- /**
195
- * Set the track number tag.
196
- * @param track - Track number
197
- */
198
- setTrack(track: number): void {
199
- if (this.tagPtr === 0) return;
200
- this.module._taglib_tag_set_track?.(this.tagPtr, track);
201
- }
202
-
203
- /**
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
207
- */
208
- save(): boolean {
209
- if (this.fileId !== 0) {
210
- return this.module._taglib_file_save?.(this.fileId) !== 0;
211
- }
212
- return false;
213
- }
214
-
215
- /**
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
220
- */
221
- getFileBuffer(): Uint8Array {
222
- console.warn(
223
- "getFileBuffer() is not implemented in Workers API. Use Core API for this functionality.",
224
- );
225
- return new Uint8Array(0);
226
- }
227
-
228
- /**
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
232
- */
233
- extendedTag(): ExtendedTag {
234
- const basicTag = this.tag();
235
-
236
- return {
237
- ...basicTag,
238
- // Advanced fields placeholder - would be populated by PropertyMap reading
239
- acoustidFingerprint: undefined,
240
- acoustidId: undefined,
241
- musicbrainzTrackId: undefined,
242
- musicbrainzReleaseId: undefined,
243
- musicbrainzArtistId: undefined,
244
- musicbrainzReleaseGroupId: undefined,
245
- albumArtist: undefined,
246
- composer: undefined,
247
- discNumber: undefined,
248
- totalTracks: undefined,
249
- totalDiscs: undefined,
250
- bpm: undefined,
251
- compilation: undefined,
252
- titleSort: undefined,
253
- artistSort: undefined,
254
- albumSort: undefined,
255
- replayGainTrackGain: undefined,
256
- replayGainTrackPeak: undefined,
257
- replayGainAlbumGain: undefined,
258
- replayGainAlbumPeak: undefined,
259
- appleSoundCheck: undefined,
260
- };
261
- }
262
-
263
- /**
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
267
- */
268
- setExtendedTag(tag: Partial<ExtendedTag>): void {
269
- if (tag.title !== undefined) this.setTitle(tag.title);
270
- if (tag.artist !== undefined) this.setArtist(tag.artist);
271
- if (tag.album !== undefined) this.setAlbum(tag.album);
272
- if (tag.comment !== undefined) this.setComment(tag.comment);
273
- if (tag.genre !== undefined) this.setGenre(tag.genre);
274
- if (tag.year !== undefined) this.setYear(tag.year);
275
- if (tag.track !== undefined) this.setTrack(tag.track);
276
- }
277
-
278
- /**
279
- * Clean up resources.
280
- * Always call this when done to prevent memory leaks.
281
- */
282
- dispose(): void {
283
- if (this.fileId !== 0) {
284
- this.module._taglib_file_delete?.(this.fileId);
285
- this.fileId = 0;
286
- }
287
- }
288
- }
289
-
290
- /**
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
- * ```
307
- */
308
- export class TagLibWorkers {
309
- private module: TagLibModule;
310
-
311
- private constructor(module: TagLibModule) {
312
- this.module = module;
313
- }
314
-
315
- /**
316
- * Initialize TagLib for Workers with Wasm binary
317
- *
318
- * @param wasmBinary - The WebAssembly binary as Uint8Array
319
- * @param config - Optional configuration for the Wasm module
320
- *
321
- * @example
322
- * ```typescript
323
- * // In a Cloudflare Worker
324
- * import wasmBinary from "../build/taglib.wasm";
325
- *
326
- * const taglib = await TagLibWorkers.initialize(wasmBinary);
327
- * const file = taglib.open(audioBuffer);
328
- * const metadata = file.tag();
329
- * ```
330
- */
331
- static async initialize(
332
- wasmBinary: Uint8Array,
333
- config?: TagLibConfig,
334
- ): Promise<TagLibWorkers> {
335
- const module = await loadTagLibModuleForWorkers(wasmBinary, config);
336
- return new TagLibWorkers(module);
337
- }
338
-
339
- /**
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.open(audioData);
352
- * ```
353
- */
354
- open(buffer: Uint8Array): AudioFileWorkers {
355
- if (!this.module.HEAPU8) {
356
- throw new MemoryError(
357
- "Wasm module not properly initialized: missing HEAPU8. " +
358
- "The module may not have loaded correctly in the Workers environment.",
359
- );
360
- }
361
-
362
- // Use Emscripten's allocate function for proper memory management
363
- let dataPtr: number;
364
- if (this.module.allocate && this.module.ALLOC_NORMAL !== undefined) {
365
- dataPtr = this.module.allocate(buffer, this.module.ALLOC_NORMAL);
366
- } else {
367
- dataPtr = this.module._malloc(buffer.length);
368
- this.module.HEAPU8.set(buffer, dataPtr);
369
- }
370
-
371
- if (!this.module._taglib_file_new_from_buffer) {
372
- throw new EnvironmentError(
373
- "Workers",
374
- "requires C-style functions which are not available. Use the Core API instead for this environment",
375
- "C-style function exports",
376
- );
377
- }
378
-
379
- const fileId = this.module._taglib_file_new_from_buffer(
380
- dataPtr,
381
- buffer.length,
382
- );
383
-
384
- if (fileId === 0) {
385
- // Free the allocated memory since file creation failed
386
- this.module._free(dataPtr);
387
- throw new InvalidFormatError(
388
- "Failed to open audio file. File format may be invalid or not supported",
389
- buffer.length,
390
- );
391
- }
392
-
393
- // Free the temporary buffer copy (TagLib has made its own copy in ByteVector)
394
- this.module._free(dataPtr);
395
-
396
- return new AudioFileWorkers(this.module, fileId);
397
- }
398
-
399
- /**
400
- * @deprecated Use `open()` instead. This method will be removed in the next major version.
401
- * Open an audio file from a buffer (backward compatibility).
402
- * @param buffer Audio file data as Uint8Array
403
- * @returns Audio file instance
404
- */
405
- openFile(buffer: Uint8Array): AudioFileWorkers {
406
- return this.open(buffer);
407
- }
408
-
409
- /**
410
- * Get the underlying Wasm module for advanced usage.
411
- * @returns The initialized TagLib Wasm module
412
- */
413
- getModule(): TagLibModule {
414
- return this.module;
415
- }
416
- }
417
-
418
- /**
419
- * Utility function to process audio metadata in a Cloudflare Worker
420
- *
421
- * @example
422
- * ```typescript
423
- * export default {
424
- * async fetch(request: Request): Promise<Response> {
425
- * if (request.method === "POST") {
426
- * const audioData = new Uint8Array(await request.arrayBuffer());
427
- * const metadata = await processAudioMetadata(wasmBinary, audioData);
428
- * return Response.json(metadata);
429
- * }
430
- * return new Response("Method not allowed", { status: 405 });
431
- * }
432
- * };
433
- * ```
434
- */
435
- export async function processAudioMetadata(
436
- wasmBinary: Uint8Array,
437
- audioData: Uint8Array,
438
- config?: TagLibConfig,
439
- ): Promise<
440
- { tag: Tag; properties: AudioProperties | null; format: AudioFormat }
441
- > {
442
- const taglib = await TagLibWorkers.initialize(wasmBinary, config);
443
- const file = taglib.open(audioData);
444
-
445
- try {
446
- const tag = file.tag();
447
- const properties = file.audioProperties();
448
- const format = file.format();
449
-
450
- return { tag, properties, format };
451
- } finally {
452
- file.dispose();
453
- }
454
- }
455
-
456
- /**
457
- * Re-export commonly used types for convenience.
458
- * These types define the structure of metadata, audio properties,
459
- * and configuration options.
460
- */
461
- export type { AudioFormat, AudioProperties, ExtendedTag, Tag, TagLibConfig };