https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • m

    maximillian

    04/08/2022, 9:03 AM
    That works for my use case, thanks
  • x

    xHyroM

    04/08/2022, 10:57 AM
    Okay, and what about Deno packages?
  • i

    Isaac McFadyen | YYZ01

    04/08/2022, 12:11 PM
    Deno packages are specific to the deno runtime so they won't work either.
  • i

    Isaac McFadyen | YYZ01

    04/08/2022, 12:12 PM
    And while you might find a non-nodejs email package, it also won't work. The issue here is that the Workers runtime doesn't proxy anything other than HTTP, so SMTP won't work.
  • x

    xHyroM

    04/08/2022, 12:16 PM
    Okay, ill try to use mailgun
  • s

    simpson

    04/08/2022, 1:54 PM
    are there any how-to guides on converting a worker to function for noobs?
  • k

    kian

    04/08/2022, 1:55 PM
    Not so much a how-to but there's https://developers.cloudflare.com/pages/platform/functions/ - which has a section for converting a Worker. https://developers.cloudflare.com/pages/platform/functions/#migrating-from-workers
  • s

    simpson

    04/08/2022, 2:01 PM
    all we are doing is uploading or deleting an image from Cloudflare Images
    Copy code
    addEventListener('fetch', event => {
      const { request } = event;
      const { url } = request;
    
      if (request.method === 'POST') {
        return event.respondWith(handleRequest(request));
      } else if (request.method === 'GET') {
        return event.respondWith(new Response(`The request was a GET`));
      }
    });
    
    
    /**
     * Many more examples available at:
     *   https://developers.cloudflare.com/workers/examples
     * @param {Request} request
     * @returns {Promise<Response>}
     */
    async function handleRequest(request) {
    
      const authToken = await request.headers.get("authorization");
    
      const formData = await request.formData();
    
      const oldImageId = formData.get("old_image_id");
    
      formData.delete("old_image_id");
    
      const response = await fetch(
        `https://api.cloudflare.com/client/v4/accounts/63d719b39174eb1cfc9816ccf9a7512f/images/v1`,
        {
          method: "POST",
          body: formData,
          headers: {
            Authorization: `Bearer ######`,
          },
        }
      );
    
      await fetch(
        `https://api.cloudflare.com/client/v4/accounts/63d719b39174eb1cfc9816ccf9a7512f/images/v1/${oldImageId}`,
        {
          method: "DELETE",
          headers: {
            Authorization: `Bearer #######`,
          },
        }
      );
    
      return response;
    }
  • s

    simpson

    04/08/2022, 6:32 PM
    but we can't get this to work in functions
  • h

    Hazard

    04/08/2022, 11:26 PM
    How do I test functions locally with SvelteKit?
  • e

    Evаn

    04/08/2022, 11:28 PM
    Do you mean manual tests or integration tests?
  • e

    Evаn

    04/08/2022, 11:28 PM
    The dev server should Just Work for backend routes
  • h

    Hazard

    04/08/2022, 11:28 PM
    i mean run dev server locally
  • h

    Hazard

    04/08/2022, 11:29 PM
    yeah but, im getting confused by the "serve a folder of static assets"
  • h

    Hazard

    04/08/2022, 11:29 PM
    i've used normal workers before, but unsure how to run the dev server locally with sveltekit
  • h

    Hazard

    04/08/2022, 11:30 PM
    does the SvelteKit adapter do output of worker code into the ./dist folder?
  • h

    Hazard

    04/08/2022, 11:30 PM
    actually lemme give that a try
  • e

    Evаn

    04/08/2022, 11:31 PM
    The work there is done by
    adapter-cloudflare
    yep, it builds it into
    .svelte-kit/cloudflare
  • h

    Hazard

    04/08/2022, 11:31 PM
    very cool
  • e

    Evаn

    04/08/2022, 11:32 PM
    but you should just be able to use
    svelte-kit dev
    , no need to build
  • e

    Evаn

    04/08/2022, 11:32 PM
    unless you're using CF-specific stuff like KV or durable objects
  • h

    Hazard

    04/08/2022, 11:32 PM
    i was thinking of using KV
  • e

    Evаn

    04/08/2022, 11:33 PM
    you can run miniflare on the build output
  • e

    Evаn

    04/08/2022, 11:33 PM
    but the way I do it is by mocking KV for development so I can still use
    svelte-kit dev
    Copy code
    ts
    /** For development */
    async function getDummyStore() {
      const { Miniflare } = await import("miniflare")
      const mf = new Miniflare({
        script: "",
        kvNamespaces: ["STORE"],
        kvPersist: true
      })
      return mf.getKVNamespace("STORE")
    }
  • h

    Hazard

    04/08/2022, 11:34 PM
    looks like a nice solution
  • s

    Seb

    04/09/2022, 8:03 AM
    Is it possible to add npm packages to be used in functions, and if so how?
  • s

    simpson

    04/09/2022, 12:34 PM
    Can anyone help point this functions noob in the right direction? We really want to move our rogue worker in to a function so it is packaged with the rest of our app
  • s

    Stew

    04/09/2022, 3:25 PM
    Has anyone had issues purging cache from functions? I add header
    response.headers.set('Cloudflare-CDN-Cache-Control', 'public, max-age=120')
    Which works fine, I see the age header on subsequent refreshes increment up to 120 then the page is fetched from origin again. But if I purge the cache, both `by url `and
    purge everything
    , it still increments up to 120 before fetching from origin again. I'd expect it to fetch from origin within the ~30s purge window. The only thing I notice is this response header
    cf-cache-status: DYNAMIC
    , this should be
    HIT
    right? (not sure why it's showing dynamic when it does cache successfully, my function just returns the current timestamp so I know it's working fine)
  • s

    Stew

    04/09/2022, 3:46 PM
    I can see static assets get cached and purged successfully, just the functions don't behave how I expect
  • i

    Isaac McFadyen | YYZ01

    04/09/2022, 4:00 PM
    So Functions shouldn't, AFAIK, have the cache applied in front of them.
1...113114115...392Latest