-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
359 lines (333 loc) · 7.9 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const RUNTIME_BROWSER = 0;
//const RUNTIME_NODE = 1;
//const RUNTIME_DENO = 2;
//const RUNTIME_BUN = 3;
const runtime = detectRuntime();
class File {
constructor(f, start, end) {
this._file = f;
this._start = start;
this._end = end;
this._size = end - start;
}
get size() {
return this._size;
}
stream() {
throw new Error("Must implement stream()");
}
slice() {
throw new Error("Must implement slice()");
}
}
class DirectoryTree {
constructor(rootPath) {
this._rootPath = rootPath;
}
/**
* @returns {Promise<File>}
*/
async openFile(path) {
throw new Error("Must implement openFile()");
}
}
//class NodeFile extends File {
//
// constructor(f, start, end) {
// super(f, start, end);
// }
//
// slice(start, end, contentType) {
// return new NodeFile(this._file, start ? start : this._start, end ? end : this._end);
// }
//
// stream() {
// const nodeStream = this._file.createReadStream({
// start: this._start,
// end: this._end,
// });
//
// let done = false;
// let controller;
//
// const rs = new ReadableStream({
//
// async start(contr) {
// controller = contr;
// },
//
// pull(contr) {
// if (contr.desiredSize > 0) {
// nodeStream.resume();
// }
// },
//
// cancel() {
// nodeStream.close();
// done = true;
// }
//
// });
//
// nodeStream.on('data', (chunk) => {
// if (controller.desiredSize > 0) {
// controller.enqueue(chunk);
// }
// else {
// nodeStream.pause();
// }
// });
//
// nodeStream.on('close', (chunk) => {
// if (!done) {
// controller.close();
// done = true;
// }
// });
//
// return rs;
// }
//}
//
//class DenoFile extends File {
// constructor(f, start, end) {
// super(f, start, end);
//
// this._reader = null;
// }
//
// slice(start, end, contentType) {
// return new DenoFile(this._file, start ? start : this._start, end ? end : this._end);
// }
//
// stream() {
//
// const self = this;
//
// if (this._start !== undefined) {
// return new ReadableStream({
//
// async start(controller) {
// await self._file.seek(self._start, Deno.SeekMode.Start);
// self._reader = self._file.readable.getReader();
//
// },
//
// async pull(controller) {
// const { value, done } = await self._reader.read();
// if (done) {
// controller.close();
// }
// else {
// controller.enqueue(value);
// }
// }
// });
// }
// else {
// return this._file.readable;
// }
// }
//}
//
//class BunFile {
// constructor(blob) {
// this._blob = blob;
// this._reader = null;
// }
//
// get size() {
// return this._blob.size;
// }
//
// slice(start, end, contentType) {
// return new BunFile(this._blob.slice(start, end, contentType));
// //return new BunFile(this._blob, start ? start : this._start, end ? end : this._end);
// }
//
// stream() {
//
// const self = this;
//
// return new ReadableStream({
//
// async start(controller) {
// self._reader = self._blob.stream().getReader();
// },
//
// async pull(controller) {
// const { value, done } = await self._reader.read();
// if (done) {
// controller.close();
// }
// else {
// controller.enqueue(value);
// }
// }
// });
// }
//}
class BrowserDirectoryTree extends DirectoryTree {
constructor() {
const rootPath = '/';
super(rootPath);
this._files = {};
}
async openFile(path) {
if (this._files[path] !== undefined) {
return this._files[path];
}
else {
throw new Error("No such file: " + path);
}
}
addFiles(files) {
for (const file of files) {
this._files['/' + file.name] = file;
}
}
async selectFiles() {
const files = await new Promise((resolve, reject) => {
const fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
fileInput.setAttribute('hidden', '');
fileInput.setAttribute('multiple', '');
document.body.appendChild(fileInput);
fileInput.addEventListener('change', (evt) => {
resolve(fileInput.files);
document.body.removeChild(fileInput);
});
fileInput.click();
});
this.addFiles(files);
return files;
}
}
//class NodeDirectoryTree extends DirectoryTree {
// constructor(rootPath) {
// super(rootPath);
// }
//
// // TODO: CRITICAL: path security
// async openFile(path) {
// const nodePath = await import('path');
// const absPath = nodePath.join(this._rootPath, path);
// const fs = await import('fs');
// const f = await fs.promises.open(absPath);
// const fileInfo = await f.stat();
// return new NodeFile(f, 0, fileInfo.size);
// }
//}
//
//class DenoDirectoryTree extends DirectoryTree {
// constructor(rootPath) {
// super(rootPath);
// }
//
// async openFile(path) {
// const denoPath = await import("https://deno.land/[email protected]/path/mod.ts");
// const absPath = denoPath.join(this._rootPath, path);
// const f = await Deno.open(absPath, { read: true, write: false });
// const fileInfo = await f.stat();
// return new DenoFile(f, 0, fileInfo.size);
// }
//}
//
//class BunDirectoryTree extends DirectoryTree {
// constructor(rootPath) {
// super(rootPath);
// }
//
// async openFile(path) {
// const bunPath = await import('path');
// const absPath = bunPath.join(this._rootPath, path);
// const f = await Bun.file(absPath);
// // TODO: wrapping in a Response is a hack because Bun file blobs are
// // currently broken for range requests:
// // https://github.com/oven-sh/bun/issues/7057
// const blob = await new Response(f.stream()).blob();
// return blob;
// //return new BunFile(f);
// }
//}
async function openFile(path) {
switch (runtime) {
case RUNTIME_BROWSER: {
return new Promise((resolve, reject) => {
const fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
fileInput.setAttribute('hidden', '');
document.body.appendChild(fileInput);
fileInput.addEventListener('change', (evt) => {
resolve(fileInput.files[0]);
document.body.removeChild(fileInput);
});
fileInput.click();
});
break;
}
//case RUNTIME_NODE: {
// const fs = await import('fs');
// const f = await fs.promises.open(path);
// return new NodeFile(f);
// break;
//}
//case RUNTIME_DENO: {
// // TODO: need to close the files we're opening
// const f = await Deno.open(path);
// const fileInfo = await f.stat();
// return new DenoFile(f, 0, fileInfo.size);
// break;
//}
//case RUNTIME_BUN: {
// const f = Bun.file(path);
// //return new BunFile(f);
// return f;
// break;
//}
}
}
function openDirectory(path) {
switch (runtime) {
case RUNTIME_BROWSER: {
return new BrowserDirectoryTree();
break;
}
//case RUNTIME_NODE: {
// return new NodeDirectoryTree(path);
// break;
//}
//case RUNTIME_DENO: {
// return new DenoDirectoryTree(path);
// break;
//}
//case RUNTIME_BUN: {
// return new BunDirectoryTree(path);
// break;
//}
default: {
throw new Error("Runtime not implemented:" + runtime);
break;
}
}
}
function detectRuntime() {
let runtime = RUNTIME_BROWSER;
//if (typeof process !== 'undefined' && process.versions.bun !== undefined) {
// runtime = RUNTIME_BUN;
//}
//else if (isNode()) {
// runtime = RUNTIME_NODE;
//}
//else if (window.Deno !== undefined) {
// runtime = RUNTIME_DENO;
//}
return runtime;
}
//function isNode() {
// return (typeof process !== 'undefined' && process.release.name === 'node');
//}
export {
openFile,
openDirectory,
}