s3db.js 8.1.3 → 8.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cli.js +430 -0
- package/dist/s3db.cjs.js +41 -25
- package/dist/s3db.cjs.min.js +1 -1
- package/dist/s3db.es.js +41 -25
- package/dist/s3db.es.min.js +1 -1
- package/dist/s3db.iife.js +41 -25
- package/dist/s3db.iife.min.js +1 -1
- package/mcp/server.js +2 -1
- package/package.json +6 -2
- package/src/database.class.js +3 -2
- package/src/plugins/audit.plugin.js +43 -22
package/dist/s3db.iife.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var S3DB=function(e,t,r,i,s,n,o,a,c,l,u,h,f){"use strict";const d="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",p=Object.fromEntries([...d].map((e,t)=>[e,t])),m=e=>{if("number"!=typeof e||isNaN(e))return"undefined";if(!isFinite(e))return"undefined";if(0===e)return d[0];if(e<0)return"-"+m(-Math.floor(e));e=Math.floor(e);let t="";for(;e;)t=d[e%62]+t,e=Math.floor(e/62);return t},g=e=>{if("string"!=typeof e)return NaN;if(""===e)return 0;let t=!1;"-"===e[0]&&(t=!0,e=e.slice(1));let r=0;for(let t=0;t<e.length;t++){const i=p[e[t]];if(void 0===i)return NaN;r=62*r+i}return t?-r:r},y=e=>{if("number"!=typeof e||isNaN(e))return"undefined";if(!isFinite(e))return"undefined";const t=e<0;e=Math.abs(e);const[r,i]=e.toString().split("."),s=m(Number(r));return i?(t?"-":"")+s+"."+i:(t?"-":"")+s},b=e=>{if("string"!=typeof e)return NaN;let t=!1;"-"===e[0]&&(t=!0,e=e.slice(1));const[r,i]=e.split("."),s=g(r);if(isNaN(s))return NaN;const n=i?Number(s+"."+i):s;return t?-n:n};function w(e){"string"!=typeof e&&(e=String(e));let t=0;for(let r=0;r<e.length;r++){const i=e.codePointAt(r);i<=127?t+=1:i<=2047?t+=2:i<=65535?t+=3:i<=1114111&&(t+=4,i>65535&&r++)}return t}function v(e){let t=0;for(const r of Object.keys(e))t+=w(r);return t}function k(e){return null==e?"":"boolean"==typeof e?e?"1":"0":"number"==typeof e?String(e):"string"==typeof e?e:Array.isArray(e)?0===e.length?"[]":e.map(e=>String(e)).join("|"):"object"==typeof e?JSON.stringify(e):String(e)}function _(e){const t={};for(const[r,i]of Object.entries(e)){const e=w(k(i));t[r]=e}return t}function S(e){const t=_(e);return Object.values(t).reduce((e,t)=>e+t,0)+v(e)}function O(e={}){const{version:t="1",timestamps:r=!1,id:i=""}=e,s={_v:String(t)};r&&(s.createdAt="2024-01-01T00:00:00.000Z",s.updatedAt="2024-01-01T00:00:00.000Z"),i&&(s.id=i);const n={};for(const[e,t]of Object.entries(s))n[e]=t;return S(n)}function R(e={}){const{s3Limit:t=2048,systemConfig:r={}}=e;return t-O(r)}class j extends Error{constructor({verbose:e,bucket:t,key:r,message:i,code:s,statusCode:n,requestId:o,awsMessage:a,original:c,commandName:l,commandInput:u,metadata:h,suggestion:f,...d}){e&&(i+=`\n\nVerbose:\n\n${JSON.stringify(d,null,2)}`),super(i),"function"==typeof Error.captureStackTrace?Error.captureStackTrace(this,this.constructor):this.stack=new Error(i).stack,super.name=this.constructor.name,this.name=this.constructor.name,this.bucket=t,this.key=r,this.thrownAt=new Date,this.code=s,this.statusCode=n,this.requestId=o,this.awsMessage=a,this.original=c,this.commandName=l,this.commandInput=u,this.metadata=h,this.suggestion=f,this.data={bucket:t,key:r,...d,verbose:e,message:i}}toJson(){return{name:this.name,message:this.message,code:this.code,statusCode:this.statusCode,requestId:this.requestId,awsMessage:this.awsMessage,bucket:this.bucket,key:this.key,thrownAt:this.thrownAt,commandName:this.commandName,commandInput:this.commandInput,metadata:this.metadata,suggestion:this.suggestion,data:this.data,original:this.original,stack:this.stack}}toString(){return`${this.name} | ${this.message}`}}class x extends j{constructor(e,t={}){let r,i,s,n,o,a;t.original&&(o=t.original,r=o.code||o.Code||o.name,i=o.statusCode||o.$metadata&&o.$metadata.httpStatusCode,s=o.requestId||o.$metadata&&o.$metadata.requestId,n=o.message,a=o.$metadata?{...o.$metadata}:void 0),super({message:e,...t,code:r,statusCode:i,requestId:s,awsMessage:n,original:o,metadata:a})}}class E extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}}class A extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}}class D extends x{constructor({bucket:e,resourceName:t,id:r,original:i,...s}){if("string"!=typeof r)throw new Error("id must be a string");if("string"!=typeof e)throw new Error("bucket must be a string");if("string"!=typeof t)throw new Error("resourceName must be a string");super(`Resource not found: ${t}/${r} [bucket:${e}]`,{bucket:e,resourceName:t,id:r,original:i,...s})}}class C extends x{constructor({bucket:e,original:t,...r}){if("string"!=typeof e)throw new Error("bucket must be a string");super(`Bucket does not exists [bucket:${e}]`,{bucket:e,original:t,...r})}}class N extends x{constructor({bucket:e,key:t,resourceName:r,id:i,original:s,...n}){if("string"!=typeof t)throw new Error("key must be a string");if("string"!=typeof e)throw new Error("bucket must be a string");if(void 0!==i&&"string"!=typeof i)throw new Error("id must be a string");super(`No such key: ${t} [bucket:${e}]`,{bucket:e,key:t,resourceName:r,id:i,original:s,...n}),this.resourceName=r,this.id=i}}class $ extends x{constructor({bucket:e,key:t,resourceName:r,id:i,original:s,...n}){if("string"!=typeof t)throw new Error("key must be a string");if("string"!=typeof e)throw new Error("bucket must be a string");super(`Not found: ${t} [bucket:${e}]`,{bucket:e,key:t,resourceName:r,id:i,original:s,...n}),this.resourceName=r,this.id=i}}class P extends x{constructor({bucket:e,original:t,...r}){if("string"!=typeof e)throw new Error("bucket must be a string");super(`Missing metadata for bucket [bucket:${e}]`,{bucket:e,original:t,...r})}}class I extends x{constructor({bucket:e,resourceName:t,attributes:r,validation:i,message:s,original:n,...o}){if("string"!=typeof e)throw new Error("bucket must be a string");if("string"!=typeof t)throw new Error("resourceName must be a string");super(s||`Validation error: This item is not valid. Resource=${t} [bucket:${e}].\n${JSON.stringify(i,null,2)}`,{bucket:e,resourceName:t,attributes:r,validation:i,original:n,...o})}}class T extends x{}const M={NotFound:$,NoSuchKey:N,UnknownError:T,NoSuchBucket:C,MissingMetadata:P,InvalidResourceItem:I};function L(e,t={}){const r=e.code||e.Code||e.name,i=e.$metadata?{...e.$metadata}:void 0,s=t.commandName,n=t.commandInput;let o;return"NoSuchKey"===r||"NotFound"===r?(o="Check if the key exists in the specified bucket and if your credentials have permission.",new N({...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"NoSuchBucket"===r?(o="Check if the bucket exists and if your credentials have permission.",new C({...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"AccessDenied"===r||403===e.statusCode||"Forbidden"===r?(o="Check your credentials and bucket policy.",new A("Access denied",{...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"ValidationError"===r||400===e.statusCode?(o="Check the request parameters and payload.",new E("Validation error",{...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"MissingMetadata"===r?(o="Check if the object metadata is present and valid.",new P({...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):(o="Check the error details and AWS documentation.",new T("Unknown error",{...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o}))}class U extends x{constructor(e,t={}){super(e,{...t,suggestion:"Check the connection string format and credentials."})}}class F extends x{constructor(e,t={}){super(e,{...t,suggestion:"Check if the crypto library is available and input is valid."})}}class B extends x{constructor(e,t={}){super(e,{...t,suggestion:"Check schema definition and input data."})}}class q extends x{constructor(e,t={}){super(e,{...t,suggestion:t.suggestion||"Check resource configuration, attributes, and operation context."}),Object.assign(this,t)}}class z extends x{constructor(e,t={}){super(e,{...t,suggestion:t.suggestion||"Check partition definition, fields, and input values."})}}function H(e){if(null==e){const e=new Error("fnOrPromise cannot be null or undefined");return e.stack=(new Error).stack,[!1,e,void 0]}if("function"==typeof e)try{const t=e();return null==t?[!0,null,t]:"function"==typeof t.then?t.then(e=>[!0,null,e]).catch(e=>{if(e instanceof Error&&Object.isExtensible(e)){const t=Object.getOwnPropertyDescriptor(e,"stack");if(t&&t.writable&&t.configurable&&e.hasOwnProperty("stack"))try{e.stack=(new Error).stack}catch(e){}}return[!1,e,void 0]}):[!0,null,t]}catch(e){if(e instanceof Error&&Object.isExtensible(e)){const t=Object.getOwnPropertyDescriptor(e,"stack");if(t&&t.writable&&t.configurable&&e.hasOwnProperty("stack"))try{e.stack=(new Error).stack}catch(e){}}return[!1,e,void 0]}return"function"==typeof e.then?Promise.resolve(e).then(e=>[!0,null,e]).catch(e=>{if(e instanceof Error&&Object.isExtensible(e)){const t=Object.getOwnPropertyDescriptor(e,"stack");if(t&&t.writable&&t.configurable&&e.hasOwnProperty("stack"))try{e.stack=(new Error).stack}catch(e){}}return[!1,e,void 0]}):[!0,null,e]}function V(e){try{return[!0,null,e()]}catch(e){return[!1,e,null]}}var K=H;async function W(){let e;if("undefined"!=typeof process){const[t,r,i]=await K(async()=>{const{webcrypto:e}=await import("crypto");return e});if(!t)throw new F("Crypto API not available",{original:r,context:"dynamicCrypto"});e=i}else"undefined"!=typeof window&&(e=window.crypto);if(!e)throw new F("Could not load any crypto library",{context:"dynamicCrypto"});return e}async function J(e){const[t,r,i]=await K(W);if(!t)throw new F("Crypto API not available",{original:r});const s=(new TextEncoder).encode(e),[n,o,a]=await K(()=>i.subtle.digest("SHA-256",s));if(!n)throw new F("SHA-256 digest failed",{original:o,input:e});return Array.from(new Uint8Array(a)).map(e=>e.toString(16).padStart(2,"0")).join("")}async function G(e,t){const[r,i,s]=await K(W);if(!r)throw new F("Crypto API not available",{original:i});const n=s.getRandomValues(new Uint8Array(16)),[o,a,c]=await K(()=>Z(t,n));if(!o)throw new F("Key derivation failed",{original:a,passphrase:t,salt:n});const l=s.getRandomValues(new Uint8Array(12)),u=(new TextEncoder).encode(e),[h,f,d]=await K(()=>s.subtle.encrypt({name:"AES-GCM",iv:l},c,u));if(!h)throw new F("Encryption failed",{original:f,content:e});const p=new Uint8Array(n.length+l.length+d.byteLength);return p.set(n),p.set(l,n.length),p.set(new Uint8Array(d),n.length+l.length),function(e){if("undefined"!=typeof process)return Buffer.from(e).toString("base64");{const[t,r,i]=V(()=>String.fromCharCode.apply(null,new Uint8Array(e)));if(!t)throw new F("Failed to convert ArrayBuffer to base64 (browser)",{original:r});return window.btoa(i)}}(p)}async function Y(e,t){const[r,i,s]=await K(W);if(!r)throw new F("Crypto API not available",{original:i});const n=function(e){if("undefined"!=typeof process)return new Uint8Array(Buffer.from(e,"base64"));{const[t,r,i]=V(()=>window.atob(e));if(!t)throw new F("Failed to decode base64 (browser)",{original:r});const s=i.length,n=new Uint8Array(s);for(let e=0;e<s;e++)n[e]=i.charCodeAt(e);return n}}(e),o=n.slice(0,16),a=n.slice(16,28),c=n.slice(28),[l,u,h]=await K(()=>Z(t,o));if(!l)throw new F("Key derivation failed (decrypt)",{original:u,passphrase:t,salt:o});const[f,d,p]=await K(()=>s.subtle.decrypt({name:"AES-GCM",iv:a},h,c));if(!f)throw new F("Decryption failed",{original:d,encryptedBase64:e});return(new TextDecoder).decode(p)}async function Q(e){if("undefined"==typeof process)throw new F("MD5 hashing is only available in Node.js environment",{context:"md5"});const[t,r,i]=await K(async()=>{const{createHash:t}=await import("crypto");return t("md5").update(e).digest("base64")});if(!t)throw new F("MD5 hashing failed",{original:r,data:e});return i}async function Z(e,t){const[r,i,s]=await K(W);if(!r)throw new F("Crypto API not available",{original:i});const n=(new TextEncoder).encode(e),[o,a,c]=await K(()=>s.subtle.importKey("raw",n,{name:"PBKDF2"},!1,["deriveKey"]));if(!o)throw new F("importKey failed",{original:a,passphrase:e});const[l,u,h]=await K(()=>s.subtle.deriveKey({name:"PBKDF2",salt:t,iterations:1e5,hash:"SHA-256"},c,{name:"AES-GCM",length:256},!0,["encrypt","decrypt"]));if(!l)throw new F("deriveKey failed",{original:u,passphrase:e,salt:t});return h}const X=t.customAlphabet(t.urlAlphabet,22),ee=t.customAlphabet("ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789",16);var te,re=Object.freeze({__proto__:null,idGenerator:X,passwordGenerator:ee});function ie(){}function se(){se.init.call(this)}function ne(e){return void 0===e._maxListeners?se.defaultMaxListeners:e._maxListeners}function oe(e,t,r,i){var s,n,o,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((n=e._events)?(n.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),n=e._events),o=n[t]):(n=e._events=new ie,e._eventsCount=0),o){if("function"==typeof o?o=n[t]=i?[r,o]:[o,r]:i?o.unshift(r):o.push(r),!o.warned&&(s=ne(e))&&s>0&&o.length>s){o.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=o.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else o=n[t]=r,++e._eventsCount;return e}function ae(e,t,r){var i=!1;function s(){e.removeListener(t,s),i||(i=!0,r.apply(e,arguments))}return s.listener=r,s}function ce(e){var t=this._events;if(t){var r=t[e];if("function"==typeof r)return 1;if(r)return r.length}return 0}function le(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}ie.prototype=Object.create(null),se.EventEmitter=se,se.usingDomains=!1,se.prototype.domain=void 0,se.prototype._events=void 0,se.prototype._maxListeners=void 0,se.defaultMaxListeners=10,se.init=function(){this.domain=null,se.usingDomains&&(!te.active||this instanceof te.Domain||(this.domain=te.active)),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new ie,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},se.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},se.prototype.getMaxListeners=function(){return ne(this)},se.prototype.emit=function(e){var t,r,i,s,n,o,a,c="error"===e;if(o=this._events)c=c&&null==o.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var l=new Error('Uncaught, unspecified "error" event. ('+t+")");throw l.context=t,l}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(r=o[e]))return!1;var u="function"==typeof r;switch(i=arguments.length){case 1:!function(e,t,r){if(t)e.call(r);else for(var i=e.length,s=le(e,i),n=0;n<i;++n)s[n].call(r)}(r,u,this);break;case 2:!function(e,t,r,i){if(t)e.call(r,i);else for(var s=e.length,n=le(e,s),o=0;o<s;++o)n[o].call(r,i)}(r,u,this,arguments[1]);break;case 3:!function(e,t,r,i,s){if(t)e.call(r,i,s);else for(var n=e.length,o=le(e,n),a=0;a<n;++a)o[a].call(r,i,s)}(r,u,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,i,s,n){if(t)e.call(r,i,s,n);else for(var o=e.length,a=le(e,o),c=0;c<o;++c)a[c].call(r,i,s,n)}(r,u,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(i-1),n=1;n<i;n++)s[n-1]=arguments[n];!function(e,t,r,i){if(t)e.apply(r,i);else for(var s=e.length,n=le(e,s),o=0;o<s;++o)n[o].apply(r,i)}(r,u,this,s)}return!0},se.prototype.addListener=function(e,t){return oe(this,e,t,!1)},se.prototype.on=se.prototype.addListener,se.prototype.prependListener=function(e,t){return oe(this,e,t,!0)},se.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,ae(this,e,t)),this},se.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,ae(this,e,t)),this},se.prototype.removeListener=function(e,t){var r,i,s,n,o;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(i=this._events))return this;if(!(r=i[e]))return this;if(r===t||r.listener&&r.listener===t)0===--this._eventsCount?this._events=new ie:(delete i[e],i.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(s=-1,n=r.length;n-- >0;)if(r[n]===t||r[n].listener&&r[n].listener===t){o=r[n].listener,s=n;break}if(s<0)return this;if(1===r.length){if(r[0]=void 0,0===--this._eventsCount)return this._events=new ie,this;delete i[e]}else!function(e,t){for(var r=t,i=r+1,s=e.length;i<s;r+=1,i+=1)e[r]=e[i];e.pop()}(r,s);i.removeListener&&this.emit("removeListener",e,o||t)}return this},se.prototype.off=function(e,t){return this.removeListener(e,t)},se.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new ie,this._eventsCount=0):r[e]&&(0===--this._eventsCount?this._events=new ie:delete r[e]),this;if(0===arguments.length){for(var i,s=Object.keys(r),n=0;n<s.length;++n)"removeListener"!==(i=s[n])&&this.removeAllListeners(i);return this.removeAllListeners("removeListener"),this._events=new ie,this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},se.prototype.listeners=function(e){var t,r=this._events;return r&&(t=r[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),r=0;r<t.length;++r)t[r]=e[r].listener||e[r];return t}(t):[]},se.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):ce.call(e,t)},se.prototype.listenerCount=ce,se.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var ue=class extends se{constructor(e={}){super(),this.name=this.constructor.name,this.options=e,this.hooks=new Map}async setup(e){this.database=e,this.beforeSetup(),await this.onSetup(),this.afterSetup()}async start(){this.beforeStart(),await this.onStart(),this.afterStart()}async stop(){this.beforeStop(),await this.onStop(),this.afterStop()}async onSetup(){}async onStart(){}async onStop(){}addHook(e,t,r){this.hooks.has(e)||this.hooks.set(e,new Map);const i=this.hooks.get(e);i.has(t)||i.set(t,[]),i.get(t).push(r)}removeHook(e,t,r){const i=this.hooks.get(e);if(i&&i.has(t)){const e=i.get(t),s=e.indexOf(r);s>-1&&e.splice(s,1)}}wrapResourceMethod(e,t,r){const i=e[t];if(e._pluginWrappers||(e._pluginWrappers=new Map),e._pluginWrappers.has(t)||e._pluginWrappers.set(t,[]),e._pluginWrappers.get(t).push(r),!e[`_wrapped_${t}`]){e[`_wrapped_${t}`]=i;const r=i&&i._isMockFunction;e[t]=async function(...r){let i=await e[`_wrapped_${t}`](...r);for(const s of e._pluginWrappers.get(t))i=await s.call(this,i,r,t);return i},r&&(Object.setPrototypeOf(e[t],Object.getPrototypeOf(i)),Object.assign(e[t],i))}}addMiddleware(e,t,r){if(e._pluginMiddlewares||(e._pluginMiddlewares={}),!e._pluginMiddlewares[t]){e._pluginMiddlewares[t]=[];const r=e[t].bind(e);e[t]=async function(...i){let s=-1;const n=async(...i)=>(s++,s<e._pluginMiddlewares[t].length?await e._pluginMiddlewares[t][s].call(this,n,...i):await r(...i));return await n(...i)}}e._pluginMiddlewares[t].push(r)}getPartitionValues(e,t){if(!t.config?.partitions)return{};const r={};for(const[i,s]of Object.entries(t.config.partitions))if(s.fields){r[i]={};for(const[n,o]of Object.entries(s.fields)){const s=this.getNestedFieldValue(e,n);null!=s&&(r[i][n]=t.applyPartitionRule(s,o))}}else r[i]={};return r}getNestedFieldValue(e,t){if(!t.includes("."))return e[t]??null;const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return null;i=i[e]}return i??null}beforeSetup(){this.emit("plugin.beforeSetup",new Date)}afterSetup(){this.emit("plugin.afterSetup",new Date)}beforeStart(){this.emit("plugin.beforeStart",new Date)}afterStart(){this.emit("plugin.afterStart",new Date)}beforeStop(){this.emit("plugin.beforeStop",new Date)}afterStop(){this.emit("plugin.afterStop",new Date)}};const he={setup(e){},start(){},stop(){}};function fe(e,t){for(var r=0,i=e.length-1;i>=0;i--){var s=e[i];"."===s?e.splice(i,1):".."===s?(e.splice(i,1),r++):r&&(e.splice(i,1),r--)}if(t)for(;r--;r)e.unshift("..");return e}var de=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,pe=function(e){return de.exec(e).slice(1)};function me(){for(var e="",t=!1,r=arguments.length-1;r>=-1&&!t;r--){var i=r>=0?arguments[r]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(e=i+"/"+e,t="/"===i.charAt(0))}return(t?"/":"")+(e=fe(ve(e.split("/"),function(e){return!!e}),!t).join("/"))||"."}function ge(e){var t=ye(e),r="/"===ke(e,-1);return(e=fe(ve(e.split("/"),function(e){return!!e}),!t).join("/"))||t||(e="."),e&&r&&(e+="/"),(t?"/":"")+e}function ye(e){return"/"===e.charAt(0)}function be(){return ge(ve(Array.prototype.slice.call(arguments,0),function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))}var we={extname:function(e){return pe(e)[3]},basename:function(e,t){var r=pe(e)[2];return t&&r.substr(-1*t.length)===t&&(r=r.substr(0,r.length-t.length)),r},dirname:function(e){var t=pe(e),r=t[0],i=t[1];return r||i?(i&&(i=i.substr(0,i.length-1)),r+i):"."},sep:"/",delimiter:":",relative:function(e,t){function r(e){for(var t=0;t<e.length&&""===e[t];t++);for(var r=e.length-1;r>=0&&""===e[r];r--);return t>r?[]:e.slice(t,r-t+1)}e=me(e).substr(1),t=me(t).substr(1);for(var i=r(e.split("/")),s=r(t.split("/")),n=Math.min(i.length,s.length),o=n,a=0;a<n;a++)if(i[a]!==s[a]){o=a;break}var c=[];for(a=o;a<i.length;a++)c.push("..");return(c=c.concat(s.slice(o))).join("/")},join:be,isAbsolute:ye,normalize:ge,resolve:me};function ve(e,t){if(e.filter)return e.filter(t);for(var r=[],i=0;i<e.length;i++)t(e[i],i,e)&&r.push(e[i]);return r}var ke="b"==="ab".substr(-1)?function(e,t,r){return e.substr(t,r)}:function(e,t,r){return t<0&&(t=e.length+t),e.substr(t,r)};class _e extends se{constructor(e={}){super(),this.config=e}async _set(e,t){}async _get(e){}async _del(e){}async _clear(e){}validateKey(e){if(null==e||"string"!=typeof e||!e)throw new Error("Invalid key")}async set(e,t){return this.validateKey(e),await this._set(e,t),this.emit("set",t),t}async get(e){this.validateKey(e);const t=await this._get(e);return this.emit("get",t),t}async del(e){this.validateKey(e);const t=await this._del(e);return this.emit("delete",t),t}async delete(e){return this.del(e)}async clear(e){const t=await this._clear(e);return this.emit("clear",t),t}}function Se(){throw new Error("setTimeout has not been defined")}function Oe(){throw new Error("clearTimeout has not been defined")}var Re=Se,je=Oe;function xe(e){if(Re===setTimeout)return setTimeout(e,0);if((Re===Se||!Re)&&setTimeout)return Re=setTimeout,setTimeout(e,0);try{return Re(e,0)}catch(t){try{return Re.call(null,e,0)}catch(t){return Re.call(this,e,0)}}}"function"==typeof global.setTimeout&&(Re=setTimeout),"function"==typeof global.clearTimeout&&(je=clearTimeout);var Ee,Ae=[],De=!1,Ce=-1;function Ne(){De&&Ee&&(De=!1,Ee.length?Ae=Ee.concat(Ae):Ce=-1,Ae.length&&$e())}function $e(){if(!De){var e=xe(Ne);De=!0;for(var t=Ae.length;t;){for(Ee=Ae,Ae=[];++Ce<t;)Ee&&Ee[Ce].run();Ce=-1,t=Ae.length}Ee=null,De=!1,function(e){if(je===clearTimeout)return clearTimeout(e);if((je===Oe||!je)&&clearTimeout)return je=clearTimeout,clearTimeout(e);try{return je(e)}catch(t){try{return je.call(null,e)}catch(t){return je.call(this,e)}}}(e)}}function Pe(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];Ae.push(new Ie(e,t)),1!==Ae.length||De||xe($e)}function Ie(e,t){this.fun=e,this.array=t}Ie.prototype.run=function(){this.fun.apply(null,this.array)};var Te,Me={env:{}};Te="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var r=function(){};r.prototype=t.prototype,e.prototype=new r,e.prototype.constructor=e};var Le=/%[sdj%]/g;function Ue(e){if(!Qe(e)){for(var t=[],r=0;r<arguments.length;r++)t.push(ze(arguments[r]));return t.join(" ")}r=1;for(var i=arguments,s=i.length,n=String(e).replace(Le,function(e){if("%%"===e)return"%";if(r>=s)return e;switch(e){case"%s":return String(i[r++]);case"%d":return Number(i[r++]);case"%j":try{return JSON.stringify(i[r++])}catch(e){return"[Circular]"}default:return e}}),o=i[r];r<s;o=i[++r])Ye(o)||!et(o)?n+=" "+o:n+=" "+ze(o);return n}function Fe(e,t){if(Ze(global.process))return function(){return Fe(e,t).apply(this,arguments)};if(!0===Me.noDeprecation)return e;var r=!1;return function(){if(!r){if(Me.throwDeprecation)throw new Error(t);Me.traceDeprecation?console.trace(t):console.error(t),r=!0}return e.apply(this,arguments)}}var Be,qe={};function ze(e,t){var r={seen:[],stylize:Ve};return arguments.length>=3&&(r.depth=arguments[2]),arguments.length>=4&&(r.colors=arguments[3]),Ge(t)?r.showHidden=t:t&&function(e,t){if(!t||!et(t))return e;var r=Object.keys(t),i=r.length;for(;i--;)e[r[i]]=t[r[i]]}(r,t),Ze(r.showHidden)&&(r.showHidden=!1),Ze(r.depth)&&(r.depth=2),Ze(r.colors)&&(r.colors=!1),Ze(r.customInspect)&&(r.customInspect=!0),r.colors&&(r.stylize=He),Ke(r,e,r.depth)}function He(e,t){var r=ze.styles[t];return r?"["+ze.colors[r][0]+"m"+e+"["+ze.colors[r][1]+"m":e}function Ve(e,t){return e}function Ke(e,t,r){if(e.customInspect&&t&&it(t.inspect)&&t.inspect!==ze&&(!t.constructor||t.constructor.prototype!==t)){var i=t.inspect(r,e);return Qe(i)||(i=Ke(e,i,r)),i}var s=function(e,t){if(Ze(t))return e.stylize("undefined","undefined");if(Qe(t)){var r="'"+JSON.stringify(t).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return e.stylize(r,"string")}if(i=t,"number"==typeof i)return e.stylize(""+t,"number");var i;if(Ge(t))return e.stylize(""+t,"boolean");if(Ye(t))return e.stylize("null","null")}(e,t);if(s)return s;var n=Object.keys(t),o=function(e){var t={};return e.forEach(function(e,r){t[e]=!0}),t}(n);if(e.showHidden&&(n=Object.getOwnPropertyNames(t)),rt(t)&&(n.indexOf("message")>=0||n.indexOf("description")>=0))return We(t);if(0===n.length){if(it(t)){var a=t.name?": "+t.name:"";return e.stylize("[Function"+a+"]","special")}if(Xe(t))return e.stylize(RegExp.prototype.toString.call(t),"regexp");if(tt(t))return e.stylize(Date.prototype.toString.call(t),"date");if(rt(t))return We(t)}var c,l,u="",h=!1,f=["{","}"];(c=t,Array.isArray(c)&&(h=!0,f=["[","]"]),it(t))&&(u=" [Function"+(t.name?": "+t.name:"")+"]");return Xe(t)&&(u=" "+RegExp.prototype.toString.call(t)),tt(t)&&(u=" "+Date.prototype.toUTCString.call(t)),rt(t)&&(u=" "+We(t)),0!==n.length||h&&0!=t.length?r<0?Xe(t)?e.stylize(RegExp.prototype.toString.call(t),"regexp"):e.stylize("[Object]","special"):(e.seen.push(t),l=h?function(e,t,r,i,s){for(var n=[],o=0,a=t.length;o<a;++o)nt(t,String(o))?n.push(Je(e,t,r,i,String(o),!0)):n.push("");return s.forEach(function(s){s.match(/^\d+$/)||n.push(Je(e,t,r,i,s,!0))}),n}(e,t,r,o,n):n.map(function(i){return Je(e,t,r,o,i,h)}),e.seen.pop(),function(e,t,r){var i=e.reduce(function(e,t){return t.indexOf("\n"),e+t.replace(/\u001b\[\d\d?m/g,"").length+1},0);if(i>60)return r[0]+(""===t?"":t+"\n ")+" "+e.join(",\n ")+" "+r[1];return r[0]+t+" "+e.join(", ")+" "+r[1]}(l,u,f)):f[0]+u+f[1]}function We(e){return"["+Error.prototype.toString.call(e)+"]"}function Je(e,t,r,i,s,n){var o,a,c;if((c=Object.getOwnPropertyDescriptor(t,s)||{value:t[s]}).get?a=c.set?e.stylize("[Getter/Setter]","special"):e.stylize("[Getter]","special"):c.set&&(a=e.stylize("[Setter]","special")),nt(i,s)||(o="["+s+"]"),a||(e.seen.indexOf(c.value)<0?(a=Ye(r)?Ke(e,c.value,null):Ke(e,c.value,r-1)).indexOf("\n")>-1&&(a=n?a.split("\n").map(function(e){return" "+e}).join("\n").substr(2):"\n"+a.split("\n").map(function(e){return" "+e}).join("\n")):a=e.stylize("[Circular]","special")),Ze(o)){if(n&&s.match(/^\d+$/))return a;(o=JSON.stringify(""+s)).match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(o=o.substr(1,o.length-2),o=e.stylize(o,"name")):(o=o.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),o=e.stylize(o,"string"))}return o+": "+a}function Ge(e){return"boolean"==typeof e}function Ye(e){return null===e}function Qe(e){return"string"==typeof e}function Ze(e){return void 0===e}function Xe(e){return et(e)&&"[object RegExp]"===st(e)}function et(e){return"object"==typeof e&&null!==e}function tt(e){return et(e)&&"[object Date]"===st(e)}function rt(e){return et(e)&&("[object Error]"===st(e)||e instanceof Error)}function it(e){return"function"==typeof e}function st(e){return Object.prototype.toString.call(e)}function nt(e,t){return Object.prototype.hasOwnProperty.call(e,t)}ze.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},ze.styles={special:"cyan",number:"yellow",boolean:"yellow",undefined:"grey",null:"bold",string:"green",date:"magenta",regexp:"red"};var ot=[],at=[],ct="undefined"!=typeof Uint8Array?Uint8Array:Array,lt=!1;function ut(){lt=!0;for(var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t=0;t<64;++t)ot[t]=e[t],at[e.charCodeAt(t)]=t;at["-".charCodeAt(0)]=62,at["_".charCodeAt(0)]=63}function ht(e){return ot[e>>18&63]+ot[e>>12&63]+ot[e>>6&63]+ot[63&e]}function ft(e,t,r){for(var i,s=[],n=t;n<r;n+=3)i=(e[n]<<16)+(e[n+1]<<8)+e[n+2],s.push(ht(i));return s.join("")}function dt(e){var t;lt||ut();for(var r=e.length,i=r%3,s="",n=[],o=16383,a=0,c=r-i;a<c;a+=o)n.push(ft(e,a,a+o>c?c:a+o));return 1===i?(t=e[r-1],s+=ot[t>>2],s+=ot[t<<4&63],s+="=="):2===i&&(t=(e[r-2]<<8)+e[r-1],s+=ot[t>>10],s+=ot[t>>4&63],s+=ot[t<<2&63],s+="="),n.push(s),n.join("")}function pt(e,t,r,i,s){var n,o,a=8*s-i-1,c=(1<<a)-1,l=c>>1,u=-7,h=r?s-1:0,f=r?-1:1,d=e[t+h];for(h+=f,n=d&(1<<-u)-1,d>>=-u,u+=a;u>0;n=256*n+e[t+h],h+=f,u-=8);for(o=n&(1<<-u)-1,n>>=-u,u+=i;u>0;o=256*o+e[t+h],h+=f,u-=8);if(0===n)n=1-l;else{if(n===c)return o?NaN:1/0*(d?-1:1);o+=Math.pow(2,i),n-=l}return(d?-1:1)*o*Math.pow(2,n-i)}function mt(e,t,r,i,s,n){var o,a,c,l=8*n-s-1,u=(1<<l)-1,h=u>>1,f=23===s?Math.pow(2,-24)-Math.pow(2,-77):0,d=i?0:n-1,p=i?1:-1,m=t<0||0===t&&1/t<0?1:0;for(t=Math.abs(t),isNaN(t)||t===1/0?(a=isNaN(t)?1:0,o=u):(o=Math.floor(Math.log(t)/Math.LN2),t*(c=Math.pow(2,-o))<1&&(o--,c*=2),(t+=o+h>=1?f/c:f*Math.pow(2,1-h))*c>=2&&(o++,c/=2),o+h>=u?(a=0,o=u):o+h>=1?(a=(t*c-1)*Math.pow(2,s),o+=h):(a=t*Math.pow(2,h-1)*Math.pow(2,s),o=0));s>=8;e[r+d]=255&a,d+=p,a/=256,s-=8);for(o=o<<s|a,l+=s;l>0;e[r+d]=255&o,d+=p,o/=256,l-=8);e[r+d-p]|=128*m}var gt={}.toString,yt=Array.isArray||function(e){return"[object Array]"==gt.call(e)};function bt(){return vt.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function wt(e,t){if(bt()<t)throw new RangeError("Invalid typed array length");return vt.TYPED_ARRAY_SUPPORT?(e=new Uint8Array(t)).__proto__=vt.prototype:(null===e&&(e=new vt(t)),e.length=t),e}function vt(e,t,r){if(!(vt.TYPED_ARRAY_SUPPORT||this instanceof vt))return new vt(e,t,r);if("number"==typeof e){if("string"==typeof t)throw new Error("If encoding is specified then the first argument must be a string");return St(this,e)}return kt(this,e,t,r)}function kt(e,t,r,i){if("number"==typeof t)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&t instanceof ArrayBuffer?function(e,t,r,i){if(t.byteLength,r<0||t.byteLength<r)throw new RangeError("'offset' is out of bounds");if(t.byteLength<r+(i||0))throw new RangeError("'length' is out of bounds");t=void 0===r&&void 0===i?new Uint8Array(t):void 0===i?new Uint8Array(t,r):new Uint8Array(t,r,i);vt.TYPED_ARRAY_SUPPORT?(e=t).__proto__=vt.prototype:e=Ot(e,t);return e}(e,t,r,i):"string"==typeof t?function(e,t,r){"string"==typeof r&&""!==r||(r="utf8");if(!vt.isEncoding(r))throw new TypeError('"encoding" must be a valid string encoding');var i=0|xt(t,r);e=wt(e,i);var s=e.write(t,r);s!==i&&(e=e.slice(0,s));return e}(e,t,r):function(e,t){if(jt(t)){var r=0|Rt(t.length);return 0===(e=wt(e,r)).length||t.copy(e,0,0,r),e}if(t){if("undefined"!=typeof ArrayBuffer&&t.buffer instanceof ArrayBuffer||"length"in t)return"number"!=typeof t.length||(i=t.length)!=i?wt(e,0):Ot(e,t);if("Buffer"===t.type&&yt(t.data))return Ot(e,t.data)}var i;throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}(e,t)}function _t(e){if("number"!=typeof e)throw new TypeError('"size" argument must be a number');if(e<0)throw new RangeError('"size" argument must not be negative')}function St(e,t){if(_t(t),e=wt(e,t<0?0:0|Rt(t)),!vt.TYPED_ARRAY_SUPPORT)for(var r=0;r<t;++r)e[r]=0;return e}function Ot(e,t){var r=t.length<0?0:0|Rt(t.length);e=wt(e,r);for(var i=0;i<r;i+=1)e[i]=255&t[i];return e}function Rt(e){if(e>=bt())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+bt().toString(16)+" bytes");return 0|e}function jt(e){return!(null==e||!e._isBuffer)}function xt(e,t){if(jt(e))return e.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(e)||e instanceof ArrayBuffer))return e.byteLength;"string"!=typeof e&&(e=""+e);var r=e.length;if(0===r)return 0;for(var i=!1;;)switch(t){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return er(e).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return tr(e).length;default:if(i)return er(e).length;t=(""+t).toLowerCase(),i=!0}}function Et(e,t,r){var i=!1;if((void 0===t||t<0)&&(t=0),t>this.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if((r>>>=0)<=(t>>>=0))return"";for(e||(e="utf8");;)switch(e){case"hex":return zt(this,t,r);case"utf8":case"utf-8":return Ut(this,t,r);case"ascii":return Bt(this,t,r);case"latin1":case"binary":return qt(this,t,r);case"base64":return Lt(this,t,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return Ht(this,t,r);default:if(i)throw new TypeError("Unknown encoding: "+e);e=(e+"").toLowerCase(),i=!0}}function At(e,t,r){var i=e[t];e[t]=e[r],e[r]=i}function Dt(e,t,r,i,s){if(0===e.length)return-1;if("string"==typeof r?(i=r,r=0):r>2147483647?r=2147483647:r<-2147483648&&(r=-2147483648),r=+r,isNaN(r)&&(r=s?0:e.length-1),r<0&&(r=e.length+r),r>=e.length){if(s)return-1;r=e.length-1}else if(r<0){if(!s)return-1;r=0}if("string"==typeof t&&(t=vt.from(t,i)),jt(t))return 0===t.length?-1:Ct(e,t,r,i,s);if("number"==typeof t)return t&=255,vt.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?s?Uint8Array.prototype.indexOf.call(e,t,r):Uint8Array.prototype.lastIndexOf.call(e,t,r):Ct(e,[t],r,i,s);throw new TypeError("val must be string, number or Buffer")}function Ct(e,t,r,i,s){var n,o=1,a=e.length,c=t.length;if(void 0!==i&&("ucs2"===(i=String(i).toLowerCase())||"ucs-2"===i||"utf16le"===i||"utf-16le"===i)){if(e.length<2||t.length<2)return-1;o=2,a/=2,c/=2,r/=2}function l(e,t){return 1===o?e[t]:e.readUInt16BE(t*o)}if(s){var u=-1;for(n=r;n<a;n++)if(l(e,n)===l(t,-1===u?0:n-u)){if(-1===u&&(u=n),n-u+1===c)return u*o}else-1!==u&&(n-=n-u),u=-1}else for(r+c>a&&(r=a-c),n=r;n>=0;n--){for(var h=!0,f=0;f<c;f++)if(l(e,n+f)!==l(t,f)){h=!1;break}if(h)return n}return-1}function Nt(e,t,r,i){r=Number(r)||0;var s=e.length-r;i?(i=Number(i))>s&&(i=s):i=s;var n=t.length;if(n%2!=0)throw new TypeError("Invalid hex string");i>n/2&&(i=n/2);for(var o=0;o<i;++o){var a=parseInt(t.substr(2*o,2),16);if(isNaN(a))return o;e[r+o]=a}return o}function $t(e,t,r,i){return rr(er(t,e.length-r),e,r,i)}function Pt(e,t,r,i){return rr(function(e){for(var t=[],r=0;r<e.length;++r)t.push(255&e.charCodeAt(r));return t}(t),e,r,i)}function It(e,t,r,i){return Pt(e,t,r,i)}function Tt(e,t,r,i){return rr(tr(t),e,r,i)}function Mt(e,t,r,i){return rr(function(e,t){for(var r,i,s,n=[],o=0;o<e.length&&!((t-=2)<0);++o)i=(r=e.charCodeAt(o))>>8,s=r%256,n.push(s),n.push(i);return n}(t,e.length-r),e,r,i)}function Lt(e,t,r){return 0===t&&r===e.length?dt(e):dt(e.slice(t,r))}function Ut(e,t,r){r=Math.min(e.length,r);for(var i=[],s=t;s<r;){var n,o,a,c,l=e[s],u=null,h=l>239?4:l>223?3:l>191?2:1;if(s+h<=r)switch(h){case 1:l<128&&(u=l);break;case 2:128==(192&(n=e[s+1]))&&(c=(31&l)<<6|63&n)>127&&(u=c);break;case 3:n=e[s+1],o=e[s+2],128==(192&n)&&128==(192&o)&&(c=(15&l)<<12|(63&n)<<6|63&o)>2047&&(c<55296||c>57343)&&(u=c);break;case 4:n=e[s+1],o=e[s+2],a=e[s+3],128==(192&n)&&128==(192&o)&&128==(192&a)&&(c=(15&l)<<18|(63&n)<<12|(63&o)<<6|63&a)>65535&&c<1114112&&(u=c)}null===u?(u=65533,h=1):u>65535&&(u-=65536,i.push(u>>>10&1023|55296),u=56320|1023&u),i.push(u),s+=h}return function(e){var t=e.length;if(t<=Ft)return String.fromCharCode.apply(String,e);var r="",i=0;for(;i<t;)r+=String.fromCharCode.apply(String,e.slice(i,i+=Ft));return r}(i)}vt.TYPED_ARRAY_SUPPORT=void 0===global.TYPED_ARRAY_SUPPORT||global.TYPED_ARRAY_SUPPORT,bt(),vt.poolSize=8192,vt._augment=function(e){return e.__proto__=vt.prototype,e},vt.from=function(e,t,r){return kt(null,e,t,r)},vt.TYPED_ARRAY_SUPPORT&&(vt.prototype.__proto__=Uint8Array.prototype,vt.__proto__=Uint8Array,"undefined"!=typeof Symbol&&Symbol.species&&vt[Symbol.species]),vt.alloc=function(e,t,r){return function(e,t,r,i){return _t(t),t<=0?wt(e,t):void 0!==r?"string"==typeof i?wt(e,t).fill(r,i):wt(e,t).fill(r):wt(e,t)}(null,e,t,r)},vt.allocUnsafe=function(e){return St(null,e)},vt.allocUnsafeSlow=function(e){return St(null,e)},vt.isBuffer=function(e){return null!=e&&(!!e._isBuffer||ir(e)||function(e){return"function"==typeof e.readFloatLE&&"function"==typeof e.slice&&ir(e.slice(0,0))}(e))},vt.compare=function(e,t){if(!jt(e)||!jt(t))throw new TypeError("Arguments must be Buffers");if(e===t)return 0;for(var r=e.length,i=t.length,s=0,n=Math.min(r,i);s<n;++s)if(e[s]!==t[s]){r=e[s],i=t[s];break}return r<i?-1:i<r?1:0},vt.isEncoding=function(e){switch(String(e).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"latin1":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return!0;default:return!1}},vt.concat=function(e,t){if(!yt(e))throw new TypeError('"list" argument must be an Array of Buffers');if(0===e.length)return vt.alloc(0);var r;if(void 0===t)for(t=0,r=0;r<e.length;++r)t+=e[r].length;var i=vt.allocUnsafe(t),s=0;for(r=0;r<e.length;++r){var n=e[r];if(!jt(n))throw new TypeError('"list" argument must be an Array of Buffers');n.copy(i,s),s+=n.length}return i},vt.byteLength=xt,vt.prototype._isBuffer=!0,vt.prototype.swap16=function(){var e=this.length;if(e%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(var t=0;t<e;t+=2)At(this,t,t+1);return this},vt.prototype.swap32=function(){var e=this.length;if(e%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(var t=0;t<e;t+=4)At(this,t,t+3),At(this,t+1,t+2);return this},vt.prototype.swap64=function(){var e=this.length;if(e%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(var t=0;t<e;t+=8)At(this,t,t+7),At(this,t+1,t+6),At(this,t+2,t+5),At(this,t+3,t+4);return this},vt.prototype.toString=function(){var e=0|this.length;return 0===e?"":0===arguments.length?Ut(this,0,e):Et.apply(this,arguments)},vt.prototype.equals=function(e){if(!jt(e))throw new TypeError("Argument must be a Buffer");return this===e||0===vt.compare(this,e)},vt.prototype.inspect=function(){var e="";return this.length>0&&(e=this.toString("hex",0,50).match(/.{2}/g).join(" "),this.length>50&&(e+=" ... ")),"<Buffer "+e+">"},vt.prototype.compare=function(e,t,r,i,s){if(!jt(e))throw new TypeError("Argument must be a Buffer");if(void 0===t&&(t=0),void 0===r&&(r=e?e.length:0),void 0===i&&(i=0),void 0===s&&(s=this.length),t<0||r>e.length||i<0||s>this.length)throw new RangeError("out of range index");if(i>=s&&t>=r)return 0;if(i>=s)return-1;if(t>=r)return 1;if(this===e)return 0;for(var n=(s>>>=0)-(i>>>=0),o=(r>>>=0)-(t>>>=0),a=Math.min(n,o),c=this.slice(i,s),l=e.slice(t,r),u=0;u<a;++u)if(c[u]!==l[u]){n=c[u],o=l[u];break}return n<o?-1:o<n?1:0},vt.prototype.includes=function(e,t,r){return-1!==this.indexOf(e,t,r)},vt.prototype.indexOf=function(e,t,r){return Dt(this,e,t,r,!0)},vt.prototype.lastIndexOf=function(e,t,r){return Dt(this,e,t,r,!1)},vt.prototype.write=function(e,t,r,i){if(void 0===t)i="utf8",r=this.length,t=0;else if(void 0===r&&"string"==typeof t)i=t,r=this.length,t=0;else{if(!isFinite(t))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");t|=0,isFinite(r)?(r|=0,void 0===i&&(i="utf8")):(i=r,r=void 0)}var s=this.length-t;if((void 0===r||r>s)&&(r=s),e.length>0&&(r<0||t<0)||t>this.length)throw new RangeError("Attempt to write outside buffer bounds");i||(i="utf8");for(var n=!1;;)switch(i){case"hex":return Nt(this,e,t,r);case"utf8":case"utf-8":return $t(this,e,t,r);case"ascii":return Pt(this,e,t,r);case"latin1":case"binary":return It(this,e,t,r);case"base64":return Tt(this,e,t,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return Mt(this,e,t,r);default:if(n)throw new TypeError("Unknown encoding: "+i);i=(""+i).toLowerCase(),n=!0}},vt.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var Ft=4096;function Bt(e,t,r){var i="";r=Math.min(e.length,r);for(var s=t;s<r;++s)i+=String.fromCharCode(127&e[s]);return i}function qt(e,t,r){var i="";r=Math.min(e.length,r);for(var s=t;s<r;++s)i+=String.fromCharCode(e[s]);return i}function zt(e,t,r){var i=e.length;(!t||t<0)&&(t=0),(!r||r<0||r>i)&&(r=i);for(var s="",n=t;n<r;++n)s+=Xt(e[n]);return s}function Ht(e,t,r){for(var i=e.slice(t,r),s="",n=0;n<i.length;n+=2)s+=String.fromCharCode(i[n]+256*i[n+1]);return s}function Vt(e,t,r){if(e%1!=0||e<0)throw new RangeError("offset is not uint");if(e+t>r)throw new RangeError("Trying to access beyond buffer length")}function Kt(e,t,r,i,s,n){if(!jt(e))throw new TypeError('"buffer" argument must be a Buffer instance');if(t>s||t<n)throw new RangeError('"value" argument is out of bounds');if(r+i>e.length)throw new RangeError("Index out of range")}function Wt(e,t,r,i){t<0&&(t=65535+t+1);for(var s=0,n=Math.min(e.length-r,2);s<n;++s)e[r+s]=(t&255<<8*(i?s:1-s))>>>8*(i?s:1-s)}function Jt(e,t,r,i){t<0&&(t=4294967295+t+1);for(var s=0,n=Math.min(e.length-r,4);s<n;++s)e[r+s]=t>>>8*(i?s:3-s)&255}function Gt(e,t,r,i,s,n){if(r+i>e.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function Yt(e,t,r,i,s){return s||Gt(e,0,r,4),mt(e,t,r,i,23,4),r+4}function Qt(e,t,r,i,s){return s||Gt(e,0,r,8),mt(e,t,r,i,52,8),r+8}vt.prototype.slice=function(e,t){var r,i=this.length;if((e=~~e)<0?(e+=i)<0&&(e=0):e>i&&(e=i),(t=void 0===t?i:~~t)<0?(t+=i)<0&&(t=0):t>i&&(t=i),t<e&&(t=e),vt.TYPED_ARRAY_SUPPORT)(r=this.subarray(e,t)).__proto__=vt.prototype;else{var s=t-e;r=new vt(s,void 0);for(var n=0;n<s;++n)r[n]=this[n+e]}return r},vt.prototype.readUIntLE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=this[e],s=1,n=0;++n<t&&(s*=256);)i+=this[e+n]*s;return i},vt.prototype.readUIntBE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=this[e+--t],s=1;t>0&&(s*=256);)i+=this[e+--t]*s;return i},vt.prototype.readUInt8=function(e,t){return t||Vt(e,1,this.length),this[e]},vt.prototype.readUInt16LE=function(e,t){return t||Vt(e,2,this.length),this[e]|this[e+1]<<8},vt.prototype.readUInt16BE=function(e,t){return t||Vt(e,2,this.length),this[e]<<8|this[e+1]},vt.prototype.readUInt32LE=function(e,t){return t||Vt(e,4,this.length),(this[e]|this[e+1]<<8|this[e+2]<<16)+16777216*this[e+3]},vt.prototype.readUInt32BE=function(e,t){return t||Vt(e,4,this.length),16777216*this[e]+(this[e+1]<<16|this[e+2]<<8|this[e+3])},vt.prototype.readIntLE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=this[e],s=1,n=0;++n<t&&(s*=256);)i+=this[e+n]*s;return i>=(s*=128)&&(i-=Math.pow(2,8*t)),i},vt.prototype.readIntBE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=t,s=1,n=this[e+--i];i>0&&(s*=256);)n+=this[e+--i]*s;return n>=(s*=128)&&(n-=Math.pow(2,8*t)),n},vt.prototype.readInt8=function(e,t){return t||Vt(e,1,this.length),128&this[e]?-1*(255-this[e]+1):this[e]},vt.prototype.readInt16LE=function(e,t){t||Vt(e,2,this.length);var r=this[e]|this[e+1]<<8;return 32768&r?4294901760|r:r},vt.prototype.readInt16BE=function(e,t){t||Vt(e,2,this.length);var r=this[e+1]|this[e]<<8;return 32768&r?4294901760|r:r},vt.prototype.readInt32LE=function(e,t){return t||Vt(e,4,this.length),this[e]|this[e+1]<<8|this[e+2]<<16|this[e+3]<<24},vt.prototype.readInt32BE=function(e,t){return t||Vt(e,4,this.length),this[e]<<24|this[e+1]<<16|this[e+2]<<8|this[e+3]},vt.prototype.readFloatLE=function(e,t){return t||Vt(e,4,this.length),pt(this,e,!0,23,4)},vt.prototype.readFloatBE=function(e,t){return t||Vt(e,4,this.length),pt(this,e,!1,23,4)},vt.prototype.readDoubleLE=function(e,t){return t||Vt(e,8,this.length),pt(this,e,!0,52,8)},vt.prototype.readDoubleBE=function(e,t){return t||Vt(e,8,this.length),pt(this,e,!1,52,8)},vt.prototype.writeUIntLE=function(e,t,r,i){(e=+e,t|=0,r|=0,i)||Kt(this,e,t,r,Math.pow(2,8*r)-1,0);var s=1,n=0;for(this[t]=255&e;++n<r&&(s*=256);)this[t+n]=e/s&255;return t+r},vt.prototype.writeUIntBE=function(e,t,r,i){(e=+e,t|=0,r|=0,i)||Kt(this,e,t,r,Math.pow(2,8*r)-1,0);var s=r-1,n=1;for(this[t+s]=255&e;--s>=0&&(n*=256);)this[t+s]=e/n&255;return t+r},vt.prototype.writeUInt8=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,1,255,0),vt.TYPED_ARRAY_SUPPORT||(e=Math.floor(e)),this[t]=255&e,t+1},vt.prototype.writeUInt16LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,65535,0),vt.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8):Wt(this,e,t,!0),t+2},vt.prototype.writeUInt16BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,65535,0),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>8,this[t+1]=255&e):Wt(this,e,t,!1),t+2},vt.prototype.writeUInt32LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,4294967295,0),vt.TYPED_ARRAY_SUPPORT?(this[t+3]=e>>>24,this[t+2]=e>>>16,this[t+1]=e>>>8,this[t]=255&e):Jt(this,e,t,!0),t+4},vt.prototype.writeUInt32BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,4294967295,0),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>24,this[t+1]=e>>>16,this[t+2]=e>>>8,this[t+3]=255&e):Jt(this,e,t,!1),t+4},vt.prototype.writeIntLE=function(e,t,r,i){if(e=+e,t|=0,!i){var s=Math.pow(2,8*r-1);Kt(this,e,t,r,s-1,-s)}var n=0,o=1,a=0;for(this[t]=255&e;++n<r&&(o*=256);)e<0&&0===a&&0!==this[t+n-1]&&(a=1),this[t+n]=(e/o|0)-a&255;return t+r},vt.prototype.writeIntBE=function(e,t,r,i){if(e=+e,t|=0,!i){var s=Math.pow(2,8*r-1);Kt(this,e,t,r,s-1,-s)}var n=r-1,o=1,a=0;for(this[t+n]=255&e;--n>=0&&(o*=256);)e<0&&0===a&&0!==this[t+n+1]&&(a=1),this[t+n]=(e/o|0)-a&255;return t+r},vt.prototype.writeInt8=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,1,127,-128),vt.TYPED_ARRAY_SUPPORT||(e=Math.floor(e)),e<0&&(e=255+e+1),this[t]=255&e,t+1},vt.prototype.writeInt16LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,32767,-32768),vt.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8):Wt(this,e,t,!0),t+2},vt.prototype.writeInt16BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,32767,-32768),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>8,this[t+1]=255&e):Wt(this,e,t,!1),t+2},vt.prototype.writeInt32LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,2147483647,-2147483648),vt.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8,this[t+2]=e>>>16,this[t+3]=e>>>24):Jt(this,e,t,!0),t+4},vt.prototype.writeInt32BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,2147483647,-2147483648),e<0&&(e=4294967295+e+1),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>24,this[t+1]=e>>>16,this[t+2]=e>>>8,this[t+3]=255&e):Jt(this,e,t,!1),t+4},vt.prototype.writeFloatLE=function(e,t,r){return Yt(this,e,t,!0,r)},vt.prototype.writeFloatBE=function(e,t,r){return Yt(this,e,t,!1,r)},vt.prototype.writeDoubleLE=function(e,t,r){return Qt(this,e,t,!0,r)},vt.prototype.writeDoubleBE=function(e,t,r){return Qt(this,e,t,!1,r)},vt.prototype.copy=function(e,t,r,i){if(r||(r=0),i||0===i||(i=this.length),t>=e.length&&(t=e.length),t||(t=0),i>0&&i<r&&(i=r),i===r)return 0;if(0===e.length||0===this.length)return 0;if(t<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("sourceStart out of bounds");if(i<0)throw new RangeError("sourceEnd out of bounds");i>this.length&&(i=this.length),e.length-t<i-r&&(i=e.length-t+r);var s,n=i-r;if(this===e&&r<t&&t<i)for(s=n-1;s>=0;--s)e[s+t]=this[s+r];else if(n<1e3||!vt.TYPED_ARRAY_SUPPORT)for(s=0;s<n;++s)e[s+t]=this[s+r];else Uint8Array.prototype.set.call(e,this.subarray(r,r+n),t);return n},vt.prototype.fill=function(e,t,r,i){if("string"==typeof e){if("string"==typeof t?(i=t,t=0,r=this.length):"string"==typeof r&&(i=r,r=this.length),1===e.length){var s=e.charCodeAt(0);s<256&&(e=s)}if(void 0!==i&&"string"!=typeof i)throw new TypeError("encoding must be a string");if("string"==typeof i&&!vt.isEncoding(i))throw new TypeError("Unknown encoding: "+i)}else"number"==typeof e&&(e&=255);if(t<0||this.length<t||this.length<r)throw new RangeError("Out of range index");if(r<=t)return this;var n;if(t>>>=0,r=void 0===r?this.length:r>>>0,e||(e=0),"number"==typeof e)for(n=t;n<r;++n)this[n]=e;else{var o=jt(e)?e:er(new vt(e,i).toString()),a=o.length;for(n=0;n<r-t;++n)this[n+t]=o[n%a]}return this};var Zt=/[^+\/0-9A-Za-z-_]/g;function Xt(e){return e<16?"0"+e.toString(16):e.toString(16)}function er(e,t){var r;t=t||1/0;for(var i=e.length,s=null,n=[],o=0;o<i;++o){if((r=e.charCodeAt(o))>55295&&r<57344){if(!s){if(r>56319){(t-=3)>-1&&n.push(239,191,189);continue}if(o+1===i){(t-=3)>-1&&n.push(239,191,189);continue}s=r;continue}if(r<56320){(t-=3)>-1&&n.push(239,191,189),s=r;continue}r=65536+(s-55296<<10|r-56320)}else s&&(t-=3)>-1&&n.push(239,191,189);if(s=null,r<128){if((t-=1)<0)break;n.push(r)}else if(r<2048){if((t-=2)<0)break;n.push(r>>6|192,63&r|128)}else if(r<65536){if((t-=3)<0)break;n.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((t-=4)<0)break;n.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return n}function tr(e){return function(e){var t,r,i,s,n,o;lt||ut();var a=e.length;if(a%4>0)throw new Error("Invalid string. Length must be a multiple of 4");n="="===e[a-2]?2:"="===e[a-1]?1:0,o=new ct(3*a/4-n),i=n>0?a-4:a;var c=0;for(t=0,r=0;t<i;t+=4,r+=3)s=at[e.charCodeAt(t)]<<18|at[e.charCodeAt(t+1)]<<12|at[e.charCodeAt(t+2)]<<6|at[e.charCodeAt(t+3)],o[c++]=s>>16&255,o[c++]=s>>8&255,o[c++]=255&s;return 2===n?(s=at[e.charCodeAt(t)]<<2|at[e.charCodeAt(t+1)]>>4,o[c++]=255&s):1===n&&(s=at[e.charCodeAt(t)]<<10|at[e.charCodeAt(t+1)]<<4|at[e.charCodeAt(t+2)]>>2,o[c++]=s>>8&255,o[c++]=255&s),o}(function(e){if((e=function(e){return e.trim?e.trim():e.replace(/^\s+|\s+$/g,"")}(e).replace(Zt,"")).length<2)return"";for(;e.length%4!=0;)e+="=";return e}(e))}function rr(e,t,r,i){for(var s=0;s<i&&!(s+r>=t.length||s>=e.length);++s)t[s+r]=e[s];return s}function ir(e){return!!e.constructor&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}function sr(){this.head=null,this.tail=null,this.length=0}sr.prototype.push=function(e){var t={data:e,next:null};this.length>0?this.tail.next=t:this.head=t,this.tail=t,++this.length},sr.prototype.unshift=function(e){var t={data:e,next:this.head};0===this.length&&(this.tail=t),this.head=t,++this.length},sr.prototype.shift=function(){if(0!==this.length){var e=this.head.data;return 1===this.length?this.head=this.tail=null:this.head=this.head.next,--this.length,e}},sr.prototype.clear=function(){this.head=this.tail=null,this.length=0},sr.prototype.join=function(e){if(0===this.length)return"";for(var t=this.head,r=""+t.data;t=t.next;)r+=e+t.data;return r},sr.prototype.concat=function(e){if(0===this.length)return vt.alloc(0);if(1===this.length)return this.head.data;for(var t=vt.allocUnsafe(e>>>0),r=this.head,i=0;r;)r.data.copy(t,i),i+=r.data.length,r=r.next;return t};var nr=vt.isEncoding||function(e){switch(e&&e.toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":case"raw":return!0;default:return!1}};function or(e){switch(this.encoding=(e||"utf8").toLowerCase().replace(/[-_]/,""),function(e){if(e&&!nr(e))throw new Error("Unknown encoding: "+e)}(e),this.encoding){case"utf8":this.surrogateSize=3;break;case"ucs2":case"utf16le":this.surrogateSize=2,this.detectIncompleteChar=cr;break;case"base64":this.surrogateSize=3,this.detectIncompleteChar=lr;break;default:return void(this.write=ar)}this.charBuffer=new vt(6),this.charReceived=0,this.charLength=0}function ar(e){return e.toString(this.encoding)}function cr(e){this.charReceived=e.length%2,this.charLength=this.charReceived?2:0}function lr(e){this.charReceived=e.length%3,this.charLength=this.charReceived?3:0}or.prototype.write=function(e){for(var t="";this.charLength;){var r=e.length>=this.charLength-this.charReceived?this.charLength-this.charReceived:e.length;if(e.copy(this.charBuffer,this.charReceived,0,r),this.charReceived+=r,this.charReceived<this.charLength)return"";if(e=e.slice(r,e.length),!((s=(t=this.charBuffer.slice(0,this.charLength).toString(this.encoding)).charCodeAt(t.length-1))>=55296&&s<=56319)){if(this.charReceived=this.charLength=0,0===e.length)return t;break}this.charLength+=this.surrogateSize,t=""}this.detectIncompleteChar(e);var i=e.length;this.charLength&&(e.copy(this.charBuffer,0,e.length-this.charReceived,i),i-=this.charReceived);var s;i=(t+=e.toString(this.encoding,0,i)).length-1;if((s=t.charCodeAt(i))>=55296&&s<=56319){var n=this.surrogateSize;return this.charLength+=n,this.charReceived+=n,this.charBuffer.copy(this.charBuffer,n,0,n),e.copy(this.charBuffer,0,0,n),t.substring(0,i)}return t},or.prototype.detectIncompleteChar=function(e){for(var t=e.length>=3?3:e.length;t>0;t--){var r=e[e.length-t];if(1==t&&r>>5==6){this.charLength=2;break}if(t<=2&&r>>4==14){this.charLength=3;break}if(t<=3&&r>>3==30){this.charLength=4;break}}this.charReceived=t},or.prototype.end=function(e){var t="";if(e&&e.length&&(t=this.write(e)),this.charReceived){var r=this.charReceived,i=this.charBuffer,s=this.encoding;t+=i.slice(0,r).toString(s)}return t},fr.ReadableState=hr;var ur=function(e){if(Ze(Be)&&(Be=Me.env.NODE_DEBUG||""),e=e.toUpperCase(),!qe[e])if(new RegExp("\\b"+e+"\\b","i").test(Be)){qe[e]=function(){var t=Ue.apply(null,arguments);console.error("%s %d: %s",e,0,t)}}else qe[e]=function(){};return qe[e]}("stream");function hr(e,t){e=e||{},this.objectMode=!!e.objectMode,t instanceof Fr&&(this.objectMode=this.objectMode||!!e.readableObjectMode);var r=e.highWaterMark,i=this.objectMode?16:16384;this.highWaterMark=r||0===r?r:i,this.highWaterMark=~~this.highWaterMark,this.buffer=new sr,this.length=0,this.pipes=null,this.pipesCount=0,this.flowing=null,this.ended=!1,this.endEmitted=!1,this.reading=!1,this.sync=!0,this.needReadable=!1,this.emittedReadable=!1,this.readableListening=!1,this.resumeScheduled=!1,this.defaultEncoding=e.defaultEncoding||"utf8",this.ranOut=!1,this.awaitDrain=0,this.readingMore=!1,this.decoder=null,this.encoding=null,e.encoding&&(this.decoder=new or(e.encoding),this.encoding=e.encoding)}function fr(e){if(!(this instanceof fr))return new fr(e);this._readableState=new hr(e,this),this.readable=!0,e&&"function"==typeof e.read&&(this._read=e.read),se.call(this)}function dr(e,t,r,i,s){var n=function(e,t){var r=null;Buffer.isBuffer(t)||"string"==typeof t||null==t||e.objectMode||(r=new TypeError("Invalid non-string/buffer chunk"));return r}(t,r);if(n)e.emit("error",n);else if(null===r)t.reading=!1,function(e,t){if(t.ended)return;if(t.decoder){var r=t.decoder.end();r&&r.length&&(t.buffer.push(r),t.length+=t.objectMode?1:r.length)}t.ended=!0,gr(e)}(e,t);else if(t.objectMode||r&&r.length>0)if(t.ended&&!s){var o=new Error("stream.push() after EOF");e.emit("error",o)}else if(t.endEmitted&&s){var a=new Error("stream.unshift() after end event");e.emit("error",a)}else{var c;!t.decoder||s||i||(r=t.decoder.write(r),c=!t.objectMode&&0===r.length),s||(t.reading=!1),c||(t.flowing&&0===t.length&&!t.sync?(e.emit("data",r),e.read(0)):(t.length+=t.objectMode?1:r.length,s?t.buffer.unshift(r):t.buffer.push(r),t.needReadable&&gr(e))),function(e,t){t.readingMore||(t.readingMore=!0,Pe(br,e,t))}(e,t)}else s||(t.reading=!1);return function(e){return!e.ended&&(e.needReadable||e.length<e.highWaterMark||0===e.length)}(t)}Te(fr,se),fr.prototype.push=function(e,t){var r=this._readableState;return r.objectMode||"string"!=typeof e||(t=t||r.defaultEncoding)!==r.encoding&&(e=Buffer.from(e,t),t=""),dr(this,r,e,t,!1)},fr.prototype.unshift=function(e){return dr(this,this._readableState,e,"",!0)},fr.prototype.isPaused=function(){return!1===this._readableState.flowing},fr.prototype.setEncoding=function(e){return this._readableState.decoder=new or(e),this._readableState.encoding=e,this};var pr=8388608;function mr(e,t){return e<=0||0===t.length&&t.ended?0:t.objectMode?1:e!=e?t.flowing&&t.length?t.buffer.head.data.length:t.length:(e>t.highWaterMark&&(t.highWaterMark=function(e){return e>=pr?e=pr:(e--,e|=e>>>1,e|=e>>>2,e|=e>>>4,e|=e>>>8,e|=e>>>16,e++),e}(e)),e<=t.length?e:t.ended?t.length:(t.needReadable=!0,0))}function gr(e){var t=e._readableState;t.needReadable=!1,t.emittedReadable||(ur("emitReadable",t.flowing),t.emittedReadable=!0,t.sync?Pe(yr,e):yr(e))}function yr(e){ur("emit readable"),e.emit("readable"),kr(e)}function br(e,t){for(var r=t.length;!t.reading&&!t.flowing&&!t.ended&&t.length<t.highWaterMark&&(ur("maybeReadMore read 0"),e.read(0),r!==t.length);)r=t.length;t.readingMore=!1}function wr(e){ur("readable nexttick read 0"),e.read(0)}function vr(e,t){t.reading||(ur("resume read 0"),e.read(0)),t.resumeScheduled=!1,t.awaitDrain=0,e.emit("resume"),kr(e),t.flowing&&!t.reading&&e.read(0)}function kr(e){var t=e._readableState;for(ur("flow",t.flowing);t.flowing&&null!==e.read(););}function _r(e,t){return 0===t.length?null:(t.objectMode?r=t.buffer.shift():!e||e>=t.length?(r=t.decoder?t.buffer.join(""):1===t.buffer.length?t.buffer.head.data:t.buffer.concat(t.length),t.buffer.clear()):r=function(e,t,r){var i;e<t.head.data.length?(i=t.head.data.slice(0,e),t.head.data=t.head.data.slice(e)):i=e===t.head.data.length?t.shift():r?function(e,t){var r=t.head,i=1,s=r.data;e-=s.length;for(;r=r.next;){var n=r.data,o=e>n.length?n.length:e;if(o===n.length?s+=n:s+=n.slice(0,e),0===(e-=o)){o===n.length?(++i,r.next?t.head=r.next:t.head=t.tail=null):(t.head=r,r.data=n.slice(o));break}++i}return t.length-=i,s}(e,t):function(e,t){var r=Buffer.allocUnsafe(e),i=t.head,s=1;i.data.copy(r),e-=i.data.length;for(;i=i.next;){var n=i.data,o=e>n.length?n.length:e;if(n.copy(r,r.length-e,0,o),0===(e-=o)){o===n.length?(++s,i.next?t.head=i.next:t.head=t.tail=null):(t.head=i,i.data=n.slice(o));break}++s}return t.length-=s,r}(e,t);return i}(e,t.buffer,t.decoder),r);var r}function Sr(e){var t=e._readableState;if(t.length>0)throw new Error('"endReadable()" called on non-empty stream');t.endEmitted||(t.ended=!0,Pe(Or,t,e))}function Or(e,t){e.endEmitted||0!==e.length||(e.endEmitted=!0,t.readable=!1,t.emit("end"))}function Rr(e,t){for(var r=0,i=e.length;r<i;r++)if(e[r]===t)return r;return-1}function jr(){}function xr(e,t,r){this.chunk=e,this.encoding=t,this.callback=r,this.next=null}function Er(e,t){Object.defineProperty(this,"buffer",{get:Fe(function(){return this.getBuffer()},"_writableState.buffer is deprecated. Use _writableState.getBuffer instead.")}),e=e||{},this.objectMode=!!e.objectMode,t instanceof Fr&&(this.objectMode=this.objectMode||!!e.writableObjectMode);var r=e.highWaterMark,i=this.objectMode?16:16384;this.highWaterMark=r||0===r?r:i,this.highWaterMark=~~this.highWaterMark,this.needDrain=!1,this.ending=!1,this.ended=!1,this.finished=!1;var s=!1===e.decodeStrings;this.decodeStrings=!s,this.defaultEncoding=e.defaultEncoding||"utf8",this.length=0,this.writing=!1,this.corked=0,this.sync=!0,this.bufferProcessing=!1,this.onwrite=function(e){!function(e,t){var r=e._writableState,i=r.sync,s=r.writecb;if(function(e){e.writing=!1,e.writecb=null,e.length-=e.writelen,e.writelen=0}(r),t)!function(e,t,r,i,s){--t.pendingcb,r?Pe(s,i):s(i);e._writableState.errorEmitted=!0,e.emit("error",i)}(e,r,i,t,s);else{var n=$r(r);n||r.corked||r.bufferProcessing||!r.bufferedRequest||Nr(e,r),i?Pe(Cr,e,r,n,s):Cr(e,r,n,s)}}(t,e)},this.writecb=null,this.writelen=0,this.bufferedRequest=null,this.lastBufferedRequest=null,this.pendingcb=0,this.prefinished=!1,this.errorEmitted=!1,this.bufferedRequestCount=0,this.corkedRequestsFree=new Tr(this)}function Ar(e){if(!(this instanceof Ar||this instanceof Fr))return new Ar(e);this._writableState=new Er(e,this),this.writable=!0,e&&("function"==typeof e.write&&(this._write=e.write),"function"==typeof e.writev&&(this._writev=e.writev)),se.call(this)}function Dr(e,t,r,i,s,n,o){t.writelen=i,t.writecb=o,t.writing=!0,t.sync=!0,r?e._writev(s,t.onwrite):e._write(s,n,t.onwrite),t.sync=!1}function Cr(e,t,r,i){r||function(e,t){0===t.length&&t.needDrain&&(t.needDrain=!1,e.emit("drain"))}(e,t),t.pendingcb--,i(),Ir(e,t)}function Nr(e,t){t.bufferProcessing=!0;var r=t.bufferedRequest;if(e._writev&&r&&r.next){var i=t.bufferedRequestCount,s=new Array(i),n=t.corkedRequestsFree;n.entry=r;for(var o=0;r;)s[o]=r,r=r.next,o+=1;Dr(e,t,!0,t.length,s,"",n.finish),t.pendingcb++,t.lastBufferedRequest=null,n.next?(t.corkedRequestsFree=n.next,n.next=null):t.corkedRequestsFree=new Tr(t)}else{for(;r;){var a=r.chunk,c=r.encoding,l=r.callback;if(Dr(e,t,!1,t.objectMode?1:a.length,a,c,l),r=r.next,t.writing)break}null===r&&(t.lastBufferedRequest=null)}t.bufferedRequestCount=0,t.bufferedRequest=r,t.bufferProcessing=!1}function $r(e){return e.ending&&0===e.length&&null===e.bufferedRequest&&!e.finished&&!e.writing}function Pr(e,t){t.prefinished||(t.prefinished=!0,e.emit("prefinish"))}function Ir(e,t){var r=$r(t);return r&&(0===t.pendingcb?(Pr(e,t),t.finished=!0,e.emit("finish")):Pr(e,t)),r}function Tr(e){var t=this;this.next=null,this.entry=null,this.finish=function(r){var i=t.entry;for(t.entry=null;i;){var s=i.callback;e.pendingcb--,s(r),i=i.next}e.corkedRequestsFree?e.corkedRequestsFree.next=t:e.corkedRequestsFree=t}}fr.prototype.read=function(e){ur("read",e),e=parseInt(e,10);var t=this._readableState,r=e;if(0!==e&&(t.emittedReadable=!1),0===e&&t.needReadable&&(t.length>=t.highWaterMark||t.ended))return ur("read: emitReadable",t.length,t.ended),0===t.length&&t.ended?Sr(this):gr(this),null;if(0===(e=mr(e,t))&&t.ended)return 0===t.length&&Sr(this),null;var i,s=t.needReadable;return ur("need readable",s),(0===t.length||t.length-e<t.highWaterMark)&&ur("length less than watermark",s=!0),t.ended||t.reading?ur("reading or ended",s=!1):s&&(ur("do read"),t.reading=!0,t.sync=!0,0===t.length&&(t.needReadable=!0),this._read(t.highWaterMark),t.sync=!1,t.reading||(e=mr(r,t))),null===(i=e>0?_r(e,t):null)?(t.needReadable=!0,e=0):t.length-=e,0===t.length&&(t.ended||(t.needReadable=!0),r!==e&&t.ended&&Sr(this)),null!==i&&this.emit("data",i),i},fr.prototype._read=function(e){this.emit("error",new Error("not implemented"))},fr.prototype.pipe=function(e,t){var r=this,i=this._readableState;switch(i.pipesCount){case 0:i.pipes=e;break;case 1:i.pipes=[i.pipes,e];break;default:i.pipes.push(e)}i.pipesCount+=1,ur("pipe count=%d opts=%j",i.pipesCount,t);var s=!t||!1!==t.end?o:l;function n(e){ur("onunpipe"),e===r&&l()}function o(){ur("onend"),e.end()}i.endEmitted?Pe(s):r.once("end",s),e.on("unpipe",n);var a=function(e){return function(){var t=e._readableState;ur("pipeOnDrain",t.awaitDrain),t.awaitDrain&&t.awaitDrain--,0===t.awaitDrain&&e.listeners("data").length&&(t.flowing=!0,kr(e))}}(r);e.on("drain",a);var c=!1;function l(){ur("cleanup"),e.removeListener("close",d),e.removeListener("finish",p),e.removeListener("drain",a),e.removeListener("error",f),e.removeListener("unpipe",n),r.removeListener("end",o),r.removeListener("end",l),r.removeListener("data",h),c=!0,!i.awaitDrain||e._writableState&&!e._writableState.needDrain||a()}var u=!1;function h(t){ur("ondata"),u=!1,!1!==e.write(t)||u||((1===i.pipesCount&&i.pipes===e||i.pipesCount>1&&-1!==Rr(i.pipes,e))&&!c&&(ur("false write response, pause",r._readableState.awaitDrain),r._readableState.awaitDrain++,u=!0),r.pause())}function f(t){var r;ur("onerror",t),m(),e.removeListener("error",f),0===(r="error",e.listeners(r).length)&&e.emit("error",t)}function d(){e.removeListener("finish",p),m()}function p(){ur("onfinish"),e.removeListener("close",d),m()}function m(){ur("unpipe"),r.unpipe(e)}return r.on("data",h),function(e,t,r){if("function"==typeof e.prependListener)return e.prependListener(t,r);e._events&&e._events[t]?Array.isArray(e._events[t])?e._events[t].unshift(r):e._events[t]=[r,e._events[t]]:e.on(t,r)}(e,"error",f),e.once("close",d),e.once("finish",p),e.emit("pipe",r),i.flowing||(ur("pipe resume"),r.resume()),e},fr.prototype.unpipe=function(e){var t=this._readableState;if(0===t.pipesCount)return this;if(1===t.pipesCount)return e&&e!==t.pipes||(e||(e=t.pipes),t.pipes=null,t.pipesCount=0,t.flowing=!1,e&&e.emit("unpipe",this)),this;if(!e){var r=t.pipes,i=t.pipesCount;t.pipes=null,t.pipesCount=0,t.flowing=!1;for(var s=0;s<i;s++)r[s].emit("unpipe",this);return this}var n=Rr(t.pipes,e);return-1===n||(t.pipes.splice(n,1),t.pipesCount-=1,1===t.pipesCount&&(t.pipes=t.pipes[0]),e.emit("unpipe",this)),this},fr.prototype.on=function(e,t){var r=se.prototype.on.call(this,e,t);if("data"===e)!1!==this._readableState.flowing&&this.resume();else if("readable"===e){var i=this._readableState;i.endEmitted||i.readableListening||(i.readableListening=i.needReadable=!0,i.emittedReadable=!1,i.reading?i.length&&gr(this):Pe(wr,this))}return r},fr.prototype.addListener=fr.prototype.on,fr.prototype.resume=function(){var e=this._readableState;return e.flowing||(ur("resume"),e.flowing=!0,function(e,t){t.resumeScheduled||(t.resumeScheduled=!0,Pe(vr,e,t))}(this,e)),this},fr.prototype.pause=function(){return ur("call pause flowing=%j",this._readableState.flowing),!1!==this._readableState.flowing&&(ur("pause"),this._readableState.flowing=!1,this.emit("pause")),this},fr.prototype.wrap=function(e){var t=this._readableState,r=!1,i=this;for(var s in e.on("end",function(){if(ur("wrapped end"),t.decoder&&!t.ended){var e=t.decoder.end();e&&e.length&&i.push(e)}i.push(null)}),e.on("data",function(s){(ur("wrapped data"),t.decoder&&(s=t.decoder.write(s)),t.objectMode&&null==s)||(t.objectMode||s&&s.length)&&(i.push(s)||(r=!0,e.pause()))}),e)void 0===this[s]&&"function"==typeof e[s]&&(this[s]=function(t){return function(){return e[t].apply(e,arguments)}}(s));return function(e,t){for(var r=0,i=e.length;r<i;r++)t(e[r],r)}(["error","close","destroy","pause","resume"],function(t){e.on(t,i.emit.bind(i,t))}),i._read=function(t){ur("wrapped _read",t),r&&(r=!1,e.resume())},i},fr._fromList=_r,Ar.WritableState=Er,Te(Ar,se),Er.prototype.getBuffer=function(){for(var e=this.bufferedRequest,t=[];e;)t.push(e),e=e.next;return t},Ar.prototype.pipe=function(){this.emit("error",new Error("Cannot pipe, not readable"))},Ar.prototype.write=function(e,t,r){var i=this._writableState,s=!1;return"function"==typeof t&&(r=t,t=null),vt.isBuffer(e)?t="buffer":t||(t=i.defaultEncoding),"function"!=typeof r&&(r=jr),i.ended?function(e,t){var r=new Error("write after end");e.emit("error",r),Pe(t,r)}(this,r):function(e,t,r,i){var s=!0,n=!1;return null===r?n=new TypeError("May not write null values to stream"):vt.isBuffer(r)||"string"==typeof r||void 0===r||t.objectMode||(n=new TypeError("Invalid non-string/buffer chunk")),n&&(e.emit("error",n),Pe(i,n),s=!1),s}(this,i,e,r)&&(i.pendingcb++,s=function(e,t,r,i,s){r=function(e,t,r){e.objectMode||!1===e.decodeStrings||"string"!=typeof t||(t=vt.from(t,r));return t}(t,r,i),vt.isBuffer(r)&&(i="buffer");var n=t.objectMode?1:r.length;t.length+=n;var o=t.length<t.highWaterMark;o||(t.needDrain=!0);if(t.writing||t.corked){var a=t.lastBufferedRequest;t.lastBufferedRequest=new xr(r,i,s),a?a.next=t.lastBufferedRequest:t.bufferedRequest=t.lastBufferedRequest,t.bufferedRequestCount+=1}else Dr(e,t,!1,n,r,i,s);return o}(this,i,e,t,r)),s},Ar.prototype.cork=function(){this._writableState.corked++},Ar.prototype.uncork=function(){var e=this._writableState;e.corked&&(e.corked--,e.writing||e.corked||e.finished||e.bufferProcessing||!e.bufferedRequest||Nr(this,e))},Ar.prototype.setDefaultEncoding=function(e){if("string"==typeof e&&(e=e.toLowerCase()),!(["hex","utf8","utf-8","ascii","binary","base64","ucs2","ucs-2","utf16le","utf-16le","raw"].indexOf((e+"").toLowerCase())>-1))throw new TypeError("Unknown encoding: "+e);return this._writableState.defaultEncoding=e,this},Ar.prototype._write=function(e,t,r){r(new Error("not implemented"))},Ar.prototype._writev=null,Ar.prototype.end=function(e,t,r){var i=this._writableState;"function"==typeof e?(r=e,e=null,t=null):"function"==typeof t&&(r=t,t=null),null!=e&&this.write(e,t),i.corked&&(i.corked=1,this.uncork()),i.ending||i.finished||function(e,t,r){t.ending=!0,Ir(e,t),r&&(t.finished?Pe(r):e.once("finish",r));t.ended=!0,e.writable=!1}(this,i,r)},Te(Fr,fr);for(var Mr=Object.keys(Ar.prototype),Lr=0;Lr<Mr.length;Lr++){var Ur=Mr[Lr];Fr.prototype[Ur]||(Fr.prototype[Ur]=Ar.prototype[Ur])}function Fr(e){if(!(this instanceof Fr))return new Fr(e);fr.call(this,e),Ar.call(this,e),e&&!1===e.readable&&(this.readable=!1),e&&!1===e.writable&&(this.writable=!1),this.allowHalfOpen=!0,e&&!1===e.allowHalfOpen&&(this.allowHalfOpen=!1),this.once("end",Br)}function Br(){this.allowHalfOpen||this._writableState.ended||Pe(qr,this)}function qr(e){e.end()}function zr(e){this.afterTransform=function(t,r){return function(e,t,r){var i=e._transformState;i.transforming=!1;var s=i.writecb;if(!s)return e.emit("error",new Error("no writecb in Transform class"));i.writechunk=null,i.writecb=null,null!=r&&e.push(r);s(t);var n=e._readableState;n.reading=!1,(n.needReadable||n.length<n.highWaterMark)&&e._read(n.highWaterMark)}(e,t,r)},this.needTransform=!1,this.transforming=!1,this.writecb=null,this.writechunk=null,this.writeencoding=null}function Hr(e){if(!(this instanceof Hr))return new Hr(e);Fr.call(this,e),this._transformState=new zr(this);var t=this;this._readableState.needReadable=!0,this._readableState.sync=!1,e&&("function"==typeof e.transform&&(this._transform=e.transform),"function"==typeof e.flush&&(this._flush=e.flush)),this.once("prefinish",function(){"function"==typeof this._flush?this._flush(function(e){Vr(t,e)}):Vr(t)})}function Vr(e,t){if(t)return e.emit("error",t);var r=e._writableState,i=e._transformState;if(r.length)throw new Error("Calling transform done when ws.length != 0");if(i.transforming)throw new Error("Calling transform done when still transforming");return e.push(null)}function Kr(e){if(!(this instanceof Kr))return new Kr(e);Hr.call(this,e)}function Wr(){se.call(this)}Te(Hr,Fr),Hr.prototype.push=function(e,t){return this._transformState.needTransform=!1,Fr.prototype.push.call(this,e,t)},Hr.prototype._transform=function(e,t,r){throw new Error("Not implemented")},Hr.prototype._write=function(e,t,r){var i=this._transformState;if(i.writecb=r,i.writechunk=e,i.writeencoding=t,!i.transforming){var s=this._readableState;(i.needTransform||s.needReadable||s.length<s.highWaterMark)&&this._read(s.highWaterMark)}},Hr.prototype._read=function(e){var t=this._transformState;null!==t.writechunk&&t.writecb&&!t.transforming?(t.transforming=!0,this._transform(t.writechunk,t.writeencoding,t.afterTransform)):t.needTransform=!0},Te(Kr,Hr),Kr.prototype._transform=function(e,t,r){r(null,e)},Te(Wr,se),Wr.Readable=fr,Wr.Writable=Ar,Wr.Duplex=Fr,Wr.Transform=Hr,Wr.PassThrough=Kr,Wr.Stream=Wr,Wr.prototype.pipe=function(e,t){var r=this;function i(t){e.writable&&!1===e.write(t)&&r.pause&&r.pause()}function s(){r.readable&&r.resume&&r.resume()}r.on("data",i),e.on("drain",s),e._isStdio||t&&!1===t.end||(r.on("end",o),r.on("close",a));var n=!1;function o(){n||(n=!0,e.end())}function a(){n||(n=!0,"function"==typeof e.destroy&&e.destroy())}function c(e){if(l(),0===se.listenerCount(this,"error"))throw e}function l(){r.removeListener("data",i),e.removeListener("drain",s),r.removeListener("end",o),r.removeListener("close",a),r.removeListener("error",c),e.removeListener("error",c),r.removeListener("end",l),r.removeListener("close",l),e.removeListener("close",l)}return r.on("error",c),e.on("error",c),r.on("end",l),r.on("close",l),e.on("close",l),e.emit("pipe",r),e};class Jr extends se{constructor({resource:e}){super(),this.resource=e,this.client=e.client,this.stream=new s.ReadableStream({highWaterMark:3*this.client.parallelism,start:this._start.bind(this),pull:this._pull.bind(this),cancel:this._cancel.bind(this)})}build(){return this.stream.getReader()}async _start(e){this.controller=e,this.continuationToken=null,this.closeNextIteration=!1}async _pull(e){if(this.closeNextIteration)return void e.close();const t=await this.client.listObjects({prefix:`resource=${this.resource.name}`,continuationToken:this.continuationToken}),r=t?.Contents.map(e=>e.Key).map(e=>e.replace(this.client.config.keyPrefix,"")).map(e=>e.startsWith("/")?e.replace("/",""):e).map(e=>e.replace(`resource=${this.resource.name}/id=`,""));this.continuationToken=t.NextContinuationToken,this.enqueue(r),t.IsTruncated||(this.closeNextIteration=!0)}enqueue(e){e.forEach(e=>{this.controller.enqueue(e),this.emit("id",e)})}_cancel(e){}}var Gr=Jr;class Yr extends Gr{enqueue(e){this.controller.enqueue(e),this.emit("page",e)}}class Qr extends se{constructor({resource:e,batchSize:t=10,concurrency:r=5}){if(super(),!e)throw new Error("Resource is required for ResourceReader");this.resource=e,this.client=e.client,this.batchSize=t,this.concurrency=r,this.input=new Yr({resource:this.resource}),this.transform=new Hr({objectMode:!0,transform:this._transform.bind(this)}),this.input.on("data",e=>{this.transform.write(e)}),this.input.on("end",()=>{this.transform.end()}),this.input.on("error",e=>{this.emit("error",e)}),this.transform.on("data",e=>{this.emit("data",e)}),this.transform.on("end",()=>{this.emit("end")}),this.transform.on("error",e=>{this.emit("error",e)})}build(){return this}async _transform(e,t,r){const[s,n]=await K(async()=>{await i.PromisePool.for(e).withConcurrency(this.concurrency).handleError(async(e,t)=>{this.emit("error",e,t)}).process(async e=>{const t=await this.resource.get(e);return this.push(t),t})});r(n)}resume(){this.input.resume()}}class Zr extends se{constructor({resource:e,batchSize:t=10,concurrency:r=5}){super(),this.resource=e,this.client=e.client,this.batchSize=t,this.concurrency=r,this.buffer=[],this.writing=!1,this.writable=new Ar({objectMode:!0,write:this._write.bind(this)}),this.writable.on("finish",()=>{this.emit("finish")}),this.writable.on("error",e=>{this.emit("error",e)})}build(){return this}write(e){return this.buffer.push(e),this._maybeWrite().catch(e=>{this.emit("error",e)}),!0}end(){this.ended=!0,this._maybeWrite().catch(e=>{this.emit("error",e)})}async _maybeWrite(){if(!this.writing&&(0!==this.buffer.length||this.ended)){for(this.writing=!0;this.buffer.length>0;){const e=this.buffer.splice(0,this.batchSize),[t,r]=await K(async()=>{await i.PromisePool.for(e).withConcurrency(this.concurrency).handleError(async(e,t)=>{this.emit("error",e,t)}).process(async e=>{const[t,r,i]=await K(async()=>await this.resource.insert(e));return t?i:(this.emit("error",r,e),null)})});t||this.emit("error",r)}this.writing=!1,this.ended&&this.writable.emit("finish")}}async _write(e,t,r){r()}}function Xr(e){return new Promise((t,r)=>{if(!e)return r(new Error("streamToString: stream is undefined"));const i=[];e.on("data",e=>i.push(e)),e.on("error",r),e.on("end",()=>t(Buffer.concat(i).toString("utf-8")))})}var ei=class extends _e{constructor({client:e,keyPrefix:t="cache",ttl:r=0,prefix:i}){super(),this.client=e,this.keyPrefix=t,this.config.ttl=r,this.config.client=e,this.config.prefix=void 0!==i?i:t+(t.endsWith("/")?"":"/")}async _set(e,t){let i=JSON.stringify(t);const s=i.length;return i=r.gzipSync(i).toString("base64"),this.client.putObject({key:be(this.keyPrefix,e),body:i,contentEncoding:"gzip",contentType:"application/gzip",metadata:{compressor:"zlib",compressed:"true","client-id":this.client.id,"length-serialized":String(s),"length-compressed":String(i.length),"compression-gain":(i.length/s).toFixed(2)}})}async _get(e){const[t,i,s]=await K(async()=>{const{Body:t}=await this.client.getObject(be(this.keyPrefix,e));let i=await Xr(t);return i=Buffer.from(i,"base64"),i=r.unzipSync(i).toString(),JSON.parse(i)});if(t)return s;if("NoSuchKey"===i.name||"NotFound"===i.name)return null;throw i}async _del(e){return await this.client.deleteObject(be(this.keyPrefix,e)),!0}async _clear(){const e=await this.client.getAllKeys({prefix:this.keyPrefix});await this.client.deleteObjects(e)}async size(){return(await this.keys()).length}async keys(){const e=await this.client.getAllKeys({prefix:this.keyPrefix}),t=this.keyPrefix.endsWith("/")?this.keyPrefix:this.keyPrefix+"/";return e.map(e=>e.startsWith(t)?e.slice(t.length):e)}};var ti=class extends _e{constructor(e={}){super(e),this.cache={},this.meta={},this.maxSize=void 0!==e.maxSize?e.maxSize:1e3,this.ttl=void 0!==e.ttl?e.ttl:3e5}async _set(e,t){if(this.maxSize>0&&Object.keys(this.cache).length>=this.maxSize){const e=Object.entries(this.meta).sort((e,t)=>e[1].ts-t[1].ts)[0]?.[0];e&&(delete this.cache[e],delete this.meta[e])}return this.cache[e]=t,this.meta[e]={ts:Date.now()},t}async _get(e){if(!Object.prototype.hasOwnProperty.call(this.cache,e))return null;if(this.ttl>0){const t=Date.now(),r=this.meta[e];if(r&&t-r.ts>1e3*this.ttl)return delete this.cache[e],delete this.meta[e],null}return this.cache[e]}async _del(e){return delete this.cache[e],delete this.meta[e],!0}async _clear(e){if(!e)return this.cache={},this.meta={},!0;for(const t of Object.keys(this.cache))t.startsWith(e)&&(delete this.cache[t],delete this.meta[t]);return!0}async size(){return Object.keys(this.cache).length}async keys(){return Object.keys(this.cache)}},ri={};class ii extends _e{constructor({directory:e,prefix:t="cache",ttl:r=36e5,enableCompression:i=!0,compressionThreshold:s=1024,createDirectory:n=!0,fileExtension:o=".cache",enableMetadata:a=!0,maxFileSize:c=10485760,enableStats:l=!1,enableCleanup:u=!0,cleanupInterval:h=3e5,encoding:f="utf8",fileMode:d=420,enableBackup:p=!1,backupSuffix:m=".bak",enableLocking:g=!1,lockTimeout:y=5e3,enableJournal:b=!1,journalFile:w="cache.journal",...v}){if(super(v),!e)throw new Error("FilesystemCache: directory parameter is required");this.directory=we.resolve(e),this.prefix=t,this.ttl=r,this.enableCompression=i,this.compressionThreshold=s,this.createDirectory=n,this.fileExtension=o,this.enableMetadata=a,this.maxFileSize=c,this.enableStats=l,this.enableCleanup=u,this.cleanupInterval=h,this.encoding=f,this.fileMode=d,this.enableBackup=p,this.backupSuffix=m,this.enableLocking=g,this.lockTimeout=y,this.enableJournal=b,this.journalFile=we.join(this.directory,w),this.stats={hits:0,misses:0,sets:0,deletes:0,clears:0,errors:0},this.locks=new Map,this.cleanupTimer=null,this._init()}async _init(){this.createDirectory&&await this._ensureDirectory(this.directory),this.enableCleanup&&this.cleanupInterval>0&&(this.cleanupTimer=setInterval(()=>{this._cleanup().catch(e=>{console.warn("FilesystemCache cleanup error:",e.message)})},this.cleanupInterval))}async _ensureDirectory(e){const[t,r]=await K(async()=>{await n.mkdir(e,{recursive:!0})});if(!t&&"EEXIST"!==r.code)throw new Error(`Failed to create cache directory: ${r.message}`)}_getFilePath(e){const t=e.replace(/[<>:"/\\|?*]/g,"_"),r=`${this.prefix}_${t}${this.fileExtension}`;return we.join(this.directory,r)}_getMetadataPath(e){return e+".meta"}async _set(e,t){const i=this._getFilePath(e);try{let s=JSON.stringify(t);const o=Buffer.byteLength(s,this.encoding);if(o>this.maxFileSize)throw new Error(`Cache data exceeds maximum file size: ${o} > ${this.maxFileSize}`);let a=!1,c=s;if(this.enableCompression&&o>=this.compressionThreshold){c=r.gzipSync(Buffer.from(s,this.encoding)).toString("base64"),a=!0}if(this.enableBackup&&await this._fileExists(i)){const e=i+this.backupSuffix;await this._copyFile(i,e)}this.enableLocking&&await this._acquireLock(i);try{if(await n.writeFile(i,c,{encoding:a?"utf8":this.encoding,mode:this.fileMode}),this.enableMetadata){const t={key:e,timestamp:Date.now(),ttl:this.ttl,compressed:a,originalSize:o,compressedSize:a?Buffer.byteLength(c,"utf8"):o,compressionRatio:a?(Buffer.byteLength(c,"utf8")/o).toFixed(2):1};await n.writeFile(this._getMetadataPath(i),JSON.stringify(t),{encoding:this.encoding,mode:this.fileMode})}this.enableStats&&this.stats.sets++,this.enableJournal&&await this._journalOperation("set",e,{size:o,compressed:a})}finally{this.enableLocking&&this._releaseLock(i)}return t}catch(t){throw this.enableStats&&this.stats.errors++,new Error(`Failed to set cache key '${e}': ${t.message}`)}}async _get(e){const t=this._getFilePath(e);try{if(!await this._fileExists(t))return this.enableStats&&this.stats.misses++,null;let i=!1;if(this.enableMetadata){const e=this._getMetadataPath(t);if(await this._fileExists(e)){const[t,r,s]=await K(async()=>{const t=await n.readFile(e,this.encoding);return JSON.parse(t)});if(t&&s.ttl>0){i=Date.now()-s.timestamp>s.ttl}}}else if(this.ttl>0){const e=await n.stat(t);i=Date.now()-e.mtime.getTime()>this.ttl}if(i)return await this._del(e),this.enableStats&&this.stats.misses++,null;this.enableLocking&&await this._acquireLock(t);try{const e=await n.readFile(t,this.encoding);let i=!1;if(this.enableMetadata){const e=this._getMetadataPath(t);if(await this._fileExists(e)){const[t,r,s]=await K(async()=>{const t=await n.readFile(e,this.encoding);return JSON.parse(t)});t&&(i=s.compressed)}}let s=e;if(i||this.enableCompression&&e.match(/^[A-Za-z0-9+/=]+$/))try{const t=Buffer.from(e,"base64");s=r.gunzipSync(t).toString(this.encoding)}catch(t){s=e}const o=JSON.parse(s);return this.enableStats&&this.stats.hits++,o}finally{this.enableLocking&&this._releaseLock(t)}}catch(t){return this.enableStats&&this.stats.errors++,await this._del(e),null}}async _del(e){const t=this._getFilePath(e);try{if(await this._fileExists(t)&&await n.unlink(t),this.enableMetadata){const e=this._getMetadataPath(t);await this._fileExists(e)&&await n.unlink(e)}if(this.enableBackup){const e=t+this.backupSuffix;await this._fileExists(e)&&await n.unlink(e)}return this.enableStats&&this.stats.deletes++,this.enableJournal&&await this._journalOperation("delete",e),!0}catch(t){throw this.enableStats&&this.stats.errors++,new Error(`Failed to delete cache key '${e}': ${t.message}`)}}async _clear(e){try{if(!await this._fileExists(this.directory))return this.enableStats&&this.stats.clears++,!0;const t=(await n.readdir(this.directory)).filter(t=>{if(!t.startsWith(this.prefix))return!1;if(!t.endsWith(this.fileExtension))return!1;if(e){return t.slice(this.prefix.length+1,-this.fileExtension.length).startsWith(e)}return!0});for(const e of t){const t=we.join(this.directory,e);try{await this._fileExists(t)&&await n.unlink(t)}catch(e){if("ENOENT"!==e.code)throw e}if(this.enableMetadata)try{const e=this._getMetadataPath(t);await this._fileExists(e)&&await n.unlink(e)}catch(e){if("ENOENT"!==e.code)throw e}if(this.enableBackup)try{const e=t+this.backupSuffix;await this._fileExists(e)&&await n.unlink(e)}catch(e){if("ENOENT"!==e.code)throw e}}return this.enableStats&&this.stats.clears++,this.enableJournal&&await this._journalOperation("clear",e||"all",{count:t.length}),!0}catch(e){if("ENOENT"===e.code)return this.enableStats&&this.stats.clears++,!0;throw this.enableStats&&this.stats.errors++,new Error(`Failed to clear cache: ${e.message}`)}}async size(){return(await this.keys()).length}async keys(){try{const e=(await n.readdir(this.directory)).filter(e=>e.startsWith(this.prefix)&&e.endsWith(this.fileExtension));return e.map(e=>e.slice(this.prefix.length+1,-this.fileExtension.length))}catch(e){return console.warn("FilesystemCache: Failed to list keys:",e.message),[]}}async _fileExists(e){const[t]=await K(async()=>{await n.stat(e)});return t}async _copyFile(e,t){const[r,i]=await K(async()=>{const r=await n.readFile(e);await n.writeFile(t,r)});r||console.warn("FilesystemCache: Failed to create backup:",i.message)}async _cleanup(){if(this.ttl&&!(this.ttl<=0))try{const e=await n.readdir(this.directory),t=Date.now();for(const r of e){if(!r.startsWith(this.prefix)||!r.endsWith(this.fileExtension))continue;const e=we.join(this.directory,r);let i=!1;if(this.enableMetadata){const r=this._getMetadataPath(e);if(await this._fileExists(r)){const[e,s,o]=await K(async()=>{const e=await n.readFile(r,this.encoding);return JSON.parse(e)});if(e&&o.ttl>0){i=t-o.timestamp>o.ttl}}}else{const[r,s,o]=await K(async()=>await n.stat(e));if(r){i=t-o.mtime.getTime()>this.ttl}}if(i){const e=r.slice(this.prefix.length+1,-this.fileExtension.length);await this._del(e)}}}catch(e){console.warn("FilesystemCache cleanup error:",e.message)}}async _acquireLock(e){if(!this.enableLocking)return;const t=e,r=Date.now();for(;this.locks.has(t);){if(Date.now()-r>this.lockTimeout)throw new Error(`Lock timeout for file: ${e}`);await new Promise(e=>setTimeout(e,10))}this.locks.set(t,Date.now())}_releaseLock(e){this.enableLocking&&this.locks.delete(e)}async _journalOperation(e,t,r={}){if(!this.enableJournal)return;const i={timestamp:(new Date).toISOString(),operation:e,key:t,metadata:r},[s,n]=await K(async()=>{const e=JSON.stringify(i)+"\n";await ri.promises.appendFile(this.journalFile,e,this.encoding)});s||console.warn("FilesystemCache journal error:",n.message)}destroy(){this.cleanupTimer&&(clearInterval(this.cleanupTimer),this.cleanupTimer=null)}getStats(){return{...this.stats,directory:this.directory,ttl:this.ttl,compression:this.enableCompression,metadata:this.enableMetadata,cleanup:this.enableCleanup,locking:this.enableLocking,journal:this.enableJournal}}}class si extends ii{constructor({partitionStrategy:e="hierarchical",trackUsage:t=!0,preloadRelated:r=!1,preloadThreshold:i=10,maxCacheSize:s=null,usageStatsFile:n="partition-usage.json",...o}){super(o),this.partitionStrategy=e,this.trackUsage=t,this.preloadRelated=r,this.preloadThreshold=i,this.maxCacheSize=s,this.usageStatsFile=we.join(this.directory,n),this.partitionUsage=new Map,this.loadUsageStats()}_getPartitionCacheKey(e,t,r,i={},s={}){const n=[`resource=${e}`,`action=${t}`];if(r&&Object.keys(i).length>0){n.push(`partition=${r}`);const e=Object.entries(i).sort(([e],[t])=>e.localeCompare(t));for(const[t,r]of e)null!=r&&n.push(`${t}=${r}`)}if(Object.keys(s).length>0){const e=Object.entries(s).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>`${e}=${t}`).join("|");n.push(`params=${Buffer.from(e).toString("base64")}`)}return n.join("/")+this.fileExtension}_getPartitionDirectory(e,t,r={}){const i=we.join(this.directory,`resource=${e}`);if(!t)return i;if("flat"===this.partitionStrategy)return we.join(i,"partitions");if("temporal"===this.partitionStrategy&&this._isTemporalPartition(t,r))return this._getTemporalDirectory(i,t,r);const s=[i,`partition=${t}`],n=Object.entries(r).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of n)null!=t&&s.push(`${e}=${this._sanitizePathValue(t)}`);return we.join(...s)}async _set(e,t,r={}){const{resource:i,action:s,partition:n,partitionValues:o,params:a}=r;if(i&&n){const e=this._getPartitionCacheKey(i,s,n,o,a),r=this._getPartitionDirectory(i,n,o);await this._ensureDirectory(r);const c=we.join(r,this._sanitizeFileName(e));this.trackUsage&&await this._trackPartitionUsage(i,n,o);const l={data:t,metadata:{resource:i,partition:n,partitionValues:o,timestamp:Date.now(),ttl:this.ttl}};return this._writeFileWithMetadata(c,l)}return super._set(e,t)}async set(e,t,r,i={}){if("string"==typeof e&&"string"==typeof t&&i.partition){const s=this._getPartitionCacheKey(e,t,i.partition,i.partitionValues,i.params);return this._set(s,r,{resource:e,action:t,...i})}return super.set(e,t)}async get(e,t,r={}){if("string"==typeof e&&"string"==typeof t&&r.partition){const i=this._getPartitionCacheKey(e,t,r.partition,r.partitionValues,r.params);return this._get(i,{resource:e,action:t,...r})}return super.get(e)}async _get(e,t={}){const{resource:r,action:i,partition:s,partitionValues:n,params:o}=t;if(r&&s){const e=this._getPartitionCacheKey(r,i,s,n,o),t=this._getPartitionDirectory(r,s,n),a=we.join(t,this._sanitizeFileName(e));if(!await this._fileExists(a))return this.preloadRelated&&await this._preloadRelatedPartitions(r,s,n),null;const c=await this._readFileWithMetadata(a);return c&&this.trackUsage&&await this._trackPartitionUsage(r,s,n),c?.data||null}return super._get(e)}async clearPartition(e,t,r={}){const i=this._getPartitionDirectory(e,t,r),[s,o]=await K(async()=>{await this._fileExists(i)&&await n.rm(i,{recursive:!0})});s||console.warn(`Failed to clear partition cache: ${o.message}`);const a=this._getUsageKey(e,t,r);return this.partitionUsage.delete(a),await this._saveUsageStats(),s}async clearResourcePartitions(e){const t=we.join(this.directory,`resource=${e}`),[r,i]=await K(async()=>{await this._fileExists(t)&&await n.rm(t,{recursive:!0})});for(const[t]of this.partitionUsage.entries())t.startsWith(`${e}/`)&&this.partitionUsage.delete(t);return await this._saveUsageStats(),r}async getPartitionStats(e,t=null){const r={totalFiles:0,totalSize:0,partitions:{},usage:{}},i=we.join(this.directory,`resource=${e}`);if(!await this._fileExists(i))return r;await this._calculateDirectoryStats(i,r);for(const[i,s]of this.partitionUsage.entries())if(i.startsWith(`${e}/`)){const e=i.split("/")[1];t&&e!==t||(r.usage[e]=s)}return r}async getCacheRecommendations(e){const t=[],r=Date.now();for(const[i,s]of this.partitionUsage.entries())if(i.startsWith(`${e}/`)){const[,e]=i.split("/"),n=(r-s.lastAccess)/864e5,o=s.count/Math.max(1,n);let a="keep",c=s.count;n>30?(a="archive",c=0):o<.1?(a="reduce_ttl",c=1):o>10&&(a="preload",c=100),t.push({partition:e,recommendation:a,priority:c,usage:o,lastAccess:new Date(s.lastAccess).toISOString()})}return t.sort((e,t)=>t.priority-e.priority)}async warmPartitionCache(e,t={}){const{partitions:r=[],maxFiles:i=1e3}=t;let s=0;for(const t of r){const r=`${e}/${t}`,n=this.partitionUsage.get(r);if(n&&n.count>=this.preloadThreshold&&(console.log(`🔥 Warming cache for ${e}/${t} (${n.count} accesses)`),s++),s>=i)break}return s}async _trackPartitionUsage(e,t,r){const i=this._getUsageKey(e,t,r),s=this.partitionUsage.get(i)||{count:0,firstAccess:Date.now(),lastAccess:Date.now()};s.count++,s.lastAccess=Date.now(),this.partitionUsage.set(i,s),s.count%10==0&&await this._saveUsageStats()}_getUsageKey(e,t,r){const i=Object.entries(r).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>`${e}=${t}`).join("|");return`${e}/${t}/${i}`}async _preloadRelatedPartitions(e,t,r){console.log(`🎯 Preloading related partitions for ${e}/${t}`),r.timestamp||r.date}_isTemporalPartition(e,t){const r=["date","timestamp","createdAt","updatedAt"];return Object.keys(t).some(e=>r.some(t=>e.toLowerCase().includes(t)))}_getTemporalDirectory(e,t,r){const i=Object.values(r)[0];if("string"==typeof i&&i.match(/^\d{4}-\d{2}-\d{2}/)){const[t,r,s]=i.split("-");return we.join(e,"temporal",t,r,s)}return we.join(e,`partition=${t}`)}_sanitizePathValue(e){return String(e).replace(/[<>:"/\\|?*]/g,"_")}_sanitizeFileName(e){return e.replace(/[<>:"/\\|?*]/g,"_")}async _calculateDirectoryStats(e,t){const[r,i,s]=await K(()=>n.readdir(e));if(r)for(const r of s){const i=we.join(e,r),[s,o,a]=await K(()=>n.stat(i));s&&(a.isDirectory()?await this._calculateDirectoryStats(i,t):(t.totalFiles++,t.totalSize+=a.size))}}async loadUsageStats(){const[e,t,r]=await K(async()=>{const e=await n.readFile(this.usageStatsFile,"utf8");return JSON.parse(e)});e&&r&&(this.partitionUsage=new Map(Object.entries(r)))}async _saveUsageStats(){const e=Object.fromEntries(this.partitionUsage);await K(async()=>{await n.writeFile(this.usageStatsFile,JSON.stringify(e,null,2),"utf8")})}async _writeFileWithMetadata(e,t){const r=JSON.stringify(t),[i,s]=await K(async()=>{await n.writeFile(e,r,{encoding:this.encoding,mode:this.fileMode})});if(!i)throw new Error(`Failed to write cache file: ${s.message}`);return!0}async _readFileWithMetadata(e){const[t,r,i]=await K(async()=>await n.readFile(e,this.encoding));if(!t||!i)return null;try{return JSON.parse(i)}catch(e){return{data:i}}}}const ni={async setup(e){e&&e.client&&(this.client=e.client,this.map={PutObjectCommand:"put",GetObjectCommand:"get",HeadObjectCommand:"head",DeleteObjectCommand:"delete",DeleteObjectsCommand:"delete",ListObjectsV2Command:"list"},this.costs={total:0,prices:{put:5e-6,copy:5e-6,list:5e-6,post:5e-6,get:4e-4/1e3,select:4e-4/1e3,delete:4e-4/1e3,head:4e-4/1e3},requests:{total:0,put:0,post:0,copy:0,list:0,get:0,select:0,delete:0,head:0},events:{total:0,PutObjectCommand:0,GetObjectCommand:0,HeadObjectCommand:0,DeleteObjectCommand:0,DeleteObjectsCommand:0,ListObjectsV2Command:0}},this.client.costs=JSON.parse(JSON.stringify(this.costs)))},async start(){this.client&&(this.client.on("command.response",e=>this.addRequest(e,this.map[e])),this.client.on("command.error",e=>this.addRequest(e,this.map[e])))},addRequest(e,t){t&&(this.costs.events[e]++,this.costs.events.total++,this.costs.requests.total++,this.costs.requests[t]++,this.costs.total+=this.costs.prices[t],this.client&&this.client.costs&&(this.client.costs.events[e]++,this.client.costs.events.total++,this.client.costs.requests.total++,this.client.costs.requests[t]++,this.client.costs.total+=this.client.costs.prices[t]))}};var oi=class extends se{constructor(e={}){super(),this.config=e,this.name=this.constructor.name,this.enabled=!1!==e.enabled}async initialize(e){this.database=e,this.emit("initialized",{replicator:this.name})}async replicate(e,t,r,i){throw new Error(`replicate() method must be implemented by ${this.name}`)}async replicateBatch(e,t){throw new Error(`replicateBatch() method must be implemented by ${this.name}`)}async testConnection(){throw new Error(`testConnection() method must be implemented by ${this.name}`)}async getStatus(){return{name:this.name,config:this.config,connected:!1}}async cleanup(){this.emit("cleanup",{replicator:this.name})}validateConfig(){return{isValid:!0,errors:[]}}};var ai=class extends oi{constructor(e={},t={}){super(e),this.projectId=e.projectId,this.datasetId=e.datasetId,this.bigqueryClient=null,this.credentials=e.credentials,this.location=e.location||"US",this.logTable=e.logTable,this.resources=this.parseResourcesConfig(t)}parseResourcesConfig(e){const t={};for(const[r,i]of Object.entries(e))"string"==typeof i?t[r]=[{table:i,actions:["insert"],transform:null}]:Array.isArray(i)?t[r]=i.map(e=>"string"==typeof e?{table:e,actions:["insert"],transform:null}:{table:e.table,actions:e.actions||["insert"],transform:e.transform||null}):"object"==typeof i&&(t[r]=[{table:i.table,actions:i.actions||["insert"],transform:i.transform||null}]);return t}validateConfig(){const e=[];this.projectId||e.push("projectId is required"),this.datasetId||e.push("datasetId is required"),0===Object.keys(this.resources).length&&e.push("At least one resource must be configured");for(const[t,r]of Object.entries(this.resources))for(const i of r){i.table||e.push(`Table name is required for resource '${t}'`),Array.isArray(i.actions)&&0!==i.actions.length||e.push(`Actions array is required for resource '${t}'`);const r=["insert","update","delete"],s=i.actions.filter(e=>!r.includes(e));s.length>0&&e.push(`Invalid actions for resource '${t}': ${s.join(", ")}. Valid actions: ${r.join(", ")}`),i.transform&&"function"!=typeof i.transform&&e.push(`Transform must be a function for resource '${t}'`)}return{isValid:0===e.length,errors:e}}async initialize(e){await super.initialize(e);const[t,r,i]=await K(()=>import("@google-cloud/bigquery"));if(!t)throw this.config.verbose&&console.warn(`[BigqueryReplicator] Failed to import BigQuery SDK: ${r.message}`),this.emit("initialization_error",{replicator:this.name,error:r.message}),r;const{BigQuery:s}=i;this.bigqueryClient=new s({projectId:this.projectId,credentials:this.credentials,location:this.location}),this.emit("initialized",{replicator:this.name,projectId:this.projectId,datasetId:this.datasetId,resources:Object.keys(this.resources)})}shouldReplicateResource(e){return this.resources.hasOwnProperty(e)}shouldReplicateAction(e,t){return!!this.resources[e]&&this.resources[e].some(e=>e.actions.includes(t))}getTablesForResource(e,t){return this.resources[e]?this.resources[e].filter(e=>e.actions.includes(t)).map(e=>({table:e.table,transform:e.transform})):[]}applyTransform(e,t){let r=this._cleanInternalFields(e);return t?t(JSON.parse(JSON.stringify(r))):r}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}async replicate(e,t,r,i,s=null){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};if(!this.shouldReplicateAction(e,t))return{skipped:!0,reason:"action_not_included"};const n=this.getTablesForResource(e,t);if(0===n.length)return{skipped:!0,reason:"no_tables_for_action"};const o=[],a=[],[c,l,u]=await K(async()=>{const s=this.bigqueryClient.dataset(this.datasetId);for(const e of n){const[n,c]=await K(async()=>{const n=s.table(e.table);let a;if("insert"===t){const t=this.applyTransform(r,e.transform);try{a=await n.insert([t])}catch(e){const{errors:t,response:r}=e;throw this.config.verbose&&(console.error("[BigqueryReplicator] BigQuery insert error details:"),t&&console.error(JSON.stringify(t,null,2)),r&&console.error(JSON.stringify(r,null,2))),e}}else if("update"===t){const t=this.applyTransform(r,e.transform),s=Object.keys(t).filter(e=>"id"!==e).map(e=>`${e} = @${e}`).join(", "),n={id:i,...t},o=`UPDATE \`${this.projectId}.${this.datasetId}.${e.table}\` SET ${s} WHERE id = @id`,c=2;let l=null;for(let e=1;e<=c;e++){const[t,r]=await K(async()=>{const[e]=await this.bigqueryClient.createQueryJob({query:o,params:n,location:this.location});return await e.getQueryResults(),[e]});if(t){a=t;break}if(l=r,this.config.verbose&&(console.warn(`[BigqueryReplicator] Update attempt ${e} failed: ${r.message}`),r.errors&&(console.error("[BigqueryReplicator] BigQuery update error details:"),console.error("Errors:",JSON.stringify(r.errors,null,2)))),r?.message?.includes("streaming buffer")&&e<c){const e=30;this.config.verbose&&console.warn(`[BigqueryReplicator] Retrying in ${e} seconds due to streaming buffer issue`),await new Promise(t=>setTimeout(t,1e3*e));continue}throw r}if(!a)throw l}else{if("delete"!==t)throw new Error(`Unsupported operation: ${t}`);{const t=`DELETE FROM \`${this.projectId}.${this.datasetId}.${e.table}\` WHERE id = @id`;try{const[e]=await this.bigqueryClient.createQueryJob({query:t,params:{id:i},location:this.location});await e.getQueryResults(),a=[e]}catch(e){throw this.config.verbose&&(console.error("[BigqueryReplicator] BigQuery delete error details:"),console.error("Query:",t),e.errors&&console.error("Errors:",JSON.stringify(e.errors,null,2)),e.response&&console.error("Response:",JSON.stringify(e.response,null,2))),e}}}o.push({table:e.table,success:!0,jobId:a[0]?.id})});n||a.push({table:e.table,error:c.message})}if(this.logTable){const[n,o]=await K(async()=>{const n=s.table(this.logTable);await n.insert([{resource_name:e,operation:t,record_id:i,data:JSON.stringify(r),timestamp:(new Date).toISOString(),source:"s3db-replicator"}])})}const c=0===a.length;return a.length>0&&console.warn(`[BigqueryReplicator] Replication completed with errors for ${e}:`,a),this.emit("replicated",{replicator:this.name,resourceName:e,operation:t,id:i,tables:n.map(e=>e.table),results:o,errors:a,success:c}),{success:c,results:o,errors:a,tables:n.map(e=>e.table)}});return c?u:(this.config.verbose&&console.warn(`[BigqueryReplicator] Replication failed for ${e}: ${l.message}`),this.emit("replicator_error",{replicator:this.name,resourceName:e,operation:t,id:i,error:l.message}),{success:!1,error:l.message})}async replicateBatch(e,t){const r=[],i=[];for(const s of t){const[t,n,o]=await K(()=>this.replicate(e,s.operation,s.data,s.id,s.beforeData));t?r.push(o):(this.config.verbose&&console.warn(`[BigqueryReplicator] Batch replication failed for record ${s.id}: ${n.message}`),i.push({id:s.id,error:n.message}))}return i.length>0&&console.warn(`[BigqueryReplicator] Batch replication completed with ${i.length} error(s) for ${e}:`,i),{success:0===i.length,results:r,errors:i}}async testConnection(){const[e,t]=await K(async()=>{this.bigqueryClient||await this.initialize();const e=this.bigqueryClient.dataset(this.datasetId);return await e.getMetadata(),!0});return!!e||(this.config.verbose&&console.warn(`[BigqueryReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}async cleanup(){}getStatus(){return{...super.getStatus(),projectId:this.projectId,datasetId:this.datasetId,resources:this.resources,logTable:this.logTable}}};var ci=class extends oi{constructor(e={},t={}){super(e),this.connectionString=e.connectionString,this.host=e.host,this.port=e.port||5432,this.database=e.database,this.user=e.user,this.password=e.password,this.client=null,this.ssl=e.ssl,this.logTable=e.logTable,this.resources=this.parseResourcesConfig(t)}parseResourcesConfig(e){const t={};for(const[r,i]of Object.entries(e))"string"==typeof i?t[r]=[{table:i,actions:["insert"]}]:Array.isArray(i)?t[r]=i.map(e=>"string"==typeof e?{table:e,actions:["insert"]}:{table:e.table,actions:e.actions||["insert"]}):"object"==typeof i&&(t[r]=[{table:i.table,actions:i.actions||["insert"]}]);return t}validateConfig(){const e=[];this.connectionString||this.host&&this.database||e.push("Either connectionString or host+database must be provided"),0===Object.keys(this.resources).length&&e.push("At least one resource must be configured");for(const[t,r]of Object.entries(this.resources))for(const i of r){i.table||e.push(`Table name is required for resource '${t}'`),Array.isArray(i.actions)&&0!==i.actions.length||e.push(`Actions array is required for resource '${t}'`);const r=["insert","update","delete"],s=i.actions.filter(e=>!r.includes(e));s.length>0&&e.push(`Invalid actions for resource '${t}': ${s.join(", ")}. Valid actions: ${r.join(", ")}`)}return{isValid:0===e.length,errors:e}}async initialize(e){await super.initialize(e);const[t,r,i]=await K(()=>import("pg"));if(!t)throw this.config.verbose&&console.warn(`[PostgresReplicator] Failed to import pg SDK: ${r.message}`),this.emit("initialization_error",{replicator:this.name,error:r.message}),r;const{Client:s}=i,n=this.connectionString?{connectionString:this.connectionString,ssl:this.ssl}:{host:this.host,port:this.port,database:this.database,user:this.user,password:this.password,ssl:this.ssl};this.client=new s(n),await this.client.connect(),this.logTable&&await this.createLogTableIfNotExists(),this.emit("initialized",{replicator:this.name,database:this.database||"postgres",resources:Object.keys(this.resources)})}async createLogTableIfNotExists(){const e=`\n CREATE TABLE IF NOT EXISTS ${this.logTable} (\n id SERIAL PRIMARY KEY,\n resource_name VARCHAR(255) NOT NULL,\n operation VARCHAR(50) NOT NULL,\n record_id VARCHAR(255) NOT NULL,\n data JSONB,\n timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(),\n source VARCHAR(100) DEFAULT 's3db-replicator',\n created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n );\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_resource_name ON ${this.logTable}(resource_name);\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_operation ON ${this.logTable}(operation);\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_record_id ON ${this.logTable}(record_id);\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_timestamp ON ${this.logTable}(timestamp);\n `;await this.client.query(e)}shouldReplicateResource(e){return this.resources.hasOwnProperty(e)}shouldReplicateAction(e,t){return!!this.resources[e]&&this.resources[e].some(e=>e.actions.includes(t))}getTablesForResource(e,t){return this.resources[e]?this.resources[e].filter(e=>e.actions.includes(t)).map(e=>e.table):[]}async replicate(e,t,r,i,s=null){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};if(!this.shouldReplicateAction(e,t))return{skipped:!0,reason:"action_not_included"};const n=this.getTablesForResource(e,t);if(0===n.length)return{skipped:!0,reason:"no_tables_for_action"};const o=[],a=[],[c,l,u]=await K(async()=>{for(const e of n){const[s,n]=await K(async()=>{let s;if("insert"===t){const t=this._cleanInternalFields(r),i=Object.keys(t),n=i.map(e=>t[e]),o=i.map(e=>`"${e}"`).join(", "),a=i.map((e,t)=>`$${t+1}`).join(", "),c=`INSERT INTO ${e} (${o}) VALUES (${a}) ON CONFLICT (id) DO NOTHING RETURNING *`;s=await this.client.query(c,n)}else if("update"===t){const t=this._cleanInternalFields(r),n=Object.keys(t).filter(e=>"id"!==e),o=n.map((e,t)=>`"${e}"=$${t+1}`).join(", "),a=n.map(e=>t[e]);a.push(i);const c=`UPDATE ${e} SET ${o} WHERE id=$${n.length+1} RETURNING *`;s=await this.client.query(c,a)}else{if("delete"!==t)throw new Error(`Unsupported operation: ${t}`);{const t=`DELETE FROM ${e} WHERE id=$1 RETURNING *`;s=await this.client.query(t,[i])}}o.push({table:e,success:!0,rows:s.rows,rowCount:s.rowCount})});s||a.push({table:e,error:n.message})}if(this.logTable){const[s,n]=await K(async()=>{await this.client.query(`INSERT INTO ${this.logTable} (resource_name, operation, record_id, data, timestamp, source) VALUES ($1, $2, $3, $4, $5, $6)`,[e,t,i,JSON.stringify(r),(new Date).toISOString(),"s3db-replicator"])})}const s=0===a.length;return a.length>0&&console.warn(`[PostgresReplicator] Replication completed with errors for ${e}:`,a),this.emit("replicated",{replicator:this.name,resourceName:e,operation:t,id:i,tables:n,results:o,errors:a,success:s}),{success:s,results:o,errors:a,tables:n}});return c?u:(this.config.verbose&&console.warn(`[PostgresReplicator] Replication failed for ${e}: ${l.message}`),this.emit("replicator_error",{replicator:this.name,resourceName:e,operation:t,id:i,error:l.message}),{success:!1,error:l.message})}async replicateBatch(e,t){const r=[],i=[];for(const s of t){const[t,n,o]=await K(()=>this.replicate(e,s.operation,s.data,s.id,s.beforeData));t?r.push(o):(this.config.verbose&&console.warn(`[PostgresReplicator] Batch replication failed for record ${s.id}: ${n.message}`),i.push({id:s.id,error:n.message}))}return i.length>0&&console.warn(`[PostgresReplicator] Batch replication completed with ${i.length} error(s) for ${e}:`,i),{success:0===i.length,results:r,errors:i}}async testConnection(){const[e,t]=await K(async()=>(this.client||await this.initialize(),await this.client.query("SELECT 1"),!0));return!!e||(this.config.verbose&&console.warn(`[PostgresReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}async cleanup(){this.client&&await this.client.end()}getStatus(){return{...super.getStatus(),database:this.database||"postgres",resources:this.resources,logTable:this.logTable}}};function li(){}function ui(){}li.defaultMaxSockets=4,ui.defaultMaxSockets=4;class hi{constructor(e){let t;const[r,i,s]=K(()=>new URL(e));if(!r)throw new U("Invalid connection string: "+e,{original:i,input:e});t=s,this.region="us-east-1","s3:"===t.protocol?this.defineFromS3(t):this.defineFromCustomUri(t);for(const[e,r]of t.searchParams.entries())this[e]=r}defineFromS3(e){const[t,r,i]=V(()=>decodeURIComponent(e.hostname));if(!t)throw new U("Invalid bucket in connection string",{original:r,input:e.hostname});this.bucket=i||"s3db";const[s,n,o]=V(()=>decodeURIComponent(e.username));if(!s)throw new U("Invalid accessKeyId in connection string",{original:n,input:e.username});this.accessKeyId=o;const[a,c,l]=V(()=>decodeURIComponent(e.password));if(!a)throw new U("Invalid secretAccessKey in connection string",{original:c,input:e.password});if(this.secretAccessKey=l,this.endpoint="https://s3.us-east-1.amazonaws.com",["/","",null].includes(e.pathname))this.keyPrefix="";else{let[,...t]=e.pathname.split("/");this.keyPrefix=[...t||[]].join("/")}}defineFromCustomUri(e){this.forcePathStyle=!0,this.endpoint=e.origin;const[t,r,i]=V(()=>decodeURIComponent(e.username));if(!t)throw new U("Invalid accessKeyId in connection string",{original:r,input:e.username});this.accessKeyId=i;const[s,n,o]=V(()=>decodeURIComponent(e.password));if(!s)throw new U("Invalid secretAccessKey in connection string",{original:n,input:e.password});if(this.secretAccessKey=o,["/","",null].includes(e.pathname))this.bucket="s3db",this.keyPrefix="";else{let[,t,...r]=e.pathname.split("/");if(t){const[e,r,i]=V(()=>decodeURIComponent(t));if(!e)throw new U("Invalid bucket in connection string",{original:r,input:t});this.bucket=i}else this.bucket="s3db";this.keyPrefix=[...r||[]].join("/")}}}class fi extends se{constructor({verbose:e=!1,id:t=null,AwsS3Client:r,connectionString:i,parallelism:s=10,httpClientOptions:n={}}){super(),this.verbose=e,this.id=t??X(77),this.parallelism=s,this.config=new hi(i),this.httpClientOptions={keepAlive:!0,keepAliveMsecs:1e3,maxSockets:50,maxFreeSockets:10,timeout:6e4,...n},this.client=r||this.createClient()}createClient(){const e=new li(this.httpClientOptions),t=new ui(this.httpClientOptions),r=new l.NodeHttpHandler({httpAgent:e,httpsAgent:t});let i={region:this.config.region,endpoint:this.config.endpoint,requestHandler:r};this.config.forcePathStyle&&(i.forcePathStyle=!0),this.config.accessKeyId&&(i.credentials={accessKeyId:this.config.accessKeyId,secretAccessKey:this.config.secretAccessKey});const s=new u.S3Client(i);return s.middlewareStack.add((e,t)=>async r=>{if("DeleteObjectsCommand"===t.commandName){const e=r.request.body;if(e&&"string"==typeof e){const t=await Q(e);r.request.headers["Content-MD5"]=t}}return e(r)},{step:"build",name:"addContentMd5ForDeleteObjects",priority:"high"}),s}async sendCommand(e){this.emit("command.request",e.constructor.name,e.input);const[t,r,i]=await K(()=>this.client.send(e));if(!t){throw L(r,{bucket:this.config.bucket,key:e.input&&e.input.Key,commandName:e.constructor.name,commandInput:e.input})}return this.emit("command.response",e.constructor.name,i,e.input),i}async putObject({key:e,metadata:t,contentType:r,body:i,contentEncoding:s,contentLength:n}){const o="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"";o&&we.join(o,e);const a={};if(t)for(const[e,r]of Object.entries(t)){const t=String(e).replace(/[^a-zA-Z0-9\-_]/g,"_"),i=String(r),s=/[^\x00-\x7F]/.test(i);a[t]=s?Buffer.from(i,"utf8").toString("base64"):i}const c={Bucket:this.config.bucket,Key:o?we.join(o,e):e,Metadata:a,Body:i||Buffer.alloc(0)};let l,h;void 0!==r&&(c.ContentType=r),void 0!==s&&(c.ContentEncoding=s),void 0!==n&&(c.ContentLength=n);try{return l=await this.sendCommand(new u.PutObjectCommand(c)),l}catch(t){throw h=t,L(t,{bucket:this.config.bucket,key:e,commandName:"PutObjectCommand",commandInput:c})}finally{this.emit("putObject",h||l,{key:e,metadata:t,contentType:r,body:i,contentEncoding:s,contentLength:n})}}async getObject(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"",r={Bucket:this.config.bucket,Key:t?we.join(t,e):e};let i,s;try{if(i=await this.sendCommand(new u.GetObjectCommand(r)),i.Metadata){const e={};for(const[t,r]of Object.entries(i.Metadata))if("string"==typeof r)try{const i=Buffer.from(r,"base64").toString("utf8"),s=/[^\x00-\x7F]/.test(i),n=Buffer.from(i,"utf8").toString("base64")===r;e[t]=n&&s&&i!==r?i:r}catch(i){e[t]=r}else e[t]=r;i.Metadata=e}return i}catch(t){throw s=t,L(t,{bucket:this.config.bucket,key:e,commandName:"GetObjectCommand",commandInput:r})}finally{this.emit("getObject",s||i,{key:e})}}async headObject(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"",r={Bucket:this.config.bucket,Key:t?we.join(t,e):e};let i,s;try{return i=await this.sendCommand(new u.HeadObjectCommand(r)),i}catch(t){throw s=t,L(t,{bucket:this.config.bucket,key:e,commandName:"HeadObjectCommand",commandInput:r})}finally{this.emit("headObject",s||i,{key:e})}}async copyObject({from:e,to:t}){const r={Bucket:this.config.bucket,Key:this.config.keyPrefix?we.join(this.config.keyPrefix,t):t,CopySource:we.join(this.config.bucket,this.config.keyPrefix?we.join(this.config.keyPrefix,e):e)};let i,s;try{return i=await this.sendCommand(new u.CopyObjectCommand(r)),i}catch(e){throw s=e,L(e,{bucket:this.config.bucket,key:t,commandName:"CopyObjectCommand",commandInput:r})}finally{this.emit("copyObject",s||i,{from:e,to:t})}}async exists(e){const[t,r]=await K(()=>this.headObject(e));if(t)return!0;if("NoSuchKey"===r.name||"NotFound"===r.name)return!1;throw r}async deleteObject(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"";t&&we.join(t,e);const r={Bucket:this.config.bucket,Key:t?we.join(t,e):e};let i,s;try{return i=await this.sendCommand(new u.DeleteObjectCommand(r)),i}catch(t){throw s=t,L(t,{bucket:this.config.bucket,key:e,commandName:"DeleteObjectCommand",commandInput:r})}finally{this.emit("deleteObject",s||i,{key:e})}}async deleteObjects(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"",r=a.chunk(e,1e3),{results:s,errors:n}=await i.PromisePool.for(r).withConcurrency(this.parallelism).process(async e=>{for(const r of e)t&&we.join(t,r),this.config.bucket,await this.exists(r);const r={Bucket:this.config.bucket,Delete:{Objects:e.map(e=>({Key:t?we.join(t,e):e}))}};let i;const[s,n,o]=await K(()=>this.sendCommand(new u.DeleteObjectsCommand(r)));if(!s)throw n;return i=o,i&&i.Errors&&i.Errors.length,i&&i.Deleted&&(i.Deleted.length,e.length),i}),o={deleted:s,notFound:n};return this.emit("deleteObjects",o,e),o}async deleteAll({prefix:e}={}){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"";let r,i=0;do{const s=new u.ListObjectsV2Command({Bucket:this.config.bucket,Prefix:t?we.join(t,e||""):e||"",ContinuationToken:r}),n=await this.client.send(s);if(n.Contents&&n.Contents.length>0){const t=new u.DeleteObjectsCommand({Bucket:this.config.bucket,Delete:{Objects:n.Contents.map(e=>({Key:e.Key}))}}),r=await this.client.send(t),s=r.Deleted?r.Deleted.length:0;i+=s,this.emit("deleteAll",{prefix:e,batch:s,total:i})}r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return this.emit("deleteAllComplete",{prefix:e,totalDeleted:i}),i}async moveObject({from:e,to:t}){const[r,i]=await K(async()=>{await this.copyObject({from:e,to:t}),await this.deleteObject(e)});if(!r)throw new T("Unknown error in moveObject",{bucket:this.config.bucket,from:e,to:t,original:i});return!0}async listObjects({prefix:e,maxKeys:t=1e3,continuationToken:r}={}){const i={Bucket:this.config.bucket,MaxKeys:t,ContinuationToken:r,Prefix:this.config.keyPrefix?we.join(this.config.keyPrefix,e||""):e||""},[s,n,o]=await K(()=>this.sendCommand(new u.ListObjectsV2Command(i)));if(!s)throw new T("Unknown error in listObjects",{prefix:e,bucket:this.config.bucket,original:n});return this.emit("listObjects",o,i),o}async count({prefix:e}={}){let t,r=0,i=!0;for(;i;){const s={prefix:e,continuationToken:t},n=await this.listObjects(s);r+=n.KeyCount||0,i=n.IsTruncated||!1,t=n.NextContinuationToken}return this.emit("count",r,{prefix:e}),r}async getAllKeys({prefix:e}={}){let t,r=[],i=!0;for(;i;){const s={prefix:e,continuationToken:t},n=await this.listObjects(s);n.Contents&&(r=r.concat(n.Contents.map(e=>e.Key))),i=n.IsTruncated||!1,t=n.NextContinuationToken}return this.config.keyPrefix&&(r=r.map(e=>e.replace(this.config.keyPrefix,"")).map(e=>e.startsWith("/")?e.replace("/",""):e)),this.emit("getAllKeys",r,{prefix:e}),r}async getContinuationTokenAfterOffset(e={}){const{prefix:t,offset:r=1e3}=e;if(0===r)return null;let i,s=!0,n=0;for(;s;){const e={prefix:t,maxKeys:r<1e3?r:r-n>1e3?1e3:r-n,continuationToken:i},o=await this.listObjects(e);if(o.Contents&&(n+=o.Contents.length),s=o.IsTruncated||!1,i=o.NextContinuationToken,n>=r)break}return this.emit("getContinuationTokenAfterOffset",i||null,e),i||null}async getKeysPage(e={}){const{prefix:t,offset:r=0,amount:i=100}=e;let s,n=[],o=!0;if(r>0&&(s=await this.getContinuationTokenAfterOffset({prefix:t,offset:r}),!s))return this.emit("getKeysPage",[],e),[];for(;o;){const e={prefix:t,continuationToken:s},r=await this.listObjects(e);if(r.Contents&&(n=n.concat(r.Contents.map(e=>e.Key))),o=r.IsTruncated||!1,s=r.NextContinuationToken,n.length>=i){n=n.slice(0,i);break}}return this.config.keyPrefix&&(n=n.map(e=>e.replace(this.config.keyPrefix,"")).map(e=>e.startsWith("/")?e.replace("/",""):e)),this.emit("getKeysPage",n,e),n}async moveAllObjects({prefixFrom:e,prefixTo:t}){const r=await this.getAllKeys({prefix:e}),{results:s,errors:n}=await i.PromisePool.for(r).withConcurrency(this.parallelism).process(async r=>{const i=r.replace(e,t),[s,n]=await K(async()=>{await this.moveObject({from:r,to:i})});if(!s)throw new T("Unknown error in moveAllObjects",{bucket:this.config.bucket,from:r,to:i,original:n});return i});if(this.emit("moveAllObjects",{results:s,errors:n},{prefixFrom:e,prefixTo:t}),n.length>0)throw new Error("Some objects could not be moved");return s}}var di=fi;async function pi(e,t,r){if(!this.passphrase)return t.push(new E("Missing configuration for secrets encryption.",{actual:e,type:"encryptionKeyMissing",suggestion:"Provide a passphrase for secret encryption."})),e;const[i,s,n]=await K(()=>G(String(e),this.passphrase));return i?n:(t.push(new E("Problem encrypting secret.",{actual:e,type:"encryptionProblem",error:s,suggestion:"Check the passphrase and input value."})),e)}async function mi(e,t,r){if(a.isString(e))return e;const[i,s,n]=V(()=>JSON.stringify(e));if(!i)throw new E("Failed to stringify JSON",{original:s,input:e});return n}class gi extends f{constructor({options:e,passphrase:t,autoEncrypt:r=!0}={}){super(a.merge({},{useNewCustomCheckerFunction:!0,messages:{encryptionKeyMissing:"Missing configuration for secrets encryption.",encryptionProblem:"Problem encrypting secret. Actual: {actual}. Error: {error}"},defaults:{string:{trim:!0},object:{strict:"remove"},number:{convert:!0}}},e)),this.passphrase=t,this.autoEncrypt=r,this.alias("secret",{type:"string",custom:this.autoEncrypt?pi:void 0,messages:{string:"The '{field}' field must be a string.",stringMin:"This secret '{field}' field length must be at least {expected} long."}}),this.alias("secretAny",{type:"any",custom:this.autoEncrypt?pi:void 0}),this.alias("secretNumber",{type:"number",custom:this.autoEncrypt?pi:void 0}),this.alias("json",{type:"any",custom:this.autoEncrypt?mi:void 0})}}const yi=new Proxy(gi,{instance:null,construct(e,t){return this.instance||(this.instance=new e(...t)),this.instance}});const bi={trim:e=>null==e?e:e.trim(),encrypt:async(e,{passphrase:t})=>{if(null==e)return e;const[r,i,s]=await H(()=>G(e,t));return r?s:e},decrypt:async(e,{passphrase:t})=>{if(null==e)return e;const[r,i,s]=await H(()=>Y(e,t));return r?"null"===s?null:"undefined"!==s?s:void 0:e},toString:e=>null==e?e:String(e),fromArray:(e,{separator:t})=>{if(null==e||!Array.isArray(e))return e;if(0===e.length)return"";return e.map(e=>"string"==typeof e?e.replace(/\\/g,"\\\\").replace(new RegExp(`\\${t}`,"g"),`\\${t}`):String(e)).join(t)},toArray:(e,{separator:t})=>{if(Array.isArray(e))return e;if(null==e)return e;if(""===e)return[];const r=[];let i="",s=0;const n=String(e);for(;s<n.length;)"\\"===n[s]&&s+1<n.length?(i+=n[s+1],s+=2):n[s]===t?(r.push(i),i="",s++):(i+=n[s],s++);return r.push(i),r},toJSON:e=>{if(null===e)return null;if(void 0===e)return;if("string"==typeof e){const[t,r,i]=V(()=>JSON.parse(e));return e}const[t,r,i]=V(()=>JSON.stringify(e));return t?i:e},fromJSON:e=>{if(null===e)return null;if(void 0===e)return;if("string"!=typeof e)return e;if(""===e)return"";const[t,r,i]=V(()=>JSON.parse(e));return t?i:e},toNumber:e=>a.isString(e)?e.includes(".")?parseFloat(e):parseInt(e):e,toBool:e=>[!0,1,"true","1","yes","y"].includes(e),fromBool:e=>[!0,1,"true","1","yes","y"].includes(e)?"1":"0",fromBase62:e=>{if(null==e||""===e)return e;if("number"==typeof e)return e;if("string"==typeof e){const t=g(e);return isNaN(t)?void 0:t}},toBase62:e=>{if(null==e||""===e)return e;if("number"==typeof e)return m(e);if("string"==typeof e){const t=Number(e);return isNaN(t)?e:m(t)}return e},fromBase62Decimal:e=>{if(null==e||""===e)return e;if("number"==typeof e)return e;if("string"==typeof e){const t=b(e);return isNaN(t)?void 0:t}},toBase62Decimal:e=>{if(null==e||""===e)return e;if("number"==typeof e)return y(e);if("string"==typeof e){const t=Number(e);return isNaN(t)?e:y(t)}return e},fromArrayOfNumbers:(e,{separator:t})=>{if(null==e||!Array.isArray(e))return e;if(0===e.length)return"";return e.map(e=>{if("number"==typeof e&&!isNaN(e))return m(e);const t=Number(e);return isNaN(t)?"":m(t)}).join(t)},toArrayOfNumbers:(e,{separator:t})=>{if(Array.isArray(e))return e.map(e=>"number"==typeof e?e:g(e));if(null==e)return e;if(""===e)return[];const r=String(e),i=[];let s="",n=0;for(;n<r.length;)"\\"===r[n]&&n+1<r.length?(s+=r[n+1],n+=2):r[n]===t?(i.push(s),s="",n++):(s+=r[n],n++);return i.push(s),i.map(e=>{if("number"==typeof e)return e;if("string"==typeof e&&""!==e){const t=g(e);return isNaN(t)?NaN:t}return NaN})},fromArrayOfDecimals:(e,{separator:t})=>{if(null==e||!Array.isArray(e))return e;if(0===e.length)return"";return e.map(e=>{if("number"==typeof e&&!isNaN(e))return y(e);const t=Number(e);return isNaN(t)?"":y(t)}).join(t)},toArrayOfDecimals:(e,{separator:t})=>{if(Array.isArray(e))return e.map(e=>"number"==typeof e?e:b(e));if(null==e)return e;if(""===e)return[];const r=String(e),i=[];let s="",n=0;for(;n<r.length;)"\\"===r[n]&&n+1<r.length?(s+=r[n+1],n+=2):r[n]===t?(i.push(s),s="",n++):(s+=r[n],n++);return i.push(s),i.map(e=>{if("number"==typeof e)return e;if("string"==typeof e&&""!==e){const t=b(e);return isNaN(t)?NaN:t}return NaN})}};class wi{constructor(e){const{map:t,name:r,attributes:i,passphrase:s,version:n=1,options:o={}}=e;this.name=r,this.version=n,this.attributes=i||{},this.passphrase=s??"secret",this.options=a.merge({},this.defaultOptions(),o),this.allNestedObjectsOptional=this.options.allNestedObjectsOptional??!1;const c=this.preprocessAttributesForValidation(this.attributes);if(this.validator=new yi({autoEncrypt:!1}).compile(a.merge({$$async:!0},c)),this.options.generateAutoHooks&&this.generateAutoHooks(),a.isEmpty(t)){const e=h.flatten(this.attributes,{safe:!0}),t=Object.keys(e).filter(e=>!e.includes("$$")),r=this.extractObjectKeys(this.attributes),i=[...new Set([...t,...r])],{mapping:s,reversedMapping:n}=function(e){const t={},r={};return e.forEach((e,i)=>{const s=m(i);t[e]=s,r[s]=e}),{mapping:t,reversedMapping:r}}(i);this.map=s,this.reversedMap=n}else this.map=t,this.reversedMap=a.invert(t)}defaultOptions(){return{autoEncrypt:!0,autoDecrypt:!0,arraySeparator:"|",generateAutoHooks:!0,hooks:{beforeMap:{},afterMap:{},beforeUnmap:{},afterUnmap:{}}}}addHook(e,t,r){this.options.hooks[e][t]||(this.options.hooks[e][t]=[]),this.options.hooks[e][t]=a.uniq([...this.options.hooks[e][t],r])}extractObjectKeys(e,t=""){const r=[];for(const[i,s]of Object.entries(e)){if(i.startsWith("$$"))continue;const e=t?`${t}.${i}`:i;"object"!=typeof s||null===s||Array.isArray(s)||(r.push(e),"object"===s.$$type&&r.push(...this.extractObjectKeys(s,e)))}return r}generateAutoHooks(){const e=h.flatten(a.cloneDeep(this.attributes),{safe:!0});for(const[t,r]of Object.entries(e))if(r.includes("array")){if(r.includes("items:string"))this.addHook("beforeMap",t,"fromArray"),this.addHook("afterUnmap",t,"toArray");else if(r.includes("items:number")){r.includes("integer:true")||r.includes("|integer:")||r.includes("|integer")?(this.addHook("beforeMap",t,"fromArrayOfNumbers"),this.addHook("afterUnmap",t,"toArrayOfNumbers")):(this.addHook("beforeMap",t,"fromArrayOfDecimals"),this.addHook("afterUnmap",t,"toArrayOfDecimals"))}}else if(r.includes("secret"))this.options.autoEncrypt&&this.addHook("beforeMap",t,"encrypt"),this.options.autoDecrypt&&this.addHook("afterUnmap",t,"decrypt");else{if(r.includes("number")){r.includes("integer:true")||r.includes("|integer:")||r.includes("|integer")?(this.addHook("beforeMap",t,"toBase62"),this.addHook("afterUnmap",t,"fromBase62")):(this.addHook("beforeMap",t,"toBase62Decimal"),this.addHook("afterUnmap",t,"fromBase62Decimal"));continue}r.includes("boolean")?(this.addHook("beforeMap",t,"fromBool"),this.addHook("afterUnmap",t,"toBool")):(r.includes("json")||"object"===r||r.includes("object"))&&(this.addHook("beforeMap",t,"toJSON"),this.addHook("afterUnmap",t,"fromJSON"))}}static import(e){let{map:t,name:r,options:i,version:s,attributes:n}=a.isString(e)?JSON.parse(e):e;const[o,c,l]=V(()=>wi._importAttributes(n));if(!o)throw new B("Failed to import schema attributes",{original:c,input:n});n=l;return new wi({map:t,name:r,options:i,version:s,attributes:n})}static _importAttributes(e){if("string"==typeof e){const[t,r,i]=V(()=>JSON.parse(e));if(t&&"object"==typeof i&&null!==i){const[t,r,s]=V(()=>wi._importAttributes(i));if(!t)throw new B("Failed to parse nested schema attribute",{original:r,input:e});return s}return e}if(Array.isArray(e)){const[t,r,i]=V(()=>e.map(e=>wi._importAttributes(e)));if(!t)throw new B("Failed to import array schema attributes",{original:r,input:e});return i}if("object"==typeof e&&null!==e){const t={};for(const[r,i]of Object.entries(e)){const[e,s,n]=V(()=>wi._importAttributes(i));if(!e)throw new B("Failed to import object schema attribute",{original:s,key:r,input:i});t[r]=n}return t}return e}export(){return{version:this.version,name:this.name,options:this.options,attributes:this._exportAttributes(this.attributes),map:this.map}}_exportAttributes(e){if("string"==typeof e)return e;if(Array.isArray(e))return e.map(e=>this._exportAttributes(e));if("object"==typeof e&&null!==e){const t={};for(const[r,i]of Object.entries(e))t[r]=this._exportAttributes(i);return t}return e}async applyHooksActions(e,t){const r=a.cloneDeep(e);for(const[e,i]of Object.entries(this.options.hooks[t]))for(const t of i){const i=a.get(r,e);void 0!==i&&"function"==typeof bi[t]&&a.set(r,e,await bi[t](i,{passphrase:this.passphrase,separator:this.options.arraySeparator}))}return r}async validate(e,{mutateOriginal:t=!1}={}){let r=t?e:a.cloneDeep(e);return await this.validator(r)}async mapper(e){let t=a.cloneDeep(e);t=await this.applyHooksActions(t,"beforeMap");const r=h.flatten(t,{safe:!0}),i={_v:this.version+""};for(const[e,t]of Object.entries(r)){const r=this.map[e]||e,s=this.getAttributeDefinition(e);"number"==typeof t&&"string"==typeof s&&s.includes("number")?i[r]=m(t):"string"==typeof t?"[object Object]"===t?i[r]="{}":(t.startsWith("{")||t.startsWith("["),i[r]=t):Array.isArray(t)||"object"==typeof t&&null!==t?i[r]=JSON.stringify(t):i[r]=t}return await this.applyHooksActions(i,"afterMap"),i}async unmapper(e,t){let r=a.cloneDeep(e);delete r._v,r=await this.applyHooksActions(r,"beforeUnmap");const i=t?a.invert(t):this.reversedMap,s={};for(const[e,t]of Object.entries(r)){const r=i&&i[e]?i[e]:e;let n=t;const o=this.getAttributeDefinition(r);if("string"!=typeof o||!o.includes("number")||o.includes("array")||o.includes("decimal")){if("string"==typeof t)if("[object Object]"===t)n={};else if(t.startsWith("{")||t.startsWith("[")){const[e,r,i]=V(()=>JSON.parse(t));e&&(n=i)}}else"string"==typeof n&&""!==n?n=g(n):"number"==typeof n||(n=void 0);if(this.attributes&&"string"==typeof o&&o.includes("array"))if(Array.isArray(n));else if("string"==typeof n&&n.trim().startsWith("[")){const[e,t,r]=V(()=>JSON.parse(n));e&&Array.isArray(r)&&(n=r)}else n=bi.toArray(n,{separator:this.options.arraySeparator});if(this.options.hooks&&this.options.hooks.afterUnmap&&this.options.hooks.afterUnmap[r])for(const e of this.options.hooks.afterUnmap[r])"function"==typeof bi[e]&&(n=await bi[e](n,{passphrase:this.passphrase,separator:this.options.arraySeparator}));s[r]=n}await this.applyHooksActions(s,"afterUnmap");const n=h.unflatten(s);for(const[t,r]of Object.entries(e))t.startsWith("$")&&(n[t]=r);return n}getAttributeDefinition(e){const t=e.split(".");let r=this.attributes;for(const e of t){if(!r)return;r=r[e]}return r}preprocessAttributesForValidation(e){const t={};for(const[r,i]of Object.entries(e))if("object"!=typeof i||null===i||Array.isArray(i))t[r]=i;else{const e=i.$$type&&i.$$type.includes("required"),s=i.$$type&&i.$$type.includes("optional"),n={type:"object",properties:this.preprocessAttributesForValidation(i),strict:!1};e||(s||this.allNestedObjectsOptional)&&(n.optional=!0),t[r]=n}return t}}var vi=wi;const ki=2047;var _i=Object.freeze({__proto__:null,S3_METADATA_LIMIT_BYTES:ki,handleGet:async function({resource:e,metadata:t,body:r}){return{metadata:t,body:r}},handleInsert:async function({resource:e,data:t,mappedData:r,originalData:i}){const s=S(r),n=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}});if(s>n)throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${s} bytes, effective limit: ${n} bytes, absolute limit: 2047 bytes`);return{mappedData:r,body:""}},handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){const n=S(i),o=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}});if(n>o)throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${n} bytes, effective limit: ${o} bytes, absolute limit: 2047 bytes`);return{mappedData:i,body:JSON.stringify(i)}},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){const s=S(i),n=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}});if(s>n)throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${s} bytes, effective limit: ${n} bytes, absolute limit: 2047 bytes`);return{mappedData:i,body:""}}});var Si=Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){if(r&&""!==r.trim())try{const e=JSON.parse(r);return{metadata:{...e,...t},body:r}}catch(e){return{metadata:t,body:r}}return{metadata:t,body:r}},handleInsert:async function({resource:e,data:t,mappedData:r,originalData:i}){const s=S(r);return s>R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}})?(e.emit("exceedsLimit",{operation:"insert",totalSize:s,limit:2047,excess:s-2047,data:i||t}),{mappedData:{_v:r._v},body:JSON.stringify(r)}):{mappedData:r,body:""}},handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){const n=S(i);return n>R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}})&&e.emit("exceedsLimit",{operation:"update",id:t,totalSize:n,limit:2047,excess:n-2047,data:s||r}),{mappedData:i,body:JSON.stringify(r)}},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){const n=S(i);return n>R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}})&&e.emit("exceedsLimit",{operation:"upsert",id:t,totalSize:n,limit:2047,excess:n-2047,data:s||r}),{mappedData:i,body:JSON.stringify(r)}}});const Oi="$truncated",Ri="true",ji=w(Oi)+w(Ri);async function xi({resource:e,data:t,mappedData:r,originalData:i}){const s=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}}),n=_(r),o=Object.entries(n).sort(([,e],[,t])=>e-t),a={};let c=0,l=!1;r._v&&(a._v=r._v,c+=n._v);for(const[e,t]of o){if("_v"===e)continue;const i=r[e];if(!(c+(t+(l?0:ji))<=s)){const t=s-c-(l?0:ji);if(t>0){const r=Ei(i,t);a[e]=r,l=!0,c+=w(r)}else a[e]="",l=!0;break}a[e]=i,c+=t}let u=S(a)+(l?ji:0);for(;u>s;){const e=Object.keys(a).filter(e=>"_v"!==e&&"$truncated"!==e);if(0===e.length)break;a[e[e.length-1]]="",u=S(a)+ji,l=!0}return l&&(a[Oi]=Ri),{mappedData:a,body:""}}function Ei(e,t){if("string"==typeof e)return Ai(e,t);if("object"==typeof e&&null!==e){return Ai(JSON.stringify(e),t)}return Ai(String(e),t)}function Ai(e,t){const r=new TextEncoder;let i=r.encode(e);if(i.length<=t)return e;let s=e.length;for(;s>0;){const n=e.substring(0,s);if(i=r.encode(n),i.length<=t)return n;s--}return""}var Di=Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){return{metadata:t,body:r}},handleInsert:xi,handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){return xi({resource:e,data:r,mappedData:i,originalData:s})},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){return xi({resource:e,data:r,mappedData:i})}});const Ci="$overflow",Ni="true",$i=w(Ci)+w(Ni);async function Pi({resource:e,data:t,mappedData:r,originalData:i}){const s=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}}),n=_(r),o=Object.entries(n).sort(([,e],[,t])=>e-t),a={},c={};let l=0,u=!1;r._v&&(a._v=r._v,l+=n._v);let h=s;for(const[e,t]of o)"_v"!==e&&(!u&&l+t>s&&(h-=$i,u=!0),!u&&l+t<=h?(a[e]=r[e],l+=t):(c[e]=r[e],u=!0));u&&(a[Ci]=Ni);return{mappedData:a,body:Object.keys(c).length>0?JSON.stringify(c):""}}async function Ii({resource:e,data:t,mappedData:r}){const i={_v:r._v||String(e.version)};i._map=JSON.stringify(e.schema.map);return{mappedData:i,body:JSON.stringify(r)}}const Ti={"user-managed":Si,"enforce-limits":_i,"truncate-data":Di,"body-overflow":Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){let i={};if(r&&""!==r.trim()){const[e,t,s]=V(()=>JSON.parse(r));i=e?s:{}}const s={...i,...t};return delete s.$overflow,{metadata:s,body:r}},handleInsert:Pi,handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){return Pi({resource:e,data:r,mappedData:i,originalData:s})},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){return Pi({resource:e,data:r,mappedData:i})}}),"body-only":Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){let i={};if(r&&""!==r.trim()){const[e,t,s]=V(()=>JSON.parse(r));i=e?s:{}}return{metadata:{...i,...t},body:r}},handleInsert:Ii,handleUpdate:async function({resource:e,id:t,data:r,mappedData:i}){const s={_v:i._v||String(e.version)};return s._map=JSON.stringify(e.schema.map),{mappedData:s,body:JSON.stringify(i)}},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){return Ii({resource:e,data:r,mappedData:i})}})};function Mi(e){const t=Ti[e];if(!t)throw new Error(`Unknown behavior: ${e}. Available behaviors: ${Object.keys(Ti).join(", ")}`);return t}const Li=Object.keys(Ti),Ui="user-managed";class Fi extends se{constructor(e={}){super(),this._instanceId=X(7);const t=function(e){const t=[];e.name?"string"!=typeof e.name?t.push("Resource 'name' must be a string"):""===e.name.trim()&&t.push("Resource 'name' cannot be empty"):t.push("Resource 'name' is required");e.client||t.push("S3 'client' is required");e.attributes?"object"!=typeof e.attributes||Array.isArray(e.attributes)?t.push("Resource 'attributes' must be an object"):0===Object.keys(e.attributes).length&&t.push("Resource 'attributes' cannot be empty"):t.push("Resource 'attributes' are required");void 0!==e.version&&"string"!=typeof e.version&&t.push("Resource 'version' must be a string");void 0!==e.behavior&&"string"!=typeof e.behavior&&t.push("Resource 'behavior' must be a string");void 0!==e.passphrase&&"string"!=typeof e.passphrase&&t.push("Resource 'passphrase' must be a string");void 0!==e.parallelism&&("number"==typeof e.parallelism&&Number.isInteger(e.parallelism)?e.parallelism<1&&t.push("Resource 'parallelism' must be greater than 0"):t.push("Resource 'parallelism' must be an integer"));void 0===e.observers||Array.isArray(e.observers)||t.push("Resource 'observers' must be an array");const r=["cache","autoDecrypt","timestamps","paranoid","allNestedObjectsOptional"];for(const i of r)void 0!==e[i]&&"boolean"!=typeof e[i]&&t.push(`Resource '${i}' must be a boolean`);void 0!==e.idGenerator&&("function"!=typeof e.idGenerator&&"number"!=typeof e.idGenerator?t.push("Resource 'idGenerator' must be a function or a number (size)"):"number"==typeof e.idGenerator&&e.idGenerator<=0&&t.push("Resource 'idGenerator' size must be greater than 0"));void 0!==e.idSize&&("number"==typeof e.idSize&&Number.isInteger(e.idSize)?e.idSize<=0&&t.push("Resource 'idSize' must be greater than 0"):t.push("Resource 'idSize' must be an integer"));if(void 0!==e.partitions)if("object"!=typeof e.partitions||Array.isArray(e.partitions))t.push("Resource 'partitions' must be an object");else for(const[r,i]of Object.entries(e.partitions))if("object"!=typeof i||Array.isArray(i))t.push(`Partition '${r}' must be an object`);else if(i.fields)if("object"!=typeof i.fields||Array.isArray(i.fields))t.push(`Partition '${r}.fields' must be an object`);else for(const[e,s]of Object.entries(i.fields))"string"!=typeof s&&t.push(`Partition '${r}.fields.${e}' must be a string`);else t.push(`Partition '${r}' must have a 'fields' property`);if(void 0!==e.hooks)if("object"!=typeof e.hooks||Array.isArray(e.hooks))t.push("Resource 'hooks' must be an object");else{const r=["beforeInsert","afterInsert","beforeUpdate","afterUpdate","beforeDelete","afterDelete"];for(const[i,s]of Object.entries(e.hooks))if(r.includes(i))if(Array.isArray(s))for(let e=0;e<s.length;e++){const t=s[e];if("function"==typeof t);else if("string"==typeof t)continue}else t.push(`Resource 'hooks.${i}' must be an array`);else t.push(`Invalid hook event '${i}'. Valid events: ${r.join(", ")}`)}if(void 0!==e.events)if("object"!=typeof e.events||Array.isArray(e.events))t.push("Resource 'events' must be an object");else for(const[r,i]of Object.entries(e.events))if(Array.isArray(i))for(let e=0;e<i.length;e++){"function"!=typeof i[e]&&t.push(`Resource 'events.${r}[${e}]' must be a function`)}else"function"!=typeof i&&t.push(`Resource 'events.${r}' must be a function or array of functions`);return{isValid:0===t.length,errors:t}}(e);if(!t.isValid){const r=t.errors.map(e=>` • ${e}`).join("\n");throw new q(`Invalid Resource ${e.name||"[unnamed]"} configuration:\n${r}`,{resourceName:e.name,validation:t.errors})}const{name:r,client:i,version:s="1",attributes:n={},behavior:o=Ui,passphrase:a="secret",parallelism:c=10,observers:l=[],cache:u=!1,autoDecrypt:h=!0,timestamps:f=!1,partitions:d={},paranoid:p=!0,allNestedObjectsOptional:m=!0,hooks:g={},idGenerator:y,idSize:b=22,versioningEnabled:w=!1,events:v={}}=e;if(this.name=r,this.client=i,this.version=s,this.behavior=o,this.observers=l,this.parallelism=c,this.passphrase=a??"secret",this.versioningEnabled=w,this.idGenerator=this.configureIdGenerator(y,b),this.idSize="number"==typeof y&&y>0?y:"number"==typeof b&&b>0?b:22,this.idGeneratorType=this.getIdGeneratorType(y,this.idSize),this.config={cache:u,hooks:g,paranoid:p,timestamps:f,partitions:d,autoDecrypt:h,allNestedObjectsOptional:m},this.hooks={beforeInsert:[],afterInsert:[],beforeUpdate:[],afterUpdate:[],beforeDelete:[],afterDelete:[]},this.attributes=n||{},this.map=e.map,this.applyConfiguration({map:this.map}),g)for(const[e,t]of Object.entries(g))if(Array.isArray(t)&&this.hooks[e])for(const r of t)"function"==typeof r&&this.hooks[e].push(r.bind(this));if(v&&Object.keys(v).length>0)for(const[e,t]of Object.entries(v))if(Array.isArray(t))for(const r of t)"function"==typeof r&&this.on(e,r);else"function"==typeof t&&this.on(e,t);this._initMiddleware()}configureIdGenerator(e,r){return"function"==typeof e?()=>String(e()):"number"==typeof e&&e>0?t.customAlphabet(t.urlAlphabet,e):"number"==typeof r&&r>0&&22!==r?t.customAlphabet(t.urlAlphabet,r):X}getIdGeneratorType(e,t){return"function"==typeof e?"custom_function":t}get options(){return{timestamps:this.config.timestamps,partitions:this.config.partitions||{},cache:this.config.cache,autoDecrypt:this.config.autoDecrypt,paranoid:this.config.paranoid,allNestedObjectsOptional:this.config.allNestedObjectsOptional}}export(){const e=this.schema.export();return e.behavior=this.behavior,e.timestamps=this.config.timestamps,e.partitions=this.config.partitions||{},e.paranoid=this.config.paranoid,e.allNestedObjectsOptional=this.config.allNestedObjectsOptional,e.autoDecrypt=this.config.autoDecrypt,e.cache=this.config.cache,e.hooks=this.hooks,e.map=this.map,e}applyConfiguration({map:e}={}){this.config.timestamps&&(this.attributes.createdAt||(this.attributes.createdAt="string|optional"),this.attributes.updatedAt||(this.attributes.updatedAt="string|optional"),this.config.partitions||(this.config.partitions={}),this.config.partitions.byCreatedDate||(this.config.partitions.byCreatedDate={fields:{createdAt:"date|maxlength:10"}}),this.config.partitions.byUpdatedDate||(this.config.partitions.byUpdatedDate={fields:{updatedAt:"date|maxlength:10"}})),this.setupPartitionHooks(),this.versioningEnabled&&(this.config.partitions.byVersion||(this.config.partitions.byVersion={fields:{_v:"string"}})),this.schema=new vi({name:this.name,attributes:this.attributes,passphrase:this.passphrase,version:this.version,options:{autoDecrypt:this.config.autoDecrypt,allNestedObjectsOptional:this.config.allNestedObjectsOptional},map:e||this.map}),this.validatePartitions()}updateAttributes(e){const t=this.attributes;return this.attributes=e,this.applyConfiguration({map:this.schema?.map}),{oldAttributes:t,newAttributes:e}}addHook(e,t){this.hooks[e]&&this.hooks[e].push(t.bind(this))}async executeHooks(e,t){if(!this.hooks[e])return t;let r=t;for(const t of this.hooks[e])r=await t(r);return r}setupPartitionHooks(){if(!this.config.partitions)return;const e=this.config.partitions;0!==Object.keys(e).length&&(this.hooks.afterInsert||(this.hooks.afterInsert=[]),this.hooks.afterInsert.push(async e=>(await this.createPartitionReferences(e),e)),this.hooks.afterDelete||(this.hooks.afterDelete=[]),this.hooks.afterDelete.push(async e=>(await this.deletePartitionReferences(e),e)))}async validate(e){const t={original:a.cloneDeep(e),isValid:!1,errors:[]},r=await this.schema.validate(e,{mutateOriginal:!1});return!0===r?t.isValid=!0:t.errors=r,t.data=e,t}validatePartitions(){if(!this.config.partitions)return;const e=this.config.partitions;if(0===Object.keys(e).length)return;const t=Object.keys(this.attributes||{});for(const[r,i]of Object.entries(e))if(i.fields)for(const e of Object.keys(i.fields))if(!this.fieldExistsInAttributes(e))throw new z(`Partition '${r}' uses field '${e}' which does not exist in resource attributes. Available fields: ${t.join(", ")}.`,{resourceName:this.name,partitionName:r,fieldName:e,availableFields:t,operation:"validatePartitions"})}fieldExistsInAttributes(e){if(e.startsWith("_"))return!0;if(!e.includes("."))return Object.keys(this.attributes||{}).includes(e);const t=e.split(".");let r=this.attributes||{};for(const e of t){if(!r||"object"!=typeof r||!(e in r))return!1;r=r[e]}return!0}applyPartitionRule(e,t){if(null==e)return e;let r=e;if("string"==typeof t&&t.includes("maxlength:")){const e=t.match(/maxlength:(\d+)/);if(e){const t=parseInt(e[1]);"string"==typeof r&&r.length>t&&(r=r.substring(0,t))}}if(t.includes("date"))if(r instanceof Date)r=r.toISOString().split("T")[0];else if("string"==typeof r)if(r.includes("T")&&r.includes("Z"))r=r.split("T")[0];else{const e=new Date(r);isNaN(e.getTime())||(r=e.toISOString().split("T")[0])}return r}getResourceKey(e){return be("resource="+this.name,"data",`id=${e}`)}getPartitionKey({partitionName:e,id:t,data:r}){if(!this.config.partitions||!this.config.partitions[e])throw new z(`Partition '${e}' not found`,{resourceName:this.name,partitionName:e,operation:"getPartitionKey"});const i=this.config.partitions[e],s=[],n=Object.entries(i.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of n){const i=this.getNestedFieldValue(r,e),n=this.applyPartitionRule(i,t);if(null==n)return null;s.push(`${e}=${n}`)}if(0===s.length)return null;const o=t||r?.id;return o?be(`resource=${this.name}`,`partition=${e}`,...s,`id=${o}`):null}getNestedFieldValue(e,t){if(!t.includes("."))return e[t];const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return;i=i[e]}return i}calculateContentLength(e){return e?Buffer.isBuffer(e)?e.length:"string"==typeof e?Buffer.byteLength(e,"utf8"):"object"==typeof e?Buffer.byteLength(JSON.stringify(e),"utf8"):Buffer.byteLength(String(e),"utf8"):0}async insert({id:e,...t}){if(await this.exists(e))throw new Error(`Resource with id '${e}' already exists`);this.getResourceKey(e||"(auto)"),this.options.timestamps&&(t.createdAt=(new Date).toISOString(),t.updatedAt=(new Date).toISOString());const r={id:e,...this.applyDefaults(t)},i=await this.executeHooks("beforeInsert",r),s=Object.keys(i).filter(e=>!(e in r)||i[e]!==r[e]),n={};for(const e of s)n[e]=i[e];const{errors:o,isValid:a,data:c}=await this.validate(i);if(!a){const e=o&&o.length&&o[0].message?o[0].message:"Insert failed";throw new I({bucket:this.client.config.bucket,resourceName:this.name,attributes:i,validation:o,message:e})}const{id:l,...u}=c;Object.assign(u,n);let h=l||e;if(!h&&(h=this.idGenerator(),!h||""===h.trim())){const{idGenerator:e}=await Promise.resolve().then(function(){return re});h=e()}const f=await this.schema.mapper(u);f._v=String(this.version);const d=Mi(this.behavior),{mappedData:p,body:m}=await d.handleInsert({resource:this,data:u,mappedData:f,originalData:r}),g=p,y=this.getResourceKey(h);let b;if(m&&""!==m){const[e,t]=await K(()=>Promise.resolve(JSON.parse(m)));e&&(b="application/json")}if("body-only"===this.behavior&&(!m||""===m))throw new Error(`[Resource.insert] Attempt to save object without body! Data: id=${h}, resource=${this.name}`);const[w,v,k]=await K(()=>this.client.putObject({key:y,body:m,contentType:b,metadata:g}));if(!w){const e=v&&v.message?v.message:"";if(e.includes("metadata headers exceed")||e.includes("Insert failed")){const e=S(g),t=R({s3Limit:2047,systemConfig:{version:this.version,timestamps:this.config.timestamps,id:h}}),r=e-t;throw v.totalSize=e,v.limit=2047,v.effectiveLimit=t,v.excess=r,new q("metadata headers exceed",{resourceName:this.name,operation:"insert",id:h,totalSize:e,effectiveLimit:t,excess:r,suggestion:"Reduce metadata size or number of fields."})}throw v}const _=await this.get(h),O=await this.executeHooks("afterInsert",_);return this.emit("insert",O),O}async get(e){if(a.isObject(e))throw new Error("id cannot be an object");if(a.isEmpty(e))throw new Error("id cannot be empty");const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.getObject(t));if(!r)throw L(i,{bucket:this.client.config.bucket,key:t,resourceName:this.name,operation:"get",id:e});const n=s.Metadata?._v||this.version,o="string"==typeof n&&n.startsWith("v")?n.slice(1):n,c=await this.getSchemaForVersion(o);let l=await c.unmapper(s.Metadata);const u=Mi(this.behavior);let h="";if(s.ContentLength>0){const[e,r,i]=await K(()=>this.client.getObject(t));h=e?await Xr(i.Body):""}const{metadata:f}=await u.handleGet({resource:this,metadata:l,body:h});let d=await this.composeFullObjectFromWrite({id:e,metadata:f,body:h,behavior:this.behavior});d._contentLength=s.ContentLength,d._lastModified=s.LastModified,d._hasContent=s.ContentLength>0,d._mimeType=s.ContentType||null,d._v=o,s.VersionId&&(d._versionId=s.VersionId),s.Expiration&&(d._expiresAt=s.Expiration),d._definitionHash=this.getDefinitionHash(),o!==this.version&&(d=await this.applyVersionMapping(d,o,this.version)),this.emit("get",d);return d}async exists(e){const t=this.getResourceKey(e),[r,i]=await K(()=>this.client.headObject(t));return r}async update(e,t){if(a.isEmpty(e))throw new Error("id cannot be empty");if(!await this.exists(e))throw new Error(`Resource with id '${e}' does not exist`);const r=await this.get(e),i=a.cloneDeep(t);let s=a.cloneDeep(r);for(const[e,t]of Object.entries(i))if(e.includes(".")){let r=s;const i=e.split(".");for(let e=0;e<i.length-1;e++)"object"==typeof r[i[e]]&&null!==r[i[e]]||(r[i[e]]={}),r=r[i[e]];r[i[i.length-1]]=a.cloneDeep(t)}else"object"!=typeof t||null===t||Array.isArray(t)?s[e]=a.cloneDeep(t):s[e]=a.merge({},s[e],t);if(this.config.timestamps){const e=(new Date).toISOString();s.updatedAt=e,s.metadata||(s.metadata={}),s.metadata.updatedAt=e}const n=await this.executeHooks("beforeUpdate",a.cloneDeep(s)),o={...r,...n,id:e},{isValid:c,errors:l,data:u}=await this.validate(a.cloneDeep(o));if(!c)throw new I({bucket:this.client.config.bucket,resourceName:this.name,attributes:n,validation:l,message:"validation: "+(l&&l.length?JSON.stringify(l):"unknown")});await this.schema.mapper(u);const h=Mi(this.behavior),f=await this.schema.mapper({...r,...n});f._v=String(this.version),await h.handleUpdate({resource:this,id:e,data:{...r,...n},mappedData:f,originalData:{...i,id:e}});const{id:d,...p}=u,m={...r,id:e},g={...p,id:e};await this.handlePartitionReferenceUpdates(m,g);const y=await this.schema.mapper(p);y._v=String(this.version);const b=Mi(this.behavior),{mappedData:w,body:v}=await b.handleUpdate({resource:this,id:e,data:p,mappedData:y,originalData:{...i,id:e}}),k=w,_=this.getResourceKey(e);let O,j=v;if(""===v&&"body-overflow"!==this.behavior){const[e,t,r]=await K(()=>this.client.getObject(_));if(e&&r.ContentLength>0){const e=Buffer.from(await r.Body.transformToByteArray()),t=e.toString(),[i,s]=await K(()=>Promise.resolve(JSON.parse(t)));i||(j=e,O=r.ContentType)}}let x=O;if(j&&""!==j&&!x){const[e,t]=await K(()=>Promise.resolve(JSON.parse(j)));e&&(x="application/json")}this.versioningEnabled&&r._v!==this.version&&await this.createHistoricalVersion(e,r);const[E,A]=await K(()=>this.client.putObject({key:_,body:j,contentType:x,metadata:k}));if(!E&&A&&A.message&&A.message.includes("metadata headers exceed")){const t=S(k),r=R({s3Limit:2047,systemConfig:{version:this.version,timestamps:this.config.timestamps,id:e}}),i=t-r;throw A.totalSize=t,A.limit=2047,A.effectiveLimit=r,A.excess=i,this.emit("exceedsLimit",{operation:"update",totalSize:t,limit:2047,effectiveLimit:r,excess:i,data:p}),new q("metadata headers exceed",{resourceName:this.name,operation:"update",id:e,totalSize:t,effectiveLimit:r,excess:i,suggestion:"Reduce metadata size or number of fields."})}if(!E)throw L(A,{bucket:this.client.config.bucket,key:_,resourceName:this.name,operation:"update",id:e});const D=await this.composeFullObjectFromWrite({id:e,metadata:k,body:j,behavior:this.behavior}),C=await this.executeHooks("afterUpdate",D);return this.emit("update",{...D,$before:{...r},$after:{...C}}),C}async delete(e){if(a.isEmpty(e))throw new Error("id cannot be empty");let t,r=null;const[i,s,n]=await K(()=>this.get(e));i?t=n:(t={id:e},r=s),await this.executeHooks("beforeDelete",t);const o=this.getResourceKey(e),[c,l,u]=await K(()=>this.client.deleteObject(o));if(this.emit("delete",{...t,$before:{...t},$after:null}),r)throw L(r,{bucket:this.client.config.bucket,key:o,resourceName:this.name,operation:"delete",id:e});if(!c)throw L(l,{key:o,resourceName:this.name,operation:"delete",id:e});return await this.executeHooks("afterDelete",t),u}async upsert({id:e,...t}){return await this.exists(e)?this.update(e,t):this.insert({id:e,...t})}async count({partition:e=null,partitionValues:t={}}={}){let r;if(e&&Object.keys(t).length>0){const i=this.config.partitions[e];if(!i)throw new z(`Partition '${e}' not found`,{resourceName:this.name,partitionName:e,operation:"count"});const s=[],n=Object.entries(i.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,r]of n){const i=t[e];if(null!=i){const t=this.applyPartitionRule(i,r);s.push(`${e}=${t}`)}}r=s.length>0?`resource=${this.name}/partition=${e}/${s.join("/")}`:`resource=${this.name}/partition=${e}`}else r=`resource=${this.name}/data`;const i=await this.client.count({prefix:r});return this.emit("count",i),i}async insertMany(e){const{results:t}=await i.PromisePool.for(e).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,t),this.observers.map(r=>r.emit("error",this.name,e,t))}).process(async e=>await this.insert(e));return this.emit("insertMany",e.length),t}async deleteMany(e){const t=a.chunk(e.map(e=>this.getResourceKey(e)),1e3);e.map(e=>this.getResourceKey(e));const{results:r}=await i.PromisePool.for(t).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,t),this.observers.map(r=>r.emit("error",this.name,e,t))}).process(async e=>{const t=await this.client.deleteObjects(e);return e.forEach(e=>{const t=e.split("/").find(e=>e.startsWith("id=")),r=t?t.replace("id=",""):null;r&&(this.emit("deleted",r),this.observers.map(e=>e.emit("deleted",this.name,r)))}),t});return this.emit("deleteMany",e.length),r}async deleteAll(){if(!1!==this.config.paranoid)throw new q("deleteAll() is a dangerous operation and requires paranoid: false option.",{resourceName:this.name,operation:"deleteAll",paranoid:this.config.paranoid,suggestion:"Set paranoid: false to allow deleteAll."});const e=`resource=${this.name}/data`,t=await this.client.deleteAll({prefix:e});return this.emit("deleteAll",{version:this.version,prefix:e,deletedCount:t}),{deletedCount:t,version:this.version}}async deleteAllData(){if(!1!==this.config.paranoid)throw new q("deleteAllData() is a dangerous operation and requires paranoid: false option.",{resourceName:this.name,operation:"deleteAllData",paranoid:this.config.paranoid,suggestion:"Set paranoid: false to allow deleteAllData."});const e=`resource=${this.name}`,t=await this.client.deleteAll({prefix:e});return this.emit("deleteAllData",{resource:this.name,prefix:e,deletedCount:t}),{deletedCount:t,resource:this.name}}async listIds({partition:e=null,partitionValues:t={},limit:r,offset:i=0}={}){let s;if(e&&Object.keys(t).length>0){if(!this.config.partitions||!this.config.partitions[e])throw new z(`Partition '${e}' not found`,{resourceName:this.name,partitionName:e,operation:"listIds"});const r=this.config.partitions[e],i=[],n=Object.entries(r.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,r]of n){const s=t[e];if(null!=s){const t=this.applyPartitionRule(s,r);i.push(`${e}=${t}`)}}s=i.length>0?`resource=${this.name}/partition=${e}/${i.join("/")}`:`resource=${this.name}/partition=${e}`}else s=`resource=${this.name}/data`;const n=(await this.client.getKeysPage({prefix:s,offset:i,amount:r||1e3})).map(e=>{const t=e.split("/").find(e=>e.startsWith("id="));return t?t.replace("id=",""):null}).filter(Boolean);return this.emit("listIds",n.length),n}async list({partition:e=null,partitionValues:t={},limit:r,offset:i=0}={}){const[s,n,o]=await K(async()=>e?await this.listPartition({partition:e,partitionValues:t,limit:r,offset:i}):await this.listMain({limit:r,offset:i}));return s?o:this.handleListError(n,{partition:e,partitionValues:t})}async listMain({limit:e,offset:t=0}){const[r,i,s]=await K(()=>this.listIds({limit:e,offset:t}));if(!r)throw i;const n=await this.processListResults(s,"main");return this.emit("list",{count:n.length,errors:0}),n}async listPartition({partition:e,partitionValues:t,limit:r,offset:i=0}){if(!this.config.partitions?.[e])return this.emit("list",{partition:e,partitionValues:t,count:0,errors:0}),[];const s=this.config.partitions[e],n=this.buildPartitionPrefix(e,s,t),[o,a,c]=await K(()=>this.client.getAllKeys({prefix:n}));if(!o)throw a;const l=this.extractIdsFromKeys(c).slice(i),u=r?l.slice(0,r):l,h=await this.processPartitionResults(u,e,s,c);return this.emit("list",{partition:e,partitionValues:t,count:h.length,errors:0}),h}buildPartitionPrefix(e,t,r){const i=[],s=Object.entries(t.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of s){const s=r[e];if(null!=s){const r=this.applyPartitionRule(s,t);i.push(`${e}=${r}`)}}return i.length>0?`resource=${this.name}/partition=${e}/${i.join("/")}`:`resource=${this.name}/partition=${e}`}extractIdsFromKeys(e){return e.map(e=>{const t=e.split("/").find(e=>e.startsWith("id="));return t?t.replace("id=",""):null}).filter(Boolean)}async processListResults(e,t="main"){const{results:r,errors:s}=await i.PromisePool.for(e).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,content),this.observers.map(t=>t.emit("error",this.name,e,content))}).process(async e=>{const[r,i,s]=await K(()=>this.get(e));return r?s:this.handleResourceError(i,e,t)});return this.emit("list",{count:r.length,errors:0}),r}async processPartitionResults(e,t,r,s){const n=Object.entries(r.fields).sort(([e],[t])=>e.localeCompare(t)),{results:o,errors:a}=await i.PromisePool.for(e).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,content),this.observers.map(t=>t.emit("error",this.name,e,content))}).process(async e=>{const[r,i,o]=await K(async()=>{const r=this.extractPartitionValuesFromKey(e,s,n);return await this.getFromPartition({id:e,partitionName:t,partitionValues:r})});return r?o:this.handleResourceError(i,e,"partition")});return o.filter(e=>null!==e)}extractPartitionValuesFromKey(e,t,r){const i=t.find(t=>t.includes(`id=${e}`));if(!i)throw new z(`Partition key not found for ID ${e}`,{resourceName:this.name,id:e,operation:"extractPartitionValuesFromKey"});const s=i.split("/"),n={};for(const[e]of r){const t=s.find(t=>t.startsWith(`${e}=`));if(t){const r=t.replace(`${e}=`,"");n[e]=r}}return n}handleResourceError(e,t,r){if(e.message.includes("Cipher job failed")||e.message.includes("OperationError"))return{id:t,_decryptionFailed:!0,_error:e.message,..."partition"===r&&{_partition:r}};throw e}handleListError(e,{partition:t,partitionValues:r}){return e.message.includes("Partition '")&&e.message.includes("' not found"),this.emit("list",{partition:t,partitionValues:r,count:0,errors:1}),[]}async getMany(e){const{results:t,errors:r}=await i.PromisePool.for(e).withConcurrency(this.client.parallelism).handleError(async(e,t)=>(this.emit("error",e,content),this.observers.map(t=>t.emit("error",this.name,e,content)),{id:t,_error:e.message,_decryptionFailed:e.message.includes("Cipher job failed")||e.message.includes("OperationError")})).process(async e=>{const[t,r,i]=await K(()=>this.get(e));if(t)return i;if(r.message.includes("Cipher job failed")||r.message.includes("OperationError"))return{id:e,_decryptionFailed:!0,_error:r.message};throw r});return this.emit("getMany",e.length),t}async getAll(){const[e,t,r]=await K(()=>this.listIds());if(!e)throw t;const i=[];for(const e of r){const[t,r,s]=await K(()=>this.get(e));t&&i.push(s)}return i}async page({offset:e=0,size:t=100,partition:r=null,partitionValues:i={},skipCount:s=!1}={}){const[n,o,a]=await K(async()=>{let n=null,o=null;if(!s){const[e,s,a]=await K(()=>this.count({partition:r,partitionValues:i}));e?(n=a,o=Math.ceil(n/t)):(n=null,o=null)}const a=Math.floor(e/t);let c=[];if(t<=0)c=[];else{const[s,n,o]=await K(()=>this.list({partition:r,partitionValues:i,limit:t,offset:e}));c=s?o:[]}const l={items:c,totalItems:n,page:a,pageSize:t,totalPages:o,hasMore:c.length===t&&e+t<(n||1/0),_debug:{requestedSize:t,requestedOffset:e,actualItemsReturned:c.length,skipCount:s,hasTotalItems:null!==n}};return this.emit("page",l),l});return n?a:{items:[],totalItems:null,page:Math.floor(e/t),pageSize:t,totalPages:null,_debug:{requestedSize:t,requestedOffset:e,actualItemsReturned:0,skipCount:s,hasTotalItems:!1,error:o.message}}}readable(){return new Qr({resource:this}).build()}writable(){return new Zr({resource:this}).build()}async setContent({id:e,buffer:t,contentType:r="application/octet-stream"}){const[i,s,n]=await K(()=>this.get(e));if(!i||!n)throw new q(`Resource with id '${e}' not found`,{resourceName:this.name,id:e,operation:"setContent"});const o={...n,_hasContent:!0,_contentLength:t.length,_mimeType:r},a=await this.schema.mapper(o),[c,l]=await K(()=>this.client.putObject({key:this.getResourceKey(e),metadata:a,body:t,contentType:r}));if(!c)throw l;return this.emit("setContent",{id:e,contentType:r,contentLength:t.length}),o}async content(e){const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.getObject(t));if(!r){if("NoSuchKey"===i.name)return{buffer:null,contentType:null};throw i}const n=Buffer.from(await s.Body.transformToByteArray()),o=s.ContentType||null;return this.emit("content",e,n.length,o),{buffer:n,contentType:o}}async hasContent(e){const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.headObject(t));return!!r&&s.ContentLength>0}async deleteContent(e){const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.headObject(t));if(!r)throw i;const n=s.Metadata||{},[o,a,c]=await K(()=>this.client.putObject({key:t,body:"",metadata:n}));if(!o)throw a;return this.emit("deleteContent",e),c}getDefinitionHash(){const e={attributes:this.attributes,behavior:this.behavior},t=c(e);return`sha256:${o.createHash("sha256").update(t).digest("hex")}`}extractVersionFromKey(e){const t=e.split("/").find(e=>e.startsWith("v="));return t?t.replace("v=",""):null}async getSchemaForVersion(e){if(e===this.version)return this.schema;const[t,r,i]=await K(()=>Promise.resolve(new vi({name:this.name,attributes:this.attributes,passphrase:this.passphrase,version:e,options:{...this.config,autoDecrypt:!0,autoEncrypt:!0}})));return t?i:this.schema}async createPartitionReferences(e){const t=this.config.partitions;if(t&&0!==Object.keys(t).length)for(const[r,i]of Object.entries(t)){const t=this.getPartitionKey({partitionName:r,id:e.id,data:e});if(t){const e={_v:String(this.version)};await this.client.putObject({key:t,metadata:e,body:"",contentType:void 0})}}}async deletePartitionReferences(e){const t=this.config.partitions;if(!t||0===Object.keys(t).length)return;const r=[];for(const[i,s]of Object.entries(t)){const t=this.getPartitionKey({partitionName:i,id:e.id,data:e});t&&r.push(t)}if(r.length>0){const[e,t]=await K(()=>this.client.deleteObjects(r))}}async query(e={},{limit:t=100,offset:r=0,partition:i=null,partitionValues:s={}}={}){if(0===Object.keys(e).length)return await this.list({partition:i,partitionValues:s,limit:t,offset:r});const n=[];let o=r;const a=Math.min(t,50);for(;n.length<t;){const t=await this.list({partition:i,partitionValues:s,limit:a,offset:o});if(0===t.length)break;const r=t.filter(t=>Object.entries(e).every(([e,r])=>t[e]===r));if(n.push(...r),o+=a,t.length<a)break}return n.slice(0,t)}async handlePartitionReferenceUpdates(e,t){const r=this.config.partitions;if(!r||0===Object.keys(r).length)return;for(const[i,s]of Object.entries(r)){const[r,n]=await K(()=>this.handlePartitionReferenceUpdate(i,s,e,t))}const i=t.id||e.id;for(const[e,s]of Object.entries(r)){const r=`resource=${this.name}/partition=${e}`;let s=[];const[n,o,a]=await K(()=>this.client.getAllKeys({prefix:r}));if(!n)continue;s=a;const c=this.getPartitionKey({partitionName:e,id:i,data:t});for(const e of s)if(e.endsWith(`/id=${i}`)&&e!==c){const[t,r]=await K(()=>this.client.deleteObject(e))}}}async handlePartitionReferenceUpdate(e,t,r,i){const s=i.id||r.id,n=this.getPartitionKey({partitionName:e,id:s,data:r}),o=this.getPartitionKey({partitionName:e,id:s,data:i});if(n!==o){if(n){const[e,t]=await K(async()=>{await this.client.deleteObject(n)})}if(o){const[e,t]=await K(async()=>{const e={_v:String(this.version)};await this.client.putObject({key:o,metadata:e,body:"",contentType:void 0})})}}else if(o){const[e,t]=await K(async()=>{const e={_v:String(this.version)};await this.client.putObject({key:o,metadata:e,body:"",contentType:void 0})})}}async updatePartitionReferences(e){const t=this.config.partitions;if(t&&0!==Object.keys(t).length)for(const[r,i]of Object.entries(t)){if(!i||!i.fields||"object"!=typeof i.fields)continue;const t=this.getPartitionKey({partitionName:r,id:e.id,data:e});if(t){const e={_v:String(this.version)},[r,i]=await K(async()=>{await this.client.putObject({key:t,metadata:e,body:"",contentType:void 0})})}}}async getFromPartition({id:e,partitionName:t,partitionValues:r={}}){if(!this.config.partitions||!this.config.partitions[t])throw new z(`Partition '${t}' not found`,{resourceName:this.name,partitionName:t,operation:"getFromPartition"});const i=this.config.partitions[t],s=[],n=Object.entries(i.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of n){const i=r[e];if(null!=i){const r=this.applyPartitionRule(i,t);s.push(`${e}=${r}`)}}if(0===s.length)throw new z(`No partition values provided for partition '${t}'`,{resourceName:this.name,partitionName:t,operation:"getFromPartition"});const o=be(`resource=${this.name}`,`partition=${t}`,...s,`id=${e}`),[a,c]=await K(async()=>{await this.client.headObject(o)});if(!a)throw new q(`Resource with id '${e}' not found in partition '${t}'`,{resourceName:this.name,id:e,partitionName:t,operation:"getFromPartition"});const l=await this.get(e);return l._partition=t,l._partitionValues=r,this.emit("getFromPartition",l),l}async createHistoricalVersion(e,t){const r=be(`resource=${this.name}`,"historical",`id=${e}`),i={...t,_v:t._v||this.version,_historicalTimestamp:(new Date).toISOString()},s=await this.schema.mapper(i),n=Mi(this.behavior),{mappedData:o,body:a}=await n.handleInsert({resource:this,data:i,mappedData:s}),c={...o,_v:t._v||this.version,_historicalTimestamp:i._historicalTimestamp};let l;if(a&&""!==a){const[e,t]=await K(()=>Promise.resolve(JSON.parse(a)));e&&(l="application/json")}await this.client.putObject({key:r,metadata:c,body:a,contentType:l})}async applyVersionMapping(e,t,r){if(t===r)return e;return{...e,_v:r,_originalVersion:t,_versionMapped:!0}}async composeFullObjectFromWrite({id:e,metadata:t,body:r,behavior:i}){const s={};t&&"true"===t.$truncated&&(s.$truncated="true"),t&&"true"===t.$overflow&&(s.$overflow="true");let n={};const[o,a,c]=await K(()=>this.schema.unmapper(t));n=o?c:t;const l=e=>{if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))r.startsWith("_")||(t[r]=i);return t},u=e=>{if("object"==typeof e&&null!==e)return e;if("string"==typeof e){if("[object Object]"===e)return{};if(e.startsWith("{")||e.startsWith("[")){const[t,r,i]=V(()=>JSON.parse(e));return t?i:e}return e}return e};if("body-overflow"===i){const i=t&&"true"===t.$overflow;let s={};if(i&&r){const[e,t,i]=await K(()=>Promise.resolve(JSON.parse(r)));if(e){const[e,t,r]=await K(()=>this.schema.unmapper(i));s=e?r:{}}}const o={...n,...s,id:e};Object.keys(o).forEach(e=>{o[e]=u(o[e])});const a=l(o);return i&&(a.$overflow="true"),a}if("body-only"===i){const[i,s,n]=await K(()=>Promise.resolve(r?JSON.parse(r):{}));let o=this.schema.map;if(t&&t._map){const[e,r,i]=await K(()=>Promise.resolve("string"==typeof t._map?JSON.parse(t._map):t._map));o=e?i:this.schema.map}const[a,c,l]=await K(()=>this.schema.unmapper(n,o)),h=a?{...l,id:e}:{id:e};return Object.keys(h).forEach(e=>{h[e]=u(h[e])}),h}if("user-managed"===i&&r&&""!==r.trim()){const[t,i,s]=await K(()=>Promise.resolve(JSON.parse(r)));if(t){const[t,r,i]=await K(()=>this.schema.unmapper(s)),o={...t?i:{},...n,id:e};return Object.keys(o).forEach(e=>{o[e]=u(o[e])}),l(o)}}const h={...n,id:e};Object.keys(h).forEach(e=>{h[e]=u(h[e])});const f=l(h);return s.$truncated&&(f.$truncated=s.$truncated),s.$overflow&&(f.$overflow=s.$overflow),f}emit(e,...t){return super.emit(e,...t)}async replace(e,t){await this.delete(e),await new Promise(e=>setTimeout(e,100));const r=Date.now();for(;Date.now()-r<5e3;){if(!await this.exists(e))break;await new Promise(e=>setTimeout(e,50))}try{return await this.insert({...t,id:e})}catch(r){if(r&&r.message&&r.message.includes("already exists")){return await this.update(e,t)}throw r}}_initMiddleware(){this._middlewares=new Map,this._middlewareMethods=["get","list","listIds","getAll","count","page","insert","update","delete","deleteMany","exists","getMany","content","hasContent","query","getFromPartition","setContent","deleteContent","replace"];for(const e of this._middlewareMethods)this._middlewares.set(e,[]),this[`_original_${e}`]||(this[`_original_${e}`]=this[e].bind(this),this[e]=async(...t)=>{const r={resource:this,args:t,method:e};let i=-1;const s=this._middlewares.get(e),n=async t=>{if(t<=i)throw new Error("next() called multiple times");return i=t,t<s.length?await s[t](r,()=>n(t+1)):await this[`_original_${e}`](...r.args)};return await n(0)})}useMiddleware(e,t){if(this._middlewares||this._initMiddleware(),!this._middlewares.has(e))throw new q(`No such method for middleware: ${e}`,{operation:"useMiddleware",method:e});this._middlewares.get(e).push(t)}applyDefaults(e){const t={...e};for(const[e,r]of Object.entries(this.attributes))if(void 0===t[e]&&"string"==typeof r&&r.includes("default:")){const i=r.match(/default:([^|]+)/);if(i){let s=i[1];r.includes("boolean")?s="true"===s:r.includes("number")&&(s=Number(s)),t[e]=s}}return t}}var Bi=Fi;class qi extends se{constructor(e){super(),this.id=X(7),this.version="1",this.s3dbVersion=(()=>{const[e,t,r]=K(()=>"8.1.3");return e?r:"latest"})(),this.resources={},this.savedMetadata=null,this.options=e,this.verbose=e.verbose||!1,this.parallelism=parseInt(e.parallelism+"")||10,this.plugins={},this.pluginList=e.plugins||[],this.cache=e.cache,this.passphrase=e.passphrase||"secret",this.versioningEnabled=e.versioningEnabled||!1,this.persistHooks=e.persistHooks||!1,this._initHooks();let t=e.connectionString;if(!t&&(e.bucket||e.accessKeyId||e.secretAccessKey)){const{bucket:r,region:i,accessKeyId:s,secretAccessKey:n,endpoint:o,forcePathStyle:a}=e;if(o){const e=new URL(o);s&&(e.username=encodeURIComponent(s)),n&&(e.password=encodeURIComponent(n)),e.pathname=`/${r||"s3db"}`,a&&e.searchParams.set("forcePathStyle","true"),t=e.toString()}else if(s&&n){const e=new URLSearchParams;e.set("region",i||"us-east-1"),a&&e.set("forcePathStyle","true"),t=`s3://${encodeURIComponent(s)}:${encodeURIComponent(n)}@${r||"s3db"}?${e.toString()}`}}this.client=e.client||new di({verbose:this.verbose,parallelism:this.parallelism,connectionString:t}),this.bucket=this.client.bucket,this.keyPrefix=this.client.keyPrefix,this._exitListenerRegistered||(this._exitListenerRegistered=!0,"undefined"!=typeof process&&process.on("exit",async()=>{if(this.isConnected())try{await this.disconnect()}catch(e){}}))}async connect(){await this.startPlugins();let e=null,t=!1,r=[];if(await this.client.exists("s3db.json"))try{const i=await this.client.getObject("s3db.json"),s=await Xr(i?.Body);try{e=JSON.parse(s)}catch(i){r.push("JSON parsing failed - attempting recovery"),t=!0,e=await this._attemptJsonRecovery(s,r),e||(await this._createCorruptedBackup(s),r.push("Created backup of corrupted file - starting with blank metadata"),e=this.blankMetadataStructure())}const n=await this._validateAndHealMetadata(e,r);n!==e&&(e=n,t=!0)}catch(i){r.push(`Critical error reading s3db.json: ${i.message}`),await this._createCorruptedBackup(),e=this.blankMetadataStructure(),t=!0}else e=this.blankMetadataStructure(),await this.uploadMetadataFile();t&&await this._uploadHealedMetadata(e,r),this.savedMetadata=e;const i=this.detectDefinitionChanges(e);for(const[t,r]of Object.entries(e.resources||{})){const e=r.currentVersion||"v0",i=r.versions?.[e];if(i){let s,n;void 0!==i.idGenerator?"custom_function"===i.idGenerator?(s=void 0,n=i.idSize||22):"number"==typeof i.idGenerator&&(s=i.idGenerator,n=i.idSize||i.idGenerator):n=i.idSize||22,this.resources[t]=new Bi({name:t,client:this.client,database:this,version:e,attributes:i.attributes,behavior:i.behavior||"user-managed",parallelism:this.parallelism,passphrase:this.passphrase,observers:[this],cache:this.cache,timestamps:void 0!==i.timestamps&&i.timestamps,partitions:r.partitions||i.partitions||{},paranoid:void 0===i.paranoid||i.paranoid,allNestedObjectsOptional:void 0===i.allNestedObjectsOptional||i.allNestedObjectsOptional,autoDecrypt:void 0===i.autoDecrypt||i.autoDecrypt,hooks:this.persistHooks?this._deserializeHooks(i.hooks||{}):i.hooks||{},versioningEnabled:this.versioningEnabled,map:i.map,idGenerator:s,idSize:n})}}i.length>0&&this.emit("resourceDefinitionsChanged",{changes:i,metadata:this.savedMetadata}),this.emit("connected",new Date)}detectDefinitionChanges(e){const t=[];for(const[r,i]of Object.entries(this.resources)){const s=this.generateDefinitionHash(i.export()),n=e.resources?.[r];if(n){const e=n.currentVersion||"v0",i=n.versions?.[e],o=i?.hash;o!==s&&t.push({type:"changed",resourceName:r,currentHash:s,savedHash:o,fromVersion:e,toVersion:this.getNextVersion(n.versions)})}else t.push({type:"new",resourceName:r,currentHash:s,savedHash:null})}for(const[r,i]of Object.entries(e.resources||{}))if(!this.resources[r]){const e=i.currentVersion||"v0",s=i.versions?.[e];t.push({type:"deleted",resourceName:r,currentHash:null,savedHash:s?.hash,deletedVersion:e})}return t}generateDefinitionHash(e,t=void 0){const r={...e.attributes};e.timestamps&&(delete r.createdAt,delete r.updatedAt);const i={attributes:r,behavior:t||e.behavior||"user-managed",partitions:e.partitions||{}},s=c(i);return`sha256:${o.createHash("sha256").update(s).digest("hex")}`}getNextVersion(e={}){const t=Object.keys(e).filter(e=>e.startsWith("v")).map(e=>parseInt(e.substring(1))).filter(e=>!isNaN(e));return`v${(t.length>0?Math.max(...t):-1)+1}`}_serializeHooks(e){if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))Array.isArray(i)?t[r]=i.map(e=>{if("function"==typeof e)try{return{__s3db_serialized_function:!0,code:e.toString(),name:e.name||"anonymous"}}catch(e){return this.verbose&&console.warn(`Failed to serialize hook for event '${r}':`,e.message),null}return e}):t[r]=i;return t}_deserializeHooks(e){if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))Array.isArray(i)?t[r]=i.map(e=>{if(e&&"object"==typeof e&&e.__s3db_serialized_function){try{const t=new Function("return "+e.code)();if("function"==typeof t)return t}catch(t){this.verbose&&console.warn(`Failed to deserialize hook '${e.name}' for event '${r}':`,t.message)}return null}return e}).filter(e=>null!==e):t[r]=i;return t}async startPlugins(){const e=this;if(!a.isEmpty(this.pluginList)){const t=this.pluginList.map(e=>a.isFunction(e)?new e(this):e),r=t.map(async t=>{t.beforeSetup&&await t.beforeSetup(),await t.setup(e),t.afterSetup&&await t.afterSetup();const r=this._getPluginName(t);this.plugins[r]=t});await Promise.all(r);const i=t.map(async e=>{e.beforeStart&&await e.beforeStart(),await e.start(),e.afterStart&&await e.afterStart()});await Promise.all(i)}}_getPluginName(e,t=null){return t||e.constructor.name.replace("Plugin","").toLowerCase()}async usePlugin(e,t=null){const r=this._getPluginName(e,t);return this.plugins[r]=e,this.isConnected()&&(await e.setup(this),await e.start()),e}async uploadMetadataFile(){const e={version:this.version,s3dbVersion:this.s3dbVersion,lastUpdated:(new Date).toISOString(),resources:{}};Object.entries(this.resources).forEach(([t,r])=>{const i=r.export(),s=this.generateDefinitionHash(i),n=this.savedMetadata?.resources?.[t],o=n?.currentVersion||"v0",a=n?.versions?.[o];let c,l;a&&a.hash===s?(c=o,l=!1):(c=this.getNextVersion(n?.versions),l=!0),e.resources[t]={currentVersion:c,partitions:r.config.partitions||{},versions:{...n?.versions,[c]:{hash:s,attributes:i.attributes,behavior:i.behavior||"user-managed",timestamps:r.config.timestamps,partitions:r.config.partitions,paranoid:r.config.paranoid,allNestedObjectsOptional:r.config.allNestedObjectsOptional,autoDecrypt:r.config.autoDecrypt,cache:r.config.cache,hooks:this.persistHooks?this._serializeHooks(r.config.hooks):r.config.hooks,idSize:r.idSize,idGenerator:r.idGeneratorType,createdAt:l?(new Date).toISOString():a?.createdAt}}},r.version!==c&&(r.version=c,r.emit("versionUpdated",{oldVersion:o,newVersion:c}))}),await this.client.putObject({key:"s3db.json",body:JSON.stringify(e,null,2),contentType:"application/json"}),this.savedMetadata=e,this.emit("metadataUploaded",e)}blankMetadataStructure(){return{version:"1",s3dbVersion:this.s3dbVersion,lastUpdated:(new Date).toISOString(),resources:{}}}async _attemptJsonRecovery(e,t){if(!e||"string"!=typeof e)return t.push("Content is empty or not a string"),null;const r=[()=>e.replace(/,(\s*[}\]])/g,"$1"),()=>e.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g,'$1"$2":'),()=>{let t=0,r=0,i=!1,s=!1;for(let n=0;n<e.length;n++){const o=e[n];s?s=!1:"\\"!==o?'"'!==o?i||("{"===o?t++:"}"===o?t--:"["===o?r++:"]"===o&&r--):i=!i:s=!0}let n=e;for(;r>0;)n+="]",r--;for(;t>0;)n+="}",t--;return n}];for(const[e,i]of r.entries())try{const r=i(),s=JSON.parse(r);return t.push(`JSON recovery successful using fix #${e+1}`),s}catch(e){}return t.push("All JSON recovery attempts failed"),null}async _validateAndHealMetadata(e,t){if(!e||"object"!=typeof e)return t.push("Metadata is not an object - using blank structure"),this.blankMetadataStructure();let r={...e},i=!1;r.version&&"string"==typeof r.version||(r.version&&"number"==typeof r.version?(r.version=String(r.version),t.push("Converted version from number to string"),i=!0):(r.version="1",t.push("Added missing or invalid version field"),i=!0)),r.s3dbVersion&&"string"==typeof r.s3dbVersion||(r.s3dbVersion&&"string"!=typeof r.s3dbVersion?(r.s3dbVersion=String(r.s3dbVersion),t.push("Converted s3dbVersion to string"),i=!0):(r.s3dbVersion=this.s3dbVersion,t.push("Added missing s3dbVersion field"),i=!0)),r.resources&&"object"==typeof r.resources&&!Array.isArray(r.resources)||(r.resources={},t.push("Fixed invalid resources field"),i=!0),r.lastUpdated||(r.lastUpdated=(new Date).toISOString(),t.push("Added missing lastUpdated field"),i=!0);const s={};for(const[e,n]of Object.entries(r.resources)){const r=this._healResourceStructure(e,n,t);r?(s[e]=r,r!==n&&(i=!0)):(t.push(`Removed invalid resource: ${e}`),i=!0)}return r.resources=s,i?r:e}_healResourceStructure(e,t,r){if(!t||"object"!=typeof t)return r.push(`Resource ${e}: invalid structure`),null;let i={...t},s=!1;i.currentVersion||(i.currentVersion="v0",r.push(`Resource ${e}: added missing currentVersion`),s=!0),i.versions&&"object"==typeof i.versions&&!Array.isArray(i.versions)||(i.versions={},r.push(`Resource ${e}: fixed invalid versions object`),s=!0),i.partitions&&"object"==typeof i.partitions&&!Array.isArray(i.partitions)||(i.partitions={},r.push(`Resource ${e}: fixed invalid partitions object`),s=!0);const n=i.currentVersion;if(!i.versions[n]){const t=Object.keys(i.versions);if(!(t.length>0))return r.push(`Resource ${e}: no valid versions found - removing resource`),null;i.currentVersion=t[0],r.push(`Resource ${e}: changed currentVersion from ${n} to ${i.currentVersion}`),s=!0}const o=i.versions[i.currentVersion];if(!o||"object"!=typeof o)return r.push(`Resource ${e}: invalid version data - removing resource`),null;if(!o.attributes||"object"!=typeof o.attributes)return r.push(`Resource ${e}: missing or invalid attributes - removing resource`),null;if(o.hooks){const t=this._healHooksStructure(o.hooks,e,r);t!==o.hooks&&(i.versions[i.currentVersion].hooks=t,s=!0)}return s?i:t}_healHooksStructure(e,t,r){if(!e||"object"!=typeof e)return r.push(`Resource ${t}: invalid hooks structure - using empty hooks`),{};const i={};let s=!1;for(const[n,o]of Object.entries(e))if(Array.isArray(o)){const e=o.filter(e=>null!=e&&""!==e);i[n]=e,e.length!==o.length&&(r.push(`Resource ${t}: cleaned invalid hooks for event ${n}`),s=!0)}else r.push(`Resource ${t}: hooks for event ${n} is not an array - removing`),s=!0;return s?i:e}async _createCorruptedBackup(e=null){try{const t=`s3db.json.corrupted.${(new Date).toISOString().replace(/[:.]/g,"-")}.backup`;if(!e)try{const t=await this.client.getObject("s3db.json");e=await Xr(t?.Body)}catch(t){e="Unable to read corrupted file content"}await this.client.putObject({key:t,body:e,contentType:"application/json"}),this.verbose&&console.warn(`S3DB: Created backup of corrupted s3db.json as ${t}`)}catch(e){this.verbose&&console.warn(`S3DB: Failed to create backup: ${e.message}`)}}async _uploadHealedMetadata(e,t){try{this.verbose&&t.length>0&&(console.warn("S3DB Self-Healing Operations:"),t.forEach(e=>console.warn(` - ${e}`))),e.lastUpdated=(new Date).toISOString(),await this.client.putObject({key:"s3db.json",body:JSON.stringify(e,null,2),contentType:"application/json"}),this.emit("metadataHealed",{healingLog:t,metadata:e}),this.verbose&&console.warn("S3DB: Successfully uploaded healed metadata")}catch(e){throw this.verbose&&console.error(`S3DB: Failed to upload healed metadata: ${e.message}`),e}}resourceExists(e){return!!this.resources[e]}resourceExistsWithSameHash({name:e,attributes:t,behavior:r="user-managed",partitions:i={},options:s={}}){if(!this.resources[e])return{exists:!1,sameHash:!1,hash:null};const n=this.resources[e],o=this.generateDefinitionHash(n.export()),a=new Bi({name:e,attributes:t,behavior:r,partitions:i,client:this.client,version:n.version,passphrase:this.passphrase,versioningEnabled:this.versioningEnabled,...s}),c=this.generateDefinitionHash(a.export());return{exists:!0,sameHash:o===c,hash:c,existingHash:o}}async createResource({name:e,attributes:t,behavior:r="user-managed",hooks:i,...s}){if(this.resources[e]){const n=this.resources[e];if(Object.assign(n.config,{cache:this.cache,...s}),r&&(n.behavior=r),n.versioningEnabled=this.versioningEnabled,n.updateAttributes(t),i)for(const[e,t]of Object.entries(i))if(Array.isArray(t)&&n.hooks[e])for(const r of t)"function"==typeof r&&n.hooks[e].push(r.bind(n));const o=this.generateDefinitionHash(n.export(),n.behavior),a=this.savedMetadata?.resources?.[e],c=a?.currentVersion||"v0",l=a?.versions?.[c];return l&&l.hash===o||await this.uploadMetadataFile(),this.emit("s3db.resourceUpdated",e),n}const n=this.savedMetadata?.resources?.[e],o=n?.currentVersion||"v0",a=new Bi({name:e,client:this.client,version:void 0!==s.version?s.version:o,attributes:t,behavior:r,parallelism:this.parallelism,passphrase:void 0!==s.passphrase?s.passphrase:this.passphrase,observers:[this],cache:void 0!==s.cache?s.cache:this.cache,timestamps:void 0!==s.timestamps&&s.timestamps,partitions:s.partitions||{},paranoid:void 0===s.paranoid||s.paranoid,allNestedObjectsOptional:void 0===s.allNestedObjectsOptional||s.allNestedObjectsOptional,autoDecrypt:void 0===s.autoDecrypt||s.autoDecrypt,hooks:i||{},versioningEnabled:this.versioningEnabled,map:s.map,idGenerator:s.idGenerator,idSize:s.idSize,events:s.events||{}});return a.database=this,this.resources[e]=a,await this.uploadMetadataFile(),this.emit("s3db.resourceCreated",e),a}resource(e){return this.resources[e]?this.resources[e]:Promise.reject(`resource ${e} does not exist`)}async listResources(){return Object.keys(this.resources).map(e=>({name:e}))}async getResource(e){if(!this.resources[e])throw new D({bucket:this.client.config.bucket,resourceName:e,id:e});return this.resources[e]}get config(){return{version:this.version,s3dbVersion:this.s3dbVersion,bucket:this.bucket,keyPrefix:this.keyPrefix,parallelism:this.parallelism,verbose:this.verbose}}isConnected(){return!!this.savedMetadata}async disconnect(){try{if(this.pluginList&&this.pluginList.length>0){for(const e of this.pluginList)e&&"function"==typeof e.removeAllListeners&&e.removeAllListeners();const e=this.pluginList.map(async e=>{try{e&&"function"==typeof e.stop&&await e.stop()}catch(e){}});await Promise.all(e)}if(this.resources&&Object.keys(this.resources).length>0){for(const[e,t]of Object.entries(this.resources))try{t&&"function"==typeof t.removeAllListeners&&t.removeAllListeners(),t._pluginWrappers&&t._pluginWrappers.clear(),t._pluginMiddlewares&&(t._pluginMiddlewares={}),t.observers&&Array.isArray(t.observers)&&(t.observers=[])}catch(e){}Object.keys(this.resources).forEach(e=>delete this.resources[e])}this.client&&"function"==typeof this.client.removeAllListeners&&this.client.removeAllListeners(),this.removeAllListeners(),this.savedMetadata=null,this.plugins={},this.pluginList=[],this.emit("disconnected",new Date)}catch(e){}}_initHooks(){this._hooks=new Map,this._hookEvents=["beforeConnect","afterConnect","beforeCreateResource","afterCreateResource","beforeUploadMetadata","afterUploadMetadata","beforeDisconnect","afterDisconnect","resourceCreated","resourceUpdated"];for(const e of this._hookEvents)this._hooks.set(e,[]);this._wrapHookableMethods()}_wrapHookableMethods(){this._hooksInstalled||(this._originalConnect=this.connect.bind(this),this._originalCreateResource=this.createResource.bind(this),this._originalUploadMetadataFile=this.uploadMetadataFile.bind(this),this._originalDisconnect=this.disconnect.bind(this),this.connect=async(...e)=>{await this._executeHooks("beforeConnect",{args:e});const t=await this._originalConnect(...e);return await this._executeHooks("afterConnect",{result:t,args:e}),t},this.createResource=async e=>{await this._executeHooks("beforeCreateResource",{config:e});const t=await this._originalCreateResource(e);return await this._executeHooks("afterCreateResource",{resource:t,config:e}),t},this.uploadMetadataFile=async(...e)=>{await this._executeHooks("beforeUploadMetadata",{args:e});const t=await this._originalUploadMetadataFile(...e);return await this._executeHooks("afterUploadMetadata",{result:t,args:e}),t},this.disconnect=async(...e)=>{await this._executeHooks("beforeDisconnect",{args:e});const t=await this._originalDisconnect(...e);return await this._executeHooks("afterDisconnect",{result:t,args:e}),t},this._hooksInstalled=!0)}addHook(e,t){if(this._hooks||this._initHooks(),!this._hooks.has(e))throw new Error(`Unknown hook event: ${e}. Available events: ${this._hookEvents.join(", ")}`);if("function"!=typeof t)throw new Error("Hook function must be a function");this._hooks.get(e).push(t)}async _executeHooks(e,t={}){if(!this._hooks||!this._hooks.has(e))return;const r=this._hooks.get(e);for(const i of r)try{await i({database:this,...t})}catch(r){this.emit("hookError",{event:e,error:r,context:t})}}removeHook(e,t){if(!this._hooks||!this._hooks.has(e))return;const r=this._hooks.get(e),i=r.indexOf(t);i>-1&&r.splice(i,1)}getHooks(e){return this._hooks&&this._hooks.has(e)?[...this._hooks.get(e)]:[]}clearHooks(e){this._hooks&&this._hooks.has(e)&&(this._hooks.get(e).length=0)}}class zi extends qi{}function Hi(e){return"string"==typeof e?e.trim().toLowerCase():e}var Vi=class extends oi{constructor(e={},t=[],r=null){super(e),this.instanceId=Math.random().toString(36).slice(2,10),this.client=r,this.connectionString=e.connectionString;let i=t;if(t)if(Array.isArray(t)){i={};for(const e of t)"string"==typeof e&&(i[Hi(e)]=e)}else"string"==typeof t&&(i[Hi(t)]=t);else i={};this.resourcesMap=this._normalizeResources(i)}_normalizeResources(e){if(!e)return{};if(Array.isArray(e)){const t={};for(const r of e)"string"==typeof r?t[Hi(r)]=r:"object"==typeof r&&r.resource&&(t[Hi(r.resource)]=r);return t}if("object"==typeof e){const t={};for(const[r,i]of Object.entries(e)){const e=Hi(r);"string"==typeof i?t[e]=i:Array.isArray(i)?t[e]=i.map(e=>("string"==typeof e||"object"==typeof e&&e.resource,e)):("function"==typeof i||"object"==typeof i&&i.resource)&&(t[e]=i)}return t}return"function"==typeof e?e:{}}validateConfig(){const e=[];return this.client||this.connectionString||e.push("You must provide a client or a connectionString"),(!this.resourcesMap||"object"==typeof this.resourcesMap&&0===Object.keys(this.resourcesMap).length)&&e.push("You must provide a resources map or array"),{isValid:0===e.length,errors:e}}async initialize(e){await super.initialize(e);const[t,r]=await K(async()=>{if(this.client)this.targetDatabase=this.client;else{if(!this.connectionString)throw new Error("S3dbReplicator: No client or connectionString provided");{const e={connectionString:this.connectionString,region:this.region,keyPrefix:this.keyPrefix,verbose:this.config.verbose||!1};this.targetDatabase=new zi(e),await this.targetDatabase.connect()}}this.emit("connected",{replicator:this.name,target:this.connectionString||"client-provided"})});if(!t)throw this.config.verbose&&console.warn(`[S3dbReplicator] Initialization failed: ${r.message}`),r}async replicate(e,t,r,i,s){let n,o,a,c;"object"==typeof e&&e.resource?(n=e.resource,o=e.operation,a=e.data,c=e.id):(n=e,o=t,a=r,c=i);const l=Hi(n),u=this.resourcesMap[l];if(!u)throw new Error(`[S3dbReplicator] Resource not configured: ${n}`);if(Array.isArray(u)){const e=[];for(const t of u){const[r,i,s]=await K(async()=>await this._replicateToSingleDestination(t,l,o,a,c));if(!r)throw this.config&&this.config.verbose&&console.warn(`[S3dbReplicator] Failed to replicate to destination ${JSON.stringify(t)}: ${i.message}`),i;e.push(s)}return e}{const[e,t,r]=await K(async()=>await this._replicateToSingleDestination(u,l,o,a,c));if(!e)throw this.config&&this.config.verbose&&console.warn(`[S3dbReplicator] Failed to replicate to destination ${JSON.stringify(u)}: ${t.message}`),t;return r}}async _replicateToSingleDestination(e,t,r,i,s){let n;if(n="string"==typeof e?e:"object"==typeof e&&e.resource?e.resource:t,"object"==typeof e&&e.actions&&Array.isArray(e.actions)&&!e.actions.includes(r))return{skipped:!0,reason:"action_not_supported",action:r,destination:n};const o=this._getDestResourceObj(n);let a,c;if("object"==typeof e&&e.transform&&"function"==typeof e.transform?(a=e.transform(i),a&&i&&i.id&&!a.id&&(a.id=i.id)):"object"==typeof e&&e.transformer&&"function"==typeof e.transformer?(a=e.transformer(i),a&&i&&i.id&&!a.id&&(a.id=i.id)):a=i,!a&&i&&(a=i),"insert"===r)c=await o.insert(a);else if("update"===r)c=await o.update(s,a);else{if("delete"!==r)throw new Error(`Invalid operation: ${r}. Supported operations are: insert, update, delete`);c=await o.delete(s)}return c}_applyTransformer(e,t){let r=this._cleanInternalFields(t);const i=Hi(e),s=this.resourcesMap[i];let n;if(!s)return r;if(Array.isArray(s)){for(const e of s){if("object"==typeof e&&e.transform&&"function"==typeof e.transform){n=e.transform(r);break}if("object"==typeof e&&e.transformer&&"function"==typeof e.transformer){n=e.transformer(r);break}}n||(n=r)}else"object"==typeof s?"function"==typeof s.transform?n=s.transform(r):"function"==typeof s.transformer&&(n=s.transformer(r)):n="function"==typeof s?s(r):r;return n&&r&&r.id&&!n.id&&(n.id=r.id),!n&&r&&(n=r),n}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}_resolveDestResource(e,t){const r=Hi(e),i=this.resourcesMap[r];if(!i)return e;if(Array.isArray(i)){for(const e of i){if("string"==typeof e)return e;if("object"==typeof e&&e.resource)return e.resource}return e}return"string"==typeof i?i:"function"==typeof i?e:"object"==typeof i&&i.resource?i.resource:e}_getDestResourceObj(e){const t=Object.keys(this.client.resources||{}),r=Hi(e),i=t.find(e=>Hi(e)===r);if(!i)throw new Error(`[S3dbReplicator] Destination resource not found: ${e}. Available: ${t.join(", ")}`);return this.client.resources[i]}async replicateBatch(e,t){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};const r=[],i=[];for(const s of t){const[t,n,o]=await K(()=>this.replicate({resource:e,operation:s.operation,id:s.id,data:s.data,beforeData:s.beforeData}));t?r.push(o):(this.config.verbose&&console.warn(`[S3dbReplicator] Batch replication failed for record ${s.id}: ${n.message}`),i.push({id:s.id,error:n.message}))}return i.length>0&&console.warn(`[S3dbReplicator] Batch replication completed with ${i.length} error(s) for ${e}:`,i),this.emit("batch_replicated",{replicator:this.name,resourceName:e,total:t.length,successful:r.length,errors:i.length}),{success:0===i.length,results:r,errors:i,total:t.length}}async testConnection(){const[e,t]=await K(async()=>{if(!this.targetDatabase)throw new Error("No target database configured");return"function"==typeof this.targetDatabase.connect&&await this.targetDatabase.connect(),!0});return!!e||(this.config.verbose&&console.warn(`[S3dbReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}async getStatus(){return{...await super.getStatus(),connected:!!this.targetDatabase,targetDatabase:this.connectionString||"client-provided",resources:Object.keys(this.resourcesMap||{}),totalreplicators:this.listenerCount("replicated"),totalErrors:this.listenerCount("replicator_error")}}async cleanup(){this.targetDatabase&&this.targetDatabase.removeAllListeners(),await super.cleanup()}shouldReplicateResource(e,t){const r=Hi(e),i=this.resourcesMap[r];if(!i)return!1;if(!t)return!0;if(Array.isArray(i)){for(const e of i)if("object"==typeof e&&e.resource){if(!e.actions||!Array.isArray(e.actions))return!0;if(e.actions.includes(t))return!0}else if("string"==typeof e)return!0;return!1}return"object"==typeof i&&i.resource?!i.actions||!Array.isArray(i.actions)||i.actions.includes(t):"string"==typeof i||"function"==typeof i}};var Ki=class extends oi{constructor(e={},t=[],r=null){if(super(e),this.client=r,this.queueUrl=e.queueUrl,this.queues=e.queues||{},this.defaultQueue=e.defaultQueue||e.defaultQueueUrl||e.queueUrlDefault,this.region=e.region||"us-east-1",this.sqsClient=r||null,this.messageGroupId=e.messageGroupId,this.deduplicationId=e.deduplicationId,Array.isArray(t)){this.resources={};for(const e of t)"string"==typeof e?this.resources[e]=!0:"object"==typeof e&&e.name&&(this.resources[e.name]=e)}else if("object"==typeof t){this.resources=t;for(const[e,r]of Object.entries(t))r&&r.queueUrl&&(this.queues[e]=r.queueUrl)}else this.resources={}}validateConfig(){const e=[];return this.queueUrl||0!==Object.keys(this.queues).length||this.defaultQueue||this.resourceQueueMap||e.push("Either queueUrl, queues object, defaultQueue, or resourceQueueMap must be provided"),{isValid:0===e.length,errors:e}}getQueueUrlsForResource(e){if(this.resourceQueueMap&&this.resourceQueueMap[e])return this.resourceQueueMap[e];if(this.queues[e])return[this.queues[e]];if(this.queueUrl)return[this.queueUrl];if(this.defaultQueue)return[this.defaultQueue];throw new Error(`No queue URL found for resource '${e}'`)}_applyTransformer(e,t){let r=this._cleanInternalFields(t);const i=this.resources[e];let s=r;return i?("function"==typeof i.transform?s=i.transform(r):"function"==typeof i.transformer&&(s=i.transformer(r)),s||r):r}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}createMessage(e,t,r,i,s=null){const n={resource:e,action:t,timestamp:(new Date).toISOString(),source:"s3db-replicator"};switch(t){case"insert":case"delete":default:return{...n,data:r};case"update":return{...n,before:s,data:r}}}async initialize(e,t){if(await super.initialize(e),!this.sqsClient){const[e,r,i]=await K(()=>import("@aws-sdk/client-sqs"));if(!e)throw this.config.verbose&&console.warn(`[SqsReplicator] Failed to import SQS SDK: ${r.message}`),this.emit("initialization_error",{replicator:this.name,error:r.message}),r;const{SQSClient:s}=i;this.sqsClient=t||new s({region:this.region,credentials:this.config.credentials}),this.emit("initialized",{replicator:this.name,queueUrl:this.queueUrl,queues:this.queues,defaultQueue:this.defaultQueue})}}async replicate(e,t,r,i,s=null){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};const[n,o,a]=await K(async()=>{const{SendMessageCommand:n}=await import("@aws-sdk/client-sqs"),o=this.getQueueUrlsForResource(e),a=this._applyTransformer(e,r),c=this.createMessage(e,t,a,i,s),l=[];for(const r of o){const s=new n({QueueUrl:r,MessageBody:JSON.stringify(c),MessageGroupId:this.messageGroupId,MessageDeduplicationId:this.deduplicationId?`${e}:${t}:${i}`:void 0}),o=await this.sqsClient.send(s);l.push({queueUrl:r,messageId:o.MessageId}),this.emit("replicated",{replicator:this.name,resource:e,operation:t,id:i,queueUrl:r,messageId:o.MessageId,success:!0})}return{success:!0,results:l}});return n?a:(this.config.verbose&&console.warn(`[SqsReplicator] Replication failed for ${e}: ${o.message}`),this.emit("replicator_error",{replicator:this.name,resource:e,operation:t,id:i,error:o.message}),{success:!1,error:o.message})}async replicateBatch(e,t){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};const[r,i,s]=await K(async()=>{const{SendMessageBatchCommand:r}=await import("@aws-sdk/client-sqs"),i=this.getQueueUrlsForResource(e),s=[];for(let e=0;e<t.length;e+=10)s.push(t.slice(e,e+10));const n=[],o=[];for(const t of s){const[s,a]=await K(async()=>{const s=t.map((t,r)=>({Id:`${t.id}-${r}`,MessageBody:JSON.stringify(this.createMessage(e,t.operation,t.data,t.id,t.beforeData)),MessageGroupId:this.messageGroupId,MessageDeduplicationId:this.deduplicationId?`${e}:${t.operation}:${t.id}`:void 0})),o=new r({QueueUrl:i[0],Entries:s}),a=await this.sqsClient.send(o);n.push(a)});if(!s&&(o.push({batch:t.length,error:a.message}),a.message&&(a.message.includes("Batch error")||a.message.includes("Connection")||a.message.includes("Network"))))throw a}return o.length>0&&console.warn(`[SqsReplicator] Batch replication completed with ${o.length} error(s) for ${e}:`,o),this.emit("batch_replicated",{replicator:this.name,resource:e,queueUrl:i[0],total:t.length,successful:n.length,errors:o.length}),{success:0===o.length,results:n,errors:o,total:t.length,queueUrl:i[0]}});if(r)return s;const n=i?.message||i||"Unknown error";return this.config.verbose&&console.warn(`[SqsReplicator] Batch replication failed for ${e}: ${n}`),this.emit("batch_replicator_error",{replicator:this.name,resource:e,error:n}),{success:!1,error:n}}async testConnection(){const[e,t]=await K(async()=>{this.sqsClient||await this.initialize(this.database);const{GetQueueAttributesCommand:e}=await import("@aws-sdk/client-sqs"),t=new e({QueueUrl:this.queueUrl,AttributeNames:["QueueArn"]});return await this.sqsClient.send(t),!0});return!!e||(this.config.verbose&&console.warn(`[SqsReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}async getStatus(){return{...await super.getStatus(),connected:!!this.sqsClient,queueUrl:this.queueUrl,region:this.region,resources:Object.keys(this.resources||{}),totalreplicators:this.listenerCount("replicated"),totalErrors:this.listenerCount("replicator_error")}}async cleanup(){this.sqsClient&&this.sqsClient.destroy(),await super.cleanup()}shouldReplicateResource(e){return this.resourceQueueMap&&Object.keys(this.resourceQueueMap).includes(e)||this.queues&&Object.keys(this.queues).includes(e)||!(!this.defaultQueue&&!this.queueUrl)||this.resources&&Object.keys(this.resources).includes(e)||!1}};const Wi={s3db:Vi,sqs:Ki,bigquery:ai,postgres:ci};function Ji(e){return"string"==typeof e?e.trim().toLowerCase():e}return e.AVAILABLE_BEHAVIORS=Li,e.AuditPlugin=class extends ue{constructor(e={}){super(e),this.auditResource=null,this.config={includeData:!1!==e.includeData,includePartitions:!1!==e.includePartitions,maxDataSize:e.maxDataSize||1e4,...e}}async onSetup(){const[e,t,r]=await K(()=>this.database.createResource({name:"audits",attributes:{id:"string|required",resourceName:"string|required",operation:"string|required",recordId:"string|required",userId:"string|optional",timestamp:"string|required",oldData:"string|optional",newData:"string|optional",partition:"string|optional",partitionValues:"string|optional",metadata:"string|optional"},behavior:"body-overflow"}));if(this.auditResource=e?r:this.database.resources.audits||null,e||this.auditResource){this.database.addHook("afterCreateResource",e=>{"audits"!==e.resource.name&&this.setupResourceAuditing(e.resource)});for(const e of Object.values(this.database.resources))"audits"!==e.name&&this.setupResourceAuditing(e)}}async onStart(){}async onStop(){}setupResourceAuditing(e){e.on("insert",async t=>{const r=this.config.includePartitions?this.getPartitionValues(t,e):null;await this.logAudit({resourceName:e.name,operation:"insert",recordId:t.id||"auto-generated",oldData:null,newData:this.config.includeData?JSON.stringify(this.truncateData(t)):null,partition:r?this.getPrimaryPartition(r):null,partitionValues:r?JSON.stringify(r):null})}),e.on("update",async t=>{let r=t.$before;if(this.config.includeData&&!r){const[i,s,n]=await K(()=>e.get(t.id));i&&(r=n)}const i=this.config.includePartitions?this.getPartitionValues(t,e):null;await this.logAudit({resourceName:e.name,operation:"update",recordId:t.id,oldData:r&&this.config.includeData?JSON.stringify(this.truncateData(r)):null,newData:this.config.includeData?JSON.stringify(this.truncateData(t)):null,partition:i?this.getPrimaryPartition(i):null,partitionValues:i?JSON.stringify(i):null})}),e.on("delete",async t=>{let r=t;if(this.config.includeData&&!r){const[i,s,n]=await K(()=>e.get(t.id));i&&(r=n)}const i=r&&this.config.includePartitions?this.getPartitionValues(r,e):null;await this.logAudit({resourceName:e.name,operation:"delete",recordId:t.id,oldData:r&&this.config.includeData?JSON.stringify(this.truncateData(r)):null,newData:null,partition:i?this.getPrimaryPartition(i):null,partitionValues:i?JSON.stringify(i):null})});const t=e.deleteMany.bind(e),r=this;e.deleteMany=async function(i){const s=[];if(r.config.includeData)for(const t of i){const[r,i,n]=await K(()=>e.get(t));r?s.push(n):s.push({id:t})}else s.push(...i.map(e=>({id:e})));const n=await t(i);for(const t of s){const i=t&&r.config.includePartitions?r.getPartitionValues(t,e):null;await r.logAudit({resourceName:e.name,operation:"deleteMany",recordId:t.id,oldData:t&&r.config.includeData?JSON.stringify(r.truncateData(t)):null,newData:null,partition:i?r.getPrimaryPartition(i):null,partitionValues:i?JSON.stringify(i):null})}return n},e._originalDeleteMany=t}installEventListenersForResource(e){return this.setupResourceAuditing(e)}async logAudit(e){if(!this.auditResource)return;const t={id:`audit-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,userId:this.getCurrentUserId?.()||"system",timestamp:(new Date).toISOString(),metadata:JSON.stringify({source:"audit-plugin",version:"2.0"}),resourceName:e.resourceName,operation:e.operation,recordId:e.recordId};null!==e.oldData&&(t.oldData=e.oldData),null!==e.newData&&(t.newData=e.newData),null!==e.partition&&(t.partition=e.partition),null!==e.partitionValues&&(t.partitionValues=e.partitionValues);try{await this.auditResource.insert(t)}catch(e){console.warn("Audit logging failed:",e.message)}}getPartitionValues(e,t){if(!this.config.includePartitions)return null;const r=t.config?.partitions||t.partitions;if(!r)return null;const i={};for(const[t,s]of Object.entries(r)){const r={};for(const t of Object.keys(s.fields))r[t]=this.getNestedFieldValue(e,t);Object.values(r).some(e=>null!=e)&&(i[t]=r)}return Object.keys(i).length>0?i:null}getNestedFieldValue(e,t){const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return;i=i[e]}return i}getPrimaryPartition(e){if(!e)return null;const t=Object.keys(e);return t.length>0?t[0]:null}truncateData(e){if(!this.config.includeData)return null;const t=JSON.stringify(e);return t.length<=this.config.maxDataSize?e:{...e,_truncated:!0,_originalSize:t.length,_truncatedAt:(new Date).toISOString()}}async getAuditLogs(e={}){if(!this.auditResource)return[];const{resourceName:t,operation:r,recordId:i,partition:s,startDate:n,endDate:o,limit:a=100,offset:c=0}=e;let l={};t&&(l.resourceName=t),r&&(l.operation=r),i&&(l.recordId=i),s&&(l.partition=s),(n||o)&&(l.timestamp={},n&&(l.timestamp.$gte=n),o&&(l.timestamp.$lte=o));return(await this.auditResource.page({query:l,limit:a,offset:c})).items||[]}async getRecordHistory(e,t){return await this.getAuditLogs({resourceName:e,recordId:t})}async getPartitionHistory(e,t,r){return await this.getAuditLogs({resourceName:e,partition:t,partitionValues:JSON.stringify(r)})}async getAuditStats(e={}){const t=await this.getAuditLogs(e),r={total:t.length,byOperation:{},byResource:{},byPartition:{},byUser:{},timeline:{}};for(const e of t){r.byOperation[e.operation]=(r.byOperation[e.operation]||0)+1,r.byResource[e.resourceName]=(r.byResource[e.resourceName]||0)+1,e.partition&&(r.byPartition[e.partition]=(r.byPartition[e.partition]||0)+1),r.byUser[e.userId]=(r.byUser[e.userId]||0)+1;const t=e.timestamp.split("T")[0];r.timeline[t]=(r.timeline[t]||0)+1}return r}},e.AuthenticationError=class extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}},e.BaseError=j,e.CachePlugin=class extends ue{constructor(e={}){super(e),this.driverName=e.driver||"s3",this.ttl=e.ttl,this.maxSize=e.maxSize,this.config=e.config||{},this.includePartitions=!1!==e.includePartitions,this.partitionStrategy=e.partitionStrategy||"hierarchical",this.partitionAware=!1!==e.partitionAware,this.trackUsage=!1!==e.trackUsage,this.preloadRelated=!1!==e.preloadRelated,this.legacyConfig={memoryOptions:e.memoryOptions,filesystemOptions:e.filesystemOptions,s3Options:e.s3Options,driver:e.driver}}async setup(e){await super.setup(e)}async onSetup(){if(this.driverName&&"object"==typeof this.driverName)this.driver=this.driverName;else if("memory"===this.driverName){const e={...this.legacyConfig.memoryOptions,...this.config};void 0!==this.ttl&&(e.ttl=this.ttl),void 0!==this.maxSize&&(e.maxSize=this.maxSize),this.driver=new ti(e)}else if("filesystem"===this.driverName){const e={...this.legacyConfig.filesystemOptions,...this.config};void 0!==this.ttl&&(e.ttl=this.ttl),void 0!==this.maxSize&&(e.maxSize=this.maxSize),this.partitionAware?this.driver=new si({partitionStrategy:this.partitionStrategy,trackUsage:this.trackUsage,preloadRelated:this.preloadRelated,...e}):this.driver=new ii(e)}else{const e={client:this.database.client,...this.legacyConfig.s3Options,...this.config};void 0!==this.ttl&&(e.ttl=this.ttl),void 0!==this.maxSize&&(e.maxSize=this.maxSize),this.driver=new ei(e)}this.installDatabaseHooks(),this.installResourceHooks()}installDatabaseHooks(){this.database.addHook("afterCreateResource",async({resource:e})=>{this.installResourceHooksForResource(e)})}async onStart(){}async onStop(){}installResourceHooks(){for(const e of Object.values(this.database.resources))this.installResourceHooksForResource(e)}installResourceHooksForResource(e){if(!this.driver)return;Object.defineProperty(e,"cache",{value:this.driver,writable:!0,configurable:!0,enumerable:!1}),e.cacheKeyFor=async(t={})=>{const{action:r,params:i={},partition:s,partitionValues:n}=t;return this.generateCacheKey(e,r,i,s,n)},this.driver instanceof si&&(e.clearPartitionCache=async(t,r={})=>await this.driver.clearPartition(e.name,t,r),e.getPartitionCacheStats=async(t=null)=>await this.driver.getPartitionStats(e.name,t),e.getCacheRecommendations=async()=>await this.driver.getCacheRecommendations(e.name),e.warmPartitionCache=async(t=[],r={})=>await this.driver.warmPartitionCache(e.name,{partitions:t,...r}));const t=["count","listIds","getMany","getAll","page","list","get","exists","content","hasContent","query","getFromPartition"];for(const r of t)e.useMiddleware(r,async(t,i)=>{let s;if("getMany"===r)s=await e.cacheKeyFor({action:r,params:{ids:t.args[0]}});else if("page"===r){const{offset:i,size:n,partition:o,partitionValues:a}=t.args[0]||{};s=await e.cacheKeyFor({action:r,params:{offset:i,size:n},partition:o,partitionValues:a})}else if("list"===r||"listIds"===r||"count"===r){const{partition:i,partitionValues:n}=t.args[0]||{};s=await e.cacheKeyFor({action:r,partition:i,partitionValues:n})}else if("query"===r){const i=t.args[0]||{},n=t.args[1]||{};s=await e.cacheKeyFor({action:r,params:{filter:i,options:{limit:n.limit,offset:n.offset}},partition:n.partition,partitionValues:n.partitionValues})}else if("getFromPartition"===r){const{id:i,partitionName:n,partitionValues:o}=t.args[0]||{};s=await e.cacheKeyFor({action:r,params:{id:i,partitionName:n},partition:n,partitionValues:o})}else"getAll"===r?s=await e.cacheKeyFor({action:r}):["get","exists","content","hasContent"].includes(r)&&(s=await e.cacheKeyFor({action:r,params:{id:t.args[0]}}));if(this.driver instanceof si){let n,o;if("list"===r||"listIds"===r||"count"===r||"page"===r){const e=t.args[0]||{};n=e.partition,o=e.partitionValues}else if("query"===r){const e=t.args[1]||{};n=e.partition,o=e.partitionValues}else if("getFromPartition"===r){const{partitionName:e,partitionValues:r}=t.args[0]||{};n=e,o=r}const[a,c,l]=await K(()=>e.cache._get(s,{resource:e.name,action:r,partition:n,partitionValues:o}));if(a&&null!=l)return l;if(!a&&"NoSuchKey"!==c.name)throw c;const u=await i();return await e.cache._set(s,u,{resource:e.name,action:r,partition:n,partitionValues:o}),u}{const[t,r,n]=await K(()=>e.cache.get(s));if(t&&null!=n)return n;if(!t&&"NoSuchKey"!==r.name)throw r;const o=await i();return await e.cache.set(s,o),o}});const r=["insert","update","delete","deleteMany","setContent","deleteContent","replace"];for(const t of r)e.useMiddleware(t,async(r,i)=>{const s=await i();if("insert"===t)await this.clearCacheForResource(e,r.args[0]);else if("update"===t)await this.clearCacheForResource(e,{id:r.args[0],...r.args[1]});else if("delete"===t){let t={id:r.args[0]};if("function"==typeof e.get){const[i,s,n]=await K(()=>e.get(r.args[0]));i&&n&&(t=n)}await this.clearCacheForResource(e,t)}else if("setContent"===t||"deleteContent"===t){const t=r.args[0]?.id||r.args[0];await this.clearCacheForResource(e,{id:t})}else if("replace"===t){const t=r.args[0];await this.clearCacheForResource(e,{id:t,...r.args[1]})}else"deleteMany"===t&&await this.clearCacheForResource(e);return s})}async clearCacheForResource(e,t){if(!e.cache)return;const r=`resource=${e.name}`;if(t&&t.id){const i=["get","exists","content","hasContent"];for(const r of i)try{const i=await this.generateCacheKey(e,r,{id:t.id});await e.cache.clear(i.replace(".json.gz",""))}catch(e){}if(!0===this.config.includePartitions&&e.config?.partitions&&Object.keys(e.config.partitions).length>0){const i=this.getPartitionValues(t,e);for(const[t,s]of Object.entries(i))if(s&&Object.keys(s).length>0&&Object.values(s).some(e=>null!=e))try{const i=be(r,`partition=${t}`);await e.cache.clear(i)}catch(e){}}}try{await e.cache.clear(r)}catch(t){const i=["count","list","listIds","getAll","page","query"];for(const t of i)try{await e.cache.clear(`${r}/action=${t}`),await e.cache.clear(`resource=${e.name}/action=${t}`)}catch(e){}}}async generateCacheKey(e,t,r={},i=null,s=null){const n=[`resource=${e.name}`,`action=${t}`];if(i&&s&&Object.keys(s).length>0){n.push(`partition:${i}`);for(const[e,t]of Object.entries(s))null!=t&&n.push(`${e}:${t}`)}if(Object.keys(r).length>0){const e=await this.hashParams(r);n.push(e)}return be(...n)+".json.gz"}async hashParams(e){const t=Object.keys(e).sort().map(t=>`${t}:${JSON.stringify(e[t])}`).join("|")||"empty";return await J(t)}async getCacheStats(){return this.driver?{size:await this.driver.size(),keys:await this.driver.keys(),driver:this.driver.constructor.name}:null}async clearAllCache(){if(this.driver)for(const e of Object.values(this.database.resources))if(e.cache){const t=`resource=${e.name}`;await e.cache.clear(t)}}async warmCache(e,t={}){const r=this.database.resources[e];if(!r)throw new Error(`Resource '${e}' not found`);const{includePartitions:i=!0}=t;if(this.driver instanceof si&&r.warmPartitionCache){const e=r.config.partitions?Object.keys(r.config.partitions):[];return await r.warmPartitionCache(e,t)}if(await r.getAll(),i&&r.config.partitions)for(const[e,t]of Object.entries(r.config.partitions))if(t.fields){const t=await r.getAll(),i=Array.isArray(t)?t:[],s=new Set;for(const t of i.slice(0,10)){const i=this.getPartitionValues(t,r);i[e]&&s.add(JSON.stringify(i[e]))}for(const t of s){const i=JSON.parse(t);await r.list({partition:e,partitionValues:i})}}}async getPartitionCacheStats(e,t=null){if(!(this.driver instanceof si))throw new Error("Partition cache statistics are only available with PartitionAwareFilesystemCache");return await this.driver.getPartitionStats(e,t)}async getCacheRecommendations(e){if(!(this.driver instanceof si))throw new Error("Cache recommendations are only available with PartitionAwareFilesystemCache");return await this.driver.getCacheRecommendations(e)}async clearPartitionCache(e,t,r={}){if(!(this.driver instanceof si))throw new Error("Partition cache clearing is only available with PartitionAwareFilesystemCache");return await this.driver.clearPartition(e,t,r)}async analyzeCacheUsage(){if(!(this.driver instanceof si))return{message:"Cache usage analysis is only available with PartitionAwareFilesystemCache"};const e={totalResources:Object.keys(this.database.resources).length,resourceStats:{},recommendations:{},summary:{mostUsedPartitions:[],leastUsedPartitions:[],suggestedOptimizations:[]}};for(const[t,r]of Object.entries(this.database.resources))try{e.resourceStats[t]=await this.driver.getPartitionStats(t),e.recommendations[t]=await this.driver.getCacheRecommendations(t)}catch(r){e.resourceStats[t]={error:r.message}}const t=Object.values(e.recommendations).flat();return e.summary.mostUsedPartitions=t.filter(e=>"preload"===e.recommendation).sort((e,t)=>t.priority-e.priority).slice(0,5),e.summary.leastUsedPartitions=t.filter(e=>"archive"===e.recommendation).slice(0,5),e.summary.suggestedOptimizations=[`Consider preloading ${e.summary.mostUsedPartitions.length} high-usage partitions`,`Archive ${e.summary.leastUsedPartitions.length} unused partitions`,"Monitor cache hit rates for partition efficiency"],e}},e.Client=fi,e.ConnectionString=hi,e.ConnectionStringError=U,e.CostsPlugin=ni,e.CryptoError=F,e.DEFAULT_BEHAVIOR=Ui,e.Database=qi,e.DatabaseError=class extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}},e.EncryptionError=class extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}},e.ErrorMap=M,e.FullTextPlugin=class extends ue{constructor(e={}){super(),this.indexResource=null,this.config={minWordLength:e.minWordLength||3,maxResults:e.maxResults||100,...e},this.indexes=new Map}async setup(e){this.database=e;const[t,r,i]=await K(()=>e.createResource({name:"fulltext_indexes",attributes:{id:"string|required",resourceName:"string|required",fieldName:"string|required",word:"string|required",recordIds:"json|required",count:"number|required",lastUpdated:"string|required"}}));this.indexResource=t?i:e.resources.fulltext_indexes,await this.loadIndexes(),this.installDatabaseHooks(),this.installIndexingHooks()}async start(){}async stop(){await this.saveIndexes(),this.removeDatabaseHooks()}async loadIndexes(){if(!this.indexResource)return;const[e,t,r]=await K(()=>this.indexResource.getAll());if(e)for(const e of r){const t=`${e.resourceName}:${e.fieldName}:${e.word}`;this.indexes.set(t,{recordIds:e.recordIds||[],count:e.count||0})}}async saveIndexes(){if(!this.indexResource)return;const[e,t]=await K(async()=>{const e=await this.indexResource.getAll();for(const t of e)await this.indexResource.delete(t.id);for(const[e,t]of this.indexes.entries()){const[r,i,s]=e.split(":");await this.indexResource.insert({id:`index-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,resourceName:r,fieldName:i,word:s,recordIds:t.recordIds,count:t.count,lastUpdated:(new Date).toISOString()})}})}installDatabaseHooks(){this.database.addHook("afterCreateResource",e=>{"fulltext_indexes"!==e.name&&this.installResourceHooks(e)})}removeDatabaseHooks(){this.database.removeHook("afterCreateResource",this.installResourceHooks.bind(this))}installIndexingHooks(){this.database.plugins||(this.database.plugins={}),this.database.plugins.fulltext=this;for(const e of Object.values(this.database.resources))"fulltext_indexes"!==e.name&&this.installResourceHooks(e);this.database._fulltextProxyInstalled||(this.database._previousCreateResourceForFullText=this.database.createResource,this.database.createResource=async function(...e){const t=await this._previousCreateResourceForFullText(...e);return this.plugins?.fulltext&&"fulltext_indexes"!==t.name&&this.plugins.fulltext.installResourceHooks(t),t},this.database._fulltextProxyInstalled=!0);for(const e of Object.values(this.database.resources))"fulltext_indexes"!==e.name&&this.installResourceHooks(e)}installResourceHooks(e){e._insert=e.insert,e._update=e.update,e._delete=e.delete,e._deleteMany=e.deleteMany,this.wrapResourceMethod(e,"insert",async(t,r,i)=>{const[s]=r;return this.indexRecord(e.name,t.id,s).catch(()=>{}),t}),this.wrapResourceMethod(e,"update",async(t,r,i)=>{const[s,n]=r;return this.removeRecordFromIndex(e.name,s).catch(()=>{}),this.indexRecord(e.name,s,t).catch(()=>{}),t}),this.wrapResourceMethod(e,"delete",async(t,r,i)=>{const[s]=r;return this.removeRecordFromIndex(e.name,s).catch(()=>{}),t}),this.wrapResourceMethod(e,"deleteMany",async(t,r,i)=>{const[s]=r;for(const t of s)this.removeRecordFromIndex(e.name,t).catch(()=>{});return t})}async indexRecord(e,t,r){const i=this.getIndexedFields(e);if(i&&0!==i.length)for(const s of i){const i=this.getFieldValue(r,s);if(!i)continue;const n=this.tokenize(i);for(const r of n){if(r.length<this.config.minWordLength)continue;const i=`${e}:${s}:${r.toLowerCase()}`,n=this.indexes.get(i)||{recordIds:[],count:0};n.recordIds.includes(t)||(n.recordIds.push(t),n.count=n.recordIds.length),this.indexes.set(i,n)}}}async removeRecordFromIndex(e,t){for(const[r,i]of this.indexes.entries())if(r.startsWith(`${e}:`)){const e=i.recordIds.indexOf(t);e>-1&&(i.recordIds.splice(e,1),i.count=i.recordIds.length,0===i.recordIds.length?this.indexes.delete(r):this.indexes.set(r,i))}}getFieldValue(e,t){if(!t.includes("."))return e&&void 0!==e[t]?e[t]:null;const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return null;i=i[e]}return i}tokenize(e){if(!e)return[];return String(e).toLowerCase().replace(/[^\w\s\u00C0-\u017F]/g," ").split(/\s+/).filter(e=>e.length>0)}getIndexedFields(e){if(this.config.fields)return this.config.fields;return{users:["name","email"],products:["name","description"],articles:["title","content"]}[e]||[]}async search(e,t,r={}){const{fields:i=null,limit:s=this.config.maxResults,offset:n=0,exactMatch:o=!1}=r;if(!t||0===t.trim().length)return[];const a=this.tokenize(t),c=new Map,l=i||this.getIndexedFields(e);if(0===l.length)return[];for(const t of a)if(!(t.length<this.config.minWordLength))for(const r of l)if(o){const i=`${e}:${r}:${t.toLowerCase()}`,s=this.indexes.get(i);if(s)for(const e of s.recordIds){const t=c.get(e)||0;c.set(e,t+1)}}else for(const[i,s]of this.indexes.entries())if(i.startsWith(`${e}:${r}:${t.toLowerCase()}`))for(const e of s.recordIds){const t=c.get(e)||0;c.set(e,t+1)}return Array.from(c.entries()).map(([e,t])=>({recordId:e,score:t})).sort((e,t)=>t.score-e.score).slice(n,n+s)}async searchRecords(e,t,r={}){const i=await this.search(e,t,r);if(0===i.length)return[];const s=this.database.resources[e];if(!s)throw new Error(`Resource '${e}' not found`);const n=i.map(e=>e.recordId);return(await s.getMany(n)).filter(e=>e&&"object"==typeof e).map(e=>{const t=i.find(t=>t.recordId===e.id);return{...e,_searchScore:t?t.score:0}}).sort((e,t)=>t._searchScore-e._searchScore)}async rebuildIndex(e){const t=this.database.resources[e];if(!t)throw new Error(`Resource '${e}' not found`);for(const[t]of this.indexes.entries())t.startsWith(`${e}:`)&&this.indexes.delete(t);const r=await t.getAll();for(let t=0;t<r.length;t+=100){const i=r.slice(t,t+100);for(const t of i){const[r,i]=await K(()=>this.indexRecord(e,t.id,t))}}await this.saveIndexes()}async getIndexStats(){const e={totalIndexes:this.indexes.size,resources:{},totalWords:0};for(const[t,r]of this.indexes.entries()){const[i,s]=t.split(":");e.resources[i]||(e.resources[i]={fields:{},totalRecords:new Set,totalWords:0}),e.resources[i].fields[s]||(e.resources[i].fields[s]={words:0,totalOccurrences:0}),e.resources[i].fields[s].words++,e.resources[i].fields[s].totalOccurrences+=r.count,e.resources[i].totalWords++;for(const t of r.recordIds)e.resources[i].totalRecords.add(t);e.totalWords++}for(const t in e.resources)e.resources[t].totalRecords=e.resources[t].totalRecords.size;return e}async rebuildAllIndexes({timeout:e}={}){return e?Promise.race([this._rebuildAllIndexesInternal(),new Promise((t,r)=>setTimeout(()=>r(new Error("Timeout")),e))]):this._rebuildAllIndexesInternal()}async _rebuildAllIndexesInternal(){const e=Object.keys(this.database.resources).filter(e=>"fulltext_indexes"!==e);for(const t of e){const[e,r]=await K(()=>this.rebuildIndex(t))}}async clearIndex(e){for(const[t]of this.indexes.entries())t.startsWith(`${e}:`)&&this.indexes.delete(t);await this.saveIndexes()}async clearAllIndexes(){this.indexes.clear(),await this.saveIndexes()}},e.InvalidResourceItem=I,e.MetricsPlugin=class extends ue{constructor(e={}){super(),this.config={collectPerformance:!1!==e.collectPerformance,collectErrors:!1!==e.collectErrors,collectUsage:!1!==e.collectUsage,retentionDays:e.retentionDays||30,flushInterval:e.flushInterval||6e4,...e},this.metrics={operations:{insert:{count:0,totalTime:0,errors:0},update:{count:0,totalTime:0,errors:0},delete:{count:0,totalTime:0,errors:0},get:{count:0,totalTime:0,errors:0},list:{count:0,totalTime:0,errors:0},count:{count:0,totalTime:0,errors:0}},resources:{},errors:[],performance:[],startTime:(new Date).toISOString()},this.flushTimer=null}async setup(e){if(this.database=e,"undefined"!=typeof process&&"test"===process.env.NODE_ENV)return;const[t,r]=await K(async()=>{const[t,r,i]=await K(()=>e.createResource({name:"metrics",attributes:{id:"string|required",type:"string|required",resourceName:"string",operation:"string",count:"number|required",totalTime:"number|required",errors:"number|required",avgTime:"number|required",timestamp:"string|required",metadata:"json"}}));this.metricsResource=t?i:e.resources.metrics;const[s,n,o]=await K(()=>e.createResource({name:"error_logs",attributes:{id:"string|required",resourceName:"string|required",operation:"string|required",error:"string|required",timestamp:"string|required",metadata:"json"}}));this.errorsResource=s?o:e.resources.error_logs;const[a,c,l]=await K(()=>e.createResource({name:"performance_logs",attributes:{id:"string|required",resourceName:"string|required",operation:"string|required",duration:"number|required",timestamp:"string|required",metadata:"json"}}));this.performanceResource=a?l:e.resources.performance_logs});t||(this.metricsResource=e.resources.metrics,this.errorsResource=e.resources.error_logs,this.performanceResource=e.resources.performance_logs),this.installDatabaseHooks(),this.installMetricsHooks(),"undefined"!=typeof process&&"test"!==process.env.NODE_ENV&&this.startFlushTimer()}async start(){}async stop(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null),this.removeDatabaseHooks()}installDatabaseHooks(){this.database.addHook("afterCreateResource",e=>{"metrics"!==e.name&&"error_logs"!==e.name&&"performance_logs"!==e.name&&this.installResourceHooks(e)})}removeDatabaseHooks(){this.database.removeHook("afterCreateResource",this.installResourceHooks.bind(this))}installMetricsHooks(){for(const e of Object.values(this.database.resources))["metrics","error_logs","performance_logs"].includes(e.name)||this.installResourceHooks(e);this.database._createResource=this.database.createResource,this.database.createResource=async function(...e){const t=await this._createResource(...e);return this.plugins?.metrics&&!["metrics","error_logs","performance_logs"].includes(t.name)&&this.plugins.metrics.installResourceHooks(t),t}}installResourceHooks(e){e._insert=e.insert,e._update=e.update,e._delete=e.delete,e._deleteMany=e.deleteMany,e._get=e.get,e._getMany=e.getMany,e._getAll=e.getAll,e._list=e.list,e._listIds=e.listIds,e._count=e.count,e._page=e.page,e.insert=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._insert(...t));if(this.recordOperation(e.name,"insert",Date.now()-r,!i),i||this.recordError(e.name,"insert",s),!i)throw s;return n}.bind(this),e.update=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._update(...t));if(this.recordOperation(e.name,"update",Date.now()-r,!i),i||this.recordError(e.name,"update",s),!i)throw s;return n}.bind(this),e.delete=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._delete(...t));if(this.recordOperation(e.name,"delete",Date.now()-r,!i),i||this.recordError(e.name,"delete",s),!i)throw s;return n}.bind(this),e.deleteMany=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._deleteMany(...t));if(this.recordOperation(e.name,"delete",Date.now()-r,!i),i||this.recordError(e.name,"delete",s),!i)throw s;return n}.bind(this),e.get=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._get(...t));if(this.recordOperation(e.name,"get",Date.now()-r,!i),i||this.recordError(e.name,"get",s),!i)throw s;return n}.bind(this),e.getMany=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._getMany(...t));if(this.recordOperation(e.name,"get",Date.now()-r,!i),i||this.recordError(e.name,"get",s),!i)throw s;return n}.bind(this),e.getAll=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._getAll(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this),e.list=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._list(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this),e.listIds=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._listIds(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this),e.count=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._count(...t));if(this.recordOperation(e.name,"count",Date.now()-r,!i),i||this.recordError(e.name,"count",s),!i)throw s;return n}.bind(this),e.page=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._page(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this)}recordOperation(e,t,r,i){this.metrics.operations[t]&&(this.metrics.operations[t].count++,this.metrics.operations[t].totalTime+=r,i&&this.metrics.operations[t].errors++),this.metrics.resources[e]||(this.metrics.resources[e]={insert:{count:0,totalTime:0,errors:0},update:{count:0,totalTime:0,errors:0},delete:{count:0,totalTime:0,errors:0},get:{count:0,totalTime:0,errors:0},list:{count:0,totalTime:0,errors:0},count:{count:0,totalTime:0,errors:0}}),this.metrics.resources[e][t]&&(this.metrics.resources[e][t].count++,this.metrics.resources[e][t].totalTime+=r,i&&this.metrics.resources[e][t].errors++),this.config.collectPerformance&&this.metrics.performance.push({resourceName:e,operation:t,duration:r,timestamp:(new Date).toISOString()})}recordError(e,t,r){this.config.collectErrors&&this.metrics.errors.push({resourceName:e,operation:t,error:r.message,stack:r.stack,timestamp:(new Date).toISOString()})}startFlushTimer(){this.flushTimer&&clearInterval(this.flushTimer),this.config.flushInterval>0&&(this.flushTimer=setInterval(()=>{this.flushMetrics().catch(()=>{})},this.config.flushInterval))}async flushMetrics(){if(!this.metricsResource)return;const[e,t]=await K(async()=>{let e,t,r,i;"undefined"!=typeof process&&"test"===process.env.NODE_ENV?(e={},t={},r={},i={}):(e={global:"true"},t={perf:"true"},r={error:"true"},i={resource:"true"});for(const[t,r]of Object.entries(this.metrics.operations))r.count>0&&await this.metricsResource.insert({id:`metrics-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,type:"operation",resourceName:"global",operation:t,count:r.count,totalTime:r.totalTime,errors:r.errors,avgTime:r.count>0?r.totalTime/r.count:0,timestamp:(new Date).toISOString(),metadata:e});for(const[e,t]of Object.entries(this.metrics.resources))for(const[r,s]of Object.entries(t))s.count>0&&await this.metricsResource.insert({id:`metrics-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,type:"operation",resourceName:e,operation:r,count:s.count,totalTime:s.totalTime,errors:s.errors,avgTime:s.count>0?s.totalTime/s.count:0,timestamp:(new Date).toISOString(),metadata:i});if(this.config.collectPerformance&&this.metrics.performance.length>0)for(const e of this.metrics.performance)await this.performanceResource.insert({id:`perf-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,resourceName:e.resourceName,operation:e.operation,duration:e.duration,timestamp:e.timestamp,metadata:t});if(this.config.collectErrors&&this.metrics.errors.length>0)for(const e of this.metrics.errors)await this.errorsResource.insert({id:`error-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,resourceName:e.resourceName,operation:e.operation,error:e.error,stack:e.stack,timestamp:e.timestamp,metadata:r});this.resetMetrics()})}resetMetrics(){for(const e of Object.keys(this.metrics.operations))this.metrics.operations[e]={count:0,totalTime:0,errors:0};for(const e of Object.keys(this.metrics.resources))for(const t of Object.keys(this.metrics.resources[e]))this.metrics.resources[e][t]={count:0,totalTime:0,errors:0};this.metrics.performance=[],this.metrics.errors=[]}async getMetrics(e={}){const{type:t="operation",resourceName:r,operation:i,startDate:s,endDate:n,limit:o=100,offset:a=0}=e;if(!this.metricsResource)return[];let c=(await this.metricsResource.getAll()).filter(e=>(!t||e.type===t)&&((!r||e.resourceName===r)&&((!i||e.operation===i)&&(!(s&&new Date(e.timestamp)<new Date(s))&&!(n&&new Date(e.timestamp)>new Date(n))))));return c.sort((e,t)=>new Date(t.timestamp)-new Date(e.timestamp)),c.slice(a,a+o)}async getErrorLogs(e={}){if(!this.errorsResource)return[];const{resourceName:t,operation:r,startDate:i,endDate:s,limit:n=100,offset:o=0}=e;let a=(await this.errorsResource.getAll()).filter(e=>(!t||e.resourceName===t)&&((!r||e.operation===r)&&(!(i&&new Date(e.timestamp)<new Date(i))&&!(s&&new Date(e.timestamp)>new Date(s)))));return a.sort((e,t)=>new Date(t.timestamp)-new Date(e.timestamp)),a.slice(o,o+n)}async getPerformanceLogs(e={}){if(!this.performanceResource)return[];const{resourceName:t,operation:r,startDate:i,endDate:s,limit:n=100,offset:o=0}=e;let a=(await this.performanceResource.getAll()).filter(e=>(!t||e.resourceName===t)&&((!r||e.operation===r)&&(!(i&&new Date(e.timestamp)<new Date(i))&&!(s&&new Date(e.timestamp)>new Date(s)))));return a.sort((e,t)=>new Date(t.timestamp)-new Date(e.timestamp)),a.slice(o,o+n)}async getStats(){const e=new Date,t=new Date(e.getTime()-864e5),[r,i,s]=await Promise.all([this.getMetrics({startDate:t.toISOString()}),this.getErrorLogs({startDate:t.toISOString()}),this.getPerformanceLogs({startDate:t.toISOString()})]),n={period:"24h",totalOperations:0,totalErrors:i.length,avgResponseTime:0,operationsByType:{},resources:{},uptime:{startTime:this.metrics.startTime,duration:e.getTime()-new Date(this.metrics.startTime).getTime()}};for(const e of r)if("operation"===e.type){n.totalOperations+=e.count,n.operationsByType[e.operation]||(n.operationsByType[e.operation]={count:0,errors:0,avgTime:0}),n.operationsByType[e.operation].count+=e.count,n.operationsByType[e.operation].errors+=e.errors;const t=n.operationsByType[e.operation],r=t.count,i=(t.avgTime*(r-e.count)+e.totalTime)/r;t.avgTime=i}const o=r.reduce((e,t)=>e+t.totalTime,0),a=r.reduce((e,t)=>e+t.count,0);return n.avgResponseTime=a>0?o/a:0,n}async cleanupOldData(){const e=new Date;if(e.setDate(e.getDate()-this.config.retentionDays),this.metricsResource){const t=await this.getMetrics({endDate:e.toISOString()});for(const e of t)await this.metricsResource.delete(e.id)}if(this.errorsResource){const t=await this.getErrorLogs({endDate:e.toISOString()});for(const e of t)await this.errorsResource.delete(e.id)}if(this.performanceResource){const t=await this.getPerformanceLogs({endDate:e.toISOString()});for(const e of t)await this.performanceResource.delete(e.id)}}},e.MissingMetadata=P,e.NoSuchBucket=C,e.NoSuchKey=N,e.NotFound=$,e.PartitionError=z,e.PermissionError=A,e.Plugin=ue,e.PluginObject=he,e.ReplicatorPlugin=class extends ue{constructor(e={}){if(super(),!e.replicators||!Array.isArray(e.replicators))throw new Error("ReplicatorPlugin: replicators array is required");for(const t of e.replicators){if(!t.driver)throw new Error("ReplicatorPlugin: each replicator must have a driver");if(!t.resources||"object"!=typeof t.resources)throw new Error("ReplicatorPlugin: each replicator must have resources config");if(0===Object.keys(t.resources).length)throw new Error("ReplicatorPlugin: each replicator must have at least one resource configured")}this.config={replicators:e.replicators||[],logErrors:!1!==e.logErrors,replicatorLogResource:e.replicatorLogResource||"replicator_log",enabled:!1!==e.enabled,batchSize:e.batchSize||100,maxRetries:e.maxRetries||3,timeout:e.timeout||3e4,verbose:e.verbose||!1,...e},this.replicators=[],this.database=null,this.eventListenersInstalled=new Set}async decompressData(e){return e}filterInternalFields(e){if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))r.startsWith("_")||"$overflow"===r||"$before"===r||"$after"===r||(t[r]=i);return t}async getCompleteData(e,t){const[r,i,s]=await K(()=>e.get(t.id));return r?s:t}installEventListeners(e,t,r){e&&!this.eventListenersInstalled.has(e.name)&&e.name!==this.config.replicatorLogResource&&(e.on("insert",async t=>{const[i,s]=await K(async()=>{const i={...t,createdAt:(new Date).toISOString()};await r.processReplicatorEvent("insert",e.name,i.id,i)});i||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Insert event failed for resource ${e.name}: ${s.message}`),this.emit("error",{operation:"insert",error:s.message,resource:e.name}))}),e.on("update",async(t,i)=>{const[s,n]=await K(async()=>{const s=await r.getCompleteData(e,t),n={...s,updatedAt:(new Date).toISOString()};await r.processReplicatorEvent("update",e.name,s.id,n,i)});s||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Update event failed for resource ${e.name}: ${n.message}`),this.emit("error",{operation:"update",error:n.message,resource:e.name}))}),e.on("delete",async t=>{const[i,s]=await K(async()=>{await r.processReplicatorEvent("delete",e.name,t.id,t)});i||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Delete event failed for resource ${e.name}: ${s.message}`),this.emit("error",{operation:"delete",error:s.message,resource:e.name}))}),this.eventListenersInstalled.add(e.name))}async setup(e){if(this.database=e,this.config.persistReplicatorLog){const[t,r,i]=await K(()=>e.createResource({name:this.config.replicatorLogResource||"replicator_logs",attributes:{id:"string|required",resource:"string|required",action:"string|required",data:"json",timestamp:"number|required",createdAt:"string|required"},behavior:"truncate-data"}));this.replicatorLogResource=t?i:e.resources[this.config.replicatorLogResource||"replicator_logs"]}await this.initializeReplicators(e),this.installDatabaseHooks();for(const t of Object.values(e.resources))t.name!==(this.config.replicatorLogResource||"replicator_logs")&&this.installEventListeners(t,e,this)}async start(){}async stop(){for(const e of this.replicators||[])e&&"function"==typeof e.cleanup&&await e.cleanup();this.removeDatabaseHooks()}installDatabaseHooks(){this.database.addHook("afterCreateResource",e=>{e.name!==(this.config.replicatorLogResource||"replicator_logs")&&this.installEventListeners(e,this.database,this)})}removeDatabaseHooks(){this.database.removeHook("afterCreateResource",this.installEventListeners.bind(this))}createReplicator(e,t,r,i){return function(e,t={},r=[],i=null){const s=Wi[e];if(!s)throw new Error(`Unknown replicator driver: ${e}. Available drivers: ${Object.keys(Wi).join(", ")}`);return new s(t,r,i)}(e,t,r,i)}async initializeReplicators(e){for(const t of this.config.replicators){const{driver:r,config:i={},resources:s,client:n,...o}=t,a=s||i.resources||{},c={...i,...o},l=this.createReplicator(r,c,a,n);l&&(await l.initialize(e),this.replicators.push(l))}}async uploadMetadataFile(e){"function"==typeof e.uploadMetadataFile&&await e.uploadMetadataFile()}async retryWithBackoff(e,t=3){let r;for(let i=1;i<=t;i++){const[s,n]=await K(e);if(s)return s;{if(r=n,this.config.verbose&&console.warn(`[ReplicatorPlugin] Retry attempt ${i}/${t} failed: ${n.message}`),i===t)throw n;const e=1e3*Math.pow(2,i-1);this.config.verbose&&console.warn(`[ReplicatorPlugin] Waiting ${e}ms before retry...`),await new Promise(t=>setTimeout(t,e))}}throw r}async logError(e,t,r,i,s,n){const[o,a]=await K(async()=>{const o=this.config.replicatorLogResource;if(this.database&&this.database.resources&&this.database.resources[o]){const a=this.database.resources[o];await a.insert({replicator:e.name||e.id,resourceName:t,operation:r,recordId:i,data:JSON.stringify(s),error:n.message,timestamp:(new Date).toISOString(),status:"error"})}});o||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to log error for ${t}: ${a.message}`),this.emit("replicator_log_error",{replicator:e.name||e.id,resourceName:t,operation:r,recordId:i,originalError:n.message,logError:a.message}))}async processReplicatorEvent(e,t,r,i,s=null){if(!this.config.enabled)return;const n=this.replicators.filter(r=>r.shouldReplicateResource&&r.shouldReplicateResource(t,e));if(0===n.length)return;const o=n.map(async n=>{const[o,a,c]=await K(async()=>{const o=await this.retryWithBackoff(()=>n.replicate(t,e,i,r,s),this.config.maxRetries);return this.emit("replicated",{replicator:n.name||n.id,resourceName:t,operation:e,recordId:r,result:o,success:!0}),o});if(o)return c;throw this.config.verbose&&console.warn(`[ReplicatorPlugin] Replication failed for ${n.name||n.id} on ${t}: ${a.message}`),this.emit("replicator_error",{replicator:n.name||n.id,resourceName:t,operation:e,recordId:r,error:a.message}),this.config.logErrors&&this.database&&await this.logError(n,t,e,r,i,a),a});return Promise.allSettled(o)}async processreplicatorItem(e){const t=this.replicators.filter(t=>t.shouldReplicateResource&&t.shouldReplicateResource(e.resourceName,e.operation));if(0===t.length)return;const r=t.map(async t=>{const[r,i]=await K(async()=>{const[r,i,s]=await K(()=>t.replicate(e.resourceName,e.operation,e.data,e.recordId,e.beforeData));return r?(this.emit("replicated",{replicator:t.name||t.id,resourceName:e.resourceName,operation:e.operation,recordId:e.recordId,result:s,success:!0}),{success:!0,result:s}):(this.config.verbose&&console.warn(`[ReplicatorPlugin] Replicator item processing failed for ${t.name||t.id} on ${e.resourceName}: ${i.message}`),this.emit("replicator_error",{replicator:t.name||t.id,resourceName:e.resourceName,operation:e.operation,recordId:e.recordId,error:i.message}),this.config.logErrors&&this.database&&await this.logError(t,e.resourceName,e.operation,e.recordId,e.data,i),{success:!1,error:i.message})});return r||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Wrapper processing failed for ${t.name||t.id} on ${e.resourceName}: ${i.message}`),this.emit("replicator_error",{replicator:t.name||t.id,resourceName:e.resourceName,operation:e.operation,recordId:e.recordId,error:i.message}),this.config.logErrors&&this.database&&await this.logError(t,e.resourceName,e.operation,e.recordId,e.data,i),{success:!1,error:i.message})});return Promise.allSettled(r)}async logreplicator(e){const t=this.replicatorLog||this.database.resources[Ji(this.config.replicatorLogResource)];if(!t)return this.database&&this.database.options&&this.database.options.connectionString,void this.emit("replicator.log.failed",{error:"replicator log resource not found",item:e});const r={id:e.id||`repl-${Date.now()}-${Math.random().toString(36).slice(2)}`,resource:e.resource||e.resourceName||"",action:e.operation||e.action||"",data:e.data||{},timestamp:"number"==typeof e.timestamp?e.timestamp:Date.now(),createdAt:e.createdAt||(new Date).toISOString().slice(0,10)},[i,s]=await K(async()=>{await t.insert(r)});i||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to log replicator item: ${s.message}`),this.emit("replicator.log.failed",{error:s,item:e}))}async updatereplicatorLog(e,t){if(!this.replicatorLog)return;const[r,i]=await K(async()=>{await this.replicatorLog.update(e,{...t,lastAttempt:(new Date).toISOString()})});r||this.emit("replicator.updateLog.failed",{error:i.message,logId:e,updates:t})}async getreplicatorStats(){return{replicators:await Promise.all(this.replicators.map(async e=>{const t=await e.getStatus();return{id:e.id,driver:e.driver,config:e.config,status:t}})),queue:{length:this.queue.length,isProcessing:this.isProcessing},stats:this.stats,lastSync:this.stats.lastSync}}async getreplicatorLogs(e={}){if(!this.replicatorLog)return[];const{resourceName:t,operation:r,status:i,limit:s=100,offset:n=0}=e;let o={};t&&(o.resourceName=t),r&&(o.operation=r),i&&(o.status=i);return(await this.replicatorLog.list(o)).slice(n,n+s)}async retryFailedreplicators(){if(!this.replicatorLog)return{retried:0};const e=await this.replicatorLog.list({status:"failed"});let t=0;for(const r of e){const[e,i]=await K(async()=>{await this.processReplicatorEvent(r.resourceName,r.operation,r.recordId,r.data)});e&&t++}return{retried:t}}async syncAllData(e){const t=this.replicators.find(t=>t.id===e);if(!t)throw new Error(`Replicator not found: ${e}`);this.stats.lastSync=(new Date).toISOString();for(const r in this.database.resources)if(Ji(r)!==Ji("replicator_logs")&&t.shouldReplicateResource(r)){this.emit("replicator.sync.resource",{resourceName:r,replicatorId:e});const i=this.database.resources[r],s=await i.getAll();for(const e of s)await t.replicate(r,"insert",e,e.id)}this.emit("replicator.sync.completed",{replicatorId:e,stats:this.stats})}async cleanup(){const[e,t]=await K(async()=>{if(this.replicators&&this.replicators.length>0){const e=this.replicators.map(async e=>{const[t,r]=await K(async()=>{e&&"function"==typeof e.cleanup&&await e.cleanup()});t||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to cleanup replicator ${e.name||e.id}: ${r.message}`),this.emit("replicator_cleanup_error",{replicator:e.name||e.id||"unknown",driver:e.driver||"unknown",error:r.message}))});await Promise.allSettled(e)}this.replicators=[],this.database=null,this.eventListenersInstalled.clear(),this.removeAllListeners()});e||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to cleanup plugin: ${t.message}`),this.emit("replicator_plugin_cleanup_error",{error:t.message}))}},e.Resource=Fi,e.ResourceError=q,e.ResourceIdsPageReader=Yr,e.ResourceIdsReader=Jr,e.ResourceNotFound=D,e.ResourceReader=Qr,e.ResourceWriter=Zr,e.S3db=qi,e.S3dbError=x,e.Schema=wi,e.SchemaError=B,e.UnknownError=T,e.ValidationError=E,e.Validator=gi,e.behaviors=Ti,e.calculateAttributeNamesSize=v,e.calculateAttributeSizes=_,e.calculateEffectiveLimit=R,e.calculateSystemOverhead=O,e.calculateTotalSize=S,e.calculateUTF8Bytes=w,e.decode=g,e.decodeDecimal=b,e.decrypt=Y,e.default=zi,e.encode=m,e.encodeDecimal=y,e.encrypt=G,e.getBehavior=Mi,e.getSizeBreakdown=function(e){const t=_(e),r=v(e),i=Object.values(t).reduce((e,t)=>e+t,0),s=i+r,n=Object.entries(t).sort(([,e],[,t])=>t-e).map(([e,t])=>({attribute:e,size:t,percentage:(t/s*100).toFixed(2)+"%"}));return{total:s,valueSizes:t,namesSize:r,valueTotal:i,breakdown:n,detailedBreakdown:{values:i,names:r,total:s}}},e.idGenerator=X,e.mapAwsError=L,e.md5=Q,e.passwordGenerator=ee,e.sha256=J,e.streamToString=Xr,e.transformValue=k,e.tryFn=H,e.tryFnSync=V,Object.defineProperty(e,"__esModule",{value:!0}),e}({},nanoid,zlib,PromisePool,streams,promises,crypto,_,stringify,nodeHttpHandler,AWS,flat,FastestValidator);
|
|
1
|
+
var S3DB=function(e,t,r,i,s,n,o,a,c,l,u,h,f){"use strict";const d="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",p=Object.fromEntries([...d].map((e,t)=>[e,t])),m=e=>{if("number"!=typeof e||isNaN(e))return"undefined";if(!isFinite(e))return"undefined";if(0===e)return d[0];if(e<0)return"-"+m(-Math.floor(e));e=Math.floor(e);let t="";for(;e;)t=d[e%62]+t,e=Math.floor(e/62);return t},g=e=>{if("string"!=typeof e)return NaN;if(""===e)return 0;let t=!1;"-"===e[0]&&(t=!0,e=e.slice(1));let r=0;for(let t=0;t<e.length;t++){const i=p[e[t]];if(void 0===i)return NaN;r=62*r+i}return t?-r:r},y=e=>{if("number"!=typeof e||isNaN(e))return"undefined";if(!isFinite(e))return"undefined";const t=e<0;e=Math.abs(e);const[r,i]=e.toString().split("."),s=m(Number(r));return i?(t?"-":"")+s+"."+i:(t?"-":"")+s},b=e=>{if("string"!=typeof e)return NaN;let t=!1;"-"===e[0]&&(t=!0,e=e.slice(1));const[r,i]=e.split("."),s=g(r);if(isNaN(s))return NaN;const n=i?Number(s+"."+i):s;return t?-n:n};function w(e){"string"!=typeof e&&(e=String(e));let t=0;for(let r=0;r<e.length;r++){const i=e.codePointAt(r);i<=127?t+=1:i<=2047?t+=2:i<=65535?t+=3:i<=1114111&&(t+=4,i>65535&&r++)}return t}function v(e){let t=0;for(const r of Object.keys(e))t+=w(r);return t}function k(e){return null==e?"":"boolean"==typeof e?e?"1":"0":"number"==typeof e?String(e):"string"==typeof e?e:Array.isArray(e)?0===e.length?"[]":e.map(e=>String(e)).join("|"):"object"==typeof e?JSON.stringify(e):String(e)}function _(e){const t={};for(const[r,i]of Object.entries(e)){const e=w(k(i));t[r]=e}return t}function S(e){const t=_(e);return Object.values(t).reduce((e,t)=>e+t,0)+v(e)}function O(e={}){const{version:t="1",timestamps:r=!1,id:i=""}=e,s={_v:String(t)};r&&(s.createdAt="2024-01-01T00:00:00.000Z",s.updatedAt="2024-01-01T00:00:00.000Z"),i&&(s.id=i);const n={};for(const[e,t]of Object.entries(s))n[e]=t;return S(n)}function R(e={}){const{s3Limit:t=2048,systemConfig:r={}}=e;return t-O(r)}class j extends Error{constructor({verbose:e,bucket:t,key:r,message:i,code:s,statusCode:n,requestId:o,awsMessage:a,original:c,commandName:l,commandInput:u,metadata:h,suggestion:f,...d}){e&&(i+=`\n\nVerbose:\n\n${JSON.stringify(d,null,2)}`),super(i),"function"==typeof Error.captureStackTrace?Error.captureStackTrace(this,this.constructor):this.stack=new Error(i).stack,super.name=this.constructor.name,this.name=this.constructor.name,this.bucket=t,this.key=r,this.thrownAt=new Date,this.code=s,this.statusCode=n,this.requestId=o,this.awsMessage=a,this.original=c,this.commandName=l,this.commandInput=u,this.metadata=h,this.suggestion=f,this.data={bucket:t,key:r,...d,verbose:e,message:i}}toJson(){return{name:this.name,message:this.message,code:this.code,statusCode:this.statusCode,requestId:this.requestId,awsMessage:this.awsMessage,bucket:this.bucket,key:this.key,thrownAt:this.thrownAt,commandName:this.commandName,commandInput:this.commandInput,metadata:this.metadata,suggestion:this.suggestion,data:this.data,original:this.original,stack:this.stack}}toString(){return`${this.name} | ${this.message}`}}class x extends j{constructor(e,t={}){let r,i,s,n,o,a;t.original&&(o=t.original,r=o.code||o.Code||o.name,i=o.statusCode||o.$metadata&&o.$metadata.httpStatusCode,s=o.requestId||o.$metadata&&o.$metadata.requestId,n=o.message,a=o.$metadata?{...o.$metadata}:void 0),super({message:e,...t,code:r,statusCode:i,requestId:s,awsMessage:n,original:o,metadata:a})}}class E extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}}class A extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}}class D extends x{constructor({bucket:e,resourceName:t,id:r,original:i,...s}){if("string"!=typeof r)throw new Error("id must be a string");if("string"!=typeof e)throw new Error("bucket must be a string");if("string"!=typeof t)throw new Error("resourceName must be a string");super(`Resource not found: ${t}/${r} [bucket:${e}]`,{bucket:e,resourceName:t,id:r,original:i,...s})}}class C extends x{constructor({bucket:e,original:t,...r}){if("string"!=typeof e)throw new Error("bucket must be a string");super(`Bucket does not exists [bucket:${e}]`,{bucket:e,original:t,...r})}}class N extends x{constructor({bucket:e,key:t,resourceName:r,id:i,original:s,...n}){if("string"!=typeof t)throw new Error("key must be a string");if("string"!=typeof e)throw new Error("bucket must be a string");if(void 0!==i&&"string"!=typeof i)throw new Error("id must be a string");super(`No such key: ${t} [bucket:${e}]`,{bucket:e,key:t,resourceName:r,id:i,original:s,...n}),this.resourceName=r,this.id=i}}class $ extends x{constructor({bucket:e,key:t,resourceName:r,id:i,original:s,...n}){if("string"!=typeof t)throw new Error("key must be a string");if("string"!=typeof e)throw new Error("bucket must be a string");super(`Not found: ${t} [bucket:${e}]`,{bucket:e,key:t,resourceName:r,id:i,original:s,...n}),this.resourceName=r,this.id=i}}class P extends x{constructor({bucket:e,original:t,...r}){if("string"!=typeof e)throw new Error("bucket must be a string");super(`Missing metadata for bucket [bucket:${e}]`,{bucket:e,original:t,...r})}}class I extends x{constructor({bucket:e,resourceName:t,attributes:r,validation:i,message:s,original:n,...o}){if("string"!=typeof e)throw new Error("bucket must be a string");if("string"!=typeof t)throw new Error("resourceName must be a string");super(s||`Validation error: This item is not valid. Resource=${t} [bucket:${e}].\n${JSON.stringify(i,null,2)}`,{bucket:e,resourceName:t,attributes:r,validation:i,original:n,...o})}}class T extends x{}const M={NotFound:$,NoSuchKey:N,UnknownError:T,NoSuchBucket:C,MissingMetadata:P,InvalidResourceItem:I};function L(e,t={}){const r=e.code||e.Code||e.name,i=e.$metadata?{...e.$metadata}:void 0,s=t.commandName,n=t.commandInput;let o;return"NoSuchKey"===r||"NotFound"===r?(o="Check if the key exists in the specified bucket and if your credentials have permission.",new N({...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"NoSuchBucket"===r?(o="Check if the bucket exists and if your credentials have permission.",new C({...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"AccessDenied"===r||403===e.statusCode||"Forbidden"===r?(o="Check your credentials and bucket policy.",new A("Access denied",{...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"ValidationError"===r||400===e.statusCode?(o="Check the request parameters and payload.",new E("Validation error",{...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):"MissingMetadata"===r?(o="Check if the object metadata is present and valid.",new P({...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o})):(o="Check the error details and AWS documentation.",new T("Unknown error",{...t,original:e,metadata:i,commandName:s,commandInput:n,suggestion:o}))}class U extends x{constructor(e,t={}){super(e,{...t,suggestion:"Check the connection string format and credentials."})}}class F extends x{constructor(e,t={}){super(e,{...t,suggestion:"Check if the crypto library is available and input is valid."})}}class B extends x{constructor(e,t={}){super(e,{...t,suggestion:"Check schema definition and input data."})}}class z extends x{constructor(e,t={}){super(e,{...t,suggestion:t.suggestion||"Check resource configuration, attributes, and operation context."}),Object.assign(this,t)}}class q extends x{constructor(e,t={}){super(e,{...t,suggestion:t.suggestion||"Check partition definition, fields, and input values."})}}function H(e){if(null==e){const e=new Error("fnOrPromise cannot be null or undefined");return e.stack=(new Error).stack,[!1,e,void 0]}if("function"==typeof e)try{const t=e();return null==t?[!0,null,t]:"function"==typeof t.then?t.then(e=>[!0,null,e]).catch(e=>{if(e instanceof Error&&Object.isExtensible(e)){const t=Object.getOwnPropertyDescriptor(e,"stack");if(t&&t.writable&&t.configurable&&e.hasOwnProperty("stack"))try{e.stack=(new Error).stack}catch(e){}}return[!1,e,void 0]}):[!0,null,t]}catch(e){if(e instanceof Error&&Object.isExtensible(e)){const t=Object.getOwnPropertyDescriptor(e,"stack");if(t&&t.writable&&t.configurable&&e.hasOwnProperty("stack"))try{e.stack=(new Error).stack}catch(e){}}return[!1,e,void 0]}return"function"==typeof e.then?Promise.resolve(e).then(e=>[!0,null,e]).catch(e=>{if(e instanceof Error&&Object.isExtensible(e)){const t=Object.getOwnPropertyDescriptor(e,"stack");if(t&&t.writable&&t.configurable&&e.hasOwnProperty("stack"))try{e.stack=(new Error).stack}catch(e){}}return[!1,e,void 0]}):[!0,null,e]}function V(e){try{return[!0,null,e()]}catch(e){return[!1,e,null]}}var K=H;async function W(){let e;if("undefined"!=typeof process){const[t,r,i]=await K(async()=>{const{webcrypto:e}=await import("crypto");return e});if(!t)throw new F("Crypto API not available",{original:r,context:"dynamicCrypto"});e=i}else"undefined"!=typeof window&&(e=window.crypto);if(!e)throw new F("Could not load any crypto library",{context:"dynamicCrypto"});return e}async function J(e){const[t,r,i]=await K(W);if(!t)throw new F("Crypto API not available",{original:r});const s=(new TextEncoder).encode(e),[n,o,a]=await K(()=>i.subtle.digest("SHA-256",s));if(!n)throw new F("SHA-256 digest failed",{original:o,input:e});return Array.from(new Uint8Array(a)).map(e=>e.toString(16).padStart(2,"0")).join("")}async function G(e,t){const[r,i,s]=await K(W);if(!r)throw new F("Crypto API not available",{original:i});const n=s.getRandomValues(new Uint8Array(16)),[o,a,c]=await K(()=>Z(t,n));if(!o)throw new F("Key derivation failed",{original:a,passphrase:t,salt:n});const l=s.getRandomValues(new Uint8Array(12)),u=(new TextEncoder).encode(e),[h,f,d]=await K(()=>s.subtle.encrypt({name:"AES-GCM",iv:l},c,u));if(!h)throw new F("Encryption failed",{original:f,content:e});const p=new Uint8Array(n.length+l.length+d.byteLength);return p.set(n),p.set(l,n.length),p.set(new Uint8Array(d),n.length+l.length),function(e){if("undefined"!=typeof process)return Buffer.from(e).toString("base64");{const[t,r,i]=V(()=>String.fromCharCode.apply(null,new Uint8Array(e)));if(!t)throw new F("Failed to convert ArrayBuffer to base64 (browser)",{original:r});return window.btoa(i)}}(p)}async function Y(e,t){const[r,i,s]=await K(W);if(!r)throw new F("Crypto API not available",{original:i});const n=function(e){if("undefined"!=typeof process)return new Uint8Array(Buffer.from(e,"base64"));{const[t,r,i]=V(()=>window.atob(e));if(!t)throw new F("Failed to decode base64 (browser)",{original:r});const s=i.length,n=new Uint8Array(s);for(let e=0;e<s;e++)n[e]=i.charCodeAt(e);return n}}(e),o=n.slice(0,16),a=n.slice(16,28),c=n.slice(28),[l,u,h]=await K(()=>Z(t,o));if(!l)throw new F("Key derivation failed (decrypt)",{original:u,passphrase:t,salt:o});const[f,d,p]=await K(()=>s.subtle.decrypt({name:"AES-GCM",iv:a},h,c));if(!f)throw new F("Decryption failed",{original:d,encryptedBase64:e});return(new TextDecoder).decode(p)}async function Q(e){if("undefined"==typeof process)throw new F("MD5 hashing is only available in Node.js environment",{context:"md5"});const[t,r,i]=await K(async()=>{const{createHash:t}=await import("crypto");return t("md5").update(e).digest("base64")});if(!t)throw new F("MD5 hashing failed",{original:r,data:e});return i}async function Z(e,t){const[r,i,s]=await K(W);if(!r)throw new F("Crypto API not available",{original:i});const n=(new TextEncoder).encode(e),[o,a,c]=await K(()=>s.subtle.importKey("raw",n,{name:"PBKDF2"},!1,["deriveKey"]));if(!o)throw new F("importKey failed",{original:a,passphrase:e});const[l,u,h]=await K(()=>s.subtle.deriveKey({name:"PBKDF2",salt:t,iterations:1e5,hash:"SHA-256"},c,{name:"AES-GCM",length:256},!0,["encrypt","decrypt"]));if(!l)throw new F("deriveKey failed",{original:u,passphrase:e,salt:t});return h}const X=t.customAlphabet(t.urlAlphabet,22),ee=t.customAlphabet("ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789",16);var te,re=Object.freeze({__proto__:null,idGenerator:X,passwordGenerator:ee});function ie(){}function se(){se.init.call(this)}function ne(e){return void 0===e._maxListeners?se.defaultMaxListeners:e._maxListeners}function oe(e,t,r,i){var s,n,o,a;if("function"!=typeof r)throw new TypeError('"listener" argument must be a function');if((n=e._events)?(n.newListener&&(e.emit("newListener",t,r.listener?r.listener:r),n=e._events),o=n[t]):(n=e._events=new ie,e._eventsCount=0),o){if("function"==typeof o?o=n[t]=i?[r,o]:[o,r]:i?o.unshift(r):o.push(r),!o.warned&&(s=ne(e))&&s>0&&o.length>s){o.warned=!0;var c=new Error("Possible EventEmitter memory leak detected. "+o.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");c.name="MaxListenersExceededWarning",c.emitter=e,c.type=t,c.count=o.length,a=c,"function"==typeof console.warn?console.warn(a):console.log(a)}}else o=n[t]=r,++e._eventsCount;return e}function ae(e,t,r){var i=!1;function s(){e.removeListener(t,s),i||(i=!0,r.apply(e,arguments))}return s.listener=r,s}function ce(e){var t=this._events;if(t){var r=t[e];if("function"==typeof r)return 1;if(r)return r.length}return 0}function le(e,t){for(var r=new Array(t);t--;)r[t]=e[t];return r}ie.prototype=Object.create(null),se.EventEmitter=se,se.usingDomains=!1,se.prototype.domain=void 0,se.prototype._events=void 0,se.prototype._maxListeners=void 0,se.defaultMaxListeners=10,se.init=function(){this.domain=null,se.usingDomains&&(!te.active||this instanceof te.Domain||(this.domain=te.active)),this._events&&this._events!==Object.getPrototypeOf(this)._events||(this._events=new ie,this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},se.prototype.setMaxListeners=function(e){if("number"!=typeof e||e<0||isNaN(e))throw new TypeError('"n" argument must be a positive number');return this._maxListeners=e,this},se.prototype.getMaxListeners=function(){return ne(this)},se.prototype.emit=function(e){var t,r,i,s,n,o,a,c="error"===e;if(o=this._events)c=c&&null==o.error;else if(!c)return!1;if(a=this.domain,c){if(t=arguments[1],!a){if(t instanceof Error)throw t;var l=new Error('Uncaught, unspecified "error" event. ('+t+")");throw l.context=t,l}return t||(t=new Error('Uncaught, unspecified "error" event')),t.domainEmitter=this,t.domain=a,t.domainThrown=!1,a.emit("error",t),!1}if(!(r=o[e]))return!1;var u="function"==typeof r;switch(i=arguments.length){case 1:!function(e,t,r){if(t)e.call(r);else for(var i=e.length,s=le(e,i),n=0;n<i;++n)s[n].call(r)}(r,u,this);break;case 2:!function(e,t,r,i){if(t)e.call(r,i);else for(var s=e.length,n=le(e,s),o=0;o<s;++o)n[o].call(r,i)}(r,u,this,arguments[1]);break;case 3:!function(e,t,r,i,s){if(t)e.call(r,i,s);else for(var n=e.length,o=le(e,n),a=0;a<n;++a)o[a].call(r,i,s)}(r,u,this,arguments[1],arguments[2]);break;case 4:!function(e,t,r,i,s,n){if(t)e.call(r,i,s,n);else for(var o=e.length,a=le(e,o),c=0;c<o;++c)a[c].call(r,i,s,n)}(r,u,this,arguments[1],arguments[2],arguments[3]);break;default:for(s=new Array(i-1),n=1;n<i;n++)s[n-1]=arguments[n];!function(e,t,r,i){if(t)e.apply(r,i);else for(var s=e.length,n=le(e,s),o=0;o<s;++o)n[o].apply(r,i)}(r,u,this,s)}return!0},se.prototype.addListener=function(e,t){return oe(this,e,t,!1)},se.prototype.on=se.prototype.addListener,se.prototype.prependListener=function(e,t){return oe(this,e,t,!0)},se.prototype.once=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.on(e,ae(this,e,t)),this},se.prototype.prependOnceListener=function(e,t){if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');return this.prependListener(e,ae(this,e,t)),this},se.prototype.removeListener=function(e,t){var r,i,s,n,o;if("function"!=typeof t)throw new TypeError('"listener" argument must be a function');if(!(i=this._events))return this;if(!(r=i[e]))return this;if(r===t||r.listener&&r.listener===t)0===--this._eventsCount?this._events=new ie:(delete i[e],i.removeListener&&this.emit("removeListener",e,r.listener||t));else if("function"!=typeof r){for(s=-1,n=r.length;n-- >0;)if(r[n]===t||r[n].listener&&r[n].listener===t){o=r[n].listener,s=n;break}if(s<0)return this;if(1===r.length){if(r[0]=void 0,0===--this._eventsCount)return this._events=new ie,this;delete i[e]}else!function(e,t){for(var r=t,i=r+1,s=e.length;i<s;r+=1,i+=1)e[r]=e[i];e.pop()}(r,s);i.removeListener&&this.emit("removeListener",e,o||t)}return this},se.prototype.off=function(e,t){return this.removeListener(e,t)},se.prototype.removeAllListeners=function(e){var t,r;if(!(r=this._events))return this;if(!r.removeListener)return 0===arguments.length?(this._events=new ie,this._eventsCount=0):r[e]&&(0===--this._eventsCount?this._events=new ie:delete r[e]),this;if(0===arguments.length){for(var i,s=Object.keys(r),n=0;n<s.length;++n)"removeListener"!==(i=s[n])&&this.removeAllListeners(i);return this.removeAllListeners("removeListener"),this._events=new ie,this._eventsCount=0,this}if("function"==typeof(t=r[e]))this.removeListener(e,t);else if(t)do{this.removeListener(e,t[t.length-1])}while(t[0]);return this},se.prototype.listeners=function(e){var t,r=this._events;return r&&(t=r[e])?"function"==typeof t?[t.listener||t]:function(e){for(var t=new Array(e.length),r=0;r<t.length;++r)t[r]=e[r].listener||e[r];return t}(t):[]},se.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):ce.call(e,t)},se.prototype.listenerCount=ce,se.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]};var ue=class extends se{constructor(e={}){super(),this.name=this.constructor.name,this.options=e,this.hooks=new Map}async setup(e){this.database=e,this.beforeSetup(),await this.onSetup(),this.afterSetup()}async start(){this.beforeStart(),await this.onStart(),this.afterStart()}async stop(){this.beforeStop(),await this.onStop(),this.afterStop()}async onSetup(){}async onStart(){}async onStop(){}addHook(e,t,r){this.hooks.has(e)||this.hooks.set(e,new Map);const i=this.hooks.get(e);i.has(t)||i.set(t,[]),i.get(t).push(r)}removeHook(e,t,r){const i=this.hooks.get(e);if(i&&i.has(t)){const e=i.get(t),s=e.indexOf(r);s>-1&&e.splice(s,1)}}wrapResourceMethod(e,t,r){const i=e[t];if(e._pluginWrappers||(e._pluginWrappers=new Map),e._pluginWrappers.has(t)||e._pluginWrappers.set(t,[]),e._pluginWrappers.get(t).push(r),!e[`_wrapped_${t}`]){e[`_wrapped_${t}`]=i;const r=i&&i._isMockFunction;e[t]=async function(...r){let i=await e[`_wrapped_${t}`](...r);for(const s of e._pluginWrappers.get(t))i=await s.call(this,i,r,t);return i},r&&(Object.setPrototypeOf(e[t],Object.getPrototypeOf(i)),Object.assign(e[t],i))}}addMiddleware(e,t,r){if(e._pluginMiddlewares||(e._pluginMiddlewares={}),!e._pluginMiddlewares[t]){e._pluginMiddlewares[t]=[];const r=e[t].bind(e);e[t]=async function(...i){let s=-1;const n=async(...i)=>(s++,s<e._pluginMiddlewares[t].length?await e._pluginMiddlewares[t][s].call(this,n,...i):await r(...i));return await n(...i)}}e._pluginMiddlewares[t].push(r)}getPartitionValues(e,t){if(!t.config?.partitions)return{};const r={};for(const[i,s]of Object.entries(t.config.partitions))if(s.fields){r[i]={};for(const[n,o]of Object.entries(s.fields)){const s=this.getNestedFieldValue(e,n);null!=s&&(r[i][n]=t.applyPartitionRule(s,o))}}else r[i]={};return r}getNestedFieldValue(e,t){if(!t.includes("."))return e[t]??null;const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return null;i=i[e]}return i??null}beforeSetup(){this.emit("plugin.beforeSetup",new Date)}afterSetup(){this.emit("plugin.afterSetup",new Date)}beforeStart(){this.emit("plugin.beforeStart",new Date)}afterStart(){this.emit("plugin.afterStart",new Date)}beforeStop(){this.emit("plugin.beforeStop",new Date)}afterStop(){this.emit("plugin.afterStop",new Date)}};const he={setup(e){},start(){},stop(){}};function fe(e,t){for(var r=0,i=e.length-1;i>=0;i--){var s=e[i];"."===s?e.splice(i,1):".."===s?(e.splice(i,1),r++):r&&(e.splice(i,1),r--)}if(t)for(;r--;r)e.unshift("..");return e}var de=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,pe=function(e){return de.exec(e).slice(1)};function me(){for(var e="",t=!1,r=arguments.length-1;r>=-1&&!t;r--){var i=r>=0?arguments[r]:"/";if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(e=i+"/"+e,t="/"===i.charAt(0))}return(t?"/":"")+(e=fe(ve(e.split("/"),function(e){return!!e}),!t).join("/"))||"."}function ge(e){var t=ye(e),r="/"===ke(e,-1);return(e=fe(ve(e.split("/"),function(e){return!!e}),!t).join("/"))||t||(e="."),e&&r&&(e+="/"),(t?"/":"")+e}function ye(e){return"/"===e.charAt(0)}function be(){return ge(ve(Array.prototype.slice.call(arguments,0),function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e}).join("/"))}var we={extname:function(e){return pe(e)[3]},basename:function(e,t){var r=pe(e)[2];return t&&r.substr(-1*t.length)===t&&(r=r.substr(0,r.length-t.length)),r},dirname:function(e){var t=pe(e),r=t[0],i=t[1];return r||i?(i&&(i=i.substr(0,i.length-1)),r+i):"."},sep:"/",delimiter:":",relative:function(e,t){function r(e){for(var t=0;t<e.length&&""===e[t];t++);for(var r=e.length-1;r>=0&&""===e[r];r--);return t>r?[]:e.slice(t,r-t+1)}e=me(e).substr(1),t=me(t).substr(1);for(var i=r(e.split("/")),s=r(t.split("/")),n=Math.min(i.length,s.length),o=n,a=0;a<n;a++)if(i[a]!==s[a]){o=a;break}var c=[];for(a=o;a<i.length;a++)c.push("..");return(c=c.concat(s.slice(o))).join("/")},join:be,isAbsolute:ye,normalize:ge,resolve:me};function ve(e,t){if(e.filter)return e.filter(t);for(var r=[],i=0;i<e.length;i++)t(e[i],i,e)&&r.push(e[i]);return r}var ke="b"==="ab".substr(-1)?function(e,t,r){return e.substr(t,r)}:function(e,t,r){return t<0&&(t=e.length+t),e.substr(t,r)};class _e extends se{constructor(e={}){super(),this.config=e}async _set(e,t){}async _get(e){}async _del(e){}async _clear(e){}validateKey(e){if(null==e||"string"!=typeof e||!e)throw new Error("Invalid key")}async set(e,t){return this.validateKey(e),await this._set(e,t),this.emit("set",t),t}async get(e){this.validateKey(e);const t=await this._get(e);return this.emit("get",t),t}async del(e){this.validateKey(e);const t=await this._del(e);return this.emit("delete",t),t}async delete(e){return this.del(e)}async clear(e){const t=await this._clear(e);return this.emit("clear",t),t}}function Se(){throw new Error("setTimeout has not been defined")}function Oe(){throw new Error("clearTimeout has not been defined")}var Re=Se,je=Oe;function xe(e){if(Re===setTimeout)return setTimeout(e,0);if((Re===Se||!Re)&&setTimeout)return Re=setTimeout,setTimeout(e,0);try{return Re(e,0)}catch(t){try{return Re.call(null,e,0)}catch(t){return Re.call(this,e,0)}}}"function"==typeof global.setTimeout&&(Re=setTimeout),"function"==typeof global.clearTimeout&&(je=clearTimeout);var Ee,Ae=[],De=!1,Ce=-1;function Ne(){De&&Ee&&(De=!1,Ee.length?Ae=Ee.concat(Ae):Ce=-1,Ae.length&&$e())}function $e(){if(!De){var e=xe(Ne);De=!0;for(var t=Ae.length;t;){for(Ee=Ae,Ae=[];++Ce<t;)Ee&&Ee[Ce].run();Ce=-1,t=Ae.length}Ee=null,De=!1,function(e){if(je===clearTimeout)return clearTimeout(e);if((je===Oe||!je)&&clearTimeout)return je=clearTimeout,clearTimeout(e);try{return je(e)}catch(t){try{return je.call(null,e)}catch(t){return je.call(this,e)}}}(e)}}function Pe(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];Ae.push(new Ie(e,t)),1!==Ae.length||De||xe($e)}function Ie(e,t){this.fun=e,this.array=t}Ie.prototype.run=function(){this.fun.apply(null,this.array)};var Te,Me={env:{}};Te="function"==typeof Object.create?function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})}:function(e,t){e.super_=t;var r=function(){};r.prototype=t.prototype,e.prototype=new r,e.prototype.constructor=e};var Le=/%[sdj%]/g;function Ue(e){if(!Qe(e)){for(var t=[],r=0;r<arguments.length;r++)t.push(qe(arguments[r]));return t.join(" ")}r=1;for(var i=arguments,s=i.length,n=String(e).replace(Le,function(e){if("%%"===e)return"%";if(r>=s)return e;switch(e){case"%s":return String(i[r++]);case"%d":return Number(i[r++]);case"%j":try{return JSON.stringify(i[r++])}catch(e){return"[Circular]"}default:return e}}),o=i[r];r<s;o=i[++r])Ye(o)||!et(o)?n+=" "+o:n+=" "+qe(o);return n}function Fe(e,t){if(Ze(global.process))return function(){return Fe(e,t).apply(this,arguments)};if(!0===Me.noDeprecation)return e;var r=!1;return function(){if(!r){if(Me.throwDeprecation)throw new Error(t);Me.traceDeprecation?console.trace(t):console.error(t),r=!0}return e.apply(this,arguments)}}var Be,ze={};function qe(e,t){var r={seen:[],stylize:Ve};return arguments.length>=3&&(r.depth=arguments[2]),arguments.length>=4&&(r.colors=arguments[3]),Ge(t)?r.showHidden=t:t&&function(e,t){if(!t||!et(t))return e;var r=Object.keys(t),i=r.length;for(;i--;)e[r[i]]=t[r[i]]}(r,t),Ze(r.showHidden)&&(r.showHidden=!1),Ze(r.depth)&&(r.depth=2),Ze(r.colors)&&(r.colors=!1),Ze(r.customInspect)&&(r.customInspect=!0),r.colors&&(r.stylize=He),Ke(r,e,r.depth)}function He(e,t){var r=qe.styles[t];return r?"["+qe.colors[r][0]+"m"+e+"["+qe.colors[r][1]+"m":e}function Ve(e,t){return e}function Ke(e,t,r){if(e.customInspect&&t&&it(t.inspect)&&t.inspect!==qe&&(!t.constructor||t.constructor.prototype!==t)){var i=t.inspect(r,e);return Qe(i)||(i=Ke(e,i,r)),i}var s=function(e,t){if(Ze(t))return e.stylize("undefined","undefined");if(Qe(t)){var r="'"+JSON.stringify(t).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return e.stylize(r,"string")}if(i=t,"number"==typeof i)return e.stylize(""+t,"number");var i;if(Ge(t))return e.stylize(""+t,"boolean");if(Ye(t))return e.stylize("null","null")}(e,t);if(s)return s;var n=Object.keys(t),o=function(e){var t={};return e.forEach(function(e,r){t[e]=!0}),t}(n);if(e.showHidden&&(n=Object.getOwnPropertyNames(t)),rt(t)&&(n.indexOf("message")>=0||n.indexOf("description")>=0))return We(t);if(0===n.length){if(it(t)){var a=t.name?": "+t.name:"";return e.stylize("[Function"+a+"]","special")}if(Xe(t))return e.stylize(RegExp.prototype.toString.call(t),"regexp");if(tt(t))return e.stylize(Date.prototype.toString.call(t),"date");if(rt(t))return We(t)}var c,l,u="",h=!1,f=["{","}"];(c=t,Array.isArray(c)&&(h=!0,f=["[","]"]),it(t))&&(u=" [Function"+(t.name?": "+t.name:"")+"]");return Xe(t)&&(u=" "+RegExp.prototype.toString.call(t)),tt(t)&&(u=" "+Date.prototype.toUTCString.call(t)),rt(t)&&(u=" "+We(t)),0!==n.length||h&&0!=t.length?r<0?Xe(t)?e.stylize(RegExp.prototype.toString.call(t),"regexp"):e.stylize("[Object]","special"):(e.seen.push(t),l=h?function(e,t,r,i,s){for(var n=[],o=0,a=t.length;o<a;++o)nt(t,String(o))?n.push(Je(e,t,r,i,String(o),!0)):n.push("");return s.forEach(function(s){s.match(/^\d+$/)||n.push(Je(e,t,r,i,s,!0))}),n}(e,t,r,o,n):n.map(function(i){return Je(e,t,r,o,i,h)}),e.seen.pop(),function(e,t,r){var i=e.reduce(function(e,t){return t.indexOf("\n"),e+t.replace(/\u001b\[\d\d?m/g,"").length+1},0);if(i>60)return r[0]+(""===t?"":t+"\n ")+" "+e.join(",\n ")+" "+r[1];return r[0]+t+" "+e.join(", ")+" "+r[1]}(l,u,f)):f[0]+u+f[1]}function We(e){return"["+Error.prototype.toString.call(e)+"]"}function Je(e,t,r,i,s,n){var o,a,c;if((c=Object.getOwnPropertyDescriptor(t,s)||{value:t[s]}).get?a=c.set?e.stylize("[Getter/Setter]","special"):e.stylize("[Getter]","special"):c.set&&(a=e.stylize("[Setter]","special")),nt(i,s)||(o="["+s+"]"),a||(e.seen.indexOf(c.value)<0?(a=Ye(r)?Ke(e,c.value,null):Ke(e,c.value,r-1)).indexOf("\n")>-1&&(a=n?a.split("\n").map(function(e){return" "+e}).join("\n").substr(2):"\n"+a.split("\n").map(function(e){return" "+e}).join("\n")):a=e.stylize("[Circular]","special")),Ze(o)){if(n&&s.match(/^\d+$/))return a;(o=JSON.stringify(""+s)).match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)?(o=o.substr(1,o.length-2),o=e.stylize(o,"name")):(o=o.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'"),o=e.stylize(o,"string"))}return o+": "+a}function Ge(e){return"boolean"==typeof e}function Ye(e){return null===e}function Qe(e){return"string"==typeof e}function Ze(e){return void 0===e}function Xe(e){return et(e)&&"[object RegExp]"===st(e)}function et(e){return"object"==typeof e&&null!==e}function tt(e){return et(e)&&"[object Date]"===st(e)}function rt(e){return et(e)&&("[object Error]"===st(e)||e instanceof Error)}function it(e){return"function"==typeof e}function st(e){return Object.prototype.toString.call(e)}function nt(e,t){return Object.prototype.hasOwnProperty.call(e,t)}qe.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]},qe.styles={special:"cyan",number:"yellow",boolean:"yellow",undefined:"grey",null:"bold",string:"green",date:"magenta",regexp:"red"};var ot=[],at=[],ct="undefined"!=typeof Uint8Array?Uint8Array:Array,lt=!1;function ut(){lt=!0;for(var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",t=0;t<64;++t)ot[t]=e[t],at[e.charCodeAt(t)]=t;at["-".charCodeAt(0)]=62,at["_".charCodeAt(0)]=63}function ht(e){return ot[e>>18&63]+ot[e>>12&63]+ot[e>>6&63]+ot[63&e]}function ft(e,t,r){for(var i,s=[],n=t;n<r;n+=3)i=(e[n]<<16)+(e[n+1]<<8)+e[n+2],s.push(ht(i));return s.join("")}function dt(e){var t;lt||ut();for(var r=e.length,i=r%3,s="",n=[],o=16383,a=0,c=r-i;a<c;a+=o)n.push(ft(e,a,a+o>c?c:a+o));return 1===i?(t=e[r-1],s+=ot[t>>2],s+=ot[t<<4&63],s+="=="):2===i&&(t=(e[r-2]<<8)+e[r-1],s+=ot[t>>10],s+=ot[t>>4&63],s+=ot[t<<2&63],s+="="),n.push(s),n.join("")}function pt(e,t,r,i,s){var n,o,a=8*s-i-1,c=(1<<a)-1,l=c>>1,u=-7,h=r?s-1:0,f=r?-1:1,d=e[t+h];for(h+=f,n=d&(1<<-u)-1,d>>=-u,u+=a;u>0;n=256*n+e[t+h],h+=f,u-=8);for(o=n&(1<<-u)-1,n>>=-u,u+=i;u>0;o=256*o+e[t+h],h+=f,u-=8);if(0===n)n=1-l;else{if(n===c)return o?NaN:1/0*(d?-1:1);o+=Math.pow(2,i),n-=l}return(d?-1:1)*o*Math.pow(2,n-i)}function mt(e,t,r,i,s,n){var o,a,c,l=8*n-s-1,u=(1<<l)-1,h=u>>1,f=23===s?Math.pow(2,-24)-Math.pow(2,-77):0,d=i?0:n-1,p=i?1:-1,m=t<0||0===t&&1/t<0?1:0;for(t=Math.abs(t),isNaN(t)||t===1/0?(a=isNaN(t)?1:0,o=u):(o=Math.floor(Math.log(t)/Math.LN2),t*(c=Math.pow(2,-o))<1&&(o--,c*=2),(t+=o+h>=1?f/c:f*Math.pow(2,1-h))*c>=2&&(o++,c/=2),o+h>=u?(a=0,o=u):o+h>=1?(a=(t*c-1)*Math.pow(2,s),o+=h):(a=t*Math.pow(2,h-1)*Math.pow(2,s),o=0));s>=8;e[r+d]=255&a,d+=p,a/=256,s-=8);for(o=o<<s|a,l+=s;l>0;e[r+d]=255&o,d+=p,o/=256,l-=8);e[r+d-p]|=128*m}var gt={}.toString,yt=Array.isArray||function(e){return"[object Array]"==gt.call(e)};function bt(){return vt.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function wt(e,t){if(bt()<t)throw new RangeError("Invalid typed array length");return vt.TYPED_ARRAY_SUPPORT?(e=new Uint8Array(t)).__proto__=vt.prototype:(null===e&&(e=new vt(t)),e.length=t),e}function vt(e,t,r){if(!(vt.TYPED_ARRAY_SUPPORT||this instanceof vt))return new vt(e,t,r);if("number"==typeof e){if("string"==typeof t)throw new Error("If encoding is specified then the first argument must be a string");return St(this,e)}return kt(this,e,t,r)}function kt(e,t,r,i){if("number"==typeof t)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&t instanceof ArrayBuffer?function(e,t,r,i){if(t.byteLength,r<0||t.byteLength<r)throw new RangeError("'offset' is out of bounds");if(t.byteLength<r+(i||0))throw new RangeError("'length' is out of bounds");t=void 0===r&&void 0===i?new Uint8Array(t):void 0===i?new Uint8Array(t,r):new Uint8Array(t,r,i);vt.TYPED_ARRAY_SUPPORT?(e=t).__proto__=vt.prototype:e=Ot(e,t);return e}(e,t,r,i):"string"==typeof t?function(e,t,r){"string"==typeof r&&""!==r||(r="utf8");if(!vt.isEncoding(r))throw new TypeError('"encoding" must be a valid string encoding');var i=0|xt(t,r);e=wt(e,i);var s=e.write(t,r);s!==i&&(e=e.slice(0,s));return e}(e,t,r):function(e,t){if(jt(t)){var r=0|Rt(t.length);return 0===(e=wt(e,r)).length||t.copy(e,0,0,r),e}if(t){if("undefined"!=typeof ArrayBuffer&&t.buffer instanceof ArrayBuffer||"length"in t)return"number"!=typeof t.length||(i=t.length)!=i?wt(e,0):Ot(e,t);if("Buffer"===t.type&&yt(t.data))return Ot(e,t.data)}var i;throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}(e,t)}function _t(e){if("number"!=typeof e)throw new TypeError('"size" argument must be a number');if(e<0)throw new RangeError('"size" argument must not be negative')}function St(e,t){if(_t(t),e=wt(e,t<0?0:0|Rt(t)),!vt.TYPED_ARRAY_SUPPORT)for(var r=0;r<t;++r)e[r]=0;return e}function Ot(e,t){var r=t.length<0?0:0|Rt(t.length);e=wt(e,r);for(var i=0;i<r;i+=1)e[i]=255&t[i];return e}function Rt(e){if(e>=bt())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+bt().toString(16)+" bytes");return 0|e}function jt(e){return!(null==e||!e._isBuffer)}function xt(e,t){if(jt(e))return e.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(e)||e instanceof ArrayBuffer))return e.byteLength;"string"!=typeof e&&(e=""+e);var r=e.length;if(0===r)return 0;for(var i=!1;;)switch(t){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return er(e).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return tr(e).length;default:if(i)return er(e).length;t=(""+t).toLowerCase(),i=!0}}function Et(e,t,r){var i=!1;if((void 0===t||t<0)&&(t=0),t>this.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if((r>>>=0)<=(t>>>=0))return"";for(e||(e="utf8");;)switch(e){case"hex":return qt(this,t,r);case"utf8":case"utf-8":return Ut(this,t,r);case"ascii":return Bt(this,t,r);case"latin1":case"binary":return zt(this,t,r);case"base64":return Lt(this,t,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return Ht(this,t,r);default:if(i)throw new TypeError("Unknown encoding: "+e);e=(e+"").toLowerCase(),i=!0}}function At(e,t,r){var i=e[t];e[t]=e[r],e[r]=i}function Dt(e,t,r,i,s){if(0===e.length)return-1;if("string"==typeof r?(i=r,r=0):r>2147483647?r=2147483647:r<-2147483648&&(r=-2147483648),r=+r,isNaN(r)&&(r=s?0:e.length-1),r<0&&(r=e.length+r),r>=e.length){if(s)return-1;r=e.length-1}else if(r<0){if(!s)return-1;r=0}if("string"==typeof t&&(t=vt.from(t,i)),jt(t))return 0===t.length?-1:Ct(e,t,r,i,s);if("number"==typeof t)return t&=255,vt.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?s?Uint8Array.prototype.indexOf.call(e,t,r):Uint8Array.prototype.lastIndexOf.call(e,t,r):Ct(e,[t],r,i,s);throw new TypeError("val must be string, number or Buffer")}function Ct(e,t,r,i,s){var n,o=1,a=e.length,c=t.length;if(void 0!==i&&("ucs2"===(i=String(i).toLowerCase())||"ucs-2"===i||"utf16le"===i||"utf-16le"===i)){if(e.length<2||t.length<2)return-1;o=2,a/=2,c/=2,r/=2}function l(e,t){return 1===o?e[t]:e.readUInt16BE(t*o)}if(s){var u=-1;for(n=r;n<a;n++)if(l(e,n)===l(t,-1===u?0:n-u)){if(-1===u&&(u=n),n-u+1===c)return u*o}else-1!==u&&(n-=n-u),u=-1}else for(r+c>a&&(r=a-c),n=r;n>=0;n--){for(var h=!0,f=0;f<c;f++)if(l(e,n+f)!==l(t,f)){h=!1;break}if(h)return n}return-1}function Nt(e,t,r,i){r=Number(r)||0;var s=e.length-r;i?(i=Number(i))>s&&(i=s):i=s;var n=t.length;if(n%2!=0)throw new TypeError("Invalid hex string");i>n/2&&(i=n/2);for(var o=0;o<i;++o){var a=parseInt(t.substr(2*o,2),16);if(isNaN(a))return o;e[r+o]=a}return o}function $t(e,t,r,i){return rr(er(t,e.length-r),e,r,i)}function Pt(e,t,r,i){return rr(function(e){for(var t=[],r=0;r<e.length;++r)t.push(255&e.charCodeAt(r));return t}(t),e,r,i)}function It(e,t,r,i){return Pt(e,t,r,i)}function Tt(e,t,r,i){return rr(tr(t),e,r,i)}function Mt(e,t,r,i){return rr(function(e,t){for(var r,i,s,n=[],o=0;o<e.length&&!((t-=2)<0);++o)i=(r=e.charCodeAt(o))>>8,s=r%256,n.push(s),n.push(i);return n}(t,e.length-r),e,r,i)}function Lt(e,t,r){return 0===t&&r===e.length?dt(e):dt(e.slice(t,r))}function Ut(e,t,r){r=Math.min(e.length,r);for(var i=[],s=t;s<r;){var n,o,a,c,l=e[s],u=null,h=l>239?4:l>223?3:l>191?2:1;if(s+h<=r)switch(h){case 1:l<128&&(u=l);break;case 2:128==(192&(n=e[s+1]))&&(c=(31&l)<<6|63&n)>127&&(u=c);break;case 3:n=e[s+1],o=e[s+2],128==(192&n)&&128==(192&o)&&(c=(15&l)<<12|(63&n)<<6|63&o)>2047&&(c<55296||c>57343)&&(u=c);break;case 4:n=e[s+1],o=e[s+2],a=e[s+3],128==(192&n)&&128==(192&o)&&128==(192&a)&&(c=(15&l)<<18|(63&n)<<12|(63&o)<<6|63&a)>65535&&c<1114112&&(u=c)}null===u?(u=65533,h=1):u>65535&&(u-=65536,i.push(u>>>10&1023|55296),u=56320|1023&u),i.push(u),s+=h}return function(e){var t=e.length;if(t<=Ft)return String.fromCharCode.apply(String,e);var r="",i=0;for(;i<t;)r+=String.fromCharCode.apply(String,e.slice(i,i+=Ft));return r}(i)}vt.TYPED_ARRAY_SUPPORT=void 0===global.TYPED_ARRAY_SUPPORT||global.TYPED_ARRAY_SUPPORT,bt(),vt.poolSize=8192,vt._augment=function(e){return e.__proto__=vt.prototype,e},vt.from=function(e,t,r){return kt(null,e,t,r)},vt.TYPED_ARRAY_SUPPORT&&(vt.prototype.__proto__=Uint8Array.prototype,vt.__proto__=Uint8Array,"undefined"!=typeof Symbol&&Symbol.species&&vt[Symbol.species]),vt.alloc=function(e,t,r){return function(e,t,r,i){return _t(t),t<=0?wt(e,t):void 0!==r?"string"==typeof i?wt(e,t).fill(r,i):wt(e,t).fill(r):wt(e,t)}(null,e,t,r)},vt.allocUnsafe=function(e){return St(null,e)},vt.allocUnsafeSlow=function(e){return St(null,e)},vt.isBuffer=function(e){return null!=e&&(!!e._isBuffer||ir(e)||function(e){return"function"==typeof e.readFloatLE&&"function"==typeof e.slice&&ir(e.slice(0,0))}(e))},vt.compare=function(e,t){if(!jt(e)||!jt(t))throw new TypeError("Arguments must be Buffers");if(e===t)return 0;for(var r=e.length,i=t.length,s=0,n=Math.min(r,i);s<n;++s)if(e[s]!==t[s]){r=e[s],i=t[s];break}return r<i?-1:i<r?1:0},vt.isEncoding=function(e){switch(String(e).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"latin1":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return!0;default:return!1}},vt.concat=function(e,t){if(!yt(e))throw new TypeError('"list" argument must be an Array of Buffers');if(0===e.length)return vt.alloc(0);var r;if(void 0===t)for(t=0,r=0;r<e.length;++r)t+=e[r].length;var i=vt.allocUnsafe(t),s=0;for(r=0;r<e.length;++r){var n=e[r];if(!jt(n))throw new TypeError('"list" argument must be an Array of Buffers');n.copy(i,s),s+=n.length}return i},vt.byteLength=xt,vt.prototype._isBuffer=!0,vt.prototype.swap16=function(){var e=this.length;if(e%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(var t=0;t<e;t+=2)At(this,t,t+1);return this},vt.prototype.swap32=function(){var e=this.length;if(e%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(var t=0;t<e;t+=4)At(this,t,t+3),At(this,t+1,t+2);return this},vt.prototype.swap64=function(){var e=this.length;if(e%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(var t=0;t<e;t+=8)At(this,t,t+7),At(this,t+1,t+6),At(this,t+2,t+5),At(this,t+3,t+4);return this},vt.prototype.toString=function(){var e=0|this.length;return 0===e?"":0===arguments.length?Ut(this,0,e):Et.apply(this,arguments)},vt.prototype.equals=function(e){if(!jt(e))throw new TypeError("Argument must be a Buffer");return this===e||0===vt.compare(this,e)},vt.prototype.inspect=function(){var e="";return this.length>0&&(e=this.toString("hex",0,50).match(/.{2}/g).join(" "),this.length>50&&(e+=" ... ")),"<Buffer "+e+">"},vt.prototype.compare=function(e,t,r,i,s){if(!jt(e))throw new TypeError("Argument must be a Buffer");if(void 0===t&&(t=0),void 0===r&&(r=e?e.length:0),void 0===i&&(i=0),void 0===s&&(s=this.length),t<0||r>e.length||i<0||s>this.length)throw new RangeError("out of range index");if(i>=s&&t>=r)return 0;if(i>=s)return-1;if(t>=r)return 1;if(this===e)return 0;for(var n=(s>>>=0)-(i>>>=0),o=(r>>>=0)-(t>>>=0),a=Math.min(n,o),c=this.slice(i,s),l=e.slice(t,r),u=0;u<a;++u)if(c[u]!==l[u]){n=c[u],o=l[u];break}return n<o?-1:o<n?1:0},vt.prototype.includes=function(e,t,r){return-1!==this.indexOf(e,t,r)},vt.prototype.indexOf=function(e,t,r){return Dt(this,e,t,r,!0)},vt.prototype.lastIndexOf=function(e,t,r){return Dt(this,e,t,r,!1)},vt.prototype.write=function(e,t,r,i){if(void 0===t)i="utf8",r=this.length,t=0;else if(void 0===r&&"string"==typeof t)i=t,r=this.length,t=0;else{if(!isFinite(t))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");t|=0,isFinite(r)?(r|=0,void 0===i&&(i="utf8")):(i=r,r=void 0)}var s=this.length-t;if((void 0===r||r>s)&&(r=s),e.length>0&&(r<0||t<0)||t>this.length)throw new RangeError("Attempt to write outside buffer bounds");i||(i="utf8");for(var n=!1;;)switch(i){case"hex":return Nt(this,e,t,r);case"utf8":case"utf-8":return $t(this,e,t,r);case"ascii":return Pt(this,e,t,r);case"latin1":case"binary":return It(this,e,t,r);case"base64":return Tt(this,e,t,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return Mt(this,e,t,r);default:if(n)throw new TypeError("Unknown encoding: "+i);i=(""+i).toLowerCase(),n=!0}},vt.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var Ft=4096;function Bt(e,t,r){var i="";r=Math.min(e.length,r);for(var s=t;s<r;++s)i+=String.fromCharCode(127&e[s]);return i}function zt(e,t,r){var i="";r=Math.min(e.length,r);for(var s=t;s<r;++s)i+=String.fromCharCode(e[s]);return i}function qt(e,t,r){var i=e.length;(!t||t<0)&&(t=0),(!r||r<0||r>i)&&(r=i);for(var s="",n=t;n<r;++n)s+=Xt(e[n]);return s}function Ht(e,t,r){for(var i=e.slice(t,r),s="",n=0;n<i.length;n+=2)s+=String.fromCharCode(i[n]+256*i[n+1]);return s}function Vt(e,t,r){if(e%1!=0||e<0)throw new RangeError("offset is not uint");if(e+t>r)throw new RangeError("Trying to access beyond buffer length")}function Kt(e,t,r,i,s,n){if(!jt(e))throw new TypeError('"buffer" argument must be a Buffer instance');if(t>s||t<n)throw new RangeError('"value" argument is out of bounds');if(r+i>e.length)throw new RangeError("Index out of range")}function Wt(e,t,r,i){t<0&&(t=65535+t+1);for(var s=0,n=Math.min(e.length-r,2);s<n;++s)e[r+s]=(t&255<<8*(i?s:1-s))>>>8*(i?s:1-s)}function Jt(e,t,r,i){t<0&&(t=4294967295+t+1);for(var s=0,n=Math.min(e.length-r,4);s<n;++s)e[r+s]=t>>>8*(i?s:3-s)&255}function Gt(e,t,r,i,s,n){if(r+i>e.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function Yt(e,t,r,i,s){return s||Gt(e,0,r,4),mt(e,t,r,i,23,4),r+4}function Qt(e,t,r,i,s){return s||Gt(e,0,r,8),mt(e,t,r,i,52,8),r+8}vt.prototype.slice=function(e,t){var r,i=this.length;if((e=~~e)<0?(e+=i)<0&&(e=0):e>i&&(e=i),(t=void 0===t?i:~~t)<0?(t+=i)<0&&(t=0):t>i&&(t=i),t<e&&(t=e),vt.TYPED_ARRAY_SUPPORT)(r=this.subarray(e,t)).__proto__=vt.prototype;else{var s=t-e;r=new vt(s,void 0);for(var n=0;n<s;++n)r[n]=this[n+e]}return r},vt.prototype.readUIntLE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=this[e],s=1,n=0;++n<t&&(s*=256);)i+=this[e+n]*s;return i},vt.prototype.readUIntBE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=this[e+--t],s=1;t>0&&(s*=256);)i+=this[e+--t]*s;return i},vt.prototype.readUInt8=function(e,t){return t||Vt(e,1,this.length),this[e]},vt.prototype.readUInt16LE=function(e,t){return t||Vt(e,2,this.length),this[e]|this[e+1]<<8},vt.prototype.readUInt16BE=function(e,t){return t||Vt(e,2,this.length),this[e]<<8|this[e+1]},vt.prototype.readUInt32LE=function(e,t){return t||Vt(e,4,this.length),(this[e]|this[e+1]<<8|this[e+2]<<16)+16777216*this[e+3]},vt.prototype.readUInt32BE=function(e,t){return t||Vt(e,4,this.length),16777216*this[e]+(this[e+1]<<16|this[e+2]<<8|this[e+3])},vt.prototype.readIntLE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=this[e],s=1,n=0;++n<t&&(s*=256);)i+=this[e+n]*s;return i>=(s*=128)&&(i-=Math.pow(2,8*t)),i},vt.prototype.readIntBE=function(e,t,r){e|=0,t|=0,r||Vt(e,t,this.length);for(var i=t,s=1,n=this[e+--i];i>0&&(s*=256);)n+=this[e+--i]*s;return n>=(s*=128)&&(n-=Math.pow(2,8*t)),n},vt.prototype.readInt8=function(e,t){return t||Vt(e,1,this.length),128&this[e]?-1*(255-this[e]+1):this[e]},vt.prototype.readInt16LE=function(e,t){t||Vt(e,2,this.length);var r=this[e]|this[e+1]<<8;return 32768&r?4294901760|r:r},vt.prototype.readInt16BE=function(e,t){t||Vt(e,2,this.length);var r=this[e+1]|this[e]<<8;return 32768&r?4294901760|r:r},vt.prototype.readInt32LE=function(e,t){return t||Vt(e,4,this.length),this[e]|this[e+1]<<8|this[e+2]<<16|this[e+3]<<24},vt.prototype.readInt32BE=function(e,t){return t||Vt(e,4,this.length),this[e]<<24|this[e+1]<<16|this[e+2]<<8|this[e+3]},vt.prototype.readFloatLE=function(e,t){return t||Vt(e,4,this.length),pt(this,e,!0,23,4)},vt.prototype.readFloatBE=function(e,t){return t||Vt(e,4,this.length),pt(this,e,!1,23,4)},vt.prototype.readDoubleLE=function(e,t){return t||Vt(e,8,this.length),pt(this,e,!0,52,8)},vt.prototype.readDoubleBE=function(e,t){return t||Vt(e,8,this.length),pt(this,e,!1,52,8)},vt.prototype.writeUIntLE=function(e,t,r,i){(e=+e,t|=0,r|=0,i)||Kt(this,e,t,r,Math.pow(2,8*r)-1,0);var s=1,n=0;for(this[t]=255&e;++n<r&&(s*=256);)this[t+n]=e/s&255;return t+r},vt.prototype.writeUIntBE=function(e,t,r,i){(e=+e,t|=0,r|=0,i)||Kt(this,e,t,r,Math.pow(2,8*r)-1,0);var s=r-1,n=1;for(this[t+s]=255&e;--s>=0&&(n*=256);)this[t+s]=e/n&255;return t+r},vt.prototype.writeUInt8=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,1,255,0),vt.TYPED_ARRAY_SUPPORT||(e=Math.floor(e)),this[t]=255&e,t+1},vt.prototype.writeUInt16LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,65535,0),vt.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8):Wt(this,e,t,!0),t+2},vt.prototype.writeUInt16BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,65535,0),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>8,this[t+1]=255&e):Wt(this,e,t,!1),t+2},vt.prototype.writeUInt32LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,4294967295,0),vt.TYPED_ARRAY_SUPPORT?(this[t+3]=e>>>24,this[t+2]=e>>>16,this[t+1]=e>>>8,this[t]=255&e):Jt(this,e,t,!0),t+4},vt.prototype.writeUInt32BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,4294967295,0),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>24,this[t+1]=e>>>16,this[t+2]=e>>>8,this[t+3]=255&e):Jt(this,e,t,!1),t+4},vt.prototype.writeIntLE=function(e,t,r,i){if(e=+e,t|=0,!i){var s=Math.pow(2,8*r-1);Kt(this,e,t,r,s-1,-s)}var n=0,o=1,a=0;for(this[t]=255&e;++n<r&&(o*=256);)e<0&&0===a&&0!==this[t+n-1]&&(a=1),this[t+n]=(e/o|0)-a&255;return t+r},vt.prototype.writeIntBE=function(e,t,r,i){if(e=+e,t|=0,!i){var s=Math.pow(2,8*r-1);Kt(this,e,t,r,s-1,-s)}var n=r-1,o=1,a=0;for(this[t+n]=255&e;--n>=0&&(o*=256);)e<0&&0===a&&0!==this[t+n+1]&&(a=1),this[t+n]=(e/o|0)-a&255;return t+r},vt.prototype.writeInt8=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,1,127,-128),vt.TYPED_ARRAY_SUPPORT||(e=Math.floor(e)),e<0&&(e=255+e+1),this[t]=255&e,t+1},vt.prototype.writeInt16LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,32767,-32768),vt.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8):Wt(this,e,t,!0),t+2},vt.prototype.writeInt16BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,2,32767,-32768),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>8,this[t+1]=255&e):Wt(this,e,t,!1),t+2},vt.prototype.writeInt32LE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,2147483647,-2147483648),vt.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8,this[t+2]=e>>>16,this[t+3]=e>>>24):Jt(this,e,t,!0),t+4},vt.prototype.writeInt32BE=function(e,t,r){return e=+e,t|=0,r||Kt(this,e,t,4,2147483647,-2147483648),e<0&&(e=4294967295+e+1),vt.TYPED_ARRAY_SUPPORT?(this[t]=e>>>24,this[t+1]=e>>>16,this[t+2]=e>>>8,this[t+3]=255&e):Jt(this,e,t,!1),t+4},vt.prototype.writeFloatLE=function(e,t,r){return Yt(this,e,t,!0,r)},vt.prototype.writeFloatBE=function(e,t,r){return Yt(this,e,t,!1,r)},vt.prototype.writeDoubleLE=function(e,t,r){return Qt(this,e,t,!0,r)},vt.prototype.writeDoubleBE=function(e,t,r){return Qt(this,e,t,!1,r)},vt.prototype.copy=function(e,t,r,i){if(r||(r=0),i||0===i||(i=this.length),t>=e.length&&(t=e.length),t||(t=0),i>0&&i<r&&(i=r),i===r)return 0;if(0===e.length||0===this.length)return 0;if(t<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("sourceStart out of bounds");if(i<0)throw new RangeError("sourceEnd out of bounds");i>this.length&&(i=this.length),e.length-t<i-r&&(i=e.length-t+r);var s,n=i-r;if(this===e&&r<t&&t<i)for(s=n-1;s>=0;--s)e[s+t]=this[s+r];else if(n<1e3||!vt.TYPED_ARRAY_SUPPORT)for(s=0;s<n;++s)e[s+t]=this[s+r];else Uint8Array.prototype.set.call(e,this.subarray(r,r+n),t);return n},vt.prototype.fill=function(e,t,r,i){if("string"==typeof e){if("string"==typeof t?(i=t,t=0,r=this.length):"string"==typeof r&&(i=r,r=this.length),1===e.length){var s=e.charCodeAt(0);s<256&&(e=s)}if(void 0!==i&&"string"!=typeof i)throw new TypeError("encoding must be a string");if("string"==typeof i&&!vt.isEncoding(i))throw new TypeError("Unknown encoding: "+i)}else"number"==typeof e&&(e&=255);if(t<0||this.length<t||this.length<r)throw new RangeError("Out of range index");if(r<=t)return this;var n;if(t>>>=0,r=void 0===r?this.length:r>>>0,e||(e=0),"number"==typeof e)for(n=t;n<r;++n)this[n]=e;else{var o=jt(e)?e:er(new vt(e,i).toString()),a=o.length;for(n=0;n<r-t;++n)this[n+t]=o[n%a]}return this};var Zt=/[^+\/0-9A-Za-z-_]/g;function Xt(e){return e<16?"0"+e.toString(16):e.toString(16)}function er(e,t){var r;t=t||1/0;for(var i=e.length,s=null,n=[],o=0;o<i;++o){if((r=e.charCodeAt(o))>55295&&r<57344){if(!s){if(r>56319){(t-=3)>-1&&n.push(239,191,189);continue}if(o+1===i){(t-=3)>-1&&n.push(239,191,189);continue}s=r;continue}if(r<56320){(t-=3)>-1&&n.push(239,191,189),s=r;continue}r=65536+(s-55296<<10|r-56320)}else s&&(t-=3)>-1&&n.push(239,191,189);if(s=null,r<128){if((t-=1)<0)break;n.push(r)}else if(r<2048){if((t-=2)<0)break;n.push(r>>6|192,63&r|128)}else if(r<65536){if((t-=3)<0)break;n.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((t-=4)<0)break;n.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return n}function tr(e){return function(e){var t,r,i,s,n,o;lt||ut();var a=e.length;if(a%4>0)throw new Error("Invalid string. Length must be a multiple of 4");n="="===e[a-2]?2:"="===e[a-1]?1:0,o=new ct(3*a/4-n),i=n>0?a-4:a;var c=0;for(t=0,r=0;t<i;t+=4,r+=3)s=at[e.charCodeAt(t)]<<18|at[e.charCodeAt(t+1)]<<12|at[e.charCodeAt(t+2)]<<6|at[e.charCodeAt(t+3)],o[c++]=s>>16&255,o[c++]=s>>8&255,o[c++]=255&s;return 2===n?(s=at[e.charCodeAt(t)]<<2|at[e.charCodeAt(t+1)]>>4,o[c++]=255&s):1===n&&(s=at[e.charCodeAt(t)]<<10|at[e.charCodeAt(t+1)]<<4|at[e.charCodeAt(t+2)]>>2,o[c++]=s>>8&255,o[c++]=255&s),o}(function(e){if((e=function(e){return e.trim?e.trim():e.replace(/^\s+|\s+$/g,"")}(e).replace(Zt,"")).length<2)return"";for(;e.length%4!=0;)e+="=";return e}(e))}function rr(e,t,r,i){for(var s=0;s<i&&!(s+r>=t.length||s>=e.length);++s)t[s+r]=e[s];return s}function ir(e){return!!e.constructor&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}function sr(){this.head=null,this.tail=null,this.length=0}sr.prototype.push=function(e){var t={data:e,next:null};this.length>0?this.tail.next=t:this.head=t,this.tail=t,++this.length},sr.prototype.unshift=function(e){var t={data:e,next:this.head};0===this.length&&(this.tail=t),this.head=t,++this.length},sr.prototype.shift=function(){if(0!==this.length){var e=this.head.data;return 1===this.length?this.head=this.tail=null:this.head=this.head.next,--this.length,e}},sr.prototype.clear=function(){this.head=this.tail=null,this.length=0},sr.prototype.join=function(e){if(0===this.length)return"";for(var t=this.head,r=""+t.data;t=t.next;)r+=e+t.data;return r},sr.prototype.concat=function(e){if(0===this.length)return vt.alloc(0);if(1===this.length)return this.head.data;for(var t=vt.allocUnsafe(e>>>0),r=this.head,i=0;r;)r.data.copy(t,i),i+=r.data.length,r=r.next;return t};var nr=vt.isEncoding||function(e){switch(e&&e.toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":case"raw":return!0;default:return!1}};function or(e){switch(this.encoding=(e||"utf8").toLowerCase().replace(/[-_]/,""),function(e){if(e&&!nr(e))throw new Error("Unknown encoding: "+e)}(e),this.encoding){case"utf8":this.surrogateSize=3;break;case"ucs2":case"utf16le":this.surrogateSize=2,this.detectIncompleteChar=cr;break;case"base64":this.surrogateSize=3,this.detectIncompleteChar=lr;break;default:return void(this.write=ar)}this.charBuffer=new vt(6),this.charReceived=0,this.charLength=0}function ar(e){return e.toString(this.encoding)}function cr(e){this.charReceived=e.length%2,this.charLength=this.charReceived?2:0}function lr(e){this.charReceived=e.length%3,this.charLength=this.charReceived?3:0}or.prototype.write=function(e){for(var t="";this.charLength;){var r=e.length>=this.charLength-this.charReceived?this.charLength-this.charReceived:e.length;if(e.copy(this.charBuffer,this.charReceived,0,r),this.charReceived+=r,this.charReceived<this.charLength)return"";if(e=e.slice(r,e.length),!((s=(t=this.charBuffer.slice(0,this.charLength).toString(this.encoding)).charCodeAt(t.length-1))>=55296&&s<=56319)){if(this.charReceived=this.charLength=0,0===e.length)return t;break}this.charLength+=this.surrogateSize,t=""}this.detectIncompleteChar(e);var i=e.length;this.charLength&&(e.copy(this.charBuffer,0,e.length-this.charReceived,i),i-=this.charReceived);var s;i=(t+=e.toString(this.encoding,0,i)).length-1;if((s=t.charCodeAt(i))>=55296&&s<=56319){var n=this.surrogateSize;return this.charLength+=n,this.charReceived+=n,this.charBuffer.copy(this.charBuffer,n,0,n),e.copy(this.charBuffer,0,0,n),t.substring(0,i)}return t},or.prototype.detectIncompleteChar=function(e){for(var t=e.length>=3?3:e.length;t>0;t--){var r=e[e.length-t];if(1==t&&r>>5==6){this.charLength=2;break}if(t<=2&&r>>4==14){this.charLength=3;break}if(t<=3&&r>>3==30){this.charLength=4;break}}this.charReceived=t},or.prototype.end=function(e){var t="";if(e&&e.length&&(t=this.write(e)),this.charReceived){var r=this.charReceived,i=this.charBuffer,s=this.encoding;t+=i.slice(0,r).toString(s)}return t},fr.ReadableState=hr;var ur=function(e){if(Ze(Be)&&(Be=Me.env.NODE_DEBUG||""),e=e.toUpperCase(),!ze[e])if(new RegExp("\\b"+e+"\\b","i").test(Be)){ze[e]=function(){var t=Ue.apply(null,arguments);console.error("%s %d: %s",e,0,t)}}else ze[e]=function(){};return ze[e]}("stream");function hr(e,t){e=e||{},this.objectMode=!!e.objectMode,t instanceof Fr&&(this.objectMode=this.objectMode||!!e.readableObjectMode);var r=e.highWaterMark,i=this.objectMode?16:16384;this.highWaterMark=r||0===r?r:i,this.highWaterMark=~~this.highWaterMark,this.buffer=new sr,this.length=0,this.pipes=null,this.pipesCount=0,this.flowing=null,this.ended=!1,this.endEmitted=!1,this.reading=!1,this.sync=!0,this.needReadable=!1,this.emittedReadable=!1,this.readableListening=!1,this.resumeScheduled=!1,this.defaultEncoding=e.defaultEncoding||"utf8",this.ranOut=!1,this.awaitDrain=0,this.readingMore=!1,this.decoder=null,this.encoding=null,e.encoding&&(this.decoder=new or(e.encoding),this.encoding=e.encoding)}function fr(e){if(!(this instanceof fr))return new fr(e);this._readableState=new hr(e,this),this.readable=!0,e&&"function"==typeof e.read&&(this._read=e.read),se.call(this)}function dr(e,t,r,i,s){var n=function(e,t){var r=null;Buffer.isBuffer(t)||"string"==typeof t||null==t||e.objectMode||(r=new TypeError("Invalid non-string/buffer chunk"));return r}(t,r);if(n)e.emit("error",n);else if(null===r)t.reading=!1,function(e,t){if(t.ended)return;if(t.decoder){var r=t.decoder.end();r&&r.length&&(t.buffer.push(r),t.length+=t.objectMode?1:r.length)}t.ended=!0,gr(e)}(e,t);else if(t.objectMode||r&&r.length>0)if(t.ended&&!s){var o=new Error("stream.push() after EOF");e.emit("error",o)}else if(t.endEmitted&&s){var a=new Error("stream.unshift() after end event");e.emit("error",a)}else{var c;!t.decoder||s||i||(r=t.decoder.write(r),c=!t.objectMode&&0===r.length),s||(t.reading=!1),c||(t.flowing&&0===t.length&&!t.sync?(e.emit("data",r),e.read(0)):(t.length+=t.objectMode?1:r.length,s?t.buffer.unshift(r):t.buffer.push(r),t.needReadable&&gr(e))),function(e,t){t.readingMore||(t.readingMore=!0,Pe(br,e,t))}(e,t)}else s||(t.reading=!1);return function(e){return!e.ended&&(e.needReadable||e.length<e.highWaterMark||0===e.length)}(t)}Te(fr,se),fr.prototype.push=function(e,t){var r=this._readableState;return r.objectMode||"string"!=typeof e||(t=t||r.defaultEncoding)!==r.encoding&&(e=Buffer.from(e,t),t=""),dr(this,r,e,t,!1)},fr.prototype.unshift=function(e){return dr(this,this._readableState,e,"",!0)},fr.prototype.isPaused=function(){return!1===this._readableState.flowing},fr.prototype.setEncoding=function(e){return this._readableState.decoder=new or(e),this._readableState.encoding=e,this};var pr=8388608;function mr(e,t){return e<=0||0===t.length&&t.ended?0:t.objectMode?1:e!=e?t.flowing&&t.length?t.buffer.head.data.length:t.length:(e>t.highWaterMark&&(t.highWaterMark=function(e){return e>=pr?e=pr:(e--,e|=e>>>1,e|=e>>>2,e|=e>>>4,e|=e>>>8,e|=e>>>16,e++),e}(e)),e<=t.length?e:t.ended?t.length:(t.needReadable=!0,0))}function gr(e){var t=e._readableState;t.needReadable=!1,t.emittedReadable||(ur("emitReadable",t.flowing),t.emittedReadable=!0,t.sync?Pe(yr,e):yr(e))}function yr(e){ur("emit readable"),e.emit("readable"),kr(e)}function br(e,t){for(var r=t.length;!t.reading&&!t.flowing&&!t.ended&&t.length<t.highWaterMark&&(ur("maybeReadMore read 0"),e.read(0),r!==t.length);)r=t.length;t.readingMore=!1}function wr(e){ur("readable nexttick read 0"),e.read(0)}function vr(e,t){t.reading||(ur("resume read 0"),e.read(0)),t.resumeScheduled=!1,t.awaitDrain=0,e.emit("resume"),kr(e),t.flowing&&!t.reading&&e.read(0)}function kr(e){var t=e._readableState;for(ur("flow",t.flowing);t.flowing&&null!==e.read(););}function _r(e,t){return 0===t.length?null:(t.objectMode?r=t.buffer.shift():!e||e>=t.length?(r=t.decoder?t.buffer.join(""):1===t.buffer.length?t.buffer.head.data:t.buffer.concat(t.length),t.buffer.clear()):r=function(e,t,r){var i;e<t.head.data.length?(i=t.head.data.slice(0,e),t.head.data=t.head.data.slice(e)):i=e===t.head.data.length?t.shift():r?function(e,t){var r=t.head,i=1,s=r.data;e-=s.length;for(;r=r.next;){var n=r.data,o=e>n.length?n.length:e;if(o===n.length?s+=n:s+=n.slice(0,e),0===(e-=o)){o===n.length?(++i,r.next?t.head=r.next:t.head=t.tail=null):(t.head=r,r.data=n.slice(o));break}++i}return t.length-=i,s}(e,t):function(e,t){var r=Buffer.allocUnsafe(e),i=t.head,s=1;i.data.copy(r),e-=i.data.length;for(;i=i.next;){var n=i.data,o=e>n.length?n.length:e;if(n.copy(r,r.length-e,0,o),0===(e-=o)){o===n.length?(++s,i.next?t.head=i.next:t.head=t.tail=null):(t.head=i,i.data=n.slice(o));break}++s}return t.length-=s,r}(e,t);return i}(e,t.buffer,t.decoder),r);var r}function Sr(e){var t=e._readableState;if(t.length>0)throw new Error('"endReadable()" called on non-empty stream');t.endEmitted||(t.ended=!0,Pe(Or,t,e))}function Or(e,t){e.endEmitted||0!==e.length||(e.endEmitted=!0,t.readable=!1,t.emit("end"))}function Rr(e,t){for(var r=0,i=e.length;r<i;r++)if(e[r]===t)return r;return-1}function jr(){}function xr(e,t,r){this.chunk=e,this.encoding=t,this.callback=r,this.next=null}function Er(e,t){Object.defineProperty(this,"buffer",{get:Fe(function(){return this.getBuffer()},"_writableState.buffer is deprecated. Use _writableState.getBuffer instead.")}),e=e||{},this.objectMode=!!e.objectMode,t instanceof Fr&&(this.objectMode=this.objectMode||!!e.writableObjectMode);var r=e.highWaterMark,i=this.objectMode?16:16384;this.highWaterMark=r||0===r?r:i,this.highWaterMark=~~this.highWaterMark,this.needDrain=!1,this.ending=!1,this.ended=!1,this.finished=!1;var s=!1===e.decodeStrings;this.decodeStrings=!s,this.defaultEncoding=e.defaultEncoding||"utf8",this.length=0,this.writing=!1,this.corked=0,this.sync=!0,this.bufferProcessing=!1,this.onwrite=function(e){!function(e,t){var r=e._writableState,i=r.sync,s=r.writecb;if(function(e){e.writing=!1,e.writecb=null,e.length-=e.writelen,e.writelen=0}(r),t)!function(e,t,r,i,s){--t.pendingcb,r?Pe(s,i):s(i);e._writableState.errorEmitted=!0,e.emit("error",i)}(e,r,i,t,s);else{var n=$r(r);n||r.corked||r.bufferProcessing||!r.bufferedRequest||Nr(e,r),i?Pe(Cr,e,r,n,s):Cr(e,r,n,s)}}(t,e)},this.writecb=null,this.writelen=0,this.bufferedRequest=null,this.lastBufferedRequest=null,this.pendingcb=0,this.prefinished=!1,this.errorEmitted=!1,this.bufferedRequestCount=0,this.corkedRequestsFree=new Tr(this)}function Ar(e){if(!(this instanceof Ar||this instanceof Fr))return new Ar(e);this._writableState=new Er(e,this),this.writable=!0,e&&("function"==typeof e.write&&(this._write=e.write),"function"==typeof e.writev&&(this._writev=e.writev)),se.call(this)}function Dr(e,t,r,i,s,n,o){t.writelen=i,t.writecb=o,t.writing=!0,t.sync=!0,r?e._writev(s,t.onwrite):e._write(s,n,t.onwrite),t.sync=!1}function Cr(e,t,r,i){r||function(e,t){0===t.length&&t.needDrain&&(t.needDrain=!1,e.emit("drain"))}(e,t),t.pendingcb--,i(),Ir(e,t)}function Nr(e,t){t.bufferProcessing=!0;var r=t.bufferedRequest;if(e._writev&&r&&r.next){var i=t.bufferedRequestCount,s=new Array(i),n=t.corkedRequestsFree;n.entry=r;for(var o=0;r;)s[o]=r,r=r.next,o+=1;Dr(e,t,!0,t.length,s,"",n.finish),t.pendingcb++,t.lastBufferedRequest=null,n.next?(t.corkedRequestsFree=n.next,n.next=null):t.corkedRequestsFree=new Tr(t)}else{for(;r;){var a=r.chunk,c=r.encoding,l=r.callback;if(Dr(e,t,!1,t.objectMode?1:a.length,a,c,l),r=r.next,t.writing)break}null===r&&(t.lastBufferedRequest=null)}t.bufferedRequestCount=0,t.bufferedRequest=r,t.bufferProcessing=!1}function $r(e){return e.ending&&0===e.length&&null===e.bufferedRequest&&!e.finished&&!e.writing}function Pr(e,t){t.prefinished||(t.prefinished=!0,e.emit("prefinish"))}function Ir(e,t){var r=$r(t);return r&&(0===t.pendingcb?(Pr(e,t),t.finished=!0,e.emit("finish")):Pr(e,t)),r}function Tr(e){var t=this;this.next=null,this.entry=null,this.finish=function(r){var i=t.entry;for(t.entry=null;i;){var s=i.callback;e.pendingcb--,s(r),i=i.next}e.corkedRequestsFree?e.corkedRequestsFree.next=t:e.corkedRequestsFree=t}}fr.prototype.read=function(e){ur("read",e),e=parseInt(e,10);var t=this._readableState,r=e;if(0!==e&&(t.emittedReadable=!1),0===e&&t.needReadable&&(t.length>=t.highWaterMark||t.ended))return ur("read: emitReadable",t.length,t.ended),0===t.length&&t.ended?Sr(this):gr(this),null;if(0===(e=mr(e,t))&&t.ended)return 0===t.length&&Sr(this),null;var i,s=t.needReadable;return ur("need readable",s),(0===t.length||t.length-e<t.highWaterMark)&&ur("length less than watermark",s=!0),t.ended||t.reading?ur("reading or ended",s=!1):s&&(ur("do read"),t.reading=!0,t.sync=!0,0===t.length&&(t.needReadable=!0),this._read(t.highWaterMark),t.sync=!1,t.reading||(e=mr(r,t))),null===(i=e>0?_r(e,t):null)?(t.needReadable=!0,e=0):t.length-=e,0===t.length&&(t.ended||(t.needReadable=!0),r!==e&&t.ended&&Sr(this)),null!==i&&this.emit("data",i),i},fr.prototype._read=function(e){this.emit("error",new Error("not implemented"))},fr.prototype.pipe=function(e,t){var r=this,i=this._readableState;switch(i.pipesCount){case 0:i.pipes=e;break;case 1:i.pipes=[i.pipes,e];break;default:i.pipes.push(e)}i.pipesCount+=1,ur("pipe count=%d opts=%j",i.pipesCount,t);var s=!t||!1!==t.end?o:l;function n(e){ur("onunpipe"),e===r&&l()}function o(){ur("onend"),e.end()}i.endEmitted?Pe(s):r.once("end",s),e.on("unpipe",n);var a=function(e){return function(){var t=e._readableState;ur("pipeOnDrain",t.awaitDrain),t.awaitDrain&&t.awaitDrain--,0===t.awaitDrain&&e.listeners("data").length&&(t.flowing=!0,kr(e))}}(r);e.on("drain",a);var c=!1;function l(){ur("cleanup"),e.removeListener("close",d),e.removeListener("finish",p),e.removeListener("drain",a),e.removeListener("error",f),e.removeListener("unpipe",n),r.removeListener("end",o),r.removeListener("end",l),r.removeListener("data",h),c=!0,!i.awaitDrain||e._writableState&&!e._writableState.needDrain||a()}var u=!1;function h(t){ur("ondata"),u=!1,!1!==e.write(t)||u||((1===i.pipesCount&&i.pipes===e||i.pipesCount>1&&-1!==Rr(i.pipes,e))&&!c&&(ur("false write response, pause",r._readableState.awaitDrain),r._readableState.awaitDrain++,u=!0),r.pause())}function f(t){var r;ur("onerror",t),m(),e.removeListener("error",f),0===(r="error",e.listeners(r).length)&&e.emit("error",t)}function d(){e.removeListener("finish",p),m()}function p(){ur("onfinish"),e.removeListener("close",d),m()}function m(){ur("unpipe"),r.unpipe(e)}return r.on("data",h),function(e,t,r){if("function"==typeof e.prependListener)return e.prependListener(t,r);e._events&&e._events[t]?Array.isArray(e._events[t])?e._events[t].unshift(r):e._events[t]=[r,e._events[t]]:e.on(t,r)}(e,"error",f),e.once("close",d),e.once("finish",p),e.emit("pipe",r),i.flowing||(ur("pipe resume"),r.resume()),e},fr.prototype.unpipe=function(e){var t=this._readableState;if(0===t.pipesCount)return this;if(1===t.pipesCount)return e&&e!==t.pipes||(e||(e=t.pipes),t.pipes=null,t.pipesCount=0,t.flowing=!1,e&&e.emit("unpipe",this)),this;if(!e){var r=t.pipes,i=t.pipesCount;t.pipes=null,t.pipesCount=0,t.flowing=!1;for(var s=0;s<i;s++)r[s].emit("unpipe",this);return this}var n=Rr(t.pipes,e);return-1===n||(t.pipes.splice(n,1),t.pipesCount-=1,1===t.pipesCount&&(t.pipes=t.pipes[0]),e.emit("unpipe",this)),this},fr.prototype.on=function(e,t){var r=se.prototype.on.call(this,e,t);if("data"===e)!1!==this._readableState.flowing&&this.resume();else if("readable"===e){var i=this._readableState;i.endEmitted||i.readableListening||(i.readableListening=i.needReadable=!0,i.emittedReadable=!1,i.reading?i.length&&gr(this):Pe(wr,this))}return r},fr.prototype.addListener=fr.prototype.on,fr.prototype.resume=function(){var e=this._readableState;return e.flowing||(ur("resume"),e.flowing=!0,function(e,t){t.resumeScheduled||(t.resumeScheduled=!0,Pe(vr,e,t))}(this,e)),this},fr.prototype.pause=function(){return ur("call pause flowing=%j",this._readableState.flowing),!1!==this._readableState.flowing&&(ur("pause"),this._readableState.flowing=!1,this.emit("pause")),this},fr.prototype.wrap=function(e){var t=this._readableState,r=!1,i=this;for(var s in e.on("end",function(){if(ur("wrapped end"),t.decoder&&!t.ended){var e=t.decoder.end();e&&e.length&&i.push(e)}i.push(null)}),e.on("data",function(s){(ur("wrapped data"),t.decoder&&(s=t.decoder.write(s)),t.objectMode&&null==s)||(t.objectMode||s&&s.length)&&(i.push(s)||(r=!0,e.pause()))}),e)void 0===this[s]&&"function"==typeof e[s]&&(this[s]=function(t){return function(){return e[t].apply(e,arguments)}}(s));return function(e,t){for(var r=0,i=e.length;r<i;r++)t(e[r],r)}(["error","close","destroy","pause","resume"],function(t){e.on(t,i.emit.bind(i,t))}),i._read=function(t){ur("wrapped _read",t),r&&(r=!1,e.resume())},i},fr._fromList=_r,Ar.WritableState=Er,Te(Ar,se),Er.prototype.getBuffer=function(){for(var e=this.bufferedRequest,t=[];e;)t.push(e),e=e.next;return t},Ar.prototype.pipe=function(){this.emit("error",new Error("Cannot pipe, not readable"))},Ar.prototype.write=function(e,t,r){var i=this._writableState,s=!1;return"function"==typeof t&&(r=t,t=null),vt.isBuffer(e)?t="buffer":t||(t=i.defaultEncoding),"function"!=typeof r&&(r=jr),i.ended?function(e,t){var r=new Error("write after end");e.emit("error",r),Pe(t,r)}(this,r):function(e,t,r,i){var s=!0,n=!1;return null===r?n=new TypeError("May not write null values to stream"):vt.isBuffer(r)||"string"==typeof r||void 0===r||t.objectMode||(n=new TypeError("Invalid non-string/buffer chunk")),n&&(e.emit("error",n),Pe(i,n),s=!1),s}(this,i,e,r)&&(i.pendingcb++,s=function(e,t,r,i,s){r=function(e,t,r){e.objectMode||!1===e.decodeStrings||"string"!=typeof t||(t=vt.from(t,r));return t}(t,r,i),vt.isBuffer(r)&&(i="buffer");var n=t.objectMode?1:r.length;t.length+=n;var o=t.length<t.highWaterMark;o||(t.needDrain=!0);if(t.writing||t.corked){var a=t.lastBufferedRequest;t.lastBufferedRequest=new xr(r,i,s),a?a.next=t.lastBufferedRequest:t.bufferedRequest=t.lastBufferedRequest,t.bufferedRequestCount+=1}else Dr(e,t,!1,n,r,i,s);return o}(this,i,e,t,r)),s},Ar.prototype.cork=function(){this._writableState.corked++},Ar.prototype.uncork=function(){var e=this._writableState;e.corked&&(e.corked--,e.writing||e.corked||e.finished||e.bufferProcessing||!e.bufferedRequest||Nr(this,e))},Ar.prototype.setDefaultEncoding=function(e){if("string"==typeof e&&(e=e.toLowerCase()),!(["hex","utf8","utf-8","ascii","binary","base64","ucs2","ucs-2","utf16le","utf-16le","raw"].indexOf((e+"").toLowerCase())>-1))throw new TypeError("Unknown encoding: "+e);return this._writableState.defaultEncoding=e,this},Ar.prototype._write=function(e,t,r){r(new Error("not implemented"))},Ar.prototype._writev=null,Ar.prototype.end=function(e,t,r){var i=this._writableState;"function"==typeof e?(r=e,e=null,t=null):"function"==typeof t&&(r=t,t=null),null!=e&&this.write(e,t),i.corked&&(i.corked=1,this.uncork()),i.ending||i.finished||function(e,t,r){t.ending=!0,Ir(e,t),r&&(t.finished?Pe(r):e.once("finish",r));t.ended=!0,e.writable=!1}(this,i,r)},Te(Fr,fr);for(var Mr=Object.keys(Ar.prototype),Lr=0;Lr<Mr.length;Lr++){var Ur=Mr[Lr];Fr.prototype[Ur]||(Fr.prototype[Ur]=Ar.prototype[Ur])}function Fr(e){if(!(this instanceof Fr))return new Fr(e);fr.call(this,e),Ar.call(this,e),e&&!1===e.readable&&(this.readable=!1),e&&!1===e.writable&&(this.writable=!1),this.allowHalfOpen=!0,e&&!1===e.allowHalfOpen&&(this.allowHalfOpen=!1),this.once("end",Br)}function Br(){this.allowHalfOpen||this._writableState.ended||Pe(zr,this)}function zr(e){e.end()}function qr(e){this.afterTransform=function(t,r){return function(e,t,r){var i=e._transformState;i.transforming=!1;var s=i.writecb;if(!s)return e.emit("error",new Error("no writecb in Transform class"));i.writechunk=null,i.writecb=null,null!=r&&e.push(r);s(t);var n=e._readableState;n.reading=!1,(n.needReadable||n.length<n.highWaterMark)&&e._read(n.highWaterMark)}(e,t,r)},this.needTransform=!1,this.transforming=!1,this.writecb=null,this.writechunk=null,this.writeencoding=null}function Hr(e){if(!(this instanceof Hr))return new Hr(e);Fr.call(this,e),this._transformState=new qr(this);var t=this;this._readableState.needReadable=!0,this._readableState.sync=!1,e&&("function"==typeof e.transform&&(this._transform=e.transform),"function"==typeof e.flush&&(this._flush=e.flush)),this.once("prefinish",function(){"function"==typeof this._flush?this._flush(function(e){Vr(t,e)}):Vr(t)})}function Vr(e,t){if(t)return e.emit("error",t);var r=e._writableState,i=e._transformState;if(r.length)throw new Error("Calling transform done when ws.length != 0");if(i.transforming)throw new Error("Calling transform done when still transforming");return e.push(null)}function Kr(e){if(!(this instanceof Kr))return new Kr(e);Hr.call(this,e)}function Wr(){se.call(this)}Te(Hr,Fr),Hr.prototype.push=function(e,t){return this._transformState.needTransform=!1,Fr.prototype.push.call(this,e,t)},Hr.prototype._transform=function(e,t,r){throw new Error("Not implemented")},Hr.prototype._write=function(e,t,r){var i=this._transformState;if(i.writecb=r,i.writechunk=e,i.writeencoding=t,!i.transforming){var s=this._readableState;(i.needTransform||s.needReadable||s.length<s.highWaterMark)&&this._read(s.highWaterMark)}},Hr.prototype._read=function(e){var t=this._transformState;null!==t.writechunk&&t.writecb&&!t.transforming?(t.transforming=!0,this._transform(t.writechunk,t.writeencoding,t.afterTransform)):t.needTransform=!0},Te(Kr,Hr),Kr.prototype._transform=function(e,t,r){r(null,e)},Te(Wr,se),Wr.Readable=fr,Wr.Writable=Ar,Wr.Duplex=Fr,Wr.Transform=Hr,Wr.PassThrough=Kr,Wr.Stream=Wr,Wr.prototype.pipe=function(e,t){var r=this;function i(t){e.writable&&!1===e.write(t)&&r.pause&&r.pause()}function s(){r.readable&&r.resume&&r.resume()}r.on("data",i),e.on("drain",s),e._isStdio||t&&!1===t.end||(r.on("end",o),r.on("close",a));var n=!1;function o(){n||(n=!0,e.end())}function a(){n||(n=!0,"function"==typeof e.destroy&&e.destroy())}function c(e){if(l(),0===se.listenerCount(this,"error"))throw e}function l(){r.removeListener("data",i),e.removeListener("drain",s),r.removeListener("end",o),r.removeListener("close",a),r.removeListener("error",c),e.removeListener("error",c),r.removeListener("end",l),r.removeListener("close",l),e.removeListener("close",l)}return r.on("error",c),e.on("error",c),r.on("end",l),r.on("close",l),e.on("close",l),e.emit("pipe",r),e};class Jr extends se{constructor({resource:e}){super(),this.resource=e,this.client=e.client,this.stream=new s.ReadableStream({highWaterMark:3*this.client.parallelism,start:this._start.bind(this),pull:this._pull.bind(this),cancel:this._cancel.bind(this)})}build(){return this.stream.getReader()}async _start(e){this.controller=e,this.continuationToken=null,this.closeNextIteration=!1}async _pull(e){if(this.closeNextIteration)return void e.close();const t=await this.client.listObjects({prefix:`resource=${this.resource.name}`,continuationToken:this.continuationToken}),r=t?.Contents.map(e=>e.Key).map(e=>e.replace(this.client.config.keyPrefix,"")).map(e=>e.startsWith("/")?e.replace("/",""):e).map(e=>e.replace(`resource=${this.resource.name}/id=`,""));this.continuationToken=t.NextContinuationToken,this.enqueue(r),t.IsTruncated||(this.closeNextIteration=!0)}enqueue(e){e.forEach(e=>{this.controller.enqueue(e),this.emit("id",e)})}_cancel(e){}}var Gr=Jr;class Yr extends Gr{enqueue(e){this.controller.enqueue(e),this.emit("page",e)}}class Qr extends se{constructor({resource:e,batchSize:t=10,concurrency:r=5}){if(super(),!e)throw new Error("Resource is required for ResourceReader");this.resource=e,this.client=e.client,this.batchSize=t,this.concurrency=r,this.input=new Yr({resource:this.resource}),this.transform=new Hr({objectMode:!0,transform:this._transform.bind(this)}),this.input.on("data",e=>{this.transform.write(e)}),this.input.on("end",()=>{this.transform.end()}),this.input.on("error",e=>{this.emit("error",e)}),this.transform.on("data",e=>{this.emit("data",e)}),this.transform.on("end",()=>{this.emit("end")}),this.transform.on("error",e=>{this.emit("error",e)})}build(){return this}async _transform(e,t,r){const[s,n]=await K(async()=>{await i.PromisePool.for(e).withConcurrency(this.concurrency).handleError(async(e,t)=>{this.emit("error",e,t)}).process(async e=>{const t=await this.resource.get(e);return this.push(t),t})});r(n)}resume(){this.input.resume()}}class Zr extends se{constructor({resource:e,batchSize:t=10,concurrency:r=5}){super(),this.resource=e,this.client=e.client,this.batchSize=t,this.concurrency=r,this.buffer=[],this.writing=!1,this.writable=new Ar({objectMode:!0,write:this._write.bind(this)}),this.writable.on("finish",()=>{this.emit("finish")}),this.writable.on("error",e=>{this.emit("error",e)})}build(){return this}write(e){return this.buffer.push(e),this._maybeWrite().catch(e=>{this.emit("error",e)}),!0}end(){this.ended=!0,this._maybeWrite().catch(e=>{this.emit("error",e)})}async _maybeWrite(){if(!this.writing&&(0!==this.buffer.length||this.ended)){for(this.writing=!0;this.buffer.length>0;){const e=this.buffer.splice(0,this.batchSize),[t,r]=await K(async()=>{await i.PromisePool.for(e).withConcurrency(this.concurrency).handleError(async(e,t)=>{this.emit("error",e,t)}).process(async e=>{const[t,r,i]=await K(async()=>await this.resource.insert(e));return t?i:(this.emit("error",r,e),null)})});t||this.emit("error",r)}this.writing=!1,this.ended&&this.writable.emit("finish")}}async _write(e,t,r){r()}}function Xr(e){return new Promise((t,r)=>{if(!e)return r(new Error("streamToString: stream is undefined"));const i=[];e.on("data",e=>i.push(e)),e.on("error",r),e.on("end",()=>t(Buffer.concat(i).toString("utf-8")))})}var ei=class extends _e{constructor({client:e,keyPrefix:t="cache",ttl:r=0,prefix:i}){super(),this.client=e,this.keyPrefix=t,this.config.ttl=r,this.config.client=e,this.config.prefix=void 0!==i?i:t+(t.endsWith("/")?"":"/")}async _set(e,t){let i=JSON.stringify(t);const s=i.length;return i=r.gzipSync(i).toString("base64"),this.client.putObject({key:be(this.keyPrefix,e),body:i,contentEncoding:"gzip",contentType:"application/gzip",metadata:{compressor:"zlib",compressed:"true","client-id":this.client.id,"length-serialized":String(s),"length-compressed":String(i.length),"compression-gain":(i.length/s).toFixed(2)}})}async _get(e){const[t,i,s]=await K(async()=>{const{Body:t}=await this.client.getObject(be(this.keyPrefix,e));let i=await Xr(t);return i=Buffer.from(i,"base64"),i=r.unzipSync(i).toString(),JSON.parse(i)});if(t)return s;if("NoSuchKey"===i.name||"NotFound"===i.name)return null;throw i}async _del(e){return await this.client.deleteObject(be(this.keyPrefix,e)),!0}async _clear(){const e=await this.client.getAllKeys({prefix:this.keyPrefix});await this.client.deleteObjects(e)}async size(){return(await this.keys()).length}async keys(){const e=await this.client.getAllKeys({prefix:this.keyPrefix}),t=this.keyPrefix.endsWith("/")?this.keyPrefix:this.keyPrefix+"/";return e.map(e=>e.startsWith(t)?e.slice(t.length):e)}};var ti=class extends _e{constructor(e={}){super(e),this.cache={},this.meta={},this.maxSize=void 0!==e.maxSize?e.maxSize:1e3,this.ttl=void 0!==e.ttl?e.ttl:3e5}async _set(e,t){if(this.maxSize>0&&Object.keys(this.cache).length>=this.maxSize){const e=Object.entries(this.meta).sort((e,t)=>e[1].ts-t[1].ts)[0]?.[0];e&&(delete this.cache[e],delete this.meta[e])}return this.cache[e]=t,this.meta[e]={ts:Date.now()},t}async _get(e){if(!Object.prototype.hasOwnProperty.call(this.cache,e))return null;if(this.ttl>0){const t=Date.now(),r=this.meta[e];if(r&&t-r.ts>1e3*this.ttl)return delete this.cache[e],delete this.meta[e],null}return this.cache[e]}async _del(e){return delete this.cache[e],delete this.meta[e],!0}async _clear(e){if(!e)return this.cache={},this.meta={},!0;for(const t of Object.keys(this.cache))t.startsWith(e)&&(delete this.cache[t],delete this.meta[t]);return!0}async size(){return Object.keys(this.cache).length}async keys(){return Object.keys(this.cache)}},ri={};class ii extends _e{constructor({directory:e,prefix:t="cache",ttl:r=36e5,enableCompression:i=!0,compressionThreshold:s=1024,createDirectory:n=!0,fileExtension:o=".cache",enableMetadata:a=!0,maxFileSize:c=10485760,enableStats:l=!1,enableCleanup:u=!0,cleanupInterval:h=3e5,encoding:f="utf8",fileMode:d=420,enableBackup:p=!1,backupSuffix:m=".bak",enableLocking:g=!1,lockTimeout:y=5e3,enableJournal:b=!1,journalFile:w="cache.journal",...v}){if(super(v),!e)throw new Error("FilesystemCache: directory parameter is required");this.directory=we.resolve(e),this.prefix=t,this.ttl=r,this.enableCompression=i,this.compressionThreshold=s,this.createDirectory=n,this.fileExtension=o,this.enableMetadata=a,this.maxFileSize=c,this.enableStats=l,this.enableCleanup=u,this.cleanupInterval=h,this.encoding=f,this.fileMode=d,this.enableBackup=p,this.backupSuffix=m,this.enableLocking=g,this.lockTimeout=y,this.enableJournal=b,this.journalFile=we.join(this.directory,w),this.stats={hits:0,misses:0,sets:0,deletes:0,clears:0,errors:0},this.locks=new Map,this.cleanupTimer=null,this._init()}async _init(){this.createDirectory&&await this._ensureDirectory(this.directory),this.enableCleanup&&this.cleanupInterval>0&&(this.cleanupTimer=setInterval(()=>{this._cleanup().catch(e=>{console.warn("FilesystemCache cleanup error:",e.message)})},this.cleanupInterval))}async _ensureDirectory(e){const[t,r]=await K(async()=>{await n.mkdir(e,{recursive:!0})});if(!t&&"EEXIST"!==r.code)throw new Error(`Failed to create cache directory: ${r.message}`)}_getFilePath(e){const t=e.replace(/[<>:"/\\|?*]/g,"_"),r=`${this.prefix}_${t}${this.fileExtension}`;return we.join(this.directory,r)}_getMetadataPath(e){return e+".meta"}async _set(e,t){const i=this._getFilePath(e);try{let s=JSON.stringify(t);const o=Buffer.byteLength(s,this.encoding);if(o>this.maxFileSize)throw new Error(`Cache data exceeds maximum file size: ${o} > ${this.maxFileSize}`);let a=!1,c=s;if(this.enableCompression&&o>=this.compressionThreshold){c=r.gzipSync(Buffer.from(s,this.encoding)).toString("base64"),a=!0}if(this.enableBackup&&await this._fileExists(i)){const e=i+this.backupSuffix;await this._copyFile(i,e)}this.enableLocking&&await this._acquireLock(i);try{if(await n.writeFile(i,c,{encoding:a?"utf8":this.encoding,mode:this.fileMode}),this.enableMetadata){const t={key:e,timestamp:Date.now(),ttl:this.ttl,compressed:a,originalSize:o,compressedSize:a?Buffer.byteLength(c,"utf8"):o,compressionRatio:a?(Buffer.byteLength(c,"utf8")/o).toFixed(2):1};await n.writeFile(this._getMetadataPath(i),JSON.stringify(t),{encoding:this.encoding,mode:this.fileMode})}this.enableStats&&this.stats.sets++,this.enableJournal&&await this._journalOperation("set",e,{size:o,compressed:a})}finally{this.enableLocking&&this._releaseLock(i)}return t}catch(t){throw this.enableStats&&this.stats.errors++,new Error(`Failed to set cache key '${e}': ${t.message}`)}}async _get(e){const t=this._getFilePath(e);try{if(!await this._fileExists(t))return this.enableStats&&this.stats.misses++,null;let i=!1;if(this.enableMetadata){const e=this._getMetadataPath(t);if(await this._fileExists(e)){const[t,r,s]=await K(async()=>{const t=await n.readFile(e,this.encoding);return JSON.parse(t)});if(t&&s.ttl>0){i=Date.now()-s.timestamp>s.ttl}}}else if(this.ttl>0){const e=await n.stat(t);i=Date.now()-e.mtime.getTime()>this.ttl}if(i)return await this._del(e),this.enableStats&&this.stats.misses++,null;this.enableLocking&&await this._acquireLock(t);try{const e=await n.readFile(t,this.encoding);let i=!1;if(this.enableMetadata){const e=this._getMetadataPath(t);if(await this._fileExists(e)){const[t,r,s]=await K(async()=>{const t=await n.readFile(e,this.encoding);return JSON.parse(t)});t&&(i=s.compressed)}}let s=e;if(i||this.enableCompression&&e.match(/^[A-Za-z0-9+/=]+$/))try{const t=Buffer.from(e,"base64");s=r.gunzipSync(t).toString(this.encoding)}catch(t){s=e}const o=JSON.parse(s);return this.enableStats&&this.stats.hits++,o}finally{this.enableLocking&&this._releaseLock(t)}}catch(t){return this.enableStats&&this.stats.errors++,await this._del(e),null}}async _del(e){const t=this._getFilePath(e);try{if(await this._fileExists(t)&&await n.unlink(t),this.enableMetadata){const e=this._getMetadataPath(t);await this._fileExists(e)&&await n.unlink(e)}if(this.enableBackup){const e=t+this.backupSuffix;await this._fileExists(e)&&await n.unlink(e)}return this.enableStats&&this.stats.deletes++,this.enableJournal&&await this._journalOperation("delete",e),!0}catch(t){throw this.enableStats&&this.stats.errors++,new Error(`Failed to delete cache key '${e}': ${t.message}`)}}async _clear(e){try{if(!await this._fileExists(this.directory))return this.enableStats&&this.stats.clears++,!0;const t=(await n.readdir(this.directory)).filter(t=>{if(!t.startsWith(this.prefix))return!1;if(!t.endsWith(this.fileExtension))return!1;if(e){return t.slice(this.prefix.length+1,-this.fileExtension.length).startsWith(e)}return!0});for(const e of t){const t=we.join(this.directory,e);try{await this._fileExists(t)&&await n.unlink(t)}catch(e){if("ENOENT"!==e.code)throw e}if(this.enableMetadata)try{const e=this._getMetadataPath(t);await this._fileExists(e)&&await n.unlink(e)}catch(e){if("ENOENT"!==e.code)throw e}if(this.enableBackup)try{const e=t+this.backupSuffix;await this._fileExists(e)&&await n.unlink(e)}catch(e){if("ENOENT"!==e.code)throw e}}return this.enableStats&&this.stats.clears++,this.enableJournal&&await this._journalOperation("clear",e||"all",{count:t.length}),!0}catch(e){if("ENOENT"===e.code)return this.enableStats&&this.stats.clears++,!0;throw this.enableStats&&this.stats.errors++,new Error(`Failed to clear cache: ${e.message}`)}}async size(){return(await this.keys()).length}async keys(){try{const e=(await n.readdir(this.directory)).filter(e=>e.startsWith(this.prefix)&&e.endsWith(this.fileExtension));return e.map(e=>e.slice(this.prefix.length+1,-this.fileExtension.length))}catch(e){return console.warn("FilesystemCache: Failed to list keys:",e.message),[]}}async _fileExists(e){const[t]=await K(async()=>{await n.stat(e)});return t}async _copyFile(e,t){const[r,i]=await K(async()=>{const r=await n.readFile(e);await n.writeFile(t,r)});r||console.warn("FilesystemCache: Failed to create backup:",i.message)}async _cleanup(){if(this.ttl&&!(this.ttl<=0))try{const e=await n.readdir(this.directory),t=Date.now();for(const r of e){if(!r.startsWith(this.prefix)||!r.endsWith(this.fileExtension))continue;const e=we.join(this.directory,r);let i=!1;if(this.enableMetadata){const r=this._getMetadataPath(e);if(await this._fileExists(r)){const[e,s,o]=await K(async()=>{const e=await n.readFile(r,this.encoding);return JSON.parse(e)});if(e&&o.ttl>0){i=t-o.timestamp>o.ttl}}}else{const[r,s,o]=await K(async()=>await n.stat(e));if(r){i=t-o.mtime.getTime()>this.ttl}}if(i){const e=r.slice(this.prefix.length+1,-this.fileExtension.length);await this._del(e)}}}catch(e){console.warn("FilesystemCache cleanup error:",e.message)}}async _acquireLock(e){if(!this.enableLocking)return;const t=e,r=Date.now();for(;this.locks.has(t);){if(Date.now()-r>this.lockTimeout)throw new Error(`Lock timeout for file: ${e}`);await new Promise(e=>setTimeout(e,10))}this.locks.set(t,Date.now())}_releaseLock(e){this.enableLocking&&this.locks.delete(e)}async _journalOperation(e,t,r={}){if(!this.enableJournal)return;const i={timestamp:(new Date).toISOString(),operation:e,key:t,metadata:r},[s,n]=await K(async()=>{const e=JSON.stringify(i)+"\n";await ri.promises.appendFile(this.journalFile,e,this.encoding)});s||console.warn("FilesystemCache journal error:",n.message)}destroy(){this.cleanupTimer&&(clearInterval(this.cleanupTimer),this.cleanupTimer=null)}getStats(){return{...this.stats,directory:this.directory,ttl:this.ttl,compression:this.enableCompression,metadata:this.enableMetadata,cleanup:this.enableCleanup,locking:this.enableLocking,journal:this.enableJournal}}}class si extends ii{constructor({partitionStrategy:e="hierarchical",trackUsage:t=!0,preloadRelated:r=!1,preloadThreshold:i=10,maxCacheSize:s=null,usageStatsFile:n="partition-usage.json",...o}){super(o),this.partitionStrategy=e,this.trackUsage=t,this.preloadRelated=r,this.preloadThreshold=i,this.maxCacheSize=s,this.usageStatsFile=we.join(this.directory,n),this.partitionUsage=new Map,this.loadUsageStats()}_getPartitionCacheKey(e,t,r,i={},s={}){const n=[`resource=${e}`,`action=${t}`];if(r&&Object.keys(i).length>0){n.push(`partition=${r}`);const e=Object.entries(i).sort(([e],[t])=>e.localeCompare(t));for(const[t,r]of e)null!=r&&n.push(`${t}=${r}`)}if(Object.keys(s).length>0){const e=Object.entries(s).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>`${e}=${t}`).join("|");n.push(`params=${Buffer.from(e).toString("base64")}`)}return n.join("/")+this.fileExtension}_getPartitionDirectory(e,t,r={}){const i=we.join(this.directory,`resource=${e}`);if(!t)return i;if("flat"===this.partitionStrategy)return we.join(i,"partitions");if("temporal"===this.partitionStrategy&&this._isTemporalPartition(t,r))return this._getTemporalDirectory(i,t,r);const s=[i,`partition=${t}`],n=Object.entries(r).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of n)null!=t&&s.push(`${e}=${this._sanitizePathValue(t)}`);return we.join(...s)}async _set(e,t,r={}){const{resource:i,action:s,partition:n,partitionValues:o,params:a}=r;if(i&&n){const e=this._getPartitionCacheKey(i,s,n,o,a),r=this._getPartitionDirectory(i,n,o);await this._ensureDirectory(r);const c=we.join(r,this._sanitizeFileName(e));this.trackUsage&&await this._trackPartitionUsage(i,n,o);const l={data:t,metadata:{resource:i,partition:n,partitionValues:o,timestamp:Date.now(),ttl:this.ttl}};return this._writeFileWithMetadata(c,l)}return super._set(e,t)}async set(e,t,r,i={}){if("string"==typeof e&&"string"==typeof t&&i.partition){const s=this._getPartitionCacheKey(e,t,i.partition,i.partitionValues,i.params);return this._set(s,r,{resource:e,action:t,...i})}return super.set(e,t)}async get(e,t,r={}){if("string"==typeof e&&"string"==typeof t&&r.partition){const i=this._getPartitionCacheKey(e,t,r.partition,r.partitionValues,r.params);return this._get(i,{resource:e,action:t,...r})}return super.get(e)}async _get(e,t={}){const{resource:r,action:i,partition:s,partitionValues:n,params:o}=t;if(r&&s){const e=this._getPartitionCacheKey(r,i,s,n,o),t=this._getPartitionDirectory(r,s,n),a=we.join(t,this._sanitizeFileName(e));if(!await this._fileExists(a))return this.preloadRelated&&await this._preloadRelatedPartitions(r,s,n),null;const c=await this._readFileWithMetadata(a);return c&&this.trackUsage&&await this._trackPartitionUsage(r,s,n),c?.data||null}return super._get(e)}async clearPartition(e,t,r={}){const i=this._getPartitionDirectory(e,t,r),[s,o]=await K(async()=>{await this._fileExists(i)&&await n.rm(i,{recursive:!0})});s||console.warn(`Failed to clear partition cache: ${o.message}`);const a=this._getUsageKey(e,t,r);return this.partitionUsage.delete(a),await this._saveUsageStats(),s}async clearResourcePartitions(e){const t=we.join(this.directory,`resource=${e}`),[r,i]=await K(async()=>{await this._fileExists(t)&&await n.rm(t,{recursive:!0})});for(const[t]of this.partitionUsage.entries())t.startsWith(`${e}/`)&&this.partitionUsage.delete(t);return await this._saveUsageStats(),r}async getPartitionStats(e,t=null){const r={totalFiles:0,totalSize:0,partitions:{},usage:{}},i=we.join(this.directory,`resource=${e}`);if(!await this._fileExists(i))return r;await this._calculateDirectoryStats(i,r);for(const[i,s]of this.partitionUsage.entries())if(i.startsWith(`${e}/`)){const e=i.split("/")[1];t&&e!==t||(r.usage[e]=s)}return r}async getCacheRecommendations(e){const t=[],r=Date.now();for(const[i,s]of this.partitionUsage.entries())if(i.startsWith(`${e}/`)){const[,e]=i.split("/"),n=(r-s.lastAccess)/864e5,o=s.count/Math.max(1,n);let a="keep",c=s.count;n>30?(a="archive",c=0):o<.1?(a="reduce_ttl",c=1):o>10&&(a="preload",c=100),t.push({partition:e,recommendation:a,priority:c,usage:o,lastAccess:new Date(s.lastAccess).toISOString()})}return t.sort((e,t)=>t.priority-e.priority)}async warmPartitionCache(e,t={}){const{partitions:r=[],maxFiles:i=1e3}=t;let s=0;for(const t of r){const r=`${e}/${t}`,n=this.partitionUsage.get(r);if(n&&n.count>=this.preloadThreshold&&(console.log(`🔥 Warming cache for ${e}/${t} (${n.count} accesses)`),s++),s>=i)break}return s}async _trackPartitionUsage(e,t,r){const i=this._getUsageKey(e,t,r),s=this.partitionUsage.get(i)||{count:0,firstAccess:Date.now(),lastAccess:Date.now()};s.count++,s.lastAccess=Date.now(),this.partitionUsage.set(i,s),s.count%10==0&&await this._saveUsageStats()}_getUsageKey(e,t,r){const i=Object.entries(r).sort(([e],[t])=>e.localeCompare(t)).map(([e,t])=>`${e}=${t}`).join("|");return`${e}/${t}/${i}`}async _preloadRelatedPartitions(e,t,r){console.log(`🎯 Preloading related partitions for ${e}/${t}`),r.timestamp||r.date}_isTemporalPartition(e,t){const r=["date","timestamp","createdAt","updatedAt"];return Object.keys(t).some(e=>r.some(t=>e.toLowerCase().includes(t)))}_getTemporalDirectory(e,t,r){const i=Object.values(r)[0];if("string"==typeof i&&i.match(/^\d{4}-\d{2}-\d{2}/)){const[t,r,s]=i.split("-");return we.join(e,"temporal",t,r,s)}return we.join(e,`partition=${t}`)}_sanitizePathValue(e){return String(e).replace(/[<>:"/\\|?*]/g,"_")}_sanitizeFileName(e){return e.replace(/[<>:"/\\|?*]/g,"_")}async _calculateDirectoryStats(e,t){const[r,i,s]=await K(()=>n.readdir(e));if(r)for(const r of s){const i=we.join(e,r),[s,o,a]=await K(()=>n.stat(i));s&&(a.isDirectory()?await this._calculateDirectoryStats(i,t):(t.totalFiles++,t.totalSize+=a.size))}}async loadUsageStats(){const[e,t,r]=await K(async()=>{const e=await n.readFile(this.usageStatsFile,"utf8");return JSON.parse(e)});e&&r&&(this.partitionUsage=new Map(Object.entries(r)))}async _saveUsageStats(){const e=Object.fromEntries(this.partitionUsage);await K(async()=>{await n.writeFile(this.usageStatsFile,JSON.stringify(e,null,2),"utf8")})}async _writeFileWithMetadata(e,t){const r=JSON.stringify(t),[i,s]=await K(async()=>{await n.writeFile(e,r,{encoding:this.encoding,mode:this.fileMode})});if(!i)throw new Error(`Failed to write cache file: ${s.message}`);return!0}async _readFileWithMetadata(e){const[t,r,i]=await K(async()=>await n.readFile(e,this.encoding));if(!t||!i)return null;try{return JSON.parse(i)}catch(e){return{data:i}}}}const ni={async setup(e){e&&e.client&&(this.client=e.client,this.map={PutObjectCommand:"put",GetObjectCommand:"get",HeadObjectCommand:"head",DeleteObjectCommand:"delete",DeleteObjectsCommand:"delete",ListObjectsV2Command:"list"},this.costs={total:0,prices:{put:5e-6,copy:5e-6,list:5e-6,post:5e-6,get:4e-4/1e3,select:4e-4/1e3,delete:4e-4/1e3,head:4e-4/1e3},requests:{total:0,put:0,post:0,copy:0,list:0,get:0,select:0,delete:0,head:0},events:{total:0,PutObjectCommand:0,GetObjectCommand:0,HeadObjectCommand:0,DeleteObjectCommand:0,DeleteObjectsCommand:0,ListObjectsV2Command:0}},this.client.costs=JSON.parse(JSON.stringify(this.costs)))},async start(){this.client&&(this.client.on("command.response",e=>this.addRequest(e,this.map[e])),this.client.on("command.error",e=>this.addRequest(e,this.map[e])))},addRequest(e,t){t&&(this.costs.events[e]++,this.costs.events.total++,this.costs.requests.total++,this.costs.requests[t]++,this.costs.total+=this.costs.prices[t],this.client&&this.client.costs&&(this.client.costs.events[e]++,this.client.costs.events.total++,this.client.costs.requests.total++,this.client.costs.requests[t]++,this.client.costs.total+=this.client.costs.prices[t]))}};var oi=class extends se{constructor(e={}){super(),this.config=e,this.name=this.constructor.name,this.enabled=!1!==e.enabled}async initialize(e){this.database=e,this.emit("initialized",{replicator:this.name})}async replicate(e,t,r,i){throw new Error(`replicate() method must be implemented by ${this.name}`)}async replicateBatch(e,t){throw new Error(`replicateBatch() method must be implemented by ${this.name}`)}async testConnection(){throw new Error(`testConnection() method must be implemented by ${this.name}`)}async getStatus(){return{name:this.name,config:this.config,connected:!1}}async cleanup(){this.emit("cleanup",{replicator:this.name})}validateConfig(){return{isValid:!0,errors:[]}}};var ai=class extends oi{constructor(e={},t={}){super(e),this.projectId=e.projectId,this.datasetId=e.datasetId,this.bigqueryClient=null,this.credentials=e.credentials,this.location=e.location||"US",this.logTable=e.logTable,this.resources=this.parseResourcesConfig(t)}parseResourcesConfig(e){const t={};for(const[r,i]of Object.entries(e))"string"==typeof i?t[r]=[{table:i,actions:["insert"],transform:null}]:Array.isArray(i)?t[r]=i.map(e=>"string"==typeof e?{table:e,actions:["insert"],transform:null}:{table:e.table,actions:e.actions||["insert"],transform:e.transform||null}):"object"==typeof i&&(t[r]=[{table:i.table,actions:i.actions||["insert"],transform:i.transform||null}]);return t}validateConfig(){const e=[];this.projectId||e.push("projectId is required"),this.datasetId||e.push("datasetId is required"),0===Object.keys(this.resources).length&&e.push("At least one resource must be configured");for(const[t,r]of Object.entries(this.resources))for(const i of r){i.table||e.push(`Table name is required for resource '${t}'`),Array.isArray(i.actions)&&0!==i.actions.length||e.push(`Actions array is required for resource '${t}'`);const r=["insert","update","delete"],s=i.actions.filter(e=>!r.includes(e));s.length>0&&e.push(`Invalid actions for resource '${t}': ${s.join(", ")}. Valid actions: ${r.join(", ")}`),i.transform&&"function"!=typeof i.transform&&e.push(`Transform must be a function for resource '${t}'`)}return{isValid:0===e.length,errors:e}}async initialize(e){await super.initialize(e);const[t,r,i]=await K(()=>import("@google-cloud/bigquery"));if(!t)throw this.config.verbose&&console.warn(`[BigqueryReplicator] Failed to import BigQuery SDK: ${r.message}`),this.emit("initialization_error",{replicator:this.name,error:r.message}),r;const{BigQuery:s}=i;this.bigqueryClient=new s({projectId:this.projectId,credentials:this.credentials,location:this.location}),this.emit("initialized",{replicator:this.name,projectId:this.projectId,datasetId:this.datasetId,resources:Object.keys(this.resources)})}shouldReplicateResource(e){return this.resources.hasOwnProperty(e)}shouldReplicateAction(e,t){return!!this.resources[e]&&this.resources[e].some(e=>e.actions.includes(t))}getTablesForResource(e,t){return this.resources[e]?this.resources[e].filter(e=>e.actions.includes(t)).map(e=>({table:e.table,transform:e.transform})):[]}applyTransform(e,t){let r=this._cleanInternalFields(e);return t?t(JSON.parse(JSON.stringify(r))):r}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}async replicate(e,t,r,i,s=null){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};if(!this.shouldReplicateAction(e,t))return{skipped:!0,reason:"action_not_included"};const n=this.getTablesForResource(e,t);if(0===n.length)return{skipped:!0,reason:"no_tables_for_action"};const o=[],a=[],[c,l,u]=await K(async()=>{const s=this.bigqueryClient.dataset(this.datasetId);for(const e of n){const[n,c]=await K(async()=>{const n=s.table(e.table);let a;if("insert"===t){const t=this.applyTransform(r,e.transform);try{a=await n.insert([t])}catch(e){const{errors:t,response:r}=e;throw this.config.verbose&&(console.error("[BigqueryReplicator] BigQuery insert error details:"),t&&console.error(JSON.stringify(t,null,2)),r&&console.error(JSON.stringify(r,null,2))),e}}else if("update"===t){const t=this.applyTransform(r,e.transform),s=Object.keys(t).filter(e=>"id"!==e).map(e=>`${e} = @${e}`).join(", "),n={id:i,...t},o=`UPDATE \`${this.projectId}.${this.datasetId}.${e.table}\` SET ${s} WHERE id = @id`,c=2;let l=null;for(let e=1;e<=c;e++){const[t,r]=await K(async()=>{const[e]=await this.bigqueryClient.createQueryJob({query:o,params:n,location:this.location});return await e.getQueryResults(),[e]});if(t){a=t;break}if(l=r,this.config.verbose&&(console.warn(`[BigqueryReplicator] Update attempt ${e} failed: ${r.message}`),r.errors&&(console.error("[BigqueryReplicator] BigQuery update error details:"),console.error("Errors:",JSON.stringify(r.errors,null,2)))),r?.message?.includes("streaming buffer")&&e<c){const e=30;this.config.verbose&&console.warn(`[BigqueryReplicator] Retrying in ${e} seconds due to streaming buffer issue`),await new Promise(t=>setTimeout(t,1e3*e));continue}throw r}if(!a)throw l}else{if("delete"!==t)throw new Error(`Unsupported operation: ${t}`);{const t=`DELETE FROM \`${this.projectId}.${this.datasetId}.${e.table}\` WHERE id = @id`;try{const[e]=await this.bigqueryClient.createQueryJob({query:t,params:{id:i},location:this.location});await e.getQueryResults(),a=[e]}catch(e){throw this.config.verbose&&(console.error("[BigqueryReplicator] BigQuery delete error details:"),console.error("Query:",t),e.errors&&console.error("Errors:",JSON.stringify(e.errors,null,2)),e.response&&console.error("Response:",JSON.stringify(e.response,null,2))),e}}}o.push({table:e.table,success:!0,jobId:a[0]?.id})});n||a.push({table:e.table,error:c.message})}if(this.logTable){const[n,o]=await K(async()=>{const n=s.table(this.logTable);await n.insert([{resource_name:e,operation:t,record_id:i,data:JSON.stringify(r),timestamp:(new Date).toISOString(),source:"s3db-replicator"}])})}const c=0===a.length;return a.length>0&&console.warn(`[BigqueryReplicator] Replication completed with errors for ${e}:`,a),this.emit("replicated",{replicator:this.name,resourceName:e,operation:t,id:i,tables:n.map(e=>e.table),results:o,errors:a,success:c}),{success:c,results:o,errors:a,tables:n.map(e=>e.table)}});return c?u:(this.config.verbose&&console.warn(`[BigqueryReplicator] Replication failed for ${e}: ${l.message}`),this.emit("replicator_error",{replicator:this.name,resourceName:e,operation:t,id:i,error:l.message}),{success:!1,error:l.message})}async replicateBatch(e,t){const r=[],i=[];for(const s of t){const[t,n,o]=await K(()=>this.replicate(e,s.operation,s.data,s.id,s.beforeData));t?r.push(o):(this.config.verbose&&console.warn(`[BigqueryReplicator] Batch replication failed for record ${s.id}: ${n.message}`),i.push({id:s.id,error:n.message}))}return i.length>0&&console.warn(`[BigqueryReplicator] Batch replication completed with ${i.length} error(s) for ${e}:`,i),{success:0===i.length,results:r,errors:i}}async testConnection(){const[e,t]=await K(async()=>{this.bigqueryClient||await this.initialize();const e=this.bigqueryClient.dataset(this.datasetId);return await e.getMetadata(),!0});return!!e||(this.config.verbose&&console.warn(`[BigqueryReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}async cleanup(){}getStatus(){return{...super.getStatus(),projectId:this.projectId,datasetId:this.datasetId,resources:this.resources,logTable:this.logTable}}};var ci=class extends oi{constructor(e={},t={}){super(e),this.connectionString=e.connectionString,this.host=e.host,this.port=e.port||5432,this.database=e.database,this.user=e.user,this.password=e.password,this.client=null,this.ssl=e.ssl,this.logTable=e.logTable,this.resources=this.parseResourcesConfig(t)}parseResourcesConfig(e){const t={};for(const[r,i]of Object.entries(e))"string"==typeof i?t[r]=[{table:i,actions:["insert"]}]:Array.isArray(i)?t[r]=i.map(e=>"string"==typeof e?{table:e,actions:["insert"]}:{table:e.table,actions:e.actions||["insert"]}):"object"==typeof i&&(t[r]=[{table:i.table,actions:i.actions||["insert"]}]);return t}validateConfig(){const e=[];this.connectionString||this.host&&this.database||e.push("Either connectionString or host+database must be provided"),0===Object.keys(this.resources).length&&e.push("At least one resource must be configured");for(const[t,r]of Object.entries(this.resources))for(const i of r){i.table||e.push(`Table name is required for resource '${t}'`),Array.isArray(i.actions)&&0!==i.actions.length||e.push(`Actions array is required for resource '${t}'`);const r=["insert","update","delete"],s=i.actions.filter(e=>!r.includes(e));s.length>0&&e.push(`Invalid actions for resource '${t}': ${s.join(", ")}. Valid actions: ${r.join(", ")}`)}return{isValid:0===e.length,errors:e}}async initialize(e){await super.initialize(e);const[t,r,i]=await K(()=>import("pg"));if(!t)throw this.config.verbose&&console.warn(`[PostgresReplicator] Failed to import pg SDK: ${r.message}`),this.emit("initialization_error",{replicator:this.name,error:r.message}),r;const{Client:s}=i,n=this.connectionString?{connectionString:this.connectionString,ssl:this.ssl}:{host:this.host,port:this.port,database:this.database,user:this.user,password:this.password,ssl:this.ssl};this.client=new s(n),await this.client.connect(),this.logTable&&await this.createLogTableIfNotExists(),this.emit("initialized",{replicator:this.name,database:this.database||"postgres",resources:Object.keys(this.resources)})}async createLogTableIfNotExists(){const e=`\n CREATE TABLE IF NOT EXISTS ${this.logTable} (\n id SERIAL PRIMARY KEY,\n resource_name VARCHAR(255) NOT NULL,\n operation VARCHAR(50) NOT NULL,\n record_id VARCHAR(255) NOT NULL,\n data JSONB,\n timestamp TIMESTAMP WITH TIME ZONE DEFAULT NOW(),\n source VARCHAR(100) DEFAULT 's3db-replicator',\n created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n );\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_resource_name ON ${this.logTable}(resource_name);\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_operation ON ${this.logTable}(operation);\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_record_id ON ${this.logTable}(record_id);\n CREATE INDEX IF NOT EXISTS idx_${this.logTable}_timestamp ON ${this.logTable}(timestamp);\n `;await this.client.query(e)}shouldReplicateResource(e){return this.resources.hasOwnProperty(e)}shouldReplicateAction(e,t){return!!this.resources[e]&&this.resources[e].some(e=>e.actions.includes(t))}getTablesForResource(e,t){return this.resources[e]?this.resources[e].filter(e=>e.actions.includes(t)).map(e=>e.table):[]}async replicate(e,t,r,i,s=null){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};if(!this.shouldReplicateAction(e,t))return{skipped:!0,reason:"action_not_included"};const n=this.getTablesForResource(e,t);if(0===n.length)return{skipped:!0,reason:"no_tables_for_action"};const o=[],a=[],[c,l,u]=await K(async()=>{for(const e of n){const[s,n]=await K(async()=>{let s;if("insert"===t){const t=this._cleanInternalFields(r),i=Object.keys(t),n=i.map(e=>t[e]),o=i.map(e=>`"${e}"`).join(", "),a=i.map((e,t)=>`$${t+1}`).join(", "),c=`INSERT INTO ${e} (${o}) VALUES (${a}) ON CONFLICT (id) DO NOTHING RETURNING *`;s=await this.client.query(c,n)}else if("update"===t){const t=this._cleanInternalFields(r),n=Object.keys(t).filter(e=>"id"!==e),o=n.map((e,t)=>`"${e}"=$${t+1}`).join(", "),a=n.map(e=>t[e]);a.push(i);const c=`UPDATE ${e} SET ${o} WHERE id=$${n.length+1} RETURNING *`;s=await this.client.query(c,a)}else{if("delete"!==t)throw new Error(`Unsupported operation: ${t}`);{const t=`DELETE FROM ${e} WHERE id=$1 RETURNING *`;s=await this.client.query(t,[i])}}o.push({table:e,success:!0,rows:s.rows,rowCount:s.rowCount})});s||a.push({table:e,error:n.message})}if(this.logTable){const[s,n]=await K(async()=>{await this.client.query(`INSERT INTO ${this.logTable} (resource_name, operation, record_id, data, timestamp, source) VALUES ($1, $2, $3, $4, $5, $6)`,[e,t,i,JSON.stringify(r),(new Date).toISOString(),"s3db-replicator"])})}const s=0===a.length;return a.length>0&&console.warn(`[PostgresReplicator] Replication completed with errors for ${e}:`,a),this.emit("replicated",{replicator:this.name,resourceName:e,operation:t,id:i,tables:n,results:o,errors:a,success:s}),{success:s,results:o,errors:a,tables:n}});return c?u:(this.config.verbose&&console.warn(`[PostgresReplicator] Replication failed for ${e}: ${l.message}`),this.emit("replicator_error",{replicator:this.name,resourceName:e,operation:t,id:i,error:l.message}),{success:!1,error:l.message})}async replicateBatch(e,t){const r=[],i=[];for(const s of t){const[t,n,o]=await K(()=>this.replicate(e,s.operation,s.data,s.id,s.beforeData));t?r.push(o):(this.config.verbose&&console.warn(`[PostgresReplicator] Batch replication failed for record ${s.id}: ${n.message}`),i.push({id:s.id,error:n.message}))}return i.length>0&&console.warn(`[PostgresReplicator] Batch replication completed with ${i.length} error(s) for ${e}:`,i),{success:0===i.length,results:r,errors:i}}async testConnection(){const[e,t]=await K(async()=>(this.client||await this.initialize(),await this.client.query("SELECT 1"),!0));return!!e||(this.config.verbose&&console.warn(`[PostgresReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}async cleanup(){this.client&&await this.client.end()}getStatus(){return{...super.getStatus(),database:this.database||"postgres",resources:this.resources,logTable:this.logTable}}};function li(){}function ui(){}li.defaultMaxSockets=4,ui.defaultMaxSockets=4;class hi{constructor(e){let t;const[r,i,s]=K(()=>new URL(e));if(!r)throw new U("Invalid connection string: "+e,{original:i,input:e});t=s,this.region="us-east-1","s3:"===t.protocol?this.defineFromS3(t):this.defineFromCustomUri(t);for(const[e,r]of t.searchParams.entries())this[e]=r}defineFromS3(e){const[t,r,i]=V(()=>decodeURIComponent(e.hostname));if(!t)throw new U("Invalid bucket in connection string",{original:r,input:e.hostname});this.bucket=i||"s3db";const[s,n,o]=V(()=>decodeURIComponent(e.username));if(!s)throw new U("Invalid accessKeyId in connection string",{original:n,input:e.username});this.accessKeyId=o;const[a,c,l]=V(()=>decodeURIComponent(e.password));if(!a)throw new U("Invalid secretAccessKey in connection string",{original:c,input:e.password});if(this.secretAccessKey=l,this.endpoint="https://s3.us-east-1.amazonaws.com",["/","",null].includes(e.pathname))this.keyPrefix="";else{let[,...t]=e.pathname.split("/");this.keyPrefix=[...t||[]].join("/")}}defineFromCustomUri(e){this.forcePathStyle=!0,this.endpoint=e.origin;const[t,r,i]=V(()=>decodeURIComponent(e.username));if(!t)throw new U("Invalid accessKeyId in connection string",{original:r,input:e.username});this.accessKeyId=i;const[s,n,o]=V(()=>decodeURIComponent(e.password));if(!s)throw new U("Invalid secretAccessKey in connection string",{original:n,input:e.password});if(this.secretAccessKey=o,["/","",null].includes(e.pathname))this.bucket="s3db",this.keyPrefix="";else{let[,t,...r]=e.pathname.split("/");if(t){const[e,r,i]=V(()=>decodeURIComponent(t));if(!e)throw new U("Invalid bucket in connection string",{original:r,input:t});this.bucket=i}else this.bucket="s3db";this.keyPrefix=[...r||[]].join("/")}}}class fi extends se{constructor({verbose:e=!1,id:t=null,AwsS3Client:r,connectionString:i,parallelism:s=10,httpClientOptions:n={}}){super(),this.verbose=e,this.id=t??X(77),this.parallelism=s,this.config=new hi(i),this.httpClientOptions={keepAlive:!0,keepAliveMsecs:1e3,maxSockets:50,maxFreeSockets:10,timeout:6e4,...n},this.client=r||this.createClient()}createClient(){const e=new li(this.httpClientOptions),t=new ui(this.httpClientOptions),r=new l.NodeHttpHandler({httpAgent:e,httpsAgent:t});let i={region:this.config.region,endpoint:this.config.endpoint,requestHandler:r};this.config.forcePathStyle&&(i.forcePathStyle=!0),this.config.accessKeyId&&(i.credentials={accessKeyId:this.config.accessKeyId,secretAccessKey:this.config.secretAccessKey});const s=new u.S3Client(i);return s.middlewareStack.add((e,t)=>async r=>{if("DeleteObjectsCommand"===t.commandName){const e=r.request.body;if(e&&"string"==typeof e){const t=await Q(e);r.request.headers["Content-MD5"]=t}}return e(r)},{step:"build",name:"addContentMd5ForDeleteObjects",priority:"high"}),s}async sendCommand(e){this.emit("command.request",e.constructor.name,e.input);const[t,r,i]=await K(()=>this.client.send(e));if(!t){throw L(r,{bucket:this.config.bucket,key:e.input&&e.input.Key,commandName:e.constructor.name,commandInput:e.input})}return this.emit("command.response",e.constructor.name,i,e.input),i}async putObject({key:e,metadata:t,contentType:r,body:i,contentEncoding:s,contentLength:n}){const o="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"";o&&we.join(o,e);const a={};if(t)for(const[e,r]of Object.entries(t)){const t=String(e).replace(/[^a-zA-Z0-9\-_]/g,"_"),i=String(r),s=/[^\x00-\x7F]/.test(i);a[t]=s?Buffer.from(i,"utf8").toString("base64"):i}const c={Bucket:this.config.bucket,Key:o?we.join(o,e):e,Metadata:a,Body:i||Buffer.alloc(0)};let l,h;void 0!==r&&(c.ContentType=r),void 0!==s&&(c.ContentEncoding=s),void 0!==n&&(c.ContentLength=n);try{return l=await this.sendCommand(new u.PutObjectCommand(c)),l}catch(t){throw h=t,L(t,{bucket:this.config.bucket,key:e,commandName:"PutObjectCommand",commandInput:c})}finally{this.emit("putObject",h||l,{key:e,metadata:t,contentType:r,body:i,contentEncoding:s,contentLength:n})}}async getObject(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"",r={Bucket:this.config.bucket,Key:t?we.join(t,e):e};let i,s;try{if(i=await this.sendCommand(new u.GetObjectCommand(r)),i.Metadata){const e={};for(const[t,r]of Object.entries(i.Metadata))if("string"==typeof r)try{const i=Buffer.from(r,"base64").toString("utf8"),s=/[^\x00-\x7F]/.test(i),n=Buffer.from(i,"utf8").toString("base64")===r;e[t]=n&&s&&i!==r?i:r}catch(i){e[t]=r}else e[t]=r;i.Metadata=e}return i}catch(t){throw s=t,L(t,{bucket:this.config.bucket,key:e,commandName:"GetObjectCommand",commandInput:r})}finally{this.emit("getObject",s||i,{key:e})}}async headObject(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"",r={Bucket:this.config.bucket,Key:t?we.join(t,e):e};let i,s;try{return i=await this.sendCommand(new u.HeadObjectCommand(r)),i}catch(t){throw s=t,L(t,{bucket:this.config.bucket,key:e,commandName:"HeadObjectCommand",commandInput:r})}finally{this.emit("headObject",s||i,{key:e})}}async copyObject({from:e,to:t}){const r={Bucket:this.config.bucket,Key:this.config.keyPrefix?we.join(this.config.keyPrefix,t):t,CopySource:we.join(this.config.bucket,this.config.keyPrefix?we.join(this.config.keyPrefix,e):e)};let i,s;try{return i=await this.sendCommand(new u.CopyObjectCommand(r)),i}catch(e){throw s=e,L(e,{bucket:this.config.bucket,key:t,commandName:"CopyObjectCommand",commandInput:r})}finally{this.emit("copyObject",s||i,{from:e,to:t})}}async exists(e){const[t,r]=await K(()=>this.headObject(e));if(t)return!0;if("NoSuchKey"===r.name||"NotFound"===r.name)return!1;throw r}async deleteObject(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"";t&&we.join(t,e);const r={Bucket:this.config.bucket,Key:t?we.join(t,e):e};let i,s;try{return i=await this.sendCommand(new u.DeleteObjectCommand(r)),i}catch(t){throw s=t,L(t,{bucket:this.config.bucket,key:e,commandName:"DeleteObjectCommand",commandInput:r})}finally{this.emit("deleteObject",s||i,{key:e})}}async deleteObjects(e){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"",r=a.chunk(e,1e3),{results:s,errors:n}=await i.PromisePool.for(r).withConcurrency(this.parallelism).process(async e=>{for(const r of e)t&&we.join(t,r),this.config.bucket,await this.exists(r);const r={Bucket:this.config.bucket,Delete:{Objects:e.map(e=>({Key:t?we.join(t,e):e}))}};let i;const[s,n,o]=await K(()=>this.sendCommand(new u.DeleteObjectsCommand(r)));if(!s)throw n;return i=o,i&&i.Errors&&i.Errors.length,i&&i.Deleted&&(i.Deleted.length,e.length),i}),o={deleted:s,notFound:n};return this.emit("deleteObjects",o,e),o}async deleteAll({prefix:e}={}){const t="string"==typeof this.config.keyPrefix?this.config.keyPrefix:"";let r,i=0;do{const s=new u.ListObjectsV2Command({Bucket:this.config.bucket,Prefix:t?we.join(t,e||""):e||"",ContinuationToken:r}),n=await this.client.send(s);if(n.Contents&&n.Contents.length>0){const t=new u.DeleteObjectsCommand({Bucket:this.config.bucket,Delete:{Objects:n.Contents.map(e=>({Key:e.Key}))}}),r=await this.client.send(t),s=r.Deleted?r.Deleted.length:0;i+=s,this.emit("deleteAll",{prefix:e,batch:s,total:i})}r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return this.emit("deleteAllComplete",{prefix:e,totalDeleted:i}),i}async moveObject({from:e,to:t}){const[r,i]=await K(async()=>{await this.copyObject({from:e,to:t}),await this.deleteObject(e)});if(!r)throw new T("Unknown error in moveObject",{bucket:this.config.bucket,from:e,to:t,original:i});return!0}async listObjects({prefix:e,maxKeys:t=1e3,continuationToken:r}={}){const i={Bucket:this.config.bucket,MaxKeys:t,ContinuationToken:r,Prefix:this.config.keyPrefix?we.join(this.config.keyPrefix,e||""):e||""},[s,n,o]=await K(()=>this.sendCommand(new u.ListObjectsV2Command(i)));if(!s)throw new T("Unknown error in listObjects",{prefix:e,bucket:this.config.bucket,original:n});return this.emit("listObjects",o,i),o}async count({prefix:e}={}){let t,r=0,i=!0;for(;i;){const s={prefix:e,continuationToken:t},n=await this.listObjects(s);r+=n.KeyCount||0,i=n.IsTruncated||!1,t=n.NextContinuationToken}return this.emit("count",r,{prefix:e}),r}async getAllKeys({prefix:e}={}){let t,r=[],i=!0;for(;i;){const s={prefix:e,continuationToken:t},n=await this.listObjects(s);n.Contents&&(r=r.concat(n.Contents.map(e=>e.Key))),i=n.IsTruncated||!1,t=n.NextContinuationToken}return this.config.keyPrefix&&(r=r.map(e=>e.replace(this.config.keyPrefix,"")).map(e=>e.startsWith("/")?e.replace("/",""):e)),this.emit("getAllKeys",r,{prefix:e}),r}async getContinuationTokenAfterOffset(e={}){const{prefix:t,offset:r=1e3}=e;if(0===r)return null;let i,s=!0,n=0;for(;s;){const e={prefix:t,maxKeys:r<1e3?r:r-n>1e3?1e3:r-n,continuationToken:i},o=await this.listObjects(e);if(o.Contents&&(n+=o.Contents.length),s=o.IsTruncated||!1,i=o.NextContinuationToken,n>=r)break}return this.emit("getContinuationTokenAfterOffset",i||null,e),i||null}async getKeysPage(e={}){const{prefix:t,offset:r=0,amount:i=100}=e;let s,n=[],o=!0;if(r>0&&(s=await this.getContinuationTokenAfterOffset({prefix:t,offset:r}),!s))return this.emit("getKeysPage",[],e),[];for(;o;){const e={prefix:t,continuationToken:s},r=await this.listObjects(e);if(r.Contents&&(n=n.concat(r.Contents.map(e=>e.Key))),o=r.IsTruncated||!1,s=r.NextContinuationToken,n.length>=i){n=n.slice(0,i);break}}return this.config.keyPrefix&&(n=n.map(e=>e.replace(this.config.keyPrefix,"")).map(e=>e.startsWith("/")?e.replace("/",""):e)),this.emit("getKeysPage",n,e),n}async moveAllObjects({prefixFrom:e,prefixTo:t}){const r=await this.getAllKeys({prefix:e}),{results:s,errors:n}=await i.PromisePool.for(r).withConcurrency(this.parallelism).process(async r=>{const i=r.replace(e,t),[s,n]=await K(async()=>{await this.moveObject({from:r,to:i})});if(!s)throw new T("Unknown error in moveAllObjects",{bucket:this.config.bucket,from:r,to:i,original:n});return i});if(this.emit("moveAllObjects",{results:s,errors:n},{prefixFrom:e,prefixTo:t}),n.length>0)throw new Error("Some objects could not be moved");return s}}var di=fi;async function pi(e,t,r){if(!this.passphrase)return t.push(new E("Missing configuration for secrets encryption.",{actual:e,type:"encryptionKeyMissing",suggestion:"Provide a passphrase for secret encryption."})),e;const[i,s,n]=await K(()=>G(String(e),this.passphrase));return i?n:(t.push(new E("Problem encrypting secret.",{actual:e,type:"encryptionProblem",error:s,suggestion:"Check the passphrase and input value."})),e)}async function mi(e,t,r){if(a.isString(e))return e;const[i,s,n]=V(()=>JSON.stringify(e));if(!i)throw new E("Failed to stringify JSON",{original:s,input:e});return n}class gi extends f{constructor({options:e,passphrase:t,autoEncrypt:r=!0}={}){super(a.merge({},{useNewCustomCheckerFunction:!0,messages:{encryptionKeyMissing:"Missing configuration for secrets encryption.",encryptionProblem:"Problem encrypting secret. Actual: {actual}. Error: {error}"},defaults:{string:{trim:!0},object:{strict:"remove"},number:{convert:!0}}},e)),this.passphrase=t,this.autoEncrypt=r,this.alias("secret",{type:"string",custom:this.autoEncrypt?pi:void 0,messages:{string:"The '{field}' field must be a string.",stringMin:"This secret '{field}' field length must be at least {expected} long."}}),this.alias("secretAny",{type:"any",custom:this.autoEncrypt?pi:void 0}),this.alias("secretNumber",{type:"number",custom:this.autoEncrypt?pi:void 0}),this.alias("json",{type:"any",custom:this.autoEncrypt?mi:void 0})}}const yi=new Proxy(gi,{instance:null,construct(e,t){return this.instance||(this.instance=new e(...t)),this.instance}});const bi={trim:e=>null==e?e:e.trim(),encrypt:async(e,{passphrase:t})=>{if(null==e)return e;const[r,i,s]=await H(()=>G(e,t));return r?s:e},decrypt:async(e,{passphrase:t})=>{if(null==e)return e;const[r,i,s]=await H(()=>Y(e,t));return r?"null"===s?null:"undefined"!==s?s:void 0:e},toString:e=>null==e?e:String(e),fromArray:(e,{separator:t})=>{if(null==e||!Array.isArray(e))return e;if(0===e.length)return"";return e.map(e=>"string"==typeof e?e.replace(/\\/g,"\\\\").replace(new RegExp(`\\${t}`,"g"),`\\${t}`):String(e)).join(t)},toArray:(e,{separator:t})=>{if(Array.isArray(e))return e;if(null==e)return e;if(""===e)return[];const r=[];let i="",s=0;const n=String(e);for(;s<n.length;)"\\"===n[s]&&s+1<n.length?(i+=n[s+1],s+=2):n[s]===t?(r.push(i),i="",s++):(i+=n[s],s++);return r.push(i),r},toJSON:e=>{if(null===e)return null;if(void 0===e)return;if("string"==typeof e){const[t,r,i]=V(()=>JSON.parse(e));return e}const[t,r,i]=V(()=>JSON.stringify(e));return t?i:e},fromJSON:e=>{if(null===e)return null;if(void 0===e)return;if("string"!=typeof e)return e;if(""===e)return"";const[t,r,i]=V(()=>JSON.parse(e));return t?i:e},toNumber:e=>a.isString(e)?e.includes(".")?parseFloat(e):parseInt(e):e,toBool:e=>[!0,1,"true","1","yes","y"].includes(e),fromBool:e=>[!0,1,"true","1","yes","y"].includes(e)?"1":"0",fromBase62:e=>{if(null==e||""===e)return e;if("number"==typeof e)return e;if("string"==typeof e){const t=g(e);return isNaN(t)?void 0:t}},toBase62:e=>{if(null==e||""===e)return e;if("number"==typeof e)return m(e);if("string"==typeof e){const t=Number(e);return isNaN(t)?e:m(t)}return e},fromBase62Decimal:e=>{if(null==e||""===e)return e;if("number"==typeof e)return e;if("string"==typeof e){const t=b(e);return isNaN(t)?void 0:t}},toBase62Decimal:e=>{if(null==e||""===e)return e;if("number"==typeof e)return y(e);if("string"==typeof e){const t=Number(e);return isNaN(t)?e:y(t)}return e},fromArrayOfNumbers:(e,{separator:t})=>{if(null==e||!Array.isArray(e))return e;if(0===e.length)return"";return e.map(e=>{if("number"==typeof e&&!isNaN(e))return m(e);const t=Number(e);return isNaN(t)?"":m(t)}).join(t)},toArrayOfNumbers:(e,{separator:t})=>{if(Array.isArray(e))return e.map(e=>"number"==typeof e?e:g(e));if(null==e)return e;if(""===e)return[];const r=String(e),i=[];let s="",n=0;for(;n<r.length;)"\\"===r[n]&&n+1<r.length?(s+=r[n+1],n+=2):r[n]===t?(i.push(s),s="",n++):(s+=r[n],n++);return i.push(s),i.map(e=>{if("number"==typeof e)return e;if("string"==typeof e&&""!==e){const t=g(e);return isNaN(t)?NaN:t}return NaN})},fromArrayOfDecimals:(e,{separator:t})=>{if(null==e||!Array.isArray(e))return e;if(0===e.length)return"";return e.map(e=>{if("number"==typeof e&&!isNaN(e))return y(e);const t=Number(e);return isNaN(t)?"":y(t)}).join(t)},toArrayOfDecimals:(e,{separator:t})=>{if(Array.isArray(e))return e.map(e=>"number"==typeof e?e:b(e));if(null==e)return e;if(""===e)return[];const r=String(e),i=[];let s="",n=0;for(;n<r.length;)"\\"===r[n]&&n+1<r.length?(s+=r[n+1],n+=2):r[n]===t?(i.push(s),s="",n++):(s+=r[n],n++);return i.push(s),i.map(e=>{if("number"==typeof e)return e;if("string"==typeof e&&""!==e){const t=b(e);return isNaN(t)?NaN:t}return NaN})}};class wi{constructor(e){const{map:t,name:r,attributes:i,passphrase:s,version:n=1,options:o={}}=e;this.name=r,this.version=n,this.attributes=i||{},this.passphrase=s??"secret",this.options=a.merge({},this.defaultOptions(),o),this.allNestedObjectsOptional=this.options.allNestedObjectsOptional??!1;const c=this.preprocessAttributesForValidation(this.attributes);if(this.validator=new yi({autoEncrypt:!1}).compile(a.merge({$$async:!0},c)),this.options.generateAutoHooks&&this.generateAutoHooks(),a.isEmpty(t)){const e=h.flatten(this.attributes,{safe:!0}),t=Object.keys(e).filter(e=>!e.includes("$$")),r=this.extractObjectKeys(this.attributes),i=[...new Set([...t,...r])],{mapping:s,reversedMapping:n}=function(e){const t={},r={};return e.forEach((e,i)=>{const s=m(i);t[e]=s,r[s]=e}),{mapping:t,reversedMapping:r}}(i);this.map=s,this.reversedMap=n}else this.map=t,this.reversedMap=a.invert(t)}defaultOptions(){return{autoEncrypt:!0,autoDecrypt:!0,arraySeparator:"|",generateAutoHooks:!0,hooks:{beforeMap:{},afterMap:{},beforeUnmap:{},afterUnmap:{}}}}addHook(e,t,r){this.options.hooks[e][t]||(this.options.hooks[e][t]=[]),this.options.hooks[e][t]=a.uniq([...this.options.hooks[e][t],r])}extractObjectKeys(e,t=""){const r=[];for(const[i,s]of Object.entries(e)){if(i.startsWith("$$"))continue;const e=t?`${t}.${i}`:i;"object"!=typeof s||null===s||Array.isArray(s)||(r.push(e),"object"===s.$$type&&r.push(...this.extractObjectKeys(s,e)))}return r}generateAutoHooks(){const e=h.flatten(a.cloneDeep(this.attributes),{safe:!0});for(const[t,r]of Object.entries(e))if(r.includes("array")){if(r.includes("items:string"))this.addHook("beforeMap",t,"fromArray"),this.addHook("afterUnmap",t,"toArray");else if(r.includes("items:number")){r.includes("integer:true")||r.includes("|integer:")||r.includes("|integer")?(this.addHook("beforeMap",t,"fromArrayOfNumbers"),this.addHook("afterUnmap",t,"toArrayOfNumbers")):(this.addHook("beforeMap",t,"fromArrayOfDecimals"),this.addHook("afterUnmap",t,"toArrayOfDecimals"))}}else if(r.includes("secret"))this.options.autoEncrypt&&this.addHook("beforeMap",t,"encrypt"),this.options.autoDecrypt&&this.addHook("afterUnmap",t,"decrypt");else{if(r.includes("number")){r.includes("integer:true")||r.includes("|integer:")||r.includes("|integer")?(this.addHook("beforeMap",t,"toBase62"),this.addHook("afterUnmap",t,"fromBase62")):(this.addHook("beforeMap",t,"toBase62Decimal"),this.addHook("afterUnmap",t,"fromBase62Decimal"));continue}r.includes("boolean")?(this.addHook("beforeMap",t,"fromBool"),this.addHook("afterUnmap",t,"toBool")):(r.includes("json")||"object"===r||r.includes("object"))&&(this.addHook("beforeMap",t,"toJSON"),this.addHook("afterUnmap",t,"fromJSON"))}}static import(e){let{map:t,name:r,options:i,version:s,attributes:n}=a.isString(e)?JSON.parse(e):e;const[o,c,l]=V(()=>wi._importAttributes(n));if(!o)throw new B("Failed to import schema attributes",{original:c,input:n});n=l;return new wi({map:t,name:r,options:i,version:s,attributes:n})}static _importAttributes(e){if("string"==typeof e){const[t,r,i]=V(()=>JSON.parse(e));if(t&&"object"==typeof i&&null!==i){const[t,r,s]=V(()=>wi._importAttributes(i));if(!t)throw new B("Failed to parse nested schema attribute",{original:r,input:e});return s}return e}if(Array.isArray(e)){const[t,r,i]=V(()=>e.map(e=>wi._importAttributes(e)));if(!t)throw new B("Failed to import array schema attributes",{original:r,input:e});return i}if("object"==typeof e&&null!==e){const t={};for(const[r,i]of Object.entries(e)){const[e,s,n]=V(()=>wi._importAttributes(i));if(!e)throw new B("Failed to import object schema attribute",{original:s,key:r,input:i});t[r]=n}return t}return e}export(){return{version:this.version,name:this.name,options:this.options,attributes:this._exportAttributes(this.attributes),map:this.map}}_exportAttributes(e){if("string"==typeof e)return e;if(Array.isArray(e))return e.map(e=>this._exportAttributes(e));if("object"==typeof e&&null!==e){const t={};for(const[r,i]of Object.entries(e))t[r]=this._exportAttributes(i);return t}return e}async applyHooksActions(e,t){const r=a.cloneDeep(e);for(const[e,i]of Object.entries(this.options.hooks[t]))for(const t of i){const i=a.get(r,e);void 0!==i&&"function"==typeof bi[t]&&a.set(r,e,await bi[t](i,{passphrase:this.passphrase,separator:this.options.arraySeparator}))}return r}async validate(e,{mutateOriginal:t=!1}={}){let r=t?e:a.cloneDeep(e);return await this.validator(r)}async mapper(e){let t=a.cloneDeep(e);t=await this.applyHooksActions(t,"beforeMap");const r=h.flatten(t,{safe:!0}),i={_v:this.version+""};for(const[e,t]of Object.entries(r)){const r=this.map[e]||e,s=this.getAttributeDefinition(e);"number"==typeof t&&"string"==typeof s&&s.includes("number")?i[r]=m(t):"string"==typeof t?"[object Object]"===t?i[r]="{}":(t.startsWith("{")||t.startsWith("["),i[r]=t):Array.isArray(t)||"object"==typeof t&&null!==t?i[r]=JSON.stringify(t):i[r]=t}return await this.applyHooksActions(i,"afterMap"),i}async unmapper(e,t){let r=a.cloneDeep(e);delete r._v,r=await this.applyHooksActions(r,"beforeUnmap");const i=t?a.invert(t):this.reversedMap,s={};for(const[e,t]of Object.entries(r)){const r=i&&i[e]?i[e]:e;let n=t;const o=this.getAttributeDefinition(r);if("string"!=typeof o||!o.includes("number")||o.includes("array")||o.includes("decimal")){if("string"==typeof t)if("[object Object]"===t)n={};else if(t.startsWith("{")||t.startsWith("[")){const[e,r,i]=V(()=>JSON.parse(t));e&&(n=i)}}else"string"==typeof n&&""!==n?n=g(n):"number"==typeof n||(n=void 0);if(this.attributes&&"string"==typeof o&&o.includes("array"))if(Array.isArray(n));else if("string"==typeof n&&n.trim().startsWith("[")){const[e,t,r]=V(()=>JSON.parse(n));e&&Array.isArray(r)&&(n=r)}else n=bi.toArray(n,{separator:this.options.arraySeparator});if(this.options.hooks&&this.options.hooks.afterUnmap&&this.options.hooks.afterUnmap[r])for(const e of this.options.hooks.afterUnmap[r])"function"==typeof bi[e]&&(n=await bi[e](n,{passphrase:this.passphrase,separator:this.options.arraySeparator}));s[r]=n}await this.applyHooksActions(s,"afterUnmap");const n=h.unflatten(s);for(const[t,r]of Object.entries(e))t.startsWith("$")&&(n[t]=r);return n}getAttributeDefinition(e){const t=e.split(".");let r=this.attributes;for(const e of t){if(!r)return;r=r[e]}return r}preprocessAttributesForValidation(e){const t={};for(const[r,i]of Object.entries(e))if("object"!=typeof i||null===i||Array.isArray(i))t[r]=i;else{const e=i.$$type&&i.$$type.includes("required"),s=i.$$type&&i.$$type.includes("optional"),n={type:"object",properties:this.preprocessAttributesForValidation(i),strict:!1};e||(s||this.allNestedObjectsOptional)&&(n.optional=!0),t[r]=n}return t}}var vi=wi;const ki=2047;var _i=Object.freeze({__proto__:null,S3_METADATA_LIMIT_BYTES:ki,handleGet:async function({resource:e,metadata:t,body:r}){return{metadata:t,body:r}},handleInsert:async function({resource:e,data:t,mappedData:r,originalData:i}){const s=S(r),n=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}});if(s>n)throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${s} bytes, effective limit: ${n} bytes, absolute limit: 2047 bytes`);return{mappedData:r,body:""}},handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){const n=S(i),o=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}});if(n>o)throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${n} bytes, effective limit: ${o} bytes, absolute limit: 2047 bytes`);return{mappedData:i,body:JSON.stringify(i)}},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){const s=S(i),n=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}});if(s>n)throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${s} bytes, effective limit: ${n} bytes, absolute limit: 2047 bytes`);return{mappedData:i,body:""}}});var Si=Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){if(r&&""!==r.trim())try{const e=JSON.parse(r);return{metadata:{...e,...t},body:r}}catch(e){return{metadata:t,body:r}}return{metadata:t,body:r}},handleInsert:async function({resource:e,data:t,mappedData:r,originalData:i}){const s=S(r);return s>R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}})?(e.emit("exceedsLimit",{operation:"insert",totalSize:s,limit:2047,excess:s-2047,data:i||t}),{mappedData:{_v:r._v},body:JSON.stringify(r)}):{mappedData:r,body:""}},handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){const n=S(i);return n>R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}})&&e.emit("exceedsLimit",{operation:"update",id:t,totalSize:n,limit:2047,excess:n-2047,data:s||r}),{mappedData:i,body:JSON.stringify(r)}},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){const n=S(i);return n>R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t}})&&e.emit("exceedsLimit",{operation:"upsert",id:t,totalSize:n,limit:2047,excess:n-2047,data:s||r}),{mappedData:i,body:JSON.stringify(r)}}});const Oi="$truncated",Ri="true",ji=w(Oi)+w(Ri);async function xi({resource:e,data:t,mappedData:r,originalData:i}){const s=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}}),n=_(r),o=Object.entries(n).sort(([,e],[,t])=>e-t),a={};let c=0,l=!1;r._v&&(a._v=r._v,c+=n._v);for(const[e,t]of o){if("_v"===e)continue;const i=r[e];if(!(c+(t+(l?0:ji))<=s)){const t=s-c-(l?0:ji);if(t>0){const r=Ei(i,t);a[e]=r,l=!0,c+=w(r)}else a[e]="",l=!0;break}a[e]=i,c+=t}let u=S(a)+(l?ji:0);for(;u>s;){const e=Object.keys(a).filter(e=>"_v"!==e&&"$truncated"!==e);if(0===e.length)break;a[e[e.length-1]]="",u=S(a)+ji,l=!0}return l&&(a[Oi]=Ri),{mappedData:a,body:""}}function Ei(e,t){if("string"==typeof e)return Ai(e,t);if("object"==typeof e&&null!==e){return Ai(JSON.stringify(e),t)}return Ai(String(e),t)}function Ai(e,t){const r=new TextEncoder;let i=r.encode(e);if(i.length<=t)return e;let s=e.length;for(;s>0;){const n=e.substring(0,s);if(i=r.encode(n),i.length<=t)return n;s--}return""}var Di=Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){return{metadata:t,body:r}},handleInsert:xi,handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){return xi({resource:e,data:r,mappedData:i,originalData:s})},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){return xi({resource:e,data:r,mappedData:i})}});const Ci="$overflow",Ni="true",$i=w(Ci)+w(Ni);async function Pi({resource:e,data:t,mappedData:r,originalData:i}){const s=R({s3Limit:ki,systemConfig:{version:e.version,timestamps:e.config.timestamps,id:t.id}}),n=_(r),o=Object.entries(n).sort(([,e],[,t])=>e-t),a={},c={};let l=0,u=!1;r._v&&(a._v=r._v,l+=n._v);let h=s;for(const[e,t]of o)"_v"!==e&&(!u&&l+t>s&&(h-=$i,u=!0),!u&&l+t<=h?(a[e]=r[e],l+=t):(c[e]=r[e],u=!0));u&&(a[Ci]=Ni);return{mappedData:a,body:Object.keys(c).length>0?JSON.stringify(c):""}}async function Ii({resource:e,data:t,mappedData:r}){const i={_v:r._v||String(e.version)};i._map=JSON.stringify(e.schema.map);return{mappedData:i,body:JSON.stringify(r)}}const Ti={"user-managed":Si,"enforce-limits":_i,"truncate-data":Di,"body-overflow":Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){let i={};if(r&&""!==r.trim()){const[e,t,s]=V(()=>JSON.parse(r));i=e?s:{}}const s={...i,...t};return delete s.$overflow,{metadata:s,body:r}},handleInsert:Pi,handleUpdate:async function({resource:e,id:t,data:r,mappedData:i,originalData:s}){return Pi({resource:e,data:r,mappedData:i,originalData:s})},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){return Pi({resource:e,data:r,mappedData:i})}}),"body-only":Object.freeze({__proto__:null,handleGet:async function({resource:e,metadata:t,body:r}){let i={};if(r&&""!==r.trim()){const[e,t,s]=V(()=>JSON.parse(r));i=e?s:{}}return{metadata:{...i,...t},body:r}},handleInsert:Ii,handleUpdate:async function({resource:e,id:t,data:r,mappedData:i}){const s={_v:i._v||String(e.version)};return s._map=JSON.stringify(e.schema.map),{mappedData:s,body:JSON.stringify(i)}},handleUpsert:async function({resource:e,id:t,data:r,mappedData:i}){return Ii({resource:e,data:r,mappedData:i})}})};function Mi(e){const t=Ti[e];if(!t)throw new Error(`Unknown behavior: ${e}. Available behaviors: ${Object.keys(Ti).join(", ")}`);return t}const Li=Object.keys(Ti),Ui="user-managed";class Fi extends se{constructor(e={}){super(),this._instanceId=X(7);const t=function(e){const t=[];e.name?"string"!=typeof e.name?t.push("Resource 'name' must be a string"):""===e.name.trim()&&t.push("Resource 'name' cannot be empty"):t.push("Resource 'name' is required");e.client||t.push("S3 'client' is required");e.attributes?"object"!=typeof e.attributes||Array.isArray(e.attributes)?t.push("Resource 'attributes' must be an object"):0===Object.keys(e.attributes).length&&t.push("Resource 'attributes' cannot be empty"):t.push("Resource 'attributes' are required");void 0!==e.version&&"string"!=typeof e.version&&t.push("Resource 'version' must be a string");void 0!==e.behavior&&"string"!=typeof e.behavior&&t.push("Resource 'behavior' must be a string");void 0!==e.passphrase&&"string"!=typeof e.passphrase&&t.push("Resource 'passphrase' must be a string");void 0!==e.parallelism&&("number"==typeof e.parallelism&&Number.isInteger(e.parallelism)?e.parallelism<1&&t.push("Resource 'parallelism' must be greater than 0"):t.push("Resource 'parallelism' must be an integer"));void 0===e.observers||Array.isArray(e.observers)||t.push("Resource 'observers' must be an array");const r=["cache","autoDecrypt","timestamps","paranoid","allNestedObjectsOptional"];for(const i of r)void 0!==e[i]&&"boolean"!=typeof e[i]&&t.push(`Resource '${i}' must be a boolean`);void 0!==e.idGenerator&&("function"!=typeof e.idGenerator&&"number"!=typeof e.idGenerator?t.push("Resource 'idGenerator' must be a function or a number (size)"):"number"==typeof e.idGenerator&&e.idGenerator<=0&&t.push("Resource 'idGenerator' size must be greater than 0"));void 0!==e.idSize&&("number"==typeof e.idSize&&Number.isInteger(e.idSize)?e.idSize<=0&&t.push("Resource 'idSize' must be greater than 0"):t.push("Resource 'idSize' must be an integer"));if(void 0!==e.partitions)if("object"!=typeof e.partitions||Array.isArray(e.partitions))t.push("Resource 'partitions' must be an object");else for(const[r,i]of Object.entries(e.partitions))if("object"!=typeof i||Array.isArray(i))t.push(`Partition '${r}' must be an object`);else if(i.fields)if("object"!=typeof i.fields||Array.isArray(i.fields))t.push(`Partition '${r}.fields' must be an object`);else for(const[e,s]of Object.entries(i.fields))"string"!=typeof s&&t.push(`Partition '${r}.fields.${e}' must be a string`);else t.push(`Partition '${r}' must have a 'fields' property`);if(void 0!==e.hooks)if("object"!=typeof e.hooks||Array.isArray(e.hooks))t.push("Resource 'hooks' must be an object");else{const r=["beforeInsert","afterInsert","beforeUpdate","afterUpdate","beforeDelete","afterDelete"];for(const[i,s]of Object.entries(e.hooks))if(r.includes(i))if(Array.isArray(s))for(let e=0;e<s.length;e++){const t=s[e];if("function"==typeof t);else if("string"==typeof t)continue}else t.push(`Resource 'hooks.${i}' must be an array`);else t.push(`Invalid hook event '${i}'. Valid events: ${r.join(", ")}`)}if(void 0!==e.events)if("object"!=typeof e.events||Array.isArray(e.events))t.push("Resource 'events' must be an object");else for(const[r,i]of Object.entries(e.events))if(Array.isArray(i))for(let e=0;e<i.length;e++){"function"!=typeof i[e]&&t.push(`Resource 'events.${r}[${e}]' must be a function`)}else"function"!=typeof i&&t.push(`Resource 'events.${r}' must be a function or array of functions`);return{isValid:0===t.length,errors:t}}(e);if(!t.isValid){const r=t.errors.map(e=>` • ${e}`).join("\n");throw new z(`Invalid Resource ${e.name||"[unnamed]"} configuration:\n${r}`,{resourceName:e.name,validation:t.errors})}const{name:r,client:i,version:s="1",attributes:n={},behavior:o=Ui,passphrase:a="secret",parallelism:c=10,observers:l=[],cache:u=!1,autoDecrypt:h=!0,timestamps:f=!1,partitions:d={},paranoid:p=!0,allNestedObjectsOptional:m=!0,hooks:g={},idGenerator:y,idSize:b=22,versioningEnabled:w=!1,events:v={}}=e;if(this.name=r,this.client=i,this.version=s,this.behavior=o,this.observers=l,this.parallelism=c,this.passphrase=a??"secret",this.versioningEnabled=w,this.idGenerator=this.configureIdGenerator(y,b),this.idSize="number"==typeof y&&y>0?y:"number"==typeof b&&b>0?b:22,this.idGeneratorType=this.getIdGeneratorType(y,this.idSize),this.config={cache:u,hooks:g,paranoid:p,timestamps:f,partitions:d,autoDecrypt:h,allNestedObjectsOptional:m},this.hooks={beforeInsert:[],afterInsert:[],beforeUpdate:[],afterUpdate:[],beforeDelete:[],afterDelete:[]},this.attributes=n||{},this.map=e.map,this.applyConfiguration({map:this.map}),g)for(const[e,t]of Object.entries(g))if(Array.isArray(t)&&this.hooks[e])for(const r of t)"function"==typeof r&&this.hooks[e].push(r.bind(this));if(v&&Object.keys(v).length>0)for(const[e,t]of Object.entries(v))if(Array.isArray(t))for(const r of t)"function"==typeof r&&this.on(e,r);else"function"==typeof t&&this.on(e,t);this._initMiddleware()}configureIdGenerator(e,r){return"function"==typeof e?()=>String(e()):"number"==typeof e&&e>0?t.customAlphabet(t.urlAlphabet,e):"number"==typeof r&&r>0&&22!==r?t.customAlphabet(t.urlAlphabet,r):X}getIdGeneratorType(e,t){return"function"==typeof e?"custom_function":t}get options(){return{timestamps:this.config.timestamps,partitions:this.config.partitions||{},cache:this.config.cache,autoDecrypt:this.config.autoDecrypt,paranoid:this.config.paranoid,allNestedObjectsOptional:this.config.allNestedObjectsOptional}}export(){const e=this.schema.export();return e.behavior=this.behavior,e.timestamps=this.config.timestamps,e.partitions=this.config.partitions||{},e.paranoid=this.config.paranoid,e.allNestedObjectsOptional=this.config.allNestedObjectsOptional,e.autoDecrypt=this.config.autoDecrypt,e.cache=this.config.cache,e.hooks=this.hooks,e.map=this.map,e}applyConfiguration({map:e}={}){this.config.timestamps&&(this.attributes.createdAt||(this.attributes.createdAt="string|optional"),this.attributes.updatedAt||(this.attributes.updatedAt="string|optional"),this.config.partitions||(this.config.partitions={}),this.config.partitions.byCreatedDate||(this.config.partitions.byCreatedDate={fields:{createdAt:"date|maxlength:10"}}),this.config.partitions.byUpdatedDate||(this.config.partitions.byUpdatedDate={fields:{updatedAt:"date|maxlength:10"}})),this.setupPartitionHooks(),this.versioningEnabled&&(this.config.partitions.byVersion||(this.config.partitions.byVersion={fields:{_v:"string"}})),this.schema=new vi({name:this.name,attributes:this.attributes,passphrase:this.passphrase,version:this.version,options:{autoDecrypt:this.config.autoDecrypt,allNestedObjectsOptional:this.config.allNestedObjectsOptional},map:e||this.map}),this.validatePartitions()}updateAttributes(e){const t=this.attributes;return this.attributes=e,this.applyConfiguration({map:this.schema?.map}),{oldAttributes:t,newAttributes:e}}addHook(e,t){this.hooks[e]&&this.hooks[e].push(t.bind(this))}async executeHooks(e,t){if(!this.hooks[e])return t;let r=t;for(const t of this.hooks[e])r=await t(r);return r}setupPartitionHooks(){if(!this.config.partitions)return;const e=this.config.partitions;0!==Object.keys(e).length&&(this.hooks.afterInsert||(this.hooks.afterInsert=[]),this.hooks.afterInsert.push(async e=>(await this.createPartitionReferences(e),e)),this.hooks.afterDelete||(this.hooks.afterDelete=[]),this.hooks.afterDelete.push(async e=>(await this.deletePartitionReferences(e),e)))}async validate(e){const t={original:a.cloneDeep(e),isValid:!1,errors:[]},r=await this.schema.validate(e,{mutateOriginal:!1});return!0===r?t.isValid=!0:t.errors=r,t.data=e,t}validatePartitions(){if(!this.config.partitions)return;const e=this.config.partitions;if(0===Object.keys(e).length)return;const t=Object.keys(this.attributes||{});for(const[r,i]of Object.entries(e))if(i.fields)for(const e of Object.keys(i.fields))if(!this.fieldExistsInAttributes(e))throw new q(`Partition '${r}' uses field '${e}' which does not exist in resource attributes. Available fields: ${t.join(", ")}.`,{resourceName:this.name,partitionName:r,fieldName:e,availableFields:t,operation:"validatePartitions"})}fieldExistsInAttributes(e){if(e.startsWith("_"))return!0;if(!e.includes("."))return Object.keys(this.attributes||{}).includes(e);const t=e.split(".");let r=this.attributes||{};for(const e of t){if(!r||"object"!=typeof r||!(e in r))return!1;r=r[e]}return!0}applyPartitionRule(e,t){if(null==e)return e;let r=e;if("string"==typeof t&&t.includes("maxlength:")){const e=t.match(/maxlength:(\d+)/);if(e){const t=parseInt(e[1]);"string"==typeof r&&r.length>t&&(r=r.substring(0,t))}}if(t.includes("date"))if(r instanceof Date)r=r.toISOString().split("T")[0];else if("string"==typeof r)if(r.includes("T")&&r.includes("Z"))r=r.split("T")[0];else{const e=new Date(r);isNaN(e.getTime())||(r=e.toISOString().split("T")[0])}return r}getResourceKey(e){return be("resource="+this.name,"data",`id=${e}`)}getPartitionKey({partitionName:e,id:t,data:r}){if(!this.config.partitions||!this.config.partitions[e])throw new q(`Partition '${e}' not found`,{resourceName:this.name,partitionName:e,operation:"getPartitionKey"});const i=this.config.partitions[e],s=[],n=Object.entries(i.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of n){const i=this.getNestedFieldValue(r,e),n=this.applyPartitionRule(i,t);if(null==n)return null;s.push(`${e}=${n}`)}if(0===s.length)return null;const o=t||r?.id;return o?be(`resource=${this.name}`,`partition=${e}`,...s,`id=${o}`):null}getNestedFieldValue(e,t){if(!t.includes("."))return e[t];const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return;i=i[e]}return i}calculateContentLength(e){return e?Buffer.isBuffer(e)?e.length:"string"==typeof e?Buffer.byteLength(e,"utf8"):"object"==typeof e?Buffer.byteLength(JSON.stringify(e),"utf8"):Buffer.byteLength(String(e),"utf8"):0}async insert({id:e,...t}){if(await this.exists(e))throw new Error(`Resource with id '${e}' already exists`);this.getResourceKey(e||"(auto)"),this.options.timestamps&&(t.createdAt=(new Date).toISOString(),t.updatedAt=(new Date).toISOString());const r={id:e,...this.applyDefaults(t)},i=await this.executeHooks("beforeInsert",r),s=Object.keys(i).filter(e=>!(e in r)||i[e]!==r[e]),n={};for(const e of s)n[e]=i[e];const{errors:o,isValid:a,data:c}=await this.validate(i);if(!a){const e=o&&o.length&&o[0].message?o[0].message:"Insert failed";throw new I({bucket:this.client.config.bucket,resourceName:this.name,attributes:i,validation:o,message:e})}const{id:l,...u}=c;Object.assign(u,n);let h=l||e;if(!h&&(h=this.idGenerator(),!h||""===h.trim())){const{idGenerator:e}=await Promise.resolve().then(function(){return re});h=e()}const f=await this.schema.mapper(u);f._v=String(this.version);const d=Mi(this.behavior),{mappedData:p,body:m}=await d.handleInsert({resource:this,data:u,mappedData:f,originalData:r}),g=p,y=this.getResourceKey(h);let b;if(m&&""!==m){const[e,t]=await K(()=>Promise.resolve(JSON.parse(m)));e&&(b="application/json")}if("body-only"===this.behavior&&(!m||""===m))throw new Error(`[Resource.insert] Attempt to save object without body! Data: id=${h}, resource=${this.name}`);const[w,v,k]=await K(()=>this.client.putObject({key:y,body:m,contentType:b,metadata:g}));if(!w){const e=v&&v.message?v.message:"";if(e.includes("metadata headers exceed")||e.includes("Insert failed")){const e=S(g),t=R({s3Limit:2047,systemConfig:{version:this.version,timestamps:this.config.timestamps,id:h}}),r=e-t;throw v.totalSize=e,v.limit=2047,v.effectiveLimit=t,v.excess=r,new z("metadata headers exceed",{resourceName:this.name,operation:"insert",id:h,totalSize:e,effectiveLimit:t,excess:r,suggestion:"Reduce metadata size or number of fields."})}throw v}const _=await this.get(h),O=await this.executeHooks("afterInsert",_);return this.emit("insert",O),O}async get(e){if(a.isObject(e))throw new Error("id cannot be an object");if(a.isEmpty(e))throw new Error("id cannot be empty");const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.getObject(t));if(!r)throw L(i,{bucket:this.client.config.bucket,key:t,resourceName:this.name,operation:"get",id:e});const n=s.Metadata?._v||this.version,o="string"==typeof n&&n.startsWith("v")?n.slice(1):n,c=await this.getSchemaForVersion(o);let l=await c.unmapper(s.Metadata);const u=Mi(this.behavior);let h="";if(s.ContentLength>0){const[e,r,i]=await K(()=>this.client.getObject(t));h=e?await Xr(i.Body):""}const{metadata:f}=await u.handleGet({resource:this,metadata:l,body:h});let d=await this.composeFullObjectFromWrite({id:e,metadata:f,body:h,behavior:this.behavior});d._contentLength=s.ContentLength,d._lastModified=s.LastModified,d._hasContent=s.ContentLength>0,d._mimeType=s.ContentType||null,d._v=o,s.VersionId&&(d._versionId=s.VersionId),s.Expiration&&(d._expiresAt=s.Expiration),d._definitionHash=this.getDefinitionHash(),o!==this.version&&(d=await this.applyVersionMapping(d,o,this.version)),this.emit("get",d);return d}async exists(e){const t=this.getResourceKey(e),[r,i]=await K(()=>this.client.headObject(t));return r}async update(e,t){if(a.isEmpty(e))throw new Error("id cannot be empty");if(!await this.exists(e))throw new Error(`Resource with id '${e}' does not exist`);const r=await this.get(e),i=a.cloneDeep(t);let s=a.cloneDeep(r);for(const[e,t]of Object.entries(i))if(e.includes(".")){let r=s;const i=e.split(".");for(let e=0;e<i.length-1;e++)"object"==typeof r[i[e]]&&null!==r[i[e]]||(r[i[e]]={}),r=r[i[e]];r[i[i.length-1]]=a.cloneDeep(t)}else"object"!=typeof t||null===t||Array.isArray(t)?s[e]=a.cloneDeep(t):s[e]=a.merge({},s[e],t);if(this.config.timestamps){const e=(new Date).toISOString();s.updatedAt=e,s.metadata||(s.metadata={}),s.metadata.updatedAt=e}const n=await this.executeHooks("beforeUpdate",a.cloneDeep(s)),o={...r,...n,id:e},{isValid:c,errors:l,data:u}=await this.validate(a.cloneDeep(o));if(!c)throw new I({bucket:this.client.config.bucket,resourceName:this.name,attributes:n,validation:l,message:"validation: "+(l&&l.length?JSON.stringify(l):"unknown")});await this.schema.mapper(u);const h=Mi(this.behavior),f=await this.schema.mapper({...r,...n});f._v=String(this.version),await h.handleUpdate({resource:this,id:e,data:{...r,...n},mappedData:f,originalData:{...i,id:e}});const{id:d,...p}=u,m={...r,id:e},g={...p,id:e};await this.handlePartitionReferenceUpdates(m,g);const y=await this.schema.mapper(p);y._v=String(this.version);const b=Mi(this.behavior),{mappedData:w,body:v}=await b.handleUpdate({resource:this,id:e,data:p,mappedData:y,originalData:{...i,id:e}}),k=w,_=this.getResourceKey(e);let O,j=v;if(""===v&&"body-overflow"!==this.behavior){const[e,t,r]=await K(()=>this.client.getObject(_));if(e&&r.ContentLength>0){const e=Buffer.from(await r.Body.transformToByteArray()),t=e.toString(),[i,s]=await K(()=>Promise.resolve(JSON.parse(t)));i||(j=e,O=r.ContentType)}}let x=O;if(j&&""!==j&&!x){const[e,t]=await K(()=>Promise.resolve(JSON.parse(j)));e&&(x="application/json")}this.versioningEnabled&&r._v!==this.version&&await this.createHistoricalVersion(e,r);const[E,A]=await K(()=>this.client.putObject({key:_,body:j,contentType:x,metadata:k}));if(!E&&A&&A.message&&A.message.includes("metadata headers exceed")){const t=S(k),r=R({s3Limit:2047,systemConfig:{version:this.version,timestamps:this.config.timestamps,id:e}}),i=t-r;throw A.totalSize=t,A.limit=2047,A.effectiveLimit=r,A.excess=i,this.emit("exceedsLimit",{operation:"update",totalSize:t,limit:2047,effectiveLimit:r,excess:i,data:p}),new z("metadata headers exceed",{resourceName:this.name,operation:"update",id:e,totalSize:t,effectiveLimit:r,excess:i,suggestion:"Reduce metadata size or number of fields."})}if(!E)throw L(A,{bucket:this.client.config.bucket,key:_,resourceName:this.name,operation:"update",id:e});const D=await this.composeFullObjectFromWrite({id:e,metadata:k,body:j,behavior:this.behavior}),C=await this.executeHooks("afterUpdate",D);return this.emit("update",{...D,$before:{...r},$after:{...C}}),C}async delete(e){if(a.isEmpty(e))throw new Error("id cannot be empty");let t,r=null;const[i,s,n]=await K(()=>this.get(e));i?t=n:(t={id:e},r=s),await this.executeHooks("beforeDelete",t);const o=this.getResourceKey(e),[c,l,u]=await K(()=>this.client.deleteObject(o));if(this.emit("delete",{...t,$before:{...t},$after:null}),r)throw L(r,{bucket:this.client.config.bucket,key:o,resourceName:this.name,operation:"delete",id:e});if(!c)throw L(l,{key:o,resourceName:this.name,operation:"delete",id:e});return await this.executeHooks("afterDelete",t),u}async upsert({id:e,...t}){return await this.exists(e)?this.update(e,t):this.insert({id:e,...t})}async count({partition:e=null,partitionValues:t={}}={}){let r;if(e&&Object.keys(t).length>0){const i=this.config.partitions[e];if(!i)throw new q(`Partition '${e}' not found`,{resourceName:this.name,partitionName:e,operation:"count"});const s=[],n=Object.entries(i.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,r]of n){const i=t[e];if(null!=i){const t=this.applyPartitionRule(i,r);s.push(`${e}=${t}`)}}r=s.length>0?`resource=${this.name}/partition=${e}/${s.join("/")}`:`resource=${this.name}/partition=${e}`}else r=`resource=${this.name}/data`;const i=await this.client.count({prefix:r});return this.emit("count",i),i}async insertMany(e){const{results:t}=await i.PromisePool.for(e).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,t),this.observers.map(r=>r.emit("error",this.name,e,t))}).process(async e=>await this.insert(e));return this.emit("insertMany",e.length),t}async deleteMany(e){const t=a.chunk(e.map(e=>this.getResourceKey(e)),1e3);e.map(e=>this.getResourceKey(e));const{results:r}=await i.PromisePool.for(t).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,t),this.observers.map(r=>r.emit("error",this.name,e,t))}).process(async e=>{const t=await this.client.deleteObjects(e);return e.forEach(e=>{const t=e.split("/").find(e=>e.startsWith("id=")),r=t?t.replace("id=",""):null;r&&(this.emit("deleted",r),this.observers.map(e=>e.emit("deleted",this.name,r)))}),t});return this.emit("deleteMany",e.length),r}async deleteAll(){if(!1!==this.config.paranoid)throw new z("deleteAll() is a dangerous operation and requires paranoid: false option.",{resourceName:this.name,operation:"deleteAll",paranoid:this.config.paranoid,suggestion:"Set paranoid: false to allow deleteAll."});const e=`resource=${this.name}/data`,t=await this.client.deleteAll({prefix:e});return this.emit("deleteAll",{version:this.version,prefix:e,deletedCount:t}),{deletedCount:t,version:this.version}}async deleteAllData(){if(!1!==this.config.paranoid)throw new z("deleteAllData() is a dangerous operation and requires paranoid: false option.",{resourceName:this.name,operation:"deleteAllData",paranoid:this.config.paranoid,suggestion:"Set paranoid: false to allow deleteAllData."});const e=`resource=${this.name}`,t=await this.client.deleteAll({prefix:e});return this.emit("deleteAllData",{resource:this.name,prefix:e,deletedCount:t}),{deletedCount:t,resource:this.name}}async listIds({partition:e=null,partitionValues:t={},limit:r,offset:i=0}={}){let s;if(e&&Object.keys(t).length>0){if(!this.config.partitions||!this.config.partitions[e])throw new q(`Partition '${e}' not found`,{resourceName:this.name,partitionName:e,operation:"listIds"});const r=this.config.partitions[e],i=[],n=Object.entries(r.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,r]of n){const s=t[e];if(null!=s){const t=this.applyPartitionRule(s,r);i.push(`${e}=${t}`)}}s=i.length>0?`resource=${this.name}/partition=${e}/${i.join("/")}`:`resource=${this.name}/partition=${e}`}else s=`resource=${this.name}/data`;const n=(await this.client.getKeysPage({prefix:s,offset:i,amount:r||1e3})).map(e=>{const t=e.split("/").find(e=>e.startsWith("id="));return t?t.replace("id=",""):null}).filter(Boolean);return this.emit("listIds",n.length),n}async list({partition:e=null,partitionValues:t={},limit:r,offset:i=0}={}){const[s,n,o]=await K(async()=>e?await this.listPartition({partition:e,partitionValues:t,limit:r,offset:i}):await this.listMain({limit:r,offset:i}));return s?o:this.handleListError(n,{partition:e,partitionValues:t})}async listMain({limit:e,offset:t=0}){const[r,i,s]=await K(()=>this.listIds({limit:e,offset:t}));if(!r)throw i;const n=await this.processListResults(s,"main");return this.emit("list",{count:n.length,errors:0}),n}async listPartition({partition:e,partitionValues:t,limit:r,offset:i=0}){if(!this.config.partitions?.[e])return this.emit("list",{partition:e,partitionValues:t,count:0,errors:0}),[];const s=this.config.partitions[e],n=this.buildPartitionPrefix(e,s,t),[o,a,c]=await K(()=>this.client.getAllKeys({prefix:n}));if(!o)throw a;const l=this.extractIdsFromKeys(c).slice(i),u=r?l.slice(0,r):l,h=await this.processPartitionResults(u,e,s,c);return this.emit("list",{partition:e,partitionValues:t,count:h.length,errors:0}),h}buildPartitionPrefix(e,t,r){const i=[],s=Object.entries(t.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of s){const s=r[e];if(null!=s){const r=this.applyPartitionRule(s,t);i.push(`${e}=${r}`)}}return i.length>0?`resource=${this.name}/partition=${e}/${i.join("/")}`:`resource=${this.name}/partition=${e}`}extractIdsFromKeys(e){return e.map(e=>{const t=e.split("/").find(e=>e.startsWith("id="));return t?t.replace("id=",""):null}).filter(Boolean)}async processListResults(e,t="main"){const{results:r,errors:s}=await i.PromisePool.for(e).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,content),this.observers.map(t=>t.emit("error",this.name,e,content))}).process(async e=>{const[r,i,s]=await K(()=>this.get(e));return r?s:this.handleResourceError(i,e,t)});return this.emit("list",{count:r.length,errors:0}),r}async processPartitionResults(e,t,r,s){const n=Object.entries(r.fields).sort(([e],[t])=>e.localeCompare(t)),{results:o,errors:a}=await i.PromisePool.for(e).withConcurrency(this.parallelism).handleError(async(e,t)=>{this.emit("error",e,content),this.observers.map(t=>t.emit("error",this.name,e,content))}).process(async e=>{const[r,i,o]=await K(async()=>{const r=this.extractPartitionValuesFromKey(e,s,n);return await this.getFromPartition({id:e,partitionName:t,partitionValues:r})});return r?o:this.handleResourceError(i,e,"partition")});return o.filter(e=>null!==e)}extractPartitionValuesFromKey(e,t,r){const i=t.find(t=>t.includes(`id=${e}`));if(!i)throw new q(`Partition key not found for ID ${e}`,{resourceName:this.name,id:e,operation:"extractPartitionValuesFromKey"});const s=i.split("/"),n={};for(const[e]of r){const t=s.find(t=>t.startsWith(`${e}=`));if(t){const r=t.replace(`${e}=`,"");n[e]=r}}return n}handleResourceError(e,t,r){if(e.message.includes("Cipher job failed")||e.message.includes("OperationError"))return{id:t,_decryptionFailed:!0,_error:e.message,..."partition"===r&&{_partition:r}};throw e}handleListError(e,{partition:t,partitionValues:r}){return e.message.includes("Partition '")&&e.message.includes("' not found"),this.emit("list",{partition:t,partitionValues:r,count:0,errors:1}),[]}async getMany(e){const{results:t,errors:r}=await i.PromisePool.for(e).withConcurrency(this.client.parallelism).handleError(async(e,t)=>(this.emit("error",e,content),this.observers.map(t=>t.emit("error",this.name,e,content)),{id:t,_error:e.message,_decryptionFailed:e.message.includes("Cipher job failed")||e.message.includes("OperationError")})).process(async e=>{const[t,r,i]=await K(()=>this.get(e));if(t)return i;if(r.message.includes("Cipher job failed")||r.message.includes("OperationError"))return{id:e,_decryptionFailed:!0,_error:r.message};throw r});return this.emit("getMany",e.length),t}async getAll(){const[e,t,r]=await K(()=>this.listIds());if(!e)throw t;const i=[];for(const e of r){const[t,r,s]=await K(()=>this.get(e));t&&i.push(s)}return i}async page({offset:e=0,size:t=100,partition:r=null,partitionValues:i={},skipCount:s=!1}={}){const[n,o,a]=await K(async()=>{let n=null,o=null;if(!s){const[e,s,a]=await K(()=>this.count({partition:r,partitionValues:i}));e?(n=a,o=Math.ceil(n/t)):(n=null,o=null)}const a=Math.floor(e/t);let c=[];if(t<=0)c=[];else{const[s,n,o]=await K(()=>this.list({partition:r,partitionValues:i,limit:t,offset:e}));c=s?o:[]}const l={items:c,totalItems:n,page:a,pageSize:t,totalPages:o,hasMore:c.length===t&&e+t<(n||1/0),_debug:{requestedSize:t,requestedOffset:e,actualItemsReturned:c.length,skipCount:s,hasTotalItems:null!==n}};return this.emit("page",l),l});return n?a:{items:[],totalItems:null,page:Math.floor(e/t),pageSize:t,totalPages:null,_debug:{requestedSize:t,requestedOffset:e,actualItemsReturned:0,skipCount:s,hasTotalItems:!1,error:o.message}}}readable(){return new Qr({resource:this}).build()}writable(){return new Zr({resource:this}).build()}async setContent({id:e,buffer:t,contentType:r="application/octet-stream"}){const[i,s,n]=await K(()=>this.get(e));if(!i||!n)throw new z(`Resource with id '${e}' not found`,{resourceName:this.name,id:e,operation:"setContent"});const o={...n,_hasContent:!0,_contentLength:t.length,_mimeType:r},a=await this.schema.mapper(o),[c,l]=await K(()=>this.client.putObject({key:this.getResourceKey(e),metadata:a,body:t,contentType:r}));if(!c)throw l;return this.emit("setContent",{id:e,contentType:r,contentLength:t.length}),o}async content(e){const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.getObject(t));if(!r){if("NoSuchKey"===i.name)return{buffer:null,contentType:null};throw i}const n=Buffer.from(await s.Body.transformToByteArray()),o=s.ContentType||null;return this.emit("content",e,n.length,o),{buffer:n,contentType:o}}async hasContent(e){const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.headObject(t));return!!r&&s.ContentLength>0}async deleteContent(e){const t=this.getResourceKey(e),[r,i,s]=await K(()=>this.client.headObject(t));if(!r)throw i;const n=s.Metadata||{},[o,a,c]=await K(()=>this.client.putObject({key:t,body:"",metadata:n}));if(!o)throw a;return this.emit("deleteContent",e),c}getDefinitionHash(){const e={attributes:this.attributes,behavior:this.behavior},t=c(e);return`sha256:${o.createHash("sha256").update(t).digest("hex")}`}extractVersionFromKey(e){const t=e.split("/").find(e=>e.startsWith("v="));return t?t.replace("v=",""):null}async getSchemaForVersion(e){if(e===this.version)return this.schema;const[t,r,i]=await K(()=>Promise.resolve(new vi({name:this.name,attributes:this.attributes,passphrase:this.passphrase,version:e,options:{...this.config,autoDecrypt:!0,autoEncrypt:!0}})));return t?i:this.schema}async createPartitionReferences(e){const t=this.config.partitions;if(t&&0!==Object.keys(t).length)for(const[r,i]of Object.entries(t)){const t=this.getPartitionKey({partitionName:r,id:e.id,data:e});if(t){const e={_v:String(this.version)};await this.client.putObject({key:t,metadata:e,body:"",contentType:void 0})}}}async deletePartitionReferences(e){const t=this.config.partitions;if(!t||0===Object.keys(t).length)return;const r=[];for(const[i,s]of Object.entries(t)){const t=this.getPartitionKey({partitionName:i,id:e.id,data:e});t&&r.push(t)}if(r.length>0){const[e,t]=await K(()=>this.client.deleteObjects(r))}}async query(e={},{limit:t=100,offset:r=0,partition:i=null,partitionValues:s={}}={}){if(0===Object.keys(e).length)return await this.list({partition:i,partitionValues:s,limit:t,offset:r});const n=[];let o=r;const a=Math.min(t,50);for(;n.length<t;){const t=await this.list({partition:i,partitionValues:s,limit:a,offset:o});if(0===t.length)break;const r=t.filter(t=>Object.entries(e).every(([e,r])=>t[e]===r));if(n.push(...r),o+=a,t.length<a)break}return n.slice(0,t)}async handlePartitionReferenceUpdates(e,t){const r=this.config.partitions;if(!r||0===Object.keys(r).length)return;for(const[i,s]of Object.entries(r)){const[r,n]=await K(()=>this.handlePartitionReferenceUpdate(i,s,e,t))}const i=t.id||e.id;for(const[e,s]of Object.entries(r)){const r=`resource=${this.name}/partition=${e}`;let s=[];const[n,o,a]=await K(()=>this.client.getAllKeys({prefix:r}));if(!n)continue;s=a;const c=this.getPartitionKey({partitionName:e,id:i,data:t});for(const e of s)if(e.endsWith(`/id=${i}`)&&e!==c){const[t,r]=await K(()=>this.client.deleteObject(e))}}}async handlePartitionReferenceUpdate(e,t,r,i){const s=i.id||r.id,n=this.getPartitionKey({partitionName:e,id:s,data:r}),o=this.getPartitionKey({partitionName:e,id:s,data:i});if(n!==o){if(n){const[e,t]=await K(async()=>{await this.client.deleteObject(n)})}if(o){const[e,t]=await K(async()=>{const e={_v:String(this.version)};await this.client.putObject({key:o,metadata:e,body:"",contentType:void 0})})}}else if(o){const[e,t]=await K(async()=>{const e={_v:String(this.version)};await this.client.putObject({key:o,metadata:e,body:"",contentType:void 0})})}}async updatePartitionReferences(e){const t=this.config.partitions;if(t&&0!==Object.keys(t).length)for(const[r,i]of Object.entries(t)){if(!i||!i.fields||"object"!=typeof i.fields)continue;const t=this.getPartitionKey({partitionName:r,id:e.id,data:e});if(t){const e={_v:String(this.version)},[r,i]=await K(async()=>{await this.client.putObject({key:t,metadata:e,body:"",contentType:void 0})})}}}async getFromPartition({id:e,partitionName:t,partitionValues:r={}}){if(!this.config.partitions||!this.config.partitions[t])throw new q(`Partition '${t}' not found`,{resourceName:this.name,partitionName:t,operation:"getFromPartition"});const i=this.config.partitions[t],s=[],n=Object.entries(i.fields).sort(([e],[t])=>e.localeCompare(t));for(const[e,t]of n){const i=r[e];if(null!=i){const r=this.applyPartitionRule(i,t);s.push(`${e}=${r}`)}}if(0===s.length)throw new q(`No partition values provided for partition '${t}'`,{resourceName:this.name,partitionName:t,operation:"getFromPartition"});const o=be(`resource=${this.name}`,`partition=${t}`,...s,`id=${e}`),[a,c]=await K(async()=>{await this.client.headObject(o)});if(!a)throw new z(`Resource with id '${e}' not found in partition '${t}'`,{resourceName:this.name,id:e,partitionName:t,operation:"getFromPartition"});const l=await this.get(e);return l._partition=t,l._partitionValues=r,this.emit("getFromPartition",l),l}async createHistoricalVersion(e,t){const r=be(`resource=${this.name}`,"historical",`id=${e}`),i={...t,_v:t._v||this.version,_historicalTimestamp:(new Date).toISOString()},s=await this.schema.mapper(i),n=Mi(this.behavior),{mappedData:o,body:a}=await n.handleInsert({resource:this,data:i,mappedData:s}),c={...o,_v:t._v||this.version,_historicalTimestamp:i._historicalTimestamp};let l;if(a&&""!==a){const[e,t]=await K(()=>Promise.resolve(JSON.parse(a)));e&&(l="application/json")}await this.client.putObject({key:r,metadata:c,body:a,contentType:l})}async applyVersionMapping(e,t,r){if(t===r)return e;return{...e,_v:r,_originalVersion:t,_versionMapped:!0}}async composeFullObjectFromWrite({id:e,metadata:t,body:r,behavior:i}){const s={};t&&"true"===t.$truncated&&(s.$truncated="true"),t&&"true"===t.$overflow&&(s.$overflow="true");let n={};const[o,a,c]=await K(()=>this.schema.unmapper(t));n=o?c:t;const l=e=>{if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))r.startsWith("_")||(t[r]=i);return t},u=e=>{if("object"==typeof e&&null!==e)return e;if("string"==typeof e){if("[object Object]"===e)return{};if(e.startsWith("{")||e.startsWith("[")){const[t,r,i]=V(()=>JSON.parse(e));return t?i:e}return e}return e};if("body-overflow"===i){const i=t&&"true"===t.$overflow;let s={};if(i&&r){const[e,t,i]=await K(()=>Promise.resolve(JSON.parse(r)));if(e){const[e,t,r]=await K(()=>this.schema.unmapper(i));s=e?r:{}}}const o={...n,...s,id:e};Object.keys(o).forEach(e=>{o[e]=u(o[e])});const a=l(o);return i&&(a.$overflow="true"),a}if("body-only"===i){const[i,s,n]=await K(()=>Promise.resolve(r?JSON.parse(r):{}));let o=this.schema.map;if(t&&t._map){const[e,r,i]=await K(()=>Promise.resolve("string"==typeof t._map?JSON.parse(t._map):t._map));o=e?i:this.schema.map}const[a,c,l]=await K(()=>this.schema.unmapper(n,o)),h=a?{...l,id:e}:{id:e};return Object.keys(h).forEach(e=>{h[e]=u(h[e])}),h}if("user-managed"===i&&r&&""!==r.trim()){const[t,i,s]=await K(()=>Promise.resolve(JSON.parse(r)));if(t){const[t,r,i]=await K(()=>this.schema.unmapper(s)),o={...t?i:{},...n,id:e};return Object.keys(o).forEach(e=>{o[e]=u(o[e])}),l(o)}}const h={...n,id:e};Object.keys(h).forEach(e=>{h[e]=u(h[e])});const f=l(h);return s.$truncated&&(f.$truncated=s.$truncated),s.$overflow&&(f.$overflow=s.$overflow),f}emit(e,...t){return super.emit(e,...t)}async replace(e,t){await this.delete(e),await new Promise(e=>setTimeout(e,100));const r=Date.now();for(;Date.now()-r<5e3;){if(!await this.exists(e))break;await new Promise(e=>setTimeout(e,50))}try{return await this.insert({...t,id:e})}catch(r){if(r&&r.message&&r.message.includes("already exists")){return await this.update(e,t)}throw r}}_initMiddleware(){this._middlewares=new Map,this._middlewareMethods=["get","list","listIds","getAll","count","page","insert","update","delete","deleteMany","exists","getMany","content","hasContent","query","getFromPartition","setContent","deleteContent","replace"];for(const e of this._middlewareMethods)this._middlewares.set(e,[]),this[`_original_${e}`]||(this[`_original_${e}`]=this[e].bind(this),this[e]=async(...t)=>{const r={resource:this,args:t,method:e};let i=-1;const s=this._middlewares.get(e),n=async t=>{if(t<=i)throw new Error("next() called multiple times");return i=t,t<s.length?await s[t](r,()=>n(t+1)):await this[`_original_${e}`](...r.args)};return await n(0)})}useMiddleware(e,t){if(this._middlewares||this._initMiddleware(),!this._middlewares.has(e))throw new z(`No such method for middleware: ${e}`,{operation:"useMiddleware",method:e});this._middlewares.get(e).push(t)}applyDefaults(e){const t={...e};for(const[e,r]of Object.entries(this.attributes))if(void 0===t[e]&&"string"==typeof r&&r.includes("default:")){const i=r.match(/default:([^|]+)/);if(i){let s=i[1];r.includes("boolean")?s="true"===s:r.includes("number")&&(s=Number(s)),t[e]=s}}return t}}var Bi=Fi;class zi extends se{constructor(e){super(),this.id=X(7),this.version="1",this.s3dbVersion=(()=>{const[e,t,r]=K(()=>"8.2.0");return e?r:"latest"})(),this.resources={},this.savedMetadata=null,this.options=e,this.verbose=e.verbose||!1,this.parallelism=parseInt(e.parallelism+"")||10,this.plugins=e.plugins||[],this.pluginRegistry={},this.pluginList=e.plugins||[],this.cache=e.cache,this.passphrase=e.passphrase||"secret",this.versioningEnabled=e.versioningEnabled||!1,this.persistHooks=e.persistHooks||!1,this._initHooks();let t=e.connectionString;if(!t&&(e.bucket||e.accessKeyId||e.secretAccessKey)){const{bucket:r,region:i,accessKeyId:s,secretAccessKey:n,endpoint:o,forcePathStyle:a}=e;if(o){const e=new URL(o);s&&(e.username=encodeURIComponent(s)),n&&(e.password=encodeURIComponent(n)),e.pathname=`/${r||"s3db"}`,a&&e.searchParams.set("forcePathStyle","true"),t=e.toString()}else if(s&&n){const e=new URLSearchParams;e.set("region",i||"us-east-1"),a&&e.set("forcePathStyle","true"),t=`s3://${encodeURIComponent(s)}:${encodeURIComponent(n)}@${r||"s3db"}?${e.toString()}`}}this.client=e.client||new di({verbose:this.verbose,parallelism:this.parallelism,connectionString:t}),this.bucket=this.client.bucket,this.keyPrefix=this.client.keyPrefix,this._exitListenerRegistered||(this._exitListenerRegistered=!0,"undefined"!=typeof process&&process.on("exit",async()=>{if(this.isConnected())try{await this.disconnect()}catch(e){}}))}async connect(){await this.startPlugins();let e=null,t=!1,r=[];if(await this.client.exists("s3db.json"))try{const i=await this.client.getObject("s3db.json"),s=await Xr(i?.Body);try{e=JSON.parse(s)}catch(i){r.push("JSON parsing failed - attempting recovery"),t=!0,e=await this._attemptJsonRecovery(s,r),e||(await this._createCorruptedBackup(s),r.push("Created backup of corrupted file - starting with blank metadata"),e=this.blankMetadataStructure())}const n=await this._validateAndHealMetadata(e,r);n!==e&&(e=n,t=!0)}catch(i){r.push(`Critical error reading s3db.json: ${i.message}`),await this._createCorruptedBackup(),e=this.blankMetadataStructure(),t=!0}else e=this.blankMetadataStructure(),await this.uploadMetadataFile();t&&await this._uploadHealedMetadata(e,r),this.savedMetadata=e;const i=this.detectDefinitionChanges(e);for(const[t,r]of Object.entries(e.resources||{})){const e=r.currentVersion||"v0",i=r.versions?.[e];if(i){let s,n;void 0!==i.idGenerator?"custom_function"===i.idGenerator?(s=void 0,n=i.idSize||22):"number"==typeof i.idGenerator&&(s=i.idGenerator,n=i.idSize||i.idGenerator):n=i.idSize||22,this.resources[t]=new Bi({name:t,client:this.client,database:this,version:e,attributes:i.attributes,behavior:i.behavior||"user-managed",parallelism:this.parallelism,passphrase:this.passphrase,observers:[this],cache:this.cache,timestamps:void 0!==i.timestamps&&i.timestamps,partitions:r.partitions||i.partitions||{},paranoid:void 0===i.paranoid||i.paranoid,allNestedObjectsOptional:void 0===i.allNestedObjectsOptional||i.allNestedObjectsOptional,autoDecrypt:void 0===i.autoDecrypt||i.autoDecrypt,hooks:this.persistHooks?this._deserializeHooks(i.hooks||{}):i.hooks||{},versioningEnabled:this.versioningEnabled,map:i.map,idGenerator:s,idSize:n})}}i.length>0&&this.emit("resourceDefinitionsChanged",{changes:i,metadata:this.savedMetadata}),this.emit("connected",new Date)}detectDefinitionChanges(e){const t=[];for(const[r,i]of Object.entries(this.resources)){const s=this.generateDefinitionHash(i.export()),n=e.resources?.[r];if(n){const e=n.currentVersion||"v0",i=n.versions?.[e],o=i?.hash;o!==s&&t.push({type:"changed",resourceName:r,currentHash:s,savedHash:o,fromVersion:e,toVersion:this.getNextVersion(n.versions)})}else t.push({type:"new",resourceName:r,currentHash:s,savedHash:null})}for(const[r,i]of Object.entries(e.resources||{}))if(!this.resources[r]){const e=i.currentVersion||"v0",s=i.versions?.[e];t.push({type:"deleted",resourceName:r,currentHash:null,savedHash:s?.hash,deletedVersion:e})}return t}generateDefinitionHash(e,t=void 0){const r={...e.attributes};e.timestamps&&(delete r.createdAt,delete r.updatedAt);const i={attributes:r,behavior:t||e.behavior||"user-managed",partitions:e.partitions||{}},s=c(i);return`sha256:${o.createHash("sha256").update(s).digest("hex")}`}getNextVersion(e={}){const t=Object.keys(e).filter(e=>e.startsWith("v")).map(e=>parseInt(e.substring(1))).filter(e=>!isNaN(e));return`v${(t.length>0?Math.max(...t):-1)+1}`}_serializeHooks(e){if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))Array.isArray(i)?t[r]=i.map(e=>{if("function"==typeof e)try{return{__s3db_serialized_function:!0,code:e.toString(),name:e.name||"anonymous"}}catch(e){return this.verbose&&console.warn(`Failed to serialize hook for event '${r}':`,e.message),null}return e}):t[r]=i;return t}_deserializeHooks(e){if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))Array.isArray(i)?t[r]=i.map(e=>{if(e&&"object"==typeof e&&e.__s3db_serialized_function){try{const t=new Function("return "+e.code)();if("function"==typeof t)return t}catch(t){this.verbose&&console.warn(`Failed to deserialize hook '${e.name}' for event '${r}':`,t.message)}return null}return e}).filter(e=>null!==e):t[r]=i;return t}async startPlugins(){const e=this;if(!a.isEmpty(this.pluginList)){const t=this.pluginList.map(e=>a.isFunction(e)?new e(this):e),r=t.map(async t=>{t.beforeSetup&&await t.beforeSetup(),await t.setup(e),t.afterSetup&&await t.afterSetup();const r=this._getPluginName(t);this.pluginRegistry[r]=t});await Promise.all(r);const i=t.map(async e=>{e.beforeStart&&await e.beforeStart(),await e.start(),e.afterStart&&await e.afterStart()});await Promise.all(i)}}_getPluginName(e,t=null){return t||e.constructor.name.replace("Plugin","").toLowerCase()}async usePlugin(e,t=null){const r=this._getPluginName(e,t);return this.plugins[r]=e,this.isConnected()&&(await e.setup(this),await e.start()),e}async uploadMetadataFile(){const e={version:this.version,s3dbVersion:this.s3dbVersion,lastUpdated:(new Date).toISOString(),resources:{}};Object.entries(this.resources).forEach(([t,r])=>{const i=r.export(),s=this.generateDefinitionHash(i),n=this.savedMetadata?.resources?.[t],o=n?.currentVersion||"v0",a=n?.versions?.[o];let c,l;a&&a.hash===s?(c=o,l=!1):(c=this.getNextVersion(n?.versions),l=!0),e.resources[t]={currentVersion:c,partitions:r.config.partitions||{},versions:{...n?.versions,[c]:{hash:s,attributes:i.attributes,behavior:i.behavior||"user-managed",timestamps:r.config.timestamps,partitions:r.config.partitions,paranoid:r.config.paranoid,allNestedObjectsOptional:r.config.allNestedObjectsOptional,autoDecrypt:r.config.autoDecrypt,cache:r.config.cache,hooks:this.persistHooks?this._serializeHooks(r.config.hooks):r.config.hooks,idSize:r.idSize,idGenerator:r.idGeneratorType,createdAt:l?(new Date).toISOString():a?.createdAt}}},r.version!==c&&(r.version=c,r.emit("versionUpdated",{oldVersion:o,newVersion:c}))}),await this.client.putObject({key:"s3db.json",body:JSON.stringify(e,null,2),contentType:"application/json"}),this.savedMetadata=e,this.emit("metadataUploaded",e)}blankMetadataStructure(){return{version:"1",s3dbVersion:this.s3dbVersion,lastUpdated:(new Date).toISOString(),resources:{}}}async _attemptJsonRecovery(e,t){if(!e||"string"!=typeof e)return t.push("Content is empty or not a string"),null;const r=[()=>e.replace(/,(\s*[}\]])/g,"$1"),()=>e.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g,'$1"$2":'),()=>{let t=0,r=0,i=!1,s=!1;for(let n=0;n<e.length;n++){const o=e[n];s?s=!1:"\\"!==o?'"'!==o?i||("{"===o?t++:"}"===o?t--:"["===o?r++:"]"===o&&r--):i=!i:s=!0}let n=e;for(;r>0;)n+="]",r--;for(;t>0;)n+="}",t--;return n}];for(const[e,i]of r.entries())try{const r=i(),s=JSON.parse(r);return t.push(`JSON recovery successful using fix #${e+1}`),s}catch(e){}return t.push("All JSON recovery attempts failed"),null}async _validateAndHealMetadata(e,t){if(!e||"object"!=typeof e)return t.push("Metadata is not an object - using blank structure"),this.blankMetadataStructure();let r={...e},i=!1;r.version&&"string"==typeof r.version||(r.version&&"number"==typeof r.version?(r.version=String(r.version),t.push("Converted version from number to string"),i=!0):(r.version="1",t.push("Added missing or invalid version field"),i=!0)),r.s3dbVersion&&"string"==typeof r.s3dbVersion||(r.s3dbVersion&&"string"!=typeof r.s3dbVersion?(r.s3dbVersion=String(r.s3dbVersion),t.push("Converted s3dbVersion to string"),i=!0):(r.s3dbVersion=this.s3dbVersion,t.push("Added missing s3dbVersion field"),i=!0)),r.resources&&"object"==typeof r.resources&&!Array.isArray(r.resources)||(r.resources={},t.push("Fixed invalid resources field"),i=!0),r.lastUpdated||(r.lastUpdated=(new Date).toISOString(),t.push("Added missing lastUpdated field"),i=!0);const s={};for(const[e,n]of Object.entries(r.resources)){const r=this._healResourceStructure(e,n,t);r?(s[e]=r,r!==n&&(i=!0)):(t.push(`Removed invalid resource: ${e}`),i=!0)}return r.resources=s,i?r:e}_healResourceStructure(e,t,r){if(!t||"object"!=typeof t)return r.push(`Resource ${e}: invalid structure`),null;let i={...t},s=!1;i.currentVersion||(i.currentVersion="v0",r.push(`Resource ${e}: added missing currentVersion`),s=!0),i.versions&&"object"==typeof i.versions&&!Array.isArray(i.versions)||(i.versions={},r.push(`Resource ${e}: fixed invalid versions object`),s=!0),i.partitions&&"object"==typeof i.partitions&&!Array.isArray(i.partitions)||(i.partitions={},r.push(`Resource ${e}: fixed invalid partitions object`),s=!0);const n=i.currentVersion;if(!i.versions[n]){const t=Object.keys(i.versions);if(!(t.length>0))return r.push(`Resource ${e}: no valid versions found - removing resource`),null;i.currentVersion=t[0],r.push(`Resource ${e}: changed currentVersion from ${n} to ${i.currentVersion}`),s=!0}const o=i.versions[i.currentVersion];if(!o||"object"!=typeof o)return r.push(`Resource ${e}: invalid version data - removing resource`),null;if(!o.attributes||"object"!=typeof o.attributes)return r.push(`Resource ${e}: missing or invalid attributes - removing resource`),null;if(o.hooks){const t=this._healHooksStructure(o.hooks,e,r);t!==o.hooks&&(i.versions[i.currentVersion].hooks=t,s=!0)}return s?i:t}_healHooksStructure(e,t,r){if(!e||"object"!=typeof e)return r.push(`Resource ${t}: invalid hooks structure - using empty hooks`),{};const i={};let s=!1;for(const[n,o]of Object.entries(e))if(Array.isArray(o)){const e=o.filter(e=>null!=e&&""!==e);i[n]=e,e.length!==o.length&&(r.push(`Resource ${t}: cleaned invalid hooks for event ${n}`),s=!0)}else r.push(`Resource ${t}: hooks for event ${n} is not an array - removing`),s=!0;return s?i:e}async _createCorruptedBackup(e=null){try{const t=`s3db.json.corrupted.${(new Date).toISOString().replace(/[:.]/g,"-")}.backup`;if(!e)try{const t=await this.client.getObject("s3db.json");e=await Xr(t?.Body)}catch(t){e="Unable to read corrupted file content"}await this.client.putObject({key:t,body:e,contentType:"application/json"}),this.verbose&&console.warn(`S3DB: Created backup of corrupted s3db.json as ${t}`)}catch(e){this.verbose&&console.warn(`S3DB: Failed to create backup: ${e.message}`)}}async _uploadHealedMetadata(e,t){try{this.verbose&&t.length>0&&(console.warn("S3DB Self-Healing Operations:"),t.forEach(e=>console.warn(` - ${e}`))),e.lastUpdated=(new Date).toISOString(),await this.client.putObject({key:"s3db.json",body:JSON.stringify(e,null,2),contentType:"application/json"}),this.emit("metadataHealed",{healingLog:t,metadata:e}),this.verbose&&console.warn("S3DB: Successfully uploaded healed metadata")}catch(e){throw this.verbose&&console.error(`S3DB: Failed to upload healed metadata: ${e.message}`),e}}resourceExists(e){return!!this.resources[e]}resourceExistsWithSameHash({name:e,attributes:t,behavior:r="user-managed",partitions:i={},options:s={}}){if(!this.resources[e])return{exists:!1,sameHash:!1,hash:null};const n=this.resources[e],o=this.generateDefinitionHash(n.export()),a=new Bi({name:e,attributes:t,behavior:r,partitions:i,client:this.client,version:n.version,passphrase:this.passphrase,versioningEnabled:this.versioningEnabled,...s}),c=this.generateDefinitionHash(a.export());return{exists:!0,sameHash:o===c,hash:c,existingHash:o}}async createResource({name:e,attributes:t,behavior:r="user-managed",hooks:i,...s}){if(this.resources[e]){const n=this.resources[e];if(Object.assign(n.config,{cache:this.cache,...s}),r&&(n.behavior=r),n.versioningEnabled=this.versioningEnabled,n.updateAttributes(t),i)for(const[e,t]of Object.entries(i))if(Array.isArray(t)&&n.hooks[e])for(const r of t)"function"==typeof r&&n.hooks[e].push(r.bind(n));const o=this.generateDefinitionHash(n.export(),n.behavior),a=this.savedMetadata?.resources?.[e],c=a?.currentVersion||"v0",l=a?.versions?.[c];return l&&l.hash===o||await this.uploadMetadataFile(),this.emit("s3db.resourceUpdated",e),n}const n=this.savedMetadata?.resources?.[e],o=n?.currentVersion||"v0",a=new Bi({name:e,client:this.client,version:void 0!==s.version?s.version:o,attributes:t,behavior:r,parallelism:this.parallelism,passphrase:void 0!==s.passphrase?s.passphrase:this.passphrase,observers:[this],cache:void 0!==s.cache?s.cache:this.cache,timestamps:void 0!==s.timestamps&&s.timestamps,partitions:s.partitions||{},paranoid:void 0===s.paranoid||s.paranoid,allNestedObjectsOptional:void 0===s.allNestedObjectsOptional||s.allNestedObjectsOptional,autoDecrypt:void 0===s.autoDecrypt||s.autoDecrypt,hooks:i||{},versioningEnabled:this.versioningEnabled,map:s.map,idGenerator:s.idGenerator,idSize:s.idSize,events:s.events||{}});return a.database=this,this.resources[e]=a,await this.uploadMetadataFile(),this.emit("s3db.resourceCreated",e),a}resource(e){return this.resources[e]?this.resources[e]:Promise.reject(`resource ${e} does not exist`)}async listResources(){return Object.keys(this.resources).map(e=>({name:e}))}async getResource(e){if(!this.resources[e])throw new D({bucket:this.client.config.bucket,resourceName:e,id:e});return this.resources[e]}get config(){return{version:this.version,s3dbVersion:this.s3dbVersion,bucket:this.bucket,keyPrefix:this.keyPrefix,parallelism:this.parallelism,verbose:this.verbose}}isConnected(){return!!this.savedMetadata}async disconnect(){try{if(this.pluginList&&this.pluginList.length>0){for(const e of this.pluginList)e&&"function"==typeof e.removeAllListeners&&e.removeAllListeners();const e=this.pluginList.map(async e=>{try{e&&"function"==typeof e.stop&&await e.stop()}catch(e){}});await Promise.all(e)}if(this.resources&&Object.keys(this.resources).length>0){for(const[e,t]of Object.entries(this.resources))try{t&&"function"==typeof t.removeAllListeners&&t.removeAllListeners(),t._pluginWrappers&&t._pluginWrappers.clear(),t._pluginMiddlewares&&(t._pluginMiddlewares={}),t.observers&&Array.isArray(t.observers)&&(t.observers=[])}catch(e){}Object.keys(this.resources).forEach(e=>delete this.resources[e])}this.client&&"function"==typeof this.client.removeAllListeners&&this.client.removeAllListeners(),this.removeAllListeners(),this.savedMetadata=null,this.plugins={},this.pluginList=[],this.emit("disconnected",new Date)}catch(e){}}_initHooks(){this._hooks=new Map,this._hookEvents=["beforeConnect","afterConnect","beforeCreateResource","afterCreateResource","beforeUploadMetadata","afterUploadMetadata","beforeDisconnect","afterDisconnect","resourceCreated","resourceUpdated"];for(const e of this._hookEvents)this._hooks.set(e,[]);this._wrapHookableMethods()}_wrapHookableMethods(){this._hooksInstalled||(this._originalConnect=this.connect.bind(this),this._originalCreateResource=this.createResource.bind(this),this._originalUploadMetadataFile=this.uploadMetadataFile.bind(this),this._originalDisconnect=this.disconnect.bind(this),this.connect=async(...e)=>{await this._executeHooks("beforeConnect",{args:e});const t=await this._originalConnect(...e);return await this._executeHooks("afterConnect",{result:t,args:e}),t},this.createResource=async e=>{await this._executeHooks("beforeCreateResource",{config:e});const t=await this._originalCreateResource(e);return await this._executeHooks("afterCreateResource",{resource:t,config:e}),t},this.uploadMetadataFile=async(...e)=>{await this._executeHooks("beforeUploadMetadata",{args:e});const t=await this._originalUploadMetadataFile(...e);return await this._executeHooks("afterUploadMetadata",{result:t,args:e}),t},this.disconnect=async(...e)=>{await this._executeHooks("beforeDisconnect",{args:e});const t=await this._originalDisconnect(...e);return await this._executeHooks("afterDisconnect",{result:t,args:e}),t},this._hooksInstalled=!0)}addHook(e,t){if(this._hooks||this._initHooks(),!this._hooks.has(e))throw new Error(`Unknown hook event: ${e}. Available events: ${this._hookEvents.join(", ")}`);if("function"!=typeof t)throw new Error("Hook function must be a function");this._hooks.get(e).push(t)}async _executeHooks(e,t={}){if(!this._hooks||!this._hooks.has(e))return;const r=this._hooks.get(e);for(const i of r)try{await i({database:this,...t})}catch(r){this.emit("hookError",{event:e,error:r,context:t})}}removeHook(e,t){if(!this._hooks||!this._hooks.has(e))return;const r=this._hooks.get(e),i=r.indexOf(t);i>-1&&r.splice(i,1)}getHooks(e){return this._hooks&&this._hooks.has(e)?[...this._hooks.get(e)]:[]}clearHooks(e){this._hooks&&this._hooks.has(e)&&(this._hooks.get(e).length=0)}}class qi extends zi{}function Hi(e){return"string"==typeof e?e.trim().toLowerCase():e}var Vi=class extends oi{constructor(e={},t=[],r=null){super(e),this.instanceId=Math.random().toString(36).slice(2,10),this.client=r,this.connectionString=e.connectionString;let i=t;if(t)if(Array.isArray(t)){i={};for(const e of t)"string"==typeof e&&(i[Hi(e)]=e)}else"string"==typeof t&&(i[Hi(t)]=t);else i={};this.resourcesMap=this._normalizeResources(i)}_normalizeResources(e){if(!e)return{};if(Array.isArray(e)){const t={};for(const r of e)"string"==typeof r?t[Hi(r)]=r:"object"==typeof r&&r.resource&&(t[Hi(r.resource)]=r);return t}if("object"==typeof e){const t={};for(const[r,i]of Object.entries(e)){const e=Hi(r);"string"==typeof i?t[e]=i:Array.isArray(i)?t[e]=i.map(e=>("string"==typeof e||"object"==typeof e&&e.resource,e)):("function"==typeof i||"object"==typeof i&&i.resource)&&(t[e]=i)}return t}return"function"==typeof e?e:{}}validateConfig(){const e=[];return this.client||this.connectionString||e.push("You must provide a client or a connectionString"),(!this.resourcesMap||"object"==typeof this.resourcesMap&&0===Object.keys(this.resourcesMap).length)&&e.push("You must provide a resources map or array"),{isValid:0===e.length,errors:e}}async initialize(e){await super.initialize(e);const[t,r]=await K(async()=>{if(this.client)this.targetDatabase=this.client;else{if(!this.connectionString)throw new Error("S3dbReplicator: No client or connectionString provided");{const e={connectionString:this.connectionString,region:this.region,keyPrefix:this.keyPrefix,verbose:this.config.verbose||!1};this.targetDatabase=new qi(e),await this.targetDatabase.connect()}}this.emit("connected",{replicator:this.name,target:this.connectionString||"client-provided"})});if(!t)throw this.config.verbose&&console.warn(`[S3dbReplicator] Initialization failed: ${r.message}`),r}async replicate(e,t,r,i,s){let n,o,a,c;"object"==typeof e&&e.resource?(n=e.resource,o=e.operation,a=e.data,c=e.id):(n=e,o=t,a=r,c=i);const l=Hi(n),u=this.resourcesMap[l];if(!u)throw new Error(`[S3dbReplicator] Resource not configured: ${n}`);if(Array.isArray(u)){const e=[];for(const t of u){const[r,i,s]=await K(async()=>await this._replicateToSingleDestination(t,l,o,a,c));if(!r)throw this.config&&this.config.verbose&&console.warn(`[S3dbReplicator] Failed to replicate to destination ${JSON.stringify(t)}: ${i.message}`),i;e.push(s)}return e}{const[e,t,r]=await K(async()=>await this._replicateToSingleDestination(u,l,o,a,c));if(!e)throw this.config&&this.config.verbose&&console.warn(`[S3dbReplicator] Failed to replicate to destination ${JSON.stringify(u)}: ${t.message}`),t;return r}}async _replicateToSingleDestination(e,t,r,i,s){let n;if(n="string"==typeof e?e:"object"==typeof e&&e.resource?e.resource:t,"object"==typeof e&&e.actions&&Array.isArray(e.actions)&&!e.actions.includes(r))return{skipped:!0,reason:"action_not_supported",action:r,destination:n};const o=this._getDestResourceObj(n);let a,c;if("object"==typeof e&&e.transform&&"function"==typeof e.transform?(a=e.transform(i),a&&i&&i.id&&!a.id&&(a.id=i.id)):"object"==typeof e&&e.transformer&&"function"==typeof e.transformer?(a=e.transformer(i),a&&i&&i.id&&!a.id&&(a.id=i.id)):a=i,!a&&i&&(a=i),"insert"===r)c=await o.insert(a);else if("update"===r)c=await o.update(s,a);else{if("delete"!==r)throw new Error(`Invalid operation: ${r}. Supported operations are: insert, update, delete`);c=await o.delete(s)}return c}_applyTransformer(e,t){let r=this._cleanInternalFields(t);const i=Hi(e),s=this.resourcesMap[i];let n;if(!s)return r;if(Array.isArray(s)){for(const e of s){if("object"==typeof e&&e.transform&&"function"==typeof e.transform){n=e.transform(r);break}if("object"==typeof e&&e.transformer&&"function"==typeof e.transformer){n=e.transformer(r);break}}n||(n=r)}else"object"==typeof s?"function"==typeof s.transform?n=s.transform(r):"function"==typeof s.transformer&&(n=s.transformer(r)):n="function"==typeof s?s(r):r;return n&&r&&r.id&&!n.id&&(n.id=r.id),!n&&r&&(n=r),n}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}_resolveDestResource(e,t){const r=Hi(e),i=this.resourcesMap[r];if(!i)return e;if(Array.isArray(i)){for(const e of i){if("string"==typeof e)return e;if("object"==typeof e&&e.resource)return e.resource}return e}return"string"==typeof i?i:"function"==typeof i?e:"object"==typeof i&&i.resource?i.resource:e}_getDestResourceObj(e){const t=Object.keys(this.client.resources||{}),r=Hi(e),i=t.find(e=>Hi(e)===r);if(!i)throw new Error(`[S3dbReplicator] Destination resource not found: ${e}. Available: ${t.join(", ")}`);return this.client.resources[i]}async replicateBatch(e,t){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};const r=[],i=[];for(const s of t){const[t,n,o]=await K(()=>this.replicate({resource:e,operation:s.operation,id:s.id,data:s.data,beforeData:s.beforeData}));t?r.push(o):(this.config.verbose&&console.warn(`[S3dbReplicator] Batch replication failed for record ${s.id}: ${n.message}`),i.push({id:s.id,error:n.message}))}return i.length>0&&console.warn(`[S3dbReplicator] Batch replication completed with ${i.length} error(s) for ${e}:`,i),this.emit("batch_replicated",{replicator:this.name,resourceName:e,total:t.length,successful:r.length,errors:i.length}),{success:0===i.length,results:r,errors:i,total:t.length}}async testConnection(){const[e,t]=await K(async()=>{if(!this.targetDatabase)throw new Error("No target database configured");return"function"==typeof this.targetDatabase.connect&&await this.targetDatabase.connect(),!0});return!!e||(this.config.verbose&&console.warn(`[S3dbReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}async getStatus(){return{...await super.getStatus(),connected:!!this.targetDatabase,targetDatabase:this.connectionString||"client-provided",resources:Object.keys(this.resourcesMap||{}),totalreplicators:this.listenerCount("replicated"),totalErrors:this.listenerCount("replicator_error")}}async cleanup(){this.targetDatabase&&this.targetDatabase.removeAllListeners(),await super.cleanup()}shouldReplicateResource(e,t){const r=Hi(e),i=this.resourcesMap[r];if(!i)return!1;if(!t)return!0;if(Array.isArray(i)){for(const e of i)if("object"==typeof e&&e.resource){if(!e.actions||!Array.isArray(e.actions))return!0;if(e.actions.includes(t))return!0}else if("string"==typeof e)return!0;return!1}return"object"==typeof i&&i.resource?!i.actions||!Array.isArray(i.actions)||i.actions.includes(t):"string"==typeof i||"function"==typeof i}};var Ki=class extends oi{constructor(e={},t=[],r=null){if(super(e),this.client=r,this.queueUrl=e.queueUrl,this.queues=e.queues||{},this.defaultQueue=e.defaultQueue||e.defaultQueueUrl||e.queueUrlDefault,this.region=e.region||"us-east-1",this.sqsClient=r||null,this.messageGroupId=e.messageGroupId,this.deduplicationId=e.deduplicationId,Array.isArray(t)){this.resources={};for(const e of t)"string"==typeof e?this.resources[e]=!0:"object"==typeof e&&e.name&&(this.resources[e.name]=e)}else if("object"==typeof t){this.resources=t;for(const[e,r]of Object.entries(t))r&&r.queueUrl&&(this.queues[e]=r.queueUrl)}else this.resources={}}validateConfig(){const e=[];return this.queueUrl||0!==Object.keys(this.queues).length||this.defaultQueue||this.resourceQueueMap||e.push("Either queueUrl, queues object, defaultQueue, or resourceQueueMap must be provided"),{isValid:0===e.length,errors:e}}getQueueUrlsForResource(e){if(this.resourceQueueMap&&this.resourceQueueMap[e])return this.resourceQueueMap[e];if(this.queues[e])return[this.queues[e]];if(this.queueUrl)return[this.queueUrl];if(this.defaultQueue)return[this.defaultQueue];throw new Error(`No queue URL found for resource '${e}'`)}_applyTransformer(e,t){let r=this._cleanInternalFields(t);const i=this.resources[e];let s=r;return i?("function"==typeof i.transform?s=i.transform(r):"function"==typeof i.transformer&&(s=i.transformer(r)),s||r):r}_cleanInternalFields(e){if(!e||"object"!=typeof e)return e;const t={...e};return Object.keys(t).forEach(e=>{(e.startsWith("$")||e.startsWith("_"))&&delete t[e]}),t}createMessage(e,t,r,i,s=null){const n={resource:e,action:t,timestamp:(new Date).toISOString(),source:"s3db-replicator"};switch(t){case"insert":case"delete":default:return{...n,data:r};case"update":return{...n,before:s,data:r}}}async initialize(e,t){if(await super.initialize(e),!this.sqsClient){const[e,r,i]=await K(()=>import("@aws-sdk/client-sqs"));if(!e)throw this.config.verbose&&console.warn(`[SqsReplicator] Failed to import SQS SDK: ${r.message}`),this.emit("initialization_error",{replicator:this.name,error:r.message}),r;const{SQSClient:s}=i;this.sqsClient=t||new s({region:this.region,credentials:this.config.credentials}),this.emit("initialized",{replicator:this.name,queueUrl:this.queueUrl,queues:this.queues,defaultQueue:this.defaultQueue})}}async replicate(e,t,r,i,s=null){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};const[n,o,a]=await K(async()=>{const{SendMessageCommand:n}=await import("@aws-sdk/client-sqs"),o=this.getQueueUrlsForResource(e),a=this._applyTransformer(e,r),c=this.createMessage(e,t,a,i,s),l=[];for(const r of o){const s=new n({QueueUrl:r,MessageBody:JSON.stringify(c),MessageGroupId:this.messageGroupId,MessageDeduplicationId:this.deduplicationId?`${e}:${t}:${i}`:void 0}),o=await this.sqsClient.send(s);l.push({queueUrl:r,messageId:o.MessageId}),this.emit("replicated",{replicator:this.name,resource:e,operation:t,id:i,queueUrl:r,messageId:o.MessageId,success:!0})}return{success:!0,results:l}});return n?a:(this.config.verbose&&console.warn(`[SqsReplicator] Replication failed for ${e}: ${o.message}`),this.emit("replicator_error",{replicator:this.name,resource:e,operation:t,id:i,error:o.message}),{success:!1,error:o.message})}async replicateBatch(e,t){if(!this.enabled||!this.shouldReplicateResource(e))return{skipped:!0,reason:"resource_not_included"};const[r,i,s]=await K(async()=>{const{SendMessageBatchCommand:r}=await import("@aws-sdk/client-sqs"),i=this.getQueueUrlsForResource(e),s=[];for(let e=0;e<t.length;e+=10)s.push(t.slice(e,e+10));const n=[],o=[];for(const t of s){const[s,a]=await K(async()=>{const s=t.map((t,r)=>({Id:`${t.id}-${r}`,MessageBody:JSON.stringify(this.createMessage(e,t.operation,t.data,t.id,t.beforeData)),MessageGroupId:this.messageGroupId,MessageDeduplicationId:this.deduplicationId?`${e}:${t.operation}:${t.id}`:void 0})),o=new r({QueueUrl:i[0],Entries:s}),a=await this.sqsClient.send(o);n.push(a)});if(!s&&(o.push({batch:t.length,error:a.message}),a.message&&(a.message.includes("Batch error")||a.message.includes("Connection")||a.message.includes("Network"))))throw a}return o.length>0&&console.warn(`[SqsReplicator] Batch replication completed with ${o.length} error(s) for ${e}:`,o),this.emit("batch_replicated",{replicator:this.name,resource:e,queueUrl:i[0],total:t.length,successful:n.length,errors:o.length}),{success:0===o.length,results:n,errors:o,total:t.length,queueUrl:i[0]}});if(r)return s;const n=i?.message||i||"Unknown error";return this.config.verbose&&console.warn(`[SqsReplicator] Batch replication failed for ${e}: ${n}`),this.emit("batch_replicator_error",{replicator:this.name,resource:e,error:n}),{success:!1,error:n}}async testConnection(){const[e,t]=await K(async()=>{this.sqsClient||await this.initialize(this.database);const{GetQueueAttributesCommand:e}=await import("@aws-sdk/client-sqs"),t=new e({QueueUrl:this.queueUrl,AttributeNames:["QueueArn"]});return await this.sqsClient.send(t),!0});return!!e||(this.config.verbose&&console.warn(`[SqsReplicator] Connection test failed: ${t.message}`),this.emit("connection_error",{replicator:this.name,error:t.message}),!1)}async getStatus(){return{...await super.getStatus(),connected:!!this.sqsClient,queueUrl:this.queueUrl,region:this.region,resources:Object.keys(this.resources||{}),totalreplicators:this.listenerCount("replicated"),totalErrors:this.listenerCount("replicator_error")}}async cleanup(){this.sqsClient&&this.sqsClient.destroy(),await super.cleanup()}shouldReplicateResource(e){return this.resourceQueueMap&&Object.keys(this.resourceQueueMap).includes(e)||this.queues&&Object.keys(this.queues).includes(e)||!(!this.defaultQueue&&!this.queueUrl)||this.resources&&Object.keys(this.resources).includes(e)||!1}};const Wi={s3db:Vi,sqs:Ki,bigquery:ai,postgres:ci};function Ji(e){return"string"==typeof e?e.trim().toLowerCase():e}return e.AVAILABLE_BEHAVIORS=Li,e.AuditPlugin=class extends ue{constructor(e={}){super(e),this.auditResource=null,this.config={includeData:!1!==e.includeData,includePartitions:!1!==e.includePartitions,maxDataSize:e.maxDataSize||1e4,...e}}async onSetup(){const[e,t,r]=await K(()=>this.database.createResource({name:"audits",attributes:{id:"string|required",resourceName:"string|required",operation:"string|required",recordId:"string|required",userId:"string|optional",timestamp:"string|required",oldData:"string|optional",newData:"string|optional",partition:"string|optional",partitionValues:"string|optional",metadata:"string|optional"},behavior:"body-overflow"}));if(this.auditResource=e?r:this.database.resources.audits||null,e||this.auditResource){this.database.addHook("afterCreateResource",e=>{"audits"!==e.resource.name&&this.setupResourceAuditing(e.resource)});for(const e of Object.values(this.database.resources))"audits"!==e.name&&this.setupResourceAuditing(e)}}async onStart(){}async onStop(){}setupResourceAuditing(e){e.on("insert",async t=>{const r=this.config.includePartitions?this.getPartitionValues(t,e):null;await this.logAudit({resourceName:e.name,operation:"insert",recordId:t.id||"auto-generated",oldData:null,newData:this.config.includeData?JSON.stringify(this.truncateData(t)):null,partition:r?this.getPrimaryPartition(r):null,partitionValues:r?JSON.stringify(r):null})}),e.on("update",async t=>{let r=t.$before;if(this.config.includeData&&!r){const[i,s,n]=await K(()=>e.get(t.id));i&&(r=n)}const i=this.config.includePartitions?this.getPartitionValues(t,e):null;await this.logAudit({resourceName:e.name,operation:"update",recordId:t.id,oldData:r&&this.config.includeData?JSON.stringify(this.truncateData(r)):null,newData:this.config.includeData?JSON.stringify(this.truncateData(t)):null,partition:i?this.getPrimaryPartition(i):null,partitionValues:i?JSON.stringify(i):null})}),e.on("delete",async t=>{let r=t;if(this.config.includeData&&!r){const[i,s,n]=await K(()=>e.get(t.id));i&&(r=n)}const i=r&&this.config.includePartitions?this.getPartitionValues(r,e):null;await this.logAudit({resourceName:e.name,operation:"delete",recordId:t.id,oldData:r&&this.config.includeData?JSON.stringify(this.truncateData(r)):null,newData:null,partition:i?this.getPrimaryPartition(i):null,partitionValues:i?JSON.stringify(i):null})});const t=e.deleteMany.bind(e),r=this;e.deleteMany=async function(i){const s=[];for(const t of i){const[r,i,n]=await K(()=>e.get(t));r?s.push(n):s.push({id:t})}const n=await t(i);for(const t of s){const i=t&&r.config.includePartitions?r.getPartitionValues(t,e):null;await r.logAudit({resourceName:e.name,operation:"deleteMany",recordId:t.id,oldData:t&&r.config.includeData?JSON.stringify(r.truncateData(t)):null,newData:null,partition:i?r.getPrimaryPartition(i):null,partitionValues:i?JSON.stringify(i):null})}return n},e._originalDeleteMany=t}installEventListenersForResource(e){return this.setupResourceAuditing(e)}async logAudit(e){if(!this.auditResource)return;const t={id:`audit-${Date.now()}-${Math.random().toString(36).substring(2,11)}`,userId:this.getCurrentUserId?.()||"system",timestamp:(new Date).toISOString(),metadata:JSON.stringify({source:"audit-plugin",version:"2.0"}),resourceName:e.resourceName,operation:e.operation,recordId:e.recordId};null!==e.oldData&&(t.oldData=e.oldData),null!==e.newData&&(t.newData=e.newData),null!==e.partition&&(t.partition=e.partition),null!==e.partitionValues&&(t.partitionValues=e.partitionValues);try{await this.auditResource.insert(t)}catch(e){console.warn("Audit logging failed:",e.message)}}getPartitionValues(e,t){if(!this.config.includePartitions)return null;const r=t.config?.partitions||t.partitions;if(!r)return null;const i={};for(const[t,s]of Object.entries(r)){const r={};for(const t of Object.keys(s.fields))r[t]=this.getNestedFieldValue(e,t);Object.values(r).some(e=>null!=e)&&(i[t]=r)}return Object.keys(i).length>0?i:null}getNestedFieldValue(e,t){const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return;i=i[e]}return i}getPrimaryPartition(e){if(!e)return null;const t=Object.keys(e);return t.length>0?t[0]:null}truncateData(e){if(!this.config.includeData)return null;const t=JSON.stringify(e);return t.length<=this.config.maxDataSize?e:{...e,_truncated:!0,_originalSize:t.length,_truncatedAt:(new Date).toISOString()}}async getAuditLogs(e={}){if(!this.auditResource)return[];const{resourceName:t,operation:r,recordId:i,partition:s,startDate:n,endDate:o,limit:a=100,offset:c=0}=e;let l=[];if(t||r||i||s||n||o){const e=Math.min(1e4,Math.max(1e3,20*(a+c)));return l=await this.auditResource.list({limit:e})||[],t&&(l=l.filter(e=>e.resourceName===t)),r&&(l=l.filter(e=>e.operation===r)),i&&(l=l.filter(e=>e.recordId===i)),s&&(l=l.filter(e=>e.partition===s)),(n||o)&&(l=l.filter(e=>{const t=new Date(e.timestamp);return!(n&&t<new Date(n))&&!(o&&t>new Date(o))})),l.slice(c,c+a)}return(await this.auditResource.page({size:a,offset:c})).items||[]}async getRecordHistory(e,t){return await this.getAuditLogs({resourceName:e,recordId:t})}async getPartitionHistory(e,t,r){return await this.getAuditLogs({resourceName:e,partition:t,partitionValues:JSON.stringify(r)})}async getAuditStats(e={}){const t=await this.getAuditLogs(e),r={total:t.length,byOperation:{},byResource:{},byPartition:{},byUser:{},timeline:{}};for(const e of t){r.byOperation[e.operation]=(r.byOperation[e.operation]||0)+1,r.byResource[e.resourceName]=(r.byResource[e.resourceName]||0)+1,e.partition&&(r.byPartition[e.partition]=(r.byPartition[e.partition]||0)+1),r.byUser[e.userId]=(r.byUser[e.userId]||0)+1;const t=e.timestamp.split("T")[0];r.timeline[t]=(r.timeline[t]||0)+1}return r}},e.AuthenticationError=class extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}},e.BaseError=j,e.CachePlugin=class extends ue{constructor(e={}){super(e),this.driverName=e.driver||"s3",this.ttl=e.ttl,this.maxSize=e.maxSize,this.config=e.config||{},this.includePartitions=!1!==e.includePartitions,this.partitionStrategy=e.partitionStrategy||"hierarchical",this.partitionAware=!1!==e.partitionAware,this.trackUsage=!1!==e.trackUsage,this.preloadRelated=!1!==e.preloadRelated,this.legacyConfig={memoryOptions:e.memoryOptions,filesystemOptions:e.filesystemOptions,s3Options:e.s3Options,driver:e.driver}}async setup(e){await super.setup(e)}async onSetup(){if(this.driverName&&"object"==typeof this.driverName)this.driver=this.driverName;else if("memory"===this.driverName){const e={...this.legacyConfig.memoryOptions,...this.config};void 0!==this.ttl&&(e.ttl=this.ttl),void 0!==this.maxSize&&(e.maxSize=this.maxSize),this.driver=new ti(e)}else if("filesystem"===this.driverName){const e={...this.legacyConfig.filesystemOptions,...this.config};void 0!==this.ttl&&(e.ttl=this.ttl),void 0!==this.maxSize&&(e.maxSize=this.maxSize),this.partitionAware?this.driver=new si({partitionStrategy:this.partitionStrategy,trackUsage:this.trackUsage,preloadRelated:this.preloadRelated,...e}):this.driver=new ii(e)}else{const e={client:this.database.client,...this.legacyConfig.s3Options,...this.config};void 0!==this.ttl&&(e.ttl=this.ttl),void 0!==this.maxSize&&(e.maxSize=this.maxSize),this.driver=new ei(e)}this.installDatabaseHooks(),this.installResourceHooks()}installDatabaseHooks(){this.database.addHook("afterCreateResource",async({resource:e})=>{this.installResourceHooksForResource(e)})}async onStart(){}async onStop(){}installResourceHooks(){for(const e of Object.values(this.database.resources))this.installResourceHooksForResource(e)}installResourceHooksForResource(e){if(!this.driver)return;Object.defineProperty(e,"cache",{value:this.driver,writable:!0,configurable:!0,enumerable:!1}),e.cacheKeyFor=async(t={})=>{const{action:r,params:i={},partition:s,partitionValues:n}=t;return this.generateCacheKey(e,r,i,s,n)},this.driver instanceof si&&(e.clearPartitionCache=async(t,r={})=>await this.driver.clearPartition(e.name,t,r),e.getPartitionCacheStats=async(t=null)=>await this.driver.getPartitionStats(e.name,t),e.getCacheRecommendations=async()=>await this.driver.getCacheRecommendations(e.name),e.warmPartitionCache=async(t=[],r={})=>await this.driver.warmPartitionCache(e.name,{partitions:t,...r}));const t=["count","listIds","getMany","getAll","page","list","get","exists","content","hasContent","query","getFromPartition"];for(const r of t)e.useMiddleware(r,async(t,i)=>{let s;if("getMany"===r)s=await e.cacheKeyFor({action:r,params:{ids:t.args[0]}});else if("page"===r){const{offset:i,size:n,partition:o,partitionValues:a}=t.args[0]||{};s=await e.cacheKeyFor({action:r,params:{offset:i,size:n},partition:o,partitionValues:a})}else if("list"===r||"listIds"===r||"count"===r){const{partition:i,partitionValues:n}=t.args[0]||{};s=await e.cacheKeyFor({action:r,partition:i,partitionValues:n})}else if("query"===r){const i=t.args[0]||{},n=t.args[1]||{};s=await e.cacheKeyFor({action:r,params:{filter:i,options:{limit:n.limit,offset:n.offset}},partition:n.partition,partitionValues:n.partitionValues})}else if("getFromPartition"===r){const{id:i,partitionName:n,partitionValues:o}=t.args[0]||{};s=await e.cacheKeyFor({action:r,params:{id:i,partitionName:n},partition:n,partitionValues:o})}else"getAll"===r?s=await e.cacheKeyFor({action:r}):["get","exists","content","hasContent"].includes(r)&&(s=await e.cacheKeyFor({action:r,params:{id:t.args[0]}}));if(this.driver instanceof si){let n,o;if("list"===r||"listIds"===r||"count"===r||"page"===r){const e=t.args[0]||{};n=e.partition,o=e.partitionValues}else if("query"===r){const e=t.args[1]||{};n=e.partition,o=e.partitionValues}else if("getFromPartition"===r){const{partitionName:e,partitionValues:r}=t.args[0]||{};n=e,o=r}const[a,c,l]=await K(()=>e.cache._get(s,{resource:e.name,action:r,partition:n,partitionValues:o}));if(a&&null!=l)return l;if(!a&&"NoSuchKey"!==c.name)throw c;const u=await i();return await e.cache._set(s,u,{resource:e.name,action:r,partition:n,partitionValues:o}),u}{const[t,r,n]=await K(()=>e.cache.get(s));if(t&&null!=n)return n;if(!t&&"NoSuchKey"!==r.name)throw r;const o=await i();return await e.cache.set(s,o),o}});const r=["insert","update","delete","deleteMany","setContent","deleteContent","replace"];for(const t of r)e.useMiddleware(t,async(r,i)=>{const s=await i();if("insert"===t)await this.clearCacheForResource(e,r.args[0]);else if("update"===t)await this.clearCacheForResource(e,{id:r.args[0],...r.args[1]});else if("delete"===t){let t={id:r.args[0]};if("function"==typeof e.get){const[i,s,n]=await K(()=>e.get(r.args[0]));i&&n&&(t=n)}await this.clearCacheForResource(e,t)}else if("setContent"===t||"deleteContent"===t){const t=r.args[0]?.id||r.args[0];await this.clearCacheForResource(e,{id:t})}else if("replace"===t){const t=r.args[0];await this.clearCacheForResource(e,{id:t,...r.args[1]})}else"deleteMany"===t&&await this.clearCacheForResource(e);return s})}async clearCacheForResource(e,t){if(!e.cache)return;const r=`resource=${e.name}`;if(t&&t.id){const i=["get","exists","content","hasContent"];for(const r of i)try{const i=await this.generateCacheKey(e,r,{id:t.id});await e.cache.clear(i.replace(".json.gz",""))}catch(e){}if(!0===this.config.includePartitions&&e.config?.partitions&&Object.keys(e.config.partitions).length>0){const i=this.getPartitionValues(t,e);for(const[t,s]of Object.entries(i))if(s&&Object.keys(s).length>0&&Object.values(s).some(e=>null!=e))try{const i=be(r,`partition=${t}`);await e.cache.clear(i)}catch(e){}}}try{await e.cache.clear(r)}catch(t){const i=["count","list","listIds","getAll","page","query"];for(const t of i)try{await e.cache.clear(`${r}/action=${t}`),await e.cache.clear(`resource=${e.name}/action=${t}`)}catch(e){}}}async generateCacheKey(e,t,r={},i=null,s=null){const n=[`resource=${e.name}`,`action=${t}`];if(i&&s&&Object.keys(s).length>0){n.push(`partition:${i}`);for(const[e,t]of Object.entries(s))null!=t&&n.push(`${e}:${t}`)}if(Object.keys(r).length>0){const e=await this.hashParams(r);n.push(e)}return be(...n)+".json.gz"}async hashParams(e){const t=Object.keys(e).sort().map(t=>`${t}:${JSON.stringify(e[t])}`).join("|")||"empty";return await J(t)}async getCacheStats(){return this.driver?{size:await this.driver.size(),keys:await this.driver.keys(),driver:this.driver.constructor.name}:null}async clearAllCache(){if(this.driver)for(const e of Object.values(this.database.resources))if(e.cache){const t=`resource=${e.name}`;await e.cache.clear(t)}}async warmCache(e,t={}){const r=this.database.resources[e];if(!r)throw new Error(`Resource '${e}' not found`);const{includePartitions:i=!0}=t;if(this.driver instanceof si&&r.warmPartitionCache){const e=r.config.partitions?Object.keys(r.config.partitions):[];return await r.warmPartitionCache(e,t)}if(await r.getAll(),i&&r.config.partitions)for(const[e,t]of Object.entries(r.config.partitions))if(t.fields){const t=await r.getAll(),i=Array.isArray(t)?t:[],s=new Set;for(const t of i.slice(0,10)){const i=this.getPartitionValues(t,r);i[e]&&s.add(JSON.stringify(i[e]))}for(const t of s){const i=JSON.parse(t);await r.list({partition:e,partitionValues:i})}}}async getPartitionCacheStats(e,t=null){if(!(this.driver instanceof si))throw new Error("Partition cache statistics are only available with PartitionAwareFilesystemCache");return await this.driver.getPartitionStats(e,t)}async getCacheRecommendations(e){if(!(this.driver instanceof si))throw new Error("Cache recommendations are only available with PartitionAwareFilesystemCache");return await this.driver.getCacheRecommendations(e)}async clearPartitionCache(e,t,r={}){if(!(this.driver instanceof si))throw new Error("Partition cache clearing is only available with PartitionAwareFilesystemCache");return await this.driver.clearPartition(e,t,r)}async analyzeCacheUsage(){if(!(this.driver instanceof si))return{message:"Cache usage analysis is only available with PartitionAwareFilesystemCache"};const e={totalResources:Object.keys(this.database.resources).length,resourceStats:{},recommendations:{},summary:{mostUsedPartitions:[],leastUsedPartitions:[],suggestedOptimizations:[]}};for(const[t,r]of Object.entries(this.database.resources))try{e.resourceStats[t]=await this.driver.getPartitionStats(t),e.recommendations[t]=await this.driver.getCacheRecommendations(t)}catch(r){e.resourceStats[t]={error:r.message}}const t=Object.values(e.recommendations).flat();return e.summary.mostUsedPartitions=t.filter(e=>"preload"===e.recommendation).sort((e,t)=>t.priority-e.priority).slice(0,5),e.summary.leastUsedPartitions=t.filter(e=>"archive"===e.recommendation).slice(0,5),e.summary.suggestedOptimizations=[`Consider preloading ${e.summary.mostUsedPartitions.length} high-usage partitions`,`Archive ${e.summary.leastUsedPartitions.length} unused partitions`,"Monitor cache hit rates for partition efficiency"],e}},e.Client=fi,e.ConnectionString=hi,e.ConnectionStringError=U,e.CostsPlugin=ni,e.CryptoError=F,e.DEFAULT_BEHAVIOR=Ui,e.Database=zi,e.DatabaseError=class extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}},e.EncryptionError=class extends x{constructor(e,t={}){super(e,t),Object.assign(this,t)}},e.ErrorMap=M,e.FullTextPlugin=class extends ue{constructor(e={}){super(),this.indexResource=null,this.config={minWordLength:e.minWordLength||3,maxResults:e.maxResults||100,...e},this.indexes=new Map}async setup(e){this.database=e;const[t,r,i]=await K(()=>e.createResource({name:"fulltext_indexes",attributes:{id:"string|required",resourceName:"string|required",fieldName:"string|required",word:"string|required",recordIds:"json|required",count:"number|required",lastUpdated:"string|required"}}));this.indexResource=t?i:e.resources.fulltext_indexes,await this.loadIndexes(),this.installDatabaseHooks(),this.installIndexingHooks()}async start(){}async stop(){await this.saveIndexes(),this.removeDatabaseHooks()}async loadIndexes(){if(!this.indexResource)return;const[e,t,r]=await K(()=>this.indexResource.getAll());if(e)for(const e of r){const t=`${e.resourceName}:${e.fieldName}:${e.word}`;this.indexes.set(t,{recordIds:e.recordIds||[],count:e.count||0})}}async saveIndexes(){if(!this.indexResource)return;const[e,t]=await K(async()=>{const e=await this.indexResource.getAll();for(const t of e)await this.indexResource.delete(t.id);for(const[e,t]of this.indexes.entries()){const[r,i,s]=e.split(":");await this.indexResource.insert({id:`index-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,resourceName:r,fieldName:i,word:s,recordIds:t.recordIds,count:t.count,lastUpdated:(new Date).toISOString()})}})}installDatabaseHooks(){this.database.addHook("afterCreateResource",e=>{"fulltext_indexes"!==e.name&&this.installResourceHooks(e)})}removeDatabaseHooks(){this.database.removeHook("afterCreateResource",this.installResourceHooks.bind(this))}installIndexingHooks(){this.database.plugins||(this.database.plugins={}),this.database.plugins.fulltext=this;for(const e of Object.values(this.database.resources))"fulltext_indexes"!==e.name&&this.installResourceHooks(e);this.database._fulltextProxyInstalled||(this.database._previousCreateResourceForFullText=this.database.createResource,this.database.createResource=async function(...e){const t=await this._previousCreateResourceForFullText(...e);return this.plugins?.fulltext&&"fulltext_indexes"!==t.name&&this.plugins.fulltext.installResourceHooks(t),t},this.database._fulltextProxyInstalled=!0);for(const e of Object.values(this.database.resources))"fulltext_indexes"!==e.name&&this.installResourceHooks(e)}installResourceHooks(e){e._insert=e.insert,e._update=e.update,e._delete=e.delete,e._deleteMany=e.deleteMany,this.wrapResourceMethod(e,"insert",async(t,r,i)=>{const[s]=r;return this.indexRecord(e.name,t.id,s).catch(()=>{}),t}),this.wrapResourceMethod(e,"update",async(t,r,i)=>{const[s,n]=r;return this.removeRecordFromIndex(e.name,s).catch(()=>{}),this.indexRecord(e.name,s,t).catch(()=>{}),t}),this.wrapResourceMethod(e,"delete",async(t,r,i)=>{const[s]=r;return this.removeRecordFromIndex(e.name,s).catch(()=>{}),t}),this.wrapResourceMethod(e,"deleteMany",async(t,r,i)=>{const[s]=r;for(const t of s)this.removeRecordFromIndex(e.name,t).catch(()=>{});return t})}async indexRecord(e,t,r){const i=this.getIndexedFields(e);if(i&&0!==i.length)for(const s of i){const i=this.getFieldValue(r,s);if(!i)continue;const n=this.tokenize(i);for(const r of n){if(r.length<this.config.minWordLength)continue;const i=`${e}:${s}:${r.toLowerCase()}`,n=this.indexes.get(i)||{recordIds:[],count:0};n.recordIds.includes(t)||(n.recordIds.push(t),n.count=n.recordIds.length),this.indexes.set(i,n)}}}async removeRecordFromIndex(e,t){for(const[r,i]of this.indexes.entries())if(r.startsWith(`${e}:`)){const e=i.recordIds.indexOf(t);e>-1&&(i.recordIds.splice(e,1),i.count=i.recordIds.length,0===i.recordIds.length?this.indexes.delete(r):this.indexes.set(r,i))}}getFieldValue(e,t){if(!t.includes("."))return e&&void 0!==e[t]?e[t]:null;const r=t.split(".");let i=e;for(const e of r){if(!i||"object"!=typeof i||!(e in i))return null;i=i[e]}return i}tokenize(e){if(!e)return[];return String(e).toLowerCase().replace(/[^\w\s\u00C0-\u017F]/g," ").split(/\s+/).filter(e=>e.length>0)}getIndexedFields(e){if(this.config.fields)return this.config.fields;return{users:["name","email"],products:["name","description"],articles:["title","content"]}[e]||[]}async search(e,t,r={}){const{fields:i=null,limit:s=this.config.maxResults,offset:n=0,exactMatch:o=!1}=r;if(!t||0===t.trim().length)return[];const a=this.tokenize(t),c=new Map,l=i||this.getIndexedFields(e);if(0===l.length)return[];for(const t of a)if(!(t.length<this.config.minWordLength))for(const r of l)if(o){const i=`${e}:${r}:${t.toLowerCase()}`,s=this.indexes.get(i);if(s)for(const e of s.recordIds){const t=c.get(e)||0;c.set(e,t+1)}}else for(const[i,s]of this.indexes.entries())if(i.startsWith(`${e}:${r}:${t.toLowerCase()}`))for(const e of s.recordIds){const t=c.get(e)||0;c.set(e,t+1)}return Array.from(c.entries()).map(([e,t])=>({recordId:e,score:t})).sort((e,t)=>t.score-e.score).slice(n,n+s)}async searchRecords(e,t,r={}){const i=await this.search(e,t,r);if(0===i.length)return[];const s=this.database.resources[e];if(!s)throw new Error(`Resource '${e}' not found`);const n=i.map(e=>e.recordId);return(await s.getMany(n)).filter(e=>e&&"object"==typeof e).map(e=>{const t=i.find(t=>t.recordId===e.id);return{...e,_searchScore:t?t.score:0}}).sort((e,t)=>t._searchScore-e._searchScore)}async rebuildIndex(e){const t=this.database.resources[e];if(!t)throw new Error(`Resource '${e}' not found`);for(const[t]of this.indexes.entries())t.startsWith(`${e}:`)&&this.indexes.delete(t);const r=await t.getAll();for(let t=0;t<r.length;t+=100){const i=r.slice(t,t+100);for(const t of i){const[r,i]=await K(()=>this.indexRecord(e,t.id,t))}}await this.saveIndexes()}async getIndexStats(){const e={totalIndexes:this.indexes.size,resources:{},totalWords:0};for(const[t,r]of this.indexes.entries()){const[i,s]=t.split(":");e.resources[i]||(e.resources[i]={fields:{},totalRecords:new Set,totalWords:0}),e.resources[i].fields[s]||(e.resources[i].fields[s]={words:0,totalOccurrences:0}),e.resources[i].fields[s].words++,e.resources[i].fields[s].totalOccurrences+=r.count,e.resources[i].totalWords++;for(const t of r.recordIds)e.resources[i].totalRecords.add(t);e.totalWords++}for(const t in e.resources)e.resources[t].totalRecords=e.resources[t].totalRecords.size;return e}async rebuildAllIndexes({timeout:e}={}){return e?Promise.race([this._rebuildAllIndexesInternal(),new Promise((t,r)=>setTimeout(()=>r(new Error("Timeout")),e))]):this._rebuildAllIndexesInternal()}async _rebuildAllIndexesInternal(){const e=Object.keys(this.database.resources).filter(e=>"fulltext_indexes"!==e);for(const t of e){const[e,r]=await K(()=>this.rebuildIndex(t))}}async clearIndex(e){for(const[t]of this.indexes.entries())t.startsWith(`${e}:`)&&this.indexes.delete(t);await this.saveIndexes()}async clearAllIndexes(){this.indexes.clear(),await this.saveIndexes()}},e.InvalidResourceItem=I,e.MetricsPlugin=class extends ue{constructor(e={}){super(),this.config={collectPerformance:!1!==e.collectPerformance,collectErrors:!1!==e.collectErrors,collectUsage:!1!==e.collectUsage,retentionDays:e.retentionDays||30,flushInterval:e.flushInterval||6e4,...e},this.metrics={operations:{insert:{count:0,totalTime:0,errors:0},update:{count:0,totalTime:0,errors:0},delete:{count:0,totalTime:0,errors:0},get:{count:0,totalTime:0,errors:0},list:{count:0,totalTime:0,errors:0},count:{count:0,totalTime:0,errors:0}},resources:{},errors:[],performance:[],startTime:(new Date).toISOString()},this.flushTimer=null}async setup(e){if(this.database=e,"undefined"!=typeof process&&"test"===process.env.NODE_ENV)return;const[t,r]=await K(async()=>{const[t,r,i]=await K(()=>e.createResource({name:"metrics",attributes:{id:"string|required",type:"string|required",resourceName:"string",operation:"string",count:"number|required",totalTime:"number|required",errors:"number|required",avgTime:"number|required",timestamp:"string|required",metadata:"json"}}));this.metricsResource=t?i:e.resources.metrics;const[s,n,o]=await K(()=>e.createResource({name:"error_logs",attributes:{id:"string|required",resourceName:"string|required",operation:"string|required",error:"string|required",timestamp:"string|required",metadata:"json"}}));this.errorsResource=s?o:e.resources.error_logs;const[a,c,l]=await K(()=>e.createResource({name:"performance_logs",attributes:{id:"string|required",resourceName:"string|required",operation:"string|required",duration:"number|required",timestamp:"string|required",metadata:"json"}}));this.performanceResource=a?l:e.resources.performance_logs});t||(this.metricsResource=e.resources.metrics,this.errorsResource=e.resources.error_logs,this.performanceResource=e.resources.performance_logs),this.installDatabaseHooks(),this.installMetricsHooks(),"undefined"!=typeof process&&"test"!==process.env.NODE_ENV&&this.startFlushTimer()}async start(){}async stop(){this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=null),this.removeDatabaseHooks()}installDatabaseHooks(){this.database.addHook("afterCreateResource",e=>{"metrics"!==e.name&&"error_logs"!==e.name&&"performance_logs"!==e.name&&this.installResourceHooks(e)})}removeDatabaseHooks(){this.database.removeHook("afterCreateResource",this.installResourceHooks.bind(this))}installMetricsHooks(){for(const e of Object.values(this.database.resources))["metrics","error_logs","performance_logs"].includes(e.name)||this.installResourceHooks(e);this.database._createResource=this.database.createResource,this.database.createResource=async function(...e){const t=await this._createResource(...e);return this.plugins?.metrics&&!["metrics","error_logs","performance_logs"].includes(t.name)&&this.plugins.metrics.installResourceHooks(t),t}}installResourceHooks(e){e._insert=e.insert,e._update=e.update,e._delete=e.delete,e._deleteMany=e.deleteMany,e._get=e.get,e._getMany=e.getMany,e._getAll=e.getAll,e._list=e.list,e._listIds=e.listIds,e._count=e.count,e._page=e.page,e.insert=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._insert(...t));if(this.recordOperation(e.name,"insert",Date.now()-r,!i),i||this.recordError(e.name,"insert",s),!i)throw s;return n}.bind(this),e.update=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._update(...t));if(this.recordOperation(e.name,"update",Date.now()-r,!i),i||this.recordError(e.name,"update",s),!i)throw s;return n}.bind(this),e.delete=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._delete(...t));if(this.recordOperation(e.name,"delete",Date.now()-r,!i),i||this.recordError(e.name,"delete",s),!i)throw s;return n}.bind(this),e.deleteMany=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._deleteMany(...t));if(this.recordOperation(e.name,"delete",Date.now()-r,!i),i||this.recordError(e.name,"delete",s),!i)throw s;return n}.bind(this),e.get=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._get(...t));if(this.recordOperation(e.name,"get",Date.now()-r,!i),i||this.recordError(e.name,"get",s),!i)throw s;return n}.bind(this),e.getMany=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._getMany(...t));if(this.recordOperation(e.name,"get",Date.now()-r,!i),i||this.recordError(e.name,"get",s),!i)throw s;return n}.bind(this),e.getAll=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._getAll(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this),e.list=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._list(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this),e.listIds=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._listIds(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this),e.count=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._count(...t));if(this.recordOperation(e.name,"count",Date.now()-r,!i),i||this.recordError(e.name,"count",s),!i)throw s;return n}.bind(this),e.page=async function(...t){const r=Date.now(),[i,s,n]=await K(()=>e._page(...t));if(this.recordOperation(e.name,"list",Date.now()-r,!i),i||this.recordError(e.name,"list",s),!i)throw s;return n}.bind(this)}recordOperation(e,t,r,i){this.metrics.operations[t]&&(this.metrics.operations[t].count++,this.metrics.operations[t].totalTime+=r,i&&this.metrics.operations[t].errors++),this.metrics.resources[e]||(this.metrics.resources[e]={insert:{count:0,totalTime:0,errors:0},update:{count:0,totalTime:0,errors:0},delete:{count:0,totalTime:0,errors:0},get:{count:0,totalTime:0,errors:0},list:{count:0,totalTime:0,errors:0},count:{count:0,totalTime:0,errors:0}}),this.metrics.resources[e][t]&&(this.metrics.resources[e][t].count++,this.metrics.resources[e][t].totalTime+=r,i&&this.metrics.resources[e][t].errors++),this.config.collectPerformance&&this.metrics.performance.push({resourceName:e,operation:t,duration:r,timestamp:(new Date).toISOString()})}recordError(e,t,r){this.config.collectErrors&&this.metrics.errors.push({resourceName:e,operation:t,error:r.message,stack:r.stack,timestamp:(new Date).toISOString()})}startFlushTimer(){this.flushTimer&&clearInterval(this.flushTimer),this.config.flushInterval>0&&(this.flushTimer=setInterval(()=>{this.flushMetrics().catch(()=>{})},this.config.flushInterval))}async flushMetrics(){if(!this.metricsResource)return;const[e,t]=await K(async()=>{let e,t,r,i;"undefined"!=typeof process&&"test"===process.env.NODE_ENV?(e={},t={},r={},i={}):(e={global:"true"},t={perf:"true"},r={error:"true"},i={resource:"true"});for(const[t,r]of Object.entries(this.metrics.operations))r.count>0&&await this.metricsResource.insert({id:`metrics-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,type:"operation",resourceName:"global",operation:t,count:r.count,totalTime:r.totalTime,errors:r.errors,avgTime:r.count>0?r.totalTime/r.count:0,timestamp:(new Date).toISOString(),metadata:e});for(const[e,t]of Object.entries(this.metrics.resources))for(const[r,s]of Object.entries(t))s.count>0&&await this.metricsResource.insert({id:`metrics-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,type:"operation",resourceName:e,operation:r,count:s.count,totalTime:s.totalTime,errors:s.errors,avgTime:s.count>0?s.totalTime/s.count:0,timestamp:(new Date).toISOString(),metadata:i});if(this.config.collectPerformance&&this.metrics.performance.length>0)for(const e of this.metrics.performance)await this.performanceResource.insert({id:`perf-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,resourceName:e.resourceName,operation:e.operation,duration:e.duration,timestamp:e.timestamp,metadata:t});if(this.config.collectErrors&&this.metrics.errors.length>0)for(const e of this.metrics.errors)await this.errorsResource.insert({id:`error-${Date.now()}-${Math.random().toString(36).substr(2,9)}`,resourceName:e.resourceName,operation:e.operation,error:e.error,stack:e.stack,timestamp:e.timestamp,metadata:r});this.resetMetrics()})}resetMetrics(){for(const e of Object.keys(this.metrics.operations))this.metrics.operations[e]={count:0,totalTime:0,errors:0};for(const e of Object.keys(this.metrics.resources))for(const t of Object.keys(this.metrics.resources[e]))this.metrics.resources[e][t]={count:0,totalTime:0,errors:0};this.metrics.performance=[],this.metrics.errors=[]}async getMetrics(e={}){const{type:t="operation",resourceName:r,operation:i,startDate:s,endDate:n,limit:o=100,offset:a=0}=e;if(!this.metricsResource)return[];let c=(await this.metricsResource.getAll()).filter(e=>(!t||e.type===t)&&((!r||e.resourceName===r)&&((!i||e.operation===i)&&(!(s&&new Date(e.timestamp)<new Date(s))&&!(n&&new Date(e.timestamp)>new Date(n))))));return c.sort((e,t)=>new Date(t.timestamp)-new Date(e.timestamp)),c.slice(a,a+o)}async getErrorLogs(e={}){if(!this.errorsResource)return[];const{resourceName:t,operation:r,startDate:i,endDate:s,limit:n=100,offset:o=0}=e;let a=(await this.errorsResource.getAll()).filter(e=>(!t||e.resourceName===t)&&((!r||e.operation===r)&&(!(i&&new Date(e.timestamp)<new Date(i))&&!(s&&new Date(e.timestamp)>new Date(s)))));return a.sort((e,t)=>new Date(t.timestamp)-new Date(e.timestamp)),a.slice(o,o+n)}async getPerformanceLogs(e={}){if(!this.performanceResource)return[];const{resourceName:t,operation:r,startDate:i,endDate:s,limit:n=100,offset:o=0}=e;let a=(await this.performanceResource.getAll()).filter(e=>(!t||e.resourceName===t)&&((!r||e.operation===r)&&(!(i&&new Date(e.timestamp)<new Date(i))&&!(s&&new Date(e.timestamp)>new Date(s)))));return a.sort((e,t)=>new Date(t.timestamp)-new Date(e.timestamp)),a.slice(o,o+n)}async getStats(){const e=new Date,t=new Date(e.getTime()-864e5),[r,i,s]=await Promise.all([this.getMetrics({startDate:t.toISOString()}),this.getErrorLogs({startDate:t.toISOString()}),this.getPerformanceLogs({startDate:t.toISOString()})]),n={period:"24h",totalOperations:0,totalErrors:i.length,avgResponseTime:0,operationsByType:{},resources:{},uptime:{startTime:this.metrics.startTime,duration:e.getTime()-new Date(this.metrics.startTime).getTime()}};for(const e of r)if("operation"===e.type){n.totalOperations+=e.count,n.operationsByType[e.operation]||(n.operationsByType[e.operation]={count:0,errors:0,avgTime:0}),n.operationsByType[e.operation].count+=e.count,n.operationsByType[e.operation].errors+=e.errors;const t=n.operationsByType[e.operation],r=t.count,i=(t.avgTime*(r-e.count)+e.totalTime)/r;t.avgTime=i}const o=r.reduce((e,t)=>e+t.totalTime,0),a=r.reduce((e,t)=>e+t.count,0);return n.avgResponseTime=a>0?o/a:0,n}async cleanupOldData(){const e=new Date;if(e.setDate(e.getDate()-this.config.retentionDays),this.metricsResource){const t=await this.getMetrics({endDate:e.toISOString()});for(const e of t)await this.metricsResource.delete(e.id)}if(this.errorsResource){const t=await this.getErrorLogs({endDate:e.toISOString()});for(const e of t)await this.errorsResource.delete(e.id)}if(this.performanceResource){const t=await this.getPerformanceLogs({endDate:e.toISOString()});for(const e of t)await this.performanceResource.delete(e.id)}}},e.MissingMetadata=P,e.NoSuchBucket=C,e.NoSuchKey=N,e.NotFound=$,e.PartitionError=q,e.PermissionError=A,e.Plugin=ue,e.PluginObject=he,e.ReplicatorPlugin=class extends ue{constructor(e={}){if(super(),!e.replicators||!Array.isArray(e.replicators))throw new Error("ReplicatorPlugin: replicators array is required");for(const t of e.replicators){if(!t.driver)throw new Error("ReplicatorPlugin: each replicator must have a driver");if(!t.resources||"object"!=typeof t.resources)throw new Error("ReplicatorPlugin: each replicator must have resources config");if(0===Object.keys(t.resources).length)throw new Error("ReplicatorPlugin: each replicator must have at least one resource configured")}this.config={replicators:e.replicators||[],logErrors:!1!==e.logErrors,replicatorLogResource:e.replicatorLogResource||"replicator_log",enabled:!1!==e.enabled,batchSize:e.batchSize||100,maxRetries:e.maxRetries||3,timeout:e.timeout||3e4,verbose:e.verbose||!1,...e},this.replicators=[],this.database=null,this.eventListenersInstalled=new Set}async decompressData(e){return e}filterInternalFields(e){if(!e||"object"!=typeof e)return e;const t={};for(const[r,i]of Object.entries(e))r.startsWith("_")||"$overflow"===r||"$before"===r||"$after"===r||(t[r]=i);return t}async getCompleteData(e,t){const[r,i,s]=await K(()=>e.get(t.id));return r?s:t}installEventListeners(e,t,r){e&&!this.eventListenersInstalled.has(e.name)&&e.name!==this.config.replicatorLogResource&&(e.on("insert",async t=>{const[i,s]=await K(async()=>{const i={...t,createdAt:(new Date).toISOString()};await r.processReplicatorEvent("insert",e.name,i.id,i)});i||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Insert event failed for resource ${e.name}: ${s.message}`),this.emit("error",{operation:"insert",error:s.message,resource:e.name}))}),e.on("update",async(t,i)=>{const[s,n]=await K(async()=>{const s=await r.getCompleteData(e,t),n={...s,updatedAt:(new Date).toISOString()};await r.processReplicatorEvent("update",e.name,s.id,n,i)});s||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Update event failed for resource ${e.name}: ${n.message}`),this.emit("error",{operation:"update",error:n.message,resource:e.name}))}),e.on("delete",async t=>{const[i,s]=await K(async()=>{await r.processReplicatorEvent("delete",e.name,t.id,t)});i||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Delete event failed for resource ${e.name}: ${s.message}`),this.emit("error",{operation:"delete",error:s.message,resource:e.name}))}),this.eventListenersInstalled.add(e.name))}async setup(e){if(this.database=e,this.config.persistReplicatorLog){const[t,r,i]=await K(()=>e.createResource({name:this.config.replicatorLogResource||"replicator_logs",attributes:{id:"string|required",resource:"string|required",action:"string|required",data:"json",timestamp:"number|required",createdAt:"string|required"},behavior:"truncate-data"}));this.replicatorLogResource=t?i:e.resources[this.config.replicatorLogResource||"replicator_logs"]}await this.initializeReplicators(e),this.installDatabaseHooks();for(const t of Object.values(e.resources))t.name!==(this.config.replicatorLogResource||"replicator_logs")&&this.installEventListeners(t,e,this)}async start(){}async stop(){for(const e of this.replicators||[])e&&"function"==typeof e.cleanup&&await e.cleanup();this.removeDatabaseHooks()}installDatabaseHooks(){this.database.addHook("afterCreateResource",e=>{e.name!==(this.config.replicatorLogResource||"replicator_logs")&&this.installEventListeners(e,this.database,this)})}removeDatabaseHooks(){this.database.removeHook("afterCreateResource",this.installEventListeners.bind(this))}createReplicator(e,t,r,i){return function(e,t={},r=[],i=null){const s=Wi[e];if(!s)throw new Error(`Unknown replicator driver: ${e}. Available drivers: ${Object.keys(Wi).join(", ")}`);return new s(t,r,i)}(e,t,r,i)}async initializeReplicators(e){for(const t of this.config.replicators){const{driver:r,config:i={},resources:s,client:n,...o}=t,a=s||i.resources||{},c={...i,...o},l=this.createReplicator(r,c,a,n);l&&(await l.initialize(e),this.replicators.push(l))}}async uploadMetadataFile(e){"function"==typeof e.uploadMetadataFile&&await e.uploadMetadataFile()}async retryWithBackoff(e,t=3){let r;for(let i=1;i<=t;i++){const[s,n]=await K(e);if(s)return s;{if(r=n,this.config.verbose&&console.warn(`[ReplicatorPlugin] Retry attempt ${i}/${t} failed: ${n.message}`),i===t)throw n;const e=1e3*Math.pow(2,i-1);this.config.verbose&&console.warn(`[ReplicatorPlugin] Waiting ${e}ms before retry...`),await new Promise(t=>setTimeout(t,e))}}throw r}async logError(e,t,r,i,s,n){const[o,a]=await K(async()=>{const o=this.config.replicatorLogResource;if(this.database&&this.database.resources&&this.database.resources[o]){const a=this.database.resources[o];await a.insert({replicator:e.name||e.id,resourceName:t,operation:r,recordId:i,data:JSON.stringify(s),error:n.message,timestamp:(new Date).toISOString(),status:"error"})}});o||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to log error for ${t}: ${a.message}`),this.emit("replicator_log_error",{replicator:e.name||e.id,resourceName:t,operation:r,recordId:i,originalError:n.message,logError:a.message}))}async processReplicatorEvent(e,t,r,i,s=null){if(!this.config.enabled)return;const n=this.replicators.filter(r=>r.shouldReplicateResource&&r.shouldReplicateResource(t,e));if(0===n.length)return;const o=n.map(async n=>{const[o,a,c]=await K(async()=>{const o=await this.retryWithBackoff(()=>n.replicate(t,e,i,r,s),this.config.maxRetries);return this.emit("replicated",{replicator:n.name||n.id,resourceName:t,operation:e,recordId:r,result:o,success:!0}),o});if(o)return c;throw this.config.verbose&&console.warn(`[ReplicatorPlugin] Replication failed for ${n.name||n.id} on ${t}: ${a.message}`),this.emit("replicator_error",{replicator:n.name||n.id,resourceName:t,operation:e,recordId:r,error:a.message}),this.config.logErrors&&this.database&&await this.logError(n,t,e,r,i,a),a});return Promise.allSettled(o)}async processreplicatorItem(e){const t=this.replicators.filter(t=>t.shouldReplicateResource&&t.shouldReplicateResource(e.resourceName,e.operation));if(0===t.length)return;const r=t.map(async t=>{const[r,i]=await K(async()=>{const[r,i,s]=await K(()=>t.replicate(e.resourceName,e.operation,e.data,e.recordId,e.beforeData));return r?(this.emit("replicated",{replicator:t.name||t.id,resourceName:e.resourceName,operation:e.operation,recordId:e.recordId,result:s,success:!0}),{success:!0,result:s}):(this.config.verbose&&console.warn(`[ReplicatorPlugin] Replicator item processing failed for ${t.name||t.id} on ${e.resourceName}: ${i.message}`),this.emit("replicator_error",{replicator:t.name||t.id,resourceName:e.resourceName,operation:e.operation,recordId:e.recordId,error:i.message}),this.config.logErrors&&this.database&&await this.logError(t,e.resourceName,e.operation,e.recordId,e.data,i),{success:!1,error:i.message})});return r||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Wrapper processing failed for ${t.name||t.id} on ${e.resourceName}: ${i.message}`),this.emit("replicator_error",{replicator:t.name||t.id,resourceName:e.resourceName,operation:e.operation,recordId:e.recordId,error:i.message}),this.config.logErrors&&this.database&&await this.logError(t,e.resourceName,e.operation,e.recordId,e.data,i),{success:!1,error:i.message})});return Promise.allSettled(r)}async logreplicator(e){const t=this.replicatorLog||this.database.resources[Ji(this.config.replicatorLogResource)];if(!t)return this.database&&this.database.options&&this.database.options.connectionString,void this.emit("replicator.log.failed",{error:"replicator log resource not found",item:e});const r={id:e.id||`repl-${Date.now()}-${Math.random().toString(36).slice(2)}`,resource:e.resource||e.resourceName||"",action:e.operation||e.action||"",data:e.data||{},timestamp:"number"==typeof e.timestamp?e.timestamp:Date.now(),createdAt:e.createdAt||(new Date).toISOString().slice(0,10)},[i,s]=await K(async()=>{await t.insert(r)});i||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to log replicator item: ${s.message}`),this.emit("replicator.log.failed",{error:s,item:e}))}async updatereplicatorLog(e,t){if(!this.replicatorLog)return;const[r,i]=await K(async()=>{await this.replicatorLog.update(e,{...t,lastAttempt:(new Date).toISOString()})});r||this.emit("replicator.updateLog.failed",{error:i.message,logId:e,updates:t})}async getreplicatorStats(){return{replicators:await Promise.all(this.replicators.map(async e=>{const t=await e.getStatus();return{id:e.id,driver:e.driver,config:e.config,status:t}})),queue:{length:this.queue.length,isProcessing:this.isProcessing},stats:this.stats,lastSync:this.stats.lastSync}}async getreplicatorLogs(e={}){if(!this.replicatorLog)return[];const{resourceName:t,operation:r,status:i,limit:s=100,offset:n=0}=e;let o={};t&&(o.resourceName=t),r&&(o.operation=r),i&&(o.status=i);return(await this.replicatorLog.list(o)).slice(n,n+s)}async retryFailedreplicators(){if(!this.replicatorLog)return{retried:0};const e=await this.replicatorLog.list({status:"failed"});let t=0;for(const r of e){const[e,i]=await K(async()=>{await this.processReplicatorEvent(r.resourceName,r.operation,r.recordId,r.data)});e&&t++}return{retried:t}}async syncAllData(e){const t=this.replicators.find(t=>t.id===e);if(!t)throw new Error(`Replicator not found: ${e}`);this.stats.lastSync=(new Date).toISOString();for(const r in this.database.resources)if(Ji(r)!==Ji("replicator_logs")&&t.shouldReplicateResource(r)){this.emit("replicator.sync.resource",{resourceName:r,replicatorId:e});const i=this.database.resources[r],s=await i.getAll();for(const e of s)await t.replicate(r,"insert",e,e.id)}this.emit("replicator.sync.completed",{replicatorId:e,stats:this.stats})}async cleanup(){const[e,t]=await K(async()=>{if(this.replicators&&this.replicators.length>0){const e=this.replicators.map(async e=>{const[t,r]=await K(async()=>{e&&"function"==typeof e.cleanup&&await e.cleanup()});t||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to cleanup replicator ${e.name||e.id}: ${r.message}`),this.emit("replicator_cleanup_error",{replicator:e.name||e.id||"unknown",driver:e.driver||"unknown",error:r.message}))});await Promise.allSettled(e)}this.replicators=[],this.database=null,this.eventListenersInstalled.clear(),this.removeAllListeners()});e||(this.config.verbose&&console.warn(`[ReplicatorPlugin] Failed to cleanup plugin: ${t.message}`),this.emit("replicator_plugin_cleanup_error",{error:t.message}))}},e.Resource=Fi,e.ResourceError=z,e.ResourceIdsPageReader=Yr,e.ResourceIdsReader=Jr,e.ResourceNotFound=D,e.ResourceReader=Qr,e.ResourceWriter=Zr,e.S3db=zi,e.S3dbError=x,e.Schema=wi,e.SchemaError=B,e.UnknownError=T,e.ValidationError=E,e.Validator=gi,e.behaviors=Ti,e.calculateAttributeNamesSize=v,e.calculateAttributeSizes=_,e.calculateEffectiveLimit=R,e.calculateSystemOverhead=O,e.calculateTotalSize=S,e.calculateUTF8Bytes=w,e.decode=g,e.decodeDecimal=b,e.decrypt=Y,e.default=qi,e.encode=m,e.encodeDecimal=y,e.encrypt=G,e.getBehavior=Mi,e.getSizeBreakdown=function(e){const t=_(e),r=v(e),i=Object.values(t).reduce((e,t)=>e+t,0),s=i+r,n=Object.entries(t).sort(([,e],[,t])=>t-e).map(([e,t])=>({attribute:e,size:t,percentage:(t/s*100).toFixed(2)+"%"}));return{total:s,valueSizes:t,namesSize:r,valueTotal:i,breakdown:n,detailedBreakdown:{values:i,names:r,total:s}}},e.idGenerator=X,e.mapAwsError=L,e.md5=Q,e.passwordGenerator=ee,e.sha256=J,e.streamToString=Xr,e.transformValue=k,e.tryFn=H,e.tryFnSync=V,Object.defineProperty(e,"__esModule",{value:!0}),e}({},nanoid,zlib,PromisePool,streams,promises,crypto,_,stringify,nodeHttpHandler,AWS,flat,FastestValidator);
|