node-taglib-sharp-extend
Advanced tools
Comparing version
@@ -68,6 +68,22 @@ import { c as IPicture, g as PictureType, F as File } from './file-oxeNrhGc.js'; | ||
type Metadata<T extends string | string[] = string[]> = { | ||
/** | ||
* audio tag | ||
*/ | ||
tag: IAudioTag<T>; | ||
/** | ||
* audio property | ||
*/ | ||
property: IAudioProperty; | ||
/** | ||
* audio quality literal | ||
*/ | ||
quality: AudioQualityType; | ||
pictures?: IParsedPicture[]; | ||
/** | ||
* picture list | ||
*/ | ||
pictures: IParsedPicture[]; | ||
/** | ||
* reasons that file metadata is not writable | ||
*/ | ||
unwritableReason: string[]; | ||
}; | ||
@@ -121,5 +137,10 @@ type ArrayableName = Exclude<{ | ||
declare function getBufferFromFile(file: File): Uint8Array | undefined; | ||
declare class CorruptError extends Error { | ||
reasons: string[]; | ||
constructor(reasons: string[]); | ||
} | ||
/** | ||
* flush file instance, auto handle memory file | ||
* @param file {@link File} instance | ||
* @throws if `file` is created from buffer and corrupt after flusing, throw {@link CorruptError} | ||
*/ | ||
@@ -140,2 +161,2 @@ declare function flushFile(file: File): File; | ||
export { type AudioQualityType, AudioTagMap, type IAudioProperty, type IAudioTag, type IParsedPicture, type Metadata, type TransformStringArrayFn, type UpdateTagKey, type UpdateTagValue, checkWebWorkerSupport, flushFile, getBufferFromFile, getFileFromBuffer, getFileFromPath, getPictureBase64, getPictureURL, parseMetadata, updatePicture, updateTag }; | ||
export { type AudioQualityType, AudioTagMap, CorruptError, type IAudioProperty, type IAudioTag, type IParsedPicture, type Metadata, type TransformStringArrayFn, type UpdateTagKey, type UpdateTagValue, checkWebWorkerSupport, flushFile, getBufferFromFile, getFileFromBuffer, getFileFromPath, getPictureBase64, getPictureURL, parseMetadata, updatePicture, updateTag }; |
@@ -70,3 +70,4 @@ import { | ||
quality, | ||
pictures: pictures.length ? pictures : void 0 | ||
pictures, | ||
unwritableReason: file.corruptionReasons | ||
}; | ||
@@ -121,6 +122,19 @@ } | ||
} | ||
var CorruptError = class extends Error { | ||
constructor(reasons) { | ||
super(`fail to flush file, ${reasons}`); | ||
this.reasons = reasons; | ||
} | ||
}; | ||
function flushFile(file) { | ||
file.save(); | ||
const abstraction = file.fileAbstraction; | ||
return abstraction instanceof MemoryFileAbstraction ? getFileFromBuffer(abstraction.name, abstraction.currentBuffer) : file; | ||
if (!(abstraction instanceof MemoryFileAbstraction)) { | ||
return file; | ||
} | ||
const result = getFileFromBuffer(abstraction.name, abstraction.currentBuffer); | ||
if (!result.isWritable) { | ||
throw new CorruptError(result.corruptionReasons); | ||
} | ||
return result; | ||
} | ||
@@ -147,2 +161,3 @@ function getPictureURL(picture) { | ||
AudioTagMap, | ||
CorruptError, | ||
checkWebWorkerSupport, | ||
@@ -149,0 +164,0 @@ flushFile, |
{ | ||
"name": "node-taglib-sharp-extend", | ||
"type": "module", | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
"description": "read and write audio file metadata in nodejs / browser(memory)", | ||
@@ -63,2 +63,5 @@ "author": "subframe7536", | ||
"peerDependenciesMeta": { | ||
"magic-string": { | ||
"optional": true | ||
}, | ||
"vite-plugin-node-polyfills": { | ||
@@ -65,0 +68,0 @@ "optional": true |
# node-taglib-sharp-extend | ||
patched [node-taglib-sharp](https://github.com/benrr101/node-taglib-sharp) v5.2.3, add support for file in buffer | ||
patch [node-taglib-sharp](https://github.com/benrr101/node-taglib-sharp) and add support for file in buffer | ||
backup your file! | ||
known issue: | ||
- `m4a` maybe corrupt after writing, see [original issue](https://github.com/benrr101/node-taglib-sharp/issues/103#issuecomment-1987243846) | ||
## Install | ||
```shell | ||
pnpm add node-taglib-sharp node-taglib-sharp-extend | ||
npm install node-taglib-sharp-extend | ||
``` | ||
```shell | ||
yarn add node-taglib-sharp-extend | ||
``` | ||
```shell | ||
pnpm add node-taglib-sharp-extend | ||
``` | ||
@@ -39,3 +50,3 @@ ## Usage | ||
const { tag, property, pictures, quality } = parseMetadata( | ||
const { tag, property, pictures, quality, unwritableReason } = parseMetadata( | ||
file, | ||
@@ -48,4 +59,5 @@ arr => arr.flatMap(a => a.split('; ')).join('') | ||
// if File is created from buffer and corrupt after flusing, throw CorruptError | ||
file = flushFile(file) | ||
console.log(getBufferFromFile(file).length) | ||
console.log('file size:', getBufferFromFile(file).length) | ||
@@ -106,3 +118,3 @@ // browser only | ||
there is a built-in vite plugin for polyfill, and please ensure that [`vite-plugin-node-polyfills`](https://github.com/davidmyersdev/vite-plugin-node-polyfills) is installed (will be installed by default through peerDependencies) | ||
there is a built-in vite plugin for polyfill, and please ensure that [`vite-plugin-node-polyfills`](https://github.com/davidmyersdev/vite-plugin-node-polyfills) is installed | ||
@@ -109,0 +121,0 @@ when dev, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
4082677
0.05%105627
0.05%153
8.51%