From 3d1d3b77803a0d1068540a646353b0720171a7ce Mon Sep 17 00:00:00 2001 From: Fernando Cuadro Date: Mon, 5 Feb 2024 11:23:08 -0300 Subject: [PATCH 1/9] initial commit Signed-off-by: Felipe Young --- lib/test/structuredfield/isId3Footer.test.ts | 27 ++++++++++++++++++++ lib/test/structuredfield/isId3Header.test.ts | 27 ++++++++++++++++++++ lib/test/structuredfield/readId3Size.test.ts | 10 ++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/test/structuredfield/isId3Footer.test.ts create mode 100644 lib/test/structuredfield/isId3Header.test.ts create mode 100644 lib/test/structuredfield/readId3Size.test.ts diff --git a/lib/test/structuredfield/isId3Footer.test.ts b/lib/test/structuredfield/isId3Footer.test.ts new file mode 100644 index 00000000..3b69fd48 --- /dev/null +++ b/lib/test/structuredfield/isId3Footer.test.ts @@ -0,0 +1,27 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { isId3Footer } from '../../src/id3/util/isId3Footer.js'; + +describe('isId3Footer', () => { + const mockID3Footer = Uint8Array.from([ + 0x33, 0x44, 0x49, 4, 0, 0, 0, 0, 0, 63, 80, 82, 73, 86, 0, 0, 0, 53, 0, 0, + 99, 111, 109, 46, 97, 112, 112, 108, 101, 46, 115, 116, 114, 101, 97, 109, + 105, 110, 103, 46, 116, 114, 97, 110, 115, 112, 111, 114, 116, 83, 116, 114, + 101, 97, 109, 84, 105, 109, 101, 115, 116, 97, 109, 112, 0, 0, 0, 0, 0, 0, + 13, 198, 135, + ]); + const mockID3FooterMissingLeadingByte = mockID3Footer.slice( + 8, + mockID3Footer.length + ); + const mockID3FooterMissingTrailingByte = mockID3Footer.slice( + 0, + mockID3Footer.length - 8 + ); + + it('Properly parses ID3 Footers', () => { + equal(isId3Footer(mockID3Footer, 0), true); + equal(isId3Footer(mockID3FooterMissingLeadingByte, 0), false); + equal(isId3Footer(mockID3FooterMissingTrailingByte, 0), true); + }); +}); diff --git a/lib/test/structuredfield/isId3Header.test.ts b/lib/test/structuredfield/isId3Header.test.ts new file mode 100644 index 00000000..7e4eba30 --- /dev/null +++ b/lib/test/structuredfield/isId3Header.test.ts @@ -0,0 +1,27 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { isId3Header } from '../../src/id3/util/isId3Header.js'; + +describe('isId3Header', () => { + const mockID3Header = Uint8Array.from([ + 73, 68, 51, 4, 0, 0, 0, 0, 0, 63, 80, 82, 73, 86, 0, 0, 0, 53, 0, 0, 99, + 111, 109, 46, 97, 112, 112, 108, 101, 46, 115, 116, 114, 101, 97, 109, 105, + 110, 103, 46, 116, 114, 97, 110, 115, 112, 111, 114, 116, 83, 116, 114, 101, + 97, 109, 84, 105, 109, 101, 115, 116, 97, 109, 112, 0, 0, 0, 0, 0, 0, 13, + 198, 135, + ]); + const mockID3HeaderMissingLeadingByte = mockID3Header.slice( + 8, + mockID3Header.length + ); + const mockID3HeaderMissingTrailingByte = mockID3Header.slice( + 0, + mockID3Header.length - 8 + ); + + it('Properly parses ID3 Headers', () => { + equal(isId3Header(mockID3Header, 0), true); + equal(isId3Header(mockID3HeaderMissingLeadingByte, 0), false); + equal(isId3Header(mockID3HeaderMissingTrailingByte, 0), true); + }); +}); diff --git a/lib/test/structuredfield/readId3Size.test.ts b/lib/test/structuredfield/readId3Size.test.ts new file mode 100644 index 00000000..0c343574 --- /dev/null +++ b/lib/test/structuredfield/readId3Size.test.ts @@ -0,0 +1,10 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { readId3Size } from '../../src/id3/util/readId3Size.js'; + +describe('readId3Size', () => { + const mockID3SizeData = Uint8Array.from([73, 68, 51, 4, 0, 0, 0, 7, 1, 0]); + it('returns Id3 size', () => { + equal(readId3Size(mockID3SizeData, 6), 114688); + }); +}); From 20fcda61f52dfa42344f3bf99ad98afaa4f3a8e9 Mon Sep 17 00:00:00 2001 From: Fernando Cuadro Date: Mon, 5 Feb 2024 14:35:49 -0300 Subject: [PATCH 2/9] added 2 more unit tests Signed-off-by: Felipe Young --- .../decodeId3TextFrame.test.ts | 23 +++++++++++++++++++ lib/test/structuredfield/readId3Size.test.ts | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 lib/test/structuredfield/decodeId3TextFrame.test.ts diff --git a/lib/test/structuredfield/decodeId3TextFrame.test.ts b/lib/test/structuredfield/decodeId3TextFrame.test.ts new file mode 100644 index 00000000..2beb61bb --- /dev/null +++ b/lib/test/structuredfield/decodeId3TextFrame.test.ts @@ -0,0 +1,23 @@ +import { equal } from 'node:assert'; +import { describe, it } from 'node:test'; +import { decodeId3TextFrame } from '../../src/id3/util/decodeId3TextFrame.js'; +import { Id3Frame } from '../../src/id3'; + +describe('decodeId3TextFrame', () => { + it('should decode a TXXX frame', () => { + const frame = { + type: 'TXXX', + data: new Uint8Array([0, 102, 111, 111, 0, 97, 98, 99]), + size: 2, // required by the _decodeTextFrame function + }; + + const testables = { + decodeId3TextFrame: decodeId3TextFrame, + }; + + const result: Id3Frame | undefined = testables.decodeId3TextFrame(frame); + equal(result!.key, 'TXXX'); + equal(result!.info, 'foo'); + equal(result!.data, 'abc'); + }); +}); diff --git a/lib/test/structuredfield/readId3Size.test.ts b/lib/test/structuredfield/readId3Size.test.ts index 0c343574..0d7bfc0e 100644 --- a/lib/test/structuredfield/readId3Size.test.ts +++ b/lib/test/structuredfield/readId3Size.test.ts @@ -5,6 +5,6 @@ import { readId3Size } from '../../src/id3/util/readId3Size.js'; describe('readId3Size', () => { const mockID3SizeData = Uint8Array.from([73, 68, 51, 4, 0, 0, 0, 7, 1, 0]); it('returns Id3 size', () => { - equal(readId3Size(mockID3SizeData, 6), 114688); + equal(readId3Size(mockID3SizeData, 6), 114816); }); }); From 5719fb099afbf8514e82623d4d00606be4f433a9 Mon Sep 17 00:00:00 2001 From: Felipe Young Date: Wed, 21 Feb 2024 14:44:43 -0300 Subject: [PATCH 3/9] remove magic numbers + remove _ Signed-off-by: Felipe Young --- lib/src/id3/getId3Frames.ts | 9 ++++++--- lib/test/structuredfield/decodeId3TextFrame.test.ts | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/src/id3/getId3Frames.ts b/lib/src/id3/getId3Frames.ts index abdc7f5f..f3f7f05f 100644 --- a/lib/src/id3/getId3Frames.ts +++ b/lib/src/id3/getId3Frames.ts @@ -6,6 +6,9 @@ import { isId3Footer } from './util/isId3Footer.js'; import { isId3Header } from './util/isId3Header.js'; import { readId3Size } from './util/readId3Size.js'; +const HEADER_FOOTER_SIZE = 10; +const FRAME_SIZE = 10; + /** * Returns an array of ID3 frames found in all the ID3 tags in the id3Data * @@ -24,10 +27,10 @@ export function getId3Frames(id3Data: Uint8Array): Id3Frame[] { while (isId3Header(id3Data, offset)) { const size = readId3Size(id3Data, offset + 6); // skip past ID3 header - offset += 10; + offset += HEADER_FOOTER_SIZE; const end = offset + size; // loop through frames in the ID3 tag - while (offset + 8 < end) { + while (offset + FRAME_SIZE < end) { const frameData: RawId3Frame = getId3FrameData(id3Data.subarray(offset)); const frame: Id3Frame | undefined = decodeId3Frame(frameData); if (frame) { @@ -39,7 +42,7 @@ export function getId3Frames(id3Data: Uint8Array): Id3Frame[] { } if (isId3Footer(id3Data, offset)) { - offset += 10; + offset += HEADER_FOOTER_SIZE; } } diff --git a/lib/test/structuredfield/decodeId3TextFrame.test.ts b/lib/test/structuredfield/decodeId3TextFrame.test.ts index 2beb61bb..a8db4b2f 100644 --- a/lib/test/structuredfield/decodeId3TextFrame.test.ts +++ b/lib/test/structuredfield/decodeId3TextFrame.test.ts @@ -8,7 +8,7 @@ describe('decodeId3TextFrame', () => { const frame = { type: 'TXXX', data: new Uint8Array([0, 102, 111, 111, 0, 97, 98, 99]), - size: 2, // required by the _decodeTextFrame function + size: 2, // required by the decodeTextFrame function }; const testables = { From 6de590145721c11a01b19b0d7fa15d0a4e880eef Mon Sep 17 00:00:00 2001 From: Felipe Young Date: Wed, 21 Feb 2024 15:10:03 -0300 Subject: [PATCH 4/9] added changes to changelog Signed-off-by: Felipe Young --- CHANGELOG.md | 100 ++++++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e9b3b1d..1c9608a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ # Changelog + All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +