@uxf/resizer 14.0.0 → 15.0.0
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/README.md +1 -1
- package/package.json +5 -5
- package/src/handler.js +20 -3
- package/src/handler.test.js +3 -0
- package/src/handler.test.ts +6 -1
- package/src/handler.ts +22 -4
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ The only runtime dependency is `sharp`. The standalone server additionally needs
|
|
|
26
26
|
yarn add @uxf/resizer hono @hono/node-server yargs --dev
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
Node `>=
|
|
29
|
+
Node `>= 26` is required (route matching uses the built-in `URLPattern`).
|
|
30
30
|
|
|
31
31
|
## Quick start
|
|
32
32
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uxf/resizer",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "15.0.0",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"description": "UXF Resizer",
|
|
6
6
|
"author": "UXFans <dev@uxf.cz>",
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
"sharp": "^0.35.5"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@hono/node-server": "2.1.
|
|
23
|
-
"@types/node": "
|
|
24
|
-
"hono": "4.13.
|
|
22
|
+
"@hono/node-server": "2.1.5",
|
|
23
|
+
"@types/node": "26",
|
|
24
|
+
"hono": "4.13.14",
|
|
25
25
|
"tsx": "4.23.15",
|
|
26
26
|
"yargs": "18.2.0"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
29
|
-
"node": ">=
|
|
29
|
+
"node": ">=26"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc -P tsconfig.json",
|
package/src/handler.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createResizerHandler = createResizerHandler;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
4
5
|
const fs_1 = require("fs");
|
|
5
6
|
const promises_1 = require("fs/promises");
|
|
6
7
|
const path_1 = require("path");
|
|
@@ -29,6 +30,21 @@ async function fileResponse(filename, contentType) {
|
|
|
29
30
|
},
|
|
30
31
|
});
|
|
31
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Writes into a temporary file next to the target and renames it into place, so a reader sharing the cache
|
|
35
|
+
* directory (nginx `try_files`, another resizer instance) never sees a partially written file, and a failed
|
|
36
|
+
* write never leaves a broken file behind.
|
|
37
|
+
*/
|
|
38
|
+
async function writeAtomically(filename, write) {
|
|
39
|
+
const tmpFilename = `${filename}.${(0, crypto_1.randomUUID)()}.tmp`;
|
|
40
|
+
try {
|
|
41
|
+
await write(tmpFilename);
|
|
42
|
+
await (0, promises_1.rename)(tmpFilename, filename);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
await (0, promises_1.rm)(tmpFilename, { force: true });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
32
48
|
/**
|
|
33
49
|
* Builds a framework-agnostic request handler for the configured routes: the same `Request -> Response`
|
|
34
50
|
* shape the Next.js route handlers use, so it can sit behind Hono, `@hono/node-server`, or any other
|
|
@@ -65,15 +81,16 @@ function createResizerHandler(config) {
|
|
|
65
81
|
const isRemote = /^https?:\/\//.test(sourceFilename);
|
|
66
82
|
if (params.toFormat === "svg") {
|
|
67
83
|
if (isRemote) {
|
|
68
|
-
|
|
84
|
+
const svg = Buffer.from(await (0, fetch_source_1.fetchSource)(sourceFilename));
|
|
85
|
+
await writeAtomically(generatedFilename, (tmpFilename) => (0, promises_1.writeFile)(tmpFilename, svg));
|
|
69
86
|
}
|
|
70
87
|
else {
|
|
71
|
-
await (0, promises_1.copyFile)(sourceFilename,
|
|
88
|
+
await writeAtomically(generatedFilename, (tmpFilename) => (0, promises_1.copyFile)(sourceFilename, tmpFilename));
|
|
72
89
|
}
|
|
73
90
|
return await fileResponse(generatedFilename, contentType);
|
|
74
91
|
}
|
|
75
92
|
const sourceFile = isRemote ? await (0, fetch_source_1.fetchSource)(sourceFilename) : sourceFilename;
|
|
76
|
-
await (0, image_converter_1.convertImage)(sourceFile,
|
|
93
|
+
await writeAtomically(generatedFilename, (tmpFilename) => (0, image_converter_1.convertImage)(sourceFile, tmpFilename, params));
|
|
77
94
|
return await fileResponse(generatedFilename, contentType);
|
|
78
95
|
}
|
|
79
96
|
catch (e) {
|
package/src/handler.test.js
CHANGED
|
@@ -17,6 +17,7 @@ let sourceDir;
|
|
|
17
17
|
let generatePath;
|
|
18
18
|
let handler;
|
|
19
19
|
let logSpy;
|
|
20
|
+
const tmpFilesIn = (dir) => (0, fs_1.existsSync)(dir) ? (0, fs_1.readdirSync)(dir, { recursive: true }).filter((f) => String(f).endsWith(".tmp")) : [];
|
|
20
21
|
beforeAll(async () => {
|
|
21
22
|
workDir = (0, fs_1.mkdtempSync)((0, path_1.join)((0, os_1.tmpdir)(), "uxf-resizer-"));
|
|
22
23
|
sourceDir = (0, path_1.join)(workDir, "source");
|
|
@@ -42,6 +43,7 @@ test("generates, caches and serves a resized image", async () => {
|
|
|
42
43
|
expect(response.headers.get("Content-Type")).toBe("image/webp");
|
|
43
44
|
expect(response.headers.get("Last-Modified")).toBeTruthy();
|
|
44
45
|
expect((0, fs_1.existsSync)(generatePath + path)).toBe(true);
|
|
46
|
+
expect(tmpFilesIn(generatePath)).toEqual([]);
|
|
45
47
|
const body = await response.arrayBuffer();
|
|
46
48
|
expect(response.headers.get("Content-Length")).toBe(String(body.byteLength));
|
|
47
49
|
const metadata = await (0, sharp_1.default)(Buffer.from(body)).metadata();
|
|
@@ -61,5 +63,6 @@ test("responds 404 when the source file is missing", async () => {
|
|
|
61
63
|
const response = await handler(new Request("http://localhost/generated/static/20_x_cv_c_FFF_nt_x/1/missing.png.webp"));
|
|
62
64
|
expect(response.status).toBe(404);
|
|
63
65
|
expect(errorSpy).toHaveBeenCalled();
|
|
66
|
+
expect(tmpFilesIn(generatePath)).toEqual([]);
|
|
64
67
|
errorSpy.mockRestore();
|
|
65
68
|
});
|
package/src/handler.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @jest-environment node
|
|
3
3
|
*/
|
|
4
|
-
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "fs";
|
|
4
|
+
import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync } from "fs";
|
|
5
5
|
import { tmpdir } from "os";
|
|
6
6
|
import { join } from "path";
|
|
7
7
|
import sharp from "sharp";
|
|
@@ -16,6 +16,9 @@ let generatePath: string;
|
|
|
16
16
|
let handler: (request: Request) => Promise<Response>;
|
|
17
17
|
let logSpy: jest.SpyInstance;
|
|
18
18
|
|
|
19
|
+
const tmpFilesIn = (dir: string) =>
|
|
20
|
+
existsSync(dir) ? readdirSync(dir, { recursive: true }).filter((f) => String(f).endsWith(".tmp")) : [];
|
|
21
|
+
|
|
19
22
|
beforeAll(async () => {
|
|
20
23
|
workDir = mkdtempSync(join(tmpdir(), "uxf-resizer-"));
|
|
21
24
|
sourceDir = join(workDir, "source");
|
|
@@ -47,6 +50,7 @@ test("generates, caches and serves a resized image", async () => {
|
|
|
47
50
|
expect(response.headers.get("Content-Type")).toBe("image/webp");
|
|
48
51
|
expect(response.headers.get("Last-Modified")).toBeTruthy();
|
|
49
52
|
expect(existsSync(generatePath + path)).toBe(true);
|
|
53
|
+
expect(tmpFilesIn(generatePath)).toEqual([]);
|
|
50
54
|
|
|
51
55
|
const body = await response.arrayBuffer();
|
|
52
56
|
expect(response.headers.get("Content-Length")).toBe(String(body.byteLength));
|
|
@@ -75,5 +79,6 @@ test("responds 404 when the source file is missing", async () => {
|
|
|
75
79
|
|
|
76
80
|
expect(response.status).toBe(404);
|
|
77
81
|
expect(errorSpy).toHaveBeenCalled();
|
|
82
|
+
expect(tmpFilesIn(generatePath)).toEqual([]);
|
|
78
83
|
errorSpy.mockRestore();
|
|
79
84
|
});
|
package/src/handler.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { randomUUID } from "crypto";
|
|
1
2
|
import { existsSync } from "fs";
|
|
2
|
-
import { copyFile, mkdir, readFile, stat, writeFile } from "fs/promises";
|
|
3
|
+
import { copyFile, mkdir, readFile, rename, rm, stat, writeFile } from "fs/promises";
|
|
3
4
|
import { dirname } from "path";
|
|
4
5
|
import { fetchSource } from "./utils/fetch-source";
|
|
5
6
|
import { getSourceFilename } from "./utils/get-source-filename";
|
|
@@ -46,6 +47,22 @@ async function fileResponse(filename: string, contentType: string): Promise<Resp
|
|
|
46
47
|
});
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Writes into a temporary file next to the target and renames it into place, so a reader sharing the cache
|
|
52
|
+
* directory (nginx `try_files`, another resizer instance) never sees a partially written file, and a failed
|
|
53
|
+
* write never leaves a broken file behind.
|
|
54
|
+
*/
|
|
55
|
+
async function writeAtomically(filename: string, write: (tmpFilename: string) => Promise<unknown>): Promise<void> {
|
|
56
|
+
const tmpFilename = `${filename}.${randomUUID()}.tmp`;
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
await write(tmpFilename);
|
|
60
|
+
await rename(tmpFilename, filename);
|
|
61
|
+
} finally {
|
|
62
|
+
await rm(tmpFilename, { force: true });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
49
66
|
/**
|
|
50
67
|
* Builds a framework-agnostic request handler for the configured routes: the same `Request -> Response`
|
|
51
68
|
* shape the Next.js route handlers use, so it can sit behind Hono, `@hono/node-server`, or any other
|
|
@@ -93,16 +110,17 @@ export function createResizerHandler(config: Config): (request: Request) => Prom
|
|
|
93
110
|
|
|
94
111
|
if (params.toFormat === "svg") {
|
|
95
112
|
if (isRemote) {
|
|
96
|
-
|
|
113
|
+
const svg = Buffer.from(await fetchSource(sourceFilename));
|
|
114
|
+
await writeAtomically(generatedFilename, (tmpFilename) => writeFile(tmpFilename, svg));
|
|
97
115
|
} else {
|
|
98
|
-
await copyFile(sourceFilename,
|
|
116
|
+
await writeAtomically(generatedFilename, (tmpFilename) => copyFile(sourceFilename, tmpFilename));
|
|
99
117
|
}
|
|
100
118
|
|
|
101
119
|
return await fileResponse(generatedFilename, contentType);
|
|
102
120
|
}
|
|
103
121
|
|
|
104
122
|
const sourceFile = isRemote ? await fetchSource(sourceFilename) : sourceFilename;
|
|
105
|
-
await convertImage(sourceFile,
|
|
123
|
+
await writeAtomically(generatedFilename, (tmpFilename) => convertImage(sourceFile, tmpFilename, params));
|
|
106
124
|
|
|
107
125
|
return await fileResponse(generatedFilename, contentType);
|
|
108
126
|
} catch (e) {
|