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

    bkyerv

    11/26/2022, 2:22 PM
    Yes, in the dashboard I declared variable PIC to my r2 namespace
  • k

    kian

    11/26/2022, 2:24 PM
    How big is the file you’re uploading
  • b

    bkyerv

    11/26/2022, 2:29 PM
    53kb
  • z

    zsmooth

    11/26/2022, 2:30 PM
    OK, so I got it working by doing this:
    Copy code
    const buffer = await fileUploaded.arrayBuffer();
      const bucketPut = await env.PIC.put(uuid, buffer, bucketPutOptions);
    However: you probably want to stream the contents to r2 and this will buffer it in the worker, and I have no idea why it works locally. Edit: Here's what you want:
    Copy code
    const bucketPut = await env.PIC.put(
        uuid,
        fileUploaded.stream(),
        bucketPutOptions
      );
  • b

    bkyerv

    11/26/2022, 2:33 PM
    what is the difference/impact between buffer method and stream method?
  • z

    zsmooth

    11/26/2022, 2:33 PM
    That gets a
    readableStream
    from the uploaded File and passes it along, so you're not buffering the file contents.
  • b

    bkyerv

    11/26/2022, 2:33 PM
    Thank you! Thank yoU!
  • z

    zsmooth

    11/26/2022, 2:34 PM
    this must be a quirk of the differences between the local dev env and production. when pages gets support for
    --experimental-local
    maybe your original code will stop working locally
  • j

    JustinNoel

    11/26/2022, 2:53 PM
    This code is specific to my app, but you could do something similar.
    Copy code
    type UsesMiddlewareProps = {
      url: string;
    };
    
    // Requests with these strings should not have middleware added to them.
    export const undesiredPaths = [
      ".css",
      ".ico",
      ".js",
      ".png",
      ".jpeg",
      ".jpg",
      ".svg",
      ".wepb",
      "/fonts/"
    ];
    
    export function getUsesMiddleware({url} : UsesMiddlewareProps): boolean {
      return ! undesiredPaths.some((undesiredString) => {
        return url.includes(undesiredString);
      });
    }
    I call
    getUsesMiddleware
    with a URL to decide if I need to do extra stuff with the request.
  • z

    zsmooth

    11/26/2022, 4:04 PM
    Continuing the logs discussion from #779390076219686943 … this morning as I was debugging, it seems like half the time I `wrangler pages publish`ed, the resulting deploy would never return any logs. Half the time it did. So i would connect, do some requests, and if no logs came through, I’d publish again and try again. Over about 10 deploys, it worked half the time.
  • z

    zsmooth

    11/26/2022, 4:04 PM
    Also, that wrangler table for deploys is terrible if your window isn’t wide enough because you can’t copy/paste the deploy url easily 🙂
  • s

    Skye

    11/26/2022, 4:05 PM
    I believe they're currently changing what does tables for wrangler
  • j

    JeremyJaydan

    11/27/2022, 10:01 PM
    Hey is there another way to access environment variables other than
    context.env
    ? I'm thinking of instantiating a faunadb client in a module then importing that, but then I'd have to dependency inject the secrets from where-ever I'm calling it from and I'm not sure about that. Alternatively I'm thinking of instantiating the client in middleware which would work but there's more logic I'd like to write associated with the faunadb client / other services. Also how would I pass that down in the middleware,
    context.env
    is that the practice? is there another preferred way of instantiating services to use across a cloudflare pages codebase? :)
  • j

    James

    11/27/2022, 10:05 PM
    You're pretty spot-on with
    context.env
    and passing it around. If it's something you plan to re-use in many places, you may want to extract it to your own Pages Plugin: https://developers.cloudflare.com/pages/platform/functions/plugins/ But that doesn't change much - it's essentially just reusable middleware.
  • j

    JeremyJaydan

    11/27/2022, 10:05 PM
    ah that makes sense! I'll look into plugins thanks!
  • s

    Skye

    11/27/2022, 10:06 PM
    If you want to pass stuff between middlewares, you should use
    context.data
  • s

    Skye

    11/27/2022, 10:06 PM
    (it's just an object - you can put whatever there)
  • j

    JeremyJaydan

    11/27/2022, 10:15 PM
    planning on passing a class instance with methods, what's the difference between
    env
    and
    data
    ? also I'm concerned about compatibility in the future for example if I set a
    env.HOUSE
    for example then cloudflare releases a binding called
    env.HOUSE
    , would that be an issue?
  • s

    Skye

    11/27/2022, 10:16 PM
    env is for your bindings, I wouldn't advise you pollute it. Data is specifically for passing whatever between middleware
  • s

    Skye

    11/27/2022, 10:16 PM
    (also you would have to add the
    HOUSE
    binding yourself - it wouldn't suddenly just appear and overwrite your functions)
  • j

    JeremyJaydan

    11/27/2022, 10:17 PM
    ah ok I'm just thinking because
    env.ASSETS
    is a default binding right? I'm guessing cloudflare doesn't want to pollute it either
  • s

    Skye

    11/27/2022, 10:18 PM
    that's the only one - and it's specific to pages, not workers as a whole
  • s

    Skye

    11/27/2022, 10:18 PM
    that was present from the beginning of functions existing, so it wasn't a breaking change
  • j

    JeremyJaydan

    11/27/2022, 10:19 PM
    ok that makes sense
  • j

    JeremyJaydan

    11/27/2022, 10:19 PM
    thanks :)
  • a

    adaptive

    11/28/2022, 1:44 PM
    I monitor the age of my workers and noticed that one function didn't evict after pushing the new code for the same function. This is an isolated event. The worker was 7000 seconds old, and still alive not being purged. Have you seen such behaviour before?
  • n

    nightshade427

    11/28/2022, 4:28 PM
    Is there a way with pages directory mode to create a queue consumer?
  • j

    James

    11/28/2022, 4:40 PM
    Not at this time, hopefully in future 🙂
  • n

    nightshade427

    11/28/2022, 4:56 PM
    Okay thanks for the heads up 👍 Is it possible in advanced mode?
  • j

    James

    11/28/2022, 5:00 PM
    No I don't believe so - you would need Pages support directly, and that generally doesn't happen while a product is in beta.
1...313314315...392Latest