From 414d55118d4b787f6190a9d5816e97a28b889772 Mon Sep 17 00:00:00 2001 From: Borewit Date: Tue, 9 Jul 2024 20:25:42 +0200 Subject: [PATCH] Remove Node.js Buffer dependencies --- lib/id3v2/FrameParser.ts | 2 +- lib/id3v2/ID3v2Parser.ts | 6 ++++-- lib/mp4/AtomToken.ts | 4 ++-- lib/mp4/MP4Parser.ts | 14 +++++++------- lib/musepack/sv7/StreamVersion7.ts | 2 +- lib/ogg/OggParser.ts | 2 +- lib/ogg/vorbis/Vorbis.ts | 2 +- lib/ogg/vorbis/VorbisDecoder.ts | 2 +- 8 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/id3v2/FrameParser.ts b/lib/id3v2/FrameParser.ts index 2e5ddbc84..1e22483e6 100644 --- a/lib/id3v2/FrameParser.ts +++ b/lib/id3v2/FrameParser.ts @@ -184,7 +184,7 @@ export class FrameParser { pic.description = util.decodeString(uint8Array.slice(offset, fzero), encoding); offset = fzero + nullTerminatorLength; - pic.data = Buffer.from(uint8Array.slice(offset, length)); + pic.data = uint8Array.slice(offset, length); output = pic; } break; diff --git a/lib/id3v2/ID3v2Parser.ts b/lib/id3v2/ID3v2Parser.ts index 27ceafe45..9bafd28e1 100644 --- a/lib/id3v2/ID3v2Parser.ts +++ b/lib/id3v2/ID3v2Parser.ts @@ -30,6 +30,8 @@ interface IFrameHeader { flags?: IFrameFlags; } +const asciiDecoder = new TextDecoder('ascii'); + export class ID3v2Parser { public static removeUnsyncBytes(buffer: Uint8Array): Uint8Array { @@ -204,7 +206,7 @@ export class ID3v2Parser { case 2: header = { - id: Buffer.from(uint8Array.slice(0, 3)).toString('ascii'), + id: asciiDecoder.decode(uint8Array.slice(0, 3)), length: Token.UINT24_BE.get(uint8Array, 3) }; if (!header.id.match(/[A-Z0-9]{3}/g)) { @@ -215,7 +217,7 @@ export class ID3v2Parser { case 3: case 4: header = { - id: Buffer.from(uint8Array.slice(0, 4)).toString('ascii'), + id: asciiDecoder.decode(uint8Array.slice(0, 4)), length: (majorVer === 4 ? UINT32SYNCSAFE : Token.UINT32_BE).get(uint8Array, 4), flags: ID3v2Parser.readFrameFlags(uint8Array.slice(8, 10)) }; diff --git a/lib/mp4/AtomToken.ts b/lib/mp4/AtomToken.ts index 929e9e452..f7ee6dcc0 100644 --- a/lib/mp4/AtomToken.ts +++ b/lib/mp4/AtomToken.ts @@ -323,7 +323,7 @@ export interface IDataAtom { /** * An array of bytes containing the value of the metadata. */ - value: Buffer; + value: Uint8Array; } /** @@ -341,7 +341,7 @@ export class DataAtom implements IGetToken { type: Token.UINT24_BE.get(buf, off + 1) }, locale: Token.UINT24_BE.get(buf, off + 4), - value: Buffer.from(new Token.Uint8ArrayType(this.len - 8).get(buf, off + 8)) + value: new Token.Uint8ArrayType(this.len - 8).get(buf, off + 8) }; } } diff --git a/lib/mp4/MP4Parser.ts b/lib/mp4/MP4Parser.ts index 9fe2e4909..df1272e4e 100644 --- a/lib/mp4/MP4Parser.ts +++ b/lib/mp4/MP4Parser.ts @@ -340,7 +340,7 @@ export class MP4Parser extends BasicParser { break; case 'rate': - const rate = dataAtom.value.toString('ascii'); + const rate = new TextDecoder('ascii').decode(dataAtom.value); await this.addTag(tagKey, rate); break; @@ -351,7 +351,7 @@ export class MP4Parser extends BasicParser { case 1: // UTF-8: Without any count or NULL terminator case 18: // Unknown: Found in m4b in combination with a '©gen' tag - await this.addTag(tagKey, dataAtom.value.toString('utf-8')); + await this.addTag(tagKey, new TextDecoder('utf-8').decode(dataAtom.value)); break; case 13: // JPEG @@ -359,7 +359,7 @@ export class MP4Parser extends BasicParser { break; await this.addTag(tagKey, { format: 'image/jpeg', - data: Buffer.from(dataAtom.value) + data: Uint8Array.from(dataAtom.value) }); break; @@ -368,7 +368,7 @@ export class MP4Parser extends BasicParser { break; await this.addTag(tagKey, { format: 'image/png', - data: Buffer.from(dataAtom.value) + data: Uint8Array.from(dataAtom.value) }); break; @@ -381,15 +381,15 @@ export class MP4Parser extends BasicParser { break; case 65: // An 8-bit signed integer - await this.addTag(tagKey, dataAtom.value.readInt8(0)); + await this.addTag(tagKey, Token.UINT8.get(dataAtom.value,0)); break; case 66: // A big-endian 16-bit signed integer - await this.addTag(tagKey, dataAtom.value.readInt16BE(0)); + await this.addTag(tagKey, Token.UINT16_BE.get(dataAtom.value,0)); break; case 67: // A big-endian 32-bit signed integer - await this.addTag(tagKey, dataAtom.value.readInt32BE(0)); + await this.addTag(tagKey, Token.UINT32_BE.get(dataAtom.value,0)); break; default: diff --git a/lib/musepack/sv7/StreamVersion7.ts b/lib/musepack/sv7/StreamVersion7.ts index 43faf893a..b086e5638 100644 --- a/lib/musepack/sv7/StreamVersion7.ts +++ b/lib/musepack/sv7/StreamVersion7.ts @@ -44,7 +44,7 @@ export const Header: IGetToken = { const header = { // word 0 - signature: Buffer.from(buf).toString('latin1', off, off + 3), + signature: new TextDecoder('latin`1').decode(buf.subarray(off, off + 3)), // versionIndex number * 1000 (3.81 = 3810) (remember that 4-byte alignment causes this to take 4-bytes) streamMinorVersion: util.getBitAllignedNumber(buf, off + 3, 0, 4), streamMajorVersion: util.getBitAllignedNumber(buf, off + 3, 4, 4), diff --git a/lib/ogg/OggParser.ts b/lib/ogg/OggParser.ts index 9b415391e..775d70713 100644 --- a/lib/ogg/OggParser.ts +++ b/lib/ogg/OggParser.ts @@ -94,7 +94,7 @@ export class OggParser extends BasicParser { const pageData = await this.tokenizer.readToken(new Token.Uint8ArrayType(segmentTable.totalPageSize)); debug('firstPage=%s, lastPage=%s, continued=%s', header.headerType.firstPage, header.headerType.lastPage, header.headerType.continued); if (header.headerType.firstPage) { - const id = new Token.StringType(7, 'ascii').get(Buffer.from(pageData), 0); + const id = new TextDecoder('ascii').decode(pageData.subarray(0, 7)); switch (id) { case '\x01vorbis': // Ogg/Vorbis debug('Set page consumer to Ogg/Vorbis'); diff --git a/lib/ogg/vorbis/Vorbis.ts b/lib/ogg/vorbis/Vorbis.ts index 2adb00348..dcee4900f 100644 --- a/lib/ogg/vorbis/Vorbis.ts +++ b/lib/ogg/vorbis/Vorbis.ts @@ -61,7 +61,7 @@ export class VorbisPictureToken implements IGetToken { const indexed_color = Token.UINT32_BE.get(buffer, offset += 4); const picDataLen = Token.UINT32_BE.get(buffer, offset += 4); - const data = Buffer.from(buffer.slice(offset += 4, offset + picDataLen)); + const data = Uint8Array.from(buffer.slice(offset += 4, offset + picDataLen)); return { type, diff --git a/lib/ogg/vorbis/VorbisDecoder.ts b/lib/ogg/vorbis/VorbisDecoder.ts index 4aecd5cd7..09613f8aa 100644 --- a/lib/ogg/vorbis/VorbisDecoder.ts +++ b/lib/ogg/vorbis/VorbisDecoder.ts @@ -13,7 +13,7 @@ export class VorbisDecoder { public readStringUtf8(): string { const len = this.readInt32(); - const value = Buffer.from(this.data).toString('utf-8', this.offset, this.offset + len); + const value = new TextDecoder('utf-8').decode(this.data.subarray(this.offset, this.offset + len)); this.offset += len; return value; }