Skip to content

Commit

Permalink
Remove Node.js Buffer dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Jul 9, 2024
1 parent 573c505 commit 414d551
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/id3v2/FrameParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions lib/id3v2/ID3v2Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface IFrameHeader {
flags?: IFrameFlags;
}

const asciiDecoder = new TextDecoder('ascii');

export class ID3v2Parser {

public static removeUnsyncBytes(buffer: Uint8Array): Uint8Array {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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))
};
Expand Down
4 changes: 2 additions & 2 deletions lib/mp4/AtomToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export interface IDataAtom {
/**
* An array of bytes containing the value of the metadata.
*/
value: Buffer;
value: Uint8Array;
}

/**
Expand All @@ -341,7 +341,7 @@ export class DataAtom implements IGetToken<IDataAtom> {
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)
};
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/mp4/MP4Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -351,15 +351,15 @@ 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
if (this.options.skipCovers)
break;
await this.addTag(tagKey, {
format: 'image/jpeg',
data: Buffer.from(dataAtom.value)
data: Uint8Array.from(dataAtom.value)
});
break;

Expand All @@ -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;

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/musepack/sv7/StreamVersion7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const Header: IGetToken<IHeader> = {

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),
Expand Down
2 changes: 1 addition & 1 deletion lib/ogg/OggParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class OggParser extends BasicParser {
const pageData = await this.tokenizer.readToken<Uint8Array>(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');
Expand Down
2 changes: 1 addition & 1 deletion lib/ogg/vorbis/Vorbis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class VorbisPictureToken implements IGetToken<IVorbisPicture> {
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,
Expand Down
2 changes: 1 addition & 1 deletion lib/ogg/vorbis/VorbisDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 414d551

Please sign in to comment.