indexeddbshim
Version:
A polyfill for IndexedDB using WebSql
1,383 lines (1,329 loc) • 416 kB
JavaScript
/*! indexeddbshim - v16.0.0 - 6/14/2025 */
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
})((function () { 'use strict';
function _arrayLikeToArray$1(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
function _arrayWithHoles(r) {
if (Array.isArray(r)) return r;
}
function _arrayWithoutHoles(r) {
if (Array.isArray(r)) return _arrayLikeToArray$1(r);
}
function asyncGeneratorStep(n, t, e, r, o, a, c) {
try {
var i = n[a](c),
u = i.value;
} catch (n) {
return void e(n);
}
i.done ? t(u) : Promise.resolve(u).then(r, o);
}
function _asyncToGenerator(n) {
return function () {
var t = this,
e = arguments;
return new Promise(function (r, o) {
var a = n.apply(t, e);
function _next(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
}
function _throw(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
}
_next(void 0);
});
};
}
function _classCallCheck$1(a, n) {
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
}
function _construct(t, e, r) {
if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
var o = [null];
o.push.apply(o, e);
var p = new (t.bind.apply(t, o))();
return p;
}
function _defineProperties(e, r) {
for (var t = 0; t < r.length; t++) {
var o = r[t];
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey$1(o.key), o);
}
}
function _createClass$1(e, r, t) {
return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
writable: false
}), e;
}
function _createForOfIteratorHelper(r, e) {
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (!t) {
if (Array.isArray(r) || (t = _unsupportedIterableToArray$1(r)) || e) {
t && (r = t);
var n = 0,
F = function () {};
return {
s: F,
n: function () {
return n >= r.length ? {
done: true
} : {
done: false,
value: r[n++]
};
},
e: function (r) {
throw r;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var o,
a = true,
u = false;
return {
s: function () {
t = t.call(r);
},
n: function () {
var r = t.next();
return a = r.done, r;
},
e: function (r) {
u = true, o = r;
},
f: function () {
try {
a || null == t.return || t.return();
} finally {
if (u) throw o;
}
}
};
}
function _defineAccessor(e, r, n, t) {
var c = {
configurable: true,
enumerable: true
};
return c[e] = t, Object.defineProperty(r, n, c);
}
function _defineProperty$1(e, r, t) {
return (r = _toPropertyKey$1(r)) in e ? Object.defineProperty(e, r, {
value: t,
enumerable: true,
configurable: true,
writable: true
}) : e[r] = t, e;
}
function _isNativeReflectConstruct() {
try {
var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
} catch (t) {}
return (_isNativeReflectConstruct = function () {
return !!t;
})();
}
function _iterableToArray(r) {
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
}
function _iterableToArrayLimit(r, l) {
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (null != t) {
var e,
n,
i,
u,
a = [],
f = true,
o = false;
try {
if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
} catch (r) {
o = true, n = r;
} finally {
try {
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
} finally {
if (o) throw n;
}
}
return a;
}
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function ownKeys$1(e, r) {
var t = Object.keys(e);
if (Object.getOwnPropertySymbols) {
var o = Object.getOwnPropertySymbols(e);
r && (o = o.filter(function (r) {
return Object.getOwnPropertyDescriptor(e, r).enumerable;
})), t.push.apply(t, o);
}
return t;
}
function _objectSpread2$1(e) {
for (var r = 1; r < arguments.length; r++) {
var t = null != arguments[r] ? arguments[r] : {};
r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) {
_defineProperty$1(e, r, t[r]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) {
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
});
}
return e;
}
function _regenerator() {
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
var e,
t,
r = "function" == typeof Symbol ? Symbol : {},
n = r.iterator || "@@iterator",
o = r.toStringTag || "@@toStringTag";
function i(r, n, o, i) {
var c = n && n.prototype instanceof Generator ? n : Generator,
u = Object.create(c.prototype);
return _regeneratorDefine(u, "_invoke", function (r, n, o) {
var i,
c,
u,
f = 0,
p = o || [],
y = false,
G = {
p: 0,
n: 0,
v: e,
a: d,
f: d.bind(e, 4),
d: function (t, r) {
return i = t, c = 0, u = e, G.n = r, a;
}
};
function d(r, n) {
for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
var o,
i = p[t],
d = G.p,
l = i[2];
r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
}
if (o || r > 1) return a;
throw y = true, n;
}
return function (o, p, l) {
if (f > 1) throw TypeError("Generator is already running");
for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
try {
if (f = 2, i) {
if (c || (o = "next"), t = i[o]) {
if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
if (!t.done) return t;
u = t.value, c < 2 && (c = 0);
} else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
i = e;
} else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
} catch (t) {
i = e, c = 1, u = t;
} finally {
f = 1;
}
}
return {
value: t,
done: y
};
};
}(r, o, i), true), u;
}
var a = {};
function Generator() {}
function GeneratorFunction() {}
function GeneratorFunctionPrototype() {}
t = Object.getPrototypeOf;
var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
return this;
}), t),
u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
function f(e) {
return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
}
return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
return this;
}), _regeneratorDefine(u, "toString", function () {
return "[object Generator]";
}), (_regenerator = function () {
return {
w: i,
m: f
};
})();
}
function _regeneratorDefine(e, r, n, t) {
var i = Object.defineProperty;
try {
i({}, "", {});
} catch (e) {
i = 0;
}
_regeneratorDefine = function (e, r, n, t) {
if (r) i ? i(e, r, {
value: n,
enumerable: !t,
configurable: !t,
writable: !t
}) : e[r] = n;else {
function o(r, n) {
_regeneratorDefine(e, r, function (e) {
return this._invoke(r, n, e);
});
}
o("next", 0), o("throw", 1), o("return", 2);
}
}, _regeneratorDefine(e, r, n, t);
}
function _slicedToArray$1(r, e) {
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray$1(r, e) || _nonIterableRest();
}
function _toConsumableArray(r) {
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray$1(r) || _nonIterableSpread();
}
function _toPrimitive(t, r) {
if ("object" != typeof t || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r);
if ("object" != typeof i) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}
function _toPropertyKey$1(t) {
var i = _toPrimitive(t, "string");
return "symbol" == typeof i ? i : i + "";
}
function _typeof$2(o) {
"@babel/helpers - typeof";
return _typeof$2 = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
return typeof o;
} : function (o) {
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
}, _typeof$2(o);
}
function _unsupportedIterableToArray$1(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray$1(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray$1(r, a) : void 0;
}
}
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}} */
var map = {};
var 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
//