Skip to content

Commit

Permalink
Improve web-stream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Feb 17, 2025
1 parent ad6ac15 commit 98a78ae
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 96 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"del-cli": "^6.0.0",
"mime": "^4.0.6",
"mocha": "^11.1.0",
"node-readable-to-web-readable-stream": "^0.3.1",
"remark-cli": "^12.0.1",
"remark-preset-lint-consistent": "^6.0.1",
"ts-node": "^10.9.2",
Expand Down
15 changes: 7 additions & 8 deletions test/metadata-parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IParser {
description: string;
webStream?: true;
randomRead?: true
initParser: ParseFileMethod;
parse: ParseFileMethod;
}

const [nodeMajorVersion] = process.versions.node.split('.').map(Number);
Expand All @@ -22,12 +22,12 @@ export const Parsers: IParser[] = [
{
description: 'parseFile',
randomRead: true,
initParser: (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
parse: (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
return mm.parseFile(filePath, options);
}
}, {
description: 'parseStream (Node.js)',
initParser: async (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
parse: async (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
const nodeStream = fs.createReadStream(filePath);
try {
return await mm.parseStream(nodeStream, {mimeType: mimeType}, options);
Expand All @@ -38,19 +38,18 @@ export const Parsers: IParser[] = [
}, {
description: 'parseWebStream',
webStream: true,
initParser: async (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
parse: async (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
const webStream = await makeReadableByteFileStream(filePath);
try {
return await mm.parseWebStream(webStream.stream, {mimeType: mimeType, size: webStream.fileSize}, options);
} finally {
await webStream.stream.cancel()
await webStream.closeFile();
await webStream.stream.cancel();
}
}
}, {
description: 'parseBlob',
webStream: true,
initParser: (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
parse: (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
if (nodeMajorVersion < 20) {
skipTest();
}
Expand All @@ -60,7 +59,7 @@ export const Parsers: IParser[] = [
}, {
description: 'parseBuffer',
randomRead: true,
initParser: (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
parse: (skipTest, filePath: string, mimeType?: string, options?: IOptions) => {
const buffer = fs.readFileSync(filePath);
const array = new Uint8Array(buffer);
return mm.parseBuffer(array, {mimeType}, options);
Expand Down
4 changes: 2 additions & 2 deletions test/test-file-aac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Parse ADTS/AAC', () => {

Parsers.forEach(parser => {
it(parser.description, async function (){
const {format} = await parser.initParser(() => this.skip(), path.join(aacSamplePath, 'adts-mpeg4.aac'), 'audio/aac', {
const {format} = await parser.parse(() => this.skip(), path.join(aacSamplePath, 'adts-mpeg4.aac'), 'audio/aac', {
duration: true
});
checkFormat(format, 'ADTS/MPEG-4', 'AAC', 'AAC LC', 16000, 1, 20399, 256000);
Expand All @@ -38,7 +38,7 @@ describe('Parse ADTS/AAC', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const {format} = await parser.initParser(() => this.skip(), path.join(aacSamplePath, 'adts-mpeg4-2.aac'), 'audio/aac', {
const {format} = await parser.parse(() => this.skip(), path.join(aacSamplePath, 'adts-mpeg4-2.aac'), 'audio/aac', {
duration: true
});
checkFormat(format, 'ADTS/MPEG-4', 'AAC', 'AAC LC', 44100, 2, 128000, 14336);
Expand Down
16 changes: 8 additions & 8 deletions test/test-file-aiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {
it(parser.description, async function(){
// AIFF file, AIFF file, stereo 8-bit data
// Source: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/AIFF/Samples.html
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'M1F1-int8-AFsp.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'M1F1-int8-AFsp.aif'), 'audio/aiff');
checkFormat(format, 'PCM', 8000, 2, 8, 23493);
});
});
Expand All @@ -42,7 +42,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {
it(parser.description, async function(){
// AIFF-C file, stereo A-law data (compression type: alaw)
// Source: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/AIFF/Samples.html
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'M1F1-AlawC-AFsp.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'M1F1-AlawC-AFsp.aif'), 'audio/aiff');
checkFormat(format, 'Alaw 2:1', 8000, 2, 16, 23493);
});
});
Expand All @@ -67,7 +67,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'Pmiscck.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'Pmiscck.aif'), 'audio/aiff');
checkFormat(format, ULAW, 8000, 1, 16, 9);
});
});
Expand All @@ -77,7 +77,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'Pnossnd.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'Pnossnd.aif'), 'audio/aiff');
checkFormat(format, ULAW, 8000, 1, 16, 0);
});
});
Expand All @@ -87,7 +87,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'Poffset.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'Poffset.aif'), 'audio/aiff');
checkFormat(format, ULAW, 8000, 1, 16, 9);
});
});
Expand All @@ -97,7 +97,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'Porder.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'Porder.aif'), 'audio/aiff');
checkFormat(format, ULAW, 8000, 1, 16, 9);
});
});
Expand All @@ -107,7 +107,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'Ptjunk.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'Ptjunk.aif'), 'audio/aiff');
checkFormat(format, ULAW, 8000, 1, 16, 9);
});
});
Expand All @@ -117,7 +117,7 @@ describe('Parse AIFF (Audio Interchange File Format)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), path.join(aiffSamplePath, 'Fnonull.aif'), 'audio/aiff');
const { format } = await parser.parse(() => this.skip(), path.join(aiffSamplePath, 'Fnonull.aif'), 'audio/aiff');
checkFormat(format, ULAW, 8000, 1, 16, 9);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/test-file-ape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Parse APE (Monkey\'s Audio)', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const {format, common, native} = await parser.initParser(() => this.skip(), path.join(samplePath, 'monkeysaudio.ape'), 'audio/ape');
const {format, common, native} = await parser.parse(() => this.skip(), path.join(samplePath, 'monkeysaudio.ape'), 'audio/ape');
checkFormat(format);
checkCommon(common);
assert.isDefined(native, 'metadata.native should be defined');
Expand Down
4 changes: 2 additions & 2 deletions test/test-file-asf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('Parse ASF', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const {format, common, native} = await parser.initParser(() => this.skip(), path.join(asfFilePath, 'asf.wma'), 'audio/x-ms-wma');
const {format, common, native} = await parser.parse(() => this.skip(), path.join(asfFilePath, 'asf.wma'), 'audio/x-ms-wma');
checkFormat(format);
checkCommon(common);
assert.isDefined(native, 'metadata.native');
Expand All @@ -124,7 +124,7 @@ describe('Parse ASF', () => {
Parsers.forEach(parser => {
it(parser.description, async function(){
const filePath = path.join(asfFilePath, 'issue_57.wma');
const {native} = await parser.initParser(() => this.skip(), filePath, 'audio/x-ms-wma');
const {native} = await parser.parse(() => this.skip(), filePath, 'audio/x-ms-wma');
const asf = mm.orderTags(native.asf);
assert.exists(asf['WM/Picture'][0], 'ASF WM/Picture should be set');
const nativePicture = asf['WM/Picture'][0];
Expand Down
8 changes: 4 additions & 4 deletions test/test-file-flac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('Parse FLAC Vorbis comment', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const {format, common, native} = await parser.initParser(() => this.skip(), path.join(samplePath, 'flac.flac'), 'audio/flac');
const {format, common, native} = await parser.parse(() => this.skip(), path.join(samplePath, 'flac.flac'), 'audio/flac');
checkFormat(format);
checkCommon(common);
checkNative(mm.orderTags(native.vorbis));
Expand All @@ -75,7 +75,7 @@ describe('Parse FLAC Vorbis comment', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const {format} = await parser.initParser(() => this.skip(), filePath, 'audio/flac');
const {format} = await parser.parse(() => this.skip(), filePath, 'audio/flac');
t.deepEqual(format.tagTypes, ['ID3v2.3', 'vorbis', 'ID3v1'], 'File has 3 tag types: "vorbis", "ID3v2.3" & "ID3v1"');
});
});
Expand All @@ -88,7 +88,7 @@ describe('Parse FLAC Vorbis comment', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const {format} = await parser.initParser(() => this.skip(), filePath, 'audio/flac');
const {format} = await parser.parse(() => this.skip(), filePath, 'audio/flac');
assert.approximately(496000, format.bitrate, 500);
});
});
Expand All @@ -107,7 +107,7 @@ describe('Parse FLAC Vorbis comment', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
return parser.initParser(() => this.skip(), tmpFilePath, 'audio/flac').then(() => {
return parser.parse(() => this.skip(), tmpFilePath, 'audio/flac').then(() => {
t.fail('Should reject');
fs.unlinkSync(tmpFilePath);
}).catch(err => {
Expand Down
4 changes: 2 additions & 2 deletions test/test-file-mp3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ describe('Parse MP3 files', () => {
.filter(parser => !parser.webStream)
.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: false});
const { format } = await parser.parse(() => this.skip(), filePath, 'audio/mpeg', {duration: false});
assert.isUndefined(format.duration, 'Don\'t expect a duration');
});
});
Expand All @@ -267,7 +267,7 @@ describe('Parse MP3 files', () => {
.filter(parser => !parser.webStream)
.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: true});
const { format } = await parser.parse(() => this.skip(), filePath, 'audio/mpeg', {duration: true});
assert.approximately(format.duration, durationSleepAwayMp3, 1 / 10, 'Expect a duration');
assert.strictEqual(format.numberOfSamples, 8831232, 'format.numberOfSamples');
});
Expand Down
16 changes: 8 additions & 8 deletions test/test-file-mp4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'id4.m4a');

const { native, format, common } = await parser.initParser(() => this.skip(), filePath, 'audio/mp4');
const { native, format, common } = await parser.parse(() => this.skip(), filePath, 'audio/mp4');
assert.ok(native, 'Native m4a tags should be present');

checkFormat(format);
Expand All @@ -94,7 +94,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'issue-74.m4a');

const {format, common, native} = await parser.initParser(() => this.skip(), filePath, 'audio/mp4');
const {format, common, native} = await parser.parse(() => this.skip(), filePath, 'audio/mp4');

assert.deepEqual(format.container, 'isom/iso2/mp41', 'format.container');
assert.deepEqual(format.codec, 'MPEG-4/AAC', 'format.codec');
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'issue-79.m4a');

const {common, format} = await parser.initParser(() => this.skip(), filePath, 'audio/mp4');
const {common, format} = await parser.parse(() => this.skip(), filePath, 'audio/mp4');

assert.deepEqual(format.container, 'M4A/mp42/isom', 'format.container');
assert.deepEqual(format.codec, 'MPEG-4/AAC', 'format.codec');
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'issue-127.m4b');

const { common, format, native} = await parser.initParser(() => this.skip(), filePath, 'audio/mp4');
const { common, format, native} = await parser.parse(() => this.skip(), filePath, 'audio/mp4');

assert.deepEqual(format.container, 'M4A/3gp5/isom', 'format.container');
assert.deepEqual(format.codec, 'MPEG-4/AAC', 'format.codec');
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'Mr. Pickles S02E07 My Dear Boy.mp4');

const { common, format, native } = await parser.initParser(() => this.skip(), filePath, 'video/mp4');
const { common, format, native } = await parser.parse(() => this.skip(), filePath, 'video/mp4');
assert.deepEqual(common.title, 'My Dear Boy');
assert.deepEqual(common.tvEpisode, 7);
assert.deepEqual(common.tvEpisodeId, '017');
Expand Down Expand Up @@ -320,7 +320,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'issue-133.m4a');

const { format } = await parser.initParser(() => this.skip(), filePath, 'video/mp4');
const { format } = await parser.parse(() => this.skip(), filePath, 'video/mp4');
assert.deepEqual(format.container, 'M4A/mp42/isom', 'format.container');
assert.deepEqual(format.codec, 'MPEG-4/AAC', 'format.codec');
});
Expand All @@ -336,7 +336,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {

const filePath = path.join(mp4Samples, 'issue-151.m4a');

const { format, common } = await parser.initParser(() => this.skip(), filePath, 'video/mp4');
const { format, common } = await parser.parse(() => this.skip(), filePath, 'video/mp4');
assert.deepEqual(format.container, 'mp42/isom', 'format.container');
assert.deepEqual(format.codec, 'MPEG-4/AAC+MP4S', 'format.codec');

Expand All @@ -360,7 +360,7 @@ describe('Parse MPEG-4 files with iTunes metadata', () => {
it(parser.description, async function(){
const filePath = path.join(mp4Samples, '01. Trumpsta (Djuro Remix).m4a');

const { format, common } = await parser.initParser(() => this.skip(), filePath, 'audio/m4a');
const { format, common } = await parser.parse(() => this.skip(), filePath, 'audio/m4a');

assert.deepEqual(format.container, 'M4A/mp42/isom', 'format.container');
assert.deepEqual(format.codec, 'MPEG-4/AAC', 'format.codec');
Expand Down
2 changes: 1 addition & 1 deletion test/test-file-mpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe('Parse MPEG', () => {
Parsers
.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: false});
const { format } = await parser.parse(() => this.skip(), filePath, 'audio/mpeg', {duration: false});
assert.strictEqual(format.duration, 0.4963265306122449);
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/test-file-musepack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Parse Musepack (.mpc)', () => {
Parsers.forEach(parser => {
it(parser.description, async function(){

const { format, common } = await parser.initParser(() => this.skip(), filePath, 'audio/musepac');
const { format, common } = await parser.parse(() => this.skip(), filePath, 'audio/musepac');
// Check format
assert.deepEqual(format.container, 'Musepack, SV7');
assert.strictEqual(format.sampleRate, 44100);
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Parse Musepack (.mpc)', () => {
Parsers.forEach(parser => {
it(parser.description, async function(){

const { format, common } = await parser.initParser(() => this.skip(), filePath, 'audio/musepac');
const { format, common } = await parser.parse(() => this.skip(), filePath, 'audio/musepac');
// Check format
assert.deepEqual(format.container, 'Musepack, SV7');
assert.strictEqual(format.sampleRate, 44100);
Expand All @@ -74,7 +74,7 @@ describe('Parse Musepack (.mpc)', () => {
Parsers.forEach(parser => {
it(parser.description, async function(){

const { format, common } = await parser.initParser(() => this.skip(), filePath, 'audio/musepac');
const { format, common } = await parser.parse(() => this.skip(), filePath, 'audio/musepac');
// Check format
assert.deepEqual(format.container, 'Musepack, SV8');
assert.strictEqual(format.sampleRate, 48000);
Expand Down
6 changes: 3 additions & 3 deletions test/test-file-ogg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Parse Ogg', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format, native, common } = await parser.initParser(() => this.skip(), filePath, 'audio/ogg');
const { format, native, common } = await parser.parse(() => this.skip(), filePath, 'audio/ogg');
checkFormat(format);
check_Nirvana_In_Bloom_VorbisTags(mm.orderTags(native.vorbis));
check_Nirvana_In_Bloom_commonTags(common);
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('Parse Ogg', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format, native, common } = await parser.initParser(() => this.skip(), filePath, 'audio/ogg');
const { format, native, common } = await parser.parse(() => this.skip(), filePath, 'audio/ogg');
checkFormat(format);
check_Nirvana_In_Bloom_VorbisTags(mm.orderTags(native.vorbis));
check_Nirvana_In_Bloom_commonTags(common);
Expand All @@ -171,7 +171,7 @@ describe('Parse Ogg', () => {

Parsers.forEach(parser => {
it(parser.description, async function(){
const { format } = await parser.initParser(() => this.skip(), filePath, 'audio/ogg');
const { format } = await parser.parse(() => this.skip(), filePath, 'audio/ogg');
checkFormat(format);
});
});
Expand Down
Loading

0 comments on commit 98a78ae

Please sign in to comment.