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
|
unit webfilecache;
{$mode objfpc}
// Enable this to write lots of debugging info to the browser console.
{$DEFINE VERBOSEWEBCACHE}
interface
uses
Classes, SysUtils, JS, Web, fpjson, pas2jsfs, pscanner, contnrs;
type
TPas2jsWebFS = Class;
{ TWebFileContent }
TWebFileContent = Class(TObject)
private
FContents: string;
FFileName: String;
FModified: Boolean;
procedure SetContents(AValue: string);
Public
Constructor Create(const aFileName,aContents : String);
Property FileName : String Read FFileName Write FFileName;
Property Contents : string Read FContents Write SetContents;
Property Modified : Boolean Read FModified;
end;
{ TWebFilesCache }
TWebFilesCache = Class(TObject)
Private
FFiles : TFPObjectHashTable;
Function FindFile(aFileName : String) : TWebFileContent;
Public
Constructor Create;
Destructor Destroy; override;
Function HasFile(aFileName : String) : Boolean;
Function GetFileContent(Const aFileName : String) : String;
function SetFileContent(const aFileName, aContent: String): Boolean;
end;
{ TPas2jsWebFile }
TPas2jsWebFile = Class(TPas2jsFile)
public
function CreateLineReader(RaiseOnError: boolean): TSourceLineReader; override;
function Load(RaiseOnError: boolean; Binary: boolean): boolean; override;
end;
{ TWebSourceLineReader }
TWebSourceLineReader = Class(TSourceLineReader)
private
FFS: TPas2jsFS;
Protected
Property FS : TPas2jsFS Read FFS;
Procedure IncLineNumber; override;
end;
// aFileName is the original filename, not normalized one
TLoadFileEvent = Reference to Procedure(Sender : TObject; aFileName : String; aError : string);
{ TLoadFileRequest }
TLoadFileRequest = Class(TObject)
FFS : TPas2jsWebFS;
FFileName : string;
FXML : TJSXMLHttpRequest;
FOnLoaded : TLoadFileEvent;
private
procedure DoChange;
Public
constructor Create(aFS: TPas2jsWebFS; const aFileName : string; aOnLoaded: TLoadFileEvent);
Procedure DoLoad(const aURL : String);
end;
{ TPas2jsWebFS }
TPas2jsWebFS = Class(TPas2jsFS)
Private
FCache : TWebFilesCache;
FLoadBaseURL: String;
FOnLoadedFile: TLoadFileEvent;
protected
// Only for names, no paths
Class Function NormalizeFileName(Const aFileName : String) : String;
function FindSourceFileName(const aFilename: string): String; override;
public
Constructor Create; override;
// Overrides
function CreateResolver: TPas2jsFSResolver; override;
function FileExists(const aFileName: String): Boolean; override;
function FindCustomJSFileName(const aFilename: string): String; override;
function FindIncludeFileName(const aFilename, ModuleDir: string): String; override;
function FindUnitFileName(const aUnitname, InFilename, ModuleDir: string; out IsForeign: boolean): String; override;
function FindUnitJSFileName(const aUnitFilename: string): String; override;
function LoadFile(Filename: string; Binary: boolean=false): TPas2jsFile; override;
procedure SaveToFile(ms: TFPJSStream; Filename: string); override;
Function SetFileContent(Const aFileName,aContents : String) : Boolean;
Function GetFileContent(Const aFileName : String) : String;
// Returns false if the file was already loaded. OnLoaded is called in either case.
Function LoadFile(aFileName : String; OnLoaded : TLoadFileEvent = Nil) : Boolean;
// Returns number of load requests. OnLoaded is called for each file in the list
Function LoadFiles(aList : TStrings;OnLoaded : TLoadFileEvent = Nil) : Integer;
Function LoadFiles(aList : array of String;OnLoaded : TLoadFileEvent = Nil) : integer;
Property OnLoadedFile : TLoadFileEvent Read FOnLoadedFile Write FOnLoadedFile;
Property LoadBaseURL : String Read FLoadBaseURL Write FLoadBaseURL;
end;
{ TPas2jsFileResolver }
{ TPas2jsWebResolver }
TPas2jsWebResolver = class(TPas2jsFSResolver)
private
function GetWebFS: TPas2jsWebFS;
public
Property WebFS : TPas2jsWebFS Read GetWebFS;
end;
implementation
{ TWebSourceLineReader }
procedure TWebSourceLineReader.IncLineNumber;
begin
if (FFS<>nil) then
FFS.IncReadLineCounter;
inherited IncLineNumber;
end;
{ TLoadFileRequest }
procedure TLoadFileRequest.DoChange;
Var
Err : String;
begin
Case FXML.readyState of
TJSXMLHttpRequest.UNSENT : ;
TJSXMLHttpRequest.OPENED : ;
TJSXMLHttpRequest.HEADERS_RECEIVED : ;
TJSXMLHttpRequest.LOADING : ;
TJSXMLHttpRequest.DONE :
begin
if (FXML.Status div 100)=2 then
begin
Err:='';
// FS will normalize filename
FFS.SetFileContent(FFileName,FXML.responsetext)
end
else
Err:='Error loading file: '+FXML.StatusText;
If Assigned(FOnLoaded) then
FOnLoaded(FFS,FFileName,Err);
if Assigned(FFS.OnLoadedFile) then
FFS.OnLoadedFile(FFS,FFileName,Err);
Free;
end;
end
end;
constructor TLoadFileRequest.Create(aFS: TPas2jsWebFS; const aFileName : string; aOnLoaded: TLoadFileEvent);
begin
FFS:=aFS;
FOnLoaded:=aOnLoaded;
FFileName:=aFileName;
end;
Procedure TLoadFileRequest.DoLoad(const aURL: String);
begin
FXML:=TJSXMLHttpRequest.new;
FXML.onreadystatechange:=@DoChange;
// Maybe one day allow do this sync, so the compiler can load files on demand.
FXML.Open('GET',aURL);
FXML.Send;
end;
{ TPas2jsWebFile }
function TPas2jsWebFile.CreateLineReader(RaiseOnError: boolean): TSourceLineReader;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': Creating line reader for ',FileName);
{$ENDIF}
if Load(RaiseOnError,False) then
begin
Result:=TWebSourceLineReader.Create(FileName,Source);
TWebSourceLineReader(Result).FFS:=Self.FS;
end
else
Result:=Nil;
end;
function TPas2jsWebFile.Load(RaiseOnError: boolean; Binary: boolean): boolean;
begin
Result:=False;
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': Loading for ',FileName);
{$ENDIF}
With (FS as TPas2jsWebFS).FCache do
if HasFile(FileName) then
begin
SetSource(GetFileContent(FileName));
Result:=True;
end;
if Not Result then
if RaiseOnError then
Raise EFileNotFoundError.Create('File not loaded '+FileName)
{$IFDEF VERBOSEWEBCACHE}
else Writeln('File not loaded '+FileName);
{$ENDIF}
end;
{ TWebFilesCache }
function TWebFilesCache.FindFile(aFileName: String): TWebFileContent;
Var
N : THTCustomNode;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': Looking for file : ',aFileName);
{$ENDIF}
N:=FFiles.Find(aFileName);
if N=Nil then
result:=Nil
else
Result:=TWebFileContent(THTObjectNode(N).Data);
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': Looking for file : ',aFileName, ': ',Assigned(Result));
{$ENDIF}
end;
constructor TWebFilesCache.Create;
begin
FFiles:=TFPObjectHashTable.Create(True);
end;
destructor TWebFilesCache.Destroy;
begin
FreeAndNil(FFiles);
inherited Destroy;
end;
function TWebFilesCache.HasFile(aFileName: String): Boolean;
begin
Result:=FindFile(aFileName)<>Nil;
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': HasFile(',aFileName,') : ',Result);
{$ENDIF}
end;
function TWebFilesCache.GetFileContent(const aFileName: String): String;
Var
W : TWebFileContent;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': GetFileContent(',aFileName,')');
{$ENDIF}
W:=FindFile(aFileName);
if Assigned(W) then
Result:=W.Contents
else
Raise EFileNotFoundError.Create('No such file '+AFileName);
end;
function TWebFilesCache.SetFileContent(const aFileName, aContent: String) : Boolean;
Var
W : TWebFileContent;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': SetFileContent(',aFileName,')');
{$ENDIF}
W:=FindFile(aFileName);
Result:=Assigned(W);
if Result then
W.Contents:=aContent
else
FFiles.Add(aFileName,TWebFileContent.Create(aFileName,aContent));
end;
{ TWebFileContent }
procedure TWebFileContent.SetContents(AValue: string);
begin
if FContents=AValue then Exit;
FContents:=AValue;
FModified:=True;
end;
constructor TWebFileContent.Create(const aFileName, aContents: String);
begin
FContents:=aContents;
FFileName:=aFileName;
end;
{ TPas2jsWebFS }
function TPas2jsWebFS.FileExists(const aFileName: String): Boolean;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FileExists(',aFileName,')');
{$ENDIF}
Result:=FCache.HasFile(NormalizeFileName(aFileName));
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FileExists(',aFileName,') : ',Result);
{$ENDIF}
end;
function TPas2jsWebFS.FindCustomJSFileName(const aFilename: string): String;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindCustomJSFileName(',aFileName,')');
{$ENDIF}
Result:=NormalizeFileName(aFileName);
If not FCache.HasFile(Result) then
Result:='';
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindCustomJSFileName(',aFileName,'): ',Result);
{$ENDIF}
end;
function TPas2jsWebFS.FindIncludeFileName(const aFilename, ModuleDir: string
): String;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindIncludeFileName(',aFileName,',',ModuleDir,')');
{$ENDIF}
Result:=NormalizeFileName(aFileName);
If not FCache.HasFile(Result) then
Result:='';
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindIncludeFileName(',aFileName,') : ',Result);
{$ENDIF}
end;
class function TPas2jsWebFS.NormalizeFileName(const aFileName: String): String;
begin
Result:=LowerCase(ExtractFileName(aFileName));
end;
function TPas2jsWebFS.FindSourceFileName(const aFilename: string): String;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindSourceFileName(',aFileName,')');
{$ENDIF}
Result:=NormalizeFileName(aFileName);
If not FCache.HasFile(Result) then
Result:='';
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindSourceFileName(',aFileName,') : ',Result);
{$ENDIF}
end;
constructor TPas2jsWebFS.Create;
begin
inherited Create;
FCache:=TWebFilesCache.Create;
end;
function TPas2jsWebFS.CreateResolver: TPas2jsFSResolver;
begin
Result:=TPas2jsWebResolver.Create(Self);
end;
function TPas2jsWebFS.FindUnitFileName(const aUnitname, InFilename, ModuleDir: string; out IsForeign: boolean): String;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindUnitFileName(',aUnitName,',',InFilename,',',ModuleDir,')');
{$ENDIF}
Result:=NormalizeFileName(aUnitName+'.pas');
isForeign:=False;
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindUnitFileName(',aUnitName,') : ',Result);
{$ENDIF}
end;
function TPas2jsWebFS.FindUnitJSFileName(const aUnitFilename: string): String;
begin
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindUnitJSFileName(',aUnitFileName,')');
{$ENDIF}
Result:=NormalizeFileName(aUnitFileName);
{$IFDEF VERBOSEWEBCACHE}
Writeln(ClassName,': FindUnitJSFileName(',aUnitFileName,') : ',Result);
{$ENDIF}
end;
function TPas2jsWebFS.LoadFile(Filename: string; Binary: boolean): TPas2jsFile;
begin
Result:=TPas2jsWebFile.Create(Self,FileName);
Result.Load(True,False);
end;
(*
// Check if we should not be using this instead, as the compiler outputs UTF8 ?
// Found on
// https://weblog.rogueamoeba.com/2017/02/27/javascript-correctly-converting-a-byte-array-to-a-utf-8-string/
function stringFromUTF8Array(data)
{
const extraByteMap = [ 1, 1, 1, 1, 2, 2, 3, 0 ];
var count = data.length;
var str = "";
for (var index = 0;index < count;)
{
var ch = data[index++];
if (ch & 0x80)
{
var extra = extraByteMap[(ch >> 3) & 0x07];
if (!(ch & 0x40) || !extra || ((index + extra) > count))
return null;
ch = ch & (0x3F >> extra);
for (;extra > 0;extra -= 1)
{
var chx = data[index++];
if ((chx & 0xC0) != 0x80)
return null;
ch = (ch << 6) | (chx & 0x3F);
}
}
str += String.fromCharCode(ch);
}
return str;
}
*)
procedure TPas2jsWebFS.SaveToFile(ms: TFPJSStream; Filename: string);
Var
aContent : String;
i : Integer;
v : JSValue;
begin
aContent:='';
for I:=0 to MS.Length-1 do
begin
v:=MS[i];
{AllowWriteln}
Writeln('Char ',i,'(',v,') : ',TJSString.fromCharCode(v));
{AllowWriteln-}
aContent:=aContent+TJSString.fromCharCode(MS[i]);
end;
SetFileContent(FileName,aContent);
end;
function TPas2jsWebFS.SetFileContent(const aFileName, aContents: String): Boolean;
begin
Result:=FCache.SetFileContent(NormalizeFileName(aFileName),aContents);
end;
function TPas2jsWebFS.GetFileContent(const aFileName: String): String;
begin
Result:=FCache.GetFileContent(NormalizeFileName(aFileName));
end;
function TPas2jsWebFS.LoadFile(aFileName: String; OnLoaded: TLoadFileEvent): Boolean;
Var
FN : String;
aURL : String;
LF : TLoadFileRequest;
begin
FN:=NormalizeFileName(aFileName);
Result:=Not FCache.HasFile(FN);
if Not result then
begin
// It is already loaded
if Assigned(OnLoaded) then
OnLoaded(Self,aFileName,'')
end
else
begin
// Not yet already loaded
aURL:=IncludeTrailingPathDelimiter(LoadBaseURL)+FN;
LF:=TLoadFileRequest.Create(Self,aFileName,OnLoaded);
LF.DoLoad(aURL);
end;
end;
function TPas2jsWebFS.LoadFiles(aList: TStrings; OnLoaded: TLoadFileEvent
): Integer;
Var
i: Integer;
begin
Result:=0;
For I:=0 to aList.Count-1 do
if LoadFile(aList[i],OnLoaded) then
Inc(Result);
end;
function TPas2jsWebFS.LoadFiles(aList: array of String; OnLoaded: TLoadFileEvent
): integer;
Var
i: Integer;
begin
Result:=0;
For I:=0 to Length(aList)-1 do
if LoadFile(aList[i],OnLoaded) then
Inc(Result);
end;
{ TPas2jsWebResolver }
function TPas2jsWebResolver.GetWebFS: TPas2jsWebFS;
begin
Result:=TPas2jsWebFS(FS)
end;
end.
|