-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathfileAttachment.js
168 lines (159 loc) · 5.23 KB
/
fileAttachment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {autoType, csvParse, csvParseRows, tsvParse, tsvParseRows} from "d3-dsv";
import {arrow4, arrow9, arrow11, jszip, exceljs} from "./dependencies.js";
import {cdn, requireDefault} from "./require.js";
import {SQLiteDatabaseClient} from "./sqlite.js";
import {inferSchema, coerceRow} from "./table.js";
import {Workbook} from "./xlsx.js";
async function remote_fetch(file) {
const response = await fetch(await file.url());
if (!response.ok) throw new Error(`Unable to load file: ${file.name}`);
return response;
}
export function enforceSchema(source, schema) {
const types = new Map(schema.map(({name, type}) => [name, type]));
return Object.assign(source.map(d => coerceRow(d, types, schema)), {schema});
}
async function dsv(file, delimiter, {array = false, typed = false} = {}) {
const text = await file.text();
const parse = (delimiter === "\t"
? (array ? tsvParseRows : tsvParse)
: (array ? csvParseRows : csvParse));
if (typed === "auto" && !array) {
const source = parse(text);
return enforceSchema(source, inferSchema(source, source.columns));
}
return parse(text, typed && autoType);
}
export class AbstractFile {
constructor(name, mimeType) {
Object.defineProperty(this, "name", {value: name, enumerable: true});
if (mimeType !== undefined) Object.defineProperty(this, "mimeType", {value: mimeType + "", enumerable: true});
}
async blob() {
return (await remote_fetch(this)).blob();
}
async arrayBuffer() {
return (await remote_fetch(this)).arrayBuffer();
}
async text() {
return (await remote_fetch(this)).text();
}
async json() {
return (await remote_fetch(this)).json();
}
async stream() {
return (await remote_fetch(this)).body;
}
async csv(options) {
return dsv(this, ",", options);
}
async tsv(options) {
return dsv(this, "\t", options);
}
async image(props) {
const url = await this.url();
return new Promise((resolve, reject) => {
const i = new Image();
if (new URL(url, document.baseURI).origin !== new URL(location).origin) {
i.crossOrigin = "anonymous";
}
Object.assign(i, props);
i.onload = () => resolve(i);
i.onerror = () => reject(new Error(`Unable to load file: ${this.name}`));
i.src = url;
});
}
async arrow({version = 4} = {}) {
switch (version) {
case 4: {
const [Arrow, response] = await Promise.all([requireDefault(arrow4.resolve()), remote_fetch(this)]);
return Arrow.Table.from(response);
}
case 9: {
const [Arrow, response] = await Promise.all([import(`${cdn}${arrow9.resolve()}`), remote_fetch(this)]);
return Arrow.tableFromIPC(response);
}
case 11: {
const [Arrow, response] = await Promise.all([import(`${cdn}${arrow11.resolve()}`), remote_fetch(this)]);
return Arrow.tableFromIPC(response);
}
default: throw new Error(`unsupported arrow version: ${version}`);
}
}
async sqlite() {
return SQLiteDatabaseClient.open(remote_fetch(this));
}
async zip() {
const [JSZip, buffer] = await Promise.all([requireDefault(jszip.resolve()), this.arrayBuffer()]);
return new ZipArchive(await JSZip.loadAsync(buffer));
}
async xml(mimeType = "application/xml") {
return (new DOMParser).parseFromString(await this.text(), mimeType);
}
async html() {
return this.xml("text/html");
}
async xlsx() {
const [ExcelJS, buffer] = await Promise.all([requireDefault(exceljs.resolve()), this.arrayBuffer()]);
return new Workbook(await new ExcelJS.Workbook().xlsx.load(buffer));
}
}
export class FileAttachment extends AbstractFile {
constructor(url, name, mimeType) {
super(name, mimeType);
Object.defineProperty(this, "_url", {value: url});
}
async url() {
return (await this._url) + "";
}
}
export function NoFileAttachments(name) {
throw new Error(`File not found: ${name}`);
}
export function FileAttachments(resolve) {
return Object.assign(
name => {
const result = resolve(name += "");
if (result == null) throw new Error(`File not found: ${name}`);
if (typeof result === "object" && "url" in result) {
const {url, mimeType} = result;
return new FileAttachment(url, name, mimeType);
}
return new FileAttachment(result, name);
},
{prototype: FileAttachment.prototype} // instanceof
);
}
export class ZipArchive {
constructor(archive) {
Object.defineProperty(this, "_", {value: archive});
this.filenames = Object.keys(archive.files).filter(name => !archive.files[name].dir);
}
file(path) {
const object = this._.file(path += "");
if (!object || object.dir) throw new Error(`file not found: ${path}`);
return new ZipArchiveEntry(object);
}
}
class ZipArchiveEntry extends AbstractFile {
constructor(object) {
super(object.name);
Object.defineProperty(this, "_", {value: object});
Object.defineProperty(this, "_url", {writable: true});
}
async url() {
return this._url || (this._url = this.blob().then(URL.createObjectURL));
}
async blob() {
return this._.async("blob");
}
async arrayBuffer() {
return this._.async("arraybuffer");
}
async text() {
return this._.async("text");
}
async json() {
return JSON.parse(await this.text());
}
}