indexeddbshim
Version:
A polyfill for IndexedDB using WebSql
1,290 lines (1,224 loc) • 389 kB
JavaScript
/*! indexeddbshim - v16.1.0 - 9/1/2025 */
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
})((function () { 'use strict';
function _typeof$1(obj) {
"@babel/helpers - typeof";
return _typeof$1 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
}, _typeof$1(obj);
}
/* eslint-disable n/no-sync -- Want sync naming */
/* eslint-disable no-restricted-syntax -- Instanceof checks */
/* eslint-disable unicorn/no-this-assignment -- TS */
/**
* @typedef {number} Integer
*/
/**
* @callback InvokeCurrentListeners
* @param {AllListeners} listeners
* @param {EventWithProps} eventCopy
* @param {string} type
* @param {boolean} [checkOnListeners]
* @returns {boolean}
*/
/**
* @typedef {{
* defaultSync?: boolean,
* extraProperties?: string[],
* legacyOutputDidListenersThrowFlag?: boolean
* }} CustomOptions
*/
/**
* @typedef {{
* __legacyOutputDidListenersThrowError: unknown,
* target: EventTarget & {
* invokeCurrentListeners: InvokeCurrentListeners,
* _earlyListeners: AllListeners,
* _listeners: AllListeners,
* _lateListeners: AllListeners,
* _defaultListeners: AllListeners
* },
* composed: boolean,
* currentTarget: EventTarget,
* eventPhase: 0|1|2|3
* defaultPrevented: boolean,
* type: string,
* bubbles: boolean,
* cancelable: boolean,
* isTrusted: boolean,
* timeStamp: Integer,
* initEvent: (type: string, bubbles: boolean, cancelable: boolean) => void,
* preventDefault: () => void,
* composedPath: () => void,
* detail: any,
* initCustomEvent: (
* type: string, canBubble: boolean, cancelable: boolean,
* detail: any
* ) => void
* }} EventWithProps
*/
// Todo: Switch to ES6 classes
var phases = {
NONE: 0,
CAPTURING_PHASE: 1,
AT_TARGET: 2,
BUBBLING_PHASE: 3
};
var ShimDOMException$1 = typeof DOMException === 'undefined'
// Todo: Better polyfill (if even needed here)
/* eslint-disable no-shadow -- Polyfill */
// eslint-disable-next-line operator-linebreak -- TS/JSDoc needs
?
/**
* @param {string} msg
* @param {string} name
* @returns {Error}
*/
function DOMException(msg, name) {
// No need for `toString` as same as for `Error`
/* eslint-enable no-shadow -- Polyfill */
var err = new Error(msg);
err.name = name;
return err;
} : DOMException;
var ev = new WeakMap();
var evCfg = new WeakMap();
// Todo: Set _ev argument outside of this function
/* eslint-disable func-name-matching -- Shim vs. Polyfill */
/* eslint-disable no-shadow -- Polyfilling */
/**
* We use an adapter class rather than a proxy not only for compatibility
* but also since we have to clone native event properties anyways in order
* to properly set `target`, etc.
* The regular DOM method `dispatchEvent` won't work with this polyfill as
* it expects a native event.
* @class
* @param {string} type
*/
var ShimEvent = /** @type {unknown} */function Event(type) {
var _this = this;
/* eslint-enable func-name-matching -- Shim vs. Polyfill */
/* eslint-enable no-shadow -- Polyfilling */
// For WebIDL checks of function's `length`, we check `arguments` for the optional arguments
// @ts-expect-error
this[Symbol.toStringTag] = 'Event';
this.toString = function () {
return '[object Event]';
};
// eslint-disable-next-line prefer-rest-params -- Don't want to change signature
var _arguments = Array.prototype.slice.call(arguments),
evInit = _arguments[1],
_ev = _arguments[2];
if (!arguments.length) {
throw new TypeError("Failed to construct 'Event': 1 argument required, but only 0 present.");
}
evInit = evInit || {};
_ev = _ev || {};
/** @type {EventWithProps} */
var _evCfg = {};
if ('composed' in evInit) {
_evCfg.composed = evInit.composed;
}
// _evCfg.isTrusted = true; // We are not always using this for user-created events
// _evCfg.timeStamp = new Date().valueOf(); // This is no longer a timestamp, but monotonic (elapsed?)
ev.set(this, _ev);
evCfg.set(this, _evCfg);
var that = /** @type {unknown} */this;
/** @type {ShimEvent} */
that.initEvent(type, evInit.bubbles, evInit.cancelable);
['target', 'currentTarget', 'eventPhase', 'defaultPrevented'].forEach(function (pr) {
var prop = /** @type {"target"|"currentTarget"|"eventPhase"|"defaultPrevented"} */
pr;
Object.defineProperty(_this, prop, {
get: function get() {
return /* prop in _evCfg && */_evCfg[prop] !== undefined ? _evCfg[prop] : prop in _ev ? _ev[prop] :
// Defaults
prop === 'eventPhase' ? 0 : prop === 'defaultPrevented' ? false : null;
}
});
});
var props = [
// Event
'type', 'bubbles', 'cancelable',
// Defaults to false
'isTrusted', 'timeStamp', 'initEvent',
// Other event properties (not used by our code)
'composedPath', 'composed'];
if (this.toString() === '[object CustomEvent]') {
props.push('detail', 'initCustomEvent');
}
Object.defineProperties(this, props.reduce(function (obj, pr) {
var prop =
/**
* @type {"type"|"bubbles"|"cancelable"|"isTrusted"|
* "timeStamp"|"initEvent"|"composedPath"|"composed"|
* "detail"|"initCustomEvent"
* }
*/
pr;
obj[prop] = {
get: function get() {
return prop in _evCfg ? _evCfg[prop] : prop in _ev ? _ev[prop] : ['bubbles', 'cancelable', 'composed'].includes(prop) ? false : undefined;
}
};
return obj;
}, /** @type {{[key: string]: any}} */{}));
};
// @ts-expect-error Casting doesn't work
ShimEvent.prototype.preventDefault = function () {
// @ts-expect-error Needed for exporting
if (!(this instanceof ShimEvent)) {
throw new TypeError('Illegal invocation');
}
var _ev = ev.get(this);
var _evCfg = evCfg.get(this);
if (this.cancelable && !_evCfg._passive) {
_evCfg.defaultPrevented = true;
if (typeof _ev.preventDefault === 'function') {
// Prevent any predefined defaults
_ev.preventDefault();
}
}
};
// @ts-expect-error Casting doesn't work
ShimEvent.prototype.stopImmediatePropagation = function () {
var _evCfg = evCfg.get(this);
_evCfg._stopImmediatePropagation = true;
};
// @ts-expect-error Casting doesn't work
ShimEvent.prototype.stopPropagation = function () {
var _evCfg = evCfg.get(this);
_evCfg._stopPropagation = true;
};
// @ts-expect-error Casting doesn't work
ShimEvent.prototype.initEvent = function (type, bubbles, cancelable) {
// Chrome currently has function length 1 only but WebIDL says 3
// const bubbles = arguments[1];
// const cancelable = arguments[2];
var _evCfg = evCfg.get(this);
if (_evCfg._dispatched) {
return;
}
Object.defineProperty(this, 'type', {
enumerable: true,
configurable: true,
get: function get() {
return type;
}
});
Object.defineProperty(this, 'bubbles', {
enumerable: true,
configurable: true,
get: function get() {
return bubbles;
}
});
Object.defineProperty(this, 'cancelable', {
enumerable: true,
configurable: true,
get: function get() {
return cancelable;
}
});
_evCfg.type = type;
if (bubbles !== undefined) {
_evCfg.bubbles = bubbles;
}
if (cancelable !== undefined) {
_evCfg.cancelable = cancelable;
}
};
['type', 'target', 'currentTarget'].forEach(function (prop) {
// @ts-expect-error Casting doesn't work
Object.defineProperty(ShimEvent.prototype, prop, {
enumerable: true,
configurable: true,
get: function get() {
throw new TypeError('Illegal invocation');
}
});
});
['eventPhase', 'defaultPrevented', 'bubbles', 'cancelable', 'timeStamp'].forEach(function (prop) {
// @ts-expect-error Casting doesn't work
Object.defineProperty(ShimEvent.prototype, prop, {
enumerable: true,
configurable: true,
get: function get() {
throw new TypeError('Illegal invocation');
}
});
});
['NONE', 'CAPTURING_PHASE', 'AT_TARGET', 'BUBBLING_PHASE'].forEach(function (prop, i) {
Object.defineProperty(ShimEvent, prop, {
enumerable: true,
writable: false,
value: i
});
// @ts-expect-error Casting doesn't work
Object.defineProperty(ShimEvent.prototype, prop, {
writable: false,
value: i
});
});
// @ts-expect-error Casting doesn't work
ShimEvent[Symbol.toStringTag] = 'Function';
// @ts-expect-error Casting doesn't work
ShimEvent.prototype[Symbol.toStringTag] = 'EventPrototype';
Object.defineProperty(ShimEvent, 'prototype', {
writable: false
});
/* eslint-disable func-name-matching -- Polyfill */
/* eslint-disable no-shadow -- Polyfill */
/**
* @class
* @param {string} type
*/
var ShimCustomEvent = /** @type {unknown} */function CustomEvent(type) {
/* eslint-enable func-name-matching -- Polyfill */
/* eslint-enable no-shadow -- Polyfill */
// eslint-disable-next-line prefer-rest-params -- Keep signature
var _arguments2 = Array.prototype.slice.call(arguments),
evInit = _arguments2[1],
_ev = _arguments2[2];
// @ts-expect-error Casting doesn't work
ShimEvent.call(this, type, evInit, _ev);
// @ts-expect-error
this[Symbol.toStringTag] = 'CustomEvent';
this.toString = function () {
return '[object CustomEvent]';
};
// var _evCfg = evCfg.get(this);
evInit = evInit || {};
// @ts-ignore
this.initCustomEvent(type, evInit.bubbles, evInit.cancelable, 'detail' in evInit ? evInit.detail : null);
};
// @ts-expect-error Casting doesn't work
Object.defineProperty(ShimCustomEvent.prototype, 'constructor', {
enumerable: false,
writable: true,
configurable: true,
value: ShimCustomEvent
});
// @ts-expect-error Casting doesn't work
ShimCustomEvent.prototype.initCustomEvent = function (type, bubbles, cancelable, detail) {
// @ts-expect-error Needed for exporting
if (!(this instanceof ShimCustomEvent)) {
throw new TypeError('Illegal invocation');
}
var _evCfg = evCfg.get(this);
// @ts-expect-error Casting doesn't work
ShimCustomEvent.call(this, type, {
bubbles: bubbles,
cancelable: cancelable,
detail: detail
// eslint-disable-next-line prefer-rest-params -- Keep signature
}, arguments[4]);
if (_evCfg._dispatched) {
return;
}
if (detail !== undefined) {
_evCfg.detail = detail;
}
Object.defineProperty(this, 'detail', {
get: function get() {
return _evCfg.detail;
}
});
};
// @ts-expect-error Casting doesn't work
ShimCustomEvent[Symbol.toStringTag] = 'Function';
// @ts-expect-error Casting doesn't work
ShimCustomEvent.prototype[Symbol.toStringTag] = 'CustomEventPrototype';
// @ts-expect-error Casting doesn't work
Object.defineProperty(ShimCustomEvent.prototype, 'detail', {
enumerable: true,
configurable: true,
get: function get() {
throw new TypeError('Illegal invocation');
}
});
Object.defineProperty(ShimCustomEvent, 'prototype', {
writable: false
});
/**
*
* @param {EventWithProps} e
* @returns {EventWithProps}
*/
function copyEvent(e) {
var bubbles = e.bubbles,
cancelable = e.cancelable,
detail = e.detail,
type = e.type;
if ('detail' in e) {
// @ts-expect-error Casting doesn't work
return new ShimCustomEvent(type, {
bubbles: bubbles,
cancelable: cancelable,
detail: detail
}, e);
}
// @ts-expect-error Casting doesn't work
return new ShimEvent(type, {
bubbles: bubbles,
cancelable: cancelable
}, e);
}
/**
* @typedef {object} ListenerOptions
* @property {boolean} [once] Remove listener after invoking once
* @property {boolean} [passive] Don't allow `preventDefault`
* @property {boolean} [capture] Use `_children` and set `eventPhase`
*/
/**
* @typedef {object} ListenerAndOptions
* @property {Listener} listener
* @property {ListenerOptions} options
*/
/**
* @typedef {object} ListenerInfo
* @property {ListenerAndOptions[]} listenersByTypeOptions
* @property {ListenerOptions} options
* @property {ListenerAndOptions[]} listenersByType
*/
/**
* @callback Listener
* @param {EventWithProps} e
* @returns {boolean}
*/
/**
* Keys are event types.
* @typedef {{[key: string]: Listener[]}} Listeners
*/
/**
* @typedef {{
* [type: string]: ListenerAndOptions[]
* }} AllListeners
*/
/**
*
* @param {AllListeners} listeners
* @param {string} type
* @param {boolean|ListenerOptions} options
* @returns {ListenerInfo}
*/
function getListenersOptions(listeners, type, options) {
var listenersByType = listeners[type];
if (listenersByType === undefined) listeners[type] = listenersByType = [];
var opts = typeof options === 'boolean' ? {
capture: options
} : options || {};
var stringifiedOptions = JSON.stringify(opts);
var listenersByTypeOptions = listenersByType.filter(function (obj) {
return stringifiedOptions === JSON.stringify(obj.options);
});
return {
listenersByTypeOptions: listenersByTypeOptions,
options: opts,
listenersByType: listenersByType
};
}
var methods = {
/**
* @param {AllListeners} listeners
* @param {Listener} listener
* @param {string} type
* @param {boolean|ListenerOptions} options
* @returns {void}
*/
addListener: function addListener(listeners, listener, type, options) {
var listenersOptions = getListenersOptions(listeners, type, options);
var listenersByTypeOptions = listenersOptions.listenersByTypeOptions;
options = listenersOptions.options;
var listenersByType = listenersOptions.listenersByType;
if (listenersByTypeOptions.some(function (l) {
return l.listener === listener;
})) return;
listenersByType.push({
listener: listener,
options: options
});
},
/**
* @param {AllListeners} listeners
* @param {Listener} listener
* @param {string} type
* @param {boolean|ListenerOptions} options
* @returns {void}
*/
removeListener: function removeListener(listeners, listener, type, options) {
var listenersOptions = getListenersOptions(listeners, type, options);
var listenersByType = listenersOptions.listenersByType;
var stringifiedOptions = JSON.stringify(listenersOptions.options);
listenersByType.some(function (l, i) {
if (l.listener === listener && stringifiedOptions === JSON.stringify(l.options)) {
listenersByType.splice(i, 1);
if (!listenersByType.length) delete listeners[type];
return true;
}
return false;
});
},
/**
*
* @param {AllListeners} listeners
* @param {Listener} listener
* @param {string} type
* @param {boolean|ListenerOptions} options
* @returns {boolean}
*/
hasListener: function hasListener(listeners, listener, type, options) {
var listenersOptions = getListenersOptions(listeners, type, options);
var listenersByTypeOptions = listenersOptions.listenersByTypeOptions;
return listenersByTypeOptions.some(function (l) {
return l.listener === listener;
});
}
};
/* eslint-disable no-shadow -- Polyfill */
/**
* @class
*/
function EventTarget() {
/* eslint-enable no-shadow -- Polyfill */
throw new TypeError('Illegal constructor');
}
/**
* @typedef {"addEarlyEventListener"|"removeEarlyEventListener"|"hasEarlyEventListener"|
* "addEventListener"|"removeEventListener"|"hasEventListener"|
* "addLateEventListener"|"removeLateEventListener"|"hasLateEventListener"|
* "addDefaultEventListener"|"removeDefaultEventListener"|"hasDefaultEventListener"
* } ListenerName
*/
Object.assign(EventTarget.prototype, ['Early', '', 'Late', 'Default'].reduce(function (/** @type {{[key: string]: Function}} */
obj, listenerType) {
['add', 'remove', 'has'].forEach(function (method) {
var mainMethod = /** @type {ListenerName} */method + listenerType + 'EventListener';
/**
* @param {string} type
* @param {Listener|{handleEvent: Listener}} listener
* @this {EventTarget & {
* _earlyListeners: AllListeners,
* _listeners: AllListeners,
* _lateListeners: AllListeners,
* _defaultListeners: AllListeners,
* }}
* @returns {boolean|void}
*/
obj[mainMethod] = function (type, listener) {
// eslint-disable-next-line prefer-rest-params -- Keep signature
var options = arguments[2]; // We keep the listener `length` as per WebIDL
if (arguments.length < 2) throw new TypeError('2 or more arguments required');
if (typeof type !== 'string') {
// @ts-expect-error It's ok to construct
throw new ShimDOMException$1('UNSPECIFIED_EVENT_TYPE_ERR', 'UNSPECIFIED_EVENT_TYPE_ERR');
}
try {
// As per code such as the following, handleEvent may throw,
// but is uncaught
// https://github.com/web-platform-tests/wpt/blob/master/IndexedDB/fire-error-event-exception.html#L54-L56
if ('handleEvent' in listener && listener.handleEvent.bind) {
listener = listener.handleEvent.bind(listener);
}
} catch (err) {
// eslint-disable-next-line no-console -- Feedback to user
console.log('Uncaught `handleEvent` error', err);
}
var arrStr = /** @type {"_earlyListeners"|"_listeners"|"_lateListeners"|"_defaultListeners"} */
'_' + listenerType.toLowerCase() + (listenerType === '' ? 'l' : 'L') + 'isteners';
if (!this[arrStr]) {
Object.defineProperty(this, arrStr, {
value: {}
});
}
var meth = /** @type {"addListener"|"removeListener"|"hasListener"} */
method + 'Listener';
return methods[meth](this[arrStr], /** @type {Listener} */listener, type, options);
};
});
return obj;
}, {}));
Object.assign(EventTarget.prototype, {
_legacyOutputDidListenersThrowCheck: undefined,
/**
* @param {CustomOptions} customOptions
* @this {EventTarget.prototype}
* @returns {void}
*/
__setOptions: function __setOptions(customOptions) {
customOptions = customOptions || {};
// Todo: Make into event properties?
this._defaultSync = customOptions.defaultSync;
this._extraProperties = customOptions.extraProperties || [];
if (customOptions.legacyOutputDidListenersThrowFlag) {
// IndexedDB
this._legacyOutputDidListenersThrowCheck = true;
this._extraProperties.push('__legacyOutputDidListenersThrowError');
}
},
/**
* @param {ShimEvent} e
* @this {EventTarget & {
* _dispatchEvent: (e: ShimEvent|ShimCustomEvent, setTarget: boolean) => boolean,
* }}
* @returns {boolean}
*/
dispatchEvent: function dispatchEvent(e) {
return this._dispatchEvent(e, true);
},
/**
* @param {EventWithProps} e
* @param {boolean} setTarget
* @this {EventTarget.prototype & {
* _earlyListeners: AllListeners,
* _listeners: AllListeners,
* _lateListeners: AllListeners,
* _defaultListeners: AllListeners,
* }}
* @returns {boolean}
*/
_dispatchEvent: function _dispatchEvent(e, setTarget) {
var _this2 = this;
['early', '', 'late', 'default'].forEach(function (listenerType) {
var arrStr = /** @type {"_earlyListeners"|"_listeners"|"_lateListeners"|"_defaultListeners"} */
'_' + listenerType + (listenerType === '' ? 'l' : 'L') + 'isteners';
if (!_this2[arrStr]) {
Object.defineProperty(_this2, arrStr, {
value: {}
});
}
});
var _evCfg = evCfg.get(e);
if (_evCfg && setTarget && _evCfg._dispatched) {
// @ts-expect-error It's ok to construct
throw new ShimDOMException$1('The object is in an invalid state.', 'InvalidStateError');
}
/** @type {EventWithProps} */
var eventCopy;
if (_evCfg) {
eventCopy = e;
} else {
eventCopy = copyEvent(e);
_evCfg = evCfg.get(eventCopy);
_evCfg._dispatched = true;
/** @type {string[]} */
this._extraProperties.forEach(function (prop) {
if (prop in e) {
/** @type {{[key: string]: any}} */eventCopy[prop] = /** @type {{[key: string]: any}} */e[prop]; // Todo: Put internal to `ShimEvent`?
}
});
}
var _eventCopy = eventCopy,
type = _eventCopy.type;
/**
*
* @returns {void}
*/
function finishEventDispatch() {
_evCfg.eventPhase = phases.NONE;
_evCfg.currentTarget = null;
delete _evCfg._children;
}
/**
*
* @returns {void}
*/
function invokeDefaults() {
// Ignore stopPropagation from defaults
_evCfg._stopImmediatePropagation = undefined;
_evCfg._stopPropagation = undefined;
// We check here for whether we should invoke since may have changed since timeout (if late listener prevented default)
if (!eventCopy.defaultPrevented || !_evCfg.cancelable) {
// 2nd check should be redundant
_evCfg.eventPhase = phases.AT_TARGET; // Temporarily set before we invoke default listeners
eventCopy.target.invokeCurrentListeners(eventCopy.target._defaultListeners, eventCopy, type);
}
finishEventDispatch();
}
var continueEventDispatch = function continueEventDispatch() {
// Ignore stop propagation of user now
_evCfg._stopImmediatePropagation = undefined;
_evCfg._stopPropagation = undefined;
if (!_this2._defaultSync) {
setTimeout(invokeDefaults, 0);
} else invokeDefaults();
_evCfg.eventPhase = phases.AT_TARGET; // Temporarily set before we invoke late listeners
// Sync default might have stopped
if (!_evCfg._stopPropagation) {
_evCfg._stopImmediatePropagation = undefined;
_evCfg._stopPropagation = undefined;
// We could allow stopPropagation by only executing upon (_evCfg._stopPropagation)
eventCopy.target.invokeCurrentListeners(eventCopy.target._lateListeners, eventCopy, type);
}
finishEventDispatch();
return !eventCopy.defaultPrevented;
};
if (setTarget) _evCfg.target = this;
switch ('eventPhase' in eventCopy && eventCopy.eventPhase) {
case phases.CAPTURING_PHASE:
{
if (_evCfg._stopPropagation) {
return continueEventDispatch();
}
this.invokeCurrentListeners(this._listeners, eventCopy, type);
var child = _evCfg._children && _evCfg._children.length && _evCfg._children.pop();
if (!child || child === eventCopy.target) {
_evCfg.eventPhase = phases.AT_TARGET;
}
if (child) child._defaultSync = this._defaultSync;
return (child || this)._dispatchEvent(eventCopy, false);
}
case phases.AT_TARGET:
if (_evCfg._stopPropagation) {
return continueEventDispatch();
}
this.invokeCurrentListeners(this._listeners, eventCopy, type, true);
if (!_evCfg.bubbles) {
return continueEventDispatch();
}
_evCfg.eventPhase = phases.BUBBLING_PHASE;
return this._dispatchEvent(eventCopy, false);
case phases.BUBBLING_PHASE:
{
if (_evCfg._stopPropagation) {
return continueEventDispatch();
}
var parent = this.__getParent && this.__getParent();
if (!parent) {
return continueEventDispatch();
}
parent.invokeCurrentListeners(parent._listeners, eventCopy, type, true);
parent._defaultSync = this._defaultSync;
return parent._dispatchEvent(eventCopy, false);
}
case phases.NONE:
default:
{
_evCfg.eventPhase = phases.AT_TARGET; // Temporarily set before we invoke early listeners
this.invokeCurrentListeners(this._earlyListeners, eventCopy, type);
if (!('__getParent' in this)) {
_evCfg.eventPhase = phases.AT_TARGET;
return this._dispatchEvent(eventCopy, false);
}
/* eslint-disable consistent-this -- Readability */
var par = this;
var root_ = this;
/* eslint-enable consistent-this -- Readability */
while (par.__getParent && (par = par.__getParent()) !== null) {
if (!_evCfg._children) {
_evCfg._children = [];
}
_evCfg._children.push(root_);
root_ = par;
}
root_._defaultSync = this._defaultSync;
_evCfg.eventPhase = phases.CAPTURING_PHASE;
return root_._dispatchEvent(eventCopy, false);
}
}
},
/**
* @type {InvokeCurrentListeners}
* @this {EventTarget.prototype & {[key: string]: Listener}}
*/
invokeCurrentListeners: function invokeCurrentListeners(listeners, eventCopy, type, checkOnListeners) {
var _this3 = this;
var _evCfg = evCfg.get(eventCopy);
_evCfg.currentTarget = this;
var listOpts = getListenersOptions(listeners, type, {});
// eslint-disable-next-line unicorn/prefer-spread -- Performance?
var listenersByType = listOpts.listenersByType.concat();
var dummyIPos = listenersByType.length ? 1 : 0;
listenersByType.some(function (listenerObj, i) {
var onListener = checkOnListeners ? _this3['on' + type] : null;
if (_evCfg._stopImmediatePropagation) return true;
if (i === dummyIPos && typeof onListener === 'function') {
// We don't splice this in as could be overwritten; executes here per
// https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-attributes:event-handlers-14
_this3.tryCatch(eventCopy, function () {
var ret = onListener.call(eventCopy.currentTarget, eventCopy);
if (ret === false) {
eventCopy.preventDefault();
}
});
}
var options = listenerObj.options;
var once = options.once,
passive = options.passive,
capture = options.capture;
_evCfg._passive = passive;
if (capture && eventCopy.target !== eventCopy.currentTarget && eventCopy.eventPhase === phases.CAPTURING_PHASE || eventCopy.eventPhase === phases.AT_TARGET || !capture && eventCopy.target !== eventCopy.currentTarget && eventCopy.eventPhase === phases.BUBBLING_PHASE) {
var listener = listenerObj.listener;
_this3.tryCatch(eventCopy, function () {
listener.call(eventCopy.currentTarget, eventCopy);
});
if (once) {
_this3.removeEventListener(type, listener, options);
}
}
return false;
});
this.tryCatch(eventCopy, function () {
var onListener = checkOnListeners ? _this3['on' + type] : null;
if (typeof onListener === 'function' && listenersByType.length < 2) {
var ret = onListener.call(eventCopy.currentTarget, eventCopy); // Won't have executed if too short
if (ret === false) {
eventCopy.preventDefault();
}
}
});
return !eventCopy.defaultPrevented;
},
/* eslint-disable promise/prefer-await-to-callbacks -- Try-catch */
/**
* @param {EventWithProps} evt
* @param {() => void} cb
* @returns {void}
*/
tryCatch: function tryCatch(evt, cb) {
/* eslint-enable promise/prefer-await-to-callbacks -- Try-catch */
try {
// Per MDN: Exceptions thrown by event handlers are reported
// as uncaught exceptions; the event handlers run on a nested
// callstack: they block the caller until they complete, but
// exceptions do not propagate to the caller.
// eslint-disable-next-line promise/prefer-await-to-callbacks, n/callback-return -- Try-catch
cb();
} catch (err) {
this.triggerErrorEvent(err, evt);
}
},
/**
* @param {unknown} err
* @param {EventWithProps} evt
* @returns {void}
*/
triggerErrorEvent: function triggerErrorEvent(err, evt) {
var error = err;
if (typeof err === 'string') {
error = new Error('Uncaught exception: ' + err);
}
var triggerGlobalErrorEvent;
var useNodeImpl = false;
if (typeof window === 'undefined' || typeof ErrorEvent === 'undefined' || window && (typeof window === "undefined" ? "undefined" : _typeof$1(window)) === 'object' && !window.dispatchEvent) {
useNodeImpl = true;
triggerGlobalErrorEvent = function triggerGlobalErrorEvent() {
setTimeout(function () {
// Node won't be able to catch in this way if we throw in the main thread
// console.log(err); // Should we auto-log for user?
throw error; // Let user listen to `process.on('uncaughtException', (err) => {});`
});
};
} else {
triggerGlobalErrorEvent = function triggerGlobalErrorEvent() {
// See https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
// and https://github.com/w3c/IndexedDB/issues/49
// Note that a regular Event will properly trigger
// `window.addEventListener('error')` handlers, but it will not trigger
// `window.onerror` as per https://html.spec.whatwg.org/multipage/webappapis.html#handler-onerror
// Note also that the following line won't handle `window.addEventListener` handlers
// if (window.onerror) window.onerror(error.message, err.fileName, err.lineNumber, error.columnNumber, error);
// `ErrorEvent` properly triggers `window.onerror` and `window.addEventListener('error')` handlers
var errEv = new ErrorEvent('error', {
error: err,
message: /** @type {Error} */error.message || '',
// We can't get the actually useful user's values!
filename: /** @type {Error & {fileName: string}} */error.fileName || '',
lineno: /** @type {Error & {lineNumber: Integer}} */error.lineNumber || 0,
colno: /** @type {Error & {columnNumber: Integer}} */error.columnNumber || 0
});
window.dispatchEvent(errEv);
// console.log(err); // Should we auto-log for user?
};
}
// Todo: This really should always run here but as we can't set the global
// `window` (e.g., using jsdom) since `setGlobalVars` becomes unable to
// shim `indexedDB` in such a case currently (apparently due to
// <https://github.com/axemclion/IndexedDBShim/issues/280>), we can't
// avoid the above Node implementation (which, while providing some
// fallback mechanism, is unstable)
if (!useNodeImpl || !this._legacyOutputDidListenersThrowCheck) triggerGlobalErrorEvent();
// See https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke and
// https://github.com/w3c/IndexedDB/issues/140 (also https://github.com/w3c/IndexedDB/issues/49 )
if (this._legacyOutputDidListenersThrowCheck) {
evt.__legacyOutputDidListenersThrowError = error;
}
}
});
EventTarget.prototype[Symbol.toStringTag] = 'EventTargetPrototype';
Object.defineProperty(EventTarget, 'prototype', {
writable: false
});
var ShimEventTarget = EventTarget;
var EventTargetFactory = {
/**
* @param {CustomOptions} customOptions
* @returns {EventTarget}
*/
createInstance: function createInstance(customOptions) {
/* eslint-disable func-name-matching -- Shim vs. Polyfill */
/* eslint-disable no-shadow -- Polyfill */
/**
* @class
* @this {typeof ShimEventTarget.prototype}
*/
var ET = /** @type {unknown} */function EventTarget() {
/* eslint-enable no-shadow -- Polyfill */
/* eslint-enable func-name-matching -- Shim vs. Polyfill */
this.__setOptions(customOptions);
};
// @ts-expect-error Casting doesn't work
ET.prototype = ShimEventTarget.prototype;
// @ts-expect-error Casting doesn't work
return new ET();
}
};
EventTarget.ShimEvent = ShimEvent;
EventTarget.ShimCustomEvent = ShimCustomEvent;
EventTarget.ShimDOMException = ShimDOMException$1;
EventTarget.ShimEventTarget = EventTarget;
EventTarget.EventTargetFactory = EventTargetFactory;
/**
* @returns {void}
*/
function setPrototypeOfCustomEvent() {
// TODO: IDL needs but reported as slow!
Object.setPrototypeOf(ShimCustomEvent, /** @type {object} */ShimEvent);
// @ts-expect-error How to overcome?
Object.setPrototypeOf(ShimCustomEvent.prototype, ShimEvent.prototype);
}
/* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
/**
* @typedef {T[keyof T]} ValueOf<T>
* @template T
*/
/* eslint-enable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
/**
* @typedef {{unlink: (path: string, cb: import('fs').NoParamCallback) => void}} FSApi
*/
/**
* @typedef {{
* DEBUG: boolean,
* cacheDatabaseInstances: boolean,
* autoName: boolean,
* fullIDLSupport: boolean,
* checkOrigin: boolean,
* cursorPreloadPackSize: number,
* UnicodeIDStart: string,
* UnicodeIDContinue: string,
* registerSCA: (
* preset: import('typeson').Preset
* ) => import('typeson').Preset,
* avoidAutoShim: boolean,
* win: {
* openDatabase: (name: string, version: string, displayName: string, estimatedSize: number) => import('websql-configurable').default
* },
* DEFAULT_DB_SIZE: number,
* useSQLiteIndexes: boolean,
* fs: FSApi,
* addNonIDBGlobals: boolean,
* replaceNonIDBGlobals: boolean,
* escapeDatabaseName: (name: string) => string,
* unescapeDatabaseName: (name: string) => string,
* databaseCharacterEscapeList: string|false,
* databaseNameLengthLimit: number|false,
* escapeNFDForDatabaseNames: boolean,
* addSQLiteExtension: boolean,
* memoryDatabase: string,
* deleteDatabaseFiles: boolean,
* databaseBasePath: string,
* sysDatabaseBasePath: string,
* sqlBusyTimeout: number,
* sqlTrace: () => void,
* sqlProfile: () => void,
* createIndexes: boolean
* }} ConfigValues
*/
/**
* @typedef {ValueOf<ConfigValues>} ConfigValue
*/
/** @type {{[key: string]: ConfigValue}} */
const map = {};
const CFG = /** @type {ConfigValues} */{};
/**
* @typedef {keyof ConfigValues} KeyofConfigValues
*/
/**
* @typedef {KeyofConfigValues[]} Config
*/
/** @type {Config} */
[
// Boolean for verbose reporting
'DEBUG',
// Effectively defaults to false (ignored unless `true`)
// Boolean (effectively defaults to true) on whether to cache WebSQL
// `openDatabase` instances
'cacheDatabaseInstances',
// Boolean on whether to auto-name databases (based on an
// auto-increment) when the empty string is supplied; useful with
// `memoryDatabase`; defaults to `false` which means the empty string
// will be used as the (valid) database name
'autoName',
// Determines whether the slow-performing `Object.setPrototypeOf`
// calls required for full WebIDL compliance will be used. Probably
// only needed for testing or environments where full introspection
// on class relationships is required; see
// http://stackoverflow.com/questions/41927589/rationales-consequences-of-webidl-class-inheritance-requirements
'fullIDLSupport',
// Effectively defaults to false (ignored unless `true`)
// Boolean on whether to perform origin checks in `IDBFactory` methods
// Effectively defaults to `true` (must be set to `false` to cancel checks)
'checkOrigin',
// Used by `IDBCursor` continue methods for number of records to cache;
// Defaults to 100
'cursorPreloadPackSize',
// See optional API (`shimIndexedDB.__setUnicodeIdentifiers`);
// or just use the Unicode builds which invoke this method
// automatically using the large, fully spec-compliant, regular
// expression strings of `src/UnicodeIdentifiers.js`)
// In the non-Unicode builds, defaults to /[$A-Z_a-z]/
'UnicodeIDStart',
// In the non-Unicode builds, defaults to /[$0-9A-Z_a-z]/
'UnicodeIDContinue',
// Used by SCA.js for optional restructuring of typeson-registry
// Structured Cloning Algorithm; should only be needed for ensuring data
// created in 3.* versions of IndexedDBShim continue to work; see the
// library `typeson-registry-sca-reverter` to get a function to do this
'registerSCA',
// BROWSER-SPECIFIC CONFIG
'avoidAutoShim',
// Where WebSQL is detected but where `indexedDB` is
// missing or poor support is known (non-Chrome Android or
// non-Safari iOS9), the shim will be auto-applied without
// `shimIndexedDB.__useShim()`. Set this to `true` to avoid forcing
// the shim for such cases.
// -----------SQL CONFIG----------
// Object (`window` in the browser) on which there may be an
// `openDatabase` method (if any) for WebSQL. (The browser
// throws if attempting to call `openDatabase` without the window
// so this is why the config doesn't just allow the function.)
// Defaults to `window` or `self` in browser builds or
// a singleton object with the `openDatabase` method set to
// the "websql" package in Node.
'win',
// For internal `openDatabase` calls made by `IDBFactory` methods;
// per the WebSQL spec, "User agents are expected to use the display name
// and the estimated database size to optimize the user experience.
// For example, a user agent could use the estimated size to suggest an
// initial quota to the user. This allows a site that is aware that it
// will try to use hundreds of megabytes to declare this upfront, instead
// of the user agent prompting the user for permission to increase the
// quota every five megabytes."
// Defaults to (4 * 1024 * 1024) or (25 * 1024 * 1024) in Safari
'DEFAULT_DB_SIZE',
// Whether to create indexes on SQLite tables (and also whether to try
// dropping)
// Effectively defaults to `false` (ignored unless `true`)
'useSQLiteIndexes',
// NODE-IMPINGING SETTINGS (created for sake of limitations in Node
// or desktop file system implementation but applied by default in
// browser for parity)
// File system module with `unlink` to remove deleted database files
'fs',
// Used when setting global shims to determine whether to try to add
// other globals shimmed by the library (`ShimDOMException`,
// `ShimDOMStringList`, `ShimEvent`, `ShimCustomEvent`, `ShimEventTarget`)
// Effectively defaults to `false` (ignored unless `true`)
'addNonIDBGlobals',
// Used when setting global shims to determine whether to try to overwrite
// other globals shimmed by the library (`DOMException`, `DOMStringList`,
// `Event`, `CustomEvent`, `EventTarget`)
// Effectively defaults to `false` (ignored unless `true`)
'replaceNonIDBGlobals',
// Overcoming limitations with node-sqlite3/storing database name on
// file systems
// https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
// Defaults to prefixing database with `D_`, escaping
// `databaseCharacterEscapeList`, escaping NUL, and
// escaping upper case letters, as well as enforcing
// `databaseNameLengthLimit`
'escapeDatabaseName',
// Not used internally; usable as a convenience method
'unescapeDatabaseName',
// Defaults to global regex representing the following
// (characters nevertheless commonly reserved in modern,
// Unicode-supporting systems): 0x00-0x1F 0x7F " * / : < > ? \ |
'databaseCharacterEscapeList',
// Defaults to 254 (shortest typical modern file length limit)
'databaseNameLengthLimit',
// Boolean defaulting to true on whether to escape NFD-escaping
// characters to avoid clashes on MacOS which performs NFD on files
'escapeNFDForDatabaseNames',
// Boolean on whether to add the `.sqlite` extension to file names;
// defaults to `true`
'addSQLiteExtension',
// Various types of in-memory databases that can auto-delete
['memoryDatabase',
/**
* @param {string} val
* @throws {TypeError}
* @returns {void}
*/
val => {
if (!/^(?::memory:|file::memory:(\?[^#]*)?(#.*)?)?$/u.test(/** @type {string} */val)) {
throw new TypeError('`memoryDatabase` must be the empty string, ":memory:", or a ' + '"file::memory:[?queryString][#hash] URL".');
}
}],
// NODE-SPECIFIC CONFIG
// Boolean on whether to delete the database file itself after
// `deleteDatabase`; defaults to `true` as the database will be empty
'deleteDatabaseFiles', 'databaseBasePath', 'sysDatabaseBasePath',
// NODE-SPECIFIC WEBSQL CONFIG
'sqlBusyTimeout',
// Defaults to 1000
'sqlTrace',
// Callback not used by default
'sqlProfile',
// Callback not used by default
'createIndexes'].forEach(prop => {
/** @type {(val: any) => void} */
let validator;
if (Array.isArray(prop)) {
[prop, validator] = prop;
}
Object.defineProperty(CFG, prop, {
get() {
return map[prop];
},
set(val) {
if (validator) {
validator(val);
}
map[prop] = val;
}
});
});
// @ts-nocheck
function getDefaultExportFromCjs(x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var regex$1;
var hasRequiredRegex;
function requireRegex() {
if (hasRequiredRegex) return regex$1;
hasRequiredRegex = 1;
regex$1 = /[\xC0-\xC5\xC7-\xCF\xD1-\xD6\xD9-\xDD\xE0-\xE5\xE7-\xEF\xF1-\xF6\xF9-\xFD\xFF-\u010F\u0112-\u0125\u0128-\u0130\u0134-\u0137\u0139-\u013E\u0143-\u0148\u014C-\u0151\u0154-\u0165\u0168-\u017E\u01A0\u01A1\u01AF\u01B0\u01CD-\u01DC\u01DE-\u01E3\u01E6-\u01F0\u01F4\u01F5\u01F8-\u021B\u021E\u021F\u0226-\u0233\u0344\u0385\u0386\u0388-\u038A\u038C\u038E-\u0390\u03AA-\u03B0\u03CA-\u03CE\u03D3\u03D4\u0400\u0401\u0403\u0407\u040C-\u040E\u0419\u0439\u0450\u0451\u0453\u0457\u045C-\u045E\u0476\u0477\u04C1\u04C2\u04D0-\u04D3\u04D6\u04D7\u04DA-\u04DF\u04E2-\u04E7\u04EA-\u04F5\u04F8\u04F9\u0622-\u0626\u06C0\u06C2\u06D3\u0929\u0931\u0934\u0958-\u095F\u09CB\u09CC\u09DC\u09DD\u09DF\u0A33\u0A36\u0A59-\u0A5B\u0A5E\u0B48\u0B4B\u0B4C\u0B5C\u0B5D\u0B94\u0BCA-\u0BCC\u0C48\u0CC0\u0CC7\u0CC8\u0CCA\u0CCB\u0D4A-\u0D4C\u0DDA\u0DDC-\u0DDE\u0F43\u0F4D\u0F52\u0F57\u0F5C\u0F69\u0F73\u0F75\u0F76\u0F78\u0F81\u0F93\u0F9D\u0FA2\u0FA7\u0FAC\u0FB9\u1026\u1B06\u1B08\u1B0A\u1B0C\u1B0E\u1B12\u1B3B\u1B3D\u1B40\u1B41\u1B43\u1E00-\u1E99\u1E9B\u1EA0-\u1EF9\u1F00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FC1-\u1FC4\u1FC6-\u1FD3\u1FD6-\u1FDB\u1FDD-\u1FEE\u1FF2-\u1FF4\u1FF6-\u1FFC\u212B\u219A\u219B\u21AE\u21CD-\u21CF\u2204\u2209\u220C\u2224\u2226\u2241\u2244\u2247\u2249\u2260\u2262\u226D-\u2271\u2274\u2275\u2278\u2279\u2280\u2281\u2284\u2285\u2288\u2289\u22AC-\u22AF\u22E0-\u22E3\u22EA-\u22ED\u2ADC\u304C\u304E\u3050\u3052\u3054\u3056\u3058\u305A\u305C\u305E\u3060\u3062\u3065\u3067\u3069\u3070\u3071\u3073\u3074\u3076\u3077\u3079\u307A\u307C\u307D\u3094\u309E\u30AC\u30AE\u30B0\u30B2\u30B4\u30B6\u30B8\u30BA\u30BC\u30BE\u30C0\u30C2\u30C5\u30C7\u30C9\u30D0\u30D1\u30D3\u30D4\u30D6\u30D7\u30D9\u30DA\u30DC\u30DD\u30F4\u30F7-\u30FA\u30FE\uAC00-\uD7A3\uFB1D\uFB1F\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFB4E]|\uD801[\uDDC9\uDDE4]|\uD804[\uDC9A\uDC9C\uDCAB\uDD2E\uDD2F\uDF4B\uDF4C\uDF83\uDF85\uDF8E\uDF91\uDFC5\uDFC7\uDFC8]|\uD805[\uDCBB\uDCBC\uDCBE\uDDBA\uDDBB]|\uD806\uDD38|\uD818[\uDD21-\uDD28]|\uD81B[\uDD68-\uDD6A]|\uD834[\uDD5E-\uDD64\uDDBB-\uDDC0]/;
return regex$1;
}
var regexExports = requireRegex();
var regex = /*@__PURE__*/getDefaultExportFromCjs(regexExports);
/* eslint-disable new-cap -- ToString is how it is defined */
/* eslint-disable sonarjs/no-control-regex -- Needed */
/**
* @typedef {number} Integer
*/
/**
* @param {string} arg
* @returns {string}
*/
function escapeUnmatchedSurrogates(arg) {
// http://stackoverflow.com/a/6701665/271577
return arg.replaceAll(/([\uD800-\uDBFF])(?![\uDC00-\uDFFF])|(^|[^\uD800-\uDBFF])([\uDC00-\uDFFF])/gu, function (_, unmatchedHighSurrogate, precedingLow, unmatchedLowSurrogate) {
// Could add a corresponding surrogate for compatibility with `node-sqlite3`: http://bugs.python.org/issue12569 and http://stackoverflow.com/a/6701665/271577
// but Chrome having problems
if (unmatchedHighSurrogate) {
return '^2' + unmatchedHighSurrogate.codePointAt().toString(16).padStart(4, '0');
}
return (precedingLow || '') + '^3' + unmatchedLowSurrogate.codePointAt().toString(16).padStart(4, '0');
});
}
/**
* @param {string} arg
* @returns {string}
*/
function escapeNameForSQLiteIdentifier(arg) {
// http://stackoverflow.com/a/6701665/271577
return '_' +
// Prevent empty string
escapeUnmatchedSurrogates(arg.replaceAll('^', '^^') // Escape our escape
// http://www.sqlite.org/src/tktview?name=57c971fc74
.replaceAll('\0', '^0')
// We need to avoid identifiers being treated as duplicates based on SQLite's ASCII-only case-insensitive table and column names
// (For SQL in general, however, see http://stackoverflow.com/a/17215009/271577
// See also https://www.sqlite.org/faq.html#q18 re: Unicode (non-ASCII) case-insensitive not working
.replaceAll(/([A-Z])/gu, '^$1'));
}
/**
* The escaping of unmatched surrogates was needed by Chrome but not Node.
* @param {string} arg
* @returns {string}
*/
function escapeSQLiteStatement(arg) {
return escapeUnmatchedSurrogates(arg.replaceAll('^', '^^').replaceAll('\0', '^0'));
}
/**
* @param {string} arg
* @returns {string}
*/
function unescapeSQLiteResponse(arg) {
return unescapeUnmatchedSurrogates(arg).replaceAll(/(\^+)0/gu, (_, esc) => {
return esc.length % 2 ? esc.slice(1) + '\0' : _;
}).replaceAll('^^', '^');
}
/**
* @param {string} arg
* @returns {string}
*/
function sqlEscape(arg) {
// https://www.sqlite.org/lang_keywords.html
// http://stackoverflow.com/a/6701665/271577
// There is no need to escape ', `, or [], as
// we should always be within double quotes
// NUL should have already been stripped
return arg.replaceAll('"', '""');
}
/**
* @param {string} arg
* @returns {string}
*/
function sqlQuote(arg) {
return '"' + sqlEscape(arg) + '"';
}
/**
* @param {string} db
* @throws {Error}
* @returns {string}
*/
function escapeDatabaseNameForSQLAndFiles(db) {
if (CFG.escapeDatabaseName) {
// We at least ensure NUL is escaped by default, but we need to still
// handle empty string and possibly also length (potentially
// throwing if too long), escaping casing (including Unicode?),
// and escaping special characters depending on file system
return CFG.escapeDatabaseName(escapeSQLiteStatement(db));
}
db = 'D' + escapeNameForSQLiteIdentifier(db);
if (CFG.escapeNFDForDatabaseNames !== false) {
// ES6 copying of regex with different flags
db = db.replaceAll(new RegExp(regex, 'gu'), function (expandable) {
return '^4' + /** @type {Integer} */expandable.codePointAt(0).toString(16).padStart(6, '0');
});
}
if (CFG.databaseCharacterEscapeList !== false) {
db = db.replace(CFG.databaseCharacterEscapeList ? new RegExp(CFG.databaseCharacterEscapeList, 'gu') : /[\u0000-\u001F\u007F"*/:<>?\\|]/gu,
// eslint-disable-line no-control-regex -- Controls needed
function (n0) {
// eslint-disable-next-line unicorn/prefer-code-point -- Switch to `codePointAt`?
return '^1' + n0.charCodeAt(0).toString(16).padStart(2, '0');
});
}
if (CFG.databaseNameLengthLimit !== false && db.length >= (CFG.databaseNameLengthLimit || 254) - (CFG.addSQLiteExtension !== false ? 7 /* '.sqlite'.length */ : 0)) {
throw new Error('Unexpectedly long database name supplied; length limit required for Node compatibility; passed length: ' + db.length + '; length limit setting: ' + (CFG.databaseNameLengthLimit || 254) + '.');
}
return db + (CFG.addSQLiteExtension !== false ? '.sqlite' : ''); // Shouldn't have quoting (do we even need NUL/case escaping here?)
}
/**
* @param {string} arg
* @returns {string}
*/
function unescapeUnmatchedSurrogates(arg) {
ret