node_shims icon indicating copy to clipboard operation
node_shims copied to clipboard

`Deno.listen` shim: `server._handle` is null

Open tjjfvi opened this issue 1 year ago • 1 comments

import { Deno } from "@deno/shim-deno"

Deno.listen({ port: 8000 })
.../node_modules/@deno/shim-deno/dist/deno/stable/functions/listen.js:48
    const listener = new Listener_js_1.Listener(server._handle.fd, {
                                                               ^
TypeError: Cannot read properties of null (reading 'fd')
    at Object.listen (.../node_modules/@deno/shim-deno/dist/deno/stable/functions/listen.js:48:64)
    at .../test.ts:4:6
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
$ node -v
v18.14.0

https://github.com/denoland/node_shims/blob/main/packages/shim-deno/src/deno/stable/functions/listen.ts#L58-L59

tjjfvi avatar Apr 10 '23 13:04 tjjfvi

Looking at the code, it makes sense that this doesn't work. As the comment says:

// server._handle.fd is assigned immediately on .listen()

But we try to access server._handle.fd before the waitFor promise is called, so it can't be assigned yet.

It also looks like even if server._handle.fd was accessible, Listener is not assigning it to its own rid property: https://github.com/denoland/node_shims/blob/main/packages/shim-deno/src/deno/internal/Listener.ts#L6 (I just didn't understand this, sorry)

So it seems that server.listen needs to be reconstructed in some way to allow for the RID being the result of a promise.

sgwilym avatar May 10 '23 12:05 sgwilym