18 Commits

Author SHA1 Message Date
8566c5f57f 1.0.15 2023-12-03 23:09:49 +01:00
4cc5accff1 fix(core): update 2023-12-03 23:09:48 +01:00
2b82a4b74a 1.0.14 2023-12-03 19:04:37 +01:00
499b7e5f42 fix(core): update 2023-12-03 19:04:36 +01:00
6b1948d834 1.0.13 2023-12-03 18:57:45 +01:00
7b340ea783 fix(core): update 2023-12-03 18:57:44 +01:00
063905bfaa 1.0.12 2023-12-03 18:41:44 +01:00
f1ad45289a fix(core): update 2023-12-03 18:41:43 +01:00
193e174fd7 1.0.11 2023-12-03 18:10:36 +01:00
703d21c426 fix(core): update 2023-12-03 18:10:35 +01:00
2d83ebac4d 1.0.10 2023-12-03 16:35:52 +01:00
7454a52d03 fix(core): update 2023-12-03 16:35:51 +01:00
1e93429186 1.0.9 2023-12-03 16:32:53 +01:00
ee926e18a7 fix(core): update 2023-12-03 16:32:52 +01:00
02a6656869 1.0.8 2023-11-24 20:21:13 +01:00
b07aba76e9 fix(core): update 2023-11-24 20:21:12 +01:00
7fdeed160a 1.0.7 2023-11-24 20:09:24 +01:00
c6cd1dc38f fix(core): update 2023-11-24 20:09:23 +01:00
10 changed files with 1150 additions and 201 deletions

View File

@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build

View File

@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: registry.gitlab.com/hosttoday/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@gitea.lossless.digital/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @shipzone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true

View File

@ -1,6 +1,6 @@
{
"name": "@push.rocks/smartjimp",
"version": "1.0.6",
"version": "1.0.15",
"private": false,
"description": "a tool fr working with images in TypeScript",
"main": "dist_ts/index.js",
@ -17,7 +17,7 @@
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.8",
"@types/node": "^20.10.0"
"@types/node": "^20.10.2"
},
"dependencies": {
"@push.rocks/levelcache": "^3.0.3",
@ -25,8 +25,8 @@
"@push.rocks/smarthash": "^3.0.4",
"@push.rocks/smartpath": "^5.0.5",
"@push.rocks/smartrequest": "^2.0.15",
"@types/sharp": "^0.32.0",
"sharp": "^0.32.6"
"jimp": "^0.22.10",
"sharp": "^0.33.0"
},
"files": [
"ts/**/*",
@ -43,5 +43,13 @@
"type": "module",
"browserslist": [
"last 1 chrome versions"
]
],
"repository": {
"type": "git",
"url": "git+https://gitlab.com/push.rocks/smartjimp.git"
},
"bugs": {
"url": "https://gitlab.com/push.rocks/smartjimp/issues"
},
"homepage": "https://gitlab.com/push.rocks/smartjimp#readme"
}

1010
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ import * as smartfile from '@push.rocks/smartfile';
let testSmartJimp: smartjimp.SmartJimp;
tap.test('first test', async () => {
testSmartJimp = new smartjimp.SmartJimp();
testSmartJimp = new smartjimp.SmartJimp({ mode: 'sharp'});
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();
});
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();

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartjimp',
version: '1.0.6',
version: '1.0.15',
description: 'a tool fr working with images in TypeScript'
}

View File

@ -11,8 +11,3 @@ import * as smartpath from '@push.rocks/smartpath';
import * as smartrequest from '@push.rocks/smartrequest';
export { levelcache, smartpath, smarthash, smartfile, smartrequest };
// third party scope
import sharp from 'sharp';
export { sharp };

7
ts/plugins.typed.ts Normal file
View File

@ -0,0 +1,7 @@
import type * as sharpType from 'sharp';
export { type sharpType };
import type * as jimpType from 'jimp';
export { type jimpType };

View File

@ -1,10 +1,16 @@
import * as plugins from './smartjimp.plugins.js';
import * as plugins from './plugins.js';
import * as pluginsTyped from './plugins.typed.js';
export interface IDimensions {
export interface IAssetVariation {
format?: 'avif' | 'webp' | 'png';
width?: number;
height?: number;
}
export interface ISmartJimpOptions {
mode: 'sharp' | 'jimp';
}
export class SmartJimp {
public levelCache = new plugins.levelcache.LevelCache({
cacheId: 'mastercache',
@ -12,53 +18,109 @@ export class SmartJimp {
maxDiskStorageInMB: 5000,
});
public options: ISmartJimpOptions;
constructor(optionsArg: ISmartJimpOptions) {
this.options = optionsArg;
}
/**
* get a key that is unique for a wanted asset variation
*/
private getCacheKey(
sourceTypeArg: 'streamfile' | 'smartfile',
sourceIdArg: string,
wantedDimensionsArg?: IDimensions
assetVariationArg?: IAssetVariation
) {
return `${sourceTypeArg}_${sourceIdArg}_${
wantedDimensionsArg
? `${wantedDimensionsArg.width || 'auto' }x${wantedDimensionsArg.height || 'auto'}`
assetVariationArg
? `${assetVariationArg.width || 'auto'}x${assetVariationArg.height || 'auto'}`
: 'original'
}`;
}
private async computeAssetVariation(assetBuffer: Buffer, wantedDimensions?: IDimensions) {
if (!wantedDimensions) {
return assetBuffer;
sharpMod: typeof pluginsTyped.sharpType.default;
public async getSharpMod(): Promise<
typeof pluginsTyped.sharpType.default
> {
if (!this.sharpMod) {
this.sharpMod = (await import('sharp')).default;
}
return this.sharpMod;
}
jimpMod: typeof pluginsTyped.jimpType;
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType.default> {
if (!this.jimpMod) {
this.jimpMod = await import('jimp');
}
return this.jimpMod.default;
}
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);
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);
}
switch (assetVariationArg.format) {
case 'png':
return await jimpImage.getBufferAsync(jimp.MIME_PNG);
default:
return await jimpImage.getBufferAsync(jimp.MIME_JPEG);
}
}
let sharpImage = plugins.sharp(assetBuffer);
sharpImage = sharpImage.resize(wantedDimensions.width, wantedDimensions.height);
const result = await sharpImage.resize(wantedDimensions.width, wantedDimensions.height).avif().toBuffer();
return result;
}
public async getFromSmartfile(
smartfileArg: plugins.smartfile.SmartFile,
wantedDimensionsArg?: IDimensions
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);
if (existingCacheEntry) {
return existingCacheEntry.contents;
} else {
const computedAssetBuffer = await this.computeAssetVariation(smartfileArg.contentBuffer, wantedDimensionsArg);
this.levelCache.storeCacheEntryByKey(cacheKey, new plugins.levelcache.CacheEntry({
const computedAssetBuffer = await this.computeAssetVariation(
smartfileArg.contentBuffer,
wantedDimensionsArg
);
this.levelCache.storeCacheEntryByKey(
cacheKey,
new plugins.levelcache.CacheEntry({
contents: computedAssetBuffer,
ttl: 600000
}));
ttl: 600000,
})
);
return computedAssetBuffer;
}
}
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();
return result;
}
}

View File

@ -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 nogitDir = plugins.path.join(packageDir, '.nogit/')