observablehq-file-attachments
Version:
Library to handle ObservableHQ's file attachments more flexibly, and to support virtualizing them.
3 lines (2 loc) • 12 kB
JavaScript
var t,e;t=this,e=function(t){const e=Symbol.for("METADATA"),n=Symbol.for("CACHED_METADATA"),r=Symbol.for("FILE"),a=Symbol.for("DIRECTORY"),i=Symbol.for("TAGS");var s={},o={};function c(t){return new Function("d","return {"+t.map((function(t,e){return JSON.stringify(t)+": d["+e+'] || ""'})).join(",")+"}")}function u(t){var e=Object.create(null),n=[];return t.forEach((function(t){for(var r in t)r in e||n.push(e[r]=r)})),n}function l(t,e){var n=t+"",r=n.length;return r<e?new Array(e-r+1).join(0)+n:n}function f(t){var e,n=t.getUTCHours(),r=t.getUTCMinutes(),a=t.getUTCSeconds(),i=t.getUTCMilliseconds();return isNaN(t)?"Invalid Date":((e=t.getUTCFullYear())<0?"-"+l(-e,6):e>9999?"+"+l(e,6):l(e,4))+"-"+l(t.getUTCMonth()+1,2)+"-"+l(t.getUTCDate(),2)+(i?"T"+l(n,2)+":"+l(r,2)+":"+l(a,2)+"."+l(i,3)+"Z":a?"T"+l(n,2)+":"+l(r,2)+":"+l(a,2)+"Z":r||n?"T"+l(n,2)+":"+l(r,2)+"Z":"")}function h(t){var e=new RegExp('["'+t+"\n\r]"),n=t.charCodeAt(0);function r(t,e){var r,a=[],i=t.length,c=0,u=0,l=i<=0,f=!1;function h(){if(l)return o;if(f)return f=!1,s;var e,r,a=c;if(34===t.charCodeAt(a)){for(;c++<i&&34!==t.charCodeAt(c)||34===t.charCodeAt(++c););return(e=c)>=i?l=!0:10===(r=t.charCodeAt(c++))?f=!0:13===r&&(f=!0,10===t.charCodeAt(c)&&++c),t.slice(a+1,e-1).replace(/""/g,'"')}for(;c<i;){if(10===(r=t.charCodeAt(e=c++)))f=!0;else if(13===r)f=!0,10===t.charCodeAt(c)&&++c;else if(r!==n)continue;return t.slice(a,e)}return l=!0,t.slice(a,i)}for(10===t.charCodeAt(i-1)&&--i,13===t.charCodeAt(i-1)&&--i;(r=h())!==o;){for(var d=[];r!==s&&r!==o;)d.push(r),r=h();e&&null==(d=e(d,u++))||a.push(d)}return a}function a(e,n){return e.map((function(e){return n.map((function(t){return l(e[t])})).join(t)}))}function i(e){return e.map(l).join(t)}function l(t){return null==t?"":t instanceof Date?f(t):e.test(t+="")?'"'+t.replace(/"/g,'""')+'"':t}return{parse:function(t,e){var n,a,i=r(t,(function(t,r){if(n)return n(t,r-1);a=t,n=e?function(t,e){var n=c(t);return function(r,a){return e(n(r),a,t)}}(t,e):c(t)}));return i.columns=a||[],i},parseRows:r,format:function(e,n){return null==n&&(n=u(e)),[n.map(l).join(t)].concat(a(e,n)).join("\n")},formatBody:function(t,e){return null==e&&(e=u(t)),a(t,e).join("\n")},formatRows:function(t){return t.map(i).join("\n")},formatRow:i,formatValue:l}}var d=h(","),y=d.parse,w=d.parseRows,p=h("\t"),g=p.parse,m=p.parseRows;function b(t){for(var e in t){var n,r,a=t[e].trim();if(a)if("true"===a)a=!0;else if("false"===a)a=!1;else if("NaN"===a)a=NaN;else if(isNaN(n=+a)){if(!(r=a.match(/^([-+]\d{2})?\d{4}(-\d{2}(-\d{2})?)?(T\d{2}:\d{2}(:\d{2}(\.\d{3})?)?(Z|[-+]\d{2}:\d{2})?)?$/)))continue;A&&r[4]&&!r[7]&&(a=a.replace(/-/g,"/").replace(/T/," ")),a=new Date(a)}else a=n;else a=null;t[e]=a}return t}const A=new Date("2019-01-01T00:00").getHours()||new Date("2019-07-01T00:00").getHours();for(var v=function(t){for(var e,n=t.length,r=n%3,a=[],i=16383,s=0,o=n-r;s<o;s+=i)a.push(E(t,s,s+i>o?o:s+i));return 1===r?(e=t[n-1],a.push(C[e>>2]+C[e<<4&63]+"==")):2===r&&(e=(t[n-2]<<8)+t[n-1],a.push(C[e>>10]+C[e>>4&63]+C[e<<2&63]+"=")),a.join("")},C=[],T=[],j="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",D=0,x=j.length;D<x;++D)C[D]=j[D],T[j.charCodeAt(D)]=D;function E(t,e,n){for(var r,a,i=[],s=e;s<n;s+=3)r=(t[s]<<16&16711680)+(t[s+1]<<8&65280)+(255&t[s+2]),i.push(C[(a=r)>>18&63]+C[a>>12&63]+C[a>>6&63]+C[63&a]);return i.join("")}T["-".charCodeAt(0)]=62,T["_".charCodeAt(0)]=63;class R{constructor(t,n,r={}){this.#noCache=!1,"string"==typeof r&&(r={name:t,contentType:r}),this[e]={...r,name:t},this.name=t,this.data=n;let{contentType:a}=r;this["content-type"]=a=a||("string"==typeof n?"text/plain":n.constructor===Array||n.constructor===Object?"application/json":"application/binary"),this[e].contentType=a}#dataResult;#noCache;async getData(t,e){let n=await(!this.#noCache&&this.#dataResult||this.data);if(n instanceof R)return n.getData(t,e);if("function"==typeof n&&(this.#dataResult=Promise.resolve(n(this,t,e)).then((t=>(this.#noCache=t instanceof ReadableStream,t))),n=await this.#dataResult,this.#noCache&&(this.#dataResult=void 0)),n instanceof ReadableStream&&"stream"!==t){let t=[];const e=await n.getReader();let r=!1,a=0;for(;!r;){const n=await e.read();n.value&&(t.push(n.value),a+=n.value.byteLength),r=r||n.done}const i=new ArrayBuffer(a),s=new Uint8Array(i);let o=0;for(const e of t)s.set(e,o),o+=e.byteLength;return i}return this.#noCache||(this.data=n),n}async json(t={utf8:!0}){const e=await this.getData("json",t);return e instanceof Blob||e instanceof ReadableStream||e instanceof ArrayBuffer?JSON.parse(await this.text(t)):e}async text(t={utf8:!0}){const{utf8:e=!0}=t,n=await this.getData("text",t);return"string"==typeof n?n:n instanceof ArrayBuffer?e?(new TextDecoder).decode(new Uint8Array(n)):String.fromCodePoint(...new Uint16Array(n)):n instanceof Blob?e?(new TextDecoder).decode(new Uint8Array(await n.arrayBuffer())):String.fromCodePoint(...new Uint16Array(await n.arrayBuffer())):JSON.stringify(n)}async url(t={utf8:!0}){const e=await this.getData("url",t),n=this?.["content-type"];if("string"==typeof e)return`data:${n};UTF-8,${e}`;const r=await this.arrayBuffer(),a=new Uint8Array(r);return`data:${n};base64,${v(a)}`}async arrayBuffer(t={utf8:!0}){const e=await this.getData("arrayBuffer",t);return e instanceof ArrayBuffer?e:e instanceof Blob?e.arrayBuffer():V(await this.text(t),t)}async blob(t={utf8:!0}){const e=await this.getData("blob",t);return e instanceof Blob?e:new Blob([new Uint8Array(await this.arrayBuffer(t))])}async csv(t={utf8:!0}){const e=await this.getData("csv",t);return Array.isArray(e)||e.constructor===Object?e:H(await this.text(),",",t)}async tsv(t={utf8:!0}){const e=await this.getData("tsv",t);return Array.isArray(e)||e.constructor===Object?e:H(await this.text(),"\t",t)}async stream(t={utf8:!0}){let e=await this.getData("stream",t);return e instanceof ReadableStream?e:new ReadableStream({pull:async t=>{const e=await this.arrayBuffer(),n=new Uint8Array(e);t.enqueue(n),t.close()}})}}const B=t=>{if(t instanceof Error)throw t;throw new Error(t)},O=t=>(t=>Array.isArray(t))(t)?t:null==t?null:B("Unexpected file level"),S=t=>(t=>t instanceof Object)(t)?t:null==t?null:B("Unexpected directory level"),U=t=>t instanceof Function,N=(t,e)=>{if("string"==typeof(t=t??-1)){switch(t){case"latest":return e-1;case"earliest":return 0;case"*":throw new Error(`Illegal version: ${t}`)}return/^[-+]?\d+$/.test(t)?N(Number.parseInt(t,10),e):t}return 0===(t=t??-1)?null:t<0?e+t<0?null:e+t:t-1},$=(t,e)=>{const n=N(e,t.length);return null===n?null:"string"==typeof n?t[i]?.[n]??null:t[n]??null},F=(t,...e)=>{0===e.length&&(e=[1]);const n=[];return e.forEach((e=>P(n,e,t))),n};function I(t,n,r,...a){if(!r){const r={name:t},i=new R(t,n),s={...r,...i[e]??{}};return M(F(i,...a),s)}if("string"==typeof r||"number"==typeof r){const i={name:t},s=new R(t,n),o={...i,...s[e]??{}};return M(F(s,r,...a),o)}{const i={name:t,...r??{}},s=new R(t,n,r),o={...i,...s[e]??{}};return M(F(s,...a),o)}}const P=(t,e,n)=>{const r=N(e,t.length);if(null===r)throw new Error(`Cannot set version ${e}`);"string"==typeof r?(t[i]||(t[i]={}),t[i][e]=n):t[r]=n},V=(t,{utf8:e=!0}={utf8:!0})=>e?(new TextEncoder).encode(t).buffer:(t=>{const e=new ArrayBuffer(2*t.length),n=new Uint16Array(e);for(let e=0;e<t.length;e++)n[e]=t.codePointAt(e);return e})(t),M=(t,n)=>t&&Object.defineProperty(t,e,{value:n});function H(t,e,{array:n=!1,typed:r=!1,utf8:a=!1}={}){const i=r?b:void 0;switch(e){case"\t":return n?m(t,i):g(t,i);case",":return n?w(t,i):y(t,i)}}class J{constructor(t,n){this.#name="(unresolved)",void 0===n?this.#target=t.then((t=>(t&&(this.#name=t.name??this.#name,this[e]={name:this.#name,...this[e]??{},...t[e]??{}}),t))):(this.#name=n,this.#target=t)}#target;#name;async url(){return(await this.#target)?.url()}async json(){return(await this.#target)?.json()}async blob(){return(await this.#target)?.blob()}async text(){return(await this.#target)?.text()}async arrayBuffer(){return(await this.#target)?.arrayBuffer()}async csv(){return(await this.#target)?.text()}async tsv(){return(await this.#target)?.text()}get name(){return this.#name??"(unresolved)"}get target(){return this.#target}get exists(){const t=this.#target.then((t=>{if(!t)throw new L(`Virtual file not found: ${this.name}`);return t}));return new J(t,this.#name)}}class L extends Error{constructor(t="Virtual file not found"){super(t)}}let Z=0;const _=(t,e,n,i)=>{const{file:s=(()=>null),directory:o=(()=>null),createFiles:c=(()=>null),createDirectory:u=(()=>null)}=i,l=async([n,...i],f)=>{if(!f)return null;if(""===n)return f?l(i,f):null;if(void 0===n)throw new Error("Accessing root as file.");if(0===i.length){const[a,o]=n.split("@");let u=O(f[a]);return u||(u=await(f[r]?.(t,e,a,o,i,f))??await c(t,e,a,o,f,u??[])??[],u&&(f[a]=u)),u?(f[a]=u,s(e,a,o,u)):null}{let r=S(f[n]);return r||(r=await(f[a]?.(t,e,n,i,f))??await u(t,e,n,i,f)??null,r&&(f[n]=r)),o(e,n,f),l(i,r)}};return l(e.split("/"),n)},k=(t,e,...n)=>r=>{try{return r()}catch(r){throw r.message=`${t.name}.${e}(${n.map((t=>JSON.stringify(t))).join(", ")}) ${r.message}`,r}},Y={version:"0.1.13",git:{revision:"723a4c5aba11a56f0f33bbc9024419b61ec3b4d8"},date:new Date(1615835054340)};t.AFile=R,t.AFileAwait=J,t.AFileSystem=class{constructor(t,e={}){const{readOnly:n,name:r}=e;this.readOnly=!!n,this.name=r||"FS_"+ ++Z,this.tree=t||B("Missing tree."),this.subscription=async function*(t){let e=Promise.resolve(t);for(t.updateCount=0;;)e=new Promise(((e,n)=>{t.updated=e,t.errored=n})),yield t,await e,t.updateCount++}(this),this.subscription.next(),this.updateCount=0,this.errored=()=>{},this.updated=()=>{}}find(t){return k(this,"find",t)((()=>new J(_(this,t,this.tree,{file:(t,e,n,r)=>{if(r.constructor===Object)throw new Error(`${t} is a directory.`);const a=$(r,n);if(!a)return null;if(Array.isArray(a)||a.constructor===Object)throw new Error(`${t} is a directory`);return a}}))))}async*waitFor(t){let e=this.find(t);if(e)yield e;else for await(const n of this.subscription){const r=n.find(t);if(r)return e=r,void(yield e)}}async*watch(t,e=!1){let n=this.find(t);(n||e)&&(yield n);for await(const r of this.subscription){const a=r.find(t);a!==n&&(n=a,(e||n)&&(yield n))}}async metadata(t){return k(this,"metadata",t)((()=>_(this,t,this.tree,{file:async(t,r,a,i)=>{const s=$(i,a);if(!s)return null;const o=i[e]||{};if((c=s)&&U(c?.json)&&U(c?.text)&&U(c?.blob)&&U(c?.buffer)&&U(c?.stream)&&U(c?.url)&&c.constructor!==Object&&!s[n]){const t=await(await fetch(await s.url(),{method:"HEAD"})).headers,r={name:s.name,url:await s.url()};t.forEach(((t,e)=>{switch(e){case"content-length":r.length=Number.parseInt(t);break;case"last-modified":r.modificationDate=new Date(t);break;case"etag":r.etag=t;break;case"content-type":r.contentType=t}}));const a=s[e]||{};Object.defineProperty(s,n,{value:{...o,...a,...r}})}var c;const u=s["content-type"],l=u?{"content-type":u}:{name:r};return{name:r,...o,...l,...s[n]??{}}}})))}add(t,e){return k(this,"add",t,e)((()=>{if(this.readOnly)throw new Error("Read only filesystem.");const n=(t,n,r,a,i)=>0===a.length?i[r]=[e]:i[r]={},r=(t,n,r,a)=>(P(a,r,e),e);try{return _(this,t,this.tree,{file:r,createDirectory:n})}catch(t){throw this.errored(t),t}finally{this.updated(this)}}))}copy(t,e){return this.add(e,this.find(t))}label(t,e){const n=t.split("/"),r=n[n.length-1].split("@")[0];n[n.length-1]=`${r}@${e}`;const a=n.join("/");return this.copy(t,a)}},t.DIRECTORY=a,t.FILE=r,t.VERSION=Y,t.VirtualFileNotFound=L,t.deleteVersion=(t,n)=>{switch(n){case"*":return t.length=0,t[i]={},void(t[e]=t[e]??{name:t[e].name})}if(null!==N(n,t.length))if("string"!=typeof n)delete t[n];else{const e=t?.[i];e&&delete e[n]}},t.dsv=H,t.entry=function(t,e,n,...r){return{[t]:I(t,e,n,...r)}},t.file=I,t.getVersion=$,t.meta=M,t.setVersion=P,t.versions=F,Object.defineProperty(t,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["observablehq-file-attachments"]={});
//# sourceMappingURL=index.js.map