vfsjs-test 1.0.21 → 1.0.23

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/main.js +52 -24
  2. package/package.json +1 -1
  3. package/vfsjs.js +54 -43
package/main.js CHANGED
@@ -1,29 +1,57 @@
1
- // Inside main.js
1
+ // main.js - Production Build Wrapper
2
2
 
3
- async function startDeveloperWorkspace() {
4
- // 1. Fetch remote libraries, populate virtual files, etc.
5
- const remoteLibraryUrl = 'https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.4/dist/confetti.browser.min.js';
6
- await vfs.importRemote('my-confetti-engine.js', remoteLibraryUrl, 'text/javascript');
3
+ window.vfs = {
4
+ add(filename, contentString, customMime = null) {
5
+ // Strip out query paths down to base keys
6
+ let cleanKey = filename.split('?')[0].split('/').pop();
7
+ window._vfsVirtualCache[cleanKey] = { content: contentString, mime: customMime };
8
+ console.log(`[VFS Cache] Staging file asset: ${cleanKey}`);
9
+ return true;
10
+ },
7
11
 
8
- // 2. Populate your script and style data maps into the active worker cache state
9
- vfs.add('app.js', `console.log("App script running natively inside virtualized layout context!");`, 'text/javascript');
10
- vfs.add('style.css', `body { background: #020617; color: #38bdf8; font-family: monospace; }`, 'text/css');
12
+ async importRemote(localFilename, remoteUrl, customMime = null) {
13
+ try {
14
+ const response = await fetch(remoteUrl);
15
+ if (!response.ok) throw new Error(`HTTP Fault: ${response.status}`);
16
+ const text = await response.text();
17
+ this.add(localFilename, text, customMime);
18
+ return true;
19
+ } catch (err) {
20
+ console.error(`[VFS Proxy Error]`, err);
21
+ return false;
22
+ }
23
+ }
24
+ };
25
+
26
+ async function buildProductionApplication() {
27
+ // 1. Stage compilation data targets
28
+ vfs.add('app.js', `
29
+ console.log("Launcher script running. Invoking confetti...");
30
+ if (typeof window.confetti === 'function') {
31
+ setInterval(() => {
32
+ window.confetti({ particleCount: 40, spread: 50, origin: { y: 0.6 } });
33
+ }, 2000);
34
+ } else {
35
+ console.error("Confetti script missing from global iframe execution scope.");
36
+ }
37
+ `, 'text/javascript');
11
38
 
12
- // 3. CRITICAL: The VFS is loaded, call launch() to spawn the iframe safely!
13
- window._vfsKernel.launch();
39
+ vfs.add('style.css', `
40
+ body { background: #020617 !important; color: #38bdf8 !important; font-family: monospace; padding: 40px; text-align: center; }
41
+ h2 { text-shadow: 0 0 8px rgba(56,189,248,0.5); }
42
+ `, 'text/css');
43
+
44
+ // 2. Fetch CDNJS/jsDelivr packages that bypass scope paths safely
45
+ await vfs.importRemote('my-confetti-engine.js', 'https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.4/dist/confetti.browser.min.js', 'text/javascript');
46
+
47
+ // 3. Compile and open viewport
48
+ window._vfsKernel.openViewport();
14
49
  }
15
50
 
16
- // Global invocation hook listener
17
- document.addEventListener('DOMContentLoaded', async () => {
18
- // Loop interval ensures window._vfsKernel is instantiated and done setting up step 1
19
- const checker = setInterval(async () => {
20
- if (window._vfsKernel) {
21
- clearInterval(checker);
22
- // Wait for the service worker confirmation to pass back up
23
- const isReady = await navigator.serviceWorker.ready;
24
- if (isReady) {
25
- startDeveloperWorkspace();
26
- }
27
- }
28
- }, 10);
29
- });
51
+ (function verifyKernelAvailability() {
52
+ if (window._vfsKernel) {
53
+ buildProductionApplication();
54
+ } else {
55
+ setTimeout(verifyKernelAvailability, 10);
56
+ }
57
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vfsjs-test",
3
- "version": "1.0.21",
3
+ "version": "1.0.23",
4
4
  "description": "",
5
5
  "main": "index.html",
6
6
  "scripts": {
package/vfsjs.js CHANGED
@@ -1,74 +1,85 @@
1
- // vfsjs.js - Version 4.0.0 (Race-Condition Proof Production Engine)
1
+ // vfsjs.js - Version 6.0.0 (Zero-Network Tag Inline Engine)
2
2
  (function() {
3
- const encoder = new TextEncoder();
4
-
5
3
  const engineCore = {
6
4
  async init() {
7
5
  try {
8
- // 1. Core Service Worker Handshake
6
+ // 1. Boot Service Worker for the root frame route interception if needed
9
7
  await navigator.serviceWorker.register('./sw.js');
10
8
  if (!navigator.serviceWorker.controller) {
11
9
  await new Promise(r => navigator.serviceWorker.addEventListener('controllerchange', r, { once: true }));
12
10
  }
13
11
 
14
- // 2. Capture the physical file's raw HTML structure exactly as it is written
15
- const developerRawHtml = `<!DOCTYPE html>\n<html>${document.documentElement.innerHTML}</html>`;
16
- this.write('index.html', developerRawHtml, 'text/html');
12
+ // 2. Capture the developer's original raw HTML string structure
13
+ const rawHtml = document.documentElement.innerHTML;
14
+ window._vfsRawTemplate = `<!DOCTYPE html>\n<html>${rawHtml}</html>`;
17
15
 
18
- // 3. Structural Reset: Wipe page visual nodes and lock outer body down solid
16
+ // 3. Clear parent UI layout instantly
19
17
  document.documentElement.innerHTML = '';
20
18
  Object.assign(document.documentElement.style, { width: '100%', height: '100%', overflow: 'hidden', margin: '0', padding: '0' });
21
19
  Object.assign(document.body.style, { width: '100%', height: '100%', overflow: 'hidden', margin: '0', padding: '0', background: '#020617' });
22
20
 
23
- console.log("[VFS Kernel] Stage 1 Ready: Core cached. Awaiting developer vfs.add() execution...");
21
+ // 4. Create the pristine layout iframe container
22
+ const iframe = document.createElement('iframe');
23
+ iframe.id = 'canvas-viewport';
24
+ Object.assign(iframe.style, {
25
+ position: 'absolute', top: '0', left: '0', width: '100%', height: '100%',
26
+ border: 'none', margin: '0', padding: '0', zIndex: '999999'
27
+ });
28
+ document.body.appendChild(iframe);
29
+
24
30
  return true;
25
31
  } catch (err) {
26
- console.error("VFS Core Initialization Failure:", err);
32
+ console.error("VFS Critical Bootstrap Failure:", err);
27
33
  return false;
28
34
  }
29
35
  },
30
36
 
31
- // PHASE 2: Call this ONLY when files are completely mapped to safely spawn the iframe!
32
- launch() {
33
- if (document.getElementById('canvas-viewport')) return;
37
+ // Called by main.js after vfs.add loops complete
38
+ openViewport() {
39
+ const iframe = document.getElementById('canvas-viewport');
40
+ if (!iframe) return;
34
41
 
35
- console.log("[VFS Kernel] Stage 2 Triggered: All assets populated. Mounting secure viewport sandbox.");
36
- const iframe = document.createElement('iframe');
37
- iframe.id = 'canvas-viewport';
38
- Object.assign(iframe.style, {
39
- position: 'absolute', top: '0', left: '0', width: '100%', height: '100%',
40
- border: 'none', margin: '0', padding: '0', zIndex: '999999'
41
- });
42
- document.body.appendChild(iframe);
42
+ // Create a virtual document parser out of our captured template
43
+ const parser = new DOMParser();
44
+ const doc = parser.parseFromString(window._vfsRawTemplate, 'text/html');
43
45
 
44
- // Point to the virtual root route safely now that assets are loaded
45
- iframe.src = './?vfs=true';
46
- },
46
+ // 1. Process and inline all marked script tags out of VFS memory maps
47
+ const scripts = Array.from(doc.querySelectorAll('script[src*="vfs=true"]'));
48
+ scripts.forEach(script => {
49
+ const srcAttr = script.getAttribute('src').split('?')[0].split('/').pop();
50
+ const virtualCode = window._vfsVirtualCache[srcAttr];
51
+
52
+ if (virtualCode) {
53
+ const inlineScript = doc.createElement('script');
54
+ inlineScript.textContent = virtualCode.content;
55
+ script.parentNode.replaceChild(inlineScript, script);
56
+ }
57
+ });
47
58
 
48
- write(filename, contentString, customMime) {
49
- if (!navigator.serviceWorker.controller) return false;
50
-
51
- let cleanKey = filename.split('?')[0];
52
- if (cleanKey.includes('/')) {
53
- cleanKey = cleanKey.split('/').pop();
54
- }
59
+ // 2. Process and inline all marked style sheets out of VFS memory maps
60
+ const links = Array.from(doc.querySelectorAll('link[rel="stylesheet"][href*="vfs=true"]'));
61
+ links.forEach(link => {
62
+ const hrefAttr = link.getAttribute('href').split('?')[0].split('/').pop();
63
+ const virtualCSS = window._vfsVirtualCache[hrefAttr];
64
+
65
+ if (virtualCSS) {
66
+ const inlineStyle = doc.createElement('style');
67
+ inlineStyle.textContent = virtualCSS.content;
68
+ link.parentNode.replaceChild(inlineStyle, link);
69
+ }
70
+ });
55
71
 
56
- if (!customMime) {
57
- const ext = cleanKey.split('.').pop().toLowerCase();
58
- const mimeMatrix = { 'js': 'text/javascript', 'css': 'text/css', 'html': 'text/html', 'json': 'application/json' };
59
- customMime = mimeMatrix[ext] || 'application/octet-stream';
60
- }
72
+ // 3. Convert modified dynamic DOM tree back to a string bundle
73
+ const compilationOutput = `<!DOCTYPE html>\n<html>${doc.documentElement.innerHTML}</html>`;
61
74
 
62
- navigator.serviceWorker.controller.postMessage({
63
- type: 'VFS_WRITE',
64
- filename: cleanKey,
65
- content: encoder.encode(contentString),
66
- mime: customMime
67
- });
68
- return true;
75
+ // 4. Inject compilation cleanly via srcdoc to bypass unpkg scope boundaries completely!
76
+ iframe.srcdoc = compilationOutput;
77
+ console.log("[VFS Kernel] Execution compilation injected successfully via srcdoc channel.");
69
78
  }
70
79
  };
71
80
 
81
+ // Shared internal key store memory map allocation
82
+ window._vfsVirtualCache = {};
72
83
  window._vfsKernel = engineCore;
73
84
 
74
85
  if (document.readyState === 'loading') {