@cmtlyt/string-zip
Version:
2 lines (1 loc) • 1.8 kB
JavaScript
import{createWorkerFunc as f,getRandomString as p,cacheByReturn as $,caniuse as o,stringToStream as b,streamToBase64String as w,base64StringToStream as R,streamToString as k}from"@cmtlyt/base";function g(e,n){let r,i=0;const c=36**n;do r=p(n);while(e.includes(r)&&++i<c);return r}function a(e){return"#"}function y(e,n,r="#",i={}){const c=new RegExp(`(^{.*?(.{${n}})})|({.*?(.{${n}})}$)`),s=r.length+2;for(let t=0;t++<e.length;){const S=e.slice(t,t+n+s);if(e.includes(S,t+n+s))for(let u=t+n+s+1;u++<e.length;){const T=e.slice(t,u);if(!e.includes(T,u)){const x=g(Object.keys(i),n),h=e.slice(t,u-1);if(c.test(h))break;i[x]=h,e=e.replace(new RegExp(h.replace(/([[{(?)\\])/g,"\\$1"),"g"),`{${r}${x}}`),t=t+9;break}}}return e}function l(e,n=6){const r={},i=y(e,n,a(),r);return JSON.stringify({cache:r,source:i,keyLength:n})}const{run:E}=f(l,[y,a,g,p],{reuse:!1});function d(e,n="#"){const{keyLength:r,cache:i}=e;let{source:c}=e;const s=new RegExp(`{${n}(.{${r}})}`,"g");let t=s.exec(c);for(;t;){const[S,u]=Array.from(t);c=c.replace(new RegExp(S,"g"),i[u]),t=s.exec(c)}return c}function m(e){const n=JSON.parse(e);return d(n,a())}const{run:A}=f(m,[d,a,g,p],{reuse:!1});function B(e){return f(l,[y,a,g,p],{...e,needPost:!1}).run}function D(e){return f(m,[d,a,g,p],{...e,needPost:!1}).run}const z=$(()=>o("TextEncoder")&&o("ReadableStream")&&o("ArrayBuffer")&&o("Uint8Array")&&o("btoa")&&o("atob")&&o("TextDecoder"));async function O(e,n=6){if(!o("CompressionStream")||!z())return l(e,n);const r=b(e).pipeThrough(new CompressionStream("gzip"));return w(r)}async function C(e){if(!o("DecompressionStream")||!z())return m(e);const n=R(e).pipeThrough(new DecompressionStream("gzip"));return k(n)}export{D as createUnzip,B as createZip,O as gzip,C as unGzip,A as unzip,m as unzipSync,E as zip,l as zipSync};