mp3-mediarecorder
Version:
MediaRecorder ponyfill that records audio as mp3
1,559 lines (1,276 loc) • 39.9 kB
JavaScript
;Object.defineProperty(exports,'__esModule',{value:true});/**
* Assert a condition.
* @param condition The condition that it should satisfy.
* @param message The error message.
* @param args The arguments for replacing placeholders in the message.
*/
function assertType(condition, message, ...args) {
if (!condition) {
throw new TypeError(format(message, args));
}
}
/**
* Convert a text and arguments to one string.
* @param message The formating text
* @param args The arguments.
*/
function format(message, args) {
let i = 0;
return message.replace(/%[os]/gu, () => anyToString(args[i++]));
}
/**
* Convert a value to a string representation.
* @param x The value to get the string representation.
*/
function anyToString(x) {
if (typeof x !== "object" || x === null) {
return String(x);
}
return Object.prototype.toString.call(x);
}
let currentErrorHandler;
/**
* Print a error message.
* @param maybeError The error object.
*/
function reportError(maybeError) {
try {
const error = maybeError instanceof Error ? maybeError : new Error(anyToString(maybeError)); // Call the user-defined error handler if exists.
if (currentErrorHandler) ; // Dispatch an `error` event if this is on a browser.
if (typeof dispatchEvent === "function" && typeof ErrorEvent === "function") {
dispatchEvent(new ErrorEvent("error", {
error,
message: error.message
}));
} // Emit an `uncaughtException` event if this is on Node.js.
//istanbul ignore else
else if (typeof process !== "undefined" && typeof process.emit === "function") {
process.emit("uncaughtException", error);
return;
} // Otherwise, print the error.
console.error(error);
} catch (_a) {// ignore.
}
}
/**
* The global object.
*/
//istanbul ignore next
const Global = typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : typeof globalThis !== "undefined" ? globalThis : undefined;
let currentWarnHandler;
/**
* The warning information.
*/
class Warning {
constructor(code, message) {
this.code = code;
this.message = message;
}
/**
* Report this warning.
* @param args The arguments of the warning.
*/
warn(...args) {
var _a;
try {
// Call the user-defined warning handler if exists.
if (currentWarnHandler) ; // Otherwise, print the warning.
const stack = ((_a = new Error().stack) !== null && _a !== void 0 ? _a : "").replace(/^(?:.+?\n){2}/gu, "\n");
console.warn(this.message, ...args, stack);
} catch (_b) {// Ignore.
}
}
}
const InitEventWasCalledWhileDispatching = new Warning("W01", "Unable to initialize event under dispatching.");
const FalsyWasAssignedToCancelBubble = new Warning("W02", "Assigning any falsy value to 'cancelBubble' property has no effect.");
const TruthyWasAssignedToReturnValue = new Warning("W03", "Assigning any truthy value to 'returnValue' property has no effect.");
const NonCancelableEventWasCanceled = new Warning("W04", "Unable to preventDefault on non-cancelable events.");
const CanceledInPassiveListener = new Warning("W05", "Unable to preventDefault inside passive event listener invocation.");
const EventListenerWasDuplicated = new Warning("W06", "An event listener wasn't added because it has been added already: %o, %o");
const OptionWasIgnored = new Warning("W07", "The %o option value was abandoned because the event listener wasn't added as duplicated.");
const InvalidEventListener = new Warning("W08", "The 'callback' argument must be a function or an object that has 'handleEvent' method: %o");
const InvalidAttributeHandler = new Warning("W09", "Event attribute handler must be a function: %o");
/*eslint-disable class-methods-use-this */
/**
* An implementation of `Event` interface, that wraps a given event object.
* `EventTarget` shim can control the internal state of this `Event` objects.
* @see https://dom.spec.whatwg.org/#event
*/
class Event$1 {
/**
* @see https://dom.spec.whatwg.org/#dom-event-none
*/
static get NONE() {
return NONE;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-capturing_phase
*/
static get CAPTURING_PHASE() {
return CAPTURING_PHASE;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-at_target
*/
static get AT_TARGET() {
return AT_TARGET;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-bubbling_phase
*/
static get BUBBLING_PHASE() {
return BUBBLING_PHASE;
}
/**
* Initialize this event instance.
* @param type The type of this event.
* @param eventInitDict Options to initialize.
* @see https://dom.spec.whatwg.org/#dom-event-event
*/
constructor(type, eventInitDict) {
Object.defineProperty(this, "isTrusted", {
value: false,
enumerable: true
});
const opts = eventInitDict !== null && eventInitDict !== void 0 ? eventInitDict : {};
internalDataMap.set(this, {
type: String(type),
bubbles: Boolean(opts.bubbles),
cancelable: Boolean(opts.cancelable),
composed: Boolean(opts.composed),
target: null,
currentTarget: null,
stopPropagationFlag: false,
stopImmediatePropagationFlag: false,
canceledFlag: false,
inPassiveListenerFlag: false,
dispatchFlag: false,
timeStamp: Date.now()
});
}
/**
* The type of this event.
* @see https://dom.spec.whatwg.org/#dom-event-type
*/
get type() {
return $(this).type;
}
/**
* The event target of the current dispatching.
* @see https://dom.spec.whatwg.org/#dom-event-target
*/
get target() {
return $(this).target;
}
/**
* The event target of the current dispatching.
* @deprecated Use the `target` property instead.
* @see https://dom.spec.whatwg.org/#dom-event-srcelement
*/
get srcElement() {
return $(this).target;
}
/**
* The event target of the current dispatching.
* @see https://dom.spec.whatwg.org/#dom-event-currenttarget
*/
get currentTarget() {
return $(this).currentTarget;
}
/**
* The event target of the current dispatching.
* This doesn't support node tree.
* @see https://dom.spec.whatwg.org/#dom-event-composedpath
*/
composedPath() {
const currentTarget = $(this).currentTarget;
if (currentTarget) {
return [currentTarget];
}
return [];
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-none
*/
get NONE() {
return NONE;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-capturing_phase
*/
get CAPTURING_PHASE() {
return CAPTURING_PHASE;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-at_target
*/
get AT_TARGET() {
return AT_TARGET;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-bubbling_phase
*/
get BUBBLING_PHASE() {
return BUBBLING_PHASE;
}
/**
* The current event phase.
* @see https://dom.spec.whatwg.org/#dom-event-eventphase
*/
get eventPhase() {
return $(this).dispatchFlag ? 2 : 0;
}
/**
* Stop event bubbling.
* Because this shim doesn't support node tree, this merely changes the `cancelBubble` property value.
* @see https://dom.spec.whatwg.org/#dom-event-stoppropagation
*/
stopPropagation() {
$(this).stopPropagationFlag = true;
}
/**
* `true` if event bubbling was stopped.
* @deprecated
* @see https://dom.spec.whatwg.org/#dom-event-cancelbubble
*/
get cancelBubble() {
return $(this).stopPropagationFlag;
}
/**
* Stop event bubbling if `true` is set.
* @deprecated Use the `stopPropagation()` method instead.
* @see https://dom.spec.whatwg.org/#dom-event-cancelbubble
*/
set cancelBubble(value) {
if (value) {
$(this).stopPropagationFlag = true;
} else {
FalsyWasAssignedToCancelBubble.warn();
}
}
/**
* Stop event bubbling and subsequent event listener callings.
* @see https://dom.spec.whatwg.org/#dom-event-stopimmediatepropagation
*/
stopImmediatePropagation() {
const data = $(this);
data.stopPropagationFlag = data.stopImmediatePropagationFlag = true;
}
/**
* `true` if this event will bubble.
* @see https://dom.spec.whatwg.org/#dom-event-bubbles
*/
get bubbles() {
return $(this).bubbles;
}
/**
* `true` if this event can be canceled by the `preventDefault()` method.
* @see https://dom.spec.whatwg.org/#dom-event-cancelable
*/
get cancelable() {
return $(this).cancelable;
}
/**
* `true` if the default behavior will act.
* @deprecated Use the `defaultPrevented` proeprty instead.
* @see https://dom.spec.whatwg.org/#dom-event-returnvalue
*/
get returnValue() {
return !$(this).canceledFlag;
}
/**
* Cancel the default behavior if `false` is set.
* @deprecated Use the `preventDefault()` method instead.
* @see https://dom.spec.whatwg.org/#dom-event-returnvalue
*/
set returnValue(value) {
if (!value) {
setCancelFlag($(this));
} else {
TruthyWasAssignedToReturnValue.warn();
}
}
/**
* Cancel the default behavior.
* @see https://dom.spec.whatwg.org/#dom-event-preventdefault
*/
preventDefault() {
setCancelFlag($(this));
}
/**
* `true` if the default behavior was canceled.
* @see https://dom.spec.whatwg.org/#dom-event-defaultprevented
*/
get defaultPrevented() {
return $(this).canceledFlag;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-composed
*/
get composed() {
return $(this).composed;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-istrusted
*/
//istanbul ignore next
get isTrusted() {
return false;
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-timestamp
*/
get timeStamp() {
return $(this).timeStamp;
}
/**
* @deprecated Don't use this method. The constructor did initialization.
*/
initEvent(type, bubbles = false, cancelable = false) {
const data = $(this);
if (data.dispatchFlag) {
InitEventWasCalledWhileDispatching.warn();
return;
}
internalDataMap.set(this, { ...data,
type: String(type),
bubbles: Boolean(bubbles),
cancelable: Boolean(cancelable),
target: null,
currentTarget: null,
stopPropagationFlag: false,
stopImmediatePropagationFlag: false,
canceledFlag: false
});
}
} //------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const NONE = 0;
const CAPTURING_PHASE = 1;
const AT_TARGET = 2;
const BUBBLING_PHASE = 3;
/**
* Private data for event wrappers.
*/
const internalDataMap = new WeakMap();
/**
* Get private data.
* @param event The event object to get private data.
* @param name The variable name to report.
* @returns The private data of the event.
*/
function $(event, name = "this") {
const retv = internalDataMap.get(event);
assertType(retv != null, "'%s' must be an object that Event constructor created, but got another one: %o", name, event);
return retv;
}
/**
* https://dom.spec.whatwg.org/#set-the-canceled-flag
* @param data private data.
*/
function setCancelFlag(data) {
if (data.inPassiveListenerFlag) {
CanceledInPassiveListener.warn();
return;
}
if (!data.cancelable) {
NonCancelableEventWasCanceled.warn();
return;
}
data.canceledFlag = true;
} // Set enumerable
Object.defineProperty(Event$1, "NONE", {
enumerable: true
});
Object.defineProperty(Event$1, "CAPTURING_PHASE", {
enumerable: true
});
Object.defineProperty(Event$1, "AT_TARGET", {
enumerable: true
});
Object.defineProperty(Event$1, "BUBBLING_PHASE", {
enumerable: true
});
const keys = Object.getOwnPropertyNames(Event$1.prototype);
for (let i = 0; i < keys.length; ++i) {
if (keys[i] === "constructor") {
continue;
}
Object.defineProperty(Event$1.prototype, keys[i], {
enumerable: true
});
} // Ensure `event instanceof window.Event` is `true`.
if (typeof Global !== "undefined" && typeof Global.Event !== "undefined") {
Object.setPrototypeOf(Event$1.prototype, Global.Event.prototype);
}
/**
* Create a new InvalidStateError instance.
* @param message The error message.
*/
function createInvalidStateError(message) {
if (Global.DOMException) {
return new Global.DOMException(message, "InvalidStateError");
}
if (DOMException == null) {
DOMException = class DOMException extends Error {
constructor(msg) {
super(msg);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, DOMException);
}
} // eslint-disable-next-line class-methods-use-this
get code() {
return 11;
} // eslint-disable-next-line class-methods-use-this
get name() {
return "InvalidStateError";
}
};
Object.defineProperties(DOMException.prototype, {
code: {
enumerable: true
},
name: {
enumerable: true
}
});
defineErrorCodeProperties(DOMException);
defineErrorCodeProperties(DOMException.prototype);
}
return new DOMException(message);
} //------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
let DOMException;
const ErrorCodeMap = {
INDEX_SIZE_ERR: 1,
DOMSTRING_SIZE_ERR: 2,
HIERARCHY_REQUEST_ERR: 3,
WRONG_DOCUMENT_ERR: 4,
INVALID_CHARACTER_ERR: 5,
NO_DATA_ALLOWED_ERR: 6,
NO_MODIFICATION_ALLOWED_ERR: 7,
NOT_FOUND_ERR: 8,
NOT_SUPPORTED_ERR: 9,
INUSE_ATTRIBUTE_ERR: 10,
INVALID_STATE_ERR: 11,
SYNTAX_ERR: 12,
INVALID_MODIFICATION_ERR: 13,
NAMESPACE_ERR: 14,
INVALID_ACCESS_ERR: 15,
VALIDATION_ERR: 16,
TYPE_MISMATCH_ERR: 17,
SECURITY_ERR: 18,
NETWORK_ERR: 19,
ABORT_ERR: 20,
URL_MISMATCH_ERR: 21,
QUOTA_EXCEEDED_ERR: 22,
TIMEOUT_ERR: 23,
INVALID_NODE_TYPE_ERR: 24,
DATA_CLONE_ERR: 25
};
function defineErrorCodeProperties(obj) {
const keys = Object.keys(ErrorCodeMap);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const value = ErrorCodeMap[key];
Object.defineProperty(obj, key, {
get() {
return value;
},
configurable: true,
enumerable: true
});
}
}
/**
* An implementation of `Event` interface, that wraps a given event object.
* This class controls the internal state of `Event`.
* @see https://dom.spec.whatwg.org/#interface-event
*/
class EventWrapper extends Event$1 {
/**
* Wrap a given event object to control states.
* @param event The event-like object to wrap.
*/
static wrap(event) {
return new (getWrapperClassOf(event))(event);
}
constructor(event) {
super(event.type, {
bubbles: event.bubbles,
cancelable: event.cancelable,
composed: event.composed
});
if (event.cancelBubble) {
super.stopPropagation();
}
if (event.defaultPrevented) {
super.preventDefault();
}
internalDataMap$1.set(this, {
original: event
}); // Define accessors
const keys = Object.keys(event);
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
if (!(key in this)) {
Object.defineProperty(this, key, defineRedirectDescriptor(event, key));
}
}
}
stopPropagation() {
super.stopPropagation();
const {
original
} = $$1(this);
if ("stopPropagation" in original) {
original.stopPropagation();
}
}
get cancelBubble() {
return super.cancelBubble;
}
set cancelBubble(value) {
super.cancelBubble = value;
const {
original
} = $$1(this);
if ("cancelBubble" in original) {
original.cancelBubble = value;
}
}
stopImmediatePropagation() {
super.stopImmediatePropagation();
const {
original
} = $$1(this);
if ("stopImmediatePropagation" in original) {
original.stopImmediatePropagation();
}
}
get returnValue() {
return super.returnValue;
}
set returnValue(value) {
super.returnValue = value;
const {
original
} = $$1(this);
if ("returnValue" in original) {
original.returnValue = value;
}
}
preventDefault() {
super.preventDefault();
const {
original
} = $$1(this);
if ("preventDefault" in original) {
original.preventDefault();
}
}
get timeStamp() {
const {
original
} = $$1(this);
if ("timeStamp" in original) {
return original.timeStamp;
}
return super.timeStamp;
}
}
/**
* Private data for event wrappers.
*/
const internalDataMap$1 = new WeakMap();
/**
* Get private data.
* @param event The event object to get private data.
* @returns The private data of the event.
*/
function $$1(event) {
const retv = internalDataMap$1.get(event);
assertType(retv != null, "'this' is expected an Event object, but got", event);
return retv;
}
/**
* Cache for wrapper classes.
* @type {WeakMap<Object, Function>}
* @private
*/
const wrapperClassCache = new WeakMap(); // Make association for wrappers.
wrapperClassCache.set(Object.prototype, EventWrapper);
if (typeof Global !== "undefined" && typeof Global.Event !== "undefined") {
wrapperClassCache.set(Global.Event.prototype, EventWrapper);
}
/**
* Get the wrapper class of a given prototype.
* @param originalEvent The event object to wrap.
*/
function getWrapperClassOf(originalEvent) {
const prototype = Object.getPrototypeOf(originalEvent);
if (prototype == null) {
return EventWrapper;
}
let wrapper = wrapperClassCache.get(prototype);
if (wrapper == null) {
wrapper = defineWrapper(getWrapperClassOf(prototype), prototype);
wrapperClassCache.set(prototype, wrapper);
}
return wrapper;
}
/**
* Define new wrapper class.
* @param BaseEventWrapper The base wrapper class.
* @param originalPrototype The prototype of the original event.
*/
function defineWrapper(BaseEventWrapper, originalPrototype) {
class CustomEventWrapper extends BaseEventWrapper {}
const keys = Object.keys(originalPrototype);
for (let i = 0; i < keys.length; ++i) {
Object.defineProperty(CustomEventWrapper.prototype, keys[i], defineRedirectDescriptor(originalPrototype, keys[i]));
}
return CustomEventWrapper;
}
/**
* Get the property descriptor to redirect a given property.
*/
function defineRedirectDescriptor(obj, key) {
const d = Object.getOwnPropertyDescriptor(obj, key);
return {
get() {
const original = $$1(this).original;
const value = original[key];
if (typeof value === "function") {
return value.bind(original);
}
return value;
},
set(value) {
const original = $$1(this).original;
original[key] = value;
},
configurable: d.configurable,
enumerable: d.enumerable
};
}
/**
* Create a new listener.
* @param callback The callback function.
* @param capture The capture flag.
* @param passive The passive flag.
* @param once The once flag.
* @param signal The abort signal.
* @param signalListener The abort event listener for the abort signal.
*/
function createListener(callback, capture, passive, once, signal, signalListener) {
return {
callback,
flags: (capture ? 1
/* Capture */
: 0) | (passive ? 2
/* Passive */
: 0) | (once ? 4
/* Once */
: 0),
signal,
signalListener
};
}
/**
* Set the `removed` flag to the given listener.
* @param listener The listener to check.
*/
function setRemoved(listener) {
listener.flags |= 8
/* Removed */
;
}
/**
* Check if the given listener has the `capture` flag or not.
* @param listener The listener to check.
*/
function isCapture(listener) {
return (listener.flags & 1
/* Capture */
) === 1
/* Capture */
;
}
/**
* Check if the given listener has the `passive` flag or not.
* @param listener The listener to check.
*/
function isPassive(listener) {
return (listener.flags & 2
/* Passive */
) === 2
/* Passive */
;
}
/**
* Check if the given listener has the `once` flag or not.
* @param listener The listener to check.
*/
function isOnce(listener) {
return (listener.flags & 4
/* Once */
) === 4
/* Once */
;
}
/**
* Check if the given listener has the `removed` flag or not.
* @param listener The listener to check.
*/
function isRemoved(listener) {
return (listener.flags & 8
/* Removed */
) === 8
/* Removed */
;
}
/**
* Call an event listener.
* @param listener The listener to call.
* @param target The event target object for `thisArg`.
* @param event The event object for the first argument.
* @param attribute `true` if this callback is an event attribute handler.
*/
function invokeCallback({
callback
}, target, event) {
try {
if (typeof callback === "function") {
callback.call(target, event);
} else if (typeof callback.handleEvent === "function") {
callback.handleEvent(event);
}
} catch (thrownError) {
reportError(thrownError);
}
}
/**
* Find the index of given listener.
* This returns `-1` if not found.
* @param list The listener list.
* @param callback The callback function to find.
* @param capture The capture flag to find.
*/
function findIndexOfListener({
listeners
}, callback, capture) {
for (let i = 0; i < listeners.length; ++i) {
if (listeners[i].callback === callback && isCapture(listeners[i]) === capture) {
return i;
}
}
return -1;
}
/**
* Add the given listener.
* Does copy-on-write if needed.
* @param list The listener list.
* @param callback The callback function.
* @param capture The capture flag.
* @param passive The passive flag.
* @param once The once flag.
* @param signal The abort signal.
*/
function addListener(list, callback, capture, passive, once, signal) {
let signalListener;
if (signal) {
signalListener = removeListener.bind(null, list, callback, capture);
signal.addEventListener("abort", signalListener);
}
const listener = createListener(callback, capture, passive, once, signal, signalListener);
if (list.cow) {
list.cow = false;
list.listeners = [...list.listeners, listener];
} else {
list.listeners.push(listener);
}
return listener;
}
/**
* Remove a listener.
* @param list The listener list.
* @param callback The callback function to find.
* @param capture The capture flag to find.
* @returns `true` if it mutated the list directly.
*/
function removeListener(list, callback, capture) {
const index = findIndexOfListener(list, callback, capture);
if (index !== -1) {
return removeListenerAt(list, index);
}
return false;
}
/**
* Remove a listener.
* @param list The listener list.
* @param index The index of the target listener.
* @param disableCow Disable copy-on-write if true.
* @returns `true` if it mutated the `listeners` array directly.
*/
function removeListenerAt(list, index, disableCow = false) {
const listener = list.listeners[index]; // Set the removed flag.
setRemoved(listener); // Dispose the abort signal listener if exists.
if (listener.signal) {
listener.signal.removeEventListener("abort", listener.signalListener);
} // Remove it from the array.
if (list.cow && !disableCow) {
list.cow = false;
list.listeners = list.listeners.filter((_, i) => i !== index);
return false;
}
list.listeners.splice(index, 1);
return true;
}
/**
* Create a new `ListenerListMap` object.
*/
function createListenerListMap() {
return Object.create(null);
}
/**
* Get the listener list of the given type.
* If the listener list has not been initialized, initialize and return it.
* @param listenerMap The listener list map.
* @param type The event type to get.
*/
function ensureListenerList(listenerMap, type) {
var _a;
return (_a = listenerMap[type]) !== null && _a !== void 0 ? _a : listenerMap[type] = {
attrCallback: undefined,
attrListener: undefined,
cow: false,
listeners: []
};
}
/**
* An implementation of the `EventTarget` interface.
* @see https://dom.spec.whatwg.org/#eventtarget
*/
class EventTarget {
/**
* Initialize this instance.
*/
constructor() {
internalDataMap$2.set(this, createListenerListMap());
} // Implementation
addEventListener(type0, callback0, options0) {
const listenerMap = $$2(this);
const {
callback,
capture,
once,
passive,
signal,
type
} = normalizeAddOptions(type0, callback0, options0);
if (callback == null || (signal === null || signal === void 0 ? void 0 : signal.aborted)) {
return;
}
const list = ensureListenerList(listenerMap, type); // Find existing listener.
const i = findIndexOfListener(list, callback, capture);
if (i !== -1) {
warnDuplicate(list.listeners[i], passive, once, signal);
return;
} // Add the new listener.
addListener(list, callback, capture, passive, once, signal);
} // Implementation
removeEventListener(type0, callback0, options0) {
const listenerMap = $$2(this);
const {
callback,
capture,
type
} = normalizeOptions(type0, callback0, options0);
const list = listenerMap[type];
if (callback != null && list) {
removeListener(list, callback, capture);
}
} // Implementation
dispatchEvent(e) {
const list = $$2(this)[String(e.type)];
if (list == null) {
return true;
}
const event = e instanceof Event$1 ? e : EventWrapper.wrap(e);
const eventData = $(event, "event");
if (eventData.dispatchFlag) {
throw createInvalidStateError("This event has been in dispatching.");
}
eventData.dispatchFlag = true;
eventData.target = eventData.currentTarget = this;
if (!eventData.stopPropagationFlag) {
const {
cow,
listeners
} = list; // Set copy-on-write flag.
list.cow = true; // Call listeners.
for (let i = 0; i < listeners.length; ++i) {
const listener = listeners[i]; // Skip if removed.
if (isRemoved(listener)) {
continue;
} // Remove this listener if has the `once` flag.
if (isOnce(listener) && removeListenerAt(list, i, !cow)) {
// Because this listener was removed, the next index is the
// same as the current value.
i -= 1;
} // Call this listener with the `passive` flag.
eventData.inPassiveListenerFlag = isPassive(listener);
invokeCallback(listener, this, event);
eventData.inPassiveListenerFlag = false; // Stop if the `event.stopImmediatePropagation()` method was called.
if (eventData.stopImmediatePropagationFlag) {
break;
}
} // Restore copy-on-write flag.
if (!cow) {
list.cow = false;
}
}
eventData.target = null;
eventData.currentTarget = null;
eventData.stopImmediatePropagationFlag = false;
eventData.stopPropagationFlag = false;
eventData.dispatchFlag = false;
return !eventData.canceledFlag;
}
}
/**
* Internal data.
*/
const internalDataMap$2 = new WeakMap();
/**
* Get private data.
* @param target The event target object to get private data.
* @param name The variable name to report.
* @returns The private data of the event.
*/
function $$2(target, name = "this") {
const retv = internalDataMap$2.get(target);
assertType(retv != null, "'%s' must be an object that EventTarget constructor created, but got another one: %o", name, target);
return retv;
}
/**
* Normalize options.
* @param options The options to normalize.
*/
function normalizeAddOptions(type, callback, options) {
var _a;
assertCallback(callback);
if (typeof options === "object" && options !== null) {
return {
type: String(type),
callback: callback !== null && callback !== void 0 ? callback : undefined,
capture: Boolean(options.capture),
passive: Boolean(options.passive),
once: Boolean(options.once),
signal: (_a = options.signal) !== null && _a !== void 0 ? _a : undefined
};
}
return {
type: String(type),
callback: callback !== null && callback !== void 0 ? callback : undefined,
capture: Boolean(options),
passive: false,
once: false,
signal: undefined
};
}
/**
* Normalize options.
* @param options The options to normalize.
*/
function normalizeOptions(type, callback, options) {
assertCallback(callback);
if (typeof options === "object" && options !== null) {
return {
type: String(type),
callback: callback !== null && callback !== void 0 ? callback : undefined,
capture: Boolean(options.capture)
};
}
return {
type: String(type),
callback: callback !== null && callback !== void 0 ? callback : undefined,
capture: Boolean(options)
};
}
/**
* Assert the type of 'callback' argument.
* @param callback The callback to check.
*/
function assertCallback(callback) {
if (typeof callback === "function" || typeof callback === "object" && callback !== null && typeof callback.handleEvent === "function") {
return;
}
if (callback == null || typeof callback === "object") {
InvalidEventListener.warn(callback);
return;
}
throw new TypeError(format(InvalidEventListener.message, [callback]));
}
/**
* Print warning for duplicated.
* @param listener The current listener that is duplicated.
* @param passive The passive flag of the new duplicated listener.
* @param once The once flag of the new duplicated listener.
* @param signal The signal object of the new duplicated listener.
*/
function warnDuplicate(listener, passive, once, signal) {
EventListenerWasDuplicated.warn(isCapture(listener) ? "capture" : "bubble", listener.callback);
if (isPassive(listener) !== passive) {
OptionWasIgnored.warn("passive");
}
if (isOnce(listener) !== once) {
OptionWasIgnored.warn("once");
}
if (listener.signal !== signal) {
OptionWasIgnored.warn("signal");
}
} // Set enumerable
const keys$1 = Object.getOwnPropertyNames(EventTarget.prototype);
for (let i = 0; i < keys$1.length; ++i) {
if (keys$1[i] === "constructor") {
continue;
}
Object.defineProperty(EventTarget.prototype, keys$1[i], {
enumerable: true
});
} // Ensure `eventTarget instanceof window.EventTarget` is `true`.
if (typeof Global !== "undefined" && typeof Global.EventTarget !== "undefined") {
Object.setPrototypeOf(EventTarget.prototype, Global.EventTarget.prototype);
}
/**
* Get the current value of a given event attribute.
* @param target The `EventTarget` object to get.
* @param type The event type.
*/
function getEventAttributeValue(target, type) {
var _a, _b;
const listMap = $$2(target, "target");
return (_b = (_a = listMap[type]) === null || _a === void 0 ? void 0 : _a.attrCallback) !== null && _b !== void 0 ? _b : null;
}
/**
* Set an event listener to a given event attribute.
* @param target The `EventTarget` object to set.
* @param type The event type.
* @param callback The event listener.
*/
function setEventAttributeValue(target, type, callback) {
if (callback != null && typeof callback !== "function") {
InvalidAttributeHandler.warn(callback);
}
if (typeof callback === "function" || typeof callback === "object" && callback !== null) {
upsertEventAttributeListener(target, type, callback);
} else {
removeEventAttributeListener(target, type);
}
} //------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Update or insert the given event attribute handler.
* @param target The `EventTarget` object to set.
* @param type The event type.
* @param callback The event listener.
*/
function upsertEventAttributeListener(target, type, callback) {
const list = ensureListenerList($$2(target, "target"), String(type));
list.attrCallback = callback;
if (list.attrListener == null) {
list.attrListener = addListener(list, defineEventAttributeCallback(list), false, false, false, undefined);
}
}
/**
* Remove the given event attribute handler.
* @param target The `EventTarget` object to remove.
* @param type The event type.
* @param callback The event listener.
*/
function removeEventAttributeListener(target, type) {
const listMap = $$2(target, "target");
const list = listMap[String(type)];
if (list && list.attrListener) {
removeListener(list, list.attrListener.callback, false);
list.attrCallback = list.attrListener = undefined;
}
}
/**
* Define the callback function for the given listener list object.
* It calls `attrCallback` property if the property value is a function.
* @param list The `ListenerList` object.
*/
function defineEventAttributeCallback(list) {
return function (event) {
const callback = list.attrCallback;
if (typeof callback === "function") {
callback.call(this, event);
}
};
}
/**
* Define an event attribute.
* @param target The `EventTarget` object to define an event attribute.
* @param type The event type to define.
* @param _eventClass Unused, but to infer `Event` class type.
* @deprecated Use `getEventAttributeValue`/`setEventAttributeValue` pair on your derived class instead because of static analysis friendly.
*/
function defineEventAttribute(target, type, _eventClass) {
Object.defineProperty(target, `on${type}`, {
get() {
return getEventAttributeValue(this, type);
},
set(value) {
setEventAttributeValue(this, type, value);
},
configurable: true,
enumerable: true
});
}var PostMessageType;
(function (PostMessageType) {
PostMessageType["DATA_AVAILABLE"] = "DATA_AVAILABLE";
PostMessageType["START_RECORDING"] = "START_RECORDING";
PostMessageType["STOP_RECORDING"] = "STOP_RECORDING";
PostMessageType["ERROR"] = "ERROR";
PostMessageType["BLOB_READY"] = "BLOB_READY";
PostMessageType["WORKER_RECORDING"] = "WORKER_RECORDING";
})(PostMessageType || (PostMessageType = {}));
const startRecordingMessage = (config) => ({
type: PostMessageType.START_RECORDING,
config,
});
const dataAvailableMessage = (data) => ({
type: PostMessageType.DATA_AVAILABLE,
data,
});
const stopRecordingMessage = () => ({
type: PostMessageType.STOP_RECORDING,
});const MP3_MIME_TYPE = 'audio/mpeg';
const SafeAudioContext = window.AudioContext || window.webkitAudioContext;
const createGain = (ctx) => (ctx.createGain || ctx.createGainNode).call(ctx);
const createScriptProcessor = (ctx) => (ctx.createScriptProcessor || ctx.createJavaScriptNode).call(ctx, 4096, 1, 1);
class Mp3MediaRecorder extends EventTarget {
constructor(stream, { audioContext, worker }) {
super();
this.mimeType = MP3_MIME_TYPE;
this.state = 'inactive';
this.audioBitsPerSecond = 0;
this.videoBitsPerSecond = 0;
this.isInternalAudioContext = false;
this.onWorkerMessage = (event) => {
const message = event.data;
switch (message.type) {
case PostMessageType.WORKER_RECORDING: {
const event = new Event('start');
this.dispatchEvent(event);
this.state = 'recording';
break;
}
case PostMessageType.ERROR: {
const error = new Error(message.error);
const errEvent = new Event('error');
errEvent.error = error;
this.dispatchEvent(errEvent);
this.state = 'inactive';
break;
}
case PostMessageType.BLOB_READY: {
const stopEvent = new Event('stop');
const fallbackDataEvent = new Event('dataavailable');
fallbackDataEvent.data = message.blob;
fallbackDataEvent.timecode = Date.now();
const dataEvent = window.BlobEvent
? new BlobEvent('dataavailable', {
data: message.blob,
timecode: Date.now(),
})
: fallbackDataEvent;
this.dispatchEvent(dataEvent);
this.dispatchEvent(stopEvent);
this.state = 'inactive';
break;
}
case PostMessageType.ERROR: {
throw new Error(message.error);
}
}
};
if (!worker) {
throw new Error('No worker provided in Mp3MediaRecorder constructor.');
}
this.stream = stream;
this.isInternalAudioContext = !audioContext;
this.audioContext = audioContext || new SafeAudioContext();
this.worker = worker;
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
this.gainNode = createGain(this.audioContext);
this.gainNode.gain.value = 1;
this.processorNode = createScriptProcessor(this.audioContext);
this.sourceNode.connect(this.gainNode);
this.gainNode.connect(this.processorNode);
this.worker.onmessage = this.onWorkerMessage;
}
start() {
if (this.state !== 'inactive') {
throw this.getStateError('start');
}
this.processorNode.onaudioprocess = (event) => {
this.worker.postMessage(dataAvailableMessage(event.inputBuffer.getChannelData(0)));
};
this.processorNode.connect(this.audioContext.destination);
if (this.audioContext.state === 'closed') {
this.audioContext = new AudioContext();
}
else if (this.audioContext.state === 'suspended') {
this.audioContext.resume();
}
this.worker.postMessage(startRecordingMessage({ sampleRate: this.audioContext.sampleRate }));
}
stop() {
if (this.state === 'inactive') {
throw this.getStateError('stop');
}
this.processorNode.disconnect();
if (this.isInternalAudioContext) {
this.audioContext.close();
}
this.worker.postMessage(stopRecordingMessage());
}
pause() {
if (this.state === 'inactive') {
throw this.getStateError('pause');
}
this.audioContext.suspend().then(() => {
this.state = 'paused';
this.dispatchEvent(new Event('pause'));
});
}
resume() {
if (this.state === 'inactive') {
throw this.getStateError('resume');
}
this.audioContext.resume().then(() => {
this.state = 'recording';
this.dispatchEvent(new Event('resume'));
});
}
requestData() {
// not implemented, dataavailable event only fires when encoding is finished
}
getStateError(method) {
return new Error(`Failed to execute '${method}' on 'MediaRecorder': The MediaRecorder's state is '${this.state}'.`);
}
}
Mp3MediaRecorder.isTypeSupported = (mimeType) => mimeType === MP3_MIME_TYPE;
defineEventAttribute(Mp3MediaRecorder.prototype, 'start');
defineEventAttribute(Mp3MediaRecorder.prototype, 'stop');
defineEventAttribute(Mp3MediaRecorder.prototype, 'pause');
defineEventAttribute(Mp3MediaRecorder.prototype, 'resume');
defineEventAttribute(Mp3MediaRecorder.prototype, 'dataavailable');
defineEventAttribute(Mp3MediaRecorder.prototype, 'error');exports.Mp3MediaRecorder=Mp3MediaRecorder;