Skip to content

Commit

Permalink
chore: image cache warmup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Dec 1, 2024
1 parent 24b7118 commit bb333c9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"copy-files-from-to": "^3.12.0",
"jimp": "^1.6.0",
"lint-staged": "^15.2.10",
"nano-memoize": "^3.0.16",
"prettier": "^3.4.1",
"pretty-quick": "^4.0.0",
"rimraf": "^6.0.1",
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions tests/blackbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
parseExpectedResult,
parseExpectedText,
snapshotResult,
warmUpCache,
} from "./utils.js";

type Type = "fast" | "slow" | "pure";
Expand Down Expand Up @@ -70,6 +71,14 @@ test("consistent test entries", async () => {
expect(testDirs).toEqual(testEntries.map(({ directory }) => directory));
});

await getZXingModule({
wasmBinary: (
await readFile(
resolve(import.meta.dirname, "../src/reader/zxing_reader.wasm"),
)
).buffer as ArrayBuffer,
});

for (const {
directory,
barcodeFormat,
Expand All @@ -80,13 +89,6 @@ for (const {
readerOptions = DEFAULT_READER_OPTIONS_FOR_TESTS,
} of testEntries) {
describe(directory, async () => {
beforeAll(async () => {
await getZXingModule({
wasmBinary: await readFile(
resolve(import.meta.dirname, "../src/reader/zxing_reader.wasm"),
),
});
});
const types = [
...(testFast ? ["fast"] : []),
...(testSlow ? ["slow"] : []),
Expand All @@ -111,6 +113,9 @@ for (const {
parseExpectedBinary(imagePath),
]);
describe(`${directory} ${imageName}`, async () => {
beforeAll(async () => {
await warmUpCache(imagePath, rotations);
});
afterAll(() => {
if (passAll) {
++summary.passAll;
Expand Down
60 changes: 38 additions & 22 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { format, parse } from "node:path";
import { Jimp } from "jimp";
import memoize from "nano-memoize";
import {
type BarcodeFormat,
type ReadResult,
Expand All @@ -20,29 +19,46 @@ export const DEFAULT_READER_OPTIONS_FOR_TESTS: ReaderOptions = {
maxNumberOfSymbols: 1,
};

const getJimpImage = memoize(async (imagePath: string) => {
return Jimp.read(imagePath);
});
type ProvidedMimeType = Parameters<
Awaited<ReturnType<typeof Jimp.read>>["getBuffer"]
>[0];

export const getRotatedImage = memoize(
async (imagePath: string, rotation: number) => {
if (rotation === 0) {
return readFile(imagePath);
}
const jimpImage = (await getJimpImage(imagePath)).clone();
return jimpImage
.rotate(rotation)
.getBuffer(
(jimpImage.mime ?? "image/png") as
| "image/bmp"
| "image/tiff"
| "image/x-ms-bmp"
| "image/gif"
| "image/jpeg"
| "image/png",
const [warmUpCache, getRotatedImage] = (() => {
const cache = new Map<string, Map<number, Buffer>>();
return [
async (imagePath: string, rotations: number[]) => {
if (cache.has(imagePath)) {
return;
}
const imageCache = new Map<number, Buffer>();
imageCache.set(0, await readFile(imagePath));
cache.set(imagePath, imageCache);
await Promise.all(
rotations.map(async (rotation) => {
if (rotation === 0) {
return;
}
const jimpImage = (await Jimp.read(imageCache.get(0)!)).clone();
imageCache.set(
rotation,
await jimpImage
.rotate(rotation)
.getBuffer((jimpImage.mime ?? "image/png") as ProvidedMimeType),
);
}),
);
},
);
},
async (imagePath: string, rotation: number) => {
const imageCache = cache.get(imagePath)?.get(rotation);
if (!imageCache) {
throw new Error("Cache not warmed up");
}
return imageCache;
},
];
})();

export { warmUpCache, getRotatedImage };

export function escapeNonGraphical(str: string): string {
const asciiNongraphs = [
Expand Down

0 comments on commit bb333c9

Please sign in to comment.