mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-09-28 11:23:45 +02:00
MORE CODE REVIEW
This commit is contained in:
@@ -288,6 +288,10 @@ export const createAes128CbcDecryptStream = (
|
||||
} else {
|
||||
// This is the last chunk
|
||||
const paddingLength = output[bytesToRead - 1]!;
|
||||
if (paddingLength === 0 || paddingLength > 16) {
|
||||
throw new Error('Invalid PKCS#7 padding. Incorrect key or corrupted data.');
|
||||
}
|
||||
|
||||
const trimmedOutput = output.subarray(0, bytesToRead - paddingLength); // PKCS#7 padding
|
||||
|
||||
controller.enqueue(trimmedOutput);
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
TAG_PROGRAM_DATE_TIME,
|
||||
TAG_TARGETDURATION,
|
||||
} from './hls-misc';
|
||||
import { HlsInputFormat } from '../input-format';
|
||||
|
||||
const IV_STRING_REGEX = /^0[xX][0-9a-fA-F]+$/;
|
||||
|
||||
@@ -603,7 +604,8 @@ export class HlsSegmentedInput extends SegmentedInput {
|
||||
return ref!;
|
||||
},
|
||||
),
|
||||
formats: this.input._formats,
|
||||
// Do not allow recursive HLS. Cool on paper, but allows for nasty infinite-depth request trees.
|
||||
formats: this.input._formats.filter(x => !(x instanceof HlsInputFormat)),
|
||||
initInput: initInput ?? undefined,
|
||||
});
|
||||
|
||||
|
||||
+3
-1
@@ -64,7 +64,9 @@ let inputFinalizationRegistry: FinalizationRegistry<SourceRef[]> | null = null;
|
||||
if (typeof FinalizationRegistry !== 'undefined') {
|
||||
inputFinalizationRegistry = new FinalizationRegistry((refs) => {
|
||||
for (const ref of refs) {
|
||||
ref.free();
|
||||
if (!ref.freed) {
|
||||
ref.free();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+7
-1
@@ -1219,8 +1219,14 @@ export class EventEmitter<TEvents extends Record<string, unknown>> {
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const entry of listeners) {
|
||||
(entry.fn as (data: unknown) => void)(data);
|
||||
try {
|
||||
(entry.fn as (data: unknown) => void)(data);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
if (entry.once) {
|
||||
listeners.delete(entry);
|
||||
}
|
||||
|
||||
@@ -388,91 +388,3 @@ export const readAllLines = (slice: FileSlice, length: number, options?: {
|
||||
|
||||
return lines;
|
||||
};
|
||||
|
||||
export class LineReader {
|
||||
getReader: () => MaybePromise<Reader>;
|
||||
ignore?: (line: string) => boolean;
|
||||
reader: Reader | null = null;
|
||||
textDecoder = new TextDecoder();
|
||||
currentLineNumber = 0; // 1-based
|
||||
readPos = 0;
|
||||
reachedEnd = false;
|
||||
lineBuffer = '';
|
||||
|
||||
constructor(getReader: () => MaybePromise<Reader>, ignore?: (line: string) => boolean) {
|
||||
this.getReader = getReader;
|
||||
this.ignore = ignore;
|
||||
}
|
||||
|
||||
readNextLine(): MaybePromise<string | null> {
|
||||
if (this.reachedEnd) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const line = this.extractLineFromBuffer();
|
||||
if (line !== null) {
|
||||
return line;
|
||||
}
|
||||
|
||||
return (async () => {
|
||||
if (!this.reader) {
|
||||
let reader = this.getReader();
|
||||
if (reader instanceof Promise) reader = await reader;
|
||||
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
let slice = this.reader.requestSliceRange(this.readPos, 0, 1024);
|
||||
if (slice instanceof Promise) slice = await slice;
|
||||
|
||||
if (!slice || slice.length === 0) {
|
||||
this.reachedEnd = true;
|
||||
const line = this.lineBuffer.trim();
|
||||
this.lineBuffer = '';
|
||||
|
||||
if (line) {
|
||||
this.currentLineNumber++;
|
||||
}
|
||||
|
||||
if (!line || this.ignore?.(line)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
const bytes = readBytes(slice, slice.length);
|
||||
this.readPos += bytes.length;
|
||||
|
||||
this.lineBuffer += this.textDecoder.decode(bytes, { stream: true });
|
||||
|
||||
const line = this.extractLineFromBuffer();
|
||||
if (line !== null) {
|
||||
return line;
|
||||
}
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
extractLineFromBuffer() {
|
||||
assert(!this.reachedEnd);
|
||||
|
||||
while (true) {
|
||||
const newlineIndex = this.lineBuffer.indexOf('\n');
|
||||
if (newlineIndex === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const line = this.lineBuffer.slice(0, newlineIndex).trim();
|
||||
this.lineBuffer = this.lineBuffer.slice(newlineIndex + 1);
|
||||
this.currentLineNumber++;
|
||||
|
||||
if (this.ignore?.(line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -233,7 +233,9 @@ export class SourceRef<S extends Source = Source> implements Disposable {
|
||||
* Calls {@link SourceRef.free}.
|
||||
*/
|
||||
[Symbol.dispose]() {
|
||||
this.free();
|
||||
if (!this.freed) {
|
||||
this.free();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user