Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Uint8Array instead of Buffer where reasonable possible #852

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/aiff/AiffParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class AIFFParser extends BasicParser {
return header.chunkSize;

case 'ID3 ': // ID3-meta-data
const id3_data = await this.tokenizer.readToken<Buffer>(new Token.BufferType(header.chunkSize));
const id3_data = await this.tokenizer.readToken<Uint8Array>(new Token.Uint8ArrayType(header.chunkSize));
const rst = strtok3.fromBuffer(id3_data);
await new ID3v2Parser().parse(this.metadata, rst, this.options);
return header.chunkSize;
Expand Down
10 changes: 5 additions & 5 deletions lib/apev2/APEv2Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface IDescriptor {
// the terminating data of the file (not including tag data)
terminatingDataBytes: number,
// the MD5 hash of the file (see notes for usage... it's a littly tricky)
fileMD5: Buffer
fileMD5: Uint8Array
}

/**
Expand Down Expand Up @@ -115,8 +115,8 @@ export const DescriptorParser: IGetToken<IDescriptor> = {
apeFrameDataBytesHigh: Token.UINT32_LE.get(buf, off + 28),
// the terminating data of the file (not including tag data)
terminatingDataBytes: Token.UINT32_LE.get(buf, off + 32),
// the MD5 hash of the file (see notes for usage... it's a littly tricky)
fileMD5: new Token.BufferType(16).get(buf, off + 36)
// the MD5 hash of the file (see notes for usage... it's a little tricky)
fileMD5: new Token.Uint8ArrayType(16).get(buf, off + 36)
};
}
};
Expand Down Expand Up @@ -156,7 +156,7 @@ export const Header: IGetToken<IHeader> = {
export const TagFooter: IGetToken<IFooter> = {
len: 32,

get: (buf, off) => {
get: (buf: Buffer, off) => {
return {
// should equal 'APETAGEX'
ID: new Token.StringType(8, 'ascii').get(buf, off),
Expand Down Expand Up @@ -199,7 +199,7 @@ export const TagItemHeader: IGetToken<ITagItemHeader> = {
};

export const TagField = footer => {
return new Token.BufferType(footer.size - TagFooter.len);
return new Token.Uint8ArrayType(footer.size - TagFooter.len);
};

export interface ITagFlags {
Expand Down
4 changes: 2 additions & 2 deletions lib/asf/AsfObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface IAsfTopLevelObjectHeader extends IAsfObjectHeader {
* Token for: 3. ASF top-level Header Object
* Ref: http://drang.s4.xrea.com/program/tips/id3tag/wmp/03_asf_top_level_header_object.html#3
*/
export const TopLevelHeaderObjectToken: IGetToken<IAsfTopLevelObjectHeader> = {
export const TopLevelHeaderObjectToken: IGetToken<IAsfTopLevelObjectHeader, Buffer> = {

len: 30,

Expand All @@ -84,7 +84,7 @@ export const TopLevelHeaderObjectToken: IGetToken<IAsfTopLevelObjectHeader> = {
* Token for: 3.1 Header Object (mandatory, one only)
* Ref: http://drang.s4.xrea.com/program/tips/id3tag/wmp/03_asf_top_level_header_object.html#3_1
*/
export const HeaderObjectToken: IGetToken<IAsfObjectHeader> = {
export const HeaderObjectToken: IGetToken<IAsfObjectHeader, Buffer> = {

len: 24,

Expand Down
2 changes: 0 additions & 2 deletions lib/asf/GUID.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Implementation of the Advanced Systems Format (ASF)

/**
* Ref:
* https://tools.ietf.org/html/draft-fleischman-asf-01, Appendix A: ASF GUIDs
Expand Down
4 changes: 2 additions & 2 deletions lib/common/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function stripNulls(str: string): string {
* @param len Length of number in bits
* @return {number} decoded bit aligned number
*/
export function getBitAllignedNumber(buf: Buffer, byteOffset: number, bitOffset: number, len: number): number {
export function getBitAllignedNumber(buf: Uint8Array, byteOffset: number, bitOffset: number, len: number): number {
const byteOff = byteOffset + ~~(bitOffset / 8);
const bitOff = bitOffset % 8;
let value = buf[byteOff];
Expand All @@ -117,7 +117,7 @@ export function getBitAllignedNumber(buf: Buffer, byteOffset: number, bitOffset:
* @param bitOffset Starting offset in bits: 0 = most significant bit, 7 is least significant bit
* @return {number} decoded bit aligned number
*/
export function isBitSet(buf: Buffer, byteOffset: number, bitOffset: number): boolean {
export function isBitSet(buf: Uint8Array, byteOffset: number, bitOffset: number): boolean {
return getBitAllignedNumber(buf, byteOffset, bitOffset, 1) === 1;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/dsdiff/DsdiffParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class DsdiffParser extends BasicParser {
break;

case 'ID3': // Unofficial ID3 tag support
const id3_data = await this.tokenizer.readToken<Buffer>(new Token.BufferType(Number(header.chunkSize)));
const id3_data = await this.tokenizer.readToken<Uint8Array>(new Token.Uint8ArrayType(Number(header.chunkSize)));
const rst = strtok3.fromBuffer(id3_data);
await new ID3v2Parser().parse(this.metadata, rst, this.options);
break;
Expand Down
6 changes: 3 additions & 3 deletions lib/flac/FlacParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class FlacParser extends AbstractID3Parser {
* Ref: https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-640004.2.3
*/
private async parseComment(dataLen: number): Promise<void> {
const data = await this.tokenizer.readToken<Buffer>(new Token.BufferType(dataLen));
const data = await this.tokenizer.readToken<Uint8Array>(new Token.Uint8ArrayType(dataLen));
const decoder = new VorbisDecoder(data, 0);
decoder.readStringUtf8(); // vendor (skip)
const commentListLength = decoder.readInt32();
Expand Down Expand Up @@ -183,7 +183,7 @@ interface IBlockStreamInfo {
// A value of zero here means the number of total samples is unknown.
totalSamples: number,
// the MD5 hash of the file (see notes for usage... it's a littly tricky)
fileMD5: Buffer;
fileMD5: Uint8Array;
}

class Metadata {
Expand Down Expand Up @@ -235,7 +235,7 @@ class Metadata {
// A value of zero here means the number of total samples is unknown.
totalSamples: util.getBitAllignedNumber(buf, off + 13, 4, 36),
// the MD5 hash of the file (see notes for usage... it's a littly tricky)
fileMD5: new Token.BufferType(16).get(buf, off + 18)
fileMD5: new Token.Uint8ArrayType(16).get(buf, off + 18)
};
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/id3v1/ID3v1Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const Iid3v1Token: IGetToken<IId3v1Header> = {
* @param off Offset in buffer in bytes
* @returns ID3v1.1 header if first 3 bytes equals 'TAG', otherwise null is returned
*/
get: (buf, off): IId3v1Header => {
get: (buf: Buffer, off): IId3v1Header => {
const header = new Id3v1StringType(3).get(buf, off);
return header === "TAG" ? {
header,
Expand Down
4 changes: 2 additions & 2 deletions lib/id3v2/ID3v2Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface IExtendedHeader {
* 4 * %0xxxxxxx
*/
export const UINT32SYNCSAFE = {
get: (buf: Buffer, off: number): number => {
get: (buf: Uint8Array, off: number): number => {
return buf[off + 3] & 0x7f | ((buf[off + 2]) << 7) |
((buf[off + 1]) << 14) | ((buf[off]) << 21);
},
Expand Down Expand Up @@ -86,7 +86,7 @@ export interface IID3v2header {
export const ID3v2Header: IGetToken<IID3v2header> = {
len: 10,

get: (buf, off): IID3v2header => {
get: (buf: Buffer, off): IID3v2header => {
return {
// ID3v2/file identifier "ID3"
fileIdentifier: new Token.StringType(3, 'ascii').get(buf, off),
Expand Down
3 changes: 2 additions & 1 deletion lib/iff/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {FourCcToken} from "../common/FourCC";
import { IGetToken } from "strtok3/lib/core";
import * as Token from 'token-types';

/**
* "EA IFF 85" Standard for Interchange Format Files
Expand Down Expand Up @@ -44,7 +45,7 @@ export const Header: IGetToken<IChunkHeader> = {
// Chunk type ID
chunkID: FourCcToken.get(buf, off),
// Chunk size
chunkSize: buf.readUInt32BE(off + 4)
chunkSize: Number(BigInt(Token.UINT32_BE.get(buf, off + 4)))
};
}
};
Expand Down
8 changes: 4 additions & 4 deletions lib/mp4/AtomToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,14 @@ export class DataAtom implements IGetToken<IDataAtom> {
public constructor(public len: number) {
}

public get(buf: Buffer, off: number): IDataAtom {
public get(buf: Uint8Array, off: number): IDataAtom {
return {
type: {
set: Token.UINT8.get(buf, off + 0),
type: Token.UINT24_BE.get(buf, off + 1)
},
locale: Token.UINT24_BE.get(buf, off + 4),
value: new Token.BufferType(this.len - 8).get(buf, off + 8)
value: Buffer.from(new Token.Uint8ArrayType(this.len - 8).get(buf, off + 8))
};
}
}
Expand Down Expand Up @@ -483,7 +483,7 @@ const stsdHeader: IGetToken<IAtomStsdHeader> = {
export interface ISampleDescription {
dataFormat: string;
dataReferenceIndex: number;
description: Buffer;
description: Uint8Array;
}

export interface IAtomStsd {
Expand All @@ -505,7 +505,7 @@ class SampleDescriptionTable implements IGetToken<ISampleDescription> {
return {
dataFormat: FourCcToken.get(buf, off),
dataReferenceIndex: Token.UINT16_BE.get(buf, off + 10),
description: new Token.BufferType(this.len - 12).get(buf, off + 12)
description: new Token.Uint8ArrayType(this.len - 12).get(buf, off + 12)
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/mpeg/MpegParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export class MpegParser extends AbstractID3Parser {
private async skipSideInformation(): Promise<void> {
const sideinfo_length = this.audioFrameHeader.calculateSideInfoLength();
// side information
await this.tokenizer.readToken(new Token.BufferType(sideinfo_length));
await this.tokenizer.readToken(new Token.Uint8ArrayType(sideinfo_length));
this.offset += sideinfo_length;
await this.readXtraInfoHeader();
return;
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: buf.toString("binary", off, off + 3),
signature: Buffer.from(buf).toString('latin1', 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/Ogg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface IPageConsumer {
* @param {IPageHeader} header Ogg page header
* @param {Buffer} pageData Ogg page data
*/
parsePage(header: IPageHeader, pageData: Buffer);
parsePage(header: IPageHeader, pageData: Uint8Array);

/**
* Calculate duration of provided header
Expand Down
10 changes: 5 additions & 5 deletions lib/ogg/OggParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ export class OggParser extends BasicParser {
get: (buf, off): Ogg.IPageHeader => {
return {
capturePattern: FourCcToken.get(buf, off),
version: buf.readUInt8(off + 4),
version: Token.UINT8.get(buf, off + 4),

headerType: {
continued: util.getBit(buf, off + 5, 0),
firstPage: util.getBit(buf, off + 5, 1),
lastPage: util.getBit(buf, off + 5, 2)
},
// packet_flag: buf.readUInt8(off + 5),
absoluteGranulePosition: buf.readIntLE(off + 6, 6), // cannot read 2 of 8 most significant bytes
absoluteGranulePosition: Number(Token.UINT64_LE.get(buf, off + 6)),
streamSerialNumber: Token.UINT32_LE.get(buf, off + 14),
pageSequenceNo: Token.UINT32_LE.get(buf, off + 18),
pageChecksum: Token.UINT32_LE.get(buf, off + 22),
page_segments: buf.readUInt8(off + 26)
page_segments: Token.UINT8.get(buf, off + 26)
};
}
};
Expand Down Expand Up @@ -89,10 +89,10 @@ export class OggParser extends BasicParser {

const segmentTable = await this.tokenizer.readToken<Ogg.ISegmentTable>(new SegmentTable(header));
debug('totalPageSize=%s', segmentTable.totalPageSize);
const pageData = await this.tokenizer.readToken<Buffer>(new Token.BufferType(segmentTable.totalPageSize));
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(pageData, 0);
const id = new Token.StringType(7, 'ascii').get(Buffer.from(pageData), 0);
switch (id) {
case '\x01vorbis': // Ogg/Vorbis
debug('Set page consumer to Ogg/Vorbis');
Expand Down
2 changes: 1 addition & 1 deletion lib/ogg/speex/Speex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Header: IGetToken<IHeader> = {

len: 80,

get: (buf, off) => {
get: (buf: Buffer, off) => {

return {
speex: new Token.StringType(8, 'ascii').get(buf, off + 0),
Expand Down
2 changes: 1 addition & 1 deletion lib/ogg/theora/Theora.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface IIdentificationHeader {
export const IdentificationHeader: IGetToken<IIdentificationHeader> = {
len: 42,

get: (buf, off): IIdentificationHeader => {
get: (buf: Buffer, off): IIdentificationHeader => {
return {
id: new Token.StringType(7, 'ascii').get(buf, off),
vmaj: buf.readUInt8(off + 7),
Expand Down
17 changes: 9 additions & 8 deletions lib/ogg/vorbis/Vorbis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export interface ICommonHeader {
export const CommonHeader: IGetToken<ICommonHeader> = {
len: 7,

get: (buf, off): ICommonHeader => {
get: (buf: Buffer, off): ICommonHeader => {
return {
packetType: buf.readUInt8(off),
vorbis: new Token.StringType(6, 'ascii').get(buf, off + 1)
Expand Down Expand Up @@ -142,14 +142,15 @@ export interface IFormatInfo {
export const IdentificationHeader: IGetToken<IFormatInfo> = {
len: 23,

get: (buf, off): IFormatInfo => {
get: (uint8Array, off): IFormatInfo => {
const dataView = new DataView(uint8Array.buffer, uint8Array.byteOffset);
return {
version: buf.readUInt32LE(off + 0),
channelMode: buf.readUInt8(off + 4),
sampleRate: buf.readUInt32LE(off + 5),
bitrateMax: buf.readUInt32LE(off + 9),
bitrateNominal: buf.readUInt32LE(off + 13),
bitrateMin: buf.readUInt32LE(off + 17)
version: dataView.getUint32(off + 0, true),
channelMode: dataView.getUint8(off + 4),
sampleRate: dataView.getUint32(off + 5, true),
bitrateMax: dataView.getUint32(off + 9, true),
bitrateNominal: dataView.getUint32(off + 13, true),
bitrateMin: dataView.getUint32(off + 17, true)
};
}
};
4 changes: 2 additions & 2 deletions lib/ogg/vorbis/VorbisDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Token from 'token-types';

export class VorbisDecoder {

constructor(private readonly data: Buffer, private offset) {
constructor(private readonly data: Uint8Array, private offset) {
}

public readInt32(): number {
Expand All @@ -13,7 +13,7 @@ export class VorbisDecoder {

public readStringUtf8(): string {
const len = this.readInt32();
const value = this.data.toString('utf8', this.offset, this.offset + len);
const value = Buffer.from(this.data).toString('utf-8', this.offset, this.offset + len);
this.offset += len;
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/riff/RiffChunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export {IChunkHeader} from '../iff';
export const Header: IGetToken<IChunkHeader> = {
len: 8,

get: (buf, off): IChunkHeader => {
get: (buf: Buffer, off): IChunkHeader => {
return {
// Group-ID
chunkID: buf.toString('binary', off, off + 4),
// Size
chunkSize: buf.readUInt32LE(off + 4)
chunkSize: Token.UINT32_LE.get(buf, 4)
};
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/wav/WaveParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class WaveParser extends BasicParser {

case 'id3 ': // The way Picard, FooBar currently stores, ID3 meta-data
case 'ID3 ': // The way Mp3Tags stores ID3 meta-data
const id3_data = await this.tokenizer.readToken<Buffer>(new Token.BufferType(header.chunkSize));
const id3_data = await this.tokenizer.readToken<Uint8Array>(new Token.Uint8ArrayType(header.chunkSize));
const rst = strtok3.fromBuffer(id3_data);
await new ID3v2Parser().parse(this.metadata, rst, this.options);
break;
Expand Down
4 changes: 2 additions & 2 deletions lib/wavpack/WavPackToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface IBlockHeader {
// false = PCM audio; true = DSD audio (ver 5.0+)
}
// crc for actual decoded data
crc: Buffer
crc: Uint8Array
}

export interface IMetadataId {
Expand Down Expand Up @@ -109,7 +109,7 @@ export class WavPack {
isDSD: WavPack.isBitSet(flags, 31)
},
// crc for actual decoded data
crc: new Token.BufferType(4).get(buf, off + 28)
crc: new Token.Uint8ArrayType(4).get(buf, off + 28)
};

if (res.flags.isDSD) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@
"debug": "^4.3.2",
"file-type": "16.5.2",
"media-typer": "^1.1.0",
"strtok3": "6.1.3",
"token-types": "3.0.0"
"strtok3": "^6.2.2",
"token-types": "^4.1.0"
},
"devDependencies": {
"@tokenizer/token": "^0.3.0",
"@types/chai": "^4.2.21",
"@types/debug": "^4.1.6",
"@types/file-type": "^10.9.1",
Expand Down
Loading