Add SourceRef system to keep track of used Sources (fixes #332)

This commit is contained in:
Vanilagy
2026-03-20 14:46:12 +01:00
parent 587e98391e
commit c91718cdba
8 changed files with 277 additions and 136 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest';
import { Reader } from '../../src/reader.js';
import { BufferSource } from '../../src/source.js';
import { createAesDecryptStream } from '../../src/aes.js';
import { createAes128CbcDecryptStream } from '../../src/aes.js';
// getRandomValues is length-limited, so let's just do this
export const fillRandom = <T extends Uint8Array>(buffer: T) => {
@@ -27,7 +27,7 @@ test('createAesDecryptStream', async () => {
const source = new BufferSource(ciphertext);
const reader = new Reader(source);
const stream = createAesDecryptStream(reader, () => ({ key, iv }));
const stream = createAes128CbcDecryptStream(reader, () => ({ key, iv }));
const streamReader = stream.getReader();
const chunks: Uint8Array[] = [];
+56
View File
@@ -0,0 +1,56 @@
import { expect, test } from 'vitest';
import { FilePathSource } from '../../src/source.js';
import path from 'node:path';
import { Input } from '../../src/input.js';
import { ALL_FORMATS, MP4 } from '../../src/input-format.js';
const __dirname = new URL('.', import.meta.url).pathname;
test('Direct source disposal', async () => {
const filePath = path.join(__dirname, '../public/video.mp4');
const source = new FilePathSource(filePath);
expect(!source._disposed);
const ref = source.ref();
ref.free();
expect(source._disposed);
});
test('Implicit source disposal', async () => {
const filePath = path.join(__dirname, '../public/video.mp4');
const source = new FilePathSource(filePath);
const input = new Input({
source,
formats: ALL_FORMATS,
});
expect(await input.getFormat()).toBe(MP4);
expect(!source._disposed);
input.dispose();
expect(source._disposed);
});
test('Implicit source disposal, double input', async () => {
const filePath = path.join(__dirname, '../public/video.mp4');
const source = new FilePathSource(filePath);
const input1 = new Input({
source,
formats: ALL_FORMATS,
});
const input2 = new Input({
source,
formats: ALL_FORMATS,
});
expect(await input1.getFormat()).toBe(MP4);
expect(await input2.getFormat()).toBe(MP4);
expect(!source._disposed);
input1.dispose();
expect(!source._disposed);
input2.dispose();
expect(source._disposed);
});