denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.87k stars 5.39k forks source link

node: add dgram.Socket APIs #18324

Open kt3k opened 2 years ago

kt3k commented 2 years ago

dgram.Socket is needed to run multicast-dns in compat mode. (feature request from community)

cmorten commented 2 years ago

Will take a look if no-one is already on this?

cmorten commented 2 years ago

TODO:

bukhalo commented 1 year ago

I'm sorely missing the implementation of the rest of dgram's methods, someone please!

bukhalo commented 1 year ago

I looked at the source code and I see that the methods in the list above, have already been released. But I still can't use the libraries that depend on dgram.

Here is for an example code that works in Node but doesn't work in Deno:

import dgram from "node:dgram";

const socket = dgram.createSocket({
  type: "udp4",
  reuseAddr: true,
});

socket.bind(5353)

socket.on("error", (err) => {
  console.error(err);
});

When running in Deno, I get this error:

Error: bind EADDRINUSE 0.0.0.0:5353
    at __node_internal_captureLargerStackTrace (ext:deno_node/internal/errors.ts:91:9)
    at __node_internal_exceptionWithHostPort (ext:deno_node/internal/errors.ts:215:10)
    at node:dgram:273:20
    at Array.processTicksAndRejections (ext:deno_node/_next_tick.ts:36:15)
    at eventLoopTick (ext:core/01_core.js:180:29) {
  errno: -48,
  code: "EADDRINUSE",
  syscall: "bind",
  address: "0.0.0.0",
  port: 5353
}

@cmorten maybe you can help me figure out the problem?

cmorten commented 1 year ago

Hi @hey-ab 👋

Trying to refresh mem as this was a while ago! From what I can see the TODO list I posted still stands - much of the dgram module internals is yet to be implemented (requires porting C code to Deno APIs).

R.E. your specific issue, I think it’s most likely due to the socket address reuse capability never being implemented, see https://github.com/denoland/deno/blob/main/ext/node/polyfills/internal_binding/udp_wrap.ts#L322

bukhalo commented 1 year ago

Hmm... That's sad, unfortunately I've never written in Rust and won't be able to help out. 😢 Am I correct in assuming that my PR is still valid in this case? https://github.com/denoland/deno-docs/pull/150

cmorten commented 1 year ago

Hmm... That's sad, unfortunately I've never written in Rust and won't be able to help out. 😢 ~Am I correct in assuming that my PR is still valid in this case? denoland/deno-docs#150~

When I say Deno APIs I meant JS/TS using the Deno.* namespace (e.g. https://deno.land/api@v1.37.0?unstable=&s=Deno.listenDatagram) - it might be that Rust isn’t needed here, often it’s hooking up the appropriate calls. Worth doing the analysis to see if possible or if there’s a feature gap on APIs or their options.

That said if there’s functionality that is required which Deno doesn’t support then yes, some Rust needed.

If this is documented that the UDP/dgram layer is fully supported then yes would agree that is misleading.