@valentinkolb/filegate 2.3.0 → 2.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valentinkolb/filegate",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,5 +1,6 @@
1
1
  import { Hono } from "hono";
2
2
  import { describeRoute } from "hono-openapi";
3
+ import { stat } from "node:fs/promises";
3
4
  import sharp from "sharp";
4
5
  import { validatePath } from "../lib/path";
5
6
  import { jsonResponse, requiresAuth } from "../lib/openapi";
@@ -8,6 +9,13 @@ import { ImageThumbnailQuerySchema, ErrorSchema } from "../schemas";
8
9
 
9
10
  const app = new Hono();
10
11
 
12
+ // Generate ETag from path, mtime, and thumbnail parameters
13
+ const generateETag = (path: string, mtime: Date, params: string): string => {
14
+ const hasher = new Bun.CryptoHasher("sha256");
15
+ hasher.update(`${path}:${mtime.getTime()}:${params}`);
16
+ return `"${hasher.digest("hex").slice(0, 16)}"`;
17
+ };
18
+
11
19
  // Supported image MIME types
12
20
  const SUPPORTED_IMAGE_TYPES = new Set([
13
21
  "image/jpeg",
@@ -74,6 +82,27 @@ app.get(
74
82
  );
75
83
  }
76
84
 
85
+ // Get file stats for Last-Modified and ETag
86
+ const fileStat = await stat(result.realPath);
87
+ const lastModified = fileStat.mtime;
88
+ const paramsKey = `${width}x${height}:${fit}:${position}:${format}:${quality}`;
89
+ const etag = generateETag(result.realPath, lastModified, paramsKey);
90
+
91
+ // Check If-None-Match (ETag)
92
+ const ifNoneMatch = c.req.header("If-None-Match");
93
+ if (ifNoneMatch === etag) {
94
+ return new Response(null, { status: 304 });
95
+ }
96
+
97
+ // Check If-Modified-Since
98
+ const ifModifiedSince = c.req.header("If-Modified-Since");
99
+ if (ifModifiedSince) {
100
+ const clientDate = new Date(ifModifiedSince);
101
+ if (lastModified <= clientDate) {
102
+ return new Response(null, { status: 304 });
103
+ }
104
+ }
105
+
77
106
  try {
78
107
  // Read file and process with Sharp
79
108
  const buffer = await file.arrayBuffer();
@@ -105,6 +134,8 @@ app.get(
105
134
  headers: {
106
135
  "Content-Type": FORMAT_MIME[format],
107
136
  "Cache-Control": "public, max-age=31536000, immutable",
137
+ "Last-Modified": lastModified.toUTCString(),
138
+ ETag: etag,
108
139
  },
109
140
  });
110
141
  } catch (err) {