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

    IEatBeans

    01/29/2023, 5:13 AM
    or do I just need to restructure my api so all endpoints requirnng auth are inside an
    auth
    folder or smth? Doesnt seem super clean that way though
  • i

    IEatBeans

    01/29/2023, 5:13 AM
    is there anyway to define a list of routes (including the params) that the middleware will apply to?
  • i

    IEatBeans

    01/29/2023, 5:14 AM
    I could just put middleware on root, and just if url matches do stuff else continue, but that will run the worker for eveyr request which is not needed and idk what I would do about params?
  • s

    Skye

    01/29/2023, 10:07 AM
    https://developers.cloudflare.com/workers/learning/using-websockets
  • s

    Skye

    01/29/2023, 10:09 AM
    You can expose an array of handlers in your onRequest code, for example.
    Copy code
    js
    export const onRequestGet = [myMiddlewareHandler, myRouteThatShouldHaveMiddlewareFirst]
    That way, you can explicitly choose which routes it runs on
  • h

    Hazard

    01/29/2023, 10:09 AM
    If I haven't misunderstood it, this appears to explain how to connect to the worker with WebSockets, not connecting to a WebSocket server from the worker.
  • h

    HardAtWork

    01/29/2023, 11:15 AM
    https://developers.cloudflare.com/workers/learning/using-websockets#writing-a-websocket-client
  • h

    Hazard

    01/29/2023, 11:19 AM
    Ah, I completely glanced over that. But so the constructor is supported then. Since when? Cause it could be that I have an old version of wrangler then and that's why it's giving me the error that the constructor is not implemented.
  • h

    HardAtWork

    01/29/2023, 11:20 AM
    Oh, are you using TypeScript?
  • h

    Hazard

    01/29/2023, 11:21 AM
    Yeah, in addition to that, I am using a library (surrealdb.js) that establishes the connection with the WebSocket() constructor.
  • h

    HardAtWork

    01/29/2023, 11:22 AM
    Do you have https://npmjs.com/package/@cloudflare/workers-types added to your
    tsconfig.json
    ?
  • h

    Hazard

    01/29/2023, 11:25 AM
    Yes, I'm getting this error during runtime
    ERROR Error: Failed to construct 'WebSocket': the constructor is not implemented.
  • h

    Hazard

    01/29/2023, 11:25 AM
    I'll try to update to the latest version of wrangler/miniflare and try again
  • k

    kian

    01/29/2023, 11:26 AM
    the constructor is not implemented
    is something thrown by the runtime
  • h

    HardAtWork

    01/29/2023, 11:26 AM
    What's your
    compatibility_date
    in your
    wrangler.toml
    ?
  • h

    HardAtWork

    01/29/2023, 11:27 AM
    WebSocket
    isn't a recent addition, but if your compat date is really old, it might not be there...
  • h

    Hazard

    01/29/2023, 11:27 AM
    I added it today due to the warning, but I pass it in the CLI as
    --compatibility-date=2023-01-29
  • s

    Skye

    01/29/2023, 11:34 AM
    Your code is using
    new WebSocket
    - which is not implemented. The guide you were linked explains how to create websockets in the workers runtime
  • d

    Dani Foldi

    01/29/2023, 3:26 PM
    I thought for a while it has been supported, even miniflare got a PR recently that added support https://github.com/cloudflare/miniflare/pull/466
  • d

    Dani Foldi

    01/29/2023, 3:27 PM
    (for clients, the pair stuff is still needed for letting external clients connect to DOs, for example)
  • k

    kian

    01/29/2023, 3:30 PM
    I can't find any explanation for why the unimplemented error is returned
  • k

    kian

    01/29/2023, 3:31 PM
    the constructor doesn't have
    jsg::unimplemented
    or
    wontimplement
    - but that is what'd throw the error
  • k

    kian

    01/29/2023, 3:31 PM
  • h

    Hazard

    01/29/2023, 4:10 PM
    perhaps thats the default behaviour of v8/js when it's not implemented?
  • k

    kian

    01/29/2023, 4:12 PM
    https://github.com/cloudflare/workerd/blob/bdfb3cde62d5a91b5e5ef0b34b554ed8c3ef4568/src/workerd/jsg/resource.c%2B%2B#L39-L45
  • k

    kian

    01/29/2023, 4:12 PM
    It's thrown from workerd
  • h

    Hazard

    01/29/2023, 4:17 PM
    I see, would it be possible to implement it through the prototype myself? I'm thinking about doing something like this as described in the docs about websockets:
    Copy code
    js
      let resp = await fetch(url, {
        headers: {
          Upgrade: 'websocket',
        },
      });
    
      let ws = resp.webSocket;
      if (!ws) {
        throw new Error("server didn't accept WebSocket");
      }
    
      ws.accept();
  • j

    James

    01/29/2023, 4:18 PM
    The
    WebSocket
    constructor should work fine: https://developers.cloudflare.com/workers/learning/using-websockets/#writing-a-websocket-client If It doesn't, I would recommend filing an issue on the
    wrangler2
    repo
  • j

    James

    01/29/2023, 4:19 PM
    > Cloudflare Workers supports the
    new WebSocket(url)
    constructor.
  • h

    Hazard

    01/29/2023, 4:20 PM
    Yeah I saw it, weird. I wonder why I'm getting this. Let me try to clean things up and try again. Made an issue: https://github.com/cloudflare/wrangler2/issues/2640
1...340341342...392Latest