indexeddbshim
Version:
A polyfill for IndexedDB using WebSql
4 lines (3 loc) • 13.5 kB
JavaScript
/*! indexeddbshim - v16.1.0 - 9/1/2025 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).IDBKeyUtils={})}(this,(function(e){"use strict";const t={},r={};function n(e,t){return new DOMException.prototype.constructor(t,e||"DOMException")}["DEBUG","cacheDatabaseInstances","autoName","fullIDLSupport","checkOrigin","cursorPreloadPackSize","UnicodeIDStart","UnicodeIDContinue","registerSCA","avoidAutoShim","win","DEFAULT_DB_SIZE","useSQLiteIndexes","fs","addNonIDBGlobals","replaceNonIDBGlobals","escapeDatabaseName","unescapeDatabaseName","databaseCharacterEscapeList","databaseNameLengthLimit","escapeNFDForDatabaseNames","addSQLiteExtension",["memoryDatabase",e=>{if(!/^(?::memory:|file::memory:(\?[^#]*)?(#.*)?)?$/u.test(e))throw new TypeError('`memoryDatabase` must be the empty string, ":memory:", or a "file::memory:[?queryString][#hash] URL".')}],"deleteDatabaseFiles","databaseBasePath","sysDatabaseBasePath","sqlBusyTimeout","sqlTrace","sqlProfile","createIndexes"].forEach((e=>{let n;Array.isArray(e)&&([e,n]=e),Object.defineProperty(r,e,{get:()=>t[e],set(r){n&&n(r),t[e]=r}})}));const o={IndexSizeError:1,HierarchyRequestError:3,WrongDocumentError:4,InvalidCharacterError:5,NoModificationAllowedError:7,NotFoundError:8,NotSupportedError:9,InUseAttributeError:10,InvalidStateError:11,SyntaxError:12,InvalidModificationError:13,NamespaceError:14,InvalidAccessError:15,TypeMismatchError:17,SecurityError:18,NetworkError:19,AbortError:20,URLMismatchError:21,QuotaExceededError:22,TimeoutError:23,InvalidNodeTypeError:24,DataCloneError:25,EncodingError:0,NotReadableError:0,UnknownError:0,ConstraintError:0,DataError:0,TransactionInactiveError:0,ReadOnlyError:0,VersionError:0,OperationError:0,NotAllowedError:0},i={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};const a=function(){function e(e,t){this[Symbol.toStringTag]="DOMException",this._code=t in o?o[t]:i[t]||0,this._name=t||"Error",this._message=void 0===e?"":""+e,Object.defineProperty(this,"code",{configurable:!0,enumerable:!0,writable:!0,value:this._code}),void 0!==t&&Object.defineProperty(this,"name",{configurable:!0,enumerable:!0,writable:!0,value:this._name}),void 0!==e&&Object.defineProperty(this,"message",{configurable:!0,enumerable:!1,writable:!0,value:this._message})}const t=function(){};return t.prototype=Object.create(Error.prototype),["name","message"].forEach((r=>{Object.defineProperty(t.prototype,r,{enumerable:!0,get(){if(!(this instanceof e||this instanceof t||this instanceof Error))throw new TypeError("Illegal invocation");return this["name"===r?"_name":"_message"]}})})),Object.defineProperty(t.prototype,"code",{configurable:!0,enumerable:!0,get(){throw new TypeError("Illegal invocation")}}),e.prototype=new t,e.prototype[Symbol.toStringTag]="DOMExceptionPrototype",Object.defineProperty(e,"prototype",{writable:!1}),Object.keys(o).forEach((t=>{Object.defineProperty(e.prototype,t,{enumerable:!0,configurable:!1,value:o[t]}),Object.defineProperty(e,t,{enumerable:!0,configurable:!1,value:o[t]})})),Object.keys(i).forEach((t=>{Object.defineProperty(e.prototype,t,{enumerable:!0,configurable:!1,value:i[t]}),Object.defineProperty(e,t,{enumerable:!0,configurable:!1,value:i[t]})})),Object.defineProperty(e.prototype,"constructor",{writable:!0,configurable:!0,enumerable:!1,value:e}),e}();function u(e,t,n){if(r.DEBUG){const r=n&&"object"==typeof n&&n.message?n.message:n,o="function"==typeof console.error?"error":"log";console[o](e+": "+t+". "+(r||"")),console.trace&&console.trace()}}let c,s=!1;try{c=n("test name","test message"),(l=c)&&"object"==typeof l&&"string"==typeof l.name&&"test name"===c.name&&"test message"===c.message&&(s=!0)}catch(e){}var l;const f=s?function(e,t,r){return u(e,t,r),n(e,t)}:function(e,t,r){return u(e,t,r),function(e,t){return new a(t,e)}(e,t)};function y(e){return function(e){return e.replaceAll(/([\uD800-\uDBFF])(?![\uDC00-\uDFFF])|(^|[^\uD800-\uDBFF])([\uDC00-\uDFFF])/gu,(function(e,t,r,n){return t?"^2"+t.codePointAt().toString(16).padStart(4,"0"):(r||"")+"^3"+n.codePointAt().toString(16).padStart(4,"0")}))}(e.replaceAll("^","^^").replaceAll("\0","^0"))}function d(e){return null!==e&&"object"==typeof e}function p(e){return null==e}function E(e,t){const n=L(e),o=L(t),i=n>o?1:n===o?0:-1;if(r.DEBUG){let r=F(n),i=F(o);"object"==typeof e&&(e=JSON.stringify(e),r=JSON.stringify(r)),"object"==typeof t&&(t=JSON.stringify(t),i=JSON.stringify(i)),r!==e&&console.warn(e+" was incorrectly encoded as "+r),i!==t&&console.warn(t+" was incorrectly encoded as "+i)}return i}const b={invalid:100,number:200,date:300,string:400,binary:500,array:600},m=Object.keys(b);m.forEach((e=>{b[e]=String.fromCodePoint(b[e])}));const g=m.reduce(((e,t)=>(e[b[t]]=t,e)),{}),h=["negativeInfinity","bigNegative","smallNegative","smallPositive","bigPositive","positiveInfinity"],v={invalid:{encode:()=>b.invalid+"-",decode(){}},number:{encode(e){let t=e===Number.MIN_VALUE?"0."+"0".repeat(214)+"2":Math.abs(e).toString(32);const r=t.indexOf(".");t=-1!==r?t.replace(".",""):t;const n=t.search(/[^0]/u);let o,i,a;return t=t.slice(n),Number.isFinite(Number(e))?e<0?e>-1?(o=h.indexOf("smallNegative"),i=N(n),a=O(I(t))):(o=h.indexOf("bigNegative"),i=O(N(-1!==r?r:t.length)),a=O(I(t))):e<1?(o=h.indexOf("smallPositive"),i=O(N(n)),a=I(t)):(o=h.indexOf("bigPositive"),i=N(-1!==r?r:t.length),a=I(t)):(i=D(2),a=D(11),o=h.indexOf(e>0?"positiveInfinity":"negativeInfinity")),b.number+"-"+o+i+a},decode(e){const t=Number(e.slice(2,3));let r=e.slice(3,5),n=e.slice(5,16);switch(h[t]){case"negativeInfinity":return Number.NEGATIVE_INFINITY;case"positiveInfinity":return Number.POSITIVE_INFINITY;case"bigPositive":return _(n,r);case"smallPositive":return r=A(O(r)),_(n,r);case"smallNegative":return r=A(r),n=O(n),-_(n,r);case"bigNegative":return r=O(r),n=O(n),-_(n,r);default:throw new Error("Invalid number.")}}},string:{encode:(e,t)=>(t&&(e=e.replaceAll(/(.)/gu,"-$1")+" "),b.string+"-"+e),decode:(e,t)=>(e=e.slice(2),t&&(e=e.slice(0,-1).replaceAll(/-(.)/gu,"$1")),e)},array:{encode(e){const t=[];for(const[r,n]of e.entries()){const e=L(n,!0);t[r]=e}return t.push(b.invalid+"-"),b.array+"-"+JSON.stringify(t)},decode(e){const t=JSON.parse(e.slice(2));t.pop();for(let e=0;e<t.length;e++){const r=F(t[e],!0);t[e]=r}return t}},date:{encode:e=>b.date+"-"+e.toJSON(),decode:e=>new Date(e.slice(2))},binary:{encode:e=>b.binary+"-"+(e.byteLength?[...S(e)].map((e=>String(e).padStart(3,"0"))):""),decode(e){const t=e.slice(2),r=t.length?t.split(",").map((e=>Number.parseInt(e))):[],n=new ArrayBuffer(r.length);return new Uint8Array(n).set(r),n}}};function N(e){const t=e.toString(32);return 1===t.length?"0"+t:t}function I(e){return(e+D(11)).slice(0,11)}function O(e){let t="";for(const r of e)t+=(31-Number.parseInt(r,32)).toString(32);return t}function _(e,t){const r=Number.parseInt(t,32);if(r<0)return R(Number.parseInt(e,32)*32**(r-10));if(r<11){const t=e.slice(0,r),n=Number.parseInt(t,32),o=e.slice(r);return R(n+Number.parseInt(o,32)*32**(r-11))}const n=e+D(r-11);return Number.parseInt(n,32)}function R(e,t=16){return Number.parseFloat(e.toPrecision(t))}function D(e){return"0".repeat(e)}function A(e){return"-"+e}function w(e){if(Array.isArray(e))return"array";if(function(e){return d(e)&&"getDate"in e&&"function"==typeof e.getDate}(e))return"date";if(function(e){return d(e)&&"byteLength"in e&&"number"==typeof e.byteLength&&("slice"in e&&"function"==typeof e.slice||"getFloat64"in e&&"function"==typeof e.getFloat64)}(e))return"binary";const t=typeof e;return["string","number"].includes(t)?t:"invalid"}function T(e,t){return P(e,t,!1,!0)}function S(e){let t=0,r=0;if(ArrayBuffer.isView(e)){if(void 0===e.buffer)throw new TypeError("Could not copy the bytes held by a buffer source as the buffer was undefined.");t=e.byteOffset,r=e.byteLength}else r=e.byteLength;return new Uint8Array("buffer"in e&&e.buffer||e,t,r)}function P(e,t,r,n){if((t=t||[]).includes(e))return{type:"array",invalid:!0,message:"An array key cannot be circular"};const o=w(e),i={type:o,value:e};switch(o){case"number":return Number.isNaN(e)?{type:"NaN",invalid:!0}:Object.is(e,-0)?{type:o,value:0}:i;case"string":return i;case"binary":return{type:"binary",value:S(e)};case"array":{const i=e,a=i.length;t.push(e);const u=[];for(let e=0;e<a;e++){if(!r&&!Object.hasOwn(i,e))return{type:o,invalid:!0,message:"Does not have own index property"};try{const a=P(i[e],t,!1,n);if(a.invalid){if(r)continue;return{type:o,invalid:!0,message:"Bad array entry value-to-key conversion"}}(!r||!n&&u.every((e=>0!==E(e,a.value)))||n&&u.every((e=>0!==E(e,a))))&&u.push(n?a:a.value)}catch(e){if(!r)throw e}}return{type:o,value:u}}case"date":{const t=e;return Number.isNaN(t.getTime())?{type:o,invalid:!0,message:"Not a valid date"}:n?{type:o,value:t.getTime()}:{type:o,value:new Date(t)}}default:{const t=null===e?"null":typeof e;return{type:t,invalid:!0,message:"Not a valid key; type "+t}}}}function M(e,t){return P(e,null,!0,t)}function U(e,t,r,n){const o=C(e,t);return o.failure?o:r?M(o.value,n):P(o.value,null,!1,n)}function C(e,t,r,n){if(Array.isArray(t)){const r=[];return t.some((t=>{const n=C(e,t);return!!n.failure||(r.push(n.value),!1)}))?{failure:!0}:{value:r}}if(""===t)return{value:e};return t.split(".").some((t=>{if("length"!==t||"string"!=typeof e&&!Array.isArray(e))if(function(e){return d(e)&&"size"in e&&"number"==typeof e.size&&"slice"in e&&"function"==typeof e.slice&&!("lastModified"in e)}(e))switch(t){case"size":case"type":e=e[t]}else{if(!function(e){return d(e)&&"name"in e&&"string"==typeof e.name&&"slice"in e&&"function"==typeof e.slice&&"lastModified"in e}(e))return!d(e)||!Object.hasOwn(e,t)||void 0===(e=e[t]);switch(t){case"name":case"lastModified":e=e[t];break;case"lastModifiedDate":e=new Date(e.lastModified)}}else e=e.length;return!1}))?{failure:!0}:{value:e}}function j(e,t,r){let n=void 0===t.lower,o=void 0===t.upper;const i=L(e,!0),a=r?t.__lowerCached:L(t.lower,!0),u=r?t.__upperCached:L(t.upper,!0);return!n&&(t.lowerOpen&&null!==i&&null!==a&&i>a||!t.lowerOpen&&(!i&&!a||null!==i&&null!==a&&i>=a))&&(n=!0),!o&&(t.upperOpen&&null!==i&&null!==u&&i<u||!t.upperOpen&&(!i&&!u||null!==i&&null!==u&&i<=u))&&(o=!0),n&&o}function L(e,t){return void 0===e?null:v[w(e)].encode(e,t)}function F(e,t){if("string"==typeof e)return v[g[e.slice(0,1)]].decode(e,t)}const x=9007199254740992;function V(e,t,r,n){e.executeSql('SELECT "currNum" FROM __sys__ WHERE "name" = ?',[y(t.__currentName)],(function(e,t){1!==t.rows.length?r(1):r(t.rows.item(0).currNum)}),(function(e,t){return n(f("DataError","Could not get the auto increment value for key",t)),!1}))}function B(e,t,n,o,i){const a='UPDATE __sys__ SET "currNum" = ? WHERE "name" = ?',u=[n,y(t.__currentName)];r.DEBUG&&console.log(a,u),e.executeSql(a,u,(function(){o(n)}),(function(e,t){return i(f("UnknownError","Could not set the auto increment value for key",t)),!1}))}function K(e,t,r,n,o){return B(e,t,r=r===x?r+2:r+1,n,o)}e.assignCurrentNumber=B,e.checkKeyCouldBeInjectedIntoValue=function(e,t){const r=t.split(".");r.pop();for(const t of r){if(!d(e))return!1;if(!Object.hasOwn(e,t))return!0;e=e[t]}return d(e)},e.convertKeyToValue=function e(t){const{type:r,value:n}=t;switch(r){case"number":case"string":return n;case"array":{const t=[],r=n.length;let o=0;for(;o<r;){const r=e(n[o]);t[o]=r,o++}return t}case"date":return new Date(n);case"binary":{const e=n.length,t=new ArrayBuffer(e);return new Uint8Array(t,n.byteOffset||0,n.byteLength).set(n),t}default:throw new Error("Bad key")}},e.convertValueToKey=T,e.convertValueToKeyRethrowingAndIfInvalid=function(e,t){const r=T(e,t);if(r.invalid)throw f("DataError",r.message||"Not a valid key; type: "+r.type);return r},e.convertValueToKeyValueDecoded=P,e.convertValueToMultiEntryKey=function(e){return P(e,null,!0,!0)},e.convertValueToMultiEntryKeyDecoded=M,e.decode=F,e.encode=L,e.evaluateKeyPathOnValue=function(e,t,r){return C(e,t)},e.extractKeyFromValueUsingKeyPath=function(e,t,r){return U(e,t,r,!0)},e.extractKeyValueDecodedFromValueUsingKeyPath=U,e.findMultiEntryMatches=function e(t,r){const n=[];if(Array.isArray(t))for(let o of t){if(Array.isArray(o)){if(r&&r.lower===r.upper)continue;if(1!==o.length){e(o,r).length>0&&n.push(o);continue}o=o[0]}(p(r)||j(o,r,!0))&&n.push(o)}else(p(r)||j(t,r,!0))&&n.push(t);return n},e.generateKeyForStore=function(e,t,r,n){V(e,t,(function(o){o>x?r("failure"):K(e,t,o,(function(){r(null,o,o)}),n)}),n)},e.injectKeyIntoValueUsingKeyPath=function(e,t,r){const n=r.split("."),o=n.pop();n.forEach((t=>{Object.hasOwn(e,t)||(e[t]={}),e=e[t]})),e[o]=t},e.isKeyInRange=j,e.isMultiEntryMatch=function(e,t){return"array"===g[t.slice(0,1)]?t.indexOf(e)>1:t===e},e.possiblyUpdateKeyGenerator=function(e,t,r,n,o){"number"!=typeof r||r<1?n():V(e,t,(function(i){const a=Math.floor(Math.min(r,x));a>=i?K(e,t,a,(function(){n(i)}),o):n()}),o)},e.roundTrip=function(e,t){return F(L(e,t),t)}}));
//# sourceMappingURL=indexeddbshim-Key.min.js.map