-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileSystem.js
567 lines (485 loc) · 15.2 KB
/
FileSystem.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import PopImage from './PopWebImageApi.js'
import FileCache_t from './FileCache.js'
import PromiseQueue from './PromiseQueue.js'
import {Debug,Warning,CreatePromise,Yield} from './PopWebApiCore.js'
import {IsObjectInstanceOf,ParseExeArguments} from './PopApi.js'
const Default = 'FileSystem.js Module';
export default Default;
// file cache, not asset cache!
// rework this system so we have an async version on desktop too
export const FileCache = new FileCache_t();
// we're interpreting the url as
// http://exefilename/exedirectory/?exearguments
export function GetExeFilename()
{
return window.location.hostname;
}
export function GetExeDirectory()
{
// exe could be path location.pathname
const Path = window.location.pathname;
// including /
const Directory = Path.substr( 0, Path.lastIndexOf("/") + 1 );
return Directory;
}
export function GetExeArguments()
{
// gr: probably shouldn't lowercase now it's proper
const UrlArgs = window.location.search.replace('?',' ').trim().split('&');
return ParseExeArguments(UrlArgs);
}
// push a file into the file system
export function SetFileCache(Filename,Contents)
{
FileCache.Set(Filename,Contents);
}
// gr: if we call fetch() 100 times for the same url, we make 100 requests
// quick fix, have a cache of pending fetch() requests
// gr: we cannot consume the result (.text or .arrayBuffer) more than once
// so inside this caching, we need to do the read too, hence extra funcs
const FetchCache = {};
// AbortController is undefined on firefox browser on hololens2
class AbortControllerStub
{
constructor()
{
this.signal = null;
}
abort()
{
}
};
window.AbortController = window.AbortController || AbortControllerStub;
// gr: hack for kandinsky;
// if any fetch's fail
let InternetStatus = true; // we can pretty safely assume it's initially fine
const InternetStatusChangedQueue = new PromiseQueue('InternetStatusChangedQueue');
export async function WaitForInternetStatusChange()
{
// wait for a change (dirty) and then return latest status
await InternetStatusChangedQueue.WaitForNext();
return InternetStatus;
}
// call this when any kind of download gets new information
function OnInternetGood()
{
InternetStatus = true;
InternetStatusChangedQueue.PushUnique();
}
// call when any fetch fails (not due to 404 or anything with a response)
function OnInternetBad()
{
InternetStatus = false;
InternetStatusChangedQueue.PushUnique();
}
async function CreateFetch(Url)
{
// gr: check for not a string?
if ( Url === undefined )
throw `Trying to fetch() undefined url`;
// attach a Cancel() function
// gr: work out when not supported
const Controller = new window.AbortController();
const Signal = Controller.signal;
function Cancel()
{
Controller.abort();
}
const Params = {};
//method: 'get',
Params.signal = Signal;
// fetch() throws when disconnected, catch it
let Fetched = null;
try
{
Fetched = await fetch(Url,Params);
if (!Fetched.ok)
throw `fetch result not ok, status=${Fetched.statusText}`;
OnInternetGood();
}
catch(e)
{
// gr; need to check for 404 here
OnInternetBad();
throw `Fetch error with ${Url}; ${e}`;
}
Fetched.Cancel = Cancel;
return Fetched;
}
async function FetchText(Url)
{
const Fetched = await CreateFetch(Url);
const Contents = await Fetched.text();
return Contents;
}
async function FetchArrayBuffer(Url)
{
const Fetched = await CreateFetch(Url);
const Contents = await Fetched.arrayBuffer();
const Contents8 = new Uint8Array(Contents);
return Contents8;
}
export async function FetchArrayBufferStream(Url,OnProgress)
{
const Fetched = await CreateFetch(Url);
// gr: do we know full file size here
Debug(`Streaming file; `,Fetched);
let KnownSize = parseInt(Fetched.headers.get("content-length"));
KnownSize = isNaN(KnownSize) ? -1 : KnownSize;
const KnownSizeKb = (KnownSize/1024).toFixed(2);
// gr: maybe future speed up with our own buffer
// https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader but currently 0 support
const Reader = Fetched.body.getReader();
async function ReaderThread()
{
Debug(`Reading fetch stream ${Url}/${KnownSizeKb}kb`);
// it's slow to keep merging chunks and notifying changes
// so push chunks to the file cache,
// the file cache can then merge them on demand (which will be
// far less frequent than this read)
let ContentChunks = [];
// gr: this function is expensive, especially when called often
// we should keep an array of chunks, and merge on demand (or at the end)
function AppendChunk(Chunk)
{
// last is undefined
if ( !Chunk )
return;
ContentChunks.push(Chunk);
OnProgress( ContentChunks, KnownSize );
}
while (true)
{
/*
// gr: testing to see if we can pause the fetch by not read()ing
if (TotalContents.length > 1024 * 500)
{
Debug(`Stopping stream ${Filename} at ${TotalContents.length / 1024}kb`);
// both of these stop network streaming
Reader.cancel();
Fetched.Cancel();
return TotalContents;
}
*/
const Chunk = await Reader.read();
OnInternetGood();
const Finished = Chunk.done;
const ChunkContents = Chunk.value;
// chunk is undefined on last (finished)read
const ChunkSize = ChunkContents ? ChunkContents.length : 0;
//Debug(`chunk ${Url} Finished=${Finished} x${ChunkSize}/${KnownSizeKb}`,Chunk);
AppendChunk(ChunkContents);
if ( Finished )
break;
}
// do a final join. OnProgress should have done this in the file cache
// so this array may be a bit redundant (and a duplicate!)
// so try and fetch the other one, but for now, keep it here to make sure
// the old way of expecting a complete buffer is here
// gr: we now only auto resolve chunks on request
//const TotalContents = JoinTypedArrays(...ContentChunks);
//return TotalContents;
return true;
}
try
{
const Contents8 = await ReaderThread();
return Contents8;
}
catch(e)
{
Warning(`Reader thread error; ${e}`);
OnInternetBad();
throw e;
}
}
async function FetchOnce(Url,FetchFunc,OnProgress)
{
if ( FetchCache.hasOwnProperty(Url) )
return FetchCache[Url];
// run the fetch, wait for it to finish, then clear the cache
try
{
FetchCache[Url] = FetchFunc(Url,OnProgress);
const Contents = await FetchCache[Url];
delete FetchCache[Url];
return Contents;
}
catch(e)
{
// gr: to make the app retry (because of the internet-bad stuff)
// delete the fetch cache
// the point of this was originally to stop multiple fetch()s
// previously, if it failed, we ended up with a dangling [rejected] fetch cache
// gr: to avoid CPU hammering, we delay this so if something is trying to fetch
// every frame, we don't constantly fetch & fail
// the downside is we POSSIBLY start a successfull one here and this fetch cache
// gets deleted (can that happen? there's a check before... maybe in multithreaded app it would happen)
await Yield(1000);
delete FetchCache[Url];
throw e;
}
}
// gr: this needs a fix like FetchOnce
export async function LoadFileAsImageAsync(Filename)
{
// return cache if availible, if it failed before, try and load again
const Cache = FileCache.GetOrFalse(Filename);
if ( Cache !== false )
{
if ( IsObjectInstanceOf(Cache,PopImage) )
return Cache;
Warning(`Converting cache from ${typeof Cache} to Pop.Image...`);
const CacheImage = await new PopImage();
CacheImage.LoadPng(Cache);
FileCache.Set(Filename,CacheImage);
return CacheImage;
}
function LoadHtmlImageAsync()
{
let Promise = CreatePromise();
const HtmlImage = new Image();
HtmlImage.onload = function ()
{
Promise.Resolve(HtmlImage);
};
HtmlImage.addEventListener('load', HtmlImage.onload, false);
HtmlImage.onerror = function (Error)
{
Promise.Reject(Error);
}
HtmlImage.crossOrigin = "anonymous";
// trigger load
HtmlImage.src = '';
HtmlImage.src = Filename;
return Promise;
}
// the API expects to return an image, so wait for the load,
// then make an image. This change will have broken the Pop.Image(Filename)
// constructor as it uses the asset cache, which is only set after this
const HtmlImage = await LoadHtmlImageAsync();
const Img = new PopImage(HtmlImage);
FileCache.Set(Filename,Img);
return Img;
}
export async function LoadFileAsStringAsync(Filename)
{
// return cache if availible, if it failed before, try and load again
const Cache = FileCache.GetOrFalse(Filename);
if ( Cache !== false )
{
// convert cache if its not a string. Remote system may deliver raw binary file
// and we don't know the type until it's requested
if ( typeof Cache == 'string' )
return Cache;
const CacheString = BytesToString(Cache);
FileCache.Set(Filename,CacheString);
return CacheString;
}
const Contents = await FetchOnce(Filename,FetchText);
FileCache.Set(Filename,Contents);
return Contents;
}
export async function LoadFileAsJsonAsync(Filename)
{
const String = await LoadFileAsStringAsync(Filename);
const Json = JSON.parse(String);
return Json;
}
export async function LoadFileAsArrayBufferAsync(Filename)
{
// return cache if availible, if it failed before, try and load again
const Cache = FileCache.GetOrFalse(Filename);
if ( Cache !== false )
return Cache;
const Contents = await FetchOnce(Filename,FetchArrayBuffer);
FileCache.Set(Filename,Contents);
return Contents;
}
export async function LoadFileAsArrayBufferStreamAsync(Filename,ResolveChunks=true,OnNewChunk)
{
// return cache if availible, if it failed before, try and load again
const Cache = FileCache.GetOrFalse(Filename,ResolveChunks);
if (Cache !== false)
{
if ( OnNewChunk )
OnNewChunk(Cache);
return Cache;
}
let NextChunkIndex = 0;
function OnStreamProgress(Contents,TotalSize)
{
// set meta of known size if we have it, so we can work out %
if ( TotalSize )
FileCache.SetKnownSize(Filename,TotalSize);
// keep re-writing a new file
FileCache.Set(Filename,null,Contents);
// callback with new chunks
if ( OnNewChunk )
{
if ( Array.isArray(Contents) )
{
while( NextChunkIndex < Contents.length )
{
OnNewChunk( Contents[NextChunkIndex] );
NextChunkIndex++;
}
}
else
{
Pop.Warning(`Expecting contents to be chunked into an array. May conflict with resolve chunks option(=${ResolveChunks})`);
}
}
}
const Contents = await FetchOnce(Filename,FetchArrayBufferStream,OnStreamProgress);
if ( Contents !== true )
throw `FetchArrayBufferStream() should now return only true, to avoid auto resolving chunks`;
//FileCache.Set(Filename,Contents);
//return Contents;
return FileCache.GetOrFalse(Filename,ResolveChunks);
}
export function LoadFileAsString(Filename)
{
// synchronous functions on web will fail
if (!FileCache.IsCached(Filename))
{
throw "Cannot synchronously load " + Filename + ", needs to be precached first with [async] Pop.AsyncCacheAsset()";
}
// gr: our asset loader currently replaces the contents of this
// with binary, so do the conversion here (as native engine does)
const Contents = FileCache.Get(Filename);
if ( typeof Contents == 'string' )
return Contents;
// convert array buffer to string
if ( Array.isArray( Contents ) || Contents instanceof Uint8Array )
{
// Pop.Debug("Convert "+Filename+" from ", typeof Contents," to string");
// this is super slow!
const ContentsString = Pop.BytesToString( Contents );
return ContentsString;
}
throw "Pop.LoadFileAsString("+Filename+") failed as contents is type " + (typeof Contents) + " and needs converting";
}
export function LoadFileAsImage(Filename)
{
// synchronous functions on web will fail
if (!FileCache.IsCached(Filename))
{
throw "Cannot synchronously load " + Filename + ", needs to be precached first with [async] Pop.AsyncCacheAsset()";
}
return FileCache.Get(Filename);
}
export function LoadFileAsArrayBuffer(Filename,ResolveChunks=true)
{
// synchronous functions on web will fail
if (!FileCache.IsCached(Filename))
{
throw "Cannot synchronously load " + Filename + ", needs to be precached first with [async] Pop.AsyncCacheAsset()";
}
// gr: our asset loader currently replaces the contents of this
// with binary, so do the conversion here (as native engine does)
const Contents = FileCache.Get(Filename,ResolveChunks);
return Contents;
}
export async function WriteToFileAsync(Filename,Contents,Append=false)
{
return WriteToFile(...arguments);
}
// on web, this call causes a Save As... dialog to appear to save the contents
export function WriteToFile(Filename,Contents,Append=false)
{
if ( Append )
throw `WriteToFile cannot append on web`;
let MimePrefix;
if ( typeof Contents == 'string' )
{
MimePrefix = "text/plain;charset=utf-8";
}
else
{
//'application/json'
}
// on web (chrome?)
// folder/folder/file.txt
// turns in folder_folder_file.txt, so clip the name
const DownloadFilename = Filename.split('/').slice(-1)[0];
// gr: "not a sequence" error means the contents need to be an array
const Options = {};
if ( MimePrefix )
Options.type = MimePrefix;
const ContentsBlob = new Blob([Contents],Options);
const DataUrl = URL.createObjectURL(ContentsBlob);
Debug(`WriteFile blob url: ${DataUrl}`);
// make a temp element to invoke the download
const a = window.document.createElement('a');
function Cleanup()
{
document.body.removeChild(a);
// delete seems okay here
URL.revokeObjectURL(ContentsBlob);
}
try
{
a.href = DataUrl;
a.download = DownloadFilename;
// gr: trying to get callback when this was succesfull or failed
//a.ping = "data:text/html,<script>alert('hi');</script>";
//a.onerror = function(e){ Debug(`link error ${e}`); }
document.body.appendChild(a);
a.click(); // returns nothing
Cleanup();
}
catch (e)
{
Cleanup();
throw e;
}
}
export const WriteStringToFile = WriteToFile;
async function LoadFilePromptAsFileAsync(Filename)
{
const OnChangedPromise = CreatePromise();
const InputElement = window.document.createElement('input');
InputElement.setAttribute('type','file');
//InputElement.multiple = true;
InputElement.setAttribute('accept','Any/*');
function OnFilesChanged(Event)
{
// extract files from the control
const Files = Array.from(InputElement.files);
Debug(`OnChanged: ${JSON.stringify(Files)}`);
OnChangedPromise.Resolve(Files);
InputElement.files = null;
}
//InputElement.addEventListener('input',OnFilesChanged,false);
InputElement.addEventListener('change',OnFilesChanged,false);
InputElement.click();
const Files = await OnChangedPromise;
if (!Files.length)
throw `User selected no files`;
// read file contents
// currently only interested in first
const File = Files[0];
return File;
}
export async function LoadFilePromptAsStringAsync(Filename)
{
const File = await LoadFilePromptAsFileAsync(Filename);
const Contents = await File.text();
return Contents;
}
export async function LoadFilePromptAsArrayBufferAsync(Filename)
{
const File = await LoadFilePromptAsFileAsync(Filename);
const Contents = await File.arrayBuffer();
const Mime = File.type;
return Contents;
}
// on web, this is a "can I synchronously load file" check
// we may need to alter this to allow currently-downloading files
// which haven't yet been cached, but not those that have started
// a fetch() but currently have no knowledge of sucess or not
export function FileExists(Filename)
{
return FileCache.IsCached(Filename);
}