Skip to content

Commit

Permalink
Restrict functions to 1 max instance
Browse files Browse the repository at this point in the history
  • Loading branch information
chetbox committed Jan 1, 2025
1 parent 07a357c commit 720143f
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,39 @@ function setColor(png: PNG, x: number, y: number, color: PaletteColor) {
png.data[index + 3] = color.a;
}

export const imagePng = functions.https.onRequest(async (request, response) => {
try {
const [depthSnapshot, canvasSnapshot] = await Promise.all([
database.ref("canvas").child(IMAGE_ID).child("depth").once("value"),
database.ref("canvas").child(IMAGE_ID).child("canvas").once("value"),
]);
const depth = depthSnapshot.val() as Canvas["depth"];
const canvas = canvasSnapshot.val() as Canvas["canvas"];
export const imagePng = functions
.runWith({ maxInstances: 1 })
.https.onRequest(async (request, response) => {
try {
const [depthSnapshot, canvasSnapshot] = await Promise.all([
database.ref("canvas").child(IMAGE_ID).child("depth").once("value"),
database.ref("canvas").child(IMAGE_ID).child("canvas").once("value"),
]);
const depth = depthSnapshot.val() as Canvas["depth"];
const canvas = canvasSnapshot.val() as Canvas["canvas"];

const size = Math.pow(2, depth);
const image = new PNG({ width: size, height: size });
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const colorIndex = colorIndexFrom(depth, canvas, x, y);
if (colorIndex !== undefined) {
setColor(image, x, y, Palette[colorIndex]);
const size = Math.pow(2, depth);
const image = new PNG({ width: size, height: size });
for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
const colorIndex = colorIndexFrom(depth, canvas, x, y);
if (colorIndex !== undefined) {
setColor(image, x, y, Palette[colorIndex]);
}
}
}
const packedImage = image.pack();
packedImage
.pipe(response)
.set("Cache-Control", "public, max-age=86400, s-maxage=86400")
.type("image/png");
} catch (error) {
response.status(500).send(error);
}
const packedImage = image.pack();
packedImage
.pipe(response)
.set("Cache-Control", "public, max-age=86400, s-maxage=86400")
.type("image/png");
} catch (error) {
response.status(500).send(error);
}
});
});

export const historyGif = functions
.runWith({ memory: "2GB", timeoutSeconds: 540 })
.runWith({ memory: "2GB", timeoutSeconds: 540, maxInstances: 1 })
.https.onRequest(async (request, response) => {
try {
const [depthSnapshot, historySnapshot] = await Promise.all([
Expand Down

0 comments on commit 720143f

Please sign in to comment.