@hpcc-js/observablehq-compiler
Version:
hpcc-js - ObservableHQ Compiler (unoffical)
187 lines (186 loc) • 5.71 kB
JavaScript
import "./chunk-rwCFCtyI.js";
//#region ../../node_modules/@observablehq/notebook-kit/dist/src/runtime/stdlib/fileAttachment.js
var e = /* @__PURE__ */ new Map(), t = !1, FileAttachment = (n, r = document.baseURI) => {
let i = new URL(n, r).href, a = e.get(i);
if (!a) {
if (t) throw Error(`File not found: ${n}`);
a = new FileAttachmentImpl(i, n.split("/").pop()), e.set(i, a);
}
return a;
};
async function fetchFile(e) {
let t = await fetch(e.href);
if (!t.ok) throw Error(`Unable to load file: ${e.name}`);
return t;
}
var AbstractFile = class {
constructor(e, t = guessMimeType(e), n, r) {
Object.defineProperty(this, "name", {
enumerable: !0,
configurable: !0,
writable: !0,
value: void 0
}), Object.defineProperty(this, "mimeType", {
enumerable: !0,
configurable: !0,
writable: !0,
value: void 0
}), Object.defineProperty(this, "lastModified", {
enumerable: !0,
configurable: !0,
writable: !0,
value: void 0
}), Object.defineProperty(this, "size", {
enumerable: !0,
configurable: !0,
writable: !0,
value: void 0
}), Object.defineProperties(this, {
name: {
value: `${e}`,
enumerable: !0
},
mimeType: {
value: `${t}`,
enumerable: !0
},
lastModified: {
value: n === void 0 ? void 0 : +n,
enumerable: !0
},
size: {
value: r === void 0 ? void 0 : +r,
enumerable: !0
}
});
}
async url() {
return this.href;
}
async blob() {
return (await fetchFile(this)).blob();
}
async arrayBuffer() {
return (await fetchFile(this)).arrayBuffer();
}
async text(e) {
return e === void 0 ? (await fetchFile(this)).text() : new TextDecoder(e).decode(await this.arrayBuffer());
}
async json() {
return (await fetchFile(this)).json();
}
async stream() {
return (await fetchFile(this)).body;
}
async dsv({ delimiter: e = ",", array: t = !1, typed: n = !1 } = {}) {
let [r, i] = await Promise.all([this.text(), import("https://cdn.jsdelivr.net/npm/d3-dsv/+esm")]), a = i.dsvFormat(e);
return (t ? a.parseRows : a.parse)(r, n && i.autoType);
}
async csv(e) {
return this.dsv({
...e,
delimiter: ","
});
}
async tsv(e) {
return this.dsv({
...e,
delimiter: " "
});
}
async image(e) {
let t = await this.url();
return new Promise((n, r) => {
let i = new Image();
new URL(t, document.baseURI).origin !== location.origin && (i.crossOrigin = "anonymous"), Object.assign(i, e), i.onload = () => n(i), i.onerror = () => r(/* @__PURE__ */ Error(`Unable to load file: ${this.name}`)), i.src = t;
});
}
async arrow() {
let [e, t] = await Promise.all([import("https://cdn.jsdelivr.net/npm/apache-arrow@17.0.0/+esm"), fetchFile(this)]);
return e.tableFromIPC(t);
}
async arquero(e) {
let t, n;
switch (this.mimeType) {
case "application/json":
t = this.text(), n = "fromJSON";
break;
case "text/tab-separated-values": e?.delimiter === void 0 && (e = {
...e,
delimiter: " "
});
case "text/csv":
t = this.text(), n = "fromCSV";
break;
default:
if (/\.arrow$/i.test(this.name)) t = this.arrow(), n = "fromArrow";
else if (/\.parquet$/i.test(this.name)) t = this.parquet(), n = "fromArrow";
else throw Error(`unable to determine Arquero loader: ${this.name}`);
break;
}
let [r, i] = await Promise.all([import("https://cdn.jsdelivr.net/npm/arquero/+esm"), t]);
return r[n](i, e);
}
async parquet() {
let [e, t, n] = await Promise.all([
import("https://cdn.jsdelivr.net/npm/apache-arrow@17.0.0/+esm"),
import("https://cdn.jsdelivr.net/npm/parquet-wasm/+esm").then(async (e) => (await e.default("https://cdn.jsdelivr.net/npm/parquet-wasm/esm/parquet_wasm_bg.wasm"), e)),
this.arrayBuffer()
]);
return e.tableFromIPC(t.readParquet(new Uint8Array(n)).intoIPCStream());
}
async zip() {
let [{ ZipArchive: e }, t] = await Promise.all([import("./zip-BCqI2MMo.js"), this.arrayBuffer()]);
return e.from(t);
}
async xml(e = "application/xml") {
return new DOMParser().parseFromString(await this.text(), e);
}
async html() {
return this.xml("text/html");
}
async xlsx() {
let [{ Workbook: e }, t] = await Promise.all([import("./xlsx-BUACicGQ.js"), this.arrayBuffer()]);
return e.load(t);
}
};
function guessMimeType(e) {
let t = e.lastIndexOf("."), n = e.lastIndexOf("/");
switch (t > 0 && (n < 0 || t > n) ? e.slice(t).toLowerCase() : "") {
case ".csv": return "text/csv";
case ".tsv": return "text/tab-separated-values";
case ".json": return "application/json";
case ".html": return "text/html";
case ".xml": return "application/xml";
case ".png": return "image/png";
case ".jpg": return "image/jpg";
case ".js": return "text/javascript";
default: return "application/octet-stream";
}
}
var FileAttachmentImpl = class extends AbstractFile {
constructor(e, t, n, r, i) {
super(t, n, r, i), Object.defineProperty(this, "href", {
enumerable: !0,
configurable: !0,
writable: !0,
value: void 0
}), Object.defineProperty(this, "href", { value: e });
}
};
Object.defineProperty(FileAttachmentImpl, "name", { value: "FileAttachment" }), FileAttachment.prototype = FileAttachmentImpl.prototype;
function fileAttachments(e) {
function FileAttachment(t) {
let n = e(t += "");
if (n == null) throw Error(`File not found: ${t}`);
if (typeof n == "object" && "url" in n) {
let { url: e, mimeType: r } = n;
return new FileAttachmentImpl(e, t, r);
}
return new FileAttachmentImpl(n, t);
}
return FileAttachment.prototype = FileAttachmentImpl.prototype, FileAttachment;
}
//#endregion
export { FileAttachment as n, fileAttachments as r, AbstractFile as t };
//# sourceMappingURL=fileAttachment-DCFb8_eF.js.map