Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pureimageの代わりに@napi-rs/canvasを使う #13748

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions packages/backend/src/misc/gen-identicon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* 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';

Expand Down Expand Up @@ -45,9 +45,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 +101,5 @@ export function genIdenticon(seed: string, stream: WriteStream): Promise<void> {
}
}

return p.encodePNGToStream(canvas, stream);
return await canvas.encode('png');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

streamは使えない感じかしら

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使う必要あるのか(普通にBufferでよくねと思って書いた

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パフォーマンスが結構違いそう

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(というかブラウザAPIではストリームを吐かせる機能ないのでその気分で書いた

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

とりあえず@napi-rs/canvasにはstream使う機能はないわね

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パフォーマンスが結構違いそう

そうかしら?

(前の実装ってファイルシステムに入れて出すみたいな感じなので普通に遅い気がするんだけど)

}
4 changes: 1 addition & 3 deletions packages/backend/src/server/ServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,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.

Loading