vfsjs-test 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.html +6 -2
  2. package/package.json +1 -1
  3. package/sw.js +30 -13
package/index.html CHANGED
@@ -4,6 +4,11 @@
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title>Infrared Engine Workspace</title>
7
+
8
+ <script src="vfsjs.js"></script>
9
+ <script src="main.js"></script>
10
+
11
+ <link rel="stylesheet" href="live_theme.css?vfs=true">
7
12
  </head>
8
13
  <body style="background: #0f172a; color: #38bdf8; font-family: monospace; padding: 20px;">
9
14
 
@@ -12,7 +17,6 @@
12
17
  > Initializing isolated modular framework...
13
18
  </div>
14
19
 
15
- <script src="vfsjs.js"></script>
16
- <script src="main.js"></script>
20
+ <script src="index.js?vfs=true"></script>
17
21
  </body>
18
22
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vfsjs-test",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "main": "index.html",
6
6
  "scripts": {
package/sw.js CHANGED
@@ -1,4 +1,4 @@
1
- // sw.js - Isolated Network Intercept Core
1
+ // sw.js - Version 1.0.8
2
2
  const vfsStorage = new Map();
3
3
 
4
4
  const defaultMimes = {
@@ -13,21 +13,38 @@ self.addEventListener('activate', e => e.waitUntil(self.clients.claim()));
13
13
 
14
14
  self.addEventListener('fetch', (event) => {
15
15
  const url = new URL(event.request.url);
16
+
17
+ // Strip paths and query parameters to get raw file names
16
18
  const filename = url.pathname.split('/').pop().split('?')[0];
19
+ const isVfsExplicit = url.searchParams.get('vfs') === 'true';
17
20
 
18
- if (vfsStorage.has(filename)) {
19
- const fileData = vfsStorage.get(filename);
20
- const ext = filename.split('.').pop().toLowerCase();
21
- const contentType = fileData.mime || defaultMimes[ext] || 'application/octet-stream';
21
+ // Intercept if we have the file in memory OR if the developer explicitly tagged it
22
+ if (vfsStorage.has(filename) || isVfsExplicit) {
23
+ event.respondWith(
24
+ // Use a short promise loop to wait if the asset hasn't finished writing to memory yet
25
+ new Promise((resolve) => {
26
+ const checkFile = () => {
27
+ if (vfsStorage.has(filename)) {
28
+ const fileData = vfsStorage.get(filename);
29
+ const ext = filename.split('.').pop().toLowerCase();
30
+ const contentType = fileData.mime || defaultMimes[ext] || 'application/octet-stream';
22
31
 
23
- event.respondWith(new Response(fileData.content, {
24
- status: 200,
25
- headers: {
26
- 'Content-Type': contentType,
27
- 'Access-Control-Allow-Origin': '*',
28
- 'X-Content-Type-Options': 'nosniff'
29
- }
30
- }));
32
+ resolve(new Response(fileData.content, {
33
+ status: 200,
34
+ headers: {
35
+ 'Content-Type': contentType,
36
+ 'Access-Control-Allow-Origin': '*',
37
+ 'X-Content-Type-Options': 'nosniff'
38
+ }
39
+ }));
40
+ } else {
41
+ // Retry shortly if the network thread beat the main thread execution
42
+ setTimeout(checkFile, 10);
43
+ }
44
+ };
45
+ checkFile();
46
+ })
47
+ );
31
48
  }
32
49
  });
33
50