@coreframe/assets 0.1.5 → 0.1.7
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/dist/storage.js +1 -1
- package/package.json +3 -3
package/dist/storage.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{fs as e,os as t,process as n}from"./environment.js";import{createHash as r,randomUUID as i}from"node:crypto";import{dirname as a,join as o,resolve as s}from"node:path";import{createB2PrivateObjectStore as c}from"@coreframe/object-storage";function l(e){return x(e),`sources/v1/sha256/${e.slice(0,2)}/${e}`}function u({config:e,
|
|
1
|
+
import{fs as e,os as t,process as n}from"./environment.js";import{createHash as r,randomUUID as i}from"node:crypto";import{dirname as a,join as o,resolve as s}from"node:path";import{createB2PrivateObjectStore as c}from"@coreframe/object-storage";function l(e){return x(e),`sources/v1/sha256/${e.slice(0,2)}/${e}`}function u({config:e,env:t=n.env}){return c({config:{credentialProfile:e.credentialProfile,endpoint:e.endpoint,region:e.region},env:t})}function d(e=n.env){return e.COREFRAME_ASSETS_CACHE_DIR?s(e.COREFRAME_ASSETS_CACHE_DIR):t.platform()===`darwin`?o(t.homedir(),`Library`,`Caches`,`coreframe`,`assets`):t.platform()===`win32`?o(e.LOCALAPPDATA??o(t.homedir(),`AppData`,`Local`),`Coreframe`,`assets`):o(e.XDG_CACHE_HOME??o(t.homedir(),`.cache`),`coreframe`,`assets`)}function f(e,t){return x(t),o(e,`v1`,`sha256`,t.slice(0,2),t)}async function p({cacheRoot:t,digest:r,env:o=n.env,source:s}){if(await C(s)!==r)throw Error(`Asset source does not match declared SHA-256 digest ${r}.`);let c=f(t,r);return await h(c,async()=>{if(await g(c,r)){await _(c);return}await e.mkdir(a(c),{recursive:!0});let t=`${c}.${n.pid}.${i()}.tmp`;try{if(await e.copyFile(s,t),await C(t)!==r)throw Error(`Asset source changed while it was being cached.`);await e.rename(t,c)}finally{await e.rm(t,{force:!0})}}),await v(t,b(o),c),c}async function m({bucket:t,cacheRoot:r,digest:o,env:s=n.env,store:c}){let u=f(r,o);if(await g(u,o))return await _(u),{fetched:!1,path:u};let d=!1;return await h(u,async()=>{if(await g(u,o)){await _(u);return}await e.mkdir(a(u),{recursive:!0});let r=`${u}.${n.pid}.${i()}.tmp`;try{if(await c.download(t,l(o),r),await C(r)!==o)throw Error(`Asset source does not match declared SHA-256 digest ${o}.`);await e.rename(r,u),d=!0}finally{await e.rm(r,{force:!0})}}),await v(r,b(s),u),{fetched:d,path:u}}async function h(t,n){await e.mkdir(a(t),{recursive:!0});let r=`${t}.lock`,i=Date.now();for(;;){let t;try{t=await e.open(r,`wx`)}catch(t){if(t.code!==`EEXIST`)throw t;let n=await e.stat(r).catch(()=>void 0);if(n&&Date.now()-n.mtimeMs>12e4){await e.rm(r,{force:!0});continue}if(Date.now()-i>=3e4)throw Error(`Timed out waiting for the asset cache lock at ${r}.`);await new Promise(e=>setTimeout(e,50));continue}try{await n()}finally{await t.close(),await e.rm(r,{force:!0})}return}}async function g(e,t){try{return await C(e)===t}catch{return!1}}async function _(t){let n=new Date;await e.utimes(t,n,n)}async function v(t,n,r){let i=await y(t),a=i.reduce((e,t)=>e+t.size,0);for(let t of i.sort((e,t)=>e.mtimeMs-t.mtimeMs)){if(a<=n)break;t.path!==r&&(await e.rm(t.path,{force:!0}),a-=t.size)}}async function y(t){let n=[],r=async t=>{let i=await e.readdir(t,{withFileTypes:!0}).catch(()=>[]);for(let a of i){let i=o(t,a.name);if(a.isDirectory())await r(i);else if(!a.name.endsWith(`.lock`)&&!a.name.endsWith(`.tmp`)){let t=await e.stat(i);n.push({path:i,size:t.size,mtimeMs:t.mtimeMs})}}};return await r(t),n}function b(e){let t=Number(e.COREFRAME_ASSETS_CACHE_MAX_BYTES);return Number.isSafeInteger(t)&&t>0?t:5368709120}function x(e){if(!/^[0-9a-f]{64}$/.test(e))throw Error(`Invalid SHA-256 asset digest: ${e}`)}function S(e){return r(`sha256`).update(e).digest(`hex`)}async function C(t){let n=r(`sha256`);for await(let r of e.createReadStream(t))n.update(r);return n.digest(`hex`)}export{f as assetCacheFile,d as assetCacheRoot,l as assetSourceKey,p as cacheAssetSourceFile,u as createB2AssetStore,m as materializeAssetSource,S as sha256,C as sha256File};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coreframe/assets",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Asset processing and object-storage workflows for Coreframe apps",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"music-metadata": "^11.14.0",
|
|
23
23
|
"nano-spawn": "^2.1.0",
|
|
24
24
|
"saxes": "^6.0.0",
|
|
25
|
-
"@coreframe/config": "^0.1.
|
|
26
|
-
"@coreframe/object-storage": "^0.1.
|
|
25
|
+
"@coreframe/config": "^0.1.4",
|
|
26
|
+
"@coreframe/object-storage": "^0.1.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^26.1.0",
|