Closed billyadelphia closed 3 months ago
I cannot reproduce this locally using Bun 1.0.35 via the oven/bun
(Debian 11) Docker container.
If you're still having problems, please provide a complete code sample and image that allows someone else to reproduce.
I cannot reproduce this locally using Bun 1.0.35 via the
oven/bun
(Debian 11) Docker container.If you're still having problems, please provide a complete code sample and image that allows someone else to reproduce.
This is the entire code
Just my small service to remove the background image and then trim it using sharp
import Fastify from "fastify";
import { removeBackground } from "@imgly/background-removal-node";
const fastify = Fastify({
// logger: true
});
import { Buffer } from "buffer";
import cors from "@fastify/cors";
import axios from "axios";
import { writeFileSync } from "fs";
import { join } from "path";
import { randomBytes } from "crypto";
import imageType from "image-type";
import { fileURLToPath } from "url";
import { dirname } from "path";
import sharp from "sharp";
import fs from "fs";
const __dirname = dirname(fileURLToPath(import.meta.url));
await fastify.register(cors, {
origin: "*",
});
const downloadImage = async (url: string) => {
const response = await axios.get(url, { responseType: "arraybuffer" });
const buffer = Buffer.from(response.data, "binary");
// Get the image type from the buffer
const type = await imageType(buffer);
console.log("type", type);
// If the image type couldn't be detected, throw an error
if (!type) {
throw new Error("Could not detect image type");
}
// Generate a random filename with the correct extension
const filename = randomBytes(16).toString("hex") + "." + type.ext;
// Full path of the image
const imagePath = join(__dirname, "backgroundImages", filename);
// Save the image
writeFileSync(imagePath, buffer);
// Return the full path of the image
return imagePath;
};
// Declare a route
fastify.post("/remove-background", async function handler(request, reply) {
console.log("search");
const body = request.body as any;
const imageUrl = body.imageUrl;
const imageId = body.imageId;
console.log("body", body);
if (!imageUrl && !imageId) {
console.log("Invalid Request", "if (!imageUrl || !imageId) {");
return reply.code(400).send("Invalid Request");
}
if (!imageUrl.startsWith("http")) {
console.log("Invalid Request", "if (!imageUrl.startsWith('http')) {");
return reply.code(400).send("Invalid Request");
}
let result: Blob | undefined;
if (imageUrl) {
try {
//download the image, and save it to a file in backgroundImages folder
const data = await downloadImage(imageUrl);
console.log("removeBackground(data, {");
result = await removeBackground(data, {
debug: true,
output: { format: "image/png", quality: 1 },
});
//remove the image file
fs.unlinkSync(data);
} catch (e) {
console.log("err await removeBackground(imageUrl);", e);
return reply.code(400).send("Invalid Request");
}
}
console.log("theResultDone");
if (!result) {
return reply.code(400).send("Invalid Request");
}
const buffer = await result.arrayBuffer();
let theBase64 = Buffer.from(buffer).toString("base64"); // you can ignore this, it's just the part of the code before I implement sharp
// console.log("base64Image", theBase64); // This will log the base64 representation of the image
//convert result to base64 png
// theBase64 = `data:image/png;base64,${theBase64}`;
// console.log("theBase64", theBase64);
console.log("sharpingsharpingsharping");
const theSharp = await sharp(buffer).trim().toBuffer();
console.log("theSharp done");
theBase64 = theSharp.toString("base64");
theBase64 = `data:image/png;base64,${theBase64}`;
const data = {
base64: theBase64,
};
// console.log("data", data);
return reply.code(200).send(data);
});
// Run the server!
try {
await fastify.listen({ port: 5832 });
console.log(`server listening on ${fastify.server.address().port}`);
} catch (err) {
fastify.log.error("ulalafast", err);
process.exit(1);
}
Please can you provide a more minimal code sample that allows someone else to reproduce. For example, please remove all networking code, and please remove the other dependencies such as @imgly/background-removal-node
.
Perhaps create a complete, minimal, standalone repo that makes it as simple as possible for someone else to reproduce.
I've just spotted that @imgly/background-removal-node
depends on an old version of sharp and will not work on Bun.
Support for Bun was added in sharp v0.33.0.
Possible bug
Is this a possible bug in a feature of sharp, unrelated to installation?
bun install sharp
completes without error.bun -e "require('sharp')"
completes without error.Are you using the latest version of sharp?
sharp
as reported bynpm view sharp dist-tags.latest
. 0.33.3What is the output of running
npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp
?What are the steps to reproduce?