@gjsify/tar 0.3.19 → 0.3.21
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/lib/esm/extract.js +1 -1
- package/lib/esm/parser.js +1 -1
- package/package.json +3 -3
package/lib/esm/extract.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseTar as e}from"./parser.js";import*as t from"node:fs";import*as n from"node:path";async function
|
|
1
|
+
import{parseTar as e}from"./parser.js";import*as t from"node:fs";import*as n from"node:path";async function extractTarball(r,i,a={}){let o=r instanceof Uint8Array?r:new Uint8Array(r),s=e(a.gzip??(o.length>=2&&o[0]===31&&o[1]===139)?await gunzip(o):o);t.mkdirSync(i,{recursive:!0});let c=a.strip??1,l=a.preventEscape??!0,u={files:[],directories:[],symlinks:[],skipped:0};for(let e of s){let r=stripComponents(e.name,c);if(r===null||r===``){u.skipped++;continue}let o=n.resolve(i,r);if(l&&!isInside(o,i))throw Error(`tar: refusing to extract ${e.name} outside ${i} (resolved=${o})`);if(a.filter&&!a.filter(e,o)){u.skipped++;continue}if(e.type===`directory`){t.mkdirSync(o,{recursive:!0}),u.directories.push(o);continue}if(e.type===`file`){t.mkdirSync(n.dirname(o),{recursive:!0}),t.writeFileSync(o,e.body);let r=a.chmod?.(e,o)??e.mode&511;if(r>0)try{t.chmodSync(o,r)}catch{}u.files.push(o);continue}if(e.type===`symlink`){t.mkdirSync(n.dirname(o),{recursive:!0});try{t.symlinkSync(e.linkname,o),u.symlinks.push(o)}catch{t.writeFileSync(o,e.linkname),u.files.push(o)}continue}u.skipped++}return u}async function gunzip(e){let t=globalThis.DecompressionStream;if(typeof t!=`function`)throw Error(`@gjsify/tar: globalThis.DecompressionStream is not available — import '@gjsify/compression-streams/register' on GJS to register it`);let n=new Blob([new Uint8Array(e)]).stream().pipeThrough(new t(`gzip`)),r=[],i=0,a=n.getReader();for(;;){let{value:e,done:t}=await a.read();if(t)break;let n=e instanceof Uint8Array?e:new Uint8Array(e);r.push(n),i+=n.length}let o=new Uint8Array(i),s=0;for(let e of r)o.set(e,s),s+=e.length;return o}function stripComponents(e,t){if(t<=0)return e;let n=e.split(`/`).filter(e=>e!==``);return n.length<=t?null:n.slice(t).join(`/`)}function isInside(e,t){let r=n.relative(t,e);return r!==``&&!r.startsWith(`..`)&&!n.isAbsolute(r)}export{extractTarball,gunzip};
|
package/lib/esm/parser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=512;function
|
|
1
|
+
const e=512;function parseTar(e){let t=[],n=null,r=null,i=null,a=0;for(;a+512<=e.length;){let o=e.subarray(a,a+512);if(allZeros(o)){let t=e.subarray(a+512,a+2*512);if(t.length===512&&allZeros(t))break;a+=512;continue}if(!validateChecksum(o))throw new TarParseError(`Bad header checksum at offset ${a} — file is not a valid tar archive`);let s=readString(o,0,100),c=parseOctal(o,100,8),l=parseOctal(o,124,12),u=parseOctal(o,136,12),d=String.fromCharCode(o[156]||0),f=readString(o,157,100),p=readString(o,257,6),m=readString(o,345,155),h=readString(o,265,32),g=readString(o,297,32);a+=512;let _=e.subarray(a,a+l);if(a+=alignToBlock(l),d===`x`){n=parsePaxRecords(_);continue}if(d===`g`)continue;if(d===`L`){r=bytesToString(_).replace(/\0+$/,``);continue}if(d===`K`){i=bytesToString(_).replace(/\0+$/,``);continue}let v=s;p===`ustar`&&m!==``&&(v=`${m}/${s}`),r!==null&&(v=r,r=null);let y=f;if(i!==null&&(y=i,i=null),n!==null){let r=n.get(`path`);r!==void 0&&(v=r);let i=n.get(`linkpath`);i!==void 0&&(y=i);let o=n.get(`size`);if(o!==void 0){let r=Number(o);if(Number.isFinite(r)){let i=a-alignToBlock(l),o=e.subarray(i,i+r);a=i+alignToBlock(r),n=null,t.push(buildEntry(v,y,d,c,u,h,g,o));continue}}n=null}t.push(buildEntry(v,y,d,c,u,h,g,_))}return t}function buildEntry(e,t,n,r,i,a,o,s){return{name:e,linkname:t,type:typeflagToType(n,e),mode:r,mtime:i,body:s,uname:a,gname:o}}function typeflagToType(e,t){switch(e){case`0`:case`\0`:case``:return t.endsWith(`/`)?`directory`:`file`;case`1`:return`hardlink`;case`2`:return`symlink`;case`5`:return`directory`;case`x`:return`pax-header`;case`g`:return`pax-global`;case`L`:return`gnu-longname`;case`K`:return`gnu-longlink`;default:return`unknown`}}function parsePaxRecords(e){let t=new Map,n=0;for(;n<e.length;){let r=n;for(;r<e.length&&e[r]!==32;)r++;if(r>=e.length)break;let i=bytesToString(e.subarray(n,r)),a=Number(i);if(!Number.isFinite(a)||a<=0)break;let o=n+a;if(o>e.length)break;let s=bytesToString(e.subarray(r+1,o-1)),c=s.indexOf(`=`);if(c>0){let e=s.slice(0,c),n=s.slice(c+1);t.set(e,n)}n=o}return t}function readString(e,t,n){let r=t,i=t+n;for(;r<i&&e[r]!==0;)r++;return bytesToString(e.subarray(t,r))}function bytesToString(e){return new TextDecoder(`utf-8`,{fatal:!1}).decode(e)}function parseOctal(e,t,n){if(n>0&&e[t]&128){let r=e[t]&127;for(let i=1;i<n;i++)r=r*256+e[t+i];return r}let r=``;for(let i=0;i<n;i++){let n=e[t+i];n===0||n===32||(r+=String.fromCharCode(n))}return r===``?0:parseInt(r,8)}function alignToBlock(e){return Math.ceil(e/512)*512}function allZeros(e){for(let t=0;t<e.length;t++)if(e[t]!==0)return!1;return!0}function validateChecksum(e){let t=parseOctal(e,148,8),n=0,r=0;for(let t=0;t<512;t++){let i=t>=148&&t<156?32:e[t];n+=i,r+=i>127?i-256:i}return t===n||t===r}var TarParseError=class extends Error{constructor(e){super(e),this.name=`TarParseError`}};export{e as BLOCK_SIZE,TarParseError,parseTar};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/tar",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
4
4
|
"description": "Streaming .tar / .tar.gz reader for the gjsify install backend (Node + GJS)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
],
|
|
35
35
|
"license": "MIT",
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@gjsify/cli": "^0.3.
|
|
38
|
-
"@gjsify/unit": "^0.3.
|
|
37
|
+
"@gjsify/cli": "^0.3.21",
|
|
38
|
+
"@gjsify/unit": "^0.3.21",
|
|
39
39
|
"typescript": "^6.0.3"
|
|
40
40
|
}
|
|
41
41
|
}
|