diff --git a/js/index.js b/js/index.js index a353dc2f..0fce4e2f 100644 --- a/js/index.js +++ b/js/index.js @@ -3,7 +3,6 @@ import Raven from "raven-js"; import createMiddleware from "raven-for-redux"; import isButterchurnSupported from "butterchurn/lib/isSupported.min"; -import { parse } from "m3u-parser"; import osx from "../skins/MacOSXAqua1-5.wsz"; import topaz from "../skins/TopazAmp1-2.wsz"; import visor from "../skins/Vizor1-01.wsz"; @@ -16,6 +15,7 @@ import "../mp3/test/01 Ghosts I.mp3"; import "../mp3/test/02 Ghosts I.mp3"; import "../mp3/test/03 Ghosts I.mp3"; import internetArchive from "../skins/Internet-Archive.wsz"; +import m3uParser from "./m3uParser"; import MilkdropWindow from "./components/MilkdropWindow"; import screenshotInitialState from "./screenshotInitialState"; import { WINDOWS } from "./constants"; @@ -305,7 +305,7 @@ Raven.context(async () => { const parentDirectory = getParentDirectory(m3u); const resonse = await fetch(m3u); const text = await resonse.text(); - const playlist = await parse(text); + const playlist = m3uParser(text); const m3uTracks = playlist.map(m3uTrack => ({ url: `${parentDirectory}${m3uTrack.file}`, duration: m3uTrack.duration, diff --git a/js/m3uParser.ts b/js/m3uParser.ts new file mode 100644 index 00000000..46f862fd --- /dev/null +++ b/js/m3uParser.ts @@ -0,0 +1,41 @@ +const EXTINF_REGEX = /^#EXTINF:(-?)(\d+),(.*)$/; + +interface M3uTrack { + title: string; + duration: number; + file: string; +} + +export default function parseM3u(content: string): Array { + // TODO: Support Windows line endings + const lines = content.split("\n").filter(Boolean); + const firstLine = lines[0]; + + if (!firstLine || !firstLine.startsWith("#EXTM3U")) { + throw new Error("Content is not a valid M3U playlist"); + } + + const tracks = []; + for (var i = 1; i < lines.length; i++) { + const line = lines[i]; + if (!line.startsWith("#EXTINF")) { + continue; + } + var result = EXTINF_REGEX.exec(line); + if (!result) { + throw new Error(`Invalid EXTINF at line ${i}`); + } + const nextLine = lines[++i]; + if (nextLine == null) { + throw new Error(`Missing track path at line ${i}`); + } + + tracks.push({ + title: result[3].trim(), + duration: Number(result[1] + result[2]), + file: nextLine.trim() + }); + } + + return tracks; +} diff --git a/package.json b/package.json index 81d6c807..868fd435 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,5 @@ ] }, "prettier": {}, - "dependencies": { - "m3u-parser": "^1.0.3" - } + "dependencies": {} } diff --git a/yarn.lock b/yarn.lock index 1628410b..5837e935 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1376,10 +1376,6 @@ block-stream@*: dependencies: inherits "~2.0.0" -bluebird@^3.1.1: - version "3.5.2" - resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.2.tgz#1be0908e054a751754549c270489c1505d4ab15a" - bluebird@^3.5.1: version "3.5.1" resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.1.tgz#d9551f9de98f1fcda1e683d17ee91a0602ee2eb9" @@ -5496,12 +5492,6 @@ lru-cache@^4.1.1: pseudomap "^1.0.2" yallist "^2.1.2" -m3u-parser@^1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/m3u-parser/-/m3u-parser-1.0.3.tgz#220fbdd5e54ccda7c230fcb09b59fd5ab4ba06a3" - dependencies: - bluebird "^3.1.1" - macaddress@^0.2.8: version "0.2.8" resolved "https://registry.yarnpkg.com/macaddress/-/macaddress-0.2.8.tgz#5904dc537c39ec6dbefeae902327135fa8511f12"