@fnlb-project/shared 1.5.68 → 1.5.69

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.
@@ -1 +1 @@
1
- import{createHash as m}from"node:crypto";import{mkdir as u,readFile as p,writeFile as f}from"node:fs/promises";import{resolve as w}from"node:path";class d{lockPromise;constructor(){this.lockPromise=void 0}get isLocked(){return!!this.lockPromise}wait(){return this.lockPromise?.promise||Promise.resolve()}lock(){let e,a=new Promise((n)=>{e=n});this.lockPromise={promise:a,resolve:e}}unlock(){this.lockPromise?.resolve(),this.lockPromise=void 0}}class h{static wait(e){return new Promise((a)=>setTimeout(a,e))}}class y{storageDir;targetFileName;displayName;releaseUrl;releaseProvider;maxDownloadRetries;maxBackoffMs;initialDelayMs;staleMs;log;success;warn;error;isLoaded=!1;lastLoadedTime=0;lock=new d;constructor(e){this.storageDir=e.storageDir,this.targetFileName=e.targetFileName,this.displayName=e.displayName??e.targetFileName,this.releaseUrl=e.releaseUrl,this.releaseProvider=e.releaseProvider,this.maxDownloadRetries=e.maxDownloadRetries??1/0,this.maxBackoffMs=e.maxBackoffMs??60000,this.initialDelayMs=e.initialDelayMs??1000,this.staleMs=e.staleMs??0,this.log=e.log,this.success=e.success,this.warn=e.warn,this.error=e.error}async ensureUpToDate(e){await this.lock.wait(),this.lock.lock();try{let a=this.getFilePath(),n=e||!this.isLoaded;if(this.isLoaded&&!e&&this.staleMs>0){let t=Date.now()-this.lastLoadedTime;if(t>=this.staleMs)n=!0,this.log?.(`${this.displayName} is stale (${Math.round(t/1000)}s old), checking for updates...`)}if(!n)return a;let l=await p(a).catch(()=>null),i,r=0,o=this.initialDelayMs;this.log?.(l?"Checking for updates...":`Downloading ${this.displayName}...`);while(r<this.maxDownloadRetries)try{i=await this.fetchReleaseInfo();break}catch(t){let s=Math.min(o*2,this.maxBackoffMs);if(r++,this.error?.("Update error:",t),this.warn?.(`Check for updates attempt ${r} failed: ${t.message}. Retrying in ${s>=60000?`${~~(s/60000)}m`:`${~~(s/1000)}s`}...`),r>=this.maxDownloadRetries)break;await new Promise((c)=>setTimeout(c,o)),o=s}if(!i){if(l)return this.warn?.("Failed to check for updates. Using existing local version."),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Loaded existing ${this.displayName} version`),a;throw new Error("[AutoUpdater] Failed to check for updates and no local file found.")}if(l){if(m("sha256").update(l).digest("hex")===i.hash)return this.success?.(`${this.displayName} v${i.version} is up to date`),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Finished loading ${this.displayName} v${i.version}`),a;this.log?.(`Downloading update for ${this.displayName} v${i.version}`)}r=0,o=this.initialDelayMs;while(r<this.maxDownloadRetries)try{let t=await fetch(i.url);if(!t.ok)throw new Error(`Download failed with status ${t.status}`);let s=await t.text();if(m("sha256").update(s).digest("hex")!==i.hash)throw new Error("Downloaded file hash mismatch...");return await u(this.storageDir,{recursive:!0}).catch(()=>{throw new Error(`Failed to create the target directory on ${this.storageDir}`)}),await f(a,s),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Finished loading ${this.displayName} v${i.version}`),a}catch(t){let s=Math.min(o*2,this.maxBackoffMs);r++,this.error?.("Download error:",t),this.warn?.(`Download attempt ${r} failed: ${t.message}. Retrying in ${s>=60000?`${~~(s/60000)}m`:`${~~(s/1000)}s`}...`),await h.wait(o),o=s}if(l)return this.warn?.("Max retries reached. Using existing local version."),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Loaded existing ${this.displayName} version`),a;throw new Error(`[AutoUpdater] Failed to download and verify update after ${r} attempts`)}finally{this.lock.unlock()}}getFilePath(){return w(this.storageDir,this.targetFileName)}async fetchReleaseInfo(){if(this.releaseProvider)return this.releaseProvider();if(!this.releaseUrl)throw new Error("No release provider configured");let e=await fetch(this.releaseUrl);if(!e.ok)throw new Error(`Status code: ${e.status}`);return await e.json()}}export{y as AutoUpdater};
1
+ import{createHash as m}from"node:crypto";import{mkdir as u,readFile as f,writeFile as p}from"node:fs/promises";import{resolve as w}from"node:path";class d{lockPromise;constructor(){this.lockPromise=void 0}get isLocked(){return!!this.lockPromise}wait(){return this.lockPromise?.promise||Promise.resolve()}lock(){let e,s=new Promise((n)=>{e=n});this.lockPromise={promise:s,resolve:e}}unlock(){this.lockPromise?.resolve(),this.lockPromise=void 0}}class h{static wait(e){return new Promise((s)=>setTimeout(s,e))}}class y{storageDir;targetFileName;displayName;releaseUrl;releaseProvider;maxDownloadRetries;maxBackoffMs;initialDelayMs;staleMs;log;success;warn;error;isLoaded=!1;lastLoadedTime=0;lock=new d;constructor(e){this.storageDir=e.storageDir,this.targetFileName=e.targetFileName,this.displayName=e.displayName??e.targetFileName,this.releaseUrl=e.releaseUrl,this.releaseProvider=e.releaseProvider,this.maxDownloadRetries=e.maxDownloadRetries??1/0,this.maxBackoffMs=e.maxBackoffMs??60000,this.initialDelayMs=e.initialDelayMs??1000,this.staleMs=e.staleMs??0,this.log=e.log,this.success=e.success,this.warn=e.warn,this.error=e.error}async ensureUpToDate(e){await this.lock.wait(),this.lock.lock();try{let s=this.getFilePath(),n=e||!this.isLoaded;if(this.isLoaded&&!e&&this.staleMs>0){let t=Date.now()-this.lastLoadedTime;if(t>=this.staleMs)n=!0,this.log?.(`${this.displayName} is stale (${Math.round(t/1000)}s old), checking for updates...`)}if(!n)return s;let l=await f(s).catch(()=>null),i,r=0,o=this.initialDelayMs;this.log?.(l?"Checking for updates...":`Downloading ${this.displayName}...`);while(r<this.maxDownloadRetries)try{i=await this.fetchReleaseInfo();break}catch(t){let a=Math.min(o*2,this.maxBackoffMs);if(r++,this.error?.("Update error:",t),this.warn?.(`Check for updates attempt ${r} failed: ${t.message}. Retrying in ${a>=60000?`${~~(a/60000)}m`:`${~~(a/1000)}s`}...`),r>=this.maxDownloadRetries)break;await new Promise((c)=>setTimeout(c,o)),o=a}if(!i){if(l)return this.warn?.("Failed to check for updates. Using existing local version."),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Loaded existing ${this.displayName} version`),s;throw new Error("[AutoUpdater] Failed to check for updates and no local file found.")}if(l){if(m("sha256").update(l).digest("hex")===i.hash)return this.success?.(`${this.displayName} v${i.version} is up to date`),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Finished loading ${this.displayName} v${i.version}`),s;this.log?.(`Downloading update for ${this.displayName} v${i.version}`)}r=0,o=this.initialDelayMs;while(r<this.maxDownloadRetries)try{let t=await fetch(i.url);if(!t.ok)throw new Error(`Download failed with status ${t.status}`);let a=Buffer.from(await t.arrayBuffer());if(m("sha256").update(a).digest("hex")!==i.hash)throw new Error("Downloaded file hash mismatch...");return await u(this.storageDir,{recursive:!0}).catch(()=>{throw new Error(`Failed to create the target directory on ${this.storageDir}`)}),await p(s,a),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Finished loading ${this.displayName} v${i.version}`),s}catch(t){let a=Math.min(o*2,this.maxBackoffMs);r++,this.error?.("Download error:",t),this.warn?.(`Download attempt ${r} failed: ${t.message}. Retrying in ${a>=60000?`${~~(a/60000)}m`:`${~~(a/1000)}s`}...`),await h.wait(o),o=a}if(l)return this.warn?.("Max retries reached. Using existing local version."),this.isLoaded=!0,this.lastLoadedTime=Date.now(),this.success?.(`Loaded existing ${this.displayName} version`),s;throw new Error(`[AutoUpdater] Failed to download and verify update after ${r} attempts`)}finally{this.lock.unlock()}}getFilePath(){return w(this.storageDir,this.targetFileName)}async fetchReleaseInfo(){if(this.releaseProvider)return this.releaseProvider();if(!this.releaseUrl)throw new Error("No release provider configured");let e=await fetch(this.releaseUrl);if(!e.ok)throw new Error(`Status code: ${e.status}`);return await e.json()}}export{y as AutoUpdater};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fnlb-project/shared",
3
- "version": "1.5.68",
3
+ "version": "1.5.69",
4
4
  "description": "FNLB Shared Library",
5
5
  "files": ["dist"],
6
6
  "author": "FNLB-Project",