almostnode 0.2.4 → 0.2.5

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.
package/dist/__sw__.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Service Worker for Mini WebContainers
3
3
  * Intercepts fetch requests and routes them to virtual servers
4
- * Version: 12 - add COEP/COOP headers for iframe embedding
4
+ * Version: 13 - redirect navigation requests from virtual context to include prefix
5
5
  */
6
6
 
7
7
  // Communication port with main thread
@@ -212,6 +212,28 @@ self.addEventListener('fetch', (event) => {
212
212
  const match = url.pathname.match(/^\/__virtual__\/(\d+)(\/.*)?$/);
213
213
 
214
214
  if (!match) {
215
+ // Not a virtual request - but check if it's a navigation from a virtual context
216
+ // This handles plain <a href="/about"> links that should stay within the virtual server
217
+ if (event.request.mode === 'navigate') {
218
+ const referer = event.request.referrer;
219
+ if (referer) {
220
+ try {
221
+ const refererUrl = new URL(referer);
222
+ const refererMatch = refererUrl.pathname.match(/^\/__virtual__\/(\d+)/);
223
+ if (refererMatch) {
224
+ // User clicked a link from within a virtual server context
225
+ // Redirect to include the virtual prefix
226
+ const virtualPrefix = refererMatch[0];
227
+ const redirectUrl = url.origin + virtualPrefix + url.pathname + url.search;
228
+ console.log('[SW] Redirecting navigation from virtual context:', url.pathname, '->', redirectUrl);
229
+ event.respondWith(Response.redirect(redirectUrl, 302));
230
+ return;
231
+ }
232
+ } catch (e) {
233
+ // Invalid referer URL, ignore
234
+ }
235
+ }
236
+ }
215
237
  // Not a virtual request, let it pass through
216
238
  return;
217
239
  }
@@ -396,7 +396,8 @@
396
396
  constructor(){
397
397
  this.root = {
398
398
  type: "directory",
399
- children: new Map()
399
+ children: new Map(),
400
+ mtime: Date.now()
400
401
  };
401
402
  }
402
403
  on(event, listener) {
@@ -509,7 +510,8 @@
509
510
  const content = typeof data === "string" ? this.encoder.encode(data) : data;
510
511
  parent.children.set(basename, {
511
512
  type: "file",
512
- content
513
+ content,
514
+ mtime: Date.now()
513
515
  });
514
516
  if (emitEvent) {
515
517
  this.notifyWatchers(normalized, existed ? "change" : "rename");
@@ -570,7 +572,8 @@
570
572
  if (!child) {
571
573
  child = {
572
574
  type: "directory",
573
- children: new Map()
575
+ children: new Map(),
576
+ mtime: Date.now()
574
577
  };
575
578
  current.children.set(segment, child);
576
579
  } else if (child.type !== "directory") {
@@ -588,8 +591,8 @@
588
591
  if (!node) {
589
592
  throw createNodeError("ENOENT", "stat", path);
590
593
  }
591
- const now = Date.now();
592
594
  const size = node.type === "file" ? node.content?.length || 0 : 0;
595
+ const mtime = node.mtime;
593
596
  return {
594
597
  isFile: ()=>node.type === "file",
595
598
  isDirectory: ()=>node.type === "directory",
@@ -600,14 +603,14 @@
600
603
  isSocket: ()=>false,
601
604
  size,
602
605
  mode: node.type === "directory" ? 493 : 420,
603
- mtime: new Date(now),
604
- atime: new Date(now),
605
- ctime: new Date(now),
606
- birthtime: new Date(now),
607
- mtimeMs: now,
608
- atimeMs: now,
609
- ctimeMs: now,
610
- birthtimeMs: now,
606
+ mtime: new Date(mtime),
607
+ atime: new Date(mtime),
608
+ ctime: new Date(mtime),
609
+ birthtime: new Date(mtime),
610
+ mtimeMs: mtime,
611
+ atimeMs: mtime,
612
+ ctimeMs: mtime,
613
+ birthtimeMs: mtime,
611
614
  nlink: 1,
612
615
  uid: 1e3,
613
616
  gid: 1e3,
@@ -661,7 +664,8 @@
661
664
  }
662
665
  parent.children.set(basename, {
663
666
  type: "directory",
664
- children: new Map()
667
+ children: new Map(),
668
+ mtime: Date.now()
665
669
  });
666
670
  }
667
671
  readdirSync(path) {
@@ -942,6 +946,14 @@
942
946
  };
943
947
  }
944
948
  }
949
+ function simpleHash(str) {
950
+ let hash = 0;
951
+ for(let i = 0; i < str.length; i++){
952
+ hash = (hash << 5) - hash + str.charCodeAt(i);
953
+ hash |= 0;
954
+ }
955
+ return hash.toString(36);
956
+ }
945
957
  class Dirent {
946
958
  name;
947
959
  _isDirectory;
@@ -59633,7 +59645,23 @@ sys 0m0.000s
59633
59645
  "@sentry/node": sentryShim,
59634
59646
  "@sentry/core": sentryShim
59635
59647
  };
59636
- function createRequire(vfs, fsShim, process, currentDir, moduleCache, options) {
59648
+ function createRequire(vfs, fsShim, process, currentDir, moduleCache, options, processedCodeCache) {
59649
+ const resolutionCache = new Map();
59650
+ const packageJsonCache = new Map();
59651
+ const getParsedPackageJson = (pkgPath)=>{
59652
+ if (packageJsonCache.has(pkgPath)) {
59653
+ return packageJsonCache.get(pkgPath);
59654
+ }
59655
+ try {
59656
+ const content = vfs.readFileSync(pkgPath, "utf8");
59657
+ const parsed = JSON.parse(content);
59658
+ packageJsonCache.set(pkgPath, parsed);
59659
+ return parsed;
59660
+ } catch {
59661
+ packageJsonCache.set(pkgPath, null);
59662
+ return null;
59663
+ }
59664
+ };
59637
59665
  const resolveModule = (id, fromDir)=>{
59638
59666
  if (id.startsWith("node:")) {
59639
59667
  id = id.slice(5);
@@ -59641,15 +59669,25 @@ sys 0m0.000s
59641
59669
  if (builtinModules[id] || id === "fs" || id === "process" || id === "url" || id === "querystring" || id === "util") {
59642
59670
  return id;
59643
59671
  }
59672
+ const cacheKey = `${fromDir}|${id}`;
59673
+ const cached = resolutionCache.get(cacheKey);
59674
+ if (cached !== void 0) {
59675
+ if (cached === null) {
59676
+ throw new Error(`Cannot find module '${id}'`);
59677
+ }
59678
+ return cached;
59679
+ }
59644
59680
  if (id.startsWith("./") || id.startsWith("../") || id.startsWith("/")) {
59645
59681
  const resolved = id.startsWith("/") ? id : resolve$2(fromDir, id);
59646
59682
  if (vfs.existsSync(resolved)) {
59647
59683
  const stats = vfs.statSync(resolved);
59648
59684
  if (stats.isFile()) {
59685
+ resolutionCache.set(cacheKey, resolved);
59649
59686
  return resolved;
59650
59687
  }
59651
59688
  const indexPath = join$1(resolved, "index.js");
59652
59689
  if (vfs.existsSync(indexPath)) {
59690
+ resolutionCache.set(cacheKey, indexPath);
59653
59691
  return indexPath;
59654
59692
  }
59655
59693
  }
@@ -59660,9 +59698,11 @@ sys 0m0.000s
59660
59698
  for (const ext of extensions){
59661
59699
  const withExt = resolved + ext;
59662
59700
  if (vfs.existsSync(withExt)) {
59701
+ resolutionCache.set(cacheKey, withExt);
59663
59702
  return withExt;
59664
59703
  }
59665
59704
  }
59705
+ resolutionCache.set(cacheKey, null);
59666
59706
  throw new Error(`Cannot find module '${id}' from '${fromDir}'`);
59667
59707
  }
59668
59708
  const tryResolveFile = (basePath)=>{
@@ -59697,9 +59737,8 @@ sys 0m0.000s
59697
59737
  const pkgName = parts[0].startsWith("@") && parts.length > 1 ? `${parts[0]}/${parts[1]}` : parts[0];
59698
59738
  const pkgRoot = join$1(nodeModulesDir, pkgName);
59699
59739
  const pkgPath = join$1(pkgRoot, "package.json");
59700
- if (vfs.existsSync(pkgPath)) {
59701
- const pkgContent = vfs.readFileSync(pkgPath, "utf8");
59702
- const pkg = JSON.parse(pkgContent);
59740
+ const pkg = getParsedPackageJson(pkgPath);
59741
+ if (pkg) {
59703
59742
  if (pkg.exports) {
59704
59743
  try {
59705
59744
  const resolved2 = s(pkg, moduleId, {
@@ -59726,11 +59765,18 @@ sys 0m0.000s
59726
59765
  while(searchDir !== "/"){
59727
59766
  const nodeModulesDir = join$1(searchDir, "node_modules");
59728
59767
  const resolved = tryResolveFromNodeModules(nodeModulesDir, id);
59729
- if (resolved) return resolved;
59768
+ if (resolved) {
59769
+ resolutionCache.set(cacheKey, resolved);
59770
+ return resolved;
59771
+ }
59730
59772
  searchDir = dirname(searchDir);
59731
59773
  }
59732
59774
  const rootResolved = tryResolveFromNodeModules("/node_modules", id);
59733
- if (rootResolved) return rootResolved;
59775
+ if (rootResolved) {
59776
+ resolutionCache.set(cacheKey, rootResolved);
59777
+ return rootResolved;
59778
+ }
59779
+ resolutionCache.set(cacheKey, null);
59734
59780
  throw new Error(`Cannot find module '${id}'`);
59735
59781
  };
59736
59782
  const loadModule = (resolvedPath)=>{
@@ -59752,19 +59798,25 @@ sys 0m0.000s
59752
59798
  module.loaded = true;
59753
59799
  return module;
59754
59800
  }
59755
- let code = vfs.readFileSync(resolvedPath, "utf8");
59801
+ const rawCode = vfs.readFileSync(resolvedPath, "utf8");
59756
59802
  const dirname$1 = dirname(resolvedPath);
59757
- const isCjsFile = resolvedPath.endsWith(".cjs");
59758
- const isAlreadyBundledCjs = code.startsWith('"use strict";\nvar __') || code.startsWith("'use strict';\nvar __");
59759
- const hasEsmImport = /\bimport\s+[\w{*'"]/m.test(code);
59760
- const hasEsmExport = /\bexport\s+(?:default|const|let|var|function|class|{|\*)/m.test(code);
59761
- if (!isCjsFile && !isAlreadyBundledCjs) {
59762
- if (resolvedPath.endsWith(".mjs") || resolvedPath.includes("/esm/") || hasEsmImport || hasEsmExport) {
59763
- code = transformEsmToCjs(code, resolvedPath);
59764
- }
59765
- }
59766
- code = transformDynamicImports(code);
59767
- const moduleRequire = createRequire(vfs, fsShim, process, dirname$1, moduleCache, options);
59803
+ const codeCacheKey = `${resolvedPath}|${simpleHash(rawCode)}`;
59804
+ let code = processedCodeCache?.get(codeCacheKey);
59805
+ if (!code) {
59806
+ code = rawCode;
59807
+ const isCjsFile = resolvedPath.endsWith(".cjs");
59808
+ const isAlreadyBundledCjs = code.startsWith('"use strict";\nvar __') || code.startsWith("'use strict';\nvar __");
59809
+ const hasEsmImport = /\bimport\s+[\w{*'"]/m.test(code);
59810
+ const hasEsmExport = /\bexport\s+(?:default|const|let|var|function|class|{|\*)/m.test(code);
59811
+ if (!isCjsFile && !isAlreadyBundledCjs) {
59812
+ if (resolvedPath.endsWith(".mjs") || resolvedPath.includes("/esm/") || hasEsmImport || hasEsmExport) {
59813
+ code = transformEsmToCjs(code, resolvedPath);
59814
+ }
59815
+ }
59816
+ code = transformDynamicImports(code);
59817
+ processedCodeCache?.set(codeCacheKey, code);
59818
+ }
59819
+ const moduleRequire = createRequire(vfs, fsShim, process, dirname$1, moduleCache, options, processedCodeCache);
59768
59820
  moduleRequire.cache = moduleCache;
59769
59821
  const consoleWrapper = createConsoleWrapper(options.onConsole);
59770
59822
  try {
@@ -59935,6 +59987,7 @@ ${code}
59935
59987
  process;
59936
59988
  moduleCache = {};
59937
59989
  options;
59990
+ processedCodeCache = new Map();
59938
59991
  constructor(vfs2, options2 = {}){
59939
59992
  this.vfs = vfs2;
59940
59993
  this.process = createProcess({
@@ -60016,7 +60069,7 @@ ${code}
60016
60069
  execute(code, filename = "/index.js") {
60017
60070
  const dirname$1 = dirname(filename);
60018
60071
  this.vfs.writeFileSync(filename, code);
60019
- const require = createRequire(this.vfs, this.fsShim, this.process, dirname$1, this.moduleCache, this.options);
60072
+ const require = createRequire(this.vfs, this.fsShim, this.process, dirname$1, this.moduleCache, this.options, this.processedCodeCache);
60020
60073
  const module = {
60021
60074
  id: filename,
60022
60075
  filename,