https://discord.cloudflare.com logo
Join Discord
Powered by
# workers-help
  • How I Prevent DDoS on Workers/Pages
    f

    Fukada

    05/11/2023, 3:49 PM
    My Friend Try Ddos My Pages Cloudflare And I Get Exceeded Limit, How I avoid exceeding the limit caused by the DDoS attack And Protect My Pages From Ddos? Any advice would be appreciated. Thank you.
    l
    i
    +6
    • 9
    • 53
  • Error ReferenceError: Worker is not defined
    u

    urOpinion > /dev/null

    05/12/2023, 12:38 PM
    Hello everyone! I started trying Cloudflare workers and i faced this error: Error ReferenceError: Worker is not defined (context- trying to create an OCR serverless service) code:
    Copy code
    import Tesseract from "tesseract.js";
    const recognizeText = async () => {
      try {
        const { data } = await Tesseract.recognize(
          "https://tesseract.projectnaptha.com/img/eng_bw.png",
          "eng"
        );
        return data.text;
      } catch (err) {
        return "Hello world! " + err;
      }
    };
    export default {
      async fetch(request, env, ctx) {
        const text = await recognizeText();
        return new Response(text, {
          headers: { "content-type": "text/plain" },
        });
      },
    };
    And here is an image of the local dev server. Thanks in advance for any help!

    https://cdn.discordapp.com/attachments/1106561137958453269/1106561138730209290/image.png▾

    z
    d
    • 3
    • 4
  • How to fix this
    r

    redux1997

    05/12/2023, 5:32 PM
    TypeError: ReadableStream.tee() buffer limit exceeded. This error usually occurs when a Request or Response with a large body is cloned, then only one of the clones is read, forcing the Workers runtime to buffer the entire body in memory. To fix this issue, remove unnecessary calls to Request/Response.clone() and ReadableStream.tee(), and always read clones/tees in parallel
    • 1
    • 1
  • how do you debug wrangler dev in vscode ? breakpoints don't bind on ws socket :(
    o

    oooyeee

    05/13/2023, 12:17 PM
    how do you debug wrangler dev in vscode ? breakpoints don't bind on ws socket 😦 also how can i access public url of a worker preview ? i see it requires at least 2 headers: "accept: \*/\*" and "cf-workers-preview-token: asdasdasd...", which i can get using wrangler dev --log-level debug, but maybe there is more practical way of accessing it also ?
    z
    • 2
    • 1
  • Assets not loading when responding with another site
    l

    Leokratis

    05/13/2023, 6:38 PM
    So I have been trying to redirect
    website2.com
    under the`website.com/2` domain and so far I have managed to do that using the code:
    Copy code
    js
    export default {
      async fetch(request) {
        async function MethodNotAllowed(request) {
          return new Response(`Method ${request.method} not allowed.`, {
            status: 405,
            headers: {
              Allow: "GET",
            },
          });
        }
        // Only GET requests work with this proxy.
        if (request.method !== "GET") return MethodNotAllowed(request);
        return fetch(`https://example.com`);
      },
    };
    It works fine but the assets are not loading and I get the plain html version of the website. I have setup the following worker routes: -
    *dreamcafe.eu.org/assets/images*
    -
    *dreamcafe.eu.org/assets*
    -
    https://dreamcafe.eu.org/guides/
    with the
    *dreamcafe.eu.org/assets*
    route it should make the website appear as normal but it doesn't does anyone know why?

    https://cdn.discordapp.com/attachments/1107014041911689277/1107014208769499166/image.png▾

    w
    c
    s
    • 4
    • 96
  • Confused about how worker functions w/ fetch calls inside
    s

    schwartzmj

    05/13/2023, 8:44 PM
    Typing up code...
    • 1
    • 8
  • WebSockets
    m

    mooon

    05/13/2023, 9:46 PM
    Is there a way to maintain multiple connections to multiple websockets without Durable Objects?
  • multipart/form-data fails in test
    s

    scfinan

    05/14/2023, 7:33 PM
    Hi there -- I'm attempting to upload a file in a test to an endpoint with a mutlipart/form-data header. Note that this endpoint DOES work when hit directly via Postman, but I can't seem to get it to work in the test environment Would really appreciate the help Since the test runner is in a node.js environment, I'm using
    form-data
    to construct the payload. The test code below:

    https://cdn.discordapp.com/attachments/1107390359266394135/1107390359383842969/Screenshot_2023-05-14_at_3.31.57_PM.png▾

    • 1
    • 1
  • POST requests with body content stuck in "pending" status
    d

    dkfdkdodkms

    05/15/2023, 12:16 AM
    Copy code
    async fetch(request:Request, env:Env, context:ExecutionContext):Promise<Response> 
        {
            let response = new Response('{}');
            response.headers.set('Access-Control-Allow-Headers', 'Content-Type, X-Json');    
            response.headers.set('Access-Control-Allow-Origin', '*');
            response.headers.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
            response.headers.set('Access-Control-Max-Age', '86400');
            return response;
        }
    The above works fine with requests without a body. When I add a body to the request the request is stuck in 'pending' status. Just adding this
    Copy code
    options.body = body;
    causes the issue.
    Copy code
    Accept:
    */*
    Accept-Encoding:
    gzip, deflate, br
    Accept-Language:
    en-US,en;q=0.9
    Cache-Control:
    no-cache
    Connection:
    keep-alive
    Content-Length:
    1780366
    Content-Type:
    application/octet-stream
    Host:
    127.0.0.1:8081
    Origin:
    http://localhost
    Pragma:
    no-cache
    Referer:
    http://localhost/
    Sec-Ch-Ua:
    "Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"
    Sec-Ch-Ua-Mobile:
    ?0
    Sec-Ch-Ua-Platform:
    "macOS"
    Sec-Fetch-Dest:
    empty
    Sec-Fetch-Mode:
    cors
    Sec-Fetch-Site:
    cross-site
    User-Agent:
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
    • 1
    • 3
  • cloudflare can not open websocket to gateway.discord.gg
    t

    tvargas

    05/15/2023, 6:28 AM
    Gateway WebSocket API sending a 401 rather than upgrading to a WebSocket when opening a connection from a Cloudflare Worker
    s
    • 2
    • 2
  • Too many API requests by single worker invocation
    l

    lasseschou

    05/15/2023, 7:22 AM
    Hi, I'm using a cron-triggered worker that makes from 10 to 20 subrequests. It has worked flawlessly for 2 years, but today I got this message "Too many API requests by single worker invocation". I'm using a paid version, unbound. Any idea how to find out what happened? It's a pretty critical cron job so I need to know what's up. Tx.
    h
    • 2
    • 5
  • In development, you should use a separate kv namespace than the one you'd use in production.
    w

    wilbo007

    05/15/2023, 1:59 PM
    Getting this error when binding a KV namespace. Not sure why this is an error, my script i've made is entirely for playing around with. I suppose it's "production" in a sense but i'm still just playing around with it
    s
    • 2
    • 9
  • is there a limit to the number of secrets/ variables you can have for a worker/pages?
    p

    PeterMbanugo

    05/15/2023, 2:33 PM
    is there a limit to the number of secrets/ variables you can have for a worker? Maybe it's a maximum of 40 entries, and after that you can't add new ones or they get an error?
    z
    • 2
    • 1
  • Workspace with separate billing?
    b

    Bamboo

    05/16/2023, 2:01 AM
    How to manage billing for different websites based on usage? Like how a freelancer can bill his individual clients according to their usage of services? And how to view which services (like workers request, stream, images) a website is using individually like per website basis? Something like workspaces with its own billing and services listed?
    c
    • 2
    • 1
  • Worker custom route redirects back to original *.workers.dev route.
    i

    Irantwomiles

    05/16/2023, 2:45 AM
    I have a worker that I would like to use a custom route instead of the original
    zone.worker.dev
    . I've followed the docs and I have the custom route added, but the custom route just redirects back to the original
    zone.worker.dev
    route. How do I prevent this? Here is my wrangler.toml files:
    Copy code
    workers_dev = false
    route = "sub.customroute.com/*"
    s
    • 2
    • 12
  • Error: Subrequest depth limit exceeded
    b

    BallAche

    05/16/2023, 3:07 AM
    Hi, guys, I got the error when this request recursed through Workers too many times. The message also says: "If you're trying to fetch from an origin server, make sure you've set the upstream option." My question is: where is this upstream option? The fetch api doesn't seem to have this option.
    h
    • 2
    • 1
  • disable cf-connecting-ip
    g

    gookie

    05/16/2023, 11:02 AM
    Hi, I am using cloudflare worker as a proxy to multiple REST API's. However I do not want that the REST API's get the users real IP from the
    cf-connecting-ip
    header. I enabled
    Remove visitor IP headers
    in the transform rules but the REST API servers still got the
    cf-connecting-ip
    header in the request. Then I made a new modify request header rule like attached images shows but the REST API servers still got the
    cf-connecting-ip
    header. What am I doing wrong?

    https://cdn.discordapp.com/attachments/1107986464396025926/1107986464530239559/image.png▾

    a
    e
    • 3
    • 25
  • How to insert data in worker d1 using datbaseID in cloudflare through JS code.
    d

    Danish Waqad

    05/16/2023, 11:39 AM
    I want to develop an JS application to insert data in mongodb tables and same data insert in cloudflare worker D1 Please Help Me. Thanks
  • Access with cname
    z

    zarg129

    05/16/2023, 12:20 PM
    Is it possible to get worker with cname like zone subdomain cname to worker custom domain -> worker custom domain?
  • Multiple workers with `wrangler dev`
    k

    krondor

    05/16/2023, 6:36 PM
    Hi, I am planning on breaking down my different services into separate workers. How do I specify local mount points for
    wrangler dev
    in my
    wrangler.toml
    h
    d
    • 3
    • 9
  • Typings for new connect method
    d

    DJtheRedstoner

    05/16/2023, 8:20 PM
    Is there any typings for the new TCP connect method yet? I'm using
    @cloudflare/workers-types@next
    and I can't seem to find any typings for it. Adding the following to
    types.d.ts
    works:
    Copy code
    ts
    declare module "cloudflare:sockets" {
        export function connect(address: SocketAddress | string, options?: SocketOptions): Socket
    }
    d
    • 2
    • 2
  • Workers request.body empty
    b

    Bamboo

    05/17/2023, 3:48 AM
    I'm running a worker locally whose work is to return the request.body back, and I made a POST request with some body in application/json format, but in response I get {}, but request.cf is working perfectly, and on request.bodyUsed, I get false export default { async fetch(request, env, ctx) { const { pathname } = new URL(request.url); if (pathname === "/api/modify") { return Response.json(request.body); } } }
    k
    • 2
    • 3
  • Rewriting by customers
    a

    Advany

    05/17/2023, 9:28 AM
    Hi there, We want to allow our customers to create rewrite code using our UI and that should be executed before the response is returned. Is there a saver way to execute that user provided code then using eval? They should only be able to rewrite the response body
    h
    s
    • 3
    • 4
  • execute customer code without "workers for platforms"
    m

    Moritz S

    05/17/2023, 12:03 PM
    Hey, we are building a flow builder based on cf infrastructure/workers. On part of the flows should be custom functions the user can write. simple example: "const sum = 1 + 1; return sum;" best practice for sure would be workers for platforms with cf enterprise. However as you can imagine we are a small startup and 3-5k per month would be a very hard bill to swallow. as code evaluation is not allowed in workers, does anyone have an other idea to run custom code for customers (securely) without workers for platforms and without an external server/service? Thankful for every hint and idea! Best Regards, Moritz
    s
    • 2
    • 1
  • I'm getting CORS policy issue with the pages function.
    a

    Ariful

    05/17/2023, 1:07 PM
    Hi, I tried to set Access-Control-Allow-Origin: * in the /functions/_middleware.ts but it still showing the the error, plz check the code below,

    https://cdn.discordapp.com/attachments/1108380350826168320/1108380351358832650/Screenshot_from_2023-05-17_18-57-37.png▾

    https://cdn.discordapp.com/attachments/1108380350826168320/1108380351887327292/Screenshot_from_2023-05-17_19-00-20.png▾

    https://cdn.discordapp.com/attachments/1108380350826168320/1108380352235442276/Screenshot_from_2023-05-17_19-00-36.png▾

    https://cdn.discordapp.com/attachments/1108380350826168320/1108380352541642933/Screenshot_from_2023-05-17_19-01-01.png▾

    n
    • 2
    • 4
  • Confusion Regarding Environments for Workers
    k

    Kynson

    05/17/2023, 1:31 PM
    I have an existing worker already deployed on the default 'production' environment, I would like to create another 'dev' environment for development purpose and only deploy it to workers.dev. Yet, things didn't go quite expected. The new 'dev' worker was deployed with the command
    wrangler publish --env dev
    wrangler.toml:
    Copy code
    name = 'foo'
    account_id = '<ACC ID>'
    main = 'src/index.ts'
    workers_dev = false
    routes = ["foo.com/_api/*", "www.foo.com/_api/*"]
    kv_namespaces = [{ binding = 'CONTENT', id = '<KV ID>', preview_id = '<KV ID>' }]
    compatibility_date = '2023-05-16'
    [env.dev]
    workers_dev = true
    kv_namespaces = [  { binding = 'CONTENT', id = '<KV ID>', preview_id = '<KV ID>' }]
    Issues faced: - A new worker was created with the name 'foo-dev' (where foo is the original name of the 'production' worker), instead of adding a new environment to the existing foo worker - The ''foo-dev' was mapped to all 3 routes i.e. foo.com/_api/\*, www.foo.com/_api/* and foo-dev.foo.workers.dev Confusion: - I was once able to publish a worker with 2 different environments without having a new worker being created. Yet I can't find the same way to do so now. (Please see the screenshot. The 'contact' worker has another environment called dev). I tried the name the 'dev' worker with the same name 'foo' and the original one got overwritten. - As shown on https://developers.cloudflare.com/workers/platform/environments/#staging-environment-with-workersdev I can set
    workers_dev
    to true so it will only deploy to workers.dev. Yet, the all three routes are mapped to the new dev worker. I am aware that
    routes
    will be inherited so this might be an expected behaviour, but the documentation seem to be misleading (or maybe I got confused). May I know if there is a way to only deploy my worker to workers.dev for a specific environment. Thank you for your help!

    https://cdn.discordapp.com/attachments/1108386385536229526/1108386386274430976/Screenshot_2023-05-17_at_21.22.27.png▾

    i
    • 2
    • 2
  • Workers code didn't loading
    a

    Azkars

    05/17/2023, 3:13 PM
    Hello. So, I want go to my code, but it didn't load. I tried few times, on normal Chrome and on incognito mode, but still nothing. Before this error, I had a look for code with VSC (I open my code, but IDE wasn't normal for Cloudflare, that's VSCode). https://cdn.discordapp.com/attachments/1108411996765954070/1108411998057807984/2023-05-17_16-40-57.mp4
    c
    n
    +4
    • 7
    • 71
  • Workers are _only_ serving TLS 1.3 right now even tho min TLS version 1.2 in Edge Certificates
    g

    gatsbylabs

    05/17/2023, 7:52 PM
    The minimum TLS version defined in Edge Certificates for the domain shows 1.2. But when running TLS tests again the domain / answering worker it only serves 1.3 connections. Any advice?
    Copy code
    ➜  curl https://example.com -svo /dev/null --tls-max 1.2
    *   Trying 172.67.75.158:443...
    * Connected to gatsby.events (172.67.75.158) port 443 (#0)
    * ALPN, offering h2
    * ALPN, offering http/1.1
    * successfully set certificate verify locations:
    *  CAfile: /etc/ssl/cert.pem
    *  CApath: none
    * (304) (OUT), TLS handshake, Client hello (1):
    } [225 bytes data]
    * error:1404B410:SSL routines:ST_CONNECT:sslv3 alert handshake failure
    * Closing connection 0

    https://cdn.discordapp.com/attachments/1108482207846252574/1108482208005628055/Screen_Shot_2023-05-17_at_12.48.13_PM.png▾

  • What CF technology should I be using?
    z

    Zac

    05/17/2023, 11:48 PM
    Hey there, am trying to work out if I can use Cloudflare for a new product - wherein and it pushes some basic data up to Basically, 30 times per second, pushing up 6k 3 digit int - that clients (db readers) will need basically real-time data. Kind of notably, the sender/receivers will very likely be talking to the same Cloudflare data center too
    • 1
    • 1
  • Interacting with the Cloudflare API from a Worker
    g

    GTP_Jordan

    05/18/2023, 5:00 AM
    I am trying to interact with the Cloudflare API from within a Worker. I want to be able to update a Zero Trust Gateway Rule per the API docs here: https://developers.cloudflare.com/api/operations/zero-trust-gateway-rules-update-zero-trust-gateway-rule The code I am using to interact with the API looks like this:
    Copy code
    const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/gateway/rules/${POLICY_ID}`;
    
    const response = await fetch(url, {
      method: 'PUT',
      headers: {
        'X-Auth-Email': API_EMAIL,
        'Authorization': `Bearer ${API_TOKEN}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        action: "block",
        enabled: false,
      })
    });
    This code is intended to disable the rule. But when I run it I just get an error:
    Copy code
    {
      "result": null,
      "success": false,
      "errors": [
        {
          "message": "must include at least one condition or expression"
        }
      ],
      "messages": []
    }
    I don't understand what "must include at least one condition or expression" means in this context. I also don't understand what the
    action
    string is supposed to do or how it should be used correctly. If anyone could help me understand this I would really appreciate it. Thanks!
1...456...10Latest