UNPKG

gs-data

Version:

> A foundational library for reading and writing network or file data on the browser side<br/> > For local file read/write operations, [gs-web-fs](https://www.npmjs.com/package/gs-web-fs), built on top of this library, offers enhanced ease of use<br/> >

2 lines (1 loc) 18.5 kB
"use strict";var e,t,a,i=require("gs-base"),o=require("js-base64");let n;exports.FileMimeTypes=void 0,(a=exports.FileMimeTypes||(exports.FileMimeTypes={})).TextPlain="text/plain",a.TextUTF8="text/plain;charset=UTF-8",a.TextUTF16BE="text/plain;charset=UTF-16BE",a.TextUTF16LE="text/plain;charset=UTF-16LE",a.HTML="text/html",a.CSS="text/css",a.JavaScript="text/javascript",a.XML="text/xml",a.JSON="application/json",a.CSV="text/csv",a.Markdown="text/markdown",a.LaTeX="application/x-latex",a.TSV="text/tab-separated-values",a.Bat="text/x-sh",a.SimpleData="text/x-simpledata",a.JPEG="image/jpeg",a.PNG="image/png",a.GIF="image/gif",a.SVG="image/svg+xml",a.WebP="image/webp",a.BMP="image/bmp",a.TIFF="image/tiff",a.HEIF="image/heif",a.HEIC="image/heic",a.ICO="image/x-icon",a.JPEG2000="image/jp2",a.AdobeXD="application/vnd.adobe.xd",a.PSD="image/vnd.adobe.photoshop",a.MP3="audio/mpeg",a.OGG="audio/ogg",a.WAV="audio/wav",a.AAC="audio/aac",a.FLAC="audio/flac",a.M4A="audio/x-m4a",a.AMR="audio/amr",a.WMA="audio/x-ms-wma",a.MP4="video/mp4",a.MPEG="video/mpeg",a.MP2T="video/MP2T",a.MPEGPS="video/MP1S",a.WebM="video/webm",a.OggVideo="video/ogg",a.QuickTime="video/quicktime",a.AVI="video/x-msvideo",a.MKV="video/x-matroska",a.MOV="video/quicktime",a.FLV="video/x-flv",a.WMV="video/x-ms-wmv",a.PDF="application/pdf",a.EXE="application/vnd.microsoft.portable-executable",a.MSI="application/x-msdownload",a.APK="application/vnd.android.package-archive",a.JAR="application/java-archive",a.DMG="application/x-apple-diskimage",a.ISO="application/x-iso9660-image",a.TTF="font/ttf",a.WOFF="font/woff",a.WOFF2="font/woff2",a.OTF="font/otf",a.EOT="application/vnd.ms-fontobject",a.SVGFont="font/svg+xml",a.DOC="application/msword",a.DOCX="application/vnd.openxmlformats-officedocument.wordprocessingml.document",a.XLS="application/vnd.ms-excel",a.XLSX="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",a.PPT="application/vnd.ms-powerpoint",a.PPTX="application/vnd.openxmlformats-officedocument.presentationml.presentation",a.ODT="application/vnd.oasis.opendocument.text",a.ODS="application/vnd.oasis.opendocument.spreadsheet",a.ODP="application/vnd.oasis.opendocument.presentation",a.RTF="application/rtf",a.Base64="application/base64",a.P7M="application/pkcs7-mime",a.P12="application/x-pkcs12",a.COM="application/x-msdos-program",a.DLL="application/x-msdownload",a.SYS="application/x-mswinexe",a.OLE="application/x-oleobject",a.OctetStream="application/octet-stream",a.UnknownBinary="application/x-unknown",a.Binary="application/x-binary",a.ZIP="application/zip",a.RAR="application/vnd.rar",a.GZIP="application/gzip",a.TAR="application/x-tar",a.BZIP2="application/x-bzip2",a.SEVEN_ZIP="application/x-7z-compressed",a.XZ="application/x-xz",a.GZ="application/x-gzip",a.TAR_GZ="application/gzip",a.LZ="application/x-lzip",a.LZH="application/x-lzh",a.Fbr="application/zip+fbr",a.M3U="audio/x-mpegurl",a.M3U8="application/x-mpegurl",a.PLS="audio/x-scpls",a.XSPF="application/xspf+xml",a.DPL="application/x-daumplaylist",exports.NetMimeTypes=void 0,(t=exports.NetMimeTypes||(exports.NetMimeTypes={})).MIME="message/rfc822",t.SMTP="application/smtp",t.HTTP="application/http",t.HTTPS="application/https",t.FTP="application/ftp",t.TCP="application/tcp",t.UDP="application/udp",t.JSONLD="application/ld+json",t.WebAssembly="application/wasm",t.SVGZ="image/svg+xml+gzip",t.MHTML="multipart/related",t.OGG="application/ogg",t.XAP="application/x-silverlight-app",t.WOFF="font/woff",t.WASM="application/wasm",t.WGT="application/widget",t.OctetStream="application/octet-stream",t.UnknownBinary="application/x-unknown",exports.MimeTypes=void 0,(e=exports.MimeTypes||(exports.MimeTypes={})).SimpleData="text/x-simpledata",e.Fbr="application/zip+fbr",e.PSD="image/vnd.adobe.photoshop",e.Bat="text/x-sh",e.TextPlain="text/plain",e.TextUTF8="text/plain;charset=UTF-8",e.TextUTF16LE="text/plain;charset=UTF-16LE",e.TextUTF16BE="text/plain;charset=UTF-16BE",e.HTML="text/html",e.CSS="text/css",e.JavaScript="text/javascript",e.XML="text/xml",e.JSON="application/json",e.CSV="text/csv",e.Markdown="text/markdown",e.RTF="application/rtf",e.LaTeX="application/x-latex",e.TSV="text/tab-separated-values",e.JPEG="image/jpeg",e.PNG="image/png",e.GIF="image/gif",e.SVG="image/svg+xml",e.WebP="image/webp",e.BMP="image/bmp",e.TIFF="image/tiff",e.HEIF="image/heif",e.HEIC="image/heic",e.ICO="image/x-icon",e.JPEG2000="image/jp2",e.MP3="audio/mpeg",e.OGG="audio/ogg",e.WAV="audio/wav",e.AAC="audio/aac",e.FLAC="audio/flac",e.M4A="audio/x-m4a",e.AMR="audio/amr",e.WMA="audio/x-ms-wma",e.MP4="video/mp4",e.MPEG="video/mpeg",e.MP2T="video/MP2T",e.MPEGPS="video/MP1S",e.WebM="video/webm",e.OggVideo="video/ogg",e.QuickTime="video/quicktime",e.AVI="video/x-msvideo",e.MKV="video/x-matroska",e.MOV="video/quicktime",e.FLV="video/x-flv",e.WMV="video/x-ms-wmv",e.PDF="application/pdf",e.ZIP="application/zip",e.RAR="application/vnd.rar",e.SEVEN_ZIP="application/x-7z-compressed",e.GZIP="application/gzip",e.TAR="application/x-tar",e.EXE="application/vnd.microsoft.portable-executable",e.MSI="application/x-msdownload",e.APK="application/vnd.android.package-archive",e.JAR="application/java-archive",e.DMG="application/x-apple-diskimage",e.ISO="application/x-iso9660-image",e.TTF="font/ttf",e.WOFF="font/woff",e.WOFF2="font/woff2",e.OTF="font/otf",e.EOT="application/vnd.ms-fontobject",e.SVGFont="font/svg+xml",e.DOC="application/msword",e.DOCX="application/vnd.openxmlformats-officedocument.wordprocessingml.document",e.AdobeXD="application/vnd.adobe.xd",e.XLS="application/vnd.ms-excel",e.XLSX="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",e.PPT="application/vnd.ms-powerpoint",e.PPTX="application/vnd.openxmlformats-officedocument.presentationml.presentation",e.ODT="application/vnd.oasis.opendocument.text",e.ODS="application/vnd.oasis.opendocument.spreadsheet",e.ODP="application/vnd.oasis.opendocument.presentation",e.Base64="application/base64",e.P7M="application/pkcs7-mime",e.P12="application/x-pkcs12",e.XZ="application/x-xz",e.BZIP2="application/x-bzip2",e.GZ="application/x-gzip",e.TAR_GZ="application/gzip",e.LZ="application/x-lzip",e.LZH="application/x-lzh",e.COM="application/x-msdos-program",e.DLL="application/x-msdownload",e.SYS="application/x-mswinexe",e.OLE="application/x-oleobject",e.Binary="application/x-binary",e.MIME="message/rfc822",e.SMTP="application/smtp",e.HTTP="application/http",e.HTTPS="application/https",e.FTP="application/ftp",e.TCP="application/tcp",e.UDP="application/udp",e.JSONLD="application/ld+json",e.WebAssembly="application/wasm",e.SVGZ="image/svg+xml+gzip",e.MHTML="multipart/related",e.XAP="application/x-silverlight-app",e.WASM="application/wasm",e.WGT="application/widget",e.OctetStream="application/octet-stream",e.UnknownBinary="application/x-unknown",e.M3U="audio/x-mpegurl",e.M3U8="application/x-mpegurl",e.PLS="audio/x-scpls",e.XSPF="application/xspf+xml",e.DPL="application/x-daumplaylist";const p=()=>n||(n={"text/x-simpledata":"txt","text/plain":"txt","text/html":"html","text/css":"css","text/javascript":"js","text/xml":"xml","application/json":"json","text/csv":"csv","text/markdown":"md","application/x-latex":"tex","text/tab-separated-values":"tsv","text/x-sh":"bat","image/jpeg":"jpg","image/png":"png","image/gif":"gif","image/svg+xml":"svg","image/webp":"webp","image/bmp":"bmp","image/tiff":"tiff","image/heif":"heif","image/heic":"heic","image/x-icon":"ico","image/jp2":"jp2","audio/mpeg":"mp3","audio/ogg":"ogg","audio/wav":"wav","audio/aac":"aac","audio/flac":"flac","audio/x-m4a":"m4a","audio/amr":"amr","audio/x-ms-wma":"wma","video/mp4":"mp4","video/webm":"webm","video/ogg":"ogg","video/quicktime":"mov","video/x-msvideo":"avi","video/x-matroska":"mkv","video/x-flv":"flv","video/x-ms-wmv":"wmv","application/pdf":"pdf","application/zip":"zip","application/vnd.rar":"rar","application/gzip":"gzip","application/x-tar":"tar","application/vnd.microsoft.portable-executable":"exe","application/x-msdownload":"msi","application/vnd.android.package-archive":"apk","application/java-archive":"jar","application/x-apple-diskimage":"dmg","application/x-iso9660-image":"iso","application/zip+fbr":"fbr","font/ttf":"ttf","font/woff":"woff","font/woff2":"woff2","font/otf":"otf","application/vnd.ms-fontobject":"eot","font/svg+xml":"svgfont","application/msword":"doc","application/vnd.openxmlformats-officedocument.wordprocessingml.document":"docx","application/vnd.ms-excel":"xls","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":"xlsx","application/vnd.ms-powerpoint":"ppt","application/vnd.openxmlformats-officedocument.presentationml.presentation":"pptx","application/vnd.oasis.opendocument.text":"odt","application/vnd.oasis.opendocument.spreadsheet":"ods","application/vnd.oasis.opendocument.presentation":"odp","application/octet-stream":"exe","application/x-unknown":"unknown","audio/x-mpegurl":"m3u","application/x-mpegurl":"m3u8","audio/x-scpls":"pls","application/xspf+xml":"xspf","application/x-daumplaylist":"dpl"});let s;const c=/^\.?(\w+)(?:\/([0-9a-z-+.]+)\s*(?:;.*charset\s*=\s*([0-9a-z-]+).*)?)?$/i;function r(e){const t=c.exec(e);let a,o;return t?.[2]?(a=`${t[1]}/${t[2]}`,o=p()?.[a]||"unknown"):t?.[1]&&(o=t[1],a=(s||(s=i.invertRecordKv(p())),s)[o]||"application/x-unknown"),t?.[3]&&(a=`${a}; charset=${t[3]}`),{ext:o,mime:a}}let l;async function m(e,t,a=0){let i;return i=e instanceof Blob?new Uint8Array(await e.slice(a,t).arrayBuffer()):e instanceof ArrayBuffer?new Uint8Array(e.slice(a,t)):e.slice(a,t),i}async function d(e,t,a=0){return(new TextDecoder).decode(await m(e,t,a))}async function x(e){const t=await async function(e,t,a=0){return Array.from(await m(e,t,a)).map((e=>e.toString(16).padStart(2,"0"))).join("").toUpperCase()}(e,8);for(const[e,a]of l||(l=[["EFBBBF","text/plain;charset=UTF-8"],["FEFF","text/plain;charset=UTF-16BE"],["FFFE","text/plain;charset=UTF-16LE"],["FFD8FF","image/jpeg"],["89504E47","image/png"],["47494638","image/gif"],["49492A00","image/tiff"],["4D4D002A","image/tiff"],["424D","image/bmp"],["00000100","image/x-icon"],["38425053","image/vnd.adobe.photoshop"],["52494646","image/webp"],["57454250","image/webp"],["494433","audio/mpeg"],["4F676753","audio/ogg"],["57415645","audio/wav"],["664C6143","audio/flac"],["000001BA","video/MP1S"],["000001BA","video/mpeg"],["000001B3","video/mpeg"],["1A45DFA3","video/x-matroska"],["6674797069736F6D","video/mp4"],["0000001866747970","video/mp4"],["667479706d703432","video/mp4"],["47","video/MP2T"],["1A45DFA3","video/webm"],["504B0304","application/zip"],["52617221","application/vnd.rar"],["1F8B08","application/gzip"],["75737461","application/x-tar"],["425A68","application/x-bzip2"],["377ABCAF271C","application/x-7z-compressed"],["25504446","application/pdf"],["D0CF11E0A1B11AE1","application/msword"],["7B5C727466","application/rtf"],["4D5A","application/vnd.microsoft.portable-executable"],["FFD8FFE0","image/jpeg"],["MZ","application/octet-stream"]]))if(t.startsWith(e))return a}const f=[["word/","application/vnd.openxmlformats-officedocument.wordprocessingml.document"],["ppt/","application/vnd.openxmlformats-officedocument.presentationml.presentation"],["xl/","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"],["@Uu","application/zip+fbr"],["mimetypeapplication/","application/vnd.adobe.xd"]];const u="//",g="SimpleData,",v=new RegExp(`^(${g})?(\\{.*\\}|\\[.*\\])$`,"i"),w=[[/^<(!DOCTYPE\s+)?html/i,"text/html"],[/^<svg/i,"image/svg+xml"],[/^<(\?xml|\w+)/i,"text/xml"],[/^[{\[]/i,"application/json"],[/^8BPS/i,"image/vnd.adobe.photoshop"],[/^@?(echo|chcp|set)/i,"text/x-sh"],[new RegExp(`^${g}`,"i"),"text/x-simpledata"],[/^DAUMPLAYLIST/i,"application/x-daumplaylist"]];async function b(e){const t=(await d(e,50)).trim();for(const[e,a]of w)if(e.test(t))return a}const h=/stream|unknown|binary|text|xml'/;async function T(e){const t=e.type||"";if(t&&!h.test(t))return t;let a=t?.includes("text/plain")?await b(e):"";if(a)return a;const i=await x(e);return/zip$/.test(i)?await async function(e){const t=await d(e,50,30);for(const[e,a]of f)if(t.includes(e))return a;return"application/zip"}(e):(a=!i||i.includes("text/plain")?await b(e):"",h.test(i)?a||t||i||void 0:a||i||t||void 0)}const y=o.decode,S=o.encode;async function F(e,t="gzip"){const a=new CompressionStream(t),i=a.writable.getWriter();return i.write(e instanceof Uint8Array?e:new Uint8Array(e)),i.close(),await new Response(a.readable).arrayBuffer()}async function P(e,t="gzip"){const a=new DecompressionStream(t),i=a.writable.getWriter();i.write(e instanceof Uint8Array?e:new Uint8Array(e)),i.close();const o=a.readable;return await new Response(o).arrayBuffer()}async function M(e,t="gzip"){return await F((new TextEncoder).encode(e),t)}async function A(e,t="gzip"){const a=atob(e),i=a.length,o=new Uint8Array(i);for(let e=0;e<i;e++)o[e]=a.charCodeAt(e);return await P(o,t)}async function D(e){if(e.fields)return B(e.fields,e.excludeFields);const t=await async function(e,t=30){const a=[];let o=1;if(i.isIterable(e)){for(const i of e)if(a.push(i),++o>t)break}else if(i.isAsyncIterable(e))for await(const i of e)if(a.push(i),++o>t)break;return a}(e.data),a={};let o,n=0;for(const e of t){const t=Object.keys(e).length;t>n&&(n=t,o=e)}n=0;for(const e in o)a[e]=n++;return B(a,e.excludeFields)}function O(e){return Object.entries(e).sort(E)}function B(e,t){if(!t)return e;const a={},i=Object.entries(e).sort(E);let o=0;for(const[e]of i)t.includes(e)||(a[e]=o++);return a}function E(e,t){return e[1]-t[1]}function z(e,t){const a=[];for(const[i]of e)a.push(t[i]);const i=JSON.stringify(a);return i.slice(1,i.length-1)}class j{_arg;constructor(e){this._arg=e}async*[Symbol.asyncIterator](){const{header:e,exportData:t}=this._arg;yield`${g}${JSON.stringify(e)}`;for(const e of t){const t=await D(e),a=O(t),o={name:e.name,fields:t};if(e.description&&(o.description=e.description),e.extraData&&(o.extraData=e.extraData),e.defaultValues&&(o.defaultValues=e.defaultValues),yield`${u}${JSON.stringify(o)}`,i.isIterable(e.data))for(const t of e.data)yield z(a,t);else if(i.isAsyncIterable(e.data))for await(const t of e.data)yield z(a,t)}}destroy(){delete this._arg}}const k=/^\s*$/;class R{_textReader;constructor(e){i.isString(e)?this._textReader=new i.StringTextReader(e):this._textReader=e}getHeader(){const e=this._textReader.firstLine;return e&&v.exec(e)?.[2]||{}}getSize(){return this._textReader.size}*read(e){let t,a,o;for(const{data:n,progress:p}of this._textReader){if(k.test(n))continue;if(n.startsWith(u)){if(o=JSON.parse(n.slice(2)),t&&t===e)return;t=o.name,a=[];for(const[e,t]of Object.entries(o.fields))a.push([e,Number(t)]);continue}if(!t||!a||e&&t!==e)continue;const s=JSON.parse(`[${n}]`),c=o?.defaultValues?i.copyObject(o?.defaultValues):{};for(const[e,t]of a)c[e]=s[t];yield{name:t,data:c,progress:p,meta:o}}}getAll(e){const t=[];for(const{data:a}of this.read(e))t.push(a);return t}async pageRead(e,t=1e3){let a=[];const i=this._textReader.size;let o,n;for(const{name:i,data:p,progress:s,meta:c}of this.read())a.push(p),a.length>=t?(await e(i,a,s,c),a=[]):(o=i,n=c);a.length&&await e(o,a,i,n)}async namedRead(e,t=1e3){await this.pageRead((async(t,a,i,o)=>{t in e&&await e[t](a,i,o)}),t)}destroy(){try{this._textReader?.destroy?.()}finally{delete this._textReader}}}var L;function C(e,t,a){return new Promise(((i,o)=>{const n=new FileReader;n.onload=()=>{i(n.result)},n.onerror=o,n[t](e,a)}))}exports.BlobTypes=void 0,(L=exports.BlobTypes||(exports.BlobTypes={}))[L.Text=1]="Text",L[L.Base64=2]="Base64",L[L.Image=3]="Image",L[L.Bitmap=4]="Bitmap",L[L.Svg=5]="Svg",L[L.Html=6]="Html",L[L.Xml=7]="Xml",L[L.Json=8]="Json",L[L.SimpleData=9]="SimpleData",L[L.NotSupped=10]="NotSupped";const U=[1,6,5,7,8,9],I=/<meta[^>]+charset\s*=\s*["']?([\w-]+)|^<\?xml[^>]+encoding\s*=\s*"?([\w-]+)/i;function G(e,t){return(new globalThis.DOMParser).parseFromString(e,t)}const V=[2,3];function W(e){switch(e){case"text/plain":return 1;case"application/base64":return 2;case"image/jpeg":case"image/png":case"image/gif":case"image/webp":case"image/bmp":case"image/x-icon":return 3;case"image/svg+xml":return 5;case"text/html":return 6;case"text/xml":return 7;case"application/json":return 8;case"text/x-simpledata":return 9;default:return 10}}exports.NamedWeakMap=class{valueCreator;objectRefs={};constructor(e){this.valueCreator=e}get(e){let t=this.objectRefs[e]?.deref();return t||(this.valueCreator&&this.set(e,t=this.valueCreator(e)),t)}set(e,t){this.objectRefs[e]=new WeakRef(t)}destroy(){for(let e in this.objectRefs)this.objectRefs[e]?.deref()||delete this.objectRefs[e]}},exports.SimpleDataExport=j,exports.SimpleDataReader=R,exports.base64ToBlob=function(e,t){const a=e.indexOf(";base64,");a>-1&&(e=e.slice(a+8));const i=atob(e),o=i.length,n=new Uint8Array(o);for(let e=0;e<o;e++)n[e]=i.charCodeAt(e);return new Blob([n],{type:t})},exports.compressData=F,exports.compressText=M,exports.compressTextToBase64=async function(e,t="gzip"){return btoa(String.fromCharCode(...new Uint8Array(await M(e,t))))},exports.compressToBase64=async function(e,t="gzip"){return btoa(String.fromCharCode(...new Uint8Array(await F(e,t))))},exports.decodeBase64=y,exports.decompressData=P,exports.decompressFromBase64=A,exports.decompressText=async function(e,t="gzip"){return(new TextDecoder).decode(await P(e,t))},exports.decompressTextFromBase64=async function(e,t="gzip"){return(new TextDecoder).decode(await A(e,t))},exports.encodeBase64=S,exports.exportSimpleData=e=>i.joinAsyncIterable(new j(e)),exports.getExtensionMime=r,exports.miniToBlobType=W,exports.parseMime=T,exports.parseMimeExt=async function(e){return r(`${await T(e)}`)},exports.readBlob=async function(e,...t){let a=0;const o=i.isNumber(t[a])?t[a++]:void 0,n=i.isString(t[a])?t[a++]:void 0,p=!o&&await T(e),s=o||W(p);let c;return 4===s?c=await globalThis.createImageBitmap(e):U.includes(s)?c=await async function(e,t,a){if(!a){const[,t,i]=I.exec(await e.slice(0,1024).text())||[];a=t||i}const i=await C(e,"readAsText",a);switch(t){case 5:return G(i,"image/svg+xml").querySelector("svg");case 7:return G(i,"text/xml");case 6:return G(i,"text/html");case 9:return new R(i);case 8:return JSON.parse(i);default:return i}}(e,s,n):V.includes(s)&&(c=await function(e,t){return new Promise((async(a,i)=>{const o=await C(e,"readAsDataURL");if(3===t){const e=new globalThis.Image;e.onload=()=>a(e),e.onerror=i,e.src=o}else a(o)}))}(e,s)),o&&10!==s?c:{...r(p),blob:e,type:s,result:c}},exports.readBlobData=C,exports.supportsCompression=function(){return typeof CompressionStream<"u"&&typeof DecompressionStream<"u"};