Skip to content

Commit

Permalink
Validate target in the options
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanilagy committed Sep 19, 2024
1 parent db1b9c4 commit 6074e27
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 155 deletions.
21 changes: 12 additions & 9 deletions build/webm-muxer.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 46 additions & 59 deletions build/webm-muxer.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions build/webm-muxer.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions build/webm-muxer.min.mjs

Large diffs are not rendered by default.

107 changes: 46 additions & 61 deletions build/webm-muxer.mjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webm-muxer",
"version": "5.0.1",
"version": "5.0.2",
"description": "WebM multiplexer in pure TypeScript with support for WebCodecs API, video & audio.",
"main": "./build/webm-muxer.js",
"module": "./build/webm-muxer.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Muxer } from './muxer';
export { SubtitleEncoder } from './subtitles';
export * from './target';
export { ArrayBufferTarget, StreamTarget, FileSystemWritableFileStreamTarget } from './target';
6 changes: 5 additions & 1 deletion src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const CODEC_PRIVATE_MAX_SIZE = 2**12;
const APP_NAME = 'https://github.com/Vanilagy/webm-muxer';
const SEGMENT_SIZE_BYTES = 6;
const CLUSTER_SIZE_BYTES = 5;
const FIRST_TIMESTAMP_BEHAVIORS = ['strict', 'offset', 'permissive'] as const;
const FIRST_TIMESTAMP_BEHAVIORS = ['strict', 'offset', 'permissive'] as const;

interface MuxerOptions<T extends Target> {
target: T,
Expand Down Expand Up @@ -136,6 +136,10 @@ export class Muxer<T extends Target> {
throw new TypeError('The muxer requires an options object to be passed to its constructor.');
}

if (!(options.target instanceof Target)) {
throw new TypeError('The target must be provided and an instance of Target.');
}

if (options.video) {
if (typeof options.video.codec !== 'string') {
throw new TypeError(`Invalid video codec: ${options.video.codec}. Must be a string.`);
Expand Down
16 changes: 12 additions & 4 deletions src/target.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
export type Target = ArrayBufferTarget | StreamTarget | FileSystemWritableFileStreamTarget;
const isTarget = Symbol('isTarget');
export abstract class Target {
// If we didn't add this symbol, then {} would be assignable to Target
[isTarget]: true;
}

export class ArrayBufferTarget {
export class ArrayBufferTarget extends Target {
buffer: ArrayBuffer = null;
}

export class StreamTarget {
export class StreamTarget extends Target {
constructor(public options: {
onData?: (data: Uint8Array, position: number) => void,
onHeader?: (data: Uint8Array, position: number) => void,
onCluster?: (data: Uint8Array, position: number, timestamp: number) => void,
chunked?: boolean,
chunkSize?: number
}) {
super();

if (typeof options !== 'object') {
throw new TypeError('StreamTarget requires an options object to be passed to its constructor.');
}
Expand Down Expand Up @@ -44,11 +50,13 @@ export class StreamTarget {
}
}

export class FileSystemWritableFileStreamTarget {
export class FileSystemWritableFileStreamTarget extends Target {
constructor(
public stream: FileSystemWritableFileStream,
public options?: { chunkSize?: number }
) {
super();

if (!(stream instanceof FileSystemWritableFileStream)) {
throw new TypeError('FileSystemWritableFileStreamTarget requires a FileSystemWritableFileStream instance.');
}
Expand Down
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"compilerOptions": {
"target": "ES2015",
"target": "ES2021",
"strict": true,
"strictNullChecks": false,
"noImplicitAny": true,
"noImplicitOverride": true,
"types": ["Node", "@types/wicg-file-system-access", "@types/dom-webcodecs"]
"noImplicitOverride": true
},
"include": [
"src/**/*",
Expand Down

0 comments on commit 6074e27

Please sign in to comment.