Overview
This module provides core type definitions used throughout the WebCodecs API. These types ensure type safety and compatibility with the W3C WebCodecs specification while adapting to Node.js environments.Type Aliases
Common type definitions like BufferSource and CodecState
Classes
DOMRectReadOnly and WebCodecsDOMException classes
Error Handling
DOM exception types with standard error codes
Codec States
State machine for encoder and decoder lifecycles
BufferSource
A union type representing binary data that can be passed to WebCodecs APIs.BufferSource accepts any typed array (Uint8Array, Float32Array, etc.) or raw ArrayBuffer. This is the standard way to pass binary data like encoded video frames or audio samples.Examples
CodecState
Represents the current state of a video or audio encoder/decoder.State Values
Initial state before
configure() is called. The codec cannot process data in this state.Active state after successful
configure() call. The codec is ready to encode or decode data.Terminal state after
close() is called. The codec cannot be used again and resources are released.State Transitions
The codec follows a strict state machine:unconfigured to configured
Call
configure() with a valid configuration object. The codec validates the configuration and prepares internal resources.configured to unconfigured
Call
reset() to return to the unconfigured state. This clears any pending work and allows reconfiguration with different parameters.Usage Example
WebCodecsDOMException
A DOM-style exception class for WebCodecs errors. Extends the standardError class with a numeric code property for compatibility with browser APIs.
Constructor
Optional error message describing what went wrong.
Optional error name (e.g., “NotSupportedError”, “InvalidStateError”). Determines the numeric code.
Instance Properties
The name of the error (e.g., “NotSupportedError”, “InvalidStateError”, “AbortError”).
Numeric error code matching the W3C DOM exception codes.
Human-readable description of the error.
Common Error Codes
The following table lists the error codes most frequently encountered when using WebCodecs:| Code | Name | Constant | Description |
|---|---|---|---|
| 9 | NotSupportedError | NOT_SUPPORTED_ERR | The requested codec or configuration is not supported |
| 11 | InvalidStateError | INVALID_STATE_ERR | Operation attempted in wrong codec state |
| 20 | AbortError | ABORT_ERR | Operation was aborted (e.g., by reset() or close()) |
NotSupportedError (code 9) is thrown when:
- The codec string is invalid or not recognized
- The codec is not available on the current platform
- The configuration parameters are outside supported ranges
InvalidStateError (code 11) is thrown when:
- Calling
encode()/decode()beforeconfigure() - Calling any method on a closed codec
- Calling
configure()on an already closed codec
AbortError (code 20) is thrown when:
reset()is called while operations are pendingclose()is called while operations are pending- The codec encounters an unrecoverable internal error
All Error Codes
Complete DOM Exception Code Reference
Complete DOM Exception Code Reference
| Code | Constant | Description |
|---|---|---|
| 1 | INDEX_SIZE_ERR | Index out of bounds |
| 2 | DOMSTRING_SIZE_ERR | String size error (deprecated) |
| 3 | HIERARCHY_REQUEST_ERR | Invalid hierarchy |
| 4 | WRONG_DOCUMENT_ERR | Wrong document |
| 5 | INVALID_CHARACTER_ERR | Invalid character |
| 6 | NO_DATA_ALLOWED_ERR | No data allowed (deprecated) |
| 7 | NO_MODIFICATION_ALLOWED_ERR | Modification not allowed |
| 8 | NOT_FOUND_ERR | Not found |
| 9 | NOT_SUPPORTED_ERR | Not supported |
| 10 | INUSE_ATTRIBUTE_ERR | Attribute in use |
| 11 | INVALID_STATE_ERR | Invalid state |
| 12 | SYNTAX_ERR | Syntax error |
| 13 | INVALID_MODIFICATION_ERR | Invalid modification |
| 14 | NAMESPACE_ERR | Namespace error |
| 15 | INVALID_ACCESS_ERR | Invalid access |
| 16 | VALIDATION_ERR | Validation error (deprecated) |
| 17 | TYPE_MISMATCH_ERR | Type mismatch |
| 18 | SECURITY_ERR | Security error |
| 19 | NETWORK_ERR | Network error |
| 20 | ABORT_ERR | Aborted |
| 21 | URL_MISMATCH_ERR | URL mismatch |
| 22 | QUOTA_EXCEEDED_ERR | Quota exceeded |
| 23 | TIMEOUT_ERR | Timeout |
| 24 | INVALID_NODE_TYPE_ERR | Invalid node type |
| 25 | DATA_CLONE_ERR | Data clone error |
Error Handling Example
DOMRectReadOnly
An immutable rectangle class representing position and dimensions. Used byVideoFrame.visibleRect to describe the visible region of a video frame.
Constructor
The x-coordinate of the rectangle’s origin.
The y-coordinate of the rectangle’s origin.
The width of the rectangle.
The height of the rectangle.
Properties
The x-coordinate of the rectangle’s origin (left edge for positive width).
The y-coordinate of the rectangle’s origin (top edge for positive height).
The width of the rectangle.
The height of the rectangle.
The y-coordinate of the top edge (minimum of y and y + height).
The x-coordinate of the right edge (maximum of x and x + width).
The y-coordinate of the bottom edge (maximum of y and y + height).
The x-coordinate of the left edge (minimum of x and x + width).
Methods
toJSON()
Returns a plain object representation of the rectangle.Usage Example
Helper Functions
createDOMException()
Factory function to create aWebCodecsDOMException instance.
Optional error message.
Optional error name that determines the numeric code.
Type Aliases
WebCodecsError
An alias forWebCodecsDOMException for convenience.
DOMException
Re-export ofWebCodecsDOMException for browser API compatibility.