@asaidimu/utils-artifacts 8.2.11 → 8.2.13

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/index.js CHANGED
@@ -1 +1,2 @@
1
- "use strict";var e=require("@asaidimu/utils-error"),t=class t extends e.SystemError{constructor(e,r){super({code:"SYNC_ERROR",message:e,cause:r}),this.name="SyncError",Object.setPrototypeOf(this,t.prototype)}},r=class e extends t{constructor(t){super(`[ArtifactContainer] Operation timed out: ${t}`),this.name="TimeoutError",Object.setPrototypeOf(this,e.prototype)}},s=class e extends t{constructor(t){super("[Serializer] The serializer has been marked as done!",t),this.name="SerializerExecutionDone",Object.setPrototypeOf(this,e.prototype)}},i=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise(e=>t=e);if(this.waiters.push(t),null==e)return void await s;let i;await Promise.race([s.then(()=>clearTimeout(i)),new Promise((s,n)=>{i=setTimeout(()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new r("Mutex lock timed out"))},e)})])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},n=class{mutex=new i({yieldMode:"microtask"});promise=null;_value=null;_error;_done=!1;retry;throws;constructor({retry:e,throws:t}={}){this.retry=Boolean(e),this.throws=Boolean(t)}resolve(e){if(this._done)throw new Error("Cannot resolve: operation is already completed.");if(this.running())throw new Error("Cannot resolve: operation is currently running.");this._value=e,this._done=!0}async do(e,t){return this._done?this.peek():this.promise?this._awaitWithTimeout(this.promise,t,"Once do() timed out"):(await this.mutex.lock(),this.promise?(this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")):(this.promise=(async()=>{try{this._value=await e(),this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.retry&&!this._done&&(this.promise=null)}return this.peek()})(),this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")))}doSync(e){if(this._done){if(this.throws&&this._error)throw this._error;return this.peek()}if(this.promise){const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}if(!this.mutex.tryLock()){const e=new Error("Cannot execute doSync: lock is currently held.");if(this.throws)throw e;return{value:null,error:e}}if(this.promise||this._done){if(this.mutex.unlock(),this._done){if(this.throws&&this._error)throw this._error;return this.peek()}const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}try{const t=e();this._value=t,this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.mutex.unlock()}return this.peek()}running(){return null!==this.promise&&!this.done()}peek(){return{value:this._value,error:this._error}}get(){if(!this._done)throw new Error("Once operation is not yet complete");if(this._error)throw this._error;return this._value}reset(){if(this.running())throw new Error("Cannot reset Once while an operation is in progress.");this._done=!1,this.promise=null,this._value=null,this._error=void 0}done(){return this._done}current(){return this.promise}_awaitWithTimeout(e,t,s="Operation timed out"){if(null==t)return e;let i;return Promise.race([e.then(e=>(clearTimeout(i),e)),new Promise((e,n)=>{i=setTimeout(()=>n(new r(s)),t)})])}},o=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new i({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new s};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let r,i=null;try{if(this._done)throw new s;i=await e(),this._lastValue=i,this._lastError=void 0,this._hasRun=!0}catch(e){r=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:i,error:r}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}},a=class{constructor(e,t,r={}){this.factory=e,this.onCleanup=t,this.options=r}_count=0;init=new n({retry:!1,throws:!1});pendingMicrotask=!1;cleanupTimer;get subscribers(){return this._count}async acquire(){this.cancelPendingCleanup(),this._count++;const e=await this.init.do(this.factory);if(e.error)throw e.error;return e.value}release(){this._count<=0?console.warn("SharedResource.release() called, but count is already 0."):(this._count--,0===this._count&&this.scheduleCleanup())}peek(){const e=this.init.peek();return e.error?null:e.value}forceCleanup(){this.cancelPendingCleanup(),this._count=0,this.executeCleanup()}cancelPendingCleanup(){this.pendingMicrotask=!1,void 0!==this.cleanupTimer&&(clearTimeout(this.cleanupTimer),this.cleanupTimer=void 0)}scheduleCleanup(){const e=this.options.gracePeriod??"microtask";if("sync"!==e)return"microtask"===e?(this.pendingMicrotask=!0,void queueMicrotask(()=>{!this.pendingMicrotask||this._count>0||(this.pendingMicrotask=!1,this.executeCleanup())})):void(this.cleanupTimer=setTimeout(()=>{this.cleanupTimer=void 0,this._count>0||this.executeCleanup()},e));this.executeCleanup()}executeCleanup(){const e=this.init.peek();try{this.onCleanup(e.value)}catch(e){console.error("[SharedResource] Error during cleanup callback:",e)}this.init.running()||this.init.reset()}};function c(e,t){if(!e.length)return;const r=e.slice(),s=t?`[${t}]`:"[ArtifactCleanup]";return async()=>{for(let e=r.length-1;e>=0;e--)try{await r[e]()}catch(t){console.error(`${s} Cleanup error at index ${e}:`,t)}}}async function h(e,t){const r=t?`[${t}]`:"[ArtifactCleanup]";for(let t=e.length-1;t>=0;t--)try{await e[t]()}catch(e){console.error(`${r} Cleanup error at index ${t}:`,e)}}function d(e){return`${e}__watched`}var u=Object.freeze({instance:void 0,error:void 0,ready:!1,cleanup:void 0,invalidate:async(e,t)=>{}});function l(e,t){let r;const s=new Promise((e,s)=>{r=setTimeout(()=>s(new Error(`Timeout: ${t}ms`)),t)});try{return Promise.race([e,s])}finally{clearTimeout(r)}}async function p(e){const t=(new TextEncoder).encode(e),r=await crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(r)).map(e=>e.toString(16).padStart(2,"0")).join("")}var f=class{cache=new Map;get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}delete(e){this.cache.delete(e)}has(e){return this.cache.has(e)}clear(){this.cache.clear()}size(){return this.cache.size}keys(){return Array.from(this.cache.keys())}package(e,t,r){const s=this.get(e);if(!s)return u;if(s.package)return s.package;const i=e,n=c(s.cleanupFunctions,i),o="singleton"!==s.scope||void 0!==s.instance,a={instance:s.instance,error:s.error,ready:o,[r||e]:s.instance,cleanup:n,invalidate:t};return s.package=a,a}invalidatePackage(e){const t=this.get(e);t&&(t.package=void 0)}async invalidateInstance(e,t=!1){const r=this.get(e);if(!r)return;const s=e;"singleton"===r.scope&&(r.stateUnsubscribe&&(r.stateUnsubscribe(),r.stateUnsubscribe=void 0),r.debounceTimer&&(clearTimeout(r.debounceTimer),r.debounceTimer=void 0),r.controller.abort(),await r.streamOnce.current(),r.streamSerializer.close(),r.stream=void 0,r.streamOnce=new n({retry:!0,throws:!0}),t||(r.streamSerializer=new o,r.controller=new AbortController)),await h(r.cleanupFunctions,s),await h(r.disposeFunctions,s),r.cleanupFunctions=[],r.disposeFunctions=[],"singleton"===r.scope&&(r.buildOnce=new n({retry:!0,throws:!0})),r.instance=void 0,r.error=void 0}},g=class{dependencies=new Map;dependents=new Map;registerNode(e){this.dependencies.has(e)||this.dependencies.set(e,new Set),this.dependents.has(e)||this.dependents.set(e,new Set)}removeNode(e){if(!this.hasNode(e))return;const t=this.dependencies.get(e);if(t)for(const r of t)this.dependents.get(r)?.delete(e);const r=this.dependents.get(e);if(r)for(const t of r)this.dependencies.get(t)?.delete(e);this.dependencies.delete(e),this.dependents.delete(e)}hasNode(e){return this.dependencies.has(e)}addDependency(e,t){this.registerNode(e),this.registerNode(t),this.dependencies.get(e).add(t),this.dependents.get(t).add(e)}removeDependency(e,t){this.dependencies.get(e)?.delete(t),this.dependents.get(t)?.delete(e)}getDependencies(e){const t=this.dependencies.get(e);return t?new Set(t):new Set}getDependents(e){const t=this.dependents.get(e);return t?new Set(t):new Set}iterDependents(e){return this.dependents.get(e)??y}setDependencies(e,t){this.registerNode(e);const r=this.dependencies.get(e),s=new Set(t);for(const t of r)s.has(t)||this.removeDependency(e,t);for(const t of s)r.has(t)||this.addDependency(e,t)}wouldCreateCycle(e,t){if(e===t)return[e,t];const r=[t],s=new Set([t]),i=new Map;for(;r.length>0;){const n=r.shift();if(n===e){const r=[];let s=e;for(;void 0!==s&&(r.push(s),s!==t);)s=i.get(s);return r.reverse(),r.unshift(e),r}const o=this.dependencies.get(n);if(o)for(const e of o)s.has(e)||(s.add(e),i.set(e,n),r.push(e))}return null}topologicalSort(){const e=new Map,t=Array.from(this.dependencies.keys());for(const r of t)e.set(r,this.dependencies.get(r)?.size??0);const r=[];for(const[t,s]of e)0===s&&r.push(t);const s=[];for(;r.length>0;){const t=r.shift();s.push(t);const i=this.dependents.get(t);if(i)for(const t of i){const s=(e.get(t)||0)-1;e.set(t,s),0===s&&r.push(t)}}if(s.length!==t.length)throw new Error("Cycle detected in graph; topological sort impossible.");return s}getTransitiveDependencies(e,t=!1){return this.bfs(e,"dependencies",t)}getTransitiveDependents(e,t=!1){return this.bfs(e,"dependents",t)}bfs(e,t,r){const s=new Set,i=new Set,n=[e];i.add(e),r&&s.add(e);const o="dependencies"===t?this.dependencies:this.dependents;for(;n.length>0;){const e=n.shift(),t=o.get(e);if(t)for(const e of t)i.has(e)||(i.add(e),s.add(e),n.push(e))}return s}getAllNodes(){return Array.from(this.dependencies.keys())}size(){return this.dependencies.size}clear(){this.dependencies.clear(),this.dependents.clear()}toDebugString(){return Array.from(this.dependencies.entries()).map(([e,t])=>{const r=Array.from(t).join(", ");return`${String(e)} → [${r||"∅"}]`}).join("\n")}},y=new Set,w=class{graph;constructor(){this.graph=new g}registerNode(e){this.graph.registerNode(e)}removeNode(e){this.graph.removeNode(e)}addDependency(e,t){this.graph.addDependency(e,t)}removeDependency(e,t){this.graph.removeDependency(e,t)}getDependents(e){return Array.from(this.graph.getDependents(e))}iterDependents(e){return this.graph.iterDependents(e)}getDependencies(e){return Array.from(this.graph.getDependencies(e))}getTransitiveDependents(e){return new Set(Array.from(this.graph.getTransitiveDependents(e,!1)))}setDependencies(e,t){const r=Array.from(t).map(e=>e);this.graph.setDependencies(e,r)}wouldCreateCycle(e,t,r){if(r?.has(t)){const e=Array.from(r),s=e.indexOf(t),i=e.slice(s);return i.push(t),i}const s=this.graph.wouldCreateCycle(e,t);return s||null}hasNode(e){return this.graph.hasNode(e)}getAllNodes(){return this.graph.getAllNodes()}clear(){this.graph.clear()}size(){return this.graph.size()}toDebugString(){return this.graph.toDebugString()}},m=["map","filter","reduce","forEach","find","findIndex","some","every","includes","flatMap","flat","slice","splice"];var v=class e extends Error{category;constructor(t,r,s){super(t,{cause:s}),this.name="ArtifactError",this.category=r,Object.setPrototypeOf(this,e.prototype)}},b=class extends v{constructor(e){super(`[ArtifactContainer] Artifact "${e}" not found.`,"system"),this.name="ArtifactNotFoundError"}},k=class extends v{constructor(){super("Build superseded by invalidation","system"),this.name="SupersededBuildError"}},S=class extends v{constructor(e){super(e,"system"),this.name="InvalidImportError"}},_=class extends v{constructor(e){super(`Artifact "${e}" instance is not JSON-serializable (POJO are required for persistence)`,"system"),this.name="InvalidExportOperationError"}},A=(new AbortController).signal,C=class{constructor(e,t,r,s,i){this.registry=e,this.cache=t,this.graph=r,this.store=s,this.observer=i}async build(e,t,r){const s=this.cache.get(e);if("singleton"===s?.scope&&s.buildOnce.done())return this.cache.package(e,(t,r)=>this.invalidate(e,t,r),r);const i=this.registry.getByString(e);if(!i)throw new b(e);const n=t??[];if(n.includes(e))throw new v(`Cycle detected: Artifact "${String(e)}" depends on itself via path: ${[...n,e].join(" -> ")}`,"system");n.push(e);let o=s;o||(o=this.createCachedArtifact(i),this.cache.set(e,o));try{if("transient"===o.scope)return this.executeBuild(i,o,n);const s=o;try{await s.buildOnce.do(()=>this.executeBuild(i,s,n))}catch(s){if(s instanceof k)return this.build(e,t,r);throw s}return s.stream&&s.streamOnce.do(s.stream),this.cache.package(e,(t,r)=>this.invalidate(e,t,r),r)}finally{n.pop()}}async executeBuild(e,t,r){const s=e.key,i=String(s),n="transient"===t.scope;t.buildCount++,"singleton"===t.scope&&(t.activeDebounceMs=e.debounce??0);const o={cleanupFunctions:[],disposeFunctions:[],capturedArtifactDeps:new Set,capturedStateSelectors:[],dependencyVersions:new Map},a=this.buildContext(e,t,r,i,o),h=await this.runWithRetries(e,a,o.dependencyVersions);if(this.commitResult(s,t,n,h,o),n){const e=h.ok?h.value:void 0;return{instance:e,error:h.ok?void 0:h.error,ready:h.ok,[s]:e,cleanup:c(o.cleanupFunctions,i),invalidate:async()=>console.warn(`[ArtifactManager] Cannot invalidate transient "${i}"`)}}}buildContext(e,t,r,s,i){const n=e.key,o="transient"===t.scope,{cleanupFunctions:a,disposeFunctions:c,capturedArtifactDeps:h,dependencyVersions:d,capturedStateSelectors:u}=i,l=async(e,t)=>{const i=t?this.computeParamKey(e,t):e;if(i===n)throw new v(`Artifact "${s}" depends on itself.`,"system");const o=this.graph.wouldCreateCycle(n,i);if(o)throw new v(`Adding dependency "${String(i)}" to "${s}" would create a cycle: ${o.join(" -> ")}`,"system");h.add(i);const a=await(t?this.resolveParameterized(e,t):this.build(e,r)),c=this.cache.get(i);return c&&d.set(i,c.version),a},p="singleton"===t.scope?t.controller.signal:A,f=async(e,t)=>{const r=await l(e,t);if(r.error)throw r.error;return r.instance},g=(e,t)=>{const r=function(e,t="."){const r=new Set,s=new Map,i=(e="")=>{if(s.has(e))return s.get(e);const n=new Proxy(()=>{},{get:(s,n)=>{if("symbol"==typeof n||"then"===n)return;if("valueOf"===n||"toString"===n)throw new Error("Cannot perform logic, arithmetic, or string operations inside a selector.");if(m.includes(n))throw new Error(`Array method .${n}() is not allowed in selectors.`);const o=e?`${e}${t}${n}`:n;return e&&r.delete(e),r.add(o),i(o)},has:()=>{throw new Error("The 'in' operator is not allowed in selectors.")},apply:()=>{throw new Error("Selectors cannot call functions or methods.")}});return s.set(e,n),n};try{e(i())}catch(e){throw new Error(`Selector failed during path analysis. Selectors must be simple property accessors only. Error: ${e instanceof Error?e.message:String(e)}`)}return Array.from(r)}(e);return u.push({paths:r,options:t}),e(this.store.get(!0))};return{state:()=>this.store.get(!0),previous:t.instance,signal:p,onCleanup:e=>a.push(e),onDispose:e=>c.push(e),use:e=>e({resolve:l,require:f,select:g}),stream:e=>{if(o)throw new v(`[ArtifactManager] Illegal stream on transient artifact "${s}"`,"system");const r=t,i=async(e,t=void 0)=>{await r.streamSerializer.do(async()=>{void 0!==r.stream&&(r.instance=e,r.error=t,r.version++,this.cache.invalidatePackage(n),await this.processStream(s))})},a={value:()=>r.instance,get signal(){return r.controller.signal},set:(...e)=>this.store.set(...e),emit:e=>i(e)};r.stream=async()=>{try{const t=await e(a);t&&r.cleanupFunctions.push(t)}catch(e){await i(void 0,e),await this.invalidate(n,!1,!0)}}}}}async runWithRetries(e,t,r){const s=t.signal,i=(e.retries??0)+1;let n=0;for(;n<i;)try{if(s.aborted)throw new k;const o=e.factory(t);let a;if(a=o instanceof Promise?e.timeout?await l(o,e.timeout):await o:o,s.aborted)throw new k;const c=this.detectStaleness(r);if(c){if(n++,r.clear(),n<i)continue;return{ok:!1,error:new v(`Build stale after all retries: dependency "${String(c)}" changed during build.`,"system")}}return{ok:!0,value:a}}catch(e){if(s.aborted||e instanceof k)throw new k;if(e instanceof v)throw e;if(n++,n>=i)return{ok:!1,error:e}}return{ok:!1,error:new Error("Build exhausted retry budget unexpectedly.")}}commitResult(e,t,r,s,i){const{cleanupFunctions:n,disposeFunctions:o,capturedArtifactDeps:a,capturedStateSelectors:c}=i;r||(this.updateDependencyGraph(e,a,c),t.cleanupFunctions=n,t.disposeFunctions=o,t.artifactDependencies=new Set(a),t.stateGroups=c.map(({paths:e,options:t})=>({paths:e,options:t}))),s.ok?(t.instance=s.value,t.error=void 0):(t.instance=void 0,t.error=s.error),t.version++,this.cache.invalidatePackage(e)}detectStaleness(e){for(const[t,r]of e){const e=this.cache.get(t);if(e&&e.version!==r)return t}return null}async invalidate(e,t=!1,r=!1){const s=this.cache.get(e);if(!s)return;if("singleton"!==s.scope)return this.executeInvalidation(e,t,r);const i=s;return i.debounceTimer&&(clearTimeout(i.debounceTimer),i.debounceTimer=void 0),!t&&i.activeDebounceMs>0?new Promise((s,n)=>{i.debounceTimer=setTimeout(()=>{i.debounceTimer=void 0,this.executeInvalidation(e,t,r).then(s).catch(n)},i.activeDebounceMs)}):this.executeInvalidation(e,t,r)}async executeInvalidation(e,t,r=!1){const s=this.cache.get(e);s&&"singleton"===s.scope&&await s.invalidationSerializer.do(async()=>{s.version++,await this.cache.invalidateInstance(e),await this.cascadeInvalidation(this.graph.iterDependents(e));const i=this.registry.get(e),n=i&&(t||!i.lazy||this.observer.hasWatchers(e))&&!r;n&&await this.build(e).catch(t=>{t instanceof k||console.error(`[ArtifactManager] Rebuild failed for "${String(e)}":`,t)}),(n||r)&&this.observer.notify(e)})}async dispose(e){this.cache.get(e)&&(await this.cache.invalidateInstance(e,!0),this.graph.removeNode(e),this.cache.delete(e))}async processStream(e){await this.cascadeInvalidation(this.graph.iterDependents(e)),this.observer.notify(e)}async cascadeInvalidation(e){if(0===e.size)return;const t=[];for(const r of e)t.push(this.invalidate(r).catch(e=>{console.error(`[ArtifactManager] Cascade failed for "${String(r)}":`,e)}));await Promise.all(t)}updateDependencyGraph(e,t,r){const s=this.cache.get(e);if(!s||"singleton"!==s.scope)return;this.graph.registerNode(e),this.graph.setDependencies(e,t),s.stateDependencies=new Set;for(const{paths:e}of r)for(const t of e)s.stateDependencies.add(t);if(s.stateUnsubscribe&&(s.stateUnsubscribe(),s.stateUnsubscribe=void 0),0===r.length)return;const i=()=>this.invalidate(e),n=new Map;for(const{paths:e,options:t}of r){const r=void 0===t?"undefined":JSON.stringify(t);let s=n.get(r);s||(s={options:t,paths:new Set},n.set(r,s));for(const t of e)s.paths.add(t)}const o=[];for(const{options:e,paths:t}of n.values()){if(0===t.size)continue;const r=Array.from(t);void 0===e?o.push(this.store.watch(r,i)):o.push(this.store.watch(r,i,e))}0===o.length?s.stateUnsubscribe=void 0:1===o.length?s.stateUnsubscribe=o[0]:s.stateUnsubscribe=()=>{for(const e of o)e()}}createCachedArtifact(e){return"transient"===e.scope?{scope:"transient",instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0}:{scope:"singleton",instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set,activeDebounceMs:e.debounce?e.debounce:0,controller:new AbortController,buildOnce:new n({retry:!0,throws:!0}),streamOnce:new n({retry:!0,throws:!0}),streamSerializer:new o({yieldMode:"microtask"}),invalidationSerializer:new o({yieldMode:"macrotask"}),stateGroups:[],artifactDependencies:new Set}}resolveStatic(e){if(!this.registry.has(e))throw new b(e);return this.build(e)}async resolveParameterized(e,t){if(!this.registry.has(e))throw new b(String(e));const r=this.registry.get(e);if(!r.paramKey)throw new v(`Artifact "${String(e)}" is not parameterized.`,"external");const s=r.paramKey(t),i=this.registry.getByString(s);if(i&&!i.paramKey&&!i.virtual)throw new v(`Parameterized artifact "${String(e)}" with params ${JSON.stringify(t)} resolves to key "${s}" which is already registered as a static artifact.`,"system");return this.registry.hasString(s)||this.registry.setVirtual(s,{key:s,factory:e=>r.factory({...e,params:t}),scope:r.scope,lazy:r.lazy,timeout:r.timeout,retries:r.retries,debounce:r.debounce,virtual:!0}),this.build(s,void 0,e)}computeParamKey(e,t){const r=this.registry.get(e);if(!r.paramKey)throw new Error(`Artifact "${String(e)}" is not parameterized.`);return r.paramKey(t)}},x=class{constructor(e,t,r){this.registry=e,this.cache=t,this.container=r}listeners=new Map;watchers=new Map;watch(e,t=6e4){return this.watchForKey(e,this.registry.get(e),t)}watchParameterized(e,t,r=6e4){const s=this.registry.get(e);if(!s.paramKey)throw new Error(`Artifact "${String(e)}" is not parameterized.`);const i=s.paramKey(t);return this.watchForKey(i,s,r,t)}watchForKey(e,t,r,s){const i="transient"===t.scope,n=i?d(e):e,o=this.watchers.get(n);if(o)return o.observer;const c=new a(async()=>{!i&&void 0===s||this.registry.hasString(e)||this.registry.setVirtual(e,{key:e,factory:i?t.factory:e=>t.factory({...e,params:s}),scope:i?"singleton":t.scope,lazy:t.lazy,timeout:t.timeout,retries:t.retries,debounce:t.debounce,paramKey:t.paramKey,virtual:!0})},async()=>{i||void 0!==s?(await this.registry.unregister(n).catch(()=>{}),this.cache.delete(n),this.watchers.delete(n)):this.cache.invalidatePackage(n),this.listeners.delete(n)},{gracePeriod:i&&void 0===s?"sync":r});let h;const l={id:n,get count(){return c.subscribers},get:(t=!1)=>0!==c.subscribers||t?this.cache.package(n,(e,t)=>this.container.invalidate(n,e,t),e):u,resolve:()=>h||(h=(async()=>{await c.acquire();try{return await this.container.resolve(n)}finally{c.release(),h=void 0}})(),h),subscribe:(e,t=!0)=>{const r=()=>e(l.get());return c.acquire().then(()=>{this.container.resolve(n).then(()=>{this.listeners.has(n)||this.listeners.set(n,new Set),t&&r(),this.listeners.get(n).add(r)})}).catch(e=>{console.error(`[ArtifactObserver] Resolution failed for "${n}":`,e),this.listeners.get(n)?.add(r)}),()=>{this.listeners.get(n)?.delete(r),c.release()}}};return this.watchers.set(n,{resource:c,observer:l}),l}evictWatcher(e){[e,d(e)].forEach(e=>{const t=this.watchers.get(e);t&&(t.resource.forceCleanup(),this.watchers.delete(e))})}notify(e){const t=this.listeners.get(e);if(t&&0!==t.size)for(const r of t)try{r()}catch(t){console.error(`[ArtifactObserver] Listener error for "${e}":`,t)}}hasWatchers(e){return this.watchers.has(e)||this.watchers.has(d(e))}getWatcherCount(e){const t=this.watchers.get(e)||this.watchers.get(d(e));return t?.resource.subscribers??0}clear(){this.watchers.clear(),this.listeners.clear()}},D=(e=>(e.Singleton="singleton",e.Transient="transient",e))(D||{}),O=class{artifacts=new Map;register({key:e,factory:t,lazy:r,...s}){const{scope:i,...n}=s,o={key:e,factory:t,scope:s.scope??"singleton",lazy:void 0===r||r,...n};return this.artifacts.set(e,o),()=>this.unregister(e)}setVirtual(e,t){this.artifacts.set(e,t)}get(e){if(!this.has(e))throw new b(String(e));return this.artifacts.get(e)}getByString(e){return this.artifacts.get(e)}has(e){return this.artifacts.has(e)}hasString(e){return this.artifacts.has(e)}async unregister(e){this.artifacts.has(e)&&this.artifacts.delete(e)}size(){return this.artifacts.size}keys(){return Array.from(this.artifacts.keys())}clear(){this.artifacts.clear()}};exports.ArtifactContainer=class e{registry;cache;graph;manager;observer;store;constructor(e){this.store={watch:(...t)=>e.watch(...t),get:()=>e.get(!0),set:(...t)=>e.set(...t),subset:(...t)=>e.subset(...t)},this.registry=new O,this.cache=new f,this.graph=new w,this.observer=new x(this.registry,this.cache,this),this.manager=new C(this.registry,this.cache,this.graph,this.store,this.observer)}debugInfo(){const e=[];return this.registry.keys().forEach(t=>{const r=t,s=this.registry.get(r),i=this.cache.get(r);if(!s)return;let n="idle";i&&("singleton"===i.scope&&void 0!==i.debounceTimer?n="debouncing":"singleton"===i.scope&&i.buildOnce.running()?n="building":i.error?n="error":void 0!==i.instance&&(n="active")),e.push({id:r,scope:s.scope??"singleton",status:n,dependencies:this.graph.getDependencies(r).map(e=>String(e)),dependents:this.graph.getDependents(r).map(e=>String(e)),stateDependencies:"singleton"===i?.scope?Array.from(i.stateDependencies):[],buildCount:i?.buildCount??0})}),e}register(e){const{key:t}=e,r=t;this.registry.has(t)&&(console.warn(`[ArtifactContainer] Overwriting "${r}".`),this.manager.dispose(t).catch(e=>{console.error(`[ArtifactContainer] Failed to dispose existing artifact "${r}":`,e)})),this.registry.register(e),this.graph.registerNode(t);const s=e.scope??"singleton";return(e.lazy??!0)||"singleton"!==s||this.resolve(t).catch(e=>{console.error(`[ArtifactContainer] Eager load failed for "${r}":`,e)}),()=>this.unregister(t)}has(e){return this.registry.has(e)}async unregister(e,t){const r=void 0!==t?this.manager.computeParamKey(e,t):e;await this.manager.dispose(r),await this.registry.unregister(r),this.observer.evictWatcher(r)}async resolve(e,t){return void 0===t?this.manager.resolveStatic(e):this.manager.resolveParameterized(e,t)}async require(e,t){const r=await this.resolve(e,t);if(r.error)throw r.error;return r.instance}watch(e,t,r){return void 0===t?this.observer.watch(e,r):this.observer.watchParameterized(e,t,r)}peek(e,t){const r=void 0!==t?this.manager.computeParamKey(e,t):e;return this.cache.get(r)?.instance}async invalidate(e,t){const r=t?.params,s=t?.replace??!1,i=void 0!==r?this.manager.computeParamKey(e,r):e;return this.manager.invalidate(i,s)}notifyObservers(e){this.observer.notify(e)}hasWatchers(e){return this.observer.hasWatchers(e)}async dispose(){const e=this.registry.keys();await Promise.allSettled(e.map(e=>this.manager.dispose(e).catch(t=>{console.error(`[ArtifactContainer] Failed to dispose artifact "${String(e)}":`,t)}))),this.registry.clear(),this.cache.clear(),this.graph.clear(),this.observer.clear()}async export(){const e=[];for(const t of this.cache.keys()){const r=this.cache.get(t);if(!r||"singleton"!==r.scope)continue;if(void 0===r.instance)continue;const s=new Set;for(const e of r.stateGroups)for(const t of e.paths)s.add(t);const i=Array.from(s).sort(),n=this.store.subset(i),o=await p(JSON.stringify(n,Object.keys(n).sort()));let a;try{a=structuredClone(r.instance)}catch(e){throw new _(t)}e.push({key:t,instance:a,state:{groups:r.stateGroups.map(e=>({paths:e.paths,options:e.options})),hash:o},dependencies:Array.from(r.artifactDependencies)})}const t={...{version:"1.0",timestamp:Date.now(),artifacts:e}},r=JSON.stringify(t),s=await p(r);return{...t,checksum:s}}async restore(e){const{checksum:t,...r}=e;if(await p(JSON.stringify(r))!==t)throw new S("Bundle checksum mismatch – data corrupted");if("1.0"!==e.version)throw new S(`Unsupported bundle version: ${e.version}`);const s=new Map;for(const t of e.artifacts)s.set(t.key,t);const i=new Set,a=new Map;for(const t of e.artifacts)for(const e of t.dependencies)a.has(e)||a.set(e,new Set),a.get(e).add(t.key);const c=async e=>{const t=new Set;for(const r of e.state.groups)for(const e of r.paths)t.add(e);const r=Array.from(t).sort(),s=this.store.subset(r);return p(JSON.stringify(s,Object.keys(s).sort()))};for(const t of e.artifacts){await c(t)!==t.state.hash&&i.add(t.key)}const h=Array.from(i);for(;h.length;){const e=h.shift(),t=a.get(e);if(t)for(const e of t)i.has(e)||(i.add(e),h.push(e))}for(const t of e.artifacts){if(i.has(t.key))continue;this.cache.get(t.key)&&await this.cache.invalidateInstance(t.key,!1);const e={scope:"singleton",instance:t.instance,error:void 0,version:1,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set(t.state.groups.flatMap(e=>e.paths)),activeDebounceMs:0,buildOnce:new n({retry:!0,throws:!0}),streamSerializer:new o({yieldMode:"microtask"}),invalidationSerializer:new o({yieldMode:"macrotask"}),stateUnsubscribe:void 0,debounceTimer:void 0,controller:new AbortController,streamOnce:new n({retry:!0,throws:!0}),stream:void 0,stateGroups:t.state.groups,artifactDependencies:new Set(t.dependencies)};e.buildOnce.resolve(t.instance),this.cache.set(t.key,e);const r=[];for(const e of t.state.groups){const s=this.store.watch(e.paths,()=>this.invalidate(t.key),e.options);r.push(s)}e.stateUnsubscribe=()=>{for(const e of r)e()},this.graph.setDependencies(t.key,t.dependencies)}}static async from(t){const r=new e(t.store);if(t.bundle){if("object"!=typeof t.bundle||!t.bundle.version||!Array.isArray(t.bundle.artifacts))throw new S("Invalid bundle: missing version or artifacts array");await r.restore(t.bundle)}for(const e of t.templates)r.register(e);return r}},exports.ArtifactScopes=D;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@asaidimu/utils-sync"),t=require("@asaidimu/utils-store/selector"),n=require("@asaidimu/utils-error"),r=require("@asaidimu/utils-events"),i=require("@asaidimu/utils-logger");function a(e,t){if(!e.length)return;let n=e.slice(),r=t?`[${t}]`:`[ArtifactCleanup]`;return async()=>{for(let e=n.length-1;e>=0;e--)try{await n[e]()}catch(t){console.error(`${r} Cleanup error at index ${e}:`,t)}}}async function o(e,t){let n=t?`[${t}]`:`[ArtifactCleanup]`;for(let t=e.length-1;t>=0;t--)try{await e[t]()}catch(e){console.error(`${n} Cleanup error at index ${t}:`,e)}}function s(e){return`${e}__watched`}const c=Object.freeze({instance:void 0,error:void 0,ready:!1,cleanup:void 0,invalidate:async(e,t)=>{}});function l(e,t){let n,r=new Promise((e,r)=>{n=setTimeout(()=>r(Error(`Timeout: ${t}ms`)),t)});try{return Promise.race([e,r])}finally{clearTimeout(n)}}async function u(e){let t=new TextEncoder().encode(e),n=await crypto.subtle.digest(`SHA-256`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}var d=class{cache=new Map;get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}delete(e){this.cache.delete(e)}has(e){return this.cache.has(e)}clear(){this.cache.clear()}size(){return this.cache.size}keys(){return Array.from(this.cache.keys())}package(e,t,n){let r=this.get(e);if(!r)return c;if(r.package)return r.package;let i=e,o=a(r.cleanupFunctions,i),s=r.scope===`singleton`?r.instance!==void 0:!0,l={instance:r.instance,error:r.error,ready:s,[n||e]:r.instance,cleanup:o,invalidate:t};return r.package=l,l}invalidatePackage(e){let t=this.get(e);t&&(t.package=void 0)}async invalidateInstance(t,n=!1){let r=this.get(t);if(!r)return;let i=t;r.scope===`singleton`&&(r.stateUnsubscribe&&=(r.stateUnsubscribe(),void 0),r.debounceTimer&&=(clearTimeout(r.debounceTimer),void 0),r.controller.abort(),await r.streamOnce.current(),r.streamSerializer.close(),r.stream=void 0,r.streamOnce=new e.Once({retry:!0,throws:!0}),n||(r.streamSerializer=new e.Serializer,r.controller=new AbortController)),await o(r.cleanupFunctions,i),await o(r.disposeFunctions,i),r.cleanupFunctions=[],r.disposeFunctions=[],r.scope===`singleton`&&(r.buildOnce=new e.Once({retry:!0,throws:!0})),r.instance=void 0,r.error=void 0}},f=class{dependencies=new Map;dependents=new Map;registerNode(e){this.dependencies.has(e)||this.dependencies.set(e,new Set),this.dependents.has(e)||this.dependents.set(e,new Set)}removeNode(e){if(!this.hasNode(e))return;let t=this.dependencies.get(e);if(t)for(let n of t)this.dependents.get(n)?.delete(e);let n=this.dependents.get(e);if(n)for(let t of n)this.dependencies.get(t)?.delete(e);this.dependencies.delete(e),this.dependents.delete(e)}hasNode(e){return this.dependencies.has(e)}addDependency(e,t){this.registerNode(e),this.registerNode(t),this.dependencies.get(e).add(t),this.dependents.get(t).add(e)}removeDependency(e,t){this.dependencies.get(e)?.delete(t),this.dependents.get(t)?.delete(e)}getDependencies(e){let t=this.dependencies.get(e);return t?new Set(t):new Set}getDependents(e){let t=this.dependents.get(e);return t?new Set(t):new Set}iterDependents(e){return this.dependents.get(e)??p}setDependencies(e,t){this.registerNode(e);let n=this.dependencies.get(e),r=new Set(t);for(let t of n)r.has(t)||this.removeDependency(e,t);for(let t of r)n.has(t)||this.addDependency(e,t)}wouldCreateCycle(e,t){if(e===t)return[e,t];let n=[t],r=new Set([t]),i=new Map;for(;n.length>0;){let a=n.shift();if(a===e){let n=[],r=e;for(;r!==void 0&&(n.push(r),r!==t);)r=i.get(r);return n.reverse(),n.unshift(e),n}let o=this.dependencies.get(a);if(o)for(let e of o)r.has(e)||(r.add(e),i.set(e,a),n.push(e))}return null}topologicalSort(){let e=new Map,t=Array.from(this.dependencies.keys());for(let n of t)e.set(n,this.dependencies.get(n)?.size??0);let n=[];for(let[t,r]of e)r===0&&n.push(t);let r=[];for(;n.length>0;){let t=n.shift();r.push(t);let i=this.dependents.get(t);if(i)for(let t of i){let r=(e.get(t)||0)-1;e.set(t,r),r===0&&n.push(t)}}if(r.length!==t.length)throw Error(`Cycle detected in graph; topological sort impossible.`);return r}getTransitiveDependencies(e,t=!1){return this.bfs(e,`dependencies`,t)}getTransitiveDependents(e,t=!1){return this.bfs(e,`dependents`,t)}bfs(e,t,n){let r=new Set,i=new Set,a=[e];i.add(e),n&&r.add(e);let o=t===`dependencies`?this.dependencies:this.dependents;for(;a.length>0;){let e=a.shift(),t=o.get(e);if(t)for(let e of t)i.has(e)||(i.add(e),r.add(e),a.push(e))}return r}getAllNodes(){return Array.from(this.dependencies.keys())}size(){return this.dependencies.size}clear(){this.dependencies.clear(),this.dependents.clear()}toDebugString(){return Array.from(this.dependencies.entries()).map(([e,t])=>{let n=Array.from(t).join(`, `);return`${String(e)} → [${n||`∅`}]`}).join(`
2
+ `)}};const p=new Set;var m=class{graph;constructor(){this.graph=new f}registerNode(e){this.graph.registerNode(e)}removeNode(e){this.graph.removeNode(e)}addDependency(e,t){this.graph.addDependency(e,t)}removeDependency(e,t){this.graph.removeDependency(e,t)}getDependents(e){return Array.from(this.graph.getDependents(e))}iterDependents(e){return this.graph.iterDependents(e)}getDependencies(e){return Array.from(this.graph.getDependencies(e))}getTransitiveDependents(e){return new Set(Array.from(this.graph.getTransitiveDependents(e,!1)))}setDependencies(e,t){let n=Array.from(t).map(e=>e);this.graph.setDependencies(e,n)}wouldCreateCycle(e,t,n){if(n?.has(t)){let e=Array.from(n),r=e.indexOf(t),i=e.slice(r);return i.push(t),i}return this.graph.wouldCreateCycle(e,t)||null}hasNode(e){return this.graph.hasNode(e)}getAllNodes(){return this.graph.getAllNodes()}clear(){this.graph.clear()}size(){return this.graph.size()}toDebugString(){return this.graph.toDebugString()}};const h={NOT_FOUND:n.CommonErrors.NOT_FOUND,DUPLICATE_KEY:n.CommonErrors.DUPLICATE_KEY,INVALID_COMMAND:n.CommonErrors.INVALID_COMMAND,INTERNAL_ERROR:n.CommonErrors.INTERNAL_ERROR,CONCURRENCY_ERROR:n.CommonErrors.CONCURRENCY_ERROR},g=`[ArtifactContainer]`;function _(e){return new n.SystemError({code:h.NOT_FOUND,message:`${g} Artifact "${e}" not found.`,operation:`artifact:resolve`})}function v(e){return new n.SystemError({code:h.INTERNAL_ERROR,message:`${g} Cycle detected: ${e.join(` -> `)}`,operation:`artifact:resolve`})}function y(e){return new n.SystemError({code:h.INVALID_COMMAND,message:`${g} ${e}`,operation:`artifact:build`})}function b(e){return new n.SystemError({code:h.INTERNAL_ERROR,message:`${g} Artifact with key:${e} has already been disposed`,operation:`artifact:watch`})}function x(e){return new n.SystemError({code:h.CONCURRENCY_ERROR,message:`${g} Operation timed out: ${e}`,operation:`artifact:build`})}function S(e){return new n.SystemError({code:h.DUPLICATE_KEY,message:`${g} An artifact with key:"${e}" already exists!`,operation:`artifact:register`})}function C(e){return new n.SystemError({code:h.INVALID_COMMAND,message:`${g} Artifact "${e}" is not parameterized.`,operation:`artifact:resolve`})}function w(e,t){return new n.SystemError({code:h.DUPLICATE_KEY,message:`${g} Parameterized artifact "${e}" with params resolves to key "${t}" which is already registered as a static artifact.`,operation:`artifact:resolve`})}function T(e){return new n.SystemError({code:h.INTERNAL_ERROR,message:`${g} Artifact "${e}" depends on itself.`,operation:`artifact:build`})}function E(e){return new n.SystemError({code:h.CONCURRENCY_ERROR,message:`${g} Build stale after all retries: dependency "${String(e)}" changed during build.`,operation:`artifact:build`})}function D(e){return new n.SystemError({code:h.INVALID_COMMAND,message:e,operation:`artifact:import`})}function O(e){return new n.SystemError({code:h.INTERNAL_ERROR,message:`${g} Artifact "${e}" instance is not JSON-serializable (POJO are required for persistence)`,operation:`artifact:export`})}var k=class extends Error{constructor(){super(`Build superseded by invalidation`),this.name=`SupersededBuildError`}};const A=new AbortController().signal;var j=class{registry;cache;graph;store;observer;logger;events;constructor(e,t,n,r,i,a,o){this.registry=e,this.cache=t,this.graph=n,this.store=r,this.observer=i,this.logger=a,this.events=o}async build(e,t,n){let r=this.cache.get(e);if(r?.scope===`singleton`&&r.buildOnce.done())return this.cache.package(e,(t,n)=>this.invalidate(e,t,n),n);let i=this.registry.getByString(e);if(!i)throw _(e);let a=t??[];if(a.includes(e))throw v([...a,e].map(String));a.push(e);let o=r;o||(o=this.createCachedArtifact(i),this.cache.set(e,o)),this.events?.emit({name:`build:start`,payload:{key:e,templateKey:n}});try{if(o.scope===`transient`)return this.executeBuild(i,o,a);let r=o;try{await r.buildOnce.do(()=>this.executeBuild(i,r,a))}catch(r){if(r instanceof k)return this.build(e,t,n);throw r}return r.stream&&r.streamOnce.do(r.stream),this.events?.emit({name:`build:complete`,payload:{key:e,instance:o.instance}}),this.cache.package(e,(t,n)=>this.invalidate(e,t,n),n)}catch(t){throw t instanceof k||this.events?.emit({name:`build:error`,payload:{key:e,error:t}}),t}finally{a.pop()}}async executeBuild(e,t,n){let r=e.key,i=String(r),o=t.scope===`transient`;t.buildCount++,t.scope===`singleton`&&(t.activeDebounceMs=e.debounce??0);let s={cleanupFunctions:[],disposeFunctions:[],capturedArtifactDeps:new Set,capturedStateSelectors:[],dependencyVersions:new Map},c=this.buildContext(e,t,n,i,s),l=await this.runWithRetries(e,c,s.dependencyVersions);if(this.commitResult(r,t,o,l,s),o){let e=l.ok?l.value:void 0;return{instance:e,error:l.ok?void 0:l.error,ready:l.ok,[r]:e,cleanup:a(s.cleanupFunctions,i),invalidate:async()=>this.logger.warn(`Cannot invalidate transient "${i}"`)}}}buildContext(e,n,r,i,a){let o=e.key,s=n.scope===`transient`,{cleanupFunctions:c,disposeFunctions:l,capturedArtifactDeps:u,dependencyVersions:d,capturedStateSelectors:f}=a,p=async(e,t)=>{let n=t?this.computeParamKey(e,t):e;if(n===o)throw T(i);let a=this.graph.wouldCreateCycle(o,n);if(a)throw v(a);u.add(n);let s=await(t?this.resolveParameterized(e,t):this.build(e,r)),c=this.cache.get(n);return c&&d.set(n,c.version),s},m=n.scope===`singleton`?n.controller.signal:A,h=async(e,t)=>{let n=await p(e,t);if(n.error)throw n.error;return n.instance},g=(e,n)=>{let r=(0,t.buildPaths)(e);return f.push({paths:r,options:n}),e(this.store.get(!0))};return{state:()=>this.store.get(!0),previous:n.instance,signal:m,onCleanup:e=>c.push(e),onDispose:e=>l.push(e),use:e=>e({resolve:p,require:h,select:g}),stream:e=>{if(s)throw y(`Illegal stream on transient artifact "${i}"`);let t=n,r=async(e,n=void 0)=>{await t.streamSerializer.do(async()=>{t.stream!==void 0&&(t.instance=e,t.error=n,t.version++,this.cache.invalidatePackage(o),await this.processStream(i),this.events?.emit({name:`stream:emit`,payload:{key:i,value:e}}))})},a={value:()=>t.instance,get signal(){return t.controller.signal},set:(...e)=>this.store.set(...e),emit:e=>r(e)};t.stream=async()=>{try{let n=await e(a);n&&t.cleanupFunctions.push(n)}catch(e){await r(void 0,e),await this.invalidate(o,!1,!0)}}}}}async runWithRetries(e,t,r){let i=t.signal,a=(e.retries??0)+1,o=0;for(;o<a;)try{if(i.aborted)throw new k;let n=e.factory(t),s;if(s=n instanceof Promise?e.timeout?await l(n,e.timeout):await n:n,i.aborted)throw new k;let c=this.detectStaleness(r);if(c){if(o++,r.clear(),o<a)continue;return{ok:!1,error:E(String(c))}}return{ok:!0,value:s}}catch(e){if(i.aborted||e instanceof k)throw new k;if(e instanceof n.SystemError)throw e;if(o++,o>=a)return{ok:!1,error:e}}return{ok:!1,error:Error(`Build exhausted retry budget unexpectedly.`)}}commitResult(e,t,n,r,i){let{cleanupFunctions:a,disposeFunctions:o,capturedArtifactDeps:s,capturedStateSelectors:c}=i;n||(this.updateDependencyGraph(e,s,c),t.cleanupFunctions=a,t.disposeFunctions=o,t.artifactDependencies=new Set(s),t.stateGroups=c.map(({paths:e,options:t})=>({paths:e,options:t}))),r.ok?(t.instance=r.value,t.error=void 0):(t.instance=void 0,t.error=r.error),t.version++,this.cache.invalidatePackage(e)}detectStaleness(e){for(let[t,n]of e){let e=this.cache.get(t);if(e&&e.version!==n)return t}return null}async invalidate(e,t=!1,n=!1){let r=this.cache.get(e);if(!r)return;if(r.scope!==`singleton`)return this.executeInvalidation(e,t,n);let i=r;return i.debounceTimer&&=(clearTimeout(i.debounceTimer),void 0),!t&&i.activeDebounceMs>0?new Promise((r,a)=>{i.debounceTimer=setTimeout(()=>{i.debounceTimer=void 0,this.executeInvalidation(e,t,n).then(r).catch(a)},i.activeDebounceMs)}):this.executeInvalidation(e,t,n)}async executeInvalidation(e,t,n=!1){let r=this.cache.get(e);!r||r.scope!==`singleton`||await r.invalidationSerializer.do(async()=>{r.version++,await this.cache.invalidateInstance(e);let i=this.graph.iterDependents(e),a=i.size>0;await this.cascadeInvalidation(i),this.events?.emit({name:`artifact:invalidated`,payload:{key:e,cascade:a,replace:t}});let o=this.registry.get(e),s=o&&(t||!o.lazy||this.observer.hasWatchers(e))&&!n;s&&await this.build(e).catch(t=>{t instanceof k||this.logger.error(`Rebuild failed for "${String(e)}"`,t)}),(s||n)&&this.observer.notify(e)})}async dispose(e){this.cache.get(e)&&(await this.cache.invalidateInstance(e,!0),this.graph.removeNode(e),this.cache.delete(e),this.events?.emit({name:`artifact:disposed`,payload:{key:e}}))}async processStream(e){await this.cascadeInvalidation(this.graph.iterDependents(e)),this.observer.notify(e)}async cascadeInvalidation(e){if(e.size===0)return;let t=[];for(let n of e)t.push(this.invalidate(n).catch(e=>{this.logger.error(`Cascade failed for "${String(n)}"`,e)}));await Promise.all(t)}updateDependencyGraph(e,t,n){let r=this.cache.get(e);if(!r||r.scope!==`singleton`)return;this.graph.registerNode(e),this.graph.setDependencies(e,t),r.stateDependencies=new Set;for(let{paths:e}of n)for(let t of e)r.stateDependencies.add(t);if(r.stateUnsubscribe&&=(r.stateUnsubscribe(),void 0),n.length===0)return;let i=()=>this.invalidate(e),a=new Map;for(let{paths:e,options:t}of n){let n=t===void 0?`undefined`:JSON.stringify(t),r=a.get(n);r||(r={options:t,paths:new Set},a.set(n,r));for(let t of e)r.paths.add(t)}let o=[];for(let{options:e,paths:t}of a.values()){if(t.size===0)continue;let n=Array.from(t);e===void 0?o.push(this.store.watch(n,i)):o.push(this.store.watch(n,i,e))}o.length===0?r.stateUnsubscribe=void 0:o.length===1?r.stateUnsubscribe=o[0]:r.stateUnsubscribe=()=>{for(let e of o)e()}}createCachedArtifact(t){return t.scope===`transient`?{scope:`transient`,instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0}:{scope:`singleton`,instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set,activeDebounceMs:t.debounce?t.debounce:0,controller:new AbortController,buildOnce:new e.Once({retry:!0,throws:!0}),streamOnce:new e.Once({retry:!0,throws:!0}),streamSerializer:new e.Serializer({yieldMode:`microtask`}),invalidationSerializer:new e.Serializer({yieldMode:`macrotask`}),stateGroups:[],artifactDependencies:new Set}}resolveStatic(e){if(!this.registry.getByString(e))throw _(e);return this.build(e)}async resolveParameterized(e,t){let n=this.registry.getByString(e);if(!n)throw _(String(e));if(!n.paramKey)throw C(String(e));let r=n.paramKey(t),i=this.registry.getByString(r);if(i&&!i.paramKey&&!i.virtual)throw w(String(e),r);return this.registry.hasString(r)||this.registry.setVirtual(r,{key:r,factory:e=>n.factory({...e,params:t}),scope:n.scope,lazy:n.lazy,timeout:n.timeout,retries:n.retries,debounce:n.debounce,virtual:!0}),this.build(r,void 0,e)}computeParamKey(e,t){let n=this.registry.getByString(e);if(!n||!n.paramKey)throw C(String(e));return n.paramKey(t)}},M=class{registry;cache;container;logger;listeners=new Map;watchers=new Map;constructor(e,t,n,r){this.registry=e,this.cache=t,this.container=n,this.logger=r}watch(e,t=60*1e3){return this.watchForKey(e,this.registry.get(e),t)}watchParameterized(e,t,n=60*1e3){let r=this.registry.get(e);if(!r.paramKey)throw Error(`Artifact "${String(e)}" is not parameterized.`);let i=r.paramKey(t);return this.watchForKey(i,r,n,t)}watchForKey(t,n,r,i){let a=n.scope===`transient`,o=a?s(t):t,l=this.watchers.get(o);if(l)return l.observer;let u=new e.SharedResource(async()=>{(a||i!==void 0)&&!this.registry.hasString(t)&&this.registry.setVirtual(t,{key:t,factory:a?n.factory:e=>n.factory({...e,params:i}),scope:a?`singleton`:n.scope,lazy:n.lazy,timeout:n.timeout,retries:n.retries,debounce:n.debounce,paramKey:n.paramKey,virtual:!0})},async()=>{a||i!==void 0?(await this.registry.unregister(o).catch(()=>{}),this.cache.delete(o),this.watchers.delete(o)):this.cache.invalidatePackage(o),this.listeners.delete(o)},{gracePeriod:a&&i===void 0?`sync`:r}),d,f={id:o,get count(){return u.subscribers},get:(e=!1)=>u.subscribers===0&&!e?c:this.cache.package(o,(e,t)=>this.container.invalidate(o,e,t),t),resolve:()=>d||(d=(async()=>{await u.acquire();try{return await this.container.resolve(o)}finally{u.release(),d=void 0}})(),d),subscribe:(e,t=!0)=>{let n=()=>e(f.get());return u.acquire().then(()=>{this.container.resolve(o).then(()=>{this.listeners.has(o)||this.listeners.set(o,new Set),t&&n(),this.listeners.get(o).add(n)})}).catch(e=>{this.logger.error(`Resolution failed for "${o}"`,e),this.listeners.get(o)?.add(n)}),()=>{this.listeners.get(o)?.delete(n),u.release()}}};return this.watchers.set(o,{resource:u,observer:f}),f}evictWatcher(e){[e,s(e)].forEach(e=>{let t=this.watchers.get(e);t&&(t.resource.forceCleanup(),this.watchers.delete(e))})}notify(e){let t=this.listeners.get(e);if(!(!t||t.size===0))for(let n of t)try{n()}catch(t){this.logger.error(`Listener error for "${e}"`,t)}}hasWatchers(e){return this.watchers.has(e)||this.watchers.has(s(e))}getWatcherCount(e){return(this.watchers.get(e)||this.watchers.get(s(e)))?.resource.subscribers??0}clear(){this.watchers.clear(),this.listeners.clear()}};let N=function(e){return e.Singleton=`singleton`,e.Transient=`transient`,e}({});var P=class{artifacts=new Map;register({key:e,factory:t,lazy:n,...r}){let{scope:i,...a}=r,o={key:e,factory:t,scope:r.scope??`singleton`,lazy:n===void 0?!0:n,...a};return this.artifacts.set(e,o),()=>this.unregister(e)}setVirtual(e,t){this.artifacts.set(e,t)}get(e){if(!this.has(e))throw _(String(e));return this.artifacts.get(e)}getByString(e){return this.artifacts.get(e)}has(e){return this.artifacts.has(e)}hasString(e){return this.artifacts.has(e)}async unregister(e){this.artifacts.has(e)&&this.artifacts.delete(e)}size(){return this.artifacts.size}keys(){return Array.from(this.artifacts.keys())}clear(){this.artifacts.clear()}},F=class t{registry;cache;graph;manager;observer;logger;store;events;constructor(e,t){this.logger=t?.logger??new i.Logger([new i.ConsoleSink],{module:`artifacts`}),this.store={watch:(...t)=>e.watch(...t),get:()=>e.get(!0),set:(...t)=>e.set(...t),subset:(...t)=>e.subset(...t)},this.events=(0,r.createEventBus)({errorHandler:e=>this.logger.error(`Lifecycle event error`,e)}),this.registry=new P,this.cache=new d,this.graph=new m,this.observer=new M(this.registry,this.cache,this,this.logger),this.manager=new j(this.registry,this.cache,this.graph,this.store,this.observer,this.logger,this.events)}debugInfo(){let e=[];return this.registry.keys().forEach(t=>{let n=t,r=this.registry.getByString(n),i=this.cache.get(n);if(!r)return;let a=`idle`;i&&(i.scope===`singleton`&&i.debounceTimer!==void 0?a=`debouncing`:i.scope===`singleton`&&i.buildOnce.running()?a=`building`:i.error?a=`error`:i.instance!==void 0&&(a=`active`)),e.push({id:n,scope:r.scope??`singleton`,status:a,dependencies:this.graph.getDependencies(n).map(e=>String(e)),dependents:this.graph.getDependents(n).map(e=>String(e)),stateDependencies:i?.scope===`singleton`?Array.from(i.stateDependencies):[],buildCount:i?.buildCount??0})}),e}register(e){let{key:t}=e,n=t;this.registry.has(t)&&(this.logger.warn(`Overwriting artifact "${n}".`),this.manager.dispose(t).catch(e=>{this.logger.error(`Failed to dispose existing artifact "${n}"`,e)})),this.registry.register(e),this.graph.registerNode(t);let r=e.scope??`singleton`,i=e.lazy??!0;return this.events.emit({name:`artifact:registered`,payload:{key:n,scope:r}}),!i&&r===`singleton`&&this.resolve(t).catch(e=>{this.logger.error(`Eager load failed for "${n}"`,e)}),()=>this.unregister(t)}has(e){return this.registry.has(e)}async unregister(e,t){let n=t===void 0?e:this.manager.computeParamKey(e,t);await this.manager.dispose(n),await this.registry.unregister(n),this.observer.evictWatcher(n)}async resolve(e,t){return t===void 0?this.manager.resolveStatic(e):this.manager.resolveParameterized(e,t)}async require(e,t){let n=await this.resolve(e,t);if(n.error)throw n.error;return n.instance}watch(e,t,n){return t===void 0?this.observer.watch(e,n):this.observer.watchParameterized(e,t,n)}peek(e,t){let n=t===void 0?e:this.manager.computeParamKey(e,t);return this.cache.get(n)?.instance}async invalidate(e,t){let n=t?.params,r=t?.replace??!1,i=n===void 0?e:this.manager.computeParamKey(e,n);return this.manager.invalidate(i,r)}on(e,t){return this.events.subscribe(e,t)}once(e,t){return this.events.once(e,t)}notifyObservers(e){this.observer.notify(e)}hasWatchers(e){return this.observer.hasWatchers(e)}async dispose(){let e=this.registry.keys();await Promise.allSettled(e.map(e=>this.manager.dispose(e).catch(t=>{this.logger.error(`Failed to dispose artifact "${String(e)}"`,t)}))),this.registry.clear(),this.cache.clear(),this.graph.clear(),this.observer.clear(),this.events.emit({name:`container:dispose`,payload:{}}),this.events.clear({permanent:!0})}async export(){let e=[];for(let t of this.cache.keys()){let n=this.cache.get(t);if(!n||n.scope!==`singleton`||n.instance===void 0)continue;let r=new Set;for(let e of n.stateGroups)for(let t of e.paths)r.add(t);let i=Array.from(r).sort(),a=this.store.subset(i),o=await u(JSON.stringify(a,Object.keys(a).sort())),s;try{s=structuredClone(n.instance)}catch{throw O(t)}e.push({key:t,instance:s,state:{groups:n.stateGroups.map(e=>({paths:e.paths,options:e.options})),hash:o},dependencies:Array.from(n.artifactDependencies)})}let t={version:`1.0`,timestamp:Date.now(),artifacts:e},n=await u(JSON.stringify(t));return{...t,checksum:n}}async restore(t){let{checksum:n,...r}=t;if(await u(JSON.stringify(r))!==n)throw D(`Bundle checksum mismatch – data corrupted`);if(t.version!==`1.0`)throw D(`Unsupported bundle version: ${t.version}`);let i=new Map;for(let e of t.artifacts)i.set(e.key,e);let a=new Set,o=new Map;for(let e of t.artifacts)for(let t of e.dependencies)o.has(t)||o.set(t,new Set),o.get(t).add(e.key);let s=async e=>{let t=new Set;for(let n of e.state.groups)for(let e of n.paths)t.add(e);let n=Array.from(t).sort(),r=this.store.subset(n);return u(JSON.stringify(r,Object.keys(r).sort()))};for(let e of t.artifacts)await s(e)!==e.state.hash&&a.add(e.key);let c=Array.from(a);for(;c.length;){let e=c.shift(),t=o.get(e);if(t)for(let e of t)a.has(e)||(a.add(e),c.push(e))}for(let n of t.artifacts){if(a.has(n.key))continue;this.cache.get(n.key)&&await this.cache.invalidateInstance(n.key,!1);let t={scope:`singleton`,instance:n.instance,error:void 0,version:1,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set(n.state.groups.flatMap(e=>e.paths)),activeDebounceMs:0,buildOnce:new e.Once({retry:!0,throws:!0}),streamSerializer:new e.Serializer({yieldMode:`microtask`}),invalidationSerializer:new e.Serializer({yieldMode:`macrotask`}),stateUnsubscribe:void 0,debounceTimer:void 0,controller:new AbortController,streamOnce:new e.Once({retry:!0,throws:!0}),stream:void 0,stateGroups:n.state.groups,artifactDependencies:new Set(n.dependencies)};t.buildOnce.resolve(n.instance),this.cache.set(n.key,t);let r=[];for(let e of n.state.groups){let t=this.store.watch(e.paths,()=>this.invalidate(n.key),e.options);r.push(t)}t.stateUnsubscribe=()=>{for(let e of r)e()},this.graph.setDependencies(n.key,n.dependencies)}}static async from(e){let n=new t(e.store,{logger:e.logger});if(e.bundle){if(typeof e.bundle!=`object`||!e.bundle.version||!Array.isArray(e.bundle.artifacts))throw D(`Invalid bundle: missing version or artifacts array`);await n.restore(e.bundle)}for(let t of e.templates)n.register(t);return n}};exports.ArtifactContainer=F,exports.ArtifactScopes=N,exports.ErrorCodes=h,exports.SupersededBuildError=k,Object.defineProperty(exports,"SystemError",{enumerable:!0,get:function(){return n.SystemError}}),exports.artifactNotFound=_,exports.buildStaleAfterRetries=E,exports.cycleDetected=v,exports.illegalScope=y,exports.invalidExport=O,exports.invalidImport=D,exports.keyConflict=S,exports.notParameterized=C,exports.paramKeyCollision=w,exports.selfDependency=T,exports.timeoutError=x,exports.watcherDisposed=b;
package/index.mjs CHANGED
@@ -1 +1,2 @@
1
- import{SystemError as e}from"@asaidimu/utils-error";var t=class t extends e{constructor(e,r){super({code:"SYNC_ERROR",message:e,cause:r}),this.name="SyncError",Object.setPrototypeOf(this,t.prototype)}},r=class e extends t{constructor(t){super(`[ArtifactContainer] Operation timed out: ${t}`),this.name="TimeoutError",Object.setPrototypeOf(this,e.prototype)}},s=class e extends t{constructor(t){super("[Serializer] The serializer has been marked as done!",t),this.name="SerializerExecutionDone",Object.setPrototypeOf(this,e.prototype)}},i=class{_locked=!1;_capacity;_yieldMode;waiters=[];constructor(e){this._capacity=e?.capacity??1/0,this._yieldMode=e?.yieldMode??"macrotask"}async lock(e){if(!this._locked)return void(this._locked=!0);if(this.waiters.length>=this._capacity)throw new Error(`Mutex queue is full (capacity: ${this._capacity})`);let t;const s=new Promise(e=>t=e);if(this.waiters.push(t),null==e)return void await s;let i;await Promise.race([s.then(()=>clearTimeout(i)),new Promise((s,n)=>{i=setTimeout(()=>{const e=this.waiters.indexOf(t);-1!==e&&this.waiters.splice(e,1),n(new r("Mutex lock timed out"))},e)})])}tryLock(){return!this._locked&&(this._locked=!0,!0)}unlock(){if(!this._locked)throw new Error("Mutex is not locked");const e=this.waiters.shift();e?"microtask"===this._yieldMode?queueMicrotask(e):setTimeout(e,0):this._locked=!1}locked(){return this._locked}pending(){return this.waiters.length}},n=class{mutex=new i({yieldMode:"microtask"});promise=null;_value=null;_error;_done=!1;retry;throws;constructor({retry:e,throws:t}={}){this.retry=Boolean(e),this.throws=Boolean(t)}resolve(e){if(this._done)throw new Error("Cannot resolve: operation is already completed.");if(this.running())throw new Error("Cannot resolve: operation is currently running.");this._value=e,this._done=!0}async do(e,t){return this._done?this.peek():this.promise?this._awaitWithTimeout(this.promise,t,"Once do() timed out"):(await this.mutex.lock(),this.promise?(this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")):(this.promise=(async()=>{try{this._value=await e(),this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.retry&&!this._done&&(this.promise=null)}return this.peek()})(),this.mutex.unlock(),this._awaitWithTimeout(this.promise,t,"Once do() timed out")))}doSync(e){if(this._done){if(this.throws&&this._error)throw this._error;return this.peek()}if(this.promise){const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}if(!this.mutex.tryLock()){const e=new Error("Cannot execute doSync: lock is currently held.");if(this.throws)throw e;return{value:null,error:e}}if(this.promise||this._done){if(this.mutex.unlock(),this._done){if(this.throws&&this._error)throw this._error;return this.peek()}const e=new Error("Cannot execute doSync while an async operation is pending.");if(this.throws)throw e;return{value:null,error:e}}try{const t=e();this._value=t,this._done=!0}catch(e){if(this._error=e,this.retry||(this._done=!0),this.throws)throw e}finally{this.mutex.unlock()}return this.peek()}running(){return null!==this.promise&&!this.done()}peek(){return{value:this._value,error:this._error}}get(){if(!this._done)throw new Error("Once operation is not yet complete");if(this._error)throw this._error;return this._value}reset(){if(this.running())throw new Error("Cannot reset Once while an operation is in progress.");this._done=!1,this.promise=null,this._value=null,this._error=void 0}done(){return this._done}current(){return this.promise}_awaitWithTimeout(e,t,s="Operation timed out"){if(null==t)return e;let i;return Promise.race([e.then(e=>(clearTimeout(i),e)),new Promise((e,n)=>{i=setTimeout(()=>n(new r(s)),t)})])}},o=class{mutex;_done=!1;_lastValue=null;_lastError=void 0;_hasRun=!1;constructor(e){this.mutex=new i({capacity:e?.capacity??1e3,yieldMode:e?.yieldMode??"macrotask"})}async do(e,t){if(this._done)return{value:null,error:new s};try{await this.mutex.lock(t)}catch(e){return{value:null,error:e}}let r,i=null;try{if(this._done)throw new s;i=await e(),this._lastValue=i,this._lastError=void 0,this._hasRun=!0}catch(e){r=e,this._lastError=e,this._hasRun=!0}finally{this.mutex.unlock()}return{value:i,error:r}}peek(){return{value:this._lastValue,error:this._lastError}}hasRun(){return this._hasRun}close(){this._done=!0}pending(){return this.mutex.pending()}running(){return this.mutex.locked()}},a=class{constructor(e,t,r={}){this.factory=e,this.onCleanup=t,this.options=r}_count=0;init=new n({retry:!1,throws:!1});pendingMicrotask=!1;cleanupTimer;get subscribers(){return this._count}async acquire(){this.cancelPendingCleanup(),this._count++;const e=await this.init.do(this.factory);if(e.error)throw e.error;return e.value}release(){this._count<=0?console.warn("SharedResource.release() called, but count is already 0."):(this._count--,0===this._count&&this.scheduleCleanup())}peek(){const e=this.init.peek();return e.error?null:e.value}forceCleanup(){this.cancelPendingCleanup(),this._count=0,this.executeCleanup()}cancelPendingCleanup(){this.pendingMicrotask=!1,void 0!==this.cleanupTimer&&(clearTimeout(this.cleanupTimer),this.cleanupTimer=void 0)}scheduleCleanup(){const e=this.options.gracePeriod??"microtask";if("sync"!==e)return"microtask"===e?(this.pendingMicrotask=!0,void queueMicrotask(()=>{!this.pendingMicrotask||this._count>0||(this.pendingMicrotask=!1,this.executeCleanup())})):void(this.cleanupTimer=setTimeout(()=>{this.cleanupTimer=void 0,this._count>0||this.executeCleanup()},e));this.executeCleanup()}executeCleanup(){const e=this.init.peek();try{this.onCleanup(e.value)}catch(e){console.error("[SharedResource] Error during cleanup callback:",e)}this.init.running()||this.init.reset()}};function c(e,t){if(!e.length)return;const r=e.slice(),s=t?`[${t}]`:"[ArtifactCleanup]";return async()=>{for(let e=r.length-1;e>=0;e--)try{await r[e]()}catch(t){console.error(`${s} Cleanup error at index ${e}:`,t)}}}async function h(e,t){const r=t?`[${t}]`:"[ArtifactCleanup]";for(let t=e.length-1;t>=0;t--)try{await e[t]()}catch(e){console.error(`${r} Cleanup error at index ${t}:`,e)}}function d(e){return`${e}__watched`}var u=Object.freeze({instance:void 0,error:void 0,ready:!1,cleanup:void 0,invalidate:async(e,t)=>{}});function l(e,t){let r;const s=new Promise((e,s)=>{r=setTimeout(()=>s(new Error(`Timeout: ${t}ms`)),t)});try{return Promise.race([e,s])}finally{clearTimeout(r)}}async function p(e){const t=(new TextEncoder).encode(e),r=await crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(r)).map(e=>e.toString(16).padStart(2,"0")).join("")}var f=class{cache=new Map;get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}delete(e){this.cache.delete(e)}has(e){return this.cache.has(e)}clear(){this.cache.clear()}size(){return this.cache.size}keys(){return Array.from(this.cache.keys())}package(e,t,r){const s=this.get(e);if(!s)return u;if(s.package)return s.package;const i=e,n=c(s.cleanupFunctions,i),o="singleton"!==s.scope||void 0!==s.instance,a={instance:s.instance,error:s.error,ready:o,[r||e]:s.instance,cleanup:n,invalidate:t};return s.package=a,a}invalidatePackage(e){const t=this.get(e);t&&(t.package=void 0)}async invalidateInstance(e,t=!1){const r=this.get(e);if(!r)return;const s=e;"singleton"===r.scope&&(r.stateUnsubscribe&&(r.stateUnsubscribe(),r.stateUnsubscribe=void 0),r.debounceTimer&&(clearTimeout(r.debounceTimer),r.debounceTimer=void 0),r.controller.abort(),await r.streamOnce.current(),r.streamSerializer.close(),r.stream=void 0,r.streamOnce=new n({retry:!0,throws:!0}),t||(r.streamSerializer=new o,r.controller=new AbortController)),await h(r.cleanupFunctions,s),await h(r.disposeFunctions,s),r.cleanupFunctions=[],r.disposeFunctions=[],"singleton"===r.scope&&(r.buildOnce=new n({retry:!0,throws:!0})),r.instance=void 0,r.error=void 0}},g=class{dependencies=new Map;dependents=new Map;registerNode(e){this.dependencies.has(e)||this.dependencies.set(e,new Set),this.dependents.has(e)||this.dependents.set(e,new Set)}removeNode(e){if(!this.hasNode(e))return;const t=this.dependencies.get(e);if(t)for(const r of t)this.dependents.get(r)?.delete(e);const r=this.dependents.get(e);if(r)for(const t of r)this.dependencies.get(t)?.delete(e);this.dependencies.delete(e),this.dependents.delete(e)}hasNode(e){return this.dependencies.has(e)}addDependency(e,t){this.registerNode(e),this.registerNode(t),this.dependencies.get(e).add(t),this.dependents.get(t).add(e)}removeDependency(e,t){this.dependencies.get(e)?.delete(t),this.dependents.get(t)?.delete(e)}getDependencies(e){const t=this.dependencies.get(e);return t?new Set(t):new Set}getDependents(e){const t=this.dependents.get(e);return t?new Set(t):new Set}iterDependents(e){return this.dependents.get(e)??y}setDependencies(e,t){this.registerNode(e);const r=this.dependencies.get(e),s=new Set(t);for(const t of r)s.has(t)||this.removeDependency(e,t);for(const t of s)r.has(t)||this.addDependency(e,t)}wouldCreateCycle(e,t){if(e===t)return[e,t];const r=[t],s=new Set([t]),i=new Map;for(;r.length>0;){const n=r.shift();if(n===e){const r=[];let s=e;for(;void 0!==s&&(r.push(s),s!==t);)s=i.get(s);return r.reverse(),r.unshift(e),r}const o=this.dependencies.get(n);if(o)for(const e of o)s.has(e)||(s.add(e),i.set(e,n),r.push(e))}return null}topologicalSort(){const e=new Map,t=Array.from(this.dependencies.keys());for(const r of t)e.set(r,this.dependencies.get(r)?.size??0);const r=[];for(const[t,s]of e)0===s&&r.push(t);const s=[];for(;r.length>0;){const t=r.shift();s.push(t);const i=this.dependents.get(t);if(i)for(const t of i){const s=(e.get(t)||0)-1;e.set(t,s),0===s&&r.push(t)}}if(s.length!==t.length)throw new Error("Cycle detected in graph; topological sort impossible.");return s}getTransitiveDependencies(e,t=!1){return this.bfs(e,"dependencies",t)}getTransitiveDependents(e,t=!1){return this.bfs(e,"dependents",t)}bfs(e,t,r){const s=new Set,i=new Set,n=[e];i.add(e),r&&s.add(e);const o="dependencies"===t?this.dependencies:this.dependents;for(;n.length>0;){const e=n.shift(),t=o.get(e);if(t)for(const e of t)i.has(e)||(i.add(e),s.add(e),n.push(e))}return s}getAllNodes(){return Array.from(this.dependencies.keys())}size(){return this.dependencies.size}clear(){this.dependencies.clear(),this.dependents.clear()}toDebugString(){return Array.from(this.dependencies.entries()).map(([e,t])=>{const r=Array.from(t).join(", ");return`${String(e)} → [${r||"∅"}]`}).join("\n")}},y=new Set,w=class{graph;constructor(){this.graph=new g}registerNode(e){this.graph.registerNode(e)}removeNode(e){this.graph.removeNode(e)}addDependency(e,t){this.graph.addDependency(e,t)}removeDependency(e,t){this.graph.removeDependency(e,t)}getDependents(e){return Array.from(this.graph.getDependents(e))}iterDependents(e){return this.graph.iterDependents(e)}getDependencies(e){return Array.from(this.graph.getDependencies(e))}getTransitiveDependents(e){return new Set(Array.from(this.graph.getTransitiveDependents(e,!1)))}setDependencies(e,t){const r=Array.from(t).map(e=>e);this.graph.setDependencies(e,r)}wouldCreateCycle(e,t,r){if(r?.has(t)){const e=Array.from(r),s=e.indexOf(t),i=e.slice(s);return i.push(t),i}const s=this.graph.wouldCreateCycle(e,t);return s||null}hasNode(e){return this.graph.hasNode(e)}getAllNodes(){return this.graph.getAllNodes()}clear(){this.graph.clear()}size(){return this.graph.size()}toDebugString(){return this.graph.toDebugString()}},m=["map","filter","reduce","forEach","find","findIndex","some","every","includes","flatMap","flat","slice","splice"];var v=class e extends Error{category;constructor(t,r,s){super(t,{cause:s}),this.name="ArtifactError",this.category=r,Object.setPrototypeOf(this,e.prototype)}},b=class extends v{constructor(e){super(`[ArtifactContainer] Artifact "${e}" not found.`,"system"),this.name="ArtifactNotFoundError"}},k=class extends v{constructor(){super("Build superseded by invalidation","system"),this.name="SupersededBuildError"}},S=class extends v{constructor(e){super(e,"system"),this.name="InvalidImportError"}},_=class extends v{constructor(e){super(`Artifact "${e}" instance is not JSON-serializable (POJO are required for persistence)`,"system"),this.name="InvalidExportOperationError"}},C=(new AbortController).signal,A=class{constructor(e,t,r,s,i){this.registry=e,this.cache=t,this.graph=r,this.store=s,this.observer=i}async build(e,t,r){const s=this.cache.get(e);if("singleton"===s?.scope&&s.buildOnce.done())return this.cache.package(e,(t,r)=>this.invalidate(e,t,r),r);const i=this.registry.getByString(e);if(!i)throw new b(e);const n=t??[];if(n.includes(e))throw new v(`Cycle detected: Artifact "${String(e)}" depends on itself via path: ${[...n,e].join(" -> ")}`,"system");n.push(e);let o=s;o||(o=this.createCachedArtifact(i),this.cache.set(e,o));try{if("transient"===o.scope)return this.executeBuild(i,o,n);const s=o;try{await s.buildOnce.do(()=>this.executeBuild(i,s,n))}catch(s){if(s instanceof k)return this.build(e,t,r);throw s}return s.stream&&s.streamOnce.do(s.stream),this.cache.package(e,(t,r)=>this.invalidate(e,t,r),r)}finally{n.pop()}}async executeBuild(e,t,r){const s=e.key,i=String(s),n="transient"===t.scope;t.buildCount++,"singleton"===t.scope&&(t.activeDebounceMs=e.debounce??0);const o={cleanupFunctions:[],disposeFunctions:[],capturedArtifactDeps:new Set,capturedStateSelectors:[],dependencyVersions:new Map},a=this.buildContext(e,t,r,i,o),h=await this.runWithRetries(e,a,o.dependencyVersions);if(this.commitResult(s,t,n,h,o),n){const e=h.ok?h.value:void 0;return{instance:e,error:h.ok?void 0:h.error,ready:h.ok,[s]:e,cleanup:c(o.cleanupFunctions,i),invalidate:async()=>console.warn(`[ArtifactManager] Cannot invalidate transient "${i}"`)}}}buildContext(e,t,r,s,i){const n=e.key,o="transient"===t.scope,{cleanupFunctions:a,disposeFunctions:c,capturedArtifactDeps:h,dependencyVersions:d,capturedStateSelectors:u}=i,l=async(e,t)=>{const i=t?this.computeParamKey(e,t):e;if(i===n)throw new v(`Artifact "${s}" depends on itself.`,"system");const o=this.graph.wouldCreateCycle(n,i);if(o)throw new v(`Adding dependency "${String(i)}" to "${s}" would create a cycle: ${o.join(" -> ")}`,"system");h.add(i);const a=await(t?this.resolveParameterized(e,t):this.build(e,r)),c=this.cache.get(i);return c&&d.set(i,c.version),a},p="singleton"===t.scope?t.controller.signal:C,f=async(e,t)=>{const r=await l(e,t);if(r.error)throw r.error;return r.instance},g=(e,t)=>{const r=function(e,t="."){const r=new Set,s=new Map,i=(e="")=>{if(s.has(e))return s.get(e);const n=new Proxy(()=>{},{get:(s,n)=>{if("symbol"==typeof n||"then"===n)return;if("valueOf"===n||"toString"===n)throw new Error("Cannot perform logic, arithmetic, or string operations inside a selector.");if(m.includes(n))throw new Error(`Array method .${n}() is not allowed in selectors.`);const o=e?`${e}${t}${n}`:n;return e&&r.delete(e),r.add(o),i(o)},has:()=>{throw new Error("The 'in' operator is not allowed in selectors.")},apply:()=>{throw new Error("Selectors cannot call functions or methods.")}});return s.set(e,n),n};try{e(i())}catch(e){throw new Error(`Selector failed during path analysis. Selectors must be simple property accessors only. Error: ${e instanceof Error?e.message:String(e)}`)}return Array.from(r)}(e);return u.push({paths:r,options:t}),e(this.store.get(!0))};return{state:()=>this.store.get(!0),previous:t.instance,signal:p,onCleanup:e=>a.push(e),onDispose:e=>c.push(e),use:e=>e({resolve:l,require:f,select:g}),stream:e=>{if(o)throw new v(`[ArtifactManager] Illegal stream on transient artifact "${s}"`,"system");const r=t,i=async(e,t=void 0)=>{await r.streamSerializer.do(async()=>{void 0!==r.stream&&(r.instance=e,r.error=t,r.version++,this.cache.invalidatePackage(n),await this.processStream(s))})},a={value:()=>r.instance,get signal(){return r.controller.signal},set:(...e)=>this.store.set(...e),emit:e=>i(e)};r.stream=async()=>{try{const t=await e(a);t&&r.cleanupFunctions.push(t)}catch(e){await i(void 0,e),await this.invalidate(n,!1,!0)}}}}}async runWithRetries(e,t,r){const s=t.signal,i=(e.retries??0)+1;let n=0;for(;n<i;)try{if(s.aborted)throw new k;const o=e.factory(t);let a;if(a=o instanceof Promise?e.timeout?await l(o,e.timeout):await o:o,s.aborted)throw new k;const c=this.detectStaleness(r);if(c){if(n++,r.clear(),n<i)continue;return{ok:!1,error:new v(`Build stale after all retries: dependency "${String(c)}" changed during build.`,"system")}}return{ok:!0,value:a}}catch(e){if(s.aborted||e instanceof k)throw new k;if(e instanceof v)throw e;if(n++,n>=i)return{ok:!1,error:e}}return{ok:!1,error:new Error("Build exhausted retry budget unexpectedly.")}}commitResult(e,t,r,s,i){const{cleanupFunctions:n,disposeFunctions:o,capturedArtifactDeps:a,capturedStateSelectors:c}=i;r||(this.updateDependencyGraph(e,a,c),t.cleanupFunctions=n,t.disposeFunctions=o,t.artifactDependencies=new Set(a),t.stateGroups=c.map(({paths:e,options:t})=>({paths:e,options:t}))),s.ok?(t.instance=s.value,t.error=void 0):(t.instance=void 0,t.error=s.error),t.version++,this.cache.invalidatePackage(e)}detectStaleness(e){for(const[t,r]of e){const e=this.cache.get(t);if(e&&e.version!==r)return t}return null}async invalidate(e,t=!1,r=!1){const s=this.cache.get(e);if(!s)return;if("singleton"!==s.scope)return this.executeInvalidation(e,t,r);const i=s;return i.debounceTimer&&(clearTimeout(i.debounceTimer),i.debounceTimer=void 0),!t&&i.activeDebounceMs>0?new Promise((s,n)=>{i.debounceTimer=setTimeout(()=>{i.debounceTimer=void 0,this.executeInvalidation(e,t,r).then(s).catch(n)},i.activeDebounceMs)}):this.executeInvalidation(e,t,r)}async executeInvalidation(e,t,r=!1){const s=this.cache.get(e);s&&"singleton"===s.scope&&await s.invalidationSerializer.do(async()=>{s.version++,await this.cache.invalidateInstance(e),await this.cascadeInvalidation(this.graph.iterDependents(e));const i=this.registry.get(e),n=i&&(t||!i.lazy||this.observer.hasWatchers(e))&&!r;n&&await this.build(e).catch(t=>{t instanceof k||console.error(`[ArtifactManager] Rebuild failed for "${String(e)}":`,t)}),(n||r)&&this.observer.notify(e)})}async dispose(e){this.cache.get(e)&&(await this.cache.invalidateInstance(e,!0),this.graph.removeNode(e),this.cache.delete(e))}async processStream(e){await this.cascadeInvalidation(this.graph.iterDependents(e)),this.observer.notify(e)}async cascadeInvalidation(e){if(0===e.size)return;const t=[];for(const r of e)t.push(this.invalidate(r).catch(e=>{console.error(`[ArtifactManager] Cascade failed for "${String(r)}":`,e)}));await Promise.all(t)}updateDependencyGraph(e,t,r){const s=this.cache.get(e);if(!s||"singleton"!==s.scope)return;this.graph.registerNode(e),this.graph.setDependencies(e,t),s.stateDependencies=new Set;for(const{paths:e}of r)for(const t of e)s.stateDependencies.add(t);if(s.stateUnsubscribe&&(s.stateUnsubscribe(),s.stateUnsubscribe=void 0),0===r.length)return;const i=()=>this.invalidate(e),n=new Map;for(const{paths:e,options:t}of r){const r=void 0===t?"undefined":JSON.stringify(t);let s=n.get(r);s||(s={options:t,paths:new Set},n.set(r,s));for(const t of e)s.paths.add(t)}const o=[];for(const{options:e,paths:t}of n.values()){if(0===t.size)continue;const r=Array.from(t);void 0===e?o.push(this.store.watch(r,i)):o.push(this.store.watch(r,i,e))}0===o.length?s.stateUnsubscribe=void 0:1===o.length?s.stateUnsubscribe=o[0]:s.stateUnsubscribe=()=>{for(const e of o)e()}}createCachedArtifact(e){return"transient"===e.scope?{scope:"transient",instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0}:{scope:"singleton",instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set,activeDebounceMs:e.debounce?e.debounce:0,controller:new AbortController,buildOnce:new n({retry:!0,throws:!0}),streamOnce:new n({retry:!0,throws:!0}),streamSerializer:new o({yieldMode:"microtask"}),invalidationSerializer:new o({yieldMode:"macrotask"}),stateGroups:[],artifactDependencies:new Set}}resolveStatic(e){if(!this.registry.has(e))throw new b(e);return this.build(e)}async resolveParameterized(e,t){if(!this.registry.has(e))throw new b(String(e));const r=this.registry.get(e);if(!r.paramKey)throw new v(`Artifact "${String(e)}" is not parameterized.`,"external");const s=r.paramKey(t),i=this.registry.getByString(s);if(i&&!i.paramKey&&!i.virtual)throw new v(`Parameterized artifact "${String(e)}" with params ${JSON.stringify(t)} resolves to key "${s}" which is already registered as a static artifact.`,"system");return this.registry.hasString(s)||this.registry.setVirtual(s,{key:s,factory:e=>r.factory({...e,params:t}),scope:r.scope,lazy:r.lazy,timeout:r.timeout,retries:r.retries,debounce:r.debounce,virtual:!0}),this.build(s,void 0,e)}computeParamKey(e,t){const r=this.registry.get(e);if(!r.paramKey)throw new Error(`Artifact "${String(e)}" is not parameterized.`);return r.paramKey(t)}},x=class{constructor(e,t,r){this.registry=e,this.cache=t,this.container=r}listeners=new Map;watchers=new Map;watch(e,t=6e4){return this.watchForKey(e,this.registry.get(e),t)}watchParameterized(e,t,r=6e4){const s=this.registry.get(e);if(!s.paramKey)throw new Error(`Artifact "${String(e)}" is not parameterized.`);const i=s.paramKey(t);return this.watchForKey(i,s,r,t)}watchForKey(e,t,r,s){const i="transient"===t.scope,n=i?d(e):e,o=this.watchers.get(n);if(o)return o.observer;const c=new a(async()=>{!i&&void 0===s||this.registry.hasString(e)||this.registry.setVirtual(e,{key:e,factory:i?t.factory:e=>t.factory({...e,params:s}),scope:i?"singleton":t.scope,lazy:t.lazy,timeout:t.timeout,retries:t.retries,debounce:t.debounce,paramKey:t.paramKey,virtual:!0})},async()=>{i||void 0!==s?(await this.registry.unregister(n).catch(()=>{}),this.cache.delete(n),this.watchers.delete(n)):this.cache.invalidatePackage(n),this.listeners.delete(n)},{gracePeriod:i&&void 0===s?"sync":r});let h;const l={id:n,get count(){return c.subscribers},get:(t=!1)=>0!==c.subscribers||t?this.cache.package(n,(e,t)=>this.container.invalidate(n,e,t),e):u,resolve:()=>h||(h=(async()=>{await c.acquire();try{return await this.container.resolve(n)}finally{c.release(),h=void 0}})(),h),subscribe:(e,t=!0)=>{const r=()=>e(l.get());return c.acquire().then(()=>{this.container.resolve(n).then(()=>{this.listeners.has(n)||this.listeners.set(n,new Set),t&&r(),this.listeners.get(n).add(r)})}).catch(e=>{console.error(`[ArtifactObserver] Resolution failed for "${n}":`,e),this.listeners.get(n)?.add(r)}),()=>{this.listeners.get(n)?.delete(r),c.release()}}};return this.watchers.set(n,{resource:c,observer:l}),l}evictWatcher(e){[e,d(e)].forEach(e=>{const t=this.watchers.get(e);t&&(t.resource.forceCleanup(),this.watchers.delete(e))})}notify(e){const t=this.listeners.get(e);if(t&&0!==t.size)for(const r of t)try{r()}catch(t){console.error(`[ArtifactObserver] Listener error for "${e}":`,t)}}hasWatchers(e){return this.watchers.has(e)||this.watchers.has(d(e))}getWatcherCount(e){const t=this.watchers.get(e)||this.watchers.get(d(e));return t?.resource.subscribers??0}clear(){this.watchers.clear(),this.listeners.clear()}},D=(e=>(e.Singleton="singleton",e.Transient="transient",e))(D||{}),O=class{artifacts=new Map;register({key:e,factory:t,lazy:r,...s}){const{scope:i,...n}=s,o={key:e,factory:t,scope:s.scope??"singleton",lazy:void 0===r||r,...n};return this.artifacts.set(e,o),()=>this.unregister(e)}setVirtual(e,t){this.artifacts.set(e,t)}get(e){if(!this.has(e))throw new b(String(e));return this.artifacts.get(e)}getByString(e){return this.artifacts.get(e)}has(e){return this.artifacts.has(e)}hasString(e){return this.artifacts.has(e)}async unregister(e){this.artifacts.has(e)&&this.artifacts.delete(e)}size(){return this.artifacts.size}keys(){return Array.from(this.artifacts.keys())}clear(){this.artifacts.clear()}},M=class e{registry;cache;graph;manager;observer;store;constructor(e){this.store={watch:(...t)=>e.watch(...t),get:()=>e.get(!0),set:(...t)=>e.set(...t),subset:(...t)=>e.subset(...t)},this.registry=new O,this.cache=new f,this.graph=new w,this.observer=new x(this.registry,this.cache,this),this.manager=new A(this.registry,this.cache,this.graph,this.store,this.observer)}debugInfo(){const e=[];return this.registry.keys().forEach(t=>{const r=t,s=this.registry.get(r),i=this.cache.get(r);if(!s)return;let n="idle";i&&("singleton"===i.scope&&void 0!==i.debounceTimer?n="debouncing":"singleton"===i.scope&&i.buildOnce.running()?n="building":i.error?n="error":void 0!==i.instance&&(n="active")),e.push({id:r,scope:s.scope??"singleton",status:n,dependencies:this.graph.getDependencies(r).map(e=>String(e)),dependents:this.graph.getDependents(r).map(e=>String(e)),stateDependencies:"singleton"===i?.scope?Array.from(i.stateDependencies):[],buildCount:i?.buildCount??0})}),e}register(e){const{key:t}=e,r=t;this.registry.has(t)&&(console.warn(`[ArtifactContainer] Overwriting "${r}".`),this.manager.dispose(t).catch(e=>{console.error(`[ArtifactContainer] Failed to dispose existing artifact "${r}":`,e)})),this.registry.register(e),this.graph.registerNode(t);const s=e.scope??"singleton";return(e.lazy??!0)||"singleton"!==s||this.resolve(t).catch(e=>{console.error(`[ArtifactContainer] Eager load failed for "${r}":`,e)}),()=>this.unregister(t)}has(e){return this.registry.has(e)}async unregister(e,t){const r=void 0!==t?this.manager.computeParamKey(e,t):e;await this.manager.dispose(r),await this.registry.unregister(r),this.observer.evictWatcher(r)}async resolve(e,t){return void 0===t?this.manager.resolveStatic(e):this.manager.resolveParameterized(e,t)}async require(e,t){const r=await this.resolve(e,t);if(r.error)throw r.error;return r.instance}watch(e,t,r){return void 0===t?this.observer.watch(e,r):this.observer.watchParameterized(e,t,r)}peek(e,t){const r=void 0!==t?this.manager.computeParamKey(e,t):e;return this.cache.get(r)?.instance}async invalidate(e,t){const r=t?.params,s=t?.replace??!1,i=void 0!==r?this.manager.computeParamKey(e,r):e;return this.manager.invalidate(i,s)}notifyObservers(e){this.observer.notify(e)}hasWatchers(e){return this.observer.hasWatchers(e)}async dispose(){const e=this.registry.keys();await Promise.allSettled(e.map(e=>this.manager.dispose(e).catch(t=>{console.error(`[ArtifactContainer] Failed to dispose artifact "${String(e)}":`,t)}))),this.registry.clear(),this.cache.clear(),this.graph.clear(),this.observer.clear()}async export(){const e=[];for(const t of this.cache.keys()){const r=this.cache.get(t);if(!r||"singleton"!==r.scope)continue;if(void 0===r.instance)continue;const s=new Set;for(const e of r.stateGroups)for(const t of e.paths)s.add(t);const i=Array.from(s).sort(),n=this.store.subset(i),o=await p(JSON.stringify(n,Object.keys(n).sort()));let a;try{a=structuredClone(r.instance)}catch(e){throw new _(t)}e.push({key:t,instance:a,state:{groups:r.stateGroups.map(e=>({paths:e.paths,options:e.options})),hash:o},dependencies:Array.from(r.artifactDependencies)})}const t={...{version:"1.0",timestamp:Date.now(),artifacts:e}},r=JSON.stringify(t),s=await p(r);return{...t,checksum:s}}async restore(e){const{checksum:t,...r}=e;if(await p(JSON.stringify(r))!==t)throw new S("Bundle checksum mismatch – data corrupted");if("1.0"!==e.version)throw new S(`Unsupported bundle version: ${e.version}`);const s=new Map;for(const t of e.artifacts)s.set(t.key,t);const i=new Set,a=new Map;for(const t of e.artifacts)for(const e of t.dependencies)a.has(e)||a.set(e,new Set),a.get(e).add(t.key);const c=async e=>{const t=new Set;for(const r of e.state.groups)for(const e of r.paths)t.add(e);const r=Array.from(t).sort(),s=this.store.subset(r);return p(JSON.stringify(s,Object.keys(s).sort()))};for(const t of e.artifacts){await c(t)!==t.state.hash&&i.add(t.key)}const h=Array.from(i);for(;h.length;){const e=h.shift(),t=a.get(e);if(t)for(const e of t)i.has(e)||(i.add(e),h.push(e))}for(const t of e.artifacts){if(i.has(t.key))continue;this.cache.get(t.key)&&await this.cache.invalidateInstance(t.key,!1);const e={scope:"singleton",instance:t.instance,error:void 0,version:1,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set(t.state.groups.flatMap(e=>e.paths)),activeDebounceMs:0,buildOnce:new n({retry:!0,throws:!0}),streamSerializer:new o({yieldMode:"microtask"}),invalidationSerializer:new o({yieldMode:"macrotask"}),stateUnsubscribe:void 0,debounceTimer:void 0,controller:new AbortController,streamOnce:new n({retry:!0,throws:!0}),stream:void 0,stateGroups:t.state.groups,artifactDependencies:new Set(t.dependencies)};e.buildOnce.resolve(t.instance),this.cache.set(t.key,e);const r=[];for(const e of t.state.groups){const s=this.store.watch(e.paths,()=>this.invalidate(t.key),e.options);r.push(s)}e.stateUnsubscribe=()=>{for(const e of r)e()},this.graph.setDependencies(t.key,t.dependencies)}}static async from(t){const r=new e(t.store);if(t.bundle){if("object"!=typeof t.bundle||!t.bundle.version||!Array.isArray(t.bundle.artifacts))throw new S("Invalid bundle: missing version or artifacts array");await r.restore(t.bundle)}for(const e of t.templates)r.register(e);return r}};export{M as ArtifactContainer,D as ArtifactScopes};
1
+ import{Once as e,Serializer as t,SharedResource as n}from"@asaidimu/utils-sync";import{buildPaths as r}from"@asaidimu/utils-store/selector";import{CommonErrors as i,SystemError as a,SystemError as o}from"@asaidimu/utils-error";import{createEventBus as s}from"@asaidimu/utils-events";import{ConsoleSink as c,Logger as l}from"@asaidimu/utils-logger";function u(e,t){if(!e.length)return;let n=e.slice(),r=t?`[${t}]`:`[ArtifactCleanup]`;return async()=>{for(let e=n.length-1;e>=0;e--)try{await n[e]()}catch(t){console.error(`${r} Cleanup error at index ${e}:`,t)}}}async function d(e,t){let n=t?`[${t}]`:`[ArtifactCleanup]`;for(let t=e.length-1;t>=0;t--)try{await e[t]()}catch(e){console.error(`${n} Cleanup error at index ${t}:`,e)}}function f(e){return`${e}__watched`}const p=Object.freeze({instance:void 0,error:void 0,ready:!1,cleanup:void 0,invalidate:async(e,t)=>{}});function m(e,t){let n,r=new Promise((e,r)=>{n=setTimeout(()=>r(Error(`Timeout: ${t}ms`)),t)});try{return Promise.race([e,r])}finally{clearTimeout(n)}}async function h(e){let t=new TextEncoder().encode(e),n=await crypto.subtle.digest(`SHA-256`,t);return Array.from(new Uint8Array(n)).map(e=>e.toString(16).padStart(2,`0`)).join(``)}var g=class{cache=new Map;get(e){return this.cache.get(e)}set(e,t){this.cache.set(e,t)}delete(e){this.cache.delete(e)}has(e){return this.cache.has(e)}clear(){this.cache.clear()}size(){return this.cache.size}keys(){return Array.from(this.cache.keys())}package(e,t,n){let r=this.get(e);if(!r)return p;if(r.package)return r.package;let i=e,a=u(r.cleanupFunctions,i),o=r.scope===`singleton`?r.instance!==void 0:!0,s={instance:r.instance,error:r.error,ready:o,[n||e]:r.instance,cleanup:a,invalidate:t};return r.package=s,s}invalidatePackage(e){let t=this.get(e);t&&(t.package=void 0)}async invalidateInstance(n,r=!1){let i=this.get(n);if(!i)return;let a=n;i.scope===`singleton`&&(i.stateUnsubscribe&&=(i.stateUnsubscribe(),void 0),i.debounceTimer&&=(clearTimeout(i.debounceTimer),void 0),i.controller.abort(),await i.streamOnce.current(),i.streamSerializer.close(),i.stream=void 0,i.streamOnce=new e({retry:!0,throws:!0}),r||(i.streamSerializer=new t,i.controller=new AbortController)),await d(i.cleanupFunctions,a),await d(i.disposeFunctions,a),i.cleanupFunctions=[],i.disposeFunctions=[],i.scope===`singleton`&&(i.buildOnce=new e({retry:!0,throws:!0})),i.instance=void 0,i.error=void 0}},_=class{dependencies=new Map;dependents=new Map;registerNode(e){this.dependencies.has(e)||this.dependencies.set(e,new Set),this.dependents.has(e)||this.dependents.set(e,new Set)}removeNode(e){if(!this.hasNode(e))return;let t=this.dependencies.get(e);if(t)for(let n of t)this.dependents.get(n)?.delete(e);let n=this.dependents.get(e);if(n)for(let t of n)this.dependencies.get(t)?.delete(e);this.dependencies.delete(e),this.dependents.delete(e)}hasNode(e){return this.dependencies.has(e)}addDependency(e,t){this.registerNode(e),this.registerNode(t),this.dependencies.get(e).add(t),this.dependents.get(t).add(e)}removeDependency(e,t){this.dependencies.get(e)?.delete(t),this.dependents.get(t)?.delete(e)}getDependencies(e){let t=this.dependencies.get(e);return t?new Set(t):new Set}getDependents(e){let t=this.dependents.get(e);return t?new Set(t):new Set}iterDependents(e){return this.dependents.get(e)??v}setDependencies(e,t){this.registerNode(e);let n=this.dependencies.get(e),r=new Set(t);for(let t of n)r.has(t)||this.removeDependency(e,t);for(let t of r)n.has(t)||this.addDependency(e,t)}wouldCreateCycle(e,t){if(e===t)return[e,t];let n=[t],r=new Set([t]),i=new Map;for(;n.length>0;){let a=n.shift();if(a===e){let n=[],r=e;for(;r!==void 0&&(n.push(r),r!==t);)r=i.get(r);return n.reverse(),n.unshift(e),n}let o=this.dependencies.get(a);if(o)for(let e of o)r.has(e)||(r.add(e),i.set(e,a),n.push(e))}return null}topologicalSort(){let e=new Map,t=Array.from(this.dependencies.keys());for(let n of t)e.set(n,this.dependencies.get(n)?.size??0);let n=[];for(let[t,r]of e)r===0&&n.push(t);let r=[];for(;n.length>0;){let t=n.shift();r.push(t);let i=this.dependents.get(t);if(i)for(let t of i){let r=(e.get(t)||0)-1;e.set(t,r),r===0&&n.push(t)}}if(r.length!==t.length)throw Error(`Cycle detected in graph; topological sort impossible.`);return r}getTransitiveDependencies(e,t=!1){return this.bfs(e,`dependencies`,t)}getTransitiveDependents(e,t=!1){return this.bfs(e,`dependents`,t)}bfs(e,t,n){let r=new Set,i=new Set,a=[e];i.add(e),n&&r.add(e);let o=t===`dependencies`?this.dependencies:this.dependents;for(;a.length>0;){let e=a.shift(),t=o.get(e);if(t)for(let e of t)i.has(e)||(i.add(e),r.add(e),a.push(e))}return r}getAllNodes(){return Array.from(this.dependencies.keys())}size(){return this.dependencies.size}clear(){this.dependencies.clear(),this.dependents.clear()}toDebugString(){return Array.from(this.dependencies.entries()).map(([e,t])=>{let n=Array.from(t).join(`, `);return`${String(e)} → [${n||`∅`}]`}).join(`
2
+ `)}};const v=new Set;var y=class{graph;constructor(){this.graph=new _}registerNode(e){this.graph.registerNode(e)}removeNode(e){this.graph.removeNode(e)}addDependency(e,t){this.graph.addDependency(e,t)}removeDependency(e,t){this.graph.removeDependency(e,t)}getDependents(e){return Array.from(this.graph.getDependents(e))}iterDependents(e){return this.graph.iterDependents(e)}getDependencies(e){return Array.from(this.graph.getDependencies(e))}getTransitiveDependents(e){return new Set(Array.from(this.graph.getTransitiveDependents(e,!1)))}setDependencies(e,t){let n=Array.from(t).map(e=>e);this.graph.setDependencies(e,n)}wouldCreateCycle(e,t,n){if(n?.has(t)){let e=Array.from(n),r=e.indexOf(t),i=e.slice(r);return i.push(t),i}return this.graph.wouldCreateCycle(e,t)||null}hasNode(e){return this.graph.hasNode(e)}getAllNodes(){return this.graph.getAllNodes()}clear(){this.graph.clear()}size(){return this.graph.size()}toDebugString(){return this.graph.toDebugString()}};const b={NOT_FOUND:i.NOT_FOUND,DUPLICATE_KEY:i.DUPLICATE_KEY,INVALID_COMMAND:i.INVALID_COMMAND,INTERNAL_ERROR:i.INTERNAL_ERROR,CONCURRENCY_ERROR:i.CONCURRENCY_ERROR},x=`[ArtifactContainer]`;function S(e){return new o({code:b.NOT_FOUND,message:`${x} Artifact "${e}" not found.`,operation:`artifact:resolve`})}function C(e){return new o({code:b.INTERNAL_ERROR,message:`${x} Cycle detected: ${e.join(` -> `)}`,operation:`artifact:resolve`})}function w(e){return new o({code:b.INVALID_COMMAND,message:`${x} ${e}`,operation:`artifact:build`})}function T(e){return new o({code:b.INTERNAL_ERROR,message:`${x} Artifact with key:${e} has already been disposed`,operation:`artifact:watch`})}function E(e){return new o({code:b.CONCURRENCY_ERROR,message:`${x} Operation timed out: ${e}`,operation:`artifact:build`})}function D(e){return new o({code:b.DUPLICATE_KEY,message:`${x} An artifact with key:"${e}" already exists!`,operation:`artifact:register`})}function O(e){return new o({code:b.INVALID_COMMAND,message:`${x} Artifact "${e}" is not parameterized.`,operation:`artifact:resolve`})}function k(e,t){return new o({code:b.DUPLICATE_KEY,message:`${x} Parameterized artifact "${e}" with params resolves to key "${t}" which is already registered as a static artifact.`,operation:`artifact:resolve`})}function A(e){return new o({code:b.INTERNAL_ERROR,message:`${x} Artifact "${e}" depends on itself.`,operation:`artifact:build`})}function j(e){return new o({code:b.CONCURRENCY_ERROR,message:`${x} Build stale after all retries: dependency "${String(e)}" changed during build.`,operation:`artifact:build`})}function M(e){return new o({code:b.INVALID_COMMAND,message:e,operation:`artifact:import`})}function N(e){return new o({code:b.INTERNAL_ERROR,message:`${x} Artifact "${e}" instance is not JSON-serializable (POJO are required for persistence)`,operation:`artifact:export`})}var P=class extends Error{constructor(){super(`Build superseded by invalidation`),this.name=`SupersededBuildError`}};const F=new AbortController().signal;var I=class{registry;cache;graph;store;observer;logger;events;constructor(e,t,n,r,i,a,o){this.registry=e,this.cache=t,this.graph=n,this.store=r,this.observer=i,this.logger=a,this.events=o}async build(e,t,n){let r=this.cache.get(e);if(r?.scope===`singleton`&&r.buildOnce.done())return this.cache.package(e,(t,n)=>this.invalidate(e,t,n),n);let i=this.registry.getByString(e);if(!i)throw S(e);let a=t??[];if(a.includes(e))throw C([...a,e].map(String));a.push(e);let o=r;o||(o=this.createCachedArtifact(i),this.cache.set(e,o)),this.events?.emit({name:`build:start`,payload:{key:e,templateKey:n}});try{if(o.scope===`transient`)return this.executeBuild(i,o,a);let r=o;try{await r.buildOnce.do(()=>this.executeBuild(i,r,a))}catch(r){if(r instanceof P)return this.build(e,t,n);throw r}return r.stream&&r.streamOnce.do(r.stream),this.events?.emit({name:`build:complete`,payload:{key:e,instance:o.instance}}),this.cache.package(e,(t,n)=>this.invalidate(e,t,n),n)}catch(t){throw t instanceof P||this.events?.emit({name:`build:error`,payload:{key:e,error:t}}),t}finally{a.pop()}}async executeBuild(e,t,n){let r=e.key,i=String(r),a=t.scope===`transient`;t.buildCount++,t.scope===`singleton`&&(t.activeDebounceMs=e.debounce??0);let o={cleanupFunctions:[],disposeFunctions:[],capturedArtifactDeps:new Set,capturedStateSelectors:[],dependencyVersions:new Map},s=this.buildContext(e,t,n,i,o),c=await this.runWithRetries(e,s,o.dependencyVersions);if(this.commitResult(r,t,a,c,o),a){let e=c.ok?c.value:void 0;return{instance:e,error:c.ok?void 0:c.error,ready:c.ok,[r]:e,cleanup:u(o.cleanupFunctions,i),invalidate:async()=>this.logger.warn(`Cannot invalidate transient "${i}"`)}}}buildContext(e,t,n,i,a){let o=e.key,s=t.scope===`transient`,{cleanupFunctions:c,disposeFunctions:l,capturedArtifactDeps:u,dependencyVersions:d,capturedStateSelectors:f}=a,p=async(e,t)=>{let r=t?this.computeParamKey(e,t):e;if(r===o)throw A(i);let a=this.graph.wouldCreateCycle(o,r);if(a)throw C(a);u.add(r);let s=await(t?this.resolveParameterized(e,t):this.build(e,n)),c=this.cache.get(r);return c&&d.set(r,c.version),s},m=t.scope===`singleton`?t.controller.signal:F,h=async(e,t)=>{let n=await p(e,t);if(n.error)throw n.error;return n.instance},g=(e,t)=>{let n=r(e);return f.push({paths:n,options:t}),e(this.store.get(!0))};return{state:()=>this.store.get(!0),previous:t.instance,signal:m,onCleanup:e=>c.push(e),onDispose:e=>l.push(e),use:e=>e({resolve:p,require:h,select:g}),stream:e=>{if(s)throw w(`Illegal stream on transient artifact "${i}"`);let n=t,r=async(e,t=void 0)=>{await n.streamSerializer.do(async()=>{n.stream!==void 0&&(n.instance=e,n.error=t,n.version++,this.cache.invalidatePackage(o),await this.processStream(i),this.events?.emit({name:`stream:emit`,payload:{key:i,value:e}}))})},a={value:()=>n.instance,get signal(){return n.controller.signal},set:(...e)=>this.store.set(...e),emit:e=>r(e)};n.stream=async()=>{try{let t=await e(a);t&&n.cleanupFunctions.push(t)}catch(e){await r(void 0,e),await this.invalidate(o,!1,!0)}}}}}async runWithRetries(e,t,n){let r=t.signal,i=(e.retries??0)+1,o=0;for(;o<i;)try{if(r.aborted)throw new P;let a=e.factory(t),s;if(s=a instanceof Promise?e.timeout?await m(a,e.timeout):await a:a,r.aborted)throw new P;let c=this.detectStaleness(n);if(c){if(o++,n.clear(),o<i)continue;return{ok:!1,error:j(String(c))}}return{ok:!0,value:s}}catch(e){if(r.aborted||e instanceof P)throw new P;if(e instanceof a)throw e;if(o++,o>=i)return{ok:!1,error:e}}return{ok:!1,error:Error(`Build exhausted retry budget unexpectedly.`)}}commitResult(e,t,n,r,i){let{cleanupFunctions:a,disposeFunctions:o,capturedArtifactDeps:s,capturedStateSelectors:c}=i;n||(this.updateDependencyGraph(e,s,c),t.cleanupFunctions=a,t.disposeFunctions=o,t.artifactDependencies=new Set(s),t.stateGroups=c.map(({paths:e,options:t})=>({paths:e,options:t}))),r.ok?(t.instance=r.value,t.error=void 0):(t.instance=void 0,t.error=r.error),t.version++,this.cache.invalidatePackage(e)}detectStaleness(e){for(let[t,n]of e){let e=this.cache.get(t);if(e&&e.version!==n)return t}return null}async invalidate(e,t=!1,n=!1){let r=this.cache.get(e);if(!r)return;if(r.scope!==`singleton`)return this.executeInvalidation(e,t,n);let i=r;return i.debounceTimer&&=(clearTimeout(i.debounceTimer),void 0),!t&&i.activeDebounceMs>0?new Promise((r,a)=>{i.debounceTimer=setTimeout(()=>{i.debounceTimer=void 0,this.executeInvalidation(e,t,n).then(r).catch(a)},i.activeDebounceMs)}):this.executeInvalidation(e,t,n)}async executeInvalidation(e,t,n=!1){let r=this.cache.get(e);!r||r.scope!==`singleton`||await r.invalidationSerializer.do(async()=>{r.version++,await this.cache.invalidateInstance(e);let i=this.graph.iterDependents(e),a=i.size>0;await this.cascadeInvalidation(i),this.events?.emit({name:`artifact:invalidated`,payload:{key:e,cascade:a,replace:t}});let o=this.registry.get(e),s=o&&(t||!o.lazy||this.observer.hasWatchers(e))&&!n;s&&await this.build(e).catch(t=>{t instanceof P||this.logger.error(`Rebuild failed for "${String(e)}"`,t)}),(s||n)&&this.observer.notify(e)})}async dispose(e){this.cache.get(e)&&(await this.cache.invalidateInstance(e,!0),this.graph.removeNode(e),this.cache.delete(e),this.events?.emit({name:`artifact:disposed`,payload:{key:e}}))}async processStream(e){await this.cascadeInvalidation(this.graph.iterDependents(e)),this.observer.notify(e)}async cascadeInvalidation(e){if(e.size===0)return;let t=[];for(let n of e)t.push(this.invalidate(n).catch(e=>{this.logger.error(`Cascade failed for "${String(n)}"`,e)}));await Promise.all(t)}updateDependencyGraph(e,t,n){let r=this.cache.get(e);if(!r||r.scope!==`singleton`)return;this.graph.registerNode(e),this.graph.setDependencies(e,t),r.stateDependencies=new Set;for(let{paths:e}of n)for(let t of e)r.stateDependencies.add(t);if(r.stateUnsubscribe&&=(r.stateUnsubscribe(),void 0),n.length===0)return;let i=()=>this.invalidate(e),a=new Map;for(let{paths:e,options:t}of n){let n=t===void 0?`undefined`:JSON.stringify(t),r=a.get(n);r||(r={options:t,paths:new Set},a.set(n,r));for(let t of e)r.paths.add(t)}let o=[];for(let{options:e,paths:t}of a.values()){if(t.size===0)continue;let n=Array.from(t);e===void 0?o.push(this.store.watch(n,i)):o.push(this.store.watch(n,i,e))}o.length===0?r.stateUnsubscribe=void 0:o.length===1?r.stateUnsubscribe=o[0]:r.stateUnsubscribe=()=>{for(let e of o)e()}}createCachedArtifact(n){return n.scope===`transient`?{scope:`transient`,instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0}:{scope:`singleton`,instance:void 0,error:void 0,version:0,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set,activeDebounceMs:n.debounce?n.debounce:0,controller:new AbortController,buildOnce:new e({retry:!0,throws:!0}),streamOnce:new e({retry:!0,throws:!0}),streamSerializer:new t({yieldMode:`microtask`}),invalidationSerializer:new t({yieldMode:`macrotask`}),stateGroups:[],artifactDependencies:new Set}}resolveStatic(e){if(!this.registry.getByString(e))throw S(e);return this.build(e)}async resolveParameterized(e,t){let n=this.registry.getByString(e);if(!n)throw S(String(e));if(!n.paramKey)throw O(String(e));let r=n.paramKey(t),i=this.registry.getByString(r);if(i&&!i.paramKey&&!i.virtual)throw k(String(e),r);return this.registry.hasString(r)||this.registry.setVirtual(r,{key:r,factory:e=>n.factory({...e,params:t}),scope:n.scope,lazy:n.lazy,timeout:n.timeout,retries:n.retries,debounce:n.debounce,virtual:!0}),this.build(r,void 0,e)}computeParamKey(e,t){let n=this.registry.getByString(e);if(!n||!n.paramKey)throw O(String(e));return n.paramKey(t)}},L=class{registry;cache;container;logger;listeners=new Map;watchers=new Map;constructor(e,t,n,r){this.registry=e,this.cache=t,this.container=n,this.logger=r}watch(e,t=60*1e3){return this.watchForKey(e,this.registry.get(e),t)}watchParameterized(e,t,n=60*1e3){let r=this.registry.get(e);if(!r.paramKey)throw Error(`Artifact "${String(e)}" is not parameterized.`);let i=r.paramKey(t);return this.watchForKey(i,r,n,t)}watchForKey(e,t,r,i){let a=t.scope===`transient`,o=a?f(e):e,s=this.watchers.get(o);if(s)return s.observer;let c=new n(async()=>{(a||i!==void 0)&&!this.registry.hasString(e)&&this.registry.setVirtual(e,{key:e,factory:a?t.factory:e=>t.factory({...e,params:i}),scope:a?`singleton`:t.scope,lazy:t.lazy,timeout:t.timeout,retries:t.retries,debounce:t.debounce,paramKey:t.paramKey,virtual:!0})},async()=>{a||i!==void 0?(await this.registry.unregister(o).catch(()=>{}),this.cache.delete(o),this.watchers.delete(o)):this.cache.invalidatePackage(o),this.listeners.delete(o)},{gracePeriod:a&&i===void 0?`sync`:r}),l,u={id:o,get count(){return c.subscribers},get:(t=!1)=>c.subscribers===0&&!t?p:this.cache.package(o,(e,t)=>this.container.invalidate(o,e,t),e),resolve:()=>l||(l=(async()=>{await c.acquire();try{return await this.container.resolve(o)}finally{c.release(),l=void 0}})(),l),subscribe:(e,t=!0)=>{let n=()=>e(u.get());return c.acquire().then(()=>{this.container.resolve(o).then(()=>{this.listeners.has(o)||this.listeners.set(o,new Set),t&&n(),this.listeners.get(o).add(n)})}).catch(e=>{this.logger.error(`Resolution failed for "${o}"`,e),this.listeners.get(o)?.add(n)}),()=>{this.listeners.get(o)?.delete(n),c.release()}}};return this.watchers.set(o,{resource:c,observer:u}),u}evictWatcher(e){[e,f(e)].forEach(e=>{let t=this.watchers.get(e);t&&(t.resource.forceCleanup(),this.watchers.delete(e))})}notify(e){let t=this.listeners.get(e);if(!(!t||t.size===0))for(let n of t)try{n()}catch(t){this.logger.error(`Listener error for "${e}"`,t)}}hasWatchers(e){return this.watchers.has(e)||this.watchers.has(f(e))}getWatcherCount(e){return(this.watchers.get(e)||this.watchers.get(f(e)))?.resource.subscribers??0}clear(){this.watchers.clear(),this.listeners.clear()}};let R=function(e){return e.Singleton=`singleton`,e.Transient=`transient`,e}({});var z=class{artifacts=new Map;register({key:e,factory:t,lazy:n,...r}){let{scope:i,...a}=r,o={key:e,factory:t,scope:r.scope??`singleton`,lazy:n===void 0?!0:n,...a};return this.artifacts.set(e,o),()=>this.unregister(e)}setVirtual(e,t){this.artifacts.set(e,t)}get(e){if(!this.has(e))throw S(String(e));return this.artifacts.get(e)}getByString(e){return this.artifacts.get(e)}has(e){return this.artifacts.has(e)}hasString(e){return this.artifacts.has(e)}async unregister(e){this.artifacts.has(e)&&this.artifacts.delete(e)}size(){return this.artifacts.size}keys(){return Array.from(this.artifacts.keys())}clear(){this.artifacts.clear()}},B=class n{registry;cache;graph;manager;observer;logger;store;events;constructor(e,t){this.logger=t?.logger??new l([new c],{module:`artifacts`}),this.store={watch:(...t)=>e.watch(...t),get:()=>e.get(!0),set:(...t)=>e.set(...t),subset:(...t)=>e.subset(...t)},this.events=s({errorHandler:e=>this.logger.error(`Lifecycle event error`,e)}),this.registry=new z,this.cache=new g,this.graph=new y,this.observer=new L(this.registry,this.cache,this,this.logger),this.manager=new I(this.registry,this.cache,this.graph,this.store,this.observer,this.logger,this.events)}debugInfo(){let e=[];return this.registry.keys().forEach(t=>{let n=t,r=this.registry.getByString(n),i=this.cache.get(n);if(!r)return;let a=`idle`;i&&(i.scope===`singleton`&&i.debounceTimer!==void 0?a=`debouncing`:i.scope===`singleton`&&i.buildOnce.running()?a=`building`:i.error?a=`error`:i.instance!==void 0&&(a=`active`)),e.push({id:n,scope:r.scope??`singleton`,status:a,dependencies:this.graph.getDependencies(n).map(e=>String(e)),dependents:this.graph.getDependents(n).map(e=>String(e)),stateDependencies:i?.scope===`singleton`?Array.from(i.stateDependencies):[],buildCount:i?.buildCount??0})}),e}register(e){let{key:t}=e,n=t;this.registry.has(t)&&(this.logger.warn(`Overwriting artifact "${n}".`),this.manager.dispose(t).catch(e=>{this.logger.error(`Failed to dispose existing artifact "${n}"`,e)})),this.registry.register(e),this.graph.registerNode(t);let r=e.scope??`singleton`,i=e.lazy??!0;return this.events.emit({name:`artifact:registered`,payload:{key:n,scope:r}}),!i&&r===`singleton`&&this.resolve(t).catch(e=>{this.logger.error(`Eager load failed for "${n}"`,e)}),()=>this.unregister(t)}has(e){return this.registry.has(e)}async unregister(e,t){let n=t===void 0?e:this.manager.computeParamKey(e,t);await this.manager.dispose(n),await this.registry.unregister(n),this.observer.evictWatcher(n)}async resolve(e,t){return t===void 0?this.manager.resolveStatic(e):this.manager.resolveParameterized(e,t)}async require(e,t){let n=await this.resolve(e,t);if(n.error)throw n.error;return n.instance}watch(e,t,n){return t===void 0?this.observer.watch(e,n):this.observer.watchParameterized(e,t,n)}peek(e,t){let n=t===void 0?e:this.manager.computeParamKey(e,t);return this.cache.get(n)?.instance}async invalidate(e,t){let n=t?.params,r=t?.replace??!1,i=n===void 0?e:this.manager.computeParamKey(e,n);return this.manager.invalidate(i,r)}on(e,t){return this.events.subscribe(e,t)}once(e,t){return this.events.once(e,t)}notifyObservers(e){this.observer.notify(e)}hasWatchers(e){return this.observer.hasWatchers(e)}async dispose(){let e=this.registry.keys();await Promise.allSettled(e.map(e=>this.manager.dispose(e).catch(t=>{this.logger.error(`Failed to dispose artifact "${String(e)}"`,t)}))),this.registry.clear(),this.cache.clear(),this.graph.clear(),this.observer.clear(),this.events.emit({name:`container:dispose`,payload:{}}),this.events.clear({permanent:!0})}async export(){let e=[];for(let t of this.cache.keys()){let n=this.cache.get(t);if(!n||n.scope!==`singleton`||n.instance===void 0)continue;let r=new Set;for(let e of n.stateGroups)for(let t of e.paths)r.add(t);let i=Array.from(r).sort(),a=this.store.subset(i),o=await h(JSON.stringify(a,Object.keys(a).sort())),s;try{s=structuredClone(n.instance)}catch{throw N(t)}e.push({key:t,instance:s,state:{groups:n.stateGroups.map(e=>({paths:e.paths,options:e.options})),hash:o},dependencies:Array.from(n.artifactDependencies)})}let t={version:`1.0`,timestamp:Date.now(),artifacts:e},n=await h(JSON.stringify(t));return{...t,checksum:n}}async restore(n){let{checksum:r,...i}=n;if(await h(JSON.stringify(i))!==r)throw M(`Bundle checksum mismatch – data corrupted`);if(n.version!==`1.0`)throw M(`Unsupported bundle version: ${n.version}`);let a=new Map;for(let e of n.artifacts)a.set(e.key,e);let o=new Set,s=new Map;for(let e of n.artifacts)for(let t of e.dependencies)s.has(t)||s.set(t,new Set),s.get(t).add(e.key);let c=async e=>{let t=new Set;for(let n of e.state.groups)for(let e of n.paths)t.add(e);let n=Array.from(t).sort(),r=this.store.subset(n);return h(JSON.stringify(r,Object.keys(r).sort()))};for(let e of n.artifacts)await c(e)!==e.state.hash&&o.add(e.key);let l=Array.from(o);for(;l.length;){let e=l.shift(),t=s.get(e);if(t)for(let e of t)o.has(e)||(o.add(e),l.push(e))}for(let r of n.artifacts){if(o.has(r.key))continue;this.cache.get(r.key)&&await this.cache.invalidateInstance(r.key,!1);let n={scope:`singleton`,instance:r.instance,error:void 0,version:1,cleanupFunctions:[],disposeFunctions:[],buildCount:0,stateDependencies:new Set(r.state.groups.flatMap(e=>e.paths)),activeDebounceMs:0,buildOnce:new e({retry:!0,throws:!0}),streamSerializer:new t({yieldMode:`microtask`}),invalidationSerializer:new t({yieldMode:`macrotask`}),stateUnsubscribe:void 0,debounceTimer:void 0,controller:new AbortController,streamOnce:new e({retry:!0,throws:!0}),stream:void 0,stateGroups:r.state.groups,artifactDependencies:new Set(r.dependencies)};n.buildOnce.resolve(r.instance),this.cache.set(r.key,n);let i=[];for(let e of r.state.groups){let t=this.store.watch(e.paths,()=>this.invalidate(r.key),e.options);i.push(t)}n.stateUnsubscribe=()=>{for(let e of i)e()},this.graph.setDependencies(r.key,r.dependencies)}}static async from(e){let t=new n(e.store,{logger:e.logger});if(e.bundle){if(typeof e.bundle!=`object`||!e.bundle.version||!Array.isArray(e.bundle.artifacts))throw M(`Invalid bundle: missing version or artifacts array`);await t.restore(e.bundle)}for(let n of e.templates)t.register(n);return t}};export{B as ArtifactContainer,R as ArtifactScopes,b as ErrorCodes,P as SupersededBuildError,a as SystemError,S as artifactNotFound,j as buildStaleAfterRetries,C as cycleDetected,w as illegalScope,N as invalidExport,M as invalidImport,D as keyConflict,O as notParameterized,k as paramKeyCollision,A as selfDependency,E as timeoutError,T as watcherDisposed};
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@asaidimu/utils-artifacts",
3
- "version": "8.2.11",
3
+ "version": "8.2.13",
4
4
  "description": "Reactive artifact container.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
7
7
  "types": "index.d.ts",
8
8
  "dependencies": {
9
- "@asaidimu/utils-events": "^1.0.0"
9
+ "@asaidimu/utils-events": "^1.0.0",
10
+ "@asaidimu/utils-error": "^1.0.0",
11
+ "@asaidimu/utils-logger": "^1.0.4",
12
+ "@asaidimu/utils-sync": "^1.0.0",
13
+ "@asaidimu/utils-store": "^10.2.5"
10
14
  },
11
15
  "keywords": [
12
16
  "typescript",
@@ -61,5 +65,6 @@
61
65
  }
62
66
  ]
63
67
  ]
64
- }
68
+ },
69
+ "peerDependencies": {}
65
70
  }