Add BufferTarget.onFinalize, OutputOptions.onFinalize, ConcurrentRunner (#349)

* Add BufferTarget.onFinalize option for awaitable async action on finalize

The `finalized` event fires synchronously and its return value is ignored,
making it unsuitable for use cases where the muxer should wait (e.g.
uploading the buffer to S3, R2, or other object stores that require a
known Content-Length and therefore can't stream via StreamTarget).

Adds a new `BufferTargetOptions` type with an `onFinalize` callback that
the muxer awaits before resolving. Matches the existing callback pattern
used by `HlsOutputFormatOptions.onSegment`, `onMaster`, etc.

When used with PathedTarget, this provides proper backpressure: the next
segment won't start being produced until the previous one has finished
uploading, keeping memory bounded regardless of video length.

- Adds `BufferTargetOptions` type, exported from the package root
- `BufferTarget` constructor now accepts optional options (backward compatible)
- `_finalize()` awaits `onFinalize` before emitting the `finalized` event
- Adds runtime validation for non-function callbacks
- Updates "Upload to a server" docs in writing-hls.md with S3 pattern
- Documents `onFinalize` in writing-media-files.md BufferTarget section
- Adds tests for callback invocation, async awaiting, backward compat, and validation

* Add ConcurrentRunner, add OutputOptions.onFinalize, adjust docs accordingly, clean up tests

* give me more control baby

* forgot to tell about it

* polish guide and js docs

* vanilagy says remove this, docs will reveal it

---------

Co-authored-by: Vanilagy <[email protected]>
This commit is contained in:
Ahmed Rowaihi
2026-04-17 18:42:09 +02:00
committed by GitHub
co-authored by Vanilagy
parent 9a7120f5f4
commit 77e70f9090
10 changed files with 544 additions and 13 deletions
+33 -1
View File
@@ -89,6 +89,22 @@ export abstract class Target extends EventEmitter<TargetEvents> {
const ARRAY_BUFFER_INITIAL_SIZE = 2 ** 16;
const ARRAY_BUFFER_MAX_SIZE = 2 ** 32;
/**
* Options for {@link BufferTarget}.
* @group Output targets
* @public
*/
export type BufferTargetOptions = {
/**
* Called once the target has been finalized, with the complete output buffer. If you return a promise, it will be
* used to apply backpressure internally.
*
* One use for this callback is for uploading to a server where the full buffer must be known before
* sending (e.g. S3 PutObject) and stream-uploading is not an option.
*/
onFinalize?: (buffer: ArrayBuffer) => MaybePromise<unknown>;
};
/**
* A target that writes data directly into an ArrayBuffer in memory. Great for performance, but not suitable for very
* large files. The buffer will be available once the output has been finalized.
@@ -107,11 +123,22 @@ export class BufferTarget extends Target {
_maxPos = 0;
/** @internal */
_supportsResize: boolean;
/** @internal */
_options: BufferTargetOptions;
/** Creates a new {@link BufferTarget}. The buffer holding the data will be created and managed internally. */
constructor() {
constructor(options: BufferTargetOptions = {}) {
super();
if (!options || typeof options !== 'object') {
throw new TypeError('BufferTarget options, when provided, must be an object.');
}
if (options.onFinalize !== undefined && typeof options.onFinalize !== 'function') {
throw new TypeError('options.onFinalize, when provided, must be a function.');
}
this._options = options;
this._supportsResize = 'resize' in new ArrayBuffer(0);
if (this._supportsResize) {
try {
@@ -178,6 +205,11 @@ export class BufferTarget extends Target {
/** @internal */
async _finalize() {
this.buffer = this._buffer.slice(0, this._maxPos);
if (this._options.onFinalize) {
await this._options.onFinalize(this.buffer);
}
this._emit('finalized');
}