-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathstream.js
More file actions
34 lines (31 loc) · 1.02 KB
/
Copy pathstream.js
File metadata and controls
34 lines (31 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import https from 'node:https';
import { inspect } from 'node:util';
import { parseStream } from '../../lib/index.js'; // music-metadata
const audioUrl = 'https://github.com/Borewit/music-metadata/raw/master/test/samples/MusicBrainz%20-%20Beth%20Hart%20-%20Sinner\'s%20Prayer%20%5Bid3v2.3%5D.V2.mp3';
function httpGet (url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
switch (res.statusCode) {
case 200:
resolve(res);
break;
case 302: // redirect
resolve(httpGet(res.headers.location));
break;
default:
reject(new Error(`Unexpected status-code:${res.statusCode}`));
}
});
});
}
(async () => {
try {
// Stream MP3 sample file from GitHub via HTTP
const stream = await httpGet(audioUrl);
const metadata = await parseStream(stream);
console.log(inspect(metadata, { showHidden: false, depth: null }));
} catch(error) {
// Oops, something went wrong
console.error(error.message);
}
})();