https://discord.cloudflare.com logo
Join Discord
Powered by
# workers-help
  • require("url") resolves to null
    m

    mattmccabe

    05/02/2023, 10:36 PM
    I have an npm dependency in my worker that has a dependency on the "url" lib/module that is built into the nodejs runtime. Excerpt:
    Copy code
    const url_1 = require("url");
    ...
    ...
    ...
    const url = new url_1.URL(input);//throws undefined
    When I run wrangler dev I receive an undefined error I am guessing because the worker env doesn't know how to resolve the require("url") which is built into the nodejs runtime environment. Is there a way to bundle some kind of replacement for nodejs's url lib/module in order to get the require("url") to resolve correctly inside of the worker runtime. I was looking at the Configuration > Bundling documentation in the Wrangler docs but no lightbulbs have gone off on how to leverage it to provide a solution. Any experience anyone can share would be greatly appreciated!
    k
    • 2
    • 13
  • Running one time scripts in context of the configured worker
    t

    torte

    05/03/2023, 11:13 AM
    I would like to run a script on my local machine which would hook into the context of my worker which is already configured on Cloudflare and then perform any operation in that context, e.g. using D1, KV directly without having to actually run a worker with a fetch handler and then do a an HTTP request to achieve essentially the same. You could think about it as simply running the
    node
    command with a file in your project which has access to the whole context of the project (e.g. databases, env variables etc). A similar approach from other language/frameworks are Django management commands. The way I imagine this is simply running
    wrangler dev
    and instead of launching a worker service (which essentially runs as an HTTP service) it would execute a worker "script" which runs once and then exits. Is this possible in workers and if so how?
    s
    • 2
    • 7
  • FetchError [ERR_NO_HANDLER]
    s

    Shami

    05/03/2023, 1:15 PM
    Hi everyone. I use Remix, CF Workers, and PlanetScale. When I connect a sample db it works. However, when I connect the original db, the error appears: "No fetch handler defined and no upstream to proxy to specified. Make sure you're calling addEventListener("fetch", ...)." How to fix this?
    e
    s
    • 3
    • 2
  • Rust worker, cannot build default project
    b

    bret_pat

    05/03/2023, 3:16 PM
    New project, ran
    Copy code
    pnpm create cloudflare workers worker-rust
    and I'm trying to run
    Copy code
    pnpm wrangler dev
    but I get an error
    Copy code
    Running custom build: cargo install -q worker-build && worker-build --release
    error[E0658]: use of unstable library feature 'bool_to_option'
    I'll add my toml to a separate post
    • 1
    • 2
  • Getting error stats for workers
    t

    tabletoe

    05/04/2023, 9:36 AM
    Hi all - I'm hitting the analytics api graphql endpoint (https://api.cloudflare.com/client/v4/graphql/) to get metric information on my workers scripts which are running. I haven't yet found a way to get information about the error rates, specifically number of 4XX vs 5XX (and 3XXs would be ideal, too). Is this possible with this api? If not, are there any api calls which I can use which can get me this information? Cheers
    k
    • 2
    • 2
  • requests just say pending
    j

    Jup

    05/04/2023, 9:32 PM
    using the workers playground testing site to just make a simple http request to a GQL API, but the request just stays stuck on pending, works with other websites, but this call just hangs and i have no idea why.
    • 1
    • 1
  • Horseman CMS not working
    a

    Ariful

    05/04/2023, 10:40 PM
    HI, I've setup 2 worker according to docs, But it's not working. plz check this https://github.com/AggressivelyMeows/Horseman/discussions/8
    w
    c
    • 3
    • 10
  • logpush and pages
    h

    happysalada

    05/05/2023, 11:21 AM
    Hi, first of all thank you for the great products , pages is just amazing ! Ive been trying to figure out how to enable logs on pages for some time and can't figure it. I'm using svelte kit with cloudflare adapter, and deploying onto cloudflare pages. I would like to store logs somehwre and I have read about logpush. Ive created a logpush job and connected it to R2, how do I connect what I deployed on pages to that job ? Is there another way to store the logs for later viewing for pages ?
  • Durable Object websocket 1006
    g

    G4G4N

    05/05/2023, 10:18 PM
    I keep getting 1006 errors in my websocket connections on the frontend. what is a good way to resolve this? Extending timeout?
  • worker redirection is not working from main domain
    h

    hannatawk

    05/06/2023, 12:01 PM
    i want to make a redirection for a user if he is in a specific country, and the redirection is being triggered if i enter from the cloudflare generated url for testing in the worker but if i enter the website directly the worker is not being triggered, please any help would be appreciated
    z
    • 2
    • 1
  • how can a worker script retrieve its current origin?
    b

    bever1337

    05/07/2023, 1:04 AM
    Howdy! What is the best way for a worker script to know its current origin? The use-case is a router library that can match request objects. In a service worker environment, that worker may intercept multiple origins, so SW routers are "origin aware." I want to re-use that library in a CF Worker context. The
    WorkerGlobalScope
    has a
    location
    property that seems unavailable (at least on local workers.). Thanks for your help!
    k
    • 2
    • 5
  • How to build with experimentalDecorators in workers
    r

    redux1997

    05/07/2023, 12:20 PM
    As workers is using esbuild to compile typesript code but its skips experimentalDecorators functionalities.Is there any solution for this?
    h
    • 2
    • 1
  • Worker is bypassed if query parameter is present
    t

    turikhay

    05/07/2023, 10:11 PM
    Hi, I noticed that index page (
    /
    ) requests that contain query parameters (
    ?anything
    ) are served by the origin instead of one of workers. I'm not sure why this happens. I would like all requests to
    /
    to be served by CF. I've set up the example to illustrate this:
    Copy code
    $ curl https://test.0x0f.ru/
    served from a worker
    Copy code
    $ curl https://test.0x0f.ru/?a=b
    served by the origin
    j
    w
    • 3
    • 7
  • Problem with worker function and caching
    i

    icess

    05/08/2023, 2:00 PM
    I have recently setup a Cloudflare worker with a custom domain. I am trying to redirect users to specific domains based on the current path (domain/code),then by searching for that code in Upstash Redis. The issue I am facing is that even after updating the Redis key’s value, I keep getting redirected to the previously set value. I believe this is due to caching, but I am unable to figure out how to disable or bypass the caching. My worker function is: export default { async fetch(request, env) { return await handleRequest(request) } } async function handleRequest(request) { const url = new URL(request.url) const path = url.pathname const code = path.substring(1) const redirect_url = await get_url(code) return Response.redirect(redirect_url, 301) } async function get_url(code){ const response = await fetch(
    https://upstash_redis.url
    , { headers: { Authorization: "Bearer Token" } }) const data = await response.json() console.log(data) if(data.result != null ){ return data.result } else { return "https://www.example_deafult.com" } }
    j
    • 2
    • 2
  • WebSockets "fan out" service
    n

    nicksrandall

    05/08/2023, 7:08 PM
    I'd like to build a simple "fan out" service and wonder if this is conceptually possible using Worker's support for WebSockets. I'd like the service to have an HTTP endpoint that an external system can post to when an event is created and then the worker would relay this event to all connected clients via WebSockets. Is such a use-case feasible using workers? I've read most of the docs on Workers integration with WebSockets but it's still unclear to me how all the pieces fit together. Would I need a durable object for this use case? Thanks in advance for any and all help!
    s
    • 2
    • 3
  • Getting mta-sts worker to work
    p

    PrizewizE

    05/09/2023, 9:38 AM
    Im trying to get the MTA-STS worker to work from https://365labs.cloud/blog/mta-sts-using-cloudflare-workers but whenever I follow the steps my worker will show; xxxxx.workers.dev is not defined in the mta-sts worker And the URL for my own domain https://mta-sts.xxx.well-known/mta-sts.txt Will show nothing, does anyone know how to solve this ?
  • Workers billing questions
    m

    mykola

    05/09/2023, 9:51 AM
    Hi guys, I have this data in my statistic in workers page: Unbound requests 9.2k Billable duration 277 GB-sec Bundled requests 221.18M Durable Objects storage 0 B However, In my billing panel, I see Cloudflare Workers request: Cumulative total this month: 11k requests What does it mean? Why 221.18M of bundled requests doesn’t count in the billing panel?

    https://cdn.discordapp.com/attachments/1105431886530084934/1105431886664314960/part2.JPG▾

    https://cdn.discordapp.com/attachments/1105431886530084934/1105431886962102392/part1.JPG▾

    u
    • 2
    • 3
  • 302 Redirect headers are missing
    k

    Krull

    05/09/2023, 3:00 PM
    Is it possible to extract the original headers from a worker fetch request to an end-point which returns a 302? e.g. const res = await fetch("http://localhost:3000/", { "redirect": "manual" }) res = {"body":{},"bodyUsed":false,"headers":{},"url":"http://localhost:3000/","redirected":false,"ok":false,"statusText":"Found","status":302} If I run the same request via curl I get the original headers (e.g. location + set-cookie): curl -v http://localhost:3000/ < HTTP/2 302 < cache-control: private < location: https://www.google.com < set-cookie: SIGNIN.REQUEST=1683699853; path=/; secure; HttpOnly < access-control-expose-headers: Request-Context < date: Tue, 09 May 2023 14:49:59 GMT < content-length: 0 Is it possible to extract the original headers using Cloudflare workers (or any other Cloudflare service)? The main problem is that the set-cookie header is lost.
    j
    • 2
    • 12
  • Uncaught (in promise) error
    s

    Silabear

    05/09/2023, 3:26 PM
    Hey, I'm tryna make a file uploading system. My full code is https://pastebin.com/dUCApxp4, but this should be the important part:```ts export default { async fetch(request, env) { const url = new URL(request.url); const key = url.pathname.slice(1); const { pathname } = new URL(request.url); if(pathname == "/") { return new Response("CDN is working! :D") } switch (request.method) { case 'PUT': if(request.headers["Authorization"] != "nop!") return "no, go away" await env.data.put(key, request.body, { customMetadata: Record({"uploader":request.headers["Author"]}) }); return "helo mate" ...``` However, when I sent a
    PUT
    request with the valid headers and data, it returns 500, and the logs show
    Uncaught (in promise) TypeError: Incorrect type for Promise: the Promise did not resolve to 'Response'.
    . I've been trying to fix this for a very long time, so any help would be appreciated!
    k
    d
    • 3
    • 18
  • WebSocket client getting 401 only when deployed
    g

    Gradyent

    05/09/2023, 4:30 PM
    Has anyone had trouble with differences in how WebSocket clients work when running locally with `wrangler dev --local `vs plain
    wrangler dev
    or when deployed? I am building a WebSocket client for Discord's Gateway API using DOs. Everything works when running locally with
    --local
    , but when I remove
    --local
    or when I deploy to CF, my WebSocket client (created via
    new WebSocket()
    ) immediately gets an error event that says
    Failed to establish the WebSocket connection: expected server to reply with HTTP status code 101 (switching protocols), but received 401 instead.
    . Discord's Gateway WS protocol doesn't require any authorization to open the WS connection normally (a token gets passed in a message later), and this exact same code works with
    wrangler dev --local
    . Could there be some difference in how
    new WebSocket()
    when deployed to Cloudflare vs when running locally?
  • Email routing help
    a

    ajgeiss0702

    05/09/2023, 9:37 PM
    Hello, I'm trying to create a worker that will recieve forwarded emails, extract some info from them, and contact my api using that info. I already have the code written to do that, however I am confused on how to get cloudflare to accept those emails and point them to the worker. The documentation for email workers seems to be essentially non-existent. If anyone has any link to something I just couldn't find, please send that. The "Using email triggers" button on the dashboard just goes to a page that talk about normal http routes (). "mail" is not mentioned a single time on that page. How can I configure it? Do I just put an email in?
    c
    c
    • 3
    • 11
  • Cloudflare zero trust and workers
    f

    faldor20

    05/10/2023, 1:01 AM
    I have a cloudflare worker i would like to use to fetch from a cloudflare page that is behind cloudflare access/zero trust ACCESS->WORKER->ACCESS->PAGE I need the worker to be able to authenticate with cloudlflare access. I have tried using client id and secret, but because the workers are short lived they must constantly reauthenticate which is very slow and breaks some requests. Is there any other way for me to avoid this authentication redirect behaviour? PS: I need this because I'm using a worker to route traffic from a subroute of our main domain to a cloudflare page. Essentially we're serving many cloudflare pages under the same domain under subroutes
    • 1
    • 1
  • Can't seem to get multipart/form-data working with Hono and Workers. The socket is interrupted
    s

    scfinan

    05/10/2023, 3:28 AM
    Copy code
    throw new Error('Multipart: Boundary not found');
                ^
    Unhandled Promise Rejection: Error: Multipart: Boundary not found
    Was missing this error
    • 1
    • 1
  • I am unable to bind a worker to another worker in local development. env.BINDING is always undefined
    j

    jasonkuhrt

    05/10/2023, 4:02 PM
    I am following these instructions but it is not working: https://developers.cloudflare.com/workers/platform/bindings/about-service-bindings/#wranglers
    h
    • 2
    • 22
  • Connecting to CockroachDB serverless using pg via workers
    b

    Bamboo

    05/11/2023, 6:55 AM
    1. Created a npx wrangler init . 2. Then "npm install pg" and added the query code into index.js 3. then, npx wrangler dev, got compatibility errors, and got suggestions to add "node_compat = true", and I did, those errors disappeared 4. Then again ran "npx wrangler dev", got error ' **could not resolve "pg-native**" '. And this is where I'm struck. I ran same code in normal js and it worked, but I don't know why is it asking "pg-native", and also I tried to install it locally and globally, and it won't get installed. 5. What to do?
    s
    • 2
    • 5
  • Where can I find TypeScript type definitions of `@cloudflare/workers-types` pkg in Github?
    y

    ysmdev

    05/11/2023, 7:01 AM
    Where can I find TypeScript type definitions of
    @cloudflare/workers-types
    pkg in Github?
    h
    • 2
    • 3
  • ReadableStream cancel callback is not executing when stream is cancelled
    r

    redux1997

    05/11/2023, 11:24 AM
    return new ReadableStream({ start(controller) { } , cancel() { } }) Cancel is not called however its working on nodejs , deno and browser .Why workers are so inconsistent with others.
  • Can you transform JSON async (like you can with HTMLRewriter)?
    k

    Krull

    05/11/2023, 1:21 PM
    I have a lot of JSON data that I need to transform to a common format. Is there anything like HTMLRewriter, but for JSON? The JSON payload is getting too big to be stored in mem in my worker
    h
    • 2
    • 8
  • In order to use Durable Objects, you must agree to pricing
    m

    morris

    05/11/2023, 2:24 PM
    I've set up the pay-as-you-go plan, updated the
    workers-chat-demo
    account_id, and run
    wrangler login
    . Running
    wrangler publish
    gives me an error: > In order to use Durable Objects, you must agree to pricing at > https://dash.cloudflare.com/aac1a529c1da5d1cbd4c1c1cb12f9675/workers/overview?enable-durable-objects This link sends me to the workers overview page, with no CTA to enable durable objects. Going to the docs, https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#durable-objects > ... Navigate to “Workers” and opt in to pricing. I expected that I should be able to set up my pay-as-you-go plan and re-login via wrangler but it's not working. What can I try next?
  • Worker A binding to Worker B leads to `403` error upon runtime fetch call. Why?
    j

    jasonkuhrt

    05/11/2023, 3:44 PM
    I have two works, A & B, A binds to B. When A does the
    env.BINDING.fetch(...)
    call it leads to a 403 response. That response is NOT coming from our code. Why is CF doing this? The body response is: error code: 1003
    • 1
    • 2
12345...10Latest