This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent sharp-related memory leak
Probably this: lovell/sharp#1803. Tried pngjs (not compatible enough), jimp (too slow), and finally back to canvas which we've used before. It is a native dependency but doesn't seem to have the particular pathological behavior that sharp does while loading blank pages repeatedly.
- Loading branch information
1 parent
8d6ba26
commit baabf57
Showing
10 changed files
with
556 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createCanvas, createImageData, loadImage } from 'canvas' | ||
|
||
function ensureImageData(imageData: ImageData): ImageData { | ||
return createImageData(imageData.data, imageData.width, imageData.height) | ||
} | ||
|
||
export async function loadImageData(path: string): Promise<ImageData> | ||
export async function loadImageData(data: Buffer): Promise<ImageData> | ||
export async function loadImageData( | ||
pathOrData: string | Buffer | ||
): Promise<ImageData> { | ||
const img = await loadImage(pathOrData) | ||
const canvas = createCanvas(img.width, img.height) | ||
const context = canvas.getContext('2d') | ||
context.drawImage(img, 0, 0) | ||
return context.getImageData(0, 0, img.width, img.height) | ||
} | ||
|
||
export async function toPNG(imageData: ImageData): Promise<Buffer> { | ||
const canvas = createCanvas(imageData.width, imageData.height) | ||
const context = canvas.getContext('2d') | ||
context.putImageData(ensureImageData(imageData), 0, 0) | ||
return canvas.toBuffer() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.