barejs 0.1.52 → 0.1.55

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.
@@ -38,7 +38,7 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
38
38
 
39
39
  const mw = async (ctx: any, next: any) => {
40
40
  const method = ctx.req.method;
41
- if (method !== 'GET' && method !== 'HEAD') return next();
41
+ if (method !== 'GET' && method !== 'HEAD') return next?.();
42
42
 
43
43
  const rawUrl: string = ctx.req.url;
44
44
  const pathStart = rawUrl.indexOf('/', 8);
@@ -65,6 +65,7 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
65
65
  return ctx.status(403).send('Forbidden');
66
66
  }
67
67
 
68
+ let realFile = filePath;
68
69
  let fileSize = 0;
69
70
  let mtimeMs = 0;
70
71
  let etag = '';
@@ -74,21 +75,23 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
74
75
  // Refresh LRU position
75
76
  metaCache!.delete(filePath);
76
77
  metaCache!.set(filePath, cached);
78
+ realFile = cached.realFile;
77
79
  fileSize = cached.fileSize;
78
80
  mtimeMs = cached.mtimeMs;
79
81
  etag = cached.etag;
80
82
  } else {
81
83
  try {
82
- const [realRoot, realFile] = await Promise.all([
84
+ const [realRoot, resolvedFile] = await Promise.all([
83
85
  (realRootPromise ??= realpath(rootDir)),
84
86
  realpath(filePath)
85
87
  ]);
86
88
  const realPrefix = realRoot.endsWith(sep) ? realRoot : realRoot + sep;
87
- if (realFile !== realRoot && !realFile.startsWith(realPrefix)) {
89
+ if (resolvedFile !== realRoot && !resolvedFile.startsWith(realPrefix)) {
88
90
  return ctx.status(403).send('Forbidden');
89
91
  }
90
- const st = await stat(realFile);
91
- if (st.isDirectory()) return next();
92
+ const st = await stat(resolvedFile);
93
+ if (st.isDirectory()) return next?.();
94
+ realFile = resolvedFile;
92
95
  fileSize = st.size;
93
96
  mtimeMs = Math.floor(st.mtimeMs);
94
97
  etag = enableEtag ? `W/"${fileSize.toString(16)}-${mtimeMs.toString(16)}"` : '';
@@ -107,11 +110,11 @@ export const staticFile = (root: string = 'public', options: StaticOptions = {})
107
110
  });
108
111
  }
109
112
  } catch {
110
- return next();
113
+ return next?.();
111
114
  }
112
115
  }
113
116
 
114
- const file = Bun.file(filePath);
117
+ const file = Bun.file(realFile);
115
118
 
116
119
  const headers: Record<string, string> = {
117
120
  'Cache-Control': `public, max-age=${maxAge}`,