Skip to content

Split any YouTube song into stems

TypeScript

fix: pin export format in the session

David Naviaf75d10f0841e
diff --git a/src/main/export-session.ts b/src/main/export-session.ts
index 1ededd5..6e01738 100644
--- a/src/main/export-session.ts
+++ b/src/main/export-session.ts
@@ -6,12 +6,13 @@ import { sanitizeName } from './library'
// No Electron here on purpose: the dialogs live in the IPC handlers, so this
// stays testable. The user picks the destination once, then the renderer sends
// each rendered WAV; main never trusts a path coming from the renderer.
-type Session = { kind: 'file' | 'folder'; path: string }
+// the format is pinned at begin: the dialog already showed that extension to the user
+type Session = { kind: 'file' | 'folder'; path: string; format: 'wav' | 'aac' }
const sessions = new Map<string, Session>()
-export function openSession(kind: 'file' | 'folder', path: string): string {
+export function openSession(kind: 'file' | 'folder', path: string, format: 'wav' | 'aac'): string {
const token = randomUUID()
- sessions.set(token, { kind, path })
+ sessions.set(token, { kind, path, format })
return token
}
@@ -19,11 +20,11 @@ export async function writeToSession(
token: string,
fileName: string | null,
wav: Uint8Array,
- format: 'wav' | 'aac',
transcode: (src: string, dest: string) => Promise<void>
): Promise<string> {
const session = sessions.get(token)
if (!session) throw new Error('Unknown export session')
+ const format = session.format
const ext = format === 'aac' ? 'm4a' : 'wav'
let dest: string
if (session.kind === 'file') {
diff --git a/src/main/index.ts b/src/main/index.ts
index cb2ce7c..f9dbdb3 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -319,7 +319,7 @@ app.whenReady().then(async () => {
filters: [{ name: format === 'aac' ? 'AAC audio' : 'WAV audio', extensions: [ext] }]
})
if (result.canceled || !result.filePath) return null
- return { token: openSession('file', result.filePath), path: result.filePath }
+ return { token: openSession('file', result.filePath, format), path: result.filePath }
}
const result = await dialog.showOpenDialog({
title: 'Choose export folder',
@@ -329,13 +329,12 @@ app.whenReady().then(async () => {
if (result.canceled || !result.filePaths[0]) return null
const dir = join(result.filePaths[0], safe)
mkdirSync(dir, { recursive: true })
- return { token: openSession('folder', dir), path: dir }
+ return { token: openSession('folder', dir, format), path: dir }
})
ipcMain.handle(
'export:write',
(_e, token: string, fileName: string | null, wav: Uint8Array) => {
- const format = loadSettings().exportFormat
- return writeToSession(token, fileName, wav, format, (src, dest) =>
+ return writeToSession(token, fileName, wav, (src, dest) =>
transcodeToAac(requireExportFfmpeg(), src, dest)
)
}
diff --git a/tests/export-session.test.mjs b/tests/export-session.test.mjs
index c393247..c7d33ee 100644
--- a/tests/export-session.test.mjs
+++ b/tests/export-session.test.mjs
@@ -18,8 +18,8 @@ const noTranscode = async () => assert.fail('transcode must not run for wav')
test('folder session sanitizes the file name and writes identical bytes', async () => {
const dir = mkdtempSync(join(root, 'folder-'))
- const token = openSession('folder', dir)
- const out = await writeToSession(token, 'a/b?', wav, 'wav', noTranscode)
+ const token = openSession('folder', dir, 'wav')
+ const out = await writeToSession(token, 'a/b?', wav, noTranscode)
assert.equal(out, join(dir, 'a-b-.wav'))
assert.deepEqual(new Uint8Array(readFileSync(out)), wav)
closeSession(token)
@@ -27,9 +27,9 @@ test('folder session sanitizes the file name and writes identical bytes', async
test('file session with aac transcodes then removes the temp wav', async () => {
const dir = mkdtempSync(join(root, 'file-'))
- const token = openSession('file', join(dir, 'song.wav'))
+ const token = openSession('file', join(dir, 'song.wav'), 'aac')
let tmp = ''
- const out = await writeToSession(token, null, wav, 'aac', async (src, dest) => {
+ const out = await writeToSession(token, null, wav, async (src, dest) => {
tmp = src
assert.equal(dest, join(dir, 'song.m4a'))
assert.ok(existsSync(src))
@@ -41,9 +41,9 @@ test('file session with aac transcodes then removes the temp wav', async () => {
test('failing transcode cleans the temp wav and propagates', async () => {
const dir = mkdtempSync(join(root, 'fail-'))
- const token = openSession('file', join(dir, 'song.m4a'))
+ const token = openSession('file', join(dir, 'song.m4a'), 'aac')
await assert.rejects(
- writeToSession(token, null, wav, 'aac', async () => { throw new Error('boom') }),
+ writeToSession(token, null, wav, async () => { throw new Error('boom') }),
/boom/
)
assert.deepEqual(readdirSync(dir), [])
@@ -51,11 +51,19 @@ test('failing transcode cleans the temp wav and propagates', async () => {
test('kind and fileName must agree; unknown token rejects', async () => {
const dir = mkdtempSync(join(root, 'mismatch-'))
- const folder = openSession('folder', dir)
- await assert.rejects(writeToSession(folder, null, wav, 'wav', noTranscode), /file name/i)
- const file = openSession('file', join(dir, 'x.wav'))
- await assert.rejects(writeToSession(file, 'x', wav, 'wav', noTranscode), /file name/i)
+ const folder = openSession('folder', dir, 'wav')
+ await assert.rejects(writeToSession(folder, null, wav, noTranscode), /file name/i)
+ const file = openSession('file', join(dir, 'x.wav'), 'wav')
+ await assert.rejects(writeToSession(file, 'x', wav, noTranscode), /file name/i)
closeSession(folder)
- await assert.rejects(writeToSession(folder, 'x', wav, 'wav', noTranscode), /Unknown export session/)
- await assert.rejects(writeToSession('nope', 'x', wav, 'wav', noTranscode), /Unknown export session/)
+ await assert.rejects(writeToSession(folder, 'x', wav, noTranscode), /Unknown export session/)
+ await assert.rejects(writeToSession('nope', 'x', wav, noTranscode), /Unknown export session/)
+})
+
+test('the format pinned at begin decides the output, not the caller', async () => {
+ const dir = mkdtempSync(join(root, 'pinned-'))
+ const token = openSession('folder', dir, 'wav')
+ // a stray 5th argument (old signature) must not switch the format
+ const out = await writeToSession(token, 'a', wav, noTranscode, 'aac')
+ assert.equal(out, join(dir, 'a.wav'))
})