Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kaciras-blog/image

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kaciras-blog/image - npm Package Compare versions

Comparing version 0.6.0 to 0.6.2

20

__tests__/build-image-cache.spec.js

@@ -10,21 +10,25 @@ "use strict";

const build_image_cache_1 = require("../lib/build-image-cache");
const tmpdir = fs_extra_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "test-"));
beforeEach(() => fs_extra_1.default.emptyDir(tmpdir));
const ROOT = fs_extra_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "test-"));
const NAME = "742c5c7e26d80750f1c32f0bbcf0caab4e41678d03f5d610c3a1057b274b2268.png";
beforeEach(() => {
fs_extra_1.default.emptyDirSync(ROOT);
fs_extra_1.default.ensureDirSync(path_1.default.join(ROOT, "image"));
});
it('should generate caches', async () => {
const picture = fs_extra_1.default.readFileSync(path_1.default.join(__dirname, "fixtures/color_text_black_bg.png"));
fs_extra_1.default.writeFileSync(path_1.default.join(tmpdir, "742c5c7e26d80750f1c32f0bbcf0caab4e41678d03f5d610c3a1057b274b2268.png"), picture);
await build_image_cache_1.buildCache(tmpdir);
expect(fs_extra_1.default.readdirSync(path_1.default.join(tmpdir, "cache/png"))).toHaveLength(1);
fs_extra_1.default.writeFileSync(path_1.default.join(ROOT, "image", NAME), picture);
await build_image_cache_1.buildCache(ROOT);
expect(fs_extra_1.default.readdirSync(path_1.default.join(ROOT, "cache/png"))).toHaveLength(1);
});
it('should skip existing cache files', async () => {
const name = "742c5c7e26d80750f1c32f0bbcf0caab4e41678d03f5d610c3a1057b274b2268.png";
const cacheDir = path_1.default.join(tmpdir, "cache/png/");
const cacheDir = path_1.default.join(ROOT, "cache/png/");
const cacheFile = path_1.default.join(cacheDir, name);
const picture = fs_extra_1.default.readFileSync(path_1.default.join(__dirname, "fixtures/color_text_black_bg.png"));
fs_extra_1.default.writeFileSync(path_1.default.join(tmpdir, name), picture);
fs_extra_1.default.writeFileSync(path_1.default.join(ROOT, "image", name), picture);
fs_extra_1.default.ensureDirSync(cacheDir);
fs_extra_1.default.writeFileSync(cacheFile, "");
await build_image_cache_1.buildCache(tmpdir);
await build_image_cache_1.buildCache(ROOT);
expect(fs_extra_1.default.readFileSync(cacheFile)).toHaveLength(0);
});
//# sourceMappingURL=build-image-cache.spec.js.map

@@ -6,13 +6,17 @@ import os from "os";

const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), "test-"));
const ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "test-"));
const NAME = "742c5c7e26d80750f1c32f0bbcf0caab4e41678d03f5d610c3a1057b274b2268.png";
beforeEach(() => fs.emptyDir(tmpdir));
beforeEach(() => {
fs.emptyDirSync(ROOT);
fs.ensureDirSync(path.join(ROOT, "image"));
});
it('should generate caches', async () => {
const picture = fs.readFileSync(path.join(__dirname, "fixtures/color_text_black_bg.png"));
fs.writeFileSync(path.join(tmpdir, "742c5c7e26d80750f1c32f0bbcf0caab4e41678d03f5d610c3a1057b274b2268.png"), picture);
fs.writeFileSync(path.join(ROOT, "image", NAME), picture);
await buildCache(tmpdir);
await buildCache(ROOT);
expect(fs.readdirSync(path.join(tmpdir, "cache/png"))).toHaveLength(1);
expect(fs.readdirSync(path.join(ROOT, "cache/png"))).toHaveLength(1);
});

@@ -22,7 +26,7 @@

const name = "742c5c7e26d80750f1c32f0bbcf0caab4e41678d03f5d610c3a1057b274b2268.png";
const cacheDir = path.join(tmpdir, "cache/png/");
const cacheDir = path.join(ROOT, "cache/png/");
const cacheFile = path.join(cacheDir, name);
const picture = fs.readFileSync(path.join(__dirname, "fixtures/color_text_black_bg.png"));
fs.writeFileSync(path.join(tmpdir, name), picture);
fs.writeFileSync(path.join(ROOT, "image", name), picture);

@@ -32,5 +36,5 @@ fs.ensureDirSync(cacheDir);

await buildCache(tmpdir);
await buildCache(ROOT);
expect(fs.readFileSync(cacheFile)).toHaveLength(0);
});

12

lib/build-image-cache.js

@@ -14,11 +14,9 @@ "use strict";

}
async function buildCache(directory) {
const service = new image_service_1.PreGenerateImageService((key) => new IgnoreOriginSlot(directory, key));
const names = await fs_extra_1.default.readdir(directory);
async function buildCache(root) {
const service = new image_service_1.PreGenerateImageService((key) => new IgnoreOriginSlot(root, key));
const originDir = path_1.default.join(root, "image");
const names = await fs_extra_1.default.readdir(originDir);
let count = 0;
for (const name of names) {
const fullPath = path_1.default.join(directory, name);
if ((await fs_extra_1.default.stat(fullPath)).isDirectory()) {
continue;
}
const fullPath = path_1.default.join(originDir, name);
const parsed = path_1.default.parse(name);

@@ -25,0 +23,0 @@ const type = parsed.ext.substring(1);

@@ -18,17 +18,14 @@ import path from "path";

*
* @param directory 原图所在的目录
* @param root 数据目录
*/
export async function buildCache(directory: string) {
const service = new PreGenerateImageService((key) => new IgnoreOriginSlot(directory, key));
const names = await fs.readdir(directory);
export async function buildCache(root: string) {
const service = new PreGenerateImageService((key) => new IgnoreOriginSlot(root, key));
const originDir = path.join(root, "image");
const names = await fs.readdir(originDir);
let count = 0;
for (const name of names) {
const fullPath = path.join(directory, name);
// TODO: 设计错误,不应该把缓存目录放在原图目录里
if ((await fs.stat(fullPath)).isDirectory()) {
continue;
}
const fullPath = path.join(originDir, name);
const parsed = path.parse(name);

@@ -35,0 +32,0 @@ const type = parsed.ext.substring(1);

@@ -34,3 +34,3 @@ "use strict";

originPath() {
return path_1.default.join(this.root, `${this.key.name}.${this.key.type}`);
return path_1.default.join(this.root, "image", `${this.key.name}.${this.key.type}`);
}

@@ -48,2 +48,4 @@ cachePath(tags) {

function localFileStore(root) {
fs_extra_1.default.ensureDirSync(path_1.default.join(root, "image"));
fs_extra_1.default.ensureDirSync(path_1.default.join(root, "cache"));
return (key) => new LocalFileSlot(root, key);

@@ -50,0 +52,0 @@ }

@@ -63,3 +63,3 @@ import fs from "fs-extra";

private originPath() {
return path.join(this.root, `${this.key.name}.${this.key.type}`);
return path.join(this.root, "image", `${this.key.name}.${this.key.type}`);
}

@@ -79,3 +79,5 @@

export function localFileStore(root: string) {
fs.ensureDirSync(path.join(root, "image"));
fs.ensureDirSync(path.join(root, "cache"));
return (key: ImageKey) => new LocalFileSlot(root, key);
}
{
"name": "@kaciras-blog/image",
"version": "0.6.0",
"version": "0.6.2",
"description": "Image processing library for Kaciras Blog",

@@ -34,3 +34,3 @@ "license": "MIT",

},
"gitHead": "f5be77743d8e46eb51df45ef232175c6a2d00df3"
"gitHead": "e05ce688ff27d00f6161af431d200de6110590bf"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc