Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
a2b408a7bf | |||
67fdcf0362 | |||
c3f840e1be | |||
6840d94517 | |||
4600cde549 | |||
33e8ba5f30 | |||
8566c5f57f | |||
4cc5accff1 | |||
2b82a4b74a | |||
499b7e5f42 | |||
6b1948d834 | |||
7b340ea783 | |||
063905bfaa | |||
f1ad45289a |
16
package.json
16
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@push.rocks/smartjimp",
|
"name": "@push.rocks/smartjimp",
|
||||||
"version": "1.0.11",
|
"version": "1.0.18",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a tool fr working with images in TypeScript",
|
"description": "a tool fr working with images in TypeScript",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -10,22 +10,24 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild --allowimplicitany)",
|
"build": "(tsbuild --allowimplicitany)",
|
||||||
"buildDocs": "tsdoc"
|
"buildDocs": "tsdoc",
|
||||||
|
"localPublish": "gitzone commit && pnpm run build && pnpm publish && pnpm publish --access public --registry=\"https://registry.npmjs.org\""
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@git.zone/tsbuild": "^2.1.66",
|
"@git.zone/tsbuild": "^2.1.72",
|
||||||
"@git.zone/tsrun": "^1.2.44",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"@git.zone/tstest": "^1.0.77",
|
"@git.zone/tstest": "^1.0.86",
|
||||||
"@push.rocks/tapbundle": "^5.0.8",
|
"@push.rocks/tapbundle": "^5.0.8",
|
||||||
"@types/node": "^20.10.2"
|
"@types/node": "^20.11.17"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@push.rocks/levelcache": "^3.0.3",
|
"@push.rocks/levelcache": "^3.0.8",
|
||||||
"@push.rocks/smartfile": "^11.0.4",
|
"@push.rocks/smartfile": "^11.0.4",
|
||||||
"@push.rocks/smarthash": "^3.0.4",
|
"@push.rocks/smarthash": "^3.0.4",
|
||||||
"@push.rocks/smartpath": "^5.0.5",
|
"@push.rocks/smartpath": "^5.0.5",
|
||||||
"@push.rocks/smartrequest": "^2.0.15",
|
"@push.rocks/smartrequest": "^2.0.15",
|
||||||
"sharp": "^0.33.0"
|
"jimp": "^0.22.10",
|
||||||
|
"sharp": "^0.33.2"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
1718
pnpm-lock.yaml
generated
1718
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
11
test/test.ts
11
test/test.ts
@ -5,7 +5,7 @@ import * as smartfile from '@push.rocks/smartfile';
|
|||||||
let testSmartJimp: smartjimp.SmartJimp;
|
let testSmartJimp: smartjimp.SmartJimp;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
testSmartJimp = new smartjimp.SmartJimp();
|
testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp'});
|
||||||
expect(testSmartJimp).toBeInstanceOf(smartjimp.SmartJimp);
|
expect(testSmartJimp).toBeInstanceOf(smartjimp.SmartJimp);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -15,4 +15,13 @@ tap.test('should be able to create a master', async () => {
|
|||||||
(await smartfile.SmartFile.fromBuffer('.nogit/result.avif', convertedAsset)).write();
|
(await smartfile.SmartFile.fromBuffer('.nogit/result.avif', convertedAsset)).write();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should be able to use jimp', async () => {
|
||||||
|
const testSmartJimp = new smartjimp.SmartJimp({ mode: 'jimp'});
|
||||||
|
const smartfileInstance = await smartfile.SmartFile.fromUrl('https://images.unsplash.com/photo-1673276628202-737bf3020ac2?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3774&q=80')
|
||||||
|
const convertedAsset = await testSmartJimp.computeAssetVariation(smartfileInstance.contents, {
|
||||||
|
format: 'png',
|
||||||
|
});
|
||||||
|
(await smartfile.SmartFile.fromBuffer('.nogit/result2.png', convertedAsset)).write();
|
||||||
|
})
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartjimp',
|
name: '@push.rocks/smartjimp',
|
||||||
version: '1.0.11',
|
version: '1.0.18',
|
||||||
description: 'a tool fr working with images in TypeScript'
|
description: 'a tool fr working with images in TypeScript'
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,3 @@ import * as smartpath from '@push.rocks/smartpath';
|
|||||||
import * as smartrequest from '@push.rocks/smartrequest';
|
import * as smartrequest from '@push.rocks/smartrequest';
|
||||||
|
|
||||||
export { levelcache, smartpath, smarthash, smartfile, smartrequest };
|
export { levelcache, smartpath, smarthash, smartfile, smartrequest };
|
||||||
|
|
||||||
// third party scope
|
|
||||||
import sharp from 'sharp';
|
|
||||||
|
|
||||||
export { sharp };
|
|
7
ts/plugins.typed.ts
Normal file
7
ts/plugins.typed.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import type * as sharpType from 'sharp';
|
||||||
|
|
||||||
|
export { type sharpType };
|
||||||
|
|
||||||
|
import type * as jimpType from 'jimp';
|
||||||
|
|
||||||
|
export { type jimpType };
|
@ -1,18 +1,29 @@
|
|||||||
import * as plugins from './smartjimp.plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
import * as pluginsTyped from './plugins.typed.js';
|
||||||
|
|
||||||
export interface IAssetVariation {
|
export interface IAssetVariation {
|
||||||
format?: 'avif' | 'webp' | 'png';
|
format?: 'avif' | 'webp' | 'png';
|
||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
|
invert?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ISmartJimpOptions {
|
||||||
|
mode: 'sharp' | 'jimp';
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartJimp {
|
export class SmartJimp {
|
||||||
public levelCache = new plugins.levelcache.LevelCache({
|
public levelCache = new plugins.levelcache.LevelCache({
|
||||||
cacheId: 'mastercache',
|
cacheId: 'mastercache',
|
||||||
maxMemoryStorageInMB: 100,
|
maxMemoryStorageInMB: 100,
|
||||||
maxDiskStorageInMB: 5000,
|
maxDiskStorageInMB: 5000,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
public options: ISmartJimpOptions;
|
||||||
|
constructor(optionsArg: ISmartJimpOptions) {
|
||||||
|
this.options = optionsArg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get a key that is unique for a wanted asset variation
|
* get a key that is unique for a wanted asset variation
|
||||||
*/
|
*/
|
||||||
@ -23,51 +34,100 @@ export class SmartJimp {
|
|||||||
) {
|
) {
|
||||||
return `${sourceTypeArg}_${sourceIdArg}_${
|
return `${sourceTypeArg}_${sourceIdArg}_${
|
||||||
assetVariationArg
|
assetVariationArg
|
||||||
? `${assetVariationArg.width || 'auto' }x${assetVariationArg.height || 'auto'}`
|
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
|
||||||
: 'original'
|
: 'original'
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg?: IAssetVariation) {
|
sharpMod: typeof pluginsTyped.sharpType.default;
|
||||||
if (!assetVariationArg) {
|
public async getSharpMod(): Promise<
|
||||||
return assetBufferArg;
|
typeof pluginsTyped.sharpType.default
|
||||||
|
> {
|
||||||
|
if (!this.sharpMod) {
|
||||||
|
this.sharpMod = (await import('sharp')).default;
|
||||||
}
|
}
|
||||||
let sharpImage = plugins.sharp(assetBufferArg);
|
return this.sharpMod;
|
||||||
sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
}
|
||||||
const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
|
||||||
switch (assetVariationArg.format) {
|
jimpMod: typeof pluginsTyped.jimpType;
|
||||||
case 'avif':
|
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType.default> {
|
||||||
sharpImage = resultResize.avif();
|
if (!this.jimpMod) {
|
||||||
case 'webp':
|
this.jimpMod = await import('jimp');
|
||||||
sharpImage = resultResize.webp();
|
}
|
||||||
case 'png':
|
return this.jimpMod.default;
|
||||||
sharpImage = resultResize.png();
|
}
|
||||||
|
|
||||||
|
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg: IAssetVariation) {
|
||||||
|
if (this.options.mode === 'sharp') {
|
||||||
|
const sharp = await this.getSharpMod();
|
||||||
|
if (!assetVariationArg) {
|
||||||
|
return assetBufferArg;
|
||||||
|
}
|
||||||
|
let sharpImage = sharp(assetBufferArg);
|
||||||
|
sharpImage = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
||||||
|
const resultResize = sharpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
||||||
|
if (assetVariationArg.invert) {
|
||||||
|
// TODO: implement invert
|
||||||
|
}
|
||||||
|
switch (assetVariationArg.format) {
|
||||||
|
case 'avif':
|
||||||
|
sharpImage = resultResize.avif();
|
||||||
|
case 'webp':
|
||||||
|
sharpImage = resultResize.webp();
|
||||||
|
case 'png':
|
||||||
|
sharpImage = resultResize.png();
|
||||||
|
}
|
||||||
|
return sharpImage.toBuffer();
|
||||||
|
} else if (this.options.mode === 'jimp') {
|
||||||
|
const jimp = await this.getJimpMod();
|
||||||
|
let jimpImage = await jimp.read(assetBufferArg);
|
||||||
|
if (assetVariationArg.width || assetVariationArg.height) {
|
||||||
|
jimpImage = jimpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
||||||
|
}
|
||||||
|
if (assetVariationArg.invert) {
|
||||||
|
jimpImage = jimpImage.invert();
|
||||||
|
}
|
||||||
|
switch (assetVariationArg.format) {
|
||||||
|
case 'png':
|
||||||
|
return await jimpImage.getBufferAsync(jimp.MIME_PNG);
|
||||||
|
default:
|
||||||
|
return await jimpImage.getBufferAsync(jimp.MIME_JPEG);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return sharpImage.toBuffer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getFromSmartfile(
|
public async getFromSmartfile(
|
||||||
smartfileArg: plugins.smartfile.SmartFile,
|
smartfileArg: plugins.smartfile.SmartFile,
|
||||||
wantedDimensionsArg?: IAssetVariation
|
wantedDimensionsArg?: IAssetVariation
|
||||||
) {
|
) {
|
||||||
const cacheKey = this.getCacheKey('smartfile', await smartfileArg.getHash(), wantedDimensionsArg);
|
const cacheKey = this.getCacheKey(
|
||||||
|
'smartfile',
|
||||||
|
await smartfileArg.getHash(),
|
||||||
|
wantedDimensionsArg
|
||||||
|
);
|
||||||
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
|
const existingCacheEntry = await this.levelCache.retrieveCacheEntryByKey(cacheKey);
|
||||||
if (existingCacheEntry) {
|
if (existingCacheEntry) {
|
||||||
return existingCacheEntry.contents;
|
return existingCacheEntry.contents;
|
||||||
} else {
|
} else {
|
||||||
const computedAssetBuffer = await this.computeAssetVariation(smartfileArg.contentBuffer, wantedDimensionsArg);
|
const computedAssetBuffer = await this.computeAssetVariation(
|
||||||
this.levelCache.storeCacheEntryByKey(cacheKey, new plugins.levelcache.CacheEntry({
|
smartfileArg.contentBuffer,
|
||||||
contents: computedAssetBuffer,
|
wantedDimensionsArg
|
||||||
ttl: 600000
|
);
|
||||||
}));
|
this.levelCache.storeCacheEntryByKey(
|
||||||
|
cacheKey,
|
||||||
|
new plugins.levelcache.CacheEntry({
|
||||||
|
contents: computedAssetBuffer,
|
||||||
|
ttl: 600000,
|
||||||
|
})
|
||||||
|
);
|
||||||
return computedAssetBuffer;
|
return computedAssetBuffer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createAvifImageFromBuffer(bufferArg: Buffer) {
|
public async createAvifImageFromBuffer(bufferArg: Buffer) {
|
||||||
const sharpImage = plugins.sharp(bufferArg);
|
const sharp = await this.getSharpMod();
|
||||||
|
const sharpImage = sharp(bufferArg);
|
||||||
const result = await sharpImage.avif().toBuffer();
|
const result = await sharpImage.avif().toBuffer();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './smartjimp.plugins.js';
|
import * as plugins from './plugins.js';
|
||||||
|
|
||||||
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
|
export const packageDir = plugins.path.join(plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
|
||||||
export const nogitDir = plugins.path.join(packageDir, '.nogit/')
|
export const nogitDir = plugins.path.join(packageDir, '.nogit/')
|
||||||
|
Reference in New Issue
Block a user