Skip to content

Split any YouTube song into stems

TypeScript

fix(export): expire export sessions after 10 minutes idle

David Navia2efce9814ebc
diff --git a/src/main/export-session.ts b/src/main/export-session.ts
index 9843ce3..5a26057 100644
--- a/src/main/export-session.ts
+++ b/src/main/export-session.ts
@@ -7,8 +7,25 @@ import { sanitizeName } from './library'
// stays testable. The user picks the destination once, then the renderer sends
// each rendered WAV; main never trusts a path coming from the renderer.
// 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' }
+type Session = { kind: 'file' | 'folder'; path: string; format: 'wav' | 'aac'; expiresAt: number }
const sessions = new Map<string, Session>()
+// a renderer that crashes mid-export never sends export:end, so an idle
+// session expires; every write pushes the deadline back
+export const SESSION_TTL_MS = 10 * 60_000
+
+function liveSession(token: string): Session | undefined {
+ const session = sessions.get(token)
+ if (session && session.expiresAt <= Date.now()) {
+ sessions.delete(token)
+ return undefined
+ }
+ return session
+}
+
+// for tests: how many sessions main still holds
+export function openSessionCount(): number {
+ return sessions.size
+}
// "Show in folder" takes a path from the renderer, so main only reveals what an
// export actually wrote this run (the file, or the folder of a separate export)
const revealable = new Set<string>()
@@ -18,8 +35,10 @@ export function canReveal(path: unknown): path is string {
}
export function openSession(kind: 'file' | 'folder', path: string, format: 'wav' | 'aac'): string {
+ const now = Date.now()
+ for (const [t, s] of sessions) if (s.expiresAt <= now) sessions.delete(t)
const token = randomUUID()
- sessions.set(token, { kind, path, format })
+ sessions.set(token, { kind, path, format, expiresAt: now + SESSION_TTL_MS })
return token
}
@@ -29,8 +48,9 @@ export async function writeToSession(
wav: Uint8Array,
transcode: (src: string, dest: string) => Promise<void>
): Promise<string> {
- const session = sessions.get(token)
+ const session = liveSession(token)
if (!session) throw new Error('Unknown export session')
+ session.expiresAt = Date.now() + SESSION_TTL_MS
const format = session.format
const ext = format === 'aac' ? 'm4a' : 'wav'
let dest: string
diff --git a/tests/export-session.test.mjs b/tests/export-session.test.mjs
index 123cdeb..8e8d14c 100644
--- a/tests/export-session.test.mjs
+++ b/tests/export-session.test.mjs
@@ -1,4 +1,4 @@
-import { test, after } from 'node:test'
+import { test, after, mock } from 'node:test'
import assert from 'node:assert/strict'
import { registerTsHooks } from './helpers/ts-resolve.mjs'
import { mkdtempSync, readFileSync, existsSync, readdirSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'
@@ -8,7 +8,7 @@ import { join } from 'node:path'
const root = mkdtempSync(join(tmpdir(), 'stemkit-session-'))
after(() => rmSync(root, { recursive: true, force: true }))
const hooks = registerTsHooks({ stubEnv: `export const userDataDir = () => ${JSON.stringify(root)}` })
-const { openSession, writeToSession, closeSession, canReveal } = await import(
+const { openSession, writeToSession, closeSession, canReveal, SESSION_TTL_MS, openSessionCount } = await import(
new URL('../src/main/export-session.ts', import.meta.url).href
)
hooks.deregister()
@@ -124,3 +124,32 @@ test('reveal refuses anything that is not a plain string path', () => {
assert.equal(canReveal(undefined), false)
assert.equal(canReveal({ toString: () => '/x.wav' }), false)
})
+
+test('a session idle past the TTL expires; a write keeps it alive', async (t) => {
+ mock.timers.enable({ apis: ['Date'], now: Date.now() })
+ t.after(() => mock.timers.reset())
+ assert.equal(SESSION_TTL_MS, 10 * 60_000)
+ const dir = mkdtempSync(join(root, 'ttl-'))
+ const live = openSession('folder', dir, 'wav')
+ const idle = openSession('folder', dir, 'wav')
+ mock.timers.tick(SESSION_TTL_MS - 1)
+ await writeToSession(live, 'a', wav, noTranscode)
+ mock.timers.tick(2)
+ await assert.rejects(writeToSession(idle, 'b', wav, noTranscode), /Unknown export session/)
+ await writeToSession(live, 'c', wav, noTranscode)
+ closeSession(live)
+})
+
+test('opening a session sweeps the ones a crashed renderer left behind', (t) => {
+ mock.timers.enable({ apis: ['Date'], now: Date.now() })
+ t.after(() => mock.timers.reset())
+ const dir = mkdtempSync(join(root, 'sweep-'))
+ const before = openSessionCount()
+ openSession('folder', dir, 'wav')
+ openSession('folder', dir, 'wav')
+ assert.equal(openSessionCount(), before + 2)
+ mock.timers.tick(SESSION_TTL_MS + 1)
+ const fresh = openSession('folder', dir, 'wav')
+ assert.equal(openSessionCount(), 1)
+ closeSession(fresh)
+})