Back

Internal curl error: Puppeteer & Bun 1.0

  • 1
  • Self Hosted
  • Functions
elnur
11 Apr, 2024, 14:53

I get the following errors when trying to run a function with Puppeteer library using the Bun 1.0 runtime.

An internal curl error has occurred within the executor! Error Number: 6. Error Msg: Could not resolve host: someId\nError Code: 500

An internal curl error has occurred within the executor! Error Number: 104. Error Msg: Connection reset by peer\nError Code: 500

Full code below:

TL;DR
Developers are encountering internal curl errors when running a function with the Puppeteer library on Bun 1.0 runtime. The errors are related to failed host resolution and connection resets. To fix the issue, ensure proper hosting resolution and check for any connection reset problems.
elnur
11 Apr, 2024, 14:54

Full code:

TypeScript
import axios from "axios";
import { execSync } from "node:child_process";
import puppeteer from "puppeteer";

let installed = false;

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }: any) => {
  // Why not try the Appwrite SDK?
  //
  // const client = new Client()
  //    .setEndpoint('https://cloud.appwrite.io/v1')
  //    .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"])
  //    .setKey(Bun.env["APPWRITE_API_KEY"]);

  // Init!
  log("Function Invocation Started");

  // You can log messages to the console
  log("Hello, Logs!");

  // If something goes wrong, log an error
  error("Hello, Errors!");

  // The `req` object contains the request data
  if (req.method === "POST") {
    try {
      // Assuming the body is already parsed as JSON
      const { url } = JSON.parse(req.body);

      // Validate the URL
      if (!url || typeof url !== "string") {
        return res.json(
          { error: "Invalid or missing url ", data: url },
          400
        );
      }

      log(`Fetching HTML content for: ${url}`);

      // Use Axios to fetch the HTML content
      const response = await axios.get(url, {
        // Axios config: Receive response as a string
        responseType: "text",
      });

      if (response.data) {
        // Return the HTML content
        return res.send(response.data);
      } else {
        if (!installed) {
          try {
            log("Installing Chromium...");
            execSync(
              "apk update && apk add chromium nss freetype harfbuzz ca-certificates ttf-freefont",
              { stdio: "inherit" }
            );
            log("Chromium installed successfully...");
            installed = true;
          } catch (installError) {
            log(`Error installing```
elnur
11 Apr, 2024, 14:54

```Chromium: ${installError}); return res.send( Error installing Chromium: ${installError.message}` ); } } else { log("Chromium already installed."); } try { const browser = await puppeteer.launch({ headless: true, executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || "chromium-browser", args: ["--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage"], }); log("Puppeteer launched successfully.");

TypeScript
      const page = await browser.newPage();
      log("Browser page opened.");

      const response = await page.goto(url, {
        waitUntil: "networkidle0",
      });
      log(`Page loaded with status code: ${response.status()}`);

      const content = await page.content();
      log("Content retrieved.");

      await browser.close();
      log("Browser session closed.");```
elnur
11 Apr, 2024, 14:54
TypeScript
          if (content) {
            return res.send(content);
          } else {
            return res.send("No HTML document was found for the given URL");
          }
        } catch (error) {
          error(`Error during Puppeteer operations: ${error}`);
          return res.send(`Error: ${error.message}`);
        }
      }
    } catch (err) {
      error(`Error fetching document: ${err.message}`);
      return res.json({ error: `Could not fetch document: ${err.message}` }, 500);
    }
  } else {
    // Method not allowed
    return res.json({ error: "Method Not Allowed" }, 405);
  }
};```
elnur
11 Apr, 2024, 14:56

Image of executions

elnur
11 Apr, 2024, 14:56
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more