-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsw.js
More file actions
947 lines (821 loc) · 27.5 KB
/
sw.js
File metadata and controls
947 lines (821 loc) · 27.5 KB
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
const APP_SHELL_FILES = ["./", "./index.html", "./main.min.js", "sw.min.js"]; // If you're using .php or some other configuration, make sure to change this!
const NETWORK_ALLOWLIST_PREFIXES = []; // For custom bypassing of the virtual file system (not used by default)
// Old SpiderMonkey check, because Firefox previously had weird SW header issues. ?boot=1 is used to ensure the context is "clean" and controlled because Firefox acted strangely with SWs in older versions; use `typeof InternalError !== "undefined"` to re-enable with Firefox.
let reloadOnRequest = false;
let pendingNavData = null; // for next navigation request
const clientSessionStore = new Map();
const handleCache = new Map();
const manifestCache = new Map();
const compiledHeaderCache = new Map();
const ruleCache = new Map();
const MAX_REGEX_SIZE = 10 * 1024 * 1024; // 10 MiB default
const dirHandleCache = new Map(); // Cache for directory handles
const MAX_DIR_CACHE_SIZE = 1000; // Prevent memory leaks
function addToCache(map, key, value) {
if (map.size >= MAX_DIR_CACHE_SIZE) map.delete(map.keys().next().value); // Simple LRU
map.set(key, value);
}
const RFS_PREFIX = "rfs";
const SYSTEM_FILE = "rfs_system.json";
const CACHE_NAME = "fc";
const FULL_APP_SHELL_URLS = APP_SHELL_FILES.map(
(file) => new URL(file, self.location.href).href,
);
const STORE_ENTRY_TTL = 600000;
const basePath = new URL("./", self.location).pathname;
const virtualPathPrefix = basePath + "n/";
// A big ol' list of common mime types. Customize if needed.
function getMimeType(filePath) {
const ext = filePath.split(".").pop().toLowerCase();
const mimeTypes = {
html: "text/html",
htm: "text/html",
css: "text/css",
js: "text/javascript",
mjs: "text/javascript",
cjs: "text/javascript",
jsx: "text/javascript",
ts: "text/javascript",
tsx: "text/javascript",
json: "application/json",
jsonld: "application/ld+json",
xml: "application/xml",
svg: "image/svg+xml",
txt: "text/plain",
md: "text/markdown",
csv: "text/csv",
webmanifest: "application/manifest+json",
png: "image/png",
jpg: "image/jpeg",
jpeg: "image/jpeg",
gif: "image/gif",
webp: "image/webp",
ico: "image/x-icon",
avif: "image/avif",
bmp: "image/bmp",
woff: "font/woff",
woff2: "font/woff2",
ttf: "font/ttf",
otf: "font/otf",
eot: "application/vnd.ms-fontobject",
mp3: "audio/mpeg",
wav: "audio/wav",
ogg: "audio/ogg",
m4a: "audio/mp4",
mp4: "video/mp4",
webm: "video/webm",
ogv: "video/ogg",
vtt: "text/vtt",
wasm: "application/wasm",
pdf: "application/pdf",
zip: "application/zip",
rar: "application/x-rar-compressed",
"7z": "application/x-7z-compressed",
tar: "application/x-tar",
gz: "application/gzip",
bin: "application/octet-stream",
dat: "application/octet-stream",
};
return mimeTypes[ext] || "text/plain";
}
let registryCache = null;
let _opfsRoot = null;
async function getOpfsRoot() {
if (!_opfsRoot) _opfsRoot = await navigator.storage.getDirectory();
return _opfsRoot;
}
function cleanupExpiredStores() {
const now = Date.now();
for (const [clientId, sessionData] of clientSessionStore.entries()) {
if (now - sessionData.timestamp > STORE_ENTRY_TTL) {
clientSessionStore.delete(clientId);
}
}
}
setInterval(cleanupExpiredStores, 60000);
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function compileRules(rulesString) {
if (!rulesString || !rulesString.trim()) return [];
if (ruleCache.has(rulesString)) return ruleCache.get(rulesString);
const compiled = [];
const lines = rulesString.trim().split(/\r?\n/);
for (const line of lines) {
const parts = line.split("->");
if (parts.length < 2) continue;
const matchPart = parts[0].trim();
let replacePart = parts.slice(1).join("->").trim();
// Parse the operator (like | or $$)
const operatorMatch = matchPart.match(
/^(.*?)\s+(\$|\$\$|\|\||\|)\s+(.*)$/s,
);
if (!operatorMatch) continue;
// funy JS trick
const [, fileMatch, operator, searchPattern] = operatorMatch;
try {
const fileRegex = new RegExp(
fileMatch.trim() === "*" ? ".*" : fileMatch.trim(),
);
let searchRegex;
if (searchPattern === "{{SCRIPT}}") {
searchRegex =
/(?:<!DOCTYPE\b[^>]*>)|(?:<head\b[^>]*>)|(?:<body\b[^>]*>)|(?:<html\b[^>]*>)|(?:^)/i;
replacePart = `$&<script>${replacePart}</script>`;
} else {
switch (operator) {
case "|":
searchRegex = new RegExp(searchPattern, "g");
break;
case "$":
searchRegex = new RegExp(escapeRegex(searchPattern), "g");
break;
case "||":
searchRegex = new RegExp(searchPattern);
break;
case "$$":
searchRegex = new RegExp(escapeRegex(searchPattern));
break;
}
}
if (searchRegex && fileRegex) {
compiled.push({ fileRegex, searchRegex, replacePart });
}
} catch (err) {
console.warn("Rule compilation failed for line " + line + ":", err);
continue;
}
}
if (ruleCache.size > 50) ruleCache.clear();
ruleCache.set(rulesString, compiled);
return compiled;
}
function applyRegexRules(filePath, fileBuffer, fileType, compiledRules) {
if (
!/^(text\/|application\/(javascript|json|xml|x-javascript|typescript))/.test(
fileType,
)
)
return fileBuffer;
if (fileBuffer.byteLength > MAX_REGEX_SIZE) return fileBuffer;
try {
let content = new TextDecoder().decode(fileBuffer);
let modified = false;
for (const rule of compiledRules) {
if (rule.fileRegex.test(filePath)) {
const newContent = content.replace(rule.searchRegex, rule.replacePart);
if (newContent !== content) {
content = newContent;
modified = true;
}
}
}
return modified ? new TextEncoder().encode(content).buffer : fileBuffer;
} catch (err) {
console.error(`Error applying regex rules to ${filePath}:`, err);
return fileBuffer;
}
}
async function getRegistry() {
if (registryCache) return registryCache;
try {
const root = await getOpfsRoot();
const handle = await root.getFileHandle(SYSTEM_FILE);
const file = await handle.getFile();
registryCache = JSON.parse(await file.text());
} catch (err) {
registryCache = {};
}
return registryCache;
}
async function getCachedFileHandle(root, folderName, relativePath) {
const fileCacheKey = `${folderName}|${relativePath}`;
if (handleCache.has(fileCacheKey)) return handleCache.get(fileCacheKey);
try {
const pathParts = relativePath
.split("/")
.filter((p) => p && p.trim() !== "");
const fileName = decodeURIComponent(pathParts.pop());
let currentDirHandle;
let currentPathKey = folderName; // Start at the folder root
if (dirHandleCache.has(currentPathKey)) {
currentDirHandle = dirHandleCache.get(currentPathKey);
} else {
// Cold start: Get RFS root to folder root
const rfsHandle = await root.getDirectoryHandle(RFS_PREFIX);
currentDirHandle = await rfsHandle.getDirectoryHandle(folderName);
addToCache(dirHandleCache, currentPathKey, currentDirHandle);
}
// Go through subdirectories using cache
for (const part of pathParts) {
const decodedPart = decodeURIComponent(part);
currentPathKey += `/${decodedPart}`;
if (dirHandleCache.has(currentPathKey)) {
currentDirHandle = dirHandleCache.get(currentPathKey);
} else {
currentDirHandle =
await currentDirHandle.getDirectoryHandle(decodedPart);
addToCache(dirHandleCache, currentPathKey, currentDirHandle);
}
}
const fileHandle = await currentDirHandle.getFileHandle(fileName);
addToCache(handleCache, fileCacheKey, fileHandle);
return fileHandle;
} catch (err) {
return null;
}
}
self.addEventListener("install", async function installCache() {
const cache = await caches.open(CACHE_NAME);
await Promise.all(
APP_SHELL_FILES.map(async (url) => {
try {
const response = await fetch(url, { cache: "reload" });
if (response.ok) await cache.put(url, response);
} catch (err) {
console.warn("Failed to cache app shell file:", url);
}
}),
);
await self.skipWaiting();
});
self.addEventListener("activate", (e) => {
e.waitUntil(
(async function () {
await self.clients.claim();
registryCache = null;
try {
await getOpfsRoot();
} catch (err) {}
const allClients = await self.clients.matchAll({
includeUncontrolled: true,
});
for (const client of allClients) client.postMessage({ type: "SW_READY" });
})(),
);
});
self.addEventListener("message", (e) => {
if (!e.data) return;
const clientId = e.source ? e.source.id : null;
switch (e.data.type) {
case "SET_RULES":
const { rules, headers, key, folderName } = e.data;
const compiledRules = compileRules(rules);
const compiledHeaders = parseCustomHeaders(headers);
// Update the global cache so cold navigations are immediately correct
compiledHeaderCache.set(folderName, {
raw: headers,
compiled: compiledHeaders,
});
if (!pendingNavData) pendingNavData = {};
pendingNavData[folderName] = {
rules,
compiledRules,
headers, // Keep raw string just in case
compiledHeaders, // Store compiled version
key,
timestamp: Date.now(),
};
if (clientId) {
const s = clientSessionStore.get(clientId) || {};
s.rules = rules;
s.compiledRules = compiledRules;
s.headers = headers;
s.compiledHeaders = compiledHeaders; // Store here too
if (key) s.key = key;
clientSessionStore.set(clientId, s);
}
if (e.ports && e.ports[0]) e.ports[0].postMessage("OK");
setTimeout(() => {
if (pendingNavData && pendingNavData[folderName]) {
if (Date.now() - pendingNavData[folderName].timestamp > 5000) {
delete pendingNavData[folderName];
}
}
}, 6000);
break;
case "INVALIDATE_CACHE":
registryCache = null;
handleCache.clear();
manifestCache.clear();
dirHandleCache.clear();
e.waitUntil(
(async function () {
const allClients = await self.clients.matchAll({
includeUncontrolled: true,
});
for (const client of allClients) {
if (client.id !== clientId)
client.postMessage({
type: "INVALIDATE_CACHE",
folderName: e.data.folderName,
});
}
})(),
);
break;
case "PREPARE_FOR_IMPORT":
registryCache = null;
if (e.source) e.source.postMessage({ type: "IMPORT_READY" });
break;
}
});
function parseCustomHeaders(rulesString) {
if (!rulesString || !rulesString.trim()) return [];
const rules = [];
rulesString
.trim()
.split("\n")
.forEach((line) => {
line = line.trim();
if (line.length === 0 || line.startsWith("#")) return;
let separator = "";
let isRawRegex = false;
const rawIndex = line.indexOf("-->"); // regex
const globIndex = line.indexOf("->"); // escaped
if (rawIndex !== -1) {
separator = "-->";
isRawRegex = true;
} else if (globIndex !== -1) {
separator = "->";
isRawRegex = false;
} else {
return;
}
const sepIndex = line.indexOf(separator);
const globPart = line.substring(0, sepIndex).trim();
const fullHeaderString = line
.substring(sepIndex + separator.length)
.trim();
// Split only at the first occurrence of the colon
const colonIndex = fullHeaderString.indexOf(":");
if (colonIndex === -1) return;
const headerName = fullHeaderString.substring(0, colonIndex).trim();
const headerValue = fullHeaderString
.substring(colonIndex + 1)
.trim()
.replace(/^['"]|['"]$/g, "");
try {
const pattern = isRawRegex
? globPart
: "^" + globPart.replace(/\./g, "\\.").replace(/\*/g, ".*") + "$";
rules.push({
regex: new RegExp(pattern),
header: headerName,
value: headerValue,
});
} catch (err) {
console.error("Invalid pattern on line:", line);
}
});
return rules;
}
function applyCustomHeaders(baseHeaders, filePath, headerRulesArray) {
if (!headerRulesArray || !Array.isArray(headerRulesArray)) return baseHeaders;
for (const rule of headerRulesArray) {
if (rule.regex.test(filePath)) {
baseHeaders[rule.header] = rule.value;
}
}
return baseHeaders;
}
function getCompiledHeaders(folderName, rawHeaders) {
// If in-memory and the raw string hasn't changed, return it
const cached = compiledHeaderCache.get(folderName);
if (cached && cached.raw === rawHeaders) {
return cached.compiled;
}
// Otherwise, parse and cache
const compiled = parseCustomHeaders(rawHeaders);
compiledHeaderCache.set(folderName, {
raw: rawHeaders,
compiled: compiled,
});
return compiled;
}
self.addEventListener("fetch", (e) => {
const { request, clientId } = e;
const url = new URL(request.url);
if (
NETWORK_ALLOWLIST_PREFIXES.some((prefix) => url.pathname.startsWith(prefix))
) {
return;
} else if (url.pathname.startsWith(virtualPathPrefix)) {
const virtualPath = url.pathname.substring(virtualPathPrefix.length);
// If there are no slashes remaining, it's a root folder access (e.g. "MyFolder")
// A correct path would be "MyFolder/" or "MyFolder/file.html"
if (!virtualPath.includes("/")) {
e.respondWith(Response.redirect(url.href + "/", 301));
return;
}
}
const cleanUrl = url.origin + url.pathname;
let virtualReferrerPath = null;
if (request.referrer && url.origin === self.location.origin) {
try {
const refUrl = new URL(request.referrer);
if (refUrl.pathname.startsWith(virtualPathPrefix)) {
const pathParts = refUrl.pathname
.substring(virtualPathPrefix.length)
.split("/");
if (pathParts.length > 0 && pathParts[0]) {
virtualReferrerPath = pathParts[0];
}
}
} catch (err) {}
}
if (virtualReferrerPath && !url.pathname.startsWith(virtualPathPrefix)) {
const newVirtualUrl = `${self.location.origin}${virtualPathPrefix}${virtualReferrerPath}${url.pathname}`;
e.respondWith(
(async () => {
const newReq = new Request(newVirtualUrl, request);
const response = await generateResponseForVirtualFile(newReq, clientId);
if (response.status !== 404) return response;
if (FULL_APP_SHELL_URLS.includes(cleanUrl)) {
const cache = await caches.match(request, { ignoreSearch: true });
return cache || fetch(request);
}
return response;
})(),
);
return;
}
if (FULL_APP_SHELL_URLS.includes(cleanUrl)) {
e.respondWith(
(async () => {
const cached = await caches.match(request, { ignoreSearch: true });
return cached || fetch(request);
})(),
);
return;
}
if (url.pathname.startsWith(virtualPathPrefix)) {
e.respondWith(
generateResponseForVirtualFile(request, clientId, e.resultingClientId),
);
return;
}
e.respondWith(
fetch(request).catch(() => new Response(null, { status: 400 })),
);
});
async function handleEncryptedRequest(
opfsRoot,
folderName,
filePath,
key,
request,
customHeaders,
) {
try {
const CHUNK_SIZE = 1024 * 1024 * 4;
const ENCRYPTED_CHUNK_OVERHEAD = 12 + 16;
let manifest;
if (manifestCache.has(folderName)) {
manifest = manifestCache.get(folderName);
} else {
const rfs = await opfsRoot.getDirectoryHandle(RFS_PREFIX);
const folderHandle = await rfs.getDirectoryHandle(folderName);
const manifestHandle = await folderHandle.getFileHandle("manifest.enc");
const manifestBuf = await (await manifestHandle.getFile()).arrayBuffer();
const iv = manifestBuf.slice(16, 28);
const encData = manifestBuf.slice(28);
try {
const dec = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv },
key,
encData,
);
manifest = JSON.parse(new TextDecoder().decode(dec));
if (manifestCache.size > 5) {
const firstKey = manifestCache.keys().next().value;
manifestCache.delete(firstKey);
}
manifestCache.set(folderName, manifest);
} catch (err) {
return new Response("Password Incorrect", { status: 403 });
}
}
const fileMeta =
manifest[filePath] ||
manifest[filePath + ".html"] ||
manifest[filePath + "/index.html"];
if (!fileMeta)
return new Response("File not found: " + filePath, { status: 404 });
const totalSize = fileMeta.size;
if (totalSize === 0) {
return new Response(new Uint8Array(0), {
status: 200,
headers: {
"Content-Type": fileMeta.type || getMimeType(filePath),
"Content-Length": "0",
},
});
}
const rawFileHandle = await getCachedFileHandle(
opfsRoot,
folderName,
fileMeta.id,
);
const rawFile = await rawFileHandle.getFile();
const rangeHeader = request.headers.get("Range");
let start = 0;
let end = totalSize - 1;
if (rangeHeader) {
const parts = rangeHeader.replace(/bytes=/, "").split("-");
start = parseInt(parts[0], 10);
if (parts[1]) end = parseInt(parts[1], 10);
if (isNaN(start)) start = 0;
if (isNaN(end)) end = totalSize - 1;
if (end >= totalSize) end = totalSize - 1;
}
if (start >= totalSize)
return new Response(null, {
status: 416,
headers: { "Content-Range": `bytes */${totalSize}` },
});
const startChunkIdx = Math.floor(start / CHUNK_SIZE);
const endChunkIdx = Math.floor(end / CHUNK_SIZE);
let i = startChunkIdx;
const stream = new ReadableStream({
async pull(controller) {
if (i > endChunkIdx) {
controller.close();
return;
}
try {
const isLastChunk = i * CHUNK_SIZE + CHUNK_SIZE >= totalSize;
const plainChunkSize = isLastChunk
? totalSize % CHUNK_SIZE || CHUNK_SIZE
: CHUNK_SIZE;
const rawOffset = i * (CHUNK_SIZE + ENCRYPTED_CHUNK_OVERHEAD);
const encChunkLen = plainChunkSize + ENCRYPTED_CHUNK_OVERHEAD;
const slicedBlob = rawFile.slice(rawOffset, rawOffset + encChunkLen);
const buf = await slicedBlob.arrayBuffer();
if (buf.byteLength === 0) {
controller.close();
return;
}
const chunkIv = buf.slice(0, 12);
const chunkCipher = buf.slice(12);
const plain = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv: chunkIv },
key,
chunkCipher,
);
const data = new Uint8Array(plain);
const globalChunkStart = i * CHUNK_SIZE;
const outputStart = Math.max(start, globalChunkStart);
const outputEnd = Math.min(end + 1, globalChunkStart + data.length);
if (outputStart < outputEnd) {
controller.enqueue(
data.subarray(
outputStart - globalChunkStart,
outputEnd - globalChunkStart,
),
);
}
i++;
} catch (err) {
controller.error(err);
}
},
});
const headers = {
...customHeaders, // Apply custom headers (CSP, etc)
"Content-Type": fileMeta.type || "application/octet-stream",
"Content-Length": end - start + 1,
"Content-Range": `bytes ${start}-${end}/${totalSize}`,
"Accept-Ranges": "bytes",
"Cache-Control": "no-store",
};
return new Response(stream, { status: 206, headers });
} catch (err) {
return new Response("Internal Encryption Error", { status: 500 });
}
}
const TEXT_MIME_REGEX =
/^(text\/|application\/(javascript|json|xml|x-javascript|typescript|x-sh|x-httpd-php|ld\+json|manifest\+json|svg\+xml))/i;
const TEXT_EXTENSIONS =
/\.(txt|html|htm|js|mjs|css|json|md|xml|svg|sh|php|py|rb|c|cpp|h|ts|sql|ini|yaml|yml)$/i;
function isLikelyText(type, path) {
return TEXT_MIME_REGEX.test(type) || TEXT_EXTENSIONS.test(path);
}
function isActuallyTextSniff(buffer) {
const view = new Uint8Array(buffer, 0, Math.min(buffer.byteLength, 4096));
if (view.length === 0) return true;
else if (view.includes(0)) return false;
// Check for BOMs (UTF-8, UTF-16LE, UTF-16BE)
if (
(view[0] === 0xef && view[1] === 0xbb && view[2] === 0xbf) ||
(view[0] === 0xff && view[1] === 0xfe) ||
(view[0] === 0xfe && view[1] === 0xff)
) {
return true;
}
try {
const decoder = new TextDecoder("utf-8", { fatal: true });
decoder.decode(view);
return true;
} catch (err) {
// If UTF-8 fails, check for legacy encodings (Latin-1)
let suspiciousBytes = 0;
for (let i = 0; i < view.length; i++) {
const byte = view[i];
// Check for common binary control characters (except TAB, LF, CR, etc.)
if (byte < 7 || (byte > 14 && byte < 32)) {
suspiciousBytes++;
}
}
// If more than 5% of the sample is "garbage" control chars, it's binary probably
return suspiciousBytes / view.length < 0.05;
}
}
async function generateResponseForVirtualFile(
request,
clientId,
resultingClientId,
) {
try {
const url = new URL(request.url);
const { mode } = request;
const virtualPath = url.pathname.substring(virtualPathPrefix.length);
const pathParts = virtualPath.split("/").map((p) => {
try {
return decodeURIComponent(p);
} catch (err) {
return p;
}
});
const folderName = pathParts[0];
let root = await getOpfsRoot();
if (!registryCache) await getRegistry();
const registry = registryCache || {};
const folderData = registry[folderName] || {};
// Here the boot-up logic happens (but only if the reloadOnRequest is enabled)
if (
reloadOnRequest &&
mode === "navigate" &&
!url.searchParams.has("boot")
) {
url.searchParams.set("boot", "1");
return new Response(
`<!DOCTYPE html><script>location.replace("${url.href}")</script>`,
{ headers: { "Content-Type": "text/html" } },
);
}
const effectiveClientId = resultingClientId || clientId;
let session =
clientSessionStore.get(effectiveClientId) ||
(clientId ? clientSessionStore.get(clientId) : null);
if (!session && pendingNavData && pendingNavData[folderName]) {
session = pendingNavData[folderName];
}
if (session) {
session.timestamp = Date.now();
if (effectiveClientId) clientSessionStore.set(effectiveClientId, session);
}
session = session || {};
let relativePath = pathParts.slice(1).join("/");
if (!relativePath || relativePath.endsWith("/"))
relativePath += "index.html";
if (folderData.encryptionType === "password") {
if (!session.key) return new Response("Session locked", { status: 403 });
const compiledHeaders =
session.compiledHeaders ||
getCompiledHeaders(folderName, folderData.headers);
const headers = applyCustomHeaders(
{
"Content-Type": getMimeType(relativePath),
"Cache-Control": "no-store",
"Accept-Ranges": "bytes",
},
relativePath,
compiledHeaders,
);
return await handleEncryptedRequest(
root,
folderName,
relativePath,
session.key,
request,
headers,
);
}
let handle = await getCachedFileHandle(root, folderName, relativePath);
if (!handle && !relativePath.includes(".") && !relativePath.endsWith("/")) {
const htmlHandle = await getCachedFileHandle(
root,
folderName,
relativePath + ".html",
);
if (htmlHandle) {
handle = htmlHandle;
relativePath += ".html";
}
}
let status = 200;
if (
!handle &&
(mode === "navigate" ||
(request.headers.get("Accept") || "").includes("text/html"))
) {
const errorHandle = await getCachedFileHandle(
root,
folderName,
"404.html",
);
if (errorHandle) {
handle = errorHandle;
relativePath = "404.html";
status = 404;
} else if (!relativePath.endsWith("index.html")) {
// Standard SPA fallback
const indexHandle = await getCachedFileHandle(
root,
folderName,
"index.html",
);
if (indexHandle) {
handle = indexHandle;
relativePath = "index.html";
}
}
}
if (!handle)
return new Response(
"File not found: " + folderName + "/" + relativePath,
{ status: 404 },
);
const file = await handle.getFile();
const totalSize = file.size;
const contentType =
file.type || getMimeType(relativePath) || "application/octet-stream";
const compiledHeaders =
session.compiledHeaders ||
getCompiledHeaders(folderName, folderData.headers);
const compiledRules =
session.compiledRules || compileRules(folderData.rules);
let finalHeaders = applyCustomHeaders(
{
"Content-Type": contentType,
"Cache-Control": "no-store",
"Accept-Ranges": "bytes",
},
relativePath,
compiledHeaders,
);
let responseBody = file;
let processedSize = totalSize;
if (
compiledRules &&
compiledRules.length > 0 &&
totalSize > 0 &&
totalSize <= MAX_REGEX_SIZE
) {
if (isLikelyText(contentType, relativePath)) {
// Only read the first 4KB to check if it's actually text
const probeBuffer = await file.slice(0, 4096).arrayBuffer();
if (isActuallyTextSniff(probeBuffer)) {
const fullBuffer = await file.arrayBuffer();
const processedBuffer = applyRegexRules(
relativePath,
fullBuffer,
contentType,
compiledRules,
);
if (processedBuffer !== fullBuffer) {
responseBody = processedBuffer; // Serve manipulated version
processedSize = processedBuffer.byteLength;
} else {
// Rules existed but didn't match.
responseBody = fullBuffer;
}
}
}
}
const rangeHeader = request.headers.get("Range");
if (rangeHeader && status === 200) {
const parts = rangeHeader.replace(/bytes=/, "").split("-");
let start = parseInt(parts[0], 10) || 0;
let end = parts[1] ? parseInt(parts[1], 10) : processedSize - 1;
if (start >= processedSize) {
return new Response(null, {
status: 416,
headers: { "Content-Range": `bytes */${processedSize}` },
});
}
end = Math.min(end, processedSize - 1);
const chunkLength = end - start + 1;
finalHeaders["Content-Range"] = `bytes ${start}-${end}/${processedSize}`;
finalHeaders["Content-Length"] = chunkLength.toString();
const rangeSlice = responseBody.slice(start, end + 1);
return new Response(rangeSlice, { status: 206, headers: finalHeaders });
}
finalHeaders["Content-Length"] = processedSize.toString();
return new Response(responseBody, { status, headers: finalHeaders });
} catch (err) {
console.error("SW fetch error:", err);
return new Response("Internal server error", { status: 500 });
}
}