socket.io-deno icon indicating copy to clipboard operation
socket.io-deno copied to clipboard

std/http/server.ts>serve is deprecated, io.handler() is incompatible with Deno.serve

Open svdvonde opened this issue 2 years ago • 2 comments

Describe the bug

Deno's std/http/server.ts>serve is deprecated and will be removed in version 1.0.0. Instead they suggest to use Deno.serve.

This library currently does not seem to be compatible with Deno.serve. It throws the following error:

Deno: No overload matches this call. Overload 1 of 3, '(handler: ServeHandler): Server', gave the following error. Argument of type '(req: Request, connInfo: ConnInfo) => Response | Promise<Response>' is not assignable to parameter of type 'ServeHandler'. Overload 2 of 3, '(options: ServeInit & (ServeOptions | ServeTlsOptions)): Server', gave the following error. Argument of type '(req: Request, connInfo: ConnInfo) => Response | Promise<Response>' is not assignable to parameter of type 'ServeInit & (ServeOptions | ServeTlsOptions)'.

To Reproduce

deps.ts

export * as socketio from "https://deno.land/x/[email protected]/mod.ts";

test.ts

import { socketio } from "./deps.ts";
const io = new socketio.Server();
Deno.serve(io.handler());

Expected behavior

Ideally I would expect the socketio handler to be compatible with Deno.serve.

Platform:

MacOS 13.5 (Intel-based) deno 1.35.3 (release, x86_64-apple-darwin) v8 11.6.189.12 typescript 5.1.6

svdvonde avatar Aug 02 '23 14:08 svdvonde

Hi! I was indeed able to reproduce, thanks for reporting the issue :+1:

darrachequesne avatar Aug 02 '23 21:08 darrachequesne

This workaround works for me until it is being fixed

// Use io.handler() directly once it allows using parameter ServeHandlerInfo instead of ConnInfo.
const customIOHandler: Deno.ServeHandler = (
  request: Request,
  info: Deno.ServeHandlerInfo
) => {
  const localAddr: Deno.Addr = {
    transport: "tcp",
    hostname: HOSTNAME,
    port: PORT,
  };

  const connInfo: ConnInfo = {
    remoteAddr: info.remoteAddr,
    localAddr,
  };
  console.log(request)
  return io.handler()(request, connInfo);
};

Deno.serve({ port: PORT, hostname: HOSTNAME }, customIOHandler);

TMarciDev avatar Feb 19 '24 13:02 TMarciDev