Skip to content

Commit

Permalink
pureimageの代わりに@napi-rs/canvasを使う (#13748)
Browse files Browse the repository at this point in the history
* pureimageの代わりに@napi-rs/canvasを使う

* remove writestream

* remove createtemp
  • Loading branch information
tamaina authored Apr 27, 2024
1 parent 2fbaed9 commit 8c9d38b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@fastify/view": "9.0.0",
"@misskey-dev/sharp-read-bmp": "1.2.0",
"@misskey-dev/summaly": "5.1.0",
"@napi-rs/canvas": "^0.1.52",
"@nestjs/common": "10.3.8",
"@nestjs/core": "10.3.8",
"@nestjs/testing": "10.3.8",
Expand Down Expand Up @@ -153,7 +154,6 @@
"promise-limit": "2.7.0",
"pug": "3.0.2",
"punycode": "2.3.1",
"pureimage": "0.3.17",
"qrcode": "1.5.3",
"random-seed": "0.3.0",
"ratelimiter": "3.4.1",
Expand Down
9 changes: 4 additions & 5 deletions packages/backend/src/misc/gen-identicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
* https://en.wikipedia.org/wiki/Identicon
*/

import * as p from 'pureimage';
import { createCanvas } from '@napi-rs/canvas';
import gen from 'random-seed';
import type { WriteStream } from 'node:fs';

const size = 128; // px
const n = 5; // resolution
Expand Down Expand Up @@ -45,9 +44,9 @@ const sideN = Math.floor(n / 2);
/**
* Generate buffer of an identicon by seed
*/
export function genIdenticon(seed: string, stream: WriteStream): Promise<void> {
export async function genIdenticon(seed: string): Promise<Buffer> {
const rand = gen.create(seed);
const canvas = p.make(size, size, undefined);
const canvas = createCanvas(size, size);
const ctx = canvas.getContext('2d');

const bgColors = colors[rand(colors.length)];
Expand Down Expand Up @@ -101,5 +100,5 @@ export function genIdenticon(seed: string, stream: WriteStream): Promise<void> {
}
}

return p.encodePNGToStream(canvas, stream);
return await canvas.encode('png');
}
5 changes: 1 addition & 4 deletions packages/backend/src/server/ServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { DI } from '@/di-symbols.js';
import type Logger from '@/logger.js';
import * as Acct from '@/misc/acct.js';
import { genIdenticon } from '@/misc/gen-identicon.js';
import { createTemp } from '@/misc/create-temp.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
Expand Down Expand Up @@ -192,9 +191,7 @@ export class ServerService implements OnApplicationShutdown {
reply.header('Cache-Control', 'public, max-age=86400');

if ((await this.metaService.fetch()).enableIdenticonGeneration) {
const [temp, cleanup] = await createTemp();
await genIdenticon(request.params.x, fs.createWriteStream(temp));
return fs.createReadStream(temp).on('close', () => cleanup());
return await genIdenticon(request.params.x);
} else {
return reply.redirect('/static-assets/avatar.png');
}
Expand Down
120 changes: 99 additions & 21 deletions pnpm-lock.yaml

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

0 comments on commit 8c9d38b

Please sign in to comment.